Skip to content

Machine core: run a transition's effects before its new activities start - #139

Merged
agorischek merged 3 commits into
mainfrom
machine-effects-first
Nov 21, 2026
Merged

agorischek merged 3 commits into
mainfrom
machine-effects-first

Conversation

@agorischek

Copy link
Copy Markdown
Collaborator

When a transition changed the state, the actor started the new state's activities before it ran the transition's effects. An activity could therefore act on a world its own transition hadn't finished setting up. The private tour hit this (githubnext/projections.private#56): its page check read a diff's loading clock before the resume effect had shifted it by the paused time, so a long pause failed the tour.

The new order

When a transition changes the state:

  1. the activities the new state no longer lists end;
  2. the transition's effects run, in order;
  3. the activities it newly lists start.

If an effect stops the actor, the new activities start on the next start. A transition that keeps the state runs its effects as before.

Guide

docs/guides/state-machines.md documents the order. It also documents a related hazard the private carousel hit (githubnext/projections.private#60): a send from a callback (a frame, a timer, a response) is handled before it returns, and can end the very runner that sent it. So a runner that does more work after sending should request it first, such as a frame loop's next frame, or check signal.aborted.

Existing machines

All three machines on main keep their behaviour:

Tests

  • packages/machine/test/actor.test.mjs:
    • the order: ended activities, then effects, then new activities;
    • an effect that stops the actor leaves the new activities to the next start;
    • a send from a callback that leaves the state ends its own activity before it returns.
  • page-lifecycle.test.mjs: a control is posted before its timeout starts. This test fails on the previous order.
  • playback-lifecycle.test.mjs: playback stopping at its end has no frames running when it asks the view to stop.

Locally: machine tests 20/20, page lifecycle 7/7, playback lifecycle 9/9, and tools check --fast passes.

agorischek and others added 2 commits November 20, 2026 23:33
When a transition changed the state, the actor started the new state's
activities before running the transition's effects, so an activity
could act on a world its own transition hadn't finished setting up. The
private tour hit this: its page check read a diff's loading clock
before the resume effect shifted it by the paused time. Now the
activities the state no longer lists end, the effects run, and then the
activities it newly lists start. If an effect stops the actor, the next
start starts them.

The guide documents the order, and that a send from a callback can end
its own runner before returning: a frame loop requests its next frame
before sending its tick, or checks signal.aborted after.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A control asked for on a ready page is posted before its expiry timer
starts, so its timeout is measured from the post; playback that stops at
its end has ended its frame loop before it asks the view to stop. The
webview test fails on the previous order.

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

The guide incorrectly implies effects are canceled when their keys disappear from a new state, so it must distinguish activity removal from keyed-effect supersession and actor-stop cleanup.

1 open issue
What changed in this PR

This PR updates the shared machine actor so transition effects run after obsolete activities end but before new activities start. This lets new activities observe setup completed by the transition’s effects.

Changes:

  • Split activity reconciliation into stop and start phases around effects.
  • Add regression tests for actor ordering, webview timeout startup, and playback cleanup.
  • Document transition ordering and cancellation during callback-driven sends.
File Description
packages/​machine/​test/​actor.test.mjs Tests ordering, stop/restart behavior, and callback-triggered cancellation.
packages/​machine/​src/​machine.ts Updates the emit API documentation with the new ordering.
packages/​machine/​src/​actor.ts Runs transition effects before starting newly listed activities.
packages/​integrations/​vscode/​test/​page-lifecycle.test.mjs Verifies ready-page controls are posted before their timeouts start.
packages/​host/​core/​test/​playback-lifecycle.test.mjs Verifies frame cleanup precedes playback stop requests.
docs/​guides/​state-machines.md Explains transition ordering and runner cancellation precautions.

Review effort: Balanced

Comment thread docs/guides/state-machines.md Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@agorischek
agorischek merged commit 07558fb into main Nov 21, 2026
46 checks passed
@agorischek
agorischek deleted the machine-effects-first branch November 21, 2026 08:45
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