Give the agent a transition table, not a paragraph: order lifecycles that reject every illegal move
A status workflow written as prose gets implemented as a switch that says yes to everything. Written as a data table, the same spec generates its own exhaustive tests. About 20 minutes to try.

Ask an agent for “an order that moves from draft to placed to paid to shipped to delivered, and can be cancelled before it ships”, and the first draft looks fine. It has a switch on the event, a case per verb, and a default that returns the current state. The happy path passes. Then someone calls ship on a draft order and it ships.
The problem isn’t the agent’s competence. The prose spec says what can happen. It never says what can’t, and a model filling in code from prose fills the gaps with the most permissive reading. A status workflow is a table, so write it as one: a list of legal (from, event, to) rows in a TypeScript file, with everything not in the list illegal by definition. Then derive the tests from the table, so the agent can’t satisfy them by being vague.
Nothing here depends on a particular agent. Any tool that can read files and run npx vitest can do the loop below, and the check that proves it worked is the test run.
The spec as data
Seven states, six events, eight legal moves. Here’s the whole spec:
// src/spec.ts
export const STATES = ["draft", "placed", "paid", "shipped", "delivered", "cancelled", "refunded"] as const;
export const EVENTS = ["place", "pay", "ship", "deliver", "cancel", "refund"] as const;
export type State = (typeof STATES)[number];
export type OrderEvent = (typeof EVENTS)[number];
// One row per legal move. Anything not listed is illegal.
export const SPEC: readonly { from: State; on: OrderEvent; to: State }[] = [
{ from: "draft", on: "place", to: "placed" },
{ from: "draft", on: "cancel", to: "cancelled" },
{ from: "placed", on: "pay", to: "paid" },
{ from: "placed", on: "cancel", to: "cancelled" },
{ from: "paid", on: "ship", to: "shipped" },
{ from: "paid", on: "cancel", to: "refunded" },
{ from: "shipped", on: "deliver", to: "delivered" },
{ from: "delivered", on: "refund", to: "refunded" },
];
export const TERMINAL: readonly State[] = ["cancelled", "refunded"];
Two rows are worth a second look, because they’re the ones a prose spec blurs. Cancelling a paid order lands in refunded, not cancelled, because money has to go back. And once an order has shipped, cancel isn’t a row at all, so the only way out is delivery and then a refund.
This file is the thing a human reviews. A change to the workflow is a change to this table, and the diff is eight lines that a product owner can read. Everything else follows from it.
The tests come from the table
The test file doesn’t list expected behaviour by hand. It walks every pair of state and event, looks the pair up in the spec, and asserts either the target state or a rejection.
// test/table.test.ts
import { describe, expect, it } from "vitest";
import { EVENTS, SPEC, STATES, TERMINAL } from "../src/spec";
import { transition } from "../src/impl";
const legal = new Map(SPEC.map((r) => [`${r.from}/${r.on}`, r.to]));
describe("every state and event pair", () => {
for (const from of STATES) {
for (const on of EVENTS) {
const to = legal.get(`${from}/${on}`);
if (to) {
it(`${from} + ${on} -> ${to}`, () => {
expect(transition(from, on)).toEqual({ ok: true, state: to });
});
} else {
it(`${from} + ${on} is rejected`, () => {
expect(transition(from, on)).toEqual({ ok: false, from, on });
});
}
}
}
});
it("terminal states have no way out", () => {
for (const s of TERMINAL) {
for (const e of EVENTS) expect(transition(s, e).ok).toBe(false);
}
});
it("the spec itself has no duplicate rows", () => {
expect(legal.size).toBe(SPEC.length);
});
Seven states times six events is 42 pairs, plus the two extra tests, so 44 in all. Eight of them assert a move. Thirty-four assert a refusal. That ratio is the point: the refusals are where agent-written state code goes wrong, and they’re the cases nobody writes by hand because they’re boring.
The return type is a result object and not an exception. A rejected move is an ordinary outcome of a web request, so the caller gets { ok: false, from, on } and decides whether that’s a 409 or a log line.
// the contract the tests expect from src/impl.ts
export type Result =
| { ok: true; state: State }
| { ok: false; from: State; on: OrderEvent };
What the first draft does
Give an agent the spec file, the test file and the contract above, and tell it to make src/impl.ts pass. Before that, it’s worth seeing what a prose-driven draft looks like against these tests. This version is the shape a typical first attempt takes: short, readable, and permissive.
// src/naive.ts
export function transition(state: State, event: OrderEvent): Result {
switch (event) {
case "place": return { ok: true, state: "placed" };
case "pay": return { ok: true, state: "paid" };
case "ship": return { ok: true, state: "shipped" };
case "deliver": return { ok: true, state: "delivered" };
case "cancel": return { ok: true, state: state === "paid" ? "refunded" : "cancelled" };
case "refund": return { ok: true, state: "refunded" };
default: return { ok: false, from: state, on: event };
}
}
It switches on the event and ignores the current state, except in one place. The default branch looks like error handling but can never run, because the event type has no other members. Pointed at the table tests, it prints:
Test Files 1 failed (1)
Tests 35 failed | 9 passed (44)
Thirty-five of 44 fail. A draft order can be paid, shipped and delivered without being placed. A cancelled order accepts refund. A shipped order can be cancelled, and it comes back as cancelled with no refund. A hand-written happy-path suite of five or six tests would have passed this code.
Each failure names the pair, for example draft + pay is rejected or shipped + place is rejected, so the agent has a concrete line to fix, and so do you when you read the run.
The implementation that passes
The fix isn’t more if statements. It’s to stop encoding the workflow in control flow at all and look the answer up in the spec.
// src/impl.ts
import { SPEC, type OrderEvent, type State } from "./spec";
export type Result =
| { ok: true; state: State }
| { ok: false; from: State; on: OrderEvent };
const table = new Map<string, State>(SPEC.map((r) => [`${r.from}/${r.on}`, r.to]));
export function transition(from: State, on: OrderEvent): Result {
const to = table.get(`${from}/${on}`);
return to ? { ok: true, state: to } : { ok: false, from, on };
}
Twelve lines. Run npx vitest run and npx tsc:
Test Files 1 passed (1)
Tests 44 passed (44)
tsc under strict exits clean. If the agent reaches for this shape on its own, good. If it writes the switch again, the table tests send it back, and you can add one sentence to the prompt: “implement transition by looking up SPEC; don’t branch on state names”.
Two properties the table can’t express
A per-pair table checks single steps. Two things only show up across sequences, and both fit in a short file. Reachability catches a spec typo that makes a state unreachable (a row with placd wouldn’t compile, but a row pointing at the wrong existing state would). A random walk catches an implementation that keeps a private flag and lets a terminal order wake up.
// test/walk.test.ts
import fc from "fast-check";
import { expect, it } from "vitest";
import { EVENTS, SPEC, STATES, TERMINAL, type State } from "../src/spec";
import { transition } from "../src/impl";
it("every state is reachable from draft", () => {
const seen = new Set<State>(["draft"]);
for (let grew = true; grew; ) {
grew = false;
for (const r of SPEC) if (seen.has(r.from) && !seen.has(r.to)) { seen.add(r.to); grew = true; }
}
expect([...seen].sort()).toEqual([...STATES].sort());
});
it("a random walk never leaves a terminal state", () => {
fc.assert(
fc.property(fc.array(fc.constantFrom(...EVENTS), { maxLength: 30 }), (events) => {
let s: State = "draft";
let done = false;
for (const e of events) {
const r = transition(s, e);
if (done) expect(r.ok).toBe(false);
if (r.ok) { s = r.state; done = TERMINAL.includes(s); }
}
}),
);
});
With both files in place the run reports 2 test files and 46 tests, all passing. The walk test is cheap insurance for the day someone adds a persistence layer, caches the state in a field, and the table-driven function stops being the only writer.
Making the compiler do some of the work
The table catches wrong behaviour at test time. Two small type choices catch some of it earlier.
First, STATES and EVENTS are as const arrays, and State and OrderEvent are derived from them. A row with a misspelt state fails tsc, so the agent can’t invent a state in the implementation without adding it to the spec first. Second, any function that switches on State elsewhere in the codebase (a label renderer, a permissions check) can end with an exhaustiveness guard:
function assertNever(x: never): never {
throw new Error(`Unhandled state: ${String(x)}`);
}
export function label(s: State): string {
switch (s) {
case "draft": return "Draft";
case "placed": return "Awaiting payment";
case "paid": return "Paid";
case "shipped": return "On its way";
case "delivered": return "Delivered";
case "cancelled": return "Cancelled";
case "refunded": return "Refunded";
default: return assertNever(s);
}
}
Add an eighth state to the spec and label stops compiling until someone decides what to call it. The agent finds out from the compiler, not from a user.
Make it yours
For C#, the same shape works with an enum pair and a FrozenDictionary<(State, Event), State> built from a spec array, with an xUnit [Theory] fed by MemberData that yields every pair. The compiler’s exhaustiveness check on switch expressions covers the label case. The idea doesn’t change: spec as data, tests generated from it, implementation a lookup.
When a move needs a guard (an order can only ship if stock is allocated), keep the table pure and put the guard in a separate predicate keyed by the same pair. Add a column to the spec rather than an if in the transition function, and generate a test per guarded row. The table stays the single place to read the rules.
If the workflow is long-lived and the states live in a database, add a test that loads every distinct status value from a fixture and asserts it’s in STATES. A status string written by an old release is a bug the type system can’t see.
What goes wrong
The tests prove the code matches the table. They don’t prove the table matches the business. If the spec says a delivered order can be refunded and finance says it can’t, every test passes. That’s why the table is the file a human reads.
Generated tests can also be generated too cleverly. If the agent is allowed to edit test/table.test.ts, it can make a failing pair disappear. Treat the spec and the test file as read-only for the implementation task, or review any diff to them first.
And a pure transition function says nothing about concurrency. Two requests that both read paid and both try ship and cancel will each succeed in isolation. That needs a version check or a conditional update in the database, covered in the ETag post
.
Don’t bother with any of this for a workflow of three states and no illegal moves worth caring about. A boolean and an if is fine. The table earns its keep from about five states, or the first time a bug report says “how did it get into that status”.
The first 20 minutes
Copy spec.ts and table.test.ts into a fresh folder, run npm i -D vitest typescript fast-check, and write impl.ts yourself in a minute so the tests go green. Then swap in your own workflow: replace the states, events and rows, and read the failures. Next time you ask an agent for a status change, hand it the table and the tests first and the prose second.