diff --git a/Cargo.lock b/Cargo.lock index a8e9438a7..1b3e8f5f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13617,6 +13617,7 @@ dependencies = [ "zeron-mcp", "zeron-notify", "zeron-rpc", + "zeron-sync", "zeron-ui", "zeron-update", ] @@ -13812,8 +13813,12 @@ dependencies = [ "anyhow", "async-trait", "base64 0.22.1", + "bytes", "chrono", "futures", + "http-body-util", + "hyper", + "hyper-util", "serde", "serde_json", "tempfile", diff --git a/apps/zeron/Cargo.toml b/apps/zeron/Cargo.toml index 2fda76d93..036c03949 100644 --- a/apps/zeron/Cargo.toml +++ b/apps/zeron/Cargo.toml @@ -27,6 +27,7 @@ zeron-update.workspace = true zeron-cli.workspace = true zeron-mcp.workspace = true zeron-notify.workspace = true +zeron-sync.workspace = true tokio.workspace = true clap.workspace = true anyhow.workspace = true diff --git a/apps/zeron/src/allocator.rs b/apps/zeron/src/allocator.rs index c037522f8..edf11b9b1 100644 --- a/apps/zeron/src/allocator.rs +++ b/apps/zeron/src/allocator.rs @@ -31,6 +31,14 @@ fn tag_heap() { } } +/// mimalloc's forced collect: what its threads freed (exited threads' +/// pages too) goes back to the system. The engine calls it after dropping +/// a large short-lived allocation ([`zeron_engine::heap`]). +pub fn trim() { + // SAFETY: mimalloc's collect frees nothing in use. + unsafe { libmimalloc_sys::mi_collect(true) }; +} + unsafe impl GlobalAlloc for Allocator { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { diff --git a/apps/zeron/src/background.rs b/apps/zeron/src/background.rs index b5626acb9..fb4cfb71b 100644 --- a/apps/zeron/src/background.rs +++ b/apps/zeron/src/background.rs @@ -55,6 +55,9 @@ fn run_engine(config: zeron_engine::EngineConfig) -> anyhow::Result<()> { /// are posted on and delivers their clicks. #[cfg(target_os = "macos")] fn run_engine(config: zeron_engine::EngineConfig) -> anyhow::Result<()> { + // Before the engine starts: its sockets then never start the polling + // wake detector (`zeron_sync::wake`). + main_loop::watch_power(); let worker = std::thread::Builder::new() .name("engine".into()) .spawn(move || { @@ -75,9 +78,26 @@ fn run_engine(config: zeron_engine::EngineConfig) -> anyhow::Result<()> { #[cfg(target_os = "macos")] mod main_loop { use std::ffi::c_void; - use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU32, Ordering}; static DONE: AtomicBool = AtomicBool::new(false); + /// The source [`finish`] signals; null until [`run`] makes it. + static STOP: AtomicPtr = AtomicPtr::new(std::ptr::null_mut()); + + /// `CFRunLoopSourceContext`, version 0: only `perform` is used. + #[repr(C)] + struct SourceContext { + version: isize, + info: *mut c_void, + retain: *const c_void, + release: *const c_void, + copy_description: *const c_void, + equal: *const c_void, + hash: *const c_void, + schedule: *const c_void, + cancel: *const c_void, + perform: extern "C" fn(*mut c_void), + } #[link(name = "CoreFoundation", kind = "framework")] unsafe extern "C" { @@ -86,39 +106,100 @@ mod main_loop { fn CFRunLoopGetMain() -> *mut c_void; fn CFRunLoopRunInMode(mode: *const c_void, seconds: f64, return_after_source: u8) -> i32; fn CFRunLoopWakeUp(run_loop: *mut c_void); - fn CFAbsoluteTimeGetCurrent() -> f64; - fn CFRunLoopTimerCreate( + fn CFRunLoopSourceCreate( allocator: *const c_void, - fire_date: f64, - interval: f64, - flags: u64, order: isize, - callout: extern "C" fn(*mut c_void, *mut c_void), - context: *mut c_void, + context: *mut SourceContext, ) -> *mut c_void; - fn CFRunLoopAddTimer(run_loop: *mut c_void, timer: *mut c_void, mode: *const c_void); + fn CFRunLoopAddSource(run_loop: *mut c_void, source: *mut c_void, mode: *const c_void); + fn CFRunLoopSourceSignal(source: *mut c_void); } - extern "C" fn tick(_timer: *mut c_void, _info: *mut c_void) {} + #[link(name = "IOKit", kind = "framework")] + unsafe extern "C" { + fn IORegisterForSystemPower( + refcon: *mut c_void, + port: *mut *mut c_void, + callback: extern "C" fn(*mut c_void, u32, u32, *mut c_void), + notifier: *mut u32, + ) -> u32; + fn IONotificationPortGetRunLoopSource(port: *mut c_void) -> *mut c_void; + fn IOAllowPowerChange(kernel_port: u32, notification_id: isize) -> i32; + } - /// Serve the main run loop until [`finish`]. A repeating timer keeps the - /// loop from returning at once for want of sources and bounds how late - /// the exit check runs. + // IOKit's power messages (IOKit/IOMessage.h). + const CAN_SYSTEM_SLEEP: u32 = 0xe000_0270; + const SYSTEM_WILL_SLEEP: u32 = 0xe000_0280; + const SYSTEM_HAS_POWERED_ON: u32 = 0xe000_0300; + + /// The root power domain's connection, for acknowledging sleep. + static ROOT_PORT: AtomicU32 = AtomicU32::new(0); + + extern "C" fn power(_refcon: *mut c_void, _service: u32, message: u32, argument: *mut c_void) { + match message { + // Every registered client must answer these, or the Mac waits + // up to 30 s before it sleeps. + CAN_SYSTEM_SLEEP | SYSTEM_WILL_SLEEP => unsafe { + IOAllowPowerChange(ROOT_PORT.load(Ordering::Acquire), argument as isize); + }, + SYSTEM_HAS_POWERED_ON => zeron_sync::wake::notify_wake(), + _ => {} + } + } + + /// Hear the Mac's sleep and wake from IOKit on the main run loop, so the + /// engine's sockets redial on wake without a detector waking the + /// process every second to compare clocks. Falls back to that detector + /// if registering fails. + pub(super) fn watch_power() { + unsafe { + let mut port = std::ptr::null_mut(); + let mut notifier = 0u32; + let root = + IORegisterForSystemPower(std::ptr::null_mut(), &mut port, power, &mut notifier); + if root == 0 || port.is_null() { + tracing::warn!( + "couldn't register for power notifications; polling for wake instead" + ); + return; + } + ROOT_PORT.store(root, Ordering::Release); + let source = IONotificationPortGetRunLoopSource(port); + CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopCommonModes); + } + zeron_sync::wake::use_os_wake_events(); + } + + /// The stop source fired: [`run`]'s wait returns and it reads `DONE`. + extern "C" fn stop(_info: *mut c_void) {} + + /// Serve the main run loop until [`finish`]. It sleeps until something + /// arrives (a banner's click, the stop source) rather than on a timer: + /// an idle engine's main thread never wakes. The stop source both keeps + /// the loop from returning at once for want of sources and makes a + /// `finish` that comes before the wait begins end it at once (a signaled + /// source is served as soon as the loop runs). pub(super) fn run() { unsafe { let main = CFRunLoopGetMain(); - let timer = CFRunLoopTimerCreate( - std::ptr::null(), - CFAbsoluteTimeGetCurrent() + 1.0, - 1.0, - 0, - 0, - tick, - std::ptr::null_mut(), - ); - CFRunLoopAddTimer(main, timer, kCFRunLoopCommonModes); + let mut context = SourceContext { + version: 0, + info: std::ptr::null_mut(), + retain: std::ptr::null(), + release: std::ptr::null(), + copy_description: std::ptr::null(), + equal: std::ptr::null(), + hash: std::ptr::null(), + schedule: std::ptr::null(), + cancel: std::ptr::null(), + perform: stop, + }; + let source = CFRunLoopSourceCreate(std::ptr::null(), 0, &mut context); + CFRunLoopAddSource(main, source, kCFRunLoopCommonModes); + STOP.store(source, Ordering::SeqCst); while !DONE.load(Ordering::SeqCst) { - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, 0); + // (Returns after each source served, so `DONE` is read then.) + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, 1); } } } @@ -126,6 +207,12 @@ mod main_loop { /// End [`run`] (any thread). pub(super) fn finish() { DONE.store(true, Ordering::SeqCst); - unsafe { CFRunLoopWakeUp(CFRunLoopGetMain()) }; + let source = STOP.load(Ordering::SeqCst); + unsafe { + if !source.is_null() { + CFRunLoopSourceSignal(source); + } + CFRunLoopWakeUp(CFRunLoopGetMain()); + } } } diff --git a/apps/zeron/src/bin/zeron-engine.rs b/apps/zeron/src/bin/zeron-engine.rs index 76d9bcbc1..0b6648a1d 100644 --- a/apps/zeron/src/bin/zeron-engine.rs +++ b/apps/zeron/src/bin/zeron-engine.rs @@ -2,8 +2,9 @@ //! own ("Zeron Engine" in Activity Monitor): the app starts it and attaches; //! sessions keep running with the app closed. Without the UI linked in. //! -//! It also answers what the engine runs its own executable for: `mcp` (the -//! Zeron MCP server injected into agents) and `--noop-browser` (browser +//! It also answers what the engine runs its own executable for: `mcp-proxy` +//! (an agent's Zeron MCP tools on stdio, served by the engine), `mcp` (the +//! standalone Zeron MCP server) and `--noop-browser` (browser //! suppression for agents' sign-in flows). And it is the engine helper of an //! interface that doesn't link the engine (the zgx app): every `zeron` //! subcommand that runs the engine's code (`headless`, `login`, `status`, @@ -17,6 +18,9 @@ use zeron_cli::{Engines, Launch}; fn main() -> anyhow::Result<()> { match std::env::args().nth(1).as_deref() { Some("--noop-browser") => Ok(()), + // An agent's Zeron MCP tools on stdio, forwarded to the engine's own + // endpoint. First, before any engine setup: it must stay tiny. + Some("mcp-proxy") => zeron_mcp::proxy::run(), Some("--version" | "-V") => { println!("zeron-engine {}", zeron_update::current_version()); Ok(()) diff --git a/apps/zeron/src/lib.rs b/apps/zeron/src/lib.rs index 265c0d7d5..f65d61475 100644 --- a/apps/zeron/src/lib.rs +++ b/apps/zeron/src/lib.rs @@ -12,11 +12,11 @@ pub mod engine_commands; pub mod ipc_cli; pub mod plugin_cli; +pub use engine_commands::run_engine_command; pub use zeron_cli::{ DEFAULT_EDGE_URL, DEFAULT_WORKOS_CLIENT_ID, edge_url_from_env, init_logging, long_running_setup, open_log_file, paths, workos_client_id_from_env, }; -pub use engine_commands::run_engine_command; /// mimalloc, macOS only: libmalloc never returns the streaming churn's /// high-water pages, so transient allocation became permanent RSS @@ -33,6 +33,9 @@ static ALLOC: allocator::Allocator = allocator::Allocator; /// `logout`, and `status` — one resolution so the CLI auth commands always /// operate on the exact session the daemon will load. pub fn engine_config_from_env() -> zeron_engine::EngineConfig { + // (Every way an engine starts here resolves its configuration first.) + #[cfg(target_os = "macos")] + zeron_engine::heap::set_trim(allocator::trim); // Dev-mode bearer (no WorkOS): an explicit token enables sync. let edge_token = std::env::var("ZERON_EDGE_TOKEN").ok(); zeron_engine::EngineConfig { diff --git a/apps/zeron/src/main.rs b/apps/zeron/src/main.rs index 0a3d049b8..30ca2a5f1 100644 --- a/apps/zeron/src/main.rs +++ b/apps/zeron/src/main.rs @@ -12,6 +12,11 @@ use zeron_app::{edge_url_from_env, paths, workos_client_id_from_env}; use zeron_cli::{Engines, Launch}; fn main() -> anyhow::Result<()> { + // The engine runs its own executable as an agent's stdio MCP forwarder + // (`zeron headless`'s agents): first, before any setup, to stay tiny. + if std::env::args().nth(1).as_deref() == Some("mcp-proxy") { + return zeron_mcp::proxy::run(); + } match zeron_cli::run(zeron_update::current_version(), Engines::InProcess) { Launch::Done(result) => result, Launch::Engine(command) => zeron_app::run_engine_command(command), diff --git a/apps/zgx/Cargo.lock b/apps/zgx/Cargo.lock index 1fa07a604..e51e42a4b 100644 --- a/apps/zgx/Cargo.lock +++ b/apps/zgx/Cargo.lock @@ -30,7 +30,7 @@ dependencies = [ [[package]] name = "accesskit_atspi_common" version = "0.19.1" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "accesskit", "accesskit_consumer", @@ -43,7 +43,7 @@ dependencies = [ [[package]] name = "accesskit_consumer" version = "0.38.0" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "accesskit", "hashbrown 0.16.1", @@ -66,7 +66,7 @@ dependencies = [ [[package]] name = "accesskit_macos" version = "0.26.3" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "accesskit", "accesskit_consumer", @@ -79,7 +79,7 @@ dependencies = [ [[package]] name = "accesskit_unix" version = "0.22.1" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "accesskit", "accesskit_atspi_common", @@ -96,7 +96,7 @@ dependencies = [ [[package]] name = "accesskit_windows" version = "0.34.0" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "accesskit", "accesskit_consumer", @@ -6450,7 +6450,7 @@ dependencies = [ [[package]] name = "wgpu-hal" version = "29.0.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "android_system_properties", "arrayvec", @@ -6888,7 +6888,7 @@ checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" [[package]] name = "winit" version = "0.30.13" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "ahash", "android-activity", @@ -7456,7 +7456,7 @@ dependencies = [ [[package]] name = "zgx" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "fixed_decimal", "foldhash 0.2.0", @@ -7485,7 +7485,7 @@ dependencies = [ [[package]] name = "zgx-3d" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "bytemuck", "zgx", @@ -7495,7 +7495,7 @@ dependencies = [ [[package]] name = "zgx-alacritty-terminal" version = "0.26.8" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "base64 0.22.1", "bitflags 2.13.2", @@ -7520,7 +7520,7 @@ dependencies = [ [[package]] name = "zgx-components" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "zgx", ] @@ -7528,7 +7528,7 @@ dependencies = [ [[package]] name = "zgx-cosmic-text" version = "0.19.14" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "bitflags 2.13.2", "fontdb", @@ -7552,7 +7552,7 @@ dependencies = [ [[package]] name = "zgx-desktop" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "accesskit", "accesskit_consumer", @@ -7603,12 +7603,12 @@ dependencies = [ [[package]] name = "zgx-ghostty-sys" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" [[package]] name = "zgx-gpu" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "ash", "block2 0.6.2", @@ -7648,7 +7648,7 @@ dependencies = [ [[package]] name = "zgx-harfrust" version = "0.5.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "bitflags 2.13.2", "bytemuck", @@ -7660,7 +7660,7 @@ dependencies = [ [[package]] name = "zgx-mlua" version = "0.12.9" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "bstr", "either", @@ -7675,7 +7675,7 @@ dependencies = [ [[package]] name = "zgx-run" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "serde_json", "ureq 3.5.0", @@ -7687,7 +7687,7 @@ dependencies = [ [[package]] name = "zgx-script" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "icu_locale_core", "icu_plurals", @@ -7712,7 +7712,7 @@ dependencies = [ [[package]] name = "zgx-swash" version = "0.2.11" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "skrifa", "yazi", @@ -7722,7 +7722,7 @@ dependencies = [ [[package]] name = "zgx-taffy" version = "0.9.2" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "arrayvec", "grid", @@ -7733,7 +7733,7 @@ dependencies = [ [[package]] name = "zgx-terminal" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "base64 0.22.1", "flate2", @@ -7749,7 +7749,7 @@ dependencies = [ [[package]] name = "zgx-vte" version = "0.15.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "arrayvec", "bitflags 2.13.2", @@ -7761,7 +7761,7 @@ dependencies = [ [[package]] name = "zgx-zune-jpeg" version = "0.5.15" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/zeronsh/zgx?rev=d0dc793b073c388ec418abbd4d19acb6ca5295f3#d0dc793b073c388ec418abbd4d19acb6ca5295f3" dependencies = [ "zune-core", ] diff --git a/apps/zgx/Cargo.toml b/apps/zgx/Cargo.toml index eae2c0300..0cf1bffe5 100644 --- a/apps/zgx/Cargo.toml +++ b/apps/zgx/Cargo.toml @@ -32,7 +32,7 @@ layout_probe = [] [dependencies] lz4_flex = { version = "0.11", default-features = false, features = ["safe-encode", "safe-decode"] } -zgx = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } +zgx = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } # `mimalloc`: the program's allocator, in place of the system's. It hands # freed memory back to the system (the system's keeps the most it ever # held), and zgx measures 1-10% less CPU for script, text and layout work. @@ -43,7 +43,7 @@ zgx = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191 # none of it, and without it pages are placeholders ("Web view · "). # zgx-run turns it on by default, but this app takes zgx-run without its # defaults (below). Nothing on macOS or Windows. -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["mimalloc", "terminal", "webkitgtk"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3", features = ["mimalloc", "terminal", "webkitgtk"] } # `inspect`: devtools for `zgx inspect` (and its MCP server), asked for # with `--inspect`. `i18n`: `ui.t`'s plurals and numbers by each locale's # own rules (ICU4X's data, about 90 KB), for ui/locales' translations. @@ -51,18 +51,18 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb # wgpu this app already links), for plugins that show 3D: the CAD plugin # hands it its own triangles (`geometry = { positions, indices, … }`). It # asks no grant; a scene draws a frame only when something in it changes. -zgx-script = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["inspect", "i18n", "scene3d"] } +zgx-script = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3", features = ["inspect", "i18n", "scene3d"] } # A picture's pixels for the clipboard (src/images.rs): zgx's own decoder, # the formats `ui.image` shows (no features of its own: the ones zgx-desktop # turns on). -zgx-gpu = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", default-features = false } +zgx-gpu = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3", default-features = false } # Each plugin's own database (`zeron.db`, src/plugins/db.rs): SQLite, # built in; `limits` to keep one from attaching any other file. rusqlite = { version = "0.32", features = ["bundled", "limits", "hooks"] } # `fetch`: zgx's own transport for `ui.fetch` and remote images (HTTP and # HTTPS, rustls), given to every window (src/main.rs): the app's scripts, # and plugins granted "network". Nothing else of the runner. -zgx-run = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", default-features = false, features = ["fetch"] } +zgx-run = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3", default-features = false, features = ["fetch"] } # `webp`: `ui.image` decodes through this crate, by what its features # allow, so turning WebP on here is all it takes (the member cards' art). # `ico`: sites' favicons (the browser tab's: src/favicons.rs), most of @@ -162,7 +162,7 @@ foreign-types = "0.5" # elements, box drawing and Powerline glyphs to the cell; zgx's own renderer # draws them from the font at its text line height, gapped. Builds Ghostty # with Zig 0.16 (cached in ~/Library/Caches/zgx-ghostty). -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["ghostty"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3", features = ["ghostty"] } # Windows' Transparency effects (src/transparency.rs), read and watched in # the registry, until zgx reports them (zgx-asks/windows-transparency); a @@ -173,7 +173,7 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb # 11 (and with Edge on Windows 10); without it, pages are placeholders. [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_Media_Audio", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Console", "Win32_System_Registry", "Win32_System_Threading", "Win32_UI_WindowsAndMessaging"] } -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["webview2"] } +zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3", features = ["webview2"] } # zgx's D-Bus (menus, portals) linked in rather than loaded: `zeron` is also # what a headless Linux server and every Cloud machine runs (`zeron @@ -185,13 +185,13 @@ libdbus-sys = { version = "0.2.7", features = ["vendored"] } # zgx's own patches (its root Cargo.toml): a git dependency does not bring # them, so every consumer repeats them. [patch.crates-io] -wgpu-hal = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -winit = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_macos = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_consumer = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_atspi_common = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_unix = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_windows = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } +wgpu-hal = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } +winit = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } +accesskit_macos = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } +accesskit_consumer = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } +accesskit_atspi_common = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } +accesskit_unix = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } +accesskit_windows = { git = "https://github.com/zeronsh/zgx", rev = "d0dc793b073c388ec418abbd4d19acb6ca5295f3" } # zgx is slow unoptimized (its own setting): optimize dependencies, keep # this crate quick to build. diff --git a/apps/zgx/src/chat.rs b/apps/zgx/src/chat.rs index ba44e8181..c65bd6d7c 100644 --- a/apps/zgx/src/chat.rs +++ b/apps/zgx/src/chat.rs @@ -250,7 +250,12 @@ impl zgx_script::Unfold for FoldedEntry { let json = lz4_flex::block::decompress_size_prepended(&self.bytes).ok(); let entry = json.and_then(|bytes| serde_json::from_slice::(&bytes).ok()).unwrap_or(Json::Null); let RowContext { doc, device, model } = &*self.context; - built_entry(&entry, doc, device.as_deref(), model.as_deref()).unwrap_or(Value::Nil) + // (Opened as it comes into view, after Settings' compact transcript + // may have been turned on: laid out compact as well, whichever shows.) + COMPACT_ANYWAY.set(true); + let row = built_entry(&entry, doc, device.as_deref(), model.as_deref()).unwrap_or(Value::Nil); + COMPACT_ANYWAY.set(false); + row } } @@ -275,14 +280,241 @@ struct Built { subagents: Option>, /// Folded, its compressed bytes' length (what a chat kept is weighed by). folded: usize, + /// A long reply's blocks, held here rather than in its row (`set_aside`). + aside: Option>, +} + +/// A long settled reply's blocks and tools, kept out of its row. A script is +/// handed a table for every value of a row it reads, so a reply of a thousand +/// steps cost its 2,000 blocks' and 1,000 tools' tables each time its chat +/// opened (about 3 ms of a long thread's open on an M5 Max, more than all it +/// builds), though its end alone shows. Its row says its runs instead +/// (ds/transcript's windowed runs: each its first block's id, how many +/// blocks, a guess at its height, a sum of what it holds), and the +/// transcript asks for a run's blocks as it builds that run +/// (`chat.reply_run`), the end's alone as it opens. +struct Aside { + /// Each run: its key (its first block's id) and its blocks. + runs: Vec<(Rc, Rc<[Value]>)>, + blocks: Rc<[Value]>, +} + +/// More blocks than this, a settled reply's are set aside (ds/transcript's +/// LONG_REPLY, past which it windows them). +const LONG_REPLY: usize = 48; +/// ds/transcript's runs: a block whose id falls on RUN starts one, and none +/// runs past RUN_MOST. +const RUN: u64 = 32; +const RUN_MOST: usize = 64; + +thread_local! { + /// The long replies set aside, by their entry's id: the latest built of + /// each, while a view's rows (or a chat kept) hold it. + static ASIDE: RefCell, std::rc::Weak