Repository navigation
Agent viewer: the page's connection as a state machine; reports in order and echoes by page - #118
Merged
Merged
Conversation
… checks @githubnext/projections-machine is a small, dependency-free state machine core for the hosts' and integrations' lifecycles: - defineMachine: states keyed by `kind`, events by `type`; every state lists a handler or `ignore` for every event, so an unconsidered case, a missing or extra state, or a transition without its state's data is a compile error. Handlers are pure and return the next state or emit(state, ...effects). - createActor: framework-agnostic. Effects and activities are data run by a runner per type, each in a generation that ends when superseded (an effect's `key`), when its state stops listing it, or when the actor stops; its signal aborts and its later events are dropped. after() is a timer activity. - useMachine (./react): one actor per component, started in a layout effect so changes made there render before paint. - explore (./testing): breadth-first walk of reachable states, with state and step invariants, the shortest path to a violation, and `complete` to fail on handlers never taken. npm run check:machines (a `tools check --fast` gate, and in CI) runs the machines/lifecycle ESLint rule (boolean refs, timers started in effects, setters called from timer or promise callbacks) against a committed baseline that may only shrink, and requires an explore test for every machine. docs/guides/state-machines.md explains when and how; AGENTS.md and CONTRIBUTING.md point to it. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… alone With two or more effect types, inferring from the options too made each runner's effect never. The options now take NoInfer of the whole type, and the compile-time tests cover a machine with two effects. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…h; tighter checks From the code review on #110: - An event a generation sent waits in the queue with that generation, and is dropped if the generation ended before it was taken (two keyed effects' synchronous results, an activity removed meanwhile). - Every cleanup an unkeyed effect returns runs when the actor stops; they shared one slot. - Listeners hear a batch before the events they send are taken, so each hears every state in order. - useMachine publishes its runners and error handler in the commit phase, never a render React may discard, and always delegates errors to the current handler or the default reporter. - explore fires a timer once while states keep listing its key, as an actor does. - check-machines: `--update` never raises the baseline; a machine counts as explored only when passed to `explore(`; a module that only imports `defineMachine` passes; the lifecycle rule resolves a setter call to the `useState` binding instead of comparing names. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…der and echoes by page The viewer page kept its connection in a closure loop: a long poll, a `stopped` flag in the store, a two-second sleep that outlived unmounting, an `instance` check, and reports posted every 400 ms without waiting. That let an older report arrive after a newer one, which the server keeps as the newest. The page also took the generation after its own report for that report's echo. With two pages reporting at once, one page took the other's change for its own echo and skipped it. - `viewerConnection` (viewer/lifecycle.ts, on @githubnext/projections-machine): `live` (waiting past a generation), `stopped` (asking again every 2 s), `replaced` by another server, `ended`. The long poll and its retry delay are an activity per ask, so unmounting aborts both. The first state that shows a projection mounts the Inspector, once. - Reports go one at a time: the latest waits for the one in flight, and a resumed page reports again. - The server names the page whose report made a generation (`reportedBy`). A page takes only that for its echo (`echoes`), and every other page applies it. Tests cover each transition and the echo, and explore every connection state: one report in flight at most, the Inspector mounted once, and replaced and ended are final. The Rust state test covers `reportedBy`. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
agorischek
force-pushed
the
viewer-lifecycle-machine
branch
from
November 20, 2026 23:43
c5a411f to
4d47a51
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Report handling can stall indefinitely behind a pending POST after reconnecting and send stale queued state while the connection is stopped, before checking whether the server instance changed.
2 open issues
What changed in this PR
This PR moves the agent viewer’s connection lifecycle onto the state-machine package introduced in #110, aiming to make reporting, reconnection, and multi-page synchronization more reliable.
Changes:
- Models polling, retry, replacement, and unmounting as explicit connection states.
- Serializes reports and identifies their originating page through
reportedBy. - Adds lifecycle and echo tests, dependency updates, and design documentation.
| File | Description |
|---|---|
packages/host/inspector/test/viewer-connection.test.mjs |
Tests connection transitions, report ordering, and page-specific echoes. |
packages/host/inspector/src/viewer/state.ts |
Adds reportedBy to viewer state. |
packages/host/inspector/src/viewer/page.ts |
Identifies report echoes by page ID. |
packages/host/inspector/src/viewer/mount.tsx |
Integrates the connection actor and abortable retry delay. |
packages/host/inspector/src/viewer/lifecycle.ts |
Defines connection states and report queuing. |
packages/host/inspector/src/viewer/apply.ts |
Uses page-specific echoes when applying mode and selection. |
packages/host/inspector/package.json |
Adds the state-machine dependency. |
packages/cli/src/view/state.rs |
Tracks report origin and tests its lifecycle. |
packages/cli/src/view/pages.rs |
Passes the reporting page ID into state updates. |
package-lock.json |
Records the Inspector’s new dependency. |
docs/design/agent-viewer.md |
Documents report serialization and page-specific echoes. |
Review effort: Balanced
…d when the page stops From the code review on #118: a report still in flight when the long poll failed kept `sending` set across the reconnection, so every later report waited behind it. A report waiting behind it went out while the page was stopped, before the reconnection had checked the server. The report in flight is now a `post` activity of `live`, keyed by its sequence number. When the page stops, the activity ends: its request is aborted and its completion is dropped. The outbox is cleared, and the page reports afresh once it resumes. A stopped page takes no reports and no completions. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…pend on it Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.


Stacked on #110, which adds
@githubnext/projections-machine. Review the last commit; this retargets tomainonce #110 merges.Bugs fixed
The code review survey found three, all from the agent viewer page's implicit lifecycle in
viewer/mount.tsx:report()posted every 400 ms without waiting for the last post. A slow POST could land after a newer one, and the server keeps whichever arrived last.generation === myReport.generation + 1as its own echo. When pages A and B both report at generation N, A's report arrives first and makes N+1. B then reads N+1 as its own echo and skips A's mode and selection.Changes
viewerConnection(viewer/lifecycle.ts) is the page's connection, with four states:live: waiting for a generation newer than the one it has;stopped: the server isn't answering, and the page asks again every 2 s;replaced: another server answers at the URL, and the page reloads;ended.stoppedreports again.ViewState.reported_byis set byreport(), cleared byshowandrefresh, and serialized asreportedBy.echoes()treats a generation as the page's own echo only whenreportedByequals its id.ViewerPage.reportedis gone.docs/design/agent-viewer.mddescribesreportedByand the one-at-a-time reports.Tests
packages/host/inspector/test/viewer-connection.test.mjs:explorechecks that no state ever has two reports in flight, that the Inspector mounts once, and thatreplacedandendedare final. It runs withcomplete: true.a_generation_a_report_made_names_its_page_until_the_next_change.Verification
tools check --fastpasses, includingcargo checkandcargo fmt.tscpasses for the Inspector.packages/cli/browser/viewer.spec.tsis left to CI.