Find implicit state machines, review state bugs, and check or visualize statecharts with Stately.
95%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Medium
Suggest reviewing before use
Use this when the request starts with application code rather than a known model. An implicit machine is behavior whose current phase and permitted next events are encoded indirectly. A single boolean can be enough; a collection of flags is not automatically one machine. The test is whether an event's valid outcome depends on a persistent phase or combination of values.
For a diff, branch, or pull request, follow state-review.md instead; it reviews only what the change touches.
The number of candidates follows from the code, not from a target. A first pass tends to stop near eight candidates whatever the repository size, which leaves most of a large codebase unexamined. When the request is to find the machines in an app, or all of them:
Signals that mark state, beyond status enums and booleans:
null when idle.Option<Option<T>>, undefined
versus null, -1, empty string) that distinguish "not looked up" from
"looked up, absent".await or callback, and the places that should compare them but do not.pending, deleting, inFlight,
suppressed), and atomics or integer constants used as a state code.disposed, stopped, closing, sealed)
and every path that must check them.inc/dec, add/remove, acquire/release,
subscribe/unsubscribe) whose two sides sit on different paths.Rank once, over the merged table, after every area is swept. Do not order by area, by discovery order, or by how detailed a finder's note is. Every row gets a rank; no tier is left grouped by area.
First merge rows that describe one machine: values in the same owner that track the same entity (several maps or sets keyed by one id, flags written by the same methods, a start flag and its matching shutdown flag) are one candidate, and the conflict to look for is an entity in two of them at once. A merged row lists every state-bearing value it absorbs, so no flag disappears in a merge.
Then score each candidate on what the source shows:
catch or error callback that runs after
the phase is set and does not restore it, or that only logs. Most severe
failures in distributed systems start as a non-fatal error that was
signalled and then mishandled, so an error path that leaves partial state
ranks above an extra happy-path await.A specific failing path raises a candidate. The lack of one does not lower it: a first pass rarely traces every interleaving, so a cluster with many encoded phases, wide reach, and async exposure ranks high with its conflict stated as a question. A sharp note on a leaf component does not outrank it.
When areas are delegated, have each finder return the four scores (value count, reader and writer count, async boundaries, consequence) with every candidate so rows from different areas compare on the same scale. Before fixing the top of the table, reopen the owner of any candidate that scores high on the first three but has a thin or inferred note, and rewrite its conflict line from source.
Visualize candidates in rank order, not only the first. Call
visualize_machine once per candidate, or visualize_machines with up to
20 per call, and put each returned link next to its candidate. Pass a
workspaceId only when the previous result reported persistence as
server; otherwise send each batch as its own call. With more
candidates than one answer can diagram, visualize every high-risk candidate,
then medium-risk ones while the budget lasts. When the table has more than 20
rows, visualize at least the top 40 in batches of 20, each batch a separate
visualize_machines call. State how many were
visualized, and offer to visualize the rest by rank or area. Every
visualized candidate keeps its returned link, in the answer or in the
inventory file. Do not drop a returned link to shorten the answer, and do
not visualize more candidates than will be linked. Send the
complete model: keep guards, effects, and failure paths. Do not shrink or
split a model to obtain a link or an image. If a link or image is missing
from a result, say so for that candidate and give its full model as an XState
v6 config the user can import into Stately.
When the host can write files, write one inventory document per sweep in a task-owned directory and name its path in the answer. It holds the ranked table and, for each candidate, its link and:
path:line that decides it.On a later sweep, update the same document in place. Mark rows whose source
is gone as possibly removed instead of deleting them, and add new rows. When
fixes are proposed, group them into issues by shared owner and shared fix, one
issue per group, and ask before filing any issue.
Provide a small table with candidate, source evidence, observed
states/events, possible invalid state or transition, kind (from
state-bugs.md), and confidence. A possible bug that
has the full evidence (state, event, numbered sequence with path:line,
acting line, consequence) and survived a re-trace is a finding; anything less
stays a question.
For the selected candidate, supply a readable statechart and source map.
Use the application's language for labels. Mark a source-derived chart as
estimated from source even when its separate formal check passes. Show it
as a Stately model when connected, and as an importable XState v6 config
otherwise. Do not erase guards, effects, hierarchy, or concurrent regions to
make the picture simpler without naming that omission.
If the user asks for bugs or verification, establish what must always hold and what must eventually happen. When the product contract is unclear, state an explicit best guess and continue reversible modeling if useful. Check one finite model with model-authoring.md and the skill's owned checker. Report each broken rule with its shortest path, model bounds, and a real test or reproduction result. A model counterexample is not yet an application bug.
Report the findings first and change code only when the user asks. Make one candidate explicit per change, in rank order, and preserve external behavior unless the user authorizes a change.
xstate@6 beta, pinned to the version
the native checker uses). Use createFSM from xstate/fsm for a flat
machine with no hierarchy, parallel regions, or actors, and setup or
createMachine from xstate otherwise. In v6, every transition is an
object, as in on: { GO: { target: 'done' } }; bare string targets are not
allowed. When the project already depends on XState v5, write a v5 machine
instead, following the xstate-v5 skill when it is installed. When the
project uses another state library (Redux, Zustand, Pinia, MobX), propose
XState v6 and ask before switching. Write plain TypeScript (a discriminated
union and a transition function) only when the user asks. State in the
change summary that xstate@6 is a beta.machine.config) to Flow.json and run
Check invariants with the same rules. The
native checker runs XState v6 semantics, so states and transitions come
from the code, not from an estimate, and
Check a fix runs on the real machine. Guard and
action behavior still comes from the rules file; cite each implementation's
path:line. Save the machine to Stately.// xstate-version: 6
import { createFSM } from 'xstate/fsm';
// Replaces `isUploading` and the early return that left it set.
export const uploadMachine = createFSM({
id: 'upload',
initial: 'idle',
states: {
idle: { on: { PICK: { target: 'uploading' } } },
uploading: {
on: {
UPLOADED: { target: 'idle' },
NO_URL: { target: 'failed' },
},
},
failed: { on: { RETRY: { target: 'uploading' } } },
},
});Not every candidate needs a machine. Leave a two-phase candidate with one owner, no async step, and no invalid combination as it is, and say why.
After the first explicit change, offer this snippet for the repository's AGENTS.md or CLAUDE.md, and add it only when the user agrees:
- Model a lifecycle with more than two phases, or any phase that crosses an
await, timer, retry, or another owner, as an explicit state machine
(XState, matching the version this repository uses). Do not add boolean
flags or status strings that encode the same lifecycle..tessl-plugin
agents
references
scripts
engine