diff --git a/apps/zgx/Cargo.lock b/apps/zgx/Cargo.lock index b685ad7cc..46ac7b1ee 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" 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=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "ahash", "android-activity", @@ -7456,7 +7456,7 @@ dependencies = [ [[package]] name = "zgx" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "fixed_decimal", "foldhash 0.2.0", @@ -7484,7 +7484,7 @@ dependencies = [ [[package]] name = "zgx-3d" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bytemuck", "zgx", @@ -7494,7 +7494,7 @@ dependencies = [ [[package]] name = "zgx-alacritty-terminal" version = "0.26.4" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "base64 0.22.1", "bitflags 2.13.2", @@ -7519,7 +7519,7 @@ dependencies = [ [[package]] name = "zgx-components" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "zgx", ] @@ -7527,7 +7527,7 @@ dependencies = [ [[package]] name = "zgx-cosmic-text" version = "0.19.14" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bitflags 2.13.2", "fontdb", @@ -7551,7 +7551,7 @@ dependencies = [ [[package]] name = "zgx-desktop" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "accesskit", "accesskit_consumer", @@ -7602,12 +7602,12 @@ dependencies = [ [[package]] name = "zgx-ghostty-sys" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" [[package]] name = "zgx-gpu" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "ash", "block2 0.6.2", @@ -7646,7 +7646,7 @@ dependencies = [ [[package]] name = "zgx-harfrust" version = "0.5.3" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bitflags 2.13.2", "bytemuck", @@ -7658,7 +7658,7 @@ dependencies = [ [[package]] name = "zgx-mlua" version = "0.12.8" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bstr", "either", @@ -7673,7 +7673,7 @@ dependencies = [ [[package]] name = "zgx-run" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "serde_json", "ureq 3.5.0", @@ -7685,7 +7685,7 @@ dependencies = [ [[package]] name = "zgx-script" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "icu_locale_core", "icu_plurals", @@ -7709,7 +7709,7 @@ dependencies = [ [[package]] name = "zgx-swash" version = "0.2.11" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "skrifa", "yazi", @@ -7719,7 +7719,7 @@ dependencies = [ [[package]] name = "zgx-taffy" version = "0.9.2" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "arrayvec", "grid", @@ -7730,7 +7730,7 @@ dependencies = [ [[package]] name = "zgx-terminal" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "base64 0.22.1", "flate2", @@ -7746,7 +7746,7 @@ dependencies = [ [[package]] name = "zgx-vte" version = "0.15.0" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "arrayvec", "bitflags 2.13.2", @@ -7758,7 +7758,7 @@ dependencies = [ [[package]] name = "zgx-zune-jpeg" version = "0.5.15" -source = "git+https://github.com/zeronsh/zgx?rev=ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b#ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "zune-core", ] diff --git a/apps/zgx/Cargo.toml b/apps/zgx/Cargo.toml index 175eef720..13757f2e1 100644 --- a/apps/zgx/Cargo.toml +++ b/apps/zgx/Cargo.toml @@ -31,7 +31,10 @@ layout_probe = [] [dependencies] lz4_flex = { version = "0.11", default-features = false, features = ["safe-encode", "safe-decode"] } -zgx = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } +# Temporarily pinned to katulevskiy/zgx linux-idle-spin-ac935c6 (zeronsh/zgx +# ac935c6 plus the Linux idle-spin fix) until that fix merges into zgx; then +# back to zeronsh/zgx at a rev that has it. Every zgx entry below follows. +zgx = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } # `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. @@ -42,7 +45,7 @@ zgx = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12 # 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 = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b", features = ["mimalloc", "terminal", "webkitgtk"] } +zgx-desktop = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0", 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. @@ -50,18 +53,18 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5 # 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 = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b", features = ["inspect", "i18n", "scene3d"] } +zgx-script = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0", 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 = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b", default-features = false } +zgx-gpu = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0", 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 = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b", default-features = false, features = ["fetch"] } +zgx-run = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0", 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 @@ -161,7 +164,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 = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b", features = ["ghostty"] } +zgx-desktop = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0", features = ["ghostty"] } # Windows' Transparency effects (src/transparency.rs), read and watched in # the registry, until zgx reports them (zgx-asks/windows-transparency); a @@ -172,7 +175,7 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5 # 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_Registry", "Win32_System_Threading", "Win32_UI_WindowsAndMessaging"] } -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b", features = ["webview2"] } +zgx-desktop = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0", 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 @@ -184,13 +187,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 = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } -winit = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } -accesskit_macos = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } -accesskit_consumer = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } -accesskit_atspi_common = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } -accesskit_unix = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } -accesskit_windows = { git = "https://github.com/zeronsh/zgx", rev = "ac935c6f888a4d2c5c8a6ac12bf47771b1a44e9b" } +wgpu-hal = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } +winit = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } +accesskit_macos = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } +accesskit_consumer = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } +accesskit_atspi_common = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } +accesskit_unix = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } +accesskit_windows = { git = "https://github.com/katulevskiy/zgx", rev = "e49ae7a3cbfc67167db24455605650d7c53803c0" } # zgx is slow unoptimized (its own setting): optimize dependencies, keep # this crate quick to build. diff --git a/apps/zgx/src/audit_shots.rs b/apps/zgx/src/audit_shots.rs index febdc88de..c35d8e7d5 100644 --- a/apps/zgx/src/audit_shots.rs +++ b/apps/zgx/src/audit_shots.rs @@ -13,12 +13,14 @@ //! A shot is `` (ui/_fixture.luau, or a name ending //! .luau) or `gallery:`, optionally `@x`, `#` (crop to //! a node's box) and `!` / `=:` / `^` (a click) / `+` (a wait) -//! steps, joined by `|`: +//! / `::` (a click, then the text typed) steps, joined by `|`: //! `gallery:composer|#gallery-demo@1240x820`. `AUDIT_SCALE` (2) is the //! pixels a point; `AUDIT_DARK=1` the dark scheme where the screen has one. //! What's transparent (the window's glass) is laid on `AUDIT_BACKDROP` //! (`#e9e9ee` light, `#1d1d22` dark), the same under both. `AUDIT_WAIT_MS` //! lets real time pass before the picture, for what comes over the network. +//! `AUDIT_MID=1` draws the picture right after the last step (no settling): +//! with a `+` wait, an animation caught partway. //! //! Plugins too: `ZERON_ZGX_DEV_PLUGINS` loads plugin folders into each shot //! (lent the design system, as the app lends it), and `ZERON_ZGX_DEV_GITHUB` @@ -48,6 +50,8 @@ enum Step { Key(String), // The pointer resting on a target (`*target`): its hover state. Hover(String), + // A field clicked, then text typed into it (`:target:text`). + Type(String, String), State(String, serde_json::Value), } @@ -86,6 +90,9 @@ fn parse(spec: &str, dark: bool) -> Shot { steps.push(Step::Key(key.to_string())); } else if let Some(target) = part.strip_prefix('*') { steps.push(Step::Hover(target.to_string())); + } else if let Some(rest) = part.strip_prefix(':') { + let (target, text) = rest.split_once(':').expect("::"); + steps.push(Step::Type(target.to_string(), text.to_string())); } else if let Some(command) = part.strip_prefix('!') { steps.push(Step::Command(command.to_string())); } else if let Some(rest) = part.strip_prefix('=') { @@ -152,6 +159,7 @@ fn take(shot: &Shot, out: &Path, scale: f32, dark: bool) -> Result { @@ -168,6 +176,10 @@ fn take(shot: &Shot, out: &Path, scale: f32, dark: bool) -> Result { app.keys(key); } + Step::Type(target, text) => { + app.target(target).map_err(|e| format!("{}: type into {target}: {e}", shot.name))?; + app.type_text(target, text); + } Step::Hover(target) => { app.target(target).map_err(|e| format!("{}: hover {target}: {e}", shot.name))?; app.hover(target); @@ -183,11 +195,17 @@ fn take(shot: &Shot, out: &Path, scale: f32, dark: bool) -> Result` wait's + // frames are all an animation gets: a picture mid-roll.) + if !mid { + app.settle(); + app.frames(10); + } } // Long enough for entrances and springs to finish. - app.frames(90); + if !mid { + app.frames(90); + } wait(&mut app); if let Some(error) = app.error() { eprintln!("audit_shots: {}: script error: {error}", shot.name); diff --git a/apps/zgx/src/chat.rs b/apps/zgx/src/chat.rs index 83a4b933f..848a4117e 100644 --- a/apps/zgx/src/chat.rs +++ b/apps/zgx/src/chat.rs @@ -135,6 +135,9 @@ struct Chats { /// Moves with each chat let go of: only the last one's look at the /// footprint, once the app has rested, counts. let_go_turn: Cell, + /// What the log last said is kept (how many, in tenths of a MB): said + /// again only when it changes, not at every switch. + let_go_said: Cell<(usize, u64)>, } /// A transcript's entry. A settled one, built into its row and older than @@ -365,6 +368,9 @@ fn plainly_has_row(entry: &Json) -> bool { if entry["id"].as_str().is_none() { return false; } + if is_crew_reports(entry) { + return true; + } match entry["role"].as_str() { Some("user") => true, // As `blocks_of`: a tool that isn't its output alone, or a thought with words. @@ -576,6 +582,10 @@ struct View { /// A finished subagent's transcript, read whole from its uploaded blob /// (a lane's): nothing to watch while it stands. snapshot: Cell, + /// A lane that shows no transcript (`session_only`: Capture's composer, + /// which needs the chat's row and the send path): its transcript isn't + /// watched, and its `messages` stay empty. + session_only: Cell, } /// A prompt sent and not yet in the transcript: shown under it meanwhile, @@ -715,6 +725,7 @@ pub fn install(_cx: &mut WindowContext, host: &ScriptHost) { pending: RefCell::default(), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); chats.views.borrow_mut().push(Rc::downgrade(&view)); if chats.views.borrow().len() == 1 { @@ -760,6 +771,12 @@ pub fn install(_cx: &mut WindowContext, host: &ScriptHost) { } let id = args.field("id").map(text).filter(|s| !s.is_empty()); let view = lanes.borrow_mut().entry(slot.clone()).or_insert_with(|| lane_view(&host, &slot)).clone(); + // (Its transcript wanted again, or no longer: the chat opened + // afresh, so its watches are what it now wants.) + let session_only = args.field("session_only").is_some_and(Value::truthy); + if view.session_only.replace(session_only) != session_only { + view.open_chat(None); + } // A finished subagent: its uploaded transcript first. match (id, args.field("frozen").is_some_and(Value::truthy)) { (Some(id), true) => view.open_frozen(id, args.field("chat").map(text).filter(|s| !s.is_empty())), @@ -971,6 +988,7 @@ fn lane_view(host: &ScriptHost, slot: &str) -> Rc { pending: RefCell::default(), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); chats.views.borrow_mut().push(Rc::downgrade(&view)); let weak = Rc::downgrade(&view); @@ -1201,11 +1219,10 @@ impl Chats { } kept.push(LetGo { at: std::time::Instant::now(), bytes, id, entries, older_todo, built, built_model, context, context_usage }); prune_let_go(&mut kept); - eprintln!( - "zeron-zgx: chat: kept chats let go of: {}, {:.1} MB", - kept.len(), - kept.iter().map(|held| held.bytes).sum::() as f64 / 1e6 - ); + let said = (kept.len(), (kept.iter().map(|held| held.bytes).sum::() as f64 / 1e5).round() as u64); + if self.let_go_said.replace(said) != said { + eprintln!("zeron-zgx: chat: kept chats let go of: {}, {:.1} MB", said.0, said.1 as f64 / 10.0); + } drop(kept); // The app's footprint once it has come to rest after the switch (as // it lets a chat go it's opening the next, drawing it, its GPU @@ -1218,6 +1235,7 @@ impl Chats { zgx::timer::sleep(LET_GO_SETTLE).await; if me.let_go_turn.get() == turn && !room_to_keep() && !me.let_go.borrow().is_empty() { me.let_go.borrow_mut().clear(); + me.let_go_said.set((0, 0)); eprintln!("zeron-zgx: chat: kept chats let go of: none (the app's footprint at rest is past its mark)"); } }); @@ -1833,7 +1851,11 @@ impl View { // A chat being made has nothing more to come yet: its rows (its // first message) are all there is. let heard = chats.creating.borrow().contains_key(&id); - chats.take_let_go(&id).unwrap_or(Open { id, heard, ..Open::default() }) + let mut open = chats.take_let_go(&id).unwrap_or(Open { id, heard, ..Open::default() }); + // (No transcript is watched: none is coming, and its row is + // what it shows.) + open.heard |= self.session_only.get(); + open }); let has_rows = next.as_ref().is_some_and(|o| o.heard); let old = self.open.replace(next); @@ -1927,17 +1949,16 @@ impl View { if let Some(open) = self.open.borrow().as_ref() { params = with_known(params, open); } - let transcript = rpc.watch( - "WatchDocMessages", - params, - move |item| { + // (A lane showing no transcript: its row and queue alone.) + let transcript = (!self.session_only.get()).then(|| { + rpc.watch("WatchDocMessages", params, move |item| { if let Some(view) = weak.upgrade() && view.generation.get() == generation { view.heard_frame(item); } - }, - ); + }) + }); let weak = Rc::downgrade(self); let queue = rpc.watch("WatchQueue", json!({ "chatId": id }), move |item| { if let Some(view) = weak.upgrade() @@ -1962,7 +1983,7 @@ impl View { } }); if let Some(open) = self.open.borrow_mut().as_mut() { - open.watches = vec![transcript, queue, waiting]; + open.watches = transcript.into_iter().chain([queue, waiting]).collect(); } } @@ -2265,6 +2286,10 @@ impl View { } fn publish_messages(&self) { + if self.session_only.get() { + self.messages.set(Value::List(Rc::from([]))); + return; + } let chats = CHATS.with(Rc::clone); let subagents; let list = { @@ -2309,8 +2334,11 @@ impl View { // then working, as the shipping app's working trailer says // (transcript.rs `sending_bridge`): sending while the send is // newer than the session's turn, working once the turn starts. + // (A crew updates card is the engine's, not a prompt: no + // sending or working line under it.) if let Some(last) = open.entries.last() && last["role"].as_str() == Some("user") + && !last.json.as_ref().is_some_and(is_crew_reports) && let Some(Value::Map(_)) = list.last() { let sent = last["createdAt"].as_f64().unwrap_or(0.) / 1000.; @@ -2423,57 +2451,14 @@ impl View { return Ok(()); } // What it carries, uploaded to the chat's host. - let mut paths = Vec::new(); - let mut images = 0; - // (Each file here, by what it was sent as: an Appshot's context names - // its picture by the path the agent has.) - let mut sent_as: std::collections::HashMap = std::collections::HashMap::new(); - // (Asked once, when there's a file to send: `store_takes_files`.) - let mut through_store = None; - let mut transfers: Vec = Vec::new(); - for a in req.field("attachments").map(Value::list).unwrap_or_default() { - let path = a.field("path").map(text).unwrap_or_default(); - let name = a.field("name").map(text).unwrap_or_else(|| path.clone()); - if path.is_empty() { - continue; + let Carried { paths, images, sent_as, transfers } = match carry(&req, target.as_deref()).await { + Ok(carried) => carried, + Err(why) => { + // Failed before it could go: the prompt says so. + self.mark_failed(&id, &message_id, None); + return Err(why); } - if a.field("kind").map(text).as_deref() == Some("image") { - images += 1; - } - // A file of the session's own project (the files pane's): on its - // computer already, so it goes by its path there. - if a.field("on_host").is_some_and(Value::truthy) { - paths.push(path); - continue; - } - let store = match through_store { - Some(store) => store, - None => { - let store = goes_through_store(target.as_deref()).await; - through_store = Some(store); - store - } - }; - let uploaded = if store { - stage(&path, &name).await.map(|(reference, transfer)| { - transfers.push(transfer); - reference - }) - } else { - upload(&path, &name, target.as_deref()).await - }; - match uploaded { - Ok(at) => { - sent_as.insert(path.clone(), at.clone()); - paths.push(at) - } - Err(why) => { - // Failed before it could go: the prompt says so. - self.mark_failed(&id, &message_id, None); - return Err(why); - } - } - } + }; // Appshots: the apps they show, as untrusted observed content after // the words (src/appshots, the shipping app's `with_appshots`). let shots: Vec = req @@ -2574,7 +2559,18 @@ impl View { p.transfers = transfers; } self.mark_failed(&id, &message_id, Some(run)); - } else if !fresh && runs_otherwise(&chat["config"], &config) { + } else if self.session_only.get() { + // (No transcript comes to say it arrived: delivered is done, + // unless the outbox holds it, which says when it leaves.) + let mut pending = self.pending.borrow_mut(); + if let Some(prompts) = pending.get_mut(&id) { + prompts.retain(|p| p.id != message_id || p.held); + if prompts.is_empty() { + pending.remove(&id); + } + } + } + if sent.is_ok() && !fresh && runs_otherwise(&chat["config"], &config) { // Sent with another agent, model or effort than the chat had (the // composer's pick, kept as a draft until now): the chat takes // them, as the shipping app's next turn does, and its icon with @@ -3326,6 +3322,83 @@ fn merged_config(config: &Json, args: &Value) -> Json { c } +/// What a message carries, sent on: each file's path where the run reads +/// it, how many are pictures, each by the local path it was sent from, and +/// what the engine must deliver first (files staged through its store). +pub(crate) struct Carried { + pub(crate) paths: Vec, + pub(crate) images: usize, + pub(crate) sent_as: std::collections::HashMap, + pub(crate) transfers: Vec, +} + +/// A request's `attachments` (`{ kind, path, name, on_host }`, as the +/// composer packs them) uploaded toward `target` (the chat's device; none: +/// this one): a file of the chat's own project goes by its path there, the +/// rest uploaded to it, or staged through the engine's store where Cloud +/// takes files so. The chat page's send and the Brain dump's (a dump's op) +/// alike. +pub(crate) async fn carry(req: &Value, target: Option<&str>) -> Result { + let mut paths = Vec::new(); + let mut images = 0; + // (Each file here, by what it was sent as: an Appshot's context names + // its picture by the path the agent has.) + let mut sent_as: std::collections::HashMap = std::collections::HashMap::new(); + // (Asked once, when there's a file to send: `store_takes_files`.) + let mut through_store = None; + let mut transfers: Vec = Vec::new(); + for a in req.field("attachments").map(Value::list).unwrap_or_default() { + let path = a.field("path").map(text).unwrap_or_default(); + let name = a.field("name").map(text).unwrap_or_else(|| path.clone()); + if path.is_empty() { + continue; + } + if a.field("kind").map(text).as_deref() == Some("image") { + images += 1; + } + // A file of the session's own project (the files pane's): on its + // computer already, so it goes by its path there. + if a.field("on_host").is_some_and(Value::truthy) { + paths.push(path); + continue; + } + let store = match through_store { + Some(store) => store, + None => { + let store = goes_through_store(target).await; + through_store = Some(store); + store + } + }; + let at = if store { + let (reference, transfer) = stage(&path, &name).await?; + transfers.push(transfer); + reference + } else { + upload(&path, &name, target).await? + }; + sent_as.insert(path.clone(), at.clone()); + paths.push(at); + } + Ok(Carried { paths, images, sent_as, transfers }) +} + +/// Chat `id`'s config with `picks` (a composer's: `model` as +/// `harness/model`, `effort`, `access`, `fast`, `options`, `plan`) laid +/// over it, as a send to it would set it (`merged_config`). +pub(crate) fn config_with(id: &str, picks: &Value) -> Json { + let chats = CHATS.with(Rc::clone); + let config = chats.chat(id).map(|c| c["config"].clone()).unwrap_or(Json::Null); + merged_config(&config, picks) +} + +/// Where chat `id` runs, as a send uploads toward it (none: this device). +pub(crate) fn target_of(id: &str) -> Option { + let chats = CHATS.with(Rc::clone); + let chat = chats.chat(id)?; + chats.target(&chat) +} + /// The prompt with its attachments named after it, as the shipping app /// writes it (crates/ui/src/attachments.rs). fn with_attachments(words: &str, paths: &[String], images: usize) -> String { @@ -4503,9 +4576,69 @@ impl Subagents { } } +/// Whether `entry` carries crew reports (an orchestrator's "Crew updates"). +fn is_crew_reports(entry: &Json) -> bool { + entry["parts"].as_array().into_iter().flatten().any(|p| p["kind"].as_str() == Some("crewReports")) +} + +/// The crew reports `entry` carries, as ds/orch_reports reads them: each +/// `{ id, from_chat, item_id?, state, summary, detail?, progress?, at }`, +/// its state in snake_case and its time in Unix seconds, in the order filed +/// (every `crewReports` part of it, one after another). None: it has none. +fn crew_reports_of(entry: &Json) -> Option { + if !is_crew_reports(entry) { + return None; + } + let reports: Vec = entry["parts"] + .as_array() + .into_iter() + .flatten() + .filter(|p| p["kind"].as_str() == Some("crewReports")) + .flat_map(|p| p["reports"].as_array().into_iter().flatten()) + .filter_map(|r| { + let id = r["id"].as_str()?; + let from = r["fromChat"].as_str()?; + let state = match r["state"].as_str().unwrap_or_default() { + s @ ("progress" | "done" | "blocked" | "failed" | "message") => s, + _ => "other", + }; + let at = r["at"] + .as_str() + .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok()) + .map(|t| t.timestamp()) + .or_else(|| seconds(&r["at"])); + let text = |key: &str| r[key].as_str().filter(|s| !s.trim().is_empty()).map_or(Value::Nil, Value::from); + Some(Value::record([ + ("id", Value::from(id)), + ("from_chat", Value::from(from)), + ("item_id", text("itemId")), + ("state", Value::from(state)), + ("summary", Value::from(single_line(r["summary"].as_str().unwrap_or_default()).as_str())), + ("detail", text("detail")), + ("progress", r["progress"].as_f64().map_or(Value::Nil, Value::Num)), + ("at", at.map_or(Value::Nil, |t| Value::Num(t as f64))), + ])) + }) + .collect(); + Some(Value::List(reports.into())) +} + /// An entry as ds/transcript's message; nil for one it doesn't show. pub(crate) fn message(entry: &Json, model: Option<&str>) -> Option { let id = entry["id"].as_str()?; + // Crew reports filed in an orchestrator's transcript (the engine's + // `crewReports` part, the wake turn's own message): a "Crew updates" + // card, never the person's bubble, whatever role carries it. + if let Some(reports) = crew_reports_of(entry) { + let created = entry["createdAt"].as_f64().unwrap_or(0.) / 1000.; + return Some(Value::record([ + ("id", Value::from(id)), + ("role", Value::from("crew_reports")), + ("text", Value::from("")), + ("reports", reports), + ("at", stamp(created as i64).map_or(Value::Nil, |at| Value::from(at.as_str()))), + ])); + } match entry["role"].as_str()? { "user" => { let text = entry["parts"] @@ -6148,6 +6281,23 @@ mod tests { assert!(would(&ms, &none, now)); } + /// A lane opened for its session only (Capture's composer): its row + /// reads ready at once, with no transcript watched or published. + #[test] + fn a_session_only_lane_is_ready_with_no_transcript() { + use zgx_script::testing::TestApp; + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/orch_push_fixture.luau"); + let app = TestApp::new(|runtime| zgx_script::ScriptHost::new(runtime, path)); + let view = super::lane_view(&app.host, "capture-test"); + view.session_only.set(true); + view.open_chat(Some("chat-brain".into())); + assert!(view.open.borrow().as_ref().is_some_and(|o| o.heard && o.watches.is_empty())); + view.publish_messages(); + assert_eq!(view.messages.get(), Value::List(std::rc::Rc::from([]))); + let session = view.session.get(); + assert_eq!(session.field("state"), Some(&Value::from("ready"))); + } + /// A queued message's files come back to an edit as chips named as they /// were attached, already on the host, a picture shown where it can be. #[test] @@ -7018,6 +7168,52 @@ mod tests { assert_eq!(found[1].field("at").and_then(Value::as_f64), Some(2.)); } + /// Crew reports in an orchestrator's transcript (the engine's + /// `crewReports` part, on the wake turn's user entry) are a "Crew + /// updates" card, never the person's bubble: each report's thread, + /// state, summary and time, in the order filed. + #[test] + fn crew_reports_are_a_card_not_a_prompt() { + let entry = json!({ + "id": "w1", "role": "user", "createdAt": 1_794_470_000_000_i64, + "parts": [ + { "kind": "text", "id": "t", "text": "[Crew updates]\n- th-a done: shipped" }, + { "kind": "crewReports", "reports": [ + { "id": "rp-1", "orchestratorId": "orc", "fromChat": "th-a", "itemId": "it-1", "state": "done", + "summary": "Shipped the\nregistry", "detail": "All green.", "progress": 1.0, "at": "2026-11-12T18:30:00Z" }, + { "id": "rp-2", "orchestratorId": "orc", "fromChat": "th-b", "state": "blocked", + "summary": "Needs a decision", "at": "2026-11-12T18:35:00+00:00" }, + { "id": "rp-3", "orchestratorId": "orc", "fromChat": "th-c", "state": "someday", + "summary": "?", "at": "2026-11-12T18:36:00Z" }, + ] }, + ], + }); + assert!(super::plainly_has_row(&entry)); + let m = message(&entry, None).expect("a card shows"); + let is = |v: Option<&Value>, want: &str| matches!(v, Some(Value::Str(s)) if s.as_ref() == want); + assert!(is(m.field("role"), "crew_reports")); + assert!(is(m.field("text"), ""), "the wake's generated prompt is the agent's, not shown"); + assert!(m.field("sender").is_none_or(|s| matches!(s, Value::Nil))); + assert!(m.field("copy").is_none_or(|s| matches!(s, Value::Nil))); + let Some(Value::List(reports)) = m.field("reports") else { panic!("reports") }; + assert_eq!(reports.len(), 3); + assert!(is(reports[0].field("id"), "rp-1")); + assert!(is(reports[0].field("from_chat"), "th-a")); + assert!(is(reports[0].field("item_id"), "it-1")); + assert!(is(reports[0].field("state"), "done")); + assert!(is(reports[0].field("summary"), "Shipped the registry")); + assert!(is(reports[0].field("detail"), "All green.")); + assert_eq!(reports[0].field("progress").and_then(Value::as_f64), Some(1.)); + assert_eq!(reports[0].field("at").and_then(Value::as_f64), Some(1_794_508_200.)); + assert!(is(reports[1].field("state"), "blocked")); + assert!(reports[1].field("item_id").is_none_or(|v| matches!(v, Value::Nil))); + assert_eq!(reports[1].field("at").and_then(Value::as_f64), Some(1_794_508_500.)); + assert!(is(reports[2].field("state"), "other"), "a newer peer's state"); + // A plain prompt is still the person's. + let plain = json!({ "id": "u", "role": "user", "parts": [{ "kind": "text", "id": "t", "text": "hi" }] }); + assert!(is(message(&plain, None).unwrap().field("role"), "user")); + } + #[test] fn a_message_from_another_chat_says_who_over_its_words() { let id = "cd27c5c2-0000-4000-8000-000000000000"; @@ -7635,6 +7831,7 @@ mod tests { pending: RefCell::default(), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }) }; let time = |label: &str, view: &Rc, frame: serde_json::Value| { @@ -7691,6 +7888,7 @@ mod tests { pending: RefCell::default(), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); for (n, step) in fixture["steps"].as_array().unwrap().iter().enumerate() { view.heard_frame(step["frame"].clone()); @@ -7732,6 +7930,7 @@ mod tests { pending: RefCell::default(), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); let rows = || format!("{:?}", view.messages.signal().with_untracked(Clone::clone)); // What it holds, as it would tell the engine: ids and the engine's sums. @@ -7820,6 +8019,7 @@ mod tests { pending: RefCell::default(), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); let draft = || format!("{:?}", view.draft.signal().with_untracked(Clone::clone)); let rows = || format!("{:?}", view.messages.signal().with_untracked(Clone::clone)); diff --git a/apps/zgx/src/chat_pending_tests.rs b/apps/zgx/src/chat_pending_tests.rs index 3b14a8410..fa7ba8ac0 100644 --- a/apps/zgx/src/chat_pending_tests.rs +++ b/apps/zgx/src/chat_pending_tests.rs @@ -27,6 +27,7 @@ fn pending_chat() -> (TestApp, Rc, Sending) { subagents: app.host.shared("test.subagents", Value::Nil), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); CHATS.with(|chats| { chats.made( diff --git a/apps/zgx/src/chat_stream_tests.rs b/apps/zgx/src/chat_stream_tests.rs index e66d7677e..26b39e818 100644 --- a/apps/zgx/src/chat_stream_tests.rs +++ b/apps/zgx/src/chat_stream_tests.rs @@ -20,6 +20,7 @@ fn open_chat() -> (TestApp, Rc) { subagents: app.host.shared("test.subagents", Value::Nil), generation: Cell::new(0), snapshot: Cell::new(false), + session_only: Cell::new(false), }); CHATS.with(|chats| chats.made("stream-chat", json!({ "id": "stream-chat" }))); view.open_chat(Some("stream-chat".into())); diff --git a/apps/zgx/src/engine/lists.rs b/apps/zgx/src/engine/lists.rs index 4d281cdbc..64a82c4be 100644 --- a/apps/zgx/src/engine/lists.rs +++ b/apps/zgx/src/engine/lists.rs @@ -220,7 +220,7 @@ pub(crate) mod work { /// The key a list method's rows go by. fn key_of(method: &str) -> &'static str { - if method == "WatchSessions" { "chatId" } else { "id" } + if matches!(method, "WatchSessions" | "WatchProgress") { "chatId" } else { "id" } } type Listener = Box; diff --git a/apps/zgx/src/engine/mod.rs b/apps/zgx/src/engine/mod.rs index d4f8e76e3..fc4ceacd2 100644 --- a/apps/zgx/src/engine/mod.rs +++ b/apps/zgx/src/engine/mod.rs @@ -193,6 +193,11 @@ impl Engine { self.config.as_ref().ok().map(|c| c.data_dir.clone()) } + /// Whether [`Engine::spawn`] runs what it's given (a window's tasks are there). + pub fn can_spawn(&self) -> bool { + self.tasks.borrow().is_some() + } + /// `work` on the connection's tasks. pub fn spawn(&self, work: impl std::future::Future + 'static) { if let Some(tasks) = &*self.tasks.borrow() { diff --git a/apps/zgx/src/main.rs b/apps/zgx/src/main.rs index 3b9e2fb38..1bfaf7d61 100644 --- a/apps/zgx/src/main.rs +++ b/apps/zgx/src/main.rs @@ -57,6 +57,10 @@ mod pasted; mod math; mod menu_bar; mod mermaid; +mod orchestration; +mod orchestration_progress; +mod scratchpad; +mod orchestration_timeline; mod plugins; mod previews; mod projects; @@ -110,6 +114,8 @@ mod picture_aspect_tests; #[cfg(test)] mod chip_copy_tests; #[cfg(test)] +mod receipt_copy_tests; +#[cfg(test)] mod drop_tests; #[cfg(test)] mod picker_settle_tests; @@ -134,6 +140,8 @@ mod i18n_tests; mod updates; #[cfg(test)] mod visuals_tests; +#[cfg(test)] +mod orchestration_board_tests; use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc, time::Duration}; @@ -813,6 +821,15 @@ fn script_host_at(cx: &mut WindowContext, path: PathBuf, entry: &str) -> ScriptH cloud::install(cx, &host); // The sessions behind the main window (src/sessions.rs). sessions::install(cx, &host); + // Orchestrators, their crews, board, asks and Brain dump + // (src/orchestration.rs). + orchestration::install(cx, &host); + // What each chat has been doing lately (src/orchestration_timeline.rs). + orchestration_timeline::install(cx, &host); + // How far each chat's work has got (src/orchestration_progress.rs). + orchestration_progress::install(cx, &host); + // The Control Room's Scratchpad (src/scratchpad.rs). + scratchpad::install(cx, &host); // What there is to update, on every device (src/updates.rs). updates::install(&host); // Adding a project, removing one (src/projects.rs). diff --git a/apps/zgx/src/orchestration.rs b/apps/zgx/src/orchestration.rs new file mode 100644 index 000000000..ec38312a8 --- /dev/null +++ b/apps/zgx/src/orchestration.rs @@ -0,0 +1,1692 @@ +//! Orchestrators for the scripts: the long-lived manager chats, their crews, +//! the board, the asks waiting on you and the Brain dump's receipts, as the +//! engine streams them (`WatchOrchestration`), with the devices' load while a +//! screen shows it (`WatchDeviceLoads`) and the messages moving between agents +//! (`WatchComms`); and the one call that changes any of it (`Orchestrate`). +//! docs/design/orchestrators.md ("Bridge") is the contract; +//! ui/account/orchestration_sample.luau is a whole example of each. +//! +//! What scripts see (snake_case keys, times in Unix seconds, enum words in +//! snake_case: "brain_dump", "needs_you", "new_orchestrator"…; a field that +//! isn't there is nil): +//! `orchestration` +//! { state = "loading" | "ready", +//! orchestrators = { { id, kind, name, goal, rules, crew_rules, notes, hue, +//! crew = { devices, max_running?, harness?, model? }, +//! wake? = { every_minutes?, at, weekdays_only, prompt, last_woke? }, +//! paused, archived, created, updated } }, +//! crew = { { chat_id, orchestrator_id, item_id?, role, +//! last_report? = { id, state, summary, at }, released, joined } }, +//! items = { { id, orchestrator_id?, title, detail, state, priority, progress?, +//! threads, links = { { kind, label, target } }, outcome?, +//! source? = { dump_id, route_id }, order, created, updated, done?, +//! space_id?, device_id?, waiting? } }, +//! asks = { { id, from_chat, orchestrator_id?, item_id?, kind, question, +//! options = { { label, detail?, images = { path_or_url }, +//! scores = { { name, value } } } }, +//! urgent, state, answer?, +//! created, answered?, origin = "tool" | "turn_end", turn_id?, +//! updated?, escalated?, answered_via?, quiet, +//! images = { path_or_url }, recommendation? = { option? (1-based), +//! label?, why? }, links = { { kind, label, target } }, context?, +//! deferred_until? } }, +//! dumps = { { id, text, state, routes = { { id, excerpt, span? = { from, to }, +//! target = { kind, id? }, intent, message, state, +//! candidates = { target }, answer?, answer_full?, answered?, delivered?, +//! direct, item_id?, +//! attachments = { path } } }, +//! summary?, device, created, updated, +//! attachments = { { path, name } } } } (newest first) } +//! `orchestration.loads` { [device id] = { cpu?, mem_used?, mem_total?, load1?, +//! cores?, online, at, +//! history = { { at, cpu } } } } (while a screen +//! wants them; `history` the CPU samples heard +//! this run, oldest first, the last six minutes) +//! `orchestration.comms` { { id, kind, from, to, summary, at } } (newest last, +//! the latest 100; `from`/`to` a chat id or "you") +//! +//! and call: +//! orchestration.op(op) one `Orchestrate` op, as the wire has it +//! (camelCase: `{ op = "answerAsk", id, answer }`): +//! its reply (`{ id }` for what it made), or +//! rejected with the engine's words +//! orchestration.answer_ask({ id, option?, text? }) +//! the person's answer: `option` 1-based (the +//! engine's 0-based), `text` their words (either +//! or both); `{ id }`, or rejected +//! orchestration.defer_ask({ id, until? }) +//! put an ask off until `until` (Unix seconds), +//! nil: back now; `{ id }`, or rejected +//! orchestration.loads_wanted(on) (an action) a screen showing loads mounts +//! (true) or goes (false): they're sampled only +//! while one wants them + +use std::{ + cell::{Cell, RefCell}, + collections::VecDeque, + rc::Rc, +}; + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use serde_json::{Value as Json, json}; +use zeron_proto::orchestration::frames::{Followed, Follower, Heard, Keyed, ListDelta, OrchestrationDelta}; +use zeron_proto::orchestration::{ + Ask, ChatActivity, CommEvent, CrewMember, DeviceLoad, Dump, OrchestrationState, Orchestrator, Route, RouteTarget, + Score, TodoProgress, WorkItem, WorkLink, +}; +use zgx_desktop::WindowContext; +use zgx_script::{ScriptHost, Shared, Value}; + +use crate::engine::{self, rpc::{Rpc, Watch}}; + +/// The most comms kept (the engine's own ring, `WatchComms`). +const COMMS: usize = 100; +/// The CPU samples kept for each device's line: six minutes at +/// `WatchDeviceLoads`' one every five seconds. +const HISTORY: usize = 72; + +/// One open window's states, and how many of its screens want loads. +struct Window { + id: u64, + now: Shared, + loads: Shared, + comms: Shared, + wants_loads: u32, +} + +#[derive(Default)] +struct Orchestration { + /// Each open window's, let go of as it closes. + windows: RefCell>, + next_window: Cell, + /// The engine watched (once, for every window). + watching: Cell, + /// The state as the engine's frames have made it (none yet: loading), + /// each row as scripts are given it, and that state as scripts were + /// given it. + heard: RefCell, + rows: RefCell, + /// Watching again after a missed frame, until its snapshot comes. + resyncing: Cell, + now: RefCell>, + comms: RefCell, + loads: RefCell, + /// Each device's CPU as it was heard, for its line (kept while the app + /// runs: a screen shown again has its last minutes at once). + history: RefCell, + /// Whether something the engine sent couldn't be read, said once. + complained: Cell, + watches: RefCell>, + /// `WatchDeviceLoads`, while a screen wants it. + loads_watch: RefCell>, +} + +thread_local! { + static ORCHESTRATION: Rc = Rc::new(Orchestration::default()); +} + +pub fn install(cx: &mut WindowContext, host: &ScriptHost) { + let o = ORCHESTRATION.with(Rc::clone); + let window = o.next_window.get(); + o.next_window.set(window + 1); + o.windows.borrow_mut().push(Window { + id: window, + now: host.shared("orchestration", o.now_value()), + loads: host.shared("orchestration.loads", o.loads_now()), + comms: host.shared("orchestration.comms", o.comms.borrow().value()), + wants_loads: 0, + }); + let me = Rc::downgrade(&o); + cx.on_closed(move || { + if let Some(o) = me.upgrade() { + o.windows.borrow_mut().retain(|w| w.id != window); + // (Its screens can't want loads any more.) + o.follow_loads(); + } + }); + if !o.watching.replace(true) { + let weak = Rc::downgrade(&o); + engine::get().on_connect(move |rpc| { + let Some(o) = weak.upgrade() else { return }; + let state = watch_state(rpc, Rc::downgrade(&o)); + let heard = Rc::downgrade(&o); + let comms = rpc.watch("WatchComms", json!({}), move |item| { + if let Some(o) = heard.upgrade() { + o.heard_comms(&item); + } + }); + *o.watches.borrow_mut() = vec![state, comms]; + // (The old connection's is gone with it.) + let old = o.loads_watch.borrow_mut().take(); + drop(old); + o.follow_loads_on(Some(rpc)); + }); + } + + host.async_action("orchestration.op", move |op| { + let mut params = crate::plugins::value_json(&op); + async move { + crate::plugins::preview::refuse("orchestration.op")?; + person_op(¶ms)?; + if params["op"] == "dump" { + carry_dump(&op, &mut params).await?; + } + let reply = engine::get().call("Orchestrate", params).await?; + Ok(if reply.is_null() { Value::Nil } else { crate::plugins::json_value(&reply) }) + } + }); + host.async_action("orchestration.answer_ask", move |args| { + let params = answer_params(&args); + async move { + crate::plugins::preview::refuse("orchestration.answer_ask")?; + let params = params?; + let reply = engine::get().call("Orchestrate", params).await?; + Ok(if reply.is_null() { Value::Nil } else { crate::plugins::json_value(&reply) }) + } + }); + host.async_action("orchestration.defer_ask", move |args| { + let params = defer_params(&args); + async move { + crate::plugins::preview::refuse("orchestration.defer_ask")?; + let params = params?; + let reply = engine::get().call("Orchestrate", params).await?; + Ok(if reply.is_null() { Value::Nil } else { crate::plugins::json_value(&reply) }) + } + }); + let a = o.clone(); + host.action("orchestration.loads_wanted", move |on| { + if crate::plugins::preview::refuse("orchestration.loads_wanted").is_err() { + return; + } + a.want_loads(window, on.truthy()) + }); +} + +/// `WatchOrchestration` as changes (`frames`: the whole state once, then +/// what moved; an engine from before them sends the whole state each time, +/// which reads as a snapshot). +fn watch_state(rpc: &Rpc, o: std::rc::Weak) -> Watch { + rpc.watch("WatchOrchestration", json!({ "changes": true }), move |item| { + if let Some(o) = o.upgrade() { + o.heard_state(item); + } + }) +} + +/// A dump's picks (`picks`: the composer's model, effort…) as `config`, and +/// its files (`attachments`, as the composer packs a message's) sent +/// on as a chat message's are (chat.rs `carry`): uploaded toward the Brain +/// dump's chat (`chat`, its id), and the op given what the engine reads, as +/// `QueueMessage` has them: `attachments` (each file's path where the run +/// reads it) and `transfers` (what it delivers first). None: the op as it +/// was, its words alone. +async fn carry_dump(op: &Value, params: &mut Json) -> Result<(), &'static str> { + params.as_object_mut().map(|o| o.remove("chat")); + // The composer's picks, made the engine's config for the Brain dump's + // chat (as a send to it sets it: chat.rs `config_with`), applied + // before the run. + params.as_object_mut().map(|o| o.remove("picks")); + if let (Some(picks), Some(chat)) = (op.field("picks"), op.field("chat").map(crate::auth::text)) { + params["config"] = crate::chat::config_with(&chat, picks); + } + if op.field("attachments").map(Value::list).unwrap_or_default().is_empty() { + params.as_object_mut().map(|o| o.remove("attachments")); + return Ok(()); + } + let target = op.field("chat").map(crate::auth::text).and_then(|id| crate::chat::target_of(&id)); + let carried = crate::chat::carry(op, target.as_deref()).await?; + params["attachments"] = json!(carried.paths); + if !carried.transfers.is_empty() { + params["transfers"] = json!(carried.transfers); + } else { + params.as_object_mut().map(|o| o.remove("transfers")); + } + Ok(()) +} + +/// `answer_ask`'s table as the `answerAsk` op: its 1-based `option` made +/// 0-based, `text` the answer's words. +fn answer_params(args: &Value) -> Result { + let id = args.field("id").map(crate::auth::text).filter(|id| !id.is_empty()).ok_or("an ask id")?; + let text = args.field("text").map(crate::auth::text).unwrap_or_default(); + let option = match args.field("option") { + Some(Value::Num(n)) if n.fract() == 0.0 && *n >= 1.0 => Some((*n as u64) - 1), + None | Some(Value::Nil) => None, + Some(_) => return Err("option is a whole number from 1".into()), + }; + if option.is_none() && text.trim().is_empty() { + return Err("an option or words".into()); + } + let mut params = json!({ "op": "answerAsk", "id": id, "answer": text.trim() }); + if let Some(option) = option { + params["option"] = json!(option); + } + Ok(params) +} + +/// `defer_ask`'s table as the `deferAsk` op: `until` (Unix seconds) as the +/// engine's time; none, back now. +fn defer_params(args: &Value) -> Result { + let id = args.field("id").map(crate::auth::text).filter(|id| !id.is_empty()).ok_or("an ask id")?; + let until = match args.field("until") { + Some(Value::Num(secs)) => { + let at = DateTime::::from_timestamp_millis((*secs * 1000.0) as i64).ok_or("until is out of range")?; + Some(at.to_rfc3339()) + } + None | Some(Value::Nil) => None, + Some(_) => return Err("until is Unix seconds".into()), + }; + let mut params = json!({ "op": "deferAsk", "id": id }); + if let Some(until) = until { + params["until"] = json!(until); + } + Ok(params) +} + +/// The ops the person sends from the window. The rest (`report`, `ask`, +/// `route`, `dumpDone`) are an agent's, through Zeron's tools, and never come +/// from here. +const PERSON_OPS: &[&str] = &[ + "createOrchestrator", + "promoteChat", + "updateOrchestrator", + "addToCrew", + "releaseFromCrew", + // (A chat put on the board: its card, made if it has none open.) + "addToBoard", + // (A card taken off the board: kept, marked removed; its chat untouched.) + "removeItem", + // (An Inbox or Next card started: its thread made.) + "startItem", + // (A card in Review: approved, Done; or changes asked for, its thread + // told and the card back in Now.) + "approveItem", + "requestChanges", + "upsertItem", + "answerAsk", + "dismissAsk", + // (An ask put off until a time, or brought back.) + "deferAsk", + "dump", + "reroute", + // (The chat's note made fresh; never sent to the chat itself.) + "progressCheck", +]; + +/// Whether `params` is one of the person's ops: else refused, unsent. +fn person_op(params: &Json) -> Result<(), String> { + match params["op"].as_str() { + Some(op) if PERSON_OPS.contains(&op) => Ok(()), + Some(_) => Err("not yours".to_string()), + None => Err("invalid".to_string()), + } +} + +impl Orchestration { + fn now_value(&self) -> Value { + self.now.borrow().clone().unwrap_or_else(loading_value) + } + + fn loads_now(&self) -> Value { + let loads = self.loads.borrow(); + if matches!(*loads, Value::Nil) { loads_value(&[]) } else { loads.clone() } + } + + /// A frame of `WatchOrchestration`: applied to the state kept, and the + /// rows it touched made again for the scripts (the rest are the same + /// values, so what reads them does nothing). A frame that doesn't follow + /// the last (one was missed) changes nothing: the state is watched again, + /// and the new watch starts whole. + fn heard_state(&self, item: Json) { + let heard = match Heard::read(&item) { + Ok(heard) => heard, + Err(error) => return self.complain("WatchOrchestration", &error), + }; + // What it changes, from the state kept (a snapshot: compared with it). + let delta = match &heard { + Heard::Delta { delta, .. } => Some(delta.clone()), + Heard::Snapshot { state, .. } => self.heard.borrow().state.as_ref().map_or_else( + || Some(OrchestrationDelta::default()), + |was| OrchestrationDelta::between(was, state), + ), + }; + let first = self.heard.borrow().state.is_none(); + if matches!(heard, Heard::Snapshot { .. }) { + self.resyncing.set(false); + } + if self.heard.borrow_mut().hear(heard) == Followed::Gap { + return self.resync(); + } + // (The same again: nothing to give.) + let Some(delta) = delta else { return }; + let value = { + let heard = self.heard.borrow(); + let Some(state) = heard.state.as_ref() else { return }; + let mut rows = self.rows.borrow_mut(); + if first { + *rows = RowValues::default(); + } + rows.forget(&delta); + rows.value(state) + }; + *self.now.borrow_mut() = Some(value.clone()); + for shared in self.targets(|w| &w.now) { + shared.set(value.clone()); + } + } + + /// Watch the state again: a frame was missed. Until the new watch's + /// snapshot comes, no delta applies; the state kept stays, for the + /// snapshot to be compared with (rows it doesn't change keep their + /// values). (Later, not inside the watch's own callback.) + fn resync(&self) { + self.heard.borrow_mut().version = None; + if self.resyncing.replace(true) { + return; + } + #[cfg(test)] + RESYNCS.with(|n| n.set(n.get() + 1)); + engine::get().spawn(async move { + let Some(rpc) = engine::get().current() else { return }; + ORCHESTRATION.with(|o| { + let state = watch_state(&rpc, Rc::downgrade(o)); + if let Some(slot) = o.watches.borrow_mut().first_mut() { + *slot = state; + } + }); + }); + } + + fn heard_comms(&self, item: &Json) { + let changed = self.comms.borrow_mut().heard(item); + match changed { + Ok(false) => {} + Ok(true) => { + let value = self.comms.borrow().value(); + for shared in self.targets(|w| &w.comms) { + shared.set(value.clone()); + } + } + Err(error) => self.complain("WatchComms", &error), + } + } + + fn heard_loads(&self, item: &Json) { + let Some(rows) = item.as_array() else { + return self.complain("WatchDeviceLoads", &"not a list"); + }; + // (A row this build can't read costs that row, not the rest.) + let loads: Vec = rows.iter().filter_map(|row| DeviceLoad::deserialize(row).ok()).collect(); + self.history.borrow_mut().heard(&loads); + let value = loads_value_with(&loads, &self.history.borrow()); + if *self.loads.borrow() == value { + return; + } + *self.loads.borrow_mut() = value.clone(); + for shared in self.targets(|w| &w.loads) { + shared.set(value.clone()); + } + } + + fn complain(&self, method: &str, error: &dyn std::fmt::Display) { + if !self.complained.replace(true) { + eprintln!("zeron-zgx: orchestration: {method} sent what this build can't read ({error}); keeping what it had"); + } + } + + /// Every window's state `pick` names, taken out first: setting one may + /// run a script that calls back in. + fn targets(&self, pick: impl Fn(&Window) -> &Shared) -> Vec { + self.windows.borrow().iter().map(|w| pick(w).clone()).collect() + } + + /// One of `window`'s screens wants loads (`on`), or no longer does. + fn want_loads(&self, window: u64, on: bool) { + if let Some(w) = self.windows.borrow_mut().iter_mut().find(|w| w.id == window) { + w.wants_loads = if on { w.wants_loads + 1 } else { w.wants_loads.saturating_sub(1) }; + } + self.follow_loads(); + } + + /// `WatchDeviceLoads` on while any screen wants it, off once none does. + fn follow_loads(&self) { + self.follow_loads_on(None); + } + + /// [`Self::follow_loads`] on `rpc`, else the connection there is now. + fn follow_loads_on(&self, rpc: Option<&Rpc>) { + let wanted = self.windows.borrow().iter().any(|w| w.wants_loads > 0); + if !wanted { + let watch = self.loads_watch.borrow_mut().take(); + drop(watch); + return; + } + if self.loads_watch.borrow().is_some() { + return; + } + // (No connection now: its `on_connect` starts it.) + let Some(rpc) = rpc.cloned().or_else(|| engine::get().current()).filter(|rpc| !rpc.is_closed()) else { return }; + let heard = Rc::downgrade(&ORCHESTRATION.with(Rc::clone)); + let watch = rpc.watch("WatchDeviceLoads", json!({}), move |item| { + if let Some(o) = heard.upgrade() { + o.heard_loads(&item); + } + }); + *self.loads_watch.borrow_mut() = Some(watch); + } +} + +/// The comms as `WatchComms` gives them, newest last, the latest [`COMMS`]. +#[derive(Default)] +struct Comms { + events: VecDeque, +} + +impl Comms { + /// `item` heard: a list of events (the stream's every frame: the + /// backlog first, then what happened since, or the whole ring again + /// after falling behind), or one event. Each is added, or put in place + /// of the one with its id; the ring stays in time order, the newest + /// [`COMMS`]. Whether anything changed. + fn heard(&mut self, item: &Json) -> Result { + let events: Vec = match item.as_array() { + // (A row this build can't read costs that row, not the rest.) + Some(rows) => rows.iter().filter_map(|row| CommEvent::deserialize(row).ok()).collect(), + None => vec![CommEvent::deserialize(item)?], + }; + let mut changed = false; + let mut out_of_order = false; + for event in events { + if let Some(held) = self.events.iter_mut().find(|e| e.id == event.id) { + if *held != event { + out_of_order |= held.at != event.at; + *held = event; + changed = true; + } + continue; + } + out_of_order |= self.events.back().is_some_and(|last| last.at > event.at); + self.events.push_back(event); + changed = true; + } + if out_of_order { + self.events.make_contiguous().sort_by_key(|e| e.at); + } + while self.events.len() > COMMS { + self.events.pop_front(); + } + Ok(changed) + } + + fn value(&self) -> Value { + Value::List(self.events.iter().map(comm_value).collect()) + } +} + +/// Each device's CPU samples as `WatchDeviceLoads` gave them, oldest first, +/// the latest [`HISTORY`]. +#[derive(Default)] +struct History { + by_device: std::collections::HashMap, f32)>>, +} + +impl History { + /// A round of loads heard: each device's CPU onto its line, once per + /// sample (a round sent again adds nothing). + fn heard(&mut self, loads: &[DeviceLoad]) { + for l in loads { + let Some(cpu) = l.cpu else { continue }; + let line = self.by_device.entry(l.device_id.clone()).or_default(); + if line.back().is_some_and(|(at, _)| *at >= l.at) { + continue; + } + line.push_back((l.at, cpu)); + while line.len() > HISTORY { + line.pop_front(); + } + } + } + + fn value(&self, device: &str) -> Value { + let line = self.by_device.get(device); + Value::List( + line.into_iter() + .flatten() + .map(|(at, cpu)| Value::record([("at", secs(at)), ("cpu", fraction(*cpu))])) + .collect(), + ) + } +} + +// ── Conversions (the wire's camelCase to the scripts' snake_case) ────────── + +/// What scripts see before the engine has said anything. +fn loading_value() -> Value { + let none = || Value::List(Vec::new().into()); + Value::record([ + ("state", Value::from("loading")), + ("orchestrators", none()), + ("crew", none()), + ("items", none()), + ("asks", none()), + ("dumps", none()), + ]) +} + +#[cfg(test)] +thread_local! { + static RESYNCS: Cell = const { Cell::new(0) }; +} + +/// One collection's rows as scripts were given them, by key, and the list +/// of them (none: made again from the rows). +struct Collection { + rows: std::collections::HashMap, + list: Option, +} + +impl Default for Collection { + fn default() -> Self { + Self { rows: Default::default(), list: None } + } +} + +impl Collection { + /// What `delta` touched made again when next asked. + fn forget(&mut self, delta: Option<&ListDelta>) { + let Some(delta) = delta else { return }; + self.list = None; + match delta.touched() { + Some(keys) => { + for key in keys { + self.rows.remove(key); + } + } + None => self.rows.clear(), + } + } + + /// `rows` as scripts see them: each row's value kept from before unless + /// it was touched. (Rows whose keys repeat are each made again: they + /// can't be told apart.) + fn value(&mut self, rows: &[T], f: impl Fn(&T) -> Value) -> Value { + if let Some(list) = &self.list { + return list.clone(); + } + let mut keys = std::collections::HashSet::with_capacity(rows.len()); + let unique = rows.iter().all(|r| keys.insert(r.key())); + if !unique { + self.rows.clear(); + let list = list(rows, f); + self.list = Some(list.clone()); + return list; + } + self.rows.retain(|k, _| keys.contains(k.as_str())); + let list = Value::List( + rows.iter() + .map(|r| self.rows.entry(r.key().to_owned()).or_insert_with(|| f(r)).clone()) + .collect(), + ); + self.list = Some(list.clone()); + list + } +} + +/// Each collection's rows as scripts were given them. +#[derive(Default)] +struct RowValues { + orchestrators: Collection, + crew: Collection, + items: Collection, + asks: Collection, + dumps: Collection, +} + +impl RowValues { + fn forget(&mut self, delta: &OrchestrationDelta) { + self.orchestrators.forget(delta.orchestrators.as_ref()); + self.crew.forget(delta.crew.as_ref()); + self.items.forget(delta.items.as_ref()); + self.asks.forget(delta.asks.as_ref()); + self.dumps.forget(delta.dumps.as_ref()); + } + + /// The whole state, as scripts see it, from the rows kept. + fn value(&mut self, s: &OrchestrationState) -> Value { + Value::record([ + ("state", Value::from("ready")), + ("orchestrators", self.orchestrators.value(&s.orchestrators, orchestrator_value)), + ("crew", self.crew.value(&s.crew, crew_value)), + ("items", self.items.value(&s.items, item_value)), + ("asks", self.asks.value(&s.asks, ask_value)), + ("dumps", self.dumps.value(&s.dumps, dump_value)), + ("settings", settings_value(s)), + ]) + } +} + +fn settings_value(s: &OrchestrationState) -> Value { + // The profile's settings: `{ crew_auto_archive, crew_idle_hours }`. + Value::record([ + ("crew_auto_archive", Value::Bool(s.settings.crew_auto_archive)), + ("crew_idle_hours", Value::from(f64::from(s.settings.crew_idle_hours))), + ]) +} + +/// The whole state, as scripts see it, made afresh. +#[cfg(test)] +fn state_value(s: &OrchestrationState) -> Value { + RowValues::default().value(s) +} + +fn orchestrator_value(o: &Orchestrator) -> Value { + let crew = Value::record([ + ("devices", strings(&o.crew.devices)), + ("max_running", opt(o.crew.max_running, |n| Value::from(f64::from(n)))), + ("harness", opt_str(o.crew.harness.as_deref())), + ("model", opt_str(o.crew.model.as_deref())), + ]); + let wake = o.wake.as_ref().map_or(Value::Nil, |w| { + Value::record([ + ("every_minutes", opt(w.every_minutes, |n| Value::from(f64::from(n)))), + ("at", strings(&w.at)), + ("weekdays_only", Value::Bool(w.weekdays_only)), + ("prompt", Value::from(w.prompt.as_str())), + ("last_woke", opt(w.last_woke_at.as_ref(), secs)), + ]) + }); + Value::record([ + ("id", Value::from(o.id.as_str())), + ("kind", word(&o.kind)), + ("name", Value::from(o.name.as_str())), + ("goal", Value::from(o.goal.as_str())), + ("rules", Value::from(o.rules.as_str())), + ("crew_rules", Value::from(o.crew_rules.as_str())), + ("notes", Value::from(o.notes.as_str())), + ("hue", Value::from(f64::from(o.hue))), + ("crew", crew), + ("wake", wake), + ("paused", Value::Bool(o.paused)), + ("archived", Value::Bool(o.archived)), + ("created", secs(&o.created_at)), + ("updated", secs(&o.updated_at)), + ]) +} + +fn crew_value(c: &CrewMember) -> Value { + let report = c.last_report.as_ref().map_or(Value::Nil, |r| { + Value::record([ + ("id", Value::from(r.id.as_str())), + ("state", word(&r.state)), + ("summary", Value::from(r.summary.as_str())), + ("at", secs(&r.at)), + ]) + }); + Value::record([ + ("chat_id", Value::from(c.chat_id.as_str())), + ("orchestrator_id", Value::from(c.orchestrator_id.as_str())), + ("item_id", opt_str(c.item_id.as_deref())), + ("role", Value::from(c.role.as_str())), + ("last_report", report), + ("released", Value::Bool(c.released)), + ("joined", secs(&c.joined_at)), + ]) +} + +fn link_value(l: &WorkLink) -> Value { + Value::record([ + ("kind", word(&l.kind)), + ("label", Value::from(l.label.as_str())), + ("target", Value::from(l.target.as_str())), + ]) +} + +fn item_value(i: &WorkItem) -> Value { + let source = i.source.as_ref().map_or(Value::Nil, |s| { + Value::record([("dump_id", Value::from(s.dump_id.as_str())), ("route_id", Value::from(s.route_id.as_str()))]) + }); + Value::record([ + ("id", Value::from(i.id.as_str())), + ("orchestrator_id", opt_str(i.orchestrator_id.as_deref())), + ("title", Value::from(i.title.as_str())), + ("detail", Value::from(i.detail.as_str())), + ("state", word(&i.state)), + ("priority", word(&i.priority)), + ("progress", opt(i.progress, fraction)), + ("threads", strings(&i.threads)), + ("links", list(&i.links, link_value)), + ("outcome", opt_str(i.outcome.as_deref())), + ("source", source), + ("order", Value::from(i.order)), + ("created", secs(&i.created_at)), + ("updated", secs(&i.updated_at)), + ("done", opt(i.done_at.as_ref(), secs)), + // (Where it starts, before it's started; why a Next card waits.) + ("space_id", opt_str(i.space_id.as_deref())), + ("device_id", opt_str(i.device_id.as_deref())), + ("waiting", opt_str(i.waiting.as_deref())), + ]) +} + +fn score_value(s: &Score) -> Value { + match s { + Score::Number(n) => Value::from(*n), + Score::Text(t) => Value::from(t.as_str()), + } +} + +fn ask_value(a: &Ask) -> Value { + let options = list(&a.options, |o| { + Value::record([ + ("label", Value::from(o.label.as_str())), + // (None when it has none, as the sample's.) + ("detail", opt_str(Some(o.detail.as_str()).filter(|d| !d.is_empty()))), + ("images", strings(&o.images)), + // (In the order written: a list, not a table keyed by name.) + ( + "scores", + list(&o.scores.0, |(name, score)| { + Value::record([("name", Value::from(name.as_str())), ("value", score_value(score))]) + }), + ), + ]) + }); + let recommendation = a.recommendation.as_ref().map(|r| { + Value::record([ + // (1-based, as Luau counts.) + ("option", opt(r.option, |i| Value::from((i + 1) as f64))), + ("label", opt_str(r.label.as_deref())), + ("why", opt_str(r.why.as_deref())), + ]) + }); + Value::record([ + ("id", Value::from(a.id.as_str())), + ("from_chat", Value::from(a.from_chat.as_str())), + ("orchestrator_id", opt_str(a.orchestrator_id.as_deref())), + ("item_id", opt_str(a.item_id.as_deref())), + ("kind", word(&a.kind)), + ("question", Value::from(a.question.as_str())), + ("options", options), + ("urgent", Value::Bool(a.urgent)), + ("state", word(&a.state)), + ("answer", opt_str(a.answer.as_deref())), + ("created", secs(&a.created_at)), + ("answered", opt(a.answered_at.as_ref(), secs)), + // A thread's question from its turn's end, not its `ask` tool. + ("origin", word(&a.origin)), + ("turn_id", opt_str(a.turn_id.as_deref())), + ("updated", opt(a.updated_at.as_ref(), secs)), + ("escalated", opt(a.escalated_at.as_ref(), secs)), + ("answered_via", opt(a.answered_via.as_ref(), word)), + // Shown without a banner, and on no card ("hasn't answered"). + ("quiet", Value::Bool(a.quiet)), + ("images", strings(&a.images)), + ("recommendation", recommendation.unwrap_or(Value::Nil)), + ("links", list(&a.links, link_value)), + ("context", opt_str(a.context.as_deref())), + // Put off until then: out of Needs you meanwhile. + ("deferred_until", opt(a.deferred_until.as_ref(), secs)), + ]) +} + +/// Where a route went: `{ kind, id? }`. +fn target_value(t: &RouteTarget) -> Value { + let (kind, id) = match t { + RouteTarget::Orchestrator { id } => ("orchestrator", Some(id)), + RouteTarget::Chat { id } => ("chat", Some(id)), + RouteTarget::NewOrchestrator { id } => ("new_orchestrator", Some(id)), + RouteTarget::NewThread { id } => ("new_thread", Some(id)), + RouteTarget::Item { id } => ("item", Some(id)), + RouteTarget::Note => ("note", None), + RouteTarget::Unsure => ("unsure", None), + RouteTarget::Other => ("other", None), + }; + // (A candidate "new" place has no id yet: none, not "".) + Value::record([("kind", Value::from(kind)), ("id", opt_str(id.map(String::as_str).filter(|id| !id.is_empty())))]) +} + +fn direct(r: &Route) -> bool { + use zeron_proto::orchestration::RouteIntent; + r.intent == RouteIntent::Question + && zeron_proto::direct_question::settle_intent(RouteIntent::Question, &r.excerpt) == RouteIntent::Question +} + +fn route_value(r: &Route) -> Value { + let span = r.span.map_or(Value::Nil, |(from, to)| { + Value::record([("from", Value::from(f64::from(from))), ("to", Value::from(f64::from(to)))]) + }); + Value::record([ + ("id", Value::from(r.id.as_str())), + ("excerpt", Value::from(r.excerpt.as_str())), + ("span", span), + ("target", target_value(&r.target)), + ("intent", word(&r.intent)), + ("message", Value::from(r.message.as_str())), + ("state", word(&r.state)), + ("candidates", list(&r.candidates, target_value)), + ("answer", opt_str(r.answer.as_deref())), + ("answer_full", opt_str(r.answer_full.as_deref())), + ("answered", opt(r.answered_at.as_ref(), secs)), + // When it was last sent to its target (a question's "Answering" is + // a turn begun since). + ("delivered", opt(r.delivered_at.as_ref(), secs)), + // A question the person wants answered (the Control room's Q&A): + // tagged so, and not work or rhetoric in their words (checked here + // too, for routes from before the engine checked). + ("direct", Value::from(direct(r))), + ("item_id", opt_str(r.item_id.as_deref())), + // The files it went with (paths where its target reads them). + ("attachments", Value::List(r.attachments.iter().map(|p| Value::from(p.as_str())).collect())), + ]) +} + +fn dump_value(d: &Dump) -> Value { + Value::record([ + ("id", Value::from(d.id.as_str())), + ("text", Value::from(d.text.as_str())), + ("state", word(&d.state)), + ("routes", list(&d.routes, route_value)), + ("summary", opt_str(d.summary.as_deref())), + ("device", Value::from(d.device_id.as_str())), + ("created", secs(&d.created_at)), + ("updated", secs(&d.updated_at)), + // The files the person dumped with it: `{ path, name }`. + ( + "attachments", + Value::List( + d.attachments + .iter() + .map(|a| Value::record([("path", Value::from(a.path.as_str())), ("name", Value::from(a.name.as_str()))])) + .collect(), + ), + ), + ]) +} + +/// The devices' loads, by device id (no history). +fn loads_value(loads: &[DeviceLoad]) -> Value { + loads_value_with(loads, &History::default()) +} + +/// The devices' loads, by device id, each with its CPU line. +fn loads_value_with(loads: &[DeviceLoad], history: &History) -> Value { + Value::record(loads.iter().map(|l| { + let bytes = |b: Option| opt(b, |b| Value::from(b as f64)); + ( + l.device_id.as_str(), + Value::record([ + ("cpu", opt(l.cpu, fraction)), + ("mem_used", bytes(l.memory_used_bytes)), + ("mem_total", bytes(l.memory_total_bytes)), + ("load1", opt(l.load1, fraction)), + ("cores", opt(l.cores, |n| Value::from(f64::from(n)))), + ("online", Value::Bool(l.online)), + ("at", secs(&l.at)), + ("history", history.value(&l.device_id)), + ]), + ) + })) +} + +fn comm_value(c: &CommEvent) -> Value { + Value::record([ + ("id", Value::from(c.id.as_str())), + ("kind", word(&c.kind)), + ("from", Value::from(c.from.as_str())), + ("to", Value::from(c.to.as_str())), + ("summary", Value::from(c.summary.as_str())), + ("at", secs(&c.at)), + ]) +} + +/// What a chat's agent is doing, for its session row: `{ kind, label, since }`. +pub(crate) fn activity_value(a: &ChatActivity) -> Value { + Value::record([("kind", word(&a.kind)), ("label", Value::from(a.label.as_str())), ("since", secs(&a.since))]) +} + +/// How far through its todo list a chat is, for its session row: +/// `{ done, total, current? }`. +pub(crate) fn todo_value(t: &TodoProgress) -> Value { + Value::record([ + ("done", Value::from(f64::from(t.done))), + ("total", Value::from(f64::from(t.total))), + ("current", opt_str(t.current.as_deref())), + ]) +} + +// ── Pieces ────────────────────────────────────────────────────────────────── + +/// An enum's wire word in snake_case (`brainDump` → `brain_dump`). +fn word(v: &T) -> Value { + match serde_json::to_value(v) { + Ok(Json::String(s)) => Value::from(snake(&s).as_str()), + _ => Value::Nil, + } +} + +fn snake(camel: &str) -> String { + let mut out = String::with_capacity(camel.len() + 4); + for c in camel.chars() { + if c.is_ascii_uppercase() { + out.push('_'); + out.push(c.to_ascii_lowercase()); + } else { + out.push(c); + } + } + out +} + +/// Unix seconds. +fn secs(t: &DateTime) -> Value { + Value::from(t.timestamp() as f64) +} + +/// A 0–1 (or load) figure, without f32's tail (0.71, not 0.7099999785). +fn fraction(x: f32) -> Value { + Value::from((f64::from(x) * 1e6).round() / 1e6) +} + +fn opt(v: Option, f: impl FnOnce(T) -> Value) -> Value { + v.map_or(Value::Nil, f) +} + +fn opt_str(v: Option<&str>) -> Value { + v.map_or(Value::Nil, Value::from) +} + +fn strings(v: &[String]) -> Value { + Value::List(v.iter().map(|s| Value::from(s.as_str())).collect()) +} + +fn list(v: &[T], f: impl Fn(&T) -> Value) -> Value { + Value::List(v.iter().map(f).collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_window_sends_only_the_person_s_ops() { + for op in PERSON_OPS { + assert!(person_op(&json!({ "op": op })).is_ok(), "{op}"); + } + for op in ["report", "ask", "route", "dumpDone", "somethingNew"] { + assert_eq!(person_op(&json!({ "op": op })), Err("not yours".to_string()), "{op}"); + } + assert_eq!(person_op(&json!({ "text": "no op" })), Err("invalid".to_string())); + // (The person starts an Inbox or Next card from the board.) + assert!(person_op(&json!({ "op": "startItem", "id": "it-1" })).is_ok()); + } + + #[test] + fn a_waiting_card_says_where_it_starts_and_why_it_waits() { + let item: WorkItem = serde_json::from_value(json!({ + "id": "x", "title": "t", "state": "planned", "priority": "normal", "createdAt": iso(NOW), "updatedAt": iso(NOW), + "spaceId": "sp-1", "deviceId": "dev-1", "waiting": "Auth's crew is full: 2 of 2 working", + })) + .unwrap(); + let v = item_value(&item); + assert_eq!(text(v.field("space_id")).as_deref(), Some("sp-1")); + assert_eq!(text(v.field("device_id")).as_deref(), Some("dev-1")); + assert_eq!(text(v.field("waiting")).as_deref(), Some("Auth's crew is full: 2 of 2 working")); + // (None of it: nil, as the sample's.) + let bare: WorkItem = serde_json::from_value(json!({ + "id": "y", "title": "t", "state": "inbox", "priority": "normal", "createdAt": iso(NOW), "updatedAt": iso(NOW), + })) + .unwrap(); + let v = item_value(&bare); + assert!(nil(v.field("space_id")) && nil(v.field("device_id")) && nil(v.field("waiting"))); + } + + #[test] + fn a_preview_can_t_drive_orchestration() { + use crate::plugins::preview; + preview::set(Some(preview::Preview { id: "sleek".into(), name: "Sleek".into() })); + assert!(preview::refuse("orchestration.op").is_err()); + assert!(preview::refuse("orchestration.loads_wanted").is_err()); + preview::set(None); + assert!(preview::refuse("orchestration.op").is_ok()); + } + + const NOW: i64 = 1_794_470_400; + + fn iso(secs: i64) -> String { + DateTime::::from_timestamp(secs, 0).unwrap().to_rfc3339() + } + + fn text(v: Option<&Value>) -> Option { + v.map(crate::auth::text) + } + + fn num(v: Option<&Value>) -> Option { + v.and_then(Value::as_f64) + } + + fn nil(v: Option<&Value>) -> bool { + v.is_none_or(|v| matches!(v, Value::Nil)) + } + + fn by_id<'a>(list: &'a Value, key: &str, id: &str) -> &'a Value { + list.list().iter().find(|v| text(v.field(key)).as_deref() == Some(id)).unwrap() + } + + /// The sample's world (ui/account/orchestration_sample.luau), a part of + /// each kind, as the engine sends it. + fn sample() -> Json { + json!({ + "orchestrators": [ + { "id": "orc-brain", "kind": "brainDump", "name": "Brain dump", "goal": "Route everything.", + "notes": "- Ask Wing", "hue": 262, "crew": {}, "createdAt": iso(NOW - 518_400), "updatedAt": iso(NOW - 40) }, + { "id": "orc-vpn", "kind": "standard", "name": "Exnomic VPN", "goal": "Phase 2 migration, safely.", + "rules": "Nothing touches prod without asking.", "crewRules": "Own worktree.", "hue": 32, + "crew": { "devices": ["dev-desk", "dev-mac"], "maxRunning": 4, "model": "claude-opus-5-5" }, + "wake": { "everyMinutes": 30, "at": ["09:00"], "weekdaysOnly": true, "prompt": "Check node health.", + "lastWokeAt": iso(NOW - 600) }, + "paused": true, "createdAt": iso(NOW - 259_200), "updatedAt": iso(NOW - 3600) }, + ], + "crew": [ + { "chatId": "th-vpn-moscow", "orchestratorId": "orc-vpn", "itemId": "it-moscow", "role": "reboot plan", + "lastReport": { "id": "rp-4", "state": "done", "summary": "Reboot plan ready", "at": iso(NOW - 3500) }, + "joinedAt": iso(NOW - 7000) }, + { "chatId": "th-vpn-merge", "orchestratorId": "orc-vpn", "role": "merge", "joinedAt": iso(NOW - 1800) }, + ], + "items": [ + { "id": "it-titlebar", "title": "Linux titlebar toggle", "state": "done", "priority": "normal", "progress": 0.71, + "threads": ["th-titlebar"], "links": [{ "kind": "pr", "label": "#1383", "target": "https://github.com/zeronsh/zeron/pull/1383" }], + "outcome": "PR #1383 open.", "source": { "dumpId": "dp-2", "routeId": "rt-6" }, "order": 2, + "createdAt": iso(NOW - 30_000), "updatedAt": iso(NOW - 26_000), "doneAt": iso(NOW - 26_000) }, + { "id": "it-live", "orchestratorId": "orc-orch", "title": "Live test", "state": "planned", "priority": "urgent", + "createdAt": iso(NOW - 5300), "updatedAt": iso(NOW - 5300) }, + ], + "asks": [ + { "id": "ak-2", "fromChat": "orc-vpn", "orchestratorId": "orc-vpn", "itemId": "it-moscow", "kind": "approval", + "question": "Reboot the Moscow node?", "options": [{ "label": "Approve" }, { "label": "Not tonight", "detail": "Tomorrow" }], + "urgent": true, "state": "open", "createdAt": iso(NOW - 3400) }, + { "id": "ak-3", "fromChat": "th-ci-cache", "kind": "question", "question": "Which bucket?", "state": "answered", + "answer": "zeron-ci-cache-us", "createdAt": iso(NOW - 95_000), "answeredAt": iso(NOW - 91_000) }, + ], + "dumps": [ + { "id": "dp-1", "text": "make the board show PR checks and look at why zgx eats 2gb", "state": "needsYou", + "summary": "1 part placed, 1 unsure.", "deviceId": "dev-mac", "createdAt": iso(NOW - 7200), "updatedAt": iso(NOW - 7100), + "routes": [ + { "id": "rt-1", "excerpt": "make the board show PR checks", "span": [0, 29], + "target": { "kind": "orchestrator", "id": "orc-orch" }, "intent": "task", "message": "Show PR checks.", + "state": "delivered", "itemId": "it-checks" }, + { "id": "rt-2", "excerpt": "look at why zgx eats 2gb", "target": { "kind": "unsure" }, "intent": "task", + "state": "pending", + "candidates": [{ "kind": "orchestrator", "id": "orc-orch" }, { "kind": "newThread", "id": "" }, + { "kind": "newOrchestrator", "id": "orc-new" }] }, + ] }, + ], + }) + } + + #[test] + fn the_state_reads_as_the_sample_does() { + let state = OrchestrationState::deserialize(&sample()).unwrap(); + let v = state_value(&state); + assert_eq!(text(v.field("state")).as_deref(), Some("ready")); + + let orchestrators = v.field("orchestrators").unwrap(); + let brain = by_id(orchestrators, "id", "orc-brain"); + assert_eq!(text(brain.field("kind")).as_deref(), Some("brain_dump")); + assert_eq!(text(brain.field("rules")).as_deref(), Some("")); + assert!(nil(brain.field("wake"))); + assert!(nil(brain.field("crew").unwrap().field("max_running"))); + assert!(brain.field("crew").unwrap().field("devices").unwrap().list().is_empty()); + let vpn = by_id(orchestrators, "id", "orc-vpn"); + assert_eq!(text(vpn.field("kind")).as_deref(), Some("standard")); + assert_eq!(text(vpn.field("crew_rules")).as_deref(), Some("Own worktree.")); + assert_eq!(num(vpn.field("hue")), Some(32.)); + assert_eq!(vpn.field("paused"), Some(&Value::Bool(true))); + assert_eq!(vpn.field("archived"), Some(&Value::Bool(false))); + assert_eq!(num(vpn.field("created")), Some((NOW - 259_200) as f64)); + assert_eq!(num(vpn.field("updated")), Some((NOW - 3600) as f64)); + let crew = vpn.field("crew").unwrap(); + assert_eq!(num(crew.field("max_running")), Some(4.)); + assert_eq!(crew.field("devices").unwrap().list().len(), 2); + assert_eq!(text(crew.field("model")).as_deref(), Some("claude-opus-5-5")); + assert!(nil(crew.field("harness"))); + let wake = vpn.field("wake").unwrap(); + assert_eq!(num(wake.field("every_minutes")), Some(30.)); + assert_eq!(wake.field("weekdays_only"), Some(&Value::Bool(true))); + assert_eq!(text(wake.field("at").unwrap().list().first()).as_deref(), Some("09:00")); + assert_eq!(num(wake.field("last_woke")), Some((NOW - 600) as f64)); + + let crew = v.field("crew").unwrap(); + let moscow = by_id(crew, "chat_id", "th-vpn-moscow"); + assert_eq!(text(moscow.field("orchestrator_id")).as_deref(), Some("orc-vpn")); + assert_eq!(text(moscow.field("item_id")).as_deref(), Some("it-moscow")); + assert_eq!(num(moscow.field("joined")), Some((NOW - 7000) as f64)); + assert_eq!(moscow.field("released"), Some(&Value::Bool(false))); + let report = moscow.field("last_report").unwrap(); + assert_eq!(text(report.field("state")).as_deref(), Some("done")); + assert_eq!(text(report.field("summary")).as_deref(), Some("Reboot plan ready")); + assert_eq!(num(report.field("at")), Some((NOW - 3500) as f64)); + let merge = by_id(crew, "chat_id", "th-vpn-merge"); + assert!(nil(merge.field("last_report"))); + assert!(nil(merge.field("item_id"))); + + let items = v.field("items").unwrap(); + let titlebar = by_id(items, "id", "it-titlebar"); + assert!(nil(titlebar.field("orchestrator_id"))); + assert_eq!(text(titlebar.field("state")).as_deref(), Some("done")); + assert_eq!(text(titlebar.field("priority")).as_deref(), Some("normal")); + // (f32's 0.71 reads as 0.71, not 0.7099999785.) + assert_eq!(num(titlebar.field("progress")), Some(0.71)); + assert_eq!(num(titlebar.field("order")), Some(2.)); + assert_eq!(num(titlebar.field("done")), Some((NOW - 26_000) as f64)); + assert_eq!(text(titlebar.field("detail")).as_deref(), Some("")); + let source = titlebar.field("source").unwrap(); + assert_eq!(text(source.field("dump_id")).as_deref(), Some("dp-2")); + assert_eq!(text(source.field("route_id")).as_deref(), Some("rt-6")); + let link = &titlebar.field("links").unwrap().list()[0]; + assert_eq!(text(link.field("kind")).as_deref(), Some("pr")); + assert_eq!(text(link.field("label")).as_deref(), Some("#1383")); + assert_eq!(text(titlebar.field("threads").unwrap().list().first()).as_deref(), Some("th-titlebar")); + let live = by_id(items, "id", "it-live"); + assert_eq!(text(live.field("priority")).as_deref(), Some("urgent")); + for key in ["progress", "outcome", "source", "done"] { + assert!(nil(live.field(key)), "{key}"); + } + assert!(live.field("links").unwrap().list().is_empty()); + assert!(live.field("threads").unwrap().list().is_empty()); + + let asks = v.field("asks").unwrap(); + let approval = by_id(asks, "id", "ak-2"); + assert_eq!(text(approval.field("kind")).as_deref(), Some("approval")); + assert_eq!(text(approval.field("from_chat")).as_deref(), Some("orc-vpn")); + assert_eq!(approval.field("urgent"), Some(&Value::Bool(true))); + assert_eq!(text(approval.field("state")).as_deref(), Some("open")); + assert!(nil(approval.field("answer"))); + assert!(nil(approval.field("answered"))); + let options = approval.field("options").unwrap().list(); + assert_eq!(text(options[0].field("label")).as_deref(), Some("Approve")); + assert!(nil(options[0].field("detail"))); + assert_eq!(text(options[1].field("detail")).as_deref(), Some("Tomorrow")); + let answered = by_id(asks, "id", "ak-3"); + assert_eq!(text(answered.field("answer")).as_deref(), Some("zeron-ci-cache-us")); + assert_eq!(num(answered.field("answered")), Some((NOW - 91_000) as f64)); + assert!(answered.field("options").unwrap().list().is_empty()); + assert!(nil(answered.field("orchestrator_id"))); + + let dump = &v.field("dumps").unwrap().list()[0]; + assert_eq!(text(dump.field("state")).as_deref(), Some("needs_you")); + assert_eq!(text(dump.field("device")).as_deref(), Some("dev-mac")); + assert_eq!(num(dump.field("created")), Some((NOW - 7200) as f64)); + assert_eq!(text(dump.field("summary")).as_deref(), Some("1 part placed, 1 unsure.")); + let routes = dump.field("routes").unwrap(); + let placed = by_id(routes, "id", "rt-1"); + let span = placed.field("span").unwrap(); + assert_eq!((num(span.field("from")), num(span.field("to"))), (Some(0.), Some(29.))); + assert_eq!(text(placed.field("target").unwrap().field("kind")).as_deref(), Some("orchestrator")); + assert_eq!(text(placed.field("target").unwrap().field("id")).as_deref(), Some("orc-orch")); + assert_eq!(text(placed.field("intent")).as_deref(), Some("task")); + assert_eq!(text(placed.field("state")).as_deref(), Some("delivered")); + assert_eq!(text(placed.field("item_id")).as_deref(), Some("it-checks")); + assert!(nil(placed.field("answer"))); + let unsure = by_id(routes, "id", "rt-2"); + assert!(nil(unsure.field("span"))); + assert_eq!(text(unsure.field("target").unwrap().field("kind")).as_deref(), Some("unsure")); + assert!(nil(unsure.field("target").unwrap().field("id"))); + assert_eq!(text(unsure.field("message")).as_deref(), Some("")); + assert_eq!(text(unsure.field("state")).as_deref(), Some("pending")); + let candidates = unsure.field("candidates").unwrap().list(); + let kinds: Vec<_> = candidates.iter().map(|c| text(c.field("kind")).unwrap()).collect(); + assert_eq!(kinds, ["orchestrator", "new_thread", "new_orchestrator"]); + // (A new place's empty id is none.) + assert!(nil(candidates[1].field("id"))); + assert_eq!(text(candidates[2].field("id")).as_deref(), Some("orc-new")); + } + + #[test] + fn words_are_snake_case() { + assert_eq!(snake("brainDump"), "brain_dump"); + assert_eq!(snake("needsYou"), "needs_you"); + assert_eq!(snake("newOrchestrator"), "new_orchestrator"); + assert_eq!(snake("other"), "other"); + // An unknown word from a newer peer reads as "other". + let item: WorkItem = serde_json::from_value(json!({ + "id": "x", "title": "t", "state": "someday", "priority": "low", "createdAt": iso(NOW), "updatedAt": iso(NOW), + })) + .unwrap(); + assert_eq!(text(item_value(&item).field("state")).as_deref(), Some("other")); + } + + #[test] + fn a_turn_end_ask_reads_with_its_origin_and_how_it_closed() { + let ask: Ask = serde_json::from_value(json!({ + "id": "turn-c-m", "fromChat": "c", "kind": "decision", "question": "Should I A, or B?", "state": "answered", + "createdAt": iso(NOW - 3600), "origin": "turnEnd", "turnId": "m", "escalatedAt": iso(NOW - 1800), + "answeredAt": iso(NOW), "answeredVia": "thread", + })) + .unwrap(); + let v = ask_value(&ask); + assert_eq!(text(v.field("origin")).as_deref(), Some("turn_end")); + assert_eq!(text(v.field("turn_id")).as_deref(), Some("m")); + assert_eq!(num(v.field("escalated")), Some((NOW - 1800) as f64)); + assert_eq!(text(v.field("answered_via")).as_deref(), Some("thread")); + assert!(nil(v.field("updated"))); + // One the `ask` tool raised: "tool", nothing else. + let tool: Ask = serde_json::from_value(json!({ + "id": "a", "fromChat": "c", "kind": "question", "question": "?", "state": "open", "createdAt": iso(NOW), + })) + .unwrap(); + let v = ask_value(&tool); + assert_eq!(text(v.field("origin")).as_deref(), Some("tool")); + assert!(nil(v.field("escalated")) && nil(v.field("answered_via"))); + assert!(matches!(v.field("quiet"), Some(Value::Bool(false)))); + } + + #[test] + fn a_decision_reads_with_its_pictures_scores_pick_and_deferral() { + let ask: Ask = serde_json::from_value(json!({ + "id": "a", "fromChat": "c", "kind": "decision", "question": "Which?", "state": "open", "createdAt": iso(NOW), + "options": [ + { "label": "(a) Tag", "detail": "No lines", "images": ["/u/1-tag.png"], "scores": { "Claude": 8, "risk": "low" } }, + { "label": "(b) Edge" } + ], + "images": ["https://x/now.png"], + "recommendation": { "option": 0, "label": "(a) Tag", "why": "Cleaner." }, + "links": [{ "kind": "branch", "label": "orch/x", "target": "orch/x" }], + "context": "Built on `c1e`.", "deferredUntil": iso(NOW + 3600), + })) + .unwrap(); + let v = ask_value(&ask); + let options = v.field("options").unwrap().list(); + let a = &options[0]; + assert_eq!(text(a.field("images").unwrap().list().first()).as_deref(), Some("/u/1-tag.png")); + let scores = a.field("scores").unwrap().list(); + assert_eq!(text(scores[0].field("name")).as_deref(), Some("Claude")); + assert_eq!(num(scores[0].field("value")), Some(8.0)); + assert_eq!(text(scores[1].field("value")).as_deref(), Some("low")); + assert!(options[1].field("images").unwrap().list().is_empty() && nil(options[1].field("detail"))); + assert_eq!(text(v.field("images").unwrap().list().first()).as_deref(), Some("https://x/now.png")); + let pick = v.field("recommendation").unwrap(); + assert_eq!(num(pick.field("option")), Some(1.0), "1-based"); + assert_eq!(text(pick.field("why")).as_deref(), Some("Cleaner.")); + assert_eq!(text(v.field("links").unwrap().list()[0].field("target")).as_deref(), Some("orch/x")); + assert_eq!(text(v.field("context")).as_deref(), Some("Built on `c1e`.")); + assert_eq!(num(v.field("deferred_until")), Some((NOW + 3600) as f64)); + // An old ask: empty lists, nils. + let old: Ask = serde_json::from_value(json!({ + "id": "b", "fromChat": "c", "kind": "question", "question": "?", "state": "open", "createdAt": iso(NOW), + })) + .unwrap(); + let v = ask_value(&old); + assert!(v.field("images").unwrap().list().is_empty() && v.field("links").unwrap().list().is_empty()); + assert!(nil(v.field("recommendation")) && nil(v.field("context")) && nil(v.field("deferred_until"))); + } + + #[test] + fn answering_with_an_option_and_deferring_make_the_engines_ops() { + let args = Value::record([("id", Value::from("a")), ("option", Value::from(2.0))]); + assert_eq!(answer_params(&args).unwrap(), json!({ "op": "answerAsk", "id": "a", "answer": "", "option": 1 })); + let args = Value::record([("id", Value::from("a")), ("option", Value::from(1.0)), ("text", Value::from(" and polish "))]); + assert_eq!(answer_params(&args).unwrap(), json!({ "op": "answerAsk", "id": "a", "answer": "and polish", "option": 0 })); + let args = Value::record([("id", Value::from("a")), ("text", Value::from("go"))]); + assert_eq!(answer_params(&args).unwrap(), json!({ "op": "answerAsk", "id": "a", "answer": "go" })); + assert!(answer_params(&Value::record([("id", Value::from("a"))])).is_err(), "an option or words"); + assert!(answer_params(&Value::record([("id", Value::from("a")), ("option", Value::from(0.0))])).is_err(), "1-based"); + assert!(answer_params(&Value::record([("option", Value::from(1.0))])).is_err(), "an id"); + let args = Value::record([("id", Value::from("a")), ("until", Value::from(NOW as f64))]); + let op = defer_params(&args).unwrap(); + assert_eq!(op["op"], "deferAsk"); + let until: DateTime = op["until"].as_str().unwrap().parse().unwrap(); + assert_eq!(until.timestamp(), NOW); + assert_eq!(defer_params(&Value::record([("id", Value::from("a"))])).unwrap(), json!({ "op": "deferAsk", "id": "a" })); + // What they make, the engine reads. + for op in [answer_params(&Value::record([("id", Value::from("a")), ("option", Value::from(1.0))])).unwrap(), op] { + assert!(serde_json::from_value::(op.clone()).is_ok(), "{op}"); + assert!(person_op(&op).is_ok()); + } + } + + #[test] + fn activity_and_todo_read_as_session_rows_carry_them() { + let a: ChatActivity = + serde_json::from_value(json!({ "kind": "testing", "label": "cargo test", "since": iso(NOW - 48) })).unwrap(); + let v = activity_value(&a); + assert_eq!(text(v.field("kind")).as_deref(), Some("testing")); + assert_eq!(text(v.field("label")).as_deref(), Some("cargo test")); + assert_eq!(num(v.field("since")), Some((NOW - 48) as f64)); + let t = todo_value(&TodoProgress { done: 3, total: 3, current: None }); + assert_eq!((num(t.field("done")), num(t.field("total"))), (Some(3.), Some(3.))); + assert!(nil(t.field("current"))); + } + + #[test] + fn loads_are_keyed_by_device() { + let loads: Vec = serde_json::from_value(json!([ + { "deviceId": "dev-desk", "cpu": 0.71, "memoryUsedBytes": 41_200_000_000u64, "memoryTotalBytes": 62_000_000_000u64, + "load1": 11.4, "cores": 16, "online": true, "at": iso(NOW) }, + { "deviceId": "dev-cloud", "online": false, "at": iso(NOW - 30) }, + ])) + .unwrap(); + let v = loads_value(&loads); + let desk = v.field("dev-desk").unwrap(); + assert_eq!(num(desk.field("cpu")), Some(0.71)); + assert_eq!(num(desk.field("mem_used")), Some(41.2e9)); + assert_eq!(num(desk.field("mem_total")), Some(62.0e9)); + assert_eq!(num(desk.field("load1")), Some(11.4)); + assert_eq!(num(desk.field("cores")), Some(16.)); + assert_eq!(desk.field("online"), Some(&Value::Bool(true))); + assert_eq!(num(desk.field("at")), Some(NOW as f64)); + let cloud = v.field("dev-cloud").unwrap(); + for key in ["cpu", "mem_used", "mem_total", "load1", "cores"] { + assert!(nil(cloud.field(key)), "{key}"); + } + assert_eq!(cloud.field("online"), Some(&Value::Bool(false))); + assert!(loads_value(&[]).field("dev-desk").is_none()); + } + + fn comm(id: &str, at: i64) -> Json { + json!({ "id": id, "kind": "route", "from": "orc-brain", "to": "you", "summary": id, "at": iso(at) }) + } + + #[test] + fn comms_append_replace_and_keep_the_latest_hundred() { + let mut comms = Comms::default(); + // On subscribe, the ring whole (in any order): newest last. + assert!(comms.heard(&json!([comm("b", NOW - 10), comm("a", NOW - 20)])).unwrap()); + let ids = |c: &Comms| c.events.iter().map(|e| e.id.clone()).collect::>(); + assert_eq!(ids(&comms), ["a", "b"]); + // The same again: nothing new. + assert!(!comms.heard(&json!([comm("a", NOW - 20), comm("b", NOW - 10)])).unwrap()); + // One more: at the end. + assert!(comms.heard(&comm("c", NOW)).unwrap()); + assert_eq!(ids(&comms), ["a", "b", "c"]); + // One heard again: in its place, not twice. + assert!(!comms.heard(&comm("c", NOW)).unwrap()); + assert_eq!(comms.events.len(), 3); + // Never more than 100: the oldest go. + for i in 0..150 { + comms.heard(&comm(&format!("n{i}"), NOW + i)).unwrap(); + } + assert_eq!(comms.events.len(), COMMS); + assert_eq!(comms.events.front().unwrap().id, "n50"); + assert_eq!(comms.events.back().unwrap().id, "n149"); + // A whole ring again replaces it, cut to the newest 100. + let ring: Vec = (0..120).map(|i| comm(&format!("r{i}"), NOW + 1000 + i)).collect(); + assert!(comms.heard(&Json::Array(ring)).unwrap()); + assert_eq!(comms.events.len(), COMMS); + assert_eq!(comms.events.front().unwrap().id, "r20"); + // What scripts see: newest last, snake_case, seconds. + let v = comms.value(); + let last = v.list().last().unwrap(); + assert_eq!(text(last.field("id")).as_deref(), Some("r119")); + assert_eq!(text(last.field("kind")).as_deref(), Some("route")); + assert_eq!(num(last.field("at")), Some((NOW + 1119) as f64)); + // Something unreadable: told, and the ring stands. + assert!(comms.heard(&json!({ "id": "x" })).is_err()); + assert_eq!(comms.events.len(), COMMS); + } + + #[test] + fn a_dump_and_its_routes_carry_their_files_and_a_route_can_be_done() { + let d: Dump = serde_json::from_value(json!({ + "id": "dp", "text": "look", "state": "routed", "deviceId": "dev", "createdAt": iso(NOW), "updatedAt": iso(NOW), + "attachments": [{ "path": "/tmp/a.png", "name": "a.png" }], + "routes": [{ "id": "rt", "excerpt": "look", "target": { "kind": "inbox" }, "intent": "task", "message": "m", + "state": "done", "answer": "Fixed in `abc`.", "answerFull": "Fixed.\n\nAll green.", "attachments": ["/srv/a.png"] }], + })) + .unwrap(); + let v = dump_value(&d); + let file = &v.field("attachments").unwrap().list()[0]; + assert_eq!(text(file.field("name")).as_deref(), Some("a.png")); + let route = &v.field("routes").unwrap().list()[0]; + assert_eq!(text(route.field("state")).as_deref(), Some("done")); + assert_eq!(text(route.field("answer_full")).as_deref(), Some("Fixed.\n\nAll green.")); + assert_eq!(text(Some(&route.field("attachments").unwrap().list()[0])).as_deref(), Some("/srv/a.png")); + } + + #[test] + fn a_dumps_picks_become_the_brain_dumps_config() { + let picks = Value::record([("model", Value::from("claude-code/claude-opus-5-5")), ("effort", Value::from("high"))]); + let config = crate::chat::config_with("orc-unknown", &picks); + assert_eq!(config["harness"], "claude-code"); + assert_eq!(config["model"], "claude-opus-5-5"); + assert_eq!(config["reasoning"], "high"); + } + + #[test] + fn each_comms_frame_adds_to_what_came_before() { + // The stream's frames: the backlog, then each burst since (also a + // list). A later frame must not stand for the whole feed. + let mut comms = Comms::default(); + let ids = |c: &Comms| c.events.iter().map(|e| e.id.clone()).collect::>(); + assert!(comms.heard(&json!([comm("a", NOW - 30), comm("b", NOW - 20)])).unwrap()); + assert!(comms.heard(&json!([comm("c", NOW - 10)])).unwrap()); + assert!(comms.heard(&json!([comm("d", NOW - 5), comm("e", NOW)])).unwrap()); + assert_eq!(ids(&comms), ["a", "b", "c", "d", "e"]); + // Fallen behind: the whole ring again, overlapping what's held. + assert!(comms.heard(&json!([comm("c", NOW - 10), comm("d", NOW - 5), comm("e", NOW), comm("f", NOW + 1)])).unwrap()); + assert_eq!(ids(&comms), ["a", "b", "c", "d", "e", "f"]); + // One that came late goes in its place in time. + assert!(comms.heard(&json!([comm("z", NOW - 25)])).unwrap()); + assert_eq!(ids(&comms), ["a", "z", "b", "c", "d", "e", "f"]); + } + + #[test] + fn each_device_keeps_its_cpu_line() { + let round = |at: i64, cpu: f32| -> Vec { + serde_json::from_value(json!([ + { "deviceId": "dev-desk", "cpu": cpu, "online": true, "at": iso(at) }, + { "deviceId": "dev-cloud", "online": false, "at": iso(at) }, + ])) + .unwrap() + }; + let mut history = History::default(); + history.heard(&round(NOW - 10, 0.2)); + history.heard(&round(NOW - 5, 0.5)); + // The same round again adds nothing. + history.heard(&round(NOW - 5, 0.5)); + let v = loads_value_with(&round(NOW - 5, 0.5), &history); + let line = v.field("dev-desk").unwrap().field("history").unwrap().list(); + assert_eq!(line.len(), 2); + assert_eq!(num(line[0].field("at")), Some((NOW - 10) as f64)); + assert_eq!(num(line[1].field("cpu")), Some(0.5)); + // No CPU, no line. + assert!(v.field("dev-cloud").unwrap().field("history").unwrap().list().is_empty()); + // At most six minutes of it. + for i in 0..200 { + history.heard(&round(NOW + i * 5, 0.1)); + } + assert_eq!(history.by_device["dev-desk"].len(), HISTORY); + } + + fn same(a: &Value, b: &Value) -> bool { + match (a, b) { + (Value::Map(a), Value::Map(b)) => Rc::ptr_eq(a, b), + (Value::List(a), Value::List(b)) => Rc::ptr_eq(a, b), + _ => false, + } + } + + fn row<'a>(v: &'a Value, list: &str, key: &str, id: &str) -> &'a Value { + v.field(list).unwrap().list().iter().find(|r| text(r.field(key)).as_deref() == Some(id)).unwrap() + } + + /// As the engine's watch on changes sends them: a snapshot, then deltas. + fn frames(states: &[OrchestrationState]) -> Vec { + let mut framer = zeron_proto::orchestration::frames::Framer::new(); + states.iter().filter_map(|s| framer.frame(s)).map(|f| serde_json::to_value(f).unwrap()).collect() + } + + /// A delta that moves one card makes that card's row again and nothing + /// else: every other row, and every other list, is the value scripts + /// already have (the same Luau table, so their selectors don't run). + #[test] + fn a_delta_keeps_every_row_it_does_not_touch() { + let s1 = OrchestrationState::deserialize(&sample()).unwrap(); + let mut s2 = s1.clone(); + let live = s2.items.iter_mut().find(|i| i.id == "it-live").unwrap(); + live.state = zeron_proto::orchestration::WorkState::Active; + let wire = frames(&[s1, s2.clone()]); + assert_eq!(wire.len(), 2); + assert_eq!(wire[1]["frame"], "delta"); + assert!(wire[1].get("asks").is_none(), "only what changed goes"); + let o = Orchestration::default(); + o.heard_state(wire[0].clone()); + let before = o.now_value(); + o.heard_state(wire[1].clone()); + let after = o.now_value(); + assert_eq!(after, state_value(&s2), "the delta gives the state the engine has"); + assert_eq!(text(row(&after, "items", "id", "it-live").field("state")).as_deref(), Some("active")); + assert!(!same(row(&before, "items", "id", "it-live"), row(&after, "items", "id", "it-live"))); + assert!(same(row(&before, "items", "id", "it-titlebar"), row(&after, "items", "id", "it-titlebar"))); + for list in ["orchestrators", "crew", "asks", "dumps"] { + assert!(same(before.field(list).unwrap(), after.field(list).unwrap()), "{list} kept whole"); + } + } + + /// What scripts see of a pushed change: a card's selector (as the + /// Board's cards read theirs) runs again only for the card that moved, + /// and every other card is the very Luau table it was. + #[test] + fn a_pushed_delta_runs_only_the_moved_card_s_selector() { + use zgx_script::testing::TestApp; + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/orch_push_fixture.luau"); + let app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); + app.frames(5); + let shared = app.host.shared("orchestration", loading_value()); + let o = Orchestration::default(); + let push = |item: Json| { + o.heard_state(item); + shared.set(o.now_value()); + app.settle(); + app.frames(3); + }; + let state = |name: &str| app.host.states().into_iter().find(|(n, _)| n == name).map(|(_, v)| v).unwrap(); + let count = |id: &str| num(state("runs").field(id)).unwrap_or(0.); + let kept = |id: &str| matches!(state("kept").field(id), Some(Value::Bool(true))); + + let s1 = OrchestrationState::deserialize(&sample()).unwrap(); + let mut s2 = s1.clone(); + s2.items.iter_mut().find(|i| i.id == "it-live").unwrap().state = zeron_proto::orchestration::WorkState::Active; + let mut s3 = s2.clone(); + s3.asks[0].question = "Something else?".into(); + let mut s4 = s3.clone(); + let mut fresh = s4.items[1].clone(); + fresh.id = "it-new".into(); + s4.items.push(fresh); + let wire = frames(&[s1, s2, s3, s4]); + assert_eq!(wire.len(), 4); + + push(wire[0].clone()); + let (titlebar, live, new) = (count("it-titlebar"), count("it-live"), count("it-new")); + // A card moved: its selector alone runs again. + push(wire[1].clone()); + assert_eq!(count("it-live"), live + 1.); + assert_eq!(count("it-titlebar"), titlebar, "an unchanged card's selector didn't run"); + assert_eq!(count("it-new"), new); + assert!(kept("it-titlebar"), "an unchanged card is the same table"); + assert!(!kept("it-live")); + // An ask changed: no card's selector runs, every card is kept. + push(wire[2].clone()); + assert_eq!((count("it-titlebar"), count("it-live")), (titlebar, live + 1.)); + assert!(kept("it-titlebar") && kept("it-live")); + // A card added: its own selector runs, the others don't. + push(wire[3].clone()); + assert_eq!(count("it-new"), new + 1.); + assert_eq!((count("it-titlebar"), count("it-live")), (titlebar, live + 1.)); + assert!(kept("it-titlebar") && kept("it-live")); + } + + /// A row added, one removed and the order changed, by a delta. + #[test] + fn a_delta_adds_removes_and_reorders_rows() { + let s1 = OrchestrationState::deserialize(&sample()).unwrap(); + let mut s2 = s1.clone(); + let gone = s2.crew.remove(0); + let mut newcomer = gone.clone(); + newcomer.chat_id = "th-new".into(); + s2.crew.insert(0, newcomer); + s2.items.reverse(); + let wire = frames(&[s1, s2.clone()]); + let o = Orchestration::default(); + o.heard_state(wire[0].clone()); + let before = o.now_value(); + o.heard_state(wire[1].clone()); + let after = o.now_value(); + assert_eq!(after, state_value(&s2)); + // Reordered rows are the same rows. + assert!(same(row(&before, "items", "id", "it-live"), row(&after, "items", "id", "it-live"))); + } + + /// A missed frame: nothing applied, and the state is watched again. + #[test] + fn a_missed_frame_changes_nothing_and_watches_again() { + let s1 = OrchestrationState::deserialize(&sample()).unwrap(); + let mut s2 = s1.clone(); + s2.items[0].title = "renamed".into(); + let mut s3 = s2.clone(); + s3.items[0].title = "renamed again".into(); + let wire = frames(&[s1, s2, s3]); + let o = Orchestration::default(); + o.heard_state(wire[0].clone()); + let before = o.now_value(); + let resyncs = RESYNCS.with(Cell::get); + o.heard_state(wire[2].clone()); + assert_eq!(RESYNCS.with(Cell::get), resyncs + 1); + assert!(same(&before, &o.now_value())); + // More of the old watch's deltas: nothing applied, no second watch. + o.heard_state(wire[1].clone()); + assert_eq!(RESYNCS.with(Cell::get), resyncs + 1); + assert!(same(&before, &o.now_value())); + // The new watch's snapshot: the state as it is, untouched rows kept. + let s3 = OrchestrationState::deserialize(&sample()).unwrap(); + let mut s3 = s3; + s3.items[0].title = "renamed again".into(); + let again = frames(std::slice::from_ref(&s3)); + o.heard_state(again[0].clone()); + let after = o.now_value(); + assert_eq!(after, state_value(&s3)); + assert!(same(before.field("asks").unwrap(), after.field("asks").unwrap())); + let id = s3.items[1].id.clone(); + assert!(same(row(&before, "items", "id", &id), row(&after, "items", "id", &id))); + // And deltas apply again after it. + let mut s4 = s3.clone(); + s4.items[1].title = "fourth".into(); + let mut framer = zeron_proto::orchestration::frames::Framer::new(); + framer.frame(&s3); + o.heard_state(serde_json::to_value(framer.frame(&s4).unwrap()).unwrap()); + assert_eq!(o.now_value(), state_value(&s4)); + } + + /// An engine from before frames sends the whole state each time: each + /// is taken as it comes, and rows that didn't change keep their values. + #[test] + fn an_older_engine_s_whole_states_keep_unchanged_rows() { + let s1 = OrchestrationState::deserialize(&sample()).unwrap(); + let mut s2 = s1.clone(); + s2.asks[0].question = "Another question?".into(); + let o = Orchestration::default(); + o.heard_state(serde_json::to_value(&s1).unwrap()); + let before = o.now_value(); + o.heard_state(serde_json::to_value(&s2).unwrap()); + let after = o.now_value(); + assert_eq!(after, state_value(&s2)); + assert!(same(before.field("items").unwrap(), after.field("items").unwrap())); + assert!(!same(before.field("asks").unwrap(), after.field("asks").unwrap())); + let id = s2.asks[1].id.clone(); + assert!(same(row(&before, "asks", "id", &id), row(&after, "asks", "id", &id))); + } + + #[test] + fn a_state_heard_again_is_not_published_and_a_bad_one_keeps_the_last() { + let o = Orchestration::default(); + assert_eq!(text(o.now_value().field("state")).as_deref(), Some("loading")); + o.heard_state(sample()); + let first = o.now_value(); + assert_eq!(text(first.field("state")).as_deref(), Some("ready")); + o.heard_state(sample()); + let again = o.now_value(); + assert!(matches!((&first, &again), (Value::Map(a), Value::Map(b)) if Rc::ptr_eq(a, b))); + o.heard_state(json!({ "orchestrators": "nope" })); + assert_eq!(o.now_value(), first); + } +} diff --git a/apps/zgx/src/orchestration_board_tests.rs b/apps/zgx/src/orchestration_board_tests.rs new file mode 100644 index 000000000..75579af09 --- /dev/null +++ b/apps/zgx/src/orchestration_board_tests.rs @@ -0,0 +1,320 @@ +//! The orchestration board's drag, as a hand does it (ui/orch_board_fixture.luau, +//! ds/orch_board): pressed, moved and let go in raw pointer events. A fast +//! flick (one move far past the card, then let go) moves it as a slow drag +//! does; a drag begun on a card's outcome is still the card's; Escape +//! cancels; a press let go where it was opens the card's thread. + +use zgx::input::{InputEvent, Key, Modifiers, PointerButton}; +use zgx::scene::Rect; +use zgx_script::{ScriptHost, Value, testing::TestApp}; + +fn mount(w: f32, h: f32) -> TestApp { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/orch_board_fixture.luau"); + let app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); + app.ui.scene.borrow_mut().resize(w, h); + app.frames(30); + app +} + +fn calls(app: &TestApp) -> String { + app.host + .states() + .into_iter() + .find(|(n, _)| n == "calls") + .map(|(_, v)| match v { + Value::Str(s) => s.to_string(), + other => format!("{other:?}"), + }) + .unwrap_or_default() +} + +fn rect(app: &mut TestApp, target: &str) -> Rect { + let id = app.target(target).unwrap_or_else(|e| panic!("{target}: {e}")); + app.ui.prepare_frame(); + app.ui.scene.borrow().hit_bounds(id) +} + +fn card(app: &mut TestApp, col: &str, title: &str) -> Rect { + rect(app, &format!("#orch-col-{col} >> role:button \"{title}\"")) +} + +fn has(app: &mut TestApp, target: &str) -> bool { + app.find(target).is_some() +} + +fn send(app: &mut TestApp, event: InputEvent) { + app.ui.dispatch_with_modifiers(event, Modifiers::default()); +} + +const P: PointerButton = PointerButton::Primary; + +/// Pressed at `from`, moved through `path` (a frame after each move when +/// `frames`), let go at the last point. +fn gesture(app: &mut TestApp, from: (f32, f32), path: &[(f32, f32)], frames: bool) { + let (x, y) = from; + send(app, InputEvent::PointerMove { x, y }); + send(app, InputEvent::PointerDown { x, y, button: P }); + for &(x, y) in path { + send(app, InputEvent::PointerMove { x, y }); + if frames { + app.frames(1); + } + } + let (x, y) = *path.last().unwrap_or(&from); + send(app, InputEvent::PointerUp { x, y, button: P }); + app.ui.input.forget_clicks(); + app.settle(); + app.frames(20); +} + +const LIVE: &str = "Live end-to-end test on a dev engine"; + +/// A flick: one move from the press far past the card (it leaves the card +/// at once), then let go there, no frame between. +#[test] +fn orchestration_board_fast_flick_moves_the_card() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let to = rect(&mut app, "#orch-col-blocked"); + let start = (from.x + 30., from.y + from.height / 2.); + let end = (to.x + to.width / 2., to.y + to.height - 8.); + gesture(&mut app, start, &[end], false); + let said = calls(&app); + assert!(said.contains("move:it-live:blocked:"), "a flick moves it: {said:?}"); + assert!(has(&mut app, &format!("#orch-col-blocked >> role:button \"{LIVE}\""))); + assert!(app.error().is_none(), "{:?}", app.error()); +} + +/// The same flick with frames drawn between (a real one spans a few). +#[test] +fn orchestration_board_fast_flick_across_frames_moves_the_card() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let to = rect(&mut app, "#orch-col-review"); + let start = (from.x + 30., from.y + 10.); + let end = (to.x + to.width / 2., to.y + 40.); + gesture(&mut app, start, &[(start.0 + 3., start.1 + 1.), end], true); + let said = calls(&app); + assert!(said.contains("move:it-live:review:"), "{said:?}"); +} + +/// A slow drag, small steps with frames between, lands where it's let go. +#[test] +fn orchestration_board_slow_drag_moves_the_card() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let to = rect(&mut app, "#orch-col-done"); + let start = (from.x + 30., from.y + from.height / 2.); + let end = (to.x + to.width / 2., to.y + to.height - 8.); + let path: Vec<(f32, f32)> = (1..=40) + .map(|i| { + let t = i as f32 / 40.; + (start.0 + (end.0 - start.0) * t, start.1 + (end.1 - start.1) * t) + }) + .collect(); + gesture(&mut app, start, &path, true); + let said = calls(&app); + assert!(said.contains("move:it-live:done:"), "{said:?}"); +} + +/// Dropped in the gutter between two columns, or on a column's head, it +/// lands in the nearest column all the same. +#[test] +fn orchestration_board_drop_between_columns_lands_in_the_nearest() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let blocked = rect(&mut app, "#orch-col-blocked"); + let start = (from.x + 30., from.y + from.height / 2.); + // Its head. + let end = (blocked.x + blocked.width / 2., blocked.y + 8.); + gesture(&mut app, start, &[end], true); + assert!(calls(&app).contains("move:it-live:blocked:"), "{}", calls(&app)); +} + +/// A card pressed on its outcome drags as readily as on its title. +#[test] +fn orchestration_board_drag_from_the_outcome() { + let mut app = mount(1400., 900.); + let outcome = rect(&mut app, "#orch-col-review >> .orch-outcome"); + let to = rect(&mut app, "#orch-col-now"); + let start = (outcome.x + outcome.width / 2., outcome.y + outcome.height / 2.); + let end = (to.x + to.width / 2., to.y + to.height - 8.); + gesture(&mut app, start, &[end], true); + let said = calls(&app); + assert!(said.contains(":active:"), "dragged by its outcome, it moves: {said:?}"); +} + +/// Escape while carried: nothing moves, and the card is back where it was. +#[test] +fn orchestration_board_escape_cancels() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let to = rect(&mut app, "#orch-col-blocked"); + let (x, y) = (from.x + 30., from.y + from.height / 2.); + send(&mut app, InputEvent::PointerMove { x, y }); + send(&mut app, InputEvent::PointerDown { x, y, button: P }); + let end = (to.x + to.width / 2., to.y + to.height - 8.); + for i in 1..=6 { + let t = i as f32 / 6.; + send(&mut app, InputEvent::PointerMove { x: x + (end.0 - x) * t, y: y + (end.1 - y) * t }); + app.frames(1); + } + assert!(app.ui.input.drag_state().is_some(), "a drag under way"); + app.key_with(Key::Escape, Modifiers::default()); + app.frames(5); + send(&mut app, InputEvent::PointerUp { x: end.0, y: end.1, button: P }); + app.ui.input.forget_clicks(); + app.settle(); + app.frames(30); + let said = calls(&app); + assert!(!said.contains("move:"), "Escape cancels: {said:?}"); + assert!(has(&mut app, &format!("#orch-col-next >> role:button \"{LIVE}\""))); + // (Nothing left lifted: every card at rest and shown.) + assert!(app.opacity(&format!("#orch-col-next >> role:button \"{LIVE}\"")) > 0.99); +} + +/// A press let go where it was, or after a wobble under the threshold, is a +/// click: the card's thread opens and nothing moves. +#[test] +fn orchestration_board_click_is_not_a_drag() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "now", "Control room"); + let at = (from.x + 30., from.y + from.height / 2.); + gesture(&mut app, at, &[], false); + let said = calls(&app); + assert!(said.contains("open:th-ui-control"), "{said:?}"); + assert!(!said.contains("move:"), "{said:?}"); + gesture(&mut app, at, &[(at.0 + 2., at.1 + 1.)], true); + let said = calls(&app); + assert_eq!(said.matches("open:th-ui-control").count(), 2, "{said:?}"); + assert!(!said.contains("move:"), "{said:?}"); +} + +/// Let go in the gutter between two columns, low down where the shorter +/// ones end: it lands in the nearer column (the board takes the drop, not +/// only the cards and columns under the pointer). +#[test] +fn orchestration_board_drop_in_the_gutter_lands_in_the_nearer_column() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let blocked = rect(&mut app, "#orch-col-blocked"); + let review = rect(&mut app, "#orch-col-review"); + let start = (from.x + 30., from.y + from.height / 2.); + // (A point past Blocked's right edge, nearer it than Review.) + let gutter = blocked.x + blocked.width + (review.x - blocked.x - blocked.width) * 0.3; + let end = (gutter, blocked.y + blocked.height - 4.); + gesture(&mut app, start, &[end], true); + assert!(calls(&app).contains("move:it-live:blocked:"), "{}", calls(&app)); +} + +/// Carried over a column, the cards from where it would land move down by +/// its height (their transforms; their layout stays), a dashed slot where +/// it would go; let go, every card is where it's laid out, nothing moved. +#[test] +fn orchestration_board_cards_make_room_and_settle() { + let mut app = mount(1400., 900.); + let from = card(&mut app, "next", LIVE); + let first = "#orch-col-blocked >> role:button \"Orchestrator screen\""; + let second = "#orch-col-blocked >> role:button \"sccache for iOS\""; + let id2 = app.target(second).unwrap(); + let shown = |app: &mut TestApp, id| { + app.ui.prepare_frame(); + app.ui.scene.borrow().transformed_bounds(id).y + }; + let rest = shown(&mut app, id2); + let one = rect(&mut app, first); + let (x, y) = (from.x + 30., from.y + from.height / 2.); + send(&mut app, InputEvent::PointerMove { x, y }); + send(&mut app, InputEvent::PointerDown { x, y, button: P }); + // Between the two cards: past the first's middle, above the second's. + let (tx, ty) = (one.x + one.width / 2., one.y + one.height - 4.); + for i in 1..=6 { + let t = i as f32 / 6.; + send(&mut app, InputEvent::PointerMove { x: x + (tx - x) * t, y: y + (ty - y) * t }); + app.frames(2); + } + app.frames(60); + assert!(has(&mut app, "#orch-col-blocked >> .orch-column-drop"), "a slot where it would land"); + let room = from.height + 4.; + let moved = shown(&mut app, id2); + assert!((moved - rest - room).abs() < 1.5, "moved down by the carried card's height: {moved} vs {rest}, {room}"); + // (The first stays: it lands after it.) + let id1 = app.target(first).unwrap(); + assert!((shown(&mut app, id1) - one.y).abs() < 0.5); + send(&mut app, InputEvent::PointerUp { x: tx, y: ty, button: P }); + app.ui.input.forget_clicks(); + app.settle(); + // Let go: the card comes in where the room was, the second where it + // stood aside, from the first frame (no spring back over the newcomer). + app.frame(); + let landed = shown(&mut app, id2); + assert!((landed - moved).abs() < 0.5, "it stays where it stood aside: {landed} vs {moved}"); + app.frames(60); + assert!((shown(&mut app, id2) - moved).abs() < 0.5); + let came = card(&mut app, "blocked", LIVE); + assert!((came.y - (one.y + one.height + 4.)).abs() < 1.5, "the card is where the room was: {} vs {}", came.y, one.y + one.height + 4.); + assert!(calls(&app).contains("move:it-live:blocked:"), "{}", calls(&app)); + assert!(!has(&mut app, ".orch-column-drop")); +} + +/// Pictures of a drag under way (run on purpose, BOARD_SHOTS=): a card +/// carried among Blocked's, and one over an empty column. +#[test] +#[ignore = "run on purpose: writes pictures of a drag under way"] +fn orchestration_board_drag_pictures() { + use zgx_gpu::GpuRenderer; + let out = std::path::PathBuf::from(std::env::var("BOARD_SHOTS").expect("BOARD_SHOTS")); + std::fs::create_dir_all(&out).unwrap(); + for (name, theme, filter, col, low) in [ + ("drag-among-dark", "zeron-dark", None, "blocked", false), + ("drag-empty-light", "zeron-light", Some("orc-orch"), "review", true), + ] { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/orch_board_fixture.luau"); + let mut app = TestApp::new(|runtime| crate::plugins::lend_design_system(ScriptHost::new(runtime, path.clone()), &path)); + let (w, h, scale) = (1400f32, 640f32, 2f32); + app.ui.scene.borrow_mut().resize(w, h); + let mut gpu = GpuRenderer::new((w * scale) as u32, (h * scale) as u32).unwrap(); + gpu.set_scale_factor(scale); + for font in crate::fonts() { + font.unwrap().install(&mut gpu.text_system().borrow_mut()); + } + gpu.install_text(&mut app.ui.scene.borrow_mut()); + app.ui.refresh_text_geometry(); + assert!(app.host.run_command(if theme.ends_with("dark") { "theme_dark" } else { "theme_light" })); + if let Some(f) = filter { + app.host.set_state("filter", Value::Str(f.into())).unwrap(); + } + app.frames(30); + let from = card(&mut app, "next", LIVE); + let lane = rect(&mut app, &format!("#orch-col-{col}")); + let (x, y) = (from.x + 30., from.y + from.height / 2.); + send(&mut app, InputEvent::PointerMove { x, y }); + send(&mut app, InputEvent::PointerDown { x, y, button: P }); + let (tx, ty) = (lane.x + 40., if low { lane.y + 60. } else { lane.y + 130. }); + for i in 1..=8 { + let t = i as f32 / 8.; + send(&mut app, InputEvent::PointerMove { x: x + (tx - x) * t, y: y + (ty - y) * t }); + app.frames(2); + } + app.frames(60); + app.ui.prepare_frame(); + let _ = app.ui.scene.borrow_mut().flush(); + let all = [zgx::scene::Rect { x: 0., y: 0., width: w, height: h }]; + gpu.render(&app.ui.scene.borrow(), &all).unwrap(); + let mut rgba = gpu.readback().unwrap(); + let back: [u8; 3] = if theme.ends_with("dark") { [0x1d, 0x1d, 0x22] } else { [0xe9, 0xe9, 0xee] }; + for px in rgba.chunks_exact_mut(4) { + let a = px[3] as u32; + for c in 0..3 { + px[c] = (px[c] as u32 + back[c] as u32 * (255 - a) / 255).min(255) as u8; + } + px[3] = 255; + } + let file = std::fs::File::create(out.join(format!("{name}.png"))).unwrap(); + let mut enc = png::Encoder::new(std::io::BufWriter::new(file), (w * scale) as u32, (h * scale) as u32); + enc.set_color(png::ColorType::Rgba); + enc.set_depth(png::BitDepth::Eight); + enc.write_header().unwrap().write_image_data(&rgba).unwrap(); + send(&mut app, InputEvent::PointerUp { x: tx, y: ty, button: P }); + } +} diff --git a/apps/zgx/src/orchestration_progress.rs b/apps/zgx/src/orchestration_progress.rs new file mode 100644 index 000000000..2f52c5e78 --- /dev/null +++ b/apps/zgx/src/orchestration_progress.rs @@ -0,0 +1,415 @@ +//! How far each chat's work has got, for the scripts: the rows the engine +//! streams (`WatchProgress`), kept whole by chat id, and the one call that +//! asks for a fresh note (`Orchestrate{op: progressCheck}`). Progress is +//! read, never asked of the agent: a check never touches the chat itself. +//! docs/design/orchestrators.md ("Progress") is the contract; +//! ui/account/orchestration_sample.luau has one of each state. +//! +//! What scripts see, `orchestration.progress` (snake_case keys, times in +//! Unix seconds, a field that isn't there is nil): +//! { [chat id] = { state, device_id, started?, last_step?, step?, +//! steps? = { done, total }, note?, note_at?, stalled, +//! tool?, tool_since?, refreshing, eta_secs?, updated, +//! proxy } } +//! `state`: "working", "waiting" (on background work), "awaiting_input", +//! "idle", "failed", "done" or "other". The clocks tick in the scripts: +//! elapsed = now − `started`, since the last step = now − `last_step`, time +//! left = `eta_secs` − (now − `updated`). `proxy`: a row the engine keeps +//! for a chat whose host writes none of its own (drawn as any other). +//! +//! and call: +//! orchestration.progress_check(chat) (async) the chat's row with its +//! note made fresh now (or on its +//! way: `refreshing`), also put in +//! `orchestration.progress` at once + +use std::{ + cell::{Cell, RefCell}, + collections::BTreeMap, + rc::Rc, +}; + +use chrono::{DateTime, Utc}; +use serde::Deserialize; +use serde_json::{Value as Json, json}; +use zeron_proto::orchestration::{Progress, ProgressState}; +use zgx_desktop::WindowContext; +use zgx_script::{ScriptHost, Shared, Value}; + +use crate::engine::{self, lists}; + +/// One open window's state. +struct Window { + id: u64, + shared: Shared, +} + +#[derive(Default)] +struct Tracker { + windows: RefCell>, + next_window: Cell, + watching: Cell, + rows: RefCell, + /// Whether something the engine sent couldn't be read, said once. + complained: Cell, + watch: RefCell>, +} + +thread_local! { + static TRACKER: Rc = Rc::new(Tracker::default()); +} + +pub fn install(cx: &mut WindowContext, host: &ScriptHost) { + let t = TRACKER.with(Rc::clone); + let window = t.next_window.get(); + t.next_window.set(window + 1); + t.windows.borrow_mut().push(Window { id: window, shared: host.shared("orchestration.progress", t.rows.borrow().value()) }); + let me = Rc::downgrade(&t); + cx.on_closed(move || { + if let Some(t) = me.upgrade() { + t.windows.borrow_mut().retain(|w| w.id != window); + } + }); + if !t.watching.replace(true) { + let weak = Rc::downgrade(&t); + engine::get().on_connect(move |rpc| { + let Some(t) = weak.upgrade() else { return }; + let heard = Rc::downgrade(&t); + // (As changes: the whole list once, then the rows that moved; + // an engine from before sends the whole list each time.) + let watch = lists::listen(rpc, "WatchProgress", move |frame| { + if let Some(t) = heard.upgrade() { + t.heard(frame.list); + } + }); + *t.watch.borrow_mut() = Some(watch); + }); + } + + host.async_action("orchestration.progress_check", move |chat| { + let chat = crate::auth::text(&chat); + async move { + crate::plugins::preview::refuse("orchestration.progress_check")?; + if chat.is_empty() { + return Err("invalid".to_string()); + } + let reply = engine::get().call("Orchestrate", json!({ "op": "progressCheck", "chatId": chat })).await?; + let Some(row) = Heard::read(&reply) else { return Ok(Value::Nil) }; + let value = row.value(); + TRACKER.with(|t| t.checked(row)); + Ok(value) + } + }); +} + +impl Tracker { + /// A frame of `WatchProgress`: every row, whole. + fn heard(&self, item: &Json) { + let Some(list) = item.as_array() else { + return self.complain(&"not a list"); + }; + // (A row this build can't read costs that row, not the rest.) + let rows: Vec = list.iter().filter_map(Heard::read).collect(); + if rows.len() < list.len() { + self.complain(&format!("{} unreadable rows", list.len() - rows.len())); + } + if self.rows.borrow_mut().replace(rows) { + self.publish(); + } + } + + /// A check's reply: its row, unless the stream already has a newer one. + fn checked(&self, row: Heard) { + if self.rows.borrow_mut().upsert(row) { + self.publish(); + } + } + + fn publish(&self) { + let value = self.rows.borrow().value(); + // (Taken out first: setting one may run a script that calls back in.) + let targets: Vec = self.windows.borrow().iter().map(|w| w.shared.clone()).collect(); + for shared in targets { + shared.set(value.clone()); + } + } + + fn complain(&self, error: &dyn std::fmt::Display) { + if !self.complained.replace(true) { + eprintln!("zeron-zgx: progress: WatchProgress sent what this build can't read ({error}); keeping what it had"); + } + } +} + +/// A row as the engine sent it: the wire's, and whether it's a proxy's +/// (`proxy`, a field newer engines add). +#[derive(Clone, Debug, PartialEq)] +struct Heard { + row: Progress, + proxy: bool, +} + +impl Heard { + fn read(json: &Json) -> Option { + let row = Progress::deserialize(json).ok()?; + Some(Heard { row, proxy: json["proxy"].as_bool().unwrap_or(false) }) + } + + fn value(&self) -> Value { + progress_value(&self.row, self.proxy) + } +} + +impl From for Heard { + fn from(row: Progress) -> Heard { + Heard { row, proxy: false } + } +} + +/// Every chat's row, by chat id, and each as scripts were given it (a row +/// that didn't change keeps its value, so what reads it does nothing). +#[derive(Default)] +struct Rows { + by_chat: BTreeMap, + values: BTreeMap, +} + +impl Rows { + /// The stream's whole list in place of what was held. Whether anything + /// changed. + fn replace(&mut self, rows: Vec) -> bool { + let by_chat: BTreeMap = rows.into_iter().map(|r| (r.row.chat_id.clone(), r)).collect(); + if by_chat == self.by_chat { + return false; + } + let old = std::mem::take(&mut self.by_chat); + let mut values = std::mem::take(&mut self.values); + self.values = by_chat + .iter() + .map(|(id, row)| { + let kept = (old.get(id) == Some(row)).then(|| values.remove(id)).flatten(); + (id.clone(), kept.unwrap_or_else(|| row.value())) + }) + .collect(); + self.by_chat = by_chat; + true + } + + /// One row in, unless the one held is newer. Whether anything changed. + fn upsert(&mut self, row: Heard) -> bool { + if let Some(held) = self.by_chat.get(&row.row.chat_id) { + if *held == row || held.row.updated_at > row.row.updated_at { + return false; + } + } + self.values.insert(row.row.chat_id.clone(), row.value()); + self.by_chat.insert(row.row.chat_id.clone(), row); + true + } + + fn value(&self) -> Value { + Value::record(self.values.iter().map(|(id, v)| (id.as_str(), v.clone()))) + } +} + +/// The word a state goes by in scripts. +fn state_word(s: ProgressState) -> &'static str { + match s { + ProgressState::Working => "working", + ProgressState::Waiting => "waiting", + ProgressState::AwaitingInput => "awaiting_input", + ProgressState::Idle => "idle", + ProgressState::Failed => "failed", + ProgressState::Done => "done", + ProgressState::Other => "other", + } +} + +/// One chat's row, as scripts see it. +fn progress_value(p: &Progress, proxy: bool) -> Value { + let secs = |t: Option<&DateTime>| t.map_or(Value::Nil, |t| Value::from(t.timestamp() as f64)); + let text = |s: Option<&str>| s.map_or(Value::Nil, Value::from); + let steps = p.steps.map_or(Value::Nil, |s| { + Value::record([("done", Value::from(f64::from(s.done))), ("total", Value::from(f64::from(s.total)))]) + }); + Value::record([ + ("chat_id", Value::from(p.chat_id.as_str())), + ("state", Value::from(state_word(p.state))), + ("device_id", Value::from(p.device_id.as_str())), + ("started", secs(p.started_at.as_ref())), + ("last_step", secs(p.last_step_at.as_ref())), + ("step", text(p.step.as_deref())), + ("steps", steps), + ("note", text(p.note.as_deref())), + ("note_at", secs(p.note_at.as_ref())), + ("stalled", Value::Bool(p.stalled)), + ("tool", text(p.tool.as_deref())), + ("tool_since", secs(p.tool_since.as_ref())), + ("refreshing", Value::Bool(p.refreshing)), + ("eta_secs", p.eta_secs.map_or(Value::Nil, |s| Value::from(f64::from(s)))), + ("updated", secs(Some(&p.updated_at))), + ("proxy", Value::Bool(proxy)), + ]) +} + +#[cfg(test)] +mod tests { + use super::*; + + const NOW: i64 = 1_794_470_400; + + fn iso(secs: i64) -> String { + DateTime::::from_timestamp(secs, 0).unwrap().to_rfc3339() + } + + fn text(v: Option<&Value>) -> Option { + v.map(crate::auth::text) + } + + fn num(v: Option<&Value>) -> Option { + v.and_then(Value::as_f64) + } + + fn nil(v: Option<&Value>) -> bool { + v.is_none_or(|v| matches!(v, Value::Nil)) + } + + fn working() -> Json { + json!({ + "chatId": "th-engine-state", "deviceId": "dev-desk", "state": "working", + "startedAt": iso(NOW - 1260), "lastStepAt": iso(NOW - 130), "step": "WatchOrchestration stream", + "steps": { "done": 5, "total": 8 }, "note": "Wiring the stream; ops next.", "noteAt": iso(NOW - 200), + "tool": "cargo test -p zeron-engine", "toolSince": iso(NOW - 540), "etaSecs": 840, "updatedAt": iso(NOW - 30), + }) + } + + #[test] + fn a_row_reads_in_snake_case_and_seconds() { + let row = Progress::deserialize(&working()).unwrap(); + let v = progress_value(&row, false); + assert_eq!(text(v.field("chat_id")).as_deref(), Some("th-engine-state")); + assert_eq!(text(v.field("state")).as_deref(), Some("working")); + assert_eq!(text(v.field("device_id")).as_deref(), Some("dev-desk")); + assert_eq!(num(v.field("started")), Some((NOW - 1260) as f64)); + assert_eq!(num(v.field("last_step")), Some((NOW - 130) as f64)); + assert_eq!(text(v.field("step")).as_deref(), Some("WatchOrchestration stream")); + let steps = v.field("steps").unwrap(); + assert_eq!((num(steps.field("done")), num(steps.field("total"))), (Some(5.), Some(8.))); + assert_eq!(text(v.field("note")).as_deref(), Some("Wiring the stream; ops next.")); + assert_eq!(num(v.field("note_at")), Some((NOW - 200) as f64)); + assert_eq!(v.field("stalled"), Some(&Value::Bool(false))); + assert_eq!(text(v.field("tool")).as_deref(), Some("cargo test -p zeron-engine")); + assert_eq!(num(v.field("tool_since")), Some((NOW - 540) as f64)); + assert_eq!(v.field("refreshing"), Some(&Value::Bool(false))); + assert_eq!(num(v.field("eta_secs")), Some(840.)); + assert_eq!(num(v.field("updated")), Some((NOW - 30) as f64)); + } + + #[test] + fn what_a_row_lacks_is_nil_and_states_are_words() { + let row: Progress = serde_json::from_value(json!({ + "chatId": "c", "deviceId": "d", "state": "awaitingInput", "stalled": true, "refreshing": true, "updatedAt": iso(NOW), + })) + .unwrap(); + let v = progress_value(&row, false); + assert_eq!(text(v.field("state")).as_deref(), Some("awaiting_input")); + assert_eq!(v.field("stalled"), Some(&Value::Bool(true))); + assert_eq!(v.field("refreshing"), Some(&Value::Bool(true))); + for key in ["started", "last_step", "step", "steps", "note", "note_at", "tool", "tool_since", "eta_secs"] { + assert!(nil(v.field(key)), "{key}"); + } + for (wire, word) in [ + ("working", "working"), + ("waiting", "waiting"), + ("idle", "idle"), + ("failed", "failed"), + ("done", "done"), + ("somethingNew", "other"), + ] { + let row: Progress = serde_json::from_value(json!({ + "chatId": "c", "deviceId": "d", "state": wire, "updatedAt": iso(NOW), + })) + .unwrap(); + assert_eq!(text(progress_value(&row, false).field("state")).as_deref(), Some(word), "{wire}"); + } + } + + #[test] + fn the_stream_replaces_and_a_check_upserts_unless_older() { + let t = Tracker::default(); + let other = json!({ "chatId": "th-b", "deviceId": "dev-mac", "state": "done", "updatedAt": iso(NOW - 600) }); + t.heard(&json!([working(), other, { "chatId": "broken" }])); + let v = t.rows.borrow().value(); + assert!(v.field("th-engine-state").is_some()); + assert_eq!(text(v.field("th-b").unwrap().field("state")).as_deref(), Some("done")); + assert!(v.field("broken").is_none()); + // The same again: nothing new. + assert!(!t.rows.borrow_mut().replace(vec![ + Progress::deserialize(&working()).unwrap().into(), + serde_json::from_value::(json!({ "chatId": "th-b", "deviceId": "dev-mac", "state": "done", "updatedAt": iso(NOW - 600) })) + .unwrap() + .into(), + ])); + // A check's reply, newer: in its place, refreshing. + let mut fresh = Progress::deserialize(&working()).unwrap(); + fresh.refreshing = true; + fresh.updated_at = DateTime::::from_timestamp(NOW, 0).unwrap(); + t.checked(fresh.clone().into()); + let v = t.rows.borrow().value(); + assert_eq!(v.field("th-engine-state").unwrap().field("refreshing"), Some(&Value::Bool(true))); + // An older one doesn't replace it. + let mut stale = fresh.clone(); + stale.refreshing = false; + stale.updated_at = DateTime::::from_timestamp(NOW - 100, 0).unwrap(); + assert!(!t.rows.borrow_mut().upsert(stale.into())); + // A frame without a chat: it goes (the stream is whole). + t.heard(&json!([working()])); + assert!(t.rows.borrow().value().field("th-b").is_none()); + // Not a list: told once, kept. + t.heard(&json!({ "nope": 1 })); + assert!(t.rows.borrow().value().field("th-engine-state").is_some()); + } + + /// A frame where one chat's row moved: that row's value is made again, + /// every other chat's is the very value scripts already have. + #[test] + fn a_frame_keeps_the_rows_it_does_not_change() { + let t = Tracker::default(); + let other = json!({ "chatId": "th-b", "deviceId": "dev-mac", "state": "done", "updatedAt": iso(NOW - 600) }); + t.heard(&json!([working(), other.clone()])); + let before = t.rows.borrow().value(); + let mut moved = working(); + moved["note"] = json!("further along"); + t.heard(&json!([moved, other])); + let after = t.rows.borrow().value(); + let same = |id: &str| match (before.field(id), after.field(id)) { + (Some(Value::Map(a)), Some(Value::Map(b))) => Rc::ptr_eq(a, b), + _ => false, + }; + assert!(same("th-b")); + assert!(!same("th-engine-state")); + assert_eq!(text(after.field("th-engine-state").unwrap().field("note")).as_deref(), Some("further along")); + } + + #[test] + fn a_proxy_row_reads_as_any_other_flagged() { + let mut proxy = working(); + proxy["proxy"] = json!(true); + let t = Tracker::default(); + t.heard(&json!([proxy, { "chatId": "th-b", "deviceId": "dev-mac", "state": "working", "updatedAt": iso(NOW) }])); + let v = t.rows.borrow().value(); + let row = v.field("th-engine-state").unwrap(); + assert_eq!(row.field("proxy"), Some(&Value::Bool(true))); + assert_eq!(text(row.field("state")).as_deref(), Some("working")); + assert_eq!(v.field("th-b").unwrap().field("proxy"), Some(&Value::Bool(false))); + } + + #[test] + fn a_preview_can_t_check_progress() { + use crate::plugins::preview; + preview::set(Some(preview::Preview { id: "sleek".into(), name: "Sleek".into() })); + assert!(preview::refuse("orchestration.progress_check").is_err()); + preview::set(None); + assert!(preview::refuse("orchestration.progress_check").is_ok()); + } +} diff --git a/apps/zgx/src/orchestration_timeline.rs b/apps/zgx/src/orchestration_timeline.rs new file mode 100644 index 000000000..f0ae5a3e7 --- /dev/null +++ b/apps/zgx/src/orchestration_timeline.rs @@ -0,0 +1,486 @@ +//! What each chat has been doing lately, for the Control room's Timeline map +//! (a line per chat over the last 15 or 60 minutes). Kept here, in the +//! window, from what `sessions` already says (src/sessions.rs): every time it +//! changes, each chat's state is compared with the last one kept, and a +//! change is written down. Nothing is asked of the engine and nothing runs on +//! a timer; a chat's history starts when this app first sees it. +//! +//! What scripts see, `orchestration.timeline`: +//! { [chat id] = { { at, status, kind?, label? }, … } } (oldest first) +//! Each entry is when that chat became so: `at` Unix seconds, `status` its +//! `sessions` status ("working", "input", "approval", "failed", "done") or +//! "idle" when it has none, `kind` and `label` what it was doing while +//! working (its `activity`'s). An entry lasts until the next one; the last +//! lasts until now. A chat seen idle with nothing before isn't listed. +//! +//! What's kept: the last 2 hours, and for each chat the one entry before +//! them (so a state begun earlier still shows from the start of the map), +//! at most 64 entries a chat and 300 chats (those changed least recently go +//! first). A chat no longer in `sessions` keeps its history until it is all +//! older than that, so a moment's gap doesn't erase it. + +use std::{ + cell::{Cell, RefCell}, + collections::{HashMap, HashSet, VecDeque}, + rc::Rc, +}; + +use zgx_desktop::WindowContext; +use zgx_script::{ScriptHost, Shared, Value}; + +/// How far back entries are kept (seconds). +const RETAIN: i64 = 2 * 60 * 60; +/// The most entries kept for one chat. +const PER_CHAT: usize = 64; +/// The most chats kept. +const CHATS: usize = 300; + +/// What a chat was doing from `at` on. +#[derive(Clone, Debug, PartialEq)] +struct Entry { + at: i64, + status: Rc, + kind: Option>, + label: Option>, +} + +impl Entry { + fn is(&self, row: &Row) -> bool { + &*self.status == row.status && self.kind.as_deref() == row.kind && self.label.as_deref() == row.label + } + + fn value(&self) -> Value { + let text = |s: &Option>| s.as_ref().map_or(Value::Nil, |s| Value::Str(s.clone())); + Value::record([ + ("at", Value::from(self.at as f64)), + ("status", Value::Str(self.status.clone())), + ("kind", text(&self.kind)), + ("label", text(&self.label)), + ]) + } +} + +/// One chat as `sessions` shows it now: its status ("idle" for none), and +/// while working what it's doing and since when. +#[derive(Clone, Copy, Debug)] +struct Row<'a> { + id: &'a str, + status: &'a str, + kind: Option<&'a str>, + label: Option<&'a str>, + since: Option, +} + +/// One chat's entries, oldest first, and them as scripts see them (made +/// again only after they change). +#[derive(Default)] +struct Ring { + entries: VecDeque, + value: Option, +} + +impl Ring { + fn latest(&self) -> Option<&Entry> { + self.entries.back() + } + + fn value(&mut self) -> Value { + self.value + .get_or_insert_with(|| Value::List(self.entries.iter().map(Entry::value).collect())) + .clone() + } + + /// Drops what's past keeping at `cutoff`: whatever is before the one + /// entry before it, and the oldest beyond [`PER_CHAT`]. Whether any went. + fn prune(&mut self, cutoff: i64) -> bool { + let before = self.entries.len(); + let inside = self.entries.iter().position(|e| e.at >= cutoff).unwrap_or(self.entries.len()); + self.entries.drain(..inside.saturating_sub(1)); + while self.entries.len() > PER_CHAT { + self.entries.pop_front(); + } + let changed = self.entries.len() != before; + if changed { + self.value = None; + } + changed + } +} + +/// Every chat's history, as heard from `sessions`. +#[derive(Default)] +struct History { + chats: HashMap, Ring>, + /// As scripts were last given it (none: made again when asked). + value: Option, +} + +impl History { + /// `rows` heard at `now` (Unix seconds): a chat whose state differs from + /// its last entry gets a new one, and what's past keeping goes. Whether + /// anything changed. + fn heard<'a>(&mut self, rows: impl IntoIterator>, now: i64) -> bool { + let cutoff = now - RETAIN; + let mut changed = false; + let mut present: HashSet<&str> = HashSet::new(); + for row in rows { + present.insert(row.id); + let latest = self.chats.get(row.id).and_then(Ring::latest); + // (A chat with no history counts as idle.) + let same = match latest { + Some(latest) => latest.is(&row), + None => row.status == "idle", + }; + if same { + continue; + } + let at = match (latest, row.since) { + // (When it began, if that's believable.) + (None, Some(since)) if since <= now && since >= cutoff => since, + (Some(latest), Some(since)) if since > latest.at && since <= now => since, + _ => now, + }; + // (Never before the last entry: the clock may have moved back.) + let at = latest.map_or(at, |l| at.max(l.at)); + let entry = Entry { + at, + status: row.status.into(), + kind: row.kind.map(Rc::from), + label: row.label.map(Rc::from), + }; + if !self.chats.contains_key(row.id) { + self.chats.insert(row.id.into(), Ring::default()); + } + let Some(ring) = self.chats.get_mut(row.id) else { continue }; + ring.entries.push_back(entry); + ring.value = None; + changed = true; + } + let before = self.chats.len(); + self.chats.retain(|id, ring| { + changed |= ring.prune(cutoff); + let Some(latest) = ring.latest() else { return false }; + // Gone from `sessions`, or idle, and nothing in the window: no + // line to draw. + let old = latest.at < cutoff; + !(old && (!present.contains(&**id) || &*latest.status == "idle")) + }); + if self.chats.len() > CHATS { + // Those changed least recently go first; of those, the ones no + // longer in `sessions` before those that are. + let mut order: Vec<(bool, i64, Rc)> = self + .chats + .iter() + .map(|(id, ring)| (present.contains(&**id), ring.latest().map_or(i64::MIN, |e| e.at), id.clone())) + .collect(); + order.sort(); + for (_, _, id) in order.into_iter().take(self.chats.len() - CHATS) { + self.chats.remove(&id); + } + } + changed |= self.chats.len() != before; + if changed { + self.value = None; + } + changed + } + + fn value(&mut self) -> Value { + if let Some(value) = &self.value { + return value.clone(); + } + let value = Value::record(self.chats.iter_mut().map(|(id, ring)| (id.clone(), ring.value()))); + self.value = Some(value.clone()); + value + } +} + +/// Every chat in `sessions`: its `chats` and each of its `children` lists. +fn rows(sessions: &Value) -> Vec> { + let chats = sessions.field("chats").map(Value::list).unwrap_or_default(); + let children = match sessions.field("children") { + Some(Value::Map(children)) => &children[..], + _ => &[], + }; + let lists = std::iter::once(chats).chain(children.iter().map(|(_, list)| list.list())); + lists.flat_map(|list| list.iter().filter_map(row)).collect() +} + +fn row(chat: &Value) -> Option> { + let id = text(chat.field("id"))?; + let status = text(chat.field("status")).unwrap_or("idle"); + // (What it's doing counts only while it works.) + let activity = chat.field("activity").filter(|_| status == "working"); + let part = |key| activity.and_then(|a| a.field(key)); + Some(Row { + id, + status, + kind: text(part("kind")), + label: text(part("label")), + since: part("since").and_then(Value::as_f64).map(|s| s as i64), + }) +} + +fn text(v: Option<&Value>) -> Option<&str> { + match v { + Some(Value::Str(s)) => Some(s), + _ => None, + } +} + +#[derive(Default)] +struct Timeline { + history: RefCell, + /// Each open window's state, let go of as it closes. + windows: RefCell>, + next_window: Cell, +} + +thread_local! { + static TIMELINE: Rc = Rc::new(Timeline::default()); +} + +pub fn install(cx: &mut WindowContext, host: &ScriptHost) { + let t = TIMELINE.with(Rc::clone); + let window = t.next_window.get(); + t.next_window.set(window + 1); + let value = t.history.borrow_mut().value(); + t.windows.borrow_mut().push((window, host.shared("orchestration.timeline", value))); + let me = Rc::downgrade(&t); + cx.on_closed(move || { + if let Some(t) = me.upgrade() { + t.windows.borrow_mut().retain(|(id, _)| *id != window); + } + }); +} + +/// `sessions` as just published: each chat's change written down, and +/// every window told if there was one. +pub(crate) fn heard_sessions(sessions: &Value) { + let t = TIMELINE.with(Rc::clone); + let now = chrono::Utc::now().timestamp(); + let value = { + let mut history = t.history.borrow_mut(); + if !history.heard(rows(sessions), now) { + return; + } + history.value() + }; + // (Taken out first: setting one may run a script that calls back in.) + let targets: Vec = t.windows.borrow().iter().map(|(_, s)| s.clone()).collect(); + for shared in targets { + shared.set(value.clone()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const NOW: i64 = 1_794_470_400; + + fn idle(id: &str) -> Row<'_> { + Row { id, status: "idle", kind: None, label: None, since: None } + } + + fn status<'a>(id: &'a str, status: &'a str) -> Row<'a> { + Row { id, status, kind: None, label: None, since: None } + } + + fn working<'a>(id: &'a str, kind: &'a str, label: &'a str, since: i64) -> Row<'a> { + Row { id, status: "working", kind: Some(kind), label: Some(label), since: Some(since) } + } + + fn ats(h: &History, id: &str) -> Vec { + h.chats[id].entries.iter().map(|e| e.at).collect() + } + + fn statuses(h: &History, id: &str) -> Vec { + h.chats[id].entries.iter().map(|e| e.status.to_string()).collect() + } + + #[test] + fn only_a_change_is_written_down() { + let mut h = History::default(); + // Seen idle with nothing before: nothing to show. + assert!(!h.heard([idle("a")], NOW)); + assert!(h.chats.is_empty()); + assert!(h.heard([working("a", "testing", "cargo test", NOW - 5)], NOW)); + // The same again: nothing new. + assert!(!h.heard([working("a", "testing", "cargo test", NOW - 5)], NOW + 10)); + assert_eq!(h.chats["a"].entries.len(), 1); + // Another label: a change. + assert!(h.heard([working("a", "testing", "cargo clippy", NOW - 5)], NOW + 20)); + assert_eq!(ats(&h, "a"), [NOW - 5, NOW + 20]); + // Waiting on you, then idle, then idle again. + assert!(h.heard([status("a", "input")], NOW + 30)); + assert!(h.heard([idle("a")], NOW + 40)); + assert!(!h.heard([idle("a")], NOW + 50)); + assert_eq!(statuses(&h, "a"), ["working", "working", "input", "idle"]); + assert_eq!(ats(&h, "a"), [NOW - 5, NOW + 20, NOW + 30, NOW + 40]); + } + + #[test] + fn since_dates_a_change_when_it_can() { + let mut h = History::default(); + // First seen working: from when it began. + assert!(h.heard([working("a", "testing", "cargo test", NOW - 300)], NOW)); + assert_eq!(ats(&h, "a"), [NOW - 300]); + // A new activity begun after the last entry: from when it began. + assert!(h.heard([working("a", "merging", "git merge", NOW + 15)], NOW + 20)); + assert_eq!(ats(&h, "a"), [NOW - 300, NOW + 15]); + // A `since` no later than the last entry, or ahead of now: now. + assert!(h.heard([working("a", "editing", "src", NOW + 15)], NOW + 30)); + assert!(h.heard([working("a", "testing", "x", NOW + 999)], NOW + 40)); + assert_eq!(ats(&h, "a"), [NOW - 300, NOW + 15, NOW + 30, NOW + 40]); + // First seen with a `since` in the future or older than kept: now. + assert!(h.heard([working("b", "testing", "x", NOW + 60)], NOW)); + assert!(h.heard([working("c", "testing", "x", NOW - RETAIN - 1)], NOW)); + assert_eq!((ats(&h, "b"), ats(&h, "c")), (vec![NOW], vec![NOW])); + // First seen waiting on you: now. + assert!(h.heard([status("d", "approval")], NOW)); + assert_eq!(ats(&h, "d"), [NOW]); + } + + #[test] + fn a_chat_keeps_its_last_two_hours_and_the_entry_before() { + let mut h = History::default(); + let start = NOW - 3 * RETAIN; + // One change a minute for over six hours, a different label each. + let labels: Vec = (0..400).map(|i| format!("step {i}")).collect(); + for (i, label) in labels.iter().enumerate() { + let at = start + i as i64 * 60; + h.heard([working("a", "testing", label, at)], at); + } + // Never more than 64. + assert_eq!(h.chats["a"].entries.len(), PER_CHAT); + assert_eq!(h.chats["a"].latest().unwrap().label.as_deref(), Some("step 399")); + + // Sparse: one change long ago, then quiet. + let mut h = History::default(); + h.heard([working("a", "testing", "old", NOW - 3 * RETAIN)], NOW - 3 * RETAIN); + h.heard([working("a", "testing", "older", NOW - 2 * RETAIN)], NOW - 2 * RETAIN); + h.heard([working("a", "testing", "before", NOW - RETAIN - 600)], NOW - RETAIN - 600); + h.heard([working("a", "testing", "inside", NOW - 600)], NOW - 600); + assert!(h.heard([working("a", "testing", "inside", NOW - 600)], NOW)); + // The one before the window stays, those before it go. + assert_eq!(ats(&h, "a"), [NOW - RETAIN - 600, NOW - 600]); + // All of it long past, still in `sessions` and working: its latest stays. + assert!(h.heard([working("a", "testing", "inside", NOW - 600)], NOW + 3 * RETAIN)); + assert_eq!(ats(&h, "a"), [NOW - 600]); + // …but gone from `sessions` with nothing in the window: forgotten. + assert!(h.heard([], NOW + 3 * RETAIN)); + assert!(h.chats.is_empty()); + } + + #[test] + fn a_chat_gone_a_moment_keeps_its_history() { + let mut h = History::default(); + h.heard([working("a", "testing", "x", NOW - 60), status("b", "input")], NOW); + // `a` missing from one publish: kept, nothing changed. + assert!(!h.heard([status("b", "input")], NOW + 5)); + assert_eq!(ats(&h, "a"), [NOW - 60]); + // Back, the same: still nothing new. + assert!(!h.heard([working("a", "testing", "x", NOW - 60), status("b", "input")], NOW + 10)); + // Idle and long quiet: no line, gone. + h.heard([idle("a"), status("b", "input")], NOW + 20); + assert!(h.heard([idle("a"), status("b", "input")], NOW + 20 + RETAIN + 1)); + assert!(!h.chats.contains_key("a")); + assert!(h.chats.contains_key("b")); + } + + #[test] + fn at_most_three_hundred_chats_the_least_recently_changed_go() { + let mut h = History::default(); + let ids: Vec = (0..320).map(|i| format!("c{i}")).collect(); + for (i, id) in ids.iter().enumerate() { + h.heard([status(id, "input")], NOW + i as i64); + } + assert_eq!(h.chats.len(), CHATS); + assert!(!h.chats.contains_key("c19")); + assert!(h.chats.contains_key("c20")); + assert!(h.chats.contains_key("c319")); + // Those still in `sessions` stay over those that aren't. + let rows: Vec = ids[20..40].iter().map(|id| status(id, "input")).collect(); + assert!(h.heard(rows.iter().copied().chain([status("new", "failed")]), NOW + 400)); + assert!(h.chats.contains_key("new")); + assert!(h.chats.contains_key("c20")); + assert!(!h.chats.contains_key("c40")); + assert_eq!(h.chats.len(), CHATS); + } + + fn text(v: Option<&Value>) -> Option { + v.map(crate::auth::text) + } + + fn nil(v: Option<&Value>) -> bool { + v.is_none_or(|v| matches!(v, Value::Nil)) + } + + #[test] + fn it_reads_sessions_and_shows_as_scripts_see_it() { + let activity = |kind: &str, label: &str, since: i64| { + Value::record([ + ("kind", Value::from(kind)), + ("label", Value::from(label)), + ("since", Value::from(since as f64)), + ]) + }; + let sessions = Value::record([ + ( + "chats", + Value::List( + vec![ + Value::record([ + ("id", Value::from("top")), + ("status", Value::from("working")), + ("activity", activity("testing", "cargo test", NOW - 30)), + ]), + Value::record([("id", Value::from("quiet"))]), + ] + .into(), + ), + ), + ( + "children", + Value::record([( + "top", + Value::List( + vec![Value::record([ + ("id", Value::from("kid")), + ("status", Value::from("input")), + // (Not working: what it was doing doesn't count.) + ("activity", activity("merging", "git merge", NOW - 5)), + ])] + .into(), + ), + )]), + ), + ]); + let rows = rows(&sessions); + assert_eq!(rows.iter().map(|r| r.id).collect::>(), ["top", "quiet", "kid"]); + let mut h = History::default(); + assert!(h.heard(rows, NOW)); + let v = h.value(); + assert!(v.field("quiet").is_none()); + let top = v.field("top").unwrap().list(); + assert_eq!(top.len(), 1); + assert_eq!(top[0].field("at").and_then(Value::as_f64), Some((NOW - 30) as f64)); + assert_eq!(text(top[0].field("status")).as_deref(), Some("working")); + assert_eq!(text(top[0].field("kind")).as_deref(), Some("testing")); + assert_eq!(text(top[0].field("label")).as_deref(), Some("cargo test")); + let kid = &v.field("kid").unwrap().list()[0]; + assert_eq!(kid.field("at").and_then(Value::as_f64), Some(NOW as f64)); + assert_eq!(text(kid.field("status")).as_deref(), Some("input")); + assert!(nil(kid.field("kind")) && nil(kid.field("label"))); + // Asked again with nothing changed: the very same value. + let again = h.value(); + assert!(matches!((&v, &again), (Value::Map(a), Value::Map(b)) if Rc::ptr_eq(a, b))); + // One chat changed: the other's list is the same one as before. + h.heard([status("top", "done"), status("kid", "input")], NOW + 10); + let next = h.value(); + let (Some(Value::List(a)), Some(Value::List(b))) = (v.field("kid"), next.field("kid")) else { panic!() }; + assert!(Rc::ptr_eq(a, b)); + assert_eq!(next.field("top").unwrap().list().len(), 2); + } +} diff --git a/apps/zgx/src/plugins/preview.rs b/apps/zgx/src/plugins/preview.rs index f6b43c8ad..8e2351788 100644 --- a/apps/zgx/src/plugins/preview.rs +++ b/apps/zgx/src/plugins/preview.rs @@ -99,6 +99,15 @@ pub const BLOCKED: &[&str] = &[ "cloud.sleep", "cloud.retry", "cloud.set_machine", + // (What an orchestrator, its crew or the Brain dump is told, and what + // the machines are asked.) + "orchestration.op", + "orchestration.loads_wanted", + "orchestration.progress_check", + // (The Scratchpad written: a preview reads it, never changes it.) + "scratchpad.set_text", + "scratchpad.set_shapes", + "scratchpad.restore", ]; pub fn blocked(action: &str) -> bool { diff --git a/apps/zgx/src/receipt_copy_tests.rs b/apps/zgx/src/receipt_copy_tests.rs new file mode 100644 index 000000000..528043314 --- /dev/null +++ b/apps/zgx/src/receipt_copy_tests.rs @@ -0,0 +1,22 @@ +//! A dump's receipt (ds/orch_receipt) copies as it was written: its words +//! are one paragraph (its parts spans in it), not a box a word, so a +//! selection across it gives the dump's own text, spaces and all, never +//! the tabs zgx puts between boxes side by side. Mounted headless on +//! ui/orch_receipt_fixture.luau (the sample's newest dump). + +use zgx_script::{ScriptHost, testing::TestApp}; + +#[test] +fn a_receipt_copies_its_dump_as_written() { + let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/orch_receipt_fixture.luau"); + let mut app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); + app.ui.scene.borrow_mut().resize(900., 500.); + app.frames(30); + assert_eq!(app.error(), None, "the fixture failed"); + let words = app.target(".orch-receipt-words").expect("the receipt's words"); + assert!(app.ui.select_all_text(words), "its text selects"); + let copied = app.ui.selected_text(); + let written = "ok so the orchestrator page should be side by side not tabs, also remind me to rerun omarchy update it needs sudo. and ask the vpn guy whether moscow rebooted fine. the CI thing can wait till next week, and is the engine state thread still on the registry part?"; + assert_eq!(copied, written, "the dump as written"); + assert!(!copied.contains('\t'), "no tabs: {copied:?}"); +} diff --git a/apps/zgx/src/scratchpad.rs b/apps/zgx/src/scratchpad.rs new file mode 100644 index 000000000..3b892ea78 --- /dev/null +++ b/apps/zgx/src/scratchpad.rs @@ -0,0 +1,502 @@ +//! The Control Room's Scratchpad, for the scripts: the document and its +//! history as the engine streams them (`WatchScratchpad`, whole on every +//! change), and the person's edits (`Scratchpad` ops). Nothing here is work: +//! no edit routes or tells an agent anything. crates/proto/src/scratchpad.rs +//! is the contract; ui/account/orchestration_sample.luau has a whole one. +//! +//! What scripts see, `scratchpad` (snake_case keys, times in Unix seconds, a +//! field that isn't there is nil): +//! { host, missing?, loaded, text, rev, updated_at?, updated_by, device_id?, +//! shapes = { { id, kind, x, y, w, h, points = { { x, y } }, text, +//! color, stroke?, z, ... } }, +//! history = { { id, at, edited_at, by, device_id?, restored_from?, +//! chars, shape_count, preview } } } +//! `host`: always true (the scripts' own default has it false: an app +//! without this module). `missing`: "engine" when the engine refused the +//! watch (one without the Scratchpad), until a frame comes. +//! `loaded`: false until the engine's first frame. `updated_by` and `by`: +//! "person", "brain_dump" or "other". History is newest first; its first is +//! the document as it is now. A shape's fields this build doesn't know ride +//! along and are written back as they came. +//! +//! and call: +//! scratchpad.set_text(text) (async) the text replaced: its `rev` +//! scratchpad.set_shapes(shapes) (async) the drawing replaced: its `rev` +//! scratchpad.restore(id) (async) a snapshot put back: its `rev` +//! scratchpad.revision(id) (async) one snapshot, whole: +//! { id, at, edited_at, by, text, shapes, +//! restored_from? } + +use std::{ + cell::{Cell, RefCell}, + rc::Rc, +}; + +use chrono::{DateTime, Utc}; +use serde::Deserialize; +use serde_json::{Value as Json, json}; +use zeron_proto::scratchpad::{Editor, Revision, RevisionInfo, ScratchpadState, Shape}; +use zgx_desktop::WindowContext; +use zgx_script::{ScriptHost, Shared, Value}; + +use crate::engine::{self, rpc::Watch}; + +/// One open window's state. +struct Window { + id: u64, + shared: Shared, +} + +#[derive(Default)] +struct Tracker { + windows: RefCell>, + next_window: Cell, + watching: Cell, + /// The last frame, as heard (nil before the first). + state: RefCell>, + /// Whether something the engine sent couldn't be read, said once. + complained: Cell, + /// The engine refused the watch: it has no Scratchpad. + missing: Cell, + watch: RefCell>, +} + +thread_local! { + static TRACKER: Rc = Rc::new(Tracker::default()); +} + +pub fn install(cx: &mut WindowContext, host: &ScriptHost) { + let t = TRACKER.with(Rc::clone); + let window = t.next_window.get(); + t.next_window.set(window + 1); + t.windows.borrow_mut().push(Window { id: window, shared: host.shared("scratchpad", t.value()) }); + let me = Rc::downgrade(&t); + cx.on_closed(move || { + if let Some(t) = me.upgrade() { + t.windows.borrow_mut().retain(|w| w.id != window); + } + }); + if !t.watching.replace(true) { + let weak = Rc::downgrade(&t); + engine::get().on_connect(move |rpc| { + let Some(t) = weak.upgrade() else { return }; + let heard = Rc::downgrade(&t); + let ended = Rc::downgrade(&t); + let watch = rpc.watch_ending( + "WatchScratchpad", + json!({}), + move |item| { + if let Some(t) = heard.upgrade() { + t.heard(&item); + } + }, + move |why| { + if let Some(t) = ended.upgrade() { + t.ended(&why); + } + }, + ); + *t.watch.borrow_mut() = Some(watch); + }); + } + + host.async_action("scratchpad.set_text", move |text| { + let text = crate::auth::text(&text); + async move { + crate::plugins::preview::refuse("scratchpad.set_text")?; + rev_of(engine::get().call("Scratchpad", json!({ "op": "setText", "text": text })).await?) + } + }); + host.async_action("scratchpad.set_shapes", move |shapes| { + let shapes = shapes_json(&shapes); + async move { + crate::plugins::preview::refuse("scratchpad.set_shapes")?; + let shapes = shapes?; + rev_of(engine::get().call("Scratchpad", json!({ "op": "setShapes", "shapes": shapes })).await?) + } + }); + host.async_action("scratchpad.restore", move |id| { + let id = crate::auth::text(&id); + async move { + crate::plugins::preview::refuse("scratchpad.restore")?; + if id.is_empty() { + return Err("invalid".to_string()); + } + rev_of(engine::get().call("Scratchpad", json!({ "op": "restore", "revisionId": id })).await?) + } + }); + host.async_action("scratchpad.revision", move |id| { + let id = crate::auth::text(&id); + async move { + crate::plugins::preview::refuse("scratchpad.revision")?; + if id.is_empty() { + return Err("invalid".to_string()); + } + let reply = engine::get().call("Scratchpad", json!({ "op": "revision", "id": id })).await?; + let revision = Revision::deserialize(&reply).map_err(|_| "unreadable".to_string())?; + Ok(revision_value(&revision)) + } + }); +} + +impl Tracker { + /// A frame of `WatchScratchpad`: the document and its history, whole. + fn heard(&self, item: &Json) { + let state = match ScratchpadState::deserialize(item) { + Ok(state) => state, + Err(e) => return self.complain(&e), + }; + let found = self.missing.replace(false); + if self.state.borrow().as_ref() == Some(&state) && !found { + return; + } + *self.state.borrow_mut() = Some(state); + self.publish(); + } + + /// The watch ended before its handle dropped: refused (an engine + /// without the Scratchpad), or the connection gone (the next one + /// watches again). + fn ended(&self, why: &str) { + if is_refusal(why) && !self.missing.replace(true) { + eprintln!("zeron-zgx: scratchpad: the engine has no Scratchpad ({why}); edits stay unsaved"); + self.publish(); + } + } + + fn value(&self) -> Value { + state_value(self.state.borrow().as_ref(), self.missing.get()) + } + + fn publish(&self) { + let value = self.value(); + // (Taken out first: setting one may run a script that calls back in.) + let targets: Vec = self.windows.borrow().iter().map(|w| w.shared.clone()).collect(); + for shared in targets { + shared.set(value.clone()); + } + } + + fn complain(&self, error: &dyn std::fmt::Display) { + if !self.complained.replace(true) { + eprintln!("zeron-zgx: scratchpad: WatchScratchpad sent what this build can't read ({error}); keeping what it had"); + } + } +} + +/// An op's `{ rev }` reply: the rev. +fn rev_of(reply: Json) -> Result { + Ok(reply["rev"].as_str().map_or(Value::Nil, Value::from)) +} + +/// The word an editor goes by in scripts. +fn editor_word(e: Editor) -> &'static str { + match e { + Editor::Person => "person", + Editor::BrainDump => "brain_dump", + Editor::Other => "other", + } +} + +fn secs(t: &DateTime) -> Value { + Value::from(t.timestamp() as f64) +} + +fn text_or_nil(s: Option<&str>) -> Value { + s.map_or(Value::Nil, Value::from) +} + +/// Whether a watch's end is the engine refusing it, not the connection +/// going ("closed") or the stream ending ("done"). +fn is_refusal(why: &str) -> bool { + !matches!(why, "closed" | "done") +} + +/// What scripts see: the document and its history (`loaded` false before +/// the first frame; `missing` while the engine has no Scratchpad). +fn state_value(state: Option<&ScratchpadState>, missing: bool) -> Value { + let missing = if missing { Value::from("engine") } else { Value::Nil }; + let Some(state) = state else { + return Value::record([ + ("host", Value::Bool(true)), + ("missing", missing), + ("loaded", Value::Bool(false)), + ("text", Value::from("")), + ("rev", Value::from("")), + ("updated_by", Value::from("person")), + ("shapes", Value::List(Vec::new().into())), + ("history", Value::List(Vec::new().into())), + ]); + }; + let pad = &state.pad; + Value::record([ + ("host", Value::Bool(true)), + ("missing", missing), + ("loaded", Value::Bool(true)), + ("text", Value::from(pad.text.as_str())), + ("rev", Value::from(pad.rev.as_str())), + ("updated_at", pad.updated_at.as_ref().map_or(Value::Nil, secs)), + ("updated_by", Value::from(editor_word(pad.updated_by))), + ("device_id", text_or_nil(pad.device_id.as_deref())), + ("shapes", shapes_value(&pad.shapes)), + ("history", Value::List(state.history.iter().map(info_value).collect())), + ]) +} + +fn info_value(r: &RevisionInfo) -> Value { + Value::record([ + ("id", Value::from(r.id.as_str())), + ("at", secs(&r.at)), + ("edited_at", secs(&r.edited_at)), + ("by", Value::from(editor_word(r.by))), + ("device_id", text_or_nil(r.device_id.as_deref())), + ("restored_from", text_or_nil(r.restored_from.as_deref())), + ("chars", Value::from(r.chars as f64)), + ("shape_count", Value::from(r.shape_count as f64)), + ("preview", Value::from(r.preview.as_str())), + ]) +} + +fn revision_value(r: &Revision) -> Value { + Value::record([ + ("id", Value::from(r.id.as_str())), + ("at", secs(&r.at)), + ("edited_at", secs(&r.edited_at)), + ("by", Value::from(editor_word(r.by))), + ("device_id", text_or_nil(r.device_id.as_deref())), + ("restored_from", text_or_nil(r.restored_from.as_deref())), + ("text", Value::from(r.text.as_str())), + ("shapes", shapes_value(&r.shapes)), + ]) +} + +fn shapes_value(shapes: &[Shape]) -> Value { + Value::List(shapes.iter().map(shape_value).collect()) +} + +/// One shape as scripts see it: its points as `{ x, y }`, and any field this +/// build doesn't know as it came. +fn shape_value(s: &Shape) -> Value { + let points = Value::List( + s.points.iter().map(|p| Value::record([("x", Value::from(p[0])), ("y", Value::from(p[1]))])).collect(), + ); + let mut fields: Vec<(String, Value)> = vec![ + ("id".into(), Value::from(s.id.as_str())), + ("kind".into(), Value::from(s.kind.as_str())), + ("x".into(), Value::from(s.x)), + ("y".into(), Value::from(s.y)), + ("w".into(), Value::from(s.w)), + ("h".into(), Value::from(s.h)), + ("points".into(), points), + ("text".into(), Value::from(s.text.as_str())), + ("color".into(), Value::from(s.color.as_str())), + ("stroke".into(), s.stroke.map_or(Value::Nil, Value::from)), + ("z".into(), Value::from(s.z as f64)), + ]; + for (k, v) in &s.extra { + fields.push((k.clone(), crate::plugins::json_value(v))); + } + Value::record(fields) +} + +/// Shapes from a script, as the wire has them: points as `[x, y]` pairs, +/// each read as a [`Shape`] (an unreadable one refuses the whole call, so a +/// half drawing is never written). +fn shapes_json(shapes: &Value) -> Result, String> { + shapes + .list() + .iter() + .map(|s| { + let mut json = crate::plugins::value_json(s); + let Some(obj) = json.as_object_mut() else { return Err("invalid".to_string()) }; + let points: Vec = obj + .get("points") + .and_then(Json::as_array) + .map(|list| { + list.iter() + .filter_map(|p| Some(json!([p["x"].as_f64()?, p["y"].as_f64()?]))) + .collect() + }) + .unwrap_or_default(); + obj.insert("points".into(), Json::Array(points)); + // (Nil fields read as absent, not null.) + obj.retain(|_, v| !v.is_null()); + Shape::deserialize(&json).map_err(|_| "invalid".to_string()) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const NOW: i64 = 1_794_470_400; + + fn iso(secs: i64) -> String { + DateTime::::from_timestamp(secs, 0).unwrap().to_rfc3339() + } + + fn text(v: Option<&Value>) -> Option { + v.map(crate::auth::text) + } + + fn num(v: Option<&Value>) -> Option { + v.and_then(Value::as_f64) + } + + fn nil(v: Option<&Value>) -> bool { + v.is_none_or(|v| matches!(v, Value::Nil)) + } + + fn frame() -> Json { + json!({ + "pad": { + "text": "# Sync\n\n- one", "rev": "r7", "updatedAt": iso(NOW - 60), "updatedBy": "brainDump", "deviceId": "dev-desk", + "shapes": [ + { "id": "a", "kind": "rect", "x": 10, "y": 20, "w": 100, "h": 40, "color": "accent", "z": 0, "roundness": 8 }, + { "id": "b", "kind": "arrow", "points": [[110, 40], [200.5, 40]], "z": 1, "stroke": 2 }, + ], + }, + "history": [ + { "id": "h2", "at": iso(NOW - 120), "editedAt": iso(NOW - 60), "by": "brainDump", "chars": 14, "shapeCount": 2, "preview": "Sync" }, + { "id": "h1", "at": iso(NOW - 900), "editedAt": iso(NOW - 880), "by": "person", "restoredFrom": "h0", + "deviceId": "dev-mac", "chars": 3, "shapeCount": 0, "preview": "old" }, + ], + }) + } + + #[test] + fn a_frame_reads_in_snake_case_and_seconds() { + let state = ScratchpadState::deserialize(&frame()).unwrap(); + let v = state_value(Some(&state), false); + assert_eq!(v.field("loaded"), Some(&Value::Bool(true))); + assert_eq!(text(v.field("text")).as_deref(), Some("# Sync\n\n- one")); + assert_eq!(text(v.field("rev")).as_deref(), Some("r7")); + assert_eq!(num(v.field("updated_at")), Some((NOW - 60) as f64)); + assert_eq!(text(v.field("updated_by")).as_deref(), Some("brain_dump")); + assert_eq!(text(v.field("device_id")).as_deref(), Some("dev-desk")); + let shapes = v.field("shapes").unwrap().list(); + assert_eq!(shapes.len(), 2); + let a = &shapes[0]; + assert_eq!(text(a.field("kind")).as_deref(), Some("rect")); + assert_eq!((num(a.field("x")), num(a.field("w"))), (Some(10.), Some(100.))); + assert_eq!(text(a.field("color")).as_deref(), Some("accent")); + assert!(nil(a.field("stroke"))); + // A renderer's own field, kept. + assert_eq!(num(a.field("roundness")), Some(8.)); + let b = &shapes[1]; + assert_eq!(text(b.field("color")).as_deref(), Some("fg")); + assert_eq!(num(b.field("stroke")), Some(2.)); + let p = b.field("points").unwrap().list(); + assert_eq!((num(p[1].field("x")), num(p[1].field("y"))), (Some(200.5), Some(40.))); + let history = v.field("history").unwrap().list(); + assert_eq!(history.len(), 2); + assert_eq!(text(history[0].field("by")).as_deref(), Some("brain_dump")); + assert_eq!(num(history[0].field("at")), Some((NOW - 120) as f64)); + assert_eq!(num(history[0].field("edited_at")), Some((NOW - 60) as f64)); + assert_eq!(num(history[0].field("shape_count")), Some(2.)); + assert!(nil(history[0].field("restored_from"))); + assert_eq!(text(history[1].field("restored_from")).as_deref(), Some("h0")); + assert_eq!(text(history[1].field("device_id")).as_deref(), Some("dev-mac")); + assert_eq!(text(history[1].field("preview")).as_deref(), Some("old")); + } + + #[test] + fn before_the_first_frame_it_is_empty_and_not_loaded() { + let v = state_value(None, false); + assert_eq!(v.field("loaded"), Some(&Value::Bool(false))); + assert_eq!(text(v.field("text")).as_deref(), Some("")); + assert!(v.field("shapes").unwrap().list().is_empty()); + assert!(v.field("history").unwrap().list().is_empty()); + assert!(nil(v.field("updated_at"))); + assert_eq!(v.field("host"), Some(&Value::Bool(true))); + assert!(nil(v.field("missing"))); + } + + #[test] + fn an_engine_without_the_scratchpad_is_said() { + let v = state_value(None, true); + assert_eq!(text(v.field("missing")).as_deref(), Some("engine")); + assert_eq!(v.field("loaded"), Some(&Value::Bool(false))); + // A refusal, not the connection going or the stream ending. + assert!(is_refusal("unknown method: WatchScratchpad")); + assert!(!is_refusal("closed") && !is_refusal("done")); + // A frame clears it. + let t = Tracker::default(); + t.ended("unknown method: WatchScratchpad"); + assert!(t.missing.get()); + t.heard(&serde_json::to_value(ScratchpadState::default()).unwrap()); + assert!(!t.missing.get()); + assert!(nil(t.value().field("missing"))); + // The connection going doesn't make it missing. + t.ended("closed"); + assert!(!t.missing.get()); + } + + #[test] + fn editors_are_words() { + for (wire, word) in [("person", "person"), ("brainDump", "brain_dump"), ("somethingNew", "other")] { + let e: Editor = serde_json::from_value(json!(wire)).unwrap(); + assert_eq!(editor_word(e), word, "{wire}"); + } + } + + #[test] + fn the_stream_publishes_only_what_changed_and_keeps_what_it_had() { + let t = Tracker::default(); + t.heard(&frame()); + assert_eq!(text(t.value().field("rev")).as_deref(), Some("r7")); + // Unreadable: told once, kept. + t.heard(&json!({ "pad": 3 })); + assert_eq!(text(t.value().field("rev")).as_deref(), Some("r7")); + let mut next = frame(); + next["pad"]["rev"] = json!("r8"); + next["pad"]["text"] = json!("new"); + t.heard(&next); + assert_eq!(text(t.value().field("text")).as_deref(), Some("new")); + } + + #[test] + fn shapes_go_back_as_the_wire_has_them_unknown_fields_kept() { + let state = ScratchpadState::deserialize(&frame()).unwrap(); + let v = state_value(Some(&state), false); + let back = shapes_json(v.field("shapes").unwrap()).unwrap(); + assert_eq!(back, state.pad.shapes); + let wire = serde_json::to_value(&back).unwrap(); + assert_eq!(wire[0]["roundness"], json!(8)); + assert_eq!(wire[1]["points"], json!([[110.0, 40.0], [200.5, 40.0]])); + // None at all, and one that isn't a shape. + assert!(shapes_json(&Value::List(Vec::new().into())).unwrap().is_empty()); + assert!(shapes_json(&Value::List(vec![Value::from("nope")].into())).is_err()); + } + + #[test] + fn a_revision_reads_whole() { + let r: Revision = serde_json::from_value(json!({ + "id": "h1", "at": iso(NOW - 900), "editedAt": iso(NOW - 880), "by": "person", "restoredFrom": "h0", + "text": "old", "shapes": [{ "id": "s", "kind": "text", "text": "API", "z": 3 }], + })) + .unwrap(); + let v = revision_value(&r); + assert_eq!(text(v.field("text")).as_deref(), Some("old")); + assert_eq!(text(v.field("restored_from")).as_deref(), Some("h0")); + assert_eq!(num(v.field("edited_at")), Some((NOW - 880) as f64)); + let s = &v.field("shapes").unwrap().list()[0]; + assert_eq!((text(s.field("text")).as_deref(), num(s.field("z"))), (Some("API"), Some(3.))); + assert!(v.field("history").is_none()); + assert_eq!(rev_of(json!({ "rev": "r9" })).unwrap(), Value::from("r9")); + } + + #[test] + fn a_preview_can_t_edit_the_scratchpad() { + use crate::plugins::preview; + preview::set(Some(preview::Preview { id: "sleek".into(), name: "Sleek".into() })); + assert!(preview::refuse("scratchpad.set_text").is_err()); + assert!(preview::refuse("scratchpad.set_shapes").is_err()); + assert!(preview::refuse("scratchpad.restore").is_err()); + // (Reading it is a preview's as much as the watch is.) + assert!(preview::refuse("scratchpad.revision").is_ok()); + preview::set(None); + assert!(preview::refuse("scratchpad.set_text").is_ok()); + } +} diff --git a/apps/zgx/src/sessions.rs b/apps/zgx/src/sessions.rs index 7e5810e89..b750ccc8f 100644 --- a/apps/zgx/src/sessions.rs +++ b/apps/zgx/src/sessions.rs @@ -13,11 +13,13 @@ //! icon? = { emoji?, image?, hue? } } }, //! chats = { { id, title, project?, device, branch?, cwd?, status?, //! updated, created, harness?, agent?, model?, -//! running_subagents, side_chats, working_side_chats } }, +//! running_subagents, side_chats, working_side_chats, +//! parent?, child_kind?, activity?, todo? } }, //! children = { [parent id] = { { id, parent, title, device, status?, -//! updated, created, harness?, agent?, child_kind? } } } (its -//! side chats, newest first; `child_kind` "subagent" or -//! "side" for one a Zeron chat started, none for the person's), +//! updated, created, harness?, agent?, child_kind?, +//! activity?, todo? } } } (its side chats, newest first; +//! `child_kind` "subagent", "side" or "crew" for one a Zeron +//! chat started, none for the person's), //! places = { pinned = { id }, sections = { { id, name, sessions = { id }, //! collapsed } } }, //! archived = { chat, … } (as `chats`, the most recently moved first), @@ -28,6 +30,10 @@ //! "failed", "done": finished but not yet seen), nil when idle; `updated` //! and `created` are Unix seconds; `agent` is ds.Mark's name for the //! chat's harness. Chats are the top-level ones that aren't archived. +//! `activity` is what its agent is doing while it works, `{ kind, label, +//! since }` (`kind` "testing", "merging"…, `since` Unix seconds); `todo` how +//! far through its todo list it is, `{ done, total, current? }`, whenever its +//! host says. //! //! and call, each rejecting with an outcome word (offline, failed): //! sessions.create({ project?, device?, config?, branch?, cwd?, title?, parent?, id?, no_project? }) @@ -81,6 +87,11 @@ use crate::engine::{self, lists, rpc::Watch}; /// A live state older than this belongs to an engine that has gone (the /// shipping app's `SESSION_STALE_MS`). const STALE_SECS: i64 = 45; +/// How long what the engine sends is gathered before the scripts are given +/// it: a turn starting changes a chat's live state and its entry, which +/// come as two frames, and each publish runs every script reading +/// `sessions` (and the Timeline after it). One frame's time. +const GATHER: Duration = Duration::from_millis(16); /// A device whose presence beat is older than this is offline (the shipping /// app's `device_online`). pub(crate) const ONLINE_SECS: i64 = 70; @@ -159,6 +170,8 @@ struct Sessions { /// When `shown_live` was judged (Unix seconds). shown_at: Cell>, shown_devices: RefCell>, + /// Whether a publish is on its way ([`Sessions::publish_soon`]). + publish_pending: Cell, /// Publishes so far (for tests). #[cfg(test)] published: Cell, @@ -191,6 +204,11 @@ struct RowWith { working_side_chats: u32, known_project: bool, agent_icon: Option, + /// What its agent is doing (while it works), and its todo progress. + activity: Option, + todo: Option, + /// When the turn it's working on began (Unix seconds), while it works. + turn_started: Option, } /// `slot` holds `item` now; whether that's new. @@ -552,7 +570,7 @@ impl Layout { } let place = place_of(c); if let Place::Child(parent) = &place { - child_row(rows, c, id, parent, live(id), at); + child_row(rows, c, id, parent, known, live(id), at); let i = *layout.parent_at.entry(parent.clone()).or_insert_with(|| { layout.parents.push(parent.clone()); layout.kids.push(Kids { ids: Vec::new(), value: Value::Nil, working: 0 }); @@ -609,9 +627,20 @@ impl Layout { } /// A side chat's row, built again if what it shows moved. -fn child_row(rows: &mut HashMap, c: &Json, id: &str, parent: &str, session: Option<&Session>, at: chrono::DateTime) { +fn child_row( + rows: &mut HashMap, + c: &Json, + id: &str, + parent: &str, + known: &HashSet<&str>, + session: Option<&Session>, + at: chrono::DateTime, +) { let status = status_of(rows.get(id).map_or_else(|| seen_marks(c), |row| row.marks), session, at); let harness = c["config"]["harness"].as_str(); + // Its project, as a top-level chat's (one not known: none). A crew + // thread is listed in the sidebar under it, as any chat is. + let project = c["spaceId"].as_str().filter(|s| known.contains(s)); let with = RowWith { status, waiting: None, @@ -619,8 +648,11 @@ fn child_row(rows: &mut HashMap, c: &Json, id: &str, parent: &str, running_subagents: 0, side_chats: 0, working_side_chats: 0, - known_project: false, + known_project: project.is_some(), agent_icon: harness.and_then(crate::agents::registry_icon), + activity: activity_of(status, session), + todo: session.and_then(|s| s.todo.clone()), + turn_started: turn_started(status, session), }; if rows.get(id).is_some_and(|row| row.with == with) { return; @@ -638,7 +670,12 @@ fn child_row(rows: &mut HashMap, c: &Json, id: &str, parent: &str, ("id", Value::from(id)), ("parent", Value::from(parent)), ("title", Value::from(title)), + // Its own title alone (none: not named yet), for a name that falls + // back otherwise (a crew thread's: its role, its item's title). + ("name", opt(c["title"].as_str().filter(|t| !t.trim().is_empty()))), ("device", Value::from(c["deviceId"].as_str().unwrap_or_default())), + ("project", opt(project)), + ("branch", opt(c["branch"].as_str())), ("status", status.map_or(Value::Nil, Value::from)), ("updated", Value::from(updated as f64)), ("created", Value::from(created as f64)), @@ -646,9 +683,18 @@ fn child_row(rows: &mut HashMap, c: &Json, id: &str, parent: &str, ("agent", harness.and_then(crate::agents::glyph).map_or(Value::Nil, |g| Value::from(g.as_str()))), // An agent from the ACP registry: its icon, as listed. ("agent_icon", with.agent_icon.as_deref().map_or(Value::Nil, Value::from)), - // Started by a Zeron chat through Zeron's tools ("subagent" or - // "side"); none for a side chat the person started. + // Started by a Zeron chat through Zeron's tools ("subagent", + // "side", or "crew": a thread of an orchestrator's); none for a side + // chat the person started. ("child_kind", opt(c["childKind"].as_str())), + // Who started it, when an orchestrator or the Brain dump did (and for + // which dump and part): `{ chat_id, dump_id?, route_id? }`. + ("started_by", started_by_value(&c["startedBy"])), + ("activity", with.activity.as_ref().map_or(Value::Nil, crate::orchestration::activity_value)), + ("todo", with.todo.as_ref().map_or(Value::Nil, crate::orchestration::todo_value)), + // When the turn it's working on began (Unix seconds), while it works: + // the Q&A's "Answering" is a turn begun since its question came. + ("turn_started", with.turn_started.map_or(Value::Nil, |t| Value::from(t as f64))), ]); rows.insert(id.to_owned(), Row { moved: moved(c), updated: updated as f64, marks: seen_marks(c), with, value }); } @@ -681,6 +727,9 @@ fn top_row( working_side_chats, known_project: project.is_some(), agent_icon: harness.and_then(crate::agents::registry_icon), + activity: activity_of(status, session), + todo: session.and_then(|s| s.todo.clone()), + turn_started: turn_started(status, session), }; if let Some(row) = rows.get(id).filter(|row| row.with == with) { return row.value.clone(); @@ -721,11 +770,33 @@ 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))), + // Whose child it is, and how, when it's one (as a side chat's row + // says). + ("parent", opt(c["parentChatId"].as_str())), + ("child_kind", opt(c["childKind"].as_str())), + ("started_by", started_by_value(&c["startedBy"])), + // What its agent is doing while it works, and its todo progress. + ("activity", with.activity.as_ref().map_or(Value::Nil, crate::orchestration::activity_value)), + ("todo", with.todo.as_ref().map_or(Value::Nil, crate::orchestration::todo_value)), + // When the turn it's working on began (Unix seconds), while it works: + // the Q&A's "Answering" is a turn begun since its question came. + ("turn_started", with.turn_started.map_or(Value::Nil, |t| Value::from(t as f64))), ]); rows.insert(id.to_owned(), Row { moved: moved(c), updated: updated as f64, marks, with, value: value.clone() }); value } +/// A chat's `startedBy`, as scripts read it (nil: none, or none readable). +fn started_by_value(v: &Json) -> Value { + let opt = |v: Option<&str>| v.filter(|s| !s.is_empty()).map_or(Value::Nil, Value::from); + let Some(chat) = v["chatId"].as_str().filter(|s| !s.is_empty()) else { return Value::Nil }; + Value::record([ + ("chat_id", Value::from(chat)), + ("dump_id", opt(v["dumpId"].as_str())), + ("route_id", opt(v["routeId"].as_str())), + ]) +} + thread_local! { static SESSIONS: Rc = Rc::new(Sessions::default()); } @@ -1013,7 +1084,7 @@ impl Sessions { } Slot::Spaces | Slot::Places | Slot::Connectivity => return self.heard(slot, frame.list.clone()), } - self.publish(); + self.publish_soon(); } fn heard(&self, slot: Slot, item: Json) { @@ -1070,10 +1141,32 @@ impl Sessions { *self.confirmed.borrow_mut() = places; } } - self.publish(); + self.publish_soon(); + } + + /// What the engine sent, given to the scripts [`GATHER`] from now with + /// whatever else comes by then. (At once in tests, and before a + /// connection's tasks are there.) + fn publish_soon(&self) { + if cfg!(test) || !engine::get().can_spawn() { + return self.publish(); + } + if self.publish_pending.replace(true) { + return; + } + engine::get().spawn(async { + zgx::timer::sleep(GATHER).await; + SESSIONS.with(|s| { + if s.publish_pending.get() { + s.publish(); + } + }); + }); } fn publish(&self) { + // (Whatever was on its way goes now.) + self.publish_pending.set(false); #[cfg(test)] self.published.set(self.published.get() + 1); let value = self.value(); @@ -1083,6 +1176,7 @@ impl Sessions { shared.set(value.clone()); } self.look_again(); + crate::orchestration_timeline::heard_sessions(&value); } /// Publishes again as the soonest live state showing goes stale, or a @@ -1414,7 +1508,7 @@ impl Sessions { for id in &dirty { let Some(Place::Child(parent)) = layout.place.get(id) else { continue }; let Some(c) = entry(id, &mut index) else { continue }; - child_row(&mut rows, c, id, parent, live(id), at); + child_row(&mut rows, c, id, parent, known, live(id), at); if !parents.contains(parent) { parents.push(parent.clone()); } @@ -1772,6 +1866,9 @@ struct Live { /// them (whether a finish is new, or was interrupted). outcome: Option, last_completed_turn: Option, + /// What its agent is doing, and its todo progress, as its row shows them. + activity: Option, + todo: Option, } /// Each chat's `Live`, judged at `now` (Unix seconds). @@ -1789,6 +1886,8 @@ fn live_of(s: &Session, at: chrono::DateTime) -> Live { subagents: zeron_proto::view::running_subagents(Some(s), at), outcome: s.last_turn_outcome.as_ref().and_then(|o| serde_json::to_string(o).ok()), last_completed_turn: s.last_completed_turn.clone(), + activity: s.activity.clone(), + todo: s.todo.clone(), } } @@ -1880,6 +1979,16 @@ fn status_of( } } +/// What a chat's agent is doing, while its row says it works. +fn activity_of(status: Option<&str>, session: Option<&Session>) -> Option { + session.filter(|_| status == Some("working")).and_then(|s| s.activity.clone()) +} + +/// When the turn a working chat is on began (Unix seconds). +fn turn_started(status: Option<&str>, session: Option<&Session>) -> Option { + session.filter(|_| status == Some("working")).and_then(|s| s.started_at).map(|t| t.timestamp()) +} + /// A chat's sleep at `now`: asleep until (`true`, its time), or woke and not /// looked at since (`false`, when it woke). fn sleep_of(chat: &Json, marks: SeenMarks, now: chrono::DateTime) -> Option<(bool, i64)> { @@ -2458,6 +2567,47 @@ mod tests { v.field("chats").unwrap().list().iter().find(|c| c.field("id").map(crate::auth::text).as_deref() == Some(id)).unwrap() } + /// A crew thread (a chat an orchestrator started, under it) carries its + /// project and branch, so the sidebar lists it under that project as any + /// chat: the orchestrator's own chat is listed in no project, and a + /// thread found only under it was in none. Its cwd (a worktree, another + /// checkout) doesn't move it: the project is its `spaceId`. Archived, it + /// is in no list, as an archived side chat is. + #[test] + fn a_crew_thread_carries_its_project_for_the_sidebar() { + let now = now_seconds(); + let s = Sessions::default(); + s.heard(Slot::Spaces, json!([{ "id": "zeron", "name": "zeron", "path": "/home/u/Documents/zeron", "deviceId": "d" }])); + s.set_chats(json!([ + { "id": "orc", "title": "Zeron fixes", "deviceId": "d", "spaceId": "zeron", "createdAt": iso(now - 7200) }, + { "id": "qa", "title": "Control Room Q&A section", "parentChatId": "orc", "childKind": "crew", "deviceId": "d", + "spaceId": "zeron", "cwd": "/home/u/Documents/dev/zeron", "branch": "main", "createdAt": iso(now - 600) }, + { "id": "new", "title": "", "lastMessagePreview": "[Message from Zeron chat Zeron fixes]", "parentChatId": "orc", "childKind": "crew", + "deviceId": "d", "spaceId": "zeron", "createdAt": iso(now - 60) }, + { "id": "lost", "title": "Project gone", "parentChatId": "orc", "childKind": "crew", "deviceId": "d", + "spaceId": "deleted", "createdAt": iso(now - 500) }, + { "id": "old", "title": "Done and archived", "parentChatId": "orc", "childKind": "crew", "deviceId": "d", + "spaceId": "zeron", "archived": true, "createdAt": iso(now - 900) }, + ])); + s.heard.set(true); + let v = s.value(); + let text = |v: Option<&zgx_script::Value>| v.and_then(|v| match v { zgx_script::Value::Nil => None, v => Some(crate::auth::text(v)) }); + let kids = v.field("children").unwrap().field("orc").unwrap().list(); + let kid = |id: &str| kids.iter().find(|c| text(c.field("id")).as_deref() == Some(id)); + let qa = kid("qa").expect("the crew thread, under its orchestrator"); + assert_eq!(text(qa.field("child_kind")).as_deref(), Some("crew")); + assert_eq!(text(qa.field("project")).as_deref(), Some("zeron")); + assert_eq!(text(qa.field("branch")).as_deref(), Some("main")); + assert_eq!(text(qa.field("name")).as_deref(), Some("Control Room Q&A section")); + assert_eq!(text(kid("lost").unwrap().field("project")), None, "a project not known: none"); + // Not titled yet: no name of its own (the sidebar names it by its role). + assert_eq!(text(kid("new").unwrap().field("name")), None); + assert!(kid("old").is_none(), "archived: not under it"); + let listed = |list: &str| v.field(list).unwrap().list().iter().filter_map(|c| text(c.field("id"))).collect::>(); + assert!(!listed("chats").contains(&"old".to_owned()) && !listed("archived").contains(&"old".to_owned())); + assert!(!listed("chats").contains(&"qa".to_owned()), "listed once: under its orchestrator"); + } + #[test] fn side_chats_are_their_parent_s_and_counted_on_its_row() { let v = with_children("2099-01-01T00:00:00Z").value(); @@ -2482,6 +2632,359 @@ mod tests { assert_eq!(p.field("running_subagents").and_then(|n| n.as_f64()), Some(2.)); } + /// What a live chat's agent is doing, and how far through its todo list + /// it is, on its row (top-level or a crew thread's): the activity only + /// while it works, the todo whenever its host says; a change to either + /// alone is published and builds its row again. + /// A snapshot of an engine's lists (`SESSIONS_SNAPSHOT`: a JSON file of + /// WatchChats, WatchSessions, WatchDevices, WatchSpaces, + /// WatchSidebarPreferences and LocalDevice) through the bridge, its + /// value written beside it (`.value.json`): what the scripts would see. + #[test] + #[ignore] + fn sessions_snapshot() { + let Ok(path) = std::env::var("SESSIONS_SNAPSHOT") else { return }; + let snap: Json = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap(); + let s = Sessions::default(); + *s.here.borrow_mut() = snap["LocalDevice"]["id"].as_str().map(String::from); + s.heard(Slot::Devices, snap["WatchDevices"].clone()); + s.heard(Slot::Spaces, snap["WatchSpaces"].clone()); + s.heard(Slot::Places, snap["WatchSidebarPreferences"].clone()); + s.heard(Slot::Live, snap["WatchSessions"].clone()); + s.heard(Slot::Chats, snap["WatchChats"].clone()); + let v = s.value(); + std::fs::write(format!("{path}.value.json"), v.to_json()).unwrap(); + } + + /// A recorded WatchSessions stream (`SESSIONS_STREAM`: [{ t, m: { item } }]) + /// over the snapshot's chats, replayed: each chat named in + /// `SESSIONS_WATCH` (comma-separated prefixes), its status at each frame. + #[test] + #[ignore] + fn sessions_stream_replay() { + let (Ok(snap), Ok(stream)) = (std::env::var("SESSIONS_SNAPSHOT"), std::env::var("SESSIONS_STREAM")) else { return }; + let watch: Vec = std::env::var("SESSIONS_WATCH").unwrap_or_default().split(',').map(String::from).collect(); + let snap: Json = serde_json::from_str(&std::fs::read_to_string(&snap).unwrap()).unwrap(); + let stream: Json = serde_json::from_str(&std::fs::read_to_string(&stream).unwrap()).unwrap(); + let s = Sessions::default(); + *s.here.borrow_mut() = snap["LocalDevice"]["deviceId"].as_str().map(String::from); + s.heard(Slot::Devices, snap["WatchDevices"].clone()); + s.heard(Slot::Spaces, snap["WatchSpaces"].clone()); + s.heard(Slot::Chats, snap["WatchChats"].clone()); + for f in stream.as_array().unwrap() { + let t = f["t"].as_f64().unwrap() as i64; + s.heard(Slot::Live, f["m"]["item"].clone()); + let v = s.value_at(t); + for c in v.field("chats").unwrap().list() { + let id = c.field("id").map(crate::auth::text).unwrap_or_default(); + if watch.iter().any(|w| !w.is_empty() && id.starts_with(w.as_str())) { + eprintln!("{t} {} {:?}", &id[..8], c.field("status").map(crate::auth::text)); + } + } + } + } + + /// Recorded change frames (`SESSIONS_FRAMES`: [{ t, m, list, changes? }] + /// of WatchChats and WatchSessions, as `{"changes": true}` streams them) + /// replayed as the listener hands them over, over the snapshot's devices + /// and projects: the watched chats' statuses at each. + #[test] + #[ignore] + fn sessions_frames_replay() { + use crate::engine::lists::{Changes, Frame}; + let (Ok(snap), Ok(frames)) = (std::env::var("SESSIONS_SNAPSHOT"), std::env::var("SESSIONS_FRAMES")) else { return }; + let watch: Vec = std::env::var("SESSIONS_WATCH").unwrap_or_default().split(',').map(String::from).collect(); + let snap: Json = serde_json::from_str(&std::fs::read_to_string(&snap).unwrap()).unwrap(); + let frames: Json = serde_json::from_str(&std::fs::read_to_string(&frames).unwrap()).unwrap(); + let s = Sessions::default(); + *s.here.borrow_mut() = snap["LocalDevice"]["deviceId"].as_str().map(String::from); + s.heard_list(Slot::Devices, &Frame::for_tests(&snap["WatchDevices"], None, "id")); + s.heard(Slot::Spaces, snap["WatchSpaces"].clone()); + for f in frames.as_array().unwrap() { + let t = f["t"].as_f64().unwrap() as i64; + let (slot, key) = if f["m"] == "WatchChats" { (Slot::Chats, "id") } else { (Slot::Live, "chatId") }; + let changes = f["changes"].as_object().map(|c| Changes { + changed: c["changed"].as_array().cloned().unwrap_or_default(), + removed: c["removed"].as_array().map(|r| r.iter().filter_map(|k| k.as_str().map(String::from)).collect()).unwrap_or_default(), + reordered: c["reordered"].as_bool().unwrap_or(false), + }); + s.heard_list(slot, &Frame::for_tests(&f["list"], changes.as_ref(), key)); + let v = s.value_at(t); + let listed: Vec = v.field("chats").unwrap().list().iter().filter_map(|c| c.field("id").map(crate::auth::text)).collect(); + for w in &watch { + if w.is_empty() { + continue; + } + let found = listed.iter().find(|id| id.starts_with(w.as_str())); + let st = found.and_then(|id| chat(&v, id).field("status").map(crate::auth::text)); + eprintln!("{t} {} {w} listed={} status={st:?}", f["m"].as_str().unwrap_or(""), found.is_some()); + } + } + } + + /// The lists as the engine streams them, one change at a time (rows + /// changing state, working with background agents, waiting on them, + /// going and coming back, chats moving), give the scripts what the same + /// lists heard whole give: every chat listed, each in its state. + #[test] + fn streamed_changes_list_what_whole_lists_do() { + use crate::engine::lists::{Changes, Frame}; + let now = now_seconds(); + let mut seed: u64 = 0x5eed; + let mut rnd = |n: u64| { + seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + (seed >> 33) % n + }; + let n = 40; + let chat_row = |i: usize, t: i64| json!({ "id": format!("c{i:02}"), "deviceId": if i % 3 == 0 { "r" } else { "here" }, "title": format!("Chat {i}"), + "spaceId": "space", "lastMessageAt": iso(t), "createdAt": iso(now - 9000), "lastSeenAt": iso(now - 5000), "archived": false }); + let session_row = |i: usize, kind: u64, t: i64| { + let mut r = json!({ "chatId": format!("c{i:02}"), "deviceId": if i % 3 == 0 { "r" } else { "here" }, "updatedAt": iso(t), + "lastCompletedTurn": format!("t{i}"), "runningSubagents": if kind == 1 || kind == 2 { 3 } else { 0 } }); + match kind { + 0 | 1 => { r["status"] = json!("working"); r["startedAt"] = json!(iso(t - 30)); } + 2 => { r["status"] = json!("idle"); r["startedAt"] = Json::Null; + r["waiting"] = json!({ "since": iso(t), "on": [{ "id": "a", "kind": "agent", "label": "x" }] }); } + _ => { r["status"] = json!("idle"); r["startedAt"] = Json::Null; + r["lastTurnOutcome"] = json!({ "status": "completed", "finishedAt": iso(t) }); } + } + r + }; + let mut chats: Vec = (0..n).map(|i| chat_row(i, now - 100)).collect(); + let mut live: Vec = (0..n).map(|i| session_row(i, (i % 4) as u64, now - 5)).collect(); + let devices = json!([{ "id": "here", "name": "desk", "lastSeenAt": iso(now) }, { "id": "r", "name": "honeypot", "lastSeenAt": iso(now) }]); + let s = Sessions::default(); + *s.here.borrow_mut() = Some("here".into()); + s.heard_list(Slot::Devices, &Frame::for_tests(&devices, None, "id")); + s.heard_list(Slot::Chats, &Frame::for_tests(&Json::Array(chats.clone()), None, "id")); + s.heard_list(Slot::Live, &Frame::for_tests(&Json::Array(live.clone()), None, "chatId")); + let shown = |v: &zgx_script::Value| -> Vec<(String, Option)> { + let mut out: Vec<(String, Option)> = v.field("chats").unwrap().list().iter() + .map(|c| (c.field("id").map(crate::auth::text).unwrap_or_default(), c.field("status").map(crate::auth::text))) + .collect(); + out.sort(); + out + }; + for step in 0..600 { + let t = now - 5 + (step as i64 % 4); + match rnd(6) { + // A row's state moves (the engine sends that row alone). + 0..=2 => { + let i = rnd(live.len() as u64) as usize; + let id: usize = live[i]["chatId"].as_str().unwrap()[1..].parse().unwrap(); + let row = session_row(id, rnd(4), t); + live[i] = row.clone(); + let changes = Changes { changed: vec![row], removed: vec![], reordered: false }; + s.heard_list(Slot::Live, &Frame::for_tests(&Json::Array(live.clone()), Some(&changes), "chatId")); + } + // A row goes from the middle; later, back at the end. + 3 => { + if live.len() > 5 { + let i = rnd(live.len() as u64) as usize; + let gone = live.remove(i); + let changes = Changes { changed: vec![], removed: vec![gone["chatId"].as_str().unwrap().to_owned()], reordered: false }; + s.heard_list(Slot::Live, &Frame::for_tests(&Json::Array(live.clone()), Some(&changes), "chatId")); + } + } + 4 => { + let have: Vec = live.iter().map(|r| r["chatId"].as_str().unwrap().to_owned()).collect(); + if let Some(id) = (0..n).find(|i| !have.contains(&format!("c{i:02}"))) { + let row = session_row(id, rnd(4), t); + live.push(row.clone()); + let changes = Changes { changed: vec![row], removed: vec![], reordered: false }; + s.heard_list(Slot::Live, &Frame::for_tests(&Json::Array(live.clone()), Some(&changes), "chatId")); + } + } + // A chat's entry moves (a new message). + _ => { + let i = rnd(n as u64) as usize; + let row = chat_row(i, t); + chats[i] = row.clone(); + let changes = Changes { changed: vec![row], removed: vec![], reordered: false }; + s.heard_list(Slot::Chats, &Frame::for_tests(&Json::Array(chats.clone()), Some(&changes), "id")); + } + } + let fresh = Sessions::default(); + *fresh.here.borrow_mut() = Some("here".into()); + fresh.heard_list(Slot::Devices, &Frame::for_tests(&devices, None, "id")); + fresh.heard_list(Slot::Chats, &Frame::for_tests(&Json::Array(chats.clone()), None, "id")); + fresh.heard_list(Slot::Live, &Frame::for_tests(&Json::Array(live.clone()), None, "chatId")); + assert_eq!(shown(&s.value_at(now)), shown(&fresh.value_at(now)), "step {step}"); + } + } + + /// Two chats of the person's on one machine (an older host), alike but + /// that one waits on (or works with) three background agents of its + /// own: both are listed, each with its state. (The live rows as the + /// dev engine sent them, times moved to now.) + #[test] + fn a_chat_running_subagents_is_listed_as_any() { + let now = now_seconds(); + let s = Sessions::default(); + s.heard.set(true); + *s.here.borrow_mut() = Some("here".into()); + let entry = |id: &str, title: &str| json!({ + "id": id, "deviceId": "e2f70bf5", "title": title, "archived": false, + "cwd": "/w", "branch": "main", "config": { "harness": "claude-code", "model": "claude-opus-5-5", "sandbox": "workspace-write" }, + "lastMessagePreview": "…", "lastMessageAt": iso(now - 5), "createdAt": iso(now - 20000), + "harnessSessionId": "h", "harnessSessionCwd": "/w", "harnessSessionHarness": "claude-code", + "spaceId": "space", "lastSeenAt": iso(now - 2000), "roomGen": 3 }); + s.set_chats(json!([entry("capy", "Explaining Capy.ai Captains"), entry("graph", "Redesign Map Graph Mode")])); + *s.spaces.borrow_mut() = json!([{ "id": "space", "name": "zeron", "deviceId": "e2f70bf5", "path": "/w" }]); + let waiting = json!([ + { "lastCompletedTurn": "t1", "chatId": "capy", "deviceId": "e2f70bf5", "status": "idle", "startedAt": null, + "updatedAt": iso(now - 1), "runningSubagents": 3, + "waiting": { "since": iso(now - 1), "on": [ + { "id": "a1", "kind": "agent", "label": "Engine: reports flow + MCP tools" }, + { "id": "a2", "kind": "agent", "label": "Control room v2 polish" }, + { "id": "a3", "kind": "agent", "label": "Map v2: three variants" } ] } }, + { "lastCompletedTurn": "t2", "chatId": "graph", "deviceId": "e2f70bf5", "status": "working", + "startedAt": iso(now - 200), "updatedAt": iso(now - 3), "runningSubagents": 0 }, + ]); + s.set_live(waiting); + s.publish(); + let v = s.value(); + let ids: Vec = v.field("chats").unwrap().list().iter().filter_map(|c| c.field("id").map(crate::auth::text)).collect(); + assert!(ids.contains(&"capy".to_string()), "listed: {ids:?}"); + assert!(ids.contains(&"graph".to_string())); + let status = |id: &str| chat(&v, id).field("status").map(crate::auth::text); + assert_eq!(status("capy").as_deref(), Some("waiting")); + assert_eq!(status("graph").as_deref(), Some("working")); + // Working itself, with its agents: working. + let working = json!([ + { "lastCompletedTurn": "t1", "chatId": "capy", "deviceId": "e2f70bf5", "status": "working", "startedAt": iso(now - 300), + "updatedAt": iso(now - 1), "runningSubagents": 3 }, + { "lastCompletedTurn": "t2", "chatId": "graph", "deviceId": "e2f70bf5", "status": "working", + "startedAt": iso(now - 200), "updatedAt": iso(now - 3), "runningSubagents": 0 }, + ]); + s.heard(Slot::Live, working.clone()); + let v = s.value(); + let ids: Vec = v.field("chats").unwrap().list().iter().filter_map(|c| c.field("id").map(crate::auth::text)).collect(); + assert!(ids.contains(&"capy".to_string()), "listed: {ids:?}"); + assert_eq!(chat(&v, "capy").field("status").map(crate::auth::text).as_deref(), Some("working")); + // As the engine streams it: one row's change at a time, back and forth. + use crate::engine::lists::{Changes, Frame}; + let mut list = working; + for (i, st) in ["idle", "working", "idle", "working"].iter().enumerate() { + let mut row = list[0].clone(); + row["status"] = json!(st); + row["updatedAt"] = json!(iso(now - 1 + i as i64)); + if *st == "idle" { + row["startedAt"] = Json::Null; + row["waiting"] = json!({ "since": iso(now - 1), "on": [{ "id": "a1", "kind": "agent", "label": "x" }] }); + } else { + row["startedAt"] = json!(iso(now - 300)); + row.as_object_mut().unwrap().remove("waiting"); + } + list[0] = row.clone(); + let changes = Changes { changed: vec![row], removed: vec![], reordered: false }; + s.heard_list(Slot::Live, &Frame::for_tests(&list, Some(&changes), "chatId")); + let v = s.value(); + let want = if *st == "idle" { "waiting" } else { "working" }; + assert_eq!(chat(&v, "capy").field("status").map(crate::auth::text).as_deref(), Some(want), "step {i}"); + } + } + + /// A thread on another machine whose host writes no activity (an older + /// release): working, then its turn over, told as a change to its row + /// alone (`startedAt` null, the outcome completed). Its row says so at + /// once, every list the scripts read (the map's, the sidebar's). + #[test] + fn a_remote_thread_s_turn_ending_shows_at_once() { + use crate::engine::lists::{Changes, Frame}; + let now = now_seconds(); + let s = Sessions::default(); + *s.here.borrow_mut() = Some("here".into()); + let chats = json!([ + { "id": "brain", "title": "Brain dump", "deviceId": "here", "createdAt": iso(now - 7200), + "lastMessageAt": iso(now - 30), "lastSeenAt": iso(now - 600) }, + { "id": "th", "title": "Verify Unique Device Names", "deviceId": "remote", "createdAt": iso(now - 900), + "lastMessageAt": iso(now - 30), "lastSeenAt": iso(now - 600) }, + ]); + s.heard_list(Slot::Chats, &Frame::for_tests(&chats, None, "id")); + let devices = json!([ + { "id": "here", "name": "desk", "lastSeenAt": iso(now - 1) }, + { "id": "remote", "name": "honeypot", "lastSeenAt": iso(now - 1) }, + ]); + s.heard_list(Slot::Devices, &Frame::for_tests(&devices, None, "id")); + let working = json!([ + { "chatId": "brain", "deviceId": "here", "status": "working", "startedAt": iso(now - 60), "updatedAt": iso(now - 2), "runningSubagents": 0 }, + { "chatId": "th", "deviceId": "remote", "status": "working", "startedAt": iso(now - 60), "updatedAt": iso(now - 2), "runningSubagents": 0 }, + ]); + s.heard_list(Slot::Live, &Frame::for_tests(&working, None, "chatId")); + let status = |v: &zgx_script::Value, id: &str| chat(v, id).field("status").map(crate::auth::text); + let v = s.value(); + assert_eq!(status(&v, "th").as_deref(), Some("working")); + // Its turn ends: one row changes. + let done = |id: &str, dev: &str| json!({ "chatId": id, "deviceId": dev, "status": "idle", "startedAt": null, "updatedAt": iso(now - 1), + "lastTurnOutcome": { "status": "completed", "finishedAt": iso(now - 1) }, "lastCompletedTurn": "t1", "runningSubagents": 0 }); + let after = json!([done("brain", "here"), done("th", "remote")]); + let changes = Changes { changed: vec![done("th", "remote"), done("brain", "here")], removed: vec![], reordered: false }; + s.heard_list(Slot::Live, &Frame::for_tests(&after, Some(&changes), "chatId")); + let v = s.value(); + assert_ne!(status(&v, "th").as_deref(), Some("working")); + assert_ne!(status(&v, "brain").as_deref(), Some("working")); + } + + #[test] + fn a_live_chat_shows_its_activity_and_todo() { + let now = now_seconds(); + let s = Sessions::default(); + s.heard.set(true); + s.set_chats(json!([ + { "id": "orc", "title": "Orchestrators", "deviceId": "d", "createdAt": iso(now - 7200) }, + { "id": "th", "title": "Engine state", "parentChatId": "orc", "childKind": "crew", "deviceId": "d", "createdAt": iso(now - 5400) }, + { "id": "idle", "title": "Idle", "deviceId": "d", "createdAt": iso(now - 600) }, + ])); + let live = |label: &str| { + json!([ + { "chatId": "orc", "deviceId": "d", "status": "working", "updatedAt": iso(now - 1), + "activity": { "kind": "delegating", "label": "send_message", "since": iso(now - 20) } }, + { "chatId": "th", "deviceId": "d", "status": "working", "updatedAt": iso(now - 1), + "activity": { "kind": "testing", "label": label, "since": iso(now - 48) }, + "todo": { "done": 5, "total": 8, "current": "WatchOrchestration stream" } }, + // Not working: its last activity isn't shown, its todo is. + { "chatId": "idle", "deviceId": "d", "status": "idle", "updatedAt": iso(now - 1), + "activity": { "kind": "editing", "label": "Edit x.rs", "since": iso(now - 300) }, + "todo": { "done": 3, "total": 3 } }, + ]) + }; + s.set_live(live("cargo test -p zeron-engine")); + s.publish(); + let v = s.value(); + let text = |v: Option<&zgx_script::Value>| v.map(crate::auth::text); + let none = |v: Option<&zgx_script::Value>| v.is_none_or(|v| matches!(v, zgx_script::Value::Nil)); + let orc = chat(&v, "orc"); + assert_eq!(text(orc.field("activity").and_then(|a| a.field("kind"))).as_deref(), Some("delegating")); + assert!(none(orc.field("todo"))); + assert!(none(orc.field("parent"))); + // The crew thread, under its orchestrator. + let th = v.field("children").unwrap().field("orc").unwrap().list()[0].clone(); + assert_eq!(text(th.field("child_kind")).as_deref(), Some("crew")); + let activity = th.field("activity").unwrap(); + assert_eq!(text(activity.field("kind")).as_deref(), Some("testing")); + assert_eq!(text(activity.field("label")).as_deref(), Some("cargo test -p zeron-engine")); + assert_eq!(activity.field("since").and_then(|n| n.as_f64()), Some((now - 48) as f64)); + let todo = th.field("todo").unwrap(); + assert_eq!(todo.field("done").and_then(|n| n.as_f64()), Some(5.)); + assert_eq!(todo.field("total").and_then(|n| n.as_f64()), Some(8.)); + assert_eq!(text(todo.field("current")).as_deref(), Some("WatchOrchestration stream")); + let idle = chat(&v, "idle"); + assert!(none(idle.field("activity"))); + assert_eq!(idle.field("todo").and_then(|t| t.field("total")).and_then(|n| n.as_f64()), Some(3.)); + assert!(none(idle.field("todo").unwrap().field("current"))); + // Its activity moves on, nothing else does: published, its row new. + let before = s.published.get(); + s.heard(Slot::Live, live("cargo test -p zeron-engine --lib orchestration")); + assert_eq!(s.published.get(), before + 1); + let v = s.value(); + let th = v.field("children").unwrap().field("orc").unwrap().list()[0].clone(); + assert_eq!( + text(th.field("activity").unwrap().field("label")).as_deref(), + Some("cargo test -p zeron-engine --lib orchestration") + ); + } + #[test] fn a_chat_s_row_is_built_again_only_as_it_changes() { let s = with_children("2099-01-01T00:00:00Z"); @@ -3091,3 +3594,17 @@ mod bench { } } } + +#[cfg(test)] +mod started_by_tests { + use super::*; + + #[test] + fn who_started_a_chat_reads_snake_case() { + let v = started_by_value(&json!({ "chatId": "orc-brain", "dumpId": "dp", "routeId": "rt" })); + assert_eq!(v.field("chat_id").map(crate::auth::text).as_deref(), Some("orc-brain")); + assert_eq!(v.field("route_id").map(crate::auth::text).as_deref(), Some("rt")); + assert!(matches!(started_by_value(&json!({ "chatId": "x" })).field("dump_id"), None | Some(Value::Nil))); + assert_eq!(started_by_value(&Json::Null), Value::Nil); + } +} diff --git a/apps/zgx/ui/account/orchestration.luau b/apps/zgx/ui/account/orchestration.luau new file mode 100644 index 000000000..27618c1da --- /dev/null +++ b/apps/zgx/ui/account/orchestration.luau @@ -0,0 +1,513 @@ +-- Orchestration as the host keeps it (src/orchestration.rs): orchestrators, +-- their crews, the board, what's asked of you and the Brain dump's dumps +-- (`WatchOrchestration`), what passes between agents (`WatchComms`) and how +-- busy each device is (`WatchDeviceLoads`, only while something shows it). +-- The shape is docs/design/orchestrators.md's "Bridge"; +-- account/orchestration_sample.luau is a whole one. +-- +-- orchestration.now:get() { state = "loading" | "ready", orchestrators, +-- crew, items, asks, dumps } +-- orchestration.loads:get() { [device id] = { cpu?, mem_used?, mem_total?, +-- load1?, cores?, online, at } } +-- orchestration.comms:get() { { id, kind, from, to, summary, at } }, +-- newest last, at most 100 +-- orchestration.loads_wanted(on) a screen showing loads starts (true) or +-- stops (false) wanting them; the host +-- samples only while one does +-- orchestration.op(op) one `Orchestrate` op, as the wire has it +-- (`{ op = "dump", text }`): a promise of its +-- reply +-- orchestration.dump(text) the Brain dump takes it: a promise of its id +-- orchestration.answer(id, { option?, text? }, ask?) an ask answered +-- (option 1-based); orchestration.answer_ask +-- on the host, else the `answerAsk` op in +-- plain words (`ask`, or its option's label, +-- gives them); fails after TIMEOUT_MS +-- orchestration.defer(id, until_ms?) an ask put off (nil: back now); +-- orchestration.defer_ask on the host, else +-- the `deferAsk` op +-- orchestration.reason(err) a failure's words +-- orchestration.dismiss(id) an ask put away +-- orchestration.reroute(dump, route, target) a route moved (or an unsure +-- one placed): `target` as a route's +-- (`{ kind = "orchestrator", id }`) +-- orchestration.move_item(id, state, order?) a card moved on the board +-- orchestration.approve_item(id) a card in Review approved +-- orchestration.request_changes(id, text) changes asked for on one +-- orchestration.set_priority(id, priority) +-- orchestration.settings:get() the profile's settings, synced and read by +-- every engine: { crew_auto_archive, +-- crew_idle_hours } (defaults until the +-- engine says: on, 4) +-- orchestration.auto_archive a state for a Switch: crew threads left +-- idle archived (writes `updateSettings`) +-- orchestration.idle_hours a state for a Select: how long idle first +-- orchestration.IDLE_HOURS the windows offered (hours) +-- +-- Chats come from `sessions.now` (account/sessions); their rows carry +-- `activity = { kind, label, since }` and `todo = { done, total, current? }`. +-- +-- How far each chat's work has got (`WatchProgress`, +-- src/orchestration_progress.rs), read and never asked of the agent: +-- +-- orchestration.progress:get() { [chat id] = { state, device_id, started?, +-- last_step?, step?, steps? = { done, total }, +-- note?, note_at?, stalled, tool?, +-- tool_since?, refreshing, eta_secs?, +-- updated } } +-- orchestration.check(chat) its note made fresh now: a promise of its +-- row (`refreshing` while the note is on its +-- way) + +local M = {} + +M.now = ui.shared("orchestration", { + state = "loading", + orchestrators = {}, + crew = {}, + items = {}, + asks = {}, + dumps = {}, +} :: any) +M.loads = ui.shared("orchestration.loads", {} :: { [string]: any }) +M.comms = ui.shared("orchestration.comms", {} :: { any }) + +-- Its parts, each followed on its own. +M.orchestrators = ui.computed(function(): { any } + return M.now:get().orchestrators or {} +end) +M.dumps = ui.computed(function(): { any } + return M.now:get().dumps or {} +end) +M.asks = ui.computed(function(): { any } + return M.now:get().asks or {} +end) + +function M.op(op: { [string]: any }): any + return ui.call("orchestration.op", op) +end + +-- The profile's settings (src/orchestration.rs `settings`). What was just +-- asked for shows at once, until the engine's state says it (or the ask +-- fails). +local DEFAULT_SETTINGS = { crew_auto_archive = true, crew_idle_hours = 4 } +local asked = ui.state("orchestration_settings_asked", {} :: { [string]: any }) +M.settings = ui.computed(function(): { crew_auto_archive: boolean, crew_idle_hours: number } + local s = M.now:get().settings or DEFAULT_SETTINGS + local a = asked:get() + local out = table.clone(s) + for k, v in a do + out[k] = v + end + return out :: any +end) +-- (What the engine now says puts an ask to rest.) +ui.effect(function() + local s = M.now:get().settings + local a = asked:peek() + if not s then + return + end + local left, settled = {}, false + for k, v in a do + if s[k] == v then + settled = true + else + left[k] = v + end + end + if settled then + asked:set(left) + end +end) + +M.IDLE_HOURS = { 1, 4, 6, 12, 24, 72, 168 } + +-- `key` (snake_case) set to `value` through `updateSettings`. +local function set_setting(key: string, value: any) + local a = table.clone(asked:peek()) + a[key] = value + asked:set(a) + M.op({ op = "updateSettings", [M.camel(key)] = value }):after(function() end, function() + local back = table.clone(asked:peek()) + back[key] = nil + asked:set(back) + end) +end + +-- A state for `key` (a Switch's, a Select's): what the settings say, and a +-- change of the person's sent to the engine. (Each follows the other: what +-- the engine says is set here; what's set here and isn't what it says is +-- asked of it.) +local function setting_state(key: string): any + local st = ui.state(`orchestration_setting_{key}`, (DEFAULT_SETTINGS :: any)[key]) + ui.effect(function() + local v = (M.settings:get() :: any)[key] + if st:peek() ~= v then + st:set(v) + end + end) + ui.effect(function() + local v = st:get() + if (M.settings:peek() :: any)[key] ~= v then + set_setting(key, v) + end + end) + return st +end +M.auto_archive = setting_state("crew_auto_archive") +M.idle_hours = setting_state("crew_idle_hours") + +-- Wanting loads, counted by the host (several screens may show them). +function M.loads_wanted(on: boolean) + ui.invoke("orchestration.loads_wanted", on) +end + +-- A dump: its words, and the files they carry (as the chat composer packs +-- a message's: `{ kind, path, name, on_host }`), sent on toward the Brain +-- dump's chat (`chat`, its id) as a message's are (src/orchestration.rs); +-- `picks`, the composer's (as a chat send's), become the op's `config`. +-- `refs`: what in the Control room it references (ds/orch_ref, dragged onto +-- Capture: `{ kind, id, label, context? }`), passed on to the engine as is. +function M.dump(text: string, attachments: { any }?, chat: string?, picks: { [string]: any }?, refs: { any }?): any + local op: { [string]: any } = { op = "dump", text = text } + op.chat = chat + if attachments and #attachments > 0 then + op.attachments = attachments + end + if refs and #refs > 0 then + local list = {} + for _, r in refs do + table.insert(list, { kind = r.kind, id = r.id, label = r.label or "", context = r.context }) + end + op.refs = list + end + -- The Brain dump's config as the composer has it picked (its model, + -- agent, effort, Fast and options), applied to its chat before the run: + -- the chat send's shape, made the engine's by the bridge. + if picks and picks.model then + op.picks = picks + end + return M.op(op) +end + +-- A host action called (`ui.call`): kept apart so the fixtures can stand +-- in for the host (one without an action, one whose engine refuses). +function M.host(name: string, arg: any): any + return ui.call(name, arg) +end + +-- How long an answer or a defer waits on the engine before it's said to +-- have failed. +M.TIMEOUT_MS = 15000 + +-- What went wrong, in words: a promise's error (a `UiError`'s `message`), +-- or what a call raised, its script position gone. +function M.reason(err: any): string + if type(err) == "string" then + return (string.gsub(err, "^.-:%d+: ", "")) + end + if type(err) == "table" or type(err) == "userdata" then + local ok, message = pcall(function(): any + return (err :: any).message + end) + if ok and type(message) == "string" then + return message + end + end + return tostring(err) +end + +-- Whether a call raised because the host has no such action (an app built +-- before it). +local function missing(err: any): boolean + return string.find(M.reason(err), "no host action named", 1, true) ~= nil +end + +-- A promise of our own (its `:after` as `ui.call`'s), settled once: by the +-- engine's reply, its error, or the timeout (`{ kind = "timeout" }`). +local function promise(): any + local p: any = { done = false } + type Waiter = { on_value: ((any) -> ())?, on_error: ((any) -> ())? } + local waiting: { Waiter } = {} + local stop: (() -> ())? = nil + local function settle(ok: boolean, v: any) + if p.done then + return + end + p.done, p.ok, p.value = true, ok, v + p.error = if ok then nil else v + local cancel = stop + stop = nil + if cancel then + cancel() + end + for _, w in waiting do + local f = if ok then w.on_value else w.on_error + if f then + f(v) + end + end + table.clear(waiting) + end + function p.after(self: any, on_value: ((any) -> ())?, on_error: ((any) -> ())?): any + if self.done then + local f = if self.ok then on_value else on_error + if f then + f(self.value) + end + else + table.insert(waiting, { on_value = on_value, on_error = on_error }) + end + return self + end + p.resolve = function(v: any) + settle(true, v) + end + p.reject = function(e: any) + settle(false, e) + end + stop = ui.after(M.TIMEOUT_MS, function() + stop = nil + settle(false, { kind = "timeout", message = "timeout" }) + end) + return p +end + +-- `call`'s promise, or (nil and) what it raised. +local function attempt(call: () -> any): (any?, any?) + local ok, p = pcall(call) + if ok then + return p, nil + end + return nil, p +end + +-- An answer as plain words, for an engine that takes only words: the +-- option's label, and " — " when there are words too. `about` is +-- the ask (its options give the label) or the label itself. +function M.plain_answer(about: any, o: { option: number?, text: string? }): string + local label: string? = nil + if type(about) == "string" then + label = about + elseif type(about) == "table" and o.option then + local opt = (about.options or {})[o.option] + label = opt and opt.label + end + if label == "" then + label = nil + end + local words = if o.text then string.match(o.text, "^%s*(.-)%s*$") or "" else "" + if label and words ~= "" then + return `{label} — {words}` + end + return label or words +end + +-- Whether the engine turned the option down (one from before options: an +-- unknown field, or an empty answer when only the option was sent). +local function refuses_option(err: any): boolean + local r = string.lower(M.reason(err)) + return string.find(r, "option", 1, true) ~= nil or string.find(r, "empty", 1, true) ~= nil +end + +-- An ask answered: an option of it (1-based, as Luau counts), words of +-- the person's, or both (one of them, at least). Needs you, a board card, +-- the decision overlay and the floating composer all answer through here. +-- (A plain string is taken as the words.) `about`, the ask or its +-- option's label, gives the plain words an older engine takes instead. A +-- promise of the engine's `{ id }`, rejected with what went wrong (or +-- `{ kind = "timeout" }` past TIMEOUT_MS). +-- +-- An app without `orchestration.answer_ask` sends the `answerAsk` op, the +-- answer as plain words; an engine that refuses the option (or says the +-- answer is empty) is asked once more, with the plain words. +function M.answer(ask_id: string, opts: { option: number?, text: string? } | string, about: any?): any + local o: { option: number?, text: string? } = if type(opts) == "string" then { text = opts } else opts + local plain = M.plain_answer(about, o) + local out = promise() + local function by_op(err: any) + if plain == "" then + out.reject(err) + return + end + local p, raised = attempt(function(): any + return M.op({ op = "answerAsk", id = ask_id, answer = plain }) + end) + if not p then + out.reject(raised) + return + end + p:after(out.resolve, out.reject) + end + local function by_host(call: { [string]: any }, again: boolean) + local p, raised = attempt(function(): any + return M.host("orchestration.answer_ask", call) + end) + if not p then + if missing(raised) then + by_op(raised) + else + out.reject(raised) + end + return + end + p:after(out.resolve, function(e: any) + if again and call.option and plain ~= "" and refuses_option(e) then + by_host({ id = ask_id, text = plain }, false) + else + out.reject(e) + end + end) + end + local call: { [string]: any } = { id = ask_id } + call.option = o.option + if o.text and o.text ~= "" then + call.text = o.text + end + by_host(call, true) + return out +end + +-- An ask put off until `until_ms` (Unix milliseconds): out of Needs you, +-- "Deferred" on its card, back (and said again) when the time comes. No +-- time: back now. A promise of the engine's `{ id }`, rejected as +-- `answer`'s is. An app without `orchestration.defer_ask` sends the +-- `deferAsk` op; an engine without that op says so, and that's the error. +function M.defer(ask_id: string, until_ms: number?): any + local secs = if until_ms then math.floor(until_ms / 1000) else nil + local out = promise() + local call: { [string]: any } = { id = ask_id } + call["until"] = secs + local p, raised = attempt(function(): any + return M.host("orchestration.defer_ask", call) + end) + if not p and missing(raised) then + local op: { [string]: any } = { op = "deferAsk", id = ask_id } + op["until"] = if secs then os.date("!%Y-%m-%dT%H:%M:%SZ", secs) else nil + p, raised = attempt(function(): any + return M.op(op) + end) + end + if not p then + out.reject(raised) + else + p:after(out.resolve, out.reject) + end + return out +end + +function M.dismiss(ask_id: string): any + return M.op({ op = "dismissAsk", id = ask_id }) +end + +-- A snake_case word as the wire spells it (`new_thread` → `newThread`). +local function camel(word: string): string + return (string.gsub(word, "_(%l)", string.upper)) +end +M.camel = camel + +-- (A new thread or orchestrator not made yet goes with an empty id: the +-- wire's `newThread`/`newOrchestrator` carry one, and the engine makes it.) +local NEEDS_ID: { [string]: boolean } = { orchestrator = true, chat = true, new_orchestrator = true, new_thread = true, item = true } +function M.reroute(dump_id: string, route_id: string, target: { kind: string, id: string? }): any + local id = target.id + if id == nil and NEEDS_ID[target.kind] then + id = "" + end + return M.op({ op = "reroute", dumpId = dump_id, routeId = route_id, target = { kind = camel(target.kind), id = id } }) +end + +function M.move_item(id: string, state: string, order: number?): any + return M.op({ op = "upsertItem", item = { id = id, state = state, order = order } }) +end + +-- A card in Review approved: Done, its approval recorded. A promise. +function M.approve_item(id: string): any + return M.op({ op = "approveItem", id = id }) +end + +-- Changes asked for on a card in Review: `text` to its thread, the card +-- back in Now. A promise. +function M.request_changes(id: string, text: string): any + return M.op({ op = "requestChanges", id = id, text = text }) +end + +function M.set_priority(id: string, priority: string): any + return M.op({ op = "upsertItem", item = { id = id, priority = priority } }) +end + +-- A chat put on the board: its card (the one it has when one isn't done +-- yet, else a new one). A promise of the card's `{ id }`. +function M.add_to_board(chat_id: string): any + return M.op({ op = "addToBoard", chatId = chat_id }) +end + +-- An Inbox or Next card started: its thread made (in its project), the +-- card in Now; at `order` when given (where it was dropped: the person +-- moving a card with no thread to Now starts it). A promise. +function M.start_item(id: string, order: number?): any + if order then + return M.op({ op = "upsertItem", item = { id = id, state = "active", order = order } }) + end + return M.op({ op = "startItem", id = id }) +end + +-- A card taken off the board (kept, marked removed; its chat untouched). +function M.remove_item(id: string): any + return M.op({ op = "removeItem", id = id }) +end + +-- A card taken off the board, put back as it was: made again from its +-- fields (the engine brings none back), a new card. A promise of `{ id }`. +function M.restore_item(item: any): any + local src = item.source + local links: { any } = {} + for _, l in item.links or {} do + table.insert(links, { kind = l.kind, label = l.label, target = l.target }) + end + return M.op({ + op = "upsertItem", + item = { + orchestratorId = item.orchestrator_id, + title = item.title, + detail = item.detail or "", + state = item.state, + priority = item.priority, + progress = item.progress, + threads = item.threads or {}, + links = links, + outcome = item.outcome, + source = if src and src.dump_id then { dumpId = src.dump_id, routeId = src.route_id or "" } else nil, + order = item.order, + }, + }) +end + +-- Whether a chat has a card on the board not done yet. +function M.on_board(chat_id: string): boolean + for _, it in M.now:peek().items or {} do + if it.state ~= "done" then + for _, t in it.threads or {} do + if t == chat_id then + return true + end + end + end + end + return false +end + +-- A dump asked to be shown (its id): the Control room brings its receipt +-- into view, opened, and clears it. Set by a board card's source, from any +-- page (the page goes to the Control room as it sets it). +M.reveal_dump = ui.state("orchestration_reveal_dump", nil :: string?) + +-- How far each chat's work has got, by chat id. +M.progress = ui.shared("orchestration.progress", {} :: { [string]: any }) + +function M.check(chat_id: string): any + return ui.call("orchestration.progress_check", chat_id) +end + +return M diff --git a/apps/zgx/ui/account/orchestration_sample.luau b/apps/zgx/ui/account/orchestration_sample.luau new file mode 100644 index 000000000..3a7b221b2 --- /dev/null +++ b/apps/zgx/ui/account/orchestration_sample.luau @@ -0,0 +1,800 @@ +-- A complete orchestration state in the shape `orchestration.now` publishes +-- (docs/design/orchestrators.md, "Bridge"), plus the chats, devices, loads +-- and comms it refers to. Fixtures, galleries and audit shots read this one +-- table so every screen is drawn against the same world. +-- +-- local sample = require("@app/account/orchestration_sample") +-- sample.now what orchestration.now:get() returns +-- sample.loads what orchestration.loads:get() returns +-- sample.comms what orchestration.comms:get() returns +-- sample.devices sessions.now devices to pair with it +-- sample.chats sessions.now chats (crew threads carry activity/todo) +-- sample.scratchpad what scratchpad.now:get() returns (account/scratchpad) +-- sample.at(t) a copy with every time shifted so "now" is `t` + +local NOW = 1794470400 -- 2026-11-12 18:40 PST + +local function ago(s) + return NOW - s +end + +local devices = { + { id = "dev-desk", name = "desk", platform = "linux", here = true, online = true }, + { id = "dev-mac", name = "macbook", platform = "macos", here = false, online = true }, + { id = "dev-cloud", name = "Cloud", platform = "linux", here = false, online = true, cloud = true }, +} + +local orchestrators = { + { + id = "orc-brain", + kind = "brain_dump", + name = "Brain dump", + goal = "Route everything I say to the right place.", + rules = "", + crew_rules = "", + notes = "- Ask Wing about the relay deploy window\n- Look at zgx memory after the tabs refactor", + hue = 262, + crew = { devices = {} }, + paused = false, + archived = false, + created = ago(86400 * 6), + updated = ago(40), + }, + { + id = "orc-orch", + kind = "standard", + name = "Orchestrators", + goal = "Ship Orchestrators and the Control room in zgx.", + rules = "Opus for UI and engine design, Sonnet for plumbing. Ask before merging to zeron/zgx.", + crew_rules = "Own worktree per thread. cargo -j 4. Never touch the production engine.", + notes = "1. Contract (done)\n2. Engine state + flow\n3. Control room + orchestrator screens\n4. Integrate, live test", + hue = 196, + crew = { devices = { "dev-desk", "dev-mac" }, max_running = 4, model = "claude-opus-5-5" }, + paused = false, + archived = false, + created = ago(3600 * 2), + updated = ago(90), + }, + { + id = "orc-vpn", + kind = "standard", + name = "Exnomic VPN", + goal = "Phase 2 migration, safely.", + rules = "Nothing touches prod without asking.", + crew_rules = "", + notes = "Phase 1 watch-only on DE + Moscow. Moscow needs a reboot.", + hue = 32, + crew = { devices = { "dev-desk" } }, + wake = { at = { "09:00" }, weekdays_only = true, prompt = "Check node health and report." }, + paused = false, + archived = false, + created = ago(86400 * 3), + updated = ago(3600), + }, + { + id = "orc-ci", + kind = "standard", + name = "CI speed", + goal = "Every PR green in under 10 minutes.", + rules = "", + crew_rules = "", + notes = "", + hue = 128, + crew = { devices = { "dev-cloud" }, harness = "codex" }, + paused = true, + archived = false, + created = ago(86400 * 9), + updated = ago(86400), + }, +} + +-- Rows as the host gives them (src/sessions.rs): a side chat's or crew +-- thread's carries its own title as `name` too (its `title` falls back to +-- what it last said when it has none). +local function hosted(list: { any }): { any } + for _, c in list do + if c.parent and c.name == nil then + c.name = c.title + end + end + return list +end + +local chats = { + { id = "orc-orch", title = "Orchestrators", device = "dev-desk", status = "working", updated = ago(20), created = ago(7200), agent = "claude", model = "claude-opus-5-5" }, + { id = "orc-vpn", title = "Exnomic VPN", device = "dev-desk", updated = ago(3600), created = ago(259200), agent = "claude", model = "claude-sonnet-5-5" }, + { id = "orc-ci", title = "CI speed", device = "dev-cloud", updated = ago(86400), created = ago(777600), agent = "codex", model = "gpt-5.5" }, + { id = "orc-brain", title = "Brain dump", device = "dev-desk", updated = ago(40), created = ago(518400), agent = "claude", model = "claude-haiku-5-5" }, + { + id = "th-engine-state", title = "Engine: orchestration state", device = "dev-desk", status = "working", + parent = "orc-orch", child_kind = "crew", updated = ago(12), created = ago(5400), agent = "claude", model = "claude-opus-5-5", + activity = { kind = "testing", label = "cargo test -p zeron-engine --lib orchestration", since = ago(48) }, + todo = { done = 5, total = 8, current = "WatchOrchestration stream" }, + }, + { + id = "th-engine-flow", title = "Engine: reports + MCP tools", device = "dev-desk", status = "working", + parent = "orc-orch", child_kind = "crew", updated = ago(5), created = ago(5400), agent = "claude", model = "claude-opus-5-5", + activity = { kind = "editing", label = "Edit crates/mcp/src/tools.rs", since = ago(6) }, + todo = { done = 3, total = 9, current = "report tool" }, + }, + { + id = "th-ui-control", title = "Control room UI", device = "dev-mac", status = "working", + parent = "orc-orch", child_kind = "crew", updated = ago(30), created = ago(5300), agent = "claude", model = "claude-opus-5-5", + activity = { kind = "building", label = "cargo build --release", since = ago(140) }, + todo = { done = 2, total = 7, current = "Map widget" }, + }, + { + id = "th-ui-orch", title = "Orchestrator screen", device = "dev-mac", status = "input", + parent = "orc-orch", child_kind = "crew", updated = ago(200), created = ago(5300), agent = "claude", model = "claude-opus-5-5", + todo = { done = 4, total = 6, current = "Crew updates card" }, + }, + { + id = "th-vpn-moscow", title = "Moscow node reboot plan", device = "dev-desk", + parent = "orc-vpn", child_kind = "crew", updated = ago(3500), created = ago(7000), agent = "claude", model = "claude-sonnet-5-5", + todo = { done = 3, total = 3 }, + }, + { + id = "th-vpn-merge", title = "Merge phase-1 fixes", device = "dev-desk", status = "working", + parent = "orc-vpn", child_kind = "crew", updated = ago(8), created = ago(1800), agent = "claude", model = "claude-sonnet-5-5", + activity = { kind = "merging", label = "gh pr merge 412 --squash", since = ago(9) }, + todo = { done = 6, total = 7, current = "Merge #412" }, + }, + { + id = "th-ci-cache", title = "sccache on the iOS lane", device = "dev-cloud", status = "failed", + parent = "orc-ci", child_kind = "crew", updated = ago(80000), created = ago(100000), agent = "codex", model = "gpt-5.5", + }, + { id = "th-titlebar", title = "Linux titlebar toggle", device = "dev-desk", status = "done", updated = ago(26000), created = ago(30000), agent = "claude", model = "claude-opus-5-5" }, +} + +local crew = { + { chat_id = "th-engine-state", orchestrator_id = "orc-orch", item_id = "it-engine", role = "engine state", released = false, joined = ago(5400), + last_report = { id = "rp-1", state = "progress", summary = "Registry kinds + WatchOrchestration in; ops next", at = ago(600) } }, + { chat_id = "th-engine-flow", orchestrator_id = "orc-orch", item_id = "it-flow", role = "reports + tools", released = false, joined = ago(5400), + last_report = { id = "rp-2", state = "progress", summary = "CrewReports part renders; coalesced wake works in tests", at = ago(300) } }, + { chat_id = "th-ui-control", orchestrator_id = "orc-orch", item_id = "it-control", role = "control room", released = false, joined = ago(5300) }, + { chat_id = "th-ui-orch", orchestrator_id = "orc-orch", item_id = "it-orch-screen", role = "orchestrator screen", released = false, joined = ago(5300), + last_report = { id = "rp-3", state = "blocked", summary = "Needs a decision: tabs or split for the orchestrator page", at = ago(200) } }, + { chat_id = "th-vpn-moscow", orchestrator_id = "orc-vpn", item_id = "it-moscow", role = "reboot plan", released = false, joined = ago(7000), + last_report = { id = "rp-4", state = "done", summary = "Reboot plan ready; needs your OK for 02:00 MSK", at = ago(3500) } }, + { chat_id = "th-vpn-merge", orchestrator_id = "orc-vpn", item_id = "it-phase1", role = "merge", released = false, joined = ago(1800) }, + { chat_id = "th-ci-cache", orchestrator_id = "orc-ci", item_id = "it-sccache", role = "sccache", released = false, joined = ago(100000), + last_report = { id = "rp-5", state = "failed", summary = "sccache can't reach the bucket from macOS runners", at = ago(90000) } }, +} + +local items = { + { id = "it-engine", orchestrator_id = "orc-orch", title = "Engine: orchestration state + RPC", detail = "", state = "active", priority = "high", + threads = { "th-engine-state" }, links = { { kind = "branch", label = "orch/engine-state", target = "orch/engine-state" } }, order = 1, created = ago(5400), updated = ago(600) }, + { id = "it-flow", orchestrator_id = "orc-orch", title = "Reports, asks, MCP tools", detail = "", state = "active", priority = "high", + threads = { "th-engine-flow" }, links = {}, order = 2, created = ago(5400), updated = ago(300) }, + { id = "it-control", orchestrator_id = "orc-orch", title = "Control room", detail = "", state = "active", priority = "urgent", + threads = { "th-ui-control" }, links = {}, order = 0, created = ago(5300), updated = ago(30) }, + { id = "it-orch-screen", orchestrator_id = "orc-orch", title = "Orchestrator screen", detail = "", state = "blocked", priority = "high", + threads = { "th-ui-orch" }, links = {}, order = 3, created = ago(5300), updated = ago(200) }, + { id = "it-live", orchestrator_id = "orc-orch", title = "Live end-to-end test on a dev engine", detail = "", state = "planned", priority = "normal", + threads = {}, links = {}, order = 4, created = ago(5300), updated = ago(5300) }, + { id = "it-moscow", orchestrator_id = "orc-vpn", title = "Reboot Moscow node", detail = "", state = "review", priority = "high", progress = 1, + threads = { "th-vpn-moscow" }, links = {}, order = 0, created = ago(7000), updated = ago(3500), + outcome = "Plan: drain at 01:45 MSK, reboot 02:00, verify wg + exporter by 02:10. Rollback: none needed." }, + { id = "it-phase1", orchestrator_id = "orc-vpn", title = "Merge phase-1 fixes", detail = "", state = "active", priority = "normal", + threads = { "th-vpn-merge" }, links = { { kind = "pr", label = "#412", target = "https://github.com/katulevskiy/exnomic/pull/412" } }, order = 1, created = ago(1800), updated = ago(8) }, + { id = "it-phase2", orchestrator_id = "orc-vpn", title = "Phase 2 migration", detail = "", state = "planned", priority = "low", + threads = {}, links = {}, order = 2, created = ago(200000), updated = ago(200000) }, + { id = "it-sccache", orchestrator_id = "orc-ci", title = "sccache for iOS", detail = "", state = "blocked", priority = "normal", + threads = { "th-ci-cache" }, links = {}, order = 0, created = ago(100000), updated = ago(90000) }, + { id = "it-titlebar", title = "Linux titlebar toggle", detail = "", state = "done", priority = "normal", progress = 1, + threads = { "th-titlebar" }, links = { { kind = "pr", label = "#1383", target = "https://github.com/zeronsh/zeron/pull/1383" } }, order = 0, + created = ago(30000), updated = ago(26000), done = ago(26000), outcome = "PR #1383 open: 88px → 10px when no window buttons." }, + { id = "it-omarchy", title = "Rerun omarchy-update (needs sudo)", detail = "", state = "inbox", priority = "normal", + threads = {}, links = {}, order = 0, created = ago(40), updated = ago(40), source = { dump_id = "dp-2", route_id = "rt-6" } }, +} + +local asks = { + { id = "ak-1", from_chat = "th-ui-orch", orchestrator_id = "orc-orch", item_id = "it-orch-screen", kind = "decision", + question = "Orchestrator page: conversation and live state side by side, or tabs?", + options = { { label = "Side by side", detail = "Chat left, crew + board right" }, { label = "Tabs", detail = "Chat · Board · Crew" } }, + urgent = false, state = "open", created = ago(200) }, + { id = "ak-2", from_chat = "orc-vpn", orchestrator_id = "orc-vpn", item_id = "it-moscow", kind = "approval", + question = "Reboot the Moscow node at **02:00 MSK** tonight?", + options = { { label = "Approve" }, { label = "Not tonight" } }, urgent = true, state = "open", created = ago(3400) }, + { id = "ak-3", from_chat = "th-ci-cache", orchestrator_id = "orc-ci", kind = "question", + question = "Which bucket should macOS runners use for sccache?", options = {}, urgent = false, state = "answered", + answer = "zeron-ci-cache-us", created = ago(95000), answered = ago(91000) }, +} + +local dumps = { + { + id = "dp-2", + attachments = { { path = "assets/favicons/zeron.png", name = "board.png" }, { path = "pending://up-1/notes.txt", name = "notes.txt" } }, + text = "ok so the orchestrator page should be side by side not tabs, also remind me to rerun omarchy update it needs sudo. and ask the vpn guy whether moscow rebooted fine. the CI thing can wait till next week, and is the engine state thread still on the registry part?", + state = "routed", + summary = "5 parts: 2 to Orchestrators, 1 to Exnomic VPN, 1 to CI speed, 1 to your inbox.", + device = "dev-desk", + created = ago(40), + routes = { + { id = "rt-5", excerpt = "the orchestrator page should be side by side not tabs", span = { from = 6, to = 59 }, + target = { kind = "chat", id = "th-ui-orch" }, intent = "info", message = "Decision from the person: side by side (chat left, crew + board right), not tabs.", state = "done", candidates = {}, + answer = "Switched to side by side; the tabs are gone.", answer_full = "Done: the orchestrator page is **side by side** now (chat left, crew and board right). Tabs removed in `ui/screens/orchestrator/page.luau`." }, + { id = "rt-6", excerpt = "remind me to rerun omarchy update it needs sudo", span = { from = 66, to = 113 }, + target = { kind = "item", id = "it-omarchy" }, intent = "task", message = "Rerun omarchy-update (needs sudo).", state = "delivered", candidates = {}, item_id = "it-omarchy" }, + { id = "rt-7", excerpt = "ask the vpn guy whether moscow rebooted fine", span = { from = 119, to = 163 }, + target = { kind = "orchestrator", id = "orc-vpn" }, intent = "question", direct = true, message = "Did the Moscow node reboot cleanly? Check wg and the exporter.", state = "answered", candidates = {}, answered = ago(25), + answer = "Not yet — scheduled for 02:00 MSK, waiting on your approval.", + answer_full = "Not yet. The reboot is **scheduled for 02:00 MSK** and waits on your approval (the ask in Needs you).\n\nOnce it runs I'll check:\n\n- `wg show` on the node: the peers back\n- the exporter's `/metrics`: scraping again\n\nI'll report either way." }, + { id = "rt-8", excerpt = "the CI thing can wait till next week", span = { from = 165, to = 201 }, + target = { kind = "orchestrator", id = "orc-ci" }, intent = "priority", message = "Deprioritise: nothing before next week.", state = "delivered", candidates = {} }, + { id = "rt-9", excerpt = "is the engine state thread still on the registry part?", span = { from = 207, to = 261 }, + target = { kind = "chat", id = "th-engine-state" }, intent = "question", direct = true, message = "Status check from the person: are you still on the registry layer?", state = "delivered", candidates = {} }, + }, + }, + { + id = "dp-1", + text = "make the board show PR checks and look at why zgx eats 2gb after the tabs refactor", + state = "needs_you", + summary = "1 part placed, 1 unsure.", + device = "dev-mac", + created = ago(7200), + routes = { + { id = "rt-1", excerpt = "make the board show PR checks", span = { from = 0, to = 29 }, + target = { kind = "orchestrator", id = "orc-orch" }, intent = "task", message = "Board cards should show PR check state (via the github plugin slot).", state = "delivered", candidates = {} }, + { id = "rt-2", excerpt = "look at why zgx eats 2gb after the tabs refactor", span = { from = 34, to = 82 }, + target = { kind = "unsure" }, intent = "task", message = "Investigate zgx memory (~2 GB) after the tabs refactor.", state = "pending", + candidates = { { kind = "orchestrator", id = "orc-orch" }, { kind = "new_thread" } } }, + }, + }, +} + +local loads = { + ["dev-desk"] = { cpu = 0.71, mem_used = 41.2e9, mem_total = 62.0e9, load1 = 11.4, cores = 16, online = true, at = NOW }, + ["dev-mac"] = { cpu = 0.38, mem_used = 22.1e9, mem_total = 36.0e9, load1 = 4.2, cores = 12, online = true, at = NOW }, + ["dev-cloud"] = { cpu = 0.04, mem_used = 1.1e9, mem_total = 8.0e9, load1 = 0.1, cores = 4, online = true, at = NOW }, +} + +local comms = { + { id = "cm-1", kind = "route", from = "orc-brain", to = "th-ui-orch", summary = "Side by side, not tabs", at = ago(38) }, + { id = "cm-2", kind = "route", from = "orc-brain", to = "orc-vpn", summary = "Did Moscow reboot cleanly?", at = ago(37) }, + { id = "cm-3", kind = "message", from = "orc-vpn", to = "you", summary = "Not yet — waiting on your approval", at = ago(30) }, + { id = "cm-4", kind = "report", from = "th-engine-flow", to = "orc-orch", summary = "Coalesced wake works in tests", at = ago(300) }, + { id = "cm-5", kind = "ask", from = "th-ui-orch", to = "you", summary = "Side by side or tabs?", at = ago(200) }, + { id = "cm-6", kind = "message", from = "orc-orch", to = "th-ui-control", summary = "Map first, then board", at = ago(150) }, + { id = "cm-7", kind = "spawn", from = "orc-vpn", to = "th-vpn-merge", summary = "Merge phase-1 fixes", at = ago(1800) }, +} + +-- Each device's CPU over the last six minutes, one sample every 5 s, as +-- the bridge keeps it from `WatchDeviceLoads` (`history`, oldest first): +-- a wander around where it stands now, with a burst or two. +local function history_of(cpu: number, seed: number): { { at: number, cpu: number } } + local out = {} + local n = 72 + for i = 1, n do + local t = i + seed * 13 + local wave = math.sin(t * 0.31) * 0.09 + math.sin(t * 1.13) * 0.05 + math.sin(t * 2.7) * 0.025 + local burst = if (i + seed * 7) % 29 < 5 then 0.18 else 0 + local v = cpu + wave + burst - 0.06 + (cpu - 0.3) * (i / n - 1) * 0.4 + table.insert(out, { at = NOW - (n - i) * 5, cpu = math.clamp(v, 0.02, 0.98) }) + end + out[n].cpu = cpu + return out +end +loads["dev-desk"].history = history_of(loads["dev-desk"].cpu, 1) +loads["dev-mac"].history = history_of(loads["dev-mac"].cpu, 2) +loads["dev-cloud"].history = history_of(loads["dev-cloud"].cpu, 3) + +-- More comms, for a feed with bursts in it. +for _, c in { + { id = "cm-8", kind = "report", from = "th-engine-state", to = "orc-orch", summary = "Registry kinds in; ops next", at = ago(600) }, + { id = "cm-9", kind = "message", from = "orc-orch", to = "th-engine-state", summary = "Good. Ops, then the stream", at = ago(590) }, + { id = "cm-10", kind = "report", from = "th-vpn-moscow", to = "orc-vpn", summary = "Reboot plan ready", at = ago(3500) }, + { id = "cm-11", kind = "ask", from = "orc-vpn", to = "you", summary = "Reboot Moscow at 02:00 MSK?", at = ago(3400) }, + { id = "cm-12", kind = "message", from = "orc-orch", to = "th-engine-flow", summary = "Coalesce wakes within 2 s", at = ago(5) }, + { id = "cm-13", kind = "report", from = "th-vpn-merge", to = "orc-vpn", summary = "Checks green, merging #412", at = ago(10) }, + { id = "cm-14", kind = "answer", from = "th-ui-orch", to = "you", summary = "Switched to side by side; the tabs are gone.", at = ago(12) }, +} do + table.insert(comms, c) +end +table.sort(comms, function(a: any, b: any): boolean + return a.at < b.at +end) + +local sample = { + NOW = NOW, + now = { + state = "ready", + orchestrators = orchestrators, + crew = crew, + items = items, + asks = asks, + dumps = dumps, + }, + loads = loads, + comms = comms, + devices = devices, + chats = hosted(chats), +} + +local TIME_KEYS = { + created = true, updated = true, done = true, joined = true, at = true, since = true, answered = true, + -- (A progress row's clocks.) + started = true, last_step = true, note_at = true, tool_since = true, + -- (The Scratchpad's.) + updated_at = true, edited_at = true, +} + +local function shifted(v, by) + if type(v) ~= "table" then + return v + end + local out = {} + for k, x in v do + -- (A todo's `done` is a count, not a time; so are a progress row's + -- steps.) + if k == "todo" or k == "steps" then + out[k] = x + elseif TIME_KEYS[k] and type(x) == "number" then + out[k] = x + by + else + out[k] = shifted(x, by) + end + end + return out +end + +function sample.at(t: number) + local copy = shifted(sample, t - NOW) + copy.NOW = t + copy.at = sample.at + copy.fleet_at = sample.fleet_at + return copy +end + +-- A busier world, for the Map's three views: four machines (this desk, a +-- macbook, a Windows box and a Cloud machine), four projects, five +-- orchestrators and their crews across them, one plain thread working on +-- its own, and the last hour of what each thread did +-- (`orchestration.timeline`). Shaped as the sample is, plus: +-- +-- sample.fleet.projects sessions.now projects +-- sample.fleet.timeline what orchestration.timeline:get() returns +-- sample.fleet_at(t) a copy with every time shifted so "now" is `t` +do + local function crew_chat(id: string, title: string, device: string, project: string, parent: string, status: string?, activity: any, todo: any, extra: any?): any + local c: any = { + id = id, title = title, device = device, project = project, parent = parent, child_kind = "crew", + status = status, updated = ago(30), created = ago(5400), agent = "claude", model = "claude-opus-5-5", + } + if activity then + c.activity = { kind = activity[1], label = activity[2], since = ago(activity[3]) } + end + if todo then + c.todo = { done = todo[1], total = todo[2], current = todo[3] } + end + for k, v in extra or {} do + c[k] = v + end + return c + end + local function orch(id: string, name: string, hue: number, project: string?, goal: string, extra: any?): any + local o: any = { + id = id, kind = "standard", name = name, goal = goal, rules = "", crew_rules = "", notes = "", hue = hue, + crew = { devices = {} }, paused = false, archived = false, created = ago(86400), updated = ago(60), project = project, + } + for k, v in extra or {} do + o[k] = v + end + return o + end + + local f_devices = { + { id = "dev-desk", name = "desk", platform = "linux", here = true, online = true }, + { id = "dev-mac", name = "macbook", platform = "macos", here = false, online = true }, + { id = "dev-forge", name = "winbox", platform = "windows", here = false, online = true }, + { id = "dev-cloud", name = "Cloud", platform = "linux", here = false, online = true, cloud = true }, + } + local f_projects = { + { id = "p-zeron-desk", name = "zeron", device = "dev-desk", path = "/home/you/zeron", git = true }, + { id = "p-exnomic-desk", name = "exnomic", device = "dev-desk", path = "/home/you/exnomic", git = true }, + { id = "p-zeron-mac", name = "zeron", device = "dev-mac", path = "/Users/you/zeron", git = true }, + { id = "p-reins-mac", name = "reins", device = "dev-mac", path = "/Users/you/reins", git = true }, + { id = "p-reins-forge", name = "reins", device = "dev-forge", path = "C:\\src\\reins", git = true }, + { id = "p-exnomic-cloud", name = "exnomic", device = "dev-cloud", path = "/work/exnomic", git = true }, + { id = "p-tally-cloud", name = "tally", device = "dev-cloud", path = "/work/tally", git = true }, + } + local f_orchestrators = { + orch("orc-brain", "Brain dump", 262, nil, "Route everything I say to the right place.", { kind = "brain_dump" }), + orch("orc-orch", "Orchestrators", 196, "p-zeron-desk", "Ship Orchestrators and the Control room in zgx."), + orch("orc-vpn", "Exnomic VPN", 32, "p-exnomic-desk", "Phase 2 migration, safely."), + orch("orc-reins", "Reins release", 318, "p-reins-mac", "Ship Reins 2.4 on macOS and Windows."), + orch("orc-tally", "Tally", 100, "p-tally-cloud", "Line counts that never lie."), + } + local f_chats = { + { id = "orc-brain", title = "Brain dump", device = "dev-desk", updated = ago(40), created = ago(518400), agent = "claude", model = "claude-haiku-5-5" }, + { id = "orc-orch", title = "Orchestrators", device = "dev-desk", project = "p-zeron-desk", status = "working", updated = ago(20), created = ago(7200), agent = "claude", model = "claude-opus-5-5", + activity = { kind = "delegating", label = "Message Engine: reports + MCP tools", since = ago(25) } }, + { id = "orc-vpn", title = "Exnomic VPN", device = "dev-desk", project = "p-exnomic-desk", updated = ago(900), created = ago(259200), agent = "claude", model = "claude-sonnet-5-5" }, + { id = "orc-reins", title = "Reins release", device = "dev-mac", project = "p-reins-mac", updated = ago(300), created = ago(90000), agent = "claude", model = "claude-opus-5-5" }, + { id = "orc-tally", title = "Tally", device = "dev-cloud", project = "p-tally-cloud", updated = ago(340), created = ago(180000), agent = "codex", model = "gpt-5.5" }, + crew_chat("th-engine-state", "Engine: orchestration state", "dev-desk", "p-zeron-desk", "orc-orch", "working", + { "testing", "cargo test -p zeron-engine --lib orchestration", 48 }, { 5, 8, "WatchOrchestration stream" }), + crew_chat("th-engine-flow", "Engine: reports + MCP tools", "dev-desk", "p-zeron-desk", "orc-orch", "working", + { "merging", "git merge orch/engine-state", 9 }, { 7, 9, "Merge engine-state" }), + crew_chat("th-ui-control", "Control room UI", "dev-mac", "p-zeron-mac", "orc-orch", "working", + { "testing", "zgx test ui control_", 95 }, { 2, 7, "Map widget" }), + crew_chat("th-ui-orch", "Orchestrator screen", "dev-mac", "p-zeron-mac", "orc-orch", "input", + nil, { 4, 6, "Crew updates card" }, { updated = ago(200) }), + crew_chat("th-vpn-moscow", "Moscow node reboot plan", "dev-desk", "p-exnomic-desk", "orc-vpn", nil, + nil, { 3, 3 }, { updated = ago(2100) }), + crew_chat("th-vpn-exporter", "Exporter image", "dev-cloud", "p-exnomic-cloud", "orc-vpn", "working", + { "building", "docker build -t exnomic/exporter .", 260 }, { 2, 5, "Image builds" }), + crew_chat("th-reins-win", "Windows installer", "dev-forge", "p-reins-forge", "orc-reins", "working", + { "building", "cargo build --release --target x86_64-pc-windows-msvc", 410 }, { 3, 5, "MSI bundle" }), + crew_chat("th-reins-notes", "Release notes", "dev-mac", "p-reins-mac", "orc-reins", "working", + { "editing", "Edit CHANGELOG.md", 30 }, { 1, 3, "2.4 highlights" }), + crew_chat("th-reins-sign", "Code signing", "dev-forge", "p-reins-forge", "orc-reins", nil, + nil, { 2, 2 }, { updated = ago(1400) }), + crew_chat("th-tally-ci", "pytest on 3.13", "dev-cloud", "p-tally-cloud", "orc-tally", "failed", + nil, { 4, 6, "Fix empty input" }, { agent = "codex", model = "gpt-5.5", updated = ago(340) }), + crew_chat("th-tally-lines", "--lines flag", "dev-cloud", "p-tally-cloud", "orc-tally", "working", + { "thinking", "", 70 }, { 2, 4, "-l alias" }, { agent = "codex", model = "gpt-5.5" }), + crew_chat("th-tally-docs", "README usage", "dev-cloud", "p-tally-cloud", "orc-tally", "done", + nil, { 2, 2 }, { agent = "codex", model = "gpt-5.5", updated = ago(2600) }), + -- A thread of the person's own, working, under no orchestrator. + { id = "th-zgx-mem", title = "zgx memory after tabs", device = "dev-desk", project = "p-zeron-desk", status = "working", + updated = ago(10), created = ago(1600), agent = "claude", model = "claude-opus-5-5", + activity = { kind = "thinking", label = "", since = ago(120) } }, + -- Finished hours ago and seen since (no unread output): the Map's + -- "Show chats" leaves it off by default (ds/orch_map_visibility). + { id = "th-titlebar", title = "Linux titlebar toggle", device = "dev-desk", project = "p-zeron-desk", updated = ago(26000), created = ago(30000), agent = "claude", model = "claude-opus-5-5" }, + -- What the Brain dump started from the person's dumps: threads of + -- their own, on whichever machine the project lives. + { id = "th-dev-names", title = "Verify unique device names", device = "dev-mac", project = "p-zeron-mac", status = "working", + updated = ago(6), created = ago(700), agent = "claude", model = "claude-sonnet-5-5", + activity = { kind = "testing", label = "cargo test -p zeron-engine devices::names", since = ago(80) }, todo = { done = 2, total = 4, current = "-1, -2 suffixes" } }, + { id = "th-omarchy", title = "Rerun omarchy-update", device = "dev-desk", status = "done", + updated = ago(1900), created = ago(2400), agent = "claude", model = "claude-haiku-5-5", todo = { done = 1, total = 1 } }, + { id = "th-wg-audit", title = "Audit wg peers", device = "dev-cloud", project = "p-exnomic-cloud", status = "input", + updated = ago(150), created = ago(1100), agent = "claude", model = "claude-sonnet-5-5", todo = { done = 3, total = 5, current = "Which peers to drop?" } }, + } + local function member(chat: string, o: string, item: string, extra: any?): any + local m: any = { chat_id = chat, orchestrator_id = o, item_id = item, role = "", released = false, joined = ago(5000) } + for k, v in extra or {} do + m[k] = v + end + return m + end + local f_crew = { + member("th-engine-state", "orc-orch", "it-engine"), + member("th-engine-flow", "orc-orch", "it-flow"), + member("th-ui-control", "orc-orch", "it-control"), + member("th-ui-orch", "orc-orch", "it-orch-screen"), + member("th-vpn-moscow", "orc-vpn", "it-moscow"), + member("th-vpn-exporter", "orc-vpn", "it-exporter"), + member("th-reins-notes", "orc-reins", "it-notes"), + member("th-reins-win", "orc-reins", "it-win"), + member("th-reins-sign", "orc-reins", "it-sign"), + member("th-tally-lines", "orc-tally", "it-lines"), + member("th-tally-ci", "orc-tally", "it-ci"), + member("th-tally-docs", "orc-tally", "it-docs"), + } + local function item(id: string, o: string, title: string, state: string, order: number, threads: { string }): any + return { id = id, orchestrator_id = o, title = title, detail = "", state = state, priority = "normal", threads = threads, links = {}, order = order, created = ago(5000), updated = ago(300) } + end + local f_items = { + item("it-engine", "orc-orch", "Engine: orchestration state", "active", 1, { "th-engine-state" }), + item("it-flow", "orc-orch", "Reports, asks, MCP tools", "active", 2, { "th-engine-flow" }), + item("it-control", "orc-orch", "Control room", "active", 3, { "th-ui-control" }), + item("it-orch-screen", "orc-orch", "Orchestrator screen", "blocked", 4, { "th-ui-orch" }), + item("it-moscow", "orc-vpn", "Reboot Moscow node", "review", 1, { "th-vpn-moscow" }), + item("it-exporter", "orc-vpn", "Exporter image", "active", 2, { "th-vpn-exporter" }), + item("it-notes", "orc-reins", "Release notes", "active", 1, { "th-reins-notes" }), + item("it-win", "orc-reins", "Windows installer", "active", 2, { "th-reins-win" }), + item("it-sign", "orc-reins", "Code signing", "done", 3, { "th-reins-sign" }), + item("it-lines", "orc-tally", "--lines flag", "active", 1, { "th-tally-lines" }), + item("it-ci", "orc-tally", "CI on 3.13", "blocked", 2, { "th-tally-ci" }), + item("it-docs", "orc-tally", "README usage", "done", 3, { "th-tally-docs" }), + } + local f_asks = { + { id = "ak-f1", from_chat = "th-ui-orch", orchestrator_id = "orc-orch", item_id = "it-orch-screen", kind = "decision", + question = "Orchestrator page: side by side, or tabs?", options = { { label = "Side by side" }, { label = "Tabs" } }, + urgent = false, state = "open", created = ago(200) }, + } + -- The person's dumps, and where the Brain dump put each part. + local function route(id: string, kind: string, target: string, excerpt: string): any + return { id = id, excerpt = excerpt, target = { kind = kind, id = target }, intent = "task", message = excerpt, state = "delivered", candidates = {} } + end + local f_dumps = { + { id = "dp-f2", text = "make sure no two devices end up with the same name, and the orchestrator page goes side by side. also the reins notes before the msi", + state = "routed", device = "dev-desk", created = ago(710), updated = ago(630), routes = { + route("rf-1", "new_thread", "th-dev-names", "make sure no two devices end up with the same name"), + route("rf-2", "chat", "th-ui-orch", "the orchestrator page goes side by side"), + route("rf-3", "orchestrator", "orc-reins", "the reins notes before the msi"), + } }, + { id = "dp-f1", text = "rerun omarchy update, and someone audit the wg peers on the exnomic box", + state = "routed", device = "dev-desk", created = ago(2420), updated = ago(1100), routes = { + route("rf-4", "new_thread", "th-omarchy", "rerun omarchy update"), + route("rf-5", "new_thread", "th-wg-audit", "audit the wg peers on the exnomic box"), + } }, + } + local f_loads = { + ["dev-desk"] = { cpu = 0.71, mem_used = 41.2e9, mem_total = 62.0e9, load1 = 11.4, cores = 16, online = true, at = NOW }, + ["dev-mac"] = { cpu = 0.46, mem_used = 22.1e9, mem_total = 36.0e9, load1 = 5.2, cores = 12, online = true, at = NOW }, + ["dev-forge"] = { cpu = 0.88, mem_used = 25.7e9, mem_total = 32.0e9, load1 = 14.1, cores = 16, online = true, at = NOW }, + ["dev-cloud"] = { cpu = 0.34, mem_used = 5.9e9, mem_total = 16.0e9, load1 = 1.6, cores = 8, online = true, at = NOW }, + } + -- What passed between them in the last hour (the Timeline's ticks, the + -- Map's pulses). + local f_comms = { + { id = "cf-1", kind = "spawn", from = "orc-vpn", to = "th-vpn-exporter", summary = "Exporter image", at = ago(1500) }, + { id = "cf-2", kind = "report", from = "th-engine-state", to = "orc-orch", summary = "Registry kinds in", at = ago(1500) }, + { id = "cf-3", kind = "report", from = "th-reins-win", to = "orc-reins", summary = "MSVC link error, retrying", at = ago(1700) }, + { id = "cf-4", kind = "report", from = "th-ui-control", to = "orc-orch", summary = "2 tests fail at 900 wide", at = ago(1250) }, + { id = "cf-5", kind = "report", from = "th-vpn-moscow", to = "orc-vpn", summary = "Reboot plan ready", at = ago(2100) }, + { id = "cf-6", kind = "report", from = "th-tally-docs", to = "orc-tally", summary = "README pushed", at = ago(2600) }, + { id = "cf-7", kind = "report", from = "th-engine-flow", to = "orc-orch", summary = "Coalesced wake works", at = ago(300) }, + { id = "cf-8", kind = "report", from = "th-reins-notes", to = "orc-reins", summary = "Draft up", at = ago(300) }, + { id = "cf-9", kind = "ask", from = "th-ui-orch", to = "you", summary = "Side by side or tabs?", at = ago(200) }, + { id = "cf-10", kind = "report", from = "th-tally-ci", to = "orc-tally", summary = "pytest: 3 failed", at = ago(340) }, + { id = "cf-11", kind = "message", from = "orc-orch", to = "th-engine-flow", summary = "Merge engine-state first", at = ago(25) }, + { id = "cf-12", kind = "spawn", from = "orc-brain", to = "th-dev-names", summary = "Verify unique device names", at = ago(700) }, + { id = "cf-13", kind = "spawn", from = "orc-brain", to = "th-wg-audit", summary = "Audit wg peers", at = ago(1100) }, + { id = "cf-14", kind = "route", from = "orc-brain", to = "th-ui-orch", summary = "Side by side, not tabs", at = ago(640) }, + { id = "cf-15", kind = "route", from = "orc-brain", to = "orc-reins", summary = "Ship notes before the MSI", at = ago(630) }, + { id = "cf-16", kind = "ask", from = "th-wg-audit", to = "you", summary = "Which peers to drop?", at = ago(150) }, + } + -- Each thread's last hour: from `at` on, its state and what it did. + local function lane(list: { any }): { any } + local out = {} + for _, e in list do + table.insert(out, { at = ago(e[1]), status = e[2], kind = e[3] }) + end + return out + end + local f_timeline = { + ["orc-orch"] = lane { { 3600, "working", "delegating" }, { 3420, "idle" }, { 1260, "working", "thinking" }, { 1190, "idle" }, { 25, "working", "delegating" } }, + ["orc-reins"] = lane { { 3150, "working", "delegating" }, { 3020, "idle" }, { 1700, "working", "thinking" }, { 1640, "idle" } }, + ["orc-tally"] = lane { { 2100, "working", "delegating" }, { 2000, "idle" }, { 340, "working", "thinking" }, { 300, "idle" } }, + ["th-engine-state"] = lane { + { 3300, "working", "thinking" }, { 3050, "working", "editing" }, { 2500, "working", "building" }, { 2300, "working", "testing" }, + { 2050, "working", "editing" }, { 1500, "idle" }, { 1380, "working", "thinking" }, { 1200, "working", "editing" }, + { 700, "working", "building" }, { 560, "working", "editing" }, { 48, "working", "testing" }, + }, + ["th-engine-flow"] = lane { + { 3500, "working", "thinking" }, { 3200, "working", "editing" }, { 2200, "working", "testing" }, { 2000, "working", "editing" }, + { 1000, "working", "testing" }, { 820, "working", "merging" }, { 760, "working", "editing" }, { 300, "working", "testing" }, { 140, "working", "editing" }, + { 9, "working", "merging" }, + }, + ["th-ui-control"] = lane { + { 2800, "working", "thinking" }, { 2600, "working", "editing" }, { 1600, "working", "building" }, { 1400, "working", "testing" }, + { 1250, "failed" }, { 1180, "working", "editing" }, { 600, "working", "building" }, { 420, "working", "editing" }, + { 95, "working", "testing" }, + }, + ["th-ui-orch"] = lane { + { 3000, "working", "thinking" }, { 2700, "working", "editing" }, { 1800, "working", "testing" }, { 1650, "working", "editing" }, + { 900, "working", "reading" }, { 620, "working", "editing" }, { 200, "input" }, + }, + ["th-vpn-moscow"] = lane { { 3550, "working", "reading" }, { 2900, "working", "editing" }, { 2100, "done" } }, + ["th-vpn-exporter"] = lane { { 1500, "working", "thinking" }, { 1300, "working", "editing" }, { 700, "working", "installing" }, { 520, "working", "editing" }, { 260, "working", "building" } }, + ["th-reins-win"] = lane { + { 3100, "working", "thinking" }, { 2900, "working", "installing" }, { 2300, "working", "building" }, { 1700, "failed" }, + { 1500, "working", "editing" }, { 1100, "working", "building" }, { 800, "working", "testing" }, { 410, "working", "building" }, + }, + ["th-reins-notes"] = lane { { 900, "working", "reading" }, { 600, "working", "editing" }, { 300, "working", "reviewing" }, { 30, "working", "editing" } }, + ["th-reins-sign"] = lane { { 2600, "working", "running" }, { 2200, "working", "pushing" }, { 2000, "done" }, { 1400, "idle" } }, + ["th-tally-ci"] = lane { { 2000, "working", "thinking" }, { 1800, "working", "editing" }, { 900, "working", "testing" }, { 340, "failed" } }, + ["th-tally-lines"] = lane { { 1000, "working", "reading" }, { 700, "working", "editing" }, { 450, "working", "testing" }, { 380, "working", "editing" }, { 70, "working", "thinking" } }, + ["th-tally-docs"] = lane { { 3400, "working", "editing" }, { 2700, "working", "pushing" }, { 2600, "done" } }, + ["th-dev-names"] = lane { { 700, "working", "reading" }, { 520, "working", "editing" }, { 80, "working", "testing" } }, + ["th-omarchy"] = lane { { 2400, "working", "running" }, { 1900, "done" } }, + ["th-wg-audit"] = lane { { 1100, "working", "reading" }, { 600, "working", "running" }, { 150, "input" } }, + ["th-zgx-mem"] = lane { { 1500, "working", "reading" }, { 1100, "working", "running" }, { 600, "working", "reading" }, { 120, "working", "thinking" } }, + } + -- Each device's CPU, a sample every five seconds for the last four minutes. + local function wave(base: number, swing: number, n: number, seed: number): { number } + local out = {} + for i = 1, n do + local v = base + swing * math.sin((i + seed) * 0.37) * math.cos((i + seed * 2) * 0.11) + swing * 0.35 * math.sin(i * 1.7 + seed) + table.insert(out, math.clamp(v, 0.02, 0.99)) + end + return out + end + -- (As the bridge keeps it: `history`, oldest first, ending on `cpu`.) + local swings: { [string]: { number } } = { ["dev-desk"] = { 0.18, 1 }, ["dev-mac"] = { 0.14, 4 }, ["dev-forge"] = { 0.12, 7 }, ["dev-cloud"] = { 0.14, 2 } } + for id, w in swings do + local l: any = (f_loads :: any)[id] + local line = wave(l.cpu, w[1], 60, w[2]) + local off = l.cpu - line[#line] + local history = {} + for i, v in line do + table.insert(history, { at = NOW - (#line - i) * 5, cpu = math.clamp(v + off, 0.02, 0.99) }) + end + l.history = history + end + (sample :: any).fleet = { + NOW = NOW, + now = { state = "ready", orchestrators = f_orchestrators, crew = f_crew, items = f_items, asks = f_asks, dumps = f_dumps }, + loads = f_loads, + comms = f_comms, + devices = f_devices, + projects = f_projects, + chats = hosted(f_chats), + timeline = f_timeline, + } +end + +function sample.fleet_at(t: number) + local copy = shifted((sample :: any).fleet, t - NOW) + copy.NOW = t + return copy +end + +-- How far each chat's work has got, as orchestration.progress:get() gives +-- it (src/orchestration_progress.rs): one row of each kind. +-- +-- sample.progress by chat id, for sample.chats +-- sample.fleet.progress by chat id, for sample.fleet.chats +do + local function row(chat: string, device: string, state: string, extra: any): any + local r: any = { chat_id = chat, device_id = device, state = state, stalled = false, refreshing = false, updated = ago(30) } + for k, v in extra do + r[k] = v + end + return r + end + local progress = { + -- An orchestrator at work, no todo list of its own. + ["orc-orch"] = row("orc-orch", "dev-desk", "working", { + started = ago(1500), last_step = ago(25), step = "Message Engine: reports + MCP tools", + note = "Sequencing the merges: engine-state lands first, then flow.", note_at = ago(140), updated = ago(25), + }), + -- Working through steps, a long tool running, an ETA. + ["th-engine-state"] = row("th-engine-state", "dev-desk", "working", { + started = ago(2460), last_step = ago(130), step = "cargo test -p zeron-engine --lib orchestration", + steps = { done = 5, total = 8 }, tool = "cargo test -p zeron-engine --lib orchestration", tool_since = ago(540), + eta_secs = 1260, note = "Registry kinds and the watch are in; running the engine tests before the ops.", + note_at = ago(200), updated = ago(30), + }), + -- A fresh note on its way. + ["th-engine-flow"] = row("th-engine-flow", "dev-desk", "working", { + started = ago(3300), last_step = ago(40), step = "report tool", steps = { done = 3, total = 9 }, eta_secs = 2700, + note = "CrewReports part renders; wiring the report tool next.", note_at = ago(420), refreshing = true, updated = ago(12), + }), + -- Stalled: one command running far longer than a step takes. + ["th-ui-control"] = row("th-ui-control", "dev-mac", "working", { + started = ago(4200), last_step = ago(1140), step = "Map widget", steps = { done = 2, total = 7 }, stalled = true, + tool = "cargo build --release", tool_since = ago(2880), + note = "Release build after the layout change; no new output for a while.", note_at = ago(1100), updated = ago(60), + }), + -- Asked the person something. + ["th-ui-orch"] = row("th-ui-orch", "dev-mac", "awaiting_input", { + started = ago(3000), last_step = ago(200), step = "Crew updates card", steps = { done = 4, total = 6 }, + note = "Asked whether the orchestrator page is side by side or tabs.", note_at = ago(190), updated = ago(200), + }), + -- Waiting on background work that wakes it. + ["th-vpn-merge"] = row("th-vpn-merge", "dev-desk", "waiting", { + started = ago(1700), last_step = ago(60), step = "Merge #412", steps = { done = 6, total = 7 }, + note = "Merge queued; waiting on CI for #412.", note_at = ago(55), updated = ago(55), + }), + ["th-vpn-moscow"] = row("th-vpn-moscow", "dev-desk", "done", { + started = ago(7000), last_step = ago(3500), steps = { done = 3, total = 3 }, + note = "Reboot plan ready for 02:00 MSK; needs your OK.", note_at = ago(3480), updated = ago(3480), + }), + ["th-ci-cache"] = row("th-ci-cache", "dev-cloud", "failed", { + started = ago(100000), last_step = ago(90100), step = "sccache on macOS runners", + note = "sccache can't reach the bucket from the macOS runners.", note_at = ago(90000), updated = ago(90000), + }), + } + sample.progress = progress + + -- The fleet's, from its chats: their todo lists, what they run, a few + -- set apart. + local f = (sample :: any).fleet + local STATE = { working = "working", input = "awaiting_input", failed = "failed", done = "done" } + local NOTES: { [string]: string } = { + ["th-engine-state"] = "Watch is in; running the engine tests before the ops.", + ["th-engine-flow"] = "Merging engine-state so the report tool builds on it.", + ["th-ui-control"] = "Map widget renders; two tests fail at 900 wide.", + ["th-ui-orch"] = "Asked whether the page is side by side or tabs.", + ["th-vpn-exporter"] = "Image builds; pushing to the registry next.", + ["th-reins-win"] = "MSVC link error fixed; rebuilding the MSI.", + ["th-reins-notes"] = "Draft up; tightening the 2.4 highlights.", + ["th-tally-ci"] = "pytest 3.13: empty input still fails.", + ["th-tally-lines"] = "Adding the -l alias and its test.", + ["th-zgx-mem"] = "Comparing heap snapshots before and after tabs.", + } + local out = {} + for _, c in f.chats do + local state = STATE[c.status or ""] + if state and c.id ~= "orc-brain" then + local r = row(c.id, c.device, state, { + started = c.created + 600, last_step = if c.activity then c.activity.since else c.updated, note = NOTES[c.id], + note_at = if NOTES[c.id] then ago(90) else nil, + }) + if c.todo then + r.steps = { done = c.todo.done, total = c.todo.total } + r.step = c.todo.current + end + local a = c.activity + if a and (a.kind == "testing" or a.kind == "building") and NOW - a.since >= 15 then + r.tool, r.tool_since = a.label, a.since + end + if state == "working" and c.todo and c.todo.done >= 3 then + r.eta_secs = (c.todo.total - c.todo.done) * 420 + end + out[c.id] = r + end + end + -- Stalled, and waiting on background work. + out["th-zgx-mem"].stalled = true + out["th-zgx-mem"].last_step = ago(980) + out["th-vpn-exporter"].state = "waiting" + out["th-reins-notes"].refreshing = true + f.progress = out +end + +-- The Scratchpad as scratchpad.now:get() gives it (src/scratchpad.rs): a +-- note with headings, a list and a code block, a small diagram, and its +-- history (newest first, the first the document as it is now): the Brain +-- dump's last edit over the person's. +do + local text = table.concat({ + "# Sync, the long way round", + "", + "Devices should agree without a central clock. Notes for later, not work yet.", + "", + "## Ideas", + "", + "- Every edit carries its device and a rev; the engine keeps the order", + "- Conflicts keep both: the newer wins, the other lands in history", + "- **Never** delete a snapshot, only write over the oldest", + "", + "## Shape of a change", + "", + "```rust", + "struct Change { rev: String, device: String, at: DateTime }", + "```", + "", + "## Open", + "", + "1. How long a device may be offline before it re-reads everything", + "2. Whether the Brain dump appends or writes a section", + }, "\n") + local function shape(id: string, kind: string, z: number, extra: any): any + local s: any = { id = id, kind = kind, x = 0, y = 0, w = 0, h = 0, points = {}, text = "", color = "fg", z = z } + for k, v in extra do + s[k] = v + end + return s + end + sample.scratchpad = { + loaded = true, + text = text, + rev = "r-41", + updated_at = ago(180), + updated_by = "brain_dump", + device_id = "dev-desk", + -- App, Engine and Edge, their arrows labelled, a freehand mark + -- under the engine and a note beside it. + shapes = { + shape("sh-app", "rect", 0, { x = 40, y = 60, w = 150, h = 70, text = "App" }), + shape("sh-engine", "rect", 1, { x = 300, y = 60, w = 150, h = 70, text = "Engine", color = "accent" }), + shape("sh-edge", "ellipse", 2, { x = 560, y = 55, w = 150, h = 80, text = "Edge", color = "blue" }), + shape("sh-rpc", "arrow", 3, { points = { { x = 194, y = 95 }, { x = 296, y = 95 } }, text = "RPC", color = "muted" }), + shape("sh-sync", "arrow", 4, { points = { { x = 454, y = 95 }, { x = 556, y = 95 } }, text = "sync", color = "muted" }), + -- (An elbow: start, elbow, end.) + shape("sh-push", "arrow", 5, { points = { { x = 635, y = 140 }, { x = 375, y = 262 }, { x = 115, y = 136 } }, text = "push", color = "orange" }), + shape("sh-mark", "stroke", 6, { + points = { { x = 306, y = 150 }, { x = 330, y = 143 }, { x = 356, y = 154 }, { x = 382, y = 144 }, { x = 408, y = 155 }, { x = 432, y = 145 }, { x = 446, y = 150 } }, + color = "green", + stroke = 2, + }), + shape("sh-note", "text", 7, { x = 318, y = 166, w = 150, h = 20, text = "retry on reconnect", color = "green" }), + }, + history = { + { id = "rv-6", at = ago(240), edited_at = ago(180), by = "brain_dump", chars = #text, shape_count = 8, preview = "Sync, the long way round" }, + { id = "rv-5", at = ago(1500), edited_at = ago(1320), by = "person", device_id = "dev-desk", chars = 612, shape_count = 7, preview = "Sync, the long way round" }, + { id = "rv-4", at = ago(5400), edited_at = ago(5100), by = "person", device_id = "dev-mac", chars = 380, shape_count = 3, preview = "Sync" }, + { id = "rv-3", at = ago(86400), edited_at = ago(86000), by = "person", device_id = "dev-desk", restored_from = "rv-1", chars = 120, shape_count = 0, preview = "Sync" }, + { id = "rv-2", at = ago(90000), edited_at = ago(89900), by = "brain_dump", chars = 64, shape_count = 0, preview = "Ask Wing about the relay deploy window" }, + }, + } +end + +return sample diff --git a/apps/zgx/ui/account/scratchpad.luau b/apps/zgx/ui/account/scratchpad.luau new file mode 100644 index 000000000..b63a89545 --- /dev/null +++ b/apps/zgx/ui/account/scratchpad.luau @@ -0,0 +1,54 @@ +-- The Control room's Scratchpad as the host keeps it (src/scratchpad.rs): +-- one document per account, Markdown and a drawing, and its history +-- (`WatchScratchpad`, whole on every change). Nothing written here is work. +-- account/orchestration_sample.luau's `scratchpad` is a whole one. +-- +-- scratchpad.now:get() { host, missing?, loaded, text, rev, updated_at?, updated_by, +-- device_id?, shapes, history } +-- `updated_by`, a revision's `by`: "person", +-- "brain_dump" or "other"; history newest first, +-- its first the document as it is now +-- scratchpad.set_text(text) a promise of the new rev +-- scratchpad.set_shapes(shapes) a promise of the new rev +-- scratchpad.restore(id) a snapshot put back: a promise of the rev +-- scratchpad.revision(id) a promise of one snapshot, whole +-- { id, at, edited_at, by, text, shapes, +-- restored_from? } +-- +-- A shape: { id, kind ("stroke", "rect", "ellipse", "arrow", "text", or a +-- newer renderer's), x, y, w, h, points = { { x, y } }, text, color (a +-- token: "fg", "muted", "accent", "red", "orange", "yellow", "green", +-- "blue", "purple"), stroke?, z }, in the drawing's own units. + +local M = {} + +-- (`host` false: an app without the Scratchpad's host module, so this +-- default is all it ever has. The host's own says true, and `missing = +-- "engine"` while the engine refuses its watch.) +M.now = ui.shared("scratchpad", { + host = false, + loaded = false, + text = "", + rev = "", + updated_by = "person", + shapes = {}, + history = {}, +} :: any) + +function M.set_text(text: string): any + return ui.call("scratchpad.set_text", text) +end + +function M.set_shapes(shapes: { any }): any + return ui.call("scratchpad.set_shapes", shapes) +end + +function M.restore(id: string): any + return ui.call("scratchpad.restore", id) +end + +function M.revision(id: string): any + return ui.call("scratchpad.revision", id) +end + +return M diff --git a/apps/zgx/ui/assets/decision/topology-a.png b/apps/zgx/ui/assets/decision/topology-a.png new file mode 100644 index 000000000..eca364263 Binary files /dev/null and b/apps/zgx/ui/assets/decision/topology-a.png differ diff --git a/apps/zgx/ui/assets/decision/topology-b.png b/apps/zgx/ui/assets/decision/topology-b.png new file mode 100644 index 000000000..a77fcf3ba Binary files /dev/null and b/apps/zgx/ui/assets/decision/topology-b.png differ diff --git a/apps/zgx/ui/assets/icons/ui/action-pause.svg b/apps/zgx/ui/assets/icons/ui/action-pause.svg new file mode 100644 index 000000000..2fb25475f --- /dev/null +++ b/apps/zgx/ui/assets/icons/ui/action-pause.svg @@ -0,0 +1,4 @@ + + + + diff --git a/apps/zgx/ui/assets/icons/ui/redo.svg b/apps/zgx/ui/assets/icons/ui/redo.svg new file mode 100644 index 000000000..8f5d1cb3b --- /dev/null +++ b/apps/zgx/ui/assets/icons/ui/redo.svg @@ -0,0 +1 @@ + diff --git a/apps/zgx/ui/assets/icons/ui/shape-ellipse.svg b/apps/zgx/ui/assets/icons/ui/shape-ellipse.svg new file mode 100644 index 000000000..cd3b2cba3 --- /dev/null +++ b/apps/zgx/ui/assets/icons/ui/shape-ellipse.svg @@ -0,0 +1 @@ + diff --git a/apps/zgx/ui/assets/icons/ui/shape-rect.svg b/apps/zgx/ui/assets/icons/ui/shape-rect.svg new file mode 100644 index 000000000..a432c9ba6 --- /dev/null +++ b/apps/zgx/ui/assets/icons/ui/shape-rect.svg @@ -0,0 +1 @@ + diff --git a/apps/zgx/ui/assets/icons/ui/shape-text.svg b/apps/zgx/ui/assets/icons/ui/shape-text.svg new file mode 100644 index 000000000..b081e9255 --- /dev/null +++ b/apps/zgx/ui/assets/icons/ui/shape-text.svg @@ -0,0 +1 @@ + diff --git a/apps/zgx/ui/assets/icons/ui/undo.svg b/apps/zgx/ui/assets/icons/ui/undo.svg new file mode 100644 index 000000000..927f032eb --- /dev/null +++ b/apps/zgx/ui/assets/icons/ui/undo.svg @@ -0,0 +1 @@ + diff --git a/apps/zgx/ui/chat_fixture.luau b/apps/zgx/ui/chat_fixture.luau index 651d89aa8..38abc75e9 100644 --- a/apps/zgx/ui/chat_fixture.luau +++ b/apps/zgx/ui/chat_fixture.luau @@ -2046,6 +2046,17 @@ for _, id in { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9" } do end, } end +-- A session opened at one of its messages (an ask's turn: the Control +-- room's cards, Needs you): `go_s1_at_u3` opens s1 at its third question. +for _, at in { { "s1", "u3" }, { "s1", "a3" }, { "s1", "u20" }, { "s1", "nope" } } do + ui.command { + id = `go_{at[1]}_at_{at[2]}`, + title = `Open {at[1]} at {at[2]}`, + run = function() + route.open_session(at[1], { message = at[2] }) + end, + } +end -- A new chat with no project asked of it (⌘N, the sidebar's New chat). ui.command { id = "go_new", diff --git a/apps/zgx/ui/control_board_live_fixture.luau b/apps/zgx/ui/control_board_live_fixture.luau new file mode 100644 index 000000000..7d347d4d3 --- /dev/null +++ b/apps/zgx/ui/control_board_live_fixture.luau @@ -0,0 +1,60 @@ +-- The Control room as a first real run leaves it (ui/control_page_fixture.luau, +-- its board's work replaced): one orchestrator, Tally, and the two things +-- it finished today, the rest of the board empty. For the board's audit +-- pictures (`control_board_live@1440x2600|#control-board-tab`) and +-- tests/orch_board.test.luau. + +local built = require("./control_page_fixture") +local orchestration = require("@account/orchestration") +local sessions = require("@account/sessions") + +local now = os.time() +-- (Done today whatever the clock says: never before this midnight.) +local here = os.date("*t", now) :: any +local midnight = now - (here.hour * 3600 + here.min * 60 + here.sec) +local function today(ago: number): number + return math.max(midnight, now - ago) +end +local n = table.clone(orchestration.now:peek()) +n.orchestrators = { + { id = "orc-tally", kind = "standard", name = "Tally", hue = 214, goal = "Line counts that never lie.", rules = "", crew_rules = "", notes = "", crew = { devices = {} }, paused = false, archived = false }, +} +n.items = { + { + id = "it-tests", + orchestrator_id = "orc-tally", + title = "Add pytest tests for count()", + state = "done", + priority = "normal", + order = 1, + outcome = "tests/test_tally.py with 11 passing pytest tests for count(), committed as 9bf0111.", + created = now - 3600, + updated = today(600), + done = today(600), + }, + { + id = "it-lines", + orchestrator_id = "orc-tally", + title = "Add --lines flag to tally.py", + state = "done", + priority = "normal", + order = 2, + outcome = "--lines flag added to tally.py, committed as 117123a. Verified: word count 3, --lines 2.", + created = now - 3000, + updated = today(300), + done = today(300), + }, +} +orchestration.now:set(n) +-- (Tally's chat, in its project: named as Tally is, so its cards say Tally +-- alone.) +local s = table.clone(sessions.now:peek()) +s.projects = table.clone(s.projects or {}) +table.insert(s.projects, { id = "p-tally", name = "tally", device = "dev-cloud", path = "/work/tally", git = true }) +s.chats = table.clone(s.chats or {}) +table.insert(s.chats, { id = "orc-tally", title = "Tally", device = "dev-cloud", project = "p-tally", updated = now - 300, created = now - 9000, agent = "claude", model = "claude-opus-5-5" }) +sessions.now:set(s) +-- (On the Board's own tab.) +require("@app/screens/control/page").tab:set("board") + +return built diff --git a/apps/zgx/ui/control_fixture.luau b/apps/zgx/ui/control_fixture.luau new file mode 100644 index 000000000..44d468c2f --- /dev/null +++ b/apps/zgx/ui/control_fixture.luau @@ -0,0 +1,42 @@ +-- The Control room in the main window (the shell on /control), drawn from +-- account/orchestration_sample with its chats and devices: what +-- tests/control_*.test.luau drive and audit shots take. No engine: the +-- host's calls are ui/control_world's. + +ui.theme.strict("warn") +local ds = require("@ds") +ds.setup() +-- (Where the window is: the Control room, as left there.) +ui.state("shell_route_path", "/control") +require("@app/screens/shell/route").launched:set(true) +require("@app/control_world")() +-- (What the shell around the page asks of the host: Settings' calls, and the +-- composer's catalog, answered here.) +require("./settings_stub") +local store = require("@app/screens/chat/store") :: any +store.catalog_on = function() end +store.load_catalog = function() end +store.catalog:set({ + state = "ready", + harnesses = { { id = "claude-code", label = "Claude", mark = "claude" } }, + complete = true, + models = { + { value = "claude-code/claude-opus-5-5", label = "Opus 5.5", harness = "claude-code" }, + { value = "claude-code/claude-haiku-5-5", label = "Haiku 5.5", harness = "claude-code" }, + }, +}) +local route = require("@app/screens/shell/route") +local shell = require("@app/screens/shell") +local built = shell() +-- Another page (Settings) opened from the page, and the Control room again +-- (as Back takes it there). +ui.command { id = "fixture.leave", title = "Fixture: open Settings", run = function() + route.settings() +end } +ui.command { id = "fixture.control", title = "Fixture: the Control room", run = function() + route.control() +end } +ui.after(0, function() + route.control() +end) +return built diff --git a/apps/zgx/ui/control_float_fixture.luau b/apps/zgx/ui/control_float_fixture.luau new file mode 100644 index 000000000..82d2b91c9 --- /dev/null +++ b/apps/zgx/ui/control_float_fixture.luau @@ -0,0 +1,25 @@ +-- The Control room in the main window (ui/control_fixture.luau's), for the +-- floating composer's pictures (screens/control/page `FloatingCapture`): +-- `fixture.float` scrolls Capture up out of view with a draft in its +-- composer and two files on it (the composer bare, floating at the page's +-- foot); `fixture.float_open` opens it out. (`theme_dark` and `theme_light`, +-- control_world's, wear either theme.) +-- +-- control_float@1440x900|!theme_dark|!fixture.float|+400|~bare-dark +-- control_float@1440x900|!theme_light|!fixture.float|!fixture.float_open|+400|~open-light + +local built = require("./control_fixture") + +ui.command { id = "fixture.float", title = "Fixture: Capture's composer floating", run = function() + require("@app/screens/chat/lane_tab").draft("orc-brain"):set("Ask vpn whether Moscow's exit is back, then") + require("@app/screens/chat/shared").attach_files({ + { name = "wg0.conf", path = "/Users/dev/wg0.conf" }, + { name = "latency.csv", path = "/Users/dev/latency.csv" }, + }, "orc-brain") + require("@app/screens/control/page").place:set(900) +end } +ui.command { id = "fixture.float_open", title = "Fixture: the floating composer opened out", run = function() + require("@app/screens/control/page").float_open:set(true) +end } + +return built diff --git a/apps/zgx/ui/control_page_fixture.luau b/apps/zgx/ui/control_page_fixture.luau new file mode 100644 index 000000000..7700e2f08 --- /dev/null +++ b/apps/zgx/ui/control_page_fixture.luau @@ -0,0 +1,16 @@ +-- The Control room's page alone (screens/control/page) on the window's +-- material, drawn from account/orchestration_sample: for the tests that +-- look at its widgets without the shell around them. + +ui.theme.strict("warn") +local ds = require("@ds") +ds.setup() +require("@app/screens/shell/route").launched:set(true) +require("@app/control_world")() +local page = require("@app/screens/control/page") +return ui.column { + w_full = true, + h_full = true, + bg = "$background", + page.Page(), +} diff --git a/apps/zgx/ui/control_qa_fixture.luau b/apps/zgx/ui/control_qa_fixture.luau new file mode 100644 index 000000000..41e89c185 --- /dev/null +++ b/apps/zgx/ui/control_qa_fixture.luau @@ -0,0 +1,28 @@ +-- The Control room's page (screens/control/page) with its Q&A full: the +-- sample's world (control_world), plus two earlier dumps whose questions +-- went to threads. Its entries, newest asked first: +-- +-- dp-2/rt-7 answered, not read yet (the sample's) +-- dp-2/rt-9 waiting, its thread at work (the sample's) +-- dp-qa2/rq-5 waiting +-- dp-qa/rq-1 answered at length, not read (asked in a real dump) +-- dp-qa/rq-3 answered, read +-- +-- and, not listed: dp-qa2/rq-6 (answered, put away), dp-qa/rq-2 (work in a +-- question's shape: a task) and dp-qa/rq-4 (rhetorical: info). + +ui.theme.strict("warn") +local ds = require("@ds") +ds.setup() +require("@app/screens/shell/route").launched:set(true) +require("@app/control_world")() +require("@app/control_qa_world")() +local page = require("@app/screens/control/page") + +return ui.column { + w_full = true, + h_full = true, + bg = "$background", + page.Page(), + ds.toast.Host {}, +} diff --git a/apps/zgx/ui/control_qa_screen_fixture.luau b/apps/zgx/ui/control_qa_screen_fixture.luau new file mode 100644 index 000000000..5a29ca137 --- /dev/null +++ b/apps/zgx/ui/control_qa_screen_fixture.luau @@ -0,0 +1,29 @@ +-- The Control room with its Q&A (control_qa_world), loaded as the shell +-- loads a screen: required while a component is being built +-- (screens/shell/boundary), so what the page makes at its top level is +-- made inside that build. `qa_screen_shown` takes the screen away and +-- brings it back, as going to a thread and back does. + +ui.theme.strict("warn") +local ds = require("@ds") +ds.setup() +require("@app/screens/shell/route").launched:set(true) +require("@app/control_world")() +require("@app/control_qa_world")() + +local shown = ui.state("qa_screen_shown", true) + +local Screen = ui.component(function(): Node + local page = require("@app/screens/control/page") + return page.Page() +end) + +return ui.column { + w_full = true, + h_full = true, + bg = "$background", + ui.show(function(): boolean + return shown:get() + end, ui.column { w_full = true, h_full = true, Screen {} }), + ds.toast.Host {}, +} diff --git a/apps/zgx/ui/control_qa_world.luau b/apps/zgx/ui/control_qa_world.luau new file mode 100644 index 000000000..4c9f0432d --- /dev/null +++ b/apps/zgx/ui/control_qa_world.luau @@ -0,0 +1,177 @@ +-- The world control_qa_fixture and control_qa_screen_fixture draw (see +-- control_qa_fixture's header for its entries): control_world, plus two +-- earlier dumps whose questions went to threads, the thread one was +-- answered in, and the Q&A marks as they start. Never the page itself. +-- +-- require("@app/control_qa_world")() once, before the screen is built + +return function() + local orchestration = require("@account/orchestration") + local sessions = require("@account/sessions") + local QA = require("@ds/orch_qa") + + local NOW = os.time() + local function ago(s: number): number + return NOW - s + end + + local CAPY_ANSWER = [[In today's flow, essentially nothing reaches Inbox or Next. I checked the code. + +**What can put a card there:** +- **Inbox:** + - the Brain dump routing a fragment straight to a board card instead of a thread; + - any card created without a state, such as an orchestrator adding an item with no state set. +- **Next:** + - an orchestrator planning an item before starting its crew; + - "Add to board" on a chat that isn't working. + +**Why you've never seen one:** you have no orchestrators. The Brain dump's "act, don't shrug" rule sends every task to a new thread that starts immediately, and those cards are created straight in Now. + +My suggestion: make the columns meaningful rather than remove them. +- **Inbox: captured, not started yet.** Fragments like "later", "someday" or "remind me" become Inbox cards instead of new threads. +- **Next: queued work.** Tasks you say should wait, or ones held back because a device is at its limit. + +The simpler alternative is to hide Inbox and Next until they have cards. Which do you want?]] + + local REAL = "Are Inbox and Next sections in the board ever used? Under what conditions does that happen? Because I've never seen a card in Inbox or Next." + local FLICKER = "Why does the map flicker on resize? Find the root cause and fix it." + local SHIP = "When does 2.0 ship?" + local RIGHT = "The new map is so much better, right?" + local dp_qa_text = `{REAL} {FLICKER} {SHIP} {RIGHT}` + local function span_of(text: string, part: string): { from: number, to: number } + local at = string.find(text, part, 1, true) :: number + return { from = at - 1, to = at - 1 + #part } + end + + local MSI = "Is the Windows MSI signed yet?" + local BUCKET = "what did the CI cache bucket end up being?" + local dp_qa2_text = `{MSI} And {BUCKET}` + + local extra = { + { + id = "dp-qa2", + text = dp_qa2_text, + state = "routed", + summary = "2 questions: Exnomic VPN, CI speed.", + device = "dev-desk", + created = ago(3 * 3600), + updated = ago(3 * 3600 - 600), + routes = { + { id = "rq-5", excerpt = MSI, span = span_of(dp_qa2_text, MSI), target = { kind = "orchestrator", id = "orc-vpn" }, + intent = "question", direct = true, message = "Is the Windows MSI signed yet?", state = "delivered", candidates = {} }, + { id = "rq-6", excerpt = BUCKET, span = span_of(dp_qa2_text, BUCKET), target = { kind = "orchestrator", id = "orc-ci" }, + intent = "question", direct = true, message = "Which bucket did CI caching settle on?", state = "answered", candidates = {}, + answer = "zeron-ci-cache-us.", answer_full = "`zeron-ci-cache-us`, in us-east-2.", answered = ago(3 * 3600 - 600) }, + }, + }, + { + id = "dp-qa", + text = dp_qa_text, + state = "routed", + summary = "4 parts: 2 questions, 1 task, 1 note.", + device = "dev-desk", + created = ago(8 * 3600), + updated = ago(8 * 3600 - 300), + routes = { + { id = "rq-1", excerpt = REAL, span = span_of(dp_qa_text, REAL), target = { kind = "chat", id = "th-capy" }, + intent = "question", direct = true, message = "Are the Board's Inbox and Next columns ever used?", state = "answered", candidates = {}, + answer = "Essentially never in today's flow: every task starts a thread at once, so cards land in Now.", + answer_full = CAPY_ANSWER, answered = ago(8 * 3600 - 240) }, + { id = "rq-2", excerpt = FLICKER, span = span_of(dp_qa_text, FLICKER), target = { kind = "chat", id = "th-ui-control" }, + intent = "task", direct = false, message = "Find why the map flickers on resize and fix it.", state = "delivered", candidates = {} }, + { id = "rq-3", excerpt = SHIP, span = span_of(dp_qa_text, SHIP), target = { kind = "orchestrator", id = "orc-orch" }, + intent = "question", direct = true, message = "When does 2.0 ship?", state = "answered", candidates = {}, + answer = "Friday, after the board lands.", answer_full = "**Friday**, once the board work lands.", answered = ago(7 * 3600) }, + { id = "rq-4", excerpt = RIGHT, span = span_of(dp_qa_text, RIGHT), target = { kind = "note" }, + intent = "info", direct = false, message = "", state = "done", candidates = {} }, + }, + }, + } + + -- In time order with the sample's (newest first). + local now = table.clone(orchestration.now:peek()) + local dumps = table.clone(now.dumps or {}) + for _, d in extra do + table.insert(dumps, d) + end + table.sort(dumps, function(a: any, b: any): boolean + return (a.created or 0) > (b.created or 0) + end) + now.dumps = dumps + orchestration.now:set(now) + + -- The thread the real question was answered in. + local s = table.clone(sessions.now:peek()) + local chats = table.clone(s.chats or {}) + table.insert(chats, { id = "th-capy", title = "Explaining Capy.ai Captains", device = "dev-desk", status = "idle", + updated = ago(8 * 3600 - 240), created = ago(9 * 3600), agent = "claude", model = "claude-opus-5-5" }) + s.chats = chats + -- At work: the engine state thread on a turn begun just now (after the + -- question to it came); the VPN orchestrator on one begun before its + -- question came, so not at it yet. + local function at(list: { any }): { any } + local out = {} + for _, c in list do + c = table.clone(c) + if c.id == "th-engine-state" then + c.status, c.turn_started = "working", ago(10) + elseif c.id == "orc-vpn" then + c.status, c.turn_started = "working", ago(4 * 3600) + end + table.insert(out, c) + end + return out + end + s.chats = at(s.chats) + local kids = {} + for parent, list in s.children or {} do + kids[parent] = at(list) + end + s.children = kids + sessions.now:set(s) + + -- Read on this device: one; put away: one. + QA.marks:set({ read = { ["dp-qa/rq-3"] = true }, dismissed = { ["dp-qa2/rq-6"] = true } }) + -- Opened from the test (`fixture.qa_reset`): as it started. + ui.command { id = "fixture.qa_reset", title = "Fixture: Q&A marks as they started", run = function() + QA.marks:set({ read = { ["dp-qa/rq-3"] = true }, dismissed = { ["dp-qa2/rq-6"] = true } }) + end } + -- A thread starts working that wasn't there before (`fixture.new_worker`). + ui.command { id = "fixture.new_worker", title = "Fixture: a new thread starts working", run = function() + local n = table.clone(sessions.now:peek()) + local list = table.clone(n.chats or {}) + table.insert(list, { id = "th-new-worker", title = "Brand new worker", device = "dev-desk", status = "working", + updated = os.time(), created = os.time(), agent = "claude", model = "claude-opus-5-5" }) + n.chats = list + sessions.now:set(n) + end } + -- A thread answers (`fixture.qa_answer`): the waiting MSI question. + ui.command { id = "fixture.qa_answer", title = "Fixture: answer the MSI question", run = function() + local n = table.clone(orchestration.now:peek()) + local ds2 = {} + for _, d in n.dumps do + if d.id == "dp-qa2" then + local d2 = table.clone(d) + local rs = {} + for _, r in d.routes do + if r.id == "rq-5" then + local r2 = table.clone(r) + r2.state = "answered" + r2.answer = "Signed this morning." + r2.answer_full = "Yes: signed this morning with the **EV cert**; the MSI on the release page is the signed one." + r2.answered = os.time() + table.insert(rs, r2) + else + table.insert(rs, r) + end + end + d2.routes = rs + table.insert(ds2, d2) + else + table.insert(ds2, d) + end + end + n.dumps = ds2 + orchestration.now:set(n) + end } +end diff --git a/apps/zgx/ui/control_world.luau b/apps/zgx/ui/control_world.luau new file mode 100644 index 000000000..1e444666a --- /dev/null +++ b/apps/zgx/ui/control_world.luau @@ -0,0 +1,728 @@ +-- The world the Control room's fixtures draw: account/orchestration_sample +-- as the host would publish it (its time shifted to now), its chats and +-- devices as `sessions.now` (crew threads under their orchestrator, as +-- the host lists them), and the host's calls stood in for: each op noted +-- in the `calls` state and answered at once, changing the world as the +-- engine would. `ui.command`s play what the engine does on its own. +-- +-- require("@app/control_world")() once, before the screen is built + +local sessions = require("@account/sessions") +local orchestration = require("@account/orchestration") +local sample_of = require("@account/orchestration_sample") + +return function() + local sample = sample_of.at(os.time()) + local calls = ui.state("calls", "") + local function note(what: string) + calls:set(if calls:peek() == "" then what else `{calls:peek()},{what}`) + end + local function settled(value: any): any + return { + after = function(_self: any, done: ((any) -> ())?, _failed: any) + if done then + done(value) + end + end, + } + end + + -- Sessions: top-level chats, and crew threads as their parent's children. + local top, children = {}, {} + for _, c0 in sample.chats do + -- (The host names a harness's mark as ds.Mark does: Codex's is OpenAI's.) + local c = table.clone(c0) + if c.agent == "codex" then + c.agent = "openai" + end + if c.parent then + children[c.parent] = children[c.parent] or {} + table.insert(children[c.parent], c) + else + table.insert(top, c) + end + end + sessions.now:set({ + state = "ready", + here = "dev-desk", + devices = sample.devices, + projects = { { id = "p-zeron", name = "zeron", device = "dev-desk", path = "/home/you/zeron", git = true } }, + chats = top, + archived = {}, + children = children, + places = { pinned = {}, sections = {} }, + places_ready = true, + } :: any) + + local now = table.clone(sample.now) + orchestration.now:set(now) + + -- A chat watched in a page's own composer (Capture's, the Brain dump's): + -- its session as the host would give it, noted by slot in `watched`. + local lanes = require("@app/screens/chat/lanes") :: any + local watched = ui.state("watched", {} :: { [string]: string }) + -- The models a composer offers (the host's catalog), and a pick made the + -- chat's (`chat.configure`), noted `configure::`. + local store = require("@app/screens/chat/store") :: any + store.catalog_on = function() end + store.load_catalog = function() end + store.catalog:set({ + state = "ready", + complete = true, + harnesses = { { id = "claude-code", label = "Claude", mark = "claude" } }, + models = { + { value = "claude-code/claude-opus-5-5", label = "Opus 5.5", harness = "claude-code" }, + { value = "claude-code/claude-haiku-5-5", label = "Haiku 5.5", harness = "claude-code" }, + }, + }) + store.configure = function(id: string, patch: any) + note(`configure:{id}:{patch.model or ""}`) + end + lanes.watch = function(slot: string, id: string?) + if not id then + -- (Let go: no longer noted, and its transcript gone, as the + -- host drops it.) + if watched:peek()[slot] then + watched:set_key({ slot }, nil) + -- (A frame on, as the host's answer comes: never under a + -- transcript still going.) + ui.after(0, function() + if not watched:peek()[slot] then + lanes.view(slot).messages:set({}) + end + end) + end + return + end + watched:set_key({ slot }, id) + local view = lanes.view(slot) + for _, c in sample.chats do + if c.id == id then + view.session:set({ id = id, state = "ready", device = c.device, harness = "claude-code", model = `claude-code/{c.model}`, running = false }) + end + end + -- The Brain dump's transcript, as the Control room's Chat tab reads + -- it: a dump, its routing turn (a tool call each part), a reply. + if slot == "control-chat" then + view.messages:set({ + { id = "bd-u1", role = "user", text = "Moscow node is flaky again, and the board needs a Review column." }, + { + id = "bd-a1", + role = "assistant", + text = "Routed two parts: the Moscow node to VPN rollout, the Review column to UI.", + tools = { + { id = "bd-t1", kind = "tool", verb = "Routed", target = "orc-vpn" }, + { id = "bd-t2", kind = "tool", verb = "Routed", target = "th-ui-orch" }, + }, + }, + { id = "bd-u2", role = "user", text = "Also check the relay window with Wing." }, + }) + end + end + orchestration.loads:set(sample.loads) + orchestration.comms:set(sample.comms) + orchestration.progress:set(sample.progress) + + local function republish(f: (any) -> ()) + local n = orchestration.now:peek() + local copy = table.clone(n) + f(copy) + orchestration.now:set(copy) + end + + local stub = orchestration :: any + local function rejected(message: string): any + return { + after = function(_self: any, _done: any, failed: ((any) -> ())?) + if failed then + failed({ kind = "action", message = message }) + end + end, + } + end + -- An ask the engine marked answered, a beat later (as its state comes). + local function mark_answered(id: string, answer: string?) + ui.after(400, function() + republish(function(n) + local asks = {} + for _, a in n.asks do + if a.id == id then + a = table.clone(a) + a.state = "answered" + a.answer = answer + a.answered = os.time() + end + table.insert(asks, a) + end + n.asks = asks + end) + end) + end + -- Which host and engine answer (`answer_mode`, or the + -- `fixture.answers_*` commands): "ok", this build's; "refuse", an + -- engine that turns answers and defers down; "no_host", an app from + -- before `orchestration.answer_ask`/`defer_ask` on an engine from before + -- options and `deferAsk` (the ops alone); "old_engine", this app on that + -- engine (it says an option with no words is empty). + local mode = ui.state("answer_mode", "ok") + -- The host's calls (account/orchestration's `host`): `answer_ask` + -- noted `answer::