diff --git a/Cargo.lock b/Cargo.lock index 4d3ae0f14..7f338367a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13347,7 +13347,7 @@ dependencies = [ [[package]] name = "zatex" -version = "0.3.7" +version = "0.3.3" dependencies = [ "tiny-skia", "ttf-parser", @@ -13598,7 +13598,7 @@ dependencies = [ [[package]] name = "zeron" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "clap", @@ -13623,7 +13623,7 @@ dependencies = [ [[package]] name = "zeron-audio" -version = "0.3.7" +version = "0.3.3" dependencies = [ "base64 0.22.1", "cpal 0.18.2", @@ -13635,7 +13635,7 @@ dependencies = [ [[package]] name = "zeron-cli" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "clap", @@ -13651,7 +13651,7 @@ dependencies = [ [[package]] name = "zeron-client" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "async-trait", @@ -13682,7 +13682,7 @@ dependencies = [ [[package]] name = "zeron-doc" -version = "0.3.7" +version = "0.3.3" dependencies = [ "chrono", "loro", @@ -13697,7 +13697,7 @@ dependencies = [ [[package]] name = "zeron-engine" -version = "0.3.7" +version = "0.3.3" dependencies = [ "alacritty_terminal", "anyhow", @@ -13757,7 +13757,7 @@ dependencies = [ [[package]] name = "zeron-harness" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "async-trait", @@ -13791,7 +13791,7 @@ dependencies = [ [[package]] name = "zeron-launch" -version = "0.3.7" +version = "0.3.3" dependencies = [ "libc", "tempfile", @@ -13800,14 +13800,14 @@ dependencies = [ [[package]] name = "zeron-markdown" -version = "0.3.7" +version = "0.3.3" dependencies = [ "pulldown-cmark", ] [[package]] name = "zeron-mcp" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "async-trait", @@ -13829,7 +13829,7 @@ dependencies = [ [[package]] name = "zeron-mobile" -version = "0.3.7" +version = "0.3.3" dependencies = [ "async-trait", "serde", @@ -13856,14 +13856,14 @@ dependencies = [ [[package]] name = "zeron-mobile-bindgen" -version = "0.3.7" +version = "0.3.3" dependencies = [ "uniffi", ] [[package]] name = "zeron-notify" -version = "0.3.7" +version = "0.3.3" dependencies = [ "objc", "sha2", @@ -13872,11 +13872,11 @@ dependencies = [ [[package]] name = "zeron-orb" -version = "0.3.7" +version = "0.3.3" [[package]] name = "zeron-plugin-guard" -version = "0.3.7" +version = "0.3.3" dependencies = [ "serde_json", "tempfile", @@ -13884,7 +13884,7 @@ dependencies = [ [[package]] name = "zeron-preview" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "async-trait", @@ -13914,11 +13914,11 @@ dependencies = [ [[package]] name = "zeron-preview-label" -version = "0.3.7" +version = "0.3.3" [[package]] name = "zeron-proto" -version = "0.3.7" +version = "0.3.3" dependencies = [ "chrono", "pulldown-cmark", @@ -13929,7 +13929,7 @@ dependencies = [ [[package]] name = "zeron-rpc" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "async-trait", @@ -13950,7 +13950,7 @@ dependencies = [ [[package]] name = "zeron-sync" -version = "0.3.7" +version = "0.3.3" dependencies = [ "base64 0.22.1", "block2 0.6.2", @@ -13976,7 +13976,7 @@ dependencies = [ [[package]] name = "zeron-syntax" -version = "0.3.7" +version = "0.3.3" dependencies = [ "thiserror 2.0.20", "tree-sitter", @@ -14011,14 +14011,14 @@ dependencies = [ [[package]] name = "zeron-term" -version = "0.3.7" +version = "0.3.3" dependencies = [ "alacritty_terminal", ] [[package]] name = "zeron-term-link" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "async-trait", @@ -14035,7 +14035,7 @@ dependencies = [ [[package]] name = "zeron-text" -version = "0.3.7" +version = "0.3.3" dependencies = [ "core-foundation 0.10.1", "core-graphics 0.24.0", @@ -14052,7 +14052,7 @@ dependencies = [ [[package]] name = "zeron-theme" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "clap", @@ -14067,7 +14067,7 @@ dependencies = [ [[package]] name = "zeron-ui" -version = "0.3.7" +version = "0.3.3" dependencies = [ "alacritty_terminal", "anyhow", @@ -14150,7 +14150,7 @@ dependencies = [ [[package]] name = "zeron-update" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "futures", @@ -14167,11 +14167,11 @@ dependencies = [ [[package]] name = "zeron-veil" -version = "0.3.7" +version = "0.3.3" [[package]] name = "zeron-visual" -version = "0.3.7" +version = "0.3.3" dependencies = [ "base64 0.22.1", "gltf", @@ -14183,7 +14183,7 @@ dependencies = [ [[package]] name = "zeron-visual-gpu" -version = "0.3.7" +version = "0.3.3" dependencies = [ "image", "pollster 0.4.0", @@ -14194,7 +14194,7 @@ dependencies = [ [[package]] name = "zeron-voice" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "cpal 0.17.3", @@ -14213,7 +14213,7 @@ dependencies = [ [[package]] name = "zeron-voice-media" -version = "0.3.7" +version = "0.3.3" dependencies = [ "async-trait", "semver", @@ -14228,7 +14228,7 @@ dependencies = [ [[package]] name = "zeron-voice-session" -version = "0.3.7" +version = "0.3.3" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index 7865ac29a..998f6d040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ members = [ ] [workspace.package] -version = "0.3.7" +version = "0.3.3" edition = "2024" license = "MIT" publish = false diff --git a/apps/zgx/Cargo.lock b/apps/zgx/Cargo.lock index d096e16e9..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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +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=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "ahash", "android-activity", @@ -7067,7 +7067,7 @@ dependencies = [ [[package]] name = "zatex" -version = "0.3.7" +version = "0.3.3" dependencies = [ "ttf-parser", ] @@ -7233,7 +7233,7 @@ checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" [[package]] name = "zeron-cli" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "clap", @@ -7248,7 +7248,7 @@ dependencies = [ [[package]] name = "zeron-launch" -version = "0.3.7" +version = "0.3.3" dependencies = [ "libc", "windows-sys 0.61.2", @@ -7256,7 +7256,7 @@ dependencies = [ [[package]] name = "zeron-notify" -version = "0.3.7" +version = "0.3.3" dependencies = [ "objc", "sha2", @@ -7265,18 +7265,18 @@ dependencies = [ [[package]] name = "zeron-plugin-guard" -version = "0.3.7" +version = "0.3.3" dependencies = [ "serde_json", ] [[package]] name = "zeron-preview-label" -version = "0.3.7" +version = "0.3.3" [[package]] name = "zeron-proto" -version = "0.3.7" +version = "0.3.3" dependencies = [ "chrono", "pulldown-cmark 0.12.2", @@ -7287,7 +7287,7 @@ dependencies = [ [[package]] name = "zeron-theme" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "clap", @@ -7301,7 +7301,7 @@ dependencies = [ [[package]] name = "zeron-update" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "futures", @@ -7318,7 +7318,7 @@ dependencies = [ [[package]] name = "zeron-visual" -version = "0.3.7" +version = "0.3.3" dependencies = [ "base64 0.22.1", "gltf", @@ -7330,7 +7330,7 @@ dependencies = [ [[package]] name = "zeron-visual-gpu" -version = "0.3.7" +version = "0.3.3" dependencies = [ "pollster", "tracing", @@ -7340,7 +7340,7 @@ dependencies = [ [[package]] name = "zeron-voice" -version = "0.3.7" +version = "0.3.3" dependencies = [ "anyhow", "cpal", @@ -7456,7 +7456,7 @@ dependencies = [ [[package]] name = "zgx" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "fixed_decimal", "foldhash 0.2.0", @@ -7476,7 +7476,6 @@ dependencies = [ "unicode-linebreak", "unicode-normalization", "unicode-segmentation", - "web-time", "zerotrie", "zerovec", "zgx-taffy", @@ -7485,7 +7484,7 @@ dependencies = [ [[package]] name = "zgx-3d" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bytemuck", "zgx", @@ -7494,8 +7493,8 @@ dependencies = [ [[package]] name = "zgx-alacritty-terminal" -version = "0.26.8" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +version = "0.26.4" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "base64 0.22.1", "bitflags 2.13.2", @@ -7520,7 +7519,7 @@ dependencies = [ [[package]] name = "zgx-components" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "zgx", ] @@ -7528,7 +7527,7 @@ dependencies = [ [[package]] name = "zgx-cosmic-text" version = "0.19.14" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bitflags 2.13.2", "fontdb", @@ -7552,7 +7551,7 @@ dependencies = [ [[package]] name = "zgx-desktop" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "accesskit", "accesskit_consumer", @@ -7603,17 +7602,16 @@ dependencies = [ [[package]] name = "zgx-ghostty-sys" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" [[package]] name = "zgx-gpu" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "ash", "block2 0.6.2", "bytemuck", - "dispatch2", "fontconfig-parser", "gif", "image", @@ -7647,8 +7645,8 @@ dependencies = [ [[package]] name = "zgx-harfrust" -version = "0.5.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +version = "0.5.3" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bitflags 2.13.2", "bytemuck", @@ -7659,8 +7657,8 @@ dependencies = [ [[package]] name = "zgx-mlua" -version = "0.12.9" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +version = "0.12.8" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "bstr", "either", @@ -7675,7 +7673,7 @@ dependencies = [ [[package]] name = "zgx-run" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "serde_json", "ureq 3.5.0", @@ -7687,7 +7685,7 @@ dependencies = [ [[package]] name = "zgx-script" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "icu_locale_core", "icu_plurals", @@ -7700,7 +7698,6 @@ dependencies = [ "serde_json", "smallvec", "unicase", - "web-time", "windows", "zgx", "zgx-3d", @@ -7712,7 +7709,7 @@ dependencies = [ [[package]] name = "zgx-swash" version = "0.2.11" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "skrifa", "yazi", @@ -7722,7 +7719,7 @@ dependencies = [ [[package]] name = "zgx-taffy" version = "0.9.2" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "arrayvec", "grid", @@ -7733,7 +7730,7 @@ dependencies = [ [[package]] name = "zgx-terminal" version = "0.1.10" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "base64 0.22.1", "flate2", @@ -7748,8 +7745,8 @@ dependencies = [ [[package]] name = "zgx-vte" -version = "0.15.4" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +version = "0.15.0" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "arrayvec", "bitflags 2.13.2", @@ -7761,7 +7758,7 @@ dependencies = [ [[package]] name = "zgx-zune-jpeg" version = "0.5.15" -source = "git+https://github.com/zeronsh/zgx?rev=d391ea7276f0de5cb3d65c191bfd66c02a4c358d#d391ea7276f0de5cb3d65c191bfd66c02a4c358d" +source = "git+https://github.com/katulevskiy/zgx?rev=e49ae7a3cbfc67167db24455605650d7c53803c0#e49ae7a3cbfc67167db24455605650d7c53803c0" dependencies = [ "zune-core", ] diff --git a/apps/zgx/Cargo.toml b/apps/zgx/Cargo.toml index 9a30cbf91..13757f2e1 100644 --- a/apps/zgx/Cargo.toml +++ b/apps/zgx/Cargo.toml @@ -4,7 +4,6 @@ # patched crates (its `[patch.crates-io]`, mirrored below), and a patch here # must not reach gpui's wgpu renderer in the shipping app. It tracks zgx's # `main` branch, pinned by commit when Zeron needs one ahead of main. -# Natural editor metrics use the pinned framework API; see docs/composer.md. [workspace] @@ -32,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 = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } +# 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. @@ -43,7 +45,7 @@ zgx = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191 # none of it, and without it pages are placeholders ("Web view · "). # zgx-run turns it on by default, but this app takes zgx-run without its # defaults (below). Nothing on macOS or Windows. -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["mimalloc", "terminal", "webkitgtk"] } +zgx-desktop = { git = "https://github.com/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. @@ -51,18 +53,18 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb # wgpu this app already links), for plugins that show 3D: the CAD plugin # hands it its own triangles (`geometry = { positions, indices, … }`). It # asks no grant; a scene draws a frame only when something in it changes. -zgx-script = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["inspect", "i18n", "scene3d"] } +zgx-script = { git = "https://github.com/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 = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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 @@ -162,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 = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", 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,8 +174,8 @@ zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb # DirectComposition tree of the window's own. The runtime ships with Windows # 11 (and with Edge on Windows 10); without it, pages are placeholders. [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Graphics_Gdi", "Win32_Media_Audio", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Console", "Win32_System_Registry", "Win32_System_Threading", "Win32_System_Time", "Win32_UI_WindowsAndMessaging"] } -zgx-desktop = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d", features = ["webview2"] } +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/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 @@ -185,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 = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -winit = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_macos = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_consumer = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_atspi_common = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_unix = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } -accesskit_windows = { git = "https://github.com/zeronsh/zgx", rev = "d391ea7276f0de5cb3d65c191bfd66c02a4c358d" } +wgpu-hal = { git = "https://github.com/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/README.md b/apps/zgx/README.md index 48a11299f..eb9f40aa3 100644 --- a/apps/zgx/README.md +++ b/apps/zgx/README.md @@ -7,9 +7,6 @@ keeping its state, so most work never rebuilds Rust. The Rust host actions, and speaks to a Zeron engine (`src/engine/`), which owns the account, this Mac's agents and GitHub for Cloud. -The composer's current behavior, layout and sizing contracts are documented -in [Composer](docs/composer.md). - ## Running it **Dev builds talk to Zeron's staging servers, not production.** Signing in diff --git a/apps/zgx/docs/composer.md b/apps/zgx/docs/composer.md deleted file mode 100644 index bf267dfdc..000000000 --- a/apps/zgx/docs/composer.md +++ /dev/null @@ -1,236 +0,0 @@ -# Composer - -The compact and expanded composers use the same `ds.Composer` component and -retain the same textarea while changing mode. The component owns text sizing -and internal geometry; the chat screen owns placement on the page. Main-chat, -side-chat and v2 consumers share the sizing contract. - -## Structure and ownership - -| Module | Responsibility | -| --- | --- | -| [`ds/composer.luau`](../ui/ds/composer.luau) | Assemble the editor, header, controls, footer and trays; resolve theme inputs; publish sizes. | -| [`ds/composer_metrics.luau`](../ui/ds/composer_metrics.luau) | Adapt the editor's natural text metrics into coherent, revisioned samples. | -| [`ds/composer_layout.luau`](../ui/ds/composer_layout.luau) | Calculate the complete target geometry and explicit text-expansion history. | -| [`ds/composer_motion.luau`](../ui/ds/composer_motion.luau) | Present one geometry snapshot per animation frame and handle interruption/snap. | -| [`screens/chat/composer.luau`](../ui/screens/chat/composer.luau) | Bind session state and place the main composer; retain the model-picker anchor and animate new-chat sends. | -| [`screens/chat/composer_place.luau`](../ui/screens/chat/composer_place.luau) | Calculate page-placement offsets from published sizes. | -| [`screens/chat/dock.luau`](../ui/screens/chat/dock.luau) | Dock at the page's foot and calculate transcript clearance. | -| [`screens/chat/lane_tab.luau`](../ui/screens/chat/lane_tab.luau) | Bind a side chat and publish its presented composer height. | - -The data flow is editor metrics → target geometry → presented geometry → -view and published sizes → page placement/transcript clearance. Footer folding -and deck/tray layout keep their own responsibilities; their measurements feed -the size contract without supplying text-layout rules. - -## Modes and visible layout - -In compact mode, one line of text sits between the leading attachment control -and trailing controls. Text exceeding the available compact width expands the -composer; an explicit newline also expands it when width is known. New-chat -placement forces the expanded form. Attachments, an active question and -dictation also force expansion. - -Expanded text occupies the width above the controls, with 16-point left/right -insets. Attachments and questions contribute a measured header above the text. -The text viewport grows with its natural wrapped content up to its limit; -additional content scrolls inside the editor. The editor manages caret reveal -and scrolling. Its native placeholder follows IME composition; a press on blank -composer space focuses the draft without a second focus change from the press -layer. Footer chips sit below the surface, and trays/deck content keep -their existing layout and interaction behavior. - -The pure `Layout.calculate(input, previous_text_expanded)` receives available -width, the last text sample, measured controls/header, resolved theme geometry -and mode flags. It returns mode, natural/limited surface height, text rectangle, -control anchors, tray inset and the next text-expansion decision. It reads no -UI state or time and does not mutate its inputs or hidden history. - -The current geometry constants are local logical points: - -| Constraint | Value | -| --- | ---: | -| Surface border | 1 per side | -| Compact inner height / total surface height | 47 / 49 | -| Compact text viewport / fallback line height | 22 | -| Expanded text side insets | 16 per side | -| Expanded text top/bottom reservation without a header | 16 / 4 | -| Expanded text box, including that top/bottom reservation | 76–260 | -| Expanded text viewport | 56–240 | -| Leading / trailing control height | 28 / 32 | -| Text-contraction hysteresis | 32 | - -Expanded total height adds the controls' reserved area, borders and any header -adjustment to the limited text box. The controls reserve at least 42 points; -the resolved control corner can increase that reservation. Header gap, corner, -typography and decoration still use the theme's tokens. `natural_height` is -computed before the text-box clamp; it is not a displayed height or transcript -reservation. - -Text expansion uses hysteresis: compact text expands above its capacity and -an expanded draft contracts only at or below capacity minus 32 points. The -previous decision is an explicit input. Forced expansion does not overwrite -text history. Unknown width preserves that history; with known width but no -sample, the previous text decision supplies the fallback. The view commits the -next decision and target geometry together in a batch. - -## Text metrics - -`Metrics.use { value, width, editor, keep?, estimate? }` returns a computed -`result` and a read-only `sample` state (the size alone: unwrapped width, -wrapped height, line height), and creates no measurement nodes. Geometry follows -the sample without subscribing to request/readiness-only changes: a new -revision of an equal size wakes no layout. The visible -textarea supplies -`Element:text_metrics(content_width)` (Rust: `EditorHandle::text_metrics`). -It uses the editor's plain/rich shaping, spans, fonts and fallback, includes a -final empty line and displayed IME composition, and excludes placeholder, -padding, border and gutter. - -The requested expanded content width is the pill width minus both borders -and both 16-point insets. It does not follow the animated textarea's insets, -height or scroll, avoiding feedback from an intermediate viewport into the -next target geometry. - -| Result field | Meaning | -| --- | --- | -| `revision`, `width` | Current request generation and expanded content width. | -| `status` | `pending` before any sample, `stale` while retaining an older sample, `ready` for the current request. | -| `sample.unwrapped_width` | Natural width used for the compact-mode decision. | -| `sample.wrapped_height`, `sample.line_height` | Natural wrapped height at the requested width and base line height. | -| `sample.width`, `sample.revision` | Width and request generation represented by the complete sample. | - -Requests follow draft, theme and settled editor metrics, including -uncommitted IME. They follow width only while the widest line doesn't fit the -requested width: a draft that fits measures the same at any width it fits, so -a pane resized asks nothing of it. A width-only query publishes immediately when the mounted -editor, draft, theme and native metric revision match the last acknowledged -request. Mount generations distinguish editor replacement from fresh userdata -wrappers. Other requests retain deferred publication, checking request and -editor revisions; replacement or unmount cancels obsolete callbacks. Width and height publish -together. Zero width of measured text is valid for an empty draft, not an -initialization sentinel; an unknown/non-positive available width cannot -publish a fresh sample. - -With `keep`, the last complete sample survives reload but starts stale until -revalidated against the mounted editor. Layout retains the last sample while -waiting; before the first sample it uses its explicit geometry fallback. -The persistence namespaces are `editor_text_metrics` and `editor_text_size`. - -`estimate(text, line_height)` stands in for the exact query where the caller -knows the answer can't change its layout. The composer passes -`Layout.estimate`: a draft with enough hard lines to fill the text box to its -cap at the last measured line height has a newline (forced open, its unwrapped -width unused) and is capped whatever it wraps to, so it is not measured again. -Finding that out looks for at most a dozen newlines. An estimate is used only -for the editor, theme and composer line-height token of the last exact -measure; a repeated equal estimate is the same request and publishes nothing. - -Framework metrics are exact. The first query per revision/width visits all -hard lines twice, past the editor's 4,096-line span cache; unchanged queries -are constant-time. A full-document query after each edit cost a 5,000-line -draft about 10,000 script calls (its highlighting) and 47 ms a keystroke, -which is why long drafts are estimated. Focus, -selection, scroll, caret blink and viewport size alone do not change the -metric revision. The API is supplied by the immutable ZGX revision pinned in -[`Cargo.toml`](../Cargo.toml), from `zeronsh/zgx` main (zgx#2148). - -## Geometry transitions and context changes - -One controller keeps origin, destination and progress, with a 220 ms `out(3)` -animation. Every view binding derives from the same presented snapshot. A changed -destination cancels the previous animation and starts from the geometry -currently presented. An unchanged visual destination retains progress, even -if natural height above the cap or other metadata changes. The view publishes -a new target only when a field it binds or the motion reads changes -(`Layout.same_target`): the pill's and text's widths, which layout applies -itself, and the natural height past the cap don't, so resizing a pane runs no -motion, frame or binding. -View bindings share a projection of the complete presented snapshot, so changes -to width or measurement metadata alone do not wake unchanged controls/insets. - -The vertical envelope and controls move together. During expansion, the editor -keeps compact width until a complete line fits above both control groups, then -switches once to the expanded width. During contraction, it takes compact width -immediately. The text top and height continue on the same vertical trajectory; -width is not continuously interpolated during a mode change. - -The viewport derives from that frame's surface, top and bottom reservations. -A separate animated clip surrounds the naturally measured header; the clip's -height never feeds back into the header measurement. These transitions retain -the textarea, draft, focus and selection, and do not write editor scroll. - -Reduced motion cancels animation and snaps to the destination. A context change -also snaps, holding that policy until the current metrics are ready and the pill -has been laid out at its target height. A deferred acknowledgement verifies -context, revision and destination before ending settlement. Page placement uses -the same context acknowledgement. Cleanup cancels animation/acknowledgement; -obsolete generations cannot update a replacement controller. - -## Published sizes and page placement - -`Composer.on_sizes` reports: - -| Field | Meaning | -| --- | --- | -| `rest` | Surface height calculated with no draft/cargo/question/dictation, retaining the forced expanded flag, plus the currently measured footer and deck. | -| `target` | Target surface height plus measured footer and deck. | -| `presented` | Actual laid-out root height, including footer/deck. | -| `active` | Whether the internal geometry transition is running. | -| `ready` | Current metrics are valid and the pill is laid out at target height. | -| `settling`, `context` | Context snap status and the generation the snapshot belongs to. | - -A new chat stands near the page's middle, under the title bar. Its top is based -on `rest`, so typing grows the composer downward without moving that top. If -the presented box would overflow the page's foot, placement lifts it by the -required amount. A session anchors at the bottom with the dock's 8-point gap. -The main chat publishes presented height through `shared.dock_h`; side chats' -`Dock.Dock` publishes their laid-out height for their transcript clearance (they -take no `on_sizes`). `on_sizes` is called only when one of its fields changes. - -While a new chat's model picker is open, a height compensation keeps its chip -and picker anchored. Closing the picker releases that compensation with a -260 ms `out(3)` animation. Sending from a new chat deliberately glides to the -session dock with the same duration, and the composer eases from its new-chat -form to its docked one on the way (no snap). The send records the session it -opens, so both effects that see the move (the page mode's and the routed -chat's) know it for a send whichever runs first. Other route/split-context changes snap; -reduced motion snaps these page movements too. Sidebar/page resizing follows -layout. The rest height and context settlement use no fixed waiting window. - -## Verification and visual diagnostics - -Run from the repository root: - -```sh -cargo test --locked --manifest-path apps/zgx/Cargo.toml --bin zeron-zgx composer -cargo test --locked --manifest-path apps/zgx/Cargo.toml --bin zeron-zgx zeron_v2 -``` - -The tests cover mode boundaries, headers, metrics/IME, interruption, context -revisions, cleanup, editor identity/focus/selection, max-height scrolling, -dictation layout, theme/transparency and logical resize/zoom reflow. Route-motion -checks compare against a second, reduced-motion shell. Set -`ZERON_ZGX_COMPOSER_TRACE=1` and use `-- --nocapture` to retain per-frame traces. - -For a local visual check, build the development binary (beta omits the fixture -CLI) and run the isolated composer preview: - -```sh -cargo build --locked --manifest-path apps/zgx/Cargo.toml --bin zeron-zgx -python3 apps/zgx/scripts/composer_frames.py --binary apps/zgx/target/debug/zeron-zgx --output .personal/composer/normal -python3 apps/zgx/scripts/composer_frames.py --binary apps/zgx/target/debug/zeron-zgx --output .personal/composer/damage --damage-check -``` - -The Linux runner needs Python, Xvfb, xdotool and ffmpeg. It records real frames, -input timing, text-region ink counts and partial/full repaint diagnostics. -The region check can detect whole-region disappearance but can miss local -clipping or missing glyphs. Diagnostic repaints alter timing, so always retain -a separate normal run. On macOS, compare native caret behavior separately with -`ZGX_NATIVE_CARET=0`; record build/platform, window size, zoom, theme, -transparency, display scale/refresh rate and the exact input sequence. - -The reported intermittent glitch has not been established as reproduced or -fixed. Headless geometry and Linux captures do not verify macOS/Windows -presentation, native IME or device-scale rendering. Existing broader-suite -failures should be assessed separately from these composer checks. diff --git a/apps/zgx/docs/plugins.md b/apps/zgx/docs/plugins.md index 5ac07018a..6ad2dd6bd 100644 --- a/apps/zgx/docs/plugins.md +++ b/apps/zgx/docs/plugins.md @@ -52,8 +52,7 @@ Your own plugins are signed as yours, and re-signed and reloaded each time you s | `navigate` | open chats and pages (`zeron.open`) | | `links.open` | open http(s) links (`zeron.open_url`), where you open links (Settings → Links) | | `notifications` | send you notifications (`zeron.notify`), as Zeron's own: Settings → Notifications decides whether they show, a few a minute at most | -| `compose` | put text in the message box, after what's there or in place of its words (`zeron.compose`), or a review comment on it (`zeron.compose_comment`); you still send it | -| `draft` | read the message box's draft as you type it (`zeron.draft`), or any chat's (`zeron.draft_of`), and take it whole, put it back, swap it or let it go (`zeron.take_draft`, `put_draft`, `swap_draft`, `drop_draft`). See [The composer's draft](#the-composers-draft) | +| `compose` | put text in the message box (`zeron.compose`), or a review comment on it (`zeron.compose_comment`); you still send it | | `github`, `github.write` | read (and write) GitHub through Zeron's engine (older calls; prefer `commands` with `gh`) | | `network` | the internet: `ui.fetch`, and remote images (`ui.image { src = "https://…" }`). See [Reaching the network](#reaching-the-network) | | `clipboard`, `files`, `web`, `windows` | zgx's clipboard, file dialogs, web views, windows | @@ -214,13 +213,9 @@ Zeron draws that row on your settings page: "Use as wallpaper", and while it's o | call | what | needs | |---|---|---| | `zeron.open("home" \| { session = id } \| { settings = page })` | open a page | `navigate` | -| `zeron.compose(text, { replace?, session? })` | text into a chat's draft: after what's there (on a line of its own), or in place of its words (`replace = true`, its chips kept); the chat shown's draft, or `session`'s ([below](#the-composers-draft)) | `compose` | +| `zeron.compose(text)` | text into the message box | `compose` | | `zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? }` | a review comment on a file or a diff, as a chip on a chat's draft (below) | `compose` | | `zeron.composed()` | in a build: your comments still on a draft, by `key`: `{ session, hovered }` | `compose` | -| `zeron.draft()` | in a build: the message box's draft, `{ session?, text, attachments = { { kind, name } } }` (below) | `draft` | -| `zeron.take_draft(key)`, `zeron.put_draft(key)`, `zeron.swap_draft(key)`, `zeron.drop_draft(key)` | the draft taken whole and kept by Zeron under your key; put back; traded with what the key keeps; let go (below) | `draft` | -| `zeron.drafts()` | in a build: what Zeron keeps for you, by key: `{ session?, text, attachments, at }` | `draft` | -| `zeron.draft_of(session)` | a promise of chat `session`'s draft as it is now, as `zeron.draft()` says it (`""`: a new chat's) | `draft` | | `zeron.toast(text)` | a short note at the window's foot | (always) | | `zeron.need("panel").open_tab { kind, title, icon?, data? }` | a tab of yours in the chat's panel; asked again, the one there is brought forward (found by `data.key` when you give one, else by its title) | `panel.tabs` | | `zeron.tab(kind, function(tab) return node end)` | how a kind of your tabs draws (`tab` = `{ kind, title, data }`) | `panel.tabs` | @@ -279,48 +274,6 @@ Comments on the diff (each cites the file and line it belongs to; L = line numbe Give each a `key` of your own: `zeron.composed()` lists yours still on a draft by it (taken off or sent, gone), and says which one's chip is under the pointer (`hovered`), so the lines it was left on can keep a mark and light up as its chip is pointed at. Zeron's Git plugin does all of this in its Changes (`plugins-builtin/git/changes.luau`): the diff's + (or a drag from it over lines) opens `ds/comment`'s composer under them, using `ds/git_changes`'s `on_comment`, `ranges` and `marks`. -### The composer's draft - -With `draft`, a plugin reads what you're typing and can set it aside and bring it back: a stash for a prompt written in the wrong chat, a counter of its length, a bar that says what it carries. - -- **`zeron.draft()`**, read in a build or an effect, is the draft shown as it changes: `session` (the chat it's for; nil on a new chat), `text` (the words as typed) and `attachments`, each chip's `kind` (`"file"`, `"image"`, `"chat"`, `"comment"`, …) and `name`. A file's path is never in it. -- **`zeron.take_draft(key)`** takes the draft shown whole (its words, its chips, the passages quoted in it) and clears the composer. Zeron keeps it, under your `key` (1 to 64 characters), so your plugin never holds a file's path. An empty draft isn't taken; taking under a key that's kept replaces what it kept. Up to 16 at once. -- **`zeron.put_draft(key)`** puts what `key` kept back on the draft shown, in whichever chat is open, and lets it go. Onto an empty composer it's as it was taken; onto words already there, its words follow after a blank line and its chips after theirs. -- **`zeron.swap_draft(key)`** trades the draft shown with what `key` keeps. Either may be empty, so one call sets the draft aside (nothing kept), brings it back (an empty composer), or swaps the two. -- **`zeron.drop_draft(key)`** lets what `key` kept go. -- **`zeron.drafts()`**, read in a build, is what Zeron keeps for you, by key: `{ session?, text, attachments, at }` (`at` as `os.time()`). Drafts are kept while Zeron runs, as chats' own drafts are. -- **`zeron.draft_of(session)`** reads another chat's draft once, shown or not: a promise of `{ session?, text, attachments }`, as `zeron.draft()` says it (`""` for a new chat's, with no session). - -With `compose` too, a plugin can change the words in place: **`zeron.compose(text, { replace = true })`** puts `text` in place of the draft's words, leaving its chips where they are, and `session` names another chat's draft. A rewrite or a snippet that expands is a read and a replace: - -```lua --- ("ui.add", "draft", "compose"): /bug alone in the box becomes the template. -ui.effect(function() - local d = zeron.draft() - if d and d.text == "/bug" then - zeron.compose("## What happened\n\n## What I expected\n\n## Steps\n", { replace = true, session = d.session }) - end -end) -``` - -Nothing is sent: the person sees the new words in the composer, and sends or edits them. `zeron.compose` takes 20,000 characters at most. - -Each is done in the order you ask. A prompt stash is one shortcut and one chip: - -```lua --- ⌘⇧S (Ctrl+Shift+S): set the draft aside, bring it back, or swap the two. -ui.command { id = "stash", title = "Stash prompt", key = "cmd+shift+s", run = function() - zeron.swap_draft("stash") -end } - --- While one is kept, a chip over the composer: click to bring it back. -zeron.place { at = "#chat-composer", before = function(): Node - return ui.show(function(): boolean - return zeron.drafts().stash ~= nil - end, ui.button { "Stashed", on_click = function() zeron.swap_draft("stash") end }) -end } -``` - ## Programs and a database A plugin can do real work of its own: run the command-line tools you already use, and keep what it learns. diff --git a/apps/zgx/plugins-builtin/git/zeron.luau b/apps/zgx/plugins-builtin/git/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-builtin/git/zeron.luau +++ b/apps/zgx/plugins-builtin/git/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-builtin/github/zeron.luau b/apps/zgx/plugins-builtin/github/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-builtin/github/zeron.luau +++ b/apps/zgx/plugins-builtin/github/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau b/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau +++ b/apps/zgx/plugins-builtin/parallax-scenes/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/account-pin/zeron.luau b/apps/zgx/plugins-dev/account-pin/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/account-pin/zeron.luau +++ b/apps/zgx/plugins-dev/account-pin/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/account-rotate/zeron.luau b/apps/zgx/plugins-dev/account-rotate/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/account-rotate/zeron.luau +++ b/apps/zgx/plugins-dev/account-rotate/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/checkout-status/zeron.luau b/apps/zgx/plugins-dev/checkout-status/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/checkout-status/zeron.luau +++ b/apps/zgx/plugins-dev/checkout-status/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/compact-sidebar/zeron.luau b/apps/zgx/plugins-dev/compact-sidebar/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/compact-sidebar/zeron.luau +++ b/apps/zgx/plugins-dev/compact-sidebar/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/composer-cat/zeron.luau b/apps/zgx/plugins-dev/composer-cat/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/composer-cat/zeron.luau +++ b/apps/zgx/plugins-dev/composer-cat/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/focus/zeron.luau b/apps/zgx/plugins-dev/focus/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/focus/zeron.luau +++ b/apps/zgx/plugins-dev/focus/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/keep-going/zeron.luau b/apps/zgx/plugins-dev/keep-going/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/keep-going/zeron.luau +++ b/apps/zgx/plugins-dev/keep-going/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/md-hello/zeron.luau b/apps/zgx/plugins-dev/md-hello/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/md-hello/zeron.luau +++ b/apps/zgx/plugins-dev/md-hello/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/notes-tool/zeron.luau b/apps/zgx/plugins-dev/notes-tool/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/notes-tool/zeron.luau +++ b/apps/zgx/plugins-dev/notes-tool/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/ping/zeron.luau b/apps/zgx/plugins-dev/ping/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/ping/zeron.luau +++ b/apps/zgx/plugins-dev/ping/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/session-status/zeron.luau b/apps/zgx/plugins-dev/session-status/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/session-status/zeron.luau +++ b/apps/zgx/plugins-dev/session-status/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/session-words/zeron.luau b/apps/zgx/plugins-dev/session-words/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/session-words/zeron.luau +++ b/apps/zgx/plugins-dev/session-words/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-dev/settings-demo/zeron.luau b/apps/zgx/plugins-dev/settings-demo/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-dev/settings-demo/zeron.luau +++ b/apps/zgx/plugins-dev/settings-demo/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/plugins-sdk/zeron.luau b/apps/zgx/plugins-sdk/zeron.luau index 553b6cc16..c5f896393 100644 --- a/apps/zgx/plugins-sdk/zeron.luau +++ b/apps/zgx/plugins-sdk/zeron.luau @@ -69,31 +69,13 @@ -- zeron.on_message(function(message) end) -- what arrives: { from = { id, name }, data, at } ("devices") -- zeron.open(route) "home", { session = id }, { settings = page } ("navigate") --- zeron.compose(text, { replace?, session? }) --- text into the message box, for you to send: after --- what's there, or in place of its words (`replace = --- true`, its chips kept); the chat shown's draft, or --- `session`'s ("compose") +-- zeron.compose(text) text into the message box, for you to send ("compose") -- zeron.compose_comment { file, line, note, to?, side?, snippet?, key?, session? } -- a review comment chip on a session's draft, sent as -- Zeron's review comments block (file:line, the note, -- the code under it) ("compose") -- zeron.composed() in a build: your comments still on a draft, by key -- ({ [key] = { session, hovered } }), reactive ("compose") --- zeron.draft() the composer's draft as you type it: { session?, --- text, attachments = { { kind, name } } }, reactive --- ("draft") --- zeron.take_draft(key), zeron.put_draft(key), zeron.swap_draft(key), --- zeron.drop_draft(key) --- the draft taken whole (words, chips, quotes) and --- kept by Zeron under your key, the composer --- cleared; put back on the draft shown; the two --- traded; let go ("draft") --- zeron.drafts() what Zeron keeps for you, by key: { session?, text, --- attachments, at }, reactive ("draft") --- zeron.draft_of(session) --- a promise of another chat's draft, as `draft()` --- says it, read once ("" a new chat's) ("draft") -- zeron.toast(text) a short note at the window's foot (always) -- zeron.notify { title, body?, session? } a notification, as Zeron's -- own (Settings → Notifications) ("notifications") @@ -1361,16 +1343,8 @@ return function(id: string): any ui.invoke(`{prefix}.open_url`, url) end - -- `text` into a chat's draft, for the person to send ("compose"): after - -- what's there, on a line of its own; with `replace = true`, in place of - -- its words (what it carries stays). The chat shown's draft, or - -- `session`'s ("" a new chat's). - function M.compose(text: string, opts: { replace: boolean?, session: string? }?) - if opts and (opts.replace or opts.session) then - ui.invoke("zeron.compose.insert", { text = text, replace = opts.replace == true, session = opts.session or ui.null }) - else - ui.invoke("zeron.compose.insert", text) - end + function M.compose(text: string) + ui.invoke("zeron.compose.insert", text) end -- A review comment on a file or a diff, as a chip on a session's draft @@ -1400,57 +1374,6 @@ return function(id: string): any return composed:get() or {} end - -- The composer's draft as you type it: the chat it's for (nil, a new - -- chat), its words, and what it carries, each chip's kind ("file", - -- "image", "chat", …) and name, never a file's path. Read in a build or - -- an effect: it changes as the draft does. Nil until Zeron says. - -- (Reached on first read: only a plugin allowed "draft" may.) - local draft: any = nil - function M.draft(): { session: string?, text: string, attachments: { { kind: string, name: string } } }? - draft = draft or ui.shared("zeron.draft", nil :: any) - return draft:get() - end - - -- The draft shown, taken whole (its words, its chips, the passages - -- quoted in it) and kept by Zeron under `key`, yours, the composer - -- cleared; what that key kept before is replaced. An empty draft isn't - -- taken. Up to 16 kept at once. - function M.take_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "take", key = key }) - end - - -- What `key` kept, put back on the draft shown as it was taken (after - -- what's there, if the composer isn't empty), and no longer kept. - function M.put_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "put", key = key }) - end - - -- The draft shown and what `key` keeps, traded: either may be empty, so - -- one call sets the draft aside, brings it back, or swaps the two. - function M.swap_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "swap", key = key }) - end - - -- What `key` kept, let go of. - function M.drop_draft(key: string) - ui.invoke(`{prefix}.draft`, { op = "drop", key = key }) - end - - -- What Zeron keeps for you, by key: the chat each was taken from, its - -- words, what it carries (as `draft()` says it) and when (os.time). - -- Read in a build: it changes as you take and put back. - local drafts = ui.shared(`{prefix}.drafts`, {} :: { [string]: any }) - function M.drafts(): { [string]: { session: string?, text: string, attachments: { { kind: string, name: string } }, at: number } } - return drafts:get() or {} - end - - -- Chat `session`'s draft as it is now, unsent: shown or not (`draft()` - -- follows the one shown). A promise of { session?, text, attachments } - -- as `draft()` says it ("" a new chat's draft, with no session). - function M.draft_of(session: string): any - return ui.call(`{prefix}.draft_of`, session) - end - function M.toast(text: string) ui.invoke(`{prefix}.toast`, text) end diff --git a/apps/zgx/scripts/composer_frames.py b/apps/zgx/scripts/composer_frames.py deleted file mode 100644 index 570934656..000000000 --- a/apps/zgx/scripts/composer_frames.py +++ /dev/null @@ -1,174 +0,0 @@ -#!/usr/bin/env python3 -"""Record the isolated composer fixture on Linux X11 and check its text region. - -Needs Xvfb, xdotool and ffmpeg. Run normal and --damage-check separately: the -comparison repaint changes rendering behavior. Output is local diagnostic data. -""" -import argparse -import json -import os -from pathlib import Path -import platform -import re -import shutil -import subprocess -import time - - -def run(binary, output, damage_check): - for tool in ("Xvfb", "xdotool", "ffmpeg"): - if not shutil.which(tool): - raise SystemExit(f"required executable missing: {tool}") - output.mkdir(parents=True, exist_ok=True) - events = [] - processes = [] - logs = [] - - def launch(args, name, **kwargs): - log = (output / name).open("w") - logs.append(log) - process = subprocess.Popen(args, stdout=log, stderr=subprocess.STDOUT, **kwargs) - processes.append(process) - return process - - try: - read, write = os.pipe() - launch(["Xvfb", "-displayfd", str(write), "-screen", "0", "1280x900x24", - "-nolisten", "tcp"], "xvfb.log", pass_fds=(write,)) - os.close(write) - display = ":" + os.read(read, 64).decode().strip() - os.close(read) - env = os.environ.copy() - env.pop("WAYLAND_DISPLAY", None) - env.pop("ZGX_DAMAGE_CHECK", None) - env.update(DISPLAY=display, XDG_CONFIG_HOME=str(output / "config"), - XDG_DATA_HOME=str(output / "data"), ZERON_ZGX_ENGINE_SPAWN="0", - ZERON_ZGX_ENGINE_PORT="27963", ZERON_ZGX_ENGINE_DATA=str(output / "engine")) - if damage_check: - env["ZGX_DAMAGE_CHECK"] = "1" - app = launch([str(binary), "fixture=composer_transition_preview.luau", "--inspect"], - "app.log", env=env) - - def x(*args, check=True): - return subprocess.run(["xdotool", *map(str, args)], env=env, - capture_output=True, text=True, check=check) - - window = None - for _ in range(100): - if app.poll() is not None: - raise RuntimeError("app exited before its window appeared; see app.log") - found = x("search", "--onlyvisible", "--pid", app.pid, check=False) - if found.returncode == 0 and found.stdout.strip(): - window = found.stdout.splitlines()[-1] - break - time.sleep(.1) - if window is None: - raise RuntimeError("window did not appear within 10 seconds") - x("windowsize", window, 1200, 800) - x("windowfocus", "--sync", window) - # Allow first presentation/font setup; all timings below are native - # input pacing for a recording, never product readiness deadlines. - time.sleep(1) - video = output / "frames.mkv" - recording = launch(["ffmpeg", "-y", "-loglevel", "error", "-f", "x11grab", - "-framerate", "60", "-video_size", "1280x900", "-i", display, - "-t", "12", "-c:v", "ffv1", "-threads", "2", str(video)], - "capture.log", env=env) - start = time.monotonic() - - def click(name, at, delay): - events.append({"seconds": round(time.monotonic() - start, 3), "action": name}) - x("mousemove", at, 32, "click", 1) - time.sleep(delay) - - time.sleep(.5) - click("ten lines", 115, .08) - click("interrupt with eighty", 195, .06) - click("reverse to short", 43, .08) - click("eighty lines", 195, .5) - click("attachment header", 279, .4) - click("short with header", 43, .4) - click("remove header", 279, .4) - click("long draft for scroll/edit", 195, .4) - x("mousemove", 150, 640, "click", 1) - x("key", "ctrl+End") - x("click", "--repeat", 5, "--delay", 30, 4) - x("type", "--delay", 20, " typed after scrolling") - events.append({"seconds": round(time.monotonic() - start, 3), "action": "scroll and character typing"}) - click("short", 43, .25) - click("wrap", 470, .4) - click("reverse wrap", 43, .08) - click("interrupt wrap", 470, .3) - click("reduce motion", 380, .1) - click("short snap", 43, .05) - click("long snap", 195, .2) - # Move the pointer away before sampling CPU; caret blink and - # engine reconnects remain valid background work in this fixture. - x("mousemove", 900, 400, "click", 1) - time.sleep(.5) - - def cpu_ticks(): - fields = Path(f"/proc/{app.pid}/stat").read_text().rsplit(")", 1)[1].split() - return int(fields[11]) + int(fields[12]) - - idle = [] - for _ in range(3): - before, at = cpu_ticks(), time.monotonic() - time.sleep(.6) - idle.append(100 * (cpu_ticks() - before) / os.sysconf("SC_CLK_TCK") / (time.monotonic() - at)) - recording.wait(timeout=20) - if recording.returncode: - raise RuntimeError("capture failed; see capture.log") - # This fixed fixture region excludes toolbar/footer/buttons. A - # minimum > 50 cannot be satisfied by a caret alone. It detects whole - # text-region disappearance, not every local glyph/clip defect. - decode = subprocess.Popen(["ffmpeg", "-loglevel", "error", "-i", str(video), - "-vf", "crop=540:360:50:390,format=gray", "-f", "rawvideo", "-"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - threshold = bytes(int(i > 120) for i in range(256)) - counts = [] - frame_size = 540 * 360 - while True: - frame = decode.stdout.read(frame_size) - if not frame: - break - if len(frame) != frame_size: - raise RuntimeError("incomplete decoded frame") - counts.append(frame.translate(threshold).count(b"\1")) - if decode.wait(): - raise RuntimeError(decode.stderr.read().decode()) - app_log = (output / "app.log").read_text() - stale = re.findall(r"ZGX_DAMAGE_CHECK frame .*", app_log) - report = {"platform": platform.platform(), "content_size": [1200, 800], - "capture_size": [1280, 900], "fps": 60, "frames": len(counts), - "theme": "zeron-dark", "zoom": "100%", "transparency": "default frosted", - "damage_check": damage_check, "minimum_text_pixels": min(counts, default=0), - "suspicious_frames": [i for i, count in enumerate(counts) if count <= 50], - "damage_mismatches": stale, "idle_cpu_percent": idle, - "events": events, "text_pixels_by_frame": counts, - "limitations": "Whole-region check; native macOS/Windows and original glitch reproduction unverified."} - (output / "report.json").write_text(json.dumps(report, indent=2) + "\n") - print(json.dumps({k: v for k, v in report.items() if k not in ("events", "text_pixels_by_frame")}, indent=2)) - if len(counts) < 600 or report["suspicious_frames"] or stale: - raise SystemExit("frame validation failed; inspect report.json and frames.mkv") - finally: - for process in reversed(processes): - if process.poll() is None: - process.terminate() - try: - process.wait(timeout=5) - except subprocess.TimeoutExpired: - process.kill() - process.wait() - for log in logs: - log.close() - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--binary", type=Path, required=True, - help="development zeron-zgx build; beta omits the fixture CLI") - parser.add_argument("--output", type=Path, required=True) - parser.add_argument("--damage-check", action="store_true") - args = parser.parse_args() - run(args.binary.resolve(), args.output.resolve(), args.damage_check) diff --git a/apps/zgx/src/agents.rs b/apps/zgx/src/agents.rs index 00506865c..590cc042c 100644 --- a/apps/zgx/src/agents.rs +++ b/apps/zgx/src/agents.rs @@ -12,19 +12,7 @@ //! login_methods = { { id, name, description?, kind, var?, //! link? } } //! (an agent from the ACP registry, as it says it is), -//! accounts = { { id, email, plan, session, week, active, -//! switchable, provider? } }, -//! groups?, login_for? } } } -//! An agent that keeps a login per model provider (Pi, OpenCode, Hermes) -//! also carries `groups`, its accounts by provider (`provider_groups`): -//! `{ { id, name, accounts, login?, key?, hint?, read_only } }`, each with -//! its own one in use and its own ways to add one (`login`, who Zeron signs -//! you in to; `key`, who issues the API key it adds; neither: `hint` says -//! how the agent does), and -//! `login_for`, the provider its sign-in under way is for. `agents.logins` -//! says each such agent's providers, `{ [harness] = { [provider] = { name, -//! login?, key? } } }`, for a composer's Account row; `agents.on_device` rows carry -//! their `provider` and its `group` name. +//! accounts = { { id, email, plan, session, week, active } } } } } //! Each agent carries all it shows, so a page keys its rows by `id` and an //! account by its `id`, and they update in place. (`session` and `week` are //! the plan's two windows, 0 to 1; `mark` is the design system's name for @@ -37,14 +25,6 @@ //! agents.cancel(id) (an action) gives up that sign-in //! agents.sign_in({ id, method }) the same, by the method an agent from the //! ACP registry offers (`login_methods`) -//! agents.sign_in({ id, provider }) the same, to one model provider of an -//! agent that keeps a login per provider -//! agents.rename({ id, account, name }) an API key's name; rejects with -//! the engine's words -//! agents.add_key({ id, provider, key, name? }) an API key for one such provider, -//! on this Mac's agent only (`remote` with -//! another device shown), checked with the -//! provider; rejects with the engine's words //! agents.set_key({ id, var, value }) a key such an agent reads from its //! environment (a `login_methods` kind //! "env_var"), kept there; failed @@ -135,9 +115,6 @@ struct Login { page: Option, /// A device code it showed, to check against that page. pairing: Option, - /// The model provider it signs in to, for an agent that keeps a login - /// per provider (its group's add row shows it); none: the agent's own. - provider: Option, } /// An agent from the ACP registry's sign-in, as it says it is @@ -219,8 +196,6 @@ thread_local! { /// (`{ ["acp."] = svg }`): ds/agent_mark finds an agent's icon there /// by its harness, so no caller has to carry it. static ICONS_SHARED: RefCell> = RefCell::default(); - /// `agents.logins`, held for every window's scripts. - static LOGINS_SHARED: RefCell> = RefCell::default(); } /// Keeps the icons a harness list carries (agents from the ACP registry). @@ -298,8 +273,6 @@ pub fn install(host: &ScriptHost) { let agents = AGENTS.with(Rc::clone); agents.shared.borrow_mut().push(host.shared("agents", agents.value())); agents.device_shared.borrow_mut().push(host.shared("agents.on_device", agents.device_value())); - // (What never changes: each per-provider agent's sign-ins.) - LOGINS_SHARED.with(|shared| shared.borrow_mut().push(host.shared("agents.logins", logins_value()))); ICONS_SHARED.with(|shared| shared.borrow_mut().push(host.shared("agent_icons", icons_value()))); if agents.shared.borrow().len() == 1 { // Usage moves as turns run: looked at again while the app is open. @@ -382,64 +355,12 @@ pub fn install(host: &ScriptHost) { let a = agents.clone(); host.async_action("agents.sign_in", move |args| { let a = a.clone(); - // `id`, or `{ id, method }` (a registry agent's method of its - // choosing), or `{ id, provider }` (one model provider's login of an - // agent that keeps one per provider). - let field = |key: &str| args.field(key).map(text).filter(|m| !m.is_empty()); - let (id, method, provider) = match args.field("id") { - Some(id) => (text(&id), field("method"), field("provider")), - None => (text(&args), None, None), + // `id`, or `{ id, method }` (a registry agent's method of its choosing). + let (id, method) = match args.field("id") { + Some(id) => (text(&id), args.field("method").map(text).filter(|m| !m.is_empty())), + None => (text(&args), None), }; - async move { - crate::plugins::preview::refuse("agents.sign_in")?; - a.sign_in(id, method, provider).await.map_err(String::from) - } - }); - // An API key for one provider of an agent that keeps a login per - // provider: this Mac's agents only (a key never goes to another - // device), rejecting with why not in the engine's words — which never - // hold the key. - let a = agents.clone(); - host.async_action("agents.add_key", move |args| { - let a = a.clone(); - let id = args.field("id").map(text).unwrap_or_default(); - let provider = args.field("provider").map(text).unwrap_or_default(); - let key = args.field("key").map(text).unwrap_or_default(); - let name = args.field("name").map(text).filter(|n| !n.trim().is_empty()); - async move { - crate::plugins::preview::refuse("agents.add_key")?; - a.add_key(id, provider, key, name).await - } - }); - // An API key for Cloud's agents (Settings → Providers with Cloud - // shown): kept by the vault, authorized for the account's Cloud device - // (`device`), the key every Cloud machine uses from its next turn. - // Rejects with the engine's words, which never hold the key. - let a = agents.clone(); - host.async_action("agents.cloud_key", move |args| { - let a = a.clone(); - let provider = args.field("provider").map(text).unwrap_or_default(); - let key = args.field("key").map(text).unwrap_or_default().trim().to_string(); - let device = args.field("device").map(text).unwrap_or_default(); - async move { - crate::plugins::preview::refuse("agents.cloud_key")?; - let params = cloud_key_params(&provider, &key, &device)?; - engine::get().call("VaultPutApiKey", params).await.map_err(|error| engine::outcome(&error).to_string())?; - a.load_accounts(true).await; - Ok(Value::Nil) - } - }); - // An API key's name (no secret: on whichever device shows it), rejecting - // with the engine's words. - let a = agents.clone(); - host.async_action("agents.rename", move |args| { - let a = a.clone(); - let (id, account) = harness_and_account(&args); - let name = args.field("name").map(text).unwrap_or_default(); - async move { - crate::plugins::preview::refuse("agents.rename")?; - a.rename(id, account, name).await - } + async move { crate::plugins::preview::refuse("agents.sign_in")?; a.sign_in(id, method).await.map_err(String::from) } }); let a = agents.clone(); host.async_action("agents.set_key", move |args| { @@ -580,90 +501,6 @@ pub(crate) fn glyph(id: &str) -> Option { } } -/// `id` as the engine's harness (an agent from the ACP registry's too). -fn harness_id(id: &str) -> Option { - serde_json::from_value(json!(id)).ok() -} - -/// One provider's accounts of an agent that keeps a login per model provider. -#[derive(Debug, PartialEq)] -struct Group { - /// Its provider's id (`openai-codex`, `anthropic`): its accounts' - /// `provider`, a sign-in's and a key's. - provider: String, - /// The provider ("ChatGPT", "Anthropic", "OpenRouter"). - name: String, - /// Its accounts: indices into the agent's list, in its order. - rows: Vec, - /// Who Zeron signs you in to for it ("ChatGPT", "Claude"): none where it - /// can't. - login: Option, - /// Who issues the API key Zeron adds for it ("OpenRouter"): none where - /// it adds none. - key: Option, -} - -/// An agent's accounts by provider: first every provider Zeron signs in to, -/// in its order, then those it adds API keys for, then any other its -/// accounts are on, in the order they come. A provider with none yet is -/// one to add (its sign-in or key). An account naming no provider (an -/// older engine's) is in the first group. -fn provider_groups(harness: zeron_proto::HarnessId, accounts: &[Json]) -> Vec { - use zeron_proto::providers::{account_group_label, api_key_option, api_key_options, login_option, login_options}; - let mut order: Vec = login_options(harness).into_iter().filter_map(|o| o.provider.map(String::from)).collect(); - for option in api_key_options(harness) { - if !order.iter().any(|p| p == option.provider) { - order.push(option.provider.to_owned()); - } - } - let first = order.first().cloned().unwrap_or_default(); - let providers: Vec = - accounts.iter().map(|a| a["provider"].as_str().map(String::from).unwrap_or_else(|| first.clone())).collect(); - for provider in &providers { - if !order.contains(provider) { - order.push(provider.clone()); - } - } - order - .into_iter() - .map(|provider| { - let rows: Vec = (0..accounts.len()).filter(|&i| providers[i] == provider).collect(); - Group { - name: account_group_label(harness, &provider), - login: login_option(harness, &provider).map(|o| o.label.to_owned()), - key: api_key_option(harness, &provider).map(|o| o.label.to_owned()), - rows, - provider, - } - }) - .filter(|g| !g.rows.is_empty() || g.login.is_some() || g.key.is_some()) - .collect() -} - -/// Each per-provider agent's providers Zeron adds to, for a composer's -/// Account row to offer (`agents.logins`): `{ [harness] = { [provider] = { -/// name, login?, key? } } }` — who it signs you in to, who issues the key. -fn logins_value() -> Value { - use zeron_proto::providers::{groups_by_provider, PROVIDERS}; - let by = PROVIDERS.iter().filter(|(h, _, _)| groups_by_provider(*h)).filter_map(|(harness, _, _)| { - let id = serde_json::to_value(harness).ok()?.as_str()?.to_owned(); - let groups: Vec<(String, Value)> = provider_groups(*harness, &[]) - .into_iter() - .map(|g| { - let value = Value::record([ - ("name", Value::from(g.name.as_str())), - ("login", g.login.as_deref().map_or(Value::Nil, Value::from)), - ("key", g.key.as_deref().map_or(Value::Nil, Value::from)), - ]); - (g.provider, value) - }) - .collect(); - Some((id, Value::record(groups.iter().map(|(k, v)| (k.as_str(), v.clone()))))) - }); - let by: Vec<(String, Value)> = by.collect(); - Value::record(by.iter().map(|(k, v)| (k.as_str(), v.clone()))) -} - /// The sign-in an agent's Add account starts: an agent signing into several /// providers starts its first (crates/proto/src/providers.rs /// `login_options`), the one the shipping app lists first. @@ -752,41 +589,9 @@ impl Agents { Some(login) if login.failed => "failed", _ => "idle", }; - // An agent that keeps a login per model provider: its - // accounts by provider, each group with its own in use and - // its own way to add one (nil: one group, as ever). - let groups = harness_id(id).filter(|h| zeron_proto::providers::groups_by_provider(*h)).map(|harness| { - let list = accounts.get(id).map(Vec::as_slice).unwrap_or_default(); - let read_only = !zeron_proto::providers::switches_accounts(harness); - let groups: Vec = provider_groups(harness, list) - .into_iter() - .map(|g| { - let rows: Vec = g.rows.iter().map(|&i| account_value(&list[i])).collect(); - Value::record([ - ("id", Value::from(g.provider.as_str())), - ("name", Value::from(g.name.as_str())), - ("login", g.login.as_deref().map_or(Value::Nil, Value::from)), - ("key", g.key.as_deref().map_or(Value::Nil, Value::from)), - ("read_only", Value::Bool(read_only)), - ("accounts", Value::List(rows.into())), - ]) - }) - .collect(); - Value::List(groups.into()) - }); Value::record([ ("id", Value::from(id)), ("name", Value::from(h["name"].as_str().unwrap_or(id))), - ("groups", groups.unwrap_or(Value::Nil)), - // Which provider's group the sign-in under way is for. - ( - "login_for", - logins - .get(id) - .and_then(|l| l.provider.as_deref()) - .filter(|_| login != "idle") - .map_or(Value::Nil, Value::from), - ), ("mark", mark(id).map_or(Value::Nil, Value::from)), // An agent added from the ACP registry: its one-ink icon // (it has no mark), and no accounts of Zeron's to show. @@ -1115,22 +920,8 @@ impl Agents { fn keep_device(&self, key: String, snapshot: &Json) { let Some(accounts) = snapshot["accounts"].as_array() else { return }; - let changed = { - let mut devices = self.device_accounts.borrow_mut(); - let device = devices.entry(key.clone()).or_default(); - // (The first sight of a device's accounts changes nothing: its - // models were looked up with them.) - let changed = if device.seen { changed_logins(&device.accounts, accounts) } else { Vec::new() }; - device.seen = true; - device.accounts = accounts.clone(); - changed - }; + self.device_accounts.borrow_mut().entry(key).or_default().accounts = accounts.clone(); self.publish_devices(); - // An agent signed in, switched, given a key or signed out lists other - // models (Pi signed in to Claude lists Claude's): its models again. - for harness in changed { - crate::chat::accounts_changed(&key, &harness); - } } /// A composer on `key`'s device: its accounts, on first sight of it. @@ -1186,11 +977,6 @@ impl Agents { let params = addressed_to(target, json!({ "harness": id, "accountId": account })); match engine::get().call("ActivateAgentAccount", params).await { Ok(snapshot) => { - // (Told against the list from before the row moved, so the - // switch counts as a change: the model list follows it.) - if let (Some(before), Some(device)) = (before, self.device_accounts.borrow_mut().get_mut(&key)) { - device.accounts = before; - } self.keep_device(key.clone(), &snapshot); if self.target.borrow().as_deref().unwrap_or_default() == key { self.take_accounts(&snapshot); @@ -1270,10 +1056,7 @@ impl Agents { /// A new account: the engine's sign-in (`StartAgentLogin`), its page /// opened, then asked after (`PollAgentLogin`) until it lands or fails. - async fn sign_in(self: Rc, id: String, method: Option, provider: Option) -> Result { - // The provider asked for (one the engine can't sign in to says why), - // else the agent's first. - let provider = provider.or_else(|| login_provider(&id).map(String::from)); + async fn sign_in(self: Rc, id: String, method: Option) -> Result { // The device it's on, for all of it (the page may show another). let device = self.target.borrow().clone(); let device = device.as_deref(); @@ -1289,12 +1072,11 @@ impl Agents { login.page = None; login.pairing = None; login.target = device.map(Into::into); - login.provider = provider.clone(); login.generation }; self.publish(); let mut params = json!({ "harness": id }); - if let Some(provider) = &provider { + if let Some(provider) = login_provider(&id) { params["provider"] = json!(provider); } // (A registry agent's method of its choosing, in the same field.) @@ -1488,41 +1270,7 @@ impl Agents { } } - /// `AddAgentApiKey` on this Mac's engine — never addressed elsewhere: - /// with another device shown, refused here, the key sent nowhere. - async fn add_key(&self, id: String, provider: String, key: String, name: Option) -> Result { - if self.target.borrow().is_some() { - return Err("remote".into()); - } - let params = json!({ "harness": id, "provider": provider, "key": key, "name": name }); - match engine::get().call("AddAgentApiKey", params).await { - Ok(snapshot) => { - self.take_accounts(&snapshot); - Ok(Value::Nil) - } - // (The engine's words never hold the key; they aren't printed.) - Err(error) if engine::outcome(&error) == "offline" => Err("offline".into()), - Err(error) => Err(error), - } - } - - /// `RenameAgentAccount`, on the device shown. - async fn rename(&self, id: String, account: String, name: String) -> Result { - let params = self.addressed(json!({ "harness": id, "accountId": account, "name": name })); - match engine::get().call("RenameAgentAccount", params).await { - Ok(snapshot) => { - self.take_accounts(&snapshot); - Ok(Value::Nil) - } - Err(error) if engine::outcome(&error) == "offline" => Err("offline".into()), - Err(error) => Err(error), - } - } - - /// A switch or a removal on the device shown, rejecting with why not in - /// the engine's words (`offline` when it can't be reached), for the - /// page to say: a refusal is never silent. - async fn account_call(&self, method: &'static str, id: String, account: String) -> Result { + async fn account_call(&self, method: &'static str, id: String, account: String) -> Result { match engine::get() .call(method, self.addressed(json!({ "harness": id, "accountId": account }))) .await @@ -1533,7 +1281,7 @@ impl Agents { } Err(error) => { eprintln!("zeron-zgx: agents: {method} failed: {error}"); - Err(if fail(&error) == "offline" { "offline".into() } else { error }) + Err(fail(&error)) } } } @@ -1779,15 +1527,6 @@ fn account_value(account: &Json, reset: Option<&Reset>) -> Value { ("session", Value::from(session)), ("week", Value::from(week_used)), ("active", Value::Bool(account["active"].as_bool().unwrap_or(false))), - ("switchable", Value::Bool(can_switch(account))), - ("provider", account["provider"].as_str().map_or(Value::Nil, Value::from)), - // An API key: no plan, so no windows to show; named by someone, or - // going by its last four. - ("key", Value::Bool(account["authKind"].as_str() == Some("api-key"))), - ( - "named", - Value::Bool(account["authKind"].as_str() == Some("api-key") && account["displayName"].as_str().is_some()), - ), ]; if account["codexResetCredits"].is_object() { let (count, list) = banked(&account["codexResetCredits"], spent); @@ -1815,39 +1554,11 @@ fn account_value(account: &Json, reset: Option<&Reset>) -> Value { #[derive(Default)] struct DeviceAccounts { accounts: Vec, - /// Its accounts have come at least once. - seen: bool, /// A composer has asked for them. asked: bool, last_forced: Option, } -/// The agents whose logins differ between two account lists: an account -/// come or gone, or another in use. (Usage alone changes nothing.) -fn changed_logins(before: &[Json], after: &[Json]) -> Vec { - fn logins(accounts: &[Json]) -> BTreeMap> { - let mut by: BTreeMap> = BTreeMap::new(); - for a in accounts { - if let (Some(harness), Some(id)) = (a["harness"].as_str(), a["id"].as_str()) { - by.entry(harness.into()).or_default().push((id.into(), a["active"].as_bool() == Some(true))); - } - } - for list in by.values_mut() { - list.sort(); - } - by - } - let (before, after) = (logins(before), logins(after)); - // (Each agent once, however many of its rows moved.) - let changed: std::collections::BTreeSet = before - .keys() - .chain(after.keys()) - .filter(|h| before.get(*h) != after.get(*h)) - .cloned() - .collect(); - changed.into_iter().collect() -} - /// Forced usage asks the provider: not more often than this. const DEVICE_USAGE_MIN: Duration = Duration::from_secs(30); /// How often usage is looked at again while the app is open. @@ -1897,16 +1608,12 @@ fn device_accounts_value(accounts: &[Json]) -> Value { ("email", Value::from(account_name(account))), ("plan", Value::from(account["planLabel"].as_str().unwrap_or(""))), ("active", Value::Bool(account["active"].as_bool().unwrap_or(false))), - ("can_switch", Value::Bool(can_switch(account))), + ( + "can_switch", + Value::Bool(account["switchable"].as_bool().unwrap_or(false) && zeron_proto::providers::switches_accounts(id)), + ), ("windows", Value::List(windows.into())), ]; - // Its model provider, where its agent keeps a login per provider - // (the Account row shows only the model's provider's), and what - // that provider's group is called. - if let Some(provider) = account["provider"].as_str().filter(|_| zeron_proto::providers::groups_by_provider(id)) { - fields.push(("provider", Value::from(provider))); - fields.push(("group", Value::from(zeron_proto::providers::account_group_label(id, provider).as_str()))); - } if let Some(error) = account["usageError"].as_str() { fields.push(("error", Value::from(error))); } @@ -1915,13 +1622,6 @@ fn device_accounts_value(accounts: &[Json]) -> Value { Value::record(by.into_iter().map(|(harness, rows)| (harness, Value::List(rows.into())))) } -/// Whether an account can be put in use: one the engine can swap to, of an -/// agent whose logins Zeron switches (never Hermes' own pool). -fn can_switch(account: &Json) -> bool { - account["switchable"].as_bool().unwrap_or(false) - && account["harness"].as_str().and_then(harness_id).is_some_and(zeron_proto::providers::switches_accounts) -} - /// What an account goes by. fn account_name(account: &Json) -> &str { account["email"].as_str().or_else(|| account["displayName"].as_str()).unwrap_or("Signed in") @@ -1966,59 +1666,6 @@ fn epoch_secs(s: &str) -> Option { mod tests { use super::*; - /// A Cloud API key goes to the vault for the account's Cloud device: - /// Anthropic's or OpenAI's only, a real key, Cloud on. - #[test] - fn a_cloud_key_is_one_cloud_runs_for_its_device() { - let ok = cloud_key_params("anthropic-key", "sk-ant-123", "cloud-acct").unwrap(); - assert_eq!(ok, json!({ "provider": "anthropic-key", "key": "sk-ant-123", "authorizedDevices": ["cloud-acct"] })); - assert!(cloud_key_params("openai-key", "sk-1", "d").is_ok()); - assert!(cloud_key_params("claude", "sk", "d").is_err(), "a login isn't a key"); - assert!(cloud_key_params("anthropic-key", "", "d").is_err()); - assert!(cloud_key_params("anthropic-key", "a\nb", "d").is_err()); - assert!(cloud_key_params("anthropic-key", &"k".repeat(5000), "d").is_err()); - assert!(cloud_key_params("anthropic-key", "sk", "").is_err(), "Cloud off"); - } - - /// Pi signed in to Claude, or another login put in use: Pi's models are - /// looked up again. Usage moving, or another agent's, isn't Pi's change. - #[test] - fn only_an_agent_whose_logins_changed_has_its_models_looked_up_again() { - let row = |harness: &str, id: &str, active: bool, used: f64| { - json!({ "id": id, "harness": harness, "active": active, "usageWindows": [{ "used": used }] }) - }; - let before = vec![row("pi", "chatgpt-a", true, 0.1), row("codex", "c1", true, 0.2)]; - // Usage alone: nothing. - let after = vec![row("pi", "chatgpt-a", true, 0.5), row("codex", "c1", true, 0.9)]; - assert!(changed_logins(&before, &after).is_empty()); - // A new Claude login for Pi: Pi. - let signed_in = vec![ - row("pi", "chatgpt-a", true, 0.1), - row("pi", "claude-b", true, 0.0), - row("codex", "c1", true, 0.2), - ]; - assert_eq!(changed_logins(&before, &signed_in), ["pi"]); - // Another in use: that agent. - let switched = vec![ - row("pi", "chatgpt-a", true, 0.1), - row("codex", "c1", false, 0.2), - row("codex", "c2", true, 0.0), - ]; - assert_eq!(changed_logins(&before, &switched), ["codex"]); - // Signed out of everything: that agent too. - assert_eq!(changed_logins(&before, &[row("codex", "c1", true, 0.2)]), ["pi"]); - // Two agents changed at once: each named once, whatever the order. - let both = vec![row("pi", "chatgpt-b", true, 0.1), row("codex", "c2", true, 0.2)]; - assert_eq!(changed_logins(&before, &both), ["codex", "pi"]); - // A switch from the composer, told against the list before its row - // moved (as switch_on does): a change, so the models follow. - let mut optimistic = before.clone(); - optimistic.push(row("pi", "chatgpt-c", false, 0.0)); - let mut engine_said = optimistic.clone(); - mark_switched(&mut engine_said, "pi", "chatgpt-c"); - assert_eq!(changed_logins(&optimistic, &engine_said), ["pi"]); - } - #[test] fn a_switch_moves_only_its_own_login_group() { let mut accounts = vec![ @@ -2063,63 +1710,6 @@ mod tests { assert_eq!(hermes[0].field("error").map(text).as_deref(), Some("no")); } - #[test] - fn a_per_provider_agents_accounts_group_by_provider() { - let pi = harness_id("pi").unwrap(); - let accounts = [ - json!({ "id": "c1", "harness": "pi", "provider": "anthropic", "active": true }), - json!({ "id": "o1", "harness": "pi", "provider": "openai-codex", "active": false }), - json!({ "id": "o2", "harness": "pi", "provider": "openai-codex", "active": true }), - json!({ "id": "k1", "harness": "pi", "provider": "kimi-coding", "active": true }), - ]; - let groups = provider_groups(pi, &accounts); - let shape: Vec<(&str, &str, Vec, Option<&str>, Option<&str>)> = groups - .iter() - .map(|g| (g.provider.as_str(), g.name.as_str(), g.rows.clone(), g.login.as_deref(), g.key.as_deref())) - .collect(); - assert_eq!(&shape[..3], &[ - // Zeron's own sign-ins first, in their order (Anthropic takes - // a Claude sign-in and an API key alike)… - ("openai-codex", "ChatGPT", vec![1, 2], Some("ChatGPT"), None), - ("anthropic", "Anthropic", vec![0], Some("Claude"), Some("Anthropic")), - // …one without accounts yet is one to add… - ("github-copilot", "GitHub Copilot", vec![], Some("GitHub Copilot"), None), - ]); - // …then the providers it adds keys for… - assert!(shape.iter().any(|g| g.0 == "openrouter" && g.4 == Some("OpenRouter") && g.2.is_empty())); - // …then one it adds neither to, its accounts still listed. - let kimi = shape.last().unwrap(); - assert_eq!((kimi.0, kimi.2.clone(), kimi.3, kimi.4), ("kimi-coding", vec![3], None, None)); - // An older engine's row, naming no provider: the first group's. - let old = provider_groups(pi, &[json!({ "id": "x", "harness": "pi" })]); - assert_eq!(old[0].rows, [0]); - } - - #[test] - fn a_device_says_each_accounts_provider_where_its_agent_keeps_one_per_provider() { - let value = device_accounts_value(&[ - json!({ "id": "o1", "harness": "pi", "provider": "openai-codex", "active": true, "switchable": true }), - json!({ "id": "g1", "harness": "pi", "provider": "github-copilot", "active": true, "switchable": true }), - json!({ "id": "x1", "harness": "grok", "provider": "issuer", "active": true, "switchable": true }), - ]); - let pi = value.field("pi").expect("pi").list().to_vec(); - assert_eq!(pi[0].field("provider").map(text).as_deref(), Some("openai-codex")); - assert_eq!(pi[0].field("group").map(text).as_deref(), Some("ChatGPT")); - assert_eq!(pi[1].field("group").map(text).as_deref(), Some("GitHub Copilot")); - // (Grok's issuer is no model provider: one group.) - let grok = value.field("grok").expect("grok").list().to_vec(); - assert!(grok[0].field("provider").is_none()); - // Each per-provider agent's providers, for the Account row to offer. - let logins = logins_value(); - let pi_logins = logins.field("pi").expect("pi"); - let copilot = pi_logins.field("github-copilot").expect("copilot"); - assert_eq!(copilot.field("login").map(text).as_deref(), Some("GitHub Copilot")); - let openrouter = pi_logins.field("openrouter").expect("openrouter"); - assert_eq!(openrouter.field("key").map(text).as_deref(), Some("OpenRouter")); - assert!(!openrouter.field("login").is_some_and(|v| v.truthy())); - assert!(logins.field("claude-code").is_none()); - } - #[test] fn accounts_read_their_two_windows() { let account = json!({ @@ -2260,22 +1850,6 @@ pub(crate) fn shown_target() -> Option { } /// `params` addressed to the device Providers shows. -/// `VaultPutApiKey`'s params for a Cloud API key: Anthropic's or OpenAI's -/// alone, a key that's there and no longer than any key is, for the -/// account's Cloud device. Else why not. -pub(crate) fn cloud_key_params(provider: &str, key: &str, device: &str) -> Result { - if !matches!(provider, "anthropic-key" | "openai-key") { - return Err("Cloud takes Anthropic and OpenAI keys".into()); - } - if key.is_empty() || key.len() > 4096 || key.chars().any(char::is_control) { - return Err("That isn't an API key".into()); - } - if device.is_empty() { - return Err("Turn Cloud on in Settings → Cloud first".into()); - } - Ok(json!({ "provider": provider, "key": key, "authorizedDevices": [device] })) -} - pub(crate) fn addressed_to_shown(params: Json) -> Json { addressed_to(shown_target().as_deref(), params) } diff --git a/apps/zgx/src/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 b8e5734fe..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, @@ -625,19 +635,6 @@ impl Pending { } } -/// The transcript's rows with each id once, the first kept: its list is keyed -/// by `id`, and a key shown twice is one it can't place (zgx's virtual list -/// panics, and the transcript stops updating until the chat is opened -/// again). A prompt pending here comes after the transcript's own rows, so -/// the engine's copy of it is the one kept. -fn one_row_per_id(list: &mut Vec) { - let mut ids: std::collections::HashSet> = std::collections::HashSet::with_capacity(list.len()); - list.retain(|row| match row.field("id") { - Some(Value::Str(id)) => ids.insert(id.clone()), - _ => true, - }); -} - /// An edit lease on a queued message (the engine's `BeginQueuedMessageEdit`). #[derive(Clone)] struct Lease { @@ -728,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 { @@ -773,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())), @@ -984,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); @@ -1214,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 @@ -1231,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)"); } }); @@ -1364,36 +1369,6 @@ impl Chats { self.publish_views(); } - /// `harness`'s logins on `device` ("" or this one: here) changed: its - /// models looked up again, as the agent now lists them (Pi signed in to - /// Claude lists Claude's models). Here, that agent's alone; another - /// device's, only while the composer runs on it. - fn accounts_changed(self: &Rc, device: &str, harness: &str) { - let here = self.here.borrow().clone(); - if device.is_empty() || here.as_deref() == Some(device) { - if !self.models.borrow().contains_key(harness) { - return; - } - let (me, id) = (self.clone(), harness.to_owned()); - engine::get().spawn(async move { - match engine::get() - .call("ListModels", json!({ "harness": id, "force": true })) - .await - { - Ok(list) => { - me.models - .borrow_mut() - .insert(id, list.as_array().cloned().unwrap_or_default()); - me.publish_catalog(); - } - Err(error) => eprintln!("zeron-zgx: chat: {id}'s models didn't list again: {error}"), - } - }); - } else if self.remote.borrow().as_ref().is_some_and(|r| r.device == device) { - self.reload_remote(); - } - } - /// The composer now runs on `device` ("" or this one: here). fn catalog_on(self: &Rc, device: &str) { let here = self.here.borrow().clone(); @@ -1665,11 +1640,6 @@ impl Chats { /// A chat just made here (`sessions.create`), as the engine's list will /// have it: `params` is its `createChat` (`chatId`, `deviceId`, `spaceId`, /// `config`, `cwd`, `branch`, `parentChatId`). -/// An agent's logins on a device changed (src/agents.rs): its models again. -pub(crate) fn accounts_changed(device: &str, harness: &str) { - CHATS.with(|c| c.clone().accounts_changed(device, harness)); -} - pub(crate) fn made(params: &Json) { let Some(id) = params["chatId"].as_str() else { return }; let mut row = json!({ "id": id }); @@ -1797,15 +1767,6 @@ fn model_value(harness: &str, m: &Json) -> Value { if m["default"].as_bool() == Some(true) { fields.push(("default", Value::Bool(true))); } - // Where it's served from, for an agent reaching models through several - // providers (Pi signed in to Codex and OpenCode): its id and its name. - let provider = &m["provider"]; - if let (Some(pid), Some(name)) = (provider["id"].as_str(), provider["name"].as_str()) - && !pid.is_empty() - && !name.is_empty() - { - fields.push(("provider", Value::record([("id", Value::from(pid)), ("name", Value::from(name))]))); - } Value::record(fields) } @@ -1890,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); @@ -1984,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() @@ -2019,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(); } } @@ -2090,9 +2054,6 @@ impl View { } } open.heard = true; - // (A prompt sent from here that the tail brought: the - // engine's copy now shows, so this one goes.) - self.drop_arrived_prompts(open); break 'applied Ok(Change::Rows); } else { open.has_whole = false; @@ -2141,7 +2102,14 @@ impl View { } if result.is_ok() { open.heard = true; - self.drop_arrived_prompts(open); + let ids: Vec<&str> = open.entries.iter().map(|e| &*e.id).collect(); + let mut pending = self.pending.borrow_mut(); + if let Some(prompts) = pending.get_mut(&open.id) { + prompts.retain(|p| !ids.contains(&p.id.as_str())); + if prompts.is_empty() { + pending.remove(&open.id); + } + } } result }; @@ -2160,20 +2128,6 @@ impl View { } } - /// Prompts sent from here that the transcript now has: the engine's copy - /// shows from now on, so this device's own goes (both would be two rows - /// with one id, which the transcript's list can't show). - fn drop_arrived_prompts(&self, open: &Open) { - let ids: std::collections::HashSet<&str> = open.entries.iter().map(|e| &*e.id).collect(); - let mut pending = self.pending.borrow_mut(); - if let Some(prompts) = pending.get_mut(&open.id) { - prompts.retain(|p| !ids.contains(p.id.as_str())); - if prompts.is_empty() { - pending.remove(&open.id); - } - } - } - /// What another view of this chat has built, for the entries here that /// are the same there: a chat in a split is watched by its pane and, when /// focused, by the window too. @@ -2332,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 = { @@ -2376,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.; @@ -2407,7 +2368,6 @@ impl View { if let Some(pending) = self.pending.borrow().get(&open.id) { list.extend(pending.iter().map(Pending::value)); } - one_row_per_id(&mut list); list }; // The list first: a reply that ended takes its words into its row @@ -2491,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 @@ -2642,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 @@ -3394,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 { @@ -4534,56 +4539,6 @@ fn entry_text(entry: &Json) -> String { out } -/// Whether a user entry is only Claude Code's note on a picture a tool read -/// (`[Image: original WxH, displayed at WxH. …]`, `[Image: source: …]`), as -/// zeron-harness's `is_synthetic_user_text` reads it: nothing else in it. -fn image_note_only(entry: &Json) -> bool { - let mut notes = 0; - for p in entry["parts"].as_array().into_iter().flatten() { - match p["kind"].as_str() { - Some("text") => { - let text = p["text"].as_str().unwrap_or_default().trim(); - if text.is_empty() { - continue; - } - if !is_image_note(text) { - return false; - } - notes += 1; - } - _ => return false, - } - } - notes > 0 -} - -/// The CLI's image note exactly (zeron-harness's `is_image_note`): one -/// line, `[Image: source: ]` (the path may be followed by `, original -/// WxH, displayed at WxH. …`), or `[Image: original WxH, displayed at WxH. -/// …]`. Something a person typed that only starts and ends like one -/// (`[Image: source: shot.png] why is button [x]`) is not it. -fn is_image_note(text: &str) -> bool { - if text.contains('\n') { - return false; - } - let Some(rest) = text.strip_prefix("[Image: ").and_then(|rest| rest.strip_suffix(']')) else { - return false; - }; - if let Some(path) = rest.strip_prefix("source: ") { - return !path.trim().is_empty() && !path.contains(['[', ']']); - } - fn dimensions(text: &str) -> Option<&str> { - let (width, rest) = text.split_once('x')?; - let digits = rest.bytes().take_while(u8::is_ascii_digit).count(); - (!width.is_empty() && width.bytes().all(|b| b.is_ascii_digit()) && digits > 0).then(|| &rest[digits..]) - } - rest.strip_prefix("original ") - .and_then(dimensions) - .and_then(|rest| rest.strip_prefix(", displayed at ")) - .and_then(dimensions) - .is_some_and(|rest| !rest.contains(['[', ']'])) -} - /// A transcript's subagents: each spawn that names its doc, once, in the order they /// were spawned, with when their turn began (a message's `tools` hold all of its). fn subagents_in(message: &Value) -> Option> { @@ -4621,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"] @@ -4634,13 +4649,6 @@ pub(crate) fn message(entry: &Json, model: Option<&str>) -> Option { .filter_map(|p| p["text"].as_str()) .collect::>() .join("\n\n"); - // Claude Code's note on a picture a tool read (its size as the - // model saw it), which a subagent's transcript kept as a message - // before the engine learned it isn't one: not something anyone - // said, so no bubble. - if image_note_only(entry) { - return None; - } // A message from another chat: its routing header is the agent's to // read; the transcript shows who sent it, over its words. let sender = zeron_proto::agent_messages::agent_message_sender(&text); @@ -5812,10 +5820,6 @@ fn session_value(chats: &Chats, open: &Open, offline: bool) -> Value { // Its folder on that device (a reply's pictures resolve by it). ("cwd", s(&chat["cwd"])), ("harness", Value::from(harness)), - // The agent the chat records (nil until its config is known, where - // `harness` stands in as Claude Code): whose `/` commands and skills - // its composer lists when its model isn't in the catalog shown. - ("agent", s(&config["harness"])), ("model", model), ("model_label", model_label), ("effort", s(&config["reasoning"])), @@ -6123,19 +6127,8 @@ fn seen_would_change(chat: &Json, session: &Json, now: chrono::DateTime String { - const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; - let month = MONTHS.get(month0 as usize).copied().unwrap_or("Jan"); - let twelve = match hour % 12 { - 0 => 12, - h => h, - }; - format!("{month} {day}, {twelve}:{minute:02} {}", if hour < 12 { "AM" } else { "PM" }) -} - -/// [`clock`] for `at` (seconds since 1970) in this machine's time zone. +/// "Jul 1, 4:12 PM", in this Mac's time zone (the shipping app's +/// `format_timestamp`). #[cfg(unix)] fn stamp(at: i64) -> Option { #[repr(C)] @@ -6168,47 +6161,21 @@ fn stamp(at: i64) -> Option { } tm.assume_init() }; - Some(clock(tm.mon as u32, tm.mday as u32, tm.hour as u32, tm.min as u32)) -} - -/// [`clock`] for `at` (seconds since 1970) in this machine's time zone: on -/// Windows through its own conversion (it has no `localtime_r`). Without -/// it every message's time was missing there, and with it the hover strip -/// that holds Copy. -#[cfg(windows)] -fn stamp(at: i64) -> Option { - use windows_sys::Win32::Foundation::{FILETIME, SYSTEMTIME}; - use windows_sys::Win32::System::Time::{FileTimeToSystemTime, SystemTimeToTzSpecificLocalTime}; - if at <= 0 { - return None; - } - // FILETIME counts 100 ns from 1601-01-01 (11,644,473,600 s before 1970). - let ticks = (at as u64).checked_add(11_644_473_600)?.checked_mul(10_000_000)?; - let file = FILETIME { - dwLowDateTime: ticks as u32, - dwHighDateTime: (ticks >> 32) as u32, + let hour = match tm.hour % 12 { + 0 => 12, + h => h, }; - // SAFETY: both calls read only the structs passed in and write only the - // caller's SYSTEMTIMEs; a null zone means the machine's current one. - let local = unsafe { - let mut utc: SYSTEMTIME = std::mem::zeroed(); - let mut local: SYSTEMTIME = std::mem::zeroed(); - if FileTimeToSystemTime(&file, &mut utc) == 0 - || SystemTimeToTzSpecificLocalTime(std::ptr::null(), &utc, &mut local) == 0 - { - return None; - } - local - }; - Some(clock( - u32::from(local.wMonth).checked_sub(1)?, - u32::from(local.wDay), - u32::from(local.wHour), - u32::from(local.wMinute), + const MONTHS: [&str; 12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + let month = MONTHS.get(tm.mon as usize).copied().unwrap_or("Jan"); + Some(format!( + "{month} {}, {hour}:{:02} {}", + tm.mday, + tm.min, + if tm.hour < 12 { "AM" } else { "PM" } )) } -#[cfg(not(any(unix, windows)))] +#[cfg(not(unix))] fn stamp(_at: i64) -> Option { None } @@ -6272,21 +6239,6 @@ mod stream_tests; #[cfg(test)] mod tests { - /// A message's time reads as the shipping app's, on every platform: the - /// formatting, and a real local time from `stamp` (Windows included, - /// where it once was always missing and hid the message's hover strip). - #[test] - fn a_messages_time_reads_like_the_shipping_apps_on_every_platform() { - assert_eq!(super::clock(6, 1, 16, 12), "Jul 1, 4:12 PM"); - assert_eq!(super::clock(0, 9, 0, 5), "Jan 9, 12:05 AM"); - assert_eq!(super::clock(11, 31, 12, 0), "Dec 31, 12:00 PM"); - let at = super::stamp(1_794_600_000).expect("a time for a real moment"); - let (month, rest) = at.split_once(' ').unwrap(); - assert!(["Nov", "Dec"].contains(&month), "{at}"); - assert!(rest.ends_with(" AM") || rest.ends_with(" PM"), "{at}"); - assert_eq!(super::stamp(0), None); - } - /// Opening a chat marks it seen only while that changes something any /// viewport reads (its last message, its terminal's report, the end of /// its sleep): a chat looked at already writes nothing, so its devices' @@ -6329,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] @@ -6810,23 +6779,6 @@ mod tests { assert_eq!(model.as_deref(), Some("pi/kimi-k3")); } - /// The composer asks a chat's own agent for its `/` list: the harness its - /// config records, also without a model or before its device's catalog; - /// none until the config is known (not `harness`'s Claude Code stand-in). - #[test] - fn a_sessions_agent_is_its_recorded_harness() { - let chats = Chats::default(); - let open = Open { id: "c".into(), ..Open::default() }; - let agent = |chats: &Chats| session_value(chats, &open, false).field("agent").map(text).filter(|a| !a.is_empty()); - assert_eq!(agent(&chats), None); - chats.chats.borrow_mut().insert( - "c".into(), - json!({ "id": "c", "deviceId": "laptop", "config": { "harness": "codex" } }), - ); - assert_eq!(agent(&chats).as_deref(), Some("codex")); - assert!(matches!(session_value(&chats, &open, false).field("model"), None | Some(Value::Nil))); - } - #[test] fn back_takes_a_questions_last_page_off_and_gives_its_pick() { let mut open = Open { id: "c".into(), ..Open::default() }; @@ -7191,33 +7143,6 @@ mod tests { assert!(apply(&mut entries, &mut wrong).is_err()); } - #[test] - fn claude_codes_image_note_is_no_message() { - let user = |text: &str| { - json!({ "id": "u", "role": "user", "createdAt": 1_788_900_000_000_i64, - "parts": [{ "id": "t", "kind": "text", "text": text }] }) - }; - for note in [ - "[Image: original 1884x2066, displayed at 1824x2000. Multiply coordinates by 1.03 to map to original image.]", - "[Image: source: /home/me/scratch/test.png]", - " [Image: source: /a.png, original 10x10, displayed at 10x10.]\n", - ] { - assert!(message(&user(note), None).is_none(), "{note} showed"); - } - // Words of its own around it, or a note-like line that isn't one, show. - assert!(message(&user("Look: [Image: original 10x10, displayed at 10x10.]"), None).is_some()); - assert!(message(&user("[Image: the logo]"), None).is_some()); - for typed in [ - "[Image: source: shot.png] why is button [x]", - "[Image: original mockup attached]\nmatch it exactly [urgent]", - "[Image: original mockup attached]", - "[Image: original 10x10, displayed at big]", - "[Image: source: ]", - ] { - assert!(message(&user(typed), None).is_some(), "{typed} hidden"); - } - } - #[test] fn a_transcript_s_subagents_are_listed_once_each_in_order() { let tool = |r: &str, title: &str| { @@ -7243,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"; @@ -7378,12 +7349,6 @@ mod tests { assert!(model_value("claude-code", &claude).field("default").is_none()); let marked = json!({ "id": "claude-opus-5-5", "default": true }); assert!(matches!(model_value("claude-code", &marked).field("default"), Some(Value::Bool(true)))); - // A model's provider rides along when the agent names one, and only then. - assert!(model_value("claude-code", &marked).field("provider").is_none()); - let pi = json!({ "id": "openai-codex/gpt-5.5", "label": "GPT-5.5", "provider": { "id": "openai-codex", "name": "Codex" } }); - let provider = model_value("pi", &pi).field("provider").cloned().expect("a provider"); - assert_eq!(provider.field("id").map(text).as_deref(), Some("openai-codex")); - assert_eq!(provider.field("name").map(text).as_deref(), Some("Codex")); } #[test] @@ -7866,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| { @@ -7922,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()); @@ -7963,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. @@ -8051,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)); @@ -8074,74 +8043,6 @@ mod tests { assert!(!rows().contains("tail engine.log"), "the whole copy replaces it: {}", rows()); } - /// A prompt sent from here that a reopened chat's opening tail brings - /// shows once: the engine's copy, the one pending here let go. Shown twice - /// it was two rows with one id, and zgx's virtual list panicked - /// ("duplicate virtual list child key"), freezing the transcript. - #[test] - fn a_prompt_the_opening_tail_brings_shows_once() { - use super::{Entry, Open, Pending, View, draft_value}; - use serde_json::json; - use std::cell::{Cell, RefCell}; - use std::rc::Rc; - use zgx_script::{ScriptHost, Value, testing::TestApp}; - let app = TestApp::new(|runtime| ScriptHost::inline(runtime, "app", "return ui.text { \"x\" }")); - let text = |id: &str, words: &str| json!({ "id": format!("{id}p"), "kind": "text", "text": words }); - let prompt = |id: &str| json!({ "id": id, "role": "user", "parts": [text(id, "go")], "createdAt": 1_762_000_000_000_i64 }); - let reply = |id: &str| json!({ "id": id, "role": "assistant", "status": "complete", "parts": [text(id, "done")], "createdAt": 1_762_000_000_000_i64 }); - let held: Vec = [prompt("u0"), reply("a0")].into_iter().map(Entry::new).collect(); - let view = Rc::new(View { - session: app.host.shared("t.session", Value::Nil), - messages: app.host.shared("t.messages", Value::List(Rc::from([]))), - subagents: app.host.shared("t.subagents", Value::List(Rc::from([]))), - draft: app.host.shared("t.draft", draft_value("", "")), - rpc: RefCell::default(), - open: RefCell::new(Some(Open { id: "c".into(), entries: held, heard: true, has_whole: true, ..Open::default() })), - pending: RefCell::default(), - generation: Cell::new(0), - snapshot: Cell::new(false), - }); - view.pending.borrow_mut().insert( - "c".into(), - vec![Pending { - id: "u1".into(), - words: "go".into(), - paths: Vec::new(), - waiting: "sending", - retry: None, - at: None, - held: false, - row: false, - transfers: Vec::new(), - }], - ); - let ids = || -> Vec { - match view.messages.signal().with_untracked(Clone::clone) { - Value::List(rows) => rows.iter().filter_map(|r| r.field("id")).map(super::text).collect(), - other => panic!("rows: {other:?}"), - } - }; - view.publish_messages(); - assert_eq!(ids(), ["u0", "a0", "u1"], "the prompt shows at once, pending here"); - // The engine's opening tail, the prompt in it. - view.heard_frame(json!({ "reset": [reply("a0"), prompt("u1")], "historyPending": true, "count": 2 })); - assert_eq!(ids(), ["u0", "a0", "u1"], "once, the engine's copy"); - assert!(view.pending.borrow().get("c").is_none(), "the copy pending here is let go"); - } - - /// Rows that would share an id (the transcript's list is keyed by it) - /// show once, the first kept: a pending prompt after the transcript's - /// copy of it, or an entry twice over. - #[test] - fn rows_sharing_an_id_show_once() { - use zgx_script::Value; - let row = |id: &str, text: &str| Value::record([("id", Value::from(id)), ("text", Value::from(text))]); - let mut list = vec![row("u1", "engine"), row("a1", "reply"), Value::record([("text", Value::from("no id"))]), row("u1", "here")]; - super::one_row_per_id(&mut list); - let texts: Vec = list.iter().filter_map(|r| r.field("text")).map(super::text).collect(); - assert_eq!(texts, ["engine", "reply", "no id"]); - } - /// A chat opened shows its rows once its transcript has come (this /// device's copy, read first whatever its host's state), not once the /// chats list has it too; "missing" only when the list is known, hasn't diff --git a/apps/zgx/src/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/composer_layout_tests.rs b/apps/zgx/src/composer_layout_tests.rs deleted file mode 100644 index 1a6aa5341..000000000 --- a/apps/zgx/src/composer_layout_tests.rs +++ /dev/null @@ -1,120 +0,0 @@ -//! Pure geometry rules run in Luau, where the production calculation lives. -use zgx_script::{ScriptHost, Value, testing::TestApp}; - -#[test] -fn composer_geometry_rules_have_boundary_header_and_purity_coverage() { - let ui = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui"); - let report = zgx_script::test_runner::run(&ui, Some("composer geometry:"), |outcome| { - if let Some(failure) = &outcome.failure { - eprintln!("{}: {failure}", outcome.name); - } - }) - .expect("geometry tests load"); - assert_eq!(report.outcomes.len(), 10, "all geometry cases must run"); - assert_eq!(report.failed(), 0, "{report:?}"); -} - -#[test] -fn composer_headers_grow_and_shrink_without_replacing_or_covering_the_editor() { - let path = - std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/composer_fixture.luau"); - let mut app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); - app.ui.scene.borrow_mut().resize(700., 600.); - app.frames(30); - app.fill("role:textbox", "keep this draft"); - app.frames(30); - let field = app.find("role:textbox").unwrap(); - let editor = app.ui.editor_within(field).unwrap(); - editor.set_selection(1, 8); - let short = app.bounds("role:textbox"); - for (state, value, header) in [ - ( - "test_composer_cargo", - r#"[{"id":"f1","kind":"file","name":"notes.md"}]"#, - "notes.md", - ), - ( - "test_composer_question", - r#"{"text":"Choose a branch","options":[{"value":"main","label":"Main"}]}"#, - "Choose a branch", - ), - ] { - app.host - .set_state(state, Value::from_json(value).unwrap()) - .unwrap(); - for _ in 0..30 { - app.frames(1); - let current = app.find("role:textbox").unwrap(); - let handle = app.ui.editor_within(current).unwrap(); - assert_eq!(handle.node, editor.node); - assert_eq!(handle.selection(), (1, 8)); - assert_eq!(app.ui.input.focused(), Some(editor.node)); - assert_eq!(handle.editor.borrow().text().as_ref(), "keep this draft"); - assert_eq!(app.error(), None); - } - let field_box = app.bounds("role:textbox"); - let role = if state.ends_with("cargo") { - "button" - } else { - "radiogroup" - }; - let head = app - .find(&format!("role:{role} \"{header}\"")) - .unwrap_or_else(|| panic!("{state}: missing {header}\n{}", app.tree())); - let head_box = app.ui.scene.borrow().transformed_bounds(head); - assert!( - field_box.height > short.height, - "header opens the expanded editor" - ); - assert!( - head_box.y + head_box.height <= field_box.y, - "header overlaps the text: {head_box:?}, {field_box:?}" - ); - app.host - .set_state( - state, - if state.ends_with("cargo") { - Value::List(Vec::new().into()) - } else { - Value::Nil - }, - ) - .unwrap(); - app.frames(40); - let back = app.bounds("role:textbox"); - assert!( - (back.y - short.y).abs() < 1. && (back.height - short.height).abs() < 1., - "removing header restores the compact draft" - ); - } -} - -#[test] -fn composer_dictation_transitions_keep_the_editors_draft_and_selection() { - let path = - std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui/composer_fixture.luau"); - let mut app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); - app.ui.scene.borrow_mut().resize(700., 600.); - app.frames(30); - app.fill("role:textbox", "retained draft"); - app.frames(30); - let field = app.find("role:textbox").unwrap(); - let editor = app.ui.editor_within(field).unwrap(); - editor.set_selection(1, 8); - for dictating in [true, false, true, false] { - app.host - .set_state("test_composer_dictating", Value::Bool(dictating)) - .unwrap(); - for _ in 0..30 { - app.frame(); - assert_eq!(app.error(), None); - let current = app.find("role:textbox").unwrap(); - assert_eq!(app.ui.editor_within(current).unwrap().node, editor.node); - assert_eq!(editor.selection(), (1, 8)); - assert_eq!(editor.value().get(), "retained draft"); - assert_eq!(app.ui.input.focused(), Some(editor.node)); - let bounds = app.bounds("role:textbox"); - assert!(bounds.width > 0. && bounds.height > 0.); - } - } -} diff --git a/apps/zgx/src/composer_metrics_tests.rs b/apps/zgx/src/composer_metrics_tests.rs deleted file mode 100644 index 5b24903d7..000000000 --- a/apps/zgx/src/composer_metrics_tests.rs +++ /dev/null @@ -1,308 +0,0 @@ -//! The measurement contract, exercised with the real editor provider. -use zgx_script::{ScriptHost, Value, testing::TestApp}; - -fn mount() -> TestApp { - let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("ui/composer_metrics_fixture.luau"); - let app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); - app.ui.scene.borrow_mut().resize(800., 600.); - app.frames(3); - assert_eq!(app.error(), None); - app -} - -fn result(app: &TestApp) -> Value { - app.host - .states() - .into_iter() - .find(|(n, _)| n == "fixture_metrics_result") - .expect("fixture result") - .1 -} - -fn number(value: &Value, key: &str) -> f64 { - value - .field(key) - .and_then(Value::as_f64) - .unwrap_or_else(|| panic!("no {key}: {value:?}")) -} - -fn ready(app: &TestApp) -> Value { - let r = result(app); - assert_eq!(r.field("status"), Some(&Value::from("ready")), "{r:?}"); - let s = r.field("sample").expect("measured sample").clone(); - assert_eq!(number(&s, "revision"), number(&r, "revision"), "{r:?}"); - assert!( - (number(&s, "width") - number(&r, "width")).abs() < 0.5, - "{r:?}" - ); - s -} - -fn editor_only(app: &mut TestApp) -> zgx::scene::NodeId { - assert!(app.find(".composer-metric-line").is_none()); - assert!(app.find(".composer-metric-wrapped").is_none()); - app.find("metrics-editor") - .expect("the editor is the only text provider") -} - -#[test] -fn composer_metrics_publish_empty_text_as_a_real_sample_then_refresh_equal_size_edits() { - let mut app = mount(); - let nodes = editor_only(&mut app); - let history = app - .host - .states() - .into_iter() - .find(|(n, _)| n == "fixture_metrics_history") - .unwrap() - .1; - assert_eq!( - history.list().first(), - Some(&Value::from("pending")), - "initial zeroes are not a measured empty draft" - ); - let empty = ready(&app); - assert_eq!(number(&empty, "unwrapped_width"), 0., "{empty:?}"); - assert!(number(&empty, "wrapped_height") >= number(&empty, "line_height")); - let mut previous = empty; - for text in ["abc", "cba"] { - app.host - .set_state("fixture_metrics_text", Value::from(text)) - .unwrap(); - assert_eq!(result(&app).field("status"), Some(&Value::from("stale"))); - app.frames(3); - let sample = ready(&app); - assert!(number(&sample, "unwrapped_width") > 0.); - assert!(number(&sample, "revision") > number(&previous, "revision")); - if text == "cba" { - assert_eq!( - number(&sample, "unwrapped_width"), - number(&previous, "unwrapped_width") - ); - assert_eq!( - number(&sample, "wrapped_height"), - number(&previous, "wrapped_height") - ); - } - assert_eq!( - editor_only(&mut app), - nodes, - "edits must retain the same editor" - ); - previous = sample; - } -} - -#[test] -fn composer_metrics_reject_unknown_width_and_coalesce_replaced_requests() { - let app = mount(); - let before = ready(&app); - app.host - .set_state("fixture_metrics_width", Value::Num(0.)) - .unwrap(); - app.frames(3); - let unknown = result(&app); - assert_eq!(unknown.field("status"), Some(&Value::from("stale"))); - assert_eq!( - unknown.field("sample"), - Some(&before), - "zero width cannot replace a valid sample" - ); - for (width, text) in [ - (100., "old"), - (250., "intermediate"), - (180., "latest\nline\n"), - ] { - app.host - .set_state("fixture_metrics_width", Value::Num(width)) - .unwrap(); - app.host - .set_state("fixture_metrics_text", Value::from(text)) - .unwrap(); - } - app.frames(3); - let sample = ready(&app); - assert_eq!(number(&sample, "width"), 180.); - assert_eq!( - number(&sample, "wrapped_height"), - 3. * number(&sample, "line_height") - ); - let settled = result(&app); - app.frames(30); - assert_eq!(result(&app), settled, "idle must not publish new revisions"); -} - -#[test] -fn composer_metrics_keep_the_target_width_when_the_presented_parent_resizes() { - let app = mount(); - app.host - .set_state( - "fixture_metrics_text", - Value::from("words ".repeat(30).as_str()), - ) - .unwrap(); - app.frames(3); - let before = ready(&app); - for width in [600., 400., 200., 500.] { - app.host - .set_state("fixture_metrics_viewport", Value::Num(width)) - .unwrap(); - app.frames(2); - assert_eq!( - ready(&app), - before, - "a transient viewport width must not change target metrics" - ); - } -} - -#[test] -fn composer_metrics_revalidate_restored_samples_and_typography_changes() { - let mut app = mount(); - app.host - .set_state("fixture_metrics_text", Value::from("some words")) - .unwrap(); - app.frames(3); - let before = ready(&app); - app.host.reload().unwrap(); - assert_eq!( - result(&app).field("status"), - Some(&Value::from("stale")), - "restored measures need a fresh layout" - ); - app.frames(3); - let restored = ready(&app); - assert!(number(&restored, "revision") > number(&before, "revision")); - editor_only(&mut app); - assert!(app.host.run_command("fixture.metrics.type")); - app.frames(3); - let changed = ready(&app); - assert_eq!(number(&changed, "line_height"), 28.); - assert!(number(&changed, "revision") > number(&restored, "revision")); - assert!(number(&changed, "unwrapped_width") > number(&restored, "unwrapped_width")); -} - -#[test] -fn composer_metrics_wrap_at_requested_width_and_preserve_a_final_empty_line() { - let app = mount(); - app.host - .set_state("fixture_metrics_text", Value::from("one\ntwo\n")) - .unwrap(); - app.frames(3); - let multiline = ready(&app); - assert!( - number(&multiline, "wrapped_height") >= 3. * number(&multiline, "line_height"), - "{multiline:?}" - ); - app.host - .set_state( - "fixture_metrics_text", - Value::from("words ".repeat(30).as_str()), - ) - .unwrap(); - app.frames(3); - let wide = ready(&app); - app.host - .set_state("fixture_metrics_width", Value::Num(120.)) - .unwrap(); - app.frames(3); - let narrow = ready(&app); - assert_eq!(number(&narrow, "width"), 120.); - assert!(number(&narrow, "wrapped_height") > number(&wide, "wrapped_height")); - assert_eq!( - number(&narrow, "unwrapped_width"), - number(&wide, "unwrapped_width") - ); -} - -#[test] -fn composer_metrics_follow_styled_editor_and_uncommitted_ime_without_extra_nodes() { - let mut app = mount(); - let node = editor_only(&mut app); - app.host - .set_state("fixture_metrics_text", Value::from("@mention hello\n")) - .unwrap(); - app.frames(3); - let editor = app.ui.editor(node).unwrap(); - let shown = editor.text_metrics(320.).unwrap(); - let sample = ready(&app); - assert_eq!( - number(&sample, "unwrapped_width"), - f64::from(shown.unwrapped_width) - ); - assert_eq!( - number(&sample, "wrapped_height"), - f64::from(shown.wrapped_height) - ); - app.ui.input.focus(&app.ui.scene, Some(node)); - editor.set_selection(0, 0); - app.ui.dispatch(zgx::input::InputEvent::ImePreedit { - text: "你好世界".repeat(30), - cursor: Some((0, 0)), - }); - app.frames(3); - let composition = ready(&app); - assert!(number(&composition, "wrapped_height") > number(&sample, "wrapped_height")); - assert!(number(&composition, "revision") > number(&sample, "revision")); - assert_eq!(editor.value().get(), "@mention hello\n"); - assert_eq!(editor_only(&mut app), node); -} - -#[test] -fn composer_metrics_acknowledge_width_only_queries_without_waiting_but_revalidate_content() { - let app = mount(); - // A draft that fits measures the same at any width it fits: a resize - // asks nothing of it (a pane dragged ran the whole chain at each frame). - let fits = ready(&app); - for width in [240., 180., 360.] { - app.host - .set_state("fixture_metrics_width", Value::Num(width)) - .unwrap(); - assert_eq!(result(&app).field("status"), Some(&Value::from("ready"))); - assert_eq!(ready(&app), fits, "a width it fits asks nothing again"); - } - // One that wraps follows the width, each published at once. - app.host - .set_state( - "fixture_metrics_text", - Value::from("words ".repeat(60).as_str()), - ) - .unwrap(); - app.frames(3); - let mut previous = ready(&app); - for width in [240., 180., 360.] { - app.host - .set_state("fixture_metrics_width", Value::Num(width)) - .unwrap(); - let sample = ready(&app); - assert_eq!(number(&sample, "width"), width); - assert!(number(&sample, "revision") > number(&previous, "revision")); - previous = sample; - } - app.host - .set_state("fixture_metrics_text", Value::from("changed words")) - .unwrap(); - app.host - .set_state("fixture_metrics_width", Value::Num(120.)) - .unwrap(); - assert_eq!(result(&app).field("status"), Some(&Value::from("stale"))); - app.frames(3); - assert_eq!(number(&ready(&app), "width"), 120.); -} - -#[test] -fn composer_metrics_width_fast_path_cannot_reuse_an_editor_replaced_under_the_same_ref() { - let mut app = mount(); - let original = editor_only(&mut app); - app.host - .set_state("fixture_metrics_editor", Value::Num(2.)) - .unwrap(); - app.host - .set_state("fixture_metrics_width", Value::Num(120.)) - .unwrap(); - assert_eq!(result(&app).field("status"), Some(&Value::from("stale"))); - app.frames(3); - assert_ne!(editor_only(&mut app), original); - assert_eq!(number(&ready(&app), "width"), 120.); -} diff --git a/apps/zgx/src/composer_motion_tests.rs b/apps/zgx/src/composer_motion_tests.rs deleted file mode 100644 index 4ee065ad3..000000000 --- a/apps/zgx/src/composer_motion_tests.rs +++ /dev/null @@ -1,219 +0,0 @@ -use zgx_script::{ScriptHost, Value, testing::TestApp}; - -struct Controller { - app: TestApp, -} - -impl Controller { - fn new() -> Self { - let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("ui/composer_transition_fixture.luau"); - let app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); - app.ui.scene.borrow_mut().resize(800., 600.); - let mut c = Self { app }; - c.frames(30); - c - } - - fn set(&self, key: &str, value: Value) { - self.app.host.set_state(key, value).unwrap(); - } - - fn height(&self, height: f64) { - self.set("fixture_transition_height", Value::Num(height)); - } - - fn frames(&mut self, n: usize) { - for _ in 0..n { - self.app.frame(); - assert_eq!(self.app.error(), None); - } - } - - fn report(&self) -> Value { - assert!(self.app.host.run_command("fixture.transition.inspect")); - self.app - .host - .states() - .into_iter() - .find(|(n, _)| n == "fixture_transition_report") - .unwrap() - .1 - } -} - -fn num(v: &Value, field: &str) -> f64 { - v.field(field) - .and_then(Value::as_f64) - .unwrap_or_else(|| panic!("missing {field}: {v:?}")) -} - -#[test] -fn composer_transition_rules_cover_width_clip_headers_and_interruption() { - let ui = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui"); - let report = zgx_script::test_runner::run(&ui, Some("composer transition:"), |outcome| { - if let Some(failure) = &outcome.failure { - eprintln!("{}: {failure}", outcome.name); - } - }) - .unwrap(); - assert_eq!(report.outcomes.len(), 4); - assert_eq!(report.failed(), 0, "{report:?}"); -} - -#[test] -fn composer_transition_retargets_from_the_presented_frame_and_ignores_equal_destinations() { - let mut c = Controller::new(); - c.height(132.); - c.height(22.); // Reversed before the first display frame: already home. - assert_eq!(c.report().field("active"), Some(&Value::Bool(false))); - c.height(132.); - c.frames(3); - let before = c.report(); - assert_eq!(before.field("active"), Some(&Value::Bool(true))); - c.height(500.); - let replaced = c.report(); - assert_eq!( - replaced.field("origin"), - before.field("frame"), - "retarget must start at the presented snapshot" - ); - assert_eq!(num(&replaced, "progress"), 0.); - c.frames(3); - let growing = c.report(); - c.height(900.); // Same capped geometry, different natural content height. - let unchanged = c.report(); - assert_eq!(num(&unchanged, "progress"), num(&growing, "progress")); - assert_eq!(unchanged.field("origin"), growing.field("origin")); - c.frames(1); - assert!(num(&c.report(), "progress") > num(&growing, "progress")); - c.frames(30); - let settled = c.report(); - assert_eq!(settled.field("active"), Some(&Value::Bool(false))); - assert_eq!(settled.field("frame"), settled.field("destination")); - assert!( - !c.app.clock.wants_frame(), - "completed controller must stop requesting frames" - ); - c.height(22.); - c.frames(3); - let shrinking = c.report(); - assert_eq!(shrinking.field("active"), Some(&Value::Bool(true))); - c.height(180.); - assert_eq!( - c.report().field("origin"), - shrinking.field("frame"), - "reversing a shrink also starts at the presented snapshot" - ); - c.frames(30); - assert_eq!(c.report().field("frame"), c.report().field("destination")); - assert!(!c.app.clock.wants_frame()); -} - -#[test] -fn composer_transition_context_and_reduce_motion_snap_and_cancel_the_running_clip() { - let mut c = Controller::new(); - c.height(132.); - c.frames(3); - c.set("fixture_transition_ready", Value::Bool(false)); - c.set("fixture_transition_snap", Value::Num(1.)); - let snapped = c.report(); - assert_eq!(snapped.field("frame"), snapped.field("destination")); - assert_eq!(snapped.field("active"), Some(&Value::Bool(false))); - c.frames(100); // Readiness is independent of how long the machine takes. - assert_eq!(c.report().field("settling"), Some(&Value::Bool(true))); - c.height(500.); // A late measurement still belongs to the same context. - assert_eq!(c.report().field("frame"), c.report().field("destination")); - c.set("fixture_transition_revision", Value::Num(2.)); - c.set("fixture_transition_ready", Value::Bool(true)); - c.frames(2); - assert_eq!(c.report().field("settling"), Some(&Value::Bool(false))); - c.height(100.); - c.frames(3); - assert_eq!(c.report().field("active"), Some(&Value::Bool(true))); - c.set("motion_reduce", Value::Bool(true)); - let reduced = c.report(); - assert_eq!(reduced.field("frame"), reduced.field("destination")); - assert_eq!(reduced.field("active"), Some(&Value::Bool(false))); - c.frames(30); - assert_eq!( - c.report(), - reduced, - "cancelled callbacks cannot revive the old transition" - ); - assert!(!c.app.clock.wants_frame()); -} - -#[test] -fn composer_transition_unmount_freezes_progress_and_remount_owns_a_new_controller() { - let mut c = Controller::new(); - c.height(500.); - c.frames(3); - let before = c.report(); - c.set("fixture_transition_shown", Value::Bool(false)); - let disposed = c.report(); - assert_eq!(disposed.field("active"), Some(&Value::Bool(false))); - assert_eq!(num(&disposed, "progress"), num(&before, "progress")); - c.height(100.); - c.frames(30); - assert_eq!( - c.report(), - disposed, - "unmounted controller cannot keep updating" - ); - assert!(!c.app.clock.wants_frame()); - c.set("fixture_transition_shown", Value::Bool(true)); - let new = c.report(); - assert_eq!(num(&new, "progress"), 1.); - assert_eq!(new.field("frame"), new.field("destination")); - c.height(180.); - c.frames(3); - assert_eq!(c.report().field("active"), Some(&Value::Bool(true))); - c.frames(30); - assert_eq!(c.report().field("frame"), c.report().field("destination")); -} - -#[test] -fn composer_transition_replaced_context_cannot_acknowledge_a_pending_revision() { - let mut c = Controller::new(); - c.set("fixture_transition_snap", Value::Num(1.)); - // Replace the context before its deferred acknowledgement can run. - c.set("fixture_transition_ready", Value::Bool(false)); - c.set("fixture_transition_revision", Value::Num(2.)); - c.set("fixture_transition_snap", Value::Num(2.)); - c.height(500.); - c.frames(40); - assert_eq!(c.report().field("settling"), Some(&Value::Bool(true))); - assert_eq!(c.report().field("frame"), c.report().field("destination")); - c.set("fixture_transition_ready", Value::Bool(true)); - c.frames(2); - assert_eq!(c.report().field("settling"), Some(&Value::Bool(false))); - assert!(!c.app.clock.wants_frame()); -} - -#[test] -fn composer_width_only_resize_publishes_the_frame_without_animating_and_preserves_height_motion() { - let mut c = Controller::new(); - for height in [22., 132.] { - c.height(height); - c.frames(30); - for width in [620., 580., 660.] { - c.set("fixture_transition_width", Value::Num(width)); - let report = c.report(); - assert_eq!(report.field("active"), Some(&Value::Bool(false))); - assert_eq!(report.field("frame"), report.field("destination")); - assert_eq!(num(report.field("frame").unwrap(), "width"), width); - assert!(!c.app.clock.wants_frame()); - } - } - c.height(500.); - c.frames(3); - let shown = c.report(); - c.set("fixture_transition_width", Value::Num(600.)); - let resized = c.report(); - assert_eq!(resized.field("active"), Some(&Value::Bool(true))); - assert_eq!(resized.field("origin"), shown.field("frame")); - c.frames(30); - assert_eq!(c.report().field("frame"), c.report().field("destination")); - assert!(!c.app.clock.wants_frame()); -} 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/links.rs b/apps/zgx/src/links.rs index a2486dcce..8d647e251 100644 --- a/apps/zgx/src/links.rs +++ b/apps/zgx/src/links.rs @@ -71,41 +71,6 @@ pub fn open(url: &str) { }); } -/// Banner clicks, taken in this process. The engine posts Zeron's banners, -/// but macOS hands a click to the app its bundle names (Zeron.app's -/// interface, this process), never to the engine that posted it, so the -/// engine's own route (`open -a … zeron://notification/…`) never runs. As -/// Zeron.app's interface always has (crates/ui `notify::on_click`), the -/// click is answered here: Zeron comes forward, on the banner's chat. -pub fn take_banner_clicks() { - zeron_notify::on_click(|target| { - activate(); - clicked(&target); - }); -} - -/// A clicked banner's target (a chat, or agent updates), opened as its -/// `zeron://notification/` link is. -fn clicked(target: &str) { - open(&zeron_notify::link(target)); -} - -/// This app to the front: a banner was clicked. (Main thread: the click -/// comes through the AppKit delegate.) -#[cfg(target_os = "macos")] -fn activate() { - use objc::{class, msg_send, sel, sel_impl}; - unsafe { - let app: *mut objc::runtime::Object = msg_send![class!(NSApplication), sharedApplication]; - if !app.is_null() { - let _: () = msg_send![app, activateIgnoringOtherApps: true]; - } - } -} - -#[cfg(not(target_os = "macos"))] -fn activate() {} - fn tell(target: Target) { let seq = SEQ.with(|s| { s.set(s.get() + 1); @@ -129,28 +94,6 @@ fn tell(target: Target) { mod tests { use super::*; - fn last() -> (Option, Option) { - LAST.with(|l| { - let l = l.borrow(); - let text = |k: &str| match l.field(k) { - Some(Value::Str(s)) => Some(s.to_string()), - Some(Value::Text(s)) => Some(s.to_string()), - _ => None, - }; - (text("chat"), text("settings")) - }) - } - - /// A banner clicked in this process (macOS hands the click to the app, - /// not the engine that posted it) opens its chat as its link would. - #[test] - fn a_clicked_banner_opens_its_chat() { - clicked("chat-42"); - assert_eq!(last(), (Some("chat-42".into()), None)); - clicked(zeron_notify::AGENT_UPDATES_TARGET); - assert_eq!(last(), (None, Some("providers".into()))); - } - #[test] fn banners_and_chat_links_open_where_zeron_s_do() { assert_eq!(parse(&zeron_notify::link("chat-1")), Some((Target::Chat("chat-1".into()), None))); diff --git a/apps/zgx/src/main.rs b/apps/zgx/src/main.rs index e3431f7b8..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; @@ -86,12 +90,6 @@ mod split_tests; #[cfg(test)] mod route_motion_tests; #[cfg(test)] -mod composer_metrics_tests; -#[cfg(test)] -mod composer_layout_tests; -#[cfg(test)] -mod composer_motion_tests; -#[cfg(test)] mod hit_sweep_tests; #[cfg(test)] mod hit_tests; @@ -116,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; @@ -140,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}; @@ -352,8 +354,6 @@ fn main() -> Result<(), Box> { } }); } - // A clicked banner: Zeron forward on its chat (src/links.rs). - links::take_banner_clicks(); let options = match screen { Screen::Intro => intro_window(), Screen::Onboarding => onboarding_window(), @@ -821,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/grants.rs b/apps/zgx/src/plugins/grants.rs index 2e731d6e2..6093dfebd 100644 --- a/apps/zgx/src/plugins/grants.rs +++ b/apps/zgx/src/plugins/grants.rs @@ -39,9 +39,6 @@ pub const SHARED_READ: &[(&str, Option<&str>)] = &[ ("zeron.project", Some("zeron.sessions")), ("zeron.device", Some("zeron.devices")), ("zeron.devices", Some("zeron.devices")), - // The draft in the window's composer, for a plugin allowed "draft": - // `{ session, text, attachments = { { kind, name } } }`, never a path. - ("zeron.draft", Some("zeron.draft")), // Whether the window shows and is in front (the SDK's `zeron.window`): // work no one would see waits for it. ("zgx.window.shown", None), @@ -80,9 +77,9 @@ pub const ACTIONS: &[(&str, &str)] = &[ pub const NEVER_REPLACED: &[&str] = &["@app/screens/plugins", "@app/screens/shell/settings"]; /// Each plugin's own actions (`plugin..`). -pub const OWN_ACTIONS: &[&str] = &["result", "ack", "send", "menus", "toast", "problem", "open_tab", "add_page", "github_watch", "github_call", "store_set", "setting_status", "open_url", "notify", "cost", "run", "db", "service_call", "agents", "menu_run", "compose_comment", "draft", "draft_of", "file", "block"]; +pub const OWN_ACTIONS: &[&str] = &["result", "ack", "send", "menus", "toast", "problem", "open_tab", "add_page", "github_watch", "github_call", "store_set", "setting_status", "open_url", "notify", "cost", "run", "db", "service_call", "agents", "menu_run", "compose_comment", "file", "block"]; /// Each plugin's own shared state, to read (`plugin..`). -pub const OWN_SHARED: &[&str] = &["calls", "inbox", "records", "github", "store", "settings", "locales", "menu_items", "composed", "drafts"]; +pub const OWN_SHARED: &[&str] = &["calls", "inbox", "records", "github", "store", "settings", "locales", "menu_items", "composed"]; /// The capability naming plugin `id`'s own calls and actions. pub fn own(id: &str) -> Capability { @@ -255,9 +252,6 @@ pub fn capabilities(id: &str, permissions: &[String], change: &[String], replace "pages.add" => out.push(Capability::OverrideElement(page_selector(id))), "navigate" => out.push(named("zeron.navigate")), "compose" => out.push(named("zeron.compose")), - // Reading the composer's draft (`zeron.draft`); taking it and - // putting it back are its own action's, checked by the host. - "draft" => out.push(named("zeron.draft")), "devices" => out.push(named("zeron.devices")), "network" => out.push(Capability::Fetch), "clipboard" => out.push(Capability::Clipboard), @@ -400,15 +394,6 @@ mod tests { assert!(!got.iter().any(|c| matches!(c, Capability::Override(_)))); } - /// "draft" reads the composer's draft; "compose" alone never does. - #[test] - fn only_draft_reads_the_draft() { - let draft = Capability::Named("zeron.draft".into()); - assert_eq!(capabilities("stash", &s(&["draft"]), &[], &[]), vec![own("stash"), draft.clone()]); - assert!(!capabilities("x", &s(&["compose"]), &[], &[]).contains(&draft)); - assert!(SHARED_READ.contains(&("zeron.draft", Some("zeron.draft")))); - } - #[test] fn listed_targets_without_the_permission_give_nothing() { let got = capabilities("x", &[], &s(&["#titlebar-actions"]), &s(&["@app/screens/chat"])); diff --git a/apps/zgx/src/plugins/mod.rs b/apps/zgx/src/plugins/mod.rs index a557ef1bc..43e8a8776 100644 --- a/apps/zgx/src/plugins/mod.rs +++ b/apps/zgx/src/plugins/mod.rs @@ -456,88 +456,6 @@ pub(crate) fn comment(asked: &Value, shown: &str) -> Option { Some(json_value(&out)) } -/// The longest key a plugin keeps a taken draft under. -const DRAFT_KEY: usize = 64; - -/// A plugin's ask of the composer's draft (`zeron.take_draft`, -/// `put_draft`, `swap_draft`, `drop_draft`), held to `{ op, key }`: `op` -/// one of "take", "put", "swap" or "drop", `key` 1 to 64 characters with -/// no control characters. Anything else is refused. -pub(crate) fn draft_ask(asked: &Value) -> Option { - let op = asked.field("op").map(text)?; - let key = asked.field("key").map(text)?; - let valid_key = !key.is_empty() && key.chars().count() <= DRAFT_KEY && !key.chars().any(char::is_control); - if !matches!(op.as_str(), "take" | "put" | "swap" | "drop") || !valid_key { - return None; - } - Some(Value::record([("op", Value::from(op.as_str())), ("key", Value::from(key.as_str()))])) -} - -/// What `zeron.compose` asks of the composer: its words alone (added to -/// the draft shown, as ever), or with `{ text, replace, session }` (the -/// draft's words replaced, or a chat named other than the one shown). -pub(crate) fn compose(asked: &Value) -> Value { - let Some(words) = asked.field("text") else { - return Value::from(text(asked).as_str()); - }; - let words = match words { - Value::Str(_) | Value::Text(_) => words.clone(), - _ => Value::from(""), - }; - let mut fields = vec![ - ("text", words), - ("replace", Value::from(matches!(asked.field("replace"), Some(Value::Bool(true))))), - ]; - if let Some(session) = asked.field("session").map(text).filter(|s| !s.is_empty()) { - fields.push(("session", Value::from(session.as_str()))); - } - Value::record(fields) -} - -/// The app's shared states a chat's draft lives in (screens/chat/shared), -/// each read with the value the app starts it at: read before the app has -/// made one, it's made as the app would. -const DRAFT_HERE: &str = "chat.composer.here"; -const DRAFT_TEXT: &str = "chat.composer.text"; -const DRAFT_CARGO: &str = "chat.composer.cargo"; -const DRAFTS_KEPT: &str = "chat.composer.drafts"; - -/// What a draft carries, as plugins are told it (as `zeron.draft()` says -/// it, screens/plugins): each chip's kind and name, never a file's path. -fn draft_attachments(cargo: &Value) -> Value { - let said = |item: &Value, field: &str, or: &str| match item.field(field) { - Some(v @ Value::Str(_)) => v.clone(), - _ => Value::from(or), - }; - vlist(cargo.items().iter().map(|item| Value::record([("kind", said(item, "kind", "file")), ("name", said(item, "name", ""))])).collect()) -} - -/// Chat `session`'s draft, for `zeron.draft_of` ("draft"): out in the -/// composer (`here` its chat, "" a new chat's; `shown` its words, `cargo` -/// its chips) or kept for it (`kept`, by chat). Its words as typed, what it -/// carries, and its chat (none for a new chat's). A chat with no draft has -/// no words and carries nothing. -pub(crate) fn draft_of(session: &str, here: &Value, shown: &Value, cargo: &Value, kept: &Value) -> Value { - let (words, carried) = if session == text(here) { - (shown.clone(), cargo.clone()) - } else { - let d = kept.field(session); - ( - d.and_then(|d| d.field("text")).cloned().unwrap_or_default(), - d.and_then(|d| d.field("cargo")).cloned().unwrap_or_default(), - ) - }; - let words = match words { - Value::Str(_) | Value::Text(_) => words, - _ => Value::from(""), - }; - let mut fields = vec![("text", words), ("attachments", draft_attachments(&carried))]; - if !session.is_empty() { - fields.push(("session", Value::from(session))); - } - Value::record(fields) -} - /// Plugins for the main window `cx` shows `host` in. pub fn install(cx: &mut WindowContext, host: &ScriptHost) { expose_surface(host); @@ -2480,49 +2398,6 @@ impl Bridge { } } }); - // The composer's draft taken (its words, its chips, the passages - // quoted in it), put back or let go, under a key of the plugin's, - // for a plugin allowed "draft": screens/plugins keeps what's taken - // (the plugin never holds its files' paths) and tells it what it - // keeps as `plugin..drafts`. - host.action(format!("plugin.{id}.draft"), { - let (me, plugin) = (me.clone(), plugin.clone()); - move |asked| { - if let Some(b) = me.upgrade() - && b.item(&plugin).is_some_and(|i| i.may("draft")) - { - match draft_ask(&asked) { - Some(asked) => b.request("draft", &plugin, asked), - None => eprintln!("zeron-zgx: plugin {plugin}: a draft ask that isn't take, put, swap or drop with a key, refused"), - } - } - } - }); - // Another chat's draft, as it is (`zeron.draft_of`, "draft"): read - // where the app keeps it, once, never followed. - host.async_action(format!("plugin.{id}.draft_of"), { - let (me, plugin) = (me.clone(), plugin.clone()); - move |asked| { - let (me, plugin) = (me.clone(), plugin.clone()); - async move { - let b = me.upgrade().ok_or("This plugin isn't on")?; - let item = b.item(&plugin).ok_or("This plugin isn't on")?; - if !item.may("draft") { - return Err("This plugin wasn't allowed to read what you type in the message box".to_string()); - } - let session = text(&asked); - if session.chars().count() > 200 { - return Err("That isn't a chat".to_string()); - } - let read = |name: &str, initial: Value| b.host.shared(name, initial).signal().with_untracked(Value::clone); - let here = read(DRAFT_HERE, Value::from("")); - let shown = read(DRAFT_TEXT, Value::from("")); - let cargo = read(DRAFT_CARGO, vlist(Vec::new())); - let kept = read(DRAFTS_KEPT, Value::record(Vec::<(&str, Value)>::new())); - Ok(draft_of(&session, &here, &shown, &cargo, &kept)) - } - } - }); host.action(format!("plugin.{id}.open_url"), { let (me, plugin) = (me.clone(), plugin.clone()); move |url| { @@ -3423,9 +3298,9 @@ impl Bridge { }); host.action("zeron.compose.insert", { let me = me.clone(); - move |asked| { + move |text_| { if let Some(b) = me.upgrade() { - b.request("compose", "", compose(&asked)); + b.request("compose", "", Value::from(text(&text_).as_str())); } } }); @@ -3567,52 +3442,6 @@ mod tests { assert_eq!(gone, vec!["clock".to_string(), "ping".to_string()]); } - /// `zeron.compose(text)` adds its words as ever; with options, it says - /// whether they replace the draft and which chat's (none: the shown). - #[test] - fn compose_takes_words_alone_or_with_replace_and_a_chat() { - assert_eq!(compose(&Value::from("hello")), Value::from("hello")); - let asked = json_value(&json!({ "text": "clearer", "replace": true, "session": "s2" })); - let out = compose(&asked); - assert_eq!(text(out.field("text").unwrap()), "clearer"); - assert_eq!(out.field("replace"), Some(&Value::from(true))); - assert_eq!(text(out.field("session").unwrap()), "s2"); - // (Only `true` replaces; a blank chat is the one shown; words that - // aren't text are none.) - let loose = json_value(&json!({ "text": 5, "replace": "yes", "session": "" })); - let out = compose(&loose); - assert_eq!(text(out.field("text").unwrap()), ""); - assert_eq!(out.field("replace"), Some(&Value::from(false))); - assert!(out.field("session").is_none()); - } - - /// `zeron.draft_of`: the chat shown's draft (from the composer), or - /// another chat's (kept), with its chat and its chips by kind and name - /// alone; a new chat's has no chat; a chat with none, nothing. - #[test] - fn a_chats_draft_is_read_where_it_is_kept_without_paths() { - let kept = json_value(&json!({ - "s2": { "text": "kept words", "cargo": [{ "kind": "file", "name": "a.rs", "path": "/Users/me/a.rs" }] }, - "": { "text": "new chat words", "cargo": [] }, - })); - let (here, shown) = (Value::from("s1"), Value::from("typing now")); - let cargo = json_value(&json!([{ "kind": "image", "name": "shot.png", "path": "/tmp/shot.png" }])); - let out = draft_of("s1", &here, &shown, &cargo, &kept); - assert_eq!(value_json(&out), json!({ "text": "typing now", "session": "s1", "attachments": [{ "kind": "image", "name": "shot.png" }] })); - let out = draft_of("s2", &here, &shown, &cargo, &kept); - assert_eq!(value_json(&out), json!({ "text": "kept words", "session": "s2", "attachments": [{ "kind": "file", "name": "a.rs" }] })); - assert!(!out.to_json().contains("/Users/me"), "never a path"); - // A chat with no draft: nothing written, nothing carried. - let out = draft_of("s9", &here, &shown, &cargo, &kept); - assert_eq!(value_json(&out), json!({ "text": "", "session": "s9", "attachments": [] })); - // A new chat's draft, kept while a chat shows: no chat. - let out = draft_of("", &here, &shown, &cargo, &kept); - assert_eq!(value_json(&out), json!({ "text": "new chat words", "attachments": [] })); - // Before the app has made its states: nothing, not an error. - let out = draft_of("s1", &Value::Nil, &Value::Nil, &Value::Nil, &Value::Nil); - assert_eq!(text(out.field("text").unwrap()), ""); - } - /// R21: a plugin's review comment goes on the chat shown alone, with /// at most the 40 lines of code that are sent (a 20,000-character /// snippet once rode along unseen, onto any chat's draft). @@ -3633,23 +3462,6 @@ mod tests { assert!(text(comment(&wide, "s1").unwrap().field("snippet").unwrap()).len() <= COMMENT_BYTES); } - /// A plugin's ask of the draft is take, put, swap or drop, under a key - /// of 1 to 64 characters; nothing else rides along to the app. - #[test] - fn a_draft_ask_is_take_put_or_drop_with_a_key() { - let ask = |v: Json| draft_ask(&json_value(&v)); - let took = ask(json!({ "op": "take", "key": "stash", "extra": "dropped" })).expect("a take"); - assert_eq!(value_json(&took), json!({ "op": "take", "key": "stash" })); - assert!(ask(json!({ "op": "put", "key": "k".repeat(64) })).is_some()); - assert!(ask(json!({ "op": "drop", "key": "é" })).is_some()); - assert!(ask(json!({ "op": "swap", "key": "stash" })).is_some()); - assert!(ask(json!({ "op": "send", "key": "stash" })).is_none(), "not an op"); - assert!(ask(json!({ "op": "take", "key": "" })).is_none(), "no key"); - assert!(ask(json!({ "op": "take" })).is_none(), "no key"); - assert!(ask(json!({ "op": "take", "key": "k".repeat(65) })).is_none(), "too long"); - assert!(ask(json!({ "op": "take", "key": "a\nb" })).is_none(), "a control character"); - } - #[test] fn only_a_plugin_on_loads_from_its_snapshot_with_its_grants() { let on = item(json!({ diff --git a/apps/zgx/src/plugins/preview.rs b/apps/zgx/src/plugins/preview.rs index 4b3e7a029..8e2351788 100644 --- a/apps/zgx/src/plugins/preview.rs +++ b/apps/zgx/src/plugins/preview.rs @@ -79,9 +79,6 @@ pub const BLOCKED: &[&str] = &[ "chat.configure", "agents.install", "agents.set_key", - // (An API key, and what an agent's keys are called.) - "agents.add_key", - "agents.rename", "agents.sign_in", "agents.sign_out", "agents.remove", @@ -95,7 +92,6 @@ pub const BLOCKED: &[&str] = &[ "projects.clone", "projects.delete", "github.disconnect", - "github.forget", "engine.restart", "billing.checkout", // (Each runs a machine, or says which one new chats run on: credits.) @@ -103,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 { @@ -540,48 +545,12 @@ mod tests { std::fs::write(path, text).unwrap(); } - /// Every action the app's code refuses to a preview (`refuse("…")`, - /// `refuse_aloud("…")`) is in `BLOCKED`: else its refusal never fires. - #[test] - fn every_action_the_code_refuses_is_blocked() { - fn walk(dir: &Path, out: &mut Vec<(PathBuf, String)>) { - for entry in std::fs::read_dir(dir).unwrap().flatten() { - let path = entry.path(); - if path.is_dir() { - walk(&path, out); - } else if path.extension().is_some_and(|e| e == "rs") { - out.push((path.clone(), std::fs::read_to_string(&path).unwrap())); - } - } - } - let mut files = Vec::new(); - walk(&Path::new(env!("CARGO_MANIFEST_DIR")).join("src"), &mut files); - let mut named = Vec::new(); - for (path, text) in &files { - for marker in ["refuse(\"", "refuse_aloud(\""] { - let mut rest = text.as_str(); - while let Some(at) = rest.find(marker) { - rest = &rest[at + marker.len()..]; - let Some(end) = rest.find('"') else { break }; - let action = &rest[..end]; - // (Action names only: this test's own doc says `refuse("…")`.) - if action.chars().all(|c| c.is_ascii_lowercase() || c == '_' || c == '.') { - named.push((path.display().to_string(), action.to_string())); - } - } - } - } - assert!(named.iter().any(|(_, a)| a == "agents.add_key"), "the scan finds the calls"); - let missing: Vec<_> = named.iter().filter(|(_, action)| !blocked(action)).collect(); - assert!(missing.is_empty(), "refused but not in BLOCKED: {missing:?}"); - } - #[test] fn preview_refuses_the_dangerous_actions_only() { set(None); assert!(refuse("terminal.start").is_ok(), "nothing in preview: all allowed"); set(Some(Preview { id: "agent".into(), name: "Agent's".into() })); - for action in ["terminal.start", "terminal.write", "account.sign_out", "account.delete", "plugins.install", "templates.use", "files.write", "chat.send", "chat.respond", "chat.retry", "chat.queue_edit", "chat.queue_finish", "chat.configure", "files.open_external", "github.forget", "github.disconnect"] { + for action in ["terminal.start", "terminal.write", "account.sign_out", "account.delete", "plugins.install", "templates.use", "files.write", "chat.send", "chat.respond", "chat.retry", "chat.queue_edit", "chat.queue_finish", "chat.configure", "files.open_external"] { assert!(refuse(action).is_err(), "{action}"); } for action in ["files.read", "files.list", "chat.open", "appearance", "templates.review", "sessions.seen"] { 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/route_motion_tests.rs b/apps/zgx/src/route_motion_tests.rs index 62348b2d8..66bc2f353 100644 --- a/apps/zgx/src/route_motion_tests.rs +++ b/apps/zgx/src/route_motion_tests.rs @@ -1,709 +1,85 @@ -//! Composer focus and layout transitions on the real shell fixture. -//! TestApp supplies the frame/timer clock; readiness is revision-based. +//! The composer between a new chat and a session (ui/screens/chat/composer, +//! mounted headless on ui/shell_fixture.luau): another chat opened or a new +//! chat puts it where it goes at once, no fade and no slide. (The Lua tests can't read an opacity: +//! ui/tests/shell_route_motion.test.luau has the panel's side of it.) -use zgx::{ - input::{InputEvent, Key}, - scene::{NodeId, Rect}, -}; -use zgx_script::{ScriptHost, Value, testing::TestApp}; +use std::time::Duration; -// Characterization, not a reproduction of the reported visual glitch. -// These snapshots observe the scene, not GPU pixels. See -// docs/composer.md for the separate visual procedure. -const FIELD: &str = "#chat-composer >> role:textbox"; -const BUILD: &str = "#shell-sidebar >> role:button \"Fix the build\""; -const DOCS: &str = "#shell-sidebar >> role:button \"Write the docs\""; -const NEW: &str = "#shell-sidebar >> role:button \"New chat\""; +use zgx_script::{ScriptHost, testing::TestApp}; -struct Shell { - app: TestApp, - frame: u32, +fn mount() -> TestApp { + // (This checkout's `ui/`, or `ZERON_ZGX_TEST_UI`'s: another copy, to see + // the test fail on code from before a fix.) + let ui = std::env::var_os("ZERON_ZGX_TEST_UI") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui")); + let path = ui.join("shell_fixture.luau"); + let app = TestApp::new(|runtime| ScriptHost::new(runtime, path)); + app.ui.scene.borrow_mut().resize(1200., 800.); + app.frames(30); + app } -impl Shell { - fn new(reduced: bool) -> Self { - Self::of("shell_fixture.luau", reduced) - } - - fn of(fixture: &str, reduced: bool) -> Self { - let ui = std::env::var_os("ZERON_ZGX_TEST_UI") - .map(std::path::PathBuf::from) - .unwrap_or_else(|| std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui")); - let app = TestApp::new(|runtime| ScriptHost::new(runtime, ui.join(fixture))); - app.ui.scene.borrow_mut().resize(1200., 800.); - let motion = app - .host - .states() - .into_iter() - .find(|(name, _)| name.ends_with("::motion_reduce")) - .expect("the shell's motion preference") - .0; - app.host.set_state(&motion, Value::Bool(reduced)).unwrap(); - let mut shell = Self { app, frame: 0 }; - shell.frames(60); - shell - } - - fn frames(&mut self, n: usize) { - for _ in 0..n { - self.frame += 1; - self.app.frame(); - assert_eq!( - self.app.error(), - None, - "script error at frame {}", - self.frame - ); - } - } - - fn geometry(&mut self) -> Geometry { - let composer = self.app.find("#chat-composer").expect("composer"); - let field = self.app.find(FIELD).expect("draft field"); - let scene = self.app.ui.scene.borrow(); - Geometry { - composer: scene.transformed_bounds(composer), - field: scene.transformed_bounds(field), - } - } +/// A frame and a little of the clock, `n` times: each look at the dock's +/// opacity, the composer's top and its height (the pill's, with what hangs +/// off it). +fn watch(app: &mut TestApp, n: usize) -> Vec<(f32, f32, f32)> { + (0..n) + .map(|_| { + app.frames(1); + std::thread::sleep(Duration::from_millis(12)); + let b = app.bounds("chat-composer"); + (app.opacity("chat-composer-dock"), b.y, b.height) + }) + .collect() } -#[derive(Clone, Copy, Debug)] -struct Geometry { - composer: Rect, - field: Rect, -} - -#[derive(Debug)] -struct Sample { - frame: u32, - editor: NodeId, - focused: Option, - selection: (usize, usize), - text: String, - presented: Geometry, - target: Geometry, - opacity: f32, - visible: bool, -} - -struct Composer { - shown: Shell, - target: Shell, - editor: NodeId, - trace: Vec, - print_trace: bool, -} - -impl Composer { - fn new(session: bool) -> Self { - let mut shown = Shell::new(false); - let mut target = Shell::new(true); - if session { - shown.app.click(BUILD); - target.app.click(BUILD); - shown.frames(60); - target.frames(60); - } - let field = shown.app.find(FIELD).unwrap(); - let editor = shown.app.ui.editor_within(field).unwrap().node; - Self { - shown, - target, - editor, - trace: Vec::new(), - print_trace: std::env::var_os("ZERON_ZGX_COMPOSER_TRACE").is_some_and(|v| v != "0"), - } - } - - /// Same input in both shells. The reduced-motion shell supplies the - /// destination through real layout rather than duplicating its formulas. - fn act(&mut self, action: impl Fn(&mut TestApp)) { - action(&mut self.shown.app); - action(&mut self.target.app); - self.target.frames(2); - } - - fn watch(&mut self, n: usize) { - for _ in 0..n { - self.shown.frames(1); - let presented = self.shown.geometry(); - let target = self.target.geometry(); - let field = self.shown.app.find(FIELD).unwrap(); - let editor = self.shown.app.ui.editor_within(field).unwrap(); - let scene = self.shown.app.ui.scene.borrow(); - let opacity = std::iter::once(editor.node) - .chain(scene.ancestors(editor.node)) - .map(|id| scene.effects(id).opacity) - .product(); - let sample = Sample { - frame: self.shown.frame, - editor: editor.node, - focused: self.shown.app.ui.input.focused(), - selection: editor.selection(), - text: editor.editor.borrow().text().into_owned(), - presented, - target, - opacity, - visible: scene.visible_bounds(editor.node).is_some(), - }; - drop(scene); - self.trace.push(sample); - let s = self.trace.last().unwrap(); - if self.print_trace { - eprintln!( - "composer-trace {} {s:?}", - std::thread::current().name().unwrap_or("unnamed") - ); - } - assert_eq!(s.editor, self.editor, "editor replaced: {:?}", self.trace); - assert!( - s.visible && s.opacity > 0.99, - "editor hidden: {:?}", - self.trace - ); - for r in [ - s.presented.composer, - s.presented.field, - s.target.composer, - s.target.field, - ] { - assert!( - r.x.is_finite() - && r.y.is_finite() - && r.width.is_finite() - && r.height.is_finite() - && r.width > 0. - && r.height > 0., - "invalid geometry: {:?}", - self.trace - ); - } - } - } - - fn last(&self) -> &Sample { - self.trace.last().expect("observed frames") - } - - fn draft(&self, text: &str, selection: (usize, usize)) { - for s in &self.trace { - assert_eq!( - s.text, text, - "draft changed at frame {}: {:?}", - s.frame, self.trace - ); - assert_eq!( - s.selection, selection, - "selection changed: {:?}", - self.trace - ); - assert_eq!(s.focused, Some(self.editor), "focus lost: {:?}", self.trace); - } - } - - fn at_target(&self) { - let s = self.last(); - for (a, b) in [ - (s.presented.composer, s.target.composer), - (s.presented.field, s.target.field), - ] { - assert!( - (a.x - b.x).abs() < 1. - && (a.y - b.y).abs() < 1. - && (a.width - b.width).abs() < 1. - && (a.height - b.height).abs() < 1., - "did not reach reduced-motion target: {:?}", - self.trace - ); - } - } -} - -fn select(app: &mut TestApp, anchor: usize, focus: usize) { - let field = app.find(FIELD).unwrap(); - app.ui - .editor_within(field) - .unwrap() - .set_selection(anchor, focus); -} - -fn type_at_caret(app: &mut TestApp, text: &str) { - // TestApp::type_text clicks again; dispatch directly so typing really - // preserves the current caret/selection, including during a transition. - app.ui.dispatch(InputEvent::Text(text.into())); - app.settle(); -} - -#[test] -fn composer_rest_size_is_independent_of_animated_draft_size_and_picker_placement() { - let mut c = Composer::new(false); - let sizes = |app: &TestApp| { - app.host - .states() - .into_iter() - .find(|(n, _)| n.ends_with("::chat_composer_sizes")) - .unwrap() - .1 - }; - let rest = sizes(&c.shown.app).field("rest").unwrap().as_f64().unwrap(); - c.act(|app| app.fill(FIELD, "one\ntwo\nthree\nfour\nfive\nsix")); - c.watch(3); - let moving = sizes(&c.shown.app); - assert_eq!(moving.field("rest").unwrap().as_f64(), Some(rest)); - assert!(moving.field("target").unwrap().as_f64().unwrap() > rest); - assert!( - moving.field("presented").unwrap().as_f64().unwrap() - < moving.field("target").unwrap().as_f64().unwrap() - ); - c.watch(40); - c.act(|app| app.fill(FIELD, "")); - c.watch(1); - assert_eq!( - sizes(&c.shown.app).field("rest").unwrap().as_f64(), - Some(rest), - "clearing needs no settle timer" - ); - c.watch(40); - c.act(|app| app.click("#chat-composer >> role:button \"Model\"")); - c.watch(2); - let button = c - .shown - .app - .find("#chat-composer >> role:button \"Model\"") - .unwrap(); - let y = c.shown.app.ui.scene.borrow().transformed_bounds(button).y; - c.act(|app| { - app.host - .set_state( - "chat.composer.text", - Value::from("one\ntwo\nthree\nfour\nfive\nsix"), - ) - .unwrap() - }); - c.watch(40); - let held = c.shown.app.ui.scene.borrow().transformed_bounds(button).y; - assert!( - (held - y).abs() < 1., - "open picker retains its anchor: {y} -> {held}" - ); -} - -#[test] -fn composer_transition_near_the_height_cap_preserves_scroll_editing_and_the_caret() { - for session in [false, true] { - let mut c = Composer::new(session); - c.act(|app| app.click(FIELD)); - let first = (0..10) - .map(|i| format!("line {i}")) - .collect::>() - .join("\n"); - c.act(|app| type_at_caret(app, &first)); - c.watch(3); - let more = (10..80).map(|i| format!("\nline {i}")).collect::(); - c.act(|app| type_at_caret(app, &more)); - c.trace.clear(); - c.watch(40); - let text = format!("{first}{more}"); - c.draft(&text, (text.len(), text.len())); - c.at_target(); - let cap = c.last().presented; - let field = c.shown.app.find(FIELD).unwrap(); - let editor = c.shown.app.ui.editor_within(field).unwrap(); - let caret_before = c - .shown - .app - .ui - .scene - .borrow() - .transformed_bounds(editor.caret); - c.act(|app| app.scroll(FIELD, -120.)); - c.watch(3); - let caret_scrolled = c - .shown - .app - .ui - .scene - .borrow() - .transformed_bounds(editor.caret); - assert!( - (caret_before.y - caret_scrolled.y).abs() > 20., - "the test must actually scroll the editor" - ); - c.act(|app| type_at_caret(app, " typed after scrolling")); - c.trace.clear(); - c.watch(20); - let text = format!("{text} typed after scrolling"); - c.draft(&text, (text.len(), text.len())); - for s in &c.trace { - assert_eq!( - s.presented.composer.height, cap.composer.height, - "typing above the cap must not resize the composer" - ); - assert_eq!(s.presented.field, cap.field); - } - let caret = c - .shown - .app - .ui - .scene - .borrow() - .transformed_bounds(editor.caret); - assert!( - caret.y >= cap.field.y - 1. - && caret.y + caret.height <= cap.field.y + cap.field.height + 1., - "typing must reveal the caret inside the viewport: {caret:?}, {cap:?}" - ); - } -} - -#[test] -fn composer_click_and_focus_preserve_geometry_and_the_selected_draft() { - for session in [false, true] { - let mut c = Composer::new(session); - c.watch(1); - let empty = c.last().presented; - c.act(|app| app.click(FIELD)); - c.trace.clear(); - c.watch(6); - c.draft("", (0, 0)); - for s in &c.trace { - assert_eq!( - s.presented.composer, empty.composer, - "empty click resized composer: {s:?}" - ); - assert_eq!( - s.presented.field, empty.field, - "empty click moved field: {s:?}" - ); - } - c.trace.clear(); - c.act(|app| app.fill(FIELD, "hello composer")); - c.watch(40); - c.at_target(); - let before = c.last().presented; - c.trace.clear(); - c.act(|app| { - app.ui.input.focus(&app.ui.scene, None); - }); - c.watch(2); - assert_eq!(c.last().focused, None); - c.act(|app| app.click(FIELD)); - c.trace.clear(); - c.watch(2); - let clicked = c.last().selection; - c.draft("hello composer", clicked); - for s in &c.trace { - assert_eq!(s.presented.field, before.field, "click moved field: {s:?}"); - } - c.act(|app| select(app, 1, 8)); - c.trace.clear(); - c.watch(40); - c.draft("hello composer", (1, 8)); - for s in &c.trace { - assert_eq!( - s.presented.composer, before.composer, - "focus resized composer: {s:?}" - ); - assert_eq!(s.presented.field, before.field, "focus moved field: {s:?}"); - } - c.at_target(); - } -} - -#[test] -fn composer_character_typing_crosses_the_wrap_boundary_and_folds_back() { - let mut c = Composer::new(true); - c.act(|app| app.click(FIELD)); - c.watch(1); - let compact = c.last().target.composer.height; - let compact_width = c.last().presented.field.width; - let mut text = String::new(); - // Find the boundary with the actual font, without copying width rules. - while c.last().target.composer.height <= compact + 1. { - assert!(text.len() < 256, "never expanded: {:?}", c.trace); - text.push('W'); - c.act(|app| type_at_caret(app, "W")); - c.trace.clear(); - c.watch(1); - c.draft(&text, (text.len(), text.len())); - } - c.watch(30); - c.at_target(); - let expanded_width = c.last().target.field.width; - let mut width = compact_width; - let mut changes = 0; - for s in &c.trace { - let next = s.presented.field.width; - assert!( - (next - compact_width).abs() < 1. || (next - expanded_width).abs() < 1., - "mode change must not rewrap through intermediate widths: {s:?}" - ); - if (next - width).abs() >= 1. { - changes += 1; - } - width = next; - } - assert_eq!( - changes, 1, - "the real editor changes width once at expansion" - ); - assert!(c.last().presented.composer.height > compact + 20.); - while !text.is_empty() { - c.act(|app| app.key(Key::Backspace)); - text.pop(); - c.trace.clear(); - c.watch(1); - c.draft(&text, (text.len(), text.len())); - } - c.watch(40); - c.at_target(); - assert!((c.last().presented.composer.height - compact).abs() < 1.); -} - -#[test] -fn composer_growth_shrink_and_typing_during_a_transition_keep_the_editor() { - for session in [false, true] { - let mut c = Composer::new(session); - c.act(|app| app.click(FIELD)); - c.watch(1); - let short = c.last().presented.composer.height; - let first = "one\ntwo\nthree\nfour\nfive\nsix"; - c.act(|app| type_at_caret(app, first)); - c.trace.clear(); - c.watch(3); - c.draft(first, (first.len(), first.len())); - let mid = c.last(); - assert!( - mid.presented.composer.height > short + 1. - && mid.presented.composer.height < mid.target.composer.height - 1., - "must interrupt an actual growth transition: {:?}", - c.trace - ); - c.act(|app| type_at_caret(app, "\nseven")); - c.trace.clear(); - c.watch(40); - let text = format!("{first}\nseven"); - c.draft(&text, (text.len(), text.len())); - c.at_target(); - let tall = c.last().presented.composer.height; - c.act(|app| { - select(app, 0, text.len()); - type_at_caret(app, "short"); - }); - c.trace.clear(); - c.watch(3); - assert!( - c.last().presented.composer.height < tall - 1. - && c.last().presented.composer.height > c.last().target.composer.height + 1., - "shrink should ease: {:?}", - c.trace - ); - c.watch(40); - c.draft("short", (5, 5)); - c.at_target(); +/// Every look after a switch: shown in full (no fade), at its final height +/// (nothing growing or shrinking), and already where it ends (`to`), give +/// or take a frame of its size being measured (a quarter of the way +/// between the two places; a slide would pass through the rest). +fn at_once(seen: &[(f32, f32, f32)], from: f32, to: f32, what: &str) { + let way = (from - to).abs() / 4.; + let tall = seen.last().unwrap().2; + for (i, (opacity, y, h)) in seen.iter().enumerate() { + assert!((opacity - 1.0).abs() < 0.01, "{what}: faded, frame {}: opacity {opacity}", i + 1); + assert!((h - tall).abs() <= 0.5, "{what}: frame {} {h} tall, growing to {tall}", i + 1); + assert!((y - to).abs() <= way, "{what}: frame {} at {y}, not yet where it goes ({to}, from {from})", i + 1); } } #[test] fn a_new_chat_and_a_session_switch_the_composer_at_once() { - let mut c = Composer::new(true); - c.watch(1); - let docked = c.last().presented.composer.y; - for selector in [NEW, BUILD] { - c.act(|app| app.click(selector)); - c.trace.clear(); - c.watch(30); - for s in &c.trace { - assert!( - (s.presented.composer.y - s.target.composer.y).abs() < 1. - && (s.presented.composer.height - s.target.composer.height).abs() < 1., - "route switch did not snap: {:?}", - c.trace - ); - } - c.at_target(); - if selector == NEW { - assert!(c.last().presented.composer.y < docked - 50.); - } else { - assert!((c.last().presented.composer.y - docked).abs() < 1.); - } - } - c.act(|app| app.fill(FIELD, "build draft")); - c.watch(40); - c.act(|app| app.click(DOCS)); - c.trace.clear(); - c.watch(30); - assert_eq!(c.last().text, ""); - c.act(|app| app.fill(FIELD, "docs draft")); - c.watch(40); - c.act(|app| app.click(BUILD)); - c.trace.clear(); - c.watch(30); - assert_eq!(c.last().text, "build draft"); - c.act(|app| { - app.click(FIELD); - select(app, 0, 5); - }); - c.trace.clear(); - c.watch(30); - c.draft("build draft", (0, 5)); - c.at_target(); -} + let mut app = mount(); + app.click("#shell-sidebar >> role:button \"Fix the build\""); + watch(&mut app, 60); + let docked = app.bounds("chat-composer").y; + assert!((app.opacity("chat-composer-dock") - 1.0).abs() < 0.01, "the composer shows, docked"); -/// A send from a new chat: the composer slides down to the dock with its -/// prompt and eases into its docked form on the way (no snap: its size -/// passes through between), as the page's one slide; with motion reduced -/// it's there at once. Any other move to or from a new chat snaps (above). -#[test] -fn a_send_from_a_new_chat_slides_the_composer_down_and_eases_it_docked() { - let snaps = |shell: &Shell| { - shell - .app - .host - .states() - .into_iter() - .find(|(n, _)| n.ends_with("::chat_composer_snap")) - .and_then(|(_, v)| v.as_f64()) - .expect("the composer's snaps") - }; - let mut shells = [false, true].map(|reduced| { - let mut shell = Shell::of("chat_fixture.luau", reduced); - assert!(shell.app.host.run_command("go_new")); - shell.frames(30); - shell.app.fill(FIELD, "please fix the build"); - shell.frames(40); - shell - }); - let start = shells[0].geometry().composer; - let snapped = snaps(&shells[0]); - for shell in &mut shells { - shell.app.key(Key::Enter); - } - shells[1].frames(2); - let end = shells[1].geometry().composer; - assert!( - end.y > start.y + 100. && end.height < start.height - 20., - "the session docks it lower, one line: {start:?} -> {end:?}" - ); - let mut trace = Vec::new(); - for _ in 0..30 { - shells[0].frames(1); - trace.push(shells[0].geometry().composer); - } - let between = trace - .iter() - .filter(|r| { - r.y > start.y + 1. - && r.y < end.y - 1. - && r.height < start.height - 1. - && r.height > end.height + 1. - }) - .count(); - assert!( - between >= 5, - "its place and its size ease together, neither snapped: {start:?} -> {end:?}, {trace:?}" - ); - for pair in trace.windows(2) { - assert!( - pair[1].y >= pair[0].y - 0.01 && pair[1].height <= pair[0].height + 0.01, - "one way, down and smaller: {trace:?}" - ); - } - let last = trace.last().unwrap(); - assert!( - (last.y - end.y).abs() < 1. && (last.height - end.height).abs() < 1., - "lands where the reduced-motion shell put it: {end:?}, {trace:?}" - ); - assert_eq!(snaps(&shells[0]), snapped, "a send snaps nothing"); -} + // To a new chat: mid-page from the first frame, never faded. + app.click("#shell-sidebar >> role:button \"New chat\""); + let seen = watch(&mut app, 60); + let hero = app.bounds("chat-composer").y; + assert!(hero < docked - 50., "it stands mid-page on a new chat ({hero} vs {docked})"); + at_once(&seen, docked, hero, "to a new chat"); -#[test] -fn composer_resize_theme_transparency_and_zoom_reflow_preserve_the_draft() { - for session in [false, true] { - let mut c = Composer::new(session); - let text = "**bold** café 你好 @mention and words to wrap\n".repeat(8); - c.act(|app| { - app.fill(FIELD, &text); - select(app, 2, 6); - }); - c.watch(40); - for (width, height, zoom, theme, frosted) in [ - (1200., 800., 1., "zeron-dark", true), - (950., 800., 1.25, "zeron-light", false), - (1400., 900., 0.9, "zeron-dark", true), - ] { - c.trace.clear(); - c.act(|app| { - app.ui.theme().set(theme).unwrap(); - app.ui.set_zoom(zoom); - // The native host reflows at window points / zoom. Model - // that logical layout here; this does not validate GPU scale. - app.ui - .scene - .borrow_mut() - .resize(width / zoom as f32, height / zoom as f32); - app.ui.refresh_text_geometry(); - let key = app - .host - .states() - .into_iter() - .find(|(key, _)| key.ends_with("::frosted")) - .unwrap() - .0; - app.host.set_state(&key, Value::Bool(frosted)).unwrap(); - }); - c.target.frames(30); - c.watch(40); - c.draft(&text, (2, 6)); - c.at_target(); - } - } -} + // And back to the session: docked from the first frame. + app.click("#shell-sidebar >> role:button \"Fix the build\""); + let seen = watch(&mut app, 60); + assert!((app.bounds("chat-composer").y - docked).abs() <= 1., "docked again"); + at_once(&seen, hero, docked, "back to the session"); -/// Typing in a long draft costs what typing in a short one past the cap -/// does: the composer's natural size can't change there (open, capped), so -/// the draft isn't measured again. Measuring walked every line twice at each -/// keystroke, past what the editor's span cache holds (a 5,000-line draft: -/// 10,039 script calls and 47 ms a key; 30,000 lines: 294 ms). -#[test] -fn typing_in_a_long_draft_costs_what_a_short_one_does() { - let per_key = |lines: usize| { - let mut shell = Shell::new(false); - shell.app.click(BUILD); - shell.frames(60); - let text: String = (0..lines) - .map(|i| format!("line {i} with some words that go on for a while here\n")) - .collect(); - shell.app.fill(FIELD, &text); - shell.frames(60); - let capped = shell.geometry().composer.height; - let before = shell.app.calls(); - for _ in 0..20 { - shell.app.ui.dispatch(InputEvent::Text("x".into())); - shell.app.settle(); - shell.frames(3); - } - let calls = (shell.app.calls() - before) / 20; - let field = shell.app.find(FIELD).unwrap(); - let editor = shell.app.ui.editor_within(field).unwrap(); - assert!( - editor - .editor - .borrow() - .text() - .ends_with("xxxxxxxxxxxxxxxxxxxx") - ); - assert!( - (shell.geometry().composer.height - capped).abs() < 0.5, - "typing past the cap must not resize the composer" - ); - calls - }; - let (short, long) = (per_key(20), per_key(5000)); + // Growing in the same chat (lines typed) still eases: seen on its way. + std::thread::sleep(Duration::from_millis(500)); + let short = app.bounds("chat-composer").height; + app.fill("#chat-composer >> role:textbox", "one\ntwo\nthree\nfour"); + let seen = watch(&mut app, 40); + let tall = seen.last().unwrap().2; + assert!(tall > short + 20., "the lines opened it out ({short} → {tall})"); assert!( - long <= short && long <= 20, - "{long} script calls a key in 5,000 lines, {short} in 20" + seen.iter().any(|(_, _, h)| *h > short + 1. && *h < tall - 1.), + "it grew at once, not eased ({short} → {tall}): {:?}", + seen.iter().map(|s| s.2).collect::>() ); } @@ -719,15 +95,12 @@ fn typing_in_a_long_draft_costs_what_a_short_one_does() { #[test] fn the_panel_s_content_and_the_page_only_fade_as_they_come_and_go() { const FLOOR: f32 = 0.3; - let mut app = Shell::new(false).app; + let mut app = mount(); app.click("#shell-sidebar >> role:button \"Fix the build\""); - app.frames(20); + watch(&mut app, 20); let warnings = |app: &mut TestApp| -> Vec { app.ui.prepare_frame(); - zgx::inspect::layout_warnings(&app.ui) - .iter() - .map(|w| w.to_string()) - .collect() + zgx::inspect::layout_warnings(&app.ui).iter().map(|w| w.to_string()).collect() }; // (Where its right edge is painted: it stands at its column's side by // the window's edge, the panel's content at the window's right, the @@ -736,11 +109,7 @@ fn the_panel_s_content_and_the_page_only_fade_as_they_come_and_go() { let node = app.find(&format!("#{id}")).expect("it"); app.ui.prepare_frame(); let b = app.ui.scene.borrow().transformed_bounds(node); - if id == "shell-page-room" { - b.x - } else { - b.x + b.width - } + if id == "shell-page-room" { b.x } else { b.x + b.width } }; // (What the fixture has before the panel ever opens.) let before = warnings(&mut app); @@ -749,68 +118,31 @@ fn the_panel_s_content_and_the_page_only_fade_as_they_come_and_go() { let seen: Vec<(f32, f32, bool)> = (0..30) .map(|_| { app.frames(1); + std::thread::sleep(Duration::from_millis(12)); // (Whether its column shows: the panel under 2 points shows none.) let shows = id == "shell-page-room" || app.opacity("shell-files") > 0.; (app.opacity(id), edge(&mut app, id), shows) }) .collect(); - let (lo, hi) = seen - .iter() - .filter(|s| s.2) - .fold((f32::MAX, f32::MIN), |(lo, hi), s| { - (lo.min(s.1), hi.max(s.1)) - }); + let (lo, hi) = seen.iter().filter(|s| s.2).fold((f32::MAX, f32::MIN), |(lo, hi), s| (lo.min(s.1), hi.max(s.1))); let drift = hi - lo; - assert!( - drift <= 0.011, - "{what}: {id} moved {drift} points: {seen:?}" - ); + assert!(drift <= 0.011, "{what}: {id} moved {drift} points: {seen:?}"); let between = seen.iter().filter(|s| s.0 > 0.02 && s.0 < 0.98).count(); - let jump = seen - .windows(2) - .map(|w| (w[1].0 - w[0].0).abs()) - .fold(0., f32::max); + let jump = seen.windows(2).map(|w| (w[1].0 - w[0].0).abs()).fold(0., f32::max); assert!(between >= 2 && jump <= 0.7, "{what}: {id} at {seen:?}"); - let showing = seen - .iter() - .filter(|s| (s.0 - to).abs() > 0.01) - .map(|s| s.0) - .fold(1., f32::min); - assert!( - showing >= FLOOR - 0.01, - "{what}: {id} fades only to the floor as it shows: {seen:?}" - ); - assert!( - (seen.last().unwrap().0 - to).abs() < 0.01, - "{what}: {id} ends at {seen:?}" - ); + let showing = seen.iter().filter(|s| (s.0 - to).abs() > 0.01).map(|s| s.0).fold(1., f32::min); + assert!(showing >= FLOOR - 0.01, "{what}: {id} fades only to the floor as it shows: {seen:?}"); + assert!((seen.last().unwrap().0 - to).abs() < 0.01, "{what}: {id} ends at {seen:?}"); // At rest, nothing it holds is laid out in a box that shows none of // it (`zgx inspect errors` names nothing new). app.frames(30); - assert_eq!( - warnings(&mut app), - before, - "{what}: settled, the layout's warnings" - ); + assert_eq!(warnings(&mut app), before, "{what}: settled, the layout's warnings"); }; - fade( - "#titlebar >> role:button \"Panel\"", - "shell-files-content", - 1., - "opening", - ); + fade("#titlebar >> role:button \"Panel\"", "shell-files-content", 1., "opening"); fade("#panel-expand", "shell-page-room", 0., "expanding"); fade("#panel-expand", "shell-page-room", 1., "collapsing"); - fade( - "#titlebar >> role:button \"Panel\"", - "shell-files-content", - FLOOR, - "closing", - ); - assert!( - app.opacity("shell-files") < 0.01, - "closed, the panel shows none of it" - ); + fade("#titlebar >> role:button \"Panel\"", "shell-files-content", FLOOR, "closing"); + assert!(app.opacity("shell-files") < 0.01, "closed, the panel shows none of it"); } /// The sidebar folds and comes back as the panel opens and closes: its @@ -823,9 +155,9 @@ fn the_panel_s_content_and_the_page_only_fade_as_they_come_and_go() { #[test] fn the_sidebar_s_content_stands_still_and_only_fades_as_it_folds() { const FLOOR: f32 = 0.3; - let mut app = Shell::new(false).app; + let mut app = mount(); app.click("#shell-sidebar >> role:button \"Fix the build\""); - app.frames(20); + watch(&mut app, 20); let at = |app: &mut TestApp, sel: &str| -> Option<(f32, f32)> { let node = app.find(sel)?; app.ui.prepare_frame(); @@ -837,41 +169,22 @@ fn the_sidebar_s_content_stands_still_and_only_fades_as_it_folds() { let mut rows = Vec::new(); for _ in 0..24 { app.frames(1); + std::thread::sleep(Duration::from_millis(12)); let side = at(&mut app, "#shell-sidebar").expect("its column"); let page = at(&mut app, "#shell-page-room").expect("the page"); let row = at(&mut app, "#shell-sidebar >> role:button \"Fix the build\""); - let fade = if app.find("#sidebar-content").is_some() { - app.opacity("sidebar-content") - } else { - 1. - }; + let fade = if app.find("#sidebar-content").is_some() { app.opacity("sidebar-content") } else { 1. }; // (Its card more than the window's gap wide: the gap is whole.) if side.1 > 10. { rows.push(row.expect("its row").0); - assert!( - (page.0 - (side.0 + side.1) - 4.).abs() < 0.05, - "{what}: the gap beside it, {}", - page.0 - side.0 - side.1 - ); - assert!( - fade >= FLOOR - 0.01, - "{what}: it fades only to the floor as it shows: {fade}" - ); + assert!((page.0 - (side.0 + side.1) - 4.).abs() < 0.05, "{what}: the gap beside it, {}", page.0 - side.0 - side.1); + assert!(fade >= FLOOR - 0.01, "{what}: it fades only to the floor as it shows: {fade}"); } } - let (lo, hi) = rows - .iter() - .fold((f32::MAX, f32::MIN), |(lo, hi), x| (lo.min(*x), hi.max(*x))); - assert!( - rows.len() >= 4 && hi - lo <= 0.011, - "{what}: its row stands at {lo}..{hi} over {} frames", - rows.len() - ); + let (lo, hi) = rows.iter().fold((f32::MAX, f32::MIN), |(lo, hi), x| (lo.min(*x), hi.max(*x))); + assert!(rows.len() >= 4 && hi - lo <= 0.011, "{what}: its row stands at {lo}..{hi} over {} frames", rows.len()); let end = if what == "folding" { FLOOR } else { 1. }; - assert!( - (app.opacity("sidebar-content") - end).abs() < 0.01, - "{what}: it ends faded to {end}" - ); + assert!((app.opacity("sidebar-content") - end).abs() < 0.01, "{what}: it ends faded to {end}"); app.frames(30); } } 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 f446abc04..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,21 +204,11 @@ struct RowWith { working_side_chats: u32, known_project: bool, agent_icon: Option, - /// The last turn its agent finished, as its session row marks it - /// (`lastCompletedTurn`): what a "finished" chime rings on moving. - turn: Option, -} - -/// A session row's finished turn, and whether the row is fresh: written in -/// the last `SESSION_STALE_MS`, so a turn's end just published rather than -/// an old row a sync or reconnect brings back (the shipping app's chime -/// rule, crates/engine/src/desktop_notify.rs `SessionNotificationState`). -fn finished_turn(session: Option<&Session>, at: chrono::DateTime) -> (Option, bool) { - let Some(s) = session else { - return (None, false); - }; - let fresh = at.signed_duration_since(s.updated_at).num_milliseconds() <= zeron_proto::view::SESSION_STALE_MS; - (s.last_completed_turn.clone(), fresh) + /// 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. @@ -567,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 }); @@ -624,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, @@ -634,9 +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), - turn: None, + 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; @@ -654,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)), @@ -662,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 }); } @@ -697,7 +727,9 @@ fn top_row( working_side_chats, known_project: project.is_some(), agent_icon: harness.and_then(crate::agents::registry_icon), - turn: finished_turn(session, at).0, + 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(); @@ -738,17 +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))), - // The last turn its agent finished (an opaque mark), and whether its - // session row was fresh when that was read: the chimes ring - // "finished" on this moving, never on the status alone ("done" is - // only unseen activity, which your own send makes too). - ("turn", with.turn.as_deref().map_or(Value::Nil, Value::from)), - ("turn_fresh", Value::from(finished_turn(session, at).1)), + // 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()); } @@ -1036,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) { @@ -1093,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(); @@ -1106,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 @@ -1437,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()); } @@ -1795,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). @@ -1812,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(), } } @@ -1903,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)> { @@ -2481,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(); @@ -2505,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"); @@ -2539,33 +3019,6 @@ mod tests { assert!(!s.rows.borrow().contains_key("b")); } - /// A chat's finished turn (what the "done" chime rings on, issue #1437): - /// its session's `lastCompletedTurn`, published as it moves, and whether - /// the row saying so is fresh. - #[test] - fn a_chat_s_finished_turn_is_published_as_it_moves() { - let now = now_seconds(); - let s = with_children("2099-01-01T00:00:00Z"); - s.heard.set(true); - let row = |turn: &str, ago: i64| { - json!([{ "chatId": "p", "deviceId": "d", "status": "idle", "updatedAt": iso(now - ago), "lastCompletedTurn": turn }]) - }; - s.set_live(row("t1", 1)); - s.publish(); - let p = s.value(); - assert_eq!(chat(&p, "p").field("turn").map(crate::auth::text).as_deref(), Some("t1")); - assert!(chat(&p, "p").field("turn_fresh").is_some_and(zgx_script::Value::truthy)); - let before = s.published.get(); - s.heard(Slot::Live, row("t2", 0)); - assert_eq!(s.published.get(), before + 1, "a finish is shown at once"); - assert_eq!(chat(&s.value(), "p").field("turn").map(crate::auth::text).as_deref(), Some("t2")); - // An end published long ago (a sync, a reconnect): not fresh. - s.heard(Slot::Live, row("t3", 600)); - let p = s.value(); - assert_eq!(chat(&p, "p").field("turn").map(crate::auth::text).as_deref(), Some("t3")); - assert!(!chat(&p, "p").field("turn_fresh").is_some_and(zgx_script::Value::truthy)); - } - #[test] fn an_update_that_changes_nothing_publishes_nothing() { let s = with_children("2099-01-01T00:00:00Z"); @@ -3141,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/src/split_tests.rs b/apps/zgx/src/split_tests.rs index 7c878059c..ee2287518 100644 --- a/apps/zgx/src/split_tests.rs +++ b/apps/zgx/src/split_tests.rs @@ -25,10 +25,7 @@ const W: f32 = 1400.; const H: f32 = 860.; fn ui() -> PathBuf { - // Compare an archived interface with the same host and frame clock. - std::env::var_os("ZERON_ZGX_TEST_UI") - .map(PathBuf::from) - .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui")) + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ui") } fn scratch(name: &str) -> PathBuf { @@ -1639,9 +1636,7 @@ fn drag_script_of(fixture: &Path, panes_n: usize) -> u64 { app.frames(60); let calls = app.calls(); drag_sidebar(&mut app, 120., |_| ()); - let per_frame = (app.calls() - calls) / 20; - eprintln!("RESIZE-CALLS {panes_n} panes: {per_frame}"); - per_frame + (app.calls() - calls) / 20 } /// The sidebar's edge dragged, the sidebar's list isn't built again, and diff --git a/apps/zgx/src/terminal.rs b/apps/zgx/src/terminal.rs index 1027bc641..1b0ee3f6e 100644 --- a/apps/zgx/src/terminal.rs +++ b/apps/zgx/src/terminal.rs @@ -161,15 +161,13 @@ async fn start(arg: Value) -> Result { let dir = claims_dir(); std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; let claim = dir.join(format!("{}-{}", std::process::id(), operation_nonce())); - // (In base64: no quote inside it for a shell to read as its own, `runnable`.) - let open = base64::engine::general_purpose::STANDARD.encode(Json::Object(params).to_string()); - let line = runnable(format!( + let line = format!( "{} terminal-bridge --port {port} --data {} --open {} --claim {}", quote(&exe.to_string_lossy()), quote(&data.to_string_lossy()), - quote(&open), + quote(&Json::Object(params).to_string()), quote(&claim.to_string_lossy()), - )); + ); note_issued(&line); Ok(Value::record([("command", Value::from(line.as_str())), ("claim", Value::from(claim.to_string_lossy().as_ref()))])) } @@ -297,42 +295,10 @@ fn command(arg: Value, device: Option) -> Result { if let Some(device) = device { line.push_str(&format!(" --device {}", quote(&device))); } - let line = runnable(line); note_issued(&line); Ok(Value::from(line.as_str())) } -/// The line zgx's terminal runs for the bridge's `line`. The Windows beta is -/// a GUI program (main.rs `windows_subsystem`), and Windows attaches none to -/// the pseudoconsole it's started on: the bridge would have no console, its -/// shell opened and nothing shown. So cmd.exe, a console program, is started -/// on it, waits for the bridge, and the bridge attaches to its console -/// (`attach_console`). Each word is in quotes with none inside (the open -/// parameters are base64), so cmd takes nothing in them for its own syntax. -#[cfg(all(windows, feature = "beta"))] -fn runnable(line: String) -> String { - let cmd = std::env::var("ComSpec").unwrap_or_else(|_| r"C:\Windows\System32\cmd.exe".into()); - format!("{} /d /s /c \"{line}\"", quote(&cmd)) -} -#[cfg(not(all(windows, feature = "beta")))] -fn runnable(line: String) -> String { - line -} - -/// The console of the cmd.exe that started this bridge (`runnable`), when -/// it has none of its own (a GUI program's). -#[cfg(all(windows, feature = "beta"))] -fn attach_console() { - use windows_sys::Win32::System::Console::{ATTACH_PARENT_PROCESS, AttachConsole, GetConsoleMode, GetStdHandle, STD_INPUT_HANDLE}; - // SAFETY: the process's own standard handle, and a mode for it to fill. - unsafe { - let mut mode = 0; - if GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mut mode) == 0 { - AttachConsole(ATTACH_PARENT_PROCESS); - } - } -} - // --------------------------------------------------------------------------- // The bridge: its own process, run by zgx's terminal. @@ -361,14 +327,10 @@ fn parse(args: &[String]) -> Result { "--port" => port = value.parse().ok(), "--data" => data = Some(PathBuf::from(value)), "--terminal" => terminal = Some(value.clone()), - // (As JSON, or JSON in base64: `start`'s.) - "--open" => { - let decoded = base64::engine::general_purpose::STANDARD.decode(value).ok().and_then(|bytes| String::from_utf8(bytes).ok()); - match serde_json::from_str::(decoded.as_deref().unwrap_or(value)) { - Ok(Json::Object(params)) => open = Some(params), - _ => return Err("--open takes OpenTerminal's parameters".into()), - } - } + "--open" => match serde_json::from_str::(value) { + Ok(Json::Object(params)) => open = Some(params), + _ => return Err("--open takes OpenTerminal's parameters".into()), + }, "--claim" => claim = Some(PathBuf::from(value)), "--device" => device = Some(value.clone()), other => return Err(format!("unknown flag {other}")), @@ -415,30 +377,7 @@ fn window_size() -> Option<(u16, u16)> { (libc::ioctl(0, libc::TIOCGWINSZ, &mut size) == 0 && size.ws_col > 0).then_some((size.ws_col, size.ws_row)) } } -/// The visible window of the console this bridge writes to: the -/// pseudoconsole zgx's terminal sized (cmd.exe's, `attach_console`), which -/// follows the tab as zgx resizes it. Windows says nothing as it changes: -/// the bridge looks on each of its turns (`bridge`), as cheap as a call. -#[cfg(windows)] -fn window_size() -> Option<(u16, u16)> { - use windows_sys::Win32::System::Console::{CONSOLE_SCREEN_BUFFER_INFO, GetConsoleScreenBufferInfo, GetStdHandle, STD_OUTPUT_HANDLE}; - // SAFETY: the process's own standard handle, and a struct for it to fill. - let info = unsafe { - let mut info: CONSOLE_SCREEN_BUFFER_INFO = std::mem::zeroed(); - (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &mut info) != 0).then_some(info) - }?; - console_window_size(info.srWindow.Left, info.srWindow.Top, info.srWindow.Right, info.srWindow.Bottom) -} - -/// Columns and rows of a console window's inclusive rectangle, if it has any. -#[cfg(any(windows, test))] -fn console_window_size(left: i16, top: i16, right: i16, bottom: i16) -> Option<(u16, u16)> { - let cols = i32::from(right) - i32::from(left) + 1; - let rows = i32::from(bottom) - i32::from(top) + 1; - (cols > 0 && rows > 0).then(|| (cols.min(i32::from(u16::MAX)) as u16, rows.min(i32::from(u16::MAX)) as u16)) -} - -#[cfg(not(any(unix, windows)))] +#[cfg(not(unix))] fn window_size() -> Option<(u16, u16)> { None } @@ -543,78 +482,6 @@ impl Drop for Raw { } } -/// The console's modes and code pages as found, put back as the bridge ends. -#[cfg(windows)] -struct Raw(Option); -#[cfg(windows)] -struct RawWas { - input: u32, - output: u32, - input_cp: u32, - output_cp: u32, -} -#[cfg(windows)] -impl Raw { - /// Its console in raw mode, as `cfmakeraw` puts a Unix one: a cooked - /// console echoed each line before the shell at the other end did (every - /// line shown twice), ended it with a carriage return and a line feed - /// (PowerShell read the second as a continued line, `>>`), and took - /// Ctrl+C as a signal to every process on it, cmd.exe and the bridge - /// included, leaving the tab with nothing behind it. Raw, what's typed goes - /// on as it is (Ctrl+C as its byte, which the shell's own console turns - /// into its interrupt), in the terminal's sequences; what the shell wrote - /// comes out as it is, in UTF-8. - fn new() -> Self { - use windows_sys::Win32::System::Console::{ - DISABLE_NEWLINE_AUTO_RETURN, ENABLE_VIRTUAL_TERMINAL_PROCESSING, GetConsoleCP, GetConsoleMode, GetConsoleOutputCP, GetStdHandle, - STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, SetConsoleCP, SetConsoleMode, SetConsoleOutputCP, - }; - // SAFETY: the process's own standard handles, modes read from them - // and changed; nothing if either isn't a console. - unsafe { - let (stdin, stdout) = (GetStdHandle(STD_INPUT_HANDLE), GetStdHandle(STD_OUTPUT_HANDLE)); - let (mut input, mut output) = (0, 0); - if GetConsoleMode(stdin, &mut input) == 0 || GetConsoleMode(stdout, &mut output) == 0 { - return Raw(None); - } - let was = RawWas { input, output, input_cp: GetConsoleCP(), output_cp: GetConsoleOutputCP() }; - SetConsoleMode(stdin, raw_input_mode(input)); - SetConsoleMode(stdout, output | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN); - const UTF8: u32 = 65001; - SetConsoleCP(UTF8); - SetConsoleOutputCP(UTF8); - Raw(Some(was)) - } - } -} -#[cfg(windows)] -impl Drop for Raw { - fn drop(&mut self) { - use windows_sys::Win32::System::Console::{GetStdHandle, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, SetConsoleCP, SetConsoleMode, SetConsoleOutputCP}; - if let Some(was) = &self.0 { - // SAFETY: restores what `new` read from the same handles. - unsafe { - SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), was.input); - SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), was.output); - SetConsoleCP(was.input_cp); - SetConsoleOutputCP(was.output_cp); - } - } - } -} - -/// A console input mode made raw: no echo, no lines, no Ctrl+C signal; -/// keys as the terminal's sequences. (Its other flags are left as found.) -#[cfg(any(windows, test))] -fn raw_input_mode(mode: u32) -> u32 { - // (windows-sys's values, here so the rule is tested everywhere.) - const ENABLE_PROCESSED_INPUT: u32 = 0x0001; - const ENABLE_LINE_INPUT: u32 = 0x0002; - const ENABLE_ECHO_INPUT: u32 = 0x0004; - const ENABLE_VIRTUAL_TERMINAL_INPUT: u32 = 0x0200; - (mode & !(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT)) | ENABLE_VIRTUAL_TERMINAL_INPUT -} - /// Sends a call. On a socket that doesn't block, what doesn't go at once /// waits in tungstenite's buffer (`rpc::would_block`) for the socket to /// have room. @@ -665,8 +532,6 @@ fn resizes() -> Option { /// Runs the bridge; returns the process's exit code. pub fn bridge(args: &[String]) -> i32 { - #[cfg(all(windows, feature = "beta"))] - attach_console(); let args = match parse(args) { Ok(args) => args, Err(why) => { @@ -685,7 +550,7 @@ pub fn bridge(args: &[String]) -> i32 { }; // (A device given is another's: the app only passes it then.) let mut device = args.device.clone(); - #[cfg(any(unix, windows))] + #[cfg(unix)] let _raw = Raw::new(); let mut size = None; // A new one: opened here, at the size zgx gave this pseudoterminal, so @@ -761,7 +626,7 @@ pub fn bridge(args: &[String]) -> i32 { let mut look_at_size = true; loop { // Its size, told as it changes (zgx resizes its pseudoterminal, and - // says so with SIGWINCH; on Windows, looked at each turn). + // says so with SIGWINCH). let now = if look_at_size { window_size() } else { size }; look_at_size = cfg!(not(unix)); if now.is_some() && now != size { @@ -909,26 +774,6 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } - /// A Windows console made raw: no echo, no lines, no Ctrl+C signal, keys - /// as sequences, and whatever else it had kept. - #[test] - fn a_raw_console_neither_echoes_nor_cooks_nor_signals() { - // A cooked console's defaults: processed, line, echo, insert, quick - // edit, extended flags. - let cooked = 0x0001 | 0x0002 | 0x0004 | 0x0020 | 0x0040 | 0x0080; - assert_eq!(super::raw_input_mode(cooked), 0x0020 | 0x0040 | 0x0080 | 0x0200); - assert_eq!(super::raw_input_mode(0x0200), 0x0200); - } - - /// A console's size is its visible window's: inclusive edges. - #[test] - fn a_consoles_size_is_its_windows() { - assert_eq!(super::console_window_size(0, 0, 79, 23), Some((80, 24))); - // Scrolled down its buffer: still the window's size. - assert_eq!(super::console_window_size(0, 900, 39, 947), Some((40, 48))); - assert_eq!(super::console_window_size(5, 5, 4, 10), None); - } - #[test] fn an_older_engine_says_to_update() { assert_eq!(said("unknown method: TerminalStatus".into()), "Update Zeron to use terminals here."); diff --git a/apps/zgx/src/tool_details.rs b/apps/zgx/src/tool_details.rs index c18978ab9..7d15c92dd 100644 --- a/apps/zgx/src/tool_details.rs +++ b/apps/zgx/src/tool_details.rs @@ -278,165 +278,13 @@ pub fn tool_fields(part: &Json) -> Vec<(&'static str, Value)> { { fields.push(("diff", diff)); } - if let Some(pictures) = pictures_value(part) { - fields.push(("pictures", pictures)); - } fields } -/// Pictures a tool row opens to, at most (the shipping chip's -/// `MAX_IMAGES_PER_TOOL`). -const MAX_PICTURES: usize = 4; - -/// The pictures a tool's result carried (`previewPaths`: the engine's copies -/// in the profile's uploads, on the chat's host), each `{ path, name }`: its -/// name the file the call named (a Read's) when it carried one, else the -/// copy's own. Nil when it carried none: a path a tool only named is never -/// read from here, and neither is any path that isn't such a copy -/// (`stored_tool_picture`). -fn pictures_value(part: &Json) -> Option { - let data_dir = engine::data_dir(); - pictures_in(part, data_dir.as_deref()) -} - -fn pictures_in(part: &Json, data_dir: Option<&std::path::Path>) -> Option { - let paths: Vec<&str> = part["previewPaths"] - .as_array()? - .iter() - .filter_map(Json::as_str) - .filter(|p| stored_tool_picture(p, data_dir)) - .take(MAX_PICTURES) - .collect(); - if paths.is_empty() { - return None; - } - // (The call's file names its one picture; several are each their own.) - let named = (paths.len() == 1) - .then(|| part["call"]["path"].as_str().and_then(file_name)) - .flatten(); - let list: Vec = paths - .iter() - .map(|path| { - let name = named.or_else(|| file_name(path)).unwrap_or_default(); - Value::record([("path", Value::from(*path)), ("name", Value::from(name))]) - }) - .collect(); - Some(Value::List(list.into())) -} - -/// Whether `path` is a copy the engine stored of a tool result's picture: -/// named as the engine names them (`tool---image.`, a -/// raster it sniffed). A local chat's pictures are read straight from disk, -/// so one that is here must also be a file within the engine's data (its -/// profiles' uploads) and no larger than a picture can be: a doc naming -/// `/dev/zero`, a picture elsewhere on this Mac, or a huge file is never -/// opened. One on another device is read from there, through its engine's -/// own jail. -fn stored_tool_picture(path: &str, data_dir: Option<&std::path::Path>) -> bool { - let Some(name) = file_name(path) else { - return false; - }; - let Some(stem) = ["png", "jpg", "webp", "gif"] - .iter() - .find_map(|ext| name.strip_suffix(&format!("-image.{ext}"))) - else { - return false; - }; - let mut parts = stem.splitn(3, '-'); - let hex = |s: Option<&str>, len: usize| { - s.is_some_and(|s| s.len() == len && s.bytes().all(|b| b.is_ascii_hexdigit())) - }; - if parts.next() != Some("tool") || !hex(parts.next(), 16) || !hex(parts.next(), 64) { - return false; - } - let Ok(meta) = std::fs::symlink_metadata(path) else { - // (Not on this device: another's, read through its engine.) - return true; - }; - let within = || -> Option { - let file = std::fs::canonicalize(path).ok()?; - let data = std::fs::canonicalize(data_dir?).ok()?; - Some(file.starts_with(data) && file.components().any(|c| c.as_os_str() == "uploads")) - }; - meta.is_file() - && meta.len() <= zeron_proto::IMAGE_MAX_BYTES as u64 - && within().unwrap_or(false) -} - -fn file_name(path: &str) -> Option<&str> { - path.rsplit(['/', '\\']).find(|s| !s.is_empty()) -} - #[cfg(test)] mod tests { use super::*; - const STORED: &str = "/home/me/.zeron/uploads/tool-0123456789abcdef-0000000000000000000000000000000000000000000000000000000000000001-image.png"; - - #[test] - fn a_result_s_stored_pictures_are_the_row_s_named_as_the_call_s_file() { - let part = json!({ - "kind": "tool", "id": "t1", "isError": false, - "call": { "kind": "readFile", "path": "/home/me/scratch/shot.png" }, - "previewPaths": [STORED], - }); - let fields = tool_fields(&part); - let (_, pictures) = fields.iter().find(|(k, _)| *k == "pictures").expect("pictures"); - let Value::List(list) = pictures else { panic!("not a list: {pictures:?}") }; - assert_eq!(list.len(), 1); - assert_eq!(list[0].field("path").map(crate::auth::text).as_deref(), Some(STORED)); - assert_eq!(list[0].field("name").map(crate::auth::text).as_deref(), Some("shot.png")); - // Several: each is its own, not all the call's one file. - let two = STORED.replace("01-image.png", "02-image.png"); - let several = json!({ - "kind": "tool", "id": "t3", - "call": { "kind": "readFile", "path": "/home/me/scratch/shot.png" }, - "previewPaths": [STORED, two], - }); - let Some(Value::List(list)) = pictures_in(&several, None) else { panic!("no pictures") }; - let names: Vec<_> = list.iter().map(|p| p.field("name").map(crate::auth::text).unwrap_or_default()).collect(); - assert!(names.iter().all(|n| n.starts_with("tool-") && n.ends_with("-image.png")), "{names:?}"); - // None stored (an older doc, a path only named): no pictures. - let bare = json!({ "kind": "tool", "id": "t2", "call": { "kind": "readFile", "path": "/x/a.png" } }); - assert!(!tool_fields(&bare).iter().any(|(k, _)| *k == "pictures")); - } - - /// Only the engine's own copies are a row's pictures: a doc naming a - /// device file, a picture elsewhere on this computer, or a huge file - /// under the engine's name is never read; a copy inside the engine's - /// uploads is. - #[test] - fn only_the_engines_stored_copies_are_pictures() { - let data = tempfile::tempdir().unwrap(); - let uploads = data.path().join("orgs").join("o").join("uploads"); - std::fs::create_dir_all(&uploads).unwrap(); - let name = "tool-0123456789abcdef-0000000000000000000000000000000000000000000000000000000000000001-image.png"; - let inside = uploads.join(name); - std::fs::write(&inside, b"\x89PNG\r\n\x1a\n").unwrap(); - let elsewhere = tempfile::tempdir().unwrap(); - let outside = elsewhere.path().join(name); - std::fs::write(&outside, b"\x89PNG\r\n\x1a\n").unwrap(); - let big = uploads.join(name.replace("01-image", "02-image")); - std::fs::File::create(&big).unwrap().set_len(zeron_proto::IMAGE_MAX_BYTES as u64 + 1).unwrap(); - let dir = Some(data.path()); - let ok = |p: &std::path::Path| stored_tool_picture(p.to_str().unwrap(), dir); - assert!(ok(&inside)); - assert!(!ok(&outside), "a copy outside the engine's data"); - assert!(!ok(&big), "larger than a picture can be"); - assert!(!stored_tool_picture(inside.to_str().unwrap(), None), "no data dir to check against"); - #[cfg(unix)] - { - let link = uploads.join(name.replace("01-image", "03-image")); - std::os::unix::fs::symlink(&outside, &link).unwrap(); - assert!(!ok(&link), "a link out of uploads"); - } - for path in ["/dev/zero", "/etc/hosts", "/home/me/Pictures/private.png", "/x/uploads/ab12-image.png", "/x/uploads/tool-0123-01-image.png", ""] { - assert!(!stored_tool_picture(path, dir), "{path}"); - } - // Another device's copy (no such file here) is read there, by its engine. - assert!(stored_tool_picture(&format!("/Users/other/.zeron/uploads/{name}"), dir)); - } - #[test] fn a_diff_is_hunks_with_three_lines_of_context_numbered_from_one() { let old = (1..=20).map(|i| format!("line {i}\n")).collect::(); diff --git a/apps/zgx/ui/account/agents.luau b/apps/zgx/ui/account/agents.luau index a5a218401..102e9ef41 100644 --- a/apps/zgx/ui/account/agents.luau +++ b/apps/zgx/ui/account/agents.luau @@ -8,8 +8,6 @@ -- list = { Agent } } -- agents.load() looks again (and the first time) -- agents.sign_in(id) a new account for that agent, through the browser --- agents.sign_in_to(id, provider) the same, for one model provider of --- an agent that keeps a login per provider -- agents.cancel(id) gives up that sign-in -- agents.dismiss(id) forgets one that failed -- agents.set_enabled(id, on) @@ -28,16 +26,6 @@ -- week_resets? }, `session` and `week` its plan's two windows, 0 to 1, and -- when each starts over (seconds since the epoch). -- --- An agent that keeps a login per model provider (Pi, OpenCode, Hermes) --- also has `groups`: its accounts by provider, `{ id, name, accounts, --- login?, key?, hint?, read_only }` (`login`, who Zeron signs you in to for --- it; `key`, who issues the API key Zeron adds; neither: `hint` says how the --- agent does), and its sign-in's provider, `login_for`. Its accounts each --- say their `provider`. --- agents.add_key(id, provider, key) an API key for one such provider on --- this Mac's agent (a promise; rejects with why not — --- "remote" while another device is shown) --- -- A Codex account also has what it banks: `banked` (how many resets) and -- `resets`, soonest to expire first ({ id, title, scope?, expires? }, -- `expires` in seconds since the epoch), and its last reset's word, @@ -64,17 +52,6 @@ export type Account = { banked: number?, resets: { Reset }?, reset: ResetWord?, - switchable: boolean?, - provider: string?, -} -export type Group = { - id: string, - name: string, - accounts: { Account }, - login: string?, - key: string?, - hint: string?, - read_only: boolean?, } export type Agent = { id: string, @@ -87,8 +64,6 @@ export type Agent = { installing: boolean, login: string, accounts: { Account }, - groups: { Group }?, - login_for: string?, } export type Now = { state: string, list: { Agent }, target: string? } @@ -110,38 +85,6 @@ function M.sign_in(id: string) ui.call("agents.sign_in", id):after(function() end, quietly()) end --- A new account for one model provider of an agent that keeps a login per --- provider (one of its `groups`). -function M.sign_in_to(id: string, provider: string) - ui.call("agents.sign_in", { id = id, provider = provider }):after(function() end, quietly()) -end - --- An API key for one provider of an agent that keeps a login per provider, --- on this Mac (a key is never sent to another device): a promise. -function M.add_key(id: string, provider: string, key: string, name: string?): any - return ui.call("agents.add_key", { id = id, provider = provider, key = key, name = name or "" }) -end - --- An API key's name (empty: its default, " ···"): a promise. -function M.rename(id: string, account: string, name: string): any - return ui.call("agents.rename", { id = id, account = account, name = name }) -end - --- A key asked for from elsewhere (the model picker's Account row): which --- agent's which provider ("pi:openrouter"), for its card to open its key --- field; "" once it has. -M.key_request = ui.state("agents_key_request", "") - --- Again, the sign-in that failed: to the provider it was for, if it was for --- one. -function M.retry(a: Agent) - if a.login_for then - M.sign_in_to(a.id, a.login_for) - else - M.sign_in(a.id) - end -end - function M.cancel(id: string) ui.invoke("agents.cancel", id) end @@ -181,10 +124,8 @@ function M.set_enabled(id: string, on: boolean) ui.call("agents.set_enabled", { id = id, enabled = on }):after(function() end, quietly()) end --- That account in use: a promise, rejecting with why not (the engine's --- words, or "offline"). -function M.switch(id: string, account: string): any - return ui.call("agents.switch", { id = id, account = account }) +function M.switch(id: string, account: string) + ui.call("agents.switch", { id = id, account = account }):after(function() end, quietly()) end -- Each device's accounts for its composers, by device id ("": this one's), @@ -192,11 +133,6 @@ end -- plan, active, can_switch, error?, windows = { { label, used } } }`. M.on_device = ui.shared("agents.on_device", {} :: { [string]: { [string]: { any } } }) --- Each per-provider agent's providers Zeron adds to, by provider: `{ --- [harness] = { [provider] = { name, login?, key? } } }` (who it signs you --- in to, who issues the key it adds). -M.logins = ui.shared("agents.logins", {} :: { [string]: { [string]: { name: string, login: string?, key: string? } } }) - -- Their accounts, on first sight of a device. (A host with no accounts to -- say, a fixture's: none come.) function M.watch_device(key: string) 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/account/sessions.luau b/apps/zgx/ui/account/sessions.luau index 7940c4c68..22b51e557 100644 --- a/apps/zgx/ui/account/sessions.luau +++ b/apps/zgx/ui/account/sessions.luau @@ -77,11 +77,6 @@ export type Chat = { -- woke (Unix seconds). sleep_until: number?, woke: number?, - -- The last turn its agent finished (an opaque mark, nil before its - -- first), and whether its session row was fresh as that was read: what - -- the "finished" chime rings on moving (screens/settings/chimes). - turn: string?, - turn_fresh: boolean?, } export type WaitingOn = { id: string, kind: string, label: string } -- A side chat: a chat under another (its `parent`). diff --git a/apps/zgx/ui/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/provider-anthropic.svg b/apps/zgx/ui/assets/icons/provider-anthropic.svg deleted file mode 100644 index cbfbc69cc..000000000 --- a/apps/zgx/ui/assets/icons/provider-anthropic.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/apps/zgx/ui/assets/icons/provider-deepseek.svg b/apps/zgx/ui/assets/icons/provider-deepseek.svg deleted file mode 100644 index cf8e9a8e9..000000000 --- a/apps/zgx/ui/assets/icons/provider-deepseek.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/apps/zgx/ui/assets/icons/provider-gemini.svg b/apps/zgx/ui/assets/icons/provider-gemini.svg deleted file mode 100644 index 953e56267..000000000 --- a/apps/zgx/ui/assets/icons/provider-gemini.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/apps/zgx/ui/assets/icons/provider-openai.svg b/apps/zgx/ui/assets/icons/provider-openai.svg deleted file mode 100644 index 3f4000510..000000000 --- a/apps/zgx/ui/assets/icons/provider-openai.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/apps/zgx/ui/assets/icons/provider-openrouter.svg b/apps/zgx/ui/assets/icons/provider-openrouter.svg deleted file mode 100644 index a9c4752f3..000000000 --- a/apps/zgx/ui/assets/icons/provider-openrouter.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/apps/zgx/ui/assets/icons/provider-xai.svg b/apps/zgx/ui/assets/icons/provider-xai.svg deleted file mode 100644 index 8c5f59324..000000000 --- a/apps/zgx/ui/assets/icons/provider-xai.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file 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 16d15fda3..38abc75e9 100644 --- a/apps/zgx/ui/chat_fixture.luau +++ b/apps/zgx/ui/chat_fixture.luau @@ -92,13 +92,6 @@ do note(`switch_on:{key}:{id}:{account}`) return { after = function(_: any, done: (any) -> ()) done(nil) end } end - -- (An Account row adding one of its provider's accounts.) - stub_agents.target = function(device: string?) - note(`target:{device or ""}`) - end - stub_agents.sign_in_to = function(id: string, provider: string) - note(`sign_in_to:{id}:{provider}`) - end end local function publish(id: string, patch: { [string]: any }?) @@ -886,16 +879,6 @@ ui.command { end, } --- A settled reply the host gave no time (as Windows did before it could --- read one): its Copy shows all the same (tests/chat_in_place). -ui.command { - id = "reply_without_time", - title = "A reply with no time", - run = function() - store.messages:push { id = "notime1", role = "assistant", text = "Answered with no clock.", copy = "Answered with no clock." } - end, -} - -- Messages from other chats, as the host gives them: one from a chat that's -- listed, one from a chat that isn't, one with a long title (tests/chat_sender). ui.command { @@ -1182,17 +1165,6 @@ ui.command { seed() end, } --- A Codex session on its agent's default model (none set, so none the --- catalog names): its agent only the one its config records. -ui.command { - id = "codex_model_less", - title = "A Codex session with no model", - run = function() - META.s3 = { title = "Codex chat", project = "atlas", agent = "codex", harness = "codex", access = "full", plan = false } - TRANSCRIPTS.s3 = { { id = "z1", role = "user", text = "Hi" } } - seed() - end, -} -- A session of long messages (each folds to five lines). do local long_chat = {} @@ -1616,29 +1588,6 @@ ui.effect(function() table.sort(keys) plugin_comments:set(table.concat(keys, ",")) end) --- The draft lifted out whole and laid back (a plugin's `zeron.take_draft` --- and `put_draft`; screens/plugins keeps it between the two, as this --- does). `test_lifted` says what was lifted: its words and how many chips. -local lifted: any = nil -local lifted_said = ui.state("test_lifted", "") -ui.command { - id = "draft_lift", - title = "A plugin takes the draft", - run = function() - lifted = require("@app/screens/chat/shared").lift() - lifted_said:set(if lifted then `{lifted.text}|{#lifted.cargo}` else "nothing") - end, -} -ui.command { - id = "draft_lay", - title = "A plugin puts the draft back", - run = function() - if lifted then - require("@app/screens/chat/shared").lay(lifted) - lifted = nil - end - end, -} ui.command { id = "comment_moves", title = "The comment is carried two lines down", @@ -2097,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/chimes_fixture.luau b/apps/zgx/ui/chimes_fixture.luau deleted file mode 100644 index a9788cbae..000000000 --- a/apps/zgx/ui/chimes_fixture.luau +++ /dev/null @@ -1,7 +0,0 @@ --- The session chimes (screens/settings/chimes) with no engine: a test sets --- the `sessions` shared state as the engine would publish it, and reads what --- played from `settings_stub_calls` ("sound.play:done", …). -require("./settings_stub") -require("@app/screens/settings/chimes") - -return ui.column {} diff --git a/apps/zgx/ui/composer_fixture.luau b/apps/zgx/ui/composer_fixture.luau index df1bf47b5..351e4a07f 100644 --- a/apps/zgx/ui/composer_fixture.luau +++ b/apps/zgx/ui/composer_fixture.luau @@ -6,8 +6,6 @@ ui.theme.strict("warn") local ds = require("@ds") local Composer = require("@ds/composer") -local Cargo = require("@ds/composer_cargo") -local Ask = require("@ds/composer_ask") ds.setup() local HARNESSES: { ds.Harness } = { { id = "claude", label = "Claude", mark = "claude" } } @@ -30,9 +28,6 @@ return ui.column { harnesses = HARNESSES, models = MODELS, notice = ui.state("test_composer_notice", nil :: Composer.Notice?), - dictating = ui.state("test_composer_dictating", false), - cargo = ui.state("test_composer_cargo", {} :: { Cargo.Item }), - question = ui.state("test_composer_question", nil :: Ask.Question?), project = ui.state("test_composer_project", "atlas" :: string?), projects = PROJECTS, }, diff --git a/apps/zgx/ui/composer_metrics_fixture.luau b/apps/zgx/ui/composer_metrics_fixture.luau deleted file mode 100644 index ed634b407..000000000 --- a/apps/zgx/ui/composer_metrics_fixture.luau +++ /dev/null @@ -1,55 +0,0 @@ -local ds = require("@ds") -local Metrics = require("@ds/composer_metrics") -ds.setup() - -local value = ui.state("fixture_metrics_text", "") -local width = ui.state("fixture_metrics_width", 320) -local viewport = ui.state("fixture_metrics_viewport", 800) -local editor_key = ui.state("fixture_metrics_editor", 1) -local field = ui.ref() -local metrics = Metrics.use { - value = value, - width = function(peek: boolean?) - return if peek then width:peek() else width:get() - end, - editor = field, - keep = "fixture", -} -local result = ui.state("fixture_metrics_result", {} :: any) -local history = ui.state("fixture_metrics_history", {} :: { string }) -ui.effect(function() - local r = metrics.result:get() - result:set(r) - local h = table.clone(history:peek()) - table.insert(h, r.status) - history:set(h) -end) - -ui.command { - id = "fixture.metrics.type", - title = "Larger composer type", - run = function() - ui.theme.tokens { zeron = { composer = { text = 18, line = 28 } } } - end, -} - -return ui.div { - w = viewport, - h_full = true, - relative = true, - ui.presence(function(): string return tostring(editor_key:get()) end, function(): Node - return ui.textarea { - id = "metrics-editor", - ref = field, - value = value, - w_full = true, - h = 22, - text_size = "$zeron.composer.text", - line_height = "$zeron.composer.line", - highlight = function(line) - local a, b = string.find(line, "@[%w_]+") - return if a then { { a, b, weight = 700 } } else {} - end, - } - end), -} diff --git a/apps/zgx/ui/composer_transition_fixture.luau b/apps/zgx/ui/composer_transition_fixture.luau deleted file mode 100644 index 66009c954..000000000 --- a/apps/zgx/ui/composer_transition_fixture.luau +++ /dev/null @@ -1,42 +0,0 @@ --- Controller-only fixture: no engine, editor or unrelated animations. -local ds = require("@ds") -local Layout = require("@ds/composer_layout") -local Motion = require("@ds/composer_motion") -ds.setup() -local width = ui.state("fixture_transition_width", 660) -local height = ui.state("fixture_transition_height", 22) -local shown = ui.state("fixture_transition_shown", true) -local snap = ui.state("fixture_transition_snap", 0) -local ready = ui.state("fixture_transition_ready", true) -local revision = ui.state("fixture_transition_revision", 0) -local settlement = ui.computed(function() - return { revision = revision:get(), status = if ready:get() then "ready" else "pending" } -end) -local report = ui.state("fixture_transition_report", {} :: any) -local target = ui.computed(function() - return Layout.calculate({ - width = width:get(), - sample = { unwrapped_width = 40, wrapped_height = height:get(), line_height = 22 }, - trailing_width = 64, control_corner = 25, - header_height = 0, header_gap = 12, - expanded = false, cargo = false, asking = false, dictating = false, - multiline = height:get() > 22, - }, false) -end) -local last: any = nil -ui.command { - id = "fixture.transition.inspect", - title = "Inspect the last controller, even after unmount", - run = function() - if last then - report:set({ frame = last.frame:peek(), origin = last.origin:peek(), - destination = last.destination:peek(), progress = last.progress:peek(), active = last.active:peek(), settling = last.settling:peek() }) - end - end, -} -local Controller = ui.component(function(): Node - last = Motion.use(target, snap, settlement) - local own = last - return ui.div { w = width, h = function() return own.frame:get().height end } -end) -return ui.show(function() return shown:get() end, Controller {}) diff --git a/apps/zgx/ui/composer_transition_preview.luau b/apps/zgx/ui/composer_transition_preview.luau deleted file mode 100644 index 050df4754..000000000 --- a/apps/zgx/ui/composer_transition_preview.luau +++ /dev/null @@ -1,65 +0,0 @@ --- Real composer for visual transition checks. All input is fixture-local; --- the development host's engine/account updates cannot replace this draft. --- It leaves the build's own settings as it found them: Reduce motion is --- persisted (`motion_reduce`), so it is only changed by its button here and --- put back as the preview goes, as is the theme it shows in. -local ds = require("@ds") -local motion = require("@ds/motion") -ds.setup() -local reduce_was = motion.reduce:peek() -local theme_was = ui.theme.current:peek() -ui.theme.current:set("zeron-dark") - -local draft = ui.state("fixture_preview_draft", "short") -local focus = ui.state("fixture_preview_focus", 0) -local cargo = ui.state("fixture_preview_cargo", {} :: { any }) -local function lines(n: number): string - local out = {} - for i = 1, n do table.insert(out, `Line {i} for composer transition checks`) end - return table.concat(out, "\n") -end -local function set_draft(text: string) - ui.batch(function() - draft:set(text) - focus:set(focus:peek() + 1) - end) -end - -local Preview = ui.component(function(): Node - ui.on_cleanup(function() - if motion.reduce:peek() ~= reduce_was then - motion.reduce:set(reduce_was) - end - ui.theme.current:set(theme_was) - end) - return ui.column { - w_full = true, h_full = true, p = "$space.4", gap = "$space.4", - bg = "$background", - ui.row { - gap = "$space.2", - ds.Button { "Short", on_click = function() set_draft("short") end }, - ds.Button { "10 lines", on_click = function() set_draft(lines(10)) end }, - ds.Button { "80 lines", on_click = function() set_draft(lines(80)) end }, - ds.Button { "Header", on_click = function() - cargo:set(if #cargo:peek() == 0 then { { id = "f", kind = "file", name = "notes.md" } } else {}) - focus:set(focus:peek() + 1) - end }, - -- (scripts/composer_frames.py clicks these by place: keep the labels.) - ds.Button { "Reduce motion", on_click = function() motion.reduce:set(not motion.reduce:peek()) end }, - ds.Button { "Wrap", on_click = function() set_draft(string.rep("W", 60)) end }, - }, - ui.div { grow = 1 }, - ui.div { - w = 660, - ds.Composer { - value = draft, cargo = cargo, focus = focus, - model = ui.state("fixture_preview_model", "sonnet" :: string?), - effort = ui.state("fixture_preview_effort", "medium" :: string?), - harnesses = { { id = "claude", label = "Claude", mark = "claude" } }, - models = { { value = "sonnet", label = "Sonnet", harness = "claude" } }, - }, - }, -} -end) - -return Preview {} diff --git a/apps/zgx/ui/consent_fixture.luau b/apps/zgx/ui/consent_fixture.luau index 0c70daf87..666c0d1fd 100644 --- a/apps/zgx/ui/consent_fixture.luau +++ b/apps/zgx/ui/consent_fixture.luau @@ -174,9 +174,9 @@ local PLUGINS: { [string]: any } = { hash = "long-1", status = "pending", author = { kind = "unknown" }, - permissions = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "draft", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, + permissions = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, allowed = {}, - ask = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "draft", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, + ask = { "ui.add", "ui.change", "profile.read", "sessions.read", "messages.read", "account.email", "files.read", "panel.tabs", "pages.add", "github", "github.write", "notifications", "links.open", "navigate", "compose", "network", "clipboard", "files", "web", "windows", "tools", "devices", "service", "commands", "native" }, ui = { change = { "#shell" } }, commands = { "git" }, tools = { { name = "a", description = "First." } }, diff --git a/apps/zgx/ui/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::