▶ Watch It Teach Itself — Interactive Replay A click-through of a real 200-event run, with the AI analyst's actual reasoning on every turn.
Every real system carries more state than anyone wrote down. Orders that got refunded before they shipped. Disputes opened on deliveries nobody modeled. Sessions in flag combinations your enum never anticipated. A traditional state machine handles exactly what you predicted — and everything else becomes an incident, a silent else branch, or quietly corrupted data.
We built and open-sourced an experiment that inverts this: SESM, the Self-Expanding State Machine.
A Pydantic model bounds what's possible. You map only the states you know. Every unmapped-but-real situation becomes a first-class discovery event — deduplicated, triaged by a cheap LLM, and either taught to the machine as a new named state or flagged as the bug it is.
The trick that makes this sane rather than terrifying: the state space is finite and computable. Three fields with 5 × 5 × 3 possible values is exactly 75 combinations, so "we handle 48% of what's possible" is a real, measurable number — and growth is coverage exploration of a bounded space, not open-ended state invention. Schema evolution stays where it belongs: in your Pydantic model and your type checker.
The Loop
An event drives the FSM into a combination it was never taught. Three validation gates fire in order — Pydantic types, your domain rules, then the explicit-state lookup — and any failure emits a DiscoveryEvent to whatever handlers you've plugged in: a webhook, a queue, a plain function, an agent dispatcher. The reaction is deliberately yours to define.
In our runs, the handler is an LLM analyst — gpt-5.4-mini with reasoning turned off, about a hundredth of a cent per judgment. It sees the schema, the mapped states, and the failing combination, and returns a validated verdict: legitimate gap → add a state (with a name), or impossible → it's a bug. Invalid replies get re-prompted with the rejection reason until the model corrects itself. Accepted states are applied to the live definition — the same combination maps cleanly on the next event.
We Measured It Properly
"It seems to work" isn't a claim we ship. So the repo contains a blinded evaluation: a 75-combination order domain where five ground-truth business rules were written down separately as an oracle the analyst never sees. All 68 unmapped combinations, judged across three arms. The honest headline:
Two findings matter more than the raw accuracy. First, the misses are systematic, not noise: a schema alone can't tell a model your business's rules, and a single neutral paragraph of context nearly triples bug detection — context is a far bigger lever than model size (mini vs. nano barely differ). Second, there's an inferability gradient: near-universal logic like "you can't ship an unpaid order" is largely inferable, while house conventions like "partial refunds only happen after shipping" are essentially unguessable. Those must be written down.
The Punchline: Safety by Construction
A 60%-accurate judge sounds disqualifying — and that's exactly the point of the architecture. We replayed the same 200-event stream twice:
- Blind run: the FSM converges — but over-expands to 74.7% coverage, past the 65.3% of the space that's actually valid. Nineteen impossible states got admitted.
- Guarded run: the rules we know are wired in as one
validate=hook. Rule-violating combinations arrive pre-flagged, and admitting them is structurally illegal — the analyst literally cannot add a state for a rule-violating discovery. Invalid admissions: zero. Not because the model got smarter — because the design doesn't depend on the model being right.
The design thesis, proven: encode what you know deterministically; let a cheap LLM judge only the genuine unknowns; gate what's left with human approval. Every layer covers the one below it.
Spend stays sublinear, too: repeat combinations are deduplicated, so 200 production events cost 65 analyses — front-loaded, then flattening as the machine converges. The whole evidence base, all runs included, cost about two cents.
It's Open Source
GuruCloudAI/sesm on GitHub
pip install sesm — MIT license, one runtime dependency (Pydantic).
The repo ships the framework, the pluggable DiscoveryHandler protocol, the provider-agnostic analyst (bring any complete(prompt) → str callable — OpenAI, Anthropic, a local model, a stub in tests), and the entire proof harness: the oracle, the truth set, the runners, the scored report, the raw per-scenario JSON, and the interactive viewer. Every number in this post is reproducible with three commands.
The Honest Footnote
SESM is an experiment, and the evaluation says clearly what it is not: it is not "let an LLM design your domain model." Blind, a mini model reproduces expert judgment about 60% of the time, and where it misses is exactly where reasonable businesses disagree with each other. What the measurements do support is narrower and more useful: a bounded state space makes the unknown enumerable, a cheap model makes triaging it nearly free, and a one-line structural rule makes the whole loop safe to run unattended. That combination — deterministic rules, cheap judgment, human gates — is the same shape we build into every agent system we ship for clients.
If your systems have lifecycles that keep outrunning their enums — orders, sessions, documents, claims — we build this kind of thing for a living.