Skip to content

Agent viewer: the page's connection as a state machine; reports in order and echoes by page - #118

Merged
agorischek merged 10 commits into
mainfrom
viewer-lifecycle-machine
Nov 21, 2026
Merged

agorischek merged 10 commits into
mainfrom
viewer-lifecycle-machine

Conversation

@agorischek

Copy link
Copy Markdown
Collaborator

Stacked on #110, which adds @githubnext/projections-machine. Review the last commit; this retargets to main once #110 merges.

Bugs fixed

The code review survey found three, all from the agent viewer page's implicit lifecycle in viewer/mount.tsx:

  1. Reports could arrive out of order. 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.
  2. A page could mistake another page's change for its own echo. The page treated generation === myReport.generation + 1 as 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.
  3. The retry delay outlived the page. The two-second sleep between retries kept running after unmount.

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.
  • 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, exactly once.
  • Reports go one at a time. While one is in flight the latest waits, replacing any earlier one that was waiting. Reports that say nothing new are skipped. A page that resumes after stopped reports again.
  • The server records which page caused a generation. ViewState.reported_by is set by report(), cleared by show and refresh, and serialized as reportedBy. echoes() treats a generation as the page's own echo only when reportedBy equals its id. ViewerPage.reported is gone.
  • docs/design/agent-viewer.md describes reportedBy and the one-at-a-time reports.

Tests

  • packages/host/inspector/test/viewer-connection.test.mjs:
    • report ordering and dedupe;
    • mount once;
    • an answer with the same generation still reports the build phase;
    • stop, retry and resume;
    • replacement;
    • the two-page echo case.
  • An explore checks that no state ever has two reports in flight, that the Inspector mounts once, and that replaced and ended are final. It runs with complete: true.
  • The Rust test a_generation_a_report_made_names_its_page_until_the_next_change.

Verification

  • tools check --fast passes, including cargo check and cargo fmt.
  • The view state's Rust tests pass.
  • tsc passes for the Inspector.
  • The Inspector's node suites pass, except the catalog test, which needs the client build and fails the same way without this change.
  • packages/cli/browser/viewer.spec.ts is left to CI.

agorischek and others added 2 commits November 20, 2026 15:19
… 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>
agorischek and others added 2 commits November 20, 2026 15:42
…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
agorischek force-pushed the viewer-lifecycle-machine branch from c5a411f to 4d47a51 Compare November 20, 2026 23:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

agorischek and others added 5 commits November 20, 2026 15:57
…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>
Base automatically changed from machine-core to main November 21, 2026 01:29
@agorischek
agorischek merged commit e5df8a8 into main Nov 21, 2026
52 checks passed
@agorischek
agorischek deleted the viewer-lifecycle-machine branch November 21, 2026 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants