João RamiresInstruction manual

Notebook

Part 5 of 5 · Applied AI engineering

Inside my software factory: from a plan to a merged branch

The first change my software factory ever merged made a slow step of my test setup more than 3,000 times faster: from 654 ms a call to about 0.18 ms. I remember it less for the speedup than for the three things the gates said on the way. The full CI run found a test that broke one of the repository’s rules. The merge tool refused a fix that no reviewer had read. And the review of that fix found that it leaked three database roles, one of them a superuser. Two gates of plain software said no, and a reviewing agent read the part that needed reading.

That is what I want from a factory: not agents that write code, because any agent does that, but a path that code has to walk before it reaches main, and that refuses to let it skip a step. The factory is the part of my setup that moves the work of coding agents through those gates. This is the fifth article of my series on applied AI engineering. The overview, a jail, a factory and an orchestrator, explains the three parts and why they are separate. Here I stay inside the factory: how one change goes from a plan to a merged branch, what each gate checks, and what broke along the way.

The unit is one change

A piece of work is one change written as an OpenSpec change: a proposal, the spec deltas, a design and a task list with a check for each task. OpenSpec is an open-source tool for writing specs next to the code, and I use it because I want to understand a system by reading its specs. The factory adds what OpenSpec has no opinion about: who moves the change, when, and what must be true before it moves.

Every move is one line in an append-only log per project, written only by the software. The status command, the gates and my dashboard all read that one file, so they can’t tell me different stories. A tick of a timer, every 3 minutes, reads the log, fetches what the jail published, checks it and starts the next step. It never retries anything on its own.

From a plan to main

I wanted two things that fight each other. Builders should never wait for a review, and nothing should merge unreviewed. On 2026-10-11 I wrote that each builder was spending a lot of time on reviews, verifications and validations, and that I wanted to build faster while staying secure with a batch review before the merge. The design is the answer to that sentence.

A change gets its own plan and a plan approval. A builder then builds it and checks its own work, and the change waits in a queue instead of waiting for a person. When enough changes wait, the factory groups them into a batch. My first rule was three changes or four hours, whichever comes first. One reviewer then reads the whole batch, so I pay for one deep read instead of three shallow ones. A reviewer who finds a fault should be the exception, and I wrote that down too.

This is the order of a batch, as a project profile states it. I trimmed it to the keys that matter:

JSON
{
  "batch_runs": ["ui-check", "fast_check", "jail_check", "review"],
  "merge_runs": ["archive", "post_archive_check", "merge"],
  "review_critical_paths": ["core/", "datasets/"],
  "merge": ["bin/merge", "--branch", "{name}", "--sha", "{sha}"]
}

The first key lists what runs before the review. A browser check looks at every page the batch changed and has to leave proof: a screenshot or a passing system-test line. A fast check runs on the host, on the tip of the whole batch. A jail check runs the project’s own guards in a fresh jailed session. Only then does the review start, so the reviewer’s time goes to code that already passed the cheap checks. After the review come my result approval, the archive of the change into the specs, a last check and the merge.

What runs where

The factory splits every step by trust. Every subagent runs in the jail: the builders, the UI check, the review and the archive. Everything that decides runs outside it, on my side, in plain Ruby with no model in it: the tick, the fetch, the log, the gate checks and the merge. The orchestrator, my main Claude Code session, also stays on my side, where it plans, briefs and gives the approvals.

An approval is an empty git commit, “Plan approval of X” or “Result approval of X”. The orchestrator makes it under the delegation I gave it, with my words attached, and the gates read the commit, not the agent’s claim. One more rule backs the log, which I added after a review: the factory rejects an approval file its own log never recorded, so a copy forged by a branch can’t pass.

The merge tool is the last place where trust matters, so it is dull on purpose. It runs on the host and merges the branch into a temporary worktree. There it runs the project’s tests, with no shell and no git hooks, because a branch can bring a hook and git would run it. If the tree it tested differs from the tree it merged, it undoes the merge. I choose the host over the jail for this one run because the point is to prove that the merged tree passes where it will live.

What a review has to prove

A reviewer is a fresh agent that did not write the code. It reads the batch read-only and writes a report whose first line is VERDICT: PASS or VERDICT: BLOCK, names the full commit it read and ends on CRITICAL: none. The commit matters most. A review that names the sha it read is a claim a script can check, and a later commit that touches a path the reviewer never saw makes the merge refuse.

That last rule earned its place. A batch passed its review, then a builder merged main into it and resolved a conflict in one Rust file. The merge refused, because the record did not cover that path. A short delta review of that one file passed, and the merge went through. Where a rule can check a fact, I let it.

I learned the rest of the review design from reviews that went wrong in two opposite ways.

A pass that did not read. One review of a three-change batch of about 160 files took 50 seconds and said PASS. Its own report said it ran no Rust tests and read none of the roughly 3,600 lines of startup code that handled the data. The factory accepted it, because the verdict line was there. Now the profile lists the critical paths, and the brief asks for one line per changed area:

TEXT
check: area core/: read
check: area datasets/: tests run: 12
check: area deploy/: not read: no change here

A PASS with no good line for a changed critical area gets the problem review_area_unread. tests run: 0 does not count as reading.

A block that cost a round. One batch needed six review rounds. Rounds four and five said BLOCK with no critical and no high finding, only medium and low notes in documents, and each round cost about 30 minutes plus a fix run. My rule was already that medium and low findings become follow-ups, but the reviewer had never been told. Round six got the grading rule in its brief and passed. A reviewer needs the same rules the orchestrator has, in its brief and not in my head.

Put together, one batch runs like this:

One batch · the shape of a run● done
  1. askThree changes are self-verified and waiting; the factory forms a batch branch
  2. └ toolUI check: a verifier opens each changed page and commits a screenshot
  3. └ toolFast check and jail check on the tip of the whole batch
  4. thinkReview: a fresh agent reads the full diff, read-only, and names the commit it read
  5. checkGate: a PASS must say, for each critical area, read, tests run or not read and why
  6. └ toolMy result approval, then the archive writes the change into the specs
  7. checkMerge: the project tests run on the merged tree, on the host, with no shell and no hooks
  8. answerMerged, with the restarts and chores it created listed for me
3 tool calls · drawn from a profile and the factory's log

What failed, and what changed

The logs are the honest record. The logs of my two projects, the trading system and a small command-center app, hold 838 checks since 2026-10-20, and 224 of them did not pass. Most of those, 142, were a branch held for my confirmation because it touched a protected path, which is the system working. The rest were refusals that stopped something from moving on, for example:

  • 45 times the changes of a batch conflicted with each other,
  • 11 failed checks of the records that prove an approval,
  • 10 reviews that skipped a critical area,
  • 6 reports whose commit did not match the branch,
  • 3 times a batch would have deleted lines from a spec.

The last one is a rule of its own: the factory refuses a merge when the batch deletes a spec line that no change declares as modified or removed.

Three other faults taught me more than the counts.

A narrowed merge check let a fault in. To make the merge faster, one change ran only part of the CI. Its archived notes named a short commit id in a line of prose, which a page test forbids, and the narrowed run never ran that test. The next merge to run the full suite failed after 111 minutes, on text that was not its own, and two more queued merges had to be stopped. Two reviews of a CI-speed change found the same weakness from the other side: choosing tests file by file is fragile when a guard test reads a whole folder. So a merge that touches Ruby, views or assets now runs the whole suite, and only text and Rust get narrowed. Every check a merge skips becomes a debt that the next full run pays, often in someone else’s change.

A merge left chores behind and told no one. A lock-file change needed a new jail toolchain, a change to the bridges needed a reinstall, a reviewed list needed a new baseline. Nobody was told. The health check found all three hours later and turned my command-center app red. Now the merge prints the chores that its changed paths create, in the same minute, and keeps them listed until I close them. A health check that finds them later is the safety net, not the plan.

Two batches from the same main. Two batches, each green on its own, were about to land one after the other. The second would have needed a merge of main and a second CI, while memory for a CI was scarce and runs were waiting hours. The orchestrator joined them after checking, with git, that they did not conflict, ran one CI on the joined tree and landed both with one fast-forward. It saved a run, and it tested the two changes together, which two separate runs never do.

How it builds my other projects

The factory knows no project name. A project supplies a profile: its approval command, its merge command, its critical paths, its page rule, its checks. The trading system was the first customer, and its own scripts did the work first, so moving a step into the factory meant copying its tests and its planted-fault list with it. I moved one part per change, and kept main of both repositories green after each move.

The trading system adds two things the generic factory does not have. Its review record is a file in git, so a hostile branch could copy one, and it keeps a second list outside the repository, in a folder the jail can’t write. The merge accepts a record only when the list has a line for exactly that file. And its merge script runs the whole CI, a run long enough that a capacity gate decides when it may start, so it can’t starve the machine.

The second customer, Beholder, is a small command-center app, and it brings its own profile and its own log. The overview gives the totals across both logs, including the merge runs that failed and never reached main.

What I’d tell someone building the same

  • Make the unit of work small and written down. The factory moves one change, and every gate reads the same record of it.
  • Let builders queue and let one reviewer read a batch. A review for each change made my builders slow.
  • Run the agents in a jail and the gates outside it. A model should never decide whether it may pass.
  • Make a review say what it read. A verdict line alone looks the same as a review that did nothing.
  • Put the grading rule in the reviewer’s brief. Otherwise it blocks on notes and costs you rounds.
  • Never narrow a check where the link from file to test is uncertain. Run the whole suite and make it faster.
  • Print the chores each merge creates, the same minute.
  • Move a system into the factory one part per change, and keep main green after each move.