My Retries Failed Because of What My First Attempts Left Behind
I wrote the code in our agent that lands work, removes worktrees and records commits. I wrote all of it as if each operation would run once, from start to finish, with nothing getting in the way. That doesn’t hold in production. Agents restart for updates. Deadlines expire. Runners fail. Each time an operation stopped partway, the next attempt found the state the first attempt had left and treated it as something it didn’t recognise. Then it failed the same way every time after that.
A stop that should have been temporary became permanent, and my own code caused it.
What I got wrong
Landings weren’t drained on shutdown. I ran landings as background tasks and never waited for them to finish. When the agent restarted, for a new executable version or a configuration change, a landing could be cut off between two git commands. The next attempt then saw a rebase in progress, read it as uncommitted changes, and kept the worktree for good. The reason it logged described the symptom, not the cause.
Abort only ran on one kind of failure. I called rebase --abort when git exited with a non-zero code. When the runner itself failed and the error was passed up the stack, the abort never ran, and the half-finished rebase stayed in place.
A partial success looked like someone else’s work. If an attempt pushed a copy’s work and then stopped, the next attempt saw a state the landing itself had created and refused it as foreign.
Records were written against the wrong parent. A record commit made against a parent that sat beside its recorded commit, not under it, left every later attempt seeing a commit the remotes didn’t have. That worktree could never land, and it failed again on every retry.
My recovery step caused the problem it was meant to fix. To restore a submodule, the landing reset it to a recorded commit, which detached it. The next attempt then refused to land because the submodule was detached. I had never asked what state my recovery code leaves behind.
Combining attempts moved history backwards. When two attempts’ record commits were rewritten together, a pinned reference ended up pointing at an older commit in the shared history. Neither attempt moved the pin backwards when it was recorded, so a check at the record step alone would not have caught it.
I didn’t plan for recovery failing. After a rejected push I ran reset --keep, and that refused too because of the index state. The main checkout was left ahead of its upstream, and every pull after that stopped with Not possible to fast-forward.
Failed setup left leftovers. When creating a worktree failed, the half-made worktree stayed on disk, and every later start refused it as in use. That happened over and over.
I put a guard on one entry path, not on the state. I added a check so that a kept worktree wouldn’t be landed while its issue was still open. It only ran on one of the paths into the operation. An attempt through another path filed a duplicate conflict issue.
What it cost
Issues were put on hold after repeated failures, for states no session could clear. Worktrees were kept forever. Duplicate conflict issues were filed. Shared history ended up with a pin moved backwards. Machines were held back. Releasing a hold just triggered the same failure again, so people had to land the work by hand through pull requests. I had built a system that couldn’t recover on its own from its own interruptions.
What I should have written
-
Make every multi-step operation resumable. Before taking a step, record which step you’re about to take. When an attempt starts, it should first look for the state its own earlier attempt would leave at each step, then either continue from there or undo it cleanly. “Uncommitted changes”, “detached”, “in use” and “ahead of upstream” should each be checked against one question before they stop anything: did I do this?
-
Drain in-flight work on shutdown. Any step that holds a lock or could leave a repository mid-command has to finish or be safely aborted before the process exits. All failures take the same abort path, whether git exits non-zero or the runner fails.
-
Plan for recovery steps failing.
reset --keep,rebase --abortandworktree removecan all refuse. For each one, the code has to say what happens next. If it doesn’t, the recovery path can just leave new damage behind. -
Put guards on the state they protect, not on one caller. If an invariant matters, enforce it where the state is checked so that every entry path gets it.
-
Test the interruption, not just the happy path. For each operation, I’ll write tests that run it, stop it between steps, run it again, and check that the second run lands. A test that runs once from a clean fixture shows the code works when nothing goes wrong, and that was never the case that hurt us.
How I’ll review this from now on
For every new multi-step repository operation, I’ll ask: if the agent dies after this step, what do the worktree, the submodules and the main checkout look like? Does the operation’s first step recognise each of those states? If the only tests start from a clean fixture and run once, the change isn’t done. It needs a test that runs the operation twice with a stop in between.
I’m sorry for the hours this cost the people who had to clean up after it by hand. The fix is clear: an operation’s first attempt is never guaranteed to be its last, so every step has to be written with a possible retry in mind.