feat(web): live session feed, so the UI tracks changes made elsewhere - #160
Open
defangdevs wants to merge 1 commit into
Open
feat(web): live session feed, so the UI tracks changes made elsewhere#160defangdevs wants to merge 1 commit into
defangdevs wants to merge 1 commit into
Conversation
The workspace and the settings session list only patched themselves after
their OWN form posts, plus a short burst poll while a session was still
"starting". A session created or removed anywhere else — the
agent-box-session CLI, an agent spawning a helper for itself, a second
browser tab — stayed invisible until someone reloaded the page.
Add {SESS_BASE}/sessions/events: a Server-Sent Events stream carrying a
fingerprint of the session state (name, agent, cwd, live/starting), one
frame per change. The page compares it with what it last rendered and
re-fetches itself on a difference, reusing the same swap its form posts
already do. Only the digest crosses the stream, never session data.
One shared watcher thread samples for all connected streams — a sample
forks `tmux list-sessions`, so per-client sampling would scale with open
tabs — and exits when the last stream leaves, so an idle box spawns
nothing. Streams are capped (past the cap clients keep polling), notice a
vanished client within a second so slots free promptly, and keep-alive
every 20s. ?poll=1 answers with the same fingerprint as one-shot JSON for
clients that cannot hold a stream open.
The client keeps that poll as a slow backstop even with the stream up
(and probes on tab re-show): a stream can die in ways neither end
notices, and a workspace that silently stopped updating is the bug this
fixes.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013VoCn5tqBY3hKKwmtCH1kx
Collaborator
|
Does this remove the polling? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The tabbed workspace and the settings session list only patched themselves after their own form posts, plus a short burst poll (8 × 2.5 s) that ran only while a session was still
starting. Anything that changed sessions from elsewhere was invisible until a manual reload:agent-box-session add/rmfrom a terminal,What this does
GET {SESS_BASE}/sessions/eventsis a Server-Sent Events stream. Each frame carries a 16-hex fingerprint of the session state the pages actually render (name, agent, working directory, live/starting). The page compares it with the state it was rendered from and, on a difference, re-fetches itself and patches#tab-bar/#sessions-list— the same swap its own form posts already perform.Design points worth reviewing:
hasRun,boxSessionId, clearedinitialPrompt) so first-spawn rewrites don't look like a change worth re-rendering for.tmux list-sessions, so sampling per client would scale with open browser tabs. The watcher runs only while ≥ 1 stream is connected and exits when the last one leaves — an idle box spawns nothing.<meta name="agent-box-events" content="…" data-fp="…">and the daemon replays the current fingerprint on connect, so a change landing between render and connect is not lost.?poll=1on the same route returns{"fp": …}as one-shot JSON. The client polls it every 5 s with noEventSourceor after a refused stream, and keeps a slow 30 s probe (plus one on tab re-show) even while the stream is up — a stream can die in ways neither end notices, and a workspace that silently stopped updating is the bug this PR exists to fix. Probes pause while the tab is hidden.tmux:OSError log line, now that a background sampler can hit it every second.User-visible effect
Sessions added or removed anywhere appear/disappear in the tab bar and the settings list within about a second, without a reload. No layout or styling change, so no screenshots — the pages look exactly as before.
Security
AWS / cost / migration
None. No template, IAM, networking or state changes; nothing to migrate.
Checks run
python3 bin/assemble-module.py --check—modules/agent-box.nixregenerated and up to date.EventSourcefallback polls and catches up, a 503'd stream falls back too, and stream slots are released when a page closes.SPA layer: saving and deleting never triggers a full page load,the tab close button arms first and only closes on a second click,?tab= selects a tab server-side,no root-page href embeds URL userinfo, plus the secret save/edit/delete specs.nix-instantiate --parse tests/sessions.nixand a compile of its extractedtestScript;playwright test -c tests/e2e --listfor the specs.Not run here: the NixOS VM checks (
sessions,settings-page) — this box is aarch64 and the flake's checks are x86_64-only, so CI is their first real run. The newsessionssubtest is the important one: it holds a stream open from the client VM through Caddy and requires a frame whose fingerprint differs from the stream's own first frame after a CLIagent-box-session add, so merely receiving a frame cannot pass it.Note, unrelated to this PR
tests/e2e/root-sessions.spec.ts:90("add a session from the tab bar…") is already broken on master: it fillsinput[name="name"], which the add form dropped when session names became auto-derived. Left alone here.🤖 Generated with Claude Code
https://claude.ai/code/session_013VoCn5tqBY3hKKwmtCH1kx