Skip to content

VS Code: a webview page's lifecycle as a state machine, so no command waits forever - #111

Merged
agorischek merged 11 commits into
mainfrom
vscode-webview-machine
Nov 21, 2026
Merged

agorischek merged 11 commits into
mainfrom
vscode-webview-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.

The bug

InspectorWebview kept the page's readiness as a ready promise and replaced it on every show(). A control() call waited on that promise before arming its 30 s timeout. If the page then loaded again before it was ready, which a backend replacement does through bind() → show(), the waiting call held a promise whose resolver had been overwritten. It never settled, and no timeout was ever armed. dispose() never settled ready either, so a command waiting on a tab that closed first also leaked forever. A command posted to a page that VS Code then dropped and reloaded waited the full 30 s.

The fix

The page's lifecycle is now an explicit machine, pageLifecycle in src/panels/page-lifecycle.ts. Its states are loading{page, waiting}, ready{page, sent} and closed, and the commands waiting on the page are part of the state.

  • A command asked for while the page loads waits across reloads and is posted when the page is ready.
  • Its timeout counts from the call. Each command has an after timer, so a page that never becomes ready still fails the command after timeoutMs.
  • A command posted to a page that reloads fails at once with "The Inspector reloaded before it answered". This covers both the extension's reload and VS Code's reload of a hidden tab (dropped, which replaces the old isReady reset in ProjectionTab.restore).
  • Closing fails every waiting command, and any asked for after.
  • The page now echoes which load it is (page in its config and in its ready), so a late ready from a replaced page isn't mistaken for the new one's.

Tests

test/page-lifecycle.test.mjs:

  • one regression test per case above;
  • an explore over every reachable state, checking that every command is posted exactly once, when the page is ready for it, and settled exactly once, whatever the page does (complete: true).

Breaking the dropped transition makes the exploration fail with the shortest path: loading --ready--> ready --asked--> ready --dropped--> loading.

Verification

  • tools check --fast passes, including check:machines.
  • tsc for the extension passes.
  • The extension's node suites pass, except two that need a built dist/; they fail the same way without this change.

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>

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

Calls to control() after disposal can leave promises pending indefinitely, and missing actor cleanup in the lifecycle test keeps the test process alive for roughly 30 seconds.

2 open issues
What changed in this PR

This PR replaces the VS Code Inspector webview’s readiness promise with an explicit page lifecycle machine, building on #110 to prevent commands from waiting indefinitely.

Changes:

  • Tracks loading, ready, and closed states, with command timeouts starting at the call.
  • Adds page-load identifiers and handles reloads, stale readiness messages, and closure.
  • Adds regression tests and state-machine exploration for command posting and settlement.
File Description
packages/​integrations/​vscode/​test/​page-lifecycle.test.mjs Adds lifecycle regression and exploration tests.
packages/​integrations/​vscode/​src/​webview/​main.ts Echoes page-load identifiers in readiness messages.
packages/​integrations/​vscode/​src/​protocol.ts Adds optional page identifiers to the webview protocol.
packages/​integrations/​vscode/​src/​panels/​projection-tab.ts Signals page drops during tab restoration.
packages/​integrations/​vscode/​src/​panels/​page-lifecycle.ts Defines lifecycle states, command handling, and timeouts.
packages/​integrations/​vscode/​src/​panels/​inspector-webview.ts Integrates the lifecycle actor with webview commands and disposal.
packages/​integrations/​vscode/​package.json Adds the state-machine dependency.
package-lock.json Records the extension’s new dependency.

Review effort: Balanced

agorischek and others added 3 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>
… waits forever

InspectorWebview kept the page's readiness as a promise it replaced on every load. A command (`control`) that awaited
the promise of a load the extension then replaced (a backend replacement reloads the page) waited forever: its 30 s
timeout was armed only after the await, and closing the panel never settled it either.

The page's lifecycle is now `pageLifecycle` (page-lifecycle.ts, on @githubnext/projections-machine): loading, ready
or closed, with the commands waiting on the page in its state.

- A command asked for while the page loads waits for the page across reloads, and is posted when it is ready.
- Its time counts from the call, as an `after` timer per command, so a page that never gets ready fails it.
- A command posted to a page that then reloads (the extension's load, or VS Code's reload of a hidden tab) fails at
  once instead of after 30 s.
- Closing fails every waiting command, and any asked for after.
- The page says which load it is in its `ready`, so a late ready from a replaced page isn't taken for the new one.

The tests cover each case and explore every state: every command is posted once, when the page is ready for it,
and settled once, whatever the page does.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…sts stop their actors

From the code review on #111: the page's lifecycle keeps running once closed, so a later control() is rejected rather
than queued on a stopped actor, and the tests stop their actors so no 30 s timer keeps the process alive.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@agorischek
agorischek force-pushed the vscode-webview-machine branch from 011c757 to 038c7f8 Compare November 20, 2026 23:43
Base automatically changed from machine-core to main November 21, 2026 01:29
@agorischek
agorischek merged commit 6d3953d into main Nov 21, 2026
52 checks passed
@agorischek
agorischek deleted the vscode-webview-machine branch November 21, 2026 03:24
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