I wrote a cleanup job that deleted other people's work
Earlier this week I shipped a change to our controller that automates promotions through pull requests. Part of that change was a housekeeping sweep: whenever the controller reads a repository’s branches, it cleans up promotion branches that no longer have a promotion behind them. Within half an hour of deploying it, the sweep had deleted roughly sixty branches it had no business touching, and over the following day it abandoned two pull requests that people were actively waiting on. One team spent an hour trying to work out who had killed their release before concluding it must have been a machine and filing a high-priority bug. Everyone else quietly renamed their branches to route around the controller.
This post is about what I got wrong, because I got several distinct things wrong and each of them would have been enough on its own.
What I built
The controller creates promotion branches under a particular naming scheme, and the design said the controller should delete its own branches once their pull request is complete or abandoned, plus sweep up any leftovers it finds. I implemented the sweep as: any branch whose name starts with our promotion prefix, and which isn’t in the controller’s current set of in-progress promotions, is a leftover. Delete it.
I also added a step the design never mentioned. Before deleting a branch, the sweep looked up any pull requests open from it and abandoned them, on the reasoning that a PR from a branch that’s about to vanish is just going to break anyway. I thought I was being tidy.
What I didn’t account for
People already used that prefix. Operators and long-running agents had been promoting by hand for a while, pushing branches named with the same prefix followed by a date, opening a PR, and waiting for review. That’s the convention everyone had settled on, and it predates my code.
The sweep had no way to tell those branches apart from its own, and it didn’t try. Its “in progress” set only ever contained promotions the controller itself had started, so by construction every hand-made branch looked like an orphan. On the first pass it deleted all of them across every repository it watched. Then, as people pushed new ones, it kept going: one PR was abandoned nine seconds after it was opened, another twenty seconds after, with its build cancelled.
To make it worse, the controller authenticates to the forge using the operator’s account. So from the outside, what people saw was the operator personally abandoning their pull request with no comment. The deletion was logged as a routine info-level event. The PR abandonment wasn’t recorded anywhere on our side at all.
The mistakes, separately
I matched by name instead of by ownership. The design’s own justification for the sweep was “a branch the controller creates is the controller’s to delete.” I turned that into “a branch that looks like one the controller creates.” Those are not the same thing, and a prefix is not a claim of ownership. The controller keeps records of every promotion it starts; the sweep should have consulted those records and deleted only branches that appear in them. Anything else under the prefix is, by definition, someone else’s work.
I added an irreversible action that wasn’t in the design. Deleting a branch is bad but recoverable: the commits still exist and a push restores it. Abandoning a pull request throws away review state, approvals, auto-complete settings and a running build, and it does so visibly under someone else’s name. Nobody asked for that. I added it because it seemed like a natural consequence of the deletion, and I didn’t stop to notice that “natural consequence” was doing a lot of work. When an implementation needs a destructive write the design doesn’t state, that’s a design question, not a detail to fill in.
I made the loud part quiet and the quiet part loud. A deletion got an event. An abandonment, performed under the operator’s identity on something the controller didn’t create, got a log line and nothing else. That’s backwards. Any action that will read to the rest of the organization as a human doing something needs to be recorded as an event and should raise an alert, so that the first person to see it is us and not the person whose work just disappeared.
I didn’t push back on the design when I should have. The design itself was loose: the prose named the exact branch shape the controller creates, but the deletion rule was written over the whole prefix. I noticed the gap and resolved it in the direction of the broader rule, because it was simpler to implement. The right move was to flag that the two statements didn’t agree and ask which one was meant. Had I asked, the answer would have been obvious to anyone who’d promoted by hand recently.
What I should have done
- Match the controller’s own branch shape and require a matching promotion record. No record, no deletion.
- Treat an unrecognized branch under the prefix as a signal, not garbage: raise a warning that names the branch and says the controller is leaving it alone.
- Only abandon pull requests the controller itself opened, and record every abandonment as an event with the PR and the reason, the same way every other forge write is recorded.
- Before deploying anything that deletes by pattern, actually look at what currently matches the pattern. Sixty branches across a dozen repositories would have been a very fast conversation.
What I’m changing
The sweep is being reworked along the lines above, and the design is being amended to say explicitly what the controller may delete, what it may abandon, and what it must report instead. I’m also writing myself a rule I intend to apply at review time, mine and others’:
Anything that deletes, abandons or cancels something on a shared system based on a name pattern alone, with no record that we created it, gets rejected. “Ours to delete” means we made it, not that it resembles something we’d make.
I’m sorry to the people whose releases I interrupted and who lost an hour hunting for a culprit that turned out to be my code running as someone else. The fix for the branches is straightforward. The fix for the habit of mind that produced them is the part I actually need to work on.