Add bounded microstep traces and inspection - #42
Merged
Conversation
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.
Summary
Add bounded run-to-completion execution, immutable typed microstep traces, and fail-open inspection callbacks for machine runtimes.
Rationale
Stable XState v5 exposes public microstep utilities and a
maxIterationsguard for non-settling machines. Python already had a W3C-style macrostep loop, but callers could only observe the final snapshot and an eventless or raised-event cycle could run without a configured bound.This PR keeps transition calculation pure. It records portable summaries at each settled microstep, resolves public snapshots at the machine boundary, and leaves action execution with interpreters and actors.
Scope
TransitionTrace,MicrostepTrace, andMacrostepTracerecords with defaulted generics.get_microsteps(machine, snapshot, event)andget_initial_microsteps(machine).Machine.transitionresults.MachineOptionsConfigand support JSONoptions.maxIterations.Machine(..., max_iterations=N)with non-Nonekeyword precedence.InvalidConfigError.InfiniteLoopErrorbefore the first enabled microstep over the limit.inspectcallbacks to sync and async interpreters and machine-backed actors.Trace boundary
Transition summaries expose only event type, source ID, and ordered target IDs. Public trace records do not expose internal
Transitionobjects, a mutable configuration set, or the internal queue. Intermediate state containers retain the existing immutable public configuration and action boundaries.Non-scope
@statelyai/inspectwire protocol.Validation
Local Python 3.14:
poetry run python -m pytest tests/ --ignore=tests/test_scxml.py: 480 passed.poetry run python -m pytest tests/test_scxml.py: 57 passed.poetry run mypy src/xstate/: passed.poetry run mypy --strict src/xstate/algorithm.py: passed.poetry check --lock: passed.poetry build: passed.poetry run python scripts/validate_distribution.py: installed-wheel smoke passed.Hosted exact head
1b1106d9385d99af61d7bdbecb6fa84b069b3e73:Risks and rollback
The highest-risk area is the macrostep loop because tracing and the iteration counter observe every enabled microstep. Regression coverage locks FIFO internal events, ignored-event records, action accumulation, limit timing, final output, and existing SCXML behavior. Inspectors are fail-open and do not participate in transition calculation.
There is additional allocation when a macrostep captures intermediate immutable records. No persisted format or external dependency changes. Rollback is a revert of this PR after reverting or retargeting PR 3.
Review focus
Stack and merge order
This is PR 2 of 3. PR 1 has merged, so this PR now targets
master. Merge it beforeactor-ref-boundary.