On this page

ADR-0325: Explicit State Machines (fsm, derive StateMachine, and fsm_test)

Context

Interactive workflows, TUI screens, agent loops, and background job runners in Tenon rely on finite state machines (FSMs). Hand-written enum + match tables suffer from recurring correctness bugs:

  1. Illegal target transitions: a handler for (Running, Retry) can accidentally return any state variant (such as Queued or Cancelled) and still typecheck.
  2. Forgotten transition handlers or silent wildcard swallowing: adding a new state or event behind a _ catch-all silently drops transitions instead of failing at compile time.
  3. Graph-level structural defects: unreachable states, accidental non-terminal dead-end sinks, transitions exiting @terminal states, duplicate (state, event) edges, and inconsistent event payload types across states go undetected until runtime.
  4. Combinatorial runtime bugs: subtle invariant violations, non-deterministic handlers, or corrupted state on rejected events are rarely caught by hand-written unit tests.

Adding core-language typestate, linear/affine ownership, or dedicated machine/state keywords to Tenon would:

Tenon already has closed nominal enums with exhaustive match, immutable structs, static-dispatch nominal interfaces (<H: I>), repeatable struct field attributes, GEN_ABI 2 pure .tn generators, and ADR-0318 implicit derive materialization. Together these deliver ~90% of compiler-enforced explicit state machine safety with zero changes to LANGUAGE.md, ~1 compact syntax, or Tenon's type system.

Decision

We introduce three pure, zero-capability (capabilities = [], edition = "2026") first-party plugins:

1. plugins/fsm/ — Pure Core Runtime, Parser, Validator & Mermaid Exporter

2. plugins/fsm_gen/ — GEN_ABI 2 derive StateMachine

3. plugins/fsm_test/ — Pure Deterministic Explorer & Shrinker

Consequences