diff --git a/Cargo.lock b/Cargo.lock index 1b3e8f5f7..4d3ae0f14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13347,7 +13347,7 @@ dependencies = [ [[package]] name = "zatex" -version = "0.3.4" +version = "0.3.7" dependencies = [ "tiny-skia", "ttf-parser", @@ -13598,7 +13598,7 @@ dependencies = [ [[package]] name = "zeron" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "clap", @@ -13617,14 +13617,13 @@ dependencies = [ "zeron-mcp", "zeron-notify", "zeron-rpc", - "zeron-sync", "zeron-ui", "zeron-update", ] [[package]] name = "zeron-audio" -version = "0.3.4" +version = "0.3.7" dependencies = [ "base64 0.22.1", "cpal 0.18.2", @@ -13636,7 +13635,7 @@ dependencies = [ [[package]] name = "zeron-cli" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "clap", @@ -13652,7 +13651,7 @@ dependencies = [ [[package]] name = "zeron-client" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "async-trait", @@ -13683,7 +13682,7 @@ dependencies = [ [[package]] name = "zeron-doc" -version = "0.3.4" +version = "0.3.7" dependencies = [ "chrono", "loro", @@ -13698,7 +13697,7 @@ dependencies = [ [[package]] name = "zeron-engine" -version = "0.3.4" +version = "0.3.7" dependencies = [ "alacritty_terminal", "anyhow", @@ -13758,7 +13757,7 @@ dependencies = [ [[package]] name = "zeron-harness" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "async-trait", @@ -13792,7 +13791,7 @@ dependencies = [ [[package]] name = "zeron-launch" -version = "0.3.4" +version = "0.3.7" dependencies = [ "libc", "tempfile", @@ -13801,24 +13800,20 @@ dependencies = [ [[package]] name = "zeron-markdown" -version = "0.3.4" +version = "0.3.7" dependencies = [ "pulldown-cmark", ] [[package]] name = "zeron-mcp" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "async-trait", "base64 0.22.1", - "bytes", "chrono", "futures", - "http-body-util", - "hyper", - "hyper-util", "serde", "serde_json", "tempfile", @@ -13834,7 +13829,7 @@ dependencies = [ [[package]] name = "zeron-mobile" -version = "0.3.4" +version = "0.3.7" dependencies = [ "async-trait", "serde", @@ -13861,14 +13856,14 @@ dependencies = [ [[package]] name = "zeron-mobile-bindgen" -version = "0.3.4" +version = "0.3.7" dependencies = [ "uniffi", ] [[package]] name = "zeron-notify" -version = "0.3.4" +version = "0.3.7" dependencies = [ "objc", "sha2", @@ -13877,11 +13872,11 @@ dependencies = [ [[package]] name = "zeron-orb" -version = "0.3.4" +version = "0.3.7" [[package]] name = "zeron-plugin-guard" -version = "0.3.4" +version = "0.3.7" dependencies = [ "serde_json", "tempfile", @@ -13889,7 +13884,7 @@ dependencies = [ [[package]] name = "zeron-preview" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "async-trait", @@ -13919,11 +13914,11 @@ dependencies = [ [[package]] name = "zeron-preview-label" -version = "0.3.4" +version = "0.3.7" [[package]] name = "zeron-proto" -version = "0.3.4" +version = "0.3.7" dependencies = [ "chrono", "pulldown-cmark", @@ -13934,7 +13929,7 @@ dependencies = [ [[package]] name = "zeron-rpc" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "async-trait", @@ -13955,7 +13950,7 @@ dependencies = [ [[package]] name = "zeron-sync" -version = "0.3.4" +version = "0.3.7" dependencies = [ "base64 0.22.1", "block2 0.6.2", @@ -13981,7 +13976,7 @@ dependencies = [ [[package]] name = "zeron-syntax" -version = "0.3.4" +version = "0.3.7" dependencies = [ "thiserror 2.0.20", "tree-sitter", @@ -14016,14 +14011,14 @@ dependencies = [ [[package]] name = "zeron-term" -version = "0.3.4" +version = "0.3.7" dependencies = [ "alacritty_terminal", ] [[package]] name = "zeron-term-link" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "async-trait", @@ -14040,7 +14035,7 @@ dependencies = [ [[package]] name = "zeron-text" -version = "0.3.4" +version = "0.3.7" dependencies = [ "core-foundation 0.10.1", "core-graphics 0.24.0", @@ -14057,7 +14052,7 @@ dependencies = [ [[package]] name = "zeron-theme" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "clap", @@ -14072,7 +14067,7 @@ dependencies = [ [[package]] name = "zeron-ui" -version = "0.3.4" +version = "0.3.7" dependencies = [ "alacritty_terminal", "anyhow", @@ -14155,7 +14150,7 @@ dependencies = [ [[package]] name = "zeron-update" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "futures", @@ -14172,11 +14167,11 @@ dependencies = [ [[package]] name = "zeron-veil" -version = "0.3.4" +version = "0.3.7" [[package]] name = "zeron-visual" -version = "0.3.4" +version = "0.3.7" dependencies = [ "base64 0.22.1", "gltf", @@ -14188,7 +14183,7 @@ dependencies = [ [[package]] name = "zeron-visual-gpu" -version = "0.3.4" +version = "0.3.7" dependencies = [ "image", "pollster 0.4.0", @@ -14199,7 +14194,7 @@ dependencies = [ [[package]] name = "zeron-voice" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "cpal 0.17.3", @@ -14218,7 +14213,7 @@ dependencies = [ [[package]] name = "zeron-voice-media" -version = "0.3.4" +version = "0.3.7" dependencies = [ "async-trait", "semver", @@ -14233,7 +14228,7 @@ dependencies = [ [[package]] name = "zeron-voice-session" -version = "0.3.4" +version = "0.3.7" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index a39b93125..7865ac29a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ members = [ ] [workspace.package] -version = "0.3.4" +version = "0.3.7" edition = "2024" license = "MIT" publish = false diff --git a/apps/zeron/Cargo.toml b/apps/zeron/Cargo.toml index 036c03949..2fda76d93 100644 --- a/apps/zeron/Cargo.toml +++ b/apps/zeron/Cargo.toml @@ -27,7 +27,6 @@ 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 fb4cfb71b..b5626acb9 100644 --- a/apps/zeron/src/background.rs +++ b/apps/zeron/src/background.rs @@ -55,9 +55,6 @@ 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 || { @@ -78,26 +75,9 @@ 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, AtomicPtr, AtomicU32, Ordering}; + use std::sync::atomic::{AtomicBool, 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" { @@ -106,100 +86,39 @@ 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 CFRunLoopSourceCreate( + fn CFAbsoluteTimeGetCurrent() -> f64; + fn CFRunLoopTimerCreate( allocator: *const c_void, + fire_date: f64, + interval: f64, + flags: u64, order: isize, - context: *mut SourceContext, + callout: extern "C" fn(*mut c_void, *mut c_void), + context: *mut c_void, ) -> *mut c_void; - fn CFRunLoopAddSource(run_loop: *mut c_void, source: *mut c_void, mode: *const c_void); - fn CFRunLoopSourceSignal(source: *mut c_void); + fn CFRunLoopAddTimer(run_loop: *mut c_void, timer: *mut c_void, mode: *const 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; - } + extern "C" fn tick(_timer: *mut c_void, _info: *mut c_void) {} - // 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). + /// 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. pub(super) fn run() { unsafe { let main = CFRunLoopGetMain(); - 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); + let timer = CFRunLoopTimerCreate( + std::ptr::null(), + CFAbsoluteTimeGetCurrent() + 1.0, + 1.0, + 0, + 0, + tick, + std::ptr::null_mut(), + ); + CFRunLoopAddTimer(main, timer, kCFRunLoopCommonModes); while !DONE.load(Ordering::SeqCst) { - // (Returns after each source served, so `DONE` is read then.) - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, 1); + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, 0); } } } @@ -207,12 +126,6 @@ mod main_loop { /// End [`run`] (any thread). pub(super) fn finish() { DONE.store(true, Ordering::SeqCst); - let source = STOP.load(Ordering::SeqCst); - unsafe { - if !source.is_null() { - CFRunLoopSourceSignal(source); - } - CFRunLoopWakeUp(CFRunLoopGetMain()); - } + unsafe { CFRunLoopWakeUp(CFRunLoopGetMain()) }; } } diff --git a/apps/zeron/src/bin/zeron-engine.rs b/apps/zeron/src/bin/zeron-engine.rs index 0b6648a1d..76d9bcbc1 100644 --- a/apps/zeron/src/bin/zeron-engine.rs +++ b/apps/zeron/src/bin/zeron-engine.rs @@ -2,9 +2,8 @@ //! 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-proxy` -//! (an agent's Zeron MCP tools on stdio, served by the engine), `mcp` (the -//! standalone Zeron MCP server) and `--noop-browser` (browser +//! It also answers what the engine runs its own executable for: `mcp` (the +//! Zeron MCP server injected into agents) 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`, @@ -18,9 +17,6 @@ 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 30ca2a5f1..0a3d049b8 100644 --- a/apps/zeron/src/main.rs +++ b/apps/zeron/src/main.rs @@ -12,11 +12,6 @@ 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 84c212da1..d096e16e9 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" dependencies = [ "ahash", "android-activity", @@ -7067,7 +7067,7 @@ dependencies = [ [[package]] name = "zatex" -version = "0.3.4" +version = "0.3.7" dependencies = [ "ttf-parser", ] @@ -7233,7 +7233,7 @@ checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" [[package]] name = "zeron-cli" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "clap", @@ -7248,7 +7248,7 @@ dependencies = [ [[package]] name = "zeron-launch" -version = "0.3.4" +version = "0.3.7" dependencies = [ "libc", "windows-sys 0.61.2", @@ -7256,7 +7256,7 @@ dependencies = [ [[package]] name = "zeron-notify" -version = "0.3.4" +version = "0.3.7" dependencies = [ "objc", "sha2", @@ -7265,18 +7265,18 @@ dependencies = [ [[package]] name = "zeron-plugin-guard" -version = "0.3.4" +version = "0.3.7" dependencies = [ "serde_json", ] [[package]] name = "zeron-preview-label" -version = "0.3.4" +version = "0.3.7" [[package]] name = "zeron-proto" -version = "0.3.4" +version = "0.3.7" dependencies = [ "chrono", "pulldown-cmark 0.12.2", @@ -7287,7 +7287,7 @@ dependencies = [ [[package]] name = "zeron-theme" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "clap", @@ -7301,7 +7301,7 @@ dependencies = [ [[package]] name = "zeron-update" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "futures", @@ -7318,7 +7318,7 @@ dependencies = [ [[package]] name = "zeron-visual" -version = "0.3.4" +version = "0.3.7" dependencies = [ "base64 0.22.1", "gltf", @@ -7330,7 +7330,7 @@ dependencies = [ [[package]] name = "zeron-visual-gpu" -version = "0.3.4" +version = "0.3.7" dependencies = [ "pollster", "tracing", @@ -7340,7 +7340,7 @@ dependencies = [ [[package]] name = "zeron-voice" -version = "0.3.4" +version = "0.3.7" dependencies = [ "anyhow", "cpal", @@ -7456,7 +7456,7 @@ dependencies = [ [[package]] name = "zgx" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" dependencies = [ "zgx", ] @@ -7528,7 +7528,7 @@ dependencies = [ [[package]] name = "zgx-cosmic-text" version = "0.19.14" -source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" [[package]] name = "zgx-gpu" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" dependencies = [ "bstr", "either", @@ -7675,7 +7675,7 @@ dependencies = [ [[package]] name = "zgx-run" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" dependencies = [ "skrifa", "yazi", @@ -7722,7 +7722,7 @@ dependencies = [ [[package]] name = "zgx-taffy" version = "0.9.2" -source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" dependencies = [ "arrayvec", "grid", @@ -7733,7 +7733,7 @@ dependencies = [ [[package]] name = "zgx-terminal" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" 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=4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88#4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" +source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" dependencies = [ "zune-core", ] diff --git a/apps/zgx/Cargo.toml b/apps/zgx/Cargo.toml index 96ab0ff4e..9a30cbf91 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88" } +zgx = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } # `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 = "4c4e0e2eb5f019884ba7fa6cd # 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["mimalloc", "terminal", "webkitgtk"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 = "4c4e0e2eb5f019884 # 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["inspect", "i18n", "scene3d"] } +zgx-script = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", default-features = false } +zgx-gpu = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", default-features = false, features = ["fetch"] } +zgx-run = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["ghostty"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["ghostty"] } # Windows' Transparency effects (src/transparency.rs), read and watched in # the registry, until zgx reports them (zgx-asks/windows-transparency); a @@ -172,8 +172,8 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "4c4e0e2eb5f019884 # DirectComposition tree of the window's own. The runtime ships with Windows # 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 = "4c4e0e2eb5f019884ba7fa6cd95a5d6caa229a88", features = ["webview2"] } +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_System_Time", "Win32_UI_WindowsAndMessaging"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 = "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" } +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" } # zgx is slow unoptimized (its own setting): optimize dependencies, keep # this crate quick to build. diff --git a/apps/zgx/docs/plugins.md b/apps/zgx/docs/plugins.md index 6ad2dd6bd..5ac07018a 100644 --- a/apps/zgx/docs/plugins.md +++ b/apps/zgx/docs/plugins.md @@ -52,7 +52,8 @@ Your own plugins are signed as yours, and re-signed and reloaded each time you s | `navigate` | open chats and pages (`zeron.open`) | | `links.open` | open http(s) links (`zeron.open_url`), where you open links (Settings → Links) | | `notifications` | send you notifications (`zeron.notify`), as Zeron's own: Settings → Notifications decides whether they show, a few a minute at most | -| `compose` | put text in the message box (`zeron.compose`), or a review comment on it (`zeron.compose_comment`); you still send it | +| `compose` | put text in the message box, after what's there or in place of its words (`zeron.compose`), or a review comment on it (`zeron.compose_comment`); you still send it | +| `draft` | read the message box's draft as you type it (`zeron.draft`), or any chat's (`zeron.draft_of`), and take it whole, put it back, swap it or let it go (`zeron.take_draft`, `put_draft`, `swap_draft`, `drop_draft`). See [The composer's draft](#the-composers-draft) | | `github`, `github.write` | read (and write) GitHub through Zeron's engine (older calls; prefer `commands` with `gh`) | | `network` | the internet: `ui.fetch`, and remote images (`ui.image { src = "https://…" }`). See [Reaching the network](#reaching-the-network) | | `clipboard`, `files`, `web`, `windows` | zgx's clipboard, file dialogs, web views, windows | @@ -213,9 +214,13 @@ Zeron draws that row on your settings page: "Use as wallpaper", and while it's o | call | what | needs | |---|---|---| | `zeron.open("home" \| { session = id } \| { settings = page })` | open a page | `navigate` | -| `zeron.compose(text)` | text into the message box | `compose` | +| `zeron.compose(text, { replace?, session? })` | text into a chat's draft: after what's there (on a line of its own), or in place of its words (`replace = true`, its chips kept); the chat shown's draft, or `session`'s ([below](#the-composers-draft)) | `compose` | | `zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? }` | a review comment on a file or a diff, as a chip on a chat's draft (below) | `compose` | | `zeron.composed()` | in a build: your comments still on a draft, by `key`: `{ session, hovered }` | `compose` | +| `zeron.draft()` | in a build: the message box's draft, `{ session?, text, attachments = { { kind, name } } }` (below) | `draft` | +| `zeron.take_draft(key)`, `zeron.put_draft(key)`, `zeron.swap_draft(key)`, `zeron.drop_draft(key)` | the draft taken whole and kept by Zeron under your key; put back; traded with what the key keeps; let go (below) | `draft` | +| `zeron.drafts()` | in a build: what Zeron keeps for you, by key: `{ session?, text, attachments, at }` | `draft` | +| `zeron.draft_of(session)` | a promise of chat `session`'s draft as it is now, as `zeron.draft()` says it (`""`: a new chat's) | `draft` | | `zeron.toast(text)` | a short note at the window's foot | (always) | | `zeron.need("panel").open_tab { kind, title, icon?, data? }` | a tab of yours in the chat's panel; asked again, the one there is brought forward (found by `data.key` when you give one, else by its title) | `panel.tabs` | | `zeron.tab(kind, function(tab) return node end)` | how a kind of your tabs draws (`tab` = `{ kind, title, data }`) | `panel.tabs` | @@ -274,6 +279,48 @@ Comments on the diff (each cites the file and line it belongs to; L = line numbe Give each a `key` of your own: `zeron.composed()` lists yours still on a draft by it (taken off or sent, gone), and says which one's chip is under the pointer (`hovered`), so the lines it was left on can keep a mark and light up as its chip is pointed at. Zeron's Git plugin does all of this in its Changes (`plugins-builtin/git/changes.luau`): the diff's + (or a drag from it over lines) opens `ds/comment`'s composer under them, using `ds/git_changes`'s `on_comment`, `ranges` and `marks`. +### The composer's draft + +With `draft`, a plugin reads what you're typing and can set it aside and bring it back: a stash for a prompt written in the wrong chat, a counter of its length, a bar that says what it carries. + +- **`zeron.draft()`**, read in a build or an effect, is the draft shown as it changes: `session` (the chat it's for; nil on a new chat), `text` (the words as typed) and `attachments`, each chip's `kind` (`"file"`, `"image"`, `"chat"`, `"comment"`, …) and `name`. A file's path is never in it. +- **`zeron.take_draft(key)`** takes the draft shown whole (its words, its chips, the passages quoted in it) and clears the composer. Zeron keeps it, under your `key` (1 to 64 characters), so your plugin never holds a file's path. An empty draft isn't taken; taking under a key that's kept replaces what it kept. Up to 16 at once. +- **`zeron.put_draft(key)`** puts what `key` kept back on the draft shown, in whichever chat is open, and lets it go. Onto an empty composer it's as it was taken; onto words already there, its words follow after a blank line and its chips after theirs. +- **`zeron.swap_draft(key)`** trades the draft shown with what `key` keeps. Either may be empty, so one call sets the draft aside (nothing kept), brings it back (an empty composer), or swaps the two. +- **`zeron.drop_draft(key)`** lets what `key` kept go. +- **`zeron.drafts()`**, read in a build, is what Zeron keeps for you, by key: `{ session?, text, attachments, at }` (`at` as `os.time()`). Drafts are kept while Zeron runs, as chats' own drafts are. +- **`zeron.draft_of(session)`** reads another chat's draft once, shown or not: a promise of `{ session?, text, attachments }`, as `zeron.draft()` says it (`""` for a new chat's, with no session). + +With `compose` too, a plugin can change the words in place: **`zeron.compose(text, { replace = true })`** puts `text` in place of the draft's words, leaving its chips where they are, and `session` names another chat's draft. A rewrite or a snippet that expands is a read and a replace: + +```lua +-- ("ui.add", "draft", "compose"): /bug alone in the box becomes the template. +ui.effect(function() + local d = zeron.draft() + if d and d.text == "/bug" then + zeron.compose("## What happened\n\n## What I expected\n\n## Steps\n", { replace = true, session = d.session }) + end +end) +``` + +Nothing is sent: the person sees the new words in the composer, and sends or edits them. `zeron.compose` takes 20,000 characters at most. + +Each is done in the order you ask. A prompt stash is one shortcut and one chip: + +```lua +-- ⌘⇧S (Ctrl+Shift+S): set the draft aside, bring it back, or swap the two. +ui.command { id = "stash", title = "Stash prompt", key = "cmd+shift+s", run = function() + zeron.swap_draft("stash") +end } + +-- While one is kept, a chip over the composer: click to bring it back. +zeron.place { at = "#chat-composer", before = function(): Node + return ui.show(function(): boolean + return zeron.drafts().stash ~= nil + end, ui.button { "Stashed", on_click = function() zeron.swap_draft("stash") end }) +end } +``` + ## Programs and a database A plugin can do real work of its own: run the command-line tools you already use, and keep what it learns. diff --git a/apps/zgx/plugins-builtin/git/zeron.luau b/apps/zgx/plugins-builtin/git/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-builtin/git/zeron.luau +++ b/apps/zgx/plugins-builtin/git/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-builtin/github/zeron.luau b/apps/zgx/plugins-builtin/github/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-builtin/github/zeron.luau +++ b/apps/zgx/plugins-builtin/github/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau b/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau +++ b/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/account-pin/zeron.luau b/apps/zgx/plugins-dev/account-pin/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/account-pin/zeron.luau +++ b/apps/zgx/plugins-dev/account-pin/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/account-rotate/zeron.luau b/apps/zgx/plugins-dev/account-rotate/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/account-rotate/zeron.luau +++ b/apps/zgx/plugins-dev/account-rotate/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/checkout-status/zeron.luau b/apps/zgx/plugins-dev/checkout-status/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/checkout-status/zeron.luau +++ b/apps/zgx/plugins-dev/checkout-status/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/compact-sidebar/zeron.luau b/apps/zgx/plugins-dev/compact-sidebar/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/compact-sidebar/zeron.luau +++ b/apps/zgx/plugins-dev/compact-sidebar/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/composer-cat/zeron.luau b/apps/zgx/plugins-dev/composer-cat/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/composer-cat/zeron.luau +++ b/apps/zgx/plugins-dev/composer-cat/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/focus/zeron.luau b/apps/zgx/plugins-dev/focus/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/focus/zeron.luau +++ b/apps/zgx/plugins-dev/focus/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/keep-going/zeron.luau b/apps/zgx/plugins-dev/keep-going/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/keep-going/zeron.luau +++ b/apps/zgx/plugins-dev/keep-going/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/md-hello/zeron.luau b/apps/zgx/plugins-dev/md-hello/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/md-hello/zeron.luau +++ b/apps/zgx/plugins-dev/md-hello/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/notes-tool/zeron.luau b/apps/zgx/plugins-dev/notes-tool/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/notes-tool/zeron.luau +++ b/apps/zgx/plugins-dev/notes-tool/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/ping/zeron.luau b/apps/zgx/plugins-dev/ping/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/ping/zeron.luau +++ b/apps/zgx/plugins-dev/ping/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/session-status/zeron.luau b/apps/zgx/plugins-dev/session-status/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/session-status/zeron.luau +++ b/apps/zgx/plugins-dev/session-status/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/session-words/zeron.luau b/apps/zgx/plugins-dev/session-words/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/session-words/zeron.luau +++ b/apps/zgx/plugins-dev/session-words/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/settings-demo/zeron.luau b/apps/zgx/plugins-dev/settings-demo/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-dev/settings-demo/zeron.luau +++ b/apps/zgx/plugins-dev/settings-demo/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-sdk/zeron.luau b/apps/zgx/plugins-sdk/zeron.luau index c5f896393..553b6cc16 100644 --- a/apps/zgx/plugins-sdk/zeron.luau +++ b/apps/zgx/plugins-sdk/zeron.luau @@ -69,13 +69,31 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text) text into the message box, for you to send ("compose") +-- zeron.compose(text, { replace?, session? }) +-- text into the message box, for you to send: after +-- what's there, or in place of its words (`replace = +-- true`, its chips kept); the chat shown's draft, or +-- `session`'s ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") +-- zeron.draft() the composer's draft as you type it: { session?, +-- text, attachments = { { kind, name } } }, reactive +-- ("draft") +-- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), +-- zeron.drop_draft(key) +-- the draft taken whole (words, chips, quotes) and +-- kept by Zeron under your key, the composer +-- cleared; put back on the draft shown; the two +-- traded; let go ("draft") +-- zeron.drafts() what Zeron keeps for you, by key: { session?, text, +-- attachments, at }, reactive ("draft") +-- zeron.draft_of(session) +-- a promise of another chat's draft, as `draft()` +-- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1343,8 +1361,16 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - function M.compose(text: string) - ui.invoke("zeron.compose.insert", text) + -- `text` into a chat's draft, for the person to send ("compose"): after + -- what's there, on a line of its own; with `replace = true`, in place of + -- its words (what it carries stays). The chat shown's draft, or + -- `session`'s ("" a new chat's). + function M.compose(text: string, opts: { replace: boolean?, session: string? }?) + if opts and (opts.replace or opts.session) then + ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) + else + ui.invoke("zeron.compose.insert", text) + end end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1374,6 +1400,57 @@ return function(id: string): any return composed:get() or {} end + -- The composer's draft as you type it: the chat it's for (nil, a new + -- chat), its words, and what it carries, each chip's kind ("file", + -- "image", "chat", …) and name, never a file's path. Read in a build or + -- an effect: it changes as the draft does. Nil until Zeron says. + -- (Reached on first read: only a plugin allowed "draft" may.) + local draft: any = nil + function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? + draft = draft or ui.shared("zeron.draft", nil :: any) + return draft:get() + end + + -- The draft shown, taken whole (its words, its chips, the passages + -- quoted in it) and kept by Zeron under `key`, yours, the composer + -- cleared; what that key kept before is replaced. An empty draft isn't + -- taken. Up to 16 kept at once. + function M.take_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "take", key = key }) + end + + -- What `key` kept, put back on the draft shown as it was taken (after + -- what's there, if the composer isn't empty), and no longer kept. + function M.put_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "put", key = key }) + end + + -- The draft shown and what `key` keeps, traded: either may be empty, so + -- one call sets the draft aside, brings it back, or swaps the two. + function M.swap_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) + end + + -- What `key` kept, let go of. + function M.drop_draft(key: string) + ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) + end + + -- What Zeron keeps for you, by key: the chat each was taken from, its + -- words, what it carries (as `draft()` says it) and when (os.time). + -- Read in a build: it changes as you take and put back. + local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) + function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } + return drafts:get() or {} + end + + -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` + -- follows the one shown). A promise of { session?, text, attachments } + -- as `draft()` says it ("" a new chat's draft, with no session). + function M.draft_of(session: string): any + return ui.call(`{prefix}.draft_of`, session) + end + function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/src/agents.rs b/apps/zgx/src/agents.rs index af62053ae..00506865c 100644 --- a/apps/zgx/src/agents.rs +++ b/apps/zgx/src/agents.rs @@ -411,6 +411,24 @@ pub fn install(host: &ScriptHost) { a.add_key(id, provider, key, name).await } }); + // An API key for Cloud's agents (Settings → Providers with Cloud + // shown): kept by the vault, authorized for the account's Cloud device + // (`device`), the key every Cloud machine uses from its next turn. + // Rejects with the engine's words, which never hold the key. + let a = agents.clone(); + host.async_action("agents.cloud_key", move |args| { + let a = a.clone(); + let provider = args.field("provider").map(text).unwrap_or_default(); + let key = args.field("key").map(text).unwrap_or_default().trim().to_string(); + let device = args.field("device").map(text).unwrap_or_default(); + async move { + crate::plugins::preview::refuse("agents.cloud_key")?; + let params = cloud_key_params(&provider, &key, &device)?; + engine::get().call("VaultPutApiKey", params).await.map_err(|error| engine::outcome(&error).to_string())?; + a.load_accounts(true).await; + Ok(Value::Nil) + } + }); // An API key's name (no secret: on whichever device shows it), rejecting // with the engine's words. let a = agents.clone(); @@ -1948,6 +1966,20 @@ fn epoch_secs(s: &str) -> Option { mod tests { use super::*; + /// A Cloud API key goes to the vault for the account's Cloud device: + /// Anthropic's or OpenAI's only, a real key, Cloud on. + #[test] + fn a_cloud_key_is_one_cloud_runs_for_its_device() { + let ok = cloud_key_params("anthropic-key", "sk-ant-123", "cloud-acct").unwrap(); + assert_eq!(ok, json!({ "provider": "anthropic-key", "key": "sk-ant-123", "authorizedDevices": ["cloud-acct"] })); + assert!(cloud_key_params("openai-key", "sk-1", "d").is_ok()); + assert!(cloud_key_params("claude", "sk", "d").is_err(), "a login isn't a key"); + assert!(cloud_key_params("anthropic-key", "", "d").is_err()); + assert!(cloud_key_params("anthropic-key", "a\nb", "d").is_err()); + assert!(cloud_key_params("anthropic-key", &"k".repeat(5000), "d").is_err()); + assert!(cloud_key_params("anthropic-key", "sk", "").is_err(), "Cloud off"); + } + /// Pi signed in to Claude, or another login put in use: Pi's models are /// looked up again. Usage moving, or another agent's, isn't Pi's change. #[test] @@ -2228,6 +2260,22 @@ pub(crate) fn shown_target() -> Option { } /// `params` addressed to the device Providers shows. +/// `VaultPutApiKey`'s params for a Cloud API key: Anthropic's or OpenAI's +/// alone, a key that's there and no longer than any key is, for the +/// account's Cloud device. Else why not. +pub(crate) fn cloud_key_params(provider: &str, key: &str, device: &str) -> Result { + if !matches!(provider, "anthropic-key" | "openai-key") { + return Err("Cloud takes Anthropic and OpenAI keys".into()); + } + if key.is_empty() || key.len() > 4096 || key.chars().any(char::is_control) { + return Err("That isn't an API key".into()); + } + if device.is_empty() { + return Err("Turn Cloud on in Settings → Cloud first".into()); + } + Ok(json!({ "provider": provider, "key": key, "authorizedDevices": [device] })) +} + pub(crate) fn addressed_to_shown(params: Json) -> Json { addressed_to(shown_target().as_deref(), params) } diff --git a/apps/zgx/src/chat.rs b/apps/zgx/src/chat.rs index e2101a68c..b8e5734fe 100644 --- a/apps/zgx/src/chat.rs +++ b/apps/zgx/src/chat.rs @@ -625,6 +625,19 @@ impl Pending { } } +/// The transcript's rows with each id once, the first kept: its list is keyed +/// by `id`, and a key shown twice is one it can't place (zgx's virtual list +/// panics, and the transcript stops updating until the chat is opened +/// again). A prompt pending here comes after the transcript's own rows, so +/// the engine's copy of it is the one kept. +fn one_row_per_id(list: &mut Vec) { + let mut ids: std::collections::HashSet> = std::collections::HashSet::with_capacity(list.len()); + list.retain(|row| match row.field("id") { + Some(Value::Str(id)) => ids.insert(id.clone()), + _ => true, + }); +} + /// An edit lease on a queued message (the engine's `BeginQueuedMessageEdit`). #[derive(Clone)] struct Lease { @@ -2077,6 +2090,9 @@ impl View { } } open.heard = true; + // (A prompt sent from here that the tail brought: the + // engine's copy now shows, so this one goes.) + self.drop_arrived_prompts(open); break 'applied Ok(Change::Rows); } else { open.has_whole = false; @@ -2125,14 +2141,7 @@ impl View { } if result.is_ok() { open.heard = true; - let ids: Vec<&str> = open.entries.iter().map(|e| &*e.id).collect(); - let mut pending = self.pending.borrow_mut(); - if let Some(prompts) = pending.get_mut(&open.id) { - prompts.retain(|p| !ids.contains(&p.id.as_str())); - if prompts.is_empty() { - pending.remove(&open.id); - } - } + self.drop_arrived_prompts(open); } result }; @@ -2151,6 +2160,20 @@ impl View { } } + /// Prompts sent from here that the transcript now has: the engine's copy + /// shows from now on, so this device's own goes (both would be two rows + /// with one id, which the transcript's list can't show). + fn drop_arrived_prompts(&self, open: &Open) { + let ids: std::collections::HashSet<&str> = open.entries.iter().map(|e| &*e.id).collect(); + let mut pending = self.pending.borrow_mut(); + if let Some(prompts) = pending.get_mut(&open.id) { + prompts.retain(|p| !ids.contains(p.id.as_str())); + if prompts.is_empty() { + pending.remove(&open.id); + } + } + } + /// What another view of this chat has built, for the entries here that /// are the same there: a chat in a split is watched by its pane and, when /// focused, by the window too. @@ -2384,6 +2407,7 @@ impl View { if let Some(pending) = self.pending.borrow().get(&open.id) { list.extend(pending.iter().map(Pending::value)); } + one_row_per_id(&mut list); list }; // The list first: a reply that ended takes its words into its row @@ -2567,18 +2591,16 @@ impl View { self.mark_failed(&id, &message_id, None); return Err("missing"); } - // 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); + 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) + })?; + } 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)) @@ -2611,23 +2633,7 @@ impl View { }); } let run = json!({ "kind": "run", "messageId": message_id, "request": request }); - 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, - }; + let sent = 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) @@ -3155,57 +3161,6 @@ 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 }); @@ -6168,8 +6123,19 @@ fn seen_would_change(chat: &Json, session: &Json, now: chrono::DateTime String { + const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + let month = MONTHS.get(month0 as usize).copied().unwrap_or("Jan"); + let twelve = match hour % 12 { + 0 => 12, + h => h, + }; + format!("{month} {day}, {twelve}:{minute:02} {}", if hour < 12 { "AM" } else { "PM" }) +} + +/// [`clock`] for `at` (seconds since 1970) in this machine's time zone. #[cfg(unix)] fn stamp(at: i64) -> Option { #[repr(C)] @@ -6202,21 +6168,47 @@ fn stamp(at: i64) -> Option { } tm.assume_init() }; - let hour = match tm.hour % 12 { - 0 => 12, - h => h, + Some(clock(tm.mon as u32, tm.mday as u32, tm.hour as u32, tm.min as u32)) +} + +/// [`clock`] for `at` (seconds since 1970) in this machine's time zone: on +/// Windows through its own conversion (it has no `localtime_r`). Without +/// it every message's time was missing there, and with it the hover strip +/// that holds Copy. +#[cfg(windows)] +fn stamp(at: i64) -> Option { + use windows_sys::Win32::Foundation::{FILETIME, SYSTEMTIME}; + use windows_sys::Win32::System::Time::{FileTimeToSystemTime, SystemTimeToTzSpecificLocalTime}; + if at <= 0 { + return None; + } + // FILETIME counts 100 ns from 1601-01-01 (11,644,473,600 s before 1970). + let ticks = (at as u64).checked_add(11_644_473_600)?.checked_mul(10_000_000)?; + let file = FILETIME { + dwLowDateTime: ticks as u32, + dwHighDateTime: (ticks >> 32) as u32, }; - const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; - let month = MONTHS.get(tm.mon as usize).copied().unwrap_or("Jan"); - Some(format!( - "{month} {}, {hour}:{:02} {}", - tm.mday, - tm.min, - if tm.hour < 12 { "AM" } else { "PM" } + // SAFETY: both calls read only the structs passed in and write only the + // caller's SYSTEMTIMEs; a null zone means the machine's current one. + let local = unsafe { + let mut utc: SYSTEMTIME = std::mem::zeroed(); + let mut local: SYSTEMTIME = std::mem::zeroed(); + if FileTimeToSystemTime(&file, &mut utc) == 0 + || SystemTimeToTzSpecificLocalTime(std::ptr::null(), &utc, &mut local) == 0 + { + return None; + } + local + }; + Some(clock( + u32::from(local.wMonth).checked_sub(1)?, + u32::from(local.wDay), + u32::from(local.wHour), + u32::from(local.wMinute), )) } -#[cfg(not(unix))] +#[cfg(not(any(unix, windows)))] fn stamp(_at: i64) -> Option { None } @@ -6280,6 +6272,21 @@ mod stream_tests; #[cfg(test)] mod tests { + /// A message's time reads as the shipping app's, on every platform: the + /// formatting, and a real local time from `stamp` (Windows included, + /// where it once was always missing and hid the message's hover strip). + #[test] + fn a_messages_time_reads_like_the_shipping_apps_on_every_platform() { + assert_eq!(super::clock(6, 1, 16, 12), "Jul 1, 4:12 PM"); + assert_eq!(super::clock(0, 9, 0, 5), "Jan 9, 12:05 AM"); + assert_eq!(super::clock(11, 31, 12, 0), "Dec 31, 12:00 PM"); + let at = super::stamp(1_794_600_000).expect("a time for a real moment"); + let (month, rest) = at.split_once(' ').unwrap(); + assert!(["Nov", "Dec"].contains(&month), "{at}"); + assert!(rest.ends_with(" AM") || rest.ends_with(" PM"), "{at}"); + assert_eq!(super::stamp(0), None); + } + /// Opening a chat marks it seen only while that changes something any /// viewport reads (its last message, its terminal's report, the end of /// its sleep): a chat looked at already writes nothing, so its devices' @@ -6662,94 +6669,6 @@ 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. @@ -8155,6 +8074,74 @@ mod tests { assert!(!rows().contains("tail engine.log"), "the whole copy replaces it: {}", rows()); } + /// A prompt sent from here that a reopened chat's opening tail brings + /// shows once: the engine's copy, the one pending here let go. Shown twice + /// it was two rows with one id, and zgx's virtual list panicked + /// ("duplicate virtual list child key"), freezing the transcript. + #[test] + fn a_prompt_the_opening_tail_brings_shows_once() { + use super::{Entry, Open, Pending, View, draft_value}; + use serde_json::json; + use std::cell::{Cell, RefCell}; + use std::rc::Rc; + use zgx_script::{ScriptHost, Value, testing::TestApp}; + let app = TestApp::new(|runtime| ScriptHost::inline(runtime, "app", "return ui.text { \"x\" }")); + let text = |id: &str, words: &str| json!({ "id": format!("{id}p"), "kind": "text", "text": words }); + let prompt = |id: &str| json!({ "id": id, "role": "user", "parts": [text(id, "go")], "createdAt": 1_762_000_000_000_i64 }); + let reply = |id: &str| json!({ "id": id, "role": "assistant", "status": "complete", "parts": [text(id, "done")], "createdAt": 1_762_000_000_000_i64 }); + let held: Vec = [prompt("u0"), reply("a0")].into_iter().map(Entry::new).collect(); + let view = Rc::new(View { + session: app.host.shared("t.session", Value::Nil), + messages: app.host.shared("t.messages", Value::List(Rc::from([]))), + subagents: app.host.shared("t.subagents", Value::List(Rc::from([]))), + draft: app.host.shared("t.draft", draft_value("", "")), + rpc: RefCell::default(), + open: RefCell::new(Some(Open { id: "c".into(), entries: held, heard: true, has_whole: true, ..Open::default() })), + pending: RefCell::default(), + generation: Cell::new(0), + snapshot: Cell::new(false), + }); + view.pending.borrow_mut().insert( + "c".into(), + vec![Pending { + id: "u1".into(), + words: "go".into(), + paths: Vec::new(), + waiting: "sending", + retry: None, + at: None, + held: false, + row: false, + transfers: Vec::new(), + }], + ); + let ids = || -> Vec { + match view.messages.signal().with_untracked(Clone::clone) { + Value::List(rows) => rows.iter().filter_map(|r| r.field("id")).map(super::text).collect(), + other => panic!("rows: {other:?}"), + } + }; + view.publish_messages(); + assert_eq!(ids(), ["u0", "a0", "u1"], "the prompt shows at once, pending here"); + // The engine's opening tail, the prompt in it. + view.heard_frame(json!({ "reset": [reply("a0"), prompt("u1")], "historyPending": true, "count": 2 })); + assert_eq!(ids(), ["u0", "a0", "u1"], "once, the engine's copy"); + assert!(view.pending.borrow().get("c").is_none(), "the copy pending here is let go"); + } + + /// Rows that would share an id (the transcript's list is keyed by it) + /// show once, the first kept: a pending prompt after the transcript's + /// copy of it, or an entry twice over. + #[test] + fn rows_sharing_an_id_show_once() { + use zgx_script::Value; + let row = |id: &str, text: &str| Value::record([("id", Value::from(id)), ("text", Value::from(text))]); + let mut list = vec![row("u1", "engine"), row("a1", "reply"), Value::record([("text", Value::from("no id"))]), row("u1", "here")]; + super::one_row_per_id(&mut list); + let texts: Vec = list.iter().filter_map(|r| r.field("text")).map(super::text).collect(); + assert_eq!(texts, ["engine", "reply", "no id"]); + } + /// A chat opened shows its rows once its transcript has come (this /// device's copy, read first whatever its host's state), not once the /// chats list has it too; "missing" only when the list is known, hasn't diff --git a/apps/zgx/src/engine/rpc.rs b/apps/zgx/src/engine/rpc.rs index 3b0169b6b..da3017c11 100644 --- a/apps/zgx/src/engine/rpc.rs +++ b/apps/zgx/src/engine/rpc.rs @@ -53,39 +53,6 @@ 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, @@ -132,20 +99,16 @@ 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, within).map_err(|error| { + let stream = TcpStream::connect_timeout(&address, DIAL_TIMEOUT).map_err(|error| { if error.kind() == std::io::ErrorKind::ConnectionRefused { DialError::Refused } else { DialError::Other(error.to_string()) } })?; - let _ = stream.set_read_timeout(Some(within)); + let _ = stream.set_read_timeout(Some(DIAL_TIMEOUT)); let _ = stream.set_nodelay(true); let mut request = format!("ws://127.0.0.1:{port}") .into_client_request() @@ -173,50 +136,6 @@ fn dial_within(port: u16, token: Option<&str>, within: Duration) -> Result 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 { @@ -724,54 +643,6 @@ 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/links.rs b/apps/zgx/src/links.rs index 8d647e251..a2486dcce 100644 --- a/apps/zgx/src/links.rs +++ b/apps/zgx/src/links.rs @@ -71,6 +71,41 @@ pub fn open(url: &str) { }); } +/// Banner clicks, taken in this process. The engine posts Zeron's banners, +/// but macOS hands a click to the app its bundle names (Zeron.app's +/// interface, this process), never to the engine that posted it, so the +/// engine's own route (`open -a … zeron://notification/…`) never runs. As +/// Zeron.app's interface always has (crates/ui `notify::on_click`), the +/// click is answered here: Zeron comes forward, on the banner's chat. +pub fn take_banner_clicks() { + zeron_notify::on_click(|target| { + activate(); + clicked(&target); + }); +} + +/// A clicked banner's target (a chat, or agent updates), opened as its +/// `zeron://notification/` link is. +fn clicked(target: &str) { + open(&zeron_notify::link(target)); +} + +/// This app to the front: a banner was clicked. (Main thread: the click +/// comes through the AppKit delegate.) +#[cfg(target_os = "macos")] +fn activate() { + use objc::{class, msg_send, sel, sel_impl}; + unsafe { + let app: *mut objc::runtime::Object = msg_send![class!(NSApplication), sharedApplication]; + if !app.is_null() { + let _: () = msg_send![app, activateIgnoringOtherApps: true]; + } + } +} + +#[cfg(not(target_os = "macos"))] +fn activate() {} + fn tell(target: Target) { let seq = SEQ.with(|s| { s.set(s.get() + 1); @@ -94,6 +129,28 @@ fn tell(target: Target) { mod tests { use super::*; + fn last() -> (Option, Option) { + LAST.with(|l| { + let l = l.borrow(); + let text = |k: &str| match l.field(k) { + Some(Value::Str(s)) => Some(s.to_string()), + Some(Value::Text(s)) => Some(s.to_string()), + _ => None, + }; + (text("chat"), text("settings")) + }) + } + + /// A banner clicked in this process (macOS hands the click to the app, + /// not the engine that posted it) opens its chat as its link would. + #[test] + fn a_clicked_banner_opens_its_chat() { + clicked("chat-42"); + assert_eq!(last(), (Some("chat-42".into()), None)); + clicked(zeron_notify::AGENT_UPDATES_TARGET); + assert_eq!(last(), (None, Some("providers".into()))); + } + #[test] fn banners_and_chat_links_open_where_zeron_s_do() { assert_eq!(parse(&zeron_notify::link("chat-1")), Some((Target::Chat("chat-1".into()), None))); diff --git a/apps/zgx/src/long_thread_tests.rs b/apps/zgx/src/long_thread_tests.rs deleted file mode 100644 index 290e90e5d..000000000 --- a/apps/zgx/src/long_thread_tests.rs +++ /dev/null @@ -1,141 +0,0 @@ -//! 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 11e523cbd..e3431f7b8 100644 --- a/apps/zgx/src/main.rs +++ b/apps/zgx/src/main.rs @@ -60,7 +60,6 @@ mod mermaid; mod plugins; mod previews; mod projects; -mod quit_prompt; mod relaunch; mod session_home; mod sessions; @@ -135,8 +134,6 @@ 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; @@ -355,6 +352,8 @@ fn main() -> Result<(), Box> { } }); } + // A clicked banner: Zeron forward on its chat (src/links.rs). + links::take_banner_clicks(); let options = match screen { Screen::Intro => intro_window(), Screen::Onboarding => onboarding_window(), @@ -374,12 +373,6 @@ 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(); @@ -674,9 +667,6 @@ 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 @@ -1109,12 +1099,6 @@ 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/grants.rs b/apps/zgx/src/plugins/grants.rs index 6093dfebd..2e731d6e2 100644 --- a/apps/zgx/src/plugins/grants.rs +++ b/apps/zgx/src/plugins/grants.rs @@ -39,6 +39,9 @@ pub const SHARED_READ: &[(&str, Option<&str>)] = &[ ("zeron.project", Some("zeron.sessions")), ("zeron.device", Some("zeron.devices")), ("zeron.devices", Some("zeron.devices")), + // The draft in the window's composer, for a plugin allowed "draft": + // `{ session, text, attachments = { { kind, name } } }`, never a path. + ("zeron.draft", Some("zeron.draft")), // Whether the window shows and is in front (the SDK's `zeron.window`): // work no one would see waits for it. ("zgx.window.shown", None), @@ -77,9 +80,9 @@ pub const ACTIONS: &[(&str, &str)] = &[ pub const NEVER_REPLACED: &[&str] = &["@app/screens/plugins", "@app/screens/shell/settings"]; /// Each plugin's own actions (`plugin..`). -pub const OWN_ACTIONS: &[&str] = &["result", "ack", "send", "menus", "toast", "problem", "open_tab", "add_page", "github_watch", "github_call", "store_set", "setting_status", "open_url", "notify", "cost", "run", "db", "service_call", "agents", "menu_run", "compose_comment", "file", "block"]; +pub const OWN_ACTIONS: &[&str] = &["result", "ack", "send", "menus", "toast", "problem", "open_tab", "add_page", "github_watch", "github_call", "store_set", "setting_status", "open_url", "notify", "cost", "run", "db", "service_call", "agents", "menu_run", "compose_comment", "draft", "draft_of", "file", "block"]; /// Each plugin's own shared state, to read (`plugin..`). -pub const OWN_SHARED: &[&str] = &["calls", "inbox", "records", "github", "store", "settings", "locales", "menu_items", "composed"]; +pub const OWN_SHARED: &[&str] = &["calls", "inbox", "records", "github", "store", "settings", "locales", "menu_items", "composed", "drafts"]; /// The capability naming plugin `id`'s own calls and actions. pub fn own(id: &str) -> Capability { @@ -252,6 +255,9 @@ pub fn capabilities(id: &str, permissions: &[String], change: &[String], replace "pages.add" => out.push(Capability::OverrideElement(page_selector(id))), "navigate" => out.push(named("zeron.navigate")), "compose" => out.push(named("zeron.compose")), + // Reading the composer's draft (`zeron.draft`); taking it and + // putting it back are its own action's, checked by the host. + "draft" => out.push(named("zeron.draft")), "devices" => out.push(named("zeron.devices")), "network" => out.push(Capability::Fetch), "clipboard" => out.push(Capability::Clipboard), @@ -394,6 +400,15 @@ mod tests { assert!(!got.iter().any(|c| matches!(c, Capability::Override(_)))); } + /// "draft" reads the composer's draft; "compose" alone never does. + #[test] + fn only_draft_reads_the_draft() { + let draft = Capability::Named("zeron.draft".into()); + assert_eq!(capabilities("stash", &s(&["draft"]), &[], &[]), vec![own("stash"), draft.clone()]); + assert!(!capabilities("x", &s(&["compose"]), &[], &[]).contains(&draft)); + assert!(SHARED_READ.contains(&("zeron.draft", Some("zeron.draft")))); + } + #[test] fn listed_targets_without_the_permission_give_nothing() { let got = capabilities("x", &[], &s(&["#titlebar-actions"]), &s(&["@app/screens/chat"])); diff --git a/apps/zgx/src/plugins/live.rs b/apps/zgx/src/plugins/live.rs index 44155cb06..d6197d413 100644 --- a/apps/zgx/src/plugins/live.rs +++ b/apps/zgx/src/plugins/live.rs @@ -219,14 +219,6 @@ 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 { @@ -248,64 +240,8 @@ pub fn plan(templates: &Path, id: &str, failed: &BTreeSet, whole_failed: want.insert(rel, path); } } - (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) - } + let changed = mirror(&templates.join(LIVE), &want, |_| false)?; + Ok((changed, Overlay { template: id.to_string(), from_template })) } /// The files of `.live` an error names, first mention first: each @@ -905,59 +841,6 @@ 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 915171524..a557ef1bc 100644 --- a/apps/zgx/src/plugins/mod.rs +++ b/apps/zgx/src/plugins/mod.rs @@ -202,17 +202,8 @@ 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, @@ -306,9 +297,6 @@ 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, @@ -358,29 +346,6 @@ 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 @@ -491,6 +456,88 @@ pub(crate) fn comment(asked: &Value, shown: &str) -> Option { Some(json_value(&out)) } +/// The longest key a plugin keeps a taken draft under. +const DRAFT_KEY: usize = 64; + +/// A plugin's ask of the composer's draft (`zeron.take_draft`, +/// `put_draft`, `swap_draft`, `drop_draft`), held to `{ op, key }`: `op` +/// one of "take", "put", "swap" or "drop", `key` 1 to 64 characters with +/// no control characters. Anything else is refused. +pub(crate) fn draft_ask(asked: &Value) -> Option { + let op = asked.field("op").map(text)?; + let key = asked.field("key").map(text)?; + let valid_key = !key.is_empty() && key.chars().count() <= DRAFT_KEY && !key.chars().any(char::is_control); + if !matches!(op.as_str(), "take" | "put" | "swap" | "drop") || !valid_key { + return None; + } + Some(Value::record([("op", Value::from(op.as_str())), ("key", Value::from(key.as_str()))])) +} + +/// What `zeron.compose` asks of the composer: its words alone (added to +/// the draft shown, as ever), or with `{ text, replace, session }` (the +/// draft's words replaced, or a chat named other than the one shown). +pub(crate) fn compose(asked: &Value) -> Value { + let Some(words) = asked.field("text") else { + return Value::from(text(asked).as_str()); + }; + let words = match words { + Value::Str(_) | Value::Text(_) => words.clone(), + _ => Value::from(""), + }; + let mut fields = vec![ + ("text", words), + ("replace", Value::from(matches!(asked.field("replace"), Some(Value::Bool(true))))), + ]; + if let Some(session) = asked.field("session").map(text).filter(|s| !s.is_empty()) { + fields.push(("session", Value::from(session.as_str()))); + } + Value::record(fields) +} + +/// The app's shared states a chat's draft lives in (screens/chat/shared), +/// each read with the value the app starts it at: read before the app has +/// made one, it's made as the app would. +const DRAFT_HERE: &str = "chat.composer.here"; +const DRAFT_TEXT: &str = "chat.composer.text"; +const DRAFT_CARGO: &str = "chat.composer.cargo"; +const DRAFTS_KEPT: &str = "chat.composer.drafts"; + +/// What a draft carries, as plugins are told it (as `zeron.draft()` says +/// it, screens/plugins): each chip's kind and name, never a file's path. +fn draft_attachments(cargo: &Value) -> Value { + let said = |item: &Value, field: &str, or: &str| match item.field(field) { + Some(v @ Value::Str(_)) => v.clone(), + _ => Value::from(or), + }; + vlist(cargo.items().iter().map(|item| Value::record([("kind", said(item, "kind", "file")), ("name", said(item, "name", ""))])).collect()) +} + +/// Chat `session`'s draft, for `zeron.draft_of` ("draft"): out in the +/// composer (`here` its chat, "" a new chat's; `shown` its words, `cargo` +/// its chips) or kept for it (`kept`, by chat). Its words as typed, what it +/// carries, and its chat (none for a new chat's). A chat with no draft has +/// no words and carries nothing. +pub(crate) fn draft_of(session: &str, here: &Value, shown: &Value, cargo: &Value, kept: &Value) -> Value { + let (words, carried) = if session == text(here) { + (shown.clone(), cargo.clone()) + } else { + let d = kept.field(session); + ( + d.and_then(|d| d.field("text")).cloned().unwrap_or_default(), + d.and_then(|d| d.field("cargo")).cloned().unwrap_or_default(), + ) + }; + let words = match words { + Value::Str(_) | Value::Text(_) => words, + _ => Value::from(""), + }; + let mut fields = vec![("text", words), ("attachments", draft_attachments(&carried))]; + if !session.is_empty() { + fields.push(("session", Value::from(session))); + } + Value::record(fields) +} + /// Plugins for the main window `cx` shows `host` in. pub fn install(cx: &mut WindowContext, host: &ScriptHost) { expose_surface(host); @@ -503,8 +550,6 @@ 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(), @@ -553,7 +598,6 @@ 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)); @@ -580,10 +624,6 @@ 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 }; @@ -716,42 +756,27 @@ 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) => { @@ -1062,7 +1087,6 @@ 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(); } @@ -1398,10 +1422,7 @@ impl Bridge { } *self.loaded.borrow_mut() = want.clone(); self.host.set_plugins(want); - // (Before the window's first load: that load takes them.) - if !self.first_load.get() { - self.reload(); - } + self.reload(); } fn reload(&self) { @@ -1763,32 +1784,7 @@ 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. @@ -2484,6 +2480,49 @@ impl Bridge { } } }); + // The composer's draft taken (its words, its chips, the passages + // quoted in it), put back or let go, under a key of the plugin's, + // for a plugin allowed "draft": screens/plugins keeps what's taken + // (the plugin never holds its files' paths) and tells it what it + // keeps as `plugin..drafts`. + host.action(format!("plugin.{id}.draft"), { + let (me, plugin) = (me.clone(), plugin.clone()); + move |asked| { + if let Some(b) = me.upgrade() + && b.item(&plugin).is_some_and(|i| i.may("draft")) + { + match draft_ask(&asked) { + Some(asked) => b.request("draft", &plugin, asked), + None => eprintln!("zeron-zgx: plugin {plugin}: a draft ask that isn't take, put, swap or drop with a key, refused"), + } + } + } + }); + // Another chat's draft, as it is (`zeron.draft_of`, "draft"): read + // where the app keeps it, once, never followed. + host.async_action(format!("plugin.{id}.draft_of"), { + let (me, plugin) = (me.clone(), plugin.clone()); + move |asked| { + let (me, plugin) = (me.clone(), plugin.clone()); + async move { + let b = me.upgrade().ok_or("This plugin isn't on")?; + let item = b.item(&plugin).ok_or("This plugin isn't on")?; + if !item.may("draft") { + return Err("This plugin wasn't allowed to read what you type in the message box".to_string()); + } + let session = text(&asked); + if session.chars().count() > 200 { + return Err("That isn't a chat".to_string()); + } + let read = |name: &str, initial: Value| b.host.shared(name, initial).signal().with_untracked(Value::clone); + let here = read(DRAFT_HERE, Value::from("")); + let shown = read(DRAFT_TEXT, Value::from("")); + let cargo = read(DRAFT_CARGO, vlist(Vec::new())); + let kept = read(DRAFTS_KEPT, Value::record(Vec::<(&str, Value)>::new())); + Ok(draft_of(&session, &here, &shown, &cargo, &kept)) + } + } + }); host.action(format!("plugin.{id}.open_url"), { let (me, plugin) = (me.clone(), plugin.clone()); move |url| { @@ -3384,9 +3423,9 @@ impl Bridge { }); host.action("zeron.compose.insert", { let me = me.clone(); - move |text_| { + move |asked| { if let Some(b) = me.upgrade() { - b.request("compose", "", Value::from(text(&text_).as_str())); + b.request("compose", "", compose(&asked)); } } }); @@ -3528,6 +3567,52 @@ mod tests { assert_eq!(gone, vec!["clock".to_string(), "ping".to_string()]); } + /// `zeron.compose(text)` adds its words as ever; with options, it says + /// whether they replace the draft and which chat's (none: the shown). + #[test] + fn compose_takes_words_alone_or_with_replace_and_a_chat() { + assert_eq!(compose(&Value::from("hello")), Value::from("hello")); + let asked = json_value(&json!({ "text": "clearer", "replace": true, "session": "s2" })); + let out = compose(&asked); + assert_eq!(text(out.field("text").unwrap()), "clearer"); + assert_eq!(out.field("replace"), Some(&Value::from(true))); + assert_eq!(text(out.field("session").unwrap()), "s2"); + // (Only `true` replaces; a blank chat is the one shown; words that + // aren't text are none.) + let loose = json_value(&json!({ "text": 5, "replace": "yes", "session": "" })); + let out = compose(&loose); + assert_eq!(text(out.field("text").unwrap()), ""); + assert_eq!(out.field("replace"), Some(&Value::from(false))); + assert!(out.field("session").is_none()); + } + + /// `zeron.draft_of`: the chat shown's draft (from the composer), or + /// another chat's (kept), with its chat and its chips by kind and name + /// alone; a new chat's has no chat; a chat with none, nothing. + #[test] + fn a_chats_draft_is_read_where_it_is_kept_without_paths() { + let kept = json_value(&json!({ + "s2": { "text": "kept words", "cargo": [{ "kind": "file", "name": "a.rs", "path": "/Users/me/a.rs" }] }, + "": { "text": "new chat words", "cargo": [] }, + })); + let (here, shown) = (Value::from("s1"), Value::from("typing now")); + let cargo = json_value(&json!([{ "kind": "image", "name": "shot.png", "path": "/tmp/shot.png" }])); + let out = draft_of("s1", &here, &shown, &cargo, &kept); + assert_eq!(value_json(&out), json!({ "text": "typing now", "session": "s1", "attachments": [{ "kind": "image", "name": "shot.png" }] })); + let out = draft_of("s2", &here, &shown, &cargo, &kept); + assert_eq!(value_json(&out), json!({ "text": "kept words", "session": "s2", "attachments": [{ "kind": "file", "name": "a.rs" }] })); + assert!(!out.to_json().contains("/Users/me"), "never a path"); + // A chat with no draft: nothing written, nothing carried. + let out = draft_of("s9", &here, &shown, &cargo, &kept); + assert_eq!(value_json(&out), json!({ "text": "", "session": "s9", "attachments": [] })); + // A new chat's draft, kept while a chat shows: no chat. + let out = draft_of("", &here, &shown, &cargo, &kept); + assert_eq!(value_json(&out), json!({ "text": "new chat words", "attachments": [] })); + // Before the app has made its states: nothing, not an error. + let out = draft_of("s1", &Value::Nil, &Value::Nil, &Value::Nil, &Value::Nil); + assert_eq!(text(out.field("text").unwrap()), ""); + } + /// R21: a plugin's review comment goes on the chat shown alone, with /// at most the 40 lines of code that are sent (a 20,000-character /// snippet once rode along unseen, onto any chat's draft). @@ -3548,6 +3633,23 @@ mod tests { assert!(text(comment(&wide, "s1").unwrap().field("snippet").unwrap()).len() <= COMMENT_BYTES); } + /// A plugin's ask of the draft is take, put, swap or drop, under a key + /// of 1 to 64 characters; nothing else rides along to the app. + #[test] + fn a_draft_ask_is_take_put_or_drop_with_a_key() { + let ask = |v: Json| draft_ask(&json_value(&v)); + let took = ask(json!({ "op": "take", "key": "stash", "extra": "dropped" })).expect("a take"); + assert_eq!(value_json(&took), json!({ "op": "take", "key": "stash" })); + assert!(ask(json!({ "op": "put", "key": "k".repeat(64) })).is_some()); + assert!(ask(json!({ "op": "drop", "key": "é" })).is_some()); + assert!(ask(json!({ "op": "swap", "key": "stash" })).is_some()); + assert!(ask(json!({ "op": "send", "key": "stash" })).is_none(), "not an op"); + assert!(ask(json!({ "op": "take", "key": "" })).is_none(), "no key"); + assert!(ask(json!({ "op": "take" })).is_none(), "no key"); + assert!(ask(json!({ "op": "take", "key": "k".repeat(65) })).is_none(), "too long"); + assert!(ask(json!({ "op": "take", "key": "a\nb" })).is_none(), "a control character"); + } + #[test] fn only_a_plugin_on_loads_from_its_snapshot_with_its_grants() { let on = item(json!({ diff --git a/apps/zgx/src/quit_prompt.rs b/apps/zgx/src/quit_prompt.rs deleted file mode 100644 index cfedb19a3..000000000 --- a/apps/zgx/src/quit_prompt.rs +++ /dev/null @@ -1,202 +0,0 @@ -//! 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 956b59618..503f84117 100644 --- a/apps/zgx/src/relaunch.rs +++ b/apps/zgx/src/relaunch.rs @@ -74,11 +74,6 @@ 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/sessions.rs b/apps/zgx/src/sessions.rs index 7e5810e89..f446abc04 100644 --- a/apps/zgx/src/sessions.rs +++ b/apps/zgx/src/sessions.rs @@ -191,6 +191,21 @@ struct RowWith { working_side_chats: u32, known_project: bool, agent_icon: Option, + /// The last turn its agent finished, as its session row marks it + /// (`lastCompletedTurn`): what a "finished" chime rings on moving. + turn: Option, +} + +/// A session row's finished turn, and whether the row is fresh: written in +/// the last `SESSION_STALE_MS`, so a turn's end just published rather than +/// an old row a sync or reconnect brings back (the shipping app's chime +/// rule, crates/engine/src/desktop_notify.rs `SessionNotificationState`). +fn finished_turn(session: Option<&Session>, at: chrono::DateTime) -> (Option, bool) { + let Some(s) = session else { + return (None, false); + }; + let fresh = at.signed_duration_since(s.updated_at).num_milliseconds() <= zeron_proto::view::SESSION_STALE_MS; + (s.last_completed_turn.clone(), fresh) } /// `slot` holds `item` now; whether that's new. @@ -621,6 +636,7 @@ fn child_row(rows: &mut HashMap, c: &Json, id: &str, parent: &str, working_side_chats: 0, known_project: false, agent_icon: harness.and_then(crate::agents::registry_icon), + turn: None, }; if rows.get(id).is_some_and(|row| row.with == with) { return; @@ -681,6 +697,7 @@ fn top_row( working_side_chats, known_project: project.is_some(), agent_icon: harness.and_then(crate::agents::registry_icon), + turn: finished_turn(session, at).0, }; if let Some(row) = rows.get(id).filter(|row| row.with == with) { return row.value.clone(); @@ -721,6 +738,12 @@ fn top_row( // it woke (Unix seconds). ("sleep_until", with.sleep.filter(|s| s.0).map_or(Value::Nil, |s| Value::from(s.1 as f64))), ("woke", with.sleep.filter(|s| !s.0).map_or(Value::Nil, |s| Value::from(s.1 as f64))), + // The last turn its agent finished (an opaque mark), and whether its + // session row was fresh when that was read: the chimes ring + // "finished" on this moving, never on the status alone ("done" is + // only unseen activity, which your own send makes too). + ("turn", with.turn.as_deref().map_or(Value::Nil, Value::from)), + ("turn_fresh", Value::from(finished_turn(session, at).1)), ]); rows.insert(id.to_owned(), Row { moved: moved(c), updated: updated as f64, marks, with, value: value.clone() }); value @@ -2516,6 +2539,33 @@ mod tests { assert!(!s.rows.borrow().contains_key("b")); } + /// A chat's finished turn (what the "done" chime rings on, issue #1437): + /// its session's `lastCompletedTurn`, published as it moves, and whether + /// the row saying so is fresh. + #[test] + fn a_chat_s_finished_turn_is_published_as_it_moves() { + let now = now_seconds(); + let s = with_children("2099-01-01T00:00:00Z"); + s.heard.set(true); + let row = |turn: &str, ago: i64| { + json!([{ "chatId": "p", "deviceId": "d", "status": "idle", "updatedAt": iso(now - ago), "lastCompletedTurn": turn }]) + }; + s.set_live(row("t1", 1)); + s.publish(); + let p = s.value(); + assert_eq!(chat(&p, "p").field("turn").map(crate::auth::text).as_deref(), Some("t1")); + assert!(chat(&p, "p").field("turn_fresh").is_some_and(zgx_script::Value::truthy)); + let before = s.published.get(); + s.heard(Slot::Live, row("t2", 0)); + assert_eq!(s.published.get(), before + 1, "a finish is shown at once"); + assert_eq!(chat(&s.value(), "p").field("turn").map(crate::auth::text).as_deref(), Some("t2")); + // An end published long ago (a sync, a reconnect): not fresh. + s.heard(Slot::Live, row("t3", 600)); + let p = s.value(); + assert_eq!(chat(&p, "p").field("turn").map(crate::auth::text).as_deref(), Some("t3")); + assert!(!chat(&p, "p").field("turn_fresh").is_some_and(zgx_script::Value::truthy)); + } + #[test] fn an_update_that_changes_nothing_publishes_nothing() { let s = with_children("2099-01-01T00:00:00Z"); diff --git a/apps/zgx/src/settings.rs b/apps/zgx/src/settings.rs index 85903c1cf..e68d8baac 100644 --- a/apps/zgx/src/settings.rs +++ b/apps/zgx/src/settings.rs @@ -94,9 +94,6 @@ 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", @@ -319,7 +316,6 @@ 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 4fce8ead2..bf18a0579 100644 --- a/apps/zgx/src/shortcut_tests.rs +++ b/apps/zgx/src/shortcut_tests.rs @@ -219,74 +219,6 @@ 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 a78157c3d..e29774ed1 100644 --- a/apps/zgx/src/transcript_paint_tests.rs +++ b/apps/zgx/src/transcript_paint_tests.rs @@ -153,14 +153,6 @@ 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 ad5ad0b37..ad6910e26 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. -pub(crate) fn shown(app: &mut TestApp) -> Vec<(String, f32)> { +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 @@ pub(crate) 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. -pub(crate) fn moved(before: &[(String, f32)], after: &[(String, f32)]) -> (Option, usize) { +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/account/sessions.luau b/apps/zgx/ui/account/sessions.luau index 22b51e557..7940c4c68 100644 --- a/apps/zgx/ui/account/sessions.luau +++ b/apps/zgx/ui/account/sessions.luau @@ -77,6 +77,11 @@ export type Chat = { -- woke (Unix seconds). sleep_until: number?, woke: number?, + -- The last turn its agent finished (an opaque mark, nil before its + -- first), and whether its session row was fresh as that was read: what + -- the "finished" chime rings on moving (screens/settings/chimes). + turn: string?, + turn_fresh: boolean?, } export type WaitingOn = { id: string, kind: string, label: string } -- A side chat: a chat under another (its `parent`). diff --git a/apps/zgx/ui/chat_fixture.luau b/apps/zgx/ui/chat_fixture.luau index 3d4dfc15b..16d15fda3 100644 --- a/apps/zgx/ui/chat_fixture.luau +++ b/apps/zgx/ui/chat_fixture.luau @@ -886,6 +886,16 @@ ui.command { end, } +-- A settled reply the host gave no time (as Windows did before it could +-- read one): its Copy shows all the same (tests/chat_in_place). +ui.command { + id = "reply_without_time", + title = "A reply with no time", + run = function() + store.messages:push { id = "notime1", role = "assistant", text = "Answered with no clock.", copy = "Answered with no clock." } + end, +} + -- Messages from other chats, as the host gives them: one from a chat that's -- listed, one from a chat that isn't, one with a long title (tests/chat_sender). ui.command { @@ -1606,6 +1616,29 @@ ui.effect(function() table.sort(keys) plugin_comments:set(table.concat(keys, ",")) end) +-- The draft lifted out whole and laid back (a plugin's `zeron.take_draft` +-- and `put_draft`; screens/plugins keeps it between the two, as this +-- does). `test_lifted` says what was lifted: its words and how many chips. +local lifted: any = nil +local lifted_said = ui.state("test_lifted", "") +ui.command { + id = "draft_lift", + title = "A plugin takes the draft", + run = function() + lifted = require("@app/screens/chat/shared").lift() + lifted_said:set(if lifted then `{lifted.text}|{#lifted.cargo}` else "nothing") + end, +} +ui.command { + id = "draft_lay", + title = "A plugin puts the draft back", + run = function() + if lifted then + require("@app/screens/chat/shared").lay(lifted) + lifted = nil + end + end, +} ui.command { id = "comment_moves", title = "The comment is carried two lines down", @@ -1643,49 +1676,6 @@ 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/chimes_fixture.luau b/apps/zgx/ui/chimes_fixture.luau new file mode 100644 index 000000000..a9788cbae --- /dev/null +++ b/apps/zgx/ui/chimes_fixture.luau @@ -0,0 +1,7 @@ +-- The session chimes (screens/settings/chimes) with no engine: a test sets +-- the `sessions` shared state as the engine would publish it, and reads what +-- played from `settings_stub_calls` ("sound.play:done", …). +require("./settings_stub") +require("@app/screens/settings/chimes") + +return ui.column {} diff --git a/apps/zgx/ui/consent_fixture.luau b/apps/zgx/ui/consent_fixture.luau index 666c0d1fd..0c70daf87 100644 --- a/apps/zgx/ui/consent_fixture.luau +++ b/apps/zgx/ui/consent_fixture.luau @@ -174,9 +174,9 @@ local PLUGINS: { [string]: any } = { hash = "long-1", status = "pending", author = { kind = "unknown" }, - permissions = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, + permissions = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "draft", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, allowed = {}, - ask = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, + ask = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "draft", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, ui = { change = { "#shell" } }, commands = { "git" }, tools = { { name = "a", description = "First." } }, diff --git a/apps/zgx/ui/ds/composer.luau b/apps/zgx/ui/ds/composer.luau index 6c602e7d7..7b876dd88 100644 --- a/apps/zgx/ui/ds/composer.luau +++ b/apps/zgx/ui/ds/composer.luau @@ -55,7 +55,6 @@ 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") @@ -1001,14 +1000,12 @@ 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(line_height: number): Layout.Sample? + estimate = function(text: string, 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 - -- 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) + return Layout.estimate(text, line_height) end, } local head_h_seen = kept("head_h", 0) @@ -1049,7 +1046,7 @@ return ui.component(function(props: Props): Node end local function has_text(): boolean - return Draft.has_text(value) + return string.find(value:get(), "%S") ~= nil end local function disabled(): boolean return read(props.disabled) == true @@ -1179,7 +1176,7 @@ return ui.component(function(props: Props): Node cargo = has_cargo(), asking = asking(), dictating = dictating(), - multiline = Draft.multiline(value), + multiline = string.find(value:get(), "\n") ~= nil, } 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 53c0f8e51..a6698dca0 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 = M.capped_lines(line_height) + local need = math.ceil((TEXT_MAX - TEXT_TOP - TEXT_BOTTOM) / math.max(1, line_height)) local lines, at = 1, 1 while lines < need do local newline = string.find(text, "\n", at, true) @@ -69,22 +69,7 @@ function M.estimate(text: string, line_height: number): Sample? lines += 1 at = newline + 1 end - 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 } + return { unwrapped_width = 0, wrapped_height = lines * 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 6556741bd..b58078b30 100644 --- a/apps/zgx/ui/ds/composer_mentions.luau +++ b/apps/zgx/ui/ds/composer_mentions.luau @@ -37,7 +37,6 @@ 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 @@ -104,10 +103,6 @@ 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 @@ -117,9 +112,6 @@ 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 @@ -128,7 +120,7 @@ function M.use(opts: Use) end local active = ui.use_state(1) local token = ui.computed(function(): Token? - local t = Draft.token(opts.value, caret:get()) + local t = M.token(opts.value:get(), caret:get()) if t and opts.enabled and not opts.enabled(t.trigger) then return nil end @@ -267,27 +259,21 @@ 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() - 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() + local key = key_now() -- (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 - was_text = text - if moved then + local moved = key ~= was_key + was_text, was_key = text, key + local gone = dismissed:peek()[key] + if gone == nil or moved then return end if t == nil or t.from ~= gone or typed then @@ -299,12 +285,11 @@ function M.use(opts: Use) -- path with it. local refs = ui.use_state({} :: { [string]: string }) ui.effect(function() - -- (Each looked for where the draft is held, not copied out of it.) - Draft.track(opts.value) + local now = opts.value:get() local kept = refs:peek() local stale: { string } = {} for tok in kept do - if #opts.value:find(tok, { limit = 1 }) == 0 then + if not string.find(now, tok, 1, true) 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 9446c8e69..267c80c96 100644 --- a/apps/zgx/ui/ds/composer_metrics.luau +++ b/apps/zgx/ui/ds/composer_metrics.luau @@ -12,8 +12,7 @@ export type Sample = { } type Request = { width: number, editor: Element?, source: Sample?, estimate: Size?, revision: number, - -- Which edit of the draft (`edits`), not the draft itself. - edit: number, theme: string, mount: number, + text: string, theme: string, mount: number, } export type Result = { -- A complete last sample, never one axis from a different request. @@ -37,7 +36,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: ((line_height: number) -> Size?)?, + estimate: ((text: string, line_height: number) -> Size?)?, } function M.use(props: Props) @@ -72,19 +71,10 @@ 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 edit = edits:get() + local text = props.value: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() @@ -92,7 +82,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(exact.line_height) + estimate = props.estimate(text, exact.line_height) end -- Reading follows the editor's settled content/IME/style revision. if estimate == nil then @@ -110,11 +100,11 @@ function M.use(props: Props) return was end revision += 1 - local now: Request = { width = width, editor = el, source = source, estimate = estimate, revision = revision, edit = edit, theme = theme, mount = mount.revision } + local now: Request = { width = width, editor = el, source = source, estimate = estimate, revision = revision, text = text, theme = theme, mount = mount.revision } last = now return now end) - local settled: { mount: number, edit: number, theme: string, revision: number, width: number }? = nil + local settled: { mount: number, text: string, 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.) @@ -133,7 +123,7 @@ function M.use(props: Props) end) end local function publish_exact(wanted: Request, source: Sample) - settled = { mount = wanted.mount, edit = wanted.edit, theme = wanted.theme, + settled = { mount = wanted.mount, text = wanted.text, 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) @@ -159,7 +149,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.edit == wanted.edit and settled.theme == wanted.theme + and settled.text == wanted.text 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 deleted file mode 100644 index 1c029e79a..000000000 --- a/apps/zgx/ui/ds/draft_text.luau +++ /dev/null @@ -1,99 +0,0 @@ --- 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 c47933d2f..9e04face3 100644 --- a/apps/zgx/ui/ds/markdown_edit.luau +++ b/apps/zgx/ui/ds/markdown_edit.luau @@ -238,35 +238,6 @@ 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. @@ -274,31 +245,32 @@ 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: 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). + -- The caret's line, in bytes from 0. local start = 0 - 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 + local i = 0 + while true do + local nl = string.find(text, "\n", i + 1, true) + if not nl or nl > a then break end - hi = lo - 1 + start = nl + i = nl 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) - local p = prefix(line) - if not p or a < start + p.stop then + -- 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 - -- 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 + local p = prefix(line) + if not p or a < start + p.stop then return nil, nil, nil end if string.match(string.sub(line, p.stop + 1), "^%s*$") then diff --git a/apps/zgx/ui/ds/mention_tokens.luau b/apps/zgx/ui/ds/mention_tokens.luau index dcf7bb750..5a6ef8ac1 100644 --- a/apps/zgx/ui/ds/mention_tokens.luau +++ b/apps/zgx/ui/ds/mention_tokens.luau @@ -31,45 +31,6 @@ 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 @@ -77,13 +38,7 @@ end -- trigger starts a token only at a start, after a space, or after an -- opening bracket. function M.token(text: string, caret: number): Token? - -- 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 before = string.sub(text, 1, caret) 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 @@ -91,9 +46,19 @@ 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, caret + REACH), "^" .. chars .. "*") or "" + local rest = string.match(string.sub(text, caret + 1), "^" .. 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 @@ -109,20 +74,19 @@ 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, base + at + 1, to), "^%d") then + if trigger == "$" and string.match(string.sub(text, at + 1, to), "^%d") then return nil end - -- Inside a fenced block, nothing. - if fenced(text, base + at) then + if fenced(at) then return nil end - return { trigger = trigger :: Trigger, query = query, from = base + at - 1, to = to } + return { trigger = trigger :: Trigger, query = query, from = 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(text, base + s) then - return { trigger = "/", query = string.sub(before, s + 1), from = base + s - 1, to = ends("[%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%._:%-]") } end return nil end diff --git a/apps/zgx/ui/ds/provider_card.luau b/apps/zgx/ui/ds/provider_card.luau index 6e3cb0f3c..959c15355 100644 --- a/apps/zgx/ui/ds/provider_card.luau +++ b/apps/zgx/ui/ds/provider_card.luau @@ -221,6 +221,9 @@ export type CardProps = { -- back, Claude's when it can't finish in the browser) or "failed". foot: (string | () -> string)?, enabled: State, + -- An agent nothing here switches or updates (Cloud's: on every Cloud + -- machine, its accounts alone managed here): no switch, no Update. + fixed: boolean?, -- The account in use, by its email. active: () -> string?, settings: boolean?, @@ -1468,7 +1471,7 @@ function M.Card(c: CardProps): Node gap = "$space.3", items = "center", ui.show(function() - return latest() ~= nil or (c.updating ~= nil and c.updating()) + return not c.fixed and (latest() ~= nil or (c.updating ~= nil and c.updating())) end, Button { locale.t("ds.provider_card.update"), tone = "secondary", @@ -1488,7 +1491,7 @@ function M.Card(c: CardProps): Node on_click = c.on_settings, } else nil, - Switch { value = c.enabled, label = locale.t("ds.provider_card.enabled", { name = provider.name }) :: any }, + if c.fixed then nil else Switch { value = c.enabled, label = locale.t("ds.provider_card.enabled", { name = provider.name }) :: any }, }, } -- Its accounts, a row each, keyed: one arriving or going is that row diff --git a/apps/zgx/ui/ds/transcript.luau b/apps/zgx/ui/ds/transcript.luau index fa94a99a6..8606ca686 100644 --- a/apps/zgx/ui/ds/transcript.luau +++ b/apps/zgx/ui/ds/transcript.luau @@ -300,9 +300,11 @@ local function strip(m: Message, row: State, hovered: () -> boolean): N justify = if mine then "end" else "start", selectable = false, ui.presence(function(): string? - -- (Never while a reply is written: it has no time yet.) + -- (Never while a reply is written. A settled message shows it + -- with or without its time: a time missing, as on a platform + -- that couldn't read one, mustn't take Copy with it.) local now = row:get() - return if hovered() and now.at and not now.streaming then "shown" else nil + return if hovered() and (now.at or now.copy) and not now.streaming then "shown" else nil end, function(): Node return ui.row { class = "message-actions", @@ -652,103 +654,9 @@ 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 function block(b: Block, brow: State): Node + local shown = ui.computed(list) + return ui.each(shown, function(b: Block, _i: number, brow: State): Node if b.kind == "tools" or b.kind == "work" then return Activity { key = `{m.id}:{b.id}`, @@ -861,219 +769,9 @@ 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(run: Run): string - return run.key + key = function(b: Block): string + return b.id end, w_full = true, gap = "$space.3", @@ -1448,6 +1146,10 @@ local function Transcript(props: Props): Node -- taken meanwhile stays where it went. local function rise(prompt: Message?) ui.after(16, function() + -- (Scrolled meanwhile: the view is the reader's, see `wheeled`.) + if glides ~= turn then + return + end local row_el = prompt and tops[prompt.id] and tops[prompt.id]:peek() local list_el = list:peek() if not row_el or not list_el or row_el.y == nil or list_el.y == nil then @@ -1460,6 +1162,9 @@ local function Transcript(props: Props): Node duration = motion.ms(GLIDE_MS), ease = "outQuad", onComplete = function() + if glides ~= turn then + return + end -- Landed: the old reply lets go of the room it held -- (above the view, so nothing seen moves), and the -- offset says where the list now rests, a real place @@ -1746,6 +1451,18 @@ local function Transcript(props: Props): Node -- (Taken somewhere of their own: a place still to come back to is -- let go.) waiting = nil + -- A send's glide under way (or its prompt still to land, a steer's): + -- the scroll is theirs, so the glide stops where it is rather than + -- pulling the view back to the end. The old reply keeps the room it + -- held until it's out of view (`rested`), so nothing shown moves. + if gliding:peek() then + glides += 1 + if glide then + glide:pause() + glide = nil + end + gliding:set(false) + end return nil end -- When focus last came to something in the list (Tab, ⇧Tab to a row's @@ -1754,6 +1471,8 @@ local function Transcript(props: Props): Node -- While the width is changing (and a moment after): the reading row -- puts the place back as its lines rewrap, the frame they do. local resizing_until = 0 + -- When the view's size last changed. + local resized_at = -math.huge local function find_reading() reading = nil local el = list:peek() @@ -1835,9 +1554,32 @@ local function Transcript(props: Props): Node local function rested() remember() find_reading() - if not at_end:peek() and ui.now() >= resizing_until then + -- Resting away from the end: let go of it once no change of size is + -- under way, or at once if a person scrolled since the last one (a + -- scroll begun just after a resize is theirs; held, the next resize, + -- however much later, would pull the view back to the end). + if not at_end:peek() and (ui.now() >= resizing_until or wheeled_at >= resized_at) then end_held = false end + -- A send's glide a scroll stopped (`wheeled`): the old reply's held + -- room goes once that reply is out of view, where it moves nothing. + local h = held:peek() + if h.id ~= "" and not gliding:peek() then + local r = rows[h.id] + local row_el = r and r.outer:peek() + local el = list:peek() + local away_from_view = not row_el + or not el + or row_el.y == nil + or row_el.height == nil + or el.y == nil + or el.height == nil + or (row_el.y :: number) + (row_el.height :: number) <= (el.y :: number) + or (row_el.y :: number) >= (el.y :: number) + (el.height :: number) + if away_from_view then + held:set { id = "", h = 0 } + end + end end local rest = ui.debounced(offset, 150, { every = true }) -- (A view just made: its place told once it has stood 150 ms.) @@ -2296,7 +2038,8 @@ local function Transcript(props: Props): Node ui.after(48, settle) end if changed then - resizing_until = ui.now() + 300 + resized_at = ui.now() + resizing_until = resized_at + 300 -- At the end: on it still, every frame of the change. if end_held and not gliding:peek() then -- (Asked of the list itself, which lands it as it lays diff --git a/apps/zgx/ui/locales/en.json b/apps/zgx/ui/locales/en.json index 1c8011483..bd46a6a85 100644 --- a/apps/zgx/ui/locales/en.json +++ b/apps/zgx/ui/locales/en.json @@ -1182,12 +1182,6 @@ "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": { @@ -1637,7 +1631,18 @@ }, "remove_row_description": "Chats that used it stay." }, - "couldnt_switch": "Couldn't switch accounts" + "couldnt_switch": "Couldn't switch accounts", + "cloud": "Cloud", + "cloud_keys": { + "title": "API keys", + "section_description": "Instead of signing in, Cloud can use an API key. A key saved here becomes the account its agent uses on every Cloud machine, from its next turn.", + "anthropic": "Anthropic API key", + "openai": "OpenAI API key", + "description": "For {agent} on Cloud. Kept in Zeron's vault, never on this computer.", + "placeholder": "Paste a key", + "save": "Save", + "saved": "{agent} on Cloud now uses this key" + } }, "devices": { "title": "Devices", @@ -3226,7 +3231,8 @@ "show": "Show in Finder", "dismiss": "Dismiss {name}", "saving": "Saving…" - } + }, + "dismiss_error": "Dismiss" }, "browser_api": { "empty": "Enter a website or localhost address.", @@ -3349,12 +3355,6 @@ "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": { @@ -3496,7 +3496,11 @@ }, "compose": { "title": "Put text in the message box", - "detail": "You still send it." + "detail": "It can add to what you've written or replace it. You still send it." + }, + "draft": { + "title": "Read what you type in the message box, and set it aside", + "detail": "It sees your words in any chat before you send them, and can take the draft away and put it back later. It never sends it." }, "network": { "title": "Connect to the internet", diff --git a/apps/zgx/ui/locales/zh-Hans.json b/apps/zgx/ui/locales/zh-Hans.json index fdbecad81..27d1fe97e 100644 --- a/apps/zgx/ui/locales/zh-Hans.json +++ b/apps/zgx/ui/locales/zh-Hans.json @@ -1182,12 +1182,6 @@ "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": { @@ -1637,7 +1631,18 @@ }, "remove_row_description": "使用过它的对话会保留。" }, - "couldnt_switch": "无法切换账户" + "couldnt_switch": "无法切换账户", + "cloud": "云端", + "cloud_keys": { + "title": "API 密钥", + "section_description": "云端也可以使用 API 密钥,而不必登录。在此保存的密钥会成为该代理在每台云端机器上使用的账户,从下一轮开始生效。", + "anthropic": "Anthropic API 密钥", + "openai": "OpenAI API 密钥", + "description": "用于云端的 {agent}。保存在 Zeron 的密钥库中,不会存放在这台电脑上。", + "placeholder": "粘贴密钥", + "save": "保存", + "saved": "云端的 {agent} 现在使用此密钥" + } }, "devices": { "title": "设备", @@ -3226,7 +3231,8 @@ "show": "在访达中显示", "dismiss": "关闭 {name}", "saving": "正在保存…" - } + }, + "dismiss_error": "关闭" }, "browser_api": { "empty": "输入网站或 localhost 地址。", @@ -3349,12 +3355,6 @@ "old": "此版本 Zeron 需要更新的引擎", "old_tip": "引擎版本低于此版本的要求。打开“关于与更新”即可回到稳定版。", "short_old": "需要更新的引擎" - }, - "quit": { - "working": "{n, plural, other {还有 # 个对话正在进行。}}", - "detail": "要在后台继续运行直到完成,还是现在停止?", - "keep": "继续运行", - "stop": "立即停止" } }, "account": { @@ -3496,7 +3496,11 @@ }, "compose": { "title": "在消息输入框中填写文本", - "detail": "仍由你来发送。" + "detail": "它可以补充或替换你写的内容,仍由你来发送。" + }, + "draft": { + "title": "读取你在消息输入框中输入的内容,并可暂存", + "detail": "它能在你发送前看到你在任意聊天中输入的文字,也能把草稿暂时取走、稍后放回。它不会替你发送。" }, "network": { "title": "连接互联网", diff --git a/apps/zgx/ui/screens/chat/completions.luau b/apps/zgx/ui/screens/chat/completions.luau index 8fe079c64..298c9738c 100644 --- a/apps/zgx/ui/screens/chat/completions.luau +++ b/apps/zgx/ui/screens/chat/completions.luau @@ -20,7 +20,6 @@ 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") @@ -198,8 +197,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() - -- (At the draft's end, read there: a long draft isn't copied for it.) - local t = Draft.end_token(b.text) + local text = b.text:get() + local t = mentions.token(text, #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 @@ -214,8 +213,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() - -- (At the draft's end, read there: a long draft isn't copied for it.) - local t = Draft.end_token(b.text) + local text = b.text:get() + local t = mentions.token(text, #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 d631a537c..10038a95a 100644 --- a/apps/zgx/ui/screens/chat/composer.luau +++ b/apps/zgx/ui/screens/chat/composer.luau @@ -17,7 +17,6 @@ -- 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") @@ -717,10 +716,10 @@ local function Composer(): Node end return LinkNotice.of { connection = sessions.connection:get(), - cloud = machine.cloud_link(Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0), + cloud = machine.cloud_link(string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0), host_unreachable = host_unreachable, host = host, - intent = Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0, + intent = string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0, } end) @@ -844,38 +843,24 @@ 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. 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.) + -- that takes the keys after (a rename) has them. (Kept: a reload, or + -- the window opening, isn't an arrival.) 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" - -- (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 + local at = `{now.name}|{now.session or ""}|{now.project or ""}|{pane or ""}` + local was = arrived:peek() + if was == 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 not chat then - arrived:set(at) + if route.take_keys_kept() or was == "?" or not chat then 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 75f61579c..b443d0be4 100644 --- a/apps/zgx/ui/screens/chat/dictation.luau +++ b/apps/zgx/ui/screens/chat/dictation.luau @@ -204,8 +204,7 @@ 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() - -- (Its edits followed, the draft not read.) - value:len() + value:get() 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 2e71304aa..9dae81e70 100644 --- a/apps/zgx/ui/screens/chat/lane_tab.luau +++ b/apps/zgx/ui/screens/chat/lane_tab.luau @@ -28,7 +28,6 @@ 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") @@ -251,53 +250,22 @@ 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:peek()[""] + local put = kept:get()[""] if put then now = { text = put.text, cargo = put.cargo, quotes = put.quotes } end @@ -212,34 +206,6 @@ local function save_new() 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 @@ -263,6 +229,11 @@ function M.showing(): string return here:peek() end +-- The same, read as it changes. +function M.showing_now(): string + return here:get() +end + function M.show(key: string) local was = here:peek() if was == key then @@ -296,15 +267,55 @@ 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 +-- The draft shown, lifted out whole to be put back later (a plugin's +-- `zeron.take_draft`): its words as typed, its chips and the passages +-- quoted in it, the composer cleared. Nil when there's nothing to lift. +function M.lift(): { text: string, cargo: { any }, quotes: { [string]: string } }? + local text, cargo, quotes = M.text:peek(), M.cargo:peek(), M.quotes:peek() + if text == "" and #cargo == 0 then + return nil + end + ui.batch(function() + M.text:set("") + M.cargo:set({}) + M.quotes:set({}) + end) + return { text = text, cargo = cargo, quotes = quotes } +end + +-- A lifted draft put back on the draft shown (`zeron.put_draft`). Onto an +-- empty composer it's as it was; onto words already there, its words follow +-- them after a blank line and its chips follow theirs, its files numbered +-- after those there. +function M.lay(d: { text: string, cargo: { any }, quotes: { [string]: string } }) + local here_text, here_cargo = M.text:peek(), M.cargo:peek() + ui.batch(function() + if here_text == "" and #here_cargo == 0 then + M.text:set(d.text) + M.cargo:set(d.cargo) + M.quotes:set(d.quotes) + return + end + local quotes = table.clone(M.quotes:peek()) + for line, passage in d.quotes do + quotes[line] = passage + end + M.quotes:set(quotes) + if d.text ~= "" then + M.text:set(if here_text == "" then d.text else `{here_text}\n\n{d.text}`) + end + for _, item in d.cargo do + local moved = table.clone(item) + moved.mention = nil + M.push_cargo(nil, moved) + end + end) + M.focus:add(1) +end + -- A passage of the transcript, quoted into the draft (its line marks it) -- or carried as a chip. function M.mention(q: Quote.Quote) @@ -934,37 +945,14 @@ 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 +-- Chat `key`'s draft's words as they are, read without following them +-- (`draft_of` follows them): what a plugin's `zeron.compose` adds to. +function M.peek_text_of(key: string): string if here:peek() == key then - return Draft.size(M.text) + return M.text:peek() end local d = kept:peek()[key] - return if d and type(d.text) == "string" then #d.text else 0 + return if d and type(d.text) == "string" then d.text else "" end -- Chat `key`'s draft changed where it's shown by its id (`field`: "text" @@ -1044,9 +1032,19 @@ local here_device = ui.computed(function(): string? end) -- The accounts' key for a device: "" for this one, whose engine answers --- for itself. +-- for itself; "cloud" for any of Cloud's (a chat's machine, or the +-- account's Cloud device), whose accounts are the vault's, every Cloud +-- machine's alike, as Settings → Providers shows them (this engine answers +-- for "cloud": crates/engine cloud_accounts). A machine can't be asked: +-- its accounts aren't its own, and asking would wake it. local function account_key(device: string?): string - return if device == nil or device == here_device:get() then "" else device + if device == nil or device == here_device:get() then + return "" + end + if cloud.on_device(device) or device == cloud.device() then + return "cloud" + end + return device end -- Models of Cursor's own pool (Composer, Grok), not the third-party one. diff --git a/apps/zgx/ui/screens/language.luau b/apps/zgx/ui/screens/language.luau index e81ba6783..adc609858 100644 --- a/apps/zgx/ui/screens/language.luau +++ b/apps/zgx/ui/screens/language.luau @@ -43,22 +43,6 @@ 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/panel/browser.luau b/apps/zgx/ui/screens/panel/browser.luau index d204c8c90..6289bada6 100644 --- a/apps/zgx/ui/screens/panel/browser.luau +++ b/apps/zgx/ui/screens/panel/browser.luau @@ -433,6 +433,32 @@ Page = ui.component(function(props: Props): Node -- Annotation mode, as the bar's toggle shows it. local mode = ui.use_state(annotating:peek()[id] == true) + -- This navigation, as the page tells it: whether its own document came + -- (committed: the page's content, not the host's error page), whether + -- its process ended after, and whether its failure was dismissed. Each + -- starts over with the next navigation (a reload, Try again). + -- A failure after the page committed is no page missing: the page drew, + -- and it's what to show (a 429 or 5xx page, Google's "unusual traffic" + -- check: WebView2 calls a page with an error status a failed + -- navigation; WebKit a load interrupted once the page was in). + local committed = ui.use_state(false) + local crashed = ui.use_state(false) + local dismissed = ui.use_state(false) + -- The page that didn't load, over where it would be (`trouble`). + local function failed(): boolean + if not has_page(web) or web.error == nil or dismissed:get() then + return false + end + return crashed:get() or not committed:get() + end + local function dismiss(): boolean + if not failed() then + return false + end + dismissed:set(true) + return true + end + -- Made with an address and no page yet (a saved tab, a link opened): -- the page loads. (A reload finds the page where it was: nothing moves.) -- A new tab with a home page set opens on it (once: the start page is @@ -556,7 +582,7 @@ Page = ui.component(function(props: Props): Node end end local function start_annotating() - if not has_page(web) or web.error then + if not has_page(web) or failed() then if mode:peek() then mode:set(false) end @@ -969,6 +995,11 @@ Page = ui.component(function(props: Props): Node return true end if e.key == "Escape" then + -- (Nothing typed to take back: the failure over the page + -- dismissed, as from anywhere else in the tab.) + if address:peek() == last_url and dismiss() then + return true + end -- (Back to the page's address, as a browser's.) suggestions.quiet(last_url) address:set(last_url) @@ -1064,7 +1095,7 @@ Page = ui.component(function(props: Props): Node tooltip = ui.t("panel.browser.annotate_tooltip", { key = ui.shortcut_text(if shell_keys then shell_keys.combo("panel.annotate") else "cmd+shift+i") }):get(), tooltip_side = "bottom", disabled = function(): boolean - return not can_leave() or web.error ~= nil + return not can_leave() or failed() end, on_change = function(on: boolean) if on then @@ -1533,7 +1564,21 @@ Page = ui.component(function(props: Props): Node on_state(value.state) end end, + on_error = function(url: string, _why: string) + -- (The page's process ended: no navigation's, and nothing left + -- drawn, committed or not.) + if url == "" then + crashed:set(true) + end + end, on_navigate = function(e: any) + if type(e) == "table" and e.kind == "started" then + committed:set(false) + crashed:set(false) + dismissed:set(false) + elseif type(e) == "table" and e.kind == "committed" then + committed:set(true) + end -- Another page: its picker is gone with the last, so the mode -- ends (the shipping app's; its marks come back by restore). if type(e) == "table" and e.kind == "committed" and annotating:peek()[id] then @@ -1572,9 +1617,7 @@ Page = ui.component(function(props: Props): Node or string.find(l, "internet connection", 1, true) ~= nil or string.find(l, "not connected", 1, true) ~= nil end - local trouble = ui.show(function(): boolean - return has_page(web) and web.error ~= nil - end, ui.column { + local trouble = ui.show(failed, ui.column { absolute = true, left = 0, right = 0, @@ -1623,6 +1666,14 @@ Page = ui.component(function(props: Props): Node web:reload() end, }, + -- What the page drew anyway, under (Escape too): until the + -- next navigation. + secondary = { + label = ui.t("panel.browser.dismiss_error") :: any, + on_click = function() + dismiss() + end, + }, } end, { w_full = true, grow = 1, min_h = 0 }), }) @@ -1634,6 +1685,14 @@ Page = ui.component(function(props: Props): Node w_full = true, grow = 1, min_h = 0, + -- A bare Escape nothing in the tab took (the error's buttons, the + -- bar's): the failure dismissed, the page under it shown. + on_key = function(e: any): boolean? + if e.key == "Escape" and not (e.shift or e.cmd or e.alt or e.ctrl) and dismiss() then + return true + end + return nil + end, bar, problem_line, find_bar, diff --git a/apps/zgx/ui/screens/plugins/init.luau b/apps/zgx/ui/screens/plugins/init.luau index 7d3cbb4b4..372587175 100644 --- a/apps/zgx/ui/screens/plugins/init.luau +++ b/apps/zgx/ui/screens/plugins/init.luau @@ -17,6 +17,8 @@ -- zeron.device { id, name } where you're working -- zeron.devices { { id, name, here } } your devices -- zeron.appearance "light" | "dark" +-- zeron.draft { session?, text, attachments = { { kind, name } } } +-- the composer's draft ("draft") -- - **What records are made from**, for the host alone (no plugin reads -- these; each reads its own `plugin..records`, holding only what it -- was allowed, src/plugins/records.rs): @@ -43,7 +45,6 @@ local store = require("@app/screens/settings/store") local PrivateText = require("@ds/private_text") local state = require("@app/screens/plugins/state") local page = require("@app/screens/plugins/page") -local chat = require("@app/screens/chat") local ChatShared = require("@app/screens/chat/shared") local Cargo = require("@ds/composer_cargo") @@ -166,6 +167,128 @@ ui.effect(function() put(out.appearance, if kind == "light" or kind == "high-contrast-light" then "light" else "dark") end) +-- What a draft carries, as plugins are told it: each chip's kind and name +-- alone, never a file's path. +local function attachments_of(cargo: { any }): { any } + local list = {} + for _, item in cargo do + table.insert(list, { kind = tostring(item.kind or "file"), name = tostring(item.name or "") }) + end + return list +end + +-- The composer's draft, for plugins allowed "draft" (`zeron.draft()`): the +-- chat it's for (nil, a new chat), its words, and what it carries. +-- Written only while a plugin that's on may read it: otherwise no key +-- typed copies the draft (a long one, every key) for no one. +local draft_out = ui.shared("zeron.draft", nil :: any) +local draft_read = ui.computed(function(): boolean + for _, it in state.now:get().list or {} do + if it.status == "on" and table.find(it.permissions or {}, "draft") then + return true + end + end + return false +end) +ui.effect(function() + if not draft_read:get() then + if draft_out:peek() ~= nil then + put(draft_out, nil) + end + return + end + local key = ChatShared.showing_now() + put(draft_out, { + session = if key ~= "" then key else ui.null, + text = ChatShared.text:get(), + attachments = attachments_of(ChatShared.cargo:get()), + }) +end) + +-- The drafts plugins took (`zeron.take_draft`), each plugin's by its own +-- keys, kept whole here so a plugin never holds its files' paths. Kept for +-- the run, as chats' drafts are; a reload of the scripts keeps them. +-- Each plugin is told what it keeps (`plugin..drafts`): by key, the +-- chat it was taken from, its words, what it carries and when. +local DRAFTS_KEPT = 16 +local shelves = ui.shared("plugins.drafts", {} :: { [string]: { [string]: any } }) +local shelves_told: { [string]: boolean } = {} +ui.effect(function() + local all = shelves:get() + for plugin in all do + shelves_told[plugin] = true + end + for plugin in shelves_told do + local told: { [string]: any } = {} + for key, d in all[plugin] or {} do + told[key] = { + session = d.session or ui.null, + text = d.text, + attachments = attachments_of(d.cargo), + at = d.at, + } + end + ui.shared(`plugin.{plugin}.drafts`, {} :: any):set(told) + end +end) + +-- A plugin's ask of the draft (the host checked "draft" and that it's +-- take, put, swap or drop with a key). +local function draft_asked(plugin: string, op: string, key: string) + local all = table.clone(shelves:peek()) + local mine = table.clone(all[plugin] or {}) + -- (Room for one more, unless it goes where that key kept one.) + local count = 0 + for _ in mine do + count += 1 + end + local full = mine[key] == nil and count >= DRAFTS_KEPT + local function kept_now(d: any, from: string): any + return { text = d.text, cargo = d.cargo, quotes = d.quotes, session = if from ~= "" then from else nil, at = os.time() } + end + if op == "take" then + if full then + return + end + local from = ChatShared.showing() + local d = ChatShared.lift() + if d == nil then + return + end + mine[key] = kept_now(d, from) + elseif op == "swap" then + -- The draft shown and what `key` kept, traded: either may be + -- nothing, so one call sets a draft aside, brings it back, or both. + local was = mine[key] + local from = ChatShared.showing() + if full then + return + end + local d = ChatShared.lift() + if d == nil and was == nil then + return + end + if was ~= nil then + ChatShared.lay(was) + end + mine[key] = if d ~= nil then kept_now(d, from) else nil + elseif op == "put" then + local d = mine[key] + if d == nil then + return + end + mine[key] = nil + ChatShared.lay(d) + elseif op == "drop" then + if mine[key] == nil then + return + end + mine[key] = nil + end + all[plugin] = if next(mine) ~= nil then mine else nil + shelves:set(all) +end + -- What a failed plugin's notice offers (a toast's action: `ds/toast`). ds.toast.action("plugins.open_folder", function(id: string) state.open_folder(id) @@ -185,8 +308,26 @@ local function run(r: any) elseif type(to) == "table" and type(to.settings) == "string" and to.settings ~= "account" then route.settings(to.settings) end - elseif r.kind == "compose" and type(r.value) == "string" then - chat.store.insert(string.sub(r.value, 1, 20000)) + elseif r.kind == "compose" and (type(r.value) == "string" or (type(r.value) == "table" and type(r.value.text) == "string")) then + -- `zeron.compose(text, { replace, session })` (the host checked + -- "compose"; its words alone come as a string): the words put in a + -- chat's draft, the one shown unless it names another ("" a new + -- chat's), in place of its words (its chips stay) or after them, on + -- a line of their own. Unsent: the person sends it. Shown, the + -- composer takes the keys. + local v = if type(r.value) == "string" then { text = r.value } else r.value + local words = string.sub(v.text, 1, 20000) + local shown = ChatShared.showing() + local key = if type(v.session) == "string" and #v.session <= 200 then v.session else shown + if v.replace ~= true then + local now = ChatShared.peek_text_of(key) + local sep = if now == "" or string.sub(now, -1) == "\n" then "" else "\n" + words = now .. sep .. words + end + ChatShared.set_draft_of(key, "text", words) + if key == shown then + ChatShared.focus:add(1) + end elseif r.kind == "compose_comment" and type(r.value) == "table" and type(r.title) == "string" then -- A plugin's review comment on a file or a diff (the host checked -- "compose"): a comment chip on the session's draft, as the files @@ -207,6 +348,13 @@ local function run(r: any) session = if type(c.session) == "string" then c.session else nil, } end + elseif r.kind == "draft" and type(r.value) == "table" and type(r.title) == "string" and r.title ~= "" then + -- A plugin taking the draft, putting one back, swapping the two, or + -- letting one go. + local op, key = r.value.op, r.value.key + if type(op) == "string" and type(key) == "string" then + draft_asked(r.title, op, key) + end elseif r.kind == "panel_tab" and type(r.value) == "table" then -- A plugin's tab in a chat's panel (the host checked "panel.tabs"). local ok, Panel = pcall(require, "@app/screens/panel") diff --git a/apps/zgx/ui/screens/plugins/words.luau b/apps/zgx/ui/screens/plugins/words.luau index 911f70bd6..3e18ba196 100644 --- a/apps/zgx/ui/screens/plugins/words.luau +++ b/apps/zgx/ui/screens/plugins/words.luau @@ -298,6 +298,9 @@ function M.permission(item: any, permission: string): Line return line(false, "map-arrow-right", "plugins.words.permission.navigate.title") elseif permission == "compose" then return line(false, "chat-round-line", "plugins.words.permission.compose.title", "plugins.words.permission.compose.detail") + elseif permission == "draft" then + -- (What you type, read as you type it: said plainly.) + return line(false, "pen", "plugins.words.permission.draft.title", "plugins.words.permission.draft.detail") elseif permission == "network" then -- The places it names (any site, with "*"), those on this device or -- your network said so, and risky then. diff --git a/apps/zgx/ui/screens/settings/chimes.luau b/apps/zgx/ui/screens/settings/chimes.luau index 65f5cb1a8..6b8447374 100644 --- a/apps/zgx/ui/screens/settings/chimes.luau +++ b/apps/zgx/ui/screens/settings/chimes.luau @@ -1,9 +1,19 @@ -- The sessions' chimes, as the shipping app plays them (crates/ui --- shell.rs, its one session detector): a chat turning done, waiting on you, --- or failed plays its sound, if Settings → Notifications has it on. A --- chat's first appearance only sets where it stands (a launch, a sync, a --- hot reload never ring), and what it was is kept in state, so a reload --- picks up where it was. +-- shell.rs, its one session detector; crates/engine desktop_notify's +-- `ping_since`): a chat waiting on you, or failed, plays its sound as its +-- status turns so; a turn finished plays "done", if Settings → +-- Notifications has it on. A chat's first appearance only sets where it +-- stands (a launch, a sync, a hot reload never ring), and what it was is +-- kept in state, so a reload picks up where it was. +-- +-- "Finished" is the turn mark moving (`turn`, the session's +-- `lastCompletedTurn`), never the status "done": that's only activity you +-- haven't seen, and your own message makes it too (issue #1437). Nor does +-- every move ring: one published with the next turn already running is +-- your message carrying it on (a steer, a queued message taken), and a +-- send that replaces the agent's runtime marks the old turn's end a moment +-- before your turn starts, so a finish waits `SETTLE` to stand; a turn +-- going on (or asking, or failing) meanwhile takes it with it. -- -- And the window's focus, told to the engine (`SetViewportFocus`), which -- holds its banners while Zeron is the app in front if Notifications says @@ -16,39 +26,120 @@ local prefs = require("@app/screens/settings/prefs") local M = {} --- Each chat's status as last seen, by id ("" for idle). -local seen = ui.state("settings_chimes_seen", {} :: { [string]: string }) +type Seen = { status: string, turn: string? } --- What a status turning on plays. +-- Each chat as last seen, by id: its status ("" for idle) and finished turn. +local seen = ui.state("settings_chimes_seen_turns", {} :: { [string]: Seen }) + +-- Finishes waiting to stand, by chat: the turn, and when it rings +-- (`ui.now()`, ms). +local settling = ui.state("settings_chimes_settling", {} :: { [string]: { turn: string, at: number } }) + +-- How long a finish waits to stand before it rings (the engine's banners' +-- `DONE_SETTLE`). +local SETTLE = 2000 + +-- What a status turning on plays. ("done" isn't one: see above.) local SOUND: { [string]: string } = { - done = "done", input = "request", failed = "attention", } +-- A turn going on, or asking, or failing: nothing has finished. +local function busy(status: string): boolean + return status == "working" or status == "waiting" or status == "input" or status == "failed" +end + +-- One chime for many at once: the most urgent. +local function louder(ring: string?, sound: string): string + if ring == nil or sound == "attention" or (sound == "request" and ring == "done") then + return sound + end + return ring +end + +local function play(ring: string?) + if ring and prefs.chimes(ring) then + host.invoke("sound.play", ring) + end +end + ui.effect(function() local now = sessions.now:get() if now.state ~= "ready" then return end local before = seen:peek() - local after: { [string]: string } = {} + local after: { [string]: Seen } = {} + local waits = table.clone(settling:peek()) + local moved = false local ring: string? = nil for _, c in now.chats do local status = c.status or "" - after[c.id] = status local was = before[c.id] - if was ~= nil and was ~= status and SOUND[status] then - -- (One chime for many at once: the most urgent.) - local sound = SOUND[status] - if ring == nil or sound == "attention" or (sound == "request" and ring == "done") then - ring = sound + -- (A session row gone for a while, a host restarting, keeps the turn + -- last seen: its coming back is no finish.) + after[c.id] = { status = status, turn = c.turn or (was and was.turn) } + if busy(status) and waits[c.id] then + waits[c.id] = nil + moved = true + end + if was ~= nil then + if was.status ~= status and SOUND[status] then + ring = louder(ring, SOUND[status]) + end + if c.turn ~= nil and c.turn ~= was.turn and c.turn_fresh ~= false and not busy(status) then + waits[c.id] = { turn = c.turn, at = ui.now() + SETTLE } + moved = true end end end + -- (A chat gone takes its finish with it.) + for id in waits do + if after[id] == nil then + waits[id] = nil + moved = true + end + end seen:set(after) - if ring and prefs.chimes(ring) then - host.invoke("sound.play", ring) + if moved then + settling:set(waits) + end + play(ring) +end) + +-- The finishes due by `upto` have stood: they ring, once each moment (one +-- chime for many). (The timer that calls this is the deadline: its clock +-- needn't be `ui.now()`'s, so that's never asked again here.) +local function stand(upto: number) + local waits = settling:peek() + local left = {} + local ring: string? = nil + for id, w in waits do + if w.at <= upto then + local now = seen:peek()[id] + if now and now.turn == w.turn and not busy(now.status) then + ring = louder(ring, "done") + end + else + left[id] = w + end + end + settling:set(left) + play(ring) +end +ui.effect(function() + local soonest: number? = nil + for _, w in settling:get() do + soonest = if soonest then math.min(soonest, w.at) else w.at + end + -- (A timer made in an effect belongs to its run: a new finish, or one + -- taken, sets it again.) + if soonest then + local upto: number = soonest + ui.after(math.max(0, upto - ui.now()), function() + stand(upto) + end) end end) diff --git a/apps/zgx/ui/screens/settings/devices.luau b/apps/zgx/ui/screens/settings/devices.luau index 9a88ef06c..06637b28c 100644 --- a/apps/zgx/ui/screens/settings/devices.luau +++ b/apps/zgx/ui/screens/settings/devices.luau @@ -169,7 +169,9 @@ local icon_of = M.icon -- choosing one calls `choose` with its id. Built once: its options follow -- the devices as they come, go and are renamed, and the choice follows -- `shown`. -function M.picker(shown: () -> string?, choose: (id: string) -> ()): Node +-- (`extra`: more choices after the computers, each `{ value, label, icon }`: +-- Providers' Cloud, whose accounts every Cloud machine uses.) +function M.picker(shown: () -> string?, choose: (id: string) -> (), extra: (() -> { any })?): Node local function computers(): { sessions.Device } return sessions.computers(sessions.devices:get(), true) end @@ -181,9 +183,12 @@ function M.picker(shown: () -> string?, choose: (id: string) -> ()): Node end return "" end + local function extras(): { any } + return if extra then extra() else {} + end return ui.show( function(): boolean - return #computers() > 1 + return #computers() > 1 or #extras() > 0 end, ui.component(function(): Node local chosen = ui.use_state(shown() or here()) @@ -208,6 +213,9 @@ function M.picker(shown: () -> string?, choose: (id: string) -> ()): Node description = if not d.here and d.online == false then ui.t("settings.devices.offline") else nil, }) end + for _, e in extras() do + table.insert(options, e) + end return options end, on_change = choose, diff --git a/apps/zgx/ui/screens/settings/general.luau b/apps/zgx/ui/screens/settings/general.luau index e02e59628..98adcfc06 100644 --- a/apps/zgx/ui/screens/settings/general.luau +++ b/apps/zgx/ui/screens/settings/general.luau @@ -28,7 +28,6 @@ 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") }, @@ -47,9 +46,6 @@ 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 }) @@ -79,7 +75,6 @@ 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. @@ -259,26 +254,6 @@ 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/screens/settings/providers.luau b/apps/zgx/ui/screens/settings/providers.luau index 2876379aa..d1e13d9a3 100644 --- a/apps/zgx/ui/screens/settings/providers.luau +++ b/apps/zgx/ui/screens/settings/providers.luau @@ -23,6 +23,7 @@ local prefs = require("@app/screens/settings/prefs") local registry = require("@account/registry") local add_agent = require("@app/screens/settings/add_agent") local sign_in_terminal = require("@app/screens/settings/sign_in_terminal") +local cloud = require("@account/cloud") local M = {} @@ -108,6 +109,20 @@ local function here(): string? end return nil end +-- Cloud, as one choice after the computers (never its machines): the +-- accounts every Cloud machine uses, kept by the vault, which the engine +-- answers for (crates/engine cloud_accounts: Claude Code and Codex, their +-- logins and API keys). Offered while Cloud is on. +local CLOUD = "cloud" +local function on_cloud(): boolean + return now().target == CLOUD +end +local function cloud_choice(): { any } + if cloud.on() or on_cloud() then + return { { value = CLOUD, label = ui.t("settings.providers.cloud"):get(), icon = "cloud" } } + end + return {} +end -- The device shown: the engine's target, else this one. local function shown_device(): string? return now().target or here() @@ -115,6 +130,9 @@ end -- (In the locale shown: read where it's drawn.) local function device_name(): string local id = shown_device() + if id == CLOUD then + return ui.t("settings.providers.cloud"):get() + end for _, d in computers() do if d.id == id then return if d.here then ui.t("settings.providers.this_mac"):get() else d.name @@ -124,8 +142,11 @@ local function device_name(): string end local function choose_device(id: string) agents.target(if id == here() then nil else id) - -- (The registry as that device sees it: what it has added, its builds.) - registry.load() + -- (The registry as that device sees it: what it has added, its builds. + -- Cloud runs only Zeron's own.) + if id ~= CLOUD then + registry.load() + end end -- Which of the page's views shows: the agents, the registry ("add"), or @@ -152,7 +173,8 @@ local function kind(): string if s == "looking" or s == "unavailable" then return s end - return "found" + -- (Cloud's page is another: its accounts, its keys, nothing to add.) + return if now().target == "cloud" then "cloud" else "found" end -- Which agent's settings are open (none: ""). @@ -223,6 +245,9 @@ local function card(row: State): Node -- no accounts, and signs in its own way. (Its updates come as any -- agent's: the engine's update coordinator, the registry its feed.) local from_registry = (first :: any).registry == true + -- Cloud's (made for it: the cards are keyed by the device shown): on + -- every Cloud machine, its accounts alone managed here. + local cloud_card = on_cloud() -- The switch follows the engine; a flip goes to it, and its answer -- comes back in the row. local on = ui.use_state(first.enabled) @@ -234,7 +259,7 @@ local function card(row: State): Node if flipped ~= said then on:set(said) end - elseif flipped ~= said then + elseif flipped ~= said and not cloud_card then agents.set_enabled(id, flipped) end end) @@ -250,6 +275,7 @@ local function card(row: State): Node return (row:get() :: any).updating == true end, sign_in_only = from_registry, + fixed = cloud_card, login_note = function(): string? return (row:get() :: any).login_note end, @@ -329,7 +355,7 @@ local function card(row: State): Node end return nil end, - settings = true, + settings = not cloud_card, on_settings = function() load_subagents() open_settings:set(id) @@ -421,7 +447,13 @@ local function cards(): Node gap = "$space.4", ui.each(ui.computed(installed), function(_a: agents.Agent, _i: number, row: State): Node return card(row) - end, { key = by_id }), + end, { + -- (By the device shown too: a card is made for its device, + -- Cloud's unlike a computer's.) + key = function(a: agents.Agent): string + return `{now().target or ""}|{a.id}` + end, + }), } ) end @@ -490,6 +522,85 @@ local function more(): Node ) end +-- Cloud's API keys: Anthropic's for Claude Code, OpenAI's for Codex, kept +-- by the vault. A key saved becomes its agent's account in use, every Cloud +-- machine's from its next turn (as signing in does). +local CLOUD_KEYS = { + { provider = "anthropic-key", agent = "Claude Code", label = "settings.providers.cloud_keys.anthropic" }, + { provider = "openai-key", agent = "Codex", label = "settings.providers.cloud_keys.openai" }, +} +local function cloud_key_row(k: { provider: string, agent: string, label: string }): Node + return ui.component(function(): Node + local value = ui.use_state("") + local busy = ui.use_state(false) + local problem = ui.use_state("") + local function save() + local key = value:peek() + if busy:peek() or key == "" then + return + end + busy:set(true) + problem:set("") + host.call("agents.cloud_key", { provider = k.provider, key = key, device = cloud.device() or "" }):after(function() + busy:set(false) + value:set("") + toast.show { title = ui.t("settings.providers.cloud_keys.saved", { agent = k.agent }):get(), tone = "success" } + end, function(err: any) + busy:set(false) + problem:set(if type(err) == "table" and err.message then tostring(err.message) else tostring(err)) + end) + end + return ds.Settings.Row { + label = ui.t(k.label), + description = function(): string + local p = problem:get() + return if p ~= "" then p else ui.t("settings.providers.cloud_keys.description", { agent = k.agent }):get() + end, + control = ui.row { + gap = "$space.2", + items = "center", + ds.TextField { + value = value, + password = true, + w = 220, + size = "sm", + name = ui.t(k.label), + placeholder = ui.t("settings.providers.cloud_keys.placeholder"), + on_commit = function() + save() + end, + }, + ds.Button { + ui.t("settings.providers.cloud_keys.save"), + tone = "secondary", + size = "sm", + busy = function(): boolean + return busy:get() + end, + disabled = function(): boolean + return value:get() == "" + end, + on_click = save, + }, + }, + } + end)({}) +end +local function cloud_keys(): Node + return ds.Settings.Section { + title = ui.t("settings.providers.cloud_keys.title"), + description = ui.t("settings.providers.cloud_keys.section_description"), + ds.Settings.Group { + ui.column { + w_full = true, + cloud_key_row(CLOUD_KEYS[1]), + ds.Settings.Rule(), + cloud_key_row(CLOUD_KEYS[2]), + }, + }, + } +end + local function body(k: string): Node if k == "looking" then return ds.ProviderCard.Loading() @@ -498,6 +609,9 @@ local function body(k: string): Node return device_name() end, agents.load) end + if k == "cloud" then + return ui.column { w_full = true, gap = "$space.6", cards(), cloud_keys() } + end return ui.column { w_full = true, gap = "$space.6", cards(), more() } end @@ -767,7 +881,7 @@ local function agents_page(): Node -- Agents are per device: which one's (a choice once there's more -- than this one). -- (The same choice as Plugins': screens/settings/devices.) - trail = devices_page.picker(shown_device, choose_device), + trail = devices_page.picker(shown_device, choose_device, cloud_choice), -- (At the top right, on the title's line, as Plugins' is.) trail_top = true, ui.presence(kind, body, { w_full = true, enter = { opacity = { 0, 1 }, duration = 120 }, exit = { opacity = 0, duration = 60 }, mode = "wait" }), @@ -792,6 +906,10 @@ function M.view(): Node -- (Once a visit: a hot reload doesn't ask again.) The page opens on the -- agents; the registry is looked at too, for what it has to add. visits.once("providers", function() + -- (Whether Cloud is on, for its choice.) + if cloud.now:peek().phase == "unknown" then + cloud.load() + end agents.load() registry.load() if view:peek() ~= "agents" then diff --git a/apps/zgx/ui/settings_stub.luau b/apps/zgx/ui/settings_stub.luau index 524fa0068..3d0e4b6e7 100644 --- a/apps/zgx/ui/settings_stub.luau +++ b/apps/zgx/ui/settings_stub.luau @@ -124,7 +124,6 @@ local engine: { [string]: any } = { }, GetTitleSettings = {}, GetNativeSubagents = { switchable = { "claude-code" }, off = {} }, - GetBackgroundSettings = { keepRunningAfterQuit = true }, } local banners = { desktop = true, background_only = true, agent_updates = true } @@ -296,7 +295,7 @@ local answers: { [string]: (arg: any) -> any } = { local w = table.clone(engine[get]) w.settings = arg.params engine[get] = w - elseif m == "SetTitleSettings" or m == "SetBackgroundSettings" then + elseif m == "SetTitleSettings" then engine[get] = arg.params end return settled(engine[get]) @@ -433,6 +432,21 @@ end -- a switch of device shows whose. local agents = require("@account/agents") local function agents_of(target: string?): any + -- Cloud (the engine answers for it from the vault): Claude Code and + -- Codex, always installed; Codex with an account, so on. + if target == "cloud" then + local function one(id: string, name: string, mark: string, accounts: { any }): any + return { id = id, name = name, mark = mark, installed = true, can_install = false, enabled = #accounts > 0, version = nil, installing = false, login = "idle", accounts = accounts } + end + return { + state = "ready", + target = target, + list = { + one("claude-code", "Claude Code", "claude", {}), + one("codex", "Codex", "openai", { { id = "c1", email = "ada@example.com", plan = "Pro", session = 0.1, week = 0.2, active = true } }), + }, + } + end local name = if target then "Codex" else "Claude Code" local id = if target then "codex" else "claude-code" return { diff --git a/apps/zgx/ui/templates/zeron-v2/.base.json b/apps/zgx/ui/templates/zeron-v2/.base.json index 571650df2..a29cc3f9f 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": "f8f9267383bb3d27", - "screens/chat/lane_tab.luau": "8173218223a811fa", + "screens/chat/composer.luau": "9c0c79de53eae373", + "screens/chat/lane_tab.luau": "12c199c168515f49", "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 f523d70aa..d6cf62303 100644 --- a/apps/zgx/ui/templates/zeron-v2/screens/chat/composer.luau +++ b/apps/zgx/ui/templates/zeron-v2/screens/chat/composer.luau @@ -37,7 +37,6 @@ 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") @@ -587,10 +586,10 @@ local function Composer(): Node end return LinkNotice.of { connection = sessions.connection:get(), - cloud = machine.cloud_link(Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0), + cloud = machine.cloud_link(string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0), host_unreachable = host_unreachable, host = host, - intent = Draft.has_text(shared.text) or #shared.cargo:get() > 0 or #queue:get() > 0, + intent = string.find(shared.text:get(), "%S") ~= nil or #shared.cargo:get() > 0 or #queue:get() > 0, } end) @@ -825,38 +824,24 @@ 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. 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.) + -- that takes the keys after (a rename) has them. (Kept: a reload, or + -- the window opening, isn't an arrival.) 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" - -- (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 + local at = `{now.name}|{now.session or ""}|{now.project or ""}|{pane or ""}` + local was = arrived:peek() + if was == 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 not chat then - arrived:set(at) + if route.take_keys_kept() or was == "?" or not chat then 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 2d3d0afe8..f4e3e08bf 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,7 +29,6 @@ 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") @@ -506,7 +505,7 @@ local function composer(chat: string, view: any, name: string, focus: State boolean, ms: number?): boolean + for _ = 1, math.ceil((ms or 5000) / 50) do + if ok() then + return true + end + app:wait('"never shown, only waited past"', { timeout_ms = 50 }) + end + return ok() +end + +local function panel(): any + local app = mount("panel_fixture.luau") + app:resize(900, 640) + app:frames(10) + expect(app:error()).to_be_nil() + expect(app:wait("#panel-empty", { timeout_ms = 15000 })).to_be(true) + app:click("#panel-open-browser") + expect(app:wait(FIELD, { timeout_ms = 15000 })).to_be(true) + return app +end + +local function calls(app: any): string + return app:state("test_web_calls") or "" +end + +-- To google.com/sorry, the page loaded (the field shows its address). +local URL = "https://www.google.com/sorry/index" +local function go(app: any): string + app:click("#outside") + app:frames(2) + app:type(FIELD, URL) + app:key("Enter") + app:frames(10) + expect(eventually(app, function(): boolean + return app:tree(FIELD):find(`="{string.sub(URL, 1, 36)}`, 1, true) ~= nil + end)).to_be(true) + app:frames(5) + return app:state("test_web_last_id") +end + +-- The page moving on, as the host tells it (`started`, `committed`). +local function navigate(app: any, kind: string) + app:set_state("test_web_nav_kind", kind) + app:command("test.web_navigate") + app:frames(5) +end + +-- The load failing, as the host has it: the error on the page's state. +local function fail(app: any, id: string, why: string) + app:set_state(`test_web_error_{id}`, why) + app:frames(10) +end + +-- The page under the overlay is the fixture's stand-in (`page `). +local function page_shown(app: any): boolean + return app:has("#stub-web") +end + +test("browser load error: a page that never came shows the failure, Try again and Dismiss", function() + local app = panel() + local id = go(app) + navigate(app, "started") + fail(app, id, "A server with the specified hostname could not be found.") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) + expect(app:has(RETRY)).to_be(true) + expect(app:has(DISMISS)).to_be(true) + expect(app:tree('role:checkbox "Annotate"')).to_contain("disabled") +end) + +test("browser load error: Dismiss hides the failure and the page stays", function() + local app = panel() + local id = go(app) + navigate(app, "started") + fail(app, id, "The page could not be loaded.") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) + app:click(DISMISS) + app:frames(10) + expect(app:has(FAILED)).to_be(false) + expect(app:has(RETRY)).to_be(false) + expect(page_shown(app)).to_be(true) + expect(app:has(`"page {URL}"`)).to_be(true) + -- Only this navigation's: the next one's failure shows again. + navigate(app, "started") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) +end) + +test("browser load error: Escape dismisses", function() + local app = panel() + local id = go(app) + navigate(app, "started") + fail(app, id, "The page could not be loaded.") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) + -- (From the address field, nothing typed in it to take back.) + app:click(FIELD) + app:frames(2) + app:key("Escape") + app:frames(10) + expect(app:has(FAILED)).to_be(false) + expect(page_shown(app)).to_be(true) +end) + +test("browser load error: Escape from the failure's own buttons dismisses", function() + local app = panel() + local id = go(app) + navigate(app, "started") + fail(app, id, "The page could not be loaded.") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) + -- (Tab from the field: past the bar's ⋯ to Try again.) + app:click(FIELD) + app:frames(2) + for _ = 1, 4 do + if app:tree(RETRY):find("focused", 1, true) then + break + end + app:key("Tab") + app:frames(2) + end + expect(app:tree(RETRY)).to_contain("focused") + app:key("Escape") + app:frames(10) + expect(app:has(FAILED)).to_be(false) + expect(page_shown(app)).to_be(true) + expect(calls(app)).never.to_contain("reload") +end) + +test("browser load error: Try again reloads the page", function() + local app = panel() + local id = go(app) + navigate(app, "started") + fail(app, id, "Could not connect to the server.") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) + app:click(RETRY) + app:frames(5) + expect(calls(app)).to_contain("reload") +end) + +test("browser load error: a page that came and drew, with an error status, shows no failure", function() + local app = panel() + local id = go(app) + -- WebView2: the server's own page (a 429) committed, then the + -- navigation reported failed, in the default words. + navigate(app, "started") + navigate(app, "committed") + fail(app, id, "The page could not be loaded.") + expect(app:has(FAILED)).to_be(false) + expect(app:has(RETRY)).to_be(false) + expect(page_shown(app)).to_be(true) + -- (And the page's controls are the page's: annotate isn't held off.) + expect(app:tree('role:checkbox "Annotate"')).never.to_contain("disabled") + -- A reload that never commits is a failure again. + navigate(app, "started") + expect(app:wait(FAILED, { timeout_ms = 3000 })).to_be(true) +end) + +test("browser load error: no network still says offline, with Dismiss", function() + local app = panel() + local id = go(app) + navigate(app, "started") + fail(app, id, "The internet connection appears to be offline.") + expect(app:wait("\"You're offline\"", { timeout_ms = 3000 })).to_be(true) + expect(app:has(RETRY)).to_be(true) + expect(app:has(DISMISS)).to_be(true) +end) diff --git a/apps/zgx/ui/tests/chat_draft_lift.test.luau b/apps/zgx/ui/tests/chat_draft_lift.test.luau new file mode 100644 index 000000000..1c17c1924 --- /dev/null +++ b/apps/zgx/ui/tests/chat_draft_lift.test.luau @@ -0,0 +1,60 @@ +-- A plugin allowed "draft" takes the composer's draft whole and puts it back +-- (`zeron.take_draft`, `put_draft`, `swap_draft`: screens/plugins keeps it +-- between, through screens/chat/shared's `lift` and `lay`). Taken, the +-- composer is clear; put back in another chat, its words and its chips are +-- as they were and go with the message; put back over words already there, +-- they follow those. + +local function open(app: any, chat: string) + app:command(`go_{chat}`) + app:frames(30) +end + +test("a draft taken in one chat is put back in another whole, chips and all", function() + local app = mount("chat_fixture.luau") + app:resize(900, 700) + open(app, "s2") + app:type('role:textbox "Message"', "Wrong chat prompt") + app:command("mention_s1") + app:frames(10) + app:command("draft_lift") + app:frames(10) + expect(app:state("test_lifted")).to_be("Wrong chat prompt|1") + -- Taken: nothing left on this chat's draft. + expect(app:has('#chat-composer >> role:button "Long chat"')).to_be(false) + expect(app:has('#chat-composer >> ~"Wrong chat prompt"')).to_be(false) + -- Put back in another chat, and sent from there. + open(app, "s3") + app:command("draft_lay") + app:frames(30) + expect(app:has('#chat-composer >> ~"Wrong chat prompt"')).to_be(true) + app:key("Enter") + app:frames(30) + expect(app:state("calls")).to_contain("send:s3:Wrong chat prompt\n\nChats attached:\n- [Long chat](zeron-chat:s1)") + expect(app:error()).to_be(nil) +end) + +test("an empty draft isn't taken", function() + local app = mount("chat_fixture.luau") + app:resize(900, 700) + open(app, "s2") + app:command("draft_lift") + app:frames(10) + expect(app:state("test_lifted")).to_be("nothing") +end) + +test("a draft put back over words already there follows them", function() + local app = mount("chat_fixture.luau") + app:resize(900, 700) + open(app, "s2") + app:type('role:textbox "Message"', "Second") + app:command("draft_lift") + app:frames(10) + app:type('role:textbox "Message"', "First") + app:command("draft_lay") + app:frames(30) + app:key("Enter") + app:frames(30) + expect(app:state("calls")).to_contain("send:s2:First\n\nSecond") + expect(app:error()).to_be(nil) +end) diff --git a/apps/zgx/ui/tests/chat_find.test.luau b/apps/zgx/ui/tests/chat_find.test.luau index 6793bcf05..a43ec658e 100644 --- a/apps/zgx/ui/tests/chat_find.test.luau +++ b/apps/zgx/ui/tests/chat_find.test.luau @@ -97,29 +97,3 @@ test("the newest match shows where it is, not at its reply's start", function() expect(y_of(app, "Part 1 explains")).to_be(nil) expect(app:error()).to_be(nil) end) - --- One reply of a thousand steps (ademark's A11 thread): its blocks are held --- in runs away from the view, and a match far up it is found all the same, --- its run built and scrolled into view. -test("a match far up a long thread's one reply is found, built and scrolled into view", function() - local app = mount("chat_fixture.luau") - app:resize(960, 700) - app:frames(20) - app:command("long_thread") - app:command("go_s7") - app:frames(40) - settle(app) - expect(y_of(app, "Step 3 of 1000.")).to_be(nil) - app:command("find_open") - settle(app) - app:type('role:textbox "Find"', "step 3 needs") - settle(app) - expect(tally(app)).to_be("1 of 1") - local y = y_of(app, "Step 3 of 1000.") - expect(y ~= nil).to_be(true) - local first = app:tree("#transcript"):match("[^\n]+") or "" - local top, h = first:match(" %-?%d+,(%-?%d+) %d+x(%d+)") - top, h = tonumber(top), tonumber(h) - expect((y :: number) >= (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/chat_in_place.test.luau b/apps/zgx/ui/tests/chat_in_place.test.luau index e9a495c75..241dcefaa 100644 --- a/apps/zgx/ui/tests/chat_in_place.test.luau +++ b/apps/zgx/ui/tests/chat_in_place.test.luau @@ -708,6 +708,21 @@ test("a move to another agent shows its seam over the prompt that moved it", fun expect(app:count('"Codex"')).to_be(1) end) +test("a settled reply with no time still shows Copy under the pointer", function() + -- (Windows gave every message no time, and the strip went with it.) + local app = mount("chat_fixture.luau") + app:resize(900, 700) + open(app, "s1") + app:command("reply_without_time") + settle(app) + app:hover('~"Answered with no clock"') + settle(app) + expect(app:count('role:button "Copy message"')).to_be(1) + app:click('role:button "Copy message"') + settle(app) + expect(app:error()).to_be(nil) +end) + test("a reply being written shows no time or Copy, even under the pointer", function() local app = mount("chat_fixture.luau") app:resize(900, 700) diff --git a/apps/zgx/ui/tests/chimes.test.luau b/apps/zgx/ui/tests/chimes.test.luau new file mode 100644 index 000000000..2b4716acb --- /dev/null +++ b/apps/zgx/ui/tests/chimes.test.luau @@ -0,0 +1,114 @@ +-- The session chimes (screens/settings/chimes, issue #1437): "done" rings +-- when a turn finishes (its `turn` mark moving and standing a moment), never +-- for a chat turning "done" alone, which is only activity you haven't seen +-- and your own send makes too. Questions and failures ring at once. + +local function chat(id: string, status: string?, turn: string?, fresh: boolean?): any + return { + id = id, + title = id, + device = "here", + status = status, + updated = 0, + created = 0, + turn = turn, + turn_fresh = if fresh == nil then true else fresh, + } +end + +local function publish(app: any, chats: { any }) + app:set_state("sessions", { + state = "ready", + devices = {}, + projects = {}, + chats = chats, + archived = {}, + places = { pinned = {}, sections = {} }, + places_ready = true, + }) + app:frames(2) +end + +local function played(app: any): { string } + local out = {} + for _, c in (app:state("settings_stub_calls") :: any) or {} do + local sound = string.match(c, "^sound%.play:(.+)$") + if sound then + table.insert(out, sound) + end + end + return out +end + +-- Long enough for a finish to stand (chimes' SETTLE, 2 s). +local function settle(app: any) + app:wait("#never-shown", { timeout_ms = 2400 }) + app:frames(3) +end + +local function open(chats: { any }): any + local app = mount("chimes_fixture.luau") + publish(app, chats) + return app +end + +test("chimes: your own send never rings done", function() + local app = open({ chat("a", nil, "t1") }) + -- The send lands: your message makes the chat unseen ("done") before the + -- live row says it's working; the turn mark hasn't moved. + publish(app, { chat("a", "done", "t1") }) + publish(app, { chat("a", "working", "t1") }) + -- A steer, or a queued message taken, as a turn ends: the end is + -- published with the next turn already running. + publish(app, { chat("a", "working", "t2") }) + -- A send that replaces the runtime: the old turn's end lands idle, then + -- your turn starts a moment later. + publish(app, { chat("a", "done", "t3") }) + publish(app, { chat("a", "working", "t3") }) + settle(app) + expect(#played(app)).to_be(0) + + -- The turn your message started finishing: that rings, once. + publish(app, { chat("a", "done", "t4") }) + expect(#played(app)).to_be(0) + settle(app) + expect(table.concat(played(app), ",")).to_be("done") + settle(app) + expect(#played(app)).to_be(1) +end) + +test("chimes: a chat's first turn finishing rings done", function() + local app = open({ chat("new", "working", nil) }) + publish(app, { chat("new", "done", "t1") }) + settle(app) + expect(table.concat(played(app), ",")).to_be("done") +end) + +test("chimes: an old row coming back, or a session row returning, never rings", function() + local app = open({ chat("a", nil, "t1") }) + -- A sync brings back an end published long ago. + publish(app, { chat("a", "done", "t2", false) }) + -- The row goes away (a host restarting) and comes back as it was. + publish(app, { chat("a", nil, nil) }) + publish(app, { chat("a", nil, "t2") }) + settle(app) + expect(#played(app)).to_be(0) +end) + +test("chimes: questions and failures ring at once", function() + local app = open({ chat("q", "working", "t1"), chat("f", "working", "t1") }) + publish(app, { chat("q", "input", "t1"), chat("f", "working", "t1") }) + expect(table.concat(played(app), ",")).to_be("request") + publish(app, { chat("q", "input", "t1"), chat("f", "failed", "t1") }) + expect(table.concat(played(app), ",")).to_be("request,attention") +end) + +test("chimes: Task completed off keeps done quiet, the rest still ring", function() + local app = open({ chat("a", "working", "t1") }) + app:set_state("settings_sound_done", false) + publish(app, { chat("a", "done", "t2") }) + settle(app) + expect(#played(app)).to_be(0) + publish(app, { chat("a", "input", "t2") }) + expect(table.concat(played(app), ",")).to_be("request") +end) diff --git a/apps/zgx/ui/tests/keep_running.test.luau b/apps/zgx/ui/tests/keep_running.test.luau deleted file mode 100644 index ff703efed..000000000 --- a/apps/zgx/ui/tests/keep_running.test.luau +++ /dev/null @@ -1,50 +0,0 @@ --- 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 deleted file mode 100644 index 629c1224e..000000000 --- a/apps/zgx/ui/tests/launch_keys.test.luau +++ /dev/null @@ -1,48 +0,0 @@ --- 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 deleted file mode 100644 index f6a8aee05..000000000 --- a/apps/zgx/ui/tests/markdown_edit.test.luau +++ /dev/null @@ -1,33 +0,0 @@ --- 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 bfda72342..0a3f464fd 100644 --- a/apps/zgx/ui/tests/mention_sessions.test.luau +++ b/apps/zgx/ui/tests/mention_sessions.test.luau @@ -255,25 +255,3 @@ 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 885592a0d..4a044737c 100644 --- a/apps/zgx/ui/tests/mentions.test.luau +++ b/apps/zgx/ui/tests/mentions.test.luau @@ -68,26 +68,6 @@ 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/model_accounts.test.luau b/apps/zgx/ui/tests/model_accounts.test.luau index 3ea1abea1..a79be2447 100644 --- a/apps/zgx/ui/tests/model_accounts.test.luau +++ b/apps/zgx/ui/tests/model_accounts.test.luau @@ -91,6 +91,33 @@ test("a chat on another computer reads that computer's accounts", function() expect(app:state("calls")).to_contain("switch_on:box:claude-code:b2") end) +-- A chat on a Cloud machine: its accounts are Cloud's, the vault's (as +-- Settings → Providers shows them under Cloud), never the machine's own, +-- which isn't asked (it would be woken). +test("a chat on a Cloud machine reads and switches Cloud's accounts", function() + local app = with_accounts({ + [""] = { ["claude-code"] = accounts() }, + cloud = { + ["claude-code"] = { + { id = "v1", email = "team@example.test", plan = "Max", active = true, can_switch = true, windows = { window("Week", 0.4) } }, + { id = "v2", email = "spare@example.test", plan = "Pro", active = false, can_switch = true, windows = {} }, + }, + }, + }) + app:set_state("chat_device", "cloud-f60cde46") + app:frames(10) + expect(app:state("calls")).to_contain("watch_device:cloud") + expect(string.find(app:state("calls"), "watch_device:cloud-f60cde46", 1, true)).to_be(nil) + app:click('role:button "Model"') + app:frames(30) + expect(app:tree("#model-picker-account")).to_contain("team@example.test") + app:click("#model-picker-account") + app:frames(30) + app:click('~"spare@example.test"') + app:frames(30) + expect(app:state("calls")).to_contain("switch_on:cloud:claude-code:v2") +end) + test("a computer whose accounts haven't come has neither, and is asked", function() local app = with_accounts(here(accounts())) app:set_state("chat_device", "somewhere-else") diff --git a/apps/zgx/ui/tests/new_chat_remembers.test.luau b/apps/zgx/ui/tests/new_chat_remembers.test.luau index 9485859cf..634623179 100644 --- a/apps/zgx/ui/tests/new_chat_remembers.test.luau +++ b/apps/zgx/ui/tests/new_chat_remembers.test.luau @@ -153,38 +153,3 @@ 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/apps/zgx/ui/tests/plugin_compose_replace.test.luau b/apps/zgx/ui/tests/plugin_compose_replace.test.luau new file mode 100644 index 000000000..e91e12e8a --- /dev/null +++ b/apps/zgx/ui/tests/plugin_compose_replace.test.luau @@ -0,0 +1,54 @@ +-- What a plugin allowed "compose" puts in a chat's draft +-- (`zeron.compose(text, opts)`, handed on by screens/plugins as the host +-- asks it): after what's there, or in place of its words with its chips +-- kept (`replace`), in the chat shown or another (`session`), never sent. +-- On the shell in ui/shell_fixture.luau, the host's asks made here. + +local seq = 0 +local function ask(app: any, value: any) + seq += 1 + app:set_state("plugins.request", { { seq = 1000 + seq, kind = "compose", title = "", value = value } }) + app:frames(10) +end + +local function open(): any + seq = 0 + local app = mount("shell_fixture.luau") + app:resize(1400, 800) + app:set_state("shell_fixture.plugins", true) + app:frames(30) + app:click('#shell-sidebar >> role:button "Fix the build"') + app:frames(30) + return app +end + +local BOX = "#chat-composer >> role:textbox" + +test("a plugin's words replace the draft's, or follow them on a line of their own", function() + local app = open() + app:type(BOX, "fix teh bulid pls") + app:frames(5) + ask(app, { text = "Please fix the build.", replace = true }) + expect(app:text(BOX)).to_be("Please fix the build.") + ask(app, { text = "It fails on main." }) + expect(app:text(BOX)).to_be("Please fix the build.\nIt fails on main.") + -- Words alone, as ever: after what's there. + ask(app, "Thanks.") + expect(app:text(BOX)).to_be("Please fix the build.\nIt fails on main.\nThanks.") + -- Nothing sent. + expect(app:state("calls")).never.to_contain("send:a1") +end) + +test("a plugin's words go in another chat's draft, which shows them when it's opened", function() + local app = open() + app:type(BOX, "here, untouched") + app:frames(5) + ask(app, { text = "for the docs", replace = true, session = "a2" }) + expect(app:text(BOX)).to_be("here, untouched") + app:click('#shell-sidebar >> role:button "Write the docs"') + app:frames(30) + expect(app:text(BOX)).to_be("for the docs") + app:click('#shell-sidebar >> role:button "Fix the build"') + app:frames(30) + expect(app:text(BOX)).to_be("here, untouched") +end) diff --git a/apps/zgx/ui/tests/plugin_draft_read.test.luau b/apps/zgx/ui/tests/plugin_draft_read.test.luau new file mode 100644 index 000000000..76e9bc044 --- /dev/null +++ b/apps/zgx/ui/tests/plugin_draft_read.test.luau @@ -0,0 +1,49 @@ +-- The composer's draft as plugins read it (`zeron.draft`, "draft"): written +-- only while a plugin that's on may read it, so with none no key typed +-- copies the draft for no one; with one, it follows what's typed, its chat +-- and its chips by kind and name. On the shell in ui/shell_fixture.luau. + +local function open(): any + local app = mount("shell_fixture.luau") + app:resize(1400, 800) + app:set_state("shell_fixture.plugins", true) + app:frames(30) + app:click('#shell-sidebar >> role:button "Fix the build"') + app:frames(30) + return app +end + +local function with_plugin(app: any, status: string) + app:set_state("plugins", { + state = "ready", + list = { { id = "stash", name = "Stash", status = status, permissions = { "ui.add", "draft" }, allowed = { "ui.add", "draft" } } }, + }) + app:frames(5) +end + +test("with no plugin that reads the draft, typing publishes nothing", function() + local app = open() + app:type("#chat-composer >> role:textbox", "a private thought") + app:frames(5) + expect(app:state("zeron.draft")).to_be_nil() + -- One that's off reads nothing either. + with_plugin(app, "off") + app:type("#chat-composer >> role:textbox", "still private") + app:frames(5) + expect(app:state("zeron.draft")).to_be_nil() +end) + +test("with a plugin on that reads the draft, it follows what's typed, and goes with the plugin", function() + local app = open() + with_plugin(app, "on") + app:type("#chat-composer >> role:textbox", "first words") + app:frames(5) + local d = app:state("zeron.draft") + expect(d).never.to_be_nil() + expect(d.text).to_be("first words") + expect(type(d.session)).to_be("string") + expect(#d.attachments).to_be(0) + -- Turned off: nothing kept out for it. + with_plugin(app, "off") + expect(app:state("zeron.draft")).to_be_nil() +end) diff --git a/apps/zgx/ui/tests/providers_cloud.test.luau b/apps/zgx/ui/tests/providers_cloud.test.luau new file mode 100644 index 000000000..b6b696335 --- /dev/null +++ b/apps/zgx/ui/tests/providers_cloud.test.luau @@ -0,0 +1,77 @@ +-- Settings → Providers offers Cloud as one more choice after the computers, +-- while Cloud is on: its agents' accounts (the vault's, every Cloud machine +-- uses them), with no switch, no settings and nothing to install, and API +-- keys to save for it. Without Cloud, no such choice. On the shell in +-- ui/shell_fixture.luau, Cloud from ui/cloud_stub.luau. + +local PAGE = "#settings-page" +local NAV = "#settings-nav" + +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(with_cloud: boolean): any + local app = mount("shell_fixture.luau") + app:resize(1200, 900) + app:frames(30) + app:command("settings_stub.sign_in") + if with_cloud then + app:command("cloud_stub.offer") + end + app:command("shell.settings") + app:frames(20) + app:click(`{NAV} >> role:tab "Providers"`) + app:frames(30) + return app +end + +test("with Cloud on, Providers offers Cloud: its agents' accounts, no switch or settings, and its API keys", function() + local app = open(true) + app:click(`{PAGE} >> role:button "Device"`) + app:frames(10) + expect(app:has('role:menuitem "Cloud"')).to_be(true) + app:click('role:menuitem "Cloud"') + app:frames(30) + expect(asked(app, "agents.target:cloud")).to_be(true) + expect(shows(app, PAGE, "Claude Code")).to_be(true) + expect(shows(app, PAGE, "Codex")).to_be(true) + expect(shows(app, PAGE, "ada@example.com")).to_be(true) + -- Nothing to switch, set or install: its accounts alone. + expect(app:has(`{PAGE} >> role:switch "Codex on"`)).to_be(false) + expect(app:has(`{PAGE} >> role:button "Codex settings"`)).to_be(false) + expect(shows(app, PAGE, "Add another agent")).to_be(false) + -- Its API keys, saved for Cloud. + expect(shows(app, PAGE, "Anthropic API key")).to_be(true) + expect(shows(app, PAGE, "OpenAI API key")).to_be(true) + app:type(`{PAGE} >> role:textbox "OpenAI API key"`, "sk-test-123") + app:frames(5) + app:click(`{PAGE} >> role:button "Save" [2]`) + app:frames(10) + expect(asked(app, "agents.cloud_key")).to_be(true) + expect(app:error()).to_be_nil() + -- Back to this computer: its agents as they were, with their switches. + app:click(`{PAGE} >> role:button "Device"`) + app:frames(10) + app:click('role:menuitem "MacBook Pro"') + app:frames(30) + expect(asked(app, "agents.target:")).to_be(true) + expect(app:has(`{PAGE} >> role:switch "Claude Code on"`)).to_be(true) + expect(shows(app, PAGE, "Anthropic API key")).to_be(false) +end) + +test("without Cloud, Providers offers no Cloud", function() + local app = open(false) + app:click(`{PAGE} >> role:button "Device"`) + app:frames(10) + expect(app:has('role:menuitem "Cloud"')).to_be(false) +end) diff --git a/apps/zgx/ui/tests/transcript_scroll_away.test.luau b/apps/zgx/ui/tests/transcript_scroll_away.test.luau new file mode 100644 index 000000000..a4dddd99a --- /dev/null +++ b/apps/zgx/ui/tests/transcript_scroll_away.test.luau @@ -0,0 +1,84 @@ +-- Scrolled away from the end, the view stays where the person took it: it +-- isn't pulled back to the end. (Nov 14: "scrolling gets stuck to the +-- bottom; scroll away and it scrolls you back down".) Two ways it was: +-- - a scroll during a send's glide (the prompt rising to the view's top, or +-- a steer's waiting to land): the glide went on to the end over it; +-- - a scroll begun within moments of the view's size changing: the end +-- stayed held, and the next resize, however much later (the sidebar, a +-- panel, the window), went back to it. +-- On chat_fixture's `long_chat` (ui/long_chat_fixture.luau). + +local function settle(app: any) + app:wait("#never-shown", { timeout_ms = 250 }) + app:frames(10) +end + +local function offset(app: any): number + return app:state("chat.transcript.offset") +end + +-- How far the view is from its end, measured by going there (which holds +-- the end again: only at the close of a test). +local function from_end(app: any): number + local at = offset(app) + app:scroll("#transcript", 1e6) + app:frames(4) + return offset(app) - at +end + +local function open(): any + local app = mount("chat_fixture.luau") + app:resize(960, 700) + app:frames(20) + app:command("long_chat_sent") + app:command("go_s7") + app:frames(30) + settle(app) + return app +end + +local function send(app: any) + app:type("role:textbox", "three and one sounds like the most optional option") + app:key("Enter") +end + +local function scroll_up(app: any) + for _ = 1, 10 do + app:wheel("#transcript", { dy = -40 }) + app:frames(1) + end +end + +test("scrolled up while a send glides, the view stays where it was taken", function() + local app = open() + send(app) + app:frames(3) + scroll_up(app) + settle(app) + settle(app) + expect(from_end(app) > 100).to_be(true) +end) + +test("scrolled up just after a resize, a later resize leaves the view where it was", function() + local app = open() + send(app) + app:frames(40) + settle(app) + for _ = 1, 60 do + app:command("long_chat_streams") + app:frames(2) + end + app:frames(10) + app:resize(940, 700) + app:frames(2) + scroll_up(app) + -- (It rests inside the resize's moment, then a while.) + settle(app) + app:wait("#never-shown", { timeout_ms = 800 }) + local at = offset(app) + app:resize(900, 700) + app:frames(10) + settle(app) + expect(math.abs(offset(app) - at) < 1).to_be(true) + expect(from_end(app) > 100).to_be(true) +end) diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index b7469e281..8dd3b8719 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -28,8 +28,6 @@ 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 @@ -76,6 +74,7 @@ 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 c5f919c94..623d6bc83 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; checked on the open file, so one - // swapped in after a check of the path is refused too.) + // while the accounts are listed.) let env_file = hermes_home.join(".env"); - if let Ok(text) = zeron_harness::bounded_file::read_to_string(&env_file, 1 << 20) { + 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) { 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 549700574..8357b4dbe 100644 --- a/crates/engine/src/deferred.rs +++ b/crates/engine/src/deferred.rs @@ -232,10 +232,6 @@ 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/desktop_notify.rs b/crates/engine/src/desktop_notify.rs index c395531a0..e27315b91 100644 --- a/crates/engine/src/desktop_notify.rs +++ b/crates/engine/src/desktop_notify.rs @@ -31,6 +31,14 @@ use zeron_proto::{Chat, ConnectivityState, HarnessUpdatePhase, HarnessUpdateStat static ENABLED: AtomicBool = AtomicBool::new(false); +/// How long a finish waits to stand before it rings. The person's own +/// message often comes with one: a steer or a queued row taken as a turn +/// ends publishes that end with Working (their message carries the turn on), +/// and a send that replaces the runtime marks the old turn's end a moment +/// before the new one starts. Neither is "finished"; a turn that follows +/// within this window takes the finish with it. +const DONE_SETTLE: chrono::Duration = chrono::Duration::seconds(2); + /// This process shows desktop banners: engines assembled from now on post /// them (and advertise [`zeron_proto::capabilities::DESKTOP_NOTIFICATIONS_V1`]). pub fn enable() { @@ -222,7 +230,12 @@ impl Prefs { /// What a banner asked for by something other than a session (a plugin's, /// through `PostNotification`) came to: shown, or why not. -pub(crate) fn asked_outcome(prefs_enabled: bool, background_only: bool, focused: bool, desktop: bool) -> &'static str { +pub(crate) fn asked_outcome( + prefs_enabled: bool, + background_only: bool, + focused: bool, + desktop: bool, +) -> &'static str { if !desktop { "unavailable" } else if !prefs_enabled { @@ -237,7 +250,13 @@ pub(crate) fn asked_outcome(prefs_enabled: bool, background_only: bool, focused: /// A banner asked for (a plugin's): shown as the engine's own are, under /// the same Settings (`notificationsEnabled`, "only when in the /// background"), clicking it opening `target` (a session). What it came to. -pub(crate) fn post_asked(data_dir: &Path, focus: &ViewportFocus, title: &str, body: &str, target: Option<&str>) -> &'static str { +pub(crate) fn post_asked( + data_dir: &Path, + focus: &ViewportFocus, + title: &str, + body: &str, + target: Option<&str>, +) -> &'static str { let prefs = Prefs::read(data_dir); let outcome = asked_outcome(prefs.enabled, prefs.background_only, focus.any(), enabled()); if outcome == "posted" { @@ -261,7 +280,9 @@ pub(crate) fn post_for( std::sync::LazyLock::new(Default::default); let body: String = body.chars().take(500).collect(); let paced = { - let mut pace = PACE.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let mut pace = PACE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); !asked_in_time(pace.entry(source.to_owned()).or_default(), Instant::now()) }; if paced { @@ -273,11 +294,19 @@ pub(crate) fn post_for( /// Whether one more banner from a source may show, given when its last /// ones did: at most [`ASKED_PER_MINUTE`] a minute, [`ASKED_APART`] apart. -pub(crate) fn asked_in_time(history: &mut std::collections::VecDeque, now: Instant) -> bool { - while history.front().is_some_and(|at| now.duration_since(*at) >= Duration::from_secs(60)) { +pub(crate) fn asked_in_time( + history: &mut std::collections::VecDeque, + now: Instant, +) -> bool { + while history + .front() + .is_some_and(|at| now.duration_since(*at) >= Duration::from_secs(60)) + { history.pop_front(); } - let apart = history.back().is_none_or(|at| now.duration_since(*at) >= ASKED_APART); + let apart = history + .back() + .is_none_or(|at| now.duration_since(*at) >= ASKED_APART); if apart && history.len() < ASKED_PER_MINUTE { history.push_back(now); return true; @@ -316,6 +345,9 @@ struct Notifier { focus: ViewportFocus, post: Post, baselines: HashMap, + /// Finishes waiting to stand ([`DONE_SETTLE`]): chat → its title and + /// when it rings. + settling: HashMap)>, connectivity: ConnectivityNotificationState, updates_seen: HashSet, } @@ -328,6 +360,7 @@ impl Notifier { focus, post, baselines: HashMap::new(), + settling: HashMap::new(), connectivity: ConnectivityNotificationState::default(), updates_seen: HashSet::new(), } @@ -356,15 +389,56 @@ impl Notifier { if !chat.is_some_and(Chat::is_top_level) { continue; } + // A turn going on (or asking, or failing) takes a waiting + // finish with it. + if status.indicator != Indicator::None { + self.settling.remove(&session.chat_id); + } if let Some(prev) = prev && let Some(ping) = status.ping_since(&prev, false) { let title = chat .and_then(|chat| chat.title.clone()) .unwrap_or_else(|| "New session".into()); - due.push((title, ping, session.chat_id.clone())); + match ping { + // Published with the next turn already running: the + // person's message carried it on, nothing finished. + Ping::Done if status.indicator == Indicator::Working => {} + Ping::Done => { + self.settling + .insert(session.chat_id.clone(), (title, now + DONE_SETTLE)); + } + _ => due.push((title, ping, session.chat_id.clone())), + } } } + self.post_due(due); + } + + /// When the next waiting finish rings. + fn next_settled(&self) -> Option> { + self.settling.values().map(|(_, at)| *at).min() + } + + /// Ring the finishes that have stood for [`DONE_SETTLE`]. + fn flush(&mut self, now: chrono::DateTime) { + let stood: Vec = self + .settling + .iter() + .filter(|(_, (_, at))| *at <= now) + .map(|(chat, _)| chat.clone()) + .collect(); + let due = stood + .into_iter() + .filter_map(|chat| { + let (title, _) = self.settling.remove(&chat)?; + Some((title, Ping::Done, chat)) + }) + .collect(); + self.post_due(due); + } + + fn post_due(&mut self, due: Vec<(String, Ping, String)>) { if due.is_empty() || self.allowed().is_none() { return; } @@ -464,6 +538,7 @@ pub(crate) fn spawn(core: &crate::EngineCore, data_dir: PathBuf) -> tokio::task: let mut updates_due: Option = Some(tokio::time::Instant::now() + UPDATE_DEBOUNCE); loop { + let settles_at = notifier.next_settled(); tokio::select! { changed = sessions.changed() => { if changed.is_err() { break; } @@ -485,6 +560,17 @@ pub(crate) fn spawn(core: &crate::EngineCore, data_dir: PathBuf) -> tokio::task: updates.borrow_and_update(); updates_due.get_or_insert(tokio::time::Instant::now() + UPDATE_DEBOUNCE); } + _ = async { + match settles_at { + Some(at) => { + let wait = (at - chrono::Utc::now()).to_std().unwrap_or_default(); + tokio::time::sleep(wait).await; + } + None => std::future::pending().await, + } + } => { + notifier.flush(chrono::Utc::now()); + } _ = async { match updates_due { Some(at) => tokio::time::sleep_until(at).await, @@ -505,21 +591,38 @@ mod tests { #[test] fn a_plugin_s_banner_keeps_to_the_settings_and_its_pace() { assert_eq!(asked_outcome(true, false, true, true), "posted"); - assert_eq!(asked_outcome(true, true, true, true), "held", "only when in the background, and a window is in front"); + assert_eq!( + asked_outcome(true, true, true, true), + "held", + "only when in the background, and a window is in front" + ); assert_eq!(asked_outcome(true, true, false, true), "posted"); assert_eq!(asked_outcome(false, false, false, true), "off"); - assert_eq!(asked_outcome(true, false, false, false), "unavailable", "a server never shows banners"); + assert_eq!( + asked_outcome(true, false, false, false), + "unavailable", + "a server never shows banners" + ); let mut history = std::collections::VecDeque::new(); let t0 = Instant::now(); assert!(asked_in_time(&mut history, t0)); - assert!(!asked_in_time(&mut history, t0 + Duration::from_secs(1)), "too soon after the last"); + assert!( + !asked_in_time(&mut history, t0 + Duration::from_secs(1)), + "too soon after the last" + ); let mut at = t0; for _ in 1..ASKED_PER_MINUTE { at += ASKED_APART; assert!(asked_in_time(&mut history, at)); } - assert!(!asked_in_time(&mut history, at + ASKED_APART), "a minute's worth"); - assert!(asked_in_time(&mut history, t0 + Duration::from_secs(61)), "a minute on, one more"); + assert!( + !asked_in_time(&mut history, at + ASKED_APART), + "a minute's worth" + ); + assert!( + asked_in_time(&mut history, t0 + Duration::from_secs(61)), + "a minute on, one more" + ); } use super::*; @@ -782,6 +885,8 @@ mod tests { &chats, now, ); + // (Once it has settled: a finish waits a moment for a turn to follow.) + notifier.flush(now + DONE_SETTLE); assert_eq!( *posted.lock().unwrap(), vec![( @@ -793,6 +898,7 @@ mod tests { // A focused window holds it back (background-only is the default). focus.set("window", true); notifier.sessions(&[session("a", Some("t3"), now)], &chats, now); + notifier.flush(now + DONE_SETTLE); assert_eq!(posted.lock().unwrap().len(), 1); // Banners off: nothing, focused or not. focus.set("window", false); @@ -802,9 +908,68 @@ mod tests { ) .unwrap(); notifier.sessions(&[session("a", Some("t4"), now)], &chats, now); + notifier.flush(now + DONE_SETTLE); assert_eq!(posted.lock().unwrap().len(), 1); } + fn working(chat_id: &str, turn: Option<&str>, now: chrono::DateTime) -> Session { + let mut row = session(chat_id, turn, now); + row.status = zeron_proto::SessionStatus::Working; + row.started_at = Some(now); + row + } + + /// Your own message never rings "finished". These are the rows + /// crates/engine/src/sessions.rs publishes at such a send. + #[test] + fn a_send_steer_or_queued_send_never_rings_finished() { + let dir = tempfile::tempdir().unwrap(); + let (mut notifier, posted, _) = notifier(dir.path()); + let chats = [chat("a", "Fix the build", None)]; + let t0 = chrono::Utc::now(); + let at = |ms: i64| t0 + chrono::Duration::milliseconds(ms); + notifier.sessions(&[session("a", Some("t1"), at(0))], &chats, at(0)); + + // A plain send to an idle chat: Working, the marker as it was. + notifier.sessions(&[working("a", Some("t1"), at(10))], &chats, at(10)); + notifier.flush(at(10_000)); + // A steer (or a queued row taken) as the turn ends: the end is + // published with Working, the person's message carrying it on. + notifier.sessions(&[working("a", Some("t2"), at(20))], &chats, at(20)); + notifier.flush(at(10_000)); + // A send that replaces the runtime: the held end lands Idle, then + // the new turn starts a moment later. + notifier.sessions(&[session("a", Some("t3"), at(30))], &chats, at(30)); + notifier.sessions(&[working("a", Some("t3"), at(200))], &chats, at(200)); + notifier.flush(at(10_000)); + assert!( + posted.lock().unwrap().is_empty(), + "rang on a send: {:?}", + posted.lock().unwrap() + ); + + // The turn your message started finishing: that rings, once. + notifier.sessions(&[session("a", Some("t4"), at(20_000))], &chats, at(20_000)); + notifier.flush(at(30_000)); + notifier.flush(at(40_000)); + assert_eq!(posted.lock().unwrap().len(), 1); + } + + /// A question or a failure doesn't wait: it rings at once. + #[test] + fn a_question_or_failure_rings_at_once() { + let dir = tempfile::tempdir().unwrap(); + let (mut notifier, posted, _) = notifier(dir.path()); + let chats = [chat("a", "Fix the build", None)]; + let now = chrono::Utc::now(); + notifier.sessions(&[working("a", Some("t1"), now)], &chats, now); + let mut asking = working("a", Some("t1"), now); + asking.status = zeron_proto::SessionStatus::AwaitingInput; + notifier.sessions(&[asking], &chats, now); + assert_eq!(posted.lock().unwrap().len(), 1); + assert_eq!(posted.lock().unwrap()[0].1, "Waiting on your input"); + } + #[test] fn agent_updates_banner_once_per_release() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/engine/src/doc_host.rs b/crates/engine/src/doc_host.rs index 2b0e4db7a..5df6f956c 100644 --- a/crates/engine/src/doc_host.rs +++ b/crates/engine/src/doc_host.rs @@ -417,20 +417,6 @@ 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, @@ -477,11 +463,6 @@ 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)); @@ -2461,25 +2442,7 @@ impl DocHost { _ = tokio::time::sleep_until(last + SYNC_PASS_SPACING), if busy => {} _ = kick.notified() => {} Ok(()) = jobs.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(()) = outbox.changed() => {} Ok(()) = socket_contention.changed() => {} Some(Ok(())) = changed(&mut registry_chats) => {} Some(Ok(())) = changed(&mut registry_synced) => {} @@ -2500,9 +2463,6 @@ 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 }; @@ -4038,13 +3998,10 @@ impl DocHost { } /// The connectivity stream: current posture first, then every change. - /// 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. + /// 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. pub fn watch_connectivity(&self) -> watch::Receiver { let tx = self .inner @@ -4056,17 +4013,12 @@ impl DocHost { { let host = self.clone(); self.spawn_worker(async move { - let mut held = 0u32; + let mut tick = tokio::time::interval(std::time::Duration::from_secs(1)); + tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); loop { - let wait = if held >= CONNECTIVITY_SETTLE_TICKS { - CONNECTIVITY_STEADY - } else { - std::time::Duration::from_secs(1) - }; - tokio::time::sleep(wait).await; + tick.tick().await; let next = host.compute_connectivity(); - let healthy = connectivity_healthy(&next); - let changed = host.inner.connectivity.get().is_some_and(|tx| { + if let Some(tx) = host.inner.connectivity.get() { tx.send_if_modified(|cur| { if *cur == next { false @@ -4074,10 +4026,8 @@ impl DocHost { *cur = next; true } - }) - }); - let timing = lock(&host.inner.connectivity_grace).timing(); - held = if changed || timing || !healthy { 0 } else { held.saturating_add(1) }; + }); + } } }); } @@ -4315,7 +4265,6 @@ 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. @@ -6192,7 +6141,6 @@ 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 { @@ -6849,15 +6797,9 @@ impl DocHost { async fn capture_source_context(&self, cwd: &str) -> Option { let repos = self.inner.repos.get()?; let path = Path::new(cwd); - // 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(); + 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(); Some(ConversationSourceContext { checkout_id: identity.id, repo_root: identity.root.to_string_lossy().into_owned(), @@ -6869,13 +6811,10 @@ impl DocHost { } /// Every fresh harness dispatch crosses this boundary, including - /// 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). + /// 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. pub(crate) async fn dispatch_with_source_context( &self, sessions: &SessionsEngine, @@ -6884,21 +6823,11 @@ impl DocHost { request: zeron_proto::RunRequest, message_id: Option, ) -> Result { - // 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"); - }); + 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"); } 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 a9e401743..14b5645ae 100644 --- a/crates/engine/src/doc_host/relay.rs +++ b/crates/engine/src/doc_host/relay.rs @@ -120,8 +120,6 @@ 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 { @@ -145,7 +143,6 @@ impl ChatRelayState { journal: Mutex::new(Journal::default()), heal_version: Mutex::new(None), heal_claims: AtomicBool::new(true), - stirred: tokio::sync::Notify::new(), }) } @@ -255,19 +252,9 @@ 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. @@ -353,7 +340,6 @@ 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(); } } } @@ -394,22 +380,14 @@ impl DocHost { let weak = Arc::downgrade(handle); self.spawn_sync_worker(cancel, async move { let mut events = state.relay.events(); - // 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 tick = tokio::time::interval(MAINTENANCE_TICK); + tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); 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)) { @@ -418,28 +396,16 @@ impl DocHost { last_refresh = Some(Instant::now()); } } - _ = tokio::time::sleep_until(next_tick) => { + _ = tick.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() { @@ -449,9 +415,10 @@ impl DocHost { } } let saved = handle.persistence.as_ref().is_some_and(|p| p.is_clean()); - 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"); + 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"); + } } if let Some(through) = state.retirable_journal(saved) && let Err(err) = host.inner.store.retire_chat_inbox(&handle.chat_id, through) @@ -478,21 +445,6 @@ 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 98e75c746..5e89a97da 100644 --- a/crates/engine/src/home_index.rs +++ b/crates/engine/src/home_index.rs @@ -8,270 +8,16 @@ //! folders, hidden folders and dependency folders altogether. It holds no //! file contents, so home has no content search. -//! -//! 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::path::Path; +use std::sync::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError, Weak}; -use std::time::{Duration, Instant}; +use std::time::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] = &[ @@ -308,80 +54,57 @@ struct HomePath { } impl HomeIndex { - /// 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 { + /// Walk `home` (canonical). `cancelled` stops the walk early. + pub fn build(home: &Path, cancelled: &AtomicBool) -> Self { let threads = std::thread::available_parallelism() .map(|n| n.get().min(8)) .unwrap_or(4); - 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)); + 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); entries.truncate(MAX_ENTRIES); Self { entries, @@ -449,19 +172,47 @@ 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 (`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(); +/// 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(); if name == ".git" || SKIPPED_DIRS.contains(&name.as_str()) { return true; } - if depth == 1 + if entry.depth() == 1 && (MACHINE_ROOTS.contains(&name.as_str()) || CLOUD_ROOT_PREFIXES.iter().any(|prefix| { name == *prefix @@ -472,7 +223,9 @@ fn skipped(name: &str, depth: usize, metadata: impl FnOnce() -> Option> = - 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 deleted file mode 100644 index 316ce6829..000000000 --- a/crates/engine/src/keep_running.rs +++ /dev/null @@ -1,187 +0,0 @@ -//! "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 1ebc1423f..cf4ce525e 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -58,7 +58,6 @@ 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; @@ -74,7 +73,6 @@ 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; @@ -1407,16 +1405,6 @@ 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 b5843b7fa..967a85bac 100644 --- a/crates/engine/src/managed.rs +++ b/crates/engine/src/managed.rs @@ -55,127 +55,28 @@ 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), and -/// whether it outlives its app ([`crate::keep_running`]). +/// app's sign-out and workspace switches restart it on the new profile). 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 { - 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, + if method != methods::STOP_ENGINE { + return 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 { @@ -334,13 +235,9 @@ 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. @@ -402,10 +299,6 @@ 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/manifest.rs b/crates/engine/src/plugins/manifest.rs index 3a7f68195..5bfd31f57 100644 --- a/crates/engine/src/plugins/manifest.rs +++ b/crates/engine/src/plugins/manifest.rs @@ -28,6 +28,9 @@ pub const KNOWN_PERMISSIONS: &[&str] = &[ "notifications", "navigate", "compose", + // Reading the composer's draft, and taking it and putting it back + // (`zeron.draft`, `take_draft`, `put_draft`). + "draft", "network", "clipboard", "files", diff --git a/crates/engine/src/plugins/mod.rs b/crates/engine/src/plugins/mod.rs index fb8f130f0..72057f05f 100644 --- a/crates/engine/src/plugins/mod.rs +++ b/crates/engine/src/plugins/mod.rs @@ -1576,7 +1576,6 @@ 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 1ff3b21c2..4914e63ba 100644 --- a/crates/engine/src/rpc.rs +++ b/crates/engine/src/rpc.rs @@ -2004,7 +2004,6 @@ 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 @@ -2152,7 +2151,6 @@ 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. @@ -3715,25 +3713,10 @@ 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(|stream| { - RpcReply::Stream( - stream - .map(move |frame| { - let _held = &viewer; - frame - }) - .boxed(), - ) - }); + .map(RpcReply::Stream); } // An open transcript keeps its chat's checkout diff live (and // discardable) even when the chat is archived; the guard lives @@ -3753,7 +3736,7 @@ impl RpcService for EngineRpc { Ok(RpcReply::Stream( stream .map(move |frame| { - let _held = (&interest, &viewer); + let _held = &interest; frame }) .boxed(), diff --git a/crates/engine/src/run_journal.rs b/crates/engine/src/run_journal.rs index 9fe50725f..636da386f 100644 --- a/crates/engine/src/run_journal.rs +++ b/crates/engine/src/run_journal.rs @@ -11,24 +11,12 @@ //! //! 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::{Arc, Condvar, Mutex, MutexGuard, PoisonError}; +use std::sync::{Mutex, MutexGuard, PoisonError}; use serde::{Deserialize, Serialize}; @@ -48,186 +36,34 @@ 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, - shared, - flusher: Some(flusher), + open_files: Mutex::new(HashMap::new()), }) } fn lock(&self) -> MutexGuard<'_, HashMap> { - self.shared.lock() + self.open_files + .lock() + .unwrap_or_else(PoisonError::into_inner) } fn path_for(&self, chat_id: &str) -> PathBuf { @@ -343,14 +179,6 @@ 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); @@ -362,9 +190,6 @@ impl RunJournal { file, next_seq, needs_newline, - pending: Vec::new(), - pending_since: None, - failing: false, }, ); } @@ -375,45 +200,23 @@ impl RunJournal { ))); }; let seq = journal.next_seq; - 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(); + 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'); } - journal.pending.extend_from_slice(&line); - journal.pending.push(b'\n'); + buf.extend_from_slice(line.as_bytes()); + buf.push(b'\n'); + journal.file.write_all(&buf)?; + journal.file.flush()?; + journal.needs_newline = false; 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( @@ -421,7 +224,6 @@ 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()); @@ -434,7 +236,6 @@ 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)) } @@ -445,7 +246,6 @@ impl RunJournal { &self, chat_id: &str, ) -> Result>, JournalError> { - self.flush(chat_id); RevEvents::open(&self.path_for(chat_id)) } @@ -462,7 +262,6 @@ 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); @@ -509,7 +308,6 @@ 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?; @@ -798,93 +596,6 @@ 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 deleted file mode 100644 index 749b72ce1..000000000 --- a/crates/engine/src/self_schedule.rs +++ /dev/null @@ -1,379 +0,0 @@ -//! 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 9ac3ba7ef..6f5f098d1 100644 --- a/crates/engine/src/sessions.rs +++ b/crates/engine/src/sessions.rs @@ -289,11 +289,6 @@ 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 @@ -340,112 +335,6 @@ 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; @@ -479,8 +368,6 @@ 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), @@ -564,43 +451,6 @@ 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); @@ -621,8 +471,6 @@ 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. @@ -867,23 +715,6 @@ 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 { @@ -1313,7 +1144,6 @@ 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); @@ -1526,24 +1356,14 @@ impl SessionsEngine { self.inner.note_message(chat_id, &request.prompt); } - // 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, - ) - }); + // 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); + } tokio::spawn(drive_run( self.inner.clone(), @@ -1569,7 +1389,6 @@ impl SessionsEngine { }, turn, side_chats_only, - title, )); Ok(run_id) } @@ -2471,8 +2290,6 @@ 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 @@ -2752,19 +2569,6 @@ 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 @@ -3021,38 +2825,38 @@ impl Inner { }) } - /// Zeron's own MCP server for a run of `chat_id`, stamped with the + /// 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 /// originating chat + device so the agent's side chats link back here. - /// None when the engine serves neither its MCP endpoint nor an IPC port. + /// None when the engine serves no port or its executable is unknown. /// - /// 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 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`]). /// - /// 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`). + /// 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. /// /// `side_chats_only`: the run's provider launches without its own - /// 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`. + /// 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`. fn zeron_mcp( &self, chat_id: &str, side_chats_only: bool, waits: bool, - ) -> Option<(zeron_proto::McpServer, Option)> { + ) -> Option { + let port = self.ipc_port.load(std::sync::atomic::Ordering::Relaxed); + if port == 0 { + return None; + } 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()), ] @@ -3070,49 +2874,6 @@ 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()) @@ -3122,16 +2883,12 @@ impl Inner { path.to_owned(), ); } - Some(( - zeron_proto::McpServer { - name: "zeron".into(), - command, - args: vec!["mcp".into()], - env, - http: None, - }, - None, - )) + Some(zeron_proto::McpServer { + name: "zeron".into(), + command, + args: vec!["mcp".into()], + env, + }) } fn workspace(&self) -> Option { @@ -4437,7 +4194,6 @@ 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). @@ -4458,14 +4214,8 @@ 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. - // 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; + if request.mcp.is_none() { + request.mcp = inner.zeron_mcp(&chat_id, side_chats_only, harness.wakes_on_background()); } // Plugins' native MCP servers come from this host's own allowed plugins, // run from their verified snapshots — never from what a sender put here. @@ -4474,21 +4224,6 @@ 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). @@ -4676,7 +4411,6 @@ 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(()) => { @@ -4918,43 +4652,22 @@ 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 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") + // 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") .ok() .and_then(|v| v.parse::().ok()) - .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(); + .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 + }, + )); // 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 @@ -4962,11 +4675,7 @@ 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 = 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 subagent_silence = session_idle * 8; let mut idle_since: Option = resume_state.idle.then(tokio::time::Instant::now); // A parked runtime whose agent reports background work (see @@ -5121,9 +4830,6 @@ 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; @@ -5170,31 +4876,11 @@ 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 (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 - }); + 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); match (base, reap_deferred_until) { (Some(base), Some(deferred)) => base.max(deferred), (base, deferred) => base.or(deferred).unwrap_or_else(tokio::time::Instant::now), @@ -5211,58 +4897,15 @@ 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() + window_now(&inner, &chat_id, session_idle, unwatched_idle)); + reap_deferred_until = Some(tokio::time::Instant::now() + session_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() + window_now(&inner, &chat_id, session_idle, unwatched_idle)); + reap_deferred_until = Some(tokio::time::Instant::now() + session_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(), @@ -5285,22 +4928,10 @@ async fn drive_run( } next = stream.next() => match next { Some(Ok(event)) => { - 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(); - } + crate::wake_trace::note_agent_event(matches!( + event, + AgentEvent::SessionStarted { .. } + )); event } // A stream error while PARKED is a post-turn child death — @@ -5341,7 +4972,6 @@ 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, ) { @@ -5352,8 +4982,6 @@ 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 @@ -5436,8 +5064,6 @@ 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 2c4e7d39d..65bea32b4 100644 --- a/crates/engine/src/titles.rs +++ b/crates/engine/src/titles.rs @@ -90,7 +90,6 @@ 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 @@ -251,56 +250,6 @@ 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 8d4a35fd6..25db7bf10 100644 --- a/crates/engine/src/workspace_search.rs +++ b/crates/engine/src/workspace_search.rs @@ -17,6 +17,7 @@ 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}; @@ -152,7 +153,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, } @@ -467,7 +468,7 @@ impl WorkspaceSearch { .as_ref() .is_none_or(|index| index.built.elapsed() >= HOME_REFRESH); if stale && home.building.is_none() { - let cancel = Arc::new(crate::home_index::WalkCancel::default()); + let cancel = Arc::new(AtomicBool::new(false)); home.building = Some(cancel.clone()); let inner = Arc::downgrade(&self.inner); let root = root.to_path_buf(); @@ -499,7 +500,7 @@ impl WorkspaceSearch { return false; } if let Some(cancel) = home.building.take() { - cancel.cancel(); + cancel.store(true, std::sync::atomic::Ordering::Relaxed); } *home = HomeEntry::default(); self.inner.home_built.notify_all(); @@ -556,7 +557,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 deleted file mode 100644 index 5142029c9..000000000 --- a/crates/engine/tests/agent_lifecycle.rs +++ /dev/null @@ -1,862 +0,0 @@ -//! 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 deleted file mode 100644 index fb87271eb..000000000 --- a/crates/engine/tests/launch_agents.rs +++ /dev/null @@ -1,331 +0,0 @@ -//! 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 d55d9100d..db438c698 100644 --- a/crates/engine/tests/steer_tool_live.rs +++ b/crates/engine/tests/steer_tool_live.rs @@ -144,7 +144,6 @@ 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 9ffcc04f9..7317e54ab 100644 --- a/crates/harness/src/acp/hermes_names.rs +++ b/crates/harness/src/acp/hermes_names.rs @@ -115,24 +115,20 @@ 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. - // (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; - } + let Ok(meta) = std::fs::metadata(catalog) else { + return; }; - let mut reader = serde_json::Deserializer::from_reader(std::io::BufReader::with_capacity( - 64 * 1024, - std::io::Read::take(file, MAX_CATALOG_BYTES), - )); + 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 names = match (Catalog { wanted: &wanted }).deserialize(&mut reader) { Ok(names) => names, Err(error) => { @@ -408,13 +404,6 @@ 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 57d6a64dd..a2e0d9a91 100644 --- a/crates/harness/src/acp/mod.rs +++ b/crates/harness/src/acp/mod.rs @@ -3655,47 +3655,28 @@ 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, &[], 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_session_mcp_servers(mcp, &[]) } /// [`acp_mcp_servers`] plus the plugins' native servers -/// ([`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`. +/// ([`RunRequest::extra_mcp`]), Zeron's own first. 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| match mcp.http.as_ref().filter(|_| http) { - Some(endpoint) => json!({ - "type": "http", - "name": mcp.name, - "url": endpoint.url, - "headers": pairs(&endpoint.headers), - }), - None => json!({ + .map(|mcp| { + json!({ "name": mcp.name, "command": mcp.command, "args": mcp.args, - "env": pairs(&mcp.env), - }), + "env": mcp + .env + .iter() + .map(|(name, value)| json!({ "name": name, "value": value })) + .collect::>(), + }) }) .collect() } @@ -4591,11 +4572,7 @@ 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, - takes_http_mcp(init), - ), + "mcpServers": acp_session_mcp_servers(request.mcp.as_ref(), &request.extra_mcp), }); if let Some(resume) = &request.resume { let method = restore_method(init, harness); @@ -8297,7 +8274,6 @@ 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!( @@ -8317,16 +8293,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![], 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), false); + let servers = acp_session_mcp_servers(Some(&zeron), std::slice::from_ref(&extra)); assert_eq!(servers.len(), 2); assert_eq!(servers[0]["name"], "zeron"); assert_eq!( @@ -8338,35 +8312,6 @@ mod mcp_injection_tests { "env": [{ "name": "FOCUS", "value": "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": {} }))); + assert_eq!(acp_session_mcp_servers(None, &[extra]).len(), 1); } } diff --git a/crates/harness/src/bounded_file.rs b/crates/harness/src/bounded_file.rs deleted file mode 100644 index 11a8fa07d..000000000 --- a/crates/harness/src/bounded_file.rs +++ /dev/null @@ -1,121 +0,0 @@ -//! 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 deleted file mode 100644 index 77ae44b5c..000000000 --- a/crates/harness/src/claude/mcp_config.rs +++ /dev/null @@ -1,492 +0,0 @@ -//! 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 594e2810d..41b4cc9c6 100644 --- a/crates/harness/src/claude/mod.rs +++ b/crates/harness/src/claude/mod.rs @@ -54,8 +54,6 @@ 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; @@ -172,18 +170,6 @@ 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."; @@ -204,136 +190,28 @@ fn mcp_config_arg(mcp: &zeron_proto::McpServer) -> String { .to_string() } -/// 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( +/// 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( mcp: Option<&zeron_proto::McpServer>, extras: &[zeron_proto::McpServer], -) -> Option<(String, bool)> { +) -> Option { 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(), mcp_server_entry(server)); + servers.insert( + server.name.clone(), + serde_json::json!({ + "command": server.command, + "args": server.args, + "env": server.env, + }), + ); } - 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 + Some(serde_json::json!({ "mcpServers": servers }).to_string()) } /// The `--model` value: the 1M context window is selected via a model-id @@ -609,19 +487,7 @@ 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. @@ -639,15 +505,6 @@ 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"); @@ -678,24 +535,16 @@ 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. - // 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; + if let Some(config) = mcp_servers_config_arg(request.mcp.as_ref(), &request.extra_mcp) { + cmd.args(["--mcp-config", &config]); + } // 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 !config.zeron_left_out - && request - .mcp - .as_ref() - .is_some_and(McpServer::waits_on_background) - { + if 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 @@ -721,7 +570,7 @@ impl ClaudeHarness { .stdout(Stdio::piped()) .stderr(Stdio::piped()) .kill_on_drop(true); - (cmd, mcp_config_dir) + cmd } /// Start one CLI process for a run, with its stdio wired up. @@ -730,15 +579,12 @@ impl ClaudeHarness { request: &RunRequest, title_only: bool, ) -> Result { - 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 + let mut child = Self::build_command(exe, request, title_only) .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(); @@ -760,7 +606,6 @@ impl ClaudeHarness { refresh_lock::config_dir_for(&request.env), ), stderr_tail, - mcp_config, }) } @@ -969,30 +814,6 @@ 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" } @@ -1219,10 +1040,7 @@ 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 { @@ -1264,7 +1082,6 @@ 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 { @@ -1291,8 +1108,6 @@ 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 @@ -1470,8 +1285,6 @@ 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 @@ -1493,7 +1306,6 @@ async fn run_session(session: Session) { kill_grace, lock_dir, mut stderr_tail, - mut _mcp_config, } = session; let RunControls { realtime: _, @@ -1587,7 +1399,6 @@ 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) { @@ -1610,8 +1421,6 @@ 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; @@ -1652,10 +1461,6 @@ 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) => { @@ -2153,7 +1958,6 @@ 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(); } @@ -2501,44 +2305,6 @@ 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 { @@ -2604,7 +2370,6 @@ 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!( @@ -2887,150 +2652,6 @@ 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 { @@ -3040,7 +2661,6 @@ 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"); @@ -3060,14 +2680,12 @@ 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 e1f92a838..f92c9c62d 100644 --- a/crates/harness/src/codex/mod.rs +++ b/crates/harness/src/codex/mod.rs @@ -152,21 +152,10 @@ 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); - // 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([ + vec![ + (key("command"), mcp.command.clone().into()), + (key("args"), json!(mcp.args)), + (key("env"), json!(mcp.env)), // 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. @@ -178,8 +167,7 @@ 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 @@ -3429,32 +3417,6 @@ 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 { @@ -3464,7 +3426,6 @@ 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(); @@ -3489,7 +3450,6 @@ 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 80c1f7e4d..287d3aa29 100644 --- a/crates/harness/src/lib.rs +++ b/crates/harness/src/lib.rs @@ -82,8 +82,6 @@ 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 { @@ -103,20 +101,6 @@ 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 { @@ -315,22 +299,6 @@ 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 @@ -450,7 +418,6 @@ 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 7b5b14c3f..2539b9f3a 100644 --- a/crates/harness/src/mock.rs +++ b/crates/harness/src/mock.rs @@ -25,143 +25,6 @@ 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(); @@ -527,18 +390,6 @@ 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 { @@ -1510,48 +1361,6 @@ 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 a91c1d57d..87c45ff30 100644 --- a/crates/harness/src/model_context.rs +++ b/crates/harness/src/model_context.rs @@ -145,32 +145,18 @@ fn hash_files<'a>( ) -> Result<(), HarnessError> { for path in files { field(hash, path.as_os_str().as_encoded_bytes()); - // 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) { + match std::fs::read(path) { 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() @@ -202,27 +188,4 @@ 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 72f52245a..d9f387e3b 100644 --- a/crates/harness/src/muse/session.rs +++ b/crates/harness/src/muse/session.rs @@ -1089,14 +1089,12 @@ 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 e06c4ed22..716c62f9c 100644 --- a/crates/harness/src/opencode/mod.rs +++ b/crates/harness/src/opencode/mod.rs @@ -5998,14 +5998,12 @@ 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(), @@ -6070,7 +6068,6 @@ 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()); @@ -6163,7 +6160,6 @@ http.createServer((req, res) => { command: "zeron".into(), args: vec!["mcp".into()], env: Default::default(), - http: None, }; let mut server = Server::spawn( &exe, @@ -6290,7 +6286,6 @@ 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( @@ -6317,7 +6312,6 @@ 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 a67e9f690..f52d5b1dd 100644 --- a/crates/harness/src/pi/mcp.rs +++ b/crates/harness/src/pi/mcp.rs @@ -63,14 +63,12 @@ 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 8052c4839..05aad6b08 100644 --- a/crates/harness/src/process.rs +++ b/crates/harness/src/process.rs @@ -164,8 +164,6 @@ 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, } @@ -187,7 +185,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=,pgid="]) + .args(["-A", "-o", "pid=,ppid="]) .stdin(std::process::Stdio::null()) .stderr(std::process::Stdio::null()) .output() @@ -201,11 +199,9 @@ 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), }) }) @@ -227,11 +223,10 @@ 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, pgid, start) = parse_stat(&stat)?; + let (ppid, start) = parse_stat(&stat)?; Some(ProcEntry { pid, ppid, - pgid, start: Some(start), }) }) @@ -241,7 +236,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")] @@ -266,7 +261,6 @@ mod proc_table { Some(ProcEntry { pid, ppid: info.pbi_ppid as i32, - pgid: info.pbi_pgid as i32, start: Some(start_of(&info)), }) }) @@ -301,124 +295,6 @@ 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() @@ -429,114 +305,17 @@ mod proc_table { None } - /// `(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). + /// `(ppid, starttime)` from a `/proc//stat` line: fields 4 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, i32, u64)> { + pub(super) fn parse_stat(stat: &str) -> Option<(i32, u64)> { let rest = &stat[stat.rfind(')')? + 1..]; let fields: Vec<&str> = rest.split_whitespace().collect(); - Some(( - fields.get(1)?.parse().ok()?, - fields.get(2)?.parse().ok()?, - fields.get(19)?.parse().ok()?, - )) + Some((fields.get(1)?.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. @@ -716,7 +495,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, 4242, 987654))); + assert_eq!(proc_table::parse_stat(stat), Some((17, 987654))); assert_eq!(proc_table::parse_stat("4242 (truncated) S 17"), None); } @@ -793,151 +572,3 @@ 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 de3a47604..19490086a 100644 --- a/crates/harness/tests/acp.rs +++ b/crates/harness/tests/acp.rs @@ -2653,7 +2653,6 @@ 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 2404a2d48..bf498ca1d 100644 --- a/crates/harness/tests/acp_tool_fixture.rs +++ b/crates/harness/tests/acp_tool_fixture.rs @@ -72,7 +72,6 @@ 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 21ea84021..4dd6d978b 100644 --- a/crates/harness/tests/claude.rs +++ b/crates/harness/tests/claude.rs @@ -243,7 +243,6 @@ 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 6dbf3a6f0..d110934ee 100644 --- a/crates/harness/tests/codex.rs +++ b/crates/harness/tests/codex.rs @@ -2330,7 +2330,6 @@ 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(); @@ -2398,7 +2397,6 @@ 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( @@ -2465,7 +2463,6 @@ 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 613afff36..d56ac8e99 100644 --- a/crates/harness/tests/cursor.rs +++ b/crates/harness/tests/cursor.rs @@ -702,7 +702,6 @@ 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 4c4eaade0..c723676b5 100644 --- a/crates/mcp/Cargo.toml +++ b/crates/mcp/Cargo.toml @@ -19,10 +19,6 @@ 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 deleted file mode 100644 index a5762842b..000000000 --- a/crates/mcp/src/http.rs +++ /dev/null @@ -1,858 +0,0 @@ -//! 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 deleted file mode 100644 index 8819f5d65..000000000 --- a/crates/mcp/src/http_tests.rs +++ /dev/null @@ -1,424 +0,0 @@ -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 a54116b2e..f381d325f 100644 --- a/crates/mcp/src/jsonrpc.rs +++ b/crates/mcp/src/jsonrpc.rs @@ -28,30 +28,55 @@ 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 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\ +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\ \n\ -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`."; +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`."; /// 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 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."; +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)."; /// Appended to [`INSTRUCTIONS`] when the host launched the agent without its /// own subagent tools ([`zeron_proto::McpServer::SIDE_CHATS_ONLY_ENV`]). @@ -62,7 +87,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. -pub(crate) fn instructions(waits: bool, side_chats_only: bool) -> std::borrow::Cow<'static, str> { +fn instructions(waits: bool, side_chats_only: bool) -> std::borrow::Cow<'static, str> { if !waits && !side_chats_only { return INSTRUCTIONS.into(); } @@ -340,7 +365,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. -pub(crate) fn cap_reply(response: Value) -> Value { +fn cap_reply(response: Value) -> Value { if response.to_string().len() <= MAX_REPLY_BYTES { return response; } @@ -386,7 +411,7 @@ fn ok_response(id: Value, result: Value) -> Value { json!({ "jsonrpc": "2.0", "id": id, "result": result }) } -pub(crate) fn error_response(id: Value, code: i64, message: &str) -> Value { +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 83b29da7a..e45cc83e7 100644 --- a/crates/mcp/src/lib.rs +++ b/crates/mcp/src/lib.rs @@ -16,9 +16,7 @@ //! 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 deleted file mode 100644 index 88a528692..000000000 --- a/crates/mcp/src/proxy.rs +++ /dev/null @@ -1,390 +0,0 @@ -//! `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/mcp/src/tools/plugins.rs b/crates/mcp/src/tools/plugins.rs index 8bdc7d7fd..d8d585bd6 100644 --- a/crates/mcp/src/tools/plugins.rs +++ b/crates/mcp/src/tools/plugins.rs @@ -164,7 +164,7 @@ pub(super) fn catalog() -> Vec { vec![ ToolDef { name: CREATE_PLUGIN, - description: "Create a Zeron plugin on this device: a folder with a manifest (zeron-plugin.json), init.luau to edit, and zeron.luau, the SDK. Zeron signs it as yours and loads it, and re-signs and reloads it as you save; the person allows once, in Zeron, any permissions it asks for. `id` is lowercase letters, digits and dashes (2-40). `permissions` are what the plugin may do (ui.add, ui.change, ui.replace, profile.read, sessions.read, messages.read, account.email, files.read, panel.tabs, pages.add, github, github.write, navigate, compose, network, clipboard, files, web, windows, tools, devices, native, commands: with a manifest `commands` list of programs it runs, as you). The reply links the authoring guide (`guide`): read it before writing.", + description: "Create a Zeron plugin on this device: a folder with a manifest (zeron-plugin.json), init.luau to edit, and zeron.luau, the SDK. Zeron signs it as yours and loads it, and re-signs and reloads it as you save; the person allows once, in Zeron, any permissions it asks for. `id` is lowercase letters, digits and dashes (2-40). `permissions` are what the plugin may do (ui.add, ui.change, ui.replace, profile.read, sessions.read, messages.read, account.email, files.read, panel.tabs, pages.add, github, github.write, navigate, compose, draft, network, clipboard, files, web, windows, tools, devices, native, commands: with a manifest `commands` list of programs it runs, as you). The reply links the authoring guide (`guide`): read it before writing.", input_schema: json!({ "type": "object", "properties": { diff --git a/crates/preview/src/discovery.rs b/crates/preview/src/discovery.rs index fcc7121ed..918037a56 100644 --- a/crates/preview/src/discovery.rs +++ b/crates/preview/src/discovery.rs @@ -197,20 +197,6 @@ 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 ffdfbfb6b..f429f2d31 100644 --- a/crates/preview/src/service.rs +++ b/crates/preview/src/service.rs @@ -40,58 +40,6 @@ 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) @@ -191,7 +139,7 @@ pub type Projects = Arc Scope + Send + Sync>; fn candidates( scope: Scope, origins: &mut discovery::Origins, -) -> Option<(Vec<(PathBuf, discovery::Listener)>, Activity)> { +) -> Option<(Vec<(PathBuf, discovery::Listener)>, bool)> { let Scope { chats, terminals, @@ -216,7 +164,7 @@ fn candidates( .into_iter() .collect(); if roots.is_empty() { - return Some((Vec::new(), Activity { live, owned: None })); + return Some((Vec::new(), live)); } roots.sort_by_key(|p| std::cmp::Reverse(p.components().count())); // The origins a listed server can have (see the filter below). @@ -226,20 +174,8 @@ 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() @@ -437,16 +373,14 @@ 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 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 + // 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 // 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; @@ -470,11 +404,11 @@ impl PreviewService { tracing::debug!( "preview scan could not read processes; keeping the last result" ); - return Activity { live: true, owned: None }; + return true; } Err(error) => { tracing::warn!(%error, "preview scan failed; keeping the last result"); - return Activity { live: true, owned: None }; + return true; } }; let (mut servers, probe) = memory.plan(candidates, Instant::now()); @@ -498,22 +432,12 @@ impl PreviewService { } active }; - 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 { + 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(wait) => {} } + tokio::select! { _ = stop.cancelled() => break, _ = tokio::time::sleep(SCAN_CADENCE) => {} } } else { - tokio::select! { - _ = stop.cancelled() => break, - _ = tokio::time::sleep(wait) => {} - _ = wake.notified() => pace.reset(), - } + tokio::select! { _ = stop.cancelled() => break, _ = wake.notified() => {} } } } }); @@ -688,9 +612,9 @@ mod tests { let mut origins = discovery::Origins::default(); let (found, active) = candidates(scope(false), &mut origins).unwrap(); assert!(found.is_empty()); - assert!(!active.active(), "nothing of this chat runs: nothing to scan for"); + assert!(!active, "nothing of this chat runs: nothing to scan for"); let (_, active) = candidates(scope(true), &mut origins).unwrap(); - assert!(active.active(), "a live run keeps the cadence before its process exists"); + assert!(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); @@ -716,9 +640,7 @@ 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.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!(active, "a process the chat started is alive"); assert!( found.iter().any(|(_, l)| l.address.port() == port && l.pid == server.0.id() @@ -728,28 +650,7 @@ mod tests { ); drop(server); let (found, active) = candidates(scope(false), &mut origins).unwrap(); - 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); + assert!(found.is_empty() && !active, "gone with its process"); } #[test] fn backoff_is_capped() { diff --git a/crates/proto/src/agent.rs b/crates/proto/src/agent.rs index 6000a8577..2ac0ee609 100644 --- a/crates/proto/src/agent.rs +++ b/crates/proto/src/agent.rs @@ -666,12 +666,7 @@ 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). -/// -/// 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)] +#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct McpServer { pub name: String, @@ -680,78 +675,9 @@ 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 deleted file mode 100644 index a357dab1d..000000000 --- a/crates/rpc/examples/idle_rig.rs +++ /dev/null @@ -1,334 +0,0 @@ -//! 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 fd73decc4..132dd6e49 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::{agent_service, serve_connection, serve_ws_listener, serve_ws_listener_scoped}; +pub use server::{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,15 +190,6 @@ 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":""}`. @@ -701,11 +692,6 @@ 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 88d6a966c..bed0a8f57 100644 --- a/crates/rpc/src/server.rs +++ b/crates/rpc/src/server.rs @@ -234,30 +234,6 @@ 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] @@ -269,15 +245,6 @@ 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 } @@ -364,7 +331,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, pinned: false }), + Some(chat) => Arc::new(AgentGuard { inner: service, chat }), 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 970452759..0e90501ab 100644 --- a/crates/sync/src/store.rs +++ b/crates/sync/src/store.rs @@ -213,8 +213,6 @@ 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 { @@ -275,7 +273,6 @@ 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()), }) } @@ -295,30 +292,10 @@ impl DocsStore { self.sync_jobs_changed.send_modify(|v| *v = v.wrapping_add(1)); } - 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()); - } - } + fn note_outbox_changed(&self) { 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() @@ -365,7 +342,7 @@ impl DocsStore { params![doc_id, batch_id, bytes], ); if matches!(result, Ok(n) if n > 0) { - self.note_outbox_changed(doc_id); + self.note_outbox_changed(); } if result.is_err() { self.failed_publications @@ -482,26 +459,6 @@ 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, @@ -561,7 +518,7 @@ impl DocsStore { } tx.commit()?; if !updates.is_empty() { - self.note_outbox_changed(doc_id); + self.note_outbox_changed(); } Ok(()) } diff --git a/crates/sync/src/wake.rs b/crates/sync/src/wake.rs index 79261244f..e7a5d4f45 100644 --- a/crates/sync/src/wake.rs +++ b/crates/sync/src/wake.rs @@ -38,70 +38,40 @@ static PATH_OFFLINE: AtomicBool = AtomicBool::new(false); /// See [`set_resting`]. static RESTING: OnceLock> = OnceLock::new(); -/// 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`]). +/// Subscribe to system-wake events (the detector spawns on first call). pub fn subscribe() -> broadcast::Receiver<()> { CHANNEL .get_or_init(|| { let (tx, _) = broadcast::channel(4); - if !OS_EVENTS.load(Ordering::Acquire) { - spawn_detector(tx.clone()); - } + 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(); + } + }); 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) } diff --git a/crates/update/src/lib.rs b/crates/update/src/lib.rs index d698dbe58..067653bbd 100644 --- a/crates/update/src/lib.rs +++ b/crates/update/src/lib.rs @@ -1248,10 +1248,11 @@ fn on_cloud() -> bool { on_cloud_platform(std::env::var("ZERON_DEVICE_PLATFORM").ok().as_deref()) } -/// Release files a Cloud machine never runs: the desktop app's background -/// engine (78 MB). A Cloud machine runs `zeron headless`, which links the -/// engine, and its disk is copied back in on every wake. -pub const CLOUD_UNUSED_FILES: &[&str] = &["zeron-engine"]; +/// Release files a Cloud machine never runs: none. (`zeron-engine` was here +/// until 2026-11-15, when `zeron headless` linked the engine. Since 0.3.0 the +/// release's `zeron` is the zgx app, whose `zeron headless` runs the +/// `zeron-engine` beside it: a machine without it never came online.) +pub const CLOUD_UNUSED_FILES: &[&str] = &[]; /// Remove [`CLOUD_UNUSED_FILES`] from the version folder `dir`. pub fn strip_cloud_unused(dir: &Path) { @@ -3692,9 +3693,11 @@ mod tests { assert_eq!(folders(&app_root), ["0.2.10", "0.2.11", "0.2.12", "current"]); } + /// `zeron headless` runs the `zeron-engine` beside it: a Cloud machine + /// keeps it in every version (it once left it out, and never came online). #[cfg(unix)] #[test] - fn a_cloud_machine_keeps_no_desktop_engine() { + fn a_cloud_machine_keeps_the_engine_its_headless_runs() { let tmp = tempfile::tempdir().unwrap(); let app_root = tmp.path().join("app"); versions_installed(&app_root, &["0.2.10", "0.2.11"]); @@ -3708,7 +3711,7 @@ mod tests { strip_unused_from_versions(&app_root); for ver in ["0.2.10", "0.2.11"] { - assert!(!app_root.join(ver).join("zeron-engine").exists(), "{ver}"); + assert!(app_root.join(ver).join("zeron-engine").exists(), "{ver}"); assert!(app_root.join(ver).join("zeron").exists(), "{ver}"); } assert!(app_root.join("notes").join("zeron-engine").exists()); @@ -4169,11 +4172,11 @@ mod tests { assert!(staged.join("zeron").is_file()); } - /// The release carries the desktop's background engine; a Cloud machine - /// stages the release without it, anywhere else keeps it. + /// The release carries `zeron-engine`, which `zeron headless` runs: a + /// Cloud machine stages it as anywhere else does. #[cfg(unix)] #[tokio::test] - async fn a_cloud_machine_stages_a_release_without_the_desktop_engine() { + async fn a_cloud_machine_stages_a_release_with_its_engine() { let tmp = tempfile::tempdir().unwrap(); let pkg = tmp.path().join("pkg"); std::fs::create_dir_all(pkg.join("zeron-pkg")).unwrap(); @@ -4190,7 +4193,7 @@ mod tests { .into(), ..Manifest::default() }; - for (cloud, kept) in [(true, false), (false, true)] { + for (cloud, kept) in [(true, true), (false, true)] { let app_root = tmp.path().join(format!("app-{cloud}")); std::fs::create_dir_all(&app_root).unwrap(); let (base, server) = serve_forever(bytes.clone()).await; diff --git a/crates/update/tests/update_transaction.rs b/crates/update/tests/update_transaction.rs index 3a50d6bb3..882ab5210 100644 --- a/crates/update/tests/update_transaction.rs +++ b/crates/update/tests/update_transaction.rs @@ -489,7 +489,7 @@ mod versioned { let mut rig = Rig::new("ok"); rig.platform = platform; rig.install_old(&["0.8.0", "0.9.0"]); - // The desktop's background engine, as the release ships it. + // The engine `zeron headless` runs, as the release ships it. for version in ["1.0.0", "2.0.0"] { std::fs::write(rig.app.join(version).join("zeron-engine"), "desktop").unwrap(); } @@ -497,12 +497,9 @@ mod versioned { rig.run_until_settled(); rig.assert_updated(&id); rig.app_settles_to(expected); - // A Cloud machine never runs it, and copies it back in on every wake. - assert_eq!( - rig.app.join("2.0.0/zeron-engine").exists(), - platform.is_none(), - "platform {platform:?}" - ); + // Kept everywhere, Cloud too: `zeron headless` runs it (a Cloud + // machine once left it out, and never came online). + assert!(rig.app.join("2.0.0/zeron-engine").exists(), "platform {platform:?}"); } } diff --git a/docs/design/cloud-device.md b/docs/design/cloud-device.md index df22d29af..994867f73 100644 --- a/docs/design/cloud-device.md +++ b/docs/design/cloud-device.md @@ -431,12 +431,12 @@ machine (`ZERON_DEVICE_PLATFORM=cloud`) keeps none of them: going back there is downloads anyway. Other Linux installs keep the newest older one, to go back to by hand. Never `current`, the running executable's folder, or a newer version (an update being staged). -A Cloud machine doesn't keep the release's `zeron-engine` either: it's the desktop app's background -engine (78 MB), and the unit runs `zeron headless`, which links the engine. The installer and the -updater leave it out, and the tidy and the pre-sleep cleanup remove it from versions installed before. -Measured on staging (send to first output, n=8 each), a machine on one version went from a median of -17.5 s to 14.2 s once neither it nor Codex's downloads came back with each wake; its wake downloaded -225 MB, 205 of them the engine it runs. +A Cloud machine keeps the release's `zeron-engine` (78 MB). From 2026-11-07 to 2026-11-15 the +installer, the updater and the tidy left it out, when the unit's `zeron headless` linked the engine; +since 0.3.0 the release's `zeron` is the zgx app, whose `zeron headless` runs that `zeron-engine`, so +a machine without it exited at once and never enrolled. The installer keeps it now, and a repair +(`~/.zeron/repair-engine`, the unit's `ExecStartPre=-`, set up by the installer and every wake) puts +it back before each start if an older updater left it out (edge/src/cloud/install-script.ts). A longer wake reads as updating: the engine says so (`POST /runner/wake-stage`), `CloudSession.stage` is `updating` while it lasts (a stage nobody clears stops showing after 10 minutes), the desktop's diff --git a/edge/src/cloud/install-script.ts b/edge/src/cloud/install-script.ts index eed6fe562..b6c96c15d 100644 --- a/edge/src/cloud/install-script.ts +++ b/edge/src/cloud/install-script.ts @@ -48,6 +48,60 @@ ExecStart=/bin/systemctl start --no-block ${CLOUD_UNIT_NAME} WantedBy=multi-user.target `; +/** + * Puts back the release's `zeron-engine` beside the `zeron` the unit runs, when + * it's missing. Since 0.3.0 the release's `zeron` is the zgx app, whose + * `zeron headless` hands off to that engine: without it the unit exits at + * once, restarts every second and never enrolls. Machines lost it to the + * installer, the engine's own updater and the pre-sleep tidy, which all left + * it out on Cloud (0.3.5's still do), so the unit runs this before every start + * (`ExecStartPre=-`: it never fails a start). Downloads only while it's + * missing, the version `current` names, from the edge's own releases. + */ +export const REPAIR_ENGINE_SCRIPT = `#!/bin/sh +# Zeron Cloud: put back a missing zeron-engine (edge/src/cloud/install-script.ts). +home="\${ZERON_CLOUD_HOME:-$HOME}" +cur="$home/.zeron/app/current" +[ -x "$cur/zeron" ] || exit 0 +[ -f "$cur/zeron-engine" ] && exit 0 +dest="$(readlink -f "$cur")" || exit 0 +ver="$(basename "$dest")" +case "$ver" in + "" | *[!A-Za-z0-9._+-]*) exit 0 ;; +esac +case "$(uname -m)" in + x86_64 | amd64) arch=x86_64 ;; + aarch64 | arm64) arch=aarch64 ;; + *) exit 0 ;; +esac +[ -n "\${ZERON_EDGE_URL:-}" ] || exit 0 +tmp="$(mktemp -d "$home/.zeron/.repair.XXXXXX")" || exit 0 +trap 'rm -rf "$tmp"' EXIT +curl -fsSL --retry 3 --connect-timeout 15 --speed-limit 1048576 --speed-time 15 \\ + -o "$tmp/zeron.tar.gz" "$ZERON_EDGE_URL/releases/zeron-$ver-linux-$arch.tar.gz" || exit 0 +tar -xzf "$tmp/zeron.tar.gz" -C "$tmp" "zeron-$ver-linux-$arch/zeron-engine" || exit 0 +chmod 755 "$tmp/zeron-$ver-linux-$arch/zeron-engine" +mv -f "$tmp/zeron-$ver-linux-$arch/zeron-engine" "$dest/zeron-engine" +echo "zeron cloud: put back zeron-engine for $ver" +`; + +/** + * Writes the repair script (`REPAIR_ENGINE_SCRIPT`) to `~/.zeron/repair-engine` + * and a drop-in running it before the engine's unit starts, each only when it + * differs, reloading systemd only then. Run by the installer and by every + * wake's first command, so machines set up before it get it as they wake. + * Best effort: a failure here never stops what follows. + * `ZERON_CLOUD_HOME` / `ZERON_CLOUD_UNIT_DIR` exist for the unit tier. + */ +export const ENSURE_REPAIR = + `{ h="\${ZERON_CLOUD_HOME:-$HOME}"; r="$h/.zeron/repair-engine"; ` + + `dd="\${ZERON_CLOUD_UNIT_DIR:-/etc/systemd/system}/${CLOUD_UNIT_NAME}.d"; ` + + `t="$(mktemp)" && printf %s '${btoa(REPAIR_ENGINE_SCRIPT)}' | base64 -d >"$t" && ` + + `{ cmp -s "$t" "$r" || install -m 0700 "$t" "$r"; } && ` + + `printf '[Service]\\nExecStartPre=-%s\\n' "$r" >"$t" && ` + + `{ cmp -s "$t" "$dd/repair-engine.conf" || { sudo mkdir -p "$dd" && sudo install -m 0644 "$t" "$dd/repair-engine.conf" && sudo systemctl daemon-reload; }; }; ` + + `rm -f "$t"; } >/dev/null 2>&1 || true`; + export const CLOUD_INSTALL_SCRIPT = `#!/bin/sh # Zeron Cloud engine installer (edge/src/cloud/install-script.ts). # POSIX sh, idempotent, provider-neutral. @@ -85,7 +139,9 @@ esac restart=0 dest="$app/$ver" -if [ -x "$dest/zeron" ]; then +# (Whole, its zeron-engine too: \`zeron headless\` runs it, and installs +# made before 2026-11-15 left it out.) +if [ -x "$dest/zeron" ] && [ -f "$dest/zeron-engine" ]; then echo "zeron $ver already installed" else tmp="$(mktemp -d "$data/.download.XXXXXX")" @@ -99,10 +155,8 @@ else tar -xzf "$tmp/zeron.tar.gz" -C "$tmp/unpacked" --strip-components=1 # Probe before switching: a binary that cannot start must not replace one that can. "$tmp/unpacked/zeron" --version >/dev/null - # The desktop app's background engine (78 MB): never run here (the unit runs - # \`zeron headless\`, which links the engine), and every byte on this disk is - # copied back in on every wake. The engine's updater leaves it out too. - rm -f "$tmp/unpacked/zeron-engine" + # (zeron-engine stays: \`zeron headless\` runs it. Without it the unit + # exits at once and the machine never enrolls.) rm -rf "$dest" mv "$tmp/unpacked" "$dest" fi @@ -172,6 +226,8 @@ else rm -f "$boot_new" fi +# A zeron-engine an update leaves out is put back before each start. +${ENSURE_REPAIR} sudo systemctl daemon-reload sudo systemctl enable ${CLOUD_UNIT_NAME} ${CLOUD_BOOT_UNIT_NAME} >/dev/null sudo systemctl start ${CLOUD_BOOT_UNIT_NAME} @@ -253,6 +309,9 @@ export const RESTART_ENGINE_COMMAND = `sudo systemctl restart ${CLOUD_UNIT_NAME} * systemd hasn't read yet gets one reload. `--no-block` answers at once (the * engine's first token says it's up). Idempotent: a no-op when it's running. */ export const START_ENGINE_COMMAND = + // (First, the repair of a missing zeron-engine before each start, for + // machines set up before it: a no-op once it's there.) + `${ENSURE_REPAIR}; ` + `sudo systemctl start --no-block ${CLOUD_UNIT_NAME} || ` + `{ sudo systemctl daemon-reload; sudo systemctl start --no-block ${CLOUD_UNIT_NAME}; }`; diff --git a/edge/test/unit/install-script.test.ts b/edge/test/unit/install-script.test.ts index b96da8ce3..a4b8420ec 100644 --- a/edge/test/unit/install-script.test.ts +++ b/edge/test/unit/install-script.test.ts @@ -13,6 +13,7 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { CLOUD_BOOT_UNIT, CLOUD_INSTALL_SCRIPT, + REPAIR_ENGINE_SCRIPT, START_ENGINE_COMMAND, STOP_ENGINE_COMMAND, installCommand, @@ -110,8 +111,15 @@ describe("cloud install script", { timeout: 60_000 }, () => { const data = join(home, ".zeron"); expect(readlinkSync(join(data, "app", "current"))).toBe(join(data, "app", "1.2.3")); expect(existsSync(join(data, "app", "1.2.3", "zeron"))).toBe(true); - // Never run on Cloud, and copied back in on every wake if kept. - expect(existsSync(join(data, "app", "1.2.3", "zeron-engine"))).toBe(false); + // Kept: `zeron headless` runs it (without it the unit exits at once and + // the machine never enrolls). + expect(existsSync(join(data, "app", "1.2.3", "zeron-engine"))).toBe(true); + // And put back before each start, should an update leave it out. + expect(statSync(join(data, "repair-engine")).mode & 0o777).toBe(0o700); + expect(readFileSync(join(data, "repair-engine"), "utf8")).toBe(REPAIR_ENGINE_SCRIPT); + expect(readFileSync(join(units, "zeron-cloud.service.d", "repair-engine.conf"), "utf8")).toBe( + `[Service]\nExecStartPre=-${join(data, "repair-engine")}\n` + ); const envFile = join(data, "env"); expect(statSync(envFile).mode & 0o777).toBe(0o600); expect(readFileSync(envFile, "utf8")).toBe( @@ -159,6 +167,45 @@ describe("cloud install script", { timeout: 60_000 }, () => { expect(calls()).toContain("systemctl restart zeron-cloud.service"); }); + it("downloads again a version installed without its zeron-engine", () => { + const { home, run, calls, clearCalls } = setup(); + run("org.user.cloud-1.codeA"); + const engine = join(home, ".zeron", "app", "1.2.3", "zeron-engine"); + rmSync(engine); + clearCalls(); + run("org.user.cloud-1.codeA"); + expect(existsSync(engine)).toBe(true); + expect(calls().filter((c) => c.includes(".tar.gz"))).toHaveLength(1); + }); + + it("puts back a zeron-engine an update left out, and does nothing once it's there", () => { + const { home, run, calls, clearCalls } = setup(); + run("org.user.cloud-1.codeA"); + const data = join(home, ".zeron"); + const engine = join(data, "app", "1.2.3", "zeron-engine"); + const repair = () => + execFileSync("sh", [join(data, "repair-engine")], { + env: { PATH: `${join(root, "bin")}:/usr/bin:/bin`, ZERON_CLOUD_HOME: home, ZERON_EDGE_URL: "https://edge.test" }, + encoding: "utf8" + }); + rmSync(engine); + clearCalls(); + expect(repair()).toContain("put back zeron-engine for 1.2.3"); + expect(existsSync(engine)).toBe(true); + expect(statSync(engine).mode & 0o111).not.toBe(0); + expect(calls().filter((c) => c.includes("/releases/zeron-1.2.3-linux-x86_64.tar.gz"))).toHaveLength(1); + // There: no download. + clearCalls(); + repair(); + expect(calls().filter((c) => c.startsWith("curl"))).toHaveLength(0); + // A download that fails leaves the start to go on (it never fails it). + rmSync(engine); + writeFileSync(join(root, "bin", "curl"), "#!/bin/sh\nexit 22\n"); + chmodSync(join(root, "bin", "curl"), 0o755); + expect(() => repair()).not.toThrow(); + expect(existsSync(engine)).toBe(false); + }); + it("doesn't wait for the network to start the engine, and starts it on boot without being asked", () => { const { units, run } = setup(); run("org.user.cloud-1.codeA"); @@ -237,9 +284,19 @@ describe("cloud install script", { timeout: 60_000 }, () => { }); it("starts the engine without waiting on it, and reloads once for a unit systemd hasn't read", () => { - const { calls, clearCalls } = setup(); + const { home, units, calls, clearCalls } = setup(); + mkdirSync(join(home, ".zeron"), { recursive: true }); const start = () => - execFileSync("sh", ["-c", START_ENGINE_COMMAND], { env: { PATH: `${join(root, "bin")}:/usr/bin:/bin` }, encoding: "utf8" }); + execFileSync("sh", ["-c", START_ENGINE_COMMAND], { + env: { PATH: `${join(root, "bin")}:/usr/bin:/bin`, ZERON_CLOUD_HOME: home, ZERON_CLOUD_UNIT_DIR: units }, + encoding: "utf8" + }); + // A machine set up before the repair: its first wake writes it, once. + clearCalls(); + start(); + expect(calls()).toEqual(["systemctl daemon-reload", "systemctl start --no-block zeron-cloud.service"]); + expect(readFileSync(join(home, ".zeron", "repair-engine"), "utf8")).toBe(REPAIR_ENGINE_SCRIPT); + expect(readFileSync(join(units, "zeron-cloud.service.d", "repair-engine.conf"), "utf8")).toContain("ExecStartPre=-"); clearCalls(); start(); expect(calls()).toEqual(["systemctl start --no-block zeron-cloud.service"]);