diff --git a/Cargo.lock b/Cargo.lock index a8e9438a7..1b3e8f5f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13617,6 +13617,7 @@ dependencies = [ "zeron-mcp", "zeron-notify", "zeron-rpc", + "zeron-sync", "zeron-ui", "zeron-update", ] @@ -13812,8 +13813,12 @@ dependencies = [ "anyhow", "async-trait", "base64 0.22.1", + "bytes", "chrono", "futures", + "http-body-util", + "hyper", + "hyper-util", "serde", "serde_json", "tempfile", diff --git a/apps/zeron/Cargo.toml b/apps/zeron/Cargo.toml index 2fda76d93..036c03949 100644 --- a/apps/zeron/Cargo.toml +++ b/apps/zeron/Cargo.toml @@ -27,6 +27,7 @@ zeron-update.workspace = true zeron-cli.workspace = true zeron-mcp.workspace = true zeron-notify.workspace = true +zeron-sync.workspace = true tokio.workspace = true clap.workspace = true anyhow.workspace = true diff --git a/apps/zeron/src/background.rs b/apps/zeron/src/background.rs index b5626acb9..fb4cfb71b 100644 --- a/apps/zeron/src/background.rs +++ b/apps/zeron/src/background.rs @@ -55,6 +55,9 @@ fn run_engine(config: zeron_engine::EngineConfig) -> anyhow::Result<()> { /// are posted on and delivers their clicks. #[cfg(target_os = "macos")] fn run_engine(config: zeron_engine::EngineConfig) -> anyhow::Result<()> { + // Before the engine starts: its sockets then never start the polling + // wake detector (`zeron_sync::wake`). + main_loop::watch_power(); let worker = std::thread::Builder::new() .name("engine".into()) .spawn(move || { @@ -75,9 +78,26 @@ fn run_engine(config: zeron_engine::EngineConfig) -> anyhow::Result<()> { #[cfg(target_os = "macos")] mod main_loop { use std::ffi::c_void; - use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU32, Ordering}; static DONE: AtomicBool = AtomicBool::new(false); + /// The source [`finish`] signals; null until [`run`] makes it. + static STOP: AtomicPtr = AtomicPtr::new(std::ptr::null_mut()); + + /// `CFRunLoopSourceContext`, version 0: only `perform` is used. + #[repr(C)] + struct SourceContext { + version: isize, + info: *mut c_void, + retain: *const c_void, + release: *const c_void, + copy_description: *const c_void, + equal: *const c_void, + hash: *const c_void, + schedule: *const c_void, + cancel: *const c_void, + perform: extern "C" fn(*mut c_void), + } #[link(name = "CoreFoundation", kind = "framework")] unsafe extern "C" { @@ -86,39 +106,100 @@ mod main_loop { fn CFRunLoopGetMain() -> *mut c_void; fn CFRunLoopRunInMode(mode: *const c_void, seconds: f64, return_after_source: u8) -> i32; fn CFRunLoopWakeUp(run_loop: *mut c_void); - fn CFAbsoluteTimeGetCurrent() -> f64; - fn CFRunLoopTimerCreate( + fn CFRunLoopSourceCreate( allocator: *const c_void, - fire_date: f64, - interval: f64, - flags: u64, order: isize, - callout: extern "C" fn(*mut c_void, *mut c_void), - context: *mut c_void, + context: *mut SourceContext, ) -> *mut c_void; - fn CFRunLoopAddTimer(run_loop: *mut c_void, timer: *mut c_void, mode: *const c_void); + fn CFRunLoopAddSource(run_loop: *mut c_void, source: *mut c_void, mode: *const c_void); + fn CFRunLoopSourceSignal(source: *mut c_void); } - extern "C" fn tick(_timer: *mut c_void, _info: *mut c_void) {} + #[link(name = "IOKit", kind = "framework")] + unsafe extern "C" { + fn IORegisterForSystemPower( + refcon: *mut c_void, + port: *mut *mut c_void, + callback: extern "C" fn(*mut c_void, u32, u32, *mut c_void), + notifier: *mut u32, + ) -> u32; + fn IONotificationPortGetRunLoopSource(port: *mut c_void) -> *mut c_void; + fn IOAllowPowerChange(kernel_port: u32, notification_id: isize) -> i32; + } - /// Serve the main run loop until [`finish`]. A repeating timer keeps the - /// loop from returning at once for want of sources and bounds how late - /// the exit check runs. + // IOKit's power messages (IOKit/IOMessage.h). + const CAN_SYSTEM_SLEEP: u32 = 0xe000_0270; + const SYSTEM_WILL_SLEEP: u32 = 0xe000_0280; + const SYSTEM_HAS_POWERED_ON: u32 = 0xe000_0300; + + /// The root power domain's connection, for acknowledging sleep. + static ROOT_PORT: AtomicU32 = AtomicU32::new(0); + + extern "C" fn power(_refcon: *mut c_void, _service: u32, message: u32, argument: *mut c_void) { + match message { + // Every registered client must answer these, or the Mac waits + // up to 30 s before it sleeps. + CAN_SYSTEM_SLEEP | SYSTEM_WILL_SLEEP => unsafe { + IOAllowPowerChange(ROOT_PORT.load(Ordering::Acquire), argument as isize); + }, + SYSTEM_HAS_POWERED_ON => zeron_sync::wake::notify_wake(), + _ => {} + } + } + + /// Hear the Mac's sleep and wake from IOKit on the main run loop, so the + /// engine's sockets redial on wake without a detector waking the + /// process every second to compare clocks. Falls back to that detector + /// if registering fails. + pub(super) fn watch_power() { + unsafe { + let mut port = std::ptr::null_mut(); + let mut notifier = 0u32; + let root = + IORegisterForSystemPower(std::ptr::null_mut(), &mut port, power, &mut notifier); + if root == 0 || port.is_null() { + tracing::warn!( + "couldn't register for power notifications; polling for wake instead" + ); + return; + } + ROOT_PORT.store(root, Ordering::Release); + let source = IONotificationPortGetRunLoopSource(port); + CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopCommonModes); + } + zeron_sync::wake::use_os_wake_events(); + } + + /// The stop source fired: [`run`]'s wait returns and it reads `DONE`. + extern "C" fn stop(_info: *mut c_void) {} + + /// Serve the main run loop until [`finish`]. It sleeps until something + /// arrives (a banner's click, the stop source) rather than on a timer: + /// an idle engine's main thread never wakes. The stop source both keeps + /// the loop from returning at once for want of sources and makes a + /// `finish` that comes before the wait begins end it at once (a signaled + /// source is served as soon as the loop runs). pub(super) fn run() { unsafe { let main = CFRunLoopGetMain(); - let timer = CFRunLoopTimerCreate( - std::ptr::null(), - CFAbsoluteTimeGetCurrent() + 1.0, - 1.0, - 0, - 0, - tick, - std::ptr::null_mut(), - ); - CFRunLoopAddTimer(main, timer, kCFRunLoopCommonModes); + let mut context = SourceContext { + version: 0, + info: std::ptr::null_mut(), + retain: std::ptr::null(), + release: std::ptr::null(), + copy_description: std::ptr::null(), + equal: std::ptr::null(), + hash: std::ptr::null(), + schedule: std::ptr::null(), + cancel: std::ptr::null(), + perform: stop, + }; + let source = CFRunLoopSourceCreate(std::ptr::null(), 0, &mut context); + CFRunLoopAddSource(main, source, kCFRunLoopCommonModes); + STOP.store(source, Ordering::SeqCst); while !DONE.load(Ordering::SeqCst) { - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, 0); + // (Returns after each source served, so `DONE` is read then.) + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, 1); } } } @@ -126,6 +207,12 @@ mod main_loop { /// End [`run`] (any thread). pub(super) fn finish() { DONE.store(true, Ordering::SeqCst); - unsafe { CFRunLoopWakeUp(CFRunLoopGetMain()) }; + let source = STOP.load(Ordering::SeqCst); + unsafe { + if !source.is_null() { + CFRunLoopSourceSignal(source); + } + CFRunLoopWakeUp(CFRunLoopGetMain()); + } } } diff --git a/apps/zeron/src/bin/zeron-engine.rs b/apps/zeron/src/bin/zeron-engine.rs index 76d9bcbc1..0b6648a1d 100644 --- a/apps/zeron/src/bin/zeron-engine.rs +++ b/apps/zeron/src/bin/zeron-engine.rs @@ -2,8 +2,9 @@ //! own ("Zeron Engine" in Activity Monitor): the app starts it and attaches; //! sessions keep running with the app closed. Without the UI linked in. //! -//! It also answers what the engine runs its own executable for: `mcp` (the -//! Zeron MCP server injected into agents) and `--noop-browser` (browser +//! It also answers what the engine runs its own executable for: `mcp-proxy` +//! (an agent's Zeron MCP tools on stdio, served by the engine), `mcp` (the +//! standalone Zeron MCP server) and `--noop-browser` (browser //! suppression for agents' sign-in flows). And it is the engine helper of an //! interface that doesn't link the engine (the zgx app): every `zeron` //! subcommand that runs the engine's code (`headless`, `login`, `status`, @@ -17,6 +18,9 @@ use zeron_cli::{Engines, Launch}; fn main() -> anyhow::Result<()> { match std::env::args().nth(1).as_deref() { Some("--noop-browser") => Ok(()), + // An agent's Zeron MCP tools on stdio, forwarded to the engine's own + // endpoint. First, before any engine setup: it must stay tiny. + Some("mcp-proxy") => zeron_mcp::proxy::run(), Some("--version" | "-V") => { println!("zeron-engine {}", zeron_update::current_version()); Ok(()) diff --git a/apps/zeron/src/main.rs b/apps/zeron/src/main.rs index 0a3d049b8..30ca2a5f1 100644 --- a/apps/zeron/src/main.rs +++ b/apps/zeron/src/main.rs @@ -12,6 +12,11 @@ use zeron_app::{edge_url_from_env, paths, workos_client_id_from_env}; use zeron_cli::{Engines, Launch}; fn main() -> anyhow::Result<()> { + // The engine runs its own executable as an agent's stdio MCP forwarder + // (`zeron headless`'s agents): first, before any setup, to stay tiny. + if std::env::args().nth(1).as_deref() == Some("mcp-proxy") { + return zeron_mcp::proxy::run(); + } match zeron_cli::run(zeron_update::current_version(), Engines::InProcess) { Launch::Done(result) => result, Launch::Engine(command) => zeron_app::run_engine_command(command), diff --git a/apps/zgx/Cargo.lock b/apps/zgx/Cargo.lock index 1fa07a604..84c212da1 100644 --- a/apps/zgx/Cargo.lock +++ b/apps/zgx/Cargo.lock @@ -30,7 +30,7 @@ dependencies = [ [[package]] name = "accesskit_atspi_common" version = "0.19.1" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "accesskit", "accesskit_consumer", @@ -43,7 +43,7 @@ dependencies = [ [[package]] name = "accesskit_consumer" version = "0.38.0" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "accesskit", "hashbrown 0.16.1", @@ -66,7 +66,7 @@ dependencies = [ [[package]] name = "accesskit_macos" version = "0.26.3" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "accesskit", "accesskit_consumer", @@ -79,7 +79,7 @@ dependencies = [ [[package]] name = "accesskit_unix" version = "0.22.1" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "accesskit", "accesskit_atspi_common", @@ -96,7 +96,7 @@ dependencies = [ [[package]] name = "accesskit_windows" version = "0.34.0" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "accesskit", "accesskit_consumer", @@ -6450,7 +6450,7 @@ dependencies = [ [[package]] name = "wgpu-hal" version = "29.0.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "android_system_properties", "arrayvec", @@ -6888,7 +6888,7 @@ checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" [[package]] name = "winit" version = "0.30.13" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "ahash", "android-activity", @@ -7456,7 +7456,7 @@ dependencies = [ [[package]] name = "zgx" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "fixed_decimal", "foldhash 0.2.0", @@ -7485,7 +7485,7 @@ dependencies = [ [[package]] name = "zgx-3d" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "bytemuck", "zgx", @@ -7495,7 +7495,7 @@ dependencies = [ [[package]] name = "zgx-alacritty-terminal" version = "0.26.8" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "base64 0.22.1", "bitflags 2.13.2", @@ -7520,7 +7520,7 @@ dependencies = [ [[package]] name = "zgx-components" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "zgx", ] @@ -7528,7 +7528,7 @@ dependencies = [ [[package]] name = "zgx-cosmic-text" version = "0.19.14" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "bitflags 2.13.2", "fontdb", @@ -7552,7 +7552,7 @@ dependencies = [ [[package]] name = "zgx-desktop" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "accesskit", "accesskit_consumer", @@ -7603,12 +7603,12 @@ dependencies = [ [[package]] name = "zgx-ghostty-sys" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" [[package]] name = "zgx-gpu" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "ash", "block2 0.6.2", @@ -7648,7 +7648,7 @@ dependencies = [ [[package]] name = "zgx-harfrust" version = "0.5.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "bitflags 2.13.2", "bytemuck", @@ -7660,7 +7660,7 @@ dependencies = [ [[package]] name = "zgx-mlua" version = "0.12.9" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "bstr", "either", @@ -7675,7 +7675,7 @@ dependencies = [ [[package]] name = "zgx-run" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "serde_json", "ureq 3.5.0", @@ -7687,7 +7687,7 @@ dependencies = [ [[package]] name = "zgx-script" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "icu_locale_core", "icu_plurals", @@ -7712,7 +7712,7 @@ dependencies = [ [[package]] name = "zgx-swash" version = "0.2.11" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "skrifa", "yazi", @@ -7722,7 +7722,7 @@ dependencies = [ [[package]] name = "zgx-taffy" version = "0.9.2" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "arrayvec", "grid", @@ -7733,7 +7733,7 @@ dependencies = [ [[package]] name = "zgx-terminal" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "base64 0.22.1", "flate2", @@ -7749,7 +7749,7 @@ dependencies = [ [[package]] name = "zgx-vte" version = "0.15.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "arrayvec", "bitflags 2.13.2", @@ -7761,7 +7761,7 @@ dependencies = [ [[package]] name = "zgx-zune-jpeg" version = "0.5.15" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" dependencies = [ "zune-core", ] diff --git a/apps/zgx/Cargo.toml b/apps/zgx/Cargo.toml index eae2c0300..96ab0ff4e 100644 --- a/apps/zgx/Cargo.toml +++ b/apps/zgx/Cargo.toml @@ -32,7 +32,7 @@ layout_probe = [] [dependencies] lz4_flex = { version = "0.11", default-features = false, features = ["safe-encode", "safe-decode"] } -zgx = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } +zgx = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } # `mimalloc`: the program's allocator, in place of the system's. It hands # freed memory back to the system (the system's keeps the most it ever # held), and zgx measures 1-10% less CPU for script, text and layout work. @@ -43,7 +43,7 @@ zgx = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191 # none of it, and without it pages are placeholders ("Web view · "). # zgx-run turns it on by default, but this app takes zgx-run without its # defaults (below). Nothing on macOS or Windows. -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["mimalloc", "terminal", "webkitgtk"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["mimalloc", "terminal", "webkitgtk"] } # `inspect`: devtools for `zgx inspect` (and its MCP server), asked for # with `--inspect`. `i18n`: `ui.t`'s plurals and numbers by each locale's # own rules (ICU4X's data, about 90 KB), for ui/locales' translations. @@ -51,18 +51,18 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb # wgpu this app already links), for plugins that show 3D: the CAD plugin # hands it its own triangles (`geometry = { positions, indices, … }`). It # asks no grant; a scene draws a frame only when something in it changes. -zgx-script = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["inspect", "i18n", "scene3d"] } +zgx-script = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["inspect", "i18n", "scene3d"] } # A picture's pixels for the clipboard (src/images.rs): zgx's own decoder, # the formats `ui.image` shows (no features of its own: the ones zgx-desktop # turns on). -zgx-gpu = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", default-features = false } +zgx-gpu = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", default-features = false } # Each plugin's own database (`zeron.db`, src/plugins/db.rs): SQLite, # built in; `limits` to keep one from attaching any other file. rusqlite = { version = "0.32", features = ["bundled", "limits", "hooks"] } # `fetch`: zgx's own transport for `ui.fetch` and remote images (HTTP and # HTTPS, rustls), given to every window (src/main.rs): the app's scripts, # and plugins granted "network". Nothing else of the runner. -zgx-run = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", default-features = false, features = ["fetch"] } +zgx-run = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", default-features = false, features = ["fetch"] } # `webp`: `ui.image` decodes through this crate, by what its features # allow, so turning WebP on here is all it takes (the member cards' art). # `ico`: sites' favicons (the browser tab's: src/favicons.rs), most of @@ -162,7 +162,7 @@ foreign-types = "0.5" # elements, box drawing and Powerline glyphs to the cell; zgx's own renderer # draws them from the font at its text line height, gapped. Builds Ghostty # with Zig 0.16 (cached in ~/Library/Caches/zgx-ghostty). -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["ghostty"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["ghostty"] } # Windows' Transparency effects (src/transparency.rs), read and watched in # the registry, until zgx reports them (zgx-asks/windows-transparency); a @@ -173,7 +173,7 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb # 11 (and with Edge on Windows 10); without it, pages are placeholders. [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_Media_Audio", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Console", "Win32_System_Registry", "Win32_System_Threading", "Win32_UI_WindowsAndMessaging"] } -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["webview2"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["webview2"] } # zgx's D-Bus (menus, portals) linked in rather than loaded: `zeron` is also # what a headless Linux server and every Cloud machine runs (`zeron @@ -185,13 +185,13 @@ libdbus-sys = { version = "0.2.7", features = ["vendored"] } # zgx's own patches (its root Cargo.toml): a git dependency does not bring # them, so every consumer repeats them. [patch.crates-io] -wgpu-hal = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -winit = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_macos = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_consumer = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_atspi_common = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_unix = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_windows = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } +wgpu-hal = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +winit = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +accesskit_macos = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +accesskit_consumer = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +accesskit_atspi_common = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +accesskit_unix = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +accesskit_windows = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } # zgx is slow unoptimized (its own setting): optimize dependencies, keep # this crate quick to build. diff --git a/apps/zgx/src/chat.rs b/apps/zgx/src/chat.rs index ba44e8181..e2101a68c 100644 --- a/apps/zgx/src/chat.rs +++ b/apps/zgx/src/chat.rs @@ -2567,16 +2567,18 @@ impl View { self.mark_failed(&id, &message_id, None); return Err("missing"); } - if fresh { - engine::get() - .call("Mutate", json!({ "op": "setChatConfig", "chatId": id, "config": config })) - .await - .map_err(|e| { - eprintln!("zeron-zgx: chat: a new chat's config wasn't set: {e}"); - self.mark_failed(&id, &message_id, None); - engine::outcome(&e) - })?; - } + // A chat just made: its config goes out now, and its first message + // right behind it rather than after its answer. Awaited first, the + // message waited out the frame that builds the new chat's page + // (the answer is read once that frame is done). The message carries + // the same agent and model, and the engine stamps them on a chat + // whose config hasn't landed yet, so the two may land in any order. + // Not when the agent changes from the one the chat was made with (a + // side chat takes its parent's): the engine refuses that switch + // while a turn runs, and the message starts one. Then the config is + // awaited first, and the message goes only once it has landed. + let set_config = fresh.then(|| json!({ "op": "setChatConfig", "chatId": id, "config": config })); + let switching = fresh && changes_agent(&chat["config"], &config); let field = |key: &str| req.field(key).map(text).filter(|s| !s.is_empty()); let cwd = field("cwd") .or_else(|| chat["cwd"].as_str().map(Into::into)) @@ -2609,7 +2611,23 @@ impl View { }); } let run = json!({ "kind": "run", "messageId": message_id, "request": request }); - let sent = command_with(&id, run.clone(), &transfers).await; + let sent = match set_config { + Some(params) => { + let engine = engine::get(); + match first_send(switching, engine.call("Mutate", params), command_with(&id, run.clone(), &transfers)).await { + Ok(sent) => sent, + Err(e) => { + // Refused (a turn already running, a host too old to + // switch provider): the message never went, and the + // person is told, as for any send that fails. + eprintln!("zeron-zgx: chat: {id}: a new chat's agent wasn't set: {e}"); + self.mark_failed(&id, &message_id, None); + return Err(engine::outcome(&e)); + } + } + } + None => command_with(&id, run.clone(), &transfers).await, + }; if sent.is_err() { // Not delivered: the prompt stays, failed, to send again. if let Some(pending) = self.pending.borrow_mut().get_mut(&id) @@ -3137,6 +3155,57 @@ async fn command(chat: &str, command: Json) -> Result<(), Outcome> { command_with(chat, command, &[]).await } +/// Whether `next` runs another agent than the `current` config names (one +/// that names none changes nothing the engine would refuse). +fn changes_agent(current: &Json, next: &Json) -> bool { + current["harness"].as_str().is_some_and(|harness| next["harness"].as_str() != Some(harness)) +} + +/// A new chat's config (`configure`) and first message (`send`). Switching +/// agent, the config is awaited first: refused, the message never goes and +/// the refusal is the answer. Otherwise both go out at once ([`both`]), the +/// config first, and the message's answer stands (it carries the same agent +/// and model; a config that didn't land is only logged). +async fn first_send(switching: bool, configure: C, send: S) -> Result +where + C: Future>, + S: Future, +{ + if switching { + configure.await?; + return Ok(send.await); + } + let (configured, sent) = both(configure, send).await; + if let Err(e) = configured { + eprintln!("zeron-zgx: chat: a new chat's config wasn't set: {e}"); + } + Ok(sent) +} + +/// Both futures, polled together (`a` first each time), and both answers. +async fn both(a: A, b: B) -> (A::Output, B::Output) { + let (mut a, mut b) = (std::pin::pin!(a), std::pin::pin!(b)); + let (mut done_a, mut done_b) = (None, None); + std::future::poll_fn(move |cx| { + if done_a.is_none() + && let std::task::Poll::Ready(out) = a.as_mut().poll(cx) + { + done_a = Some(out); + } + if done_b.is_none() + && let std::task::Poll::Ready(out) = b.as_mut().poll(cx) + { + done_b = Some(out); + } + if done_a.is_some() && done_b.is_some() { + std::task::Poll::Ready((done_a.take().unwrap(), done_b.take().unwrap())) + } else { + std::task::Poll::Pending + } + }) + .await +} + /// [`command`], with the files the engine delivers for it. async fn command_with(chat: &str, command: Json, transfers: &[Json]) -> Result<(), Outcome> { let mut params = json!({ "chatId": chat, "command": command }); @@ -6593,6 +6662,94 @@ mod tests { assert_eq!(words.lines().count(), 30); } + /// A new side chat switched from its parent's agent: its config is + /// awaited before its first message goes, and a refusal (a turn already + /// running, a host too old to switch) stops the message and is the + /// answer. The same agent: both go out in one poll, and a config that + /// didn't land leaves the sent message standing. + #[test] + fn a_new_chat_switching_agent_sends_its_message_only_once_the_config_landed() { + use std::task::{Context, Poll, Waker}; + let claude = json!({ "harness": "claude-code", "model": "opus" }); + let codex = json!({ "harness": "codex", "model": "gpt" }); + assert!(super::changes_agent(&claude, &codex)); + assert!(!super::changes_agent(&claude, &json!({ "harness": "claude-code", "model": "sonnet" }))); + assert!(!super::changes_agent(&Json::Null, &codex), "made with no agent: nothing to refuse"); + + // Config, then message, never both at once. + let log = Rc::new(std::cell::RefCell::new(Vec::new())); + let (config_gate, a, b) = (Rc::new(std::cell::Cell::new(false)), log.clone(), log.clone()); + let gate = config_gate.clone(); + let configure = std::future::poll_fn(move |_| { + if gate.get() { + a.borrow_mut().push("config landed"); + Poll::Ready(Ok::(Json::Null)) + } else { + Poll::Pending + } + }); + let send = async move { + b.borrow_mut().push("message"); + "sent" + }; + let mut cx = Context::from_waker(Waker::noop()); + let mut sending = std::pin::pin!(super::first_send(true, configure, send)); + assert!(sending.as_mut().poll(&mut cx).is_pending()); + assert!(log.borrow().is_empty(), "the message went before the config landed"); + config_gate.set(true); + assert_eq!(sending.as_mut().poll(&mut cx), Poll::Ready(Ok("sent"))); + assert_eq!(*log.borrow(), ["config landed", "message"]); + + // Refused: the message never goes, and the refusal is the answer. + let went = Rc::new(std::cell::Cell::new(false)); + let w = went.clone(); + let refused = std::pin::pin!(super::first_send( + true, + async { Err::("Wait for the current turn to finish before switching provider".into()) }, + async move { w.set(true) }, + )) + .poll(&mut cx); + assert!(matches!(refused, Poll::Ready(Err(ref e)) if e.contains("switching provider"))); + assert!(!went.get()); + + // The same agent: one poll sends both, and the message's answer stands. + let went = Rc::new(std::cell::Cell::new(false)); + let w = went.clone(); + let together = std::pin::pin!(super::first_send( + false, + async { Err::("offline".into()) }, + async move { + w.set(true); + "sent" + }, + )) + .poll(&mut cx); + assert_eq!(together, Poll::Ready(Ok("sent"))); + assert!(went.get()); + } + + /// A new chat's config and first message go out in the same poll, the + /// config first, and both answers come back (`both`). + #[test] + fn both_sends_the_first_then_the_second_in_one_poll() { + use std::task::{Context, Poll, Waker}; + let sent = Rc::new(std::cell::RefCell::new(Vec::new())); + let (a, b) = (sent.clone(), sent.clone()); + let mut joined = std::pin::pin!(super::both( + async move { + a.borrow_mut().push("config"); + 1 + }, + async move { + b.borrow_mut().push("message"); + 2 + }, + )); + let mut cx = Context::from_waker(Waker::noop()); + assert_eq!(joined.as_mut().poll(&mut cx), Poll::Ready((1, 2))); + assert_eq!(*sent.borrow(), vec!["config", "message"]); + } + /// 2026-11-11, work-laptop: a queued message on the Mac hosting it said /// "being edited on another device" while the app didn't know yet which /// device it was on. Only a known, different owner is another device. diff --git a/apps/zgx/src/engine/rpc.rs b/apps/zgx/src/engine/rpc.rs index da3017c11..3b0169b6b 100644 --- a/apps/zgx/src/engine/rpc.rs +++ b/apps/zgx/src/engine/rpc.rs @@ -53,6 +53,39 @@ pub fn read_token(data_dir: &Path) -> Option { (!token.is_empty()).then(|| token.to_string()) } +/// One answer of `method` from the engine on `port`, read here and now, on +/// this thread: a call's reply, or a stream's first item. `None` when +/// nothing answers, it refuses the token, says an error, or not within +/// `within` all told. For a launch reading what an engine that's already +/// running has (`plugins::preload`) before its window is built; everything +/// else goes through the connection ([`connect`]). +pub fn ask_now(port: u16, data_dir: &Path, method: &str, params: Value, within: Duration) -> Option { + let until = std::time::Instant::now() + within; + let token = read_token(data_dir); + let mut socket = dial_within(port, token.as_deref(), within).ok()?; + let request = json!({ "id": 1, "method": method, "params": params }).to_string(); + socket.send(tungstenite::Message::text(request)).ok()?; + let answer = loop { + let left = until.checked_duration_since(std::time::Instant::now())?; + let _ = socket.get_mut().set_read_timeout(Some(left.max(Duration::from_millis(1)))); + let tungstenite::Message::Text(text) = socket.read().ok()? else { continue }; + let Ok(mut frame) = serde_json::from_str::(&text) else { continue }; + if frame.get("id").and_then(Value::as_u64) != Some(1) { + continue; + } + if frame.get("err").is_some() || frame.get("done").is_some() { + break None; + } + if let Some(item) = frame.get_mut("item") { + break Some(item.take()); + } + break frame.get_mut("ok").map(Value::take); + }; + let _ = socket.close(None); + let _ = socket.flush(); + answer +} + #[derive(Default)] struct Inbox { frames: VecDeque, @@ -99,16 +132,20 @@ type Socket = tungstenite::WebSocket; /// The handshake, blocking (on the network thread). fn dial(port: u16, token: Option<&str>) -> Result { + dial_within(port, token, DIAL_TIMEOUT) +} + +fn dial_within(port: u16, token: Option<&str>, within: Duration) -> Result { use tungstenite::client::IntoClientRequest; let address = SocketAddr::from(([127, 0, 0, 1], port)); - let stream = TcpStream::connect_timeout(&address, DIAL_TIMEOUT).map_err(|error| { + let stream = TcpStream::connect_timeout(&address, within).map_err(|error| { if error.kind() == std::io::ErrorKind::ConnectionRefused { DialError::Refused } else { DialError::Other(error.to_string()) } })?; - let _ = stream.set_read_timeout(Some(DIAL_TIMEOUT)); + let _ = stream.set_read_timeout(Some(within)); let _ = stream.set_nodelay(true); let mut request = format!("ws://127.0.0.1:{port}") .into_client_request() @@ -136,6 +173,50 @@ fn dial(port: u16, token: Option<&str>) -> Result { } } +/// One call answered on a connection of its own, blocking (at most +/// `timeout` waiting for the answer): for the app's last words to its +/// engine as it quits, once its own connection and tasks are gone. +pub fn call_blocking( + port: u16, + data_dir: &Path, + method: &str, + params: Value, + timeout: std::time::Duration, +) -> Result { + let token = read_token(data_dir); + let mut socket = dial(port, token.as_deref()).map_err(|error| match error { + DialError::Refused => "offline".to_string(), + DialError::Unauthorized => "unauthorized".to_string(), + DialError::Other(error) => error, + })?; + let _ = socket.get_mut().set_read_timeout(Some(timeout)); + let frame = serde_json::json!({ "id": 1, "method": method, "params": params }); + socket + .send(tungstenite::Message::text(frame.to_string())) + .map_err(|error| error.to_string())?; + loop { + match socket.read().map_err(|error| error.to_string())? { + tungstenite::Message::Text(text) => { + for line in text.lines() { + let Ok(frame) = serde_json::from_str::(line) else { + continue; + }; + if frame["id"] != 1 { + continue; + } + if let Some(error) = frame["err"].as_str() { + return Err(error.to_string()); + } + let _ = socket.close(None); + return Ok(frame["ok"].clone()); + } + } + tungstenite::Message::Close(_) => return Err("closed".into()), + _ => {} + } + } +} + /// Whether a socket call only found nothing to do yet: no frame to read, or /// no room to write (the frame waits in tungstenite's buffer). pub(crate) fn would_block(error: &tungstenite::Error) -> bool { @@ -643,6 +724,54 @@ impl Drop for Watch { mod tests { use super::*; + /// An engine on a port of its own that answers each request with + /// `answer` (or, `None`, never answers), and a data dir holding a token. + fn engine_answering(answer: Option) -> (u16, std::path::PathBuf) { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let dir = std::env::temp_dir().join(format!("zgx-ask-now-{port}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("ipc-token"), "token").unwrap(); + std::thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut socket = tungstenite::accept(stream).unwrap(); + let Ok(tungstenite::Message::Text(text)) = socket.read() else { return }; + let request: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(request["method"], "WatchPlugins"); + match answer { + Some(item) => { + // (Another stream's frame first: only this request's counts.) + let other = json!({ "id": 7, "item": "not this one" }).to_string(); + socket.send(tungstenite::Message::text(other)).unwrap(); + let frame = json!({ "id": request["id"], "item": item }).to_string(); + socket.send(tungstenite::Message::text(frame)).unwrap(); + let _ = socket.read(); + } + None => std::thread::sleep(Duration::from_secs(2)), + } + }); + (port, dir) + } + + /// A launch reads what an engine already running says, at once; nothing + /// running, or none answering in time, is no answer, and no wait past it. + #[test] + fn asking_now_takes_the_first_answer_or_none_in_time() { + let (port, dir) = engine_answering(Some(json!({ "plugins": [{ "id": "git" }] }))); + let list = ask_now(port, &dir, "WatchPlugins", json!({}), Duration::from_secs(2)).unwrap(); + assert_eq!(list["plugins"][0]["id"], "git"); + + let free = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + let started = std::time::Instant::now(); + assert!(ask_now(free, &dir, "WatchPlugins", json!({}), Duration::from_secs(2)).is_none()); + assert!(started.elapsed() < Duration::from_secs(1), "refused at once"); + + let (port, dir) = engine_answering(None); + let started = std::time::Instant::now(); + assert!(ask_now(port, &dir, "WatchPlugins", json!({}), Duration::from_millis(150)).is_none()); + assert!(started.elapsed() < Duration::from_millis(600), "waited {:?}", started.elapsed()); + } + /// A connection with no socket: frames handed to it as the network /// thread would. fn loopback() -> (Rpc, Frames, OwnedFd) { diff --git a/apps/zgx/src/long_thread_tests.rs b/apps/zgx/src/long_thread_tests.rs new file mode 100644 index 000000000..290e90e5d --- /dev/null +++ b/apps/zgx/src/long_thread_tests.rs @@ -0,0 +1,141 @@ +//! A long thread, as ademark's A11 makes it: one reply of a thousand steps, +//! each a short text (one in five with a code block) and a call reading a +//! file, about 2,000 blocks, its last text holding `LONGTHREAD-END` (the +//! engine's mock `/longthread`). Its rows built by src/chat.rs from the +//! engine's entry, as the app builds them. +//! +//! Opening it costs what its end costs: a reply's blocks are windowed in runs +//! (ds/transcript's `blocks_of`), so the scripts a thousand steps call and +//! the nodes they make are about a hundred steps'. (Nov 13: 0.3.4 built all +//! 2,000 blocks' rows on every open, 44,000 script runs in one frame, about +//! 380 ms on an M1 where 0.2 took 96 ms.) Scrolled up, it passes through +//! every step to its first. + +use std::path::PathBuf; + +use serde_json::{Value as Json, json}; +use zgx_script::{ScriptHost, Value, testing::TestApp}; + +use crate::transcript_scroll_tests::{moved, shown}; + +fn ui() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui") +} + +/// The thread as the engine holds it (`/longthread `), built into its +/// rows as src/chat.rs builds them. +fn long_thread(steps: usize) -> Value { + let mut parts: Vec = Vec::with_capacity(steps * 2 + 1); + for n in 1..=steps { + let mut text = format!("Step {n} of {steps}. I read src/invoices.ts and noted what step {n} needs from it."); + if n % 5 == 0 { + text.push_str("\n\n```ts\nfunction summarize(invoice: Invoice): InvoiceSummary {\n const total = invoiceTotal(invoice);\n return { id: invoice.id, totalCents: total };\n}\n```"); + } + parts.push(json!({ "id": format!("t{}", 2 * (n - 1)), "kind": "text", "text": text })); + parts.push(json!({ + "id": format!("toolu_long_{n:04}"), + "kind": "tool", + "call": { "kind": "readFile", "path": "/private/tmp/ademark-recording/arena/src/invoices.ts" }, + "isError": false, + "resolved": true, + })); + } + parts.push(json!({ "id": format!("t{}", 2 * steps), "kind": "text", "text": "All steps done. LONGTHREAD-END" })); + let at = 1_794_000_000_000_i64; + let prompt = json!({ "id": "lt-u", "role": "user", "parts": [{ "id": "lt-up", "kind": "text", "text": format!("/longthread {steps}") }], "createdAt": at }); + let reply = json!({ "id": "lt-a", "role": "assistant", "status": "complete", "parts": parts, "createdAt": at + 1000, "durationMs": 28 }); + let rows: Vec = [prompt, reply] + .iter() + .map(|entry| crate::chat::built_entry(entry, "doc", None, Some("Opus")).unwrap()) + .collect(); + Value::List(rows.into()) +} + +/// The chat page on another chat, then the thread of `steps` opened in it: +/// the app, the script calls its opening took (until it rests), and the +/// nodes its window holds then. +fn opened(steps: usize) -> (TestApp, u64, usize) { + let fixture = ui().join("chat_fixture.luau"); + let mut app = TestApp::with_spare_time(move |runtime| ScriptHost::new(runtime, fixture)); + app.ui.scene.borrow_mut().resize(960., 700.); + app.frames(20); + assert!(app.host.run_command("go_s7")); + for _ in 0..30 { + app.spend_spare_time(); + app.frame(); + } + let messages = app.host.shared("chat.messages", Value::Nil); + let calls = app.calls(); + messages.set(long_thread(steps)); + for _ in 0..30 { + app.spend_spare_time(); + app.frame(); + } + assert_eq!(app.error(), None); + let calls = app.calls() - calls; + let nodes = app.ui.scene.borrow().len(); + let _ = app.find("transcript"); + (app, calls, nodes) +} + +#[test] +fn a_long_threads_open_costs_what_its_end_costs() { + let (mut long, calls, nodes) = opened(1000); + let (_, short_calls, short_nodes) = opened(100); + eprintln!("open: 1000 steps {calls} calls, {nodes} nodes; 100 steps {short_calls} calls, {short_nodes} nodes"); + // Ten times the steps, not ten times the nodes: a held run is a box. + // (Before the window, all their blocks built: 1000 steps held 18,340 + // nodes and took 43,091 calls; 100, 2,148 and 4,383. Now 782 and 962; + // 985 and 1,583.) + assert!(nodes < short_nodes * 5 / 4, "1000 steps held {nodes} nodes once open, 100 held {short_nodes}"); + // Budgets of its own, about twice what it takes now. + assert!(nodes < 2_000, "1000 steps held {nodes} nodes once open"); + assert!(calls < 2_500, "1000 steps took {calls} script calls to open"); + // Its end shows, opened there. + let end = shown(&mut long); + assert!(end.iter().any(|(words, _)| words == "All steps done. LONGTHREAD-END"), "the end shows: {end:?}"); +} + +/// Up from its end a trackpad's step a refresh, what shows moves by the step +/// each time (runs built as they come near, held ones above measured without +/// moving the view), through to its first step; and back to its end. +#[test] +fn a_long_thread_scrolled_up_passes_through_every_step_to_its_first() { + let steps = 300; + let (mut app, _, _) = opened(steps); + let list = app.find("transcript").expect("the transcript"); + let step = 60.; + let mut skips = Vec::new(); + let mut first = false; + for i in 0..2000 { + let before = shown(&mut app); + if before.iter().any(|(words, _)| words.starts_with(&format!("Step 1 of {steps}."))) { + first = true; + break; + } + zgx_script::driver::wheel(&mut app.ui, list, 0., -step, None); + app.spend_spare_time(); + app.frame(); + let after = shown(&mut app); + let (d, n) = moved(&before, &after); + if d.is_none_or(|d| (d - step).abs() > 1.) { + skips.push(format!("step {i}: moved {d:?} ({n} texts in both)")); + } + } + assert_eq!(app.error(), None); + assert!(skips.is_empty(), "each step of {step} moved the view by {step}:\n{}", skips.join("\n")); + assert!(first, "its first step came into view"); + // And from its very top, Jump to latest takes it back to its end. + for _ in 0..30 { + app.spend_spare_time(); + app.frame(); + } + app.click("role:button \"Jump to latest\""); + for _ in 0..90 { + app.spend_spare_time(); + app.frame(); + } + let end = shown(&mut app); + assert!(end.iter().any(|(words, _)| words == "All steps done. LONGTHREAD-END"), "back at its end: {end:?}"); + assert_eq!(app.error(), None); +} diff --git a/apps/zgx/src/main.rs b/apps/zgx/src/main.rs index 11a592697..11e523cbd 100644 --- a/apps/zgx/src/main.rs +++ b/apps/zgx/src/main.rs @@ -60,6 +60,7 @@ mod mermaid; mod plugins; mod previews; mod projects; +mod quit_prompt; mod relaunch; mod session_home; mod sessions; @@ -134,6 +135,8 @@ mod transcript_scroll_tests; #[cfg(test)] mod transcript_paint_tests; #[cfg(test)] +mod long_thread_tests; +#[cfg(test)] mod editor_scroll_tests; #[cfg(test)] mod i18n_tests; @@ -371,6 +374,12 @@ fn main() -> Result<(), Box> { links::open(url); } })?; + // Quitting with "Keep Zeron running after you quit" off: the engine + // stops once its work is done, asking first about any still running + // (src/quit_prompt.rs). (Not for a relaunch: the app is coming back.) + if !relaunch::asked() { + quit_prompt::after_quit(); + } // Quitting: an update downloaded and never restarted for installs now, // so the next launch is on it (src/about.rs). about::install_on_quit(); @@ -665,6 +674,9 @@ fn script_host_at(cx: &mut WindowContext, path: PathBuf, entry: &str) -> ScriptH cx.window_info.controls.clone(), ); host.window_zoom(&cx.ui); + // `ui.window.hold_frames`: a chat switched to keeps the page before it + // showing until its transcript has come (screens/chat/session.luau). + host.window_hold(&cx.ui); // `ui.window.size` (the lightbox fills the window with it). host.window_size(cx.viewport.clone()); // `ui.window.full_screen` and its transition (macOS's animation, going @@ -1097,6 +1109,12 @@ fn serve_with( cx.on_closed(move || { reloading.borrow_mut().take(); if let Some((host, devtools, commands, corner)) = keep.borrow_mut().take() { + // The scripts' last word before what they saved is written: a + // save still waiting on a timer (the new chat's draft, + // ui/screens/chat/shared.luau) goes now, as no timer fires + // after this. (Read at once: setting it runs what follows it.) + host.shared("app.closing", Value::Bool(false)) + .set(Value::Bool(true)); host.save(); drop(devtools); drop(commands); diff --git a/apps/zgx/src/plugins/live.rs b/apps/zgx/src/plugins/live.rs index d6197d413..44155cb06 100644 --- a/apps/zgx/src/plugins/live.rs +++ b/apps/zgx/src/plugins/live.rs @@ -219,6 +219,14 @@ pub struct Overlay { /// `failed` files (Zeron's show in their place) and the paths never templated. /// Zeron's own alone for `BUILTIN`, or `whole_failed`. How many files changed. pub fn compose(templates: &Path, id: &str, failed: &BTreeSet, whole_failed: bool) -> std::io::Result<(usize, Overlay)> { + let (want, overlay) = plan(templates, id, failed, whole_failed); + let changed = mirror(&templates.join(LIVE), &want, |_| false)?; + Ok((changed, overlay)) +} + +/// What `compose` lays down, each file's source by its path in `.live`, and +/// the overlay that is: folders listed, nothing read. +pub fn plan(templates: &Path, id: &str, failed: &BTreeSet, whole_failed: bool) -> (BTreeMap, Overlay) { let mut want = files(&templates.join(ZERON), |rel| !template_meta(rel)); let mut from_template = BTreeSet::new(); if id != BUILTIN && !whole_failed { @@ -240,8 +248,64 @@ pub fn compose(templates: &Path, id: &str, failed: &BTreeSet, whole_fail want.insert(rel, path); } } - let changed = mirror(&templates.join(LIVE), &want, |_| false)?; - Ok((changed, Overlay { template: id.to_string(), from_template })) + (want, Overlay { template: id.to_string(), from_template }) +} + +/// Beside the templates: what a launch last laid `.live` down from, and +/// `.live` as that left it (`LaunchStamp`). +pub const LAUNCH_STAMP: &str = ".launch-stamp.json"; + +/// What a launch lays `.live` down from, as it stands (each file's path, +/// size and time written; nothing read): this Zeron, `ui/`, the active +/// template, and `.live` itself. A launch that finds the one the last left +/// needs neither `sync_zeron` nor `compose`, which read every file of +/// Zeron's (twice) to write none: a quarter of a second of a warm launch +/// spent before the window, more from a cold disk. Anything changed since +/// (an update, a template edited or switched, `.live` written by anyone) +/// stamps differently, and the launch lays everything down as before. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct LaunchStamp { + pub zeron: String, + pub ui: String, + pub id: String, + pub template: String, + pub live: String, +} + +impl LaunchStamp { + /// `ui/` and template `id` under `templates` as they stand now. + pub fn now(ui: &Path, templates: &Path, id: &str) -> Self { + Self { + zeron: ZERON_VERSION.to_string(), + ui: ui_stamp(ui), + id: id.to_string(), + template: if id == BUILTIN { String::new() } else { template_stamp(&templates.join(id)) }, + live: stamp(&templates.join(LIVE), |_| true), + } + } + + /// The one the last launch left, if any. + pub fn read(templates: &Path) -> Option { + let v = read_json(&templates.join(LAUNCH_STAMP)); + let field = |k: &str| v[k].as_str().map(str::to_string); + Some(Self { zeron: field("zeron")?, ui: field("ui")?, id: field("id")?, template: field("template")?, live: field("live")? }) + } + + pub fn write(&self, templates: &Path) -> std::io::Result { + let v = serde_json::json!({ + "zeron": self.zeron, "ui": self.ui, "id": self.id, "template": self.template, "live": self.live, + }); + put(&templates.join(LAUNCH_STAMP), &serde_json::to_vec_pretty(&v).unwrap_or_default()) + } + + /// Whether `.live` can run as it is: laid from just this, and whole + /// (Zeron's copy and its hashes there, the official templates too). + pub fn still_laid(&self, ui: &Path, templates: &Path) -> bool { + Self::read(templates).as_ref() == Some(self) + && templates.join(LIVE).join("app.luau").is_file() + && templates.join(ZERON).join(".hashes.json").is_file() + && !super::official::missing(ui, templates) + } } /// The files of `.live` an error names, first mention first: each @@ -841,6 +905,59 @@ mod tests { assert_ne!(survey(&ui, &t, Some(&saved.ui_stamp)).template_stamp, used.template_stamp); } + /// A launch lays `.live` down once; the next, finding everything as the + /// last left it, runs it as it is. Anything changed since lays it down + /// again: `ui/` (an update), the template switched or edited, `.live` + /// written by anyone, a file of it gone. + #[test] + fn a_launch_runs_live_as_it_is_only_while_nothing_changed() { + let root = dir("launch-stamp"); + let ui = root.join("ui"); + write(&ui, "app.luau", "-- app"); + write(&ui, "screens/x.luau", "-- x"); + let t = root.join("templates"); + let lay = |id: &str| { + sync_zeron(&ui, &t).unwrap(); + compose(&t, id, &Default::default(), false).unwrap(); + LaunchStamp::now(&ui, &t, id).write(&t).unwrap(); + }; + let runs_as_is = |id: &str| LaunchStamp::now(&ui, &t, id).still_laid(&ui, &t); + // Nothing laid yet. + assert!(!runs_as_is(BUILTIN)); + lay(BUILTIN); + assert!(runs_as_is(BUILTIN)); + assert!(runs_as_is(BUILTIN), "looking writes nothing"); + // Zeron's own changed (an update): laid again, then as it is. + std::thread::sleep(std::time::Duration::from_millis(5)); + write(&ui, "screens/x.luau", "-- x, updated"); + assert!(!runs_as_is(BUILTIN)); + lay(BUILTIN); + assert!(runs_as_is(BUILTIN)); + assert_eq!(read(&t.join(LIVE), "screens/x.luau"), "-- x, updated"); + // `.live` written by someone else, or a file of it gone. + std::thread::sleep(std::time::Duration::from_millis(5)); + write(&t.join(LIVE), "screens/x.luau", "-- not this Zeron's"); + assert!(!runs_as_is(BUILTIN)); + lay(BUILTIN); + std::fs::remove_file(t.join(LIVE).join("app.luau")).unwrap(); + assert!(!runs_as_is(BUILTIN)); + lay(BUILTIN); + assert!(runs_as_is(BUILTIN)); + // Another template in use, then one of its files edited. + write(&t, "mine/zeron-template.json", r#"{"name":"Mine"}"#); + write(&t, "mine/screens/x.luau", "-- mine"); + assert!(!runs_as_is("mine")); + lay("mine"); + assert!(runs_as_is("mine")); + assert_eq!(read(&t.join(LIVE), "screens/x.luau"), "-- mine"); + std::thread::sleep(std::time::Duration::from_millis(5)); + write(&t, "mine/screens/x.luau", "-- mine, edited"); + assert!(!runs_as_is("mine")); + // (The overlay a launch that skips the copy still knows.) + let (_, overlay) = plan(&t, "mine", &Default::default(), false); + assert!(overlay.from_template.contains("screens/x.luau")); + } + #[test] fn ui_is_watched_by_its_folders_scripts_and_themes() { let ui = Path::new(env!("CARGO_MANIFEST_DIR")).join("ui"); diff --git a/apps/zgx/src/plugins/mod.rs b/apps/zgx/src/plugins/mod.rs index 43e8a8776..915171524 100644 --- a/apps/zgx/src/plugins/mod.rs +++ b/apps/zgx/src/plugins/mod.rs @@ -202,8 +202,17 @@ struct Gh { watches: github::Watches, } +/// How long Settings → Plugins waits for what asked it to settle: past the +/// launch (its first publish), then a moment. +const PAGE_SETTLE_LAUNCH: std::time::Duration = std::time::Duration::from_millis(1500); +const PAGE_SETTLE: std::time::Duration = std::time::Duration::from_millis(200); + pub struct Bridge { host: ScriptHost, + /// A publish of Settings → Plugins on its way (`publish_page`), and + /// whether one has been made. + page_due: Cell, + page_published: Cell, /// Where each plugin's requests may go, as its manifest names them. network: network::Network, windows: WindowFactory, @@ -297,6 +306,9 @@ pub struct Bridge { /// (`plugins.remote`; never loaded here). pub(crate) remote: remote::Remote, me: RefCell>, + /// The window's first load is still to come (until `set_view`): + /// plugins applied before it load with it, no reload. + first_load: Cell, } /// What plugins may read and ask of the app, each behind its permission, @@ -346,6 +358,29 @@ pub fn expose_own(host: &ScriptHost, id: &str) { /// finished (no organization chosen): then they're asked again once it is /// (`auth::set_up`), on the same connection, so plugins load as setup /// finishes, with no relaunch. Settings → Plugins says so meanwhile. +/// How long a launch waits for an engine already running to say which +/// plugins it allows: an answer is a few milliseconds; past this the +/// window doesn't wait, and they come with the connection as ever. +const PRELOAD_WAIT: std::time::Duration = std::time::Duration::from_millis(150); + +/// The plugins an engine that's already running allows (`WatchPlugins`' +/// first word), heard before the window's first load, which then loads +/// them with it. Otherwise their list came with the connection, after the +/// first frame, and loading them took a reload of every script in the +/// window (a fifth of a second on an M5 Max, as the first keys are typed); +/// the list the connection brings is now the one loaded, and changes +/// nothing. Only what the engine says is loaded, as ever: with no engine +/// running (a cold start), or none answering in `PRELOAD_WAIT`, nothing. +fn preload(b: &Rc) { + let Some((port, data_dir)) = engine::get().ipc_endpoint() else { return }; + let Some(list) = crate::engine::rpc::ask_now(port, &data_dir, "WatchPlugins", json!({}), PRELOAD_WAIT) else { + return; + }; + if list.get("plugins").is_some_and(Json::is_array) { + b.heard(&list); + } +} + fn connect(b: &Rc, rpc: &Rpc) { // Zeron's own plugins, shipped beside the app's scripts: the engine // scans them with yours (each connection: an engine started again @@ -468,6 +503,8 @@ pub fn install(cx: &mut WindowContext, host: &ScriptHost) { network: plugin_network, windows: cx.windows.clone(), page: host.shared("plugins", page_value("loading", None, &[], &[])), + page_due: Cell::new(false), + page_published: Cell::new(false), menus: host.shared("plugins.menus", vlist(Vec::new())), requests: host.shared("plugins.request", vlist(Vec::new())), request_list: RefCell::default(), @@ -516,6 +553,7 @@ pub fn install(cx: &mut WindowContext, host: &ScriptHost) { reviewed: Cell::new(false), remote: remote::Remote::new(host.shared("plugins.remote", remote::Remote::initial())), me: RefCell::default(), + first_load: Cell::new(true), }); *bridge.me.borrow_mut() = Rc::downgrade(&bridge); BRIDGE.with(|b| *b.borrow_mut() = Rc::downgrade(&bridge)); @@ -542,6 +580,10 @@ pub fn install(cx: &mut WindowContext, host: &ScriptHost) { } }); + // A launch whose engine already runs (it outlives the app): the plugins + // it has allowed, in the window's first load (`preload`). + preload(&bridge); + let weak = Rc::downgrade(&bridge); engine::get().on_connect(move |rpc| { let Some(b) = weak.upgrade() else { return }; @@ -674,27 +716,42 @@ pub fn prepare_live() -> PathBuf { preview::set_template_running(id != live::BUILTIN); preview::set((!preview::trusted(&root, &id)).then(|| preview::Preview { name: template_name(&root, &id), id: id.clone() })); } - if let Err(error) = live::sync_zeron(&crate::ui_dir(), &root) { - eprintln!("zeron-zgx: Zeron's own interface wasn't copied ({error}); running it from ui/"); - return fallback; - } // The built-in themes once installed as templates, retired: one in use // gives way to the default template, wearing that theme // (retired_themes.rs). if let Some(data) = crate::engine::data_dir() { retired_themes::retire(&root, &data); } + let ui = crate::ui_dir(); let id = live::active(&root); + // `.live` as the last launch left it, from the same Zeron and template: + // it runs as it is (live::LaunchStamp), nothing read or written. + let stamp = live::LaunchStamp::now(&ui, &root, &id); + if stamp.still_laid(&ui, &root) { + let (_, overlay) = live::plan(&root, &id, &Default::default(), false); + LIVE_OVERLAY.with(|o| *o.borrow_mut() = overlay); + let laid = live::Laid { ui_stamp: stamp.ui, id, template_stamp: stamp.template, ..Default::default() }; + LIVE_LAID.with(|l| *l.borrow_mut() = laid); + return root.join(live::LIVE).join("app.luau"); + } + if let Err(error) = live::sync_zeron(&ui, &root) { + eprintln!("zeron-zgx: Zeron's own interface wasn't copied ({error}); running it from ui/"); + return fallback; + } match live::compose(&root, &id, &Default::default(), false) { Ok((_, overlay)) => { LIVE_OVERLAY.with(|o| *o.borrow_mut() = overlay); - let template_stamp = if id == live::BUILTIN { String::new() } else { live::template_stamp(&root.join(&id)) }; - let laid = live::Laid { ui_stamp: live::ui_stamp(&crate::ui_dir()), id, template_stamp, ..Default::default() }; - LIVE_LAID.with(|l| *l.borrow_mut() = laid); // (Pictures of templates taken from the window, before their // previews were drawn from their colours: gone.) let _ = std::fs::remove_dir_all(root.join(".previews")); let _ = std::fs::remove_dir_all(root.join(live::LIVE).join("previews")); + // What the next launch can run as it is: `.live` as this left it. + let laid_now = live::LaunchStamp::now(&ui, &root, &id); + if let Err(error) = laid_now.write(&root) { + eprintln!("zeron-zgx: the launch stamp wasn't kept ({error})"); + } + let laid = live::Laid { ui_stamp: laid_now.ui, id, template_stamp: laid_now.template, ..Default::default() }; + LIVE_LAID.with(|l| *l.borrow_mut() = laid); root.join(live::LIVE).join("app.luau") } Err(error) => { @@ -1005,6 +1062,7 @@ fn dev_service(host: &ScriptHost, id: &str) { /// elements in it. pub fn set_view(handle: zgx::compose::ViewHandle) { if let Some(b) = BRIDGE.with(|b| b.borrow().upgrade()) { + b.first_load.set(false); *b.view.borrow_mut() = Some(handle); b.publish_page(); } @@ -1340,7 +1398,10 @@ impl Bridge { } *self.loaded.borrow_mut() = want.clone(); self.host.set_plugins(want); - self.reload(); + // (Before the window's first load: that load takes them.) + if !self.first_load.get() { + self.reload(); + } } fn reload(&self) { @@ -1702,7 +1763,32 @@ impl Bridge { ])); } + /// Settings → Plugins brought up to date, once what asked has settled: + /// asks in a row (a launch makes several: the view known, the engine's + /// list, its look) make one, after [`PAGE_SETTLE`] ([`PAGE_SETTLE_LAUNCH`] + /// for a launch's first). It walks every element of the window (a tenth + /// of a second of a launch's main thread), and nothing a launch shows + /// reads it. fn publish_page(&self) { + if cfg!(test) { + return self.publish_page_now(); + } + if self.page_due.replace(true) { + return; + } + let settle = if self.page_published.get() { PAGE_SETTLE } else { PAGE_SETTLE_LAUNCH }; + let me = Rc::downgrade(&self.me()); + self.tasks.spawn(async move { + zgx::timer::sleep(settle).await; + if let Some(b) = me.upgrade() { + b.page_due.set(false); + b.page_published.set(true); + b.publish_page_now(); + } + }); + } + + fn publish_page_now(&self) { // A plugin that failed here shows so, with why, whatever the engine // says of it; one placed where the template in use has nothing says // where it won't show, and one whose place zgx refused says why. diff --git a/apps/zgx/src/quit_prompt.rs b/apps/zgx/src/quit_prompt.rs new file mode 100644 index 000000000..cfedb19a3 --- /dev/null +++ b/apps/zgx/src/quit_prompt.rs @@ -0,0 +1,202 @@ +//! Quitting with "Keep Zeron running after you quit" off (Settings → +//! General; the engine's `crates/engine/src/keep_running.rs`). +//! +//! On (the default) nothing happens here: the engine keeps running, as it +//! always has. Off: the app tells its engine it quit, and the engine stops +//! once nothing is pending. Work still running is never ended silently: +//! the app asks first — "N chats are still working. Keep running in the +//! background until they finish, or stop them now?" — and only "Stop them +//! now" stops them (each chat's own Stop). Anything that keeps the question +//! from being asked (no engine, no answer) leaves the engine running. +//! +//! The question's words are the scripts' (`ui.t`, in the language the app +//! shows), handed over as they change (`app.quit_words`): by the time the +//! app quits, its scripts are gone. + +use std::cell::RefCell; +use std::time::Duration; + +use serde_json::{Value as Json, json}; + +/// The question, in the app's language: the message for one chat and for +/// many (its count written as [`COUNT_MARK`]), the detail, and the buttons. +#[derive(Debug, Clone, PartialEq)] +pub struct Words { + pub one: String, + pub other: String, + pub detail: String, + pub keep: String, + pub stop: String, +} + +/// The count the scripts word "many" with, replaced by the real one. +pub const COUNT_MARK: &str = "999999"; + +impl Default for Words { + fn default() -> Self { + Self { + one: "1 chat is still working.".into(), + other: format!("{COUNT_MARK} chats are still working."), + detail: "Keep running in the background until they finish, or stop them now?".into(), + keep: "Keep Running".into(), + stop: "Stop Them Now".into(), + } + } +} + +impl Words { + pub fn message(&self, working: usize) -> String { + if working == 1 { + self.one.clone() + } else { + // (However the scripts' number formatting grouped it.) + ["999,999", "999\u{a0}999", "999\u{202f}999", "999 999", "999.999", COUNT_MARK] + .iter() + .fold(self.other.clone(), |text, mark| text.replace(mark, &working.to_string())) + } + } +} + +thread_local! { + static WORDS: RefCell = RefCell::new(Words::default()); +} + +/// `app.quit_words`: the scripts' words for the question. +pub fn install(host: &zgx_script::ScriptHost) { + host.action("app.quit_words", |args| { + let field = |key: &str| { + args.field(key) + .map(crate::auth::text) + .filter(|text| !text.is_empty()) + }; + WORDS.with(|words| { + let mut words = words.borrow_mut(); + let defaults = Words::default(); + *words = Words { + one: field("one").unwrap_or(defaults.one), + other: field("other").unwrap_or(defaults.other), + detail: field("detail").unwrap_or(defaults.detail), + keep: field("keep").unwrap_or(defaults.keep), + stop: field("stop").unwrap_or(defaults.stop), + }; + }); + }); +} + +/// What the person chose for work still running. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Choice { + KeepRunning, + StopNow, +} + +/// What to tell the engine, given what it said: nothing (it keeps running), +/// or `AppQuitting` with whether to stop the work — asking the person +/// only when there's work to stop. +pub fn decide(check: &Json, ask: impl FnOnce(usize) -> Choice) -> Option { + if check["keepRunning"].as_bool() != Some(false) { + return None; + } + let working = check["working"].as_u64().unwrap_or(0) as usize; + let stop_work = working > 0 && ask(working) == Choice::StopNow; + Some(json!({ "stopWork": stop_work })) +} + +/// After the app's windows have closed, as it quits. +pub fn after_quit() { + let Some((port, data_dir)) = crate::engine::get().ipc_endpoint() else { + return; + }; + let call = |method: &str, params: Json| { + crate::engine::rpc::call_blocking(port, &data_dir, method, params, Duration::from_secs(5)) + }; + let check = match call("QuitCheck", json!({})) { + Ok(check) => check, + // No engine, or one too old to know the setting: it runs on. + Err(error) => { + eprintln!("zeron-zgx: quit: the engine didn't say what's running ({error}); it keeps running"); + return; + } + }; + let words = WORDS.with(|words| words.borrow().clone()); + let Some(quitting) = decide(&check, |working| ask(&words, working)) else { + return; + }; + if let Err(error) = call("AppQuitting", quitting) { + eprintln!("zeron-zgx: quit: couldn't tell the engine ({error}); it keeps running"); + } +} + +/// The question, as a native alert (macOS). Elsewhere work keeps running +/// until it finishes: nothing is stopped without asking. +#[cfg(target_os = "macos")] +fn ask(words: &Words, working: usize) -> Choice { + use objc::runtime::Object; + use objc::{class, msg_send, sel, sel_impl}; + let string = |text: &str| -> *mut Object { + let text = std::ffi::CString::new(text.replace('\0', "")).unwrap_or_default(); + // SAFETY: a NUL-terminated UTF-8 string; NSString copies it. + unsafe { msg_send![class!(NSString), stringWithUTF8String: text.as_ptr()] } + }; + // SAFETY: AppKit on the main thread (where the app's run ended), with + // its shared application still there; NSAlert is modal and returns the + // button pressed (1000 for the first). + unsafe { + let alert: *mut Object = msg_send![class!(NSAlert), new]; + let _: () = msg_send![alert, setMessageText: string(&words.message(working))]; + let _: () = msg_send![alert, setInformativeText: string(&words.detail)]; + let _: *mut Object = msg_send![alert, addButtonWithTitle: string(&words.keep)]; + let _: *mut Object = msg_send![alert, addButtonWithTitle: string(&words.stop)]; + let app: *mut Object = msg_send![class!(NSApplication), sharedApplication]; + let _: () = msg_send![app, activateIgnoringOtherApps: true]; + let pressed: isize = msg_send![alert, runModal]; + let _: () = msg_send![alert, release]; + if pressed == 1001 { Choice::StopNow } else { Choice::KeepRunning } + } +} + +#[cfg(not(target_os = "macos"))] +fn ask(_words: &Words, _working: usize) -> Choice { + Choice::KeepRunning +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn with_the_setting_on_nothing_is_said_or_asked() { + let asked = std::cell::Cell::new(false); + let check = json!({ "keepRunning": true, "working": 3 }); + assert_eq!(decide(&check, |_| { asked.set(true); Choice::StopNow }), None); + assert!(!asked.get()); + // An engine that doesn't say keeps running too. + assert_eq!(decide(&json!({}), |_| Choice::StopNow), None); + } + + #[test] + fn off_with_nothing_running_the_engine_stops_without_asking() { + let check = json!({ "keepRunning": false, "working": 0 }); + let quitting = decide(&check, |_| panic!("asked with nothing running")); + assert_eq!(quitting, Some(json!({ "stopWork": false }))); + } + + #[test] + fn off_with_work_running_the_person_decides() { + let check = json!({ "keepRunning": false, "working": 2 }); + let mut asked_about = 0; + let keep = decide(&check, |n| { asked_about = n; Choice::KeepRunning }); + assert_eq!(asked_about, 2); + assert_eq!(keep, Some(json!({ "stopWork": false }))); + assert_eq!(decide(&check, |_| Choice::StopNow), Some(json!({ "stopWork": true }))); + } + + #[test] + fn the_count_is_written_into_the_message() { + let words = Words::default(); + assert_eq!(words.message(1), "1 chat is still working."); + assert_eq!(words.message(4), "4 chats are still working."); + let grouped = Words { other: "999,999 chats are still working.".into(), ..Words::default() }; + assert_eq!(grouped.message(12), "12 chats are still working."); + } +} diff --git a/apps/zgx/src/relaunch.rs b/apps/zgx/src/relaunch.rs index 503f84117..956b59618 100644 --- a/apps/zgx/src/relaunch.rs +++ b/apps/zgx/src/relaunch.rs @@ -74,6 +74,11 @@ pub fn ask(quit: impl FnOnce()) { quit(); } +/// Whether this quit is a relaunch (the app comes straight back). +pub fn asked() -> bool { + ASKED.load(Ordering::SeqCst) +} + /// Last in `main`, once the app has quit: the new process, if a relaunch /// was asked for. pub fn after_quit() { diff --git a/apps/zgx/src/settings.rs b/apps/zgx/src/settings.rs index e68d8baac..85903c1cf 100644 --- a/apps/zgx/src/settings.rs +++ b/apps/zgx/src/settings.rs @@ -94,6 +94,9 @@ const ENGINE_METHODS: &[&str] = &[ "SetTitleSettings", "GetNativeSubagents", "SetNativeSubagents", + // "Keep Zeron running after you quit" (src/quit_prompt.rs). + "GetBackgroundSettings", + "SetBackgroundSettings", // Whether this window has the keys: banners set to "only when in the // background" hold while it does. "SetViewportFocus", @@ -316,6 +319,7 @@ pub fn install(_cx: &mut WindowContext, host: &ScriptHost) { forget_wallpaper(&path, &dir.join(WALLPAPER_DIR)); } }); + crate::quit_prompt::install(host); host.async_action("settings.engine", |args| { let method = args.field("method").map(text).unwrap_or_default(); let params = args.field("params").map(value_json).unwrap_or(json!({})); diff --git a/apps/zgx/src/shortcut_tests.rs b/apps/zgx/src/shortcut_tests.rs index bf18a0579..4fce8ead2 100644 --- a/apps/zgx/src/shortcut_tests.rs +++ b/apps/zgx/src/shortcut_tests.rs @@ -219,6 +219,74 @@ fn the_shortcuts_reach_their_commands_from_any_focus() { assert!(wrong.is_empty(), "{}", wrong.join("\n")); } +/// ⌘N's new chat is ready in the frame it first shows (ademark's A3: ⌘N, +/// then a key every 50 ms, timed to the first frame the keys read in the +/// new chat's composer, with the old chat gone). From a session whose +/// composer has the keys, what's typed right after ⌘N, with no frame +/// between, is the new chat's draft, not the session's; that first frame +/// shows the new chat, the session's page gone, its composer already where +/// it rests (it snaps there: nothing glides into place). From focus +/// anywhere else, the first frame gives the composer the keys. +#[test] +fn keys_typed_right_after_command_n_land_in_the_new_chat_in_its_first_frame() { + let field = "#chat-composer >> role:textbox"; + let draft = |w: &mut Window| -> String { + let node = w.app.find(field).expect("the composer's field"); + let editor = w.app.ui.editor_within(node).expect("its editor"); + editor.editor.borrow().text().to_string() + }; + let composer = |w: &mut Window| -> zgx::scene::Rect { + let node = w.app.find("#chat-composer").expect("the composer"); + w.app.ui.scene.borrow().transformed_bounds(node) + }; + let mut w = mount(); + w.app.ui.scene.borrow_mut().resize(1600., 1000.); + w.app.click("#shell-sidebar >> role:button \"Fix the build\""); + w.app.frames(30); + let session = w.route(); + for at in [Focus::Composer, Focus::Nothing, Focus::Sidebar] { + if w.route() != session { + w.app.click("#shell-sidebar >> role:button \"Fix the build\""); + w.app.frames(30); + } + focus(&mut w, at); + assert_eq!(draft(&mut w), "", "{at:?}: the session's draft starts empty"); + // ⌘N as the menu runs it, then the first keys: no frame between + // for a composer that had the keys, the first frame for any other + // focus. + w.heard.borrow_mut().clear(); + w.app.keys("cmd+n"); + let heard: Vec = w.heard.borrow_mut().drain(..).collect(); + assert_eq!(heard, ["shell.new_chat"], "{at:?}: ⌘N reached the new chat"); + assert!(w.app.host.run_command("shell.new_chat")); + if !matches!(at, Focus::Composer) { + w.app.frame(); + } + w.app.ui.dispatch(InputEvent::Text("ad".into())); + w.app.settle(); + w.app.frame(); + assert_eq!(w.route(), "/", "{at:?}: a new chat"); + assert!(w.app.find("#chat-session").is_none(), "{at:?}: the session's page is gone in the first frame"); + assert!(w.app.find("#chat-home").is_some(), "{at:?}: the new chat's page shows in the first frame"); + assert_eq!(draft(&mut w), "ad", "{at:?}: the keys are the new chat's draft"); + let first = composer(&mut w); + w.app.frames(30); + let rest = composer(&mut w); + assert!( + (first.y - rest.y).abs() < 0.5 && (first.height - rest.height).abs() < 0.5 && (first.x - rest.x).abs() < 0.5, + "{at:?}: the composer rests where the first frame put it: {first:?}, then {rest:?}" + ); + // (The draft goes, so the next new chat starts empty.) + w.app.click(field); + w.app.keys("cmd+a"); + w.app.key(zgx::input::Key::Backspace); + w.app.frames(10); + w.app.click("#shell-sidebar >> role:button \"Fix the build\""); + w.app.frames(30); + assert_eq!(draft(&mut w), "", "{at:?}: nothing typed reached the session's draft"); + } +} + /// Alt+←/→ step the history on Windows and Linux (zgx's router binds them /// there, ⌘[ ⌘] on macOS, where ⌥← ⌥→ are the caret's word-jump and must /// not): from anywhere, text fields included, as a browser's are (the diff --git a/apps/zgx/src/transcript_paint_tests.rs b/apps/zgx/src/transcript_paint_tests.rs index e29774ed1..a78157c3d 100644 --- a/apps/zgx/src/transcript_paint_tests.rs +++ b/apps/zgx/src/transcript_paint_tests.rs @@ -153,6 +153,14 @@ fn a_long_chat_come_back_to_mid_scroll_draws_what_a_whole_repaint_draws() { } } +/// One reply of a thousand steps (ademark's A11 thread), its blocks windowed +/// in runs: built as they come near, held as boxes away. +#[test] +fn a_long_thread_opened_and_scrolled_draws_what_a_whole_repaint_draws() { + let misses = drawn_against_whole("long_thread"); + assert!(misses.is_empty(), "{}", misses.join("\n")); +} + #[test] fn a_long_chat_of_other_chats_messages_opened_and_scrolled_draws_what_a_whole_repaint_draws() { let misses = drawn_against_whole("long_chat_cross"); diff --git a/apps/zgx/src/transcript_scroll_tests.rs b/apps/zgx/src/transcript_scroll_tests.rs index ad6910e26..ad5ad0b37 100644 --- a/apps/zgx/src/transcript_scroll_tests.rs +++ b/apps/zgx/src/transcript_scroll_tests.rs @@ -18,7 +18,7 @@ fn ui() -> PathBuf { } /// Each text in the transcript's view, by its words: its top. -fn shown(app: &mut TestApp) -> Vec<(String, f32)> { +pub(crate) fn shown(app: &mut TestApp) -> Vec<(String, f32)> { let view = app.bounds("transcript"); let tree = app.tree(); let mut out = Vec::new(); @@ -47,7 +47,7 @@ fn shown(app: &mut TestApp) -> Vec<(String, f32)> { /// How far what shows moved between two looks (the median of the texts seen /// in both), and how many were seen in both. -fn moved(before: &[(String, f32)], after: &[(String, f32)]) -> (Option, usize) { +pub(crate) fn moved(before: &[(String, f32)], after: &[(String, f32)]) -> (Option, usize) { let mut deltas: Vec = before .iter() .filter_map(|(words, y)| after.iter().find(|(w, _)| w == words).map(|(_, y2)| y2 - y)) diff --git a/apps/zgx/ui/chat_fixture.luau b/apps/zgx/ui/chat_fixture.luau index 95a481f9a..3d4dfc15b 100644 --- a/apps/zgx/ui/chat_fixture.luau +++ b/apps/zgx/ui/chat_fixture.luau @@ -1643,6 +1643,49 @@ ui.command { } end, } +-- `long_thread`: ademark's A11 thread, one reply of a thousand steps, each a +-- short text (one in five with a code block) and a call reading a file, its +-- last text holding `LONGTHREAD-END` (the engine's mock `/longthread`), as +-- Tall replies (s7). +local function long_thread(steps: number): { any } + local blocks: { any } = {} + for n = 1, steps do + local text = `Step {n} of {steps}. I read src/invoices.ts and noted what step {n} needs from it.` + if n % 5 == 0 then + text ..= "\n\n```ts\nfunction summarize(invoice: Invoice): InvoiceSummary {\n const total = invoiceTotal(invoice);\n return { id: invoice.id, totalCents: total };\n}\n```" + end + table.insert(blocks, { id = `t{n}`, kind = "text", text = text }) + table.insert(blocks, { + id = `g:toolu_{n}`, + kind = "tools", + summary = "Read 1 file", + live = false, + tools = { { id = `toolu_{n}`, kind = "read", verb = "Read", target = "invoices.ts", call = "src/invoices.ts" } }, + }) + end + table.insert(blocks, { id = "tend", kind = "text", text = "All steps done. LONGTHREAD-END" }) + local texts = {} + for _, b in blocks do + if b.kind == "text" then + table.insert(texts, b.text) + end + end + local whole = table.concat(texts, "\n\n") + return { + { id = "lt1", role = "user", text = `/longthread {steps}` }, + { id = "lt2", role = "assistant", text = whole, model = "Opus", at = "Nov 13, 9:00 PM", copy = whole, blocks = blocks }, + } +end +ui.command { + id = "long_thread", + title = "A thread of a thousand steps", + run = function() + TRANSCRIPTS.s7 = long_thread(1000) + if store.session:peek() and (store.session:peek() :: any).id == "s7" then + store.messages:set(TRANSCRIPTS.s7) + end + end, +} -- A long chat: many long messages, landing at once (a chat of a day's -- work), for what the transcript asks of each as it's listed and scrolled. ui.command { diff --git a/apps/zgx/ui/ds/composer.luau b/apps/zgx/ui/ds/composer.luau index 7b876dd88..6c602e7d7 100644 --- a/apps/zgx/ui/ds/composer.luau +++ b/apps/zgx/ui/ds/composer.luau @@ -55,6 +55,7 @@ local locale = require("@self/locale") local VoiceWave = require("@self/voice_wave").VoiceWave local context_usage = require("@self/context_usage") local TextMetrics = require("@self/composer_metrics") +local Draft = require("@self/draft_text") local Layout = require("@self/composer_layout") local ComposerMotion = require("@self/composer_motion") local EditorMenu = require("@self/editor_menu") @@ -1000,12 +1001,14 @@ return ui.component(function(props: Props): Node -- Past the cap a draft is measured no more: its size changes nothing -- (Layout.estimate). The type's line height changed (tracked), it is -- measured again. - estimate = function(text: string, line_height: number): Layout.Sample? + estimate = function(line_height: number): Layout.Sample? local line = ui.theme.get("zeron.composer.line") if type(line) == "number" and math.abs(line - line_height) > 0.01 then return nil end - return Layout.estimate(text, line_height) + -- Its line breaks counted where it's held, up to the cap's. + local capped = Layout.capped_lines(line_height) + return Layout.estimate_lines(1 + #value:find("\n", { limit = capped - 1 }), line_height) end, } local head_h_seen = kept("head_h", 0) @@ -1046,7 +1049,7 @@ return ui.component(function(props: Props): Node end local function has_text(): boolean - return string.find(value:get(), "%S") ~= nil + return Draft.has_text(value) end local function disabled(): boolean return read(props.disabled) == true @@ -1176,7 +1179,7 @@ return ui.component(function(props: Props): Node cargo = has_cargo(), asking = asking(), dictating = dictating(), - multiline = string.find(value:get(), "\n") ~= nil, + multiline = Draft.multiline(value), } end local target_geometry = ui.use_state(Layout.calculate(layout_input(), previous_text_expanded:peek())) diff --git a/apps/zgx/ui/ds/composer_layout.luau b/apps/zgx/ui/ds/composer_layout.luau index a6698dca0..53c0f8e51 100644 --- a/apps/zgx/ui/ds/composer_layout.luau +++ b/apps/zgx/ui/ds/composer_layout.luau @@ -59,7 +59,7 @@ end -- wraps to. Measuring a long draft walks every line at each keystroke; this -- looks for at most a dozen newlines. Nil: the draft must be measured. function M.estimate(text: string, line_height: number): Sample? - local need = math.ceil((TEXT_MAX - TEXT_TOP - TEXT_BOTTOM) / math.max(1, line_height)) + local need = M.capped_lines(line_height) local lines, at = 1, 1 while lines < need do local newline = string.find(text, "\n", at, true) @@ -69,7 +69,22 @@ function M.estimate(text: string, line_height: number): Sample? lines += 1 at = newline + 1 end - return { unwrapped_width = 0, wrapped_height = lines * line_height, line_height = line_height } + return M.estimate_lines(lines, line_height) +end + +-- How many hard lines fill the text box to its cap at `line_height`. +function M.capped_lines(line_height: number): number + return math.ceil((TEXT_MAX - TEXT_TOP - TEXT_BOTTOM) / math.max(1, line_height)) +end + +-- `estimate` for a draft of `lines` hard lines (counted up to +-- `capped_lines`, where it's held: the draft itself never read for it). +function M.estimate_lines(lines: number, line_height: number): Sample? + local need = M.capped_lines(line_height) + if lines < need then + return nil + end + return { unwrapped_width = 0, wrapped_height = need * line_height, line_height = line_height } end -- Whether two targets lay the composer out alike: every field a view binds diff --git a/apps/zgx/ui/ds/composer_mentions.luau b/apps/zgx/ui/ds/composer_mentions.luau index b58078b30..6556741bd 100644 --- a/apps/zgx/ui/ds/composer_mentions.luau +++ b/apps/zgx/ui/ds/composer_mentions.luau @@ -37,6 +37,7 @@ local M = {} M.debug = false local tokens = require("@self/mention_tokens") +local Draft = require("@self/draft_text") local locale = require("@self/locale") export type Trigger = tokens.Trigger @@ -103,6 +104,10 @@ function M.use(opts: Use) -- The token whose list was put away (Escape, a click away from it), by -- where it starts, for each draft (`key`): that token's alone, see below. local dismissed = ui.use_state({} :: { [any]: number }) + -- The draft as it was when its list was put away, and at each keystroke + -- while it stays away (nil otherwise): whether its trigger was typed + -- again reads it then alone, not a long draft at every keystroke. + local was_text: string? = nil local function key_now(): any return if opts.key then opts.key() else "" end @@ -112,6 +117,9 @@ function M.use(opts: Use) end local function dismiss(key: any, from: number?) local all = dismissed:peek() + if from ~= nil then + was_text = opts.value:peek() + end if all[key] ~= from then local next = table.clone(all) next[key] = from @@ -120,7 +128,7 @@ function M.use(opts: Use) end local active = ui.use_state(1) local token = ui.computed(function(): Token? - local t = M.token(opts.value:get(), caret:get()) + local t = Draft.token(opts.value, caret:get()) if t and opts.enabled and not opts.enabled(t.trigger) then return nil end @@ -259,21 +267,27 @@ function M.use(opts: Use) -- (moved, the token deleted), or its trigger typed again, and the next -- token's list opens. Typed further at, it stays away. (Another chat's draft shown in -- its place: that draft's own, kept with it.) - local was_text = opts.value:peek() local was_key = key_now() ui.effect(function() local t = token:get() - local text = opts.value:get() + Draft.track(opts.value) local key = key_now() + local moved = key ~= was_key + was_key = key + local gone = dismissed:peek()[key] + if gone == nil then + was_text = nil + return + end + local text = opts.value:get() -- (The draft just as it was with the token's trigger put in: that -- trigger was typed.) local typed = t ~= nil + and was_text ~= nil and #text == #was_text + 1 and string.sub(text, 1, t.from) .. string.sub(text, t.from + 2) == was_text - local moved = key ~= was_key - was_text, was_key = text, key - local gone = dismissed:peek()[key] - if gone == nil or moved then + was_text = text + if moved then return end if t == nil or t.from ~= gone or typed then @@ -285,11 +299,12 @@ function M.use(opts: Use) -- path with it. local refs = ui.use_state({} :: { [string]: string }) ui.effect(function() - local now = opts.value:get() + -- (Each looked for where the draft is held, not copied out of it.) + Draft.track(opts.value) local kept = refs:peek() local stale: { string } = {} for tok in kept do - if not string.find(now, tok, 1, true) then + if #opts.value:find(tok, { limit = 1 }) == 0 then table.insert(stale, tok) end end diff --git a/apps/zgx/ui/ds/composer_metrics.luau b/apps/zgx/ui/ds/composer_metrics.luau index 267c80c96..9446c8e69 100644 --- a/apps/zgx/ui/ds/composer_metrics.luau +++ b/apps/zgx/ui/ds/composer_metrics.luau @@ -12,7 +12,8 @@ export type Sample = { } type Request = { width: number, editor: Element?, source: Sample?, estimate: Size?, revision: number, - text: string, theme: string, mount: number, + -- Which edit of the draft (`edits`), not the draft itself. + edit: number, theme: string, mount: number, } export type Result = { -- A complete last sample, never one axis from a different request. @@ -36,7 +37,7 @@ export type Props = { -- the exact one can't change its layout (a long draft past the cap), -- with the line height last measured; nil to measure. Measuring walks -- every line of the draft, so a long one paid that at every keystroke. - estimate: ((text: string, line_height: number) -> Size?)?, + estimate: ((line_height: number) -> Size?)?, } function M.use(props: Props) @@ -71,10 +72,19 @@ function M.use(props: Props) local s = size:get() return s == nil or s.unwrapped_width >= math.max(0, props.width()) - 1 end) + -- Each edit of the draft, counted: what a request follows edits by. + -- The draft itself was copied into the request (and out again at each + -- read of it): 2 ms of a keystroke in a draft of 30,000 lines. + local edit_count = 0 + local edits = ui.computed(function(): number + props.value:len() + edit_count += 1 + return edit_count + end) local last: Request? = nil local request = ui.computed(function(): Request -- Track edits even when the measured dimensions stay equal. - local text = props.value:get() + local edit = edits:get() local width = math.max(0, if follows:get() then props.width() else props.width(true)) local theme = ui.theme.current:get() local mount = mounted:get() @@ -82,7 +92,7 @@ function M.use(props: Props) local source, estimate = nil, nil if el and width > 0 then if props.estimate and exact and exact.mount == mount.revision and exact.theme == theme then - estimate = props.estimate(text, exact.line_height) + estimate = props.estimate(exact.line_height) end -- Reading follows the editor's settled content/IME/style revision. if estimate == nil then @@ -100,11 +110,11 @@ function M.use(props: Props) return was end revision += 1 - local now: Request = { width = width, editor = el, source = source, estimate = estimate, revision = revision, text = text, theme = theme, mount = mount.revision } + local now: Request = { width = width, editor = el, source = source, estimate = estimate, revision = revision, edit = edit, theme = theme, mount = mount.revision } last = now return now end) - local settled: { mount: number, text: string, theme: string, revision: number, width: number }? = nil + local settled: { mount: number, edit: number, theme: string, revision: number, width: number }? = nil local function publish(wanted: Request, source: Size, width: number) ui.batch(function() -- (An equal size notifies nobody: layout wakes only for a new one.) @@ -123,7 +133,7 @@ function M.use(props: Props) end) end local function publish_exact(wanted: Request, source: Sample) - settled = { mount = wanted.mount, text = wanted.text, theme = wanted.theme, + settled = { mount = wanted.mount, edit = wanted.edit, theme = wanted.theme, revision = source.revision, width = wanted.width } exact = { mount = wanted.mount, theme = wanted.theme, line_height = source.line_height } publish(wanted, source, source.width) @@ -149,7 +159,7 @@ function M.use(props: Props) -- round-trip for every frame of a page resize. Content, IME, style, -- editor replacement and initial requests still need acknowledgement. if settled and settled.width ~= wanted.width and settled.mount == wanted.mount - and settled.text == wanted.text and settled.theme == wanted.theme + and settled.edit == wanted.edit and settled.theme == wanted.theme and settled.revision == wanted.source.revision then publish_exact(wanted, wanted.source) return diff --git a/apps/zgx/ui/ds/draft_text.luau b/apps/zgx/ui/ds/draft_text.luau new file mode 100644 index 000000000..1c029e79a --- /dev/null +++ b/apps/zgx/ui/ds/draft_text.luau @@ -0,0 +1,99 @@ +-- A draft's text read where it's held (its state), not copied whole into the +-- script: what the composer's bindings need of it at every keystroke. +-- +-- A state's `get` makes the whole draft a script string, and a dozen of the +-- composer's bindings each did at every keystroke: a key in a draft of +-- 30,000 lines cost 5 ms in copies alone. These read only the part they +-- need (zgx's `state:sub`, `state:len`, `state:find`), so a key costs the +-- same in a draft of 20 lines or 30,000. A zgx without `sub` reads the whole +-- draft, as before. +-- +-- draft_text.track(s) follow its edits (nothing read) +-- draft_text.size(s) its length in bytes +-- draft_text.sub(s, i, j?) `string.sub` of it +-- draft_text.has_text(s) whether it holds anything but blanks +-- draft_text.multiline(s) whether it has a line break +-- draft_text.token(s, caret) the mention token the caret is in +-- (ds/mention_tokens), or nil +-- draft_text.end_token(s) the one at its end + +local tokens = require("@self/mention_tokens") + +local M = {} + +-- Whether states read in place (`sub`, and `len` a string's length). +local in_place: boolean? = nil +local function native(s: any): boolean + if in_place == nil then + in_place = pcall(function() + return s:sub(1, 0) + end) + end + return in_place :: boolean +end + +-- Follows its edits, as `get` does, without reading it. +function M.track(s: any) + s:len() +end + +function M.size(s: any): number + return if native(s) then s:len() else #(s:get() :: string) +end + +function M.sub(s: any, i: number, j: number?): string + return if native(s) then s:sub(i, j) else string.sub(s:get(), i, j) +end + +-- How much of its start shows whether it has text: past that many blanks, +-- the rest is read. +local HEAD = 256 + +function M.has_text(s: any): boolean + if not native(s) then + return string.find(s:get(), "%S") ~= nil + end + if string.find(s:sub(1, HEAD), "%S") then + return true + end + return s:len() > HEAD and string.find(s:get(), "%S") ~= nil +end + +function M.multiline(s: any): boolean + return #s:find("\n", { limit = 1 }) > 0 +end + +-- How far either side of the caret a token is looked for first: none runs +-- this long (ds/mention_tokens' reach). +local NEAR = 1024 + +-- The token at `caret` (bytes from 0): looked for around the caret, and only +-- where there is one, in the whole draft (whether a fence above holds it). +-- The look around can't tell a fence's parity, though: a closed block +-- opened more than `NEAR` above shows it only its closing fence, which +-- reads as one still open. Where it found nothing with a fence before the +-- caret, the whole draft decides. +function M.token(s: any, caret: number): tokens.Token? + if not native(s) then + return tokens.token(s:get(), caret) + end + local lo = math.max(1, caret - NEAR + 1) + local near = s:sub(lo, caret + NEAR) + if tokens.token(near, caret - lo + 1) == nil then + local fence = string.find(near, "```", 1, true) + if fence == nil or fence > caret - lo + 1 then + return nil + end + end + return tokens.token(s:get(), caret) +end + +function M.end_token(s: any): tokens.Token? + if not native(s) then + local text: string = s:get() + return tokens.token(text, #text) + end + return M.token(s, s:len()) +end + +return M diff --git a/apps/zgx/ui/ds/markdown_edit.luau b/apps/zgx/ui/ds/markdown_edit.luau index 9e04face3..c47933d2f 100644 --- a/apps/zgx/ui/ds/markdown_edit.luau +++ b/apps/zgx/ui/ds/markdown_edit.luau @@ -238,6 +238,35 @@ function M.highlight(line: string, state: number): ({ { any } }, number) return runs(marks), 0 end +-- How many of the lines in the first `upto` bytes of `text` start with +-- `fence` after blanks (`%s`): found with a plain search, which skips the +-- lines between them rather than matching each. +local function fences(text: string, upto: number, fence: string): number + local count, from = 0, 1 + while true do + local s = string.find(text, fence, from, true) + if s == nil or s + #fence - 1 > upto then + return count + end + local i = s - 1 + while i >= 1 do + local c = string.byte(text, i) + if c == 10 or (c ~= 32 and (c < 9 or c > 13)) then + break + end + i -= 1 + end + if i < 1 or string.byte(text, i) == 10 then + count += 1 + end + local nl = string.find(text, "\n", s + #fence, true) + if nl == nil then + return count + end + from = nl + 1 + end +end + -- What a line break does at the selection `a`..`b` (bytes from 0) in a -- list item: `(from, to, text)` to put in place of `from`..`to`, or nil -- for a plain break. @@ -245,34 +274,33 @@ function M.newline(text: string, a: number, b: number): (number?, number?, strin if a ~= b then return nil, nil, nil end - -- The caret's line, in bytes from 0. + -- The caret's line, in bytes from 0: its start looked for back from the + -- caret a window at a time, not from the draft's top (a break in a + -- draft of 30,000 lines walked them all, then matched them all for + -- fences, 20 ms of an Enter). local start = 0 - local i = 0 - while true do - local nl = string.find(text, "\n", i + 1, true) - if not nl or nl > a then + local hi = a + while hi > 0 do + local lo = math.max(1, hi - 1023) + local after = string.match(string.sub(text, lo, hi), "^.*\n()") + if after then + start = lo + after - 2 break end - start = nl - i = nl + hi = lo - 1 end local stop = string.find(text, "\n", a + 1, true) local line_end = if stop then stop - 1 else #text local line = string.sub(text, start + 1, line_end) - -- Not inside a fenced code block: count the fences above. - local fences = 0 - for fence in string.gmatch(string.sub(text, 1, start), "[^\n]*") do - if string.match(fence, "^%s*```") or string.match(fence, "^%s*~~~") then - fences += 1 - end - end - if fences % 2 == 1 then - return nil, nil, nil - end local p = prefix(line) if not p or a < start + p.stop then return nil, nil, nil end + -- Not inside a fenced code block: count the fences above (lines + -- starting, after blanks, with ``` or ~~~: never both). + if (fences(text, start, "```") + fences(text, start, "~~~")) % 2 == 1 then + return nil, nil, nil + end if string.match(string.sub(line, p.stop + 1), "^%s*$") then -- An empty item: undone, a level out. return start, line_end, string.sub(p.indent, 1, math.max(0, #p.indent - 2)) diff --git a/apps/zgx/ui/ds/mention_tokens.luau b/apps/zgx/ui/ds/mention_tokens.luau index 5a6ef8ac1..dcf7bb750 100644 --- a/apps/zgx/ui/ds/mention_tokens.luau +++ b/apps/zgx/ui/ds/mention_tokens.luau @@ -31,6 +31,45 @@ export type Token = { trigger: Trigger, query: string, from: number, to: number -- What a token is made of after its trigger. local WORD = "[%w%./_:%-]" +-- How far from the caret a token is looked for, each way. A token never +-- crosses a line and none runs this long, so only the caret's line, this +-- much of it at most, is read: a keystroke in a draft of 30,000 lines costs +-- what one in a draft of a line does. (The whole draft was copied and +-- matched against at every keystroke: 9 ms of each, at 30,000 lines.) +local REACH = 1024 + +-- Whether the `upto` bytes of `text` leave it in a fenced block: an odd +-- number of its lines start with ``` (after blanks). Found with a plain +-- search, which skips the lines between fences rather than matching each. +local function fenced(text: string, upto: number): boolean + local fences, from = 0, 1 + while true do + local s = string.find(text, "```", from, true) + if s == nil or s + 2 > upto then + break + end + -- Only blanks (`%s`) between its line's start and it. + local i = s - 1 + while i >= 1 do + local c = string.byte(text, i) + if c == 10 or (c ~= 32 and (c < 9 or c > 13)) then + break + end + i -= 1 + end + if i < 1 or string.byte(text, i) == 10 then + fences += 1 + end + -- (A line counts once, however many it holds.) + local nl = string.find(text, "\n", s + 3, true) + if nl == nil then + break + end + from = nl + 1 + end + return fences % 2 == 1 +end + -- The token the caret is in, if any: its trigger, what has been typed -- before the caret (the query), and where the whole token runs, to its -- end past the caret too, so a pick replaces all of it (a caret in the @@ -38,7 +77,13 @@ local WORD = "[%w%./_:%-]" -- trigger starts a token only at a start, after a space, or after an -- opening bracket. function M.token(text: string, caret: number): Token? - local before = string.sub(text, 1, caret) + -- The caret's line before it (`REACH` of it at most), which starts + -- `base` bytes into the text. + local lo = math.max(1, caret - REACH + 1) + local near = string.sub(text, lo, caret) + local line = string.match(near, "^.*\n()") + local base = lo - 1 + (if line then line - 1 else 0) + local before = if line then string.sub(near, line) else near local function starts(at: number): boolean local prev = if at > 1 then string.sub(before, at - 1, at - 1) else "" return prev == "" or string.match(prev, "[%s%(%[{>]") ~= nil @@ -46,19 +91,9 @@ function M.token(text: string, caret: number): Token? -- Where the token ends: over its characters after the caret (a -- command's never cross a slash). local function ends(chars: string): number - local rest = string.match(string.sub(text, caret + 1), "^" .. chars .. "*") or "" + local rest = string.match(string.sub(text, caret + 1, caret + REACH), "^" .. chars .. "*") or "" return caret + #rest end - -- Inside a fenced block, nothing. - local function fenced(at: number): boolean - local fences = 0 - for line in string.gmatch(string.sub(text, 1, at), "[^\n]*") do - if string.match(line, "^%s*```") then - fences += 1 - end - end - return fences % 2 == 1 - end -- `@` and `$`: back over the token's characters (a path's slashes among -- them) to the trigger. local at = #before @@ -74,19 +109,20 @@ function M.token(text: string, caret: number): Token? local query = string.sub(before, at + 1) local to = ends(WORD) -- "$12" is money, wherever the caret sits in it. - if trigger == "$" and string.match(string.sub(text, at + 1, to), "^%d") then + if trigger == "$" and string.match(string.sub(text, base + at + 1, to), "^%d") then return nil end - if fenced(at) then + -- Inside a fenced block, nothing. + if fenced(text, base + at) then return nil end - return { trigger = trigger :: Trigger, query = query, from = at - 1, to = to } + return { trigger = trigger :: Trigger, query = query, from = base + at - 1, to = to } end -- `/`: the run after the last slash, where that slash starts a word (a -- path's slashes never do), with no slash in the run. local s = string.find(before, "/[%w%._:%-]*$") - if s and starts(s) and not fenced(s) then - return { trigger = "/", query = string.sub(before, s + 1), from = s - 1, to = ends("[%w%._:%-]") } + if s and starts(s) and not fenced(text, base + s) then + return { trigger = "/", query = string.sub(before, s + 1), from = base + s - 1, to = ends("[%w%._:%-]") } end return nil end diff --git a/apps/zgx/ui/ds/transcript.luau b/apps/zgx/ui/ds/transcript.luau index 05723704d..fa94a99a6 100644 --- a/apps/zgx/ui/ds/transcript.luau +++ b/apps/zgx/ui/ds/transcript.luau @@ -652,9 +652,103 @@ local function thought_of(m: Message, now: () -> Message, draft: State?): } end +-- A long reply's blocks are windowed, as a long text's are (ds/markdown's +-- `window`): in runs of about RUN blocks, each run built while its reach +-- (the run and RUN_OVERSCAN either side) touches the view; away, a box of its +-- last height (a guess until it has been laid out). A reply of a thousand steps, +-- each a text and a tool (ademark's A11 thread), otherwise built all its +-- blocks' rows, texts and tools, every time it opened, though its end alone +-- shows. Only a reply of more than LONG_REPLY blocks, or one being written. +local RUN = 32 +local RUN_MOST = 64 +local RUN_OVERSCAN = 600 +local LONG_REPLY = 48 +-- How much of a long reply's end is built as it's first made, before the +-- view has said what's near it (its runs' heights, kept or guessed, from its +-- end): the view a transcript opens on, at its end, and RUN_OVERSCAN past +-- it, so the first frame shows its end whole and nothing near it is built a +-- frame later. (A view not laid out yet: about a tall window's.) +local OPENING_VIEW = 1000 +-- What windowed runs keep by their reply and first block (`:`): +-- each one's height as last laid out, and whether it was built when last +-- judged, so a reply opened again holds its runs at their heights and builds +-- the same ones. Plain tables a key at a time (ds/markdown's `kept`, the oldest half +-- going as they fill). +local KEPT_RUNS = 8192 +local function kept_runs(): { get: (string) -> T?, set: (string, T) -> () } + local now: { [string]: T }, older: { [string]: T }, count = {}, {}, 0 + local function set(k: string, value: T) + if now[k] == nil then + if count >= KEPT_RUNS then + now, older, count = {}, now, 0 + end + count += 1 + end + now[k] = value + end + return { + get = function(k: string): T? + local value = now[k] + if value == nil then + value = older[k] + if value ~= nil then + set(k, value) + end + end + return value + end, + set = set, + } +end +local RUN_HEIGHTS = kept_runs() :: { get: (string) -> number?, set: (string, number) -> () } +local RUN_NEAR = kept_runs() :: { get: (string) -> boolean?, set: (string, boolean) -> () } + +-- About how tall a block will be before it's laid out: a text's lines at a +-- reply's width (about 85 characters a line) and its code blocks by their +-- lines (ds/markdown's `estimate`), a group of tools its summary's line. +local function guess_block(b: Block): number + if b.kind == "text" then + local h, chars, code, fenced = 0, 0, 0, false + local function paragraph() + if chars > 0 then + h += math.ceil(chars / 85) * 24 + 16 + chars = 0 + end + end + for line in string.gmatch((b.text or "") .. "\n", "(.-)\n") do + if string.match(line, "^%s*```") then + if fenced then + h += code * 19 + 56 + 16 + code = 0 + else + paragraph() + end + fenced = not fenced + elseif fenced then + code += 1 + elseif string.match(line, "^%s*$") then + paragraph() + else + chars += #line + 1 + end + end + paragraph() + return math.max(24, h - 16) + elseif b.kind == "tools" or b.kind == "work" then + return if b.live then 26 * math.max(1, #(b.tools or {})) else 26 + end + return 240 +end +local function guess_run(blocks: { Block }): number + local h = 0 + for _, b in blocks do + h += guess_block(b) + 12 + end + return math.max(0, h - 12) +end + local function blocks_of(list: () -> { Block }, m: Message, now: () -> Message, draft: State?, view: View?): Node - local shown = ui.computed(list) - return ui.each(shown, function(b: Block, _i: number, brow: State): Node + local function block(b: Block, brow: State): Node if b.kind == "tools" or b.kind == "work" then return Activity { key = `{m.id}:{b.id}`, @@ -767,9 +861,219 @@ local function blocks_of(list: () -> { Block }, m: Message, now: () -> Message, find = if view and view.find then view.find(`{m.id}:{b.id}`) else nil, group = `reply:{m.id}`, } + end + local function key(b: Block): string + return b.id + end + -- (A reply being written may grow long as it's watched: windowed from + -- the start, one run while it's short.) + if not view or (#(m.blocks or {}) <= LONG_REPLY and not m.streaming) then + return ui.each(ui.computed(list), function(b: Block, _i: number, brow: State): Node + return block(b, brow) + end, { key = key, w_full = true, gap = "$space.3" }) + end + local w = view :: View + -- Its runs. Where one ends is said by its blocks' ids alone (a block + -- whose id falls on RUN starts a run; none runs past RUN_MOST), so blocks + -- coming before or after (the engine's opening tail, then the whole reply; + -- a reply being written) leave the runs they don't reach as they were. + -- Each is kept (its very table) while its blocks are the same ones, so + -- only a run whose blocks changed is looked at again; and keyed by its + -- first block. + local starts: { [string]: boolean } = {} + local function starts_run(id: string): boolean + local yes = starts[id] + if yes == nil then + local h = 0 + for i = 1, #id do + h = (h * 31 + string.byte(id, i)) % 1000003 + end + yes = h % RUN == 0 + starts[id] = yes + end + return yes + end + type Run = { key: string, blocks: { Block } } + local made: { [string]: Run } = {} + -- The runs within the opening's reach of its end (by their heights, kept or + -- guessed), as they last stood: one made there is built from the start. + local reach: { [string]: boolean } = {} + local runs = ui.computed(function(): { Run } + local all = list() + local out: { Run } = {} + local now_made: { [string]: Run } = {} + local function take(from: number, to: number) + local first = all[from].id + local was = made[first] + local same = was ~= nil and #was.blocks == to - from + 1 + if same then + for i = from, to do + if (was :: Run).blocks[i - from + 1] ~= all[i] then + same = false + break + end + end + end + local run = if same then was :: Run else { key = first, blocks = table.move(all, from, to, 1, {}) } + table.insert(out, run) + now_made[first] = run + end + local from = 1 + for i = 2, #all do + if i - from >= RUN_MOST or starts_run(all[i].id) then + take(from, i - 1) + from = i + end + end + if #all >= from then + take(from, #all) + end + made = now_made + reach = {} + local list_el = w.view:peek() + local opening = (if list_el and list_el.height and list_el.height > 0 then list_el.height else OPENING_VIEW) + RUN_OVERSCAN + local h = 0 + for i = #out, 1, -1 do + if h > opening then + break + end + reach[out[i].key] = true + h += RUN_HEIGHTS.get(`{m.id}:{out[i].key}`) or guess_run(out[i].blocks) + end + return out + end) + -- A find's current match, if it's in this reply: the key of its text. + local found = if w.find then w.find("") else nil + local function current_key(): string? + local c = if found then found.current() else nil + return if c then c.key else nil + end + -- Each run's judge, by its key: all judged again as what holds one + -- built changes (a find's current match moving, the reply's writing + -- ending); each judges itself as it comes near the view or goes. + local judges: { [string]: () -> () } = {} + ui.effect(function() + current_key() + local _ = now().streaming + for _, judge in judges do + judge() + end + end) + return ui.each(runs, function(run: Run, _i: number, row: State): Node + local k = `{m.id}:{run.key}` + local was_near = RUN_NEAR.get(k) + -- Built if it was when last judged (a reply opened again where it was + -- read), and if it's near the reply's end (where a transcript opens, + -- and where a reply being written grows); else held at its height + -- until the view says it's near. + local near = ui.use_state(was_near == true or reach[run.key] == true) + local held = ui.use_state(RUN_HEIGHTS.get(k) or guess_run(run.blocks)) + local r = ui.ref() + -- Built again after being held: its first layout makes up the + -- difference to the height it was held at, as ds/markdown's runs do. + local returning = false + -- Near the view, as zgx says (`on_visible`, its box grown by + -- `visible_margin`): nil until its first layout. + local seen: boolean? = nil + local function judge() + if seen == nil then + return + end + -- (Always built: the last run of a reply being written, where its + -- words arrive; and the run holding a find's current match.) + local all = runs:peek() + local live = now().streaming == true and #all > 0 and all[#all].key == run.key + local c = current_key() + local holds = false + if c then + for _, b in row:peek().blocks do + if c == `{m.id}:{b.id}` then + holds = true + break + end + end + end + local shows = live or holds or seen :: boolean + if RUN_NEAR.get(k) ~= shows then + RUN_NEAR.set(k, shows) + end + if near:peek() ~= shows then + returning = shows + near:set(shows) + end + end + judges[run.key] = judge + ui.on_cleanup(function() + if judges[run.key] == judge then + judges[run.key] = nil + end + end) + return ui.div { + ref = r, + w_full = true, + min_w = 0, + visible_margin = RUN_OVERSCAN, + on_visible = function(visible: boolean) + -- (Built as it was made, and not near at its first layout: kept + -- until the view has been near it and gone. Let go of at once, + -- it took a frame of its own after the one the reply opened in.) + if seen == nil and not visible and near:peek() then + seen = false + return + end + seen = visible + judge() + end, + on_resize = function(size: { w: number, h: number }) + local before = held:peek() + local back = returning + returning = false + local last_height = RUN_HEIGHTS.get(k) + if last_height == nil or math.abs(size.h - last_height) >= 0.5 then + RUN_HEIGHTS.set(k, size.h) + end + if math.abs(size.h - before) < 0.5 then + return + end + held:set(size.h) + if not back or (w.at_end and w.at_end:peek()) then + return + end + -- Above the view's top (it came in from there): the view moves + -- with it, so what shows doesn't (ds/markdown's runs, the same). + local el, list_el = r:peek(), w.view:peek() + local outer = if w.row then w.row:peek() else nil + local row_above = outer ~= nil + and list_el ~= nil + and outer.y ~= nil + and outer.height ~= nil + and list_el.y ~= nil + and (outer.y :: number) + (outer.height :: number) <= (list_el.y :: number) + if el and list_el and el.y and list_el.y and (el.y :: number) < (list_el.y :: number) and not row_above then + w.offset:set(math.max(0, w.offset:peek() + size.h - before)) + end + end, + ui.presence(function(): string + return if near:get() then "built" else "held" + end, function(kind: string): Node + if kind == "built" then + return ui.each(ui.computed(function(): { Block } + return row:get().blocks + end), function(b: Block, _j: number, brow: State): Node + return block(b, brow) + end, { key = key, w_full = true, gap = "$space.3" }) + end + return ui.div { + w_full = true, + h = function(): number + return held:get() + end, + } + end, { w_full = true, min_w = 0 }), + } end, { - key = function(b: Block): string - return b.id + key = function(run: Run): string + return run.key end, w_full = true, gap = "$space.3", diff --git a/apps/zgx/ui/locales/en.json b/apps/zgx/ui/locales/en.json index ca649afdb..1c8011483 100644 --- a/apps/zgx/ui/locales/en.json +++ b/apps/zgx/ui/locales/en.json @@ -1182,6 +1182,12 @@ "terms": "co-author coauthor attribution bot commit message", "cloud": "Always on for Cloud", "on": "Agents' commits add zeronsh[bot] as a co-author" + }, + "keep_running": { + "label": "Keep Zeron running after you quit", + "on": "Chats keep working, and other devices can reach them.", + "off": "When you quit, Zeron stops once its chats are done.", + "terms": "background quit close engine agents running stop memory battery" } }, "engine": { @@ -3343,6 +3349,12 @@ "old": "This Zeron needs a newer engine", "old_tip": "Its engine is older than this version needs. Open About & updates to go back to Stable.", "short_old": "Needs a newer engine" + }, + "quit": { + "working": "{n, plural, one {# chat is still working.} other {# chats are still working.}}", + "detail": "Keep running in the background until they finish, or stop them now?", + "keep": "Keep Running", + "stop": "Stop Them Now" } }, "account": { diff --git a/apps/zgx/ui/locales/zh-Hans.json b/apps/zgx/ui/locales/zh-Hans.json index bd31e2390..fdbecad81 100644 --- a/apps/zgx/ui/locales/zh-Hans.json +++ b/apps/zgx/ui/locales/zh-Hans.json @@ -1182,6 +1182,12 @@ "terms": "共同作者 署名 机器人 提交消息 co-author coauthor attribution bot commit message", "cloud": "Cloud 始终启用", "on": "智能体的提交会将 zeronsh[bot] 添加为共同作者" + }, + "keep_running": { + "label": "退出后让 Zeron 继续运行", + "on": "对话会继续进行,其他设备也能访问它们。", + "off": "退出时,Zeron 会在对话完成后停止。", + "terms": "后台 退出 关闭 引擎 智能体 运行 停止 内存 电池 background quit close engine agents" } }, "engine": { @@ -3343,6 +3349,12 @@ "old": "此版本 Zeron 需要更新的引擎", "old_tip": "引擎版本低于此版本的要求。打开“关于与更新”即可回到稳定版。", "short_old": "需要更新的引擎" + }, + "quit": { + "working": "{n, plural, other {还有 # 个对话正在进行。}}", + "detail": "要在后台继续运行直到完成,还是现在停止?", + "keep": "继续运行", + "stop": "立即停止" } }, "account": { diff --git a/apps/zgx/ui/screens/chat/completions.luau b/apps/zgx/ui/screens/chat/completions.luau index 298c9738c..8fe079c64 100644 --- a/apps/zgx/ui/screens/chat/completions.luau +++ b/apps/zgx/ui/screens/chat/completions.luau @@ -20,6 +20,7 @@ local ChatLinks = require("@app/screens/chat/chat_links") local locale = require("@ds/locale") local route = require("@app/screens/shell/route") local mentions = require("@ds/composer_mentions") +local Draft = require("@ds/draft_text") local prefs = require("@app/screens/settings/prefs") local Invocations = require("@app/screens/chat/invocations") local store = require("@app/screens/chat/store") @@ -197,8 +198,8 @@ function M.bind(b: Bind): { [string]: any } -- so a reload with it open reads nothing. local invocations_open = ui.state(`{b.key}_invocations_open`, nil :: string?) ui.effect(function() - local text = b.text:get() - local t = mentions.token(text, #text) + -- (At the draft's end, read there: a long draft isn't copied for it.) + local t = Draft.end_token(b.text) local wanted = t ~= nil and (t.trigger == "/" or (t.trigger == "$" and prefs.completion(b.agent()).dollar)) local key = if wanted then invocation_key() else nil if key == invocations_open:peek() then @@ -213,8 +214,8 @@ function M.bind(b: Bind): { [string]: any } -- What was last searched for: kept, so a reload doesn't search again. local searched = ui.state(`{b.key}_mention_searched`, nil :: string?) ui.effect(function() - local text = b.text:get() - local t = mentions.token(text, #text) + -- (At the draft's end, read there: a long draft isn't copied for it.) + local t = Draft.end_token(b.text) if not t or t.trigger ~= "@" then return end diff --git a/apps/zgx/ui/screens/chat/composer.luau b/apps/zgx/ui/screens/chat/composer.luau index 10038a95a..d631a537c 100644 --- a/apps/zgx/ui/screens/chat/composer.luau +++ b/apps/zgx/ui/screens/chat/composer.luau @@ -17,6 +17,7 @@ -- it lays itself out. local ds = require("@ds") +local Draft = require("@ds/draft_text") local Dictation = require("@app/screens/chat/dictation") local motion = require("@ds/motion") local Placement = require("@app/screens/chat/composer_place") @@ -716,10 +717,10 @@ local function Composer(): Node end return LinkNotice.of { connection = sessions.connection:get(), - cloud = machine.cloud_link(string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0), + cloud = machine.cloud_link(Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0), host_unreachable = host_unreachable, host = host, - intent = string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0, + intent = Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0, } end) @@ -843,24 +844,38 @@ local function Composer(): Node -- the focus; back from Settings): the composer takes the keys. Once -- what brought it here is done (a palette closing -- puts focus back where it was first), so this comes last; a field - -- that takes the keys after (a rename) has them. (Kept: a reload, or - -- the window opening, isn't an arrival.) + -- that takes the keys after (a rename) has them. The window opening on + -- a chat is an arrival too: what's typed from the launch on goes into + -- the composer, with no click first (Zeron's composer has the keys as it + -- opens; ademark's A1 types from the launch and timed out on every run + -- while they were nowhere). (Kept: a reload isn't one.) local arrived = ui.state("chat_composer_arrived", "?") ui.effect(function() local now = route.current:get() local pane = route.chat:get().session local chat = now.name == "session" or now.name == "home" - local at = `{now.name}|{now.session or ""}|{now.project or ""}|{pane or ""}` - local was = arrived:peek() - if was == at then + -- (A session's project is the chat's own, known once the engine + -- lists it: its coming isn't a move, and it took the keys from + -- whatever had them by then, a dialog opened at launch.) + local project = if now.name == "home" then now.project or "" else "" + local at = `{now.name}|{now.session or ""}|{project}|{pane or ""}` + if arrived:peek() == at then return end - arrived:set(at) -- (A tab strip's arrows keep the keys on the tab they chose.) - if route.take_keys_kept() or was == "?" or not chat then + if route.take_keys_kept() or not chat then + arrived:set(at) return end + -- Marked arrived as the keys are given, not before: the window's + -- scripts loaded again meanwhile (the engine's plugins coming in as + -- the window opens) drop this turn's callback, and the scripts made + -- again give them instead. ui.after(0, function() + if arrived:peek() == at then + return + end + arrived:set(at) shared.focus:add(1) end) end) diff --git a/apps/zgx/ui/screens/chat/dictation.luau b/apps/zgx/ui/screens/chat/dictation.luau index b443d0be4..75f61579c 100644 --- a/apps/zgx/ui/screens/chat/dictation.luau +++ b/apps/zgx/ui/screens/chat/dictation.luau @@ -204,7 +204,8 @@ function M.bind(value: any, key: () -> string, blocked: () -> boolean, id: strin table.insert(bindings, own) if binding == nil then binding = own end ui.effect(function() - value:get() + -- (Its edits followed, the draft not read.) + value:len() key() local no = blocked() if pending and pending.binding == own and (no or not valid(pending)) then M.cancel() end diff --git a/apps/zgx/ui/screens/chat/lane_tab.luau b/apps/zgx/ui/screens/chat/lane_tab.luau index 9dae81e70..2e71304aa 100644 --- a/apps/zgx/ui/screens/chat/lane_tab.luau +++ b/apps/zgx/ui/screens/chat/lane_tab.luau @@ -28,6 +28,7 @@ local LinkChips = require("@app/screens/chat/link_chips") local LinkMenu = require("@app/screens/chat/link_menu") local boundary = require("@app/screens/shell/boundary") local Quote = require("@ds/quote") +local Draft = require("@ds/draft_text") -- (The chat page's places in its chats, which a split's panes share.) local Shared = require("@app/screens/chat/shared") local Asks = require("@app/screens/chat/asks") @@ -250,22 +251,53 @@ local function composer(chat: string, view: any, name: string, focus: State 0 then now = { text = text, cargo = cargo, quotes = quotes } end else - local put = kept:get()[""] + local put = kept:peek()[""] if put then now = { text = put.text, cargo = put.cargo, quotes = put.quotes } end @@ -205,6 +212,34 @@ ui.effect(function() if now ~= nil or saved_new:peek() ~= nil then saved_new:set(now) end +end +ui.effect(function() + -- What it's saved from, followed (the draft's edits, not the draft). + if here:get() == "" then + M.text:len() + M.cargo:get() + M.quotes:get() + else + kept:len() + end + -- (A run's timer goes as the next run begins: steady typing saves every + -- SAVE_EVERY, and the last keystroke's once it has waited that long.) + local wait = SAVE_EVERY - (ui.now() - saved_at) + if wait <= 0 then + save_new() + else + ui.after(wait, save_new) + end +end) +-- The window closing (the app quitting, or the window closed): Zeron's +-- window sets `app.closing` (src/main.rs) before it writes what the scripts +-- saved, and a save still waiting its turn goes now. (A timer never fires +-- once the window has closed, and the last keystrokes were lost with it.) +local closing = ui.shared("app.closing", false) +ui.effect(function() + if closing:get() == true then + save_new() + end end) -- Whether a file's name is a picture the window can draw. (zgx decodes @@ -261,6 +296,12 @@ function M.take(): (string, { any }) M.cargo:set({}) M.quotes:set({}) end) + -- A new chat's draft, sent: saved empty at once, not on the throttle's + -- next turn, which a quit in between would never reach (the message + -- coming back as the draft at the next launch). + if here:peek() == "" then + save_new() + end return text, cargo end @@ -893,6 +934,39 @@ function M.draft_of(key: string): { text: string, cargo: { any }, chosen: { [str } end +-- Follows chat `key`'s draft as `draft_of` does, wherever it is, without +-- reading its words (a long draft isn't copied for it): what follows it to +-- learn that it changed reads it only then (a split pane's mirror of it). +function M.track_draft_of(key: string) + if here:get() == key then + M.text:len() + M.cargo:get() + M.chosen:get() + else + kept:len() + end +end + +-- The options picked in chat `key`'s question, as `draft_of` reads them, +-- without its words. +function M.chosen_of(key: string): { [string]: boolean } + if here:get() == key then + return M.chosen:get() + end + local d = kept:get()[key] + return if d and type(d.chosen) == "table" then d.chosen else {} +end + +-- The length of chat `key`'s draft's words, in bytes, as `draft_of` +-- reads them (out in the composer, its draft not copied for it). +function M.draft_size_of(key: string): number + if here:peek() == key then + return Draft.size(M.text) + end + local d = kept:peek()[key] + return if d and type(d.text) == "string" then #d.text else 0 +end + -- Chat `key`'s draft changed where it's shown by its id (`field`: "text" -- or "chosen"): written wherever it is, as `draft_of` reads it. function M.set_draft_of(key: string, field: string, value: any) diff --git a/apps/zgx/ui/screens/language.luau b/apps/zgx/ui/screens/language.luau index adc609858..e81ba6783 100644 --- a/apps/zgx/ui/screens/language.luau +++ b/apps/zgx/ui/screens/language.luau @@ -43,6 +43,22 @@ end) -- The choices as a select lists them, in the locale shown (a computed -- reading their messages: they follow it). +-- The question asked when the app quits with work still running and +-- "Keep Zeron running after you quit" off (src/quit_prompt.rs), in the +-- language shown: by then the scripts are gone, so the host keeps it. +-- (Many chats are worded with 999999, which the host replaces.) +ui.effect(function() + local words = { + one = ui.t("engine.quit.working", { n = 1 }):get(), + other = ui.t("engine.quit.working", { n = 999999 }):get(), + detail = ui.t("engine.quit.detail"):get(), + keep = ui.t("engine.quit.keep"):get(), + stop = ui.t("engine.quit.stop"):get(), + } + -- (A fixture or a test host has no such action.) + pcall(ui.invoke, "app.quit_words", words) +end) + function M.options(): { { value: string, label: string } } local out = {} for _, l in M.LANGUAGES do diff --git a/apps/zgx/ui/screens/settings/general.luau b/apps/zgx/ui/screens/settings/general.luau index 98adcfc06..e02e59628 100644 --- a/apps/zgx/ui/screens/settings/general.luau +++ b/apps/zgx/ui/screens/settings/general.luau @@ -28,6 +28,7 @@ search.index(M.ID, ui.t("settings.general.title"), ui.t("settings.general.terms" { key = "compact_mode", title = ui.t("settings.general.compact_mode.label"), terms = ui.t("settings.general.compact_mode.terms") }, { key = "escape_stops", title = ui.t("settings.general.escape_stops.label"), terms = ui.t("settings.general.escape_stops.terms") }, { key = "close_panel", title = ui.t("settings.general.close_panel.label"), terms = ui.t("settings.general.close_panel.terms") }, + { key = "keep_running", title = ui.t("settings.general.keep_running.label"), terms = ui.t("settings.general.keep_running.terms") }, { key = "hide_emails", title = ui.t("settings.general.hide_emails.label"), section = ui.t("settings.general.privacy"), terms = ui.t("settings.general.hide_emails.terms") }, { key = "thread_naming", title = ui.t("settings.general.thread_naming.label"), terms = ui.t("settings.general.thread_naming.terms") }, { key = "worktrees", title = ui.t("settings.general.worktrees.label"), section = ui.t("settings.general.git"), terms = ui.t("settings.general.worktrees.terms") }, @@ -46,6 +47,9 @@ type Titles = { harness: string?, model: string? } local credit = ui.state("settings_commit_credit", nil :: Credit?) local worktrees = ui.state("settings_worktrees", nil :: Worktrees?) local titles = ui.state("settings_titles", nil :: Titles?) +-- Whether the engine outlives the app (src/quit_prompt.rs). +type Background = { keepRunningAfterQuit: boolean } +local background = ui.state("settings_background", nil :: Background?) -- Why one couldn't be had (an outcome word), by name. local failed = ui.state("settings_engine_failed", {} :: { [string]: string }) @@ -75,6 +79,7 @@ local function load_all() load("credit", "GetCommitCredit", credit) load("worktrees", "GetWorktreeSettings", worktrees) load("titles", "GetTitleSettings", titles) + load("background", "GetBackgroundSettings", background) end -- A change to the engine's: shown now, back as it was if refused. @@ -254,6 +259,26 @@ function M.view(): Node description = ui.t("settings.general.close_panel.description"), control = ds.Switch { value = prefs.close_panel_with_last_tab, label = ui.t("settings.general.close_panel.label") }, }), + row("keep_running", { + label = ui.t("settings.general.keep_running.label"), + description = function() + if failed:get().background then + return says(failed:get().background):get() + end + local b = background:get() + return if b == nil or b.keepRunningAfterQuit + then ui.t("settings.general.keep_running.on"):get() + else ui.t("settings.general.keep_running.off"):get() + end, + control = engine_switch(ui.t("settings.general.keep_running.label"), function() + local b = background:get() + return if b then b.keepRunningAfterQuit else nil + end, function(on: boolean) + change("background", background, { keepRunningAfterQuit = on }, "SetBackgroundSettings", { keepRunningAfterQuit = on }) + end, function() + return background:get() == nil + end), + }), }, }, ds.Settings.Section { diff --git a/apps/zgx/ui/settings_stub.luau b/apps/zgx/ui/settings_stub.luau index e68bd3ff8..524fa0068 100644 --- a/apps/zgx/ui/settings_stub.luau +++ b/apps/zgx/ui/settings_stub.luau @@ -124,6 +124,7 @@ local engine: { [string]: any } = { }, GetTitleSettings = {}, GetNativeSubagents = { switchable = { "claude-code" }, off = {} }, + GetBackgroundSettings = { keepRunningAfterQuit = true }, } local banners = { desktop = true, background_only = true, agent_updates = true } @@ -295,7 +296,7 @@ local answers: { [string]: (arg: any) -> any } = { local w = table.clone(engine[get]) w.settings = arg.params engine[get] = w - elseif m == "SetTitleSettings" then + elseif m == "SetTitleSettings" or m == "SetBackgroundSettings" then engine[get] = arg.params end return settled(engine[get]) diff --git a/apps/zgx/ui/templates/zeron-v2/.base.json b/apps/zgx/ui/templates/zeron-v2/.base.json index a29cc3f9f..571650df2 100644 --- a/apps/zgx/ui/templates/zeron-v2/.base.json +++ b/apps/zgx/ui/templates/zeron-v2/.base.json @@ -1,8 +1,8 @@ { "ds/sidebar.luau": "f2abf7b60b5f4d00", "ds/tokens.luau": "296d1ea72675de0c", - "screens/chat/composer.luau": "9c0c79de53eae373", - "screens/chat/lane_tab.luau": "12c199c168515f49", + "screens/chat/composer.luau": "f8f9267383bb3d27", + "screens/chat/lane_tab.luau": "8173218223a811fa", "screens/panel/init.luau": "3d5e0c712c4dda56", "screens/shell/init.luau": "c7b26c3a1ccf9a32", "screens/shell/page.luau": "c1debbce640a5d45", diff --git a/apps/zgx/ui/templates/zeron-v2/screens/chat/composer.luau b/apps/zgx/ui/templates/zeron-v2/screens/chat/composer.luau index d6cf62303..f523d70aa 100644 --- a/apps/zgx/ui/templates/zeron-v2/screens/chat/composer.luau +++ b/apps/zgx/ui/templates/zeron-v2/screens/chat/composer.luau @@ -37,6 +37,7 @@ local files = require("@app/screens/files/api") local Where = require("@app/screens/chat/where") local DropOverlay = require("@ds/drop_overlay") local mentions = require("@ds/composer_mentions") +local Draft = require("@ds/draft_text") local Invocations = require("@app/screens/chat/invocations") local Completions = require("@app/screens/chat/completions") local ZeronCommands = require("@app/screens/chat/zeron_commands") @@ -586,10 +587,10 @@ local function Composer(): Node end return LinkNotice.of { connection = sessions.connection:get(), - cloud = machine.cloud_link(string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0), + cloud = machine.cloud_link(Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0), host_unreachable = host_unreachable, host = host, - intent = string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0, + intent = Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0, } end) @@ -824,24 +825,38 @@ local function Composer(): Node -- the focus; back from Settings): the composer takes the keys. Once -- what brought it here is done (a palette closing -- puts focus back where it was first), so this comes last; a field - -- that takes the keys after (a rename) has them. (Kept: a reload, or - -- the window opening, isn't an arrival.) + -- that takes the keys after (a rename) has them. The window opening on + -- a chat is an arrival too: what's typed from the launch on goes into + -- the composer, with no click first (Zeron's composer has the keys as it + -- opens; ademark's A1 types from the launch and timed out on every run + -- while they were nowhere). (Kept: a reload isn't one.) local arrived = ui.state("chat_composer_arrived", "?") ui.effect(function() local now = route.current:get() local pane = route.chat:get().session local chat = now.name == "session" or now.name == "home" - local at = `{now.name}|{now.session or ""}|{now.project or ""}|{pane or ""}` - local was = arrived:peek() - if was == at then + -- (A session's project is the chat's own, known once the engine + -- lists it: its coming isn't a move, and it took the keys from + -- whatever had them by then, a dialog opened at launch.) + local project = if now.name == "home" then now.project or "" else "" + local at = `{now.name}|{now.session or ""}|{project}|{pane or ""}` + if arrived:peek() == at then return end - arrived:set(at) -- (A tab strip's arrows keep the keys on the tab they chose.) - if route.take_keys_kept() or was == "?" or not chat then + if route.take_keys_kept() or not chat then + arrived:set(at) return end + -- Marked arrived as the keys are given, not before: the window's + -- scripts loaded again meanwhile (the engine's plugins coming in as + -- the window opens) drop this turn's callback, and the scripts made + -- again give them instead. ui.after(0, function() + if arrived:peek() == at then + return + end + arrived:set(at) shared.focus:add(1) end) end) diff --git a/apps/zgx/ui/templates/zeron-v2/screens/chat/lane_tab.luau b/apps/zgx/ui/templates/zeron-v2/screens/chat/lane_tab.luau index f4e3e08bf..2d3d0afe8 100644 --- a/apps/zgx/ui/templates/zeron-v2/screens/chat/lane_tab.luau +++ b/apps/zgx/ui/templates/zeron-v2/screens/chat/lane_tab.luau @@ -29,6 +29,7 @@ local LinkMenu = require("@app/screens/chat/link_menu") local Shared = require("@app/screens/chat/shared") local boundary = require("@app/screens/shell/boundary") local Quote = require("@ds/quote") +local Draft = require("@ds/draft_text") local Carried = require("@app/screens/chat/carried") local Completions = require("@app/screens/chat/completions") local Invocations = require("@app/screens/chat/invocations") @@ -505,7 +506,7 @@ local function composer(chat: string, view: any, name: string, focus: State= (top :: number) and (y :: number) < (top :: number) + (h :: number)).to_be(true) + expect(app:error()).to_be(nil) +end) diff --git a/apps/zgx/ui/tests/keep_running.test.luau b/apps/zgx/ui/tests/keep_running.test.luau new file mode 100644 index 000000000..ff703efed --- /dev/null +++ b/apps/zgx/ui/tests/keep_running.test.luau @@ -0,0 +1,50 @@ +-- Settings → General → "Keep Zeron running after you quit": the engine's +-- own setting (crates/engine/src/keep_running.rs), on until turned off; a +-- flip goes to the engine and the row says what quitting does now. The +-- question asked on quitting with work running is the host's +-- (src/quit_prompt.rs), in the app's words: handed over as the language +-- changes. On the shell in ui/shell_fixture.luau. + +local PAGE = "#settings-page" +local LABEL = "Keep Zeron running after you quit" + +local function shows(app: any, scope: string, text: string): boolean + return app:tree(scope):find(text, 1, true) ~= nil +end + +local function asked(app: any, entry: string): boolean + for _, c in (app:state("settings_stub_calls") :: any) or {} do + if c == entry then + return true + end + end + return false +end + +local function open(): any + local app = mount("shell_fixture.luau") + app:resize(1200, 900) + app:frames(30) + app:command("settings_stub.sign_in") + app:command("shell.settings") + app:frames(20) + return app +end + +test("on until turned off, and the row says what quitting does", function() + local app = open() + local switch = `{PAGE} >> role:switch "{LABEL}"` + expect(asked(app, "settings.engine:GetBackgroundSettings")).to_be(true) + expect(app:tree(switch)).never.to_contain("unchecked") + expect(shows(app, PAGE, "Chats keep working, and other devices can reach them.")).to_be(true) + app:click(switch) + app:frames(10) + expect(asked(app, "settings.engine:SetBackgroundSettings")).to_be(true) + expect(app:tree(switch)).to_contain("unchecked") + expect(shows(app, PAGE, "When you quit, Zeron stops once its chats are done.")).to_be(true) + -- Refused: it goes back as it was. + app:command("settings_stub.refuse_next") + app:click(switch) + app:frames(10) + expect(app:tree(switch)).to_contain("unchecked") +end) diff --git a/apps/zgx/ui/tests/launch_keys.test.luau b/apps/zgx/ui/tests/launch_keys.test.luau new file mode 100644 index 000000000..629c1224e --- /dev/null +++ b/apps/zgx/ui/tests/launch_keys.test.luau @@ -0,0 +1,48 @@ +-- A launch gives the composer the keys: what's typed as the window opens +-- goes into it, with no click first, whether the window opens on a new chat +-- or on the session it was left on. (ademark's A1 types from the launch and +-- reads the composer; with the keys nowhere, every run timed out.) On the +-- shell in ui/shell_fixture.luau; a second `mount` is the next launch. + +local BOX = 'role:textbox "Message"' +local SIDE = "#shell-sidebar" + +local function focused(app: any): string + for line in app:tree():gmatch("[^\n]+") do + if line:find(" focused", 1, true) then + return line + end + end + return "" +end + +local function has_keys(app: any): boolean + return focused(app):find('textbox "Message"', 1, true) ~= nil +end + +local function launch(): any + local app = mount("shell_fixture.luau") + app:resize(1600, 1000) + return app +end + +test("a launch on a new chat: the composer has the keys from the first frame", function() + local app = launch() + app:frames(1) + expect(has_keys(app)).to_be(true) + app:frames(30) + expect(has_keys(app)).to_be(true) +end) + +test("a launch on the session it was left on: its composer has the keys", function() + local first = launch() + first:frames(30) + first:click(`{SIDE} >> role:button "Fix the build"`) + first:frames(30) + local app = launch() + app:frames(1) + expect(app:tree()).to_contain("Fix the build") + expect(has_keys(app)).to_be(true) + app:frames(30) + expect(has_keys(app)).to_be(true) +end) diff --git a/apps/zgx/ui/tests/markdown_edit.test.luau b/apps/zgx/ui/tests/markdown_edit.test.luau new file mode 100644 index 000000000..f6a8aee05 --- /dev/null +++ b/apps/zgx/ui/tests/markdown_edit.test.luau @@ -0,0 +1,33 @@ +-- The composer's markdown typing (ds/markdown_edit): what a line break does +-- in a list item. Run with `zgx test ui`. + +local markdown = require("@ds/markdown_edit") + +test("a break in a list item continues it, however long the draft above", function() + local head = string.rep("a line of the draft before it\n", 3000) + local text = head .. "- one" + local from, to, put = markdown.newline(text, #text, #text) + expect(from).to_be(#text) + expect(to).to_be(#text) + expect(put).to_be("\n- ") + -- An empty item: undone, its marker gone. + local empty = text .. "\n- " + local f, t, p = markdown.newline(empty, #empty, #empty) + expect(f).to_be(#text + 1) + expect(t).to_be(#empty) + expect(p).to_be("") + -- A plain line, or a selection: a plain break. + expect(markdown.newline(head .. "plain", #head + 5, #head + 5)).to_be(nil) + expect(markdown.newline(text, #text - 1, #text)).to_be(nil) +end) + +test("a break in a fenced block is plain, and the list goes on after it", function() + local head = string.rep("a line of the draft before it\n", 3000) + local fenced = head .. " ```lua\n- one" + expect(markdown.newline(fenced, #fenced, #fenced)).to_be(nil) + local tilde = head .. "~~~\n- one" + expect(markdown.newline(tilde, #tilde, #tilde)).to_be(nil) + local closed = fenced .. "\nnot a ``` fence\n```\n- two" + local _, _, put = markdown.newline(closed, #closed, #closed) + expect(put).to_be("\n- ") +end) diff --git a/apps/zgx/ui/tests/mention_sessions.test.luau b/apps/zgx/ui/tests/mention_sessions.test.luau index 0a3f464fd..bfda72342 100644 --- a/apps/zgx/ui/tests/mention_sessions.test.luau +++ b/apps/zgx/ui/tests/mention_sessions.test.luau @@ -255,3 +255,25 @@ test("the lit row follows the list as it narrows, and the arrows move it", funct settle(app) expect(takes()).to_be("/review ") end) + +-- A closed code block longer than the composer's first look around the +-- caret (ds/draft_text, 1 KB each way): that look sees only its closing +-- fence, which read as one still open. Every trigger after it opens its +-- list; one inside a block still open never does. +test("after a closed code block over 1 KB long, each trigger opens its list", function() + local app = chat() + local code = string.rep("let value = compute(input);\n", 60) + expect(#code > 1024).to_be(true) + -- (Filled, trigger and all, so the caret is at its end: typing clicks + -- the box first, mid-draft in one this tall.) + for _, t in TRIGGERS do + fresh(app, "Look at this:\n```rust\n" .. code .. "```\n" .. t.trigger) + expect(`{t.trigger} after a closed block: {shown(app)}`).to_be(`{t.trigger} after a closed block: true`) + -- (Closed, so it covers no part of the box.) + app:key("Escape") + settle(app) + -- Still inside one: nothing. + fresh(app, "Look at this:\n```rust\n" .. code .. t.trigger) + expect(`{t.trigger} inside an open block: {shown(app)}`).to_be(`{t.trigger} inside an open block: false`) + end +end) diff --git a/apps/zgx/ui/tests/mentions.test.luau b/apps/zgx/ui/tests/mentions.test.luau index 4a044737c..885592a0d 100644 --- a/apps/zgx/ui/tests/mentions.test.luau +++ b/apps/zgx/ui/tests/mentions.test.luau @@ -68,6 +68,26 @@ test("commands and skills replace to their end too", function() expect(mentions.token("costs $12", 7)).to_be_nil() end) +test("a token on a draft's later line, and none in a fence, however long the draft", function() + local head = string.rep("a line of the draft before it\n", 3000) + local text = head .. "Compare @queue.rs with" + local t = mentions.token(text, #head + 12) + assert(t, "no token at the caret") + expect(t.query).to_be("que") + expect(t.from).to_be(#head + 8) + expect(t.to).to_be(#head + 17) + expect(picked(text, #head + 12, MESSAGE_QUEUE)).to_be(head .. "Compare @message_queue.rs with") + -- A fence opened (after blanks): nothing in it; closed, a token again. + local fence = head .. " ```lua\n@queue" + expect(mentions.token(fence, #fence)).to_be_nil() + local closed = fence .. "\nnot a ``` fence\n```\n/com" + local c = mentions.token(closed, #closed) + assert(c, "no command after the fence") + expect(c.trigger).to_be("/") + expect(c.query).to_be("com") + expect(c.from).to_be(#closed - 4) +end) + test("a file is named by the shortest end of its path that is its own", function() expect(mentions.short(FILES, FILES[1])).to_be("queue.rs") expect(mentions.short(FILES, FILES[2])).to_be("message_queue.rs") diff --git a/apps/zgx/ui/tests/new_chat_remembers.test.luau b/apps/zgx/ui/tests/new_chat_remembers.test.luau index 634623179..9485859cf 100644 --- a/apps/zgx/ui/tests/new_chat_remembers.test.luau +++ b/apps/zgx/ui/tests/new_chat_remembers.test.luau @@ -153,3 +153,38 @@ test("the new page's settings, draft and what it carries survive a relaunch", fu expect(app:text('role:textbox "Message"')).to_be("Half a thought") expect(#(app:state("chat.composer.cargo") or {})).to_be(cargo) end) + +-- The page's draft is saved at most every 250 ms (screens/chat/shared): +-- what was typed since the last save waits on a timer, which never fires +-- once the window has closed. Closing (quitting, or the window closed), +-- Zeron's window sets `app.closing` (src/main.rs) before it writes what was +-- saved: the waiting save goes then, the last keystrokes with it. +local function saved_text(app: any): string? + local saved = app:state("chat_new_draft") + return if type(saved) == "table" then saved.text else nil +end + +test("the new page's last keystrokes are saved as the window closes", function() + local app = open() + app:fill('role:textbox "Message"', "Half") + settle(app) + expect(saved_text(app)).to_be("Half") + -- Typed on at once: its save waits its turn. + app:fill('role:textbox "Message"', "Half a thought") + expect(saved_text(app)).to_be("Half") + app:set_state("app.closing", true) + expect(saved_text(app)).to_be("Half a thought") +end) + +test("a message sent from the new page is no longer its saved draft, at once", function() + local app = open() + app:fill('role:textbox "Message"', "Start something") + settle(app) + expect(saved_text(app)).to_be("Start something") + app:key("Enter") + -- (Before the throttle's next turn: a quit now keeps what's saved.) + expect(saved_text(app)).to_be_nil() + settle(app) + expect(app:state("calls")).to_contain("send:new1:Start something") + expect(saved_text(app)).to_be_nil() +end) diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 8dd3b8719..b7469e281 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -28,6 +28,8 @@ tokio.workspace = true futures.workspace = true zeron-term-link.workspace = true async-trait.workspace = true +# Agents' Zeron MCP tools, served by the engine itself (`zeron_mcp::http`). +zeron-mcp.workspace = true serde.workspace = true serde_json.workspace = true rusqlite.workspace = true @@ -74,7 +76,6 @@ windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Securit [dev-dependencies] tracing-subscriber.workspace = true -zeron-mcp.workspace = true zeron-harness = { workspace = true, features = ["installer-fixture"] } # Paused-clock tests (the 30-minute idle reaper). tokio = { workspace = true, features = ["test-util"] } diff --git a/crates/engine/src/agent_accounts/stores.rs b/crates/engine/src/agent_accounts/stores.rs index 623d6bc83..c5f919c94 100644 --- a/crates/engine/src/agent_accounts/stores.rs +++ b/crates/engine/src/agent_accounts/stores.rs @@ -1506,10 +1506,10 @@ fn hermes_env_secret(var: &str, hermes_home: &Path) -> bool { !value.is_empty() }; // (Only a regular file of a sane size: no FIFO or runaway file is read - // while the accounts are listed.) + // while the accounts are listed; checked on the open file, so one + // swapped in after a check of the path is refused too.) let env_file = hermes_home.join(".env"); - let readable = std::fs::metadata(&env_file).is_ok_and(|m| m.is_file() && m.len() <= 1 << 20); - if readable && let Ok(text) = std::fs::read_to_string(&env_file) { + if let Ok(text) = zeron_harness::bounded_file::read_to_string(&env_file, 1 << 20) { for line in text.lines() { let line = line.trim(); let line = line.strip_prefix("export ").unwrap_or(line); diff --git a/crates/engine/src/deferred.rs b/crates/engine/src/deferred.rs index 8357b4dbe..549700574 100644 --- a/crates/engine/src/deferred.rs +++ b/crates/engine/src/deferred.rs @@ -232,6 +232,10 @@ impl DeferredEngine { // (Its bearer first: a run revived on boot waits // on the port, then reads both.) runtime.core().sessions.set_ipc_token_file(ipc_token_file); + // Agents' Zeron tools come from this engine itself. + if let Err(err) = runtime.core().sessions.serve_mcp_endpoint(service.clone()).await { + tracing::warn!(error = %err, "agent MCP endpoint not served; agents get `zeron mcp`"); + } runtime.core().sessions.set_ipc_port(port); } else { // None is coming: a revived run needn't wait for one. diff --git a/crates/engine/src/doc_host.rs b/crates/engine/src/doc_host.rs index 5df6f956c..2b0e4db7a 100644 --- a/crates/engine/src/doc_host.rs +++ b/crates/engine/src/doc_host.rs @@ -417,6 +417,20 @@ impl Drop for TransferProgressGuard<'_> { /// is never delayed. const DEGRADE_GRACE: std::time::Duration = std::time::Duration::from_secs(4); +/// How often the connectivity monitor looks once the posture has held +/// healthy for [`CONNECTIVITY_SETTLE_TICKS`] seconds. A fresh degradation is +/// then seen up to this late, on top of its [`DEGRADE_GRACE`]; recovery from +/// one is still seen within a second, as the monitor looks every second +/// while anything is degraded. +const CONNECTIVITY_STEADY: std::time::Duration = std::time::Duration::from_secs(5); +const CONNECTIVITY_SETTLE_TICKS: u32 = 3; + +/// Nothing degraded: local, or connected with every open chat connected. +fn connectivity_healthy(posture: &zeron_proto::Connectivity) -> bool { + use zeron_proto::ConnectivityState as S; + matches!(posture.state, S::Connected | S::Disabled) && posture.chats.iter().all(|chat| chat.connected) +} + /// One tracked degradation source in [`DegradeGrace`]. enum GraceKey<'a> { OsPath, @@ -463,6 +477,11 @@ impl DegradeGrace { now.duration_since(since) >= DEGRADE_GRACE } + /// A degradation is being timed: the report may change with time alone. + fn timing(&self) -> bool { + self.os_path.is_some() || self.registry.is_some() || !self.chats.is_empty() + } + /// Drop timers for chats that no longer have open docs. fn retain_chats(&mut self, keep: impl Fn(&str) -> bool) { self.chats.retain(|id, _| keep(id)); @@ -2442,7 +2461,25 @@ impl DocHost { _ = tokio::time::sleep_until(last + SYNC_PASS_SPACING), if busy => {} _ = kick.notified() => {} Ok(()) = jobs.changed() => {} - Ok(()) = outbox.changed() => {} + Ok(()) = outbox.changed() => { + // Rows a started chat's own client sends: nothing + // for a pass to admit. A streaming chat adds + // several a second, and each pass reads the store. + let Some(inner) = weak.upgrade() else { return }; + let changed = inner.store.take_outbox_changes(); + let handles = lock(&inner.handles); + let own = changed.iter().all(|id| { + !stopping_ids.contains(id) + && handles.get(id).is_some_and(|h| { + h.sync_started.load(Ordering::Acquire) + && !h.publication_failed.load(Ordering::Acquire) + }) + }); + drop(handles); + if own && !busy { + continue; + } + } Ok(()) = socket_contention.changed() => {} Some(Ok(())) = changed(&mut registry_chats) => {} Some(Ok(())) = changed(&mut registry_synced) => {} @@ -2463,6 +2500,9 @@ impl DocHost { // Whatever changes from here on wakes the next pass. jobs.borrow_and_update(); outbox.borrow_and_update(); + if let Some(inner) = weak.upgrade() { + inner.store.take_outbox_changes(); + } socket_contention.borrow_and_update(); let Some(inner) = weak.upgrade() else { return }; let host = Self { inner }; @@ -3998,10 +4038,13 @@ impl DocHost { } /// The connectivity stream: current posture first, then every change. - /// Lazily starts a monitor — a 1s recompute over in-memory stats - /// (atomics + small locks), published only when the value changes. The - /// retry countdown renders client-side from `retry_at_ms`, so quiet - /// periods emit nothing at all. + /// Lazily starts a monitor — a recompute over in-memory stats (atomics + + /// small locks), published only when the value changes: every second + /// while it's changing, degraded or timing a degradation's grace, and + /// every [`CONNECTIVITY_STEADY`] once it has held healthy (or local) for + /// a few seconds, so a settled engine isn't woken each second to find + /// nothing changed. The retry countdown renders client-side from + /// `retry_at_ms`, so quiet periods emit nothing at all. pub fn watch_connectivity(&self) -> watch::Receiver { let tx = self .inner @@ -4013,12 +4056,17 @@ impl DocHost { { let host = self.clone(); self.spawn_worker(async move { - let mut tick = tokio::time::interval(std::time::Duration::from_secs(1)); - tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + let mut held = 0u32; loop { - tick.tick().await; + let wait = if held >= CONNECTIVITY_SETTLE_TICKS { + CONNECTIVITY_STEADY + } else { + std::time::Duration::from_secs(1) + }; + tokio::time::sleep(wait).await; let next = host.compute_connectivity(); - if let Some(tx) = host.inner.connectivity.get() { + let healthy = connectivity_healthy(&next); + let changed = host.inner.connectivity.get().is_some_and(|tx| { tx.send_if_modified(|cur| { if *cur == next { false @@ -4026,8 +4074,10 @@ impl DocHost { *cur = next; true } - }); - } + }) + }); + let timing = lock(&host.inner.connectivity_grace).timing(); + held = if changed || timing || !healthy { 0 } else { held.saturating_add(1) }; } }); } @@ -4265,6 +4315,7 @@ impl DocHost { payload: SessionCommandPayload, transfers: Vec, ) -> Result { + tracing::debug!(target: "first_word", chat = %chat_id, "queue command"); let handle = self.open(chat_id)?; // A chat hosted on Cloud: the bytes go to the edge's store first, and // the message enters the chat only once they're there. @@ -6141,6 +6192,7 @@ impl DocHost { SessionCommandPayload::Run { .. } | SessionCommandPayload::Steer { .. } ) { crate::wake_trace::mark_command(entry.issued_at); + tracing::debug!(target: "first_word", chat = %handle.chat_id, "drain picked the run"); self.await_earlier_controls(handle, &entry.id).await; } let (status, resolution) = match self.execute(&sessions, handle, &entry).await { @@ -6797,9 +6849,15 @@ impl DocHost { async fn capture_source_context(&self, cwd: &str) -> Option { let repos = self.inner.repos.get()?; let path = Path::new(cwd); - let identity = repos.checkout_identity(path).await.ok()?; - let branch = repos.current_branch(path).await.ok()?; - let head_sha = repos.head_sha(path).await.ok().flatten(); + // Three `git` processes: side by side, not one after another. + let (identity, branch, head_sha) = tokio::join!( + repos.checkout_identity(path), + repos.current_branch(path), + repos.head_sha(path), + ); + let identity = identity.ok()?; + let branch = branch.ok()?; + let head_sha = head_sha.ok().flatten(); Some(ConversationSourceContext { checkout_id: identity.id, repo_root: identity.root.to_string_lossy().into_owned(), @@ -6811,10 +6869,13 @@ impl DocHost { } /// Every fresh harness dispatch crosses this boundary, including - /// steer/input fallbacks and crash recovery. Capture immediately before - /// dispatch so the conversation records the checkout the harness will - /// actually observe, rather than whichever checkout state was current on - /// an earlier turn. + /// steer/input fallbacks and crash recovery. The conversation records + /// the checkout the harness will actually observe, rather than whichever + /// checkout state was current on an earlier turn: read as the dispatch + /// starts, beside it rather than before it. Its `git` processes took + /// ~25 ms (M5 Max; more on slower Macs) in front of every agent start, + /// and the agent reads nothing of its checkout for far longer than that + /// (its own startup is ~250 ms before its first request). pub(crate) async fn dispatch_with_source_context( &self, sessions: &SessionsEngine, @@ -6823,11 +6884,21 @@ impl DocHost { request: zeron_proto::RunRequest, message_id: Option, ) -> Result { - if let Some(workspace) = self.workspace() - && let Some(context) = self.capture_source_context(&request.cwd).await - && let Err(err) = workspace.set_chat_source_context(chat_id, &context) - { - tracing::warn!(chat = %chat_id, error = %err, "conversation source stamp failed"); + // Stamped on a task of its own: the drain resolves the command as + // soon as the run is dispatched, never behind the `git` processes. + if self.workspace().is_some() { + let host = self.clone(); + let chat_id = chat_id.to_owned(); + let cwd = request.cwd.clone(); + self.spawn_worker(async move { + if let Some(workspace) = host.workspace() + && let Some(context) = host.capture_source_context(&cwd).await + && let Err(err) = workspace.set_chat_source_context(&chat_id, &context) + { + tracing::warn!(chat = %chat_id, error = %err, "conversation source stamp failed"); + } + tracing::debug!(target: "first_word", chat = %chat_id, "source context: stamped"); + }); } sessions .dispatch(chat_id, harness, request, message_id) diff --git a/crates/engine/src/doc_host/relay.rs b/crates/engine/src/doc_host/relay.rs index 14b5645ae..a9e401743 100644 --- a/crates/engine/src/doc_host/relay.rs +++ b/crates/engine/src/doc_host/relay.rs @@ -120,6 +120,8 @@ pub(crate) struct ChatRelayState { heal_version: Mutex>, /// The edge takes heal claims. Off once it says it doesn't know them. heal_claims: AtomicBool, + /// A commit, or a peer's update kept: wakes a resting maintenance loop. + stirred: tokio::sync::Notify, } impl ChatRelayState { @@ -143,6 +145,7 @@ impl ChatRelayState { journal: Mutex::new(Journal::default()), heal_version: Mutex::new(None), heal_claims: AtomicBool::new(true), + stirred: tokio::sync::Notify::new(), }) } @@ -252,9 +255,19 @@ impl ChatRelayState { if relaying { lock(&self.unretired).ids.push(batch_id.to_string()); } + self.stirred.notify_one(); relaying } + /// Nothing kept that a save has yet to cover: no relayed commits waiting + /// in the outbox, no peers' updates in the inbox. + fn settled(&self) -> bool { + lock(&self.unretired).ids.is_empty() && { + let journal = lock(&self.journal); + journal.newest == 0 && journal.settled == 0 + } + } + /// `epoch`: the mode epoch sampled before this checkpoint's export. An export /// from before the host last stopped relaying can't settle what that stop owes: /// it may lack commits relayed after it began. @@ -340,6 +353,7 @@ impl ChatDocHandle { pub(crate) fn note_journaled(&self, ordinal: i64) { if let Some(state) = &*lock(&self.relay) { lock(&state.journal).newest = ordinal; + state.stirred.notify_one(); } } } @@ -380,14 +394,22 @@ impl DocHost { let weak = Arc::downgrade(handle); self.spawn_sync_worker(cancel, async move { let mut events = state.relay.events(); - let mut tick = tokio::time::interval(MAINTENANCE_TICK); - tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + // The first look is at once, as the interval this replaced gave it. + let mut next_tick = Instant::now(); + let mut resting = false; + let mut adopt_owed = true; let mut last_refresh: Option = None; let mut last_room = None; let mut last_commits = 0u64; let mut last_commit_at = Instant::now(); loop { tokio::select! { + // Resting: a commit, or a peer's update kept, is looked at + // at once. + _ = state.stirred.notified(), if resting => { + resting = false; + next_tick = Instant::now(); + } event = events.recv() => { let Some(handle) = weak.upgrade() else { return }; if matches!(event, Ok(RelayEvent::LegacyPeer)) { @@ -396,16 +418,28 @@ impl DocHost { last_refresh = Some(Instant::now()); } } - _ = tick.tick() => { + _ = tokio::time::sleep_until(next_tick) => { let Some(handle) = weak.upgrade() else { return }; let now = Instant::now(); let room = lock(&handle.chat2).as_ref().and_then(|c| c.relay_answer()); + let was_relaying = state.relaying(); if room != last_room || last_refresh.is_none_or(|at| now - at >= MODE_REFRESH) { last_room = room; last_refresh = Some(now); host.refresh_relay_mode(&handle, &state); + // And with each refresh, a look for rows to adopt. + adopt_owed = true; } + // Rows a relaying host didn't relay come from before it + // relayed (a restart, a stretch of storing): its own + // commits since are relayed as they're made. So it + // looks when relaying begins and with each mode + // refresh, not every second: each look reads every + // pending row's bytes, and while it streams the + // outbox holds a second of its commits. + adopt_owed |= state.relaying() != was_relaying; if state.relaying() + && std::mem::take(&mut adopt_owed) && let Ok(pending) = host.inner.store.pending_chat_updates(&handle.chat_id) && !pending.is_empty() { @@ -415,10 +449,9 @@ impl DocHost { } } let saved = handle.persistence.as_ref().is_some_and(|p| p.is_clean()); - for id in state.retirable(saved) { - if let Err(err) = host.inner.store.acknowledge_chat_update(&handle.chat_id, &id) { - tracing::warn!(%err, "chat relay: outbox retirement failed"); - } + let retired = state.retirable(saved); + if let Err(err) = host.inner.store.acknowledge_chat_updates(&handle.chat_id, &retired) { + tracing::warn!(%err, "chat relay: outbox retirement failed"); } if let Some(through) = state.retirable_journal(saved) && let Err(err) = host.inner.store.retire_chat_inbox(&handle.chat_id, through) @@ -445,6 +478,21 @@ impl DocHost { if state.relaying() && crate::runner::session().is_some() { host.cloud_checkpoint_due(&handle, &state, now, last_commit_at); } + // Nothing time can move: no answer awaited, nothing to + // retire or heal, no Cloud checkpoint to follow, no + // commit since the last look. Rest until something + // stirs, or the mode's next refresh. + resting = room.is_some() + && state.caught_up() + && !state.heal_owed.load(Ordering::Acquire) + && crate::runner::session().is_none() + && last_commit_at < now + && state.settled(); + next_tick = if resting { + last_refresh.map_or(now, |at| at + MODE_REFRESH).max(now + MAINTENANCE_TICK) + } else { + now + MAINTENANCE_TICK + }; } } } diff --git a/crates/engine/src/home_index.rs b/crates/engine/src/home_index.rs index 5e89a97da..98e75c746 100644 --- a/crates/engine/src/home_index.rs +++ b/crates/engine/src/home_index.rs @@ -8,16 +8,270 @@ //! folders, hidden folders and dependency folders altogether. It holds no //! file contents, so home has no content search. -use std::path::Path; -use std::sync::Mutex; +//! +//! The walk is bounded and never polls. Its workers share a queue of folders +//! and sleep on a condition variable while it's empty, so a worker held up +//! in one folder costs the others nothing (the `ignore` crate's parallel +//! walker had its idle workers wake every millisecond until all were done: +//! one `open()` that never returned, a macOS privacy prompt nobody answers +//! for Desktop or Documents, kept ~4,000 wakeups a second going for good). +//! It ends at [`WALK_DEADLINE`] with what it has, or when cancelled (the new +//! project picker closed); a folder still being read then is skipped by +//! later walks until that read returns, so a stuck folder holds one thread, +//! not one per walk. + +use std::collections::{HashSet, VecDeque}; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; -use std::time::Instant; +use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError, Weak}; +use std::time::{Duration, Instant}; use crate::workspace_search::{NameKinds, NameMatch, normalize_name_query}; /// Paths kept at most; a larger home answers from the first ones walked. pub const MAX_ENTRIES: usize = 250_000; +/// A walk answers with what it has found by then. +pub const WALK_DEADLINE: Duration = Duration::from_secs(20); + +/// Folders above this depth (home's own folders, and theirs) that aren't +/// repositories are listed one by one, each child a unit of its own, so the +/// workers share a home whose projects sit under one folder. A repository, +/// or anything deeper, is walked whole by one worker with its ignore files. +const EXPAND_DEPTH: usize = 3; + +/// Folders a walk that ended left a worker reading (see the module docs). +static STALLED: Mutex>> = Mutex::new(None); + +fn stalled() -> MutexGuard<'static, Option>> { + STALLED.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// Stops a walk: it returns at once with what it has. +#[derive(Default)] +pub struct WalkCancel { + cancelled: AtomicBool, + walk: Mutex>, +} + +impl WalkCancel { + pub fn cancel(&self) { + self.cancelled.store(true, Ordering::SeqCst); + if let Some(walk) = self + .walk + .lock() + .unwrap_or_else(PoisonError::into_inner) + .upgrade() + { + walk.quit(); + } + } + + pub fn is_cancelled(&self) -> bool { + self.cancelled.load(Ordering::SeqCst) + } +} + +struct Unit { + dir: PathBuf, + /// Below home: 1 for home's own folders. + depth: usize, +} + +#[derive(Default)] +struct Queue { + units: VecDeque, + /// Workers inside a unit. + active: usize, + /// Folders a worker is inside now. + reading: HashSet, + quit: bool, +} + +/// One walk's shared state. Workers hold it until they leave, which a +/// worker stuck in `open()` does only once that returns. +struct Walk { + home: PathBuf, + queue: Mutex, + changed: Condvar, + /// `queue.quit`, read without the lock between entries. + stop: AtomicBool, + found: Mutex>, + /// Folders skipped as stalled by an earlier walk. + skip: HashSet, + /// Times an idle worker woke (tests: idle workers must not poll). + #[cfg(test)] + wakes: std::sync::atomic::AtomicUsize, +} + +impl Walk { + fn queue(&self) -> MutexGuard<'_, Queue> { + self.queue.lock().unwrap_or_else(PoisonError::into_inner) + } + + fn quit(&self) { + self.stop.store(true, Ordering::SeqCst); + self.queue().quit = true; + self.changed.notify_all(); + } + + fn quitting(&self) -> bool { + self.stop.load(Ordering::Relaxed) + } + + fn push(&self, unit: Unit) { + if self.skip.contains(&unit.dir) { + tracing::info!(dir = %unit.dir.display(), "home walk: skipping a folder an earlier walk is still reading"); + return; + } + self.queue().units.push_back(unit); + self.changed.notify_one(); + } + + /// Keep `items`; `false` once the walk has enough. + fn keep(&self, items: &mut Vec) -> bool { + let mut found = self.found.lock().unwrap_or_else(PoisonError::into_inner); + found.append(items); + if found.len() >= MAX_ENTRIES { + drop(found); + self.quit(); + return false; + } + true + } + + /// A worker: units until the queue is empty with nobody working (the + /// walk is done) or the walk quits; asleep while it waits. + fn work(self: Arc) { + loop { + let unit = { + let mut queue = self.queue(); + loop { + if queue.quit { + return; + } + if let Some(unit) = queue.units.pop_front() { + queue.active += 1; + queue.reading.insert(unit.dir.clone()); + break unit; + } + if queue.active == 0 { + // Done: wake the others and the walk's owner. + self.changed.notify_all(); + return; + } + queue = self + .changed + .wait(queue) + .unwrap_or_else(PoisonError::into_inner); + #[cfg(test)] + self.wakes.fetch_add(1, Ordering::Relaxed); + } + }; + self.visit(&unit); + let quit = { + let mut queue = self.queue(); + queue.active -= 1; + queue.reading.remove(&unit.dir); + if queue.active == 0 && queue.units.is_empty() { + self.changed.notify_all(); + } + queue.quit + }; + // A walk that ended while this folder was read marked it stalled. + if let Some(stalled) = stalled().as_mut() + && stalled.remove(&unit.dir) + { + tracing::info!(dir = %unit.dir.display(), "home walk: a stalled folder read returned"); + } + if quit { + return; + } + } + } + + fn visit(&self, unit: &Unit) { + #[cfg(test)] + tests::held(&unit.dir); + let mut items = Vec::new(); + if unit.depth < EXPAND_DEPTH && !unit.dir.join(".git").exists() { + let Ok(listing) = std::fs::read_dir(&unit.dir) else { + return; + }; + for entry in listing { + if self.quitting() { + return; + } + let Ok(entry) = entry else { continue }; + let name = entry.file_name().to_string_lossy().into_owned(); + if name.starts_with('.') { + continue; + } + let is_dir = entry.file_type().is_ok_and(|kind| kind.is_dir()); + let depth = unit.depth + 1; + if is_dir && skipped(&name, depth, || entry.metadata().ok()) { + continue; + } + let path = entry.path(); + if let Some(home_path) = self.home_path(&path, is_dir) { + items.push(home_path); + } + if is_dir { + self.push(Unit { dir: path, depth }); + } + } + self.keep(&mut items); + return; + } + let base = unit.depth; + let walk = ignore::WalkBuilder::new(&unit.dir) + .hidden(true) + .follow_links(false) + .git_ignore(true) + .git_global(true) + .git_exclude(true) + .ignore(true) + .parents(false) + .filter_entry(move |entry| { + entry.depth() == 0 + || !entry.file_type().is_some_and(|kind| kind.is_dir()) + || !skipped( + &entry.file_name().to_string_lossy().to_lowercase(), + base + entry.depth(), + || entry.metadata().ok(), + ) + }) + .build(); + for entry in walk { + if self.quitting() { + return; + } + let Ok(entry) = entry else { continue }; + if entry.depth() == 0 { + continue; + } + let is_dir = entry.file_type().is_some_and(|kind| kind.is_dir()); + if let Some(home_path) = self.home_path(entry.path(), is_dir) { + items.push(home_path); + } + if items.len() >= 512 && !self.keep(&mut items) { + return; + } + } + self.keep(&mut items); + } + + fn home_path(&self, path: &Path, is_dir: bool) -> Option { + let relative = path.strip_prefix(&self.home).ok()?; + let path = relative.to_string_lossy().replace('\\', "/"); + Some(HomePath { + lower: path.to_lowercase(), + path, + is_dir, + }) + } +} + /// Folders directly under home that a cloud client syncs (matched without /// case; OneDrive's business folders read "OneDrive - Contoso"). const CLOUD_ROOT_PREFIXES: &[&str] = &[ @@ -54,57 +308,80 @@ struct HomePath { } impl HomeIndex { - /// Walk `home` (canonical). `cancelled` stops the walk early. - pub fn build(home: &Path, cancelled: &AtomicBool) -> Self { + /// Walk `home` (canonical), for at most [`WALK_DEADLINE`]. `cancel` + /// stops the walk early. + pub fn build(home: &Path, cancel: &WalkCancel) -> Self { + Self::build_within(home, cancel, WALK_DEADLINE) + } + + fn build_within(home: &Path, cancel: &WalkCancel, deadline: Duration) -> Self { let threads = std::thread::available_parallelism() .map(|n| n.get().min(8)) .unwrap_or(4); - let collected: Mutex> = Mutex::new(Vec::new()); - ignore::WalkBuilder::new(home) - .hidden(true) - .follow_links(false) - .git_ignore(true) - .git_global(true) - .git_exclude(true) - .ignore(true) - .parents(false) - .threads(threads) - .filter_entry(|entry| entry.depth() == 0 || !skipped(entry)) - .build_parallel() - .run(|| { - let collected = &collected; - let mut batch = Batch { - items: Vec::new(), - sink: collected, - }; - Box::new(move |entry| { - if cancelled.load(Ordering::Relaxed) { - return ignore::WalkState::Quit; - } - let Ok(entry) = entry else { - return ignore::WalkState::Continue; - }; - if entry.depth() == 0 { - return ignore::WalkState::Continue; - } - let Ok(relative) = entry.path().strip_prefix(home) else { - return ignore::WalkState::Continue; - }; - let path = relative.to_string_lossy().replace('\\', "/"); - batch.items.push(HomePath { - lower: path.to_lowercase(), - path, - is_dir: entry.file_type().is_some_and(|kind| kind.is_dir()), - }); - if batch.items.len() >= 512 && batch.flush() >= MAX_ENTRIES { - return ignore::WalkState::Quit; - } - ignore::WalkState::Continue - }) - }); - let mut entries = collected - .into_inner() - .unwrap_or_else(std::sync::PoisonError::into_inner); + let walk = Arc::new(Walk { + home: home.to_path_buf(), + queue: Mutex::new(Queue::default()), + changed: Condvar::new(), + stop: AtomicBool::new(false), + found: Mutex::new(Vec::new()), + skip: stalled().clone().unwrap_or_default(), + #[cfg(test)] + wakes: Default::default(), + }); + #[cfg(test)] + tests::LAST_WALK.with(|last| *last.borrow_mut() = Arc::downgrade(&walk)); + *cancel.walk.lock().unwrap_or_else(PoisonError::into_inner) = Arc::downgrade(&walk); + if cancel.is_cancelled() { + return Self { + entries: Vec::new(), + built: Instant::now(), + }; + } + walk.push(Unit { + dir: home.to_path_buf(), + depth: 0, + }); + let mut spawned = 0; + for _ in 0..threads { + let worker = walk.clone(); + match std::thread::Builder::new() + .name("workspace-search-home-walk".into()) + .spawn(move || worker.work()) + { + Ok(_) => spawned += 1, + Err(error) => tracing::warn!(%error, "home walk worker did not start"), + } + } + if spawned > 0 { + let ends = Instant::now() + deadline; + let mut queue = walk.queue(); + while !queue.quit && (queue.active > 0 || !queue.units.is_empty()) { + let left = ends.saturating_duration_since(Instant::now()); + if left.is_zero() { + break; + } + queue = walk + .changed + .wait_timeout(queue, left) + .unwrap_or_else(PoisonError::into_inner) + .0; + } + // Past the deadline (or cancelled): folders still being read + // are skipped by later walks until their reads return. + if !queue.reading.is_empty() { + tracing::warn!( + folders = ?queue.reading, + "home walk: answering without folders whose listing hasn't returned" + ); + stalled() + .get_or_insert_with(HashSet::new) + .extend(queue.reading.iter().cloned()); + } + drop(queue); + walk.quit(); + } + let mut entries = + std::mem::take(&mut *walk.found.lock().unwrap_or_else(PoisonError::into_inner)); entries.truncate(MAX_ENTRIES); Self { entries, @@ -172,47 +449,19 @@ impl HomeIndex { } } -struct Batch<'a> { - items: Vec, - sink: &'a Mutex>, -} - -impl Batch<'_> { - /// Move the batch into the shared list; returns the list's length. - fn flush(&mut self) -> usize { - let mut all = self - .sink - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - all.append(&mut self.items); - all.len() - } -} - -impl Drop for Batch<'_> { - fn drop(&mut self) { - if !self.items.is_empty() { - self.flush(); - } - } -} - fn depth(path: &str) -> usize { path.matches('/').count() } -/// Folders the walk never enters. Only folders are checked: listing a -/// cloud-only file's name downloads nothing, while listing a cloud-only -/// folder fetches its contents. -fn skipped(entry: &ignore::DirEntry) -> bool { - if !entry.file_type().is_some_and(|kind| kind.is_dir()) { - return false; - } - let name = entry.file_name().to_string_lossy().to_lowercase(); +/// Folders the walk never enters (`name` a folder's, `depth` below home). +/// Only folders are checked: listing a cloud-only file's name downloads +/// nothing, while listing a cloud-only folder fetches its contents. +fn skipped(name: &str, depth: usize, metadata: impl FnOnce() -> Option) -> bool { + let name = name.to_lowercase(); if name == ".git" || SKIPPED_DIRS.contains(&name.as_str()) { return true; } - if entry.depth() == 1 + if depth == 1 && (MACHINE_ROOTS.contains(&name.as_str()) || CLOUD_ROOT_PREFIXES.iter().any(|prefix| { name == *prefix @@ -223,9 +472,7 @@ fn skipped(entry: &ignore::DirEntry) -> bool { { return true; } - entry - .metadata() - .is_ok_and(|metadata| is_cloud_placeholder(&metadata)) + metadata().is_some_and(|metadata| is_cloud_placeholder(&metadata)) } /// A folder whose contents a cloud client fetches on first listing. @@ -295,7 +542,7 @@ mod tests { ".config/plan.md", "boxes/plan.md", ]); - let index = HomeIndex::build(&root, &AtomicBool::new(false)); + let index = HomeIndex::build(&root, &WalkCancel::default()); assert_eq!( paths(&index, "plan", NameKinds::Files), ["boxes/plan.md", "dev/notes/plan.md"] @@ -310,7 +557,7 @@ mod tests { "dev/comet-notes.md", "x/comets/a.rs", ]); - let index = HomeIndex::build(&root, &AtomicBool::new(false)); + let index = HomeIndex::build(&root, &WalkCancel::default()); assert_eq!( paths(&index, "comet", NameKinds::Directories), ["comet", "dev/comet", "x/comets", "dev/comet/src"] @@ -330,4 +577,196 @@ mod tests { ] ); } + + thread_local! { + pub(super) static LAST_WALK: std::cell::RefCell> = + const { std::cell::RefCell::new(Weak::new()) }; + } + + /// Folders whose read waits until released, as `open()` waits on a + /// privacy prompt nobody answers. + type Gate = Arc<(Mutex, Condvar)>; + type Entered = Arc; + static HELD: Mutex> = Mutex::new(Vec::new()); + + pub(super) fn held(dir: &Path) { + let gate = HELD + .lock() + .unwrap() + .iter() + .find(|(held, _, _)| held == dir) + .map(|(_, gate, entered)| (gate.clone(), entered.clone())); + if let Some((gate, entered)) = gate { + entered.fetch_add(1, Ordering::SeqCst); + let (open, changed) = &*gate; + let mut open = open.lock().unwrap(); + while !*open { + open = changed.wait(open).unwrap(); + } + } + } + + fn hold( + dir: &Path, + ) -> ( + Arc<(Mutex, Condvar)>, + Arc, + ) { + let gate = Arc::new((Mutex::new(false), Condvar::new())); + let entered = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + HELD.lock() + .unwrap() + .push((dir.to_path_buf(), gate.clone(), entered.clone())); + (gate, entered) + } + + fn release(gate: &(Mutex, Condvar)) { + *gate.0.lock().unwrap() = true; + gate.1.notify_all(); + } + + /// One folder never answers: the walk answers at its deadline with the + /// rest, its idle workers sleep instead of polling, and later walks skip + /// that folder (no second thread stuck in it) until its read returns. + #[test] + fn a_folder_that_never_answers_neither_holds_the_walk_nor_keeps_it_awake() { + let (_dir, root) = home(&[ + "Documents/proj/plan.md", + "dev/app/src/main.rs", + "dev/notes/plan.md", + "Music/song.txt", + ]); + let stuck = root.join("Documents"); + let (gate, entered) = hold(&stuck); + + let started = Instant::now(); + let index = + HomeIndex::build_within(&root, &WalkCancel::default(), Duration::from_millis(300)); + let walk = LAST_WALK + .with(|last| last.borrow().upgrade()) + .expect("the stuck worker holds the walk"); + assert!( + started.elapsed() < Duration::from_secs(3), + "answered at its deadline" + ); + assert_eq!( + paths(&index, "plan", NameKinds::Files), + ["dev/notes/plan.md"] + ); + assert_eq!(paths(&index, "song", NameKinds::Files), ["Music/song.txt"]); + assert!(stalled().as_ref().is_some_and(|s| s.contains(&stuck))); + // While it waited out the stuck folder, idle workers slept (a + // polling worker wakes ~300 times in 300 ms). + assert!( + walk.wakes.load(Ordering::Relaxed) < 50, + "{} wakes", + walk.wakes.load(Ordering::Relaxed) + ); + + // The walk is over: its free workers left; nothing wakes while the + // stuck one waits. + let wakes = walk.wakes.load(Ordering::Relaxed); + std::thread::sleep(Duration::from_millis(300)); + assert!( + walk.wakes.load(Ordering::Relaxed) - wakes <= 1, + "idle workers must sleep, not poll" + ); + assert_eq!( + Arc::strong_count(&walk), + 2, + "only the stuck worker is left (and this test)" + ); + drop(walk); + + // A second walk doesn't send another thread into it. + let index = HomeIndex::build_within(&root, &WalkCancel::default(), Duration::from_secs(5)); + assert_eq!( + entered.load(Ordering::SeqCst), + 1, + "skipped while still stuck" + ); + assert_eq!( + paths(&index, "plan", NameKinds::Files), + ["dev/notes/plan.md"] + ); + + // Its read returns: the next walk reads it again. + release(&gate); + let deadline = Instant::now() + Duration::from_secs(5); + while stalled().as_ref().is_some_and(|s| s.contains(&stuck)) { + assert!( + Instant::now() < deadline, + "the returned read clears the stall" + ); + std::thread::sleep(Duration::from_millis(10)); + } + let index = HomeIndex::build_within(&root, &WalkCancel::default(), Duration::from_secs(5)); + assert_eq!( + paths(&index, "plan", NameKinds::Files), + ["dev/notes/plan.md", "Documents/proj/plan.md"] + ); + } + + /// Closing the picker cancels a walk at once, stuck folder or not. + #[test] + fn a_cancel_ends_the_walk_at_once() { + let (_dir, root) = home(&["Desktop/a.txt", "dev/b.txt"]); + let stuck = root.join("Desktop"); + let (gate, _) = hold(&stuck); + let cancel = Arc::new(WalkCancel::default()); + let canceller = cancel.clone(); + let started = Instant::now(); + let timer = std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(100)); + canceller.cancel(); + }); + let _ = HomeIndex::build_within(&root, &cancel, Duration::from_secs(30)); + assert!( + started.elapsed() < Duration::from_secs(5), + "cancel ended the walk" + ); + timer.join().unwrap(); + release(&gate); + } + + /// Repositories keep their ignore files, at any depth. + #[test] + fn a_repository_keeps_its_ignore_files() { + let (_dir, root) = home(&[ + "code/work/app/.git/", + "code/work/app/.gitignore", + "code/work/app/src/lib.rs", + "code/work/app/target/debug/lib.rs", + "solo/.git/", + "solo/.gitignore", + "solo/dist/lib.rs", + "solo/lib.rs", + ]); + std::fs::write(root.join("code/work/app/.gitignore"), "target/\n").unwrap(); + std::fs::write(root.join("solo/.gitignore"), "dist/\n").unwrap(); + let index = HomeIndex::build(&root, &WalkCancel::default()); + assert_eq!( + paths(&index, "lib.rs", NameKinds::Files), + ["solo/lib.rs", "code/work/app/src/lib.rs"] + ); + } +} + +#[cfg(test)] +mod bench { + /// `HOME_WALK_BENCH= cargo test -p zeron-engine --lib home_walk_bench -- --ignored --nocapture` + #[test] + #[ignore] + fn home_walk_bench() { + let root = std::fs::canonicalize(std::env::var("HOME_WALK_BENCH").unwrap()).unwrap(); + for _ in 0..3 { + let started = std::time::Instant::now(); + let index = super::HomeIndex::build(&root, &Default::default()); + println!( + "WALK entries={} ms={}", + index.len(), + started.elapsed().as_millis() + ); + } + } } diff --git a/crates/engine/src/keep_running.rs b/crates/engine/src/keep_running.rs new file mode 100644 index 000000000..316ce6829 --- /dev/null +++ b/crates/engine/src/keep_running.rs @@ -0,0 +1,187 @@ +//! "Keep Zeron running after you quit" (Settings → General), this device's +//! own setting for its background engine ([`crate::managed`]). +//! +//! On (the default): quitting the app leaves the engine running, as it +//! always has — chats keep working, other devices keep reaching this one — +//! and idle agents are stopped as they would be anyway +//! ([`crate::sessions::AGENT_IDLE_STOP`]). +//! +//! Off: the app tells the engine as it quits ([`APP_QUITTING`]), and the +//! engine stops once nothing is pending. Work still running when the app +//! quits is never ended silently: the app asks first ("keep running until +//! they finish, or stop them now?"), and only "stop them now" stops it — +//! the person's own Stop on each chat. Any later call from this device's +//! app (it opened again) calls the stop off. +//! +//! [`APP_QUITTING`]: zeron_rpc::methods::APP_QUITTING + +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::Duration; + +use serde::{Deserialize, Serialize}; + +const FILE: &str = "background.json"; + +/// How long the engine must find nothing pending before it stops: a queued +/// message dispatches, a wake turn starts, within this. +pub const QUIET_BEFORE_STOP: Duration = Duration::from_secs(5); +/// How often a quit-when-idle engine looks. +const CHECK_EVERY: Duration = Duration::from_secs(1); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct BackgroundSettings { + #[serde(default = "on")] + pub keep_running_after_quit: bool, +} + +fn on() -> bool { + true +} + +impl Default for BackgroundSettings { + fn default() -> Self { + Self { + keep_running_after_quit: true, + } + } +} + +fn path(data_dir: &Path) -> PathBuf { + data_dir.join(FILE) +} + +/// The saved setting; the default when none is saved (or it can't be read). +pub fn load(data_dir: &Path) -> BackgroundSettings { + // (Bounded, and never a FIFO or device: a few bytes are expected.) + zeron_harness::bounded_file::read(&path(data_dir), 64 * 1024) + .ok() + .and_then(|bytes| serde_json::from_slice(&bytes).ok()) + .unwrap_or_default() +} + +/// Save it (atomically: a sibling file renamed over). +pub fn save(data_dir: &Path, settings: &BackgroundSettings) -> std::io::Result<()> { + std::fs::create_dir_all(data_dir)?; + let tmp = data_dir.join(format!(".{FILE}.{}.tmp", std::process::id())); + std::fs::write(&tmp, serde_json::to_vec(settings)?)?; + std::fs::rename(&tmp, path(data_dir)) +} + +/// What a quitting app is told, and decides on. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct QuitCheck { + pub keep_running: bool, + /// Chats whose work is still running or pending. + pub working: usize, +} + +/// What the engine has pending: turns running or waiting on a question, +/// and runtimes whose turn ended with background work still live. +pub fn pending(sessions: &crate::sessions::SessionsEngine) -> usize { + sessions.chats_with_work().len() +} + +/// A quit-when-idle request, shared between the RPC that sets it and the +/// watcher that acts on it. +#[derive(Clone, Default)] +pub struct QuitWhenIdle(Arc); + +impl QuitWhenIdle { + pub fn arm(&self) { + self.0.store(true, Ordering::SeqCst); + } + + /// The app is back: keep running. + pub fn disarm(&self) -> bool { + self.0.swap(false, Ordering::SeqCst) + } + + pub fn armed(&self) -> bool { + self.0.load(Ordering::SeqCst) + } +} + +/// Until armed and nothing has been pending for [`QUIET_BEFORE_STOP`]: +/// then the engine stops. `pending` is read every [`CHECK_EVERY`]. +pub async fn wait_until_idle_after_quit(quit: QuitWhenIdle, pending: impl Fn() -> usize) { + let mut quiet_since: Option = None; + let mut tick = tokio::time::interval(CHECK_EVERY); + tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + loop { + tick.tick().await; + if !quit.armed() || pending() > 0 { + quiet_since = None; + continue; + } + let since = *quiet_since.get_or_insert_with(tokio::time::Instant::now); + if since.elapsed() >= QUIET_BEFORE_STOP { + return; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::AtomicUsize; + + #[test] + fn the_setting_is_on_until_turned_off_and_survives_a_restart() { + let dir = tempfile::tempdir().unwrap(); + assert!(load(dir.path()).keep_running_after_quit); + save(dir.path(), &BackgroundSettings { keep_running_after_quit: false }).unwrap(); + assert!(!load(dir.path()).keep_running_after_quit); + std::fs::write(path(dir.path()), b"not json").unwrap(); + assert!(load(dir.path()).keep_running_after_quit, "unreadable reads as the default"); + } + + #[cfg(unix)] + #[test] + fn a_setting_file_that_is_a_device_is_not_read() { + let dir = tempfile::tempdir().unwrap(); + std::os::unix::fs::symlink("/dev/zero", path(dir.path())).unwrap(); + let started = std::time::Instant::now(); + assert!(load(dir.path()).keep_running_after_quit); + assert!(started.elapsed() < Duration::from_secs(1)); + } + + #[tokio::test(start_paused = true)] + async fn a_quit_engine_stops_only_once_its_work_is_done() { + let quit = QuitWhenIdle::default(); + let work = Arc::new(AtomicUsize::new(2)); + let waiting = { + let (quit, work) = (quit.clone(), work.clone()); + tokio::spawn(wait_until_idle_after_quit(quit, move || work.load(Ordering::SeqCst))) + }; + // Not armed: it never stops, idle or not. + work.store(0, Ordering::SeqCst); + tokio::time::sleep(Duration::from_secs(60)).await; + assert!(!waiting.is_finished()); + // Armed with work running: it waits for the work. + work.store(2, Ordering::SeqCst); + quit.arm(); + tokio::time::sleep(Duration::from_secs(3600)).await; + assert!(!waiting.is_finished(), "stopped with work running"); + // The work ends: stopped after a quiet stretch, not at once. + work.store(0, Ordering::SeqCst); + tokio::time::sleep(QUIET_BEFORE_STOP - Duration::from_secs(2)).await; + assert!(!waiting.is_finished()); + tokio::time::sleep(Duration::from_secs(4)).await; + assert!(waiting.is_finished()); + } + + #[tokio::test(start_paused = true)] + async fn the_app_coming_back_calls_the_stop_off() { + let quit = QuitWhenIdle::default(); + let waiting = tokio::spawn(wait_until_idle_after_quit(quit.clone(), || 0)); + quit.arm(); + tokio::time::sleep(Duration::from_secs(2)).await; + assert!(quit.disarm()); + tokio::time::sleep(Duration::from_secs(600)).await; + assert!(!waiting.is_finished()); + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index cf4ce525e..1ebc1423f 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -58,6 +58,7 @@ pub(crate) mod home_index; mod http_error; pub mod instance_lock; pub mod local_import; +pub mod keep_running; pub mod managed; mod hosted_models; mod model_catalogs; @@ -73,6 +74,7 @@ pub mod run_journal; pub mod runner; pub mod wake_trace; pub mod wake_update; +pub mod self_schedule; pub mod sessions; pub mod source_control; pub mod spaces; @@ -1405,6 +1407,16 @@ impl Engine { .core() .sessions .set_ipc_token_file(zeron_rpc::ipc_auth::agent_token_path(&config.data_dir)); + // Agents' Zeron tools come from this engine itself (before the port: + // a run revived on boot waits on the port, then reads both). + if let Err(err) = runtime + .core() + .sessions + .serve_mcp_endpoint(runtime.core().rpc_service()) + .await + { + tracing::warn!(error = %err, "agent MCP endpoint not served; agents get `zeron mcp`"); + } runtime.core().sessions.set_ipc_port(config.ipc_port); update_trial::prove_and_commit(runtime.core(), &config); wake_update::start(runtime.core()); diff --git a/crates/engine/src/managed.rs b/crates/engine/src/managed.rs index 967a85bac..b5843b7fa 100644 --- a/crates/engine/src/managed.rs +++ b/crates/engine/src/managed.rs @@ -55,28 +55,127 @@ pub fn launch(config: &EngineConfig, app_exe: &Path) -> std::io::Result { } /// IPC-only lifecycle control: `StopEngine` ends the background engine (the -/// app's sign-out and workspace switches restart it on the new profile). +/// app's sign-out and workspace switches restart it on the new profile), and +/// whether it outlives its app ([`crate::keep_running`]). struct ManagedRpc { inner: Arc, stop_tx: tokio::sync::mpsc::UnboundedSender<()>, + data_dir: PathBuf, + runtime: Arc>>, + quit: crate::keep_running::QuitWhenIdle, +} + +impl ManagedRpc { + async fn sessions(&self) -> Option { + self.runtime + .lock() + .await + .as_ref() + .map(|runtime| runtime.core().sessions.clone()) + } + + async fn doc_host(&self) -> Option { + self.runtime + .lock() + .await + .as_ref() + .map(|runtime| runtime.core().doc_host.clone()) + } } #[async_trait] impl RpcService for ManagedRpc { async fn handle(&self, method: &str, params: serde_json::Value) -> Result { - if method != methods::STOP_ENGINE { - return self.inner.handle(method, params).await; + use crate::keep_running as keep; + // This device's app (or command line) is here again after asking + // the engine to stop with it: it keeps running. + if zeron_rpc::caller().is_local() + && !matches!(method, methods::APP_QUITTING | methods::QUIT_CHECK) + && self.quit.disarm() + { + tracing::info!(method, "the app is back: the engine keeps running"); + } + match method { + methods::STOP_ENGINE => { + let stop_tx = self.stop_tx.clone(); + // Let the reply reach the client before the server is aborted. + tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(500)).await; + let _ = stop_tx.send(()); + }); + RpcReply::value(&serde_json::json!({ "ok": true })) + } + methods::GET_BACKGROUND_SETTINGS => RpcReply::value(&keep::load(&self.data_dir)), + methods::SET_BACKGROUND_SETTINGS => { + let settings: keep::BackgroundSettings = serde_json::from_value(params) + .map_err(|err| RpcError::BadParams(err.to_string()))?; + keep::save(&self.data_dir, &settings) + .map_err(|err| RpcError::Failed(format!("couldn't save: {err}")))?; + RpcReply::value(&settings) + } + methods::QUIT_CHECK => { + let working = match self.sessions().await { + Some(sessions) => sessions.chats_with_work().len(), + None => 0, + }; + RpcReply::value(&keep::QuitCheck { + keep_running: keep::load(&self.data_dir).keep_running_after_quit, + working, + }) + } + methods::APP_QUITTING => { + let stop_work = params + .get("stopWork") + .and_then(serde_json::Value::as_bool) + .unwrap_or(false); + let mut stopped = 0; + if stop_work + && let (Some(sessions), Some(doc_host)) = (self.sessions().await, self.doc_host().await) + { + // The person's own Stop, on each chat still working. + for chat in sessions.chats_with_work() { + match doc_host.queue_command( + &chat, + zeron_doc::SessionCommandPayload::Interrupt {}, + ) { + Ok(_) => stopped += 1, + Err(err) => tracing::warn!(chat, error = %err, "couldn't stop a chat as the app quit"), + } + } + } + tracing::info!(stop_work, stopped, "the app quit: stopping once nothing is pending"); + self.quit.arm(); + RpcReply::value(&serde_json::json!({ "ok": true, "stopped": stopped })) + } + _ => self.inner.handle(method, params).await, } - let stop_tx = self.stop_tx.clone(); - // Let the reply reach the client before the server is aborted. - tokio::spawn(async move { - tokio::time::sleep(Duration::from_millis(500)).await; - let _ = stop_tx.send(()); - }); - RpcReply::value(&serde_json::json!({ "ok": true })) } } +/// Once the app quit with "Keep Zeron running after you quit" off +/// ([`crate::keep_running`]): until nothing is pending (or there's no +/// runtime to have any). +async fn stop_after_quit( + runtime: Arc>>, + quit: crate::keep_running::QuitWhenIdle, +) { + let sessions = loop { + if let Some(runtime) = runtime.lock().await.as_ref() { + break Some(runtime.core().sessions.clone()); + } + if quit.armed() { + break None; + } + tokio::time::sleep(Duration::from_secs(1)).await; + }; + crate::keep_running::wait_until_idle_after_quit(quit, move || { + sessions + .as_ref() + .map_or(0, crate::keep_running::pending) + }) + .await; +} + /// Take the data directory, waiting out a predecessor that is still /// draining (a restart into a new version starts the replacement first). async fn acquire_lock(data_dir: &Path) -> anyhow::Result { @@ -235,9 +334,13 @@ impl Engine { let mut engine = deferred::DeferredEngine::prepare(&config).await?; let (stop_tx, mut stop_rx) = tokio::sync::mpsc::unbounded_channel(); + let quit = crate::keep_running::QuitWhenIdle::default(); let service: Arc = Arc::new(ManagedRpc { inner: engine.service.clone(), stop_tx: stop_tx.clone(), + data_dir: config.data_dir.clone(), + runtime: engine.runtime.clone(), + quit: quit.clone(), }); // The app is waiting on this port; bind as soon as the data dir is // ours, retrying while a predecessor's listener closes. @@ -299,6 +402,10 @@ impl Engine { stop_requested = true; Ok(()) } + () = stop_after_quit(engine.runtime.clone(), quit.clone()) => { + tracing::info!("the app quit and nothing is pending: stopping"); + Ok(()) + } _ = crate::wait_for_signed_out(&mut auth_state), if synced => { tracing::info!("signed out; stopping the synced background engine"); // A brief reply window for the SignOut RPC itself. diff --git a/crates/engine/src/plugins/mod.rs b/crates/engine/src/plugins/mod.rs index 72057f05f..fb8f130f0 100644 --- a/crates/engine/src/plugins/mod.rs +++ b/crates/engine/src/plugins/mod.rs @@ -1576,6 +1576,7 @@ impl Plugins { command, args: mcp.args.clone(), env: mcp.env.clone(), + http: None, }) }) .collect() diff --git a/crates/engine/src/rpc.rs b/crates/engine/src/rpc.rs index 4914e63ba..1ff3b21c2 100644 --- a/crates/engine/src/rpc.rs +++ b/crates/engine/src/rpc.rs @@ -2004,6 +2004,7 @@ impl EngineRpc { child_kind, cloud: _, } => { + tracing::debug!(target: "first_word", chat = %chat_id, "create chat"); // A voice orchestrator is hidden, so the chats it creates are // the user's top-level sessions rather than its side chats. let parent_chat_id = parent_chat_id @@ -2151,6 +2152,7 @@ impl EngineRpc { self.workspace.change_sidebar_pin(&change).map_err(failed) } MutateParams::SetChatConfig { chat_id, config } => { + tracing::debug!(target: "first_word", chat = %chat_id, "set chat config"); // A chat may change provider between turns, never during one: // the live run belongs to the old harness and would keep // writing into the transcript the new one is about to read. @@ -3713,10 +3715,25 @@ impl RpcService for EngineRpc { .transpose() .map_err(|e| RpcError::BadParams(e.to_string()))?; let p: ChatParams = parse_params(params)?; + // Someone reading the chat (this device's app, or another + // device) keeps its parked agent for the longer idle window: + // its person is likely to write next. Not an agent reading + // it through its tools. + let viewer = (!matches!(zeron_rpc::caller(), zeron_rpc::Caller::Agent { .. })) + .then(|| self.sessions.retain_viewer(&p.chat_id)); if let Some(window) = window { return windowed_doc_messages_stream(self.doc_host.clone(), p.chat_id, window) .await - .map(RpcReply::Stream); + .map(|stream| { + RpcReply::Stream( + stream + .map(move |frame| { + let _held = &viewer; + frame + }) + .boxed(), + ) + }); } // An open transcript keeps its chat's checkout diff live (and // discardable) even when the chat is archived; the guard lives @@ -3736,7 +3753,7 @@ impl RpcService for EngineRpc { Ok(RpcReply::Stream( stream .map(move |frame| { - let _held = &interest; + let _held = (&interest, &viewer); frame }) .boxed(), diff --git a/crates/engine/src/run_journal.rs b/crates/engine/src/run_journal.rs index 636da386f..9fe50725f 100644 --- a/crates/engine/src/run_journal.rs +++ b/crates/engine/src/run_journal.rs @@ -11,12 +11,24 @@ //! //! Bounded-window compaction is deferred (whole file kept for now, per M2 scope); a torn //! trailing line from a crash mid-write is tolerated everywhere. +//! +//! Lines are written in batches: a streaming agent sends tens of events a second, and +//! a `write` each (under the one lock every chat's appends share) was a large part of +//! a streaming engine's work. An event waits in memory for [`FLUSH_AFTER`] while +//! appends keep coming (or until [`RunJournal::flush`], which a run calls with each doc +//! commit), a turn's `Done` is written at once, and every read writes what's waiting +//! first. What the last appends left waiting (a turn's usage after its `Done`) a +//! flusher thread writes [`IDLE_FLUSH`] later: it sleeps while nothing waits. +//! +//! A write that fails keeps what didn't reach the file to retry with the next write, +//! so a passing failure leaves no gap; only past [`MAX_PENDING`] is it dropped, and +//! that is logged as an error. use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; -use std::sync::{Mutex, MutexGuard, PoisonError}; +use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError}; use serde::{Deserialize, Serialize}; @@ -36,34 +48,186 @@ struct JournalLine { event: AgentEvent, } +/// A [`JournalLine`] to write, borrowing its event. +#[derive(Serialize)] +struct JournalLineRef<'a> { + seq: u64, + event: &'a AgentEvent, +} + +/// The longest an appended line waits in memory before it's written. +const FLUSH_AFTER: std::time::Duration = std::time::Duration::from_millis(100); +/// Lines waiting past this many bytes are written at once. +const FLUSH_BYTES: usize = 64 * 1024; +/// The longest a line waits when no append follows it (the flusher thread). +const IDLE_FLUSH: std::time::Duration = std::time::Duration::from_secs(1); +/// What a chat's journal keeps to retry while its writes fail; past it, what waits is +/// dropped (a gap in the journal, logged as an error). +const MAX_PENDING: usize = 8 * 1024 * 1024; + struct ChatJournal { file: File, next_seq: u64, /// True when the file ends without a newline (torn write) — the next append /// starts with one so the torn line stays isolated. needs_newline: bool, + /// Lines appended and not yet written, and since when (or since the last failed + /// write of them, which the flusher retries from). + pending: Vec, + pending_since: Option, + /// The last write failed (logged once until one succeeds). + failing: bool, +} + +impl ChatJournal { + /// Write what's waiting. A failure keeps the unwritten rest to retry (it + /// continues the line it stopped in, so nothing is torn or written twice) and + /// is `Ok`; `Err` only when lines were dropped, past [`MAX_PENDING`]. + fn write_pending(&mut self, chat: &str) -> Result<(), JournalError> { + if self.pending.is_empty() { + return Ok(()); + } + let mut written = 0; + let result = loop { + match self.file.write(&self.pending[written..]) { + Ok(0) => break Err(std::io::Error::from(std::io::ErrorKind::WriteZero)), + Ok(n) => { + written += n; + if written == self.pending.len() { + break Ok(()); + } + } + Err(err) if err.kind() == std::io::ErrorKind::Interrupted => {} + Err(err) => break Err(err), + } + }; + self.pending.drain(..written); + let Err(err) = result else { + self.pending_since = None; + if std::mem::take(&mut self.failing) { + tracing::info!(chat = %chat, "journal writes recovered"); + } + return Ok(()); + }; + if self.pending.len() <= MAX_PENDING { + if !std::mem::replace(&mut self.failing, true) { + tracing::warn!(chat = %chat, error = %err, waiting = self.pending.len(), "journal write failed; retrying"); + } + // Retried by the next append, or the flusher a while from now. + self.pending_since = Some(std::time::Instant::now()); + return Ok(()); + } + let lost = self.pending.iter().filter(|b| **b == b'\n').count(); + self.pending.clear(); + self.pending_since = None; + // What reached the file may end mid-line: the next append starts with a + // newline, so that torn line stays isolated. + self.needs_newline = true; + tracing::error!(chat = %chat, error = %err, lost, "journal write failed; events dropped"); + Err(err.into()) + } } /// Append-only JSONL journal store, one file per chat. pub struct RunJournal { dir: PathBuf, + shared: Arc, + flusher: Option>, +} + +/// What the journal shares with its flusher thread. +struct Shared { open_files: Mutex>, + /// Lines started waiting, or the journal closed. + wake: Condvar, + closed: std::sync::atomic::AtomicBool, +} + +impl Shared { + fn lock(&self) -> MutexGuard<'_, HashMap> { + self.open_files + .lock() + .unwrap_or_else(PoisonError::into_inner) + } + + /// The flusher: writes what has waited [`IDLE_FLUSH`] with no append to take it + /// along; asleep while nothing waits. + fn flush_idle(&self) { + use std::sync::atomic::Ordering; + let mut files = self.lock(); + while !self.closed.load(Ordering::SeqCst) { + let now = std::time::Instant::now(); + let mut next: Option = None; + for (chat, journal) in files.iter_mut() { + let Some(since) = journal.pending_since else { + continue; + }; + if since + IDLE_FLUSH <= now { + // (A drop is logged where it happens.) + let _ = journal.write_pending(chat); + } + if let Some(since) = journal.pending_since { + let due = since + IDLE_FLUSH; + next = Some(next.map_or(due, |next| next.min(due))); + } + } + files = match next { + Some(due) => { + self.wake + .wait_timeout(files, due.saturating_duration_since(now)) + .unwrap_or_else(PoisonError::into_inner) + .0 + } + None => self + .wake + .wait(files) + .unwrap_or_else(PoisonError::into_inner), + }; + } + } +} + +impl Drop for RunJournal { + fn drop(&mut self) { + self.shared + .closed + .store(true, std::sync::atomic::Ordering::SeqCst); + { + // (Under the lock: the flusher is waiting, or sees `closed` first.) + let _files = self.lock(); + self.shared.wake.notify_all(); + } + if let Some(flusher) = self.flusher.take() { + let _ = flusher.join(); + } + self.flush_all(); + } } impl RunJournal { pub fn open(dir: impl AsRef) -> Result { let dir = dir.as_ref().to_path_buf(); std::fs::create_dir_all(&dir)?; + let shared = Arc::new(Shared { + open_files: Mutex::new(HashMap::new()), + wake: Condvar::new(), + closed: std::sync::atomic::AtomicBool::new(false), + }); + let flusher = { + let shared = shared.clone(); + std::thread::Builder::new() + .name("journal-flush".into()) + .spawn(move || shared.flush_idle())? + }; Ok(Self { dir, - open_files: Mutex::new(HashMap::new()), + shared, + flusher: Some(flusher), }) } fn lock(&self) -> MutexGuard<'_, HashMap> { - self.open_files - .lock() - .unwrap_or_else(PoisonError::into_inner) + self.shared.lock() } fn path_for(&self, chat_id: &str) -> PathBuf { @@ -179,6 +343,14 @@ impl RunJournal { // comfortably exceeds concurrent runs, so eviction stays rare. const OPEN_FILE_CAP: usize = 16; if files.len() >= OPEN_FILE_CAP { + for (chat, journal) in files.iter_mut() { + // (A failure is logged by the write; what still waits is lost + // with the entry, as an error.) + let _ = journal.write_pending(chat); + if !journal.pending.is_empty() { + tracing::error!(chat = %chat, bytes = journal.pending.len(), "journal closed with unwritten events"); + } + } files.clear(); } let path = self.path_for(chat_id); @@ -190,6 +362,9 @@ impl RunJournal { file, next_seq, needs_newline, + pending: Vec::new(), + pending_since: None, + failing: false, }, ); } @@ -200,23 +375,45 @@ impl RunJournal { ))); }; let seq = journal.next_seq; - let line = serde_json::to_string(&JournalLine { - seq, - event: event.clone(), - })?; - let mut buf = Vec::with_capacity(line.len() + 2); - if journal.needs_newline { - buf.push(b'\n'); + let line = serde_json::to_vec(&JournalLineRef { seq, event })?; + if journal.pending.is_empty() { + if std::mem::take(&mut journal.needs_newline) { + journal.pending.push(b'\n'); + } + journal.pending_since = Some(std::time::Instant::now()); + // The flusher writes it if no append comes to. + self.shared.wake.notify_one(); } - buf.extend_from_slice(line.as_bytes()); - buf.push(b'\n'); - journal.file.write_all(&buf)?; - journal.file.flush()?; - journal.needs_newline = false; + journal.pending.extend_from_slice(&line); + journal.pending.push(b'\n'); journal.next_seq = seq + 1; + let due = matches!(event, AgentEvent::Done { .. }) + || journal.pending.len() >= FLUSH_BYTES + || journal + .pending_since + .is_some_and(|since| since.elapsed() >= FLUSH_AFTER); + if due { + journal.write_pending(chat_id)?; + } Ok(seq) } + /// Write what's waiting of a chat's journal (a run calls this with each + /// doc commit, so the journal is never behind its transcript). + pub fn flush(&self, chat_id: &str) { + if let Some(journal) = self.lock().get_mut(chat_id) { + // (A failure is logged by the write.) + let _ = journal.write_pending(chat_id); + } + } + + /// Write what's waiting of every chat's journal. + pub fn flush_all(&self) { + for (chat, journal) in self.lock().iter_mut() { + let _ = journal.write_pending(chat); + } + } + /// Events with `seq > after_seq`, in order. A cursor ahead of the last issued seq is /// from a previous era (file replaced) — falls back to a full replay, mirroring zeron. pub fn replay( @@ -224,6 +421,7 @@ impl RunJournal { chat_id: &str, after_seq: u64, ) -> Result, JournalError> { + self.flush(chat_id); let path = self.path_for(chat_id); if !path.exists() { return Ok(Vec::new()); @@ -236,6 +434,7 @@ impl RunJournal { /// The last event in a chat's journal, if any (ignores a torn tail line). pub fn last_event(&self, chat_id: &str) -> Result, JournalError> { + self.flush(chat_id); last_valid_line(&self.path_for(chat_id)) } @@ -246,6 +445,7 @@ impl RunJournal { &self, chat_id: &str, ) -> Result>, JournalError> { + self.flush(chat_id); RevEvents::open(&self.path_for(chat_id)) } @@ -262,6 +462,7 @@ impl RunJournal { chat_id: &str, tool_id: &str, ) -> Result, JournalError> { + self.flush(chat_id); let path = self.path_for(chat_id); let Some(mut lines) = RevLines::open(&path)? else { return Ok(None); @@ -308,6 +509,7 @@ impl RunJournal { /// Crash-recovery scan: chat ids whose journal's last event is NOT a `Done` — their /// runs died mid-stream and need recovery (stamp `aborted`, close the journal). pub fn stale_sessions(&self) -> Result, JournalError> { + self.flush_all(); let mut stale = Vec::new(); for entry in std::fs::read_dir(&self.dir)? { let entry = entry?; @@ -596,6 +798,93 @@ mod tests { assert_eq!(journal.replay("chat-1", 99).unwrap().len(), 3); } + /// The lines on disk, read without the journal (whose reads write what + /// waits first). + fn on_disk(dir: &Path, chat: &str) -> Vec { + read_lines(&dir.join(format!("{chat}.jsonl"))) + .unwrap() + .into_iter() + .map(|(seq, _)| seq) + .collect() + } + + /// A turn's usage comes after its `Done`, and then nothing: it reaches + /// the file within a second or so, with no append or read to take it. + #[test] + fn an_event_left_waiting_is_written_without_another_append() { + let dir = tempfile::tempdir().unwrap(); + let journal = RunJournal::open(dir.path()).unwrap(); + journal.append("chat-1", &text("a")).unwrap(); + journal.append("chat-1", &done()).unwrap(); + journal.append("chat-1", &text("usage")).unwrap(); + assert_eq!(on_disk(dir.path(), "chat-1"), vec![1, 2], "batched still"); + let deadline = std::time::Instant::now() + IDLE_FLUSH * 5; + while on_disk(dir.path(), "chat-1") != vec![1, 2, 3] { + assert!( + std::time::Instant::now() < deadline, + "the waiting event was never written" + ); + std::thread::sleep(std::time::Duration::from_millis(50)); + } + } + + /// Writes that fail keep what waits and retry it: once the file takes + /// writes again, every event is there, in order, with no gap. + #[cfg(unix)] + #[test] + fn a_failed_write_is_retried_not_left_as_a_gap() { + let dir = tempfile::tempdir().unwrap(); + let journal = RunJournal::open(dir.path()).unwrap(); + journal.append("chat-1", &text("a")).unwrap(); + journal.flush("chat-1"); + let path = dir.path().join("chat-1.jsonl"); + // A handle that can't write: every write fails (EBADF). + let writable = { + let mut files = journal.lock(); + let entry = files.get_mut("chat-1").unwrap(); + std::mem::replace(&mut entry.file, File::open(&path).unwrap()) + }; + assert_eq!(journal.append("chat-1", &text("b")).unwrap(), 2); + assert_eq!( + journal.append("chat-1", &done()).unwrap(), + 3, + "kept to retry" + ); + journal.flush("chat-1"); + assert_eq!(on_disk(dir.path(), "chat-1"), vec![1]); + journal.lock().get_mut("chat-1").unwrap().file = writable; + assert_eq!(journal.append("chat-1", &text("c")).unwrap(), 4); + journal.flush("chat-1"); + assert_eq!(on_disk(dir.path(), "chat-1"), vec![1, 2, 3, 4]); + } + + /// Past [`MAX_PENDING`] what waits is dropped: reported as an error, and + /// the journal goes on from a fresh line. + #[cfg(unix)] + #[test] + fn a_write_failing_past_the_cap_reports_the_drop() { + let dir = tempfile::tempdir().unwrap(); + let journal = RunJournal::open(dir.path()).unwrap(); + journal.append("chat-1", &text("a")).unwrap(); + journal.flush("chat-1"); + let path = dir.path().join("chat-1.jsonl"); + let writable = { + let mut files = journal.lock(); + let entry = files.get_mut("chat-1").unwrap(); + std::mem::replace(&mut entry.file, File::open(&path).unwrap()) + }; + let big = "x".repeat(1024 * 1024); + let mut failed = false; + for _ in 0..(MAX_PENDING / big.len() + 2) { + failed |= journal.append("chat-1", &text(&big)).is_err(); + } + assert!(failed, "the drop was not reported"); + journal.lock().get_mut("chat-1").unwrap().file = writable; + let next = journal.append("chat-1", &done()).unwrap(); + journal.flush("chat-1"); + assert_eq!(on_disk(dir.path(), "chat-1").last(), Some(&next)); + } + #[test] fn seq_continues_across_reopen() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/engine/src/self_schedule.rs b/crates/engine/src/self_schedule.rs new file mode 100644 index 000000000..749b72ce1 --- /dev/null +++ b/crates/engine/src/self_schedule.rs @@ -0,0 +1,379 @@ +//! What an agent scheduled for itself inside its own process: Claude's +//! `ScheduleWakeup` (a `/loop`'s next turn) and its session crons +//! (`CronCreate`). Both die with the process, so an idle agent holding one +//! is never stopped (see the idle reaper in `sessions`). +//! +//! Read from the agent's own tool calls and their results (Claude Code +//! 2.1.322's tool schemas): +//! +//! - `ScheduleWakeup { delaySeconds, prompt, reason }` sets the loop's one +//! next wake-up, `delaySeconds` from now, clamped to [60, 3600] by the +//! CLI; each call replaces the last, and `{ stop: true }` ends the loop. +//! The CLI never says which wake turn a wake-up started (a background +//! task ending starts one too), so the wake-up counts as pending until +//! its time has come: a wake turn from then on is its own, and past +//! [`WAKE_GRACE`] it has fired whether or not a turn was seen. +//! - `CronCreate` answers with the job's id; `CronDelete { id }` cancels +//! that job. A cron counts from a successful create until a successful +//! delete of its id, each id once. + +use std::collections::{HashMap, HashSet}; +use std::time::Duration; + +use tokio::time::Instant; +use zeron_proto::{AgentEvent, ToolCall}; + +/// How long past its time a wake-up is still waited for. +pub const WAKE_GRACE: Duration = Duration::from_secs(60); +/// A wake turn this close before a wake-up's time is taken as its firing. +const WAKE_EARLY: Duration = Duration::from_secs(10); +/// `delaySeconds` as the CLI clamps it. +const WAKE_DELAY: (f64, f64) = (60.0, 3600.0); + +/// A pending wake-up: when it fires, if its call said. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Wakeup { + due: Option, +} + +#[derive(Default, Debug)] +pub struct SelfScheduled { + wakeup: Option, + /// The last `ScheduleWakeup` call and the wake-up pending before it, + /// pending again if the call fails. + wakeup_call: Option<(String, Option)>, + /// `CronCreate` calls awaiting their result. + cron_calls: HashSet, + /// `CronDelete` calls awaiting their result, with the id each cancels. + delete_calls: HashMap, + /// Each live cron's create result (it names the job's id). + crons: Vec, + /// Ids already cancelled: a second delete of one changes nothing. + deleted: HashSet, +} + +impl SelfScheduled { + /// Take in one of the agent's events (its own, not a subagent's). + pub fn observe(&mut self, event: &AgentEvent, now: Instant) { + match event { + AgentEvent::ToolCall { + id, + call: ToolCall::Unknown { name, input }, + } => match name.as_str() { + "ScheduleWakeup" => { + let field = |key: &str| input.as_ref().and_then(|input| input.get(key)); + let next = if field("stop").and_then(|v| v.as_bool()) == Some(true) { + None + } else { + let delay = field("delaySeconds") + .and_then(|v| v.as_f64()) + .filter(|delay| delay.is_finite()); + Some(Wakeup { + due: delay.map(|delay| { + now + Duration::from_secs_f64( + delay.clamp(WAKE_DELAY.0, WAKE_DELAY.1), + ) + }), + }) + }; + // (A harness may send a call again as it learns more: + // what was pending before it is still what it replaced.) + let before = match &self.wakeup_call { + Some((call, before)) if call == id => *before, + _ => self.wakeup, + }; + self.wakeup_call = Some((id.clone(), before)); + self.wakeup = next; + } + "CronCreate" => { + self.cron_calls.insert(id.clone()); + } + "CronDelete" => { + if let Some(cron) = input + .as_ref() + .and_then(|input| input.get("id")) + .and_then(|v| v.as_str()) + { + self.delete_calls.insert(id.clone(), cron.to_owned()); + } + } + _ => {} + }, + AgentEvent::ToolResult { + id, + is_error, + output, + .. + } => { + if let Some((call, before)) = &self.wakeup_call + && call == id + { + if *is_error { + self.wakeup = *before; + } + self.wakeup_call = None; + } + if self.cron_calls.remove(id) && !is_error { + self.crons.push(output.clone().unwrap_or_default()); + } + if let Some(cron) = self.delete_calls.remove(id) + && !is_error + && self.deleted.insert(cron.clone()) + { + // The cron its create result names; failing that (a + // result that doesn't say), the oldest, as a count would. + let at = self + .crons + .iter() + .position(|text| names(text, &cron)) + .or_else(|| (!self.crons.is_empty()).then_some(0)); + if let Some(at) = at { + self.crons.remove(at); + } + } + } + _ => {} + } + } + + /// The runtime started a turn on its own (a wake turn): the wake-up's, + /// if its time has come, or one whose time no call said. + pub fn woke(&mut self, now: Instant) { + if self + .wakeup + .is_some_and(|w| w.due.is_none_or(|due| now + WAKE_EARLY >= due)) + { + self.wakeup = None; + } + } + + /// Why the agent must not be stopped now, if anything it scheduled is + /// still to come. + pub fn pending(&self, now: Instant) -> Option<&'static str> { + if self + .wakeup + .is_some_and(|w| w.due.is_none_or(|due| now < due + WAKE_GRACE)) + { + Some("the agent scheduled a wake-up") + } else if !self.crons.is_empty() { + Some("the agent scheduled a cron") + } else { + None + } + } +} + +/// Whether `text` names `id` as a whole word. +fn names(text: &str, id: &str) -> bool { + let word = |c: Option| c.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '-'); + !id.is_empty() + && text.match_indices(id).any(|(at, _)| { + !word(text[..at].chars().next_back()) && !word(text[at + id.len()..].chars().next()) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn call(id: &str, name: &str, input: serde_json::Value) -> AgentEvent { + AgentEvent::ToolCall { + id: id.into(), + call: ToolCall::Unknown { + name: name.into(), + input: Some(input), + }, + } + } + + fn result(id: &str, is_error: bool, output: &str) -> AgentEvent { + AgentEvent::ToolResult { + id: id.into(), + is_error, + output: Some(output.into()), + diff: None, + preview_paths: Vec::new(), + image_payloads: Vec::new(), + exit_code: None, + } + } + + fn mins(n: u64) -> Duration { + Duration::from_secs(n * 60) + } + + #[test] + fn a_wake_up_is_pending_until_its_time_whatever_wakes_the_agent_before() { + let t0 = Instant::now(); + let mut s = SelfScheduled::default(); + s.observe( + &call( + "w1", + "ScheduleWakeup", + serde_json::json!({ "delaySeconds": 1200 }), + ), + t0, + ); + s.observe(&result("w1", false, "scheduled"), t0); + // A background task's wake turn, long before: still pending. + s.woke(t0 + mins(2)); + assert!(s.pending(t0 + mins(3)).is_some()); + // Its own wake turn. + s.woke(t0 + mins(20)); + assert_eq!(s.pending(t0 + mins(20)), None); + } + + #[test] + fn a_wake_up_no_turn_was_seen_for_lapses_after_its_grace() { + let t0 = Instant::now(); + let mut s = SelfScheduled::default(); + // Clamped up to 60 s by the CLI. + s.observe( + &call( + "w1", + "ScheduleWakeup", + serde_json::json!({ "delaySeconds": 5 }), + ), + t0, + ); + assert!(s.pending(t0 + Duration::from_secs(100)).is_some()); + assert_eq!(s.pending(t0 + Duration::from_secs(60) + WAKE_GRACE), None); + } + + #[test] + fn each_call_replaces_the_wake_up_and_stop_ends_the_loop() { + let t0 = Instant::now(); + let mut s = SelfScheduled::default(); + for (i, n) in [600, 900, 1200].into_iter().enumerate() { + s.observe( + &call( + &format!("w{i}"), + "ScheduleWakeup", + serde_json::json!({ "delaySeconds": n }), + ), + t0, + ); + } + s.woke(t0 + mins(20)); + assert_eq!(s.pending(t0 + mins(20)), None, "one wake-up, not three"); + s.observe( + &call( + "w3", + "ScheduleWakeup", + serde_json::json!({ "delaySeconds": 600 }), + ), + t0, + ); + s.observe( + &call("w4", "ScheduleWakeup", serde_json::json!({ "stop": true })), + t0, + ); + assert_eq!(s.pending(t0), None); + } + + #[test] + fn a_failed_wake_up_call_leaves_the_one_before() { + let t0 = Instant::now(); + let mut s = SelfScheduled::default(); + s.observe( + &call( + "w1", + "ScheduleWakeup", + serde_json::json!({ "delaySeconds": 600 }), + ), + t0, + ); + s.observe( + &call("w2", "ScheduleWakeup", serde_json::json!({ "stop": true })), + t0, + ); + s.observe(&result("w2", true, "error"), t0); + assert!(s.pending(t0).is_some()); + // Without a delay (no input seen): pending until a wake turn. + let mut s = SelfScheduled::default(); + s.observe( + &AgentEvent::ToolCall { + id: "w".into(), + call: ToolCall::Unknown { + name: "ScheduleWakeup".into(), + input: None, + }, + }, + t0, + ); + assert!(s.pending(t0 + mins(600)).is_some()); + s.woke(t0 + mins(1)); + assert_eq!(s.pending(t0 + mins(1)), None); + } + + #[test] + fn crons_count_by_id_and_a_second_delete_of_one_changes_nothing() { + let t0 = Instant::now(); + let mut s = SelfScheduled::default(); + s.observe( + &call( + "c1", + "CronCreate", + serde_json::json!({ "cron": "*/5 * * * *" }), + ), + t0, + ); + s.observe( + &result( + "c1", + false, + "Scheduled recurring job a1b2c3d4 (*/5 * * * *)", + ), + t0, + ); + s.observe( + &call( + "c2", + "CronCreate", + serde_json::json!({ "cron": "7 * * * *" }), + ), + t0, + ); + s.observe( + &result("c2", false, "Scheduled recurring job e5f6a7b8 (7 * * * *)"), + t0, + ); + // A create that failed holds nothing. + s.observe(&call("c3", "CronCreate", serde_json::json!({})), t0); + s.observe(&result("c3", true, "invalid cron"), t0); + for call_id in ["d1", "d2"] { + s.observe( + &call( + call_id, + "CronDelete", + serde_json::json!({ "id": "a1b2c3d4" }), + ), + t0, + ); + s.observe(&result(call_id, false, "Cancelled a1b2c3d4"), t0); + } + assert_eq!(s.pending(t0), Some("the agent scheduled a cron")); + // A failed delete cancels nothing. + s.observe( + &call("d3", "CronDelete", serde_json::json!({ "id": "e5f6a7b8" })), + t0, + ); + s.observe(&result("d3", true, "no such job"), t0); + assert!(s.pending(t0).is_some()); + s.observe( + &call("d4", "CronDelete", serde_json::json!({ "id": "e5f6a7b8" })), + t0, + ); + s.observe(&result("d4", false, "Cancelled e5f6a7b8"), t0); + assert_eq!(s.pending(t0), None); + } + + #[test] + fn an_id_is_named_only_as_a_whole_word() { + assert!(names("job abc123 (hourly)", "abc123")); + assert!(names("abc123", "abc123")); + assert!(!names("job abc1234", "abc123")); + assert!(!names("job xabc123", "abc123")); + assert!(!names("anything", "")); + } +} diff --git a/crates/engine/src/sessions.rs b/crates/engine/src/sessions.rs index 6f5f098d1..9ac3ba7ef 100644 --- a/crates/engine/src/sessions.rs +++ b/crates/engine/src/sessions.rs @@ -289,6 +289,11 @@ struct Inner { /// Where this engine publishes its IPC bearer (`{data_dir}/ipc-token`), /// handed to the injected `zeron mcp` as a PATH, never the token itself. ipc_token_file: Mutex>, + /// The engine's own agent MCP endpoint ([`SessionsEngine::serve_mcp_endpoint`]): + /// runs get a token for it instead of a `zeron mcp` process. + mcp_endpoint: Mutex>>, + /// Per chat: who has its transcript open now, and when the last left. + viewers: Arc, journal: Arc, registry: Arc, /// Set-once (first wins), cleared on runtime retirement: sessions and @@ -335,6 +340,112 @@ struct Inner { plugins: OnceLock, } +/// How long a runtime parked between turns with nothing pending keeps its +/// agent process when its driver resumes sessions whole +/// ([`Harness::stops_when_idle`]). Then it is stopped, and the next message +/// resumes the session (`--resume`), paying a cold start once. +/// +/// Measured (Claude CLI 2.1.322 against a local replay server, M5 Max): a +/// parked CLI holds ~130 MB phys_footprint (~200 MB with a real login and +/// plugins) and keeps waking at rest; a stopped one costs the next message +/// ~0.18 s more before its request leaves (0.22 s resumed vs 0.04 s warm, +/// medians of 5), with the whole context back. Two minutes covers reading +/// a reply and writing the next message in one sitting, so a conversation +/// in progress stays warm, while a chat left alone gives its memory back +/// soon. +/// +/// Two guards don't trust the agent's own report of its work: the window +/// applies only to a CLI known to make that report (Claude's version gate, +/// in its driver), and before stopping one the engine looks at its +/// process's live children — anything but its configured MCP servers holds +/// it ([`zeron_harness::process::unexpected_descendants`]). +pub const AGENT_IDLE_STOP: std::time::Duration = std::time::Duration::from_secs(120); + +/// The idle window of drivers not stopped after [`AGENT_IDLE_STOP`]. +const SESSION_IDLE: std::time::Duration = std::time::Duration::from_secs(30 * 60); + +/// A live subagent silent this long is presumed lost (its terminal signal +/// went missing) and no longer holds its runtime. +const SUBAGENT_SILENCE: std::time::Duration = std::time::Duration::from_secs(4 * 60 * 60); + +/// [`AGENT_IDLE_STOP`] for a chat open nowhere: nobody is reading the reply, +/// so a next message is unlikely soon. Counted from the later of the turn's +/// end and the moment its last viewer left. Measured (the ademark A5 shape: +/// five chats seeded, then at rest): its agents gone within 30 s, against +/// two minutes; the next message to such a chat costs the resume +/// (~0.2 s before its request leaves). +pub const UNWATCHED_IDLE_STOP: std::time::Duration = std::time::Duration::from_secs(30); + +/// Who has a chat's transcript open: the app on this device or another +/// device ([`SessionsEngine::retain_viewer`]), and when the last one left. +#[derive(Default)] +struct Viewers { + open: Mutex>, + /// Bumped on every change: parked runs re-check their idle window. + changed: OnceLock>, +} + +#[derive(Default, Clone, Copy)] +struct ViewState { + count: usize, + /// When the count last fell to zero. + left_at: Option, +} + +impl Viewers { + fn sender(&self) -> &watch::Sender { + self.changed.get_or_init(|| watch::channel(0).0) + } + + fn bump(&self) { + self.sender().send_modify(|generation| *generation = generation.wrapping_add(1)); + } + + /// Whether the chat is open anywhere now, and when it was last left. + fn state(&self, chat_id: &str) -> ViewState { + lock(&self.open).get(chat_id).copied().unwrap_or_default() + } +} + +/// One open view of a chat's transcript ([`SessionsEngine::retain_viewer`]). +pub struct Viewer { + viewers: Arc, + chat_id: String, +} + +impl Drop for Viewer { + fn drop(&mut self) { + { + let mut open = lock(&self.viewers.open); + if let Some(state) = open.get_mut(&self.chat_id) { + state.count = state.count.saturating_sub(1); + if state.count == 0 { + state.left_at = Some(tokio::time::Instant::now()); + } + } + } + self.viewers.bump(); + } +} + +/// The idle window that applies to a chat now: the longer one while its +/// transcript is open anywhere. +fn window_now( + inner: &Inner, + chat_id: &str, + watched_idle: std::time::Duration, + unwatched_idle: std::time::Duration, +) -> std::time::Duration { + if inner.view_state(chat_id).0 { + watched_idle + } else { + unwatched_idle + } +} + +/// The engine binary's stdio-to-HTTP MCP forwarder ([`zeron_mcp::proxy`]). +pub const MCP_PROXY_SUBCOMMAND: &str = "mcp-proxy"; + /// Turn-start hook: called with `(chat_id, cwd)`. pub type TurnListener = Arc; @@ -368,6 +479,8 @@ impl SessionsEngine { ipc_ready: tokio::sync::Notify::new(), ipc_none: std::sync::atomic::AtomicBool::new(false), ipc_token_file: Mutex::new(None), + mcp_endpoint: Mutex::new(None), + viewers: Arc::default(), journal, registry, doc_host: Mutex::new(None), @@ -451,6 +564,43 @@ impl SessionsEngine { *lock(&self.inner.ipc_token_file) = Some(path); } + /// Serve Zeron's MCP tools to this engine's agents from the engine itself + /// ([`zeron_mcp::http`]), over `service` (the engine's RPC, reached as + /// each run's agent: [`zeron_rpc::agent_service`]). Runs started after + /// this get a token for it rather than a `zeron mcp` process. + /// `service` holds this engine, and the endpoint lives here: retirement + /// severs that ([`Self::clear_doc_host`]). + pub async fn serve_mcp_endpoint( + &self, + service: Arc, + ) -> std::io::Result<()> { + let factory: zeron_mcp::http::ToolsFactory = Arc::new(move |origin: &zeron_mcp::Origin| { + let chat = origin.chat_id.clone().unwrap_or_default(); + Arc::new(zeron_mcp::Tools::new(Arc::new(zeron_mcp::Zeron::with_client( + zeron_rpc::memory_client(zeron_rpc::agent_service(service.clone(), chat)), + origin.clone(), + )))) + }); + let endpoint = zeron_mcp::http::McpHttpServer::bind(factory).await?; + *lock(&self.inner.mcp_endpoint) = Some(Arc::new(endpoint)); + Ok(()) + } + + /// `chat_id`'s transcript is open (in this device's app or another + /// device's) until the guard drops: its parked agent gets the longer + /// idle window ([`AGENT_IDLE_STOP`], not [`UNWATCHED_IDLE_STOP`]). + pub fn retain_viewer(&self, chat_id: &str) -> Viewer { + lock(&self.inner.viewers.open) + .entry(chat_id.to_owned()) + .or_default() + .count += 1; + self.inner.viewers.bump(); + Viewer { + viewers: self.inner.viewers.clone(), + chat_id: chat_id.to_owned(), + } + } + /// Wire the plugins service: local runs get its native MCP servers. pub fn set_plugins(&self, plugins: crate::plugins::Plugins) { let _ = self.inner.plugins.set(plugins); @@ -471,6 +621,8 @@ impl SessionsEngine { /// already treats a missing doc host as "not wired". pub fn clear_doc_host(&self) { lock(&self.inner.doc_host).take(); + // The MCP endpoint's tools hold the engine's RPC, which holds this. + lock(&self.inner.mcp_endpoint).take(); } /// Bind generated-image intake to the same profile store used by attachment RPCs. @@ -715,6 +867,23 @@ impl SessionsEngine { .count() } + /// Chats with work running or pending: a turn in flight (or waiting on a + /// question), or background work live after their turn ended. + pub fn chats_with_work(&self) -> Vec { + let mut chats: std::collections::BTreeSet = lock(&self.inner.statuses) + .iter() + .filter(|(_, session)| is_active(session)) + .map(|(chat, _)| chat.clone()) + .collect(); + chats.extend( + lock(&self.inner.runs) + .iter() + .filter(|(_, run)| run.turn.background_live()) + .map(|(chat, _)| chat.clone()), + ); + chats.into_iter().collect() + } + /// How many chats have a turn in flight (the Cloud runner heartbeat's /// `activeRuns`). pub fn active_count(&self) -> usize { @@ -1144,6 +1313,7 @@ impl SessionsEngine { tracing::info!(chat = %chat_id, "message cancelled by a later Stop: not dispatched"); return Ok(String::new()); } + tracing::debug!(target: "first_word", chat = %chat_id, "dispatch: fresh run"); let harness = self.inner.registry.resolve(harness_id)?; let handle = self.doc_handle(chat_id)?; let user_id = message_id.unwrap_or_else(new_id); @@ -1356,14 +1526,24 @@ impl SessionsEngine { self.inner.note_message(chat_id, &request.prompt); } - // Name the chat NOW, off the first prompt — not after the first - // exchange completes ("called New session for a long time for no - // reason"; the titler only needs the prompt and skips titled chats; - // the Done-time call below stays as the retry for a failed - // generation). - if !idle && let Some(titles) = self.inner.titles.get() { - titles.maybe_generate(chat_id, harness_id, &request.prompt, &request.cwd); - } + // Name the chat off the first prompt as soon as the agent answers — + // not after the first exchange completes ("called New session for a + // long time for no reason"; the titler only needs the prompt and + // skips titled chats; the Done-time call below stays as the retry + // for a failed generation). Not before the agent's first output: + // the titling agent's own startup competed with this one's. + let title = (!idle) + .then(|| self.inner.titles.get().cloned()) + .flatten() + .map(|titles| { + crate::titles::TitleAfterFirstOutput::new( + titles, + chat_id, + harness_id, + &request.prompt, + &request.cwd, + ) + }); tokio::spawn(drive_run( self.inner.clone(), @@ -1389,6 +1569,7 @@ impl SessionsEngine { }, turn, side_chats_only, + title, )); Ok(run_id) } @@ -2290,6 +2471,8 @@ impl SessionsEngine { tracing::warn!(chat = %chat_id, error = %err, "shutdown interrupt failed"); } } + // Lines still waiting to be written (`run_journal`'s batching). + self.inner.journal.flush_all(); } /// The engine is going down because its account signed out: the turns @@ -2569,6 +2752,19 @@ impl Inner { seq } + /// Whether a question of this chat's waits on the person. + fn awaiting_input(&self, chat_id: &str) -> bool { + lock(&self.statuses) + .get(chat_id) + .is_some_and(|session| session.status == SessionStatus::AwaitingInput) + } + + /// Whether the chat is open anywhere, and when its last viewer left. + fn view_state(&self, chat_id: &str) -> (bool, Option) { + let state = self.viewers.state(chat_id); + (state.count > 0, state.left_at) + } + /// Bump the session's freshness on stream activity WITHOUT a status /// transition. Long silent-LOOKING stretches (thinking heartbeats, a big /// tool input being generated) still carry events — the UI's 45s @@ -2825,38 +3021,38 @@ impl Inner { }) } - /// Zeron's own MCP server for a run of `chat_id`: this binary's `zeron - /// mcp` subcommand, dialing the engine's IPC port and stamped with the + /// Zeron's own MCP server for a run of `chat_id`, stamped with the /// originating chat + device so the agent's side chats link back here. - /// None when the engine serves no port or its executable is unknown. + /// None when the engine serves neither its MCP endpoint nor an IPC port. /// - /// The bearer is the agent-scoped one (`{data_dir}/ipc-token-agent`): it - /// reaches what the MCP tools need and nothing that decides, signs, - /// updates or hands out credentials ([`zeron_rpc::agent_allowed`]). + /// With the engine's MCP endpoint up ([`SessionsEngine::serve_mcp_endpoint`]), + /// the engine serves the tools itself: the run gets a token of its own + /// ([`zeron_mcp::http::Grant`], returned beside the server and held for + /// as long as the run lives), naming this chat and revoked when the run + /// ends. Agents that speak HTTP MCP get the endpoint + /// ([`zeron_proto::McpServer::http`]) and start no process; the rest get + /// this binary's `mcp-proxy`, a few-MB stdio-to-HTTP forwarder. The token + /// reaches what the agent-scoped IPC token reached, pinned to this chat. /// - /// The IPC bearer travels as a file PATH (`ZERON_IPC_TOKEN_FILE`), not as - /// `ZERON_IPC_TOKEN`: some harnesses hand this env to their CLI on the - /// command line (Claude's inline `--mcp-config`), where any local user can - /// read it with `ps`. The file is owner-only and always current. + /// Without it (no endpoint bound): this binary's `zeron mcp`, dialing the + /// engine's IPC port with the agent-scoped bearer + /// (`{data_dir}/ipc-token-agent`, [`zeron_rpc::agent_allowed`]), handed + /// over as a file PATH (`ZERON_IPC_TOKEN_FILE`) because some harnesses + /// put this env on the command line (Claude's inline `--mcp-config`). /// /// `side_chats_only`: the run's provider launches without its own - /// subagents, so all delegation goes through this server. - /// Zeron's own MCP server for a chat's run. `waits`: the runtime wakes - /// its agent as background work ends ([`Harness::wakes_on_background`]), - /// so the agent is offered `wait_on_background`. + /// subagents, so all delegation goes through this server. `waits`: the + /// runtime wakes its agent as background work ends + /// ([`Harness::wakes_on_background`]), so the agent is offered + /// `wait_on_background`. fn zeron_mcp( &self, chat_id: &str, side_chats_only: bool, waits: bool, - ) -> Option { - let port = self.ipc_port.load(std::sync::atomic::Ordering::Relaxed); - if port == 0 { - return None; - } + ) -> Option<(zeron_proto::McpServer, Option)> { let command = crate::managed::mcp_executable()?.to_str()?.to_owned(); let mut env: std::collections::BTreeMap = [ - ("ZERON_IPC_PORT".to_owned(), port.to_string()), (zeron_proto::McpServer::CHAT_ID_ENV.to_owned(), chat_id.to_owned()), ("ZERON_DEVICE_ID".to_owned(), self.device_id.clone()), ] @@ -2874,6 +3070,49 @@ impl Inner { "1".to_owned(), ); } + let endpoint = lock(&self.mcp_endpoint).clone(); + if let Some(endpoint) = endpoint { + let origin = zeron_mcp::Origin { + chat_id: Some(chat_id.to_owned()), + device_id: Some(self.device_id.clone()), + side_chats_only, + waits_on_background: waits, + }; + match endpoint.grant(origin) { + Ok(grant) => { + let url = endpoint.url(); + env.insert(zeron_proto::McpServer::URL_ENV.to_owned(), url.clone()); + env.insert( + zeron_proto::McpServer::TOKEN_ENV.to_owned(), + grant.token().to_owned(), + ); + let headers = [( + "Authorization".to_owned(), + format!("Bearer {}", grant.token()), + )] + .into_iter() + .collect(); + return Some(( + zeron_proto::McpServer { + name: "zeron".into(), + command, + args: vec![MCP_PROXY_SUBCOMMAND.into()], + env, + http: Some(zeron_proto::McpHttp { url, headers }), + }, + Some(grant), + )); + } + Err(err) => { + tracing::warn!(error = %err, "no token for the agent MCP endpoint; using the stdio server"); + } + } + } + let port = self.ipc_port.load(std::sync::atomic::Ordering::Relaxed); + if port == 0 { + return None; + } + env.insert("ZERON_IPC_PORT".to_owned(), port.to_string()); if let Some(path) = lock(&self.ipc_token_file) .as_ref() .and_then(|path| path.to_str()) @@ -2883,12 +3122,16 @@ impl Inner { path.to_owned(), ); } - Some(zeron_proto::McpServer { - name: "zeron".into(), - command, - args: vec!["mcp".into()], - env, - }) + Some(( + zeron_proto::McpServer { + name: "zeron".into(), + command, + args: vec!["mcp".into()], + env, + http: None, + }, + None, + )) } fn workspace(&self) -> Option { @@ -4194,6 +4437,7 @@ async fn drive_run( resume_state: RunResumeState, turn: TurnControl, side_chats_only: bool, + mut title: Option, ) { let device_id = inner.device_id.clone(); // Captured for post-run auto-titling (the request moves into the harness). @@ -4214,8 +4458,14 @@ async fn drive_run( }); // The host stamps its own MCP server onto every run it drives, so the // agent can spawn and talk to side chats through the engine it runs in. - if request.mcp.is_none() { - request.mcp = inner.zeron_mcp(&chat_id, side_chats_only, harness.wakes_on_background()); + // Held for the run's life: its MCP token stops working when it drops. + let mut _mcp_grant = None; + if request.mcp.is_none() + && let Some((server, grant)) = + inner.zeron_mcp(&chat_id, side_chats_only, harness.wakes_on_background()) + { + request.mcp = Some(server); + _mcp_grant = grant; } // Plugins' native MCP servers come from this host's own allowed plugins, // run from their verified snapshots — never from what a sender put here. @@ -4224,6 +4474,21 @@ async fn drive_run( .get() .map(crate::plugins::Plugins::native_mcp_servers) .unwrap_or_default(); + // The MCP servers the agent starts from this run's config: the only + // children it may hold when stopped idle (see the reaper). + let mut known_servers: Vec = request + .mcp + .iter() + .chain(&request.extra_mcp) + .map(|server| (server.command.clone(), server.args.clone())) + .collect(); + // And those it starts from its user's own MCP config, read now (once, + // off the runtime's threads) and joined in when the reaper first looks. + let mut configured_servers = Some({ + let harness = harness.clone(); + let request = request.clone(); + tokio::task::spawn_blocking(move || harness.configured_mcp_servers(&request)) + }); // A revived turn the resumed session already holds: the provider gets // the continuation, while the transcript, preview and title keep the // user's own message (and the retries carry the revival on). @@ -4411,6 +4676,7 @@ async fn drive_run( h.credential = now; } } + tracing::debug!(target: "first_word", chat = %chat_id, "drive_run: prepared"); match prepared { Ok(()) if resume_state.idle => harness.start_idle(wire_request, controls).await, Ok(()) => { @@ -4652,22 +4918,43 @@ async fn drive_run( // steerable harness parks here instead of ending the run — the child and // its steering mailbox stay warm, and the next user message (dispatch // routes into a live run) starts the next turn with zero respawn/resume - // latency. `Some(when)` = idle since then; the 30-min reaper below ends - // a session nobody comes back to (zeron SESSION_IDLE_MS). A voice - // orchestrator holds Codex and its MCP servers (~200 MB) for calls only, - // so it goes after 5 minutes. A call holds the reaper off, and the window - // restarts at hang-up. `ZERON_SESSION_IDLE_MS` overrides both (tests). - let session_idle = std::env::var("ZERON_SESSION_IDLE_MS") + // latency. `Some(when)` = idle since then; the idle reaper below stops a + // runtime nobody comes back to. A driver whose resume brings the session + // back whole ([`Harness::stops_when_idle`]) is stopped after + // [`AGENT_IDLE_STOP`], the rest after [`SESSION_IDLE`] (zeron + // SESSION_IDLE_MS). A voice orchestrator holds Codex and its MCP servers + // (~200 MB) for calls only, so it goes after 5 minutes. A call holds the + // reaper off, and the window restarts at hang-up. Nothing pending is + // ever stopped (see the reaper arm). `ZERON_SESSION_IDLE_MS` overrides + // every window (tests, the measurement rig). + // (May ask the CLI its version: off the runtime's threads.) + let stops_when_idle = { + let harness = harness.clone(); + tokio::task::spawn_blocking(move || harness.stops_when_idle()) + .await + .unwrap_or(false) + }; + let idle_override = std::env::var("ZERON_SESSION_IDLE_MS") .ok() .and_then(|v| v.parse::().ok()) - .map(std::time::Duration::from_millis) - .unwrap_or(std::time::Duration::from_secs( - if zeron_proto::voice::is_orchestrator_chat(&chat_id) { - 5 * 60 - } else { - 30 * 60 - }, - )); + .map(std::time::Duration::from_millis); + let session_idle = idle_override.unwrap_or( + if zeron_proto::voice::is_orchestrator_chat(&chat_id) { + std::time::Duration::from_secs(5 * 60) + } else if stops_when_idle { + AGENT_IDLE_STOP + } else { + SESSION_IDLE + }, + ); + // A chat open nowhere: the shorter window, from the later of the turn's + // end and its last viewer leaving (only where the short window applies). + let short_window = idle_override.is_none() + && stops_when_idle + && !zeron_proto::voice::is_orchestrator_chat(&chat_id); + let unwatched_idle = if short_window { UNWATCHED_IDLE_STOP } else { session_idle }; + // Viewers coming and going: the window is worked out again. + let mut viewers_changed = inner.viewers.sender().subscribe(); // A live subagent stretches the window, but never unbounded: every // driver's subagent terminal is a best-effort vendor signal (claude's // untagged task_notification, grok's subagent_finished, codex thread @@ -4675,7 +4962,11 @@ async fn drive_run( // loop's registry heartbeat — until the chat is archived. A subagent // silent this long (4h by default) is presumed lost; the reap stamps its // chip failed. - let subagent_silence = session_idle * 8; + let subagent_silence = idle_override.map_or(SUBAGENT_SILENCE, |idle| idle * 8); + // Wake-ups the agent scheduled for itself (Claude's `ScheduleWakeup`) + // and session crons it created (`CronCreate`): both live in its + // process, so it is never stopped while any is pending. + let mut self_scheduled = crate::self_schedule::SelfScheduled::default(); let mut idle_since: Option = resume_state.idle.then(tokio::time::Instant::now); // A parked runtime whose agent reports background work (see @@ -4830,6 +5121,9 @@ async fn drive_run( // ended): a turn is running from this moment, not from its // first words. () = turn.woke() => { + // (The wake-up's turn only if its time has come: a + // background task's ending wakes the agent too.) + self_scheduled.woke(tokio::time::Instant::now()); if idle_since.is_some() { tracing::info!(chat = %chat_id, "parked session resumed by a wake turn"); idle_since = None; @@ -4876,11 +5170,31 @@ async fn drive_run( // A live background subagent is somebody still using the child: // the window counts from its last activity, and stretches to // `subagent_silence` while its sink is open. + // Someone opened or left a transcript: work the window out again. + Ok(()) = viewers_changed.changed(), if idle_since.is_some() => { + continue; + } _ = tokio::time::sleep_until( { - let window = if subagents.is_empty() { session_idle } else { subagent_silence }; - let base = idle_since - .map(|at| at.max(last_subagent_activity.unwrap_or(at)) + window); + let (watched, left_at) = inner.view_state(&chat_id); + let window = if !subagents.is_empty() { + subagent_silence + } else if watched { + session_idle + } else { + unwatched_idle + }; + let base = idle_since.map(|at| { + let mut from = at.max(last_subagent_activity.unwrap_or(at)); + // Left alone: from when the last viewer left. + if !watched + && subagents.is_empty() + && let Some(left) = left_at + { + from = from.max(left); + } + from + window + }); match (base, reap_deferred_until) { (Some(base), Some(deferred)) => base.max(deferred), (base, deferred) => base.or(deferred).unwrap_or_else(tokio::time::Instant::now), @@ -4897,15 +5211,58 @@ async fn drive_run( // the runtime for as long as that work lives. if turn.background_live() { tracing::info!(chat = %chat_id, "idle reaper deferred: the agent reports background work"); - reap_deferred_until = Some(tokio::time::Instant::now() + session_idle); + reap_deferred_until = Some(tokio::time::Instant::now() + window_now(&inner, &chat_id, session_idle, unwatched_idle)); held_for_background = true; continue; } if std::mem::take(&mut held_for_background) { // The work just ended: a full idle window from here. - reap_deferred_until = Some(tokio::time::Instant::now() + session_idle); + reap_deferred_until = Some(tokio::time::Instant::now() + window_now(&inner, &chat_id, session_idle, unwatched_idle)); continue; } + // Anything else still pending in this runtime holds it + // too: the agent said it waits on work, it scheduled a + // wake-up or a cron, or a question waits on the person. + // Checked again a full window later. + let scheduled = self_scheduled.pending(tokio::time::Instant::now()); + let held = if wait.is_some() { + Some("the agent waits on background work") + } else if scheduled.is_some() { + scheduled + } else if inner.awaiting_input(&chat_id) { + Some("a question waits on the person") + } else { + None + }; + if let Some(why) = held { + tracing::info!(chat = %chat_id, why, "idle reaper deferred"); + reap_deferred_until = Some(tokio::time::Instant::now() + window_now(&inner, &chat_id, session_idle, unwatched_idle)); + continue; + } + // Belt and braces, trusting no report of the agent's: + // a child of its process that isn't one of its MCP + // servers (a background shell, a dev server) holds it. + if let Some(pid) = turn.agent_pid() { + // (Unreadable: nothing added, and its servers hold.) + if let Some(reading) = configured_servers.take() { + known_servers.extend(reading.await.unwrap_or_default()); + } + let known = known_servers.clone(); + let lingering = tokio::task::spawn_blocking(move || { + zeron_harness::process::unexpected_descendants(pid, &known) + }) + .await + .unwrap_or_else(|_| vec!["(the check failed)".to_owned()]); + if !lingering.is_empty() { + tracing::info!( + chat = %chat_id, + ?lingering, + "idle reaper deferred: the agent's process runs children it didn't report" + ); + reap_deferred_until = Some(tokio::time::Instant::now() + window_now(&inner, &chat_id, session_idle, unwatched_idle)); + continue; + } + } tracing::info!( chat = %chat_id, live_subagents = subagents.len(), @@ -4928,10 +5285,22 @@ async fn drive_run( } next = stream.next() => match next { Some(Ok(event)) => { - crate::wake_trace::note_agent_event(matches!( - event, - AgentEvent::SessionStarted { .. } - )); + let started = matches!(event, AgentEvent::SessionStarted { .. }); + crate::wake_trace::note_agent_event(started); + // The agent's first output: its chat's title may start. + // (Not its startup's reports: the session, its usage + // and its commands come before its request.) + let report = started + || matches!( + event, + AgentEvent::ContextUsage { .. } + | AgentEvent::PlanUsage { .. } + | AgentEvent::Usage { .. } + | AgentEvent::AvailableCommands { .. } + ); + if !report && let Some(mut title) = title.take() { + title.fire(); + } event } // A stream error while PARKED is a post-turn child death — @@ -4972,6 +5341,7 @@ async fn drive_run( // (parent + any dirty subagent docs). last_flush = Some(tokio::time::Instant::now()); if dirty { + tracing::debug!(target: "first_word", chat = %chat_id, "segment commit"); if let Err(err) = sync_segment( doc_ref, &mut writer, &entry_id, &device_id, segment_started, &folded, ) { @@ -4982,6 +5352,8 @@ async fn drive_run( for sink in subagents.values_mut() { sink.flush(&device_id); } + // The journal keeps up with the transcript it backs. + inner.journal.flush(&chat_id); continue; } // Turn-quiesce watchdog (see the knob above). Armed only when the @@ -5064,6 +5436,8 @@ async fn drive_run( }; event }; + // Scheduled wake-ups and session crons live in the agent's process. + self_scheduled.observe(&event, tokio::time::Instant::now()); // ── subagent routing ─────────────────────────────────────────── // Tagged events NEVER fold into the parent transcript: they stream diff --git a/crates/engine/src/titles.rs b/crates/engine/src/titles.rs index 65bea32b4..2c4e7d39d 100644 --- a/crates/engine/src/titles.rs +++ b/crates/engine/src/titles.rs @@ -90,6 +90,7 @@ impl TitleGenerator { return Ok(()); // already named } + tracing::debug!(target: "first_word", chat = %chat_id, "title: start"); let generated = self.run_title_model(harness_id, prompt, cwd).await; // Fallback so a chat is always named even if the model run produced nothing. let fallback: String = prompt @@ -250,6 +251,56 @@ impl TitleGenerator { } } +/// A new chat's title, held until its agent has answered: the agent's first +/// output, or its run ending without any. Titling starts a second agent +/// process (a small model), and started beside the chat's own agent it +/// competed with that agent's startup — the moments before the first word — +/// for the CPU. Started once the reply is coming, the title still lands a +/// moment after the first word. +pub(crate) struct TitleAfterFirstOutput { + titles: TitleGenerator, + chat_id: String, + harness: HarnessId, + prompt: String, + cwd: String, + fired: bool, +} + +impl TitleAfterFirstOutput { + pub(crate) fn new(titles: TitleGenerator, chat_id: &str, harness: HarnessId, prompt: &str, cwd: &str) -> Self { + Self { + titles, + chat_id: chat_id.to_owned(), + harness, + prompt: prompt.to_owned(), + cwd: cwd.to_owned(), + fired: false, + } + } + + /// Start titling now (once). + pub(crate) fn fire(&mut self) { + if std::mem::replace(&mut self.fired, true) { + return; + } + // (Dropped outside a runtime, a shutdown's: nothing to start it on.) + if tokio::runtime::Handle::try_current().is_err() { + return; + } + tracing::debug!(target: "first_word", chat = %self.chat_id, "title: released"); + self.titles + .maybe_generate(&self.chat_id, self.harness, &self.prompt, &self.cwd); + } +} + +/// A run that ends before any output (a failed start, a Stop) still titles +/// its chat, as it did when titling started with the run. +impl Drop for TitleAfterFirstOutput { + fn drop(&mut self) { + self.fire(); + } +} + /// The cheapest model a harness offers (zeron's `cheapestModel` heuristic): /// prefer a small-tier name (haiku/mini/nano/flash/small/lite), else the last /// listed model; `None` when the catalog is empty (harness picks its default). diff --git a/crates/engine/src/workspace_search.rs b/crates/engine/src/workspace_search.rs index 25db7bf10..8d4a35fd6 100644 --- a/crates/engine/src/workspace_search.rs +++ b/crates/engine/src/workspace_search.rs @@ -17,7 +17,6 @@ use std::borrow::Cow; use std::collections::HashMap; use std::path::{Path, PathBuf}; -use std::sync::atomic::AtomicBool; use std::sync::{Arc, Condvar, Mutex, Weak}; use std::time::{Duration, Instant}; @@ -153,7 +152,7 @@ struct Entry { struct HomeEntry { index: Option>, /// A walk is running; `cancel` stops it when the index is dropped. - building: Option>, + building: Option>, last_used: Option, warmed: bool, } @@ -468,7 +467,7 @@ impl WorkspaceSearch { .as_ref() .is_none_or(|index| index.built.elapsed() >= HOME_REFRESH); if stale && home.building.is_none() { - let cancel = Arc::new(AtomicBool::new(false)); + let cancel = Arc::new(crate::home_index::WalkCancel::default()); home.building = Some(cancel.clone()); let inner = Arc::downgrade(&self.inner); let root = root.to_path_buf(); @@ -500,7 +499,7 @@ impl WorkspaceSearch { return false; } if let Some(cancel) = home.building.take() { - cancel.store(true, std::sync::atomic::Ordering::Relaxed); + cancel.cancel(); } *home = HomeEntry::default(); self.inner.home_built.notify_all(); @@ -557,7 +556,7 @@ impl WorkspaceSearch { } } -fn build_home(inner: Weak, root: PathBuf, cancel: Arc) { +fn build_home(inner: Weak, root: PathBuf, cancel: Arc) { let started = Instant::now(); let index = HomeIndex::build(&root, &cancel); let Some(inner) = inner.upgrade() else { diff --git a/crates/engine/tests/agent_lifecycle.rs b/crates/engine/tests/agent_lifecycle.rs new file mode 100644 index 000000000..5142029c9 --- /dev/null +++ b/crates/engine/tests/agent_lifecycle.rs @@ -0,0 +1,862 @@ +//! An agent process parked between turns with nothing pending is stopped +//! after a short idle period ([`zeron_engine::sessions::AGENT_IDLE_STOP`] +//! while its chat is open anywhere, [`zeron_engine::sessions::UNWATCHED_IDLE_STOP`] +//! when it's open nowhere), +//! where its driver resumes sessions whole; the next message resumes it. +//! Nothing pending is ever stopped: background work, a scheduled wake-up, a +//! question, another device reading along. +//! +//! And an agent's Zeron MCP tools come from the engine itself, over a token +//! that names its run's chat and dies with the run. +//! +//! The harness models one agent process per `run()`: `run()` is a spawn, the +//! `interrupt` token its death. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use async_trait::async_trait; +use futures::StreamExt; +use futures::stream::BoxStream; +use tokio::sync::mpsc; + +use zeron_engine::sessions::{AGENT_IDLE_STOP, UNWATCHED_IDLE_STOP}; +use zeron_engine::{EngineCore, HarnessRegistry}; +use zeron_harness::{Harness, HarnessError, RunControls, TurnControl}; +use zeron_proto::{ + AgentEvent, DoneStatus, HarnessId, Model, ReasoningLevel, RunRequest, SandboxLevel, + SessionStatus, SteeringMode, ToolCall, +}; + +const CHAT: &str = "chat-idle-stop"; + +#[derive(Default)] +struct Ledger { + spawns: AtomicUsize, + kills: AtomicUsize, + /// Each spawn's `resume` (the provider session it was told to continue). + resumes: Mutex>>, + prompts: Mutex>, + turn: Mutex>, + /// Each spawn's Zeron MCP server, as the host stamped it. + mcp: Mutex>>, + /// The real process standing in for the agent CLI, told to the host. + process: Mutex>, + /// Starts a wake turn (one nobody sent) with this prompt. + wake: Mutex>>, +} + +/// `bg` in a prompt starts background work (the test ends it); `wake` +/// schedules a wake-up (Claude's `ScheduleWakeup`) with no input seen; +/// `wake:` / `wake:stop` call it with that delay or `stop`, and +/// `cron:` / `uncron:` create and delete a session cron, each +/// answered as the CLI would. +struct Agent { + ledger: Arc, + stops_when_idle: bool, +} + +fn done() -> AgentEvent { + AgentEvent::Done { + status: DoneStatus::Completed, + result: None, + error: None, + session_id: Some("provider-session".into()), + } +} + +#[async_trait] +impl Harness for Agent { + fn id(&self) -> HarnessId { + HarnessId::Mock + } + fn display_name(&self) -> &str { + "Agent" + } + fn supports_steering(&self) -> bool { + true + } + fn steering_mode(&self) -> SteeringMode { + SteeringMode::StepBoundary + } + fn reasoning_levels(&self) -> &[ReasoningLevel] { + &[ReasoningLevel::Medium] + } + fn deterministic_turn_end(&self) -> bool { + true + } + fn stops_turn_in_place(&self) -> bool { + true + } + fn wakes_on_background(&self) -> bool { + true + } + fn stops_when_idle(&self) -> bool { + self.stops_when_idle + } + /// Claude's own reading of its user's MCP config, where the test gave + /// the run a config dir of its own. + fn configured_mcp_servers( + &self, + request: &RunRequest, + ) -> Vec { + if !request.env.contains_key("CLAUDE_CONFIG_DIR") { + return Vec::new(); + } + zeron_harness::claude::configured_mcp_servers( + &request.env, + std::path::Path::new(&request.cwd), + ) + } + async fn models(&self) -> Result, HarnessError> { + Ok(vec![]) + } + async fn run( + &self, + request: RunRequest, + mut controls: RunControls, + ) -> Result>, HarnessError> { + let ledger = self.ledger.clone(); + ledger.spawns.fetch_add(1, Ordering::SeqCst); + ledger.resumes.lock().unwrap().push(request.resume.clone()); + ledger.mcp.lock().unwrap().push(request.mcp.clone()); + *ledger.turn.lock().unwrap() = Some(controls.turn.clone()); + controls.turn.set_agent_pid(*ledger.process.lock().unwrap()); + let (tx, rx) = mpsc::channel::>(64); + tokio::spawn(async move { + let turn = controls.turn.clone(); + let send = |event: AgentEvent| { + let tx = tx.clone(); + async move { tx.send(Ok(event)).await.is_ok() } + }; + let answer = |prompt: String| { + let ledger = ledger.clone(); + let turn = turn.clone(); + let send = send.clone(); + async move { + ledger.prompts.lock().unwrap().push(prompt.clone()); + if prompt.contains("bg") { + turn.set_background(1); + } + for (n, word) in prompt.split_whitespace().enumerate() { + let id = format!("t-{n}-{word}"); + let (name, input, output) = if let Some(delay) = word.strip_prefix("wake:") + { + let input = match delay.parse::() { + Ok(secs) => serde_json::json!({ "delaySeconds": secs }), + Err(_) => serde_json::json!({ "stop": true }), + }; + ("ScheduleWakeup", input, "Scheduled".to_owned()) + } else if let Some(cron) = word.strip_prefix("cron:") { + ( + "CronCreate", + serde_json::json!({ "cron": "*/5 * * * *" }), + format!("Scheduled recurring job {cron} (*/5 * * * *)"), + ) + } else if let Some(cron) = word.strip_prefix("uncron:") { + ( + "CronDelete", + serde_json::json!({ "id": cron }), + format!("Cancelled job {cron}"), + ) + } else { + continue; + }; + let call = AgentEvent::ToolCall { + id: id.clone(), + call: ToolCall::Unknown { + name: name.into(), + input: Some(input), + }, + }; + let result = AgentEvent::ToolResult { + id, + is_error: false, + output: Some(output), + diff: None, + preview_paths: Vec::new(), + image_payloads: Vec::new(), + exit_code: None, + }; + if !send(call).await || !send(result).await { + return false; + } + } + if prompt.contains("wake") + && !prompt.contains("wake:") + && !send(AgentEvent::ToolCall { + id: "t-wake".into(), + call: ToolCall::Unknown { + name: "ScheduleWakeup".into(), + input: None, + }, + }) + .await + { + return false; + } + send(AgentEvent::TextDelta { + text: format!("on it: {prompt}"), + }) + .await + && send(done()).await + } + }; + let _ = send(AgentEvent::SessionStarted { + harness: HarnessId::Mock, + model: "agent".into(), + tools: vec![], + cwd: request.cwd.clone(), + session_id: "provider-session".into(), + assistant_message_id: "a0".into(), + }) + .await; + if !answer(request.prompt.clone()).await { + return; + } + let (wake_tx, mut wakes) = mpsc::unbounded_channel::(); + *ledger.wake.lock().unwrap() = Some(wake_tx); + loop { + tokio::select! { + biased; + Some(prompt) = wakes.recv() => { + turn.note_wake(); + if !answer(prompt).await { + return; + } + } + _ = controls.interrupt.cancelled() => { + ledger.kills.fetch_add(1, Ordering::SeqCst); + let _ = tx.send(Ok(AgentEvent::Done { + status: DoneStatus::Interrupted, + result: None, + error: None, + session_id: None, + })).await; + return; + } + steer = controls.steering.recv() => { + let Some(steer) = steer else { return }; + let boundary = AgentEvent::Steered { + assistant_message_id: None, + next_assistant_message_id: None, + }; + if !send(boundary).await || !answer(steer.prompt.clone()).await { + return; + } + } + } + } + }); + Ok( + futures::stream::unfold(rx, |mut rx| async move { rx.recv().await.map(|e| (e, rx)) }) + .boxed(), + ) + } +} + +struct Rig { + core: EngineCore, + ledger: Arc, + _dir: tempfile::TempDir, +} + +fn rig(stops_when_idle: bool) -> Rig { + let ledger = Arc::new(Ledger::default()); + let registry = HarnessRegistry::new(); + registry.register(Arc::new(Agent { + ledger: ledger.clone(), + stops_when_idle, + })); + let dir = tempfile::tempdir().unwrap(); + let core = EngineCore::assemble(dir.path(), Arc::new(registry), HarnessId::Mock, None) + .expect("engine core assembles"); + Rig { + core, + ledger, + _dir: dir, + } +} + +fn request(prompt: &str) -> RunRequest { + RunRequest { + mcp: None, + env: Default::default(), + prompt: prompt.into(), + harness: Some(HarnessId::Mock), + model: Some("agent-1".into()), + reasoning: Some(ReasoningLevel::Medium), + model_options: Default::default(), + cwd: "/tmp".into(), + sandbox: SandboxLevel::WorkspaceWrite, + auto_approve: false, + access: Default::default(), + plan: false, + attachments: Vec::new(), + worktree: None, + resume: None, + extra_mcp: Vec::new(), + } +} + +async fn wait_for(mut predicate: impl FnMut() -> bool, what: &str) { + let deadline = tokio::time::Instant::now() + Duration::from_secs(10); + while !predicate() { + assert!( + tokio::time::Instant::now() < deadline, + "timed out waiting for {what}" + ); + tokio::time::sleep(Duration::from_millis(10)).await; + } +} + +impl Rig { + async fn send(&self, prompt: &str) { + self.send_request(request(prompt)).await; + } + + async fn send_request(&self, request: RunRequest) { + let before = self.ledger.prompts.lock().unwrap().len(); + self.core + .sessions + .dispatch(CHAT, HarnessId::Mock, request, None) + .await + .unwrap(); + wait_for( + || { + self.core.sessions.session_status(CHAT).map(|s| s.status) + == Some(SessionStatus::Idle) + && self.ledger.prompts.lock().unwrap().len() > before + }, + "the turn to park", + ) + .await; + } + + /// A wake turn: the runtime starts a turn of its own (as when a + /// background task ends, or a wake-up fires), which then parks. + async fn wake_turn(&self, prompt: &str) { + let before = self.ledger.prompts.lock().unwrap().len(); + let wake = self.ledger.wake.lock().unwrap().clone().unwrap(); + wake.send(prompt.into()).unwrap(); + wait_for( + || { + self.ledger.prompts.lock().unwrap().len() > before + && self.core.sessions.session_status(CHAT).map(|s| s.status) + == Some(SessionStatus::Idle) + }, + "the wake turn to park", + ) + .await; + } + + fn live(&self) -> bool { + self.core.sessions.live_run_steerable(CHAT) + } + + fn kills(&self) -> usize { + self.ledger.kills.load(Ordering::SeqCst) + } + + async fn stopped(&self) { + wait_for( + || self.kills() == 1 && !self.live(), + "the idle agent to be stopped", + ) + .await; + } +} + +/// Just under the idle window of a chat open nowhere. +async fn unwatched_window() { + tokio::time::sleep(UNWATCHED_IDLE_STOP - Duration::from_secs(5)).await; +} + +#[tokio::test(start_paused = true)] +async fn an_unwatched_idle_agent_stops_after_30_s_and_the_next_message_resumes() { + let rig = rig(true); + rig.send("hello").await; + unwatched_window().await; + assert!(rig.live(), "stopped before its idle window ran out"); + tokio::time::sleep(Duration::from_secs(10)).await; + rig.stopped().await; + + rig.send("and again").await; + assert_eq!(rig.ledger.spawns.load(Ordering::SeqCst), 2); + assert_eq!( + *rig.ledger.resumes.lock().unwrap(), + [None, Some("provider-session".to_owned())], + "the next message continues the same provider session" + ); + rig.core.sessions.shutdown().await; +} + +/// A chat open (in this device's app or another's): the person is likely +/// to reply, so its agent keeps the two-minute window. +#[tokio::test(start_paused = true)] +async fn a_watched_chat_s_agent_stops_only_after_two_minutes() { + let rig = rig(true); + let viewer = rig.core.sessions.retain_viewer(CHAT); + rig.send("hello").await; + tokio::time::sleep(AGENT_IDLE_STOP - Duration::from_secs(5)).await; + assert!(rig.live(), "stopped before the watched window ran out"); + tokio::time::sleep(Duration::from_secs(10)).await; + rig.stopped().await; + drop(viewer); + rig.core.sessions.shutdown().await; +} + +/// Switching away from a chat gives it the short window from then. +#[tokio::test(start_paused = true)] +async fn a_watcher_leaving_starts_a_30_s_window() { + let rig = rig(true); + let viewer = rig.core.sessions.retain_viewer(CHAT); + rig.send("hello").await; + tokio::time::sleep(Duration::from_secs(100)).await; + assert!(rig.live()); + drop(viewer); + unwatched_window().await; + assert!(rig.live(), "stopped before 30 s from the viewer leaving"); + tokio::time::sleep(Duration::from_secs(10)).await; + rig.stopped().await; + rig.core.sessions.shutdown().await; +} + +#[tokio::test(start_paused = true)] +async fn a_conversation_in_progress_stays_warm() { + let rig = rig(true); + let viewer = rig.core.sessions.retain_viewer(CHAT); + rig.send("hello").await; + // Replies inside the window keep the one process. + for n in 0..5 { + tokio::time::sleep(AGENT_IDLE_STOP - Duration::from_secs(10)).await; + rig.send(&format!("reply {n}")).await; + } + assert_eq!(rig.ledger.spawns.load(Ordering::SeqCst), 1); + assert_eq!(rig.kills(), 0); + drop(viewer); + rig.core.sessions.shutdown().await; +} + +/// Holds win over either window: background work, open or not. +#[tokio::test(start_paused = true)] +async fn an_agent_with_background_work_is_never_stopped() { + let rig = rig(true); + rig.send("start a bg build").await; + // Hours of background work: never stopped. + tokio::time::sleep(Duration::from_secs(3 * 60 * 60)).await; + assert!(rig.live(), "stopped while its background work ran"); + let viewer = rig.core.sessions.retain_viewer(CHAT); + tokio::time::sleep(Duration::from_secs(60 * 60)).await; + drop(viewer); + assert!(rig.live(), "stopped while its background work ran"); + assert_eq!(rig.kills(), 0); + // The work ends: a full idle window from then, then stopped. + let turn = rig.ledger.turn.lock().unwrap().clone().unwrap(); + turn.set_background(0); + unwatched_window().await; + assert!(rig.live(), "stopped as its background work ended"); + tokio::time::sleep(AGENT_IDLE_STOP * 2).await; + rig.stopped().await; + rig.core.sessions.shutdown().await; +} + +#[tokio::test(start_paused = true)] +async fn an_agent_with_a_scheduled_wake_up_is_never_stopped() { + let rig = rig(true); + rig.send("wake me in an hour").await; + tokio::time::sleep(Duration::from_secs(2 * 60 * 60)).await; + assert!(rig.live(), "stopped with a wake-up pending"); + assert_eq!(rig.kills(), 0); + rig.core.sessions.shutdown().await; +} + +/// A `/loop` that rescheduled its wake-up twice and then stopped holds +/// nothing: each call replaced the last, and `stop` ended it. +#[tokio::test(start_paused = true)] +async fn a_wake_up_loop_that_stopped_lets_the_agent_stop() { + let rig = rig(true); + rig.send("wake:600").await; + rig.send("wake:900").await; + rig.send("wake:stop").await; + tokio::time::sleep(AGENT_IDLE_STOP * 3).await; + rig.stopped().await; + rig.core.sessions.shutdown().await; +} + +/// A background task's wake turn, long before the wake-up's time, isn't +/// its firing: the wake-up still holds the agent. Once its time (and grace) +/// has passed with no turn of its own, it holds nothing. +#[tokio::test(start_paused = true)] +async fn a_wake_turn_before_the_wake_up_keeps_it_pending() { + let rig = rig(true); + rig.send("wake:1800").await; + tokio::time::sleep(Duration::from_secs(60)).await; + rig.wake_turn("a background task ended").await; + tokio::time::sleep(Duration::from_secs(20 * 60)).await; + assert!(rig.live(), "stopped with its wake-up still to come"); + assert_eq!(rig.kills(), 0); + tokio::time::sleep(Duration::from_secs(30 * 60)).await; + rig.stopped().await; + rig.core.sessions.shutdown().await; +} + +/// The same cron deleted twice cancels it once: the other still holds. +#[tokio::test(start_paused = true)] +async fn a_cron_deleted_twice_still_leaves_the_other_holding() { + let rig = rig(true); + rig.send("cron:aaaa1111 cron:bbbb2222").await; + rig.send("uncron:aaaa1111").await; + rig.send("uncron:aaaa1111").await; + tokio::time::sleep(Duration::from_secs(3 * 60 * 60)).await; + assert!(rig.live(), "stopped with a cron still scheduled"); + assert_eq!(rig.kills(), 0); + rig.send("uncron:bbbb2222").await; + tokio::time::sleep(AGENT_IDLE_STOP * 3).await; + rig.stopped().await; + rig.core.sessions.shutdown().await; +} + +#[tokio::test(start_paused = true)] +async fn a_driver_that_does_not_opt_in_keeps_the_long_window() { + let rig = rig(false); + rig.send("hello").await; + tokio::time::sleep(Duration::from_secs(29 * 60)).await; + assert!(rig.live(), "stopped before the 30-minute window"); + tokio::time::sleep(Duration::from_secs(2 * 60)).await; + rig.stopped().await; + rig.core.sessions.shutdown().await; +} + +/// One raw HTTP/1.1 POST to the engine's MCP endpoint: (status, body). +async fn post(url: &str, token: &str, body: &str) -> (u16, String) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let rest = url.strip_prefix("http://").unwrap(); + let (authority, path) = rest.split_once('/').unwrap(); + let mut stream = tokio::net::TcpStream::connect(authority).await.unwrap(); + let request = format!( + "POST /{path} HTTP/1.1\r\nHost: {authority}\r\nAuthorization: Bearer {token}\r\n\ + Accept: application/json, text/event-stream\r\nContent-Type: application/json\r\n\ + Connection: close\r\nContent-Length: {}\r\n\r\n{body}", + body.len() + ); + stream.write_all(request.as_bytes()).await.unwrap(); + let mut raw = String::new(); + stream.read_to_string(&mut raw).await.unwrap(); + let status = raw.split_whitespace().nth(1).unwrap().parse().unwrap(); + (status, raw) +} + +#[tokio::test] +async fn a_run_s_zeron_tools_come_from_the_engine_on_a_token_that_dies_with_it() { + let rig = rig(true); + rig.core + .sessions + .serve_mcp_endpoint(rig.core.rpc_service()) + .await + .unwrap(); + rig.send("hello").await; + let mcp = rig.ledger.mcp.lock().unwrap()[0] + .clone() + .expect("the host stamps Zeron's server"); + let http = mcp.http.clone().expect("served over HTTP by the engine"); + assert!(http.url.starts_with("http://127.0.0.1:"), "{}", http.url); + let token = http.bearer_token().expect("a bearer token").to_owned(); + // Agents that speak only stdio get the small forwarder, not the engine. + assert_eq!(mcp.args, ["mcp-proxy"]); + assert_eq!(mcp.env.get(zeron_proto::McpServer::TOKEN_ENV), Some(&token)); + assert_eq!( + mcp.env.get(zeron_proto::McpServer::URL_ENV), + Some(&http.url) + ); + assert!( + !format!("{mcp:?}").contains(&token), + "a token never reaches a log line" + ); + + // The token speaks as its run's chat. + let call = serde_json::json!({ + "jsonrpc": "2.0", "id": 1, "method": "tools/call", + "params": { "name": "whoami", "arguments": {} }, + }) + .to_string(); + let (status, body) = post(&http.url, &token, &call).await; + assert_eq!(status, 200, "{body}"); + assert!(body.contains(CHAT), "whoami names the run's chat: {body}"); + let (status, _) = post(&http.url, "not-a-token", &call).await; + assert_eq!(status, 401); + + // The run ends: its token with it. + rig.core.sessions.terminate(CHAT).await.unwrap(); + wait_for(|| !rig.live(), "the run to end").await; + let mut refused = 0; + for _ in 0..100 { + if post(&http.url, &token, &call).await.0 == 401 { + refused += 1; + break; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + assert_eq!(refused, 1, "an ended run's token still worked"); + + // The next run gets a token of its own. + rig.send("again").await; + let next = rig.ledger.mcp.lock().unwrap()[1].clone().unwrap(); + let next_token = next.http.unwrap().bearer_token().unwrap().to_owned(); + assert_ne!(next_token, token); + rig.core.sessions.shutdown().await; +} + +/// A real process standing in for the agent CLI, with one child: `sleep +/// ` (a background shell, or an MCP server). Killed on drop. +#[cfg(unix)] +struct AgentProcess(std::process::Child); + +#[cfg(unix)] +impl AgentProcess { + fn with_child(secs: &str) -> Self { + Self::with_children(&[secs]) + } + + /// One `sleep ` child for each. + fn with_children(secs: &[&str]) -> Self { + let script: String = secs + .iter() + .map(|s| format!("sleep {s} & ")) + .collect::() + + "wait"; + let agent = std::process::Command::new("/bin/sh") + .args(["-c", &script]) + .spawn() + .unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while zeron_harness::process::unexpected_descendants(agent.id(), &[]).len() < secs.len() { + assert!( + std::time::Instant::now() < deadline, + "the child never started" + ); + std::thread::sleep(Duration::from_millis(20)); + } + Self(agent) + } + + /// Leading its own process group, as Zeron starts agents, with no child + /// left: its shell ran `sleep &` and exited, so the sleep is + /// re-parented to init but still in the agent's group. + fn with_orphan_in_its_group(secs: &str) -> Self { + use std::os::unix::process::CommandExt as _; + let agent = std::process::Command::new("/bin/sh") + .args([ + "-c", + &format!("/bin/sh -c 'sleep {secs} >/dev/null 2>&1 &'; read _"), + ]) + .stdin(std::process::Stdio::piped()) + .process_group(0) + .spawn() + .unwrap(); + let group = agent.id() as i32; + let deadline = std::time::Instant::now() + Duration::from_secs(5); + loop { + // `ppid pgid stat args` of every process. + let out = std::process::Command::new("ps") + .args(["-A", "-o", "ppid=,pgid=,stat=,args="]) + .output() + .unwrap(); + let rows: Vec<(i32, i32, String, String)> = String::from_utf8_lossy(&out.stdout) + .lines() + .filter_map(|line| { + let mut cols = line.split_whitespace(); + let ppid = cols.next()?.parse().ok()?; + let pgid = cols.next()?.parse().ok()?; + let stat = cols.next()?.to_owned(); + Some((ppid, pgid, stat, cols.collect::>().join(" "))) + }) + .filter(|(_, _, stat, _)| !stat.starts_with('Z')) + .collect(); + // The sleep in the group, and its shell gone. + let orphan = rows.iter().any(|(ppid, pgid, _, args)| { + *pgid == group && *ppid != group && args == &format!("sleep {secs}") + }); + if orphan && !rows.iter().any(|(ppid, ..)| *ppid == group) { + break; + } + assert!( + std::time::Instant::now() < deadline, + "the orphan never appeared" + ); + std::thread::sleep(Duration::from_millis(20)); + } + Self(agent) + } +} + +#[cfg(unix)] +impl Drop for AgentProcess { + fn drop(&mut self) { + // SAFETY: the test's own process group: `sh` and its `sleep`. + let pid = self.0.id() as i32; + for child in zeron_harness::process::unexpected_descendants(pid as u32, &[]) { + if let Some(child) = child + .split_whitespace() + .next() + .and_then(|p| p.parse::().ok()) + { + unsafe { libc::kill(child, libc::SIGKILL) }; + } + } + // SAFETY: as above; a group only when the agent leads its own. + unsafe { + if libc::getpgid(pid) == pid { + libc::kill(-pid, libc::SIGKILL); + } + } + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +/// The agent says it holds no background work, but its process still runs +/// a child nobody configured (a background shell it never reported): never +/// stopped, however long. +#[cfg(unix)] +#[tokio::test(start_paused = true)] +async fn an_agent_whose_process_runs_an_unreported_child_is_never_stopped() { + let rig = rig(true); + let agent = AgentProcess::with_child("3603"); + *rig.ledger.process.lock().unwrap() = Some(agent.0.id()); + rig.send("hello").await; + assert!( + !rig.ledger + .turn + .lock() + .unwrap() + .clone() + .unwrap() + .background_live() + ); + tokio::time::sleep(Duration::from_secs(3 * 60 * 60)).await; + assert!( + rig.live(), + "stopped while its process ran an unreported child" + ); + assert_eq!(rig.kills(), 0); + drop(agent); + rig.core.sessions.shutdown().await; +} + +/// The agent's shell ran `cmd &` and exited: the process it left has no +/// parent under the agent any more, but it's in the agent's process group, +/// which a stop would signal. Never stopped, however long. +#[cfg(unix)] +#[tokio::test(start_paused = true)] +async fn an_orphan_left_in_the_agents_process_group_holds_the_stop() { + let rig = rig(true); + let agent = AgentProcess::with_orphan_in_its_group("3609"); + *rig.ledger.process.lock().unwrap() = Some(agent.0.id()); + rig.send("hello").await; + tokio::time::sleep(Duration::from_secs(3 * 60 * 60)).await; + assert!( + rig.live(), + "stopped while an orphan of its shell ran in its process group" + ); + assert_eq!(rig.kills(), 0); + drop(agent); + rig.core.sessions.shutdown().await; +} + +/// One whose only child is the MCP server its run configured is idle, and +/// is stopped. +#[cfg(unix)] +#[tokio::test(start_paused = true)] +async fn an_agent_whose_only_child_is_its_mcp_server_is_stopped() { + let rig = rig(true); + let agent = AgentProcess::with_child("3604"); + *rig.ledger.process.lock().unwrap() = Some(agent.0.id()); + let mut first = request("hello"); + first.mcp = Some(zeron_proto::McpServer { + name: "zeron".into(), + command: "sleep".into(), + args: vec!["3604".into()], + ..Default::default() + }); + rig.send_request(first).await; + tokio::time::sleep(AGENT_IDLE_STOP + Duration::from_secs(10)).await; + rig.stopped().await; + drop(agent); + rig.core.sessions.shutdown().await; +} + +/// A run whose user configured an MCP server of their own (user scope, in a +/// config dir of the test's): `config` holds `.claude.json` as written. +#[cfg(unix)] +fn with_user_config(prompt: &str, config: &std::path::Path, claude_json: &str) -> RunRequest { + std::fs::create_dir_all(config).unwrap(); + std::fs::write(config.join(".claude.json"), claude_json).unwrap(); + let mut run = request(prompt); + run.env.insert( + "CLAUDE_CONFIG_DIR".into(), + config.to_string_lossy().into_owned(), + ); + run +} + +#[cfg(unix)] +const USER_SERVER: &str = + r#"{"mcpServers":{"mine":{"command":"sleep","args":["3605"],"env":{"TOKEN":"x"}}}}"#; + +/// The user's own MCP server (from their Claude config) is the agent's only +/// child: recognised, so the idle agent is stopped. +#[cfg(unix)] +#[tokio::test(start_paused = true)] +async fn an_agent_whose_only_child_is_a_server_from_its_user_s_config_is_stopped() { + let rig = rig(true); + let config = tempfile::tempdir().unwrap(); + let agent = AgentProcess::with_child("3605"); + *rig.ledger.process.lock().unwrap() = Some(agent.0.id()); + rig.send_request(with_user_config("hello", config.path(), USER_SERVER)) + .await; + tokio::time::sleep(AGENT_IDLE_STOP + Duration::from_secs(10)).await; + rig.stopped().await; + drop(agent); + rig.core.sessions.shutdown().await; +} + +/// The same server, beside a background shell nobody configured: held. +#[cfg(unix)] +#[tokio::test(start_paused = true)] +async fn a_background_shell_beside_a_configured_server_still_holds() { + let rig = rig(true); + let config = tempfile::tempdir().unwrap(); + let agent = AgentProcess::with_children(&["3605", "3606"]); + *rig.ledger.process.lock().unwrap() = Some(agent.0.id()); + rig.send_request(with_user_config("hello", config.path(), USER_SERVER)) + .await; + tokio::time::sleep(Duration::from_secs(3 * 60 * 60)).await; + assert!(rig.live(), "stopped with an unconfigured child running"); + assert_eq!(rig.kills(), 0); + drop(agent); + rig.core.sessions.shutdown().await; +} + +/// A config that can't be read recognises nothing: its server holds the +/// stop, the safe side. +#[cfg(unix)] +#[tokio::test(start_paused = true)] +async fn a_malformed_user_config_holds() { + let rig = rig(true); + let config = tempfile::tempdir().unwrap(); + let agent = AgentProcess::with_child("3605"); + *rig.ledger.process.lock().unwrap() = Some(agent.0.id()); + let broken = &USER_SERVER[..USER_SERVER.len() - 3]; + rig.send_request(with_user_config("hello", config.path(), broken)) + .await; + tokio::time::sleep(Duration::from_secs(3 * 60 * 60)).await; + assert!( + rig.live(), + "stopped although its child couldn't be recognised" + ); + assert_eq!(rig.kills(), 0); + drop(agent); + rig.core.sessions.shutdown().await; +} diff --git a/crates/engine/tests/launch_agents.rs b/crates/engine/tests/launch_agents.rs new file mode 100644 index 000000000..fb87271eb --- /dev/null +++ b/crates/engine/tests/launch_agents.rs @@ -0,0 +1,331 @@ +//! An engine that starts over chats whose agents were parked idle starts no +//! agent for them: each is resumed when its next message comes (ademark A5, +//! "seeded state, just launched, no agent running"). Only a turn that was +//! cut off mid-way is real work, and that one is picked back up. +//! +//! The harness is a persistent agent per `run()` (a spawn): it answers a +//! prompt and stays up, parked, for the next one; a prompt with `hold` stays +//! in flight. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use async_trait::async_trait; +use futures::StreamExt; +use futures::stream::BoxStream; +use tokio::sync::mpsc; + +use zeron_engine::{EngineCore, HarnessRegistry}; +use zeron_harness::{Harness, HarnessError, RunControls}; +use zeron_proto::{ + AgentEvent, DoneStatus, HarnessId, Model, ReasoningLevel, RunRequest, SandboxLevel, + SessionStatus, SteeringMode, +}; + +#[derive(Default)] +struct Ledger { + spawns: AtomicUsize, + /// Each spawn: its chat's first prompt and its `resume`. + runs: Mutex)>>, +} + +struct Agent { + ledger: Arc, +} + +fn done() -> AgentEvent { + AgentEvent::Done { + status: DoneStatus::Completed, + result: None, + error: None, + session_id: Some("provider-session".into()), + } +} + +#[async_trait] +impl Harness for Agent { + fn id(&self) -> HarnessId { + HarnessId::Mock + } + fn display_name(&self) -> &str { + "Agent" + } + fn supports_steering(&self) -> bool { + true + } + fn steering_mode(&self) -> SteeringMode { + SteeringMode::StepBoundary + } + fn reasoning_levels(&self) -> &[ReasoningLevel] { + &[ReasoningLevel::Medium] + } + fn deterministic_turn_end(&self) -> bool { + true + } + async fn models(&self) -> Result, HarnessError> { + Ok(vec![]) + } + async fn run( + &self, + request: RunRequest, + mut controls: RunControls, + ) -> Result>, HarnessError> { + self.ledger.spawns.fetch_add(1, Ordering::SeqCst); + self.ledger + .runs + .lock() + .unwrap() + .push((request.prompt.clone(), request.resume.clone())); + let (tx, rx) = mpsc::channel::>(64); + tokio::spawn(async move { + let send = |event: AgentEvent| { + let tx = tx.clone(); + async move { tx.send(Ok(event)).await.is_ok() } + }; + let _ = send(AgentEvent::SessionStarted { + harness: HarnessId::Mock, + model: "agent".into(), + tools: vec![], + cwd: request.cwd.clone(), + session_id: "provider-session".into(), + assistant_message_id: "a0".into(), + }) + .await; + let _ = send(AgentEvent::TextDelta { + text: format!("on it: {}", request.prompt), + }) + .await; + if !request.prompt.contains("hold") && !send(done()).await { + return; + } + loop { + tokio::select! { + _ = controls.interrupt.cancelled() => { + let _ = tx.send(Ok(AgentEvent::Done { + status: DoneStatus::Interrupted, + result: None, + error: None, + session_id: None, + })).await; + return; + } + steer = controls.steering.recv() => { + let Some(steer) = steer else { return }; + let boundary = AgentEvent::Steered { + assistant_message_id: None, + next_assistant_message_id: None, + }; + if !send(boundary).await + || !send(AgentEvent::TextDelta { text: format!("on it: {}", steer.prompt) }).await + || !send(done()).await + { + return; + } + } + } + } + }); + Ok( + futures::stream::unfold(rx, |mut rx| async move { rx.recv().await.map(|e| (e, rx)) }) + .boxed(), + ) + } +} + +fn assemble(dir: &std::path::Path, ledger: &Arc) -> EngineCore { + let registry = HarnessRegistry::new(); + registry.register(Arc::new(Agent { + ledger: ledger.clone(), + })); + let core = EngineCore::assemble(dir, Arc::new(registry), HarnessId::Mock, None) + .expect("engine core assembles"); + core.sessions.set_ipc_port(27699); + core +} + +fn request(prompt: &str) -> RunRequest { + RunRequest { + mcp: None, + env: Default::default(), + prompt: prompt.into(), + harness: Some(HarnessId::Mock), + model: Some("agent-1".into()), + reasoning: Some(ReasoningLevel::Medium), + model_options: Default::default(), + cwd: "/tmp".into(), + sandbox: SandboxLevel::WorkspaceWrite, + auto_approve: true, + access: Default::default(), + plan: false, + attachments: Vec::new(), + worktree: None, + resume: None, + extra_mcp: Vec::new(), + } +} + +async fn wait_for(mut predicate: impl FnMut() -> bool, what: &str) { + let deadline = tokio::time::Instant::now() + Duration::from_secs(15); + while !predicate() { + assert!( + tokio::time::Instant::now() < deadline, + "timed out waiting for {what}" + ); + tokio::time::sleep(Duration::from_millis(10)).await; + } +} + +const SEEDED: [&str; 5] = ["seed-0", "seed-1", "seed-2", "seed-3", "seed-4"]; + +/// Five chats, each with a finished turn and its agent parked. +async fn seed(core: &EngineCore, ledger: &Ledger) { + for chat in SEEDED { + core.sessions + .dispatch(chat, HarnessId::Mock, request("seed prompt"), None) + .await + .unwrap(); + } + wait_for( + || { + SEEDED.iter().all(|chat| { + core.sessions.session_status(chat).map(|s| s.status) == Some(SessionStatus::Idle) + }) && ledger.spawns.load(Ordering::SeqCst) == SEEDED.len() + }, + "the seeded turns to park", + ) + .await; + assert!( + SEEDED + .iter() + .all(|chat| core.sessions.live_run_steerable(chat)) + ); +} + +/// Launch: recover, then three minutes with nothing sent. +async fn launch(dir: &std::path::Path) -> (EngineCore, Arc) { + let ledger = Arc::new(Ledger::default()); + let core = assemble(dir, &ledger); + core.sessions.recover_stale().unwrap(); + tokio::time::sleep(Duration::from_secs(3)).await; + (core, ledger) +} + +/// The next message, once launched: one agent, resuming the chat's session. +async fn next_message_resumes(core: &EngineCore, ledger: &Ledger) { + core.sessions + .dispatch("seed-2", HarnessId::Mock, request("next message"), None) + .await + .unwrap(); + wait_for( + || ledger.spawns.load(Ordering::SeqCst) == 1, + "the next message's agent", + ) + .await; + assert_eq!( + ledger.runs.lock().unwrap()[0], + ( + "next message".to_owned(), + Some("provider-session".to_owned()) + ), + "it resumes the chat's own session" + ); +} + +#[tokio::test] +async fn parked_agents_are_not_restarted_after_a_shutdown() { + let tmp = tempfile::tempdir().unwrap(); + { + let ledger = Arc::new(Ledger::default()); + let core = assemble(tmp.path(), &ledger); + seed(&core, &ledger).await; + core.shutdown().await; + } + let (core, ledger) = launch(tmp.path()).await; + assert_eq!( + ledger.spawns.load(Ordering::SeqCst), + 0, + "an agent started at launch" + ); + assert!( + SEEDED + .iter() + .all(|chat| !core.sessions.live_run_steerable(chat)) + ); + next_message_resumes(&core, &ledger).await; + core.sessions.shutdown().await; +} + +#[test] +fn parked_agents_are_not_restarted_after_a_crash() { + let tmp = tempfile::tempdir().unwrap(); + // The engine dies with its agents parked: its runtime goes without a + // shutdown, as a kill -9 leaves it (journals open, nothing closed). + let seeding = tokio::runtime::Runtime::new().unwrap(); + seeding.block_on(async { + let ledger = Arc::new(Ledger::default()); + let core = assemble(tmp.path(), &ledger); + seed(&core, &ledger).await; + // Let the debounced doc saves land, as minutes of rest would. + tokio::time::sleep(Duration::from_millis(1500)).await; + // Gone without its shutdown (its data dir let go, as a dead + // process's is). + drop(core); + }); + seeding.shutdown_background(); + let launching = tokio::runtime::Runtime::new().unwrap(); + launching.block_on(async { + let (core, ledger) = launch(tmp.path()).await; + assert_eq!( + ledger.spawns.load(Ordering::SeqCst), + 0, + "an agent started at launch" + ); + next_message_resumes(&core, &ledger).await; + core.sessions.shutdown().await; + }); +} + +#[tokio::test] +async fn a_turn_cut_off_mid_way_is_picked_back_up_and_only_it() { + let tmp = tempfile::tempdir().unwrap(); + { + let ledger = Arc::new(Ledger::default()); + let core = assemble(tmp.path(), &ledger); + seed(&core, &ledger).await; + core.sessions + .dispatch( + "busy", + HarnessId::Mock, + request("hold this long task"), + None, + ) + .await + .unwrap(); + wait_for( + || core.sessions.turn_in_flight("busy") && ledger.spawns.load(Ordering::SeqCst) == 6, + "the long turn to run", + ) + .await; + core.shutdown().await; + } + let (core, ledger) = launch(tmp.path()).await; + wait_for( + || ledger.spawns.load(Ordering::SeqCst) >= 1, + "the cut-off turn's revival", + ) + .await; + tokio::time::sleep(Duration::from_secs(1)).await; + let runs = ledger.runs.lock().unwrap().clone(); + assert_eq!(runs.len(), 1, "only the cut-off turn restarts: {runs:?}"); + assert!( + core.sessions.turn_in_flight("busy"), + "the revived turn is the cut-off one" + ); + assert!( + SEEDED + .iter() + .all(|chat| !core.sessions.live_run_steerable(chat)) + ); + core.sessions.shutdown().await; +} diff --git a/crates/engine/tests/steer_tool_live.rs b/crates/engine/tests/steer_tool_live.rs index db438c698..d55d9100d 100644 --- a/crates/engine/tests/steer_tool_live.rs +++ b/crates/engine/tests/steer_tool_live.rs @@ -144,6 +144,7 @@ async fn steering_never_aborts_a_running_tool() { env: [("SLOW_MCP_DIR".to_owned(), mcp_dir.display().to_string())] .into_iter() .collect(), + http: None, }), prompt: "Call the slow_wait tool from the `slow` MCP server exactly once and \ wait for it, then tell me the secret word it returned." diff --git a/crates/harness/src/acp/hermes_names.rs b/crates/harness/src/acp/hermes_names.rs index 7317e54ab..9ffcc04f9 100644 --- a/crates/harness/src/acp/hermes_names.rs +++ b/crates/harness/src/acp/hermes_names.rs @@ -115,20 +115,24 @@ fn name_models_from(models: &mut [Model], catalog: &Path) { // map of it) would cost far more than the few names wanted. // Only a regular file of a sane size: a FIFO (or a device) there would // block the read forever, a runaway file would be parsed for minutes. - let Ok(meta) = std::fs::metadata(catalog) else { - return; + // (Checked on the open handle: one swapped for a FIFO after a check of + // its path would still block.) + let file = match crate::bounded_file::open(catalog, MAX_CATALOG_BYTES) { + Ok((file, _)) => file, + Err(err) => { + if err.kind() != std::io::ErrorKind::NotFound { + tracing::debug!( + error = %err, + "Hermes's model catalog isn't a file of a usable size; models keep their ids" + ); + } + return; + } }; - if !meta.is_file() || meta.len() > MAX_CATALOG_BYTES { - tracing::debug!( - "Hermes's model catalog isn't a file of a usable size; models keep their ids" - ); - return; - } - let Ok(file) = std::fs::File::open(catalog) else { - return; - }; - let mut reader = - serde_json::Deserializer::from_reader(std::io::BufReader::with_capacity(64 * 1024, file)); + let mut reader = serde_json::Deserializer::from_reader(std::io::BufReader::with_capacity( + 64 * 1024, + std::io::Read::take(file, MAX_CATALOG_BYTES), + )); let names = match (Catalog { wanted: &wanted }).deserialize(&mut reader) { Ok(names) => names, Err(error) => { @@ -404,6 +408,13 @@ mod tests { file.set_len(MAX_CATALOG_BYTES + 1).unwrap(); name_models_from(&mut models, &big); assert_eq!(models[0].label, "claude-opus-5-5"); + // A symlink to an endless device: refused once opened, not read. + let zero = dir.path().join("zero.json"); + std::os::unix::fs::symlink("/dev/zero", &zero).unwrap(); + let started = std::time::Instant::now(); + name_models_from(&mut models, &zero); + assert!(started.elapsed() < std::time::Duration::from_secs(1)); + assert_eq!(models[0].label, "claude-opus-5-5"); } /// Hermes's own catalog names its models; one it doesn't list keeps diff --git a/crates/harness/src/acp/mod.rs b/crates/harness/src/acp/mod.rs index a2e0d9a91..57d6a64dd 100644 --- a/crates/harness/src/acp/mod.rs +++ b/crates/harness/src/acp/mod.rs @@ -3655,28 +3655,47 @@ fn cancel_params(harness: HarnessId, session_id: &str) -> Value { /// config, never from here. #[cfg(test)] fn acp_mcp_servers(mcp: Option<&zeron_proto::McpServer>) -> Vec { - acp_session_mcp_servers(mcp, &[]) + acp_session_mcp_servers(mcp, &[], false) +} + +/// Whether the agent takes HTTP MCP servers in `session/new` +/// (`agentCapabilities.mcpCapabilities.http`). +fn takes_http_mcp(init: &Value) -> bool { + init.pointer("/agentCapabilities/mcpCapabilities/http") + .and_then(Value::as_bool) + == Some(true) } /// [`acp_mcp_servers`] plus the plugins' native servers -/// ([`RunRequest::extra_mcp`]), Zeron's own first. +/// ([`RunRequest::extra_mcp`]), Zeron's own first. With `http` (the agent +/// takes HTTP servers), Zeron's own is the engine's endpoint +/// (`{type: "http", name, url, headers: [{name, value}]}`) and no process +/// starts; otherwise its stdio command — the engine's `mcp-proxy`. fn acp_session_mcp_servers( mcp: Option<&zeron_proto::McpServer>, extras: &[zeron_proto::McpServer], + http: bool, ) -> Vec { + let pairs = |map: &std::collections::BTreeMap| { + map.iter() + .map(|(name, value)| json!({ "name": name, "value": value })) + .collect::>() + }; mcp.into_iter() .chain(extras) - .map(|mcp| { - json!({ + .map(|mcp| match mcp.http.as_ref().filter(|_| http) { + Some(endpoint) => json!({ + "type": "http", + "name": mcp.name, + "url": endpoint.url, + "headers": pairs(&endpoint.headers), + }), + None => json!({ "name": mcp.name, "command": mcp.command, "args": mcp.args, - "env": mcp - .env - .iter() - .map(|(name, value)| json!({ "name": name, "value": value })) - .collect::>(), - }) + "env": pairs(&mcp.env), + }), }) .collect() } @@ -4572,7 +4591,11 @@ async fn open_session( ) -> Result<(String, Value), HarnessError> { let params = json!({ "cwd": request.cwd, - "mcpServers": acp_session_mcp_servers(request.mcp.as_ref(), &request.extra_mcp), + "mcpServers": acp_session_mcp_servers( + request.mcp.as_ref(), + &request.extra_mcp, + takes_http_mcp(init), + ), }); if let Some(resume) = &request.resume { let method = restore_method(init, harness); @@ -8274,6 +8297,7 @@ mod mcp_injection_tests { env: [("ZERON_IPC_PORT".to_owned(), "27654".to_owned())] .into_iter() .collect(), + http: None, }; let servers = acp_mcp_servers(Some(&mcp)); assert_eq!( @@ -8293,14 +8317,16 @@ mod mcp_injection_tests { command: "/opt/zeron/zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let extra = zeron_proto::McpServer { name: "plugin-focus".into(), command: "/data/plugins/.loaded/focus/0123/bin/server".into(), args: vec![], env: [("FOCUS".to_owned(), "1".to_owned())].into_iter().collect(), + http: None, }; - let servers = acp_session_mcp_servers(Some(&zeron), std::slice::from_ref(&extra)); + let servers = acp_session_mcp_servers(Some(&zeron), std::slice::from_ref(&extra), false); assert_eq!(servers.len(), 2); assert_eq!(servers[0]["name"], "zeron"); assert_eq!( @@ -8312,6 +8338,35 @@ mod mcp_injection_tests { "env": [{ "name": "FOCUS", "value": "1" }], }) ); - assert_eq!(acp_session_mcp_servers(None, &[extra]).len(), 1); + assert_eq!(acp_session_mcp_servers(None, &[extra], false).len(), 1); + } + + /// An agent that takes HTTP servers gets the engine's endpoint for + /// Zeron's own (no process); one that doesn't gets the stdio proxy. + #[test] + fn zeron_s_server_is_http_only_where_the_agent_takes_it() { + let zeron = zeron_proto::McpServer { + name: "zeron".into(), + command: "/opt/zeron/zeron-engine".into(), + args: vec!["mcp-proxy".into()], + env: [("ZERON_MCP_TOKEN".to_owned(), "t".to_owned())].into_iter().collect(), + http: Some(zeron_proto::McpHttp { + url: "http://127.0.0.1:4321/mcp".into(), + headers: [("Authorization".to_owned(), "Bearer t".to_owned())] + .into_iter() + .collect(), + }), + }; + let http = acp_session_mcp_servers(Some(&zeron), &[], true); + assert_eq!(http[0]["type"], "http"); + assert_eq!(http[0]["url"], "http://127.0.0.1:4321/mcp"); + assert_eq!(http[0]["headers"], json!([{ "name": "Authorization", "value": "Bearer t" }])); + let stdio = acp_session_mcp_servers(Some(&zeron), &[], false); + assert_eq!(stdio[0]["command"], "/opt/zeron/zeron-engine"); + assert_eq!(stdio[0]["args"], json!(["mcp-proxy"])); + assert!(stdio[0].get("type").is_none()); + let init = json!({ "agentCapabilities": { "mcpCapabilities": { "http": true } } }); + assert!(takes_http_mcp(&init)); + assert!(!takes_http_mcp(&json!({ "agentCapabilities": {} }))); } } diff --git a/crates/harness/src/bounded_file.rs b/crates/harness/src/bounded_file.rs new file mode 100644 index 000000000..11a8fa07d --- /dev/null +++ b/crates/harness/src/bounded_file.rs @@ -0,0 +1,121 @@ +//! Reading a small file that someone else may have put in place: a config +//! file in a cloned repository, a CLI's cache. A path checked first and +//! read after can be a symlink to `/dev/zero` (read forever) or a FIFO +//! (open blocks until a writer comes), or be swapped for one in between, so +//! the checks are made on the open handle and the read itself is bounded. + +use std::io::Read; +use std::path::Path; + +/// `path`'s bytes when it is a regular file of at most `max` bytes (a +/// symlink to one is followed). A FIFO or device is refused without +/// blocking, and a file over `max` (or one growing past it as it is read) +/// is refused once `max + 1` bytes are in: `InvalidInput` and +/// `FileTooLarge` errors respectively; a missing file is `NotFound`. +pub fn read(path: &Path, max: u64) -> std::io::Result> { + let (file, len) = open(path, max)?; + let mut bytes = Vec::with_capacity(len as usize); + file.take(max.saturating_add(1)).read_to_end(&mut bytes)?; + if bytes.len() as u64 > max { + return Err(too_large()); + } + Ok(bytes) +} + +/// [`read`] as UTF-8 text. +pub fn read_to_string(path: &Path, max: u64) -> std::io::Result { + String::from_utf8(read(path, max)?) + .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err)) +} + +/// `path` opened for a streamed read, refused as [`read`] refuses it, with +/// its length when opened. Read it through `take(max + 1)` (or check what +/// came in) if it may grow meanwhile. +pub fn open(path: &Path, max: u64) -> std::io::Result<(std::fs::File, u64)> { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + // A FIFO opened for reading blocks until a writer appears; a + // terminal would become the controlling one. + options.custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY); + } + let file = options.open(path)?; + // What was opened, not what the path named a moment before. + let meta = file.metadata()?; + if !meta.is_file() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "not a regular file", + )); + } + if meta.len() > max { + return Err(too_large()); + } + // (O_NONBLOCK changes nothing for a regular file's reads.) + Ok((file, meta.len())) +} + +fn too_large() -> std::io::Error { + std::io::Error::new(std::io::ErrorKind::FileTooLarge, "file too large") +} + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use std::time::{Duration, Instant}; + + /// Runs `read` on another thread, failing the test if it hangs. + fn read_promptly(path: &Path, max: u64) -> std::io::Result> { + let path = path.to_path_buf(); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = tx.send(read(&path, max)); + }); + rx.recv_timeout(Duration::from_secs(5)) + .expect("the read hung") + } + + #[test] + fn a_regular_file_within_the_limit_is_read() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("a.json"); + std::fs::write(&path, b"{}").unwrap(); + assert_eq!(read(&path, 2).unwrap(), b"{}"); + assert_eq!( + read(&path, 1).unwrap_err().kind(), + std::io::ErrorKind::FileTooLarge + ); + // A symlink to a regular file still reads. + let link = dir.path().join("link.json"); + std::os::unix::fs::symlink(&path, &link).unwrap(); + assert_eq!(read(&link, 16).unwrap(), b"{}"); + assert_eq!( + read(&dir.path().join("missing"), 16).unwrap_err().kind(), + std::io::ErrorKind::NotFound + ); + } + + #[test] + fn a_symlink_to_dev_zero_is_refused_promptly() { + let dir = tempfile::tempdir().unwrap(); + let link = dir.path().join(".mcp.json"); + std::os::unix::fs::symlink("/dev/zero", &link).unwrap(); + let started = Instant::now(); + let err = read_promptly(&link, 1024 * 1024).unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput); + assert!(started.elapsed() < Duration::from_secs(1)); + } + + #[test] + fn a_fifo_is_refused_without_waiting_for_a_writer() { + let dir = tempfile::tempdir().unwrap(); + let fifo = dir.path().join("settings.json"); + let c_path = std::ffi::CString::new(fifo.as_os_str().as_encoded_bytes()).unwrap(); + // SAFETY: a valid NUL-terminated path. + assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0); + let err = read_promptly(&fifo, 1024).unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput); + } +} diff --git a/crates/harness/src/claude/mcp_config.rs b/crates/harness/src/claude/mcp_config.rs new file mode 100644 index 000000000..77ae44b5c --- /dev/null +++ b/crates/harness/src/claude/mcp_config.rs @@ -0,0 +1,492 @@ +//! The stdio MCP servers a Claude run starts from the user's own +//! configuration — read the way the CLI reads it — so the host can tell +//! them from a background shell among the agent's children +//! ([`crate::process::unexpected_descendants`]). +//! +//! Zeron launches Claude without `--strict-mcp-config`, so the CLI also +//! starts the servers configured in its documented MCP scopes +//! (code.claude.com/docs/en/mcp, /managed-mcp, /claude-directory): +//! +//! - **user**: `mcpServers` at the top of `.claude.json` (in +//! `$CLAUDE_CONFIG_DIR` when set, else `~/.claude.json`); +//! - **local**: `projects[].mcpServers` in the same file; +//! - **project**: `.mcp.json` at the project root, which `claude -p` loads +//! without asking unless a settings file's `disabledMcpjsonServers` +//! names the server; +//! - **plugins**: each enabled plugin (`enabledPlugins` in the settings +//! files) installed under `/plugins` (`installed_plugins.json`), +//! its servers in `.mcp.json` at the plugin root or in +//! `.claude-plugin/plugin.json`'s `mcpServers`; +//! - **managed**: `managed-mcp.json` in the OS's system location. +//! +//! Only `command` and `args` are taken: an entry's `env` (which may hold +//! secrets) is never read into anything kept or logged. Remote servers +//! (`url`) start no process and are skipped. Files are size-bounded; one +//! missing, too big or malformed adds nothing, which leaves its servers +//! unrecognised — and an unrecognised child holds an idle stop: the safe +//! side. + +use std::collections::{BTreeMap, BTreeSet}; +use std::path::{Path, PathBuf}; + +use serde_json::Value; + +use crate::process::KnownServer; + +/// `.claude.json` holds history and per-project state, and grows: the CLI +/// itself backs it up while under 8 MiB. +const CLAUDE_JSON_MAX: u64 = 16 * 1024 * 1024; +/// Every other file read here. +const FILE_MAX: u64 = 1024 * 1024; +/// The most servers taken from all sources. +const SERVERS_MAX: usize = 512; + +#[cfg(target_os = "macos")] +const MANAGED_DIR: &str = "/Library/Application Support/ClaudeCode"; +#[cfg(all(unix, not(target_os = "macos")))] +const MANAGED_DIR: &str = "/etc/claude-code"; +#[cfg(windows)] +const MANAGED_DIR: &str = r"C:\Program Files\ClaudeCode"; + +/// The stdio servers a Claude run in `cwd`, with `env` over this process's +/// environment, starts from the user's configuration. Blocking (file +/// reads); call once per run, off the async threads. +pub fn configured_mcp_servers(env: &BTreeMap, cwd: &Path) -> Vec { + let var = |name: &str| { + env.get(name) + .cloned() + .or_else(|| std::env::var(name).ok()) + .filter(|value| !value.is_empty()) + }; + let home = var("HOME") + .map(PathBuf::from) + .or_else(crate::executable::home_dir) + .unwrap_or_else(crate::executable::home_or_current_dir); + let config_dir = var("CLAUDE_CONFIG_DIR").map(PathBuf::from); + let claude_json = match &config_dir { + Some(dir) => dir.join(".claude.json"), + None => home.join(".claude.json"), + }; + let config_dir = config_dir.unwrap_or_else(|| home.join(".claude")); + let roots = project_roots(cwd); + let managed = PathBuf::from(MANAGED_DIR); + + let mut found = Vec::new(); + let mut add = |servers: Option<&Value>, extra: &[(&str, String)]| { + for (command, args) in stdio_servers(servers, &|name| { + extra + .iter() + .find(|(key, _)| *key == name) + .map(|(_, value)| value.clone()) + .or_else(|| var(name)) + }) { + if found.len() < SERVERS_MAX { + found.push((command, args)); + } + } + }; + + // User and local scopes. + let state = read_json(&claude_json, CLAUDE_JSON_MAX); + if let Some(state) = &state { + add(state.get("mcpServers"), &[]); + for root in &roots { + add( + state + .get("projects") + .and_then(|projects| projects.get(root.to_string_lossy().as_ref())) + .and_then(|project| project.get("mcpServers")), + &[], + ); + } + } + + // Settings files, lowest precedence first. + let mut settings: Vec = Vec::new(); + settings.extend(read_json(&config_dir.join("settings.json"), FILE_MAX)); + for root in roots.iter().rev() { + settings.extend(read_json( + &root.join(".claude").join("settings.json"), + FILE_MAX, + )); + settings.extend(read_json( + &root.join(".claude").join("settings.local.json"), + FILE_MAX, + )); + } + settings.extend(read_json(&managed.join("managed-settings.json"), FILE_MAX)); + + // Project scope, less the servers a settings file turned down. + let disabled: BTreeSet = settings + .iter() + .flat_map(|file| strings(file.get("disabledMcpjsonServers"))) + .collect(); + for root in &roots { + if let Some(project) = read_json(&root.join(".mcp.json"), FILE_MAX) { + let kept = project + .get("mcpServers") + .and_then(Value::as_object) + .map(|servers| { + Value::Object( + servers + .iter() + .filter(|(name, _)| !disabled.contains(name.as_str())) + .map(|(name, server)| (name.clone(), server.clone())) + .collect(), + ) + }); + add(kept.as_ref(), &[]); + } + } + + // Enabled plugins: later settings files win. + let mut enabled: BTreeMap = BTreeMap::new(); + for file in &settings { + if let Some(plugins) = file.get("enabledPlugins").and_then(Value::as_object) { + for (id, on) in plugins { + if let Some(on) = on.as_bool() { + enabled.insert(id.clone(), on); + } + } + } + } + let installed = read_json( + &config_dir.join("plugins").join("installed_plugins.json"), + FILE_MAX, + ); + let records = installed + .as_ref() + .and_then(|installed| installed.get("plugins")) + .and_then(Value::as_object); + for (id, entries) in records.into_iter().flatten() { + if enabled.get(id) != Some(&true) { + continue; + } + for entry in entries.as_array().into_iter().flatten() { + // A project-scoped install loads in its own project only. + if let Some(project) = entry.get("projectPath").and_then(Value::as_str) + && !roots.iter().any(|root| root == Path::new(project)) + { + continue; + } + let Some(root) = entry.get("installPath").and_then(Value::as_str) else { + continue; + }; + let root = PathBuf::from(root); + let plugin_root = [("CLAUDE_PLUGIN_ROOT", root.to_string_lossy().into_owned())]; + if let Some(file) = read_json(&root.join(".mcp.json"), FILE_MAX) { + add(file.get("mcpServers").or(Some(&file)), &plugin_root); + } + if let Some(manifest) = + read_json(&root.join(".claude-plugin").join("plugin.json"), FILE_MAX) + { + for declared in one_or_many(manifest.get("mcpServers")) { + match declared { + Value::String(path) => { + if let Some(file) = read_json(&root.join(path), FILE_MAX) { + add(file.get("mcpServers").or(Some(&file)), &plugin_root); + } + } + servers => add(Some(servers), &plugin_root), + } + } + } + } + } + + // Managed. + if let Some(file) = read_json(&managed.join("managed-mcp.json"), FILE_MAX) { + add(file.get("mcpServers"), &[]); + } + // (The same file can be reached by two spellings of its directory.) + let mut seen = BTreeSet::new(); + found.retain(|server| seen.insert(server.clone())); + found +} + +/// The run's directory, then the git repository root above it (where a +/// project's `.mcp.json` and its local-scope entry are kept), each once. +fn project_roots(cwd: &Path) -> Vec { + let mut roots = vec![cwd.to_path_buf()]; + if let Ok(real) = cwd.canonicalize() + && real != cwd + { + roots.push(real); + } + let mut dir = Some(cwd); + while let Some(here) = dir { + if here.join(".git").exists() { + if !roots.iter().any(|root| root == here) { + roots.push(here.to_path_buf()); + } + break; + } + dir = here.parent(); + } + roots +} + +/// A config file's JSON. Several of these sit in the project, which may be +/// a cloned repository: one that is a symlink to `/dev/zero`, a FIFO or a +/// runaway file is refused at once ([`crate::bounded_file`]). +fn read_json(path: &Path, max: u64) -> Option { + let bytes = match crate::bounded_file::read(path, max) { + Ok(bytes) => bytes, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => return None, + Err(err) => { + tracing::debug!(target: "zeron_harness::claude", path = %path.display(), error = %err, "MCP config not read"); + return None; + } + }; + match serde_json::from_slice(&bytes) { + Ok(value) => Some(value), + Err(err) => { + tracing::debug!(target: "zeron_harness::claude", path = %path.display(), error = %err, "MCP config unreadable"); + None + } + } +} + +fn strings(value: Option<&Value>) -> impl Iterator + '_ { + value + .and_then(Value::as_array) + .into_iter() + .flatten() + .filter_map(Value::as_str) + .map(str::to_owned) +} + +fn one_or_many(value: Option<&Value>) -> Vec<&Value> { + match value { + Some(Value::Array(items)) => items.iter().collect(), + Some(value) => vec![value], + None => Vec::new(), + } +} + +/// The stdio entries of a `{ name: server }` map: command and args, each +/// with `${VAR}` / `${VAR:-default}` expanded. Never the entry's `env`. +fn stdio_servers( + servers: Option<&Value>, + var: &dyn Fn(&str) -> Option, +) -> Vec { + let Some(servers) = servers.and_then(Value::as_object) else { + return Vec::new(); + }; + servers + .values() + .filter_map(|server| { + let server = server.as_object()?; + let kind = server + .get("type") + .and_then(Value::as_str) + .unwrap_or("stdio"); + if kind != "stdio" || server.contains_key("url") { + return None; + } + let command = expand(server.get("command")?.as_str()?, var); + let args = server + .get("args") + .and_then(Value::as_array) + .into_iter() + .flatten() + .filter_map(Value::as_str) + .map(|arg| expand(arg, var)) + .collect(); + Some((command, args)) + }) + .collect() +} + +/// `${VAR}` and `${VAR:-default}`, as the CLI expands them in MCP config. +fn expand(text: &str, var: &dyn Fn(&str) -> Option) -> String { + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(start) = rest.find("${") { + out.push_str(&rest[..start]); + let Some(end) = rest[start..].find('}') else { + out.push_str(&rest[start..]); + return out; + }; + let inner = &rest[start + 2..start + end]; + let (name, default) = match inner.split_once(":-") { + Some((name, default)) => (name, Some(default)), + None => (inner, None), + }; + out.push_str( + &var(name) + .or_else(|| default.map(str::to_owned)) + .unwrap_or_default(), + ); + rest = &rest[start + end + 1..]; + } + out.push_str(rest); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn write(path: &Path, value: &Value) { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, value.to_string()).unwrap(); + } + + fn servers(config: &Path, cwd: &Path) -> Vec { + let env = [ + ( + "CLAUDE_CONFIG_DIR".to_owned(), + config.to_string_lossy().into_owned(), + ), + ( + "HOME".to_owned(), + config.join("home").to_string_lossy().into_owned(), + ), + ] + .into_iter() + .collect(); + configured_mcp_servers(&env, cwd) + } + + fn known(command: &str, args: &[&str]) -> KnownServer { + ( + command.to_owned(), + args.iter().map(|arg| arg.to_string()).collect(), + ) + } + + #[test] + fn user_local_project_and_plugin_servers_are_read_without_their_env() { + let dir = tempfile::tempdir().unwrap(); + let config = dir.path().join("config"); + let project = dir.path().join("project"); + std::fs::create_dir_all(project.join(".git")).unwrap(); + let plugin = config + .join("plugins") + .join("cache") + .join("m") + .join("p") + .join("1.0.0"); + write( + &config.join(".claude.json"), + &json!({ + "mcpServers": { + "user-one": { "command": "uvx", "args": ["user-server"], "env": { "TOKEN": "secret-value" } }, + "remote": { "type": "http", "url": "https://example.com/mcp" }, + }, + "projects": { + project.to_string_lossy(): { "mcpServers": { "local-one": { "type": "stdio", "command": "${HOME}/bin/local" } } }, + "/elsewhere": { "mcpServers": { "other": { "command": "nope" } } }, + }, + }), + ); + write( + &project.join(".mcp.json"), + &json!({ "mcpServers": { + "shared": { "command": "npx", "args": ["-y", "shared-server"] }, + "turned-down": { "command": "never" }, + }}), + ); + write( + &project.join(".claude").join("settings.local.json"), + &json!({ "disabledMcpjsonServers": ["turned-down"] }), + ); + write( + &config.join("settings.json"), + &json!({ "enabledPlugins": { "p@m": true, "off@m": false } }), + ); + write( + &config.join("plugins").join("installed_plugins.json"), + &json!({ "version": 2, "plugins": { + "p@m": [{ "scope": "user", "installPath": plugin.to_string_lossy() }], + "off@m": [{ "scope": "user", "installPath": "/nowhere" }], + }}), + ); + write( + &plugin.join(".mcp.json"), + &json!({ "mcpServers": { "plug": { "command": "${CLAUDE_PLUGIN_ROOT}/bin/server", "args": ["--stdio"] } } }), + ); + + let found = servers(&config, &project); + let home = config.join("home"); + assert!(found.contains(&known("uvx", &["user-server"])), "{found:?}"); + assert!( + found.contains(&known(&format!("{}/bin/local", home.display()), &[])), + "{found:?}" + ); + assert!( + found.contains(&known("npx", &["-y", "shared-server"])), + "{found:?}" + ); + assert!( + found.contains(&known( + &format!("{}/bin/server", plugin.display()), + &["--stdio"] + )), + "{found:?}" + ); + assert!( + !found.iter().any(|(command, _)| command == "never"), + "a disabled .mcp.json server" + ); + assert!( + !found.iter().any(|(command, _)| command == "nope"), + "another project's local server" + ); + assert_eq!(found.len(), 4, "{found:?}"); + assert!(!format!("{found:?}").contains("secret-value")); + } + + #[test] + fn a_missing_or_malformed_config_adds_nothing() { + let dir = tempfile::tempdir().unwrap(); + let config = dir.path().join("config"); + assert_eq!(servers(&config, dir.path()), Vec::::new()); + std::fs::create_dir_all(&config).unwrap(); + std::fs::write(config.join(".claude.json"), b"{ not json").unwrap(); + assert_eq!(servers(&config, dir.path()), Vec::::new()); + } + + /// A cloned repository whose config files are a symlink to `/dev/zero` + /// and a FIFO: both skipped at once, and the other servers still read. + #[cfg(unix)] + #[test] + fn a_project_config_that_is_a_device_or_a_fifo_is_skipped_promptly() { + let dir = tempfile::tempdir().unwrap(); + let config = dir.path().join("config"); + let project = dir.path().join("project"); + std::fs::create_dir_all(project.join(".git")).unwrap(); + std::fs::create_dir_all(project.join(".claude")).unwrap(); + write( + &config.join(".claude.json"), + &json!({ "mcpServers": { "user-one": { "command": "uvx", "args": ["user-server"] } } }), + ); + std::os::unix::fs::symlink("/dev/zero", project.join(".mcp.json")).unwrap(); + std::os::unix::fs::symlink("/dev/zero", project.join(".claude").join("settings.json")) + .unwrap(); + let fifo = project.join(".claude").join("settings.local.json"); + let c_path = std::ffi::CString::new(fifo.as_os_str().as_encoded_bytes()).unwrap(); + // SAFETY: a valid NUL-terminated path. + assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0); + + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = tx.send(servers(&config, &project)); + }); + let found = rx + .recv_timeout(std::time::Duration::from_secs(5)) + .expect("reading the project's config hung"); + assert_eq!(found, vec![known("uvx", &["user-server"])]); + } + + #[test] + fn variables_expand_with_their_defaults() { + let var = |name: &str| (name == "SET").then(|| "value".to_owned()); + assert_eq!(expand("${SET}/x", &var), "value/x"); + assert_eq!(expand("${UNSET:-fallback}/x", &var), "fallback/x"); + assert_eq!(expand("${UNSET}", &var), ""); + assert_eq!(expand("plain ${", &var), "plain ${"); + } +} diff --git a/crates/harness/src/claude/mod.rs b/crates/harness/src/claude/mod.rs index 41b4cc9c6..594e2810d 100644 --- a/crates/harness/src/claude/mod.rs +++ b/crates/harness/src/claude/mod.rs @@ -54,6 +54,8 @@ mod background; pub mod catalog; mod discovery; mod fork_tail; +mod mcp_config; +pub use mcp_config::configured_mcp_servers; mod normalize; pub mod refresh_lock; mod wire; @@ -170,6 +172,18 @@ fn no_conversation(text: &str) -> bool { text.contains("No conversation found") } +/// The first Claude CLI live-verified to report every background task it +/// holds with `background_tasks_changed` (agents and shells: +/// `background.rs`, 2.1.312; held an idle stop off in the measurement rig +/// on 2.1.322). +const REPORTS_BACKGROUND_SINCE: semver::Version = semver::Version::new(2, 1, 312); + +/// Whether a CLI of `version` reports its background work: an unknown +/// version is treated as not. +fn reports_background(version: Option<&semver::Version>) -> bool { + version.is_some_and(|version| *version >= REPORTS_BACKGROUND_SINCE) +} + /// Shown when a chat's Claude session could not be restored. const SESSION_LOST: &str = "The previous Claude session could not be restored, so this chat \ continues in a new one without its earlier context."; @@ -190,28 +204,136 @@ fn mcp_config_arg(mcp: &zeron_proto::McpServer) -> String { .to_string() } -/// The inline `--mcp-config` JSON for Zeron's own server plus the plugins' -/// native servers ([`RunRequest::extra_mcp`]), or `None` when there are none. -/// Zeron's own server wins a name clash. -fn mcp_servers_config_arg( +/// One `mcpServers` entry: Zeron's own over Streamable HTTP when the engine +/// serves it ([`McpServer::http`]; Claude Code takes `"type": "http"` with +/// headers), else a stdio command. +fn mcp_server_entry(server: &zeron_proto::McpServer) -> Value { + match &server.http { + Some(http) => serde_json::json!({ + "type": "http", + "url": http.url, + "headers": http.headers, + }), + None => serde_json::json!({ + "command": server.command, + "args": server.args, + "env": server.env, + }), + } +} + +/// Whether an entry carries a credential (a run's MCP token), so the config +/// must not ride the command line, where `ps` shows it to every local user. +fn carries_secret(server: &zeron_proto::McpServer) -> bool { + server.http.is_some() || server.env.contains_key(McpServer::TOKEN_ENV) +} + +/// The `--mcp-config` JSON for Zeron's own server plus the plugins' +/// native servers ([`RunRequest::extra_mcp`]), or `None` when there are none, +/// and whether it carries a credential. Zeron's own server wins a name clash. +fn mcp_servers_config( mcp: Option<&zeron_proto::McpServer>, extras: &[zeron_proto::McpServer], -) -> Option { +) -> Option<(String, bool)> { if mcp.is_none() && extras.is_empty() { return None; } let mut servers = serde_json::Map::new(); for server in extras.iter().chain(mcp) { - servers.insert( - server.name.clone(), - serde_json::json!({ - "command": server.command, - "args": server.args, - "env": server.env, - }), - ); + servers.insert(server.name.clone(), mcp_server_entry(server)); } - Some(serde_json::json!({ "mcpServers": servers }).to_string()) + let secret = extras.iter().chain(mcp).any(carries_secret); + Some((serde_json::json!({ "mcpServers": servers }).to_string(), secret)) +} + +#[cfg(test)] +fn mcp_servers_config_arg( + mcp: Option<&zeron_proto::McpServer>, + extras: &[zeron_proto::McpServer], +) -> Option { + mcp_servers_config(mcp, extras).map(|(config, _)| config) +} + +/// `config` in an owner-only file of its own (`0600`, in a `0700` directory +/// removed when the returned guard drops): `--mcp-config` takes a path. +fn private_mcp_config(config: &str) -> std::io::Result<(crate::scratch::ScratchDir, PathBuf)> { + use std::io::Write as _; + let dir = crate::scratch::ScratchDir::new("claude-mcp")?; + let path = dir.path().join("mcp.json"); + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let mut file = options.open(&path)?; + // No `sync_all`: the file lives only as long as the run that reads it at + // once, and on macOS it is an `F_FULLFSYNC` that held every agent start + // for a disk-cache flush (4 ms on an M5 Max, 6 ms with builds running). + file.write_all(config.as_bytes())?; + Ok((dir, path)) +} + +/// What [`mcp_config_args`] decided. +struct McpConfigArgs { + /// `--mcp-config `, or nothing. + args: Vec, + /// The private file's directory, kept until the run ends. + dir: Option, + /// Zeron's own server was configured but left out (see below). + zeron_left_out: bool, +} + +/// The `--mcp-config` arguments for Zeron's own server and the plugins' +/// native ones. A config carrying a credential (a run's MCP token) goes in +/// an owner-only file made by `write`, never inline: the command line is +/// visible to every local user in `ps`. Should that file not be made, the +/// run fails closed: the servers carrying a credential are left out (with a +/// warning) and the chat goes on without their tools. +fn mcp_config_args( + mcp: Option<&zeron_proto::McpServer>, + extras: &[zeron_proto::McpServer], + write: impl FnOnce(&str) -> std::io::Result<(crate::scratch::ScratchDir, PathBuf)>, +) -> McpConfigArgs { + let mut out = McpConfigArgs { + args: Vec::new(), + dir: None, + zeron_left_out: false, + }; + let Some((config, secret)) = mcp_servers_config(mcp, extras) else { + return out; + }; + if !secret { + out.args = vec!["--mcp-config".into(), config.into()]; + return out; + } + match write(&config) { + Ok((dir, path)) => { + out.args = vec!["--mcp-config".into(), path.into()]; + out.dir = Some(dir); + } + Err(err) => { + let mcp_kept = mcp.filter(|server| !carries_secret(server)); + let extras_kept: Vec<_> = extras + .iter() + .filter(|server| !carries_secret(server)) + .cloned() + .collect(); + out.zeron_left_out = mcp.is_some() && mcp_kept.is_none(); + tracing::warn!( + target: "zeron_harness::claude", + error = %err, + left_out = extras.len() - extras_kept.len() + usize::from(out.zeron_left_out), + "no private MCP config file: starting without the MCP servers that carry a token" + ); + if let Some((config, secret)) = mcp_servers_config(mcp_kept, &extras_kept) { + debug_assert!(!secret); + out.args = vec!["--mcp-config".into(), config.into()]; + } + } + } + out } /// The `--model` value: the 1M context window is selected via a model-id @@ -487,7 +609,19 @@ impl ClaudeHarness { args } + #[cfg(test)] fn build_command(exe: &std::path::Path, request: &RunRequest, title_only: bool) -> Command { + Self::build_command_with_config(exe, request, title_only).0 + } + + /// The CLI command, and the private file its MCP config was written to + /// when that config carries a credential (kept until the run ends). + fn build_command_with_config( + exe: &std::path::Path, + request: &RunRequest, + title_only: bool, + ) -> (Command, Option) { + let mut mcp_config_dir = None; let mut cmd = Command::new(exe); // Its own process group, so teardown reaches the MCP servers and // tools it started; and no inherited Claude Code session markers. @@ -505,6 +639,15 @@ impl ClaudeHarness { // stream-json`) forwards its own subagents' text too, which only // adds tagged frames to its stream. cmd.env("CLAUDE_CODE_FORWARD_SUBAGENT_TEXT", "1"); + // No session title of the CLI's own. Zeron titles chats itself and + // nothing reads the CLI's title, but in stream-json mode the CLI + // sends one more Haiku request to name every session whose first + // prompt is 10+ characters: the chat's run, and every titling run + // too (its prompt is long), ~4.3 KB each on every new chat. The + // variable latches the CLI's titling as done (2.1.322 `hhi()`); it + // also leaves the process title and terminal tab alone, which a + // piped CLI never shows anyway. A `claude` the run starts inherits it. + cmd.env("CLAUDE_CODE_DISABLE_TERMINAL_TITLE", "1"); // "200K" really limits the window (see `caps_context_at_200k`). if caps_context_at_200k(request) { cmd.env("CLAUDE_CODE_DISABLE_1M_CONTEXT", "1"); @@ -535,16 +678,24 @@ impl ClaudeHarness { // Zeron's own server (and the plugins' native ones) ride beside // the user's configured servers (no `--strict-mcp-config`): the // CLI merges an inline JSON config with settings-sourced ones. - if let Some(config) = mcp_servers_config_arg(request.mcp.as_ref(), &request.extra_mcp) { - cmd.args(["--mcp-config", &config]); - } + // A config with a run's MCP token goes in an owner-only file: + // inline JSON would sit on the command line. + let config = + mcp_config_args(request.mcp.as_ref(), &request.extra_mcp, private_mcp_config); + cmd.args(&config.args); + mcp_config_dir = config.dir; // When to say it waits on background work (the Zeron tool // `wait_on_background`), where that tool is offered. Its own // description and the server's instructions say so too, but // neither reaches the model before it decides to end a turn: // live-probed on 2.1.319, Sonnet never called it with those // alone, and Sonnet and Opus both did with this note. - if request.mcp.as_ref().is_some_and(McpServer::waits_on_background) { + if !config.zeron_left_out + && request + .mcp + .as_ref() + .is_some_and(McpServer::waits_on_background) + { cmd.args(["--append-system-prompt", WAIT_ON_BACKGROUND_NOTE]); } // Delegation goes through that server's side chats only. A bare @@ -570,7 +721,7 @@ impl ClaudeHarness { .stdout(Stdio::piped()) .stderr(Stdio::piped()) .kill_on_drop(true); - cmd + (cmd, mcp_config_dir) } /// Start one CLI process for a run, with its stdio wired up. @@ -579,12 +730,15 @@ impl ClaudeHarness { request: &RunRequest, title_only: bool, ) -> Result { - let mut child = Self::build_command(exe, request, title_only) + let (mut command, mcp_config) = Self::build_command_with_config(exe, request, title_only); + tracing::debug!(target: "first_word", title_only, "claude spawning"); + let mut child = command .spawn() .map_err(|e| { let cwd = (!request.cwd.is_empty()).then(|| std::path::Path::new(&request.cwd)); crate::executable::spawn_error(e, cwd, || crate::executable::binary_hint(exe)) })?; + tracing::debug!(target: "first_word", title_only, pid = child.id(), "claude spawned"); let stdin = child.stdin.take(); let stdout = child.stdout.take(); let stderr = child.stderr.take(); @@ -606,6 +760,7 @@ impl ClaudeHarness { refresh_lock::config_dir_for(&request.env), ), stderr_tail, + mcp_config, }) } @@ -814,6 +969,30 @@ impl Harness for ClaudeHarness { fn wakes_on_background(&self) -> bool { true } + /// `--resume` brings the session back whole (live-verified 2.1.322 + /// against a replay server: the resumed request carries every earlier + /// turn), and teardown never signals mid login refresh + /// ([`refresh_lock::settle`]). Only for a CLI known to report its + /// background work (`background_tasks_changed`, + /// [`REPORTS_BACKGROUND_SINCE`]): the host's other guard against + /// stopping one mid-work. Blocking: may ask the CLI its version once. + /// Launched without `--strict-mcp-config`, the CLI also starts the + /// servers of the user's own MCP scopes ([`mcp_config`]). + fn configured_mcp_servers(&self, request: &RunRequest) -> Vec { + let cwd = if request.cwd.is_empty() { + std::env::current_dir().unwrap_or_default() + } else { + PathBuf::from(&request.cwd) + }; + configured_mcp_servers(&request.env, &cwd) + } + fn stops_when_idle(&self) -> bool { + reports_background( + self.executable_path() + .and_then(|path| crate::executable::binary_version(&path)) + .as_ref(), + ) + } fn display_name(&self) -> &str { "Claude Code" } @@ -1040,7 +1219,10 @@ impl ClaudeHarness { stdout_lines, mut stdin, stderr_tail, + mcp_config, } = Self::launch(&exe, &request, title_only)?; + // The host looks at what this process runs before stopping it idle. + controls.turn.set_agent_pid(child.id()); let lock_dir = refresh_lock::config_dir_for(&request.env); if !title_only { @@ -1082,6 +1264,7 @@ impl ClaudeHarness { kill_grace: self.kill_grace, lock_dir, stderr_tail, + _mcp_config: mcp_config, })); Ok(futures::stream::unfold(event_rx, |mut rx| async move { @@ -1108,6 +1291,8 @@ struct Runtime { stdout_lines: crate::lossy_lines::LossyLines, stdin: Stdin, stderr_tail: crate::StderrTail, + /// The private MCP config file the process was launched with. + mcp_config: Option, } /// The run's stdin, through its writer task. Until the CLI reports its @@ -1285,6 +1470,8 @@ struct Session { lock_dir: PathBuf, /// Rolling stderr tail for the crash message on an unexpected exit. stderr_tail: crate::StderrTail, + /// The private MCP config file, removed when the run ends. + _mcp_config: Option, } /// The per-run event loop: one task multiplexing stdout frames, the steering @@ -1306,6 +1493,7 @@ async fn run_session(session: Session) { kill_grace, lock_dir, mut stderr_tail, + mut _mcp_config, } = session; let RunControls { realtime: _, @@ -1399,6 +1587,7 @@ async fn run_session(session: Session) { // [`background`]), and the stops asked of them. let mut shells = background::BackgroundShells::default(); let mut task_stops_sent = 0u64; + let mut first_lines = 0u8; 'main: loop { if std::mem::take(&mut relaunch) { @@ -1421,6 +1610,8 @@ async fn run_session(session: Session) { stdout_lines = runtime.stdout_lines; stdin = runtime.stdin; stderr_tail = runtime.stderr_tail; + _mcp_config = runtime.mcp_config; + turn.set_agent_pid(child.id()); } Err(error) => { let _ = event_tx.send(Err(error)).await; @@ -1461,6 +1652,10 @@ async fn run_session(session: Session) { // The CLI is still producing: whatever it is doing is not // the quiet end a held turn end's fallback waits for. last_frame_at = tokio::time::Instant::now(); + if first_lines < 16 { + first_lines += 1; + tracing::debug!(target: "first_word", title_only, frame = %line.chars().take(60).collect::(), "claude stdout"); + } let frame = match wire::parse_frame(line) { Ok(frame) => frame, Err(e) => { @@ -1958,6 +2153,7 @@ async fn run_session(session: Session) { } stdin.close_for_teardown(); child.shutdown_with(torn_down, kill_grace).await; + turn.set_agent_pid(None); if let Some(handle) = escalation { handle.abort(); } @@ -2305,6 +2501,44 @@ mod tests { assert!(updated["questions"].is_array()); } + /// The CLI's own Haiku session-title request is skipped on chat runs and + /// titling runs alike: Zeron titles chats itself. + #[test] + fn runs_turn_off_the_clis_own_session_titles() { + let request = RunRequest { + mcp: None, + prompt: "Read src/paginate.ts and list every bug".into(), + harness: None, + model: None, + reasoning: None, + model_options: serde_json::Map::new(), + cwd: String::new(), + sandbox: zeron_proto::SandboxLevel::WorkspaceWrite, + auto_approve: false, + access: Default::default(), + plan: false, + attachments: Vec::new(), + worktree: None, + resume: None, + env: Default::default(), + extra_mcp: Vec::new(), + }; + for title_only in [false, true] { + let mut cmd = + ClaudeHarness::build_command(std::path::Path::new("claude"), &request, title_only); + let value = cmd + .as_std_mut() + .get_envs() + .find(|(key, _)| *key == "CLAUDE_CODE_DISABLE_TERMINAL_TITLE") + .and_then(|(_, value)| value.map(|v| v.to_os_string())); + assert_eq!( + value.as_deref(), + Some(std::ffi::OsStr::new("1")), + "title_only {title_only}" + ); + } + } + #[test] fn runs_ask_the_cli_to_forward_subagent_text() { let request = RunRequest { @@ -2370,6 +2604,7 @@ mod tests { command: "zeron-engine".into(), args: vec!["mcp".into()], env: env.iter().map(|(k, v)| ((*k).to_owned(), (*v).to_owned())).collect(), + http: None, }; let waits = (McpServer::WAITS_ON_BACKGROUND_ENV, "1"); assert_eq!( @@ -2652,6 +2887,150 @@ mod resolution_tests { mod mcp_injection_tests { use super::*; + /// Stopped while idle only where the CLI is known to report its + /// background work; an unknown version isn't. + #[test] + fn idle_stops_need_a_cli_that_reports_its_background_work() { + let v = |text: &str| semver::Version::parse(text).unwrap(); + assert!(reports_background(Some(&v("2.1.312")))); + assert!(reports_background(Some(&v("2.1.322")))); + assert!(reports_background(Some(&v("2.2.0")))); + assert!(!reports_background(Some(&v("2.1.311")))); + assert!(!reports_background(None)); + } + + fn engine_served() -> zeron_proto::McpServer { + zeron_proto::McpServer { + name: "zeron".into(), + command: "/opt/zeron/zeron-engine".into(), + args: vec!["mcp-proxy".into()], + env: [ + (McpServer::CHAT_ID_ENV.to_owned(), "chat-1".to_owned()), + (McpServer::TOKEN_ENV.to_owned(), "secret-token".to_owned()), + ] + .into_iter() + .collect(), + http: Some(zeron_proto::McpHttp { + url: "http://127.0.0.1:4321/mcp".into(), + headers: [("Authorization".to_owned(), "Bearer secret-token".to_owned())] + .into_iter() + .collect(), + }), + } + } + + /// The engine serves Zeron's tools itself: Claude gets the HTTP endpoint + /// (no server process), and the token never reaches the command line — + /// the config is an owner-only file, gone when the run ends. + #[test] + fn an_engine_served_server_is_http_in_a_private_file() { + let (config, secret) = mcp_servers_config(Some(&engine_served()), &[]).unwrap(); + assert!(secret); + let parsed: Value = serde_json::from_str(&config).unwrap(); + assert_eq!(parsed["mcpServers"]["zeron"]["type"], "http"); + assert_eq!(parsed["mcpServers"]["zeron"]["url"], "http://127.0.0.1:4321/mcp"); + assert_eq!( + parsed["mcpServers"]["zeron"]["headers"]["Authorization"], + "Bearer secret-token" + ); + assert!(parsed["mcpServers"]["zeron"].get("command").is_none()); + + let mut request = access_tests_request(); + request.mcp = Some(engine_served()); + let (cmd, dir) = ClaudeHarness::build_command_with_config( + std::path::Path::new("claude"), + &request, + false, + ); + let args: Vec = cmd + .as_std() + .get_args() + .map(|arg| arg.to_string_lossy().into_owned()) + .collect(); + assert!(args.iter().all(|arg| !arg.contains("secret-token")), "{args:?}"); + let at = args.iter().position(|arg| arg == "--mcp-config").unwrap(); + let path = std::path::PathBuf::from(&args[at + 1]); + assert_eq!(std::fs::read_to_string(&path).unwrap(), config); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(&path).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600); + let dir_mode = std::fs::metadata(path.parent().unwrap()).unwrap().permissions().mode(); + assert_eq!(dir_mode & 0o777, 0o700); + } + drop(dir); + assert!(!path.exists(), "the config goes with the run"); + } + + /// The private file can't be made (a full disk, an unwritable temp + /// directory): the token never falls back onto the command line. The + /// run starts without the servers carrying one; the rest are kept. + #[test] + fn a_config_whose_private_file_fails_leaves_the_token_off_the_command_line() { + let plugin = zeron_proto::McpServer { + name: "plugin".into(), + command: "/opt/plugin/server".into(), + args: vec!["--stdio".into()], + ..Default::default() + }; + let tokened_plugin = zeron_proto::McpServer { + name: "tokened".into(), + command: "/opt/plugin/other".into(), + env: [(McpServer::TOKEN_ENV.to_owned(), "plugin-secret".to_owned())] + .into_iter() + .collect(), + ..Default::default() + }; + let config = mcp_config_args(Some(&engine_served()), &[plugin, tokened_plugin], |_| { + Err(std::io::Error::other("no space left on device")) + }); + assert!(config.dir.is_none()); + assert!(config.zeron_left_out); + let args: Vec = config + .args + .iter() + .map(|arg| arg.to_string_lossy().into_owned()) + .collect(); + assert!( + args.iter() + .all(|arg| !arg.contains("secret-token") && !arg.contains("plugin-secret")), + "{args:?}" + ); + assert_eq!(args[0], "--mcp-config"); + let parsed: Value = serde_json::from_str(&args[1]).unwrap(); + let servers = parsed["mcpServers"].as_object().unwrap(); + assert_eq!(servers.keys().collect::>(), vec!["plugin"]); + + // Only Zeron's own: no `--mcp-config` at all. + let config = mcp_config_args(Some(&engine_served()), &[], |_| { + Err(std::io::Error::other("no space left on device")) + }); + assert!(config.args.is_empty()); + assert!(config.zeron_left_out); + } + + fn access_tests_request() -> RunRequest { + RunRequest { + mcp: None, + prompt: "hi".into(), + harness: None, + model: None, + reasoning: None, + model_options: serde_json::Map::new(), + cwd: String::new(), + sandbox: zeron_proto::SandboxLevel::WorkspaceWrite, + auto_approve: false, + access: AccessMode::FullAccess, + plan: false, + attachments: Vec::new(), + worktree: None, + resume: None, + env: Default::default(), + extra_mcp: Vec::new(), + } + } + #[test] fn mcp_config_arg_spells_the_server_the_way_the_cli_reads_it() { let mcp = zeron_proto::McpServer { @@ -2661,6 +3040,7 @@ mod mcp_injection_tests { env: [("ZERON_CHAT_ID".to_owned(), "chat-1".to_owned())] .into_iter() .collect(), + http: None, }; let parsed: Value = serde_json::from_str(&mcp_config_arg(&mcp)).unwrap(); assert_eq!(parsed["mcpServers"]["zeron"]["command"], "/opt/zeron/zeron"); @@ -2680,12 +3060,14 @@ mod mcp_injection_tests { command: "/opt/zeron/zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let extra = zeron_proto::McpServer { name: "plugin-focus".into(), command: "/data/plugins/.loaded/focus/0123/bin/server".into(), args: vec!["--stdio".into()], env: [("FOCUS".to_owned(), "1".to_owned())].into_iter().collect(), + http: None, }; assert_eq!(mcp_servers_config_arg(None, &[]), None); let both: Value = serde_json::from_str( diff --git a/crates/harness/src/codex/mod.rs b/crates/harness/src/codex/mod.rs index f92c9c62d..e1f92a838 100644 --- a/crates/harness/src/codex/mod.rs +++ b/crates/harness/src/codex/mod.rs @@ -152,10 +152,21 @@ fn agent_tools_off() -> [(String, Value); 3] { /// to the user's `mcp_servers` table. fn codex_mcp_overrides(mcp: &zeron_proto::McpServer) -> Vec<(String, Value)> { let key = |field: &str| format!("mcp_servers.{}.{field}", mcp.name); - vec![ - (key("command"), mcp.command.clone().into()), - (key("args"), json!(mcp.args)), - (key("env"), json!(mcp.env)), + // The engine serves Zeron's tools itself over Streamable HTTP, which + // Codex speaks: no server process. These overrides ride `thread/start` + // on the app-server's stdin, never the command line. + let transport = match &mcp.http { + Some(http) => vec![ + (key("url"), http.url.clone().into()), + (key("http_headers"), json!(http.headers)), + ], + None => vec![ + (key("command"), mcp.command.clone().into()), + (key("args"), json!(mcp.args)), + (key("env"), json!(mcp.env)), + ], + }; + transport.into_iter().chain([ // Our injected server's tools are Zeron's own: approve them in every // access mode, so Codex neither rejects a call under `never` nor // asks about one under the others. @@ -167,7 +178,8 @@ fn codex_mcp_overrides(mcp: &zeron_proto::McpServer) -> Vec<(String, Value)> { // A cold start (a fresh machine paging the binary in) can outlast // the 10 s default before the server answers `initialize`. (key("startup_timeout_sec"), 30.into()), - ] + ]) + .collect() } /// Dotted `thread/start` config overrides for the plugins' native MCP @@ -3417,6 +3429,32 @@ mod tests { mod mcp_injection_tests { use super::*; + /// The engine serves Zeron's tools itself: Codex gets its URL and + /// header (over the app-server's stdin), no command to start. + #[test] + fn an_engine_served_zeron_server_is_codex_http() { + let mcp = zeron_proto::McpServer { + name: "zeron".into(), + command: "/opt/zeron/zeron-engine".into(), + args: vec!["mcp-proxy".into()], + env: Default::default(), + http: Some(zeron_proto::McpHttp { + url: "http://127.0.0.1:4321/mcp".into(), + headers: [("Authorization".to_owned(), "Bearer t".to_owned())] + .into_iter() + .collect(), + }), + }; + let overrides: std::collections::HashMap = + codex_mcp_overrides(&mcp).into_iter().collect(); + assert_eq!(overrides["mcp_servers.zeron.url"], "http://127.0.0.1:4321/mcp"); + assert_eq!(overrides["mcp_servers.zeron.http_headers"]["Authorization"], "Bearer t"); + assert!(!overrides.contains_key("mcp_servers.zeron.command")); + assert!(!overrides.contains_key("mcp_servers.zeron.env")); + assert_eq!(overrides["mcp_servers.zeron.default_tools_approval_mode"], "approve"); + assert_eq!(overrides["mcp_servers.zeron.tool_timeout_sec"], ZERON_TOOL_TIMEOUT_SECS); + } + #[test] fn codex_mcp_overrides_use_the_dotted_mcp_servers_keys() { let mcp = zeron_proto::McpServer { @@ -3426,6 +3464,7 @@ mod mcp_injection_tests { env: [("ZERON_CHAT_ID".to_owned(), "chat-1".to_owned())] .into_iter() .collect(), + http: None, }; let overrides: serde_json::Map = codex_mcp_overrides(&mcp).into_iter().collect(); @@ -3450,6 +3489,7 @@ mod mcp_injection_tests { command: "/data/plugins/.loaded/focus/0123/bin/server".into(), args: vec!["--stdio".into()], env: [("FOCUS".to_owned(), "1".to_owned())].into_iter().collect(), + http: None, }; let overrides: serde_json::Map = codex_extra_mcp_overrides(&[extra]).into_iter().collect(); diff --git a/crates/harness/src/lib.rs b/crates/harness/src/lib.rs index 287d3aa29..80c1f7e4d 100644 --- a/crates/harness/src/lib.rs +++ b/crates/harness/src/lib.rs @@ -82,6 +82,8 @@ pub struct TurnControl { stop: std::sync::Arc, background: std::sync::Arc, processes: std::sync::Arc, + /// The agent process's pid (0: none, or not known). + agent_pid: std::sync::Arc, } impl TurnControl { @@ -101,6 +103,20 @@ impl TurnControl { .store(tasks, std::sync::atomic::Ordering::Release); } + /// Driver: the agent CLI process the runtime runs now (`None` once it + /// has gone). The host checks its live children before stopping an idle + /// runtime ([`process::unexpected_descendants`]), whatever the agent + /// reports of its background work. + pub fn set_agent_pid(&self, pid: Option) { + self.agent_pid + .store(pid.unwrap_or(0), std::sync::atomic::Ordering::Release); + } + + /// Host: the agent CLI's pid, while the driver knows it. + pub fn agent_pid(&self) -> Option { + Some(self.agent_pid.load(std::sync::atomic::Ordering::Acquire)).filter(|pid| *pid != 0) + } + /// Host: the runtime reports live background work, so retiring it now /// would kill work nobody asked to stop. pub fn background_live(&self) -> bool { @@ -299,6 +315,22 @@ pub trait Harness: Send + Sync { fn wakes_on_background(&self) -> bool { false } + /// Whether a runtime parked between turns, with nothing pending, may be + /// stopped once idle a short while (the engine's `AGENT_IDLE_STOP`) and + /// its session resumed by the next message — for drivers whose resume + /// is proven to bring the whole context back. Elsewhere the runtime + /// stays warm for the long idle window, as before. + fn stops_when_idle(&self) -> bool { + false + } + /// The stdio MCP servers the agent starts on its own, from its user's + /// configuration, beyond the ones in the run request: children of its + /// process that aren't background work ([`process::unexpected_descendants`]). + /// Never their `env`. Blocking (reads files); once per run, off the + /// async threads. + fn configured_mcp_servers(&self, _request: &RunRequest) -> Vec { + Vec::new() + } /// Whether this driver honors [`TurnControl::stop_turn`]: it ends only /// the in-flight turn (`Done { Interrupted }`) and the runtime stays up /// with its background work. Otherwise stopping a turn tears the runtime @@ -418,6 +450,7 @@ pub mod approval; pub mod archive_install; mod background; pub use background::{MarkOutcome, MarkRefused, OutputSource}; +pub mod bounded_file; mod catalog; mod catalog_failure; pub(crate) mod code_signature; diff --git a/crates/harness/src/mock.rs b/crates/harness/src/mock.rs index 2539b9f3a..7b5b14c3f 100644 --- a/crates/harness/src/mock.rs +++ b/crates/harness/src/mock.rs @@ -25,6 +25,143 @@ fn fanout_count(prompt: &str) -> Option { Some(count.clamp(1, 200)) } +/// The step count a `/longthread [N]` prompt asks for (1,000 when bare). +fn longthread_count(prompt: &str) -> Option { + let rest = prompt.trim().strip_prefix("/longthread")?; + if !rest.is_empty() && !rest.starts_with(char::is_whitespace) { + return None; + } + let count = rest.trim().parse().unwrap_or(1000); + Some(count.clamp(1, 20_000)) +} + +/// The `/longthread` reply: ademark's A11 thread (bench/replay's +/// `long-2000`), `count` steps each a short text (one in five with a code +/// block) and a call reading one small file, the last text holding +/// `LONGTHREAD-END`. Streamed unpaced, as the replay is. +fn longthread_events(count: usize) -> Vec { + let mut events = Vec::with_capacity(count * 3 + 2); + for n in 1..=count { + let mut text = format!( + "Step {n} of {count}. I read src/invoices.ts and noted what step {n} needs from it." + ); + if n % 5 == 0 { + text.push_str(concat!( + "\n\n```ts\n", + "function summarize(invoice: Invoice): InvoiceSummary {\n", + " const total = invoiceTotal(invoice);\n", + " return { id: invoice.id, totalCents: total };\n", + "}\n", + "```" + )); + } + events.push(AgentEvent::TextDelta { text }); + let id = format!("toolu_long_{n:04}"); + events.push(AgentEvent::ToolCall { + id: id.clone(), + call: zeron_proto::ToolCall::ReadFile { + path: "/private/tmp/ademark-recording/arena/src/invoices.ts".into(), + }, + }); + events.push(AgentEvent::ToolResult { + id, + is_error: false, + output: Some("export function invoiceTotal() {}\n".into()), + diff: None, + preview_paths: Vec::new(), + image_payloads: Vec::new(), + exit_code: None, + }); + } + events.push(AgentEvent::TextDelta { + text: "All steps done. LONGTHREAD-END".into(), + }); + events.push(AgentEvent::Done { + status: DoneStatus::Completed, + result: None, + error: None, + session_id: None, + }); + events +} + +/// The seconds a `/stream [SECONDS]` prompt asks for (180 when bare). +fn stream_seconds(prompt: &str) -> Option { + let rest = prompt.trim().strip_prefix("/stream")?; + if !rest.is_empty() && !rest.starts_with(char::is_whitespace) { + return None; + } + let seconds = rest.trim().parse().unwrap_or(180); + Some(seconds.clamp(1, 3600)) +} + +/// How often a `/stream` reply's chunks come, and how many characters each +/// holds: ademark's A7 replay, 80 tokens a second in 50 ms chunks (four +/// tokens of about four characters). +const STREAM_EVERY: std::time::Duration = std::time::Duration::from_millis(50); +const STREAM_CHARS: usize = 16; + +/// The `/stream` reply's text: ademark's A7 stream (bench/replay's +/// `stream-1`), one reply of text, code blocks and a diff block, as long as +/// `seconds` of chunks at its pace. +fn stream_text(seconds: u64) -> String { + let want = seconds as usize * (1000 / STREAM_EVERY.as_millis() as usize) * STREAM_CHARS; + let mut text = String::with_capacity(want + 512); + let mut n = 0; + while text.len() < want { + n += 1; + text.push_str(&format!( + "Section {n}. The invoice totals are summed per customer before the \ + discount applies, so `invoiceTotal` stays the one place that rounds. \ + The reports read the same totals, which keeps the **monthly** view and \ + the export in step.\n\n" + )); + if n % 3 == 0 { + text.push_str(concat!( + "```ts\n", + "export function invoiceTotal(invoice: Invoice): number {\n", + " const lines = invoice.lines.map((l) => l.quantity * l.unitCents);\n", + " return Math.round(lines.reduce((a, b) => a + b, 0));\n", + "}\n", + "```\n\n", + )); + } + if n == 7 { + text.push_str(concat!( + "```diff\n", + "- return total;\n", + "+ return Math.round(total);\n", + "```\n\n", + )); + } + } + text +} + +/// The `/stream` reply, paced: `STREAM_CHARS` characters every +/// `STREAM_EVERY`, then the end of the turn. +fn stream_events(seconds: u64) -> BoxStream<'static, Result> { + let chars: Vec = stream_text(seconds).chars().collect(); + let chunks: Vec = chars + .chunks(STREAM_CHARS) + .map(|c| c.iter().collect()) + .collect(); + futures::stream::iter(chunks) + .then(|text| async move { + tokio::time::sleep(STREAM_EVERY).await; + Ok(AgentEvent::TextDelta { text }) + }) + .chain(futures::stream::once(async { + Ok(AgentEvent::Done { + status: DoneStatus::Completed, + result: None, + error: None, + session_id: None, + }) + })) + .boxed() +} + /// The message a `/error ` prompt fails the run with. fn scripted_error(prompt: &str) -> Option<&str> { let message = prompt.trim().strip_prefix("/error ")?.trim(); @@ -390,6 +527,18 @@ impl Harness for MockHarness { // `ZERON_MOCK_DELAY_MS` apart, 2.5s by default. // `/error `: the run fails with that message (error cards // for any provider failure, without a real provider). + // `/longthread [N]`: ademark's A11 long thread, N steps of a text + // and a file read (1,000 when bare), for timing a long chat's open. + if let Some(count) = longthread_count(&request.prompt) { + let events = longthread_events(count).into_iter().map(Ok); + return Ok(futures::stream::iter(events).boxed()); + } + // `/stream [SECONDS]`: ademark's A7 stream, one reply of text, code + // and a diff at 80 tokens a second in 50 ms chunks, for SECONDS (180 + // when bare), for what a window spends while agents stream. + if let Some(seconds) = stream_seconds(&request.prompt) { + return Ok(stream_events(seconds)); + } if let Some(message) = scripted_error(&request.prompt) { let events = vec![ Ok(AgentEvent::Error { @@ -1361,6 +1510,48 @@ mod tests { assert_eq!(fanout_count("Walk me through the pipeline"), None); } + #[test] + fn stream_is_a7s_reply_at_its_pace() { + assert_eq!(stream_seconds("/stream"), Some(180)); + assert_eq!(stream_seconds(" /stream 30 "), Some(30)); + assert_eq!(stream_seconds("/streams"), None); + let text = stream_text(10); + // 10 s of 16-character chunks every 50 ms, and a paragraph more. + assert!((3200..3800).contains(&text.len()), "{}", text.len()); + assert!(text.contains("```ts") && text.contains("```diff")); + } + + #[test] + fn longthread_is_a11s_thread() { + assert_eq!(longthread_count("/longthread"), Some(1000)); + assert_eq!(longthread_count(" /longthread 20 "), Some(20)); + assert_eq!(longthread_count("/longthreads"), None); + let events = longthread_events(10); + let texts: Vec<&str> = events + .iter() + .filter_map(|event| match event { + AgentEvent::TextDelta { text } => Some(text.as_str()), + _ => None, + }) + .collect(); + let reads = events + .iter() + .filter(|event| { + matches!( + event, + AgentEvent::ToolCall { + call: zeron_proto::ToolCall::ReadFile { .. }, + .. + } + ) + }) + .count(); + assert_eq!((texts.len(), reads), (11, 10)); + assert!(texts[4].contains("```ts"), "one text in five holds code"); + assert!(texts[10].ends_with("LONGTHREAD-END")); + assert!(matches!(events.last(), Some(AgentEvent::Done { .. }))); + } + #[test] fn fanout_spawns_every_scout_and_every_seventh_fails() { let spawns = fanout_spawns(40) diff --git a/crates/harness/src/model_context.rs b/crates/harness/src/model_context.rs index 87c45ff30..a91c1d57d 100644 --- a/crates/harness/src/model_context.rs +++ b/crates/harness/src/model_context.rs @@ -145,18 +145,32 @@ fn hash_files<'a>( ) -> Result<(), HarnessError> { for path in files { field(hash, path.as_os_str().as_encoded_bytes()); - match std::fs::read(path) { + // Some of these sit in the project (`opencode.json` up its tree), a + // repository anyone may have written: one that is a FIFO or a + // symlink to `/dev/zero` is refused, never read. + match crate::bounded_file::read(path, MAX_HASHED_FILE) { Ok(bytes) => { hash.update([1]); field(hash, &bytes); } Err(e) if e.kind() == std::io::ErrorKind::NotFound => hash.update([0]), + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::InvalidInput | std::io::ErrorKind::FileTooLarge + ) => + { + hash.update([2]); + } Err(e) => return Err(e.into()), } } Ok(()) } +/// The most of one config or credentials file hashed. +const MAX_HASHED_FILE: u64 = 16 * 1024 * 1024; + impl ModelContext { pub(crate) fn key(&self) -> [u8; 32] { Sha256::digest(self.hash.as_bytes()).into() @@ -188,4 +202,27 @@ mod tests { std::fs::remove_file(&file).unwrap(); assert_eq!(key(), missing); } + + /// A project's `opencode.json` that is a symlink to `/dev/zero` or a + /// FIFO is hashed as unreadable at once, never read. + #[cfg(unix)] + #[test] + fn a_config_that_is_a_device_or_a_fifo_is_not_read() { + let dir = tempfile::tempdir().unwrap(); + let zero = dir.path().join("opencode.json"); + std::os::unix::fs::symlink("/dev/zero", &zero).unwrap(); + let fifo = dir.path().join("opencode.jsonc"); + let c_path = std::ffi::CString::new(fifo.as_os_str().as_encoded_bytes()).unwrap(); + // SAFETY: a valid NUL-terminated path. + assert_eq!(unsafe { libc::mkfifo(c_path.as_ptr(), 0o600) }, 0); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let mut hash = Sha256::new(); + let _ = tx.send(hash_files(&mut hash, [zero, fifo].iter()).is_ok()); + }); + assert!( + rx.recv_timeout(std::time::Duration::from_secs(5)) + .expect("hashing the project's config hung") + ); + } } diff --git a/crates/harness/src/muse/session.rs b/crates/harness/src/muse/session.rs index d9f387e3b..72f52245a 100644 --- a/crates/harness/src/muse/session.rs +++ b/crates/harness/src/muse/session.rs @@ -1089,12 +1089,14 @@ mod session_mcp_tests { command: "/opt/zeron/zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let extra = zeron_proto::McpServer { name: "plugin-focus".into(), command: "/data/plugins/.loaded/focus/0123/bin/server".into(), args: vec![], env: [("FOCUS".to_owned(), "1".to_owned())].into_iter().collect(), + http: None, }; assert_eq!(session_mcp_config(None, &[], true), None); assert_eq!( diff --git a/crates/harness/src/opencode/mod.rs b/crates/harness/src/opencode/mod.rs index 716c62f9c..e06c4ed22 100644 --- a/crates/harness/src/opencode/mod.rs +++ b/crates/harness/src/opencode/mod.rs @@ -5998,12 +5998,14 @@ mod mcp_injection_tests { command: "/opt/zeron/zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let extra = zeron_proto::McpServer { name: "plugin-focus".into(), command: "/data/plugins/.loaded/focus/0123/bin/server".into(), args: vec!["--stdio".into()], env: [("FOCUS".to_owned(), "1".to_owned())].into_iter().collect(), + http: None, }; assert_eq!( mcp_configs(Some("{\"model\":\"keep\"}".into()), None, &[], Protocol::V1).unwrap(), @@ -6068,6 +6070,7 @@ http.createServer((req, res) => {{ command: "/path with spaces/zeron".into(), args: vec!["mcp".into()], env: [("ZERON_CHAT_ID".into(), "first".into())].into(), + http: None, }; let mut second = first.clone(); second.env.insert("ZERON_CHAT_ID".into(), "second".into()); @@ -6160,6 +6163,7 @@ http.createServer((req, res) => { command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let mut server = Server::spawn( &exe, @@ -6286,6 +6290,7 @@ if (process.argv.includes('--version')) {{ command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; for _ in 0..2 { let mut server = Server::spawn( @@ -6312,6 +6317,7 @@ if (process.argv.includes('--version')) {{ command: "/path with spaces/zeron".into(), args: vec!["mcp".into()], env: [("ZERON_CHAT_ID".into(), "first".into())].into(), + http: None, }; for protocol in [Protocol::V1, Protocol::V2] { let inherited = match protocol { diff --git a/crates/harness/src/pi/mcp.rs b/crates/harness/src/pi/mcp.rs index f52d5b1dd..a67e9f690 100644 --- a/crates/harness/src/pi/mcp.rs +++ b/crates/harness/src/pi/mcp.rs @@ -63,12 +63,14 @@ mod tests { command: "/opt/zeron/zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let extra = zeron_proto::McpServer { name: "plugin-focus".into(), command: "/data/plugins/.loaded/focus/0123/bin/server".into(), args: vec![], env: Default::default(), + http: None, }; let mut cmd = Command::new("pi"); let scratch = configure_all(&mut cmd, Some(&zeron), std::slice::from_ref(&extra)) diff --git a/crates/harness/src/process.rs b/crates/harness/src/process.rs index 05aad6b08..8052c4839 100644 --- a/crates/harness/src/process.rs +++ b/crates/harness/src/process.rs @@ -164,6 +164,8 @@ pub(crate) async fn descendants(root: u32) -> Vec { struct ProcEntry { pid: i32, ppid: i32, + /// Its process group (0 where the table doesn't say). + pgid: i32, start: Option, } @@ -185,7 +187,7 @@ fn tree_of(root: i32, table: &[ProcEntry]) -> Vec { #[cfg(unix)] async fn ps_table() -> Vec { let Ok(out) = tokio::process::Command::new("ps") - .args(["-A", "-o", "pid=,ppid="]) + .args(["-A", "-o", "pid=,ppid=,pgid="]) .stdin(std::process::Stdio::null()) .stderr(std::process::Stdio::null()) .output() @@ -199,9 +201,11 @@ async fn ps_table() -> Vec { .filter_map(|line| { let mut cols = line.split_whitespace().map(str::parse::); let (pid, ppid) = (cols.next()?.ok()?, cols.next()?.ok()?); + let pgid = cols.next().and_then(Result::ok).unwrap_or(0); Some(ProcEntry { pid, ppid, + pgid, start: proc_table::start_time(pid), }) }) @@ -223,10 +227,11 @@ mod proc_table { .filter_map(|entry| entry.file_name().to_str()?.parse::().ok()) .filter_map(|pid| { let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; - let (ppid, start) = parse_stat(&stat)?; + let (ppid, pgid, start) = parse_stat(&stat)?; Some(ProcEntry { pid, ppid, + pgid, start: Some(start), }) }) @@ -236,7 +241,7 @@ mod proc_table { #[cfg(target_os = "linux")] pub(super) fn start_time(pid: i32) -> Option { let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?; - parse_stat(&stat).map(|(_, start)| start) + parse_stat(&stat).map(|(_, _, start)| start) } #[cfg(target_os = "macos")] @@ -261,6 +266,7 @@ mod proc_table { Some(ProcEntry { pid, ppid: info.pbi_ppid as i32, + pgid: info.pbi_pgid as i32, start: Some(start_of(&info)), }) }) @@ -295,6 +301,124 @@ mod proc_table { info.pbi_start_tvsec * 1_000_000 + info.pbi_start_tvusec } + #[cfg(target_os = "linux")] + pub(super) fn zombie(pid: i32) -> bool { + std::fs::read_to_string(format!("/proc/{pid}/stat")) + .ok() + .and_then(|stat| { + let rest = &stat[stat.rfind(')')? + 1..]; + rest.split_whitespace().next().map(|state| state == "Z") + }) + .unwrap_or(false) + } + + #[cfg(target_os = "linux")] + pub(super) fn argv(pid: i32) -> Option> { + let raw = std::fs::read(format!("/proc/{pid}/cmdline")).ok()?; + Some( + raw.split(|byte| *byte == 0) + .filter(|arg| !arg.is_empty()) + .map(|arg| String::from_utf8_lossy(arg).into_owned()) + .collect(), + ) + } + + /// The process's name as the kernel keeps it (its executable's file + /// name, at most 15 bytes): never its arguments. + #[cfg(target_os = "linux")] + pub(super) fn name(pid: i32) -> Option { + let comm = std::fs::read_to_string(format!("/proc/{pid}/comm")).ok()?; + let comm = comm.trim_end_matches('\n'); + (!comm.is_empty()).then(|| comm.to_owned()) + } + + /// The process's name as the kernel keeps it (its executable's file + /// name, at most 32 bytes): never its arguments. + #[cfg(target_os = "macos")] + pub(super) fn name(pid: i32) -> Option { + let info = bsd_info(pid)?; + let field = |raw: &[libc::c_char]| { + let bytes: Vec = raw + .iter() + .take_while(|byte| **byte != 0) + .map(|byte| *byte as u8) + .collect(); + (!bytes.is_empty()).then(|| String::from_utf8_lossy(&bytes).into_owned()) + }; + field(&info.pbi_name).or_else(|| field(&info.pbi_comm)) + } + + #[cfg(not(any(target_os = "linux", target_os = "macos")))] + pub(super) fn name(_pid: i32) -> Option { + None + } + + #[cfg(target_os = "macos")] + pub(super) fn zombie(pid: i32) -> bool { + // SZOMB (sys/proc.h). + bsd_info(pid).is_some_and(|info| info.pbi_status == 5) + } + + /// `KERN_PROCARGS2`: argc, the exec path, padding, then argv. + #[cfg(target_os = "macos")] + pub(super) fn argv(pid: i32) -> Option> { + let mut mib = [libc::CTL_KERN, libc::KERN_PROCARGS2, pid]; + let mut size: libc::size_t = 0; + // SAFETY: a null buffer asks for the size only. + let asked = unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 3, + std::ptr::null_mut(), + &mut size, + std::ptr::null_mut(), + 0, + ) + }; + if asked != 0 || size < 4 { + return None; + } + let mut buf = vec![0u8; size]; + // SAFETY: `buf` is `size` bytes; sysctl writes at most that and says how many. + let read = unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 3, + buf.as_mut_ptr().cast(), + &mut size, + std::ptr::null_mut(), + 0, + ) + }; + if read != 0 || size < 4 { + return None; + } + buf.truncate(size); + let argc = i32::from_ne_bytes(buf[..4].try_into().ok()?).max(0) as usize; + let mut rest = &buf[4..]; + // The exec path, then NUL padding up to argv. + let path_end = rest.iter().position(|byte| *byte == 0)?; + rest = &rest[path_end..]; + let start = rest.iter().position(|byte| *byte != 0)?; + rest = &rest[start..]; + Some( + rest.split(|byte| *byte == 0) + .take(argc) + .map(|arg| String::from_utf8_lossy(arg).into_owned()) + .collect(), + ) + } + + #[cfg(not(any(target_os = "linux", target_os = "macos")))] + pub(super) fn zombie(_pid: i32) -> bool { + false + } + + #[cfg(not(any(target_os = "linux", target_os = "macos")))] + pub(super) fn argv(_pid: i32) -> Option> { + None + } + #[cfg(not(any(target_os = "linux", target_os = "macos")))] pub(super) fn read() -> Vec { Vec::new() @@ -305,17 +429,114 @@ mod proc_table { None } - /// `(ppid, starttime)` from a `/proc//stat` line: fields 4 and 22, - /// counted after the parenthesized command name (which may itself hold - /// spaces and parentheses). + /// `(ppid, pgrp, starttime)` from a `/proc//stat` line: fields 4, + /// 5 and 22, counted after the parenthesized command name (which may + /// itself hold spaces and parentheses). #[cfg_attr(not(target_os = "linux"), allow(dead_code))] - pub(super) fn parse_stat(stat: &str) -> Option<(i32, u64)> { + pub(super) fn parse_stat(stat: &str) -> Option<(i32, i32, u64)> { let rest = &stat[stat.rfind(')')? + 1..]; let fields: Vec<&str> = rest.split_whitespace().collect(); - Some((fields.get(1)?.parse().ok()?, fields.get(19)?.parse().ok()?)) + Some(( + fields.get(1)?.parse().ok()?, + fields.get(2)?.parse().ok()?, + fields.get(19)?.parse().ok()?, + )) } } +/// An MCP server a run was configured with: its command and leading args, +/// as the agent starts it (`McpServer::command`/`args`). +pub type KnownServer = (String, Vec); + +/// The live descendants of `root` (an agent CLI) that aren't MCP servers its +/// run was configured with (`known`, and whatever those start), each as +/// `" "`: the process's name only, never its arguments (they +/// can hold anything, and this is logged). Independent of what the agent reports about its own +/// background work: a shell or dev server it started shows up here whether +/// or not the agent listed it. Zombies don't count. Blocking: one +/// process-table read and an argv read per child. +/// +/// An agent that leads its own process group (every agent Zeron starts +/// through `owned::configure`) also holds on to what was left in that +/// group: a stop signals the whole group, so `cmd &` run by a shell that +/// has since exited (re-parented to init, but still in the group) would be +/// ended with the agent. Those count too, whoever their parent is now. +/// (Claude Code 2.1.322 runs each Bash command in a session and group of +/// its own, so what its commands leave behind is outside its group: the +/// stop never signals it, and it outlives the agent either way.) +/// +/// A process table that can't be read answers with one entry saying so, so +/// a caller holding on "anything here" errs toward keeping the agent. +/// (Windows: not traced here; always empty.) +pub fn unexpected_descendants(root: u32, known: &[KnownServer]) -> Vec { + #[cfg(unix)] + { + let table = proc_table::read(); + if table.is_empty() { + return vec!["(the process table can't be read)".to_owned()]; + } + let root = root as i32; + // Its group only if it leads one: an agent sharing its parent's + // group would count every process of that parent's. + let group = table + .iter() + .any(|entry| entry.pid == root && entry.pgid == root) + .then_some(root); + let tree = group.map(|_| tree_of(root, &table)).unwrap_or_default(); + let mut found = Vec::new(); + for entry in table.iter().filter(|entry| { + entry.pid > 1 + && entry.pid != root + && (entry.ppid == root + // Left in the group, and no longer under the agent + // (what is under it counts through its child). + || (group == Some(entry.pgid) && !tree.contains(&entry.pid))) + }) { + if proc_table::zombie(entry.pid) { + continue; + } + let argv = proc_table::argv(entry.pid).unwrap_or_default(); + if known + .iter() + .any(|(command, args)| started_as(&argv, command, args)) + { + continue; + } + let name = proc_table::name(entry.pid) + .or_else(|| { + let program = std::path::Path::new(argv.first()?).file_name()?; + Some(program.to_string_lossy().into_owned()) + }) + .unwrap_or_else(|| "?".to_owned()); + found.push(format!("{} {name}", entry.pid)); + } + found + } + #[cfg(not(unix))] + { + let _ = (root, known); + Vec::new() + } +} + +/// Whether a process's `argv` is `command args…`: as given, or with an +/// interpreter in front (a `#!` script runs as `node /path/server …`), the +/// command named by path or by its file name. +#[cfg_attr(not(unix), allow(dead_code))] +fn started_as(argv: &[String], command: &str, args: &[String]) -> bool { + let name = |path: &str| { + std::path::Path::new(path) + .file_name() + .map(|name| name.to_string_lossy().into_owned()) + .unwrap_or_default() + }; + let wanted = name(command); + (0..argv.len().min(3)).any(|i| { + (argv[i] == command || (!wanted.is_empty() && name(&argv[i]) == wanted)) + && argv[i + 1..].starts_with(args) + }) +} + /// Start times recorded by [`descendants`] snapshots, consulted before every /// signal [`terminate_tree`] sends. The shared drivers pass bare pids /// between the two, so the identity travels here. @@ -495,7 +716,7 @@ mod tests { fn proc_stat_fields_survive_hostile_command_names() { // Field 22 (starttime) and 4 (ppid), with a comm holding ") (". let stat = "4242 (evil) (name) S 17 4242 4242 0 -1 4194560 100 0 0 0 1 2 0 0 20 0 1 0 987654 1000 10"; - assert_eq!(proc_table::parse_stat(stat), Some((17, 987654))); + assert_eq!(proc_table::parse_stat(stat), Some((17, 4242, 987654))); assert_eq!(proc_table::parse_stat("4242 (truncated) S 17"), None); } @@ -572,3 +793,151 @@ mod tests { } pub(crate) mod owned; + +#[cfg(all(test, unix))] +mod unexpected_descendants_tests { + use super::*; + + /// `sh` standing in for an agent, with two children: one a configured + /// MCP server, one not (a background shell's command). + fn agent() -> std::process::Child { + let child = std::process::Command::new("/bin/sh") + .args(["-c", "sleep 3601 & sleep 3602 & wait"]) + .spawn() + .unwrap(); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while unexpected_descendants(child.id(), &[]).len() < 2 { + assert!( + std::time::Instant::now() < deadline, + "the children never started" + ); + std::thread::sleep(std::time::Duration::from_millis(20)); + } + child + } + + fn end(mut agent: std::process::Child) { + let group = agent.id(); + for pid in proc_table::read().iter().filter(|e| e.ppid == group as i32) { + // SAFETY: our own test's children. + unsafe { libc::kill(pid.pid, libc::SIGKILL) }; + } + let _ = agent.kill(); + let _ = agent.wait(); + } + + #[test] + fn a_child_that_is_no_configured_mcp_server_is_reported() { + let agent = agent(); + let mcp: Vec = vec![("sleep".into(), vec!["3602".into()])]; + let found = unexpected_descendants(agent.id(), &mcp); + let unconfigured = proc_table::read() + .into_iter() + .find(|entry| { + entry.ppid == agent.id() as i32 + && proc_table::argv(entry.pid) + .is_some_and(|argv| argv.ends_with(&["3601".into()])) + }) + .unwrap() + .pid; + // Its name only: never its arguments. + assert_eq!(found, vec![format!("{unconfigured} sleep")]); + // Both configured: nothing unexpected. + let both: Vec = vec![ + ("/bin/sleep".into(), vec!["3601".into()]), + ("sleep".into(), vec!["3602".into()]), + ]; + assert_eq!( + unexpected_descendants(agent.id(), &both), + Vec::::new() + ); + end(agent); + } + + #[test] + fn a_server_run_through_an_interpreter_still_counts_as_configured() { + let argv = |args: &[&str]| args.iter().map(|a| a.to_string()).collect::>(); + let npx = argv(&["node", "/opt/homebrew/bin/npx", "-y", "server"]); + assert!(started_as(&npx, "npx", &argv(&["-y", "server"]))); + assert!(!started_as(&npx, "npx", &argv(&["-y", "other"]))); + assert!(started_as( + &argv(&["/x/zeron-engine", "mcp-proxy"]), + "/x/zeron-engine", + &argv(&["mcp-proxy"]) + )); + assert!(!started_as( + &argv(&["/bin/zsh", "-c", "npm run dev"]), + "npx", + &[] + )); + } + + /// An agent leading its own group (as Zeron starts them) whose shell ran + /// `sleep 3608 &` and exited: the orphan is re-parented, but still in + /// the group a stop signals, so it counts. Configured as an MCP + /// server, it doesn't. + #[test] + fn an_orphan_left_in_the_agents_group_is_reported() { + use std::os::unix::process::CommandExt as _; + let mut agent = std::process::Command::new("/bin/sh") + .args(["-c", "/bin/sh -c 'sleep 3608 >/dev/null 2>&1 &'; read _"]) + .stdin(std::process::Stdio::piped()) + .process_group(0) + .spawn() + .unwrap(); + let group = agent.id() as i32; + /// Ends the agent's whole group, the orphan with it. + struct Reap(i32); + impl Drop for Reap { + fn drop(&mut self) { + // SAFETY: SIGKILL to the test's own process group. + unsafe { libc::kill(-self.0, libc::SIGKILL) }; + } + } + let _reap = Reap(group); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + let orphan = loop { + let table = proc_table::read(); + let orphan = table.iter().find(|entry| { + entry.pgid == group + && entry.ppid != group + && entry.pid != group + && !proc_table::zombie(entry.pid) + && proc_table::argv(entry.pid) + .is_some_and(|argv| argv.last().is_some_and(|arg| arg == "3608")) + }); + // (Its shell has exited: the agent has no child left.) + if let Some(orphan) = orphan + && !table + .iter() + .any(|entry| entry.ppid == group && !proc_table::zombie(entry.pid)) + { + break orphan.pid; + } + assert!( + std::time::Instant::now() < deadline, + "the orphan never appeared" + ); + std::thread::sleep(std::time::Duration::from_millis(20)); + }; + let found = unexpected_descendants(group as u32, &[]); + assert_eq!(found.len(), 1, "{found:?}"); + assert!(found[0].starts_with(&format!("{orphan} ")), "{found:?}"); + let mcp: Vec = vec![("sleep".into(), vec!["3608".into()])]; + assert_eq!( + unexpected_descendants(group as u32, &mcp), + Vec::::new() + ); + let _ = agent.kill(); + let _ = agent.wait(); + } + + /// An agent sharing its parent's group (this test runner's) counts its + /// own children only, never the rest of that group. + #[test] + fn an_agent_without_its_own_group_counts_only_its_children() { + let agent = agent(); + assert_eq!(unexpected_descendants(agent.id(), &[]).len(), 2); + end(agent); + } +} diff --git a/crates/harness/tests/acp.rs b/crates/harness/tests/acp.rs index 19490086a..de3a47604 100644 --- a/crates/harness/tests/acp.rs +++ b/crates/harness/tests/acp.rs @@ -2653,6 +2653,7 @@ async fn mcp_injection_all_acp_harnesses_new_resume_and_fallback() { ("ZERON_IPC_PORT".into(), "27699".into()), ] .into(), + http: None, }); let (controls, _steer, _token) = controls(); let mut stream = harness.run(req, controls).await.unwrap(); diff --git a/crates/harness/tests/acp_tool_fixture.rs b/crates/harness/tests/acp_tool_fixture.rs index bf498ca1d..2404a2d48 100644 --- a/crates/harness/tests/acp_tool_fixture.rs +++ b/crates/harness/tests/acp_tool_fixture.rs @@ -72,6 +72,7 @@ fn server(name: &str, command: &Path, tool: &str) -> McpServer { ] .into_iter() .collect(), + http: None, } } diff --git a/crates/harness/tests/claude.rs b/crates/harness/tests/claude.rs index 4dd6d978b..21ea84021 100644 --- a/crates/harness/tests/claude.rs +++ b/crates/harness/tests/claude.rs @@ -243,6 +243,7 @@ async fn a_side_chats_only_run_launches_without_subagent_tools() { command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; let mut tools = Vec::new(); for side_chats_only in [false, true] { diff --git a/crates/harness/tests/codex.rs b/crates/harness/tests/codex.rs index d110934ee..6dbf3a6f0 100644 --- a/crates/harness/tests/codex.rs +++ b/crates/harness/tests/codex.rs @@ -2330,6 +2330,7 @@ async fn idle_voice_runtime_has_no_initial_turn_and_preserves_mcp() { command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }); let (mut controls, steer, token) = controls("Yes"); let (voice, bridge, _events) = zeron_harness::codex::realtime::channel(); @@ -2397,6 +2398,7 @@ async fn a_side_chats_only_thread_starts_without_agent_tools() { command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }; if side_chats_only { server.env.insert( @@ -2463,6 +2465,7 @@ async fn external_voice_keeps_canonical_events_without_any_local_helper() { command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), + http: None, }); let (mut controls, _steer, token) = controls("Yes"); let (voice, bridge, mut events) = channel(); diff --git a/crates/harness/tests/cursor.rs b/crates/harness/tests/cursor.rs index d56ac8e99..613afff36 100644 --- a/crates/harness/tests/cursor.rs +++ b/crates/harness/tests/cursor.rs @@ -702,6 +702,7 @@ async fn mcp_injection_reaches_shim_on_new_and_resumed_runs() { command: "/path with spaces/zeron".into(), args: vec!["mcp".into()], env: [("ZERON_CHAT_ID".into(), "origin-chat".into())].into(), + http: None, }); let (controls, _steer, _token) = controls(); let events = run_to_first_done(&harness(), req, controls).await; diff --git a/crates/mcp/Cargo.toml b/crates/mcp/Cargo.toml index c723676b5..4c4eaade0 100644 --- a/crates/mcp/Cargo.toml +++ b/crates/mcp/Cargo.toml @@ -19,6 +19,10 @@ uuid.workspace = true chrono.workspace = true async-trait.workspace = true futures.workspace = true +bytes = "1" +hyper = { version = "1", features = ["server", "http1"] } +hyper-util = { version = "0.1", features = ["tokio"] } +http-body-util = "0.1" [dev-dependencies] zeron-theme.workspace = true diff --git a/crates/mcp/src/http.rs b/crates/mcp/src/http.rs new file mode 100644 index 000000000..a5762842b --- /dev/null +++ b/crates/mcp/src/http.rs @@ -0,0 +1,858 @@ +//! Zeron's MCP tools served by the engine itself, over loopback Streamable +//! HTTP — so an agent's run needs no `zeron mcp` process of its own. +//! +//! **Stateless.** The server keeps nothing per client connection: every +//! request is authenticated by its run's bearer token and stands on its own, +//! so an agent stopped and resumed (`--resume`) re-establishes nothing. It +//! speaks both eras of the protocol on one endpoint (`/mcp`): +//! +//! - **Modern** (`2026-07-28`): a request whose `params._meta` carries +//! `io.modelcontextprotocol/protocolVersion` is served per that revision — +//! no `initialize`, `server/discover`, the `MCP-Protocol-Version` / +//! `Mcp-Method` / `Mcp-Name` headers checked against the body (`400` + +//! `HeaderMismatch` otherwise), `resultType` on every result, list changes +//! on a `subscriptions/listen` stream. +//! - **Legacy** (`2024-11-05` … `2025-11-25`): `initialize` is answered +//! without an `Mcp-Session-Id` (the 2025 transport makes sessions +//! optional), and a session header a client sends anyway is ignored. +//! +//! Either way a call answers within its own POST: a long one (a +//! `wait_for_turn`, a send that waits) streams SSE with progress +//! notifications and keep-alive comments, then its response. Closing that +//! stream cancels the call; nothing is resumable. GET and DELETE answer +//! `405`, as the 2026 transport says (a 2025 client then has no standalone +//! stream, which that revision allows). +//! +//! **Security.** Bound to `127.0.0.1` only, on a port of its own (never the +//! IPC port). Every request must present `Authorization: Bearer `, +//! a token the engine minted for ONE run ([`McpHttpServer::grant`]): it +//! names that run's chat, so whatever a request does it does as that chat, +//! and it stops working when the run ends ([`Grant`] drops). A request +//! carrying `Origin` (any browser page) is refused, and so is one whose +//! `Host` isn't the loopback address (DNS rebinding). The token reaches what +//! the old stdio server's agent token reached — the engine's +//! agent-scoped methods ([`zeron_rpc::agent_allowed`]) — and no more; the +//! methods that act on "the calling chat" are pinned to the token's chat. + +use std::collections::HashMap; +use std::convert::Infallible; +use std::net::SocketAddr; +use std::sync::{Arc, Mutex, Weak}; +use std::time::Duration; + +use bytes::Bytes; +use http_body_util::{BodyExt, Full, StreamBody, combinators::UnsyncBoxBody}; +use hyper::body::Frame; +use hyper::header::{HeaderMap, HeaderValue}; +use hyper::{Method, Request, Response, StatusCode}; +use serde_json::{Value, json}; +use tokio::sync::mpsc; + +use crate::jsonrpc::{cap_reply, error_response, handle_request}; +use crate::tools::Tools; +use crate::zeron::Origin; + +/// The modern revisions served statelessly. +pub const MODERN_VERSIONS: [&str; 1] = ["2026-07-28"]; +/// The `initialize`-era revisions answered on this endpoint (the stdio +/// server's, plus the last 2025 revision a client may ask for). +pub const LEGACY_VERSIONS: [&str; 4] = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"]; + +const META_VERSION: &str = "io.modelcontextprotocol/protocolVersion"; +const META_SERVER_INFO: &str = "io.modelcontextprotocol/serverInfo"; +const META_SUBSCRIPTION: &str = "io.modelcontextprotocol/subscriptionId"; + +/// JSON-RPC codes the 2026 revision reserves. +const HEADER_MISMATCH: i64 = -32020; +const UNSUPPORTED_VERSION: i64 = -32022; +const METHOD_NOT_FOUND: i64 = -32601; +const INVALID_REQUEST: i64 = -32600; +const PARSE_ERROR: i64 = -32700; + +/// The largest request body read. Tool arguments are small (images and +/// visual assets travel as paths). +const MAX_BODY: usize = 8 * 1024 * 1024; +/// A call still running sends a progress notification this often (when the +/// client gave a progress token), as the stdio server does. +const PROGRESS_EVERY: Duration = Duration::from_secs(30); +/// An SSE stream with nothing to say sends a comment this often, so no idle +/// timeout on the client's side closes it (clients ignore comments). +const KEEP_ALIVE_EVERY: Duration = Duration::from_secs(15); +/// How long a client may cache `tools/list` (modern `ttlMs`): plugin tools +/// come and go, and a client not listening for changes re-lists after this. +const TOOLS_TTL_MS: u64 = 30_000; +/// The most connections served at once; more wait to be accepted. Each warm +/// agent may hold one open (its `subscriptions/listen` stream), and its +/// long calls one each. +const MAX_CONNECTIONS: usize = 128; +/// How long a client has to send a request's headers once it starts one: +/// a connection trickling them (or sending none) is closed. +const HEADER_READ_TIMEOUT: Duration = Duration::from_secs(10); + +type Body = UnsyncBoxBody; + +/// Builds the tools one grant's requests run: Zeron's tools speaking for +/// `origin`, over a connection to the engine scoped to `origin`'s chat. +pub type ToolsFactory = Arc Arc + Send + Sync>; + +/// One run's access: who it speaks for, its tools (made at its first +/// call), its calls in flight (so a 2025 client's +/// `notifications/cancelled` can stop one) and its `subscriptions/listen` +/// streams. Not connection state: the same for every request the run's +/// token makes. Its calls and streams end when the run's [`Grant`] drops. +struct GrantState { + token: String, + origin: Origin, + tools: std::sync::OnceLock>, + in_flight: Mutex>, + listens: Mutex>, + next_listen: std::sync::atomic::AtomicU64, +} + +struct Inner { + port: u16, + factory: ToolsFactory, + grants: Mutex>>, +} + +/// A run's access to the endpoint: its token works until this drops. +pub struct Grant { + inner: Weak, + state: Arc, +} + +impl Grant { + /// The bearer token (`Authorization: Bearer …`). + pub fn token(&self) -> &str { + &self.state.token + } +} + +impl std::fmt::Debug for Grant { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Grant").field("origin", &self.state.origin).finish() + } +} + +impl Drop for Grant { + fn drop(&mut self) { + let Some(inner) = self.inner.upgrade() else { + return; + }; + lock(&inner.grants).retain(|grant| !Arc::ptr_eq(grant, &self.state)); + // Calls still running for a finished run end with it, and so do + // its listen streams (each held its tools' engine connection). + for (_, call) in lock(&self.state.in_flight).drain() { + call.abort(); + } + for (_, stream) in lock(&self.state.listens).drain() { + stream.abort(); + } + } +} + +/// The engine's MCP endpoint. Dropping it stops the listener. +pub struct McpHttpServer { + inner: Arc, + accept: tokio::task::JoinHandle<()>, +} + +impl Drop for McpHttpServer { + fn drop(&mut self) { + self.accept.abort(); + } +} + +impl McpHttpServer { + /// Bind `127.0.0.1` on a free port and serve until dropped. + pub async fn bind(factory: ToolsFactory) -> std::io::Result { + Self::bind_with(factory, MAX_CONNECTIONS).await + } + + /// [`Self::bind`], serving at most `max_connections` at once. + async fn bind_with(factory: ToolsFactory, max_connections: usize) -> std::io::Result { + let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await?; + let port = listener.local_addr()?.port(); + let inner = Arc::new(Inner { + port, + factory, + grants: Mutex::new(Vec::new()), + }); + let weak = Arc::downgrade(&inner); + let slots = Arc::new(tokio::sync::Semaphore::new(max_connections)); + let accept = tokio::spawn(async move { + loop { + // A free slot before the next connection is taken. + let Ok(slot) = slots.clone().acquire_owned().await else { + return; + }; + let (stream, peer) = match listener.accept().await { + Ok(accepted) => accepted, + Err(err) => { + tracing::warn!(error = %err, "mcp http: accept failed"); + tokio::time::sleep(Duration::from_millis(100)).await; + continue; + } + }; + let Some(inner) = weak.upgrade() else { return }; + tokio::spawn(async move { + serve_socket(inner, stream, peer).await; + drop(slot); + }); + } + }); + tracing::info!(port, "agent MCP endpoint listening"); + Ok(Self { inner, accept }) + } + + pub fn port(&self) -> u16 { + self.inner.port + } + + /// `http://127.0.0.1:{port}/mcp`. + pub fn url(&self) -> String { + format!("http://127.0.0.1:{}/mcp", self.inner.port) + } + + /// A fresh token for one run speaking as `origin`. + pub fn grant(&self, origin: Origin) -> std::io::Result { + let token = zeron_rpc::ipc_auth::generate_token()?; + let state = Arc::new(GrantState { + token, + origin, + tools: std::sync::OnceLock::new(), + in_flight: Mutex::new(HashMap::new()), + listens: Mutex::new(HashMap::new()), + next_listen: std::sync::atomic::AtomicU64::new(0), + }); + lock(&self.inner.grants).push(state.clone()); + Ok(Grant { + inner: Arc::downgrade(&self.inner), + state, + }) + } + + /// Tool calls running now, across runs (tests, diagnostics). + pub fn calls_in_flight(&self) -> usize { + lock(&self.inner.grants) + .iter() + .map(|grant| lock(&grant.in_flight).len()) + .sum() + } + + /// How many runs hold a live token (tests, diagnostics). + pub fn live_grants(&self) -> usize { + lock(&self.inner.grants).len() + } + + /// `subscriptions/listen` streams open now, across runs (tests, + /// diagnostics). + pub fn listens_open(&self) -> usize { + lock(&self.inner.grants) + .iter() + .map(|grant| lock(&grant.listens).len()) + .sum() + } +} + +fn lock(mutex: &Mutex) -> std::sync::MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +async fn serve_socket(inner: Arc, stream: tokio::net::TcpStream, peer: SocketAddr) { + if !peer.ip().is_loopback() { + return; + } + let _ = stream.set_nodelay(true); + let service = hyper::service::service_fn(move |request| { + let inner = inner.clone(); + async move { Ok::<_, Infallible>(handle(inner, request).await) } + }); + let io = hyper_util::rt::TokioIo::new(stream); + // (The timer is what makes the header timeout apply.) + if let Err(err) = hyper::server::conn::http1::Builder::new() + .timer(hyper_util::rt::TokioTimer::new()) + .header_read_timeout(HEADER_READ_TIMEOUT) + .keep_alive(true) + .serve_connection(io, service) + .await + { + tracing::debug!(error = %err, "mcp http: connection ended"); + } +} + +fn full(bytes: impl Into) -> Body { + Full::new(bytes.into()).boxed_unsync() +} + +fn empty(status: StatusCode) -> Response { + let mut response = Response::new(full(Bytes::new())); + *response.status_mut() = status; + response +} + +fn json_response(status: StatusCode, value: &Value) -> Response { + let mut response = Response::new(full(value.to_string())); + *response.status_mut() = status; + response + .headers_mut() + .insert("content-type", HeaderValue::from_static("application/json")); + response +} + +/// A JSON-RPC error with no request id, for refusals before the body is read. +fn refusal(status: StatusCode, message: &str) -> Response { + json_response(status, &error_response(Value::Null, INVALID_REQUEST, message)) +} + +fn header<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> { + headers.get(name).and_then(|value| value.to_str().ok()) +} + +/// The `Host` a loopback client sends: `127.0.0.1`, `localhost` or `[::1]`, +/// on this port. Anything else came through a name that resolved here +/// (DNS rebinding). +fn host_allowed(headers: &HeaderMap, port: u16) -> bool { + let Some(host) = header(headers, "host") else { + return false; + }; + let host = host.trim().to_ascii_lowercase(); + ["127.0.0.1", "localhost", "[::1]"] + .iter() + .any(|name| host == format!("{name}:{port}")) +} + +/// Constant-time equality, so a wrong token's timing says nothing about a +/// right one. +fn same_token(a: &str, b: &str) -> bool { + let (a, b) = (a.as_bytes(), b.as_bytes()); + if a.len() != b.len() { + return false; + } + a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0 +} + +fn find_grant(inner: &Inner, headers: &HeaderMap) -> Option> { + let presented = header(headers, "authorization")?.strip_prefix("Bearer ")?.trim(); + if presented.is_empty() { + return None; + } + // Every grant compared, so the match's position leaks nothing either. + let grants = lock(&inner.grants); + let mut found = None; + for grant in grants.iter() { + if same_token(&grant.token, presented) && found.is_none() { + found = Some(grant.clone()); + } + } + found +} + +/// Decode a `=?base64?…?=` header value (2026 transport, Value Encoding). +fn decode_header_value(value: &str) -> Option { + use base64::Engine as _; + match value + .strip_prefix("=?base64?") + .and_then(|rest| rest.strip_suffix("?=")) + { + Some(encoded) => base64::engine::general_purpose::STANDARD + .decode(encoded) + .ok() + .and_then(|bytes| String::from_utf8(bytes).ok()), + None => Some(value.to_owned()), + } +} + +/// Which era a message speaks, read from its own `_meta`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Era<'a> { + Modern(&'a str), + Legacy, +} + +fn era(message: &Value) -> Era<'_> { + match message + .get("params") + .and_then(|params| params.get("_meta")) + .and_then(|meta| meta.get(META_VERSION)) + .and_then(Value::as_str) + { + Some(version) => Era::Modern(version), + None => Era::Legacy, + } +} + +fn mismatch(id: Value, message: String) -> Response { + json_response( + StatusCode::BAD_REQUEST, + &error_response(id, HEADER_MISMATCH, &message), + ) +} + +/// The 2026 header checks: `MCP-Protocol-Version` and `Mcp-Method` on every +/// request, `Mcp-Name` where the method names a tool, each matching the body. +fn check_modern_headers( + headers: &HeaderMap, + id: &Value, + version: &str, + method: &str, + params: &Value, +) -> Result<(), Response> { + match header(headers, "mcp-protocol-version") { + Some(sent) if sent == version => {} + Some(sent) => { + return Err(mismatch( + id.clone(), + format!("Header mismatch: MCP-Protocol-Version '{sent}' does not match body '{version}'"), + )); + } + None => { + return Err(mismatch(id.clone(), "Missing MCP-Protocol-Version header".into())); + } + } + match header(headers, "mcp-method") { + Some(sent) if sent == method => {} + Some(sent) => { + return Err(mismatch( + id.clone(), + format!("Header mismatch: Mcp-Method '{sent}' does not match body '{method}'"), + )); + } + None => return Err(mismatch(id.clone(), "Missing Mcp-Method header".into())), + } + let named = match method { + "tools/call" | "prompts/get" => params.get("name").and_then(Value::as_str), + "resources/read" => params.get("uri").and_then(Value::as_str), + _ => return Ok(()), + }; + let Some(named) = named else { + return Ok(()); + }; + match header(headers, "mcp-name").map(decode_header_value) { + Some(Some(sent)) if sent == named => Ok(()), + Some(Some(sent)) => Err(mismatch( + id.clone(), + format!("Header mismatch: Mcp-Name header value '{sent}' does not match body value '{named}'"), + )), + Some(None) => Err(mismatch(id.clone(), "Malformed Mcp-Name header".into())), + None => Err(mismatch(id.clone(), "Missing Mcp-Name header".into())), + } +} + +fn server_info() -> Value { + json!({ "name": "zeron", "version": env!("CARGO_PKG_VERSION") }) +} + +/// A modern result: `resultType`, and the server named in `_meta`. +fn modernize(mut response: Value, method: &str) -> Value { + if let Some(result) = response.get_mut("result").and_then(Value::as_object_mut) { + result.insert("resultType".into(), json!("complete")); + let meta = result + .entry("_meta") + .or_insert_with(|| json!({})); + if let Some(meta) = meta.as_object_mut() { + meta.insert(META_SERVER_INFO.into(), server_info()); + } + if method == "tools/list" { + result.insert("ttlMs".into(), json!(TOOLS_TTL_MS)); + result.insert("cacheScope".into(), json!("private")); + } + } + response +} + +fn tools_for(inner: &Inner, grant: &GrantState) -> Arc { + grant + .tools + .get_or_init(|| (inner.factory)(&grant.origin)) + .clone() +} + +async fn handle(inner: Arc, request: Request) -> Response { + let headers = request.headers().clone(); + // Any browser page carries `Origin`; nothing that should reach this does. + if headers.contains_key("origin") { + return refusal(StatusCode::FORBIDDEN, "origin not allowed"); + } + if !host_allowed(&headers, inner.port) { + return refusal(StatusCode::FORBIDDEN, "host not allowed"); + } + if request.uri().path() != "/mcp" { + return empty(StatusCode::NOT_FOUND); + } + if request.method() != Method::POST { + // No standalone stream (2026: removed) and no sessions to delete. + let mut response = empty(StatusCode::METHOD_NOT_ALLOWED); + response + .headers_mut() + .insert("allow", HeaderValue::from_static("POST")); + return response; + } + let Some(grant) = find_grant(&inner, &headers) else { + let mut response = refusal(StatusCode::UNAUTHORIZED, "missing or wrong token"); + response + .headers_mut() + .insert("www-authenticate", HeaderValue::from_static("Bearer")); + return response; + }; + let body = match http_body_util::Limited::new(request.into_body(), MAX_BODY) + .collect() + .await + { + Ok(collected) => collected.to_bytes(), + Err(_) => return refusal(StatusCode::PAYLOAD_TOO_LARGE, "request too large"), + }; + let message: Value = match serde_json::from_slice(&body) { + Ok(message) => message, + Err(err) => { + return json_response( + StatusCode::BAD_REQUEST, + &error_response(Value::Null, PARSE_ERROR, &format!("parse error: {err}")), + ); + } + }; + if !message.is_object() { + return refusal(StatusCode::BAD_REQUEST, "expected one JSON-RPC object"); + } + let id = message.get("id").cloned(); + let Some(method) = message.get("method").and_then(Value::as_str).map(str::to_owned) else { + // A JSON-RPC response: the server sends no requests to answer. + return refusal(StatusCode::BAD_REQUEST, "expected a request or notification"); + }; + let params = message.get("params").cloned().unwrap_or(Value::Null); + let era = era(&message); + // What each agent speaks, for the record (which revision it negotiates). + match (era, method.as_str()) { + (Era::Legacy, "initialize") => tracing::info!( + chat = ?grant.origin.chat_id, + version = %params["protocolVersion"], + client = %params["clientInfo"], + header = header(&headers, "mcp-protocol-version").unwrap_or("-"), + "agent MCP: legacy initialize" + ), + (Era::Modern(version), "server/discover") => tracing::info!( + chat = ?grant.origin.chat_id, + version, + client = %params["_meta"]["io.modelcontextprotocol/clientInfo"], + "agent MCP: modern discover" + ), + _ => tracing::debug!(chat = ?grant.origin.chat_id, method, ?era, "agent MCP request"), + } + + let Some(id) = id else { + // Notifications. The 2026 transport has none (closing a stream is + // the cancel); a 2025 client cancels by id. + if method == "notifications/cancelled" + && let Some(key) = params.get("requestId").map(Value::to_string) + && let Some(call) = lock(&grant.in_flight).remove(&key) + { + call.abort(); + } + return empty(StatusCode::ACCEPTED); + }; + + match era { + Era::Modern(version) => { + if !MODERN_VERSIONS.contains(&version) { + let supported: Vec<&str> = + MODERN_VERSIONS.iter().chain(LEGACY_VERSIONS.iter()).copied().collect(); + return json_response( + StatusCode::BAD_REQUEST, + &json!({ + "jsonrpc": "2.0", + "id": id, + "error": { + "code": UNSUPPORTED_VERSION, + "message": "Unsupported protocol version", + "data": { "supported": supported, "requested": version }, + }, + }), + ); + } + if let Err(refused) = check_modern_headers(&headers, &id, version, &method, ¶ms) { + return refused; + } + match method.as_str() { + "server/discover" => { + let tools = tools_for(&inner, &grant); + json_response( + StatusCode::OK, + &json!({ + "jsonrpc": "2.0", + "id": id, + "result": { + "resultType": "complete", + "supportedVersions": MODERN_VERSIONS, + "capabilities": { "tools": { "listChanged": true } }, + "_meta": { META_SERVER_INFO: server_info() }, + "instructions": crate::jsonrpc::instructions( + tools.waits_on_background(), + tools.side_chats_only(), + ), + "ttlMs": TOOLS_TTL_MS, + "cacheScope": "private", + }, + }), + ) + } + "subscriptions/listen" => listen(&inner, &grant, id, ¶ms), + // Gone in this revision (handshake, liveness by request). + "initialize" | "ping" | "logging/setLevel" => json_response( + StatusCode::NOT_FOUND, + &error_response(id, METHOD_NOT_FOUND, &format!("unknown method: {method}")), + ), + "tools/call" => call(&inner, &grant, &headers, id, method, params, true), + _ => { + let tools = tools_for(&inner, &grant); + let response = handle_request(&tools, id, &method, params).await; + let status = if response["error"]["code"] == METHOD_NOT_FOUND { + StatusCode::NOT_FOUND + } else { + StatusCode::OK + }; + json_response(status, &modernize(response, &method)) + } + } + } + Era::Legacy => { + if method != "initialize" + && let Some(sent) = header(&headers, "mcp-protocol-version") + && !LEGACY_VERSIONS.contains(&sent) + { + return refusal( + StatusCode::BAD_REQUEST, + &format!("unsupported MCP-Protocol-Version: {sent}"), + ); + } + match method.as_str() { + "tools/call" => call(&inner, &grant, &headers, id, method, params, false), + // `initialize` included: answered with no session id, so the + // client sends none (and one sent anyway is ignored). + _ => { + let tools = tools_for(&inner, &grant); + json_response(StatusCode::OK, &handle_request(&tools, id, &method, params).await) + } + } + } + } +} + +/// Ends the listen stream it guards when the response stream is dropped +/// (the client closed it, or the connection went), and takes it off its +/// grant's streams. +struct ListenGuard { + stream: tokio::task::AbortHandle, + grant: Weak, + key: u64, +} + +impl Drop for ListenGuard { + fn drop(&mut self) { + self.stream.abort(); + if let Some(grant) = self.grant.upgrade() { + lock(&grant.listens).remove(&self.key); + } + } +} + +/// Aborts the call it guards when the response stream is dropped (the +/// client closed it: the 2026 cancel; or the connection went), and takes it +/// off its grant's calls in flight. +struct CallGuard { + call: tokio::task::AbortHandle, + grant: Weak, + key: String, +} + +impl Drop for CallGuard { + fn drop(&mut self) { + self.call.abort(); + if let Some(grant) = self.grant.upgrade() { + lock(&grant.in_flight).remove(&self.key); + } + } +} + +fn sse_headers(response: &mut Response) { + let headers = response.headers_mut(); + headers.insert("content-type", HeaderValue::from_static("text/event-stream")); + headers.insert("cache-control", HeaderValue::from_static("no-cache")); + headers.insert("x-accel-buffering", HeaderValue::from_static("no")); +} + +fn sse_event(message: &Value) -> Bytes { + Bytes::from(format!("event: message\ndata: {message}\n\n")) +} + +/// A body fed by `rx`; `guard` lives (and its call runs) as long as the +/// body does. +fn sse_body(rx: mpsc::Receiver, guard: G) -> Body { + let stream = futures::stream::unfold((rx, guard), |(mut rx, guard)| async move { + rx.recv() + .await + .map(|chunk| (Ok::<_, Infallible>(Frame::data(chunk)), (rx, guard))) + }); + BodyExt::boxed_unsync(StreamBody::new(stream)) +} + +/// `tools/call`: run on a task of its own, answered on an SSE stream (with +/// progress and keep-alives while it runs) when the client takes one, else +/// as one JSON object once it ends. +fn call( + inner: &Arc, + grant: &Arc, + headers: &HeaderMap, + id: Value, + method: String, + params: Value, + modern: bool, +) -> Response { + let tools = tools_for(inner, grant); + let progress_token = params + .get("_meta") + .and_then(|meta| meta.get("progressToken")) + .cloned(); + let streams = header(headers, "accept").is_some_and(|accept| accept.contains("text/event-stream")); + let key = id.to_string(); + let (out_tx, out_rx) = mpsc::channel::(16); + let registry = Arc::downgrade(grant); + let done_key = key.clone(); + let mut in_flight = lock(&grant.in_flight); + let task = tokio::spawn(async move { + let work = handle_request(&tools, id, &method, params); + tokio::pin!(work); + let mut progress = tokio::time::interval_at( + tokio::time::Instant::now() + PROGRESS_EVERY, + PROGRESS_EVERY, + ); + let mut keep_alive = tokio::time::interval_at( + tokio::time::Instant::now() + KEEP_ALIVE_EVERY, + KEEP_ALIVE_EVERY, + ); + let mut beat = 0u64; + let response = loop { + tokio::select! { + response = &mut work => break response, + _ = progress.tick(), if streams && progress_token.is_some() => { + beat += 1; + let note = json!({ + "jsonrpc": "2.0", + "method": "notifications/progress", + "params": { "progressToken": progress_token, "progress": beat, "message": "still working" }, + }); + let _ = out_tx.send(sse_event(¬e)).await; + } + _ = keep_alive.tick(), if streams => { + let _ = out_tx.send(Bytes::from_static(b": keep-alive\n\n")).await; + } + } + }; + let response = cap_reply(response); + let response = if modern { modernize(response, "tools/call") } else { response }; + let bytes = if streams { + sse_event(&response) + } else { + Bytes::from(response.to_string()) + }; + let _ = out_tx.send(bytes).await; + if let Some(grant) = registry.upgrade() { + lock(&grant.in_flight).remove(&done_key); + } + }); + in_flight.insert(key.clone(), task.abort_handle()); + drop(in_flight); + let guard = CallGuard { + call: task.abort_handle(), + grant: Arc::downgrade(grant), + key, + }; + if streams { + let mut response = Response::new(sse_body(out_rx, guard)); + sse_headers(&mut response); + response + } else { + // One JSON object: the body is the single message the task sends. + let mut response = Response::new(sse_body(out_rx, guard)); + response + .headers_mut() + .insert("content-type", HeaderValue::from_static("application/json")); + response + } +} + +/// `subscriptions/listen` (2026): acknowledge what's honoured — tool-list +/// changes only — and then send each one, tagged with the subscription, on +/// this stream until the client closes it. +fn listen(inner: &Arc, grant: &Arc, id: Value, params: &Value) -> Response { + let wants_tools = params + .get("notifications") + .and_then(|filter| filter.get("toolsListChanged")) + .and_then(Value::as_bool) + .unwrap_or(false); + let tools = tools_for(inner, grant); + let (out_tx, out_rx) = mpsc::channel::(16); + // Registered before it runs, under the lock a dropping grant drains. + let mut listens = lock(&grant.listens); + let task = tokio::spawn(async move { + let honoured = if wants_tools { + json!({ "toolsListChanged": true }) + } else { + json!({}) + }; + let ack = json!({ + "jsonrpc": "2.0", + "method": "notifications/subscriptions/acknowledged", + "params": { "_meta": { META_SUBSCRIPTION: id }, "notifications": honoured }, + }); + if out_tx.send(sse_event(&ack)).await.is_err() { + return; + } + let (changed_tx, mut changed) = mpsc::channel::<()>(1); + let watch = async { + if wants_tools { + tools.watch_plugin_tools(changed_tx).await; + } + std::future::pending::<()>().await; + }; + let forward = async { + let mut keep_alive = tokio::time::interval_at( + tokio::time::Instant::now() + KEEP_ALIVE_EVERY, + KEEP_ALIVE_EVERY, + ); + loop { + let chunk = tokio::select! { + Some(()) = changed.recv() => sse_event(&json!({ + "jsonrpc": "2.0", + "method": "notifications/tools/list_changed", + "params": { "_meta": { META_SUBSCRIPTION: id } }, + })), + _ = keep_alive.tick() => Bytes::from_static(b": keep-alive\n\n"), + }; + if out_tx.send(chunk).await.is_err() { + return; + } + } + }; + tokio::select! { + () = watch => {} + () = forward => {} + } + }); + let key = grant + .next_listen + .fetch_add(1, std::sync::atomic::Ordering::Relaxed); + listens.insert(key, task.abort_handle()); + drop(listens); + let guard = ListenGuard { + stream: task.abort_handle(), + grant: Arc::downgrade(grant), + key, + }; + let mut response = Response::new(sse_body(out_rx, guard)); + sse_headers(&mut response); + response +} + +#[cfg(test)] +#[path = "http_tests.rs"] +mod tests; diff --git a/crates/mcp/src/http_tests.rs b/crates/mcp/src/http_tests.rs new file mode 100644 index 000000000..8819f5d65 --- /dev/null +++ b/crates/mcp/src/http_tests.rs @@ -0,0 +1,424 @@ +use super::*; + +use crate::zeron::Zeron; +use async_trait::async_trait; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use zeron_rpc::{RpcError, RpcReply, RpcService, memory_client, methods}; + +/// What the fake engine saw: `AwaitBackground`'s chat per call, and whether +/// a blocked call was dropped (cancelled). +#[derive(Default)] +struct Engine { + awaited: Mutex>, + dropped: Mutex>>, +} + +struct DropSignal(Option>); +impl Drop for DropSignal { + fn drop(&mut self) { + if let Some(tx) = self.0.take() { + let _ = tx.send(()); + } + } +} + +#[async_trait] +impl RpcService for Engine { + async fn handle(&self, method: &str, params: Value) -> Result { + match method { + methods::AWAIT_BACKGROUND => { + let chat = params["chatId"].as_str().unwrap_or_default().to_owned(); + lock(&self.awaited).push(chat); + if params["taskIds"][0] == "block" { + let _signal = DropSignal(lock(&self.dropped).take()); + std::future::pending::<()>().await; + } + Ok(RpcReply::Value(json!({ "matched": params["taskIds"] }))) + } + methods::LIST_PLUGIN_TOOLS => Ok(RpcReply::Value(json!({ "device": { "id": "d" }, "tools": [] }))), + other => Err(RpcError::UnknownMethod(other.into())), + } + } +} + +fn origin(chat: &str) -> Origin { + Origin { + chat_id: Some(chat.into()), + device_id: Some("d".into()), + side_chats_only: false, + waits_on_background: true, + } +} + +async fn server(engine: Arc) -> McpHttpServer { + McpHttpServer::bind(factory(engine)).await.unwrap() +} + +fn factory(engine: Arc) -> ToolsFactory { + Arc::new(move |origin: &Origin| { + let service = zeron_rpc::agent_service( + engine.clone(), + origin.chat_id.clone().unwrap_or_default(), + ); + Arc::new(Tools::new(Arc::new(Zeron::with_client( + memory_client(service), + origin.clone(), + )))) + }) +} + +/// One raw HTTP/1.1 exchange: (status, head, body as sent — chunked or not). +async fn exchange(port: u16, method: &str, headers: &[(&str, String)], body: &str) -> (u16, String, String) { + let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port)).await.unwrap(); + let mut request = format!("{method} /mcp HTTP/1.1\r\nConnection: close\r\nContent-Length: {}\r\n", body.len()); + if !headers.iter().any(|(name, _)| name.eq_ignore_ascii_case("host")) { + request.push_str(&format!("Host: 127.0.0.1:{port}\r\n")); + } + for (name, value) in headers { + request.push_str(&format!("{name}: {value}\r\n")); + } + request.push_str("\r\n"); + request.push_str(body); + stream.write_all(request.as_bytes()).await.unwrap(); + let mut raw = String::new(); + tokio::time::timeout(Duration::from_secs(10), stream.read_to_string(&mut raw)) + .await + .expect("answered in time") + .unwrap(); + let (head, body) = raw.split_once("\r\n\r\n").unwrap_or((&raw, "")); + let status = head.split_whitespace().nth(1).unwrap().parse().unwrap(); + (status, head.to_ascii_lowercase(), body.to_owned()) +} + +fn bearer(token: &str) -> (&'static str, String) { + ("Authorization", format!("Bearer {token}")) +} + +fn accept() -> (&'static str, String) { + ("Accept", "application/json, text/event-stream".into()) +} + +fn legacy_call(id: u64, tool: &str, args: Value) -> String { + json!({ "jsonrpc": "2.0", "id": id, "method": "tools/call", "params": { "name": tool, "arguments": args } }) + .to_string() +} + +fn modern(id: u64, method: &str, mut params: Value) -> String { + params["_meta"] = json!({ + META_VERSION: "2026-07-28", + "io.modelcontextprotocol/clientCapabilities": {}, + }); + json!({ "jsonrpc": "2.0", "id": id, "method": method, "params": params }).to_string() +} + +fn modern_headers(method: &str, name: Option<&str>) -> Vec<(&'static str, String)> { + let mut headers = vec![ + ("MCP-Protocol-Version", "2026-07-28".to_owned()), + ("Mcp-Method", method.to_owned()), + ]; + if let Some(name) = name { + headers.push(("Mcp-Name", name.to_owned())); + } + headers +} + +#[tokio::test] +async fn a_request_without_the_run_s_token_is_refused() { + let server = server(Arc::default()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + let port = server.port(); + let init = json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {} }).to_string(); + + let (status, head, _) = exchange(port, "POST", &[accept()], &init).await; + assert_eq!(status, 401, "no token"); + assert!(head.contains("www-authenticate: bearer")); + let (status, ..) = exchange(port, "POST", &[accept(), bearer("not-the-token")], &init).await; + assert_eq!(status, 401, "wrong token"); + let (status, ..) = exchange(port, "POST", &[accept(), ("Authorization", grant.token().into())], &init).await; + assert_eq!(status, 401, "token without the Bearer scheme"); + + // A browser page (Origin) and a rebound name (Host) never get as far as + // the token check. + let with_origin = [accept(), bearer(grant.token()), ("Origin", "https://evil.example".into())]; + assert_eq!(exchange(port, "POST", &with_origin, &init).await.0, 403); + let rebound = [accept(), bearer(grant.token()), ("Host", format!("evil.example:{port}"))]; + assert_eq!(exchange(port, "POST", &rebound, &init).await.0, 403); + + let (status, head, body) = exchange(port, "POST", &[accept(), bearer(grant.token())], &init).await; + assert_eq!(status, 200); + assert!(body.contains("\"protocolVersion\""), "{body}"); + assert!(!head.contains("mcp-session-id"), "stateless: no session is minted"); + + // Ended runs' tokens stop working at once. + let token = grant.token().to_owned(); + drop(grant); + assert_eq!(server.live_grants(), 0); + assert_eq!(exchange(port, "POST", &[accept(), bearer(&token)], &init).await.0, 401); +} + +#[tokio::test] +async fn get_and_delete_are_not_served() { + let server = server(Arc::default()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + for method in ["GET", "DELETE"] { + let (status, head, _) = exchange(server.port(), method, &[bearer(grant.token())], "").await; + assert_eq!(status, 405, "{method}"); + assert!(head.contains("allow: post")); + } +} + +#[tokio::test] +async fn one_run_s_token_acts_only_as_its_own_chat() { + let engine = Arc::::default(); + let server = server(engine.clone()).await; + let a = server.grant(origin("chat-a")).unwrap(); + let b = server.grant(origin("chat-b")).unwrap(); + let call = legacy_call(1, "wait_on_background", json!({ "task_ids": ["t1"] })); + let (status, _, body) = exchange(server.port(), "POST", &[accept(), bearer(a.token())], &call).await; + assert_eq!(status, 200); + assert!(body.contains("waiting on that work"), "{body}"); + let (status, ..) = exchange(server.port(), "POST", &[accept(), bearer(b.token())], &call).await; + assert_eq!(status, 200); + assert_eq!(*lock(&engine.awaited), ["chat-a", "chat-b"], "each token speaks for its own run's chat"); + + // And underneath the tools: the engine connection a token's calls use + // refuses what would act on another chat, whatever the params say. + let client = memory_client(zeron_rpc::agent_service(engine.clone(), "chat-a".into())); + let refused = client + .call(methods::AWAIT_BACKGROUND, json!({ "chatId": "chat-b", "taskIds": ["t"], "waiting": true })) + .await + .unwrap_err(); + assert!(refused.to_string().contains("calling chat only"), "{refused}"); + assert_eq!(lock(&engine.awaited).len(), 2, "never reached the engine"); + // Methods outside the agent scope stay shut, as on the agent IPC token. + let refused = client.call(methods::STOP_ENGINE, json!({})).await.unwrap_err(); + assert!(refused.to_string().contains("isn't open to agents"), "{refused}"); +} + +#[tokio::test] +async fn modern_requests_are_stateless_and_header_checked() { + let server = server(Arc::default()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + let port = server.port(); + let auth = || vec![accept(), bearer(grant.token())]; + + // No handshake: discovery and a call each stand alone. + let mut headers = auth(); + headers.extend(modern_headers("server/discover", None)); + let (status, _, body) = exchange(port, "POST", &headers, &modern(1, "server/discover", json!({}))).await; + assert_eq!(status, 200, "{body}"); + let found: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(found["result"]["supportedVersions"], json!(["2026-07-28"])); + assert_eq!(found["result"]["resultType"], "complete"); + assert!(found["result"]["instructions"].as_str().unwrap().contains("wait_on_background")); + + let mut headers = auth(); + headers.extend(modern_headers("tools/list", None)); + let (status, _, body) = exchange(port, "POST", &headers, &modern(2, "tools/list", json!({}))).await; + assert_eq!(status, 200); + let listed: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(listed["result"]["resultType"], "complete"); + assert_eq!(listed["result"]["cacheScope"], "private"); + assert!(listed["result"]["ttlMs"].is_u64()); + assert_eq!(listed["result"]["_meta"][META_SERVER_INFO]["name"], "zeron"); + + // Headers must match the body. + let call = modern(3, "tools/call", json!({ "name": "wait_on_background", "arguments": { "task_ids": ["t"] } })); + for (headers, why) in [ + (modern_headers("tools/call", None), "Mcp-Name missing"), + (modern_headers("tools/call", Some("whoami")), "Mcp-Name differs"), + (modern_headers("tools/list", Some("wait_on_background")), "Mcp-Method differs"), + (vec![("Mcp-Method", "tools/call".into()), ("Mcp-Name", "wait_on_background".into())], "no version header"), + ] { + let mut all = auth(); + all.extend(headers); + let (status, _, body) = exchange(port, "POST", &all, &call).await; + assert_eq!(status, 400, "{why}"); + assert!(body.contains("-32020"), "{why}: {body}"); + } + // Base64-sentinel names decode before comparing. + let mut all = auth(); + all.extend(modern_headers("tools/call", Some("=?base64?d2FpdF9vbl9iYWNrZ3JvdW5k?="))); + let (status, _, body) = exchange(port, "POST", &all, &call).await; + assert_eq!(status, 200, "{body}"); + assert!(body.contains("\"resultType\":\"complete\""), "{body}"); + + // An unknown revision lists the ones served. + let mut old = modern(4, "tools/list", json!({})); + old = old.replace("2026-07-28", "2099-01-01"); + let mut headers = auth(); + headers.extend([("MCP-Protocol-Version", "2099-01-01".into()), ("Mcp-Method", "tools/list".into())]); + let (status, _, body) = exchange(port, "POST", &headers, &old).await; + assert_eq!(status, 400); + let refused: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(refused["error"]["code"], UNSUPPORTED_VERSION); + assert!(refused["error"]["data"]["supported"].as_array().unwrap().contains(&json!("2026-07-28"))); +} + +/// Until no call is in flight (the cancelled one was stopped). +async fn gone(server: &McpHttpServer) { + for _ in 0..500 { + if server.calls_in_flight() == 0 { + return; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + panic!("the call is still in flight"); +} + +#[tokio::test] +async fn a_closed_stream_cancels_its_call() { + let engine = Arc::::default(); + let server = server(engine.clone()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + let call = legacy_call(7, "wait_on_background", json!({ "task_ids": ["block"] })); + let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", server.port())).await.unwrap(); + let request = format!( + "POST /mcp HTTP/1.1\r\nHost: 127.0.0.1:{}\r\nAuthorization: Bearer {}\r\nAccept: application/json, text/event-stream\r\nContent-Length: {}\r\n\r\n{call}", + server.port(), + grant.token(), + call.len() + ); + stream.write_all(request.as_bytes()).await.unwrap(); + let mut head = [0u8; 256]; + let read = stream.read(&mut head).await.unwrap(); + let head = String::from_utf8_lossy(&head[..read]).to_ascii_lowercase(); + assert!(head.contains("text/event-stream"), "{head}"); + // Wait for the call to reach the engine, then hang up. + for _ in 0..100 { + if !lock(&engine.awaited).is_empty() { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + assert_eq!(server.calls_in_flight(), 1); + drop(stream); + gone(&server).await; +} + +#[tokio::test] +async fn a_2025_cancel_notification_stops_its_call() { + let engine = Arc::::default(); + let server = server(engine.clone()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + let port = server.port(); + let token = grant.token().to_owned(); + let call = legacy_call(9, "wait_on_background", json!({ "task_ids": ["block"] })); + let pending = tokio::spawn(async move { exchange(port, "POST", &[accept(), bearer(&token)], &call).await }); + for _ in 0..100 { + if !lock(&engine.awaited).is_empty() { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + let cancel = json!({ "jsonrpc": "2.0", "method": "notifications/cancelled", "params": { "requestId": 9 } }).to_string(); + let (status, ..) = exchange(port, "POST", &[accept(), bearer(grant.token())], &cancel).await; + assert_eq!(status, 202); + gone(&server).await; + // The call's own stream ends without an answer. + let (_, _, body) = pending.await.unwrap(); + assert!(!body.contains("\"result\""), "{body}"); +} + +/// Reads from `stream` until `text` has come, or panics. +async fn read_until(stream: &mut tokio::net::TcpStream, text: &str) -> String { + let mut seen = Vec::new(); + let mut chunk = [0u8; 4096]; + while !String::from_utf8_lossy(&seen).contains(text) { + let read = tokio::time::timeout(Duration::from_secs(10), stream.read(&mut chunk)) + .await + .expect("came in time") + .unwrap(); + assert!(read > 0, "closed before {text:?}: {}", String::from_utf8_lossy(&seen)); + seen.extend_from_slice(&chunk[..read]); + } + String::from_utf8_lossy(&seen).into_owned() +} + +/// A `subscriptions/listen` stream opened with `token` (`Connection: +/// close`, so its end ends the connection), past its acknowledgement. +async fn open_listen(port: u16, token: &str) -> tokio::net::TcpStream { + let body = modern(1, "subscriptions/listen", json!({ "notifications": { "toolsListChanged": true } })); + let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port)).await.unwrap(); + let request = format!( + "POST /mcp HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\nAuthorization: Bearer {token}\r\nAccept: application/json, text/event-stream\r\nMCP-Protocol-Version: 2026-07-28\r\nMcp-Method: subscriptions/listen\r\nContent-Length: {}\r\n\r\n{body}", + body.len() + ); + stream.write_all(request.as_bytes()).await.unwrap(); + read_until(&mut stream, "notifications/subscriptions/acknowledged").await; + stream +} + +/// A run's listen stream ends with its token: it held the tools (and their +/// engine connection) for as long as the client kept it open. +#[tokio::test] +async fn a_listen_stream_ends_when_its_run_s_token_does() { + let server = server(Arc::default()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + let mut stream = open_listen(server.port(), grant.token()).await; + assert_eq!(server.listens_open(), 1); + drop(grant); + // The stream's last chunk, then the connection closes. + let mut rest = Vec::new(); + tokio::time::timeout(Duration::from_secs(5), stream.read_to_end(&mut rest)) + .await + .expect("the listen stream outlived its token") + .unwrap(); +} + +/// A listen stream the client closes leaves its run's streams. +#[tokio::test] +async fn a_closed_listen_stream_is_let_go() { + let server = server(Arc::default()).await; + let grant = server.grant(origin("chat-a")).unwrap(); + let stream = open_listen(server.port(), grant.token()).await; + assert_eq!(server.listens_open(), 1); + drop(stream); + for _ in 0..500 { + if server.listens_open() == 0 { + return; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + panic!("the closed listen stream is still held"); +} + +/// A connection that never finishes its request's headers is closed. +#[tokio::test(start_paused = true)] +async fn a_connection_trickling_its_headers_is_closed() { + let server = server(Arc::default()).await; + let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", server.port())).await.unwrap(); + stream + .write_all(format!("POST /mcp HTTP/1.1\r\nHost: 127.0.0.1:{}\r\n", server.port()).as_bytes()) + .await + .unwrap(); + let mut rest = Vec::new(); + tokio::time::timeout(HEADER_READ_TIMEOUT * 3, stream.read_to_end(&mut rest)) + .await + .expect("still open long past the header timeout") + .unwrap(); +} + +/// Past the connection cap, a new connection waits until one ends. +#[tokio::test] +async fn connections_past_the_cap_wait_for_a_free_one() { + let engine = Arc::::default(); + let server = McpHttpServer::bind_with(factory(engine), 2).await.unwrap(); + let grant = server.grant(origin("chat-a")).unwrap(); + let port = server.port(); + let mut held = Vec::new(); + for _ in 0..2 { + held.push(tokio::net::TcpStream::connect(("127.0.0.1", port)).await.unwrap()); + } + let token = grant.token().to_owned(); + let init = json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {} }).to_string(); + let third = tokio::spawn(async move { exchange(port, "POST", &[accept(), bearer(&token)], &init).await }); + tokio::time::sleep(Duration::from_millis(500)).await; + assert!(!third.is_finished(), "served past the cap"); + drop(held.pop()); + let (status, ..) = tokio::time::timeout(Duration::from_secs(5), third) + .await + .expect("served once a connection ended") + .unwrap(); + assert_eq!(status, 200); +} diff --git a/crates/mcp/src/jsonrpc.rs b/crates/mcp/src/jsonrpc.rs index f381d325f..a54116b2e 100644 --- a/crates/mcp/src/jsonrpc.rs +++ b/crates/mcp/src/jsonrpc.rs @@ -28,55 +28,30 @@ use crate::tools::Tools; const PROTOCOL_VERSIONS: [&str; 3] = ["2025-06-18", "2025-03-26", "2024-11-05"]; const LATEST_PROTOCOL: &str = "2025-06-18"; +/// What every agent Zeron launches reads on every request (Claude Code puts +/// a server's instructions in its context whole, while its tools load only +/// when searched for), so it says only what the agent needs to reach for a +/// tool. How to use one is in that tool's own description: chat kinds, +/// hosts, Cloud and the one-level rule in `create_chat`, the visual API and +/// its style in `show_visual`. const INSTRUCTIONS: &str = "\ -Zeron runs coding agents in chats, each hosted on a device, optionally in a project \ -(a folder on that specific device). These tools operate the local Zeron engine: \ -discover devices/projects/chats, create chats with a chosen harness and \ -model, read transcripts, and send messages between chats.\n\ +Zeron runs coding agents in chats, each on a device and optionally in a project there. \ +These tools find devices, projects, harnesses, models and chats; create chats to delegate \ +work to, on any connected provider (not only your own); and message, wait on, read and \ +answer them. Chats are named by id, unique id prefix, or exact title; `whoami` gives yours. \ +For parallel work, start every chat before waiting on any: `create_chats` or `send_messages` \ +(or `wait: false`, then `wait_for_turn`).\n\ \n\ -Chats are referenced by full id, a unique id prefix, or an exact title. Use \ -`whoami` to learn which chat you are speaking from; messages you send are \ -attributed to it and a chat cannot message itself. For request/response \ -with a single chat, `send_message` with `wait: true` returns its reply. \ -For parallel delegation, use `create_chats` with a prompt for each chat or \ -`send_messages` for existing chats. These batch tools launch requests concurrently \ -even if your harness executes tool calls sequentially. Alternatively, launch ALL \ -chats/messages with `wait: false` first, then use `wait_for_turn` to collect replies. \ -Do not wait for one worker before launching the next independent worker. \ -A chat you create from your chat is your subagent by default (`kind: subagent`): \ -it lists under your chat's Subagents and in its transcript's subagent block. Use \ -`kind: side` only for a side conversation the person should treat as their own, \ -and `kind: chat` for a standalone session visible in Sessions (no parent). Omitted \ -kind: subagent with an origin/parent, standalone otherwise. \ -Discover hosts with `list_devices`, then `list_projects {device}`, \ -`list_harnesses {device}` and `list_models {device, harness}`. A project fixes \ -its host; with both project and device it must belong to that device. Use listed \ -ids when names/paths repeat. Without project or device, creation uses the local engine. \ -If a chat is `awaitingInput`, answer it with `respond_to_input`.\n\ -\n\ -Delegation is provider-agnostic: `list_harnesses` shows every provider, and \ -those marked `connected` can take work from you whichever provider you run \ -on (a claude-code agent may hand a task to codex, and the reverse). Pass \ -`harness` (and a model from `list_models`) to `create_chat` to choose one. \ -Delegation is one level deep: a chat that has a parent cannot create chats. \ -Pass `cloud: true` to run a child on a Cloud machine of its own (Codex or Claude Code); \ -from a Cloud chat it clones the same repository, and without `cloud` it shares your machine.\n\ -\n\ -When a chart, table, diagram, dashboard, or 3D view would say more than prose and \ -`show_visual` is available, write a Luau visual script and show it with `show_visual` before \ -your final reply. Zeron renders it with its own native interface and enforces its style: \ -content sits directly on the chat with no cards or boxed panels, text uses the interface font \ -and type scale, and colors are theme tokens that work on frosted glass or opaque, light or dark. \ -The reader sees the visual above that reply, so don't announce or restate it. To show the \ -reader an image file (a screenshot, a rendered PNG), call `show_image` with its path; don't \ -draw images through `show_visual`."; +Where `show_visual` is offered and a chart, table, diagram, dashboard, or 3D view would say \ +more than prose, show one with it before your final reply, and don't restate it there. Show \ +an image file with `show_image`."; /// Appended to [`INSTRUCTIONS`] where the agent is offered /// `wait_on_background` ([`zeron_proto::McpServer::WAITS_ON_BACKGROUND_ENV`]). +/// (Claude Code also gets the longer note in its system prompt.) const WAITS_ON_BACKGROUND: &str = "\n\n\ -Before ending a turn while a background command you started is still running and you'll carry on \ -once it ends (tests, a build), call `wait_on_background` with its task id, so the person sees the \ -chat waiting rather than finished. Not for anything you leave running (a dev server, a watcher)."; +Before ending a turn while background work you'll come back to is still running (tests, a \ +build), call `wait_on_background` with its task id; never for a dev server or a watcher."; /// Appended to [`INSTRUCTIONS`] when the host launched the agent without its /// own subagent tools ([`zeron_proto::McpServer::SIDE_CHATS_ONLY_ENV`]). @@ -87,7 +62,7 @@ subagent, delegate through Zeron chats instead, with `create_chats` (or `create_ /// What `initialize` tells the agent: with when to say it waits where it's /// offered `wait_on_background`, and the side-chat rule when its host turned /// the agent's own subagents off. -fn instructions(waits: bool, side_chats_only: bool) -> std::borrow::Cow<'static, str> { +pub(crate) fn instructions(waits: bool, side_chats_only: bool) -> std::borrow::Cow<'static, str> { if !waits && !side_chats_only { return INSTRUCTIONS.into(); } @@ -365,7 +340,7 @@ fn spawn_progress(token: Value, out: mpsc::Sender) -> tokio::task::JoinH /// `response`, cut down to [`MAX_REPLY_BYTES`] when it is bigger: the /// structured copy goes, and the text is truncated with a pointer to paging. -fn cap_reply(response: Value) -> Value { +pub(crate) fn cap_reply(response: Value) -> Value { if response.to_string().len() <= MAX_REPLY_BYTES { return response; } @@ -411,7 +386,7 @@ fn ok_response(id: Value, result: Value) -> Value { json!({ "jsonrpc": "2.0", "id": id, "result": result }) } -fn error_response(id: Value, code: i64, message: &str) -> Value { +pub(crate) fn error_response(id: Value, code: i64, message: &str) -> Value { json!({ "jsonrpc": "2.0", "id": id, diff --git a/crates/mcp/src/lib.rs b/crates/mcp/src/lib.rs index e45cc83e7..83b29da7a 100644 --- a/crates/mcp/src/lib.rs +++ b/crates/mcp/src/lib.rs @@ -16,7 +16,9 @@ //! environment. Every send from such an agent is then attributed to its //! originating chat, and a chat can never message itself. +pub mod http; mod jsonrpc; +pub mod proxy; mod tools; mod transcript; mod zeron; diff --git a/crates/mcp/src/proxy.rs b/crates/mcp/src/proxy.rs new file mode 100644 index 000000000..88a528692 --- /dev/null +++ b/crates/mcp/src/proxy.rs @@ -0,0 +1,390 @@ +//! `mcp-proxy`: Zeron's MCP tools on stdio for an agent that only speaks +//! stdio MCP, forwarded to the engine's own HTTP endpoint ([`crate::http`]). +//! +//! It is what an agent's run starts in place of a whole `zeron mcp` server: +//! no async runtime, no engine client, no state beyond the calls in flight — +//! std only, a thread per call — so it stays a couple of MB resident. Each +//! line from the agent is POSTed on its own connection with the run's token +//! (`ZERON_MCP_TOKEN`) to `ZERON_MCP_URL`; whatever comes back (one JSON +//! object, or an SSE stream of progress and then the response) is written +//! back as lines. Once the agent has initialized, a `subscriptions/listen` +//! stream brings tool-list changes, as the stdio server sent them. +//! +//! A call the endpoint can't take (the engine restarted and the run's token +//! went with it) is answered with an error, never left hanging. + +use std::io::{BufRead, BufReader, Read, Write}; +use std::net::TcpStream; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use serde_json::{Value, json}; + +/// The modern revision the change stream is opened with. +const LISTEN_VERSION: &str = "2026-07-28"; + +#[derive(Clone)] +struct Endpoint { + host: String, + port: u16, + path: String, + token: String, +} + +impl Endpoint { + fn from_env() -> anyhow::Result { + let url = std::env::var(zeron_proto::McpServer::URL_ENV) + .map_err(|_| anyhow::anyhow!("{} is not set", zeron_proto::McpServer::URL_ENV))?; + let token = std::env::var(zeron_proto::McpServer::TOKEN_ENV) + .map_err(|_| anyhow::anyhow!("{} is not set", zeron_proto::McpServer::TOKEN_ENV))?; + Self::parse(&url, token) + } + + /// `http://host:port/path` — loopback only. + fn parse(url: &str, token: String) -> anyhow::Result { + let rest = url + .strip_prefix("http://") + .ok_or_else(|| anyhow::anyhow!("not an http:// URL: {url}"))?; + let (authority, path) = match rest.find('/') { + Some(slash) => (&rest[..slash], &rest[slash..]), + None => (rest, "/"), + }; + let (host, port) = authority + .rsplit_once(':') + .ok_or_else(|| anyhow::anyhow!("no port in {url}"))?; + if host != "127.0.0.1" && host != "localhost" { + anyhow::bail!("refusing a non-loopback MCP endpoint: {url}"); + } + Ok(Self { + host: host.to_owned(), + port: port.parse()?, + path: path.to_owned(), + token, + }) + } + + /// POST `body`; the response's status, content type and body reader. + fn post(&self, body: &str, extra: &[(&str, String)]) -> std::io::Result { + let mut stream = TcpStream::connect((self.host.as_str(), self.port))?; + let _ = stream.set_nodelay(true); + let mut head = format!( + "POST {} HTTP/1.1\r\nHost: {}:{}\r\nAuthorization: Bearer {}\r\n\ + Content-Type: application/json\r\nAccept: application/json, text/event-stream\r\n\ + Content-Length: {}\r\nConnection: close\r\n", + self.path, + self.host, + self.port, + self.token, + body.len() + ); + for (name, value) in extra { + head.push_str(&format!("{name}: {value}\r\n")); + } + head.push_str("\r\n"); + stream.write_all(head.as_bytes())?; + stream.write_all(body.as_bytes())?; + stream.flush()?; + read_reply(stream) + } +} + +struct Reply { + status: u16, + event_stream: bool, + body: Box, +} + +fn read_reply(stream: TcpStream) -> std::io::Result { + let mut reader = BufReader::new(stream); + let mut line = String::new(); + reader.read_line(&mut line)?; + let status = line + .split_whitespace() + .nth(1) + .and_then(|code| code.parse().ok()) + .ok_or_else(|| std::io::Error::other(format!("bad status line: {line:?}")))?; + let mut length: Option = None; + let mut chunked = false; + let mut event_stream = false; + loop { + line.clear(); + if reader.read_line(&mut line)? == 0 { + break; + } + let header = line.trim_end(); + if header.is_empty() { + break; + } + let Some((name, value)) = header.split_once(':') else { + continue; + }; + let value = value.trim(); + match name.trim().to_ascii_lowercase().as_str() { + "content-length" => length = value.parse().ok(), + "transfer-encoding" => chunked = value.eq_ignore_ascii_case("chunked"), + "content-type" => event_stream = value.starts_with("text/event-stream"), + _ => {} + } + } + let body: Box = if chunked { + Box::new(BufReader::new(Chunked { inner: reader, left: 0, done: false })) + } else if let Some(length) = length { + Box::new(BufReader::new(reader.take(length))) + } else { + Box::new(reader) + }; + Ok(Reply { status, event_stream, body }) +} + +/// A `Transfer-Encoding: chunked` body, decoded. +struct Chunked { + inner: R, + left: usize, + done: bool, +} + +impl Read for Chunked { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if self.done { + return Ok(0); + } + if self.left == 0 { + let mut size = String::new(); + // The CRLF ending the previous chunk, then the size line. + loop { + size.clear(); + if self.inner.read_line(&mut size)? == 0 { + self.done = true; + return Ok(0); + } + if !size.trim().is_empty() { + break; + } + } + let hex = size.trim().split(';').next().unwrap_or("0"); + self.left = usize::from_str_radix(hex, 16) + .map_err(|_| std::io::Error::other(format!("bad chunk size: {size:?}")))?; + if self.left == 0 { + self.done = true; + return Ok(0); + } + } + let want = buf.len().min(self.left); + let read = self.inner.read(&mut buf[..want])?; + if read == 0 { + self.done = true; + } + self.left -= read; + Ok(read) + } +} + +/// Every JSON-RPC message in a reply, in order, to `each`. +fn messages(reply: Reply, mut each: impl FnMut(Value)) -> std::io::Result<()> { + let mut body = reply.body; + if !reply.event_stream { + let mut text = String::new(); + body.read_to_string(&mut text)?; + if let Ok(message) = serde_json::from_str::(text.trim()) { + each(message); + } + return Ok(()); + } + // SSE: `data:` lines make an event; a blank line ends it; `:` lines are + // comments (keep-alives). + let mut data = String::new(); + let mut line = String::new(); + loop { + line.clear(); + if body.read_line(&mut line)? == 0 { + return Ok(()); + } + let text = line.trim_end_matches(['\r', '\n']); + if text.is_empty() { + if !data.is_empty() { + if let Ok(message) = serde_json::from_str::(&data) { + each(message); + } + data.clear(); + } + } else if let Some(value) = text.strip_prefix("data:") { + if !data.is_empty() { + data.push('\n'); + } + data.push_str(value.strip_prefix(' ').unwrap_or(value)); + } + } +} + +type Out = Arc>; + +fn write_line(out: &Out, message: &Value) { + let mut out = out.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let _ = writeln!(out, "{message}"); + let _ = out.flush(); +} + +fn failure(id: Value, message: String) -> Value { + json!({ "jsonrpc": "2.0", "id": id, "error": { "code": -32603, "message": message } }) +} + +/// Forward one request; its reply (or an error) goes to `out`. +fn forward_request(endpoint: &Endpoint, out: &Out, id: Value, line: &str) { + let mut answered = false; + let result = endpoint.post(line, &[]).and_then(|reply| { + let status = reply.status; + messages(reply, |message| { + if message.get("id") == Some(&id) && message.get("method").is_none() { + answered = true; + } + write_line(out, &message); + }) + .map(|()| status) + }); + if !answered { + let why = match result { + Ok(401) => "the Zeron engine no longer knows this agent's run (it restarted?)".to_owned(), + Ok(status) => format!("the Zeron engine answered HTTP {status}"), + Err(err) => format!("the Zeron engine can't be reached: {err}"), + }; + write_line(out, &failure(id, why)); + } +} + +/// Tool-list changes, while the agent runs: a `subscriptions/listen` +/// stream, reopened when it drops, until the endpoint refuses the token. +fn listen_for_changes(endpoint: Endpoint, out: Out) { + let body = json!({ + "jsonrpc": "2.0", + "id": "zeron-proxy-listen", + "method": "subscriptions/listen", + "params": { + "_meta": { + "io.modelcontextprotocol/protocolVersion": LISTEN_VERSION, + "io.modelcontextprotocol/clientInfo": { "name": "zeron-mcp-proxy", "version": env!("CARGO_PKG_VERSION") }, + "io.modelcontextprotocol/clientCapabilities": {}, + }, + "notifications": { "toolsListChanged": true }, + }, + }) + .to_string(); + let headers = [ + ("MCP-Protocol-Version", LISTEN_VERSION.to_owned()), + ("Mcp-Method", "subscriptions/listen".to_owned()), + ]; + let mut backoff = Duration::from_secs(1); + loop { + match endpoint.post(&body, &headers) { + Ok(reply) if reply.status == 200 => { + backoff = Duration::from_secs(1); + let _ = messages(reply, |message| { + if message.get("method").and_then(Value::as_str) + == Some("notifications/tools/list_changed") + { + // As the stdio server sent it: no subscription tag. + write_line( + &out, + &json!({ "jsonrpc": "2.0", "method": "notifications/tools/list_changed" }), + ); + } + }); + } + // The run is over (or never was): nothing more will change. + Ok(reply) if reply.status == 401 || reply.status == 403 => return, + _ => {} + } + std::thread::sleep(backoff); + backoff = (backoff * 2).min(Duration::from_secs(30)); + } +} + +/// Run until stdin closes. +pub fn run() -> anyhow::Result<()> { + let endpoint = Endpoint::from_env()?; + let out: Out = Arc::new(Mutex::new(std::io::stdout())); + let mut listening = false; + let stdin = std::io::stdin(); + let mut line = String::new(); + let mut input = stdin.lock(); + loop { + line.clear(); + match input.read_line(&mut line) { + Ok(0) => return Ok(()), + Ok(_) => {} + Err(err) if err.kind() == std::io::ErrorKind::Interrupted => continue, + Err(err) => return Err(err.into()), + } + let text = line.trim(); + if text.is_empty() { + continue; + } + let Ok(message) = serde_json::from_str::(text) else { + write_line( + &out, + &json!({ "jsonrpc": "2.0", "id": null, "error": { "code": -32700, "message": "parse error" } }), + ); + continue; + }; + let method = message.get("method").and_then(Value::as_str).map(str::to_owned); + let id = message.get("id").cloned(); + let endpoint = endpoint.clone(); + let body = text.to_owned(); + match (id, method) { + (Some(id), Some(_)) => { + let out = out.clone(); + std::thread::spawn(move || forward_request(&endpoint, &out, id, &body)); + } + (None, Some(method)) => { + if method == "notifications/initialized" && !listening { + listening = true; + let out = out.clone(); + let listen = endpoint.clone(); + std::thread::spawn(move || listen_for_changes(listen, out)); + } + // Cancels and the like: the endpoint answers 202. + std::thread::spawn(move || { + let _ = endpoint.post(&body, &[]).map(|reply| messages(reply, |_| {})); + }); + } + // A response to a server request: the endpoint sends none. + _ => {} + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_a_loopback_http_endpoint_is_taken() { + let ok = Endpoint::parse("http://127.0.0.1:4321/mcp", "t".into()).unwrap(); + assert_eq!((ok.host.as_str(), ok.port, ok.path.as_str()), ("127.0.0.1", 4321, "/mcp")); + assert!(Endpoint::parse("http://example.com:80/mcp", "t".into()).is_err()); + assert!(Endpoint::parse("https://127.0.0.1:1/mcp", "t".into()).is_err()); + } + + #[test] + fn chunked_sse_decodes_into_messages_in_order() { + let sse = "event: message\ndata: {\"jsonrpc\":\"2.0\",\"method\":\"notifications/progress\"}\n\n: keep-alive\n\nevent: message\ndata: {\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{}}\n\n"; + let mut wire = String::new(); + for piece in [&sse[..10], &sse[10..70], &sse[70..]] { + wire.push_str(&format!("{:x}\r\n{piece}\r\n", piece.len())); + } + wire.push_str("0\r\n\r\n"); + let reader = BufReader::new(Chunked { + inner: BufReader::new(std::io::Cursor::new(wire.into_bytes())), + left: 0, + done: false, + }); + let mut seen = Vec::new(); + messages( + Reply { status: 200, event_stream: true, body: Box::new(reader) }, + |message| seen.push(message), + ) + .unwrap(); + assert_eq!(seen.len(), 2); + assert_eq!(seen[0]["method"], "notifications/progress"); + assert_eq!(seen[1]["id"], 1); + } +} diff --git a/crates/preview/src/discovery.rs b/crates/preview/src/discovery.rs index 918037a56..fcc7121ed 100644 --- a/crates/preview/src/discovery.rs +++ b/crates/preview/src/discovery.rs @@ -197,6 +197,20 @@ impl Owned { pub fn is_empty(&self) -> bool { self.pids.is_empty() } + /// Which processes these are (each pid with its start): equal while the + /// same ones live, whatever order they were found in. + pub fn signature(&self) -> u64 { + use std::hash::{Hash, Hasher}; + let mut pids: Vec<(u32, u64)> = self + .pids + .iter() + .map(|pid| (*pid, self.processes.get(pid).map_or(0, |p| p.started_at))) + .collect(); + pids.sort_unstable(); + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + pids.hash(&mut hasher); + hasher.finish() + } pub fn len(&self) -> usize { self.pids.len() } diff --git a/crates/preview/src/service.rs b/crates/preview/src/service.rs index f429f2d31..ffdfbfb6b 100644 --- a/crates/preview/src/service.rs +++ b/crates/preview/src/service.rs @@ -40,6 +40,58 @@ enum Verdict { const NOT_HTTP_INITIAL_BACKOFF: Duration = Duration::from_secs(4); /// How often discovery scans while something Zeron started is alive. const SCAN_CADENCE: Duration = Duration::from_secs(2); +/// The longest discovery waits between scans while only processes left +/// running (an agent's warm session, a server left up) keep it going and +/// nothing about them changes: see [`ScanPace`]. +const SCAN_RESTING_MAX: Duration = Duration::from_secs(30); + +/// What keeps discovery scanning: a live run or terminal, or processes a +/// chat started that are still alive (`owned`: which ones, and what they +/// listen on, as a signature). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct Activity { + live: bool, + owned: Option, +} + +impl Activity { + fn active(&self) -> bool { + self.live || self.owned.is_some() + } +} + +/// The wait before discovery's next scan. Every [`SCAN_CADENCE`] while a run +/// works or a terminal is open, or while what's alive changes. Once only +/// processes left running keep it going, unchanged — an agent's warm +/// session, which an engine at rest holds for every recent chat — each scan +/// waits twice as long as the last, up to [`SCAN_RESTING_MAX`]: a scan reads +/// every process on the machine, and its every-2-s cadence was most of an +/// idle engine's work. A run or terminal starting, the chats changing, or an +/// open preview losing its process wakes it at once +/// ([`PreviewService::rescan`]). +#[derive(Default)] +struct ScanPace { + resting: u32, + last: Option, +} + +impl ScanPace { + fn next(&mut self, activity: Activity) -> Duration { + let unchanged = self.last.replace(activity) == Some(activity); + if activity.live || !unchanged { + self.resting = 0; + return SCAN_CADENCE; + } + self.resting = self.resting.saturating_add(1).min(8); + (SCAN_CADENCE * 2u32.pow(self.resting)).min(SCAN_RESTING_MAX) + } + + /// A wake: the next scan starts over at the full cadence. + fn reset(&mut self) { + self.resting = 0; + self.last = None; + } +} const NOT_HTTP_MAX_BACKOFF: Duration = Duration::from_secs(60); /// (servers already confirmed HTTP, listeners to probe this cycle) @@ -139,7 +191,7 @@ pub type Projects = Arc Scope + Send + Sync>; fn candidates( scope: Scope, origins: &mut discovery::Origins, -) -> Option<(Vec<(PathBuf, discovery::Listener)>, bool)> { +) -> Option<(Vec<(PathBuf, discovery::Listener)>, Activity)> { let Scope { chats, terminals, @@ -164,7 +216,7 @@ fn candidates( .into_iter() .collect(); if roots.is_empty() { - return Some((Vec::new(), live)); + return Some((Vec::new(), Activity { live, owned: None })); } roots.sort_by_key(|p| std::cmp::Reverse(p.components().count())); // The origins a listed server can have (see the filter below). @@ -174,8 +226,20 @@ fn candidates( _ => false, }; let owned = discovery::owned(origins, &accept)?; - let active = live || !owned.is_empty(); let listeners = discovery::owned_listeners(origins, &owned)?; + let active = Activity { + live, + owned: (!owned.is_empty()).then(|| { + use std::hash::{Hash, Hasher}; + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + owned.signature().hash(&mut hasher); + let mut ports: Vec<(u32, SocketAddr)> = + listeners.iter().map(|l| (l.pid, l.address)).collect(); + ports.sort_unstable(); + ports.hash(&mut hasher); + hasher.finish() + }), + }; Some(( listeners .into_iter() @@ -373,14 +437,16 @@ impl PreviewService { let catalog = self.0.catalog.clone(); let stop = self.0.stop.clone(); let wake = self.0.scan_wake.clone(); - // Every 2 s while anything Zeron started here is alive (a run, a - // terminal, or a process either left running): a server one starts - // is listed within one cadence, as before. With none alive no + // Every 2 s while a run or terminal is alive, or what a chat left + // running changes: a server one starts is listed within one + // cadence, as before. While only unchanged processes left running + // keep it going, it slows down ([`ScanPace`]). With none alive no // listener could be listed, so the scan parks until a run or // terminal changes state, or an open preview loses its process. let scanner = tokio::spawn(async move { let mut memory = ProbeMemory::default(); let mut origins = discovery::Origins::default(); + let mut pace = ScanPace::default(); loop { let memory = &mut memory; let origins = &mut origins; @@ -404,11 +470,11 @@ impl PreviewService { tracing::debug!( "preview scan could not read processes; keeping the last result" ); - return true; + return Activity { live: true, owned: None }; } Err(error) => { tracing::warn!(%error, "preview scan failed; keeping the last result"); - return true; + return Activity { live: true, owned: None }; } }; let (mut servers, probe) = memory.plan(candidates, Instant::now()); @@ -432,12 +498,22 @@ impl PreviewService { } active }; - let active = tokio::select! { _ = stop.cancelled() => break, active = scan => active }; - if active { - // (A wake meanwhile is served by this cadence.) - tokio::select! { _ = stop.cancelled() => break, _ = tokio::time::sleep(SCAN_CADENCE) => {} } - } else { + let activity = tokio::select! { _ = stop.cancelled() => break, activity = scan => activity }; + if !activity.active() { + pace.reset(); tokio::select! { _ = stop.cancelled() => break, _ = wake.notified() => {} } + continue; + } + let wait = pace.next(activity); + if wait <= SCAN_CADENCE { + // (A wake meanwhile is served by this cadence.) + tokio::select! { _ = stop.cancelled() => break, _ = tokio::time::sleep(wait) => {} } + } else { + tokio::select! { + _ = stop.cancelled() => break, + _ = tokio::time::sleep(wait) => {} + _ = wake.notified() => pace.reset(), + } } } }); @@ -612,9 +688,9 @@ mod tests { let mut origins = discovery::Origins::default(); let (found, active) = candidates(scope(false), &mut origins).unwrap(); assert!(found.is_empty()); - assert!(!active, "nothing of this chat runs: nothing to scan for"); + assert!(!active.active(), "nothing of this chat runs: nothing to scan for"); let (_, active) = candidates(scope(true), &mut origins).unwrap(); - assert!(active, "a live run keeps the cadence before its process exists"); + assert!(active.active(), "a live run keeps the cadence before its process exists"); // What an agent of this chat starts: a listener in its folder. struct Kill(std::process::Child); @@ -640,7 +716,9 @@ mod tests { std::io::BufRead::read_line(&mut std::io::BufReader::new(stdout), &mut line).unwrap(); let port: u16 = line.trim().parse().unwrap(); let (found, active) = candidates(scope(false), &mut origins).unwrap(); - assert!(active, "a process the chat started is alive"); + assert!(active.active() && !active.live, "a process the chat started is alive"); + let (_, again) = candidates(scope(false), &mut origins).unwrap(); + assert_eq!(again, active, "the same process, listening the same: unchanged"); assert!( found.iter().any(|(_, l)| l.address.port() == port && l.pid == server.0.id() @@ -650,7 +728,28 @@ mod tests { ); drop(server); let (found, active) = candidates(scope(false), &mut origins).unwrap(); - assert!(found.is_empty() && !active, "gone with its process"); + assert!(found.is_empty() && !active.active(), "gone with its process"); + } + #[test] + fn scans_slow_down_only_while_what_runs_is_unchanged() { + let warm = Activity { live: false, owned: Some(1) }; + let mut pace = ScanPace::default(); + assert_eq!(pace.next(warm), SCAN_CADENCE, "first sight of it"); + let waits: Vec<_> = (0..6).map(|_| pace.next(warm).as_secs()).collect(); + assert_eq!(waits, [4, 8, 16, 30, 30, 30]); + // A process starting or exiting, or a new listener: full cadence. + assert_eq!(pace.next(Activity { live: false, owned: Some(2) }), SCAN_CADENCE); + assert_eq!(pace.next(Activity { live: false, owned: Some(2) }).as_secs(), 4); + // A run working keeps the cadence however long nothing changes. + let live = Activity { live: true, owned: Some(2) }; + for _ in 0..5 { + assert_eq!(pace.next(live), SCAN_CADENCE); + } + // A wake starts over. + assert_eq!(pace.next(warm), SCAN_CADENCE); + assert_eq!(pace.next(warm).as_secs(), 4); + pace.reset(); + assert_eq!(pace.next(warm), SCAN_CADENCE); } #[test] fn backoff_is_capped() { diff --git a/crates/proto/src/agent.rs b/crates/proto/src/agent.rs index 2ac0ee609..6000a8577 100644 --- a/crates/proto/src/agent.rs +++ b/crates/proto/src/agent.rs @@ -666,7 +666,12 @@ impl std::fmt::Debug for RunRequest { /// of whatever the user configured. Each driver spells it in its own dialect /// (Claude `--mcp-config`, ACP `session/new` `mcpServers`, Codex /// `mcp_servers.*` config overrides). -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +/// +/// Zeron's own server also carries [`Self::http`]: the engine serves its +/// tools itself, over loopback Streamable HTTP, so a driver whose agent +/// speaks HTTP MCP adds that and starts no process. `command`/`args`/`env` +/// then name a small stdio-to-HTTP proxy for agents that only speak stdio. +#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct McpServer { pub name: String, @@ -675,9 +680,78 @@ pub struct McpServer { pub args: Vec, #[serde(default)] pub env: std::collections::BTreeMap, + /// The same server over Streamable HTTP, for agents that speak it. + /// Additive + serde-defaulted for wire compat. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub http: Option, +} + +/// Values of these keys are credentials: never printed. +const SECRET_ENV: [&str; 1] = [McpServer::TOKEN_ENV]; + +/// Hand-written so a `{:?}` never prints a run's MCP token. +impl std::fmt::Debug for McpServer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let env: std::collections::BTreeMap<&str, &str> = self + .env + .iter() + .map(|(key, value)| { + let value = if SECRET_ENV.contains(&key.as_str()) { + "" + } else { + value.as_str() + }; + (key.as_str(), value) + }) + .collect(); + f.debug_struct("McpServer") + .field("name", &self.name) + .field("command", &self.command) + .field("args", &self.args) + .field("env", &env) + .field("http", &self.http) + .finish() + } +} + +/// An MCP server's Streamable HTTP endpoint: its URL and the headers every +/// request carries (the run's bearer token). +#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct McpHttp { + pub url: String, + #[serde(default)] + pub headers: std::collections::BTreeMap, +} + +impl McpHttp { + /// The bearer token of an `Authorization: Bearer …` header, if any. + pub fn bearer_token(&self) -> Option<&str> { + self.headers + .iter() + .find(|(name, _)| name.eq_ignore_ascii_case("authorization")) + .and_then(|(_, value)| value.strip_prefix("Bearer ")) + } +} + +/// Header names only: the values are credentials. +impl std::fmt::Debug for McpHttp { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("McpHttp") + .field("url", &self.url) + .field("headers", &self.headers.keys().collect::>()) + .finish() + } } impl McpServer { + /// The stdio proxy's endpoint URL ([`Self::http`]'s, for agents that + /// only speak stdio). + pub const URL_ENV: &'static str = "ZERON_MCP_URL"; + /// The stdio proxy's bearer token: the run's own, scoped to its chat and + /// revoked when the run ends. + pub const TOKEN_ENV: &'static str = "ZERON_MCP_TOKEN"; + /// Set to `1` on Zeron's own server when its host turned the provider's /// native subagents off (Settings → Providers) and the chat has no /// parent: the agent delegates through this server's side chats instead, diff --git a/crates/rpc/examples/idle_rig.rs b/crates/rpc/examples/idle_rig.rs new file mode 100644 index 000000000..a357dab1d --- /dev/null +++ b/crates/rpc/examples/idle_rig.rs @@ -0,0 +1,334 @@ +//! Engine idle/streaming cost rig (not shipped): drives a headless engine over +//! its IPC the way the app does, so its CPU and wakeups can be measured. +//! +//! ```sh +//! idle_rig seed # n mock chats with one finished turn each; prints ids +//! idle_rig client [chat] # the app at rest: its watches (and one viewed chat) +//! idle_rig stream ... # a run in each chat, watched like the app +//! idle_rig latency # QueueCommand → user echo / first assistant text +//! idle_rig sync # A's run → B sees it +//! ``` + +use std::time::{Duration, Instant}; + +use zeron_rpc::{RpcClient, connect_ipc, methods}; + +fn fail(message: &str) -> ! { + eprintln!("FAIL: {message}"); + std::process::exit(1); +} + +async fn connect(port: &str, dir: &str) -> RpcClient { + let port: u16 = port.parse().unwrap_or_else(|_| fail("bad port")); + connect_ipc(port, Some(std::path::Path::new(dir))) + .await + .unwrap_or_else(|err| fail(&format!("connect: {err}"))) +} + +async fn queue_run(client: &RpcClient, chat: &str, prompt: &str) { + client + .call( + methods::QUEUE_COMMAND, + serde_json::json!({ + "chatId": chat, + "command": { + "kind": "run", + "messageId": uuid::Uuid::new_v4().to_string(), + "request": { + "prompt": prompt, + "model": null, + "reasoning": null, + "cwd": "/tmp", + "sandbox": "workspace-write", + "autoApprove": true, + "resume": null, + }, + }, + }), + ) + .await + .unwrap_or_else(|err| fail(&format!("QueueCommand: {err}"))); +} + +/// Keep a stream drained in the background; counts its items. +fn drain( + mut rx: tokio::sync::mpsc::Receiver, + count: std::sync::Arc, +) { + tokio::spawn(async move { + while rx.recv().await.is_some() { + count.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + }); +} + +/// The watches the zgx app holds at rest (apps/zgx/src/sessions.rs, chat.rs, +/// plugins, updates), plus a viewed chat's when one is given. +async fn app_watches( + client: &RpcClient, + chat: Option<&str>, +) -> std::sync::Arc { + let count = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0)); + for method in [ + "WatchChats", + "WatchDevices", + "WatchSessions", + "WatchSpaces", + "WatchSidebarPreferences", + "WatchConnectivity", + "WatchHeldSends", + "WatchPlugins", + "WatchHarnessUpdates", + "WatchThemes", + ] { + match client.subscribe(method, serde_json::json!({})).await { + Ok(rx) => drain(rx, count.clone()), + Err(err) => eprintln!("{method}: {err}"), + } + } + if let Some(chat) = chat { + for (method, params) in [ + ( + "WatchDocMessages", + serde_json::json!({ "chatId": chat, "openingTail": true }), + ), + ("WatchQueue", serde_json::json!({ "chatId": chat })), + ( + "WatchWaitingMessages", + serde_json::json!({ "chatId": chat }), + ), + ("WatchPreviews", serde_json::json!({ "chatId": chat })), + ] { + match client.subscribe(method, params).await { + Ok(rx) => drain(rx, count.clone()), + Err(err) => eprintln!("{method}: {err}"), + } + } + } + count +} + +async fn create_space(client: &RpcClient) -> String { + let device = client + .call(methods::LOCAL_DEVICE, serde_json::json!({})) + .await + .unwrap_or_else(|err| fail(&format!("LocalDevice: {err}"))); + let space = uuid::Uuid::new_v4().to_string(); + let path = std::env::temp_dir().join(format!("idle-rig-{space}")); + std::fs::create_dir_all(&path).unwrap_or_else(|err| fail(&format!("mkdir: {err}"))); + client + .call( + methods::MUTATE, + serde_json::json!({ + "op": "createSpace", + "spaceId": space, + "deviceId": device["deviceId"], + "path": path, + }), + ) + .await + .unwrap_or_else(|err| fail(&format!("createSpace: {err}"))); + space +} + +async fn create_chat(client: &RpcClient, space: &str) -> String { + let chat = uuid::Uuid::new_v4().to_string(); + let mut attempts = 0; + loop { + let created = client + .call( + methods::MUTATE, + serde_json::json!({ + "op": "createChat", + "chatId": chat, + "spaceId": space, + "config": { "harness": "mock", "model": null, "reasoning": null, "sandbox": "workspace-write" }, + }), + ) + .await; + match created { + Ok(_) => return chat, + Err(err) if attempts >= 20 => fail(&format!("createChat: {err}")), + Err(_) => { + attempts += 1; + tokio::time::sleep(Duration::from_millis(250)).await; + } + } + } +} + +/// Wait until a doc-messages stream carries `needle`; the time it took. +async fn wait_text( + rx: &mut tokio::sync::mpsc::Receiver, + needle: &str, + start: Instant, +) -> Option { + let deadline = start + Duration::from_secs(30); + loop { + let left = deadline.checked_duration_since(Instant::now())?; + let item = tokio::time::timeout(left, rx.recv()).await.ok()??; + if item.to_string().contains(needle) { + return Some(start.elapsed()); + } + } +} + +fn summary(label: &str, mut samples: Vec) { + if samples.is_empty() { + println!("{label}: no samples"); + return; + } + samples.sort(); + let ms = |d: Duration| d.as_secs_f64() * 1000.0; + let mean = samples.iter().map(|d| ms(*d)).sum::() / samples.len() as f64; + println!( + "{label}: n={} min={:.1}ms p50={:.1}ms mean={:.1}ms p90={:.1}ms max={:.1}ms", + samples.len(), + ms(samples[0]), + ms(samples[samples.len() / 2]), + mean, + ms(samples[(samples.len() * 9 / 10).min(samples.len() - 1)]), + ms(*samples.last().unwrap()), + ); +} + +#[tokio::main] +async fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let [port, dir, command, rest @ ..] = args.as_slice() else { + fail("usage: idle_rig ..."); + }; + let client = connect(port, dir).await; + match (command.as_str(), rest) { + ("seed", [n]) => { + let n: usize = n.parse().unwrap_or_else(|_| fail("bad n")); + let space = create_space(&client).await; + let mut chats = Vec::new(); + for _ in 0..n { + chats.push(create_chat(&client, &space).await); + } + // One finished turn each, so every chat has a transcript. + for (i, chat) in chats.iter().enumerate() { + let mut rx = client + .subscribe( + methods::WATCH_DOC_MESSAGES, + serde_json::json!({ "chatId": chat }), + ) + .await + .unwrap_or_else(|err| fail(&format!("watch: {err}"))); + queue_run(&client, chat, &format!("seed turn {i}")).await; + // The mock's scripted reply ends with a complete status. + let _ = wait_text(&mut rx, "\"complete\"", Instant::now()).await; + println!("{chat}"); + } + } + ("create", [n]) => { + let n: usize = n.parse().unwrap_or_else(|_| fail("bad n")); + let space = create_space(&client).await; + let mut chats = Vec::new(); + for _ in 0..n { + chats.push(create_chat(&client, &space).await); + } + println!("{}", chats.join(" ")); + } + ("client", [secs, chat @ ..]) => { + let secs: u64 = secs.parse().unwrap_or_else(|_| fail("bad secs")); + let count = app_watches(&client, chat.first().map(String::as_str)).await; + tokio::time::sleep(Duration::from_secs(secs)).await; + println!( + "frames={}", + count.load(std::sync::atomic::Ordering::Relaxed) + ); + } + ("stream", [secs, chats @ ..]) => { + let secs: u64 = secs.parse().unwrap_or_else(|_| fail("bad secs")); + let count = app_watches(&client, chats.first().map(String::as_str)).await; + for chat in chats { + queue_run(&client, chat, "stream").await; + } + tokio::time::sleep(Duration::from_secs(secs)).await; + println!( + "frames={}", + count.load(std::sync::atomic::Ordering::Relaxed) + ); + } + ("latency", [rounds, chat]) => { + let rounds: usize = rounds.parse().unwrap_or_else(|_| fail("bad rounds")); + let _watches = app_watches(&client, None).await; + let mut echo = Vec::new(); + let mut first = Vec::new(); + for round in 0..rounds { + let mut rx = client + .subscribe( + methods::WATCH_DOC_MESSAGES, + serde_json::json!({ "chatId": chat }), + ) + .await + .unwrap_or_else(|err| fail(&format!("watch: {err}"))); + // Let the opening frame pass. + let _ = tokio::time::timeout(Duration::from_secs(5), rx.recv()).await; + tokio::time::sleep(Duration::from_millis(300)).await; + while rx.try_recv().is_ok() {} + let marker = format!("latency-{}-{round}", uuid::Uuid::new_v4().simple()); + let start = Instant::now(); + queue_run(&client, chat, &marker).await; + match wait_text(&mut rx, &marker, start).await { + Some(d) => echo.push(d), + None => eprintln!("round {round}: no echo"), + } + // The mock's first text delta (crates/engine/src/registry.rs). + match wait_text(&mut rx, "Streaming pipeline", start).await { + Some(d) => first.push(d), + None => eprintln!("round {round}: no assistant text"), + } + // Let the run end before the next round (the mock's is instant). + tokio::time::sleep(Duration::from_millis(1000)).await; + } + summary("user echo", echo); + summary("first assistant", first); + } + ("sync", [rounds, chat, b_port, b_dir]) => { + let rounds: usize = rounds.parse().unwrap_or_else(|_| fail("bad rounds")); + let b = connect(b_port, b_dir).await; + let mut a_rx = client + .subscribe( + methods::WATCH_DOC_MESSAGES, + serde_json::json!({ "chatId": chat }), + ) + .await + .unwrap_or_else(|err| fail(&format!("watch A: {err}"))); + let mut b_rx = b + .subscribe( + methods::WATCH_DOC_MESSAGES, + serde_json::json!({ "chatId": chat }), + ) + .await + .unwrap_or_else(|err| fail(&format!("watch B: {err}"))); + tokio::time::sleep(Duration::from_secs(2)).await; + let mut samples = Vec::new(); + let mut from_queue = Vec::new(); + for round in 0..rounds { + while a_rx.try_recv().is_ok() {} + while b_rx.try_recv().is_ok() {} + let marker = format!("sync-{}-{round}", uuid::Uuid::new_v4().simple()); + let start = Instant::now(); + queue_run(&client, chat, &marker).await; + let Some(on_a) = wait_text(&mut a_rx, &marker, start).await else { + eprintln!("round {round}: A never showed it"); + continue; + }; + match wait_text(&mut b_rx, &marker, start).await { + Some(on_b) => { + samples.push(on_b.saturating_sub(on_a)); + from_queue.push(on_b); + } + None => eprintln!("round {round}: B never showed it"), + } + tokio::time::sleep(Duration::from_millis(1500)).await; + } + summary("queued here → shown here, there: there minus here", samples); + summary("queued here → shown there", from_queue); + } + _ => fail("unknown command"), + } +} diff --git a/crates/rpc/src/lib.rs b/crates/rpc/src/lib.rs index 132dd6e49..fd73decc4 100644 --- a/crates/rpc/src/lib.rs +++ b/crates/rpc/src/lib.rs @@ -33,7 +33,7 @@ pub use device_room::{ NudgeHandler, PeerLiveness, PeerLivenessProbe, StaticToken, TokenError, TokenSource, decode_device_frame, device_room_ws_url, encode_device_frame, is_liveness_probe, }; -pub use server::{serve_connection, serve_ws_listener, serve_ws_listener_scoped}; +pub use server::{agent_service, serve_connection, serve_ws_listener, serve_ws_listener_scoped}; /// RPC method names — single source of truth for both ends. /// Full surface: docs/research/feature-inventory.md §2. @@ -190,6 +190,15 @@ pub mod methods { /// Headed IPC owners do not implement this method: closing another app's /// engine behind its windows would leave that process unusable. pub const STOP_ENGINE: &str = "StopEngine"; + /// "Keep Zeron running after you quit": `{ keepRunningAfterQuit }` + /// (the background engine's; this device's app only). + pub const GET_BACKGROUND_SETTINGS: &str = "GetBackgroundSettings"; + pub const SET_BACKGROUND_SETTINGS: &str = "SetBackgroundSettings"; + /// A quitting app asks what's still working: `{ keepRunning, working }`. + pub const QUIT_CHECK: &str = "QuitCheck"; + /// The app quits with the setting off: the engine stops once nothing is + /// pending; `{ stopWork: true }` stops the chats still working first. + pub const APP_QUITTING: &str = "AppQuitting"; /// Headless IPC only: stop starting work ahead of a Cloud sleep (the /// edge runs `zeron quiesce` before stopping the machine). Replies /// `{"quiesced":true}` or `{"quiesced":false,"busy":""}`. @@ -692,6 +701,11 @@ pub fn local_only(method: &str) -> bool { method, methods::ACCESS_TOKEN | methods::STEP_UP + // This device's background engine: whether it outlives its app. + | methods::GET_BACKGROUND_SETTINGS + | methods::SET_BACKGROUND_SETTINGS + | methods::QUIT_CHECK + | methods::APP_QUITTING // An API key typed into this device's app: stored here alone. | methods::ADD_AGENT_API_KEY | methods::DELETE_ACCOUNT diff --git a/crates/rpc/src/server.rs b/crates/rpc/src/server.rs index bed0a8f57..88d6a966c 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -234,6 +234,30 @@ pub async fn serve_ws_listener_scoped(listener: TcpListener, service: Arc, pub(crate) chat: Option, + /// `chat` is verified (the engine minted the caller's credential for + /// it), so the methods that act on the calling chat act on it alone. + pub(crate) pinned: bool, +} + +/// Methods that act on the calling chat, named by `chatId`: for a pinned +/// agent ([`agent_service`]), only its own. +const CALLING_CHAT_METHODS: [&str; 3] = [ + crate::methods::PUBLISH_VISUAL, + crate::methods::PUBLISH_IMAGE, + crate::methods::AWAIT_BACKGROUND, +]; + +/// The service as one run's agent reaches it: only [`crate::agent_allowed`] +/// methods, served as [`crate::Caller::Agent`] for `chat` — which, unlike +/// the WS handshake's claimed `x-zeron-chat`, the engine itself vouches for +/// (the run's own MCP token named it). What acts on "the calling chat" +/// ([`CALLING_CHAT_METHODS`]) is refused for any other chat. +pub fn agent_service(inner: Arc, chat: String) -> Arc { + Arc::new(AgentGuard { + inner, + chat: Some(chat), + pinned: true, + }) } #[async_trait::async_trait] @@ -245,6 +269,15 @@ impl RpcService for AgentGuard { "{method} isn't open to agents: only the person can do that, in Zeron" ))); } + if self.pinned + && CALLING_CHAT_METHODS.contains(&method) + && params.get("chatId").and_then(serde_json::Value::as_str) != self.chat.as_deref() + { + tracing::warn!(method, chat = ?self.chat, "rpc: refused an agent's call on another chat"); + return Err(RpcError::Failed(format!( + "{method} acts on the calling chat only" + ))); + } let caller = crate::Caller::Agent { chat: self.chat.clone() }; crate::with_caller(caller, self.inner.handle(method, params)).await } @@ -331,7 +364,7 @@ async fn serve_ws_socket(stream: TcpStream, service: Arc, tokens }); let service: Arc = match scope.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner) { - Some(chat) => Arc::new(AgentGuard { inner: service, chat }), + Some(chat) => Arc::new(AgentGuard { inner: service, chat, pinned: false }), None => service, }; serve_connection(service, out_tx, in_rx).await; diff --git a/crates/sync/src/store.rs b/crates/sync/src/store.rs index 0e90501ab..970452759 100644 --- a/crates/sync/src/store.rs +++ b/crates/sync/src/store.rs @@ -213,6 +213,8 @@ pub struct DocsStore { sync_jobs_changed: tokio::sync::watch::Sender, /// Bumped whenever `chat_outbox` gains rows (a durable batch to send). outbox_changed: tokio::sync::watch::Sender, + /// The docs whose outbox rows were added since [`Self::take_outbox_changes`]. + outbox_changed_docs: Mutex>, } impl DocsStore { @@ -273,6 +275,7 @@ impl DocsStore { recovery, sync_jobs_changed: tokio::sync::watch::Sender::new(0), outbox_changed: tokio::sync::watch::Sender::new(0), + outbox_changed_docs: Mutex::new(HashSet::new()), }) } @@ -292,10 +295,30 @@ impl DocsStore { self.sync_jobs_changed.send_modify(|v| *v = v.wrapping_add(1)); } - fn note_outbox_changed(&self) { + fn note_outbox_changed(&self, doc_id: &str) { + { + let mut docs = self + .outbox_changed_docs + .lock() + .unwrap_or_else(PoisonError::into_inner); + if !docs.contains(doc_id) { + docs.insert(doc_id.to_string()); + } + } self.outbox_changed.send_modify(|v| *v = v.wrapping_add(1)); } + /// Which docs had outbox rows added since the last call: the change + /// signal ([`Self::watch_outbox`]) says only that some did. + pub fn take_outbox_changes(&self) -> HashSet { + std::mem::take( + &mut *self + .outbox_changed_docs + .lock() + .unwrap_or_else(PoisonError::into_inner), + ) + } + /// What this open found and did, when the file was damaged. pub fn recovery(&self) -> Option<&crate::store_recovery::Recovery> { self.recovery.as_ref() @@ -342,7 +365,7 @@ impl DocsStore { params![doc_id, batch_id, bytes], ); if matches!(result, Ok(n) if n > 0) { - self.note_outbox_changed(); + self.note_outbox_changed(doc_id); } if result.is_err() { self.failed_publications @@ -459,6 +482,26 @@ impl DocsStore { store_blocking(|| self.acknowledge_chat_update_blocking(doc_id, batch_id)) } + /// [`Self::acknowledge_chat_update`] for several rows, in one transaction. + pub fn acknowledge_chat_updates(&self, doc_id: &str, batch_ids: &[String]) -> Result<(), StoreError> { + if batch_ids.is_empty() { + return Ok(()); + } + store_blocking(|| { + let mut conn = self.conn(); + let tx = conn.transaction()?; + { + let mut delete = + tx.prepare_cached("DELETE FROM chat_outbox WHERE doc_id=?1 AND batch_id=?2")?; + for batch_id in batch_ids { + delete.execute(params![doc_id, batch_id])?; + } + } + tx.commit()?; + Ok(()) + }) + } + fn acknowledge_chat_update_blocking( &self, doc_id: &str, @@ -518,7 +561,7 @@ impl DocsStore { } tx.commit()?; if !updates.is_empty() { - self.note_outbox_changed(); + self.note_outbox_changed(doc_id); } Ok(()) } diff --git a/crates/sync/src/wake.rs b/crates/sync/src/wake.rs index e7a5d4f45..79261244f 100644 --- a/crates/sync/src/wake.rs +++ b/crates/sync/src/wake.rs @@ -38,40 +38,70 @@ static PATH_OFFLINE: AtomicBool = AtomicBool::new(false); /// See [`set_resting`]. static RESTING: OnceLock> = OnceLock::new(); -/// Subscribe to system-wake events (the detector spawns on first call). +/// Set once the OS delivers wake events itself ([`use_os_wake_events`]). +static OS_EVENTS: AtomicBool = AtomicBool::new(false); + +/// Subscribe to system-wake events (the detector spawns on first call, unless +/// the OS delivers them: [`use_os_wake_events`]). pub fn subscribe() -> broadcast::Receiver<()> { CHANNEL .get_or_init(|| { let (tx, _) = broadcast::channel(4); - let detector = tx.clone(); - tokio::spawn(async move { - let mut interval = tokio::time::interval(TICK); - interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - interval.tick().await; // consume the immediate first tick - let mut wall = SystemTime::now(); - let mut mono = Instant::now(); - loop { - interval.tick().await; - let wall_elapsed = SystemTime::now() - .duration_since(wall) - .unwrap_or(Duration::ZERO); - let mono_elapsed = mono.elapsed(); - if wall_elapsed > mono_elapsed + JUMP_THRESHOLD { - tracing::info!( - slept_s = wall_elapsed.as_secs(), - "wake: system resumed from suspend" - ); - let _ = detector.send(()); - } - wall = SystemTime::now(); - mono = Instant::now(); - } - }); + if !OS_EVENTS.load(Ordering::Acquire) { + spawn_detector(tx.clone()); + } tx }) .subscribe() } +/// The process's OS tells it of each wake ([`notify_wake`]), so the +/// clock-comparing detector, which wakes the process every [`TICK`] to look, +/// isn't needed: it doesn't start, or stops at its next tick. Call before the +/// first [`subscribe`] where possible (the macOS background engine registers +/// for IOKit power notifications before its engine starts). +pub fn use_os_wake_events() { + OS_EVENTS.store(true, Ordering::Release); +} + +/// The system just woke (from the OS's own notification): what the detector +/// would have broadcast. +pub fn notify_wake() { + tracing::info!("wake: system resumed from sleep"); + if let Some(tx) = CHANNEL.get() { + let _ = tx.send(()); + } +} + +fn spawn_detector(detector: broadcast::Sender<()>) { + tokio::spawn(async move { + let mut interval = tokio::time::interval(TICK); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + interval.tick().await; // consume the immediate first tick + let mut wall = SystemTime::now(); + let mut mono = Instant::now(); + loop { + interval.tick().await; + if OS_EVENTS.load(Ordering::Acquire) { + return; + } + let wall_elapsed = SystemTime::now() + .duration_since(wall) + .unwrap_or(Duration::ZERO); + let mono_elapsed = mono.elapsed(); + if wall_elapsed > mono_elapsed + JUMP_THRESHOLD { + tracing::info!( + slept_s = wall_elapsed.as_secs(), + "wake: system resumed from suspend" + ); + let _ = detector.send(()); + } + wall = SystemTime::now(); + mono = Instant::now(); + } + }); +} + fn online_channel() -> &'static broadcast::Sender<()> { ONLINE.get_or_init(|| broadcast::channel(4).0) }