From a2b1978ee33f95d83bd8815cd29abf35e1546f6c Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Wed, 21 Oct 2026 12:30:38 +0200 Subject: [PATCH 1/2] feat(hub): desktop notifications for what needs attention (ADR-0045) The hub follows every running stack and each of its started lanes (on a node too, through the stack's daemon) and tells the desktop when a build failed, a candidate is held for promote, a service exited by itself or failed to start, or a lane's start or move on a node failed; on request also when a lane took the main ports or a candidate went live. Only changes notify, and the same kind for the same service once in five minutes, so a crash loop is one notification. Windows: a WinRT toast under an AppUserModelID the hub registers in HKCU (no shortcut, no admin rights). Linux: org.freedesktop.Notifications over the session bus with zbus, so the binary stays static; without a session bus nothing shows and the hub says why. A click opens the dashboard on the stack, lane and service: a `devstack://open` link on Windows, the hub itself on Linux; `/auth` takes `next` (a dashboard route only) and keeps the session of a browser that is paired already. Settings per machine in the hub (notify.json): each kind on or off, a mute per stack or lane. GET/PUT /hub/notify, POST /hub/notify/test, the bell in the dashboard's top bar, `devstack hub notify`. DEVSTACK_NO_NOTIFY switches them off. Also fixed on the way: `--browser`/DEVSTACK_BROWSER on Windows cut an address at `&` (cmd); the dashboard opened on a lane before it knew the stack runs showed the lane without its list of lanes. Refs #156 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 24 + Cargo.lock | 262 +++++++ Cargo.toml | 5 + README.md | 3 +- SECURITY.md | 2 +- crates/devstack-hub/Cargo.toml | 4 + crates/devstack-hub/src/lib.rs | 145 +++- crates/devstack-hub/src/notify/linux.rs | 175 +++++ crates/devstack-hub/src/notify/mod.rs | 686 ++++++++++++++++++ crates/devstack-hub/src/notify/rules.rs | 577 +++++++++++++++ crates/devstack-hub/src/notify/windows.rs | 188 +++++ crates/devstack-hub/src/ws.rs | 2 +- crates/devstack/src/dispatch/hub.rs | 8 +- crates/devstack/src/hubcmd.rs | 124 +++- crates/devstack/src/integration.rs | 89 ++- crates/devstack/src/main.rs | 31 + crates/devstack/src/selfcmd.rs | 4 +- crates/devstack/tests/hub.rs | 190 +++++ docs/adr/0018-dashboard-is-a-browser-page.md | 7 +- docs/adr/0045-the-hub-notifies-the-desktop.md | 131 ++++ docs/adr/README.md | 3 +- docs/dashboard-api.md | 5 +- docs/plan.md | 4 +- skills/devstack/SKILL.md | 1 + ui/src/api/mock/index.ts | 29 +- ui/src/api/transport.ts | 2 + ui/src/api/types.ts | 13 + ui/src/components/NotifyMenu.vue | 196 +++++ ui/src/components/TopBar.vue | 2 + ui/src/components/icons/icons.ts | 2 + ui/src/stores/stack.ts | 8 +- ui/src/views/StackLayout.vue | 16 +- 32 files changed, 2899 insertions(+), 39 deletions(-) create mode 100644 crates/devstack-hub/src/notify/linux.rs create mode 100644 crates/devstack-hub/src/notify/mod.rs create mode 100644 crates/devstack-hub/src/notify/rules.rs create mode 100644 crates/devstack-hub/src/notify/windows.rs create mode 100644 docs/adr/0045-the-hub-notifies-the-desktop.md create mode 100644 ui/src/components/NotifyMenu.vue diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fbdadd9..4f81e59c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,32 @@ and listed with their assets on the ## [Unreleased] +### Added + +- **Desktop notifications** ([ADR-0045](docs/adr/0045-the-hub-notifies-the-desktop.md), #156): + the hub follows every running stack and each of its started lanes (on a + node too) and tells the desktop when a build failed, a candidate is held + for `promote`, a service exited by itself or failed to start, or a lane's + start or move on a node failed; on request also when a lane took the main + ports or a candidate went live. Only changes notify, and the same thing for + the same service once in five minutes (a crash loop is one notification). + Windows shows a toast, Linux uses the desktop's notification service over + D-Bus (the binary stays static; without a desktop session nothing shows). + A click opens the dashboard on that stack, lane and service. Each kind on + or off, and a mute per stack or lane, behind the bell in the dashboard's + top bar or with `devstack hub notify [on|off …|test]`; + `DEVSTACK_NO_NOTIFY` switches them off. +- A `devstack://open?stack=…&lane=…&service=…` link opens the dashboard + there, paired; `/auth` takes `next`, a dashboard route, and keeps the + session of a browser that is paired already. + ### Fixed +- `--browser` / `DEVSTACK_BROWSER` on Windows: an address with `&` was cut + there by `cmd` (none had one before `/auth?…&next=`). +- The dashboard, opened on a lane before it knew whether the stack runs (a + link, a first visit), showed the lane without its list of lanes. + - Docs: a viewer of a shared lane reads its containers' logs, as it has since 0.11.0 (the node answers them itself); the docs and notes of 0.15.0 said otherwise. diff --git a/Cargo.lock b/Cargo.lock index 8ee8663d..e05dd422 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,6 +118,17 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -658,7 +669,9 @@ dependencies = [ "tokio", "tokio-stream", "ureq", + "windows", "windows-sys 0.61.2", + "zbus", ] [[package]] @@ -753,6 +766,12 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" +[[package]] +name = "endi" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" + [[package]] name = "equivalent" version = "1.0.3" @@ -826,6 +845,12 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + [[package]] name = "futures-sink" version = "0.3.34" @@ -921,6 +946,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "http" version = "1.5.0" @@ -1258,6 +1289,15 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -1451,6 +1491,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" +[[package]] +name = "ordered-stream" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aa2b01e1d916879f73a53d01d1d6cee68adbb31d6d9177a8cfce093cced1d50" +dependencies = [ + "futures-core", + "pin-project-lite", +] + [[package]] name = "pem" version = "4.0.0" @@ -1946,6 +1996,17 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_repr" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d3b1629de253c70a0508c3899572da79ca359fdab27c7920ff00406df418906" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + [[package]] name = "serde_spanned" version = "1.1.2" @@ -2369,6 +2430,17 @@ version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" +[[package]] +name = "uds_windows" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" +dependencies = [ + "memoffset", + "tempfile", + "windows-sys 0.61.2", +] + [[package]] name = "unicode-ident" version = "1.0.27" @@ -2423,6 +2495,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cc1186384beb7dd8eedea376413fd654937285ea6c9cfbb928dc3043ea4b606" +dependencies = [ + "js-sys", + "serde_core", + "wasm-bindgen", +] + [[package]] name = "version_check" version = "0.9.5" @@ -2556,12 +2639,107 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "windows" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" +dependencies = [ + "windows-core", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-future" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" +dependencies = [ + "windows-core", + "windows-link", + "windows-threading", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-numerics" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" +dependencies = [ + "windows-core", + "windows-link", +] + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -2622,6 +2800,15 @@ dependencies = [ "windows_x86_64_msvc 0.53.1", ] +[[package]] +name = "windows-threading" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" +dependencies = [ + "windows-link", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" @@ -2723,6 +2910,9 @@ name = "winnow" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" +dependencies = [ + "memchr", +] [[package]] name = "wit-bindgen" @@ -2768,6 +2958,68 @@ dependencies = [ "time", ] +[[package]] +name = "zbus" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "266223222c9d81e5966455a9da0783e35d56a7ccab79a03c2366b2d98f8fc8a9" +dependencies = [ + "async-trait", + "bitflags 2.13.2", + "endi", + "futures-core", + "hex", + "libc", + "ordered-stream", + "rustix", + "serde", + "serde_repr", + "socket2", + "tokio", + "uds_windows", + "uuid", + "windows-sys 0.61.2", + "winnow", + "zbus_macros", + "zbus_utils", + "zcheapstr", + "zruntime", +] + +[[package]] +name = "zbus_macros" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69191782481ef4e0ac88ca487347c52e08bb85cceb2a163b79f7281fe5be0a3c" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", + "zbus_utils", +] + +[[package]] +name = "zbus_utils" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c27a055b114bd9fc3686e90f62bd1fe612d523fc5f620bac3b1d2f42d3f6cab8" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "syn 3.0.9", + "winnow", +] + +[[package]] +name = "zcheapstr" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1afec51604565183aeb5c54c20aeab286120d4e4460f7f76e3e8bb8c0d99473" +dependencies = [ + "serde", +] + [[package]] name = "zerocopy" version = "0.8.63" @@ -2832,3 +3084,13 @@ dependencies = [ "log", "simd-adler32", ] + +[[package]] +name = "zruntime" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "467fd80a2014d186319f14b81aef027aa466712f355c90158a0e6b39de60726d" +dependencies = [ + "futures-core", + "futures-io", +] diff --git a/Cargo.toml b/Cargo.toml index f2699447..9dc5e796 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,11 +54,16 @@ sha2 = "0.11" zip = { version = "9", default-features = false, features = ["deflate"] } flate2 = "1" tar = "0.4" +# WinRT toasts (ADR-0045); windows-sys has no WinRT. +windows = { version = "0.62", features = ["Data_Xml_Dom", "Foundation", "UI_Notifications"] } +# Desktop notifications on Linux over the session bus, pure Rust (ADR-0045). +zbus = { version = "6", default-features = false, features = ["tokio", "proxy"] } windows-sys = { version = "0.61", features = [ "Win32_Foundation", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_ProcessStatus", + "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Console", "Win32_System_Diagnostics_ToolHelp", diff --git a/README.md b/README.md index 9433179c..2a11b335 100644 --- a/README.md +++ b/README.md @@ -226,10 +226,11 @@ dashboard ─────┘ one per repo, └── services: main slot (f | Variable | Effect | |---|---| -| `DEVSTACK_BROWSER` | the browser `devstack ui` pairs (`chrome`, `edge`, `firefox`, `brave`, a path) | +| `DEVSTACK_BROWSER` | the browser `devstack ui` pairs (`chrome`, `edge`, `firefox`, `brave`, a path), and that a click on a notification opens | | `DEVSTACK_HUB_PORT` | the hub's port (default 4250) | | `DEVSTACK_NO_HUB` | `devstack up` does not start the dashboard hub | | `DEVSTACK_NO_HUB_INTEGRATION` | the installer registers no hub at logon and no `devstack://` link (and records that); `devstack up` registers neither while it is set | +| `DEVSTACK_NO_NOTIFY` | the hub shows no desktop notifications (they are set per kind with `devstack hub notify` or the bell in the dashboard) | | `DEVSTACK_NO_UPDATE_CHECK` | no check for a newer release | | `DEVSTACK_OTLP_ENDPOINT` | devstack's own diagnostic log also to this OTLP/HTTP collector | | `DEVSTACK_HUB_PING_ORIGINS` | more origins (comma-separated) that may ask whether a hub runs, for a copy of the landing page | diff --git a/SECURITY.md b/SECURITY.md index 6132e5be..9c80a1ca 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -22,7 +22,7 @@ runs lanes on other machines, adds two network listeners | Hub (dashboard) | `127.0.0.1` and `::1`, port 4250 (`DEVSTACK_HUB_PORT`) | A browser paired once through a one-time code (`devstack ui`), and the CLI with the token. Details below | | `GET /hub/ping` | the hub | Anyone on the machine; it answers name and version only (with `?nonce=`, also a proof that the hub knows the token, see below). Its CORS header names only the landing page's origin (`https://nuetzliches.github.io`, shared by every GitHub Pages site of the organisation, which therefore can read the version too) plus origins in `DEVSTACK_HUB_PING_ORIGINS` | | `/hub/pick-folder`, `/hub/inspect`, `/hub/stacks/add`, `/hub/stacks/scan`, `/hub/stacks/init` | the hub | The paired browser, from the hub's own page (`GET /hub/inspect` included): opens the system's folder dialog on this machine, inspects a folder the user chose or named, lists the repositories with a stack file below one and registers them, or writes a first `devstack.toml` there, never over an existing file. Local paths only: a network path (`\\host\share`, `//host/share`; on Windows anything but a drive) is refused, as it would make the hub connect to that host as the user | -| `devstack://hub/start` | the operating system | Any web page can open the link; the browser asks first. It can only start the hub, nothing else | +| `devstack://hub/start`, `devstack://open` | the operating system | Any web page can open the links; the browser asks first. `hub/start` starts the hub; `open?stack=…&lane=…&service=…` (what a click on a notification opens, ADR-0045) opens the dashboard there in the user's browser, paired as `devstack ui` does; each value must be a name, and the hub sends the browser on only to a dashboard route. Neither does anything else | | MCP server (`devstack mcp`) | stdio of the agent that started it | That agent. Commands with a confirmation text run only with `confirm: true`, which an agent must not send without the user's consent | | Coordinator (`devstack node serve`) | the address the operator passes (`--listen`), TLS | Anyone who reaches the port may ask to join (bounded: a certificate request of normal size, ten requests a minute per address, at most 64 waiting for an admin). Everything else needs mutual TLS with a certificate of the pool's own CA, issued once an admin approved the member; the member's identity comes from the verified certificate, never from the request. Admin routes need the admin role | | Node agent (`devstack node agent`) | the address the operator passes (`--listen`), TLS | Members of the pool only: mutual TLS with the pool's CA, required for every connection, and the member must be on the coordinator's member list. A lane's API and sync are its requester's; others reach the services of a lane its owner shared with them, nothing else, checked with the coordinator at every connection | diff --git a/crates/devstack-hub/Cargo.toml b/crates/devstack-hub/Cargo.toml index 5f6ff88b..04ec88b0 100644 --- a/crates/devstack-hub/Cargo.toml +++ b/crates/devstack-hub/Cargo.toml @@ -31,8 +31,12 @@ ureq.workspace = true [target.'cfg(windows)'.dependencies] raw-window-handle.workspace = true +windows.workspace = true windows-sys.workspace = true +[target.'cfg(target_os = "linux")'.dependencies] +zbus.workspace = true + [dev-dependencies] tempfile.workspace = true ureq.workspace = true diff --git a/crates/devstack-hub/src/lib.rs b/crates/devstack-hub/src/lib.rs index f7346e71..978c88c9 100644 --- a/crates/devstack-hub/src/lib.rs +++ b/crates/devstack-hub/src/lib.rs @@ -34,6 +34,7 @@ use tokio_stream::StreamExt; static UI: Dir<'_> = include_dir!("$OUT_DIR/ui"); mod dialog; +mod notify; mod pool; pub mod release; mod share; @@ -122,7 +123,7 @@ struct AppState { token: Arc, /// blake3 of each session cookie -> created (ms); persisted, see `Sessions`. sessions: Arc>, - codes: Arc>>, + codes: Codes, stacks: watch::Receiver>, /// The Docker engine as last seen, see `watch_engine`. engine: watch::Receiver, @@ -139,6 +140,8 @@ struct AppState { /// Until when (ms) to look at the engine every 2 s: after a start request. engine_fast_until: Arc, shutdown: Arc, + /// Desktop notifications (ADR-0045). + notify: Arc, } fn now_ms() -> u128 { @@ -362,11 +365,18 @@ pub fn serve() -> Result<(), String> { let (newer_rx, release_poke) = watch_release(); let shutdown = Arc::new(tokio::sync::Notify::new()); + let codes: Codes = Arc::new(Mutex::new(HashMap::new())); + notify::register(); + let open = { + let codes = codes.clone(); + Arc::new(move |t: ¬ify::Target| open_dashboard(port, &codes, &t.path())) + }; + let notify = Arc::new(notify::Center::new(&dir, notify::platform_sink(port, open))); let state = AppState { port, token: Arc::new(token), sessions: Arc::new(Mutex::new(Sessions::load(dir.join(SESSIONS_FILE)))), - codes: Arc::new(Mutex::new(HashMap::new())), + codes, stacks: rx, engine: engine_rx, newer: newer_rx, @@ -376,7 +386,9 @@ pub fn serve() -> Result<(), String> { phases: Phases::default(), engine_fast_until, shutdown: shutdown.clone(), + notify, }; + notify::start(state.clone()); let app = router(state); if let Some(v6) = v6 { let app6 = app.clone(); @@ -429,6 +441,8 @@ fn router(state: AppState) -> axum::Router { .route("/hub/ws", get(ws::ws)) .route("/hub/update", post(update)) .route("/hub/update/check", post(update_check)) + .route("/hub/notify", get(notify::get).put(notify::put)) + .route("/hub/notify/test", post(notify::test)) .route("/hub/inspect", get(inspect)) .route("/hub/pick-folder", post(pick_folder)) .route("/hub/stacks/add", post(stack_add)) @@ -661,10 +675,35 @@ fn from_own_page(h: &HeaderMap, method: &Method, path: &str, own: &[String]) -> #[derive(Deserialize)] struct CodeQuery { code: String, + /// Where to go once paired: a dashboard route (`/#/s/?lane=`), the + /// stack a notification is about (ADR-0045). Anything else goes home. + next: Option, +} + +/// A dashboard route `/auth` may send the browser on to: the hub's own +/// page with a hash route, in characters of a path and a query only. +fn dashboard_route(next: Option<&str>) -> &str { + match next { + Some(n) + if n.starts_with("/#/") + && n.len() <= 512 + && n.bytes() + .all(|b| b.is_ascii_alphanumeric() || b"/#?=&%-._~".contains(&b)) => + { + n + } + _ => "/", + } } -/// Exchange a one-time code (60 s, single use) for the session cookie. -async fn auth(State(s): State, Query(q): Query) -> Response { +/// Exchange a one-time code (60 s, single use) for the session cookie. A +/// browser that is paired already keeps its session: a link from a +/// notification pairs again each time, and need not add one each time. +async fn auth( + State(s): State, + Query(q): Query, + headers: HeaderMap, +) -> Response { let valid = s .codes .lock() @@ -684,15 +723,20 @@ async fn auth(State(s): State, Query(q): Query) -> Response ) .into_response(); } + let next = dashboard_route(q.next.as_deref()).to_string(); + let paired = { + let mut sessions = s.sessions.lock().unwrap(); + cookie_sessions(&headers).any(|c| sessions.contains(c)) + }; + if paired { + return (StatusCode::SEE_OTHER, [(header::LOCATION, next)]).into_response(); + } let id = random_hex(); s.sessions.lock().unwrap().add(&id); let cookie = format!("{COOKIE}={id}; HttpOnly; SameSite=Strict; Path=/; Max-Age=31536000"); ( StatusCode::SEE_OTHER, - [ - (header::SET_COOKIE, cookie), - (header::LOCATION, "/".to_string()), - ], + [(header::SET_COOKIE, cookie), (header::LOCATION, next)], ) .into_response() } @@ -700,18 +744,67 @@ async fn auth(State(s): State, Query(q): Query) -> Response /// How long a pairing code works. const CODE_LIFETIME: Duration = Duration::from_secs(60); -async fn pair(State(s): State) -> Response { +/// One-time pairing codes -> when they were made. +type Codes = Arc>>; + +fn new_code(codes: &Codes) -> String { let code = random_hex(); - { - let mut codes = s.codes.lock().unwrap(); - // Codes nobody used are dropped once expired, not kept for the hub's life. - codes.retain(|_, t| t.elapsed() < CODE_LIFETIME); - codes.insert(code.clone(), Instant::now()); - } + let mut codes = codes.lock().unwrap(); + // Codes nobody used are dropped once expired, not kept for the hub's life. + codes.retain(|_, t| t.elapsed() < CODE_LIFETIME); + codes.insert(code.clone(), Instant::now()); + code +} + +async fn pair(State(s): State) -> Response { + let code = new_code(&s.codes); let url = format!("http://127.0.0.1:{}/auth?code={code}", s.port); Json(serde_json::json!({ "code": code, "url": url })).into_response() } +/// Open the dashboard at `route` in the default browser, paired: what a +/// click on a notification the hub hears itself does (Linux, ADR-0045). +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +fn open_dashboard(port: u16, codes: &Codes, route: &str) { + let code = new_code(codes); + let url = format!( + "http://127.0.0.1:{port}/auth?code={code}&next={}", + next_param(route) + ); + #[cfg(target_os = "linux")] + if let Err(e) = std::process::Command::new("xdg-open") + .arg(&url) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + { + diag::warn( + "open_dashboard_failed", + serde_json::json!({ "error": e.to_string() }), + ); + } + #[cfg(not(target_os = "linux"))] + let _ = url; +} + +/// `route` as the value of `/auth`'s `next`. +pub fn next_param(route: &str) -> String { + route + .bytes() + .map(|b| match b { + b'#' | b'?' | b'&' | b'=' | b'%' | b'+' => format!("%{b:02X}"), + _ => (b as char).to_string(), + }) + .collect() +} + +/// Remove what the hub registered with the system for its notifications +/// (`devstack self uninstall`). +pub fn unregister_notifications() { + notify::unregister(); +} + async fn shutdown(State(s): State) -> Response { s.shutdown.notify_waiters(); StatusCode::ACCEPTED.into_response() @@ -2358,6 +2451,28 @@ mod scan_tests { mod guard_tests { use super::*; + #[test] + fn auth_sends_on_to_dashboard_routes_only() { + let route = "/#/s/ab12/services/api?lane=feat-x"; + assert_eq!(dashboard_route(Some(route)), route); + // What `next_param` makes of it comes back as it was. + let uri: axum::http::Uri = format!("/auth?code=c&next={}", next_param(route)) + .parse() + .unwrap(); + let Query(q) = Query::::try_from_uri(&uri).unwrap(); + assert_eq!(q.next.as_deref(), Some(route)); + for bad in [ + None, + Some("https://evil.example/"), + Some("//evil.example/#/"), + Some(r"/#/s/x\evil"), + Some("/#/s/ + + + + diff --git a/ui/src/components/TopBar.vue b/ui/src/components/TopBar.vue index 52f0d8cb..d15b65b4 100644 --- a/ui/src/components/TopBar.vue +++ b/ui/src/components/TopBar.vue @@ -8,6 +8,7 @@ import { useStack } from '@/stores/stack'; import { useUi } from '@/stores/ui'; import StackSwitcher from './StackSwitcher.vue'; import PendingIndicator from './PendingIndicator.vue'; +import NotifyMenu from './NotifyMenu.vue'; import Icon from './icons/Icon.vue'; const hub = useHub(); @@ -88,6 +89,7 @@ const mismatch = computed(() => { {{ conn.label }} {{ conn.title }} + diff --git a/ui/src/components/icons/icons.ts b/ui/src/components/icons/icons.ts index 5b6b374f..e39e6fe0 100644 --- a/ui/src/components/icons/icons.ts +++ b/ui/src/components/icons/icons.ts @@ -15,6 +15,8 @@ export const icons = { plus: '', download: '', alert: '', + bell: '', + bellOff: '', info: '', pause: '', hourglass: '', diff --git a/ui/src/stores/stack.ts b/ui/src/stores/stack.ts index 8459836d..fee4cffb 100644 --- a/ui/src/stores/stack.ts +++ b/ui/src/stores/stack.ts @@ -302,8 +302,14 @@ export const useStack = defineStore('stack', () => { } // Follow the hub: connect when the stack starts, drop the stream when it stops. + // A lane chosen before the hub said the stack runs (a link, the first load + // of a page) streams from its own API, which does not carry the lanes: + // they are read now, as `reset` would have. watch(running, (r) => { - if (r && !sub) connect(); + if (r && !sub) { + connect(); + if (lanesOf.value !== id.value) void loadLanes(); + } if (!r && sub) { disconnect(); if (viewAt.value === null && view.value) viewAt.value = Date.now(); diff --git a/ui/src/views/StackLayout.vue b/ui/src/views/StackLayout.vue index 3f196951..d61b79eb 100644 --- a/ui/src/views/StackLayout.vue +++ b/ui/src/views/StackLayout.vue @@ -1,6 +1,6 @@