Authoring a state machine as a Mermaid schema — the stateDiagram-v2 subset with `%% @…` directives, the statechart-convert CLI and the typed *.generated.ts it emits.
Ships in docs. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
A machine is authored as one self-contained schema: a Mermaid stateDiagram-v2 where the diagram carries the
structure and %% @… directive comments carry the types and the implementation bodies. The file stays valid
Mermaid throughout — it renders anywhere without plugins. @fozy-labs/statechart-converter compiles it into a typed
createMachine definition for @fozy-labs/rx-toolkit. Reference version: 0.3.0
(fozy-labs/statechart).
Applies when you write or edit a .mmd statechart schema, a mermaid statechart block in Markdown, or run the
converter. The runtime executing the result (config semantics, MachineSignal.state, testing) is the
fozy-labs-signals skill, references/statechart.md — not this document.
stateDiagram-v2
%% @machine square
%% @context type: { result: number | null }
%% @context initial: { result: null }
%% @event SQUARE: { value: number }
[*] --> idle
%% @action square: context.result = event.value ** 2
idle --> done: SQUARE / square
done --> idle: RESET
statechart-convert square.mmd emits square.generated.ts — a plain TypeScript module, no eval:
// AUTO-GENERATED from <label> — do not edit; imports of exactly the names used;Context — the @context type body verbatim ({} without the context directives); Events — the union of events
used in labels, payloads intersected in ({ type: "SQUARE" } & { value: number }); StateId — every state path,
regions included, $final excluded;source — the input text verbatim (viz renders it; config.source carries it too);definition = createMachine<Context, Events>(config, implementations). An @action body that reads context is
wrapped in mutate(…) (an Immer draft — mutate in place); guards and delays never are. The event type inside a
guard / action is narrowed to the events whose transitions reference it.The config is run through the library's createMachine before anything is written, so an invalid machine fails
at conversion, not at runtime.
Above the stateDiagram-v2 header only blank lines and %% lines are allowed.
| Statement | Meaning |
|---|---|
[*] --> A | initial state of the enclosing scope; exactly one per scope |
A --> [*] | transition into the synthetic $final of the scope the line is written in |
A --> B | eventless transition (always) |
A --> B: label | Transition labels |
A --> B: done | onDone of compound/parallel A |
A --> B: after 3000 / after name | delayed transition; a named delay needs @delay |
state X { … } | compound state; { ends its line, } sits alone |
-- inside a block | splits it into parallel regions $0, $1, … in source order |
state "Description" as X (± {) | sets description |
state X <<choice>> | choice state; only untriggered transitions may leave it |
X alone on a line | declares a state in the scope |
note … of X: … / note … end note | ignored, but X counts as a mention |
direction, classDef, class, :::cls | ignored |
%% … at line start | directive or comment |
State ids are [A-Za-z_][A-Za-z0-9_]* (not __proto__); ids starting with $ are reserved for synthetic nodes.
Anything outside that table is an error with a line and column — deliberately including constructs Mermaid accepts but draws differently from how they read:
| Rejected | Because |
|---|---|
<<fork>>, <<join>>, other stereotypes | only <<choice>>; enter parallel regions through their own [*] |
[H], [H*] | history states are outside the schema language |
state X with no body and no description | declares nothing in Mermaid |
X : description | use state "description" as X |
[*] --> X: label, [*] --> [*] | an initial transition carries no label |
stateDiagram (v1), front matter, %%{init} | out of scope |
; in a transition line or single-line note | Mermaid reads it as a statement separator and truncates silently |
%% not at line start | not a comment there |
state a { [*] --> x } on one line | blocks open at end of line, close alone |
a block or region without [*] -->; -- outside a block | every scope needs exactly one initial state |
| a state mentioned inside two different blocks | Mermaid silently keeps the last one |
label := trigger? guard? actions?
trigger := EVENT | "after" (INT | NAME) | "done"
guard := "[" NAME "]"
actions := "/" NAME ("," NAME)*
NAME := [A-Za-z_][A-Za-z0-9_]*
Whitespace is free (X[g]/a,b ≡ X [ g ] / a , b); any character outside names, digits, [ ] / , and spaces is an
error at its column. after and done are reserved — neither works as an event or delay name. Every name in
[…], after / and after after must be declared by a directive of the matching kind.
directive := "%%" SP* "@" KIND (SP+ HEAD)? (":" SP* INLINE)?
continue := "%%" SP+ LINE -- continues the body of the nearest directive above
| Directive | Body | Meaning |
|---|---|---|
@machine <id> | forbidden | machine id; exactly one per diagram |
@context type: <ts-type> | TypeScript type | becomes Context |
@context initial: <expr> | JS expression | initial context |
@event NAME: <ts-type> | TypeScript type | payload: the event becomes { type: "NAME" } & payload |
@guard NAME: <expr> | JS expression → boolean | sees context and event |
@action NAME: <statements> | JS statements | context is an Immer draft when the body reads it |
@delay NAME: <expr> | JS expression → ms | resolves after NAME |
: plus the %%-prefixed lines below. It ends at the next %% @, the first
non-%% line, or a %%text line with no space after the marker (that is how you write an ordinary comment right
under a directive). Bare %% inside a body is an empty line; common indentation is stripped.state X { }.@machine, an unknown kind, an empty body, an @event no
transition uses. The same name under two different kinds is fine.@context type and @context initial must come as a pair — one without the other
fails at emit.The placement rule reproduces Mermaid 11.17.2 layout (held to it by differential tests): a state belongs to the
innermost block — state X { } or a -- region — that mentions it (transition end, bare declaration, note
target, state "d" as X). Mentions at the root place nothing: root-only mentions make a root state; root + block
mentions put it in the block. Mentions inside two different blocks — error; so is a mention inside the state's own
block. For A --> [*] the scope is where the line is written.
Consequences for authoring: introduce a nested state inside its block (a transition line inside the block is enough); cross-boundary transitions are written at the top level after the tree.
A .md file works as input: every ```mermaid fence declaring %% @machine is an independent machine; everything
else — prose, flowcharts, foreign stateDiagrams — is invisible to the converter. Nothing crosses a fence boundary
(directives, @context, guards live inside one block). Fences follow CommonMark (3+ backticks/tildes, ≤3 spaces
indent, closed by same char at same length or longer); the info string must start with mermaid. Two blocks with the
same @machine id — error; an unterminated mermaid fence — error. All reported lines and columns are document
coordinates, and the generated header names the block: // AUTO-GENERATED from flows.md (@machine order) — do not edit.
statechart-convert src/square.mmd #=> src/square.generated.ts
statechart-convert src/square.mmd -o gen/sq.ts
statechart-convert docs/flows.md #=> first machine of the document
statechart-convert docs/flows.md -m order #=> docs/order.generated.ts
statechart-convert docs/flows.md -m order=src/order.generated.ts -m payment=src/payment.generated.ts
statechart-convert docs/flows.md --all #=> <id>.generated.ts per machine, next to the document
--format <mmd|md> overrides the extension-based detection. --out only applies when the path is not already
implied (--machine <id> always emits <id>.generated.ts next to the input — use -m <id>=<file> to place it).
Every machine converts before anything is written. Exit codes: 0 ok, 1 read/convert/write failure or a path
collision, 2 bad arguments. Errors go to stderr as <input>:<line>[:<column>]: <message> [(at <path>)].
Programmatic API: convert(text, { fileName, ...EmitOptions }) = parse → validateMachineConfig → emit; the
Markdown helpers (findStatechartBlocks, convertMarkdown, …); every failure is a StatechartParseError with
line, column?, path? and format().
.mmd file (or one fenced block) = one machine, self-contained: structure, types, bodies.*.generated.ts.; in labels or rely on %% mid-line — Mermaid truncates silently; the converter forbids both.Load only when the situation applies — do not preload.
| Situation | File |
|---|---|
Rendering a live diagram — StatechartViz modes, interaction, theming, eval/CSP, limitations | references/viz.md |
Getting a diagram out of an existing config — toMermaid(), its dialect, the round trip back | references/round-trip.md |
What SKILL.md sets; muted values are Claude Code's defaults
/docs:statechart-craftfrom docs
npx claudepluginhub fozy-labs/astp --plugin docs2 skills in this plugin
Other plugins' skills for the same job