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

@ -13,14 +13,15 @@
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use lutra_control::Backend;
use lutra_control::{Backend, EventSink, Subscribers};
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
use serde_json::Value;
/// A control [`Backend`] over a bare engine, without a window.
///
/// Effects are recorded instead of applied; screenshots write a tiny marker
/// file so that tests can check the path handling.
/// Effects are recorded instead of applied; events are published to
/// subscribers like the desktop host does; screenshots record the path and
/// fail, because there is no window.
pub struct EngineBackend {
/// The engine, behind a lock like in the desktop app.
pub engine: Mutex<Engine>,
@ -28,6 +29,8 @@ pub struct EngineBackend {
pub effects: Mutex<Vec<Effect>>,
/// Paths passed to [`Backend::screenshot`].
pub screenshots: Mutex<Vec<PathBuf>>,
/// Event subscribers.
pub subscribers: Subscribers,
}
impl EngineBackend {
@ -37,8 +40,16 @@ impl EngineBackend {
engine: Mutex::new(Engine::new().unwrap()),
effects: Mutex::new(Vec::new()),
screenshots: Mutex::new(Vec::new()),
subscribers: Subscribers::default(),
}
}
/// Applies pending changes on disk and publishes the resulting events,
/// as the desktop host's pump thread does.
pub fn pump(&self) {
let events = self.engine.lock().unwrap().pump_vfs().unwrap();
self.subscribers.publish(&events);
}
}
impl Default for EngineBackend {
@ -51,6 +62,7 @@ impl Backend for EngineBackend {
fn execute(&self, command: &str, params: Value) -> Result<Value, EngineError> {
let out = self.engine.lock().unwrap().execute(command, params)?;
self.effects.lock().unwrap().extend(out.effects);
self.subscribers.publish(&out.events);
Ok(out.value)
}
@ -62,4 +74,8 @@ impl Backend for EngineBackend {
self.screenshots.lock().unwrap().push(path.to_path_buf());
Err("no window in tests".into())
}
fn subscribe(&self, sink: EventSink) {
self.subscribers.add(sink);
}
}