Your screen is already a state machine. Make it say so
Most app screens run on a pile of loose flags: isLoading, hasError, isRetrying. Together they form a state machine nobody wrote down. Here is the agent pass we use to find those hidden machines, pin current behavior with tests, and fold the flags into one named state without breaking anything.
Open almost any screen that loads data and you will find a small cluster of booleans near the top. isLoading. hasError. isRetrying. Maybe isEmpty, added the week someone noticed a blank list looked like a crash. Each flag was a reasonable fix on the day it landed.
Together they are a state machine. Nobody designed it, nobody drew it, and the code does not say which combinations are allowed. Four booleans can be in sixteen combinations. The screen means maybe five of them. The rest are where the strange bug reports come from: a spinner sitting on top of an error message, a retry button on a list that loaded fine, an empty state that flashes for a frame before the data arrives.
We build production apps and agent systems, and a lot of that code is now written with coding agents. That makes this pattern more common, not less. So we run a specific pass to find these hidden machines and make them explicit. This note walks through it.
Why agents grow flags
Ask a coding agent to "show an error if the request fails" and the smallest correct change is a new boolean. Set it in the catch block, render a message when it is true. Ask next week for a retry button and you get another boolean. Each request is handled locally, each diff is small and easy to approve, and the reviewer sees nothing wrong.
The trouble is what no single diff shows. Every new flag doubles the number of combinations, and no one checks the new ones. A person who has lived in the codebase for a year carries a rough map of which combinations are real. An agent opening the file cold does not. It sees four independent switches and treats them as independent, because nothing in the code says otherwise.
That is the part we care about most. Loose flags hurt users, and they hurt the developers reading the screen, but they also make the next agent session worse. The code is the only briefing that agent gets, and right now the briefing is wrong.
The pass, in five steps
A one-line prompt asking an agent to "find the implicit state machines in my app and fix them" is a good starting idea. On its own it is too open. The agent will find things, rewrite a lot, and hand back a diff too big to trust. We break it into steps, and each step produces something a person can read before the next one starts.
1. Inventory. The agent lists every place where several variables together describe what mode a screen or component is in. Booleans are the obvious ones. Nullable fields count too: an error string that is sometimes null, a data array that is sometimes undefined, a selectedId that only matters in edit mode. The output is a list per component, with file and line for each variable. No code changes yet.
2. Reachability table. For one component, the agent writes out the combinations that can actually occur and names the code path that produces each one. Then it lists the combinations that are possible in the type system but should never happen. This table is the most useful artifact in the whole pass. It turns a vague feeling ("this screen is messy") into a short, checkable claim ("five real states, eleven impossible ones, and here is the line that can produce impossible combination seven").
3. Pin today's behavior. Before touching the logic, the agent writes characterization tests for what the component does now. Not what it should do. What it does, including the odd parts. If the current code briefly shows the empty state before the data arrives, there is a test that says so. These tests are the safety net for the next step, and they make behavior changes visible later.
4. Fold into one named state. The flags become a single value with a small set of named cases. In TypeScript that is usually a discriminated union. In Swift or Kotlin it is an enum or sealed class whose cases carry their own data. Transitions move into one function that takes the current state and an event and returns the next state. The retry flag usually disappears. It becomes an attempt count carried by the loading case, so "retrying" is just loading again with a number attached.
5. Rerun and explain every difference. The characterization tests run against the new code. Any test that now fails is either a bug the refactor fixed or a regression it introduced, and a person decides which. Deliberate fixes get their test updated in the same PR with a one-line note on why. Everything else must pass unchanged.
What changes for users, developers, and agents
The original prompt that inspired this pass asks for a change that improves three things at once: the user experience, the developer experience, and the agent experience. We think that framing is right, and it is a useful test for whether a refactor was worth doing.
Users stop seeing impossible screens. If the state type has no case where loading and failed are both true, the screen cannot render a spinner over an error. The fix is not a guard clause someone has to remember. The bad combination simply cannot be written.
Developers get one place to read. Instead of tracing four setters across three handlers to work out what the screen is doing, they read one type and one transition function. Adding a feature means adding a case or an event, and the compiler points at every switch statement that now needs to handle it.
Agents get a correct briefing. A named state with a transition function is a map the next session can follow. Ask an agent to add offline support to a screen with explicit states and the natural change is an offline case plus the transitions into and out of it. Ask the same of a flag-based screen and the smallest change is a fifth boolean. The explicit version also gives the agent something to verify against: the transition function is easy to test exhaustively, so the agent can prove its change instead of eyeballing it. That fits the rule we already hold for agent work, which is evidence before merge.
Guardrails we keep
This pass touches code that users see every day, so the boundaries matter more than the cleverness.
One component per PR. The inventory can cover the whole app. The refactors ship one at a time. A reviewer can hold a single reachability table and a single state type in their head. They cannot hold twelve.
Refactor and behavior change stay separate. If step five turns up a real bug, the default is to keep the old behavior in the refactor PR and fix the bug in a follow-up. That keeps each diff honest about what it is. The exception is a bug that only exists because of an impossible combination, since the new type removes it by construction.
The reachability table goes in the PR description. It is the reviewer's checklist. For every impossible combination listed, the reviewer can confirm one of two things: the new type makes it unrepresentable, or a test covers why it cannot occur.
A person decides what was a bug. The agent is good at finding odd behavior and bad at knowing whether the odd behavior was intended. A flash of empty state might be a bug. It might be a product decision made two years ago for a reason nobody wrote down. The agent flags it and asks. It does not decide.
Not everything becomes a state machine. A single toggle stays a boolean. A form field stays a field. The pass targets places where two or more variables together encode a mode, and where some of their combinations are meaningless. If every combination is valid, the flags are fine as they are, and the agent should say so and move on.
How we start on a codebase
We do not run the whole pass on day one. The first session is inventory only, across the whole app, written to a file. We read it, pick the component with the most user-facing bug reports or the worst reachability table, and run steps two through five on that one. The rest of the inventory stays in the repo as a backlog the next sessions can pick from, which also means no agent has to rediscover it.
The first refactor usually takes the longest, because it sets the house pattern: how states are named, where transition functions live, how tests are laid out. After that we add the pattern to the project's agent instructions, and later components follow it without being asked. That is the same move as shipping know-how as skills rather than repeating it in every prompt.
The prompt that started this is one sentence. The value is in what surrounds it: a table a person can check, tests written before the change, one component at a time, and a human deciding what counts as a bug. The hidden state machine was always there. The pass just makes the code admit it.