Repository navigation
VS Code: a webview page's lifecycle as a state machine, so no command waits forever - #111
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>
There was a problem hiding this comment.
🟡 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
…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
force-pushed
the
vscode-webview-machine
branch
from
November 20, 2026 23:43
011c757 to
038c7f8
Compare
…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.The bug
InspectorWebviewkept the page's readiness as areadypromise and replaced it on everyshow(). Acontrol()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 throughbind()→show(), the waiting call held a promise whose resolver had been overwritten. It never settled, and no timeout was ever armed.dispose()never settledreadyeither, 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,
pageLifecycleinsrc/panels/page-lifecycle.ts. Its states areloading{page, waiting},ready{page, sent}andclosed, and the commands waiting on the page are part of the state.aftertimer, so a page that never becomes ready still fails the command aftertimeoutMs.dropped, which replaces the oldisReadyreset inProjectionTab.restore).pagein its config and in itsready), so a latereadyfrom a replaced page isn't mistaken for the new one's.Tests
test/page-lifecycle.test.mjs:exploreover 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
droppedtransition makes the exploration fail with the shortest path:loading --ready--> ready --asked--> ready --dropped--> loading.Verification
tools check --fastpasses, includingcheck:machines.tscfor the extension passes.dist/; they fail the same way without this change.