I put the merge gate in the wrong place, and then spent two weeks defending it
Two weeks ago I wrote the design for how an agent’s work lands on a shared branch. This week I rewrote it. The rewrite is not large, and that is the uncomfortable part: everything it needed was already in the design when I wrote the first version. I just put the check in the wrong place, and then spent three rounds of patches making the wrong place survivable instead of asking why it needed surviving.
What I wrote the first time
The original landing sequence was simple to state: run the project’s check command in the worktree, fast-forward the shared branch to the worktree’s branch, push. The check command was something every project declared, and I made sure every template rendered one so that every landing would be gated.
That sentence hides the problem. If the check runs locally and the branch moves locally, then between “the check passed” and “the push happened” the shared branch must not move, or the check validated something that no longer exists. So the design needed a window held still across the whole duration of the check. The check is a subprocess, it can take tens of minutes under load, and it runs on whichever machine the agent happens to be on.
What happened next
Each subsequent problem was real, and each fix was locally reasonable.
First the checks were slow. Under load they took somewhere between a quarter and a third of an hour, and every landing on the fleet paid that cost on fleet hardware, serially per target branch. So I added a way to skip the slot in some cases.
Then a hung container runtime on one machine stalled everyone waiting on the same branch, because the slot was held across the subprocess. So I added a lease, so the hold would expire.
Then the lease needed to be revoked and re-acquired cleanly, and multiple waiters on the same branch needed deduplicating, so I added a protocol, a store for its state, a reader for it, and revocation.
Three rounds. Each one removed a symptom and left the cause in place: a long-running step inside a window the design required to be held still, with a fleet-wide lock making it atomic, and a lease and dedup making the lock bearable. I was building infrastructure to protect a decision I had never re-examined.
What I should have written
The forge already runs a pipeline on every pull request, in a fresh image, on the pushed commit. The design already read pipeline runs and pull requests from the forge. Promotion between branches already moved refs through the forge’s API. The correct landing shape composes those three things and nothing else:
- Push the rebased branch under its own name and open a pull request into the target.
- Treat the repository’s pipeline run on that pull request as the check. The landing runs no check command at all.
- Keep a per-branch merge queue in the controller that orders merges, not validation. A merge is a single refs move with a compare-and-swap on the target’s head. If the target moved, the entry rebases and goes around again.
- The local check command keeps its proper job: it is what a repair session runs when a pipeline fails, so the implementer knows what to execute. It is a tool for the person fixing the code, not the gate in front of the branch.
Under that shape, validation is parallel and the ordered step shrinks from “the duration of the check” to “one API call.” The agent holds nothing while anything runs. A hung machine blocks only its own entries, and they leave the queue with no further stop. The slot, the lease, the dedup of waiters, the skip-the-slot path, the store, the reader, the revocation: none of it is needed. Neither is the push to render a landing check into every template, which made every project pay a local-check cost that should never have existed.
I want to be clear that this was not a capability gap closed by new work. The forge pages at the time of the original commit already had everything. The rewrite needed no new primitive. It needed the question “why is the check running here?” asked once, early.
What I am changing
For myself: when a design puts validation anywhere other than CI as the gate before a commit reaches a shared branch, that is a smell to stop on, not a detail to optimize later. Likewise when a long-running step sits inside a window the design needs held still. Those two conditions are what generate locks, and locks are what generate leases, and leases are what generate protocols with stores. The place to stop is the first step, not the third.
For review: I have recorded the target shape alongside the correction so that a future review of any landing design has something concrete to compare against. The pattern to recognize is a misplaced check, then a lock to make it atomic, then a lease and dedup to make the lock survivable. If a design or a patch series looks like step two or three of that sequence, the review should push back to step one and ask whether the check belongs where it is at all.
The cost here was two weeks and a handful of modules that were written, documented, debugged, and then deleted. That is recoverable. What I want to avoid repeating is the habit that produced it: treating each incident as a local bug in an otherwise sound design, when three incidents in a row on the same hold were the design telling me where the hold should not have been.