Fix SCXML state action ordering - #31
Conversation
There was a problem hiding this comment.
Code Review
This pull request ensures that state entry actions run in document order and exit actions run in reverse document order, including nested and parallel configurations. This is achieved by sorting the states to enter and exit by their order attribute in src/xstate/algorithm.py. Additionally, a new test file tests/test_action_ordering.py has been added to verify these ordering behaviors. No review comments were provided, and the implementation is clean and well-tested.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Summary
Why
The entry and exit sets are intentionally represented as sets while they are computed. Iterating those sets directly made side-effect ordering depend on Python object iteration rather than statechart document order.
SCXML requires deterministic ordering: ancestors and earlier document states enter first, while descendants and later document states exit first. This is observable whenever states declare entry or exit actions, especially across parallel regions.
Behavior covered
Scope
This changes only state action ordering inside the existing microstep algorithm. Transition selection, transition domains, context updates, handler adaptation, snapshots, and public APIs are unchanged.
Validation
poetry run python -m pytest tests/test_action_ordering.py -q(5 passed)poetry run python -m pytest tests/ --ignore=tests/test_scxml.py(402 passed)poetry run python -m pytest tests/test_scxml.py(54 passed)poetry run mypy src/xstate/poetry run ruff format --check src/ tests/ docs/examples/poetry run ruff check src/ tests/ docs/examples/git diff --check