Loops replaced the state machine

On the morning of 24 July, an engineer on another team got around ten messages from our AI in one hour. Each message said there was nothing left to do. Every one was about a ticket that had shipped over two months earlier.

The AI was Nex, the software factory our new-products engineering department runs on. It is one of several products we build, and I built it. Nex takes a ticket, a GitHub issue that describes one change, and carries it to a merged pull request. Each ticket gets its own conversation thread. Every time Nex wakes on that thread, it runs one task in a fresh cloud VM: plan, implement, test against a live environment, or merge. The task replies and exits. The chain of tasks from plan to merge is the ticket loop.

The hard question in a ticket loop is who decides which task runs next. In July the answer was a state machine, and I was proud of it.

The first version, built in June, read GitHub on every event to work out where a ticket stood. It ran into GitHub's rate limits and flagged new pull requests as stuck because their checks had not appeared yet. On 18 July we rebuilt it as a cleaner machine. Every ticket had a recorded stage: planning, implementing, testing, waiting for acceptance, or landing. Events went in, one function was the only code allowed to change a stage, and tasks came out. GitHub was read only by a periodic sweep and at the merge. The rebuild removed around 29,000 lines and added around 14,000.

Who decides which task runs next, before and after August
Who decides which task runs next, before and after August

Real use disagreed within days. When a person queued an implementation task by hand, the machine never saw it, so the ticket sat in planning with a finished pull request and every check green. A ticket spanning two repositories had no single pull request to watch. After both merged, the machine dispatched another implementation task every ten minutes, eighteen times, until someone stopped it. Then the machine's records were wiped and rebuilt for every open thread in production, and nothing in that rebuild knew which tickets had already shipped. Over a hundred loops came back to life on finished tickets and queued over a hundred tasks that morning. Each one told its owner there was nothing to do.

These bugs all had one shape. The machine kept its own copy of where each ticket stood, and every gap between that copy and reality became a stall or a spammed engineer. The agent that had just written the plan knew the plan was done. The agent that had just pushed the code knew whether it was ready to test. The agent that saw a test fail knew the code needed another pass. The machine rebuilt each of those facts from outside, and the agent already held them.

So on 3 August we deleted the machine. Each task now queues the next one before it exits. Planning queues implementation, and implementation queues testing. A failed test queues another implementation pass. A passing test waits for acceptance, from a person or from a standing rule that already covers the ticket. Acceptance wakes a landing task, which merges the pull request and declares the loop done. The change removed close to 40,000 lines and added around 7,000. Sixteen days after the rebuild I was proud of, most of it was gone.

The loop stayed as a thin owner of what an agent in a disposable VM cannot hold. It claims the ticket, so two threads cannot work on the same one. It delivers events such as a failing CI check, a review comment, or a merge conflict as a wake on the thread. It lets the agent name the event it is waiting for, such as a deploy finishing, and sleeps until that event or a deadline arrives. If a task exits with nothing queued and nothing awaited, the loop wakes the thread once to recover. It runs the final merge gate. What to do next stays with the agent.

Once the loop was that small, it stopped being specific to tickets. A custom loop is a thread with a name and a one-line goal, woken by the events that goal cares about until the agent declares the goal met. Our daily production release is one. Each day's loop decides whether to release, deploys, and judges the result at thirty minutes, one hour, and two hours.

A milestone loop carries work too large for one ticket. It owns a GitHub milestone, starts the child tickets, and leaves each child's plan, code, tests, and merge to that child's own ticket loop. When a child finishes, the milestone loop checks the result against the milestone's goal, files a new ticket for anything missing, and decides which children can start next.

Tickets closed each day by the architecture drift milestone loop
Tickets closed each day by the architecture drift milestone loop

That shape proved itself in October. Our first architecture drift review found sixteen classes in the Nex codebase over 1,000 lines. We made that size fail the build, gave each offender an exemption naming the ticket that would shrink it, and opened one milestone for a milestone loop to drive. Over the next week it closed around seventy-five tickets, from class splits and module moves to the build rule itself, and the last exemption went with them. I did not schedule a single split.

Those sixteen days taught me where the knowledge lives. A scheduler outside the work has to guess what happened, and each wrong guess cost us a stalled ticket or a spammed engineer. The agent that just did the work already knows what comes next, so it decides. The platform keeps only what that agent cannot carry from one disposable VM to the next.