M1.3: engine workspace, file and buffer commands, event stream

State holds the open project (VFS project, watcher, watched directories)
and versioned buffers. New commands: project.open/close/children,
file.open/save/saveAll, buffer.get/edit/close. buffer.edit takes UTF-16
change sets based on a version and fails with the new stale_version code;
closing dirty buffers or projects without force fails with
unsaved_changes. Commands queue events (projectOpened, treeChanged,
bufferChanged, dirtyChanged, fileChangedOnDisk, ...) that the engine
returns in Outcome.events; Engine::pump_vfs maps changes on disk to
events and reloads clean buffers. project.children watches listed
directories, capped at 256 native / 32 polling, least recently listed
dropped first.

Control channel: events.subscribe streams {"event": ...} lines, with
bounded per-subscriber queues (lutra_control::Subscribers) and method docs
in methods.rs. testkit's EngineBackend and the desktop Host implement the
new Backend::subscribe. Verified with 14 engine integration tests, a TCP
events test and the running app over the control channel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-14 20:24:44 +02:00
parent a63ab4ac45
commit f1976c7d67
30 changed files with 2235 additions and 46 deletions

View file

@ -4,7 +4,7 @@
use std::path::Path;
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
use lutra_control::Backend;
use lutra_control::{Backend, EventSink, Subscribers};
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
use serde_json::Value;
use tauri::AppHandle;
@ -24,6 +24,9 @@ pub struct Host {
engine: Mutex<Engine>,
/// The Tauri app, set once the app is set up.
app: OnceLock<AppHandle>,
/// Control channel connections subscribed to engine events. Forwarding
/// events to the frontend and pumping changes on disk arrive with M1.4.
subscribers: Subscribers,
}
impl Host {
@ -35,6 +38,7 @@ impl Host {
Ok(Self {
engine: Mutex::new(Engine::new()?),
app: OnceLock::new(),
subscribers: Subscribers::default(),
})
}
@ -49,12 +53,13 @@ impl Host {
self.engine.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Runs a command and applies its effects.
/// Runs a command, applies its effects and publishes its events.
///
/// # Errors
/// The engine's error for the command.
pub fn run(&self, command: &str, params: Value) -> Result<Value, EngineError> {
let outcome = self.engine().execute(command, params)?;
self.subscribers.publish(&outcome.events);
for effect in outcome.effects {
self.apply(effect);
}
@ -89,4 +94,8 @@ impl Backend for Host {
let app = self.app.get().ok_or("the window is not open yet")?;
screenshot::capture_window(app, MAIN_WINDOW, path)
}
fn subscribe(&self, sink: EventSink) {
self.subscribers.add(sink);
}
}