M3.5: pending outcomes for commands that wait for the language server

Step 1 of M3.5: the shared mechanism. M3.4 and M3.5 build on it.

- Engine: `Outcome.pending: Option<Pending>`. `Pending::new(rx, timeout,
  finish)`; `Pending::wait()` is called without the engine lock and
  returns at the answer, a dropped sender or the deadline (timeout + 1 s).
  `Engine::resolve(answered)` then runs `finish(state, reply)` inside the
  guard, under the lock. `run_waiting(lock, cmd, params, publish)` does
  all three steps for hosts.
- Hosts: `Host::run`, `testkit::EngineBackend` and the control test
  backend use `run_waiting`.
- New IPC `engine_execute_waiting` (`#[tauri::command(async)]`, off the
  main thread) with `runWaiting()` in `ui/src/ipc.ts`. Only `lsp.*`
  requests use it.

Ordering guarantee:
- `engine_execute` stays synchronous on the main thread, so ordinary
  commands (`buffer.edit`) still run in call order.
- The order towards the server is fixed under the engine lock:
  `buffer.edit` queues its didChange on the session's FIFO queue before
  it returns, and `lsp::submit` queues the request on the same queue.
  A request therefore sees every edit whose `buffer.edit` returned
  first, and the UI awaits the sync's `idle()` before sending one.

Failures:
- A stall is caught by the request timeout, with the deadline as
  backstop.
- On restart, close, quit or crash the request is dropped or resolved
  as stopped, so the waiter gets an error, never a hang.

Tests: `crates/engine/tests/pending.rs` (answer, lock free while
waiting, dropped sender, deadline, resolve, lsp.restart while a
request is pending) and Vitest for `runWaiting`. Gate: 13 steps,
321 s, Vitest 443 + 1 skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-22 06:36:20 +02:00
parent a674040797
commit 20d1d28c32
11 changed files with 603 additions and 18 deletions

View file

@ -130,17 +130,20 @@ impl Host {
}
}
/// Runs a command, publishes its events and applies its effects.
/// Runs a command, publishes its events and applies its effects. A
/// command that waits for the language server is waited for without the
/// engine lock ([`lutra_engine::run_waiting`]), so the pump and other
/// commands keep running meanwhile.
///
/// # Errors
/// The engine's error for the command.
pub fn run(&self, command: &str, params: Value) -> Result<Value, EngineError> {
let outcome = {
let mut engine = self.engine();
let outcome = engine.execute(command, params)?;
self.publish(&outcome.events);
outcome
};
let outcome = lutra_engine::run_waiting(
|| self.engine(),
command,
params,
|events| self.publish(events),
)?;
for effect in outcome.effects {
self.apply(effect);
}

View file

@ -77,6 +77,7 @@ fn run(args: &args::Args) -> Result<(), String> {
.plugin(tauri_plugin_dialog::init())
.invoke_handler(tauri::generate_handler![
engine_execute,
engine_execute_waiting,
engine_list_commands,
engine_ready
])
@ -206,6 +207,20 @@ fn engine_execute(
host.run(&command, params).map_err(|e| e.payload())
}
/// IPC: runs an engine command that may wait for the language server
/// (`lsp.*`). `async` makes Tauri run it off the main thread, so the window
/// stays responsive while it waits (the wait itself never holds the engine
/// lock, see [`Host::run`]). Unlike [`engine_execute`], calls are not
/// ordered with other commands; the frontend uses it only for requests that
/// follow the edits they depend on.
#[tauri::command(async)]
fn engine_execute_waiting(
host: tauri::State<'_, Arc<Host>>,
command: String,
params: Value,
) -> Result<Value, ErrorPayload> {
host.run(&command, params).map_err(|e| e.payload())
}
/// IPC: lists the engine's commands for the frontend.
#[tauri::command]
fn engine_list_commands(host: tauri::State<'_, Arc<Host>>) -> Vec<CommandInfo> {