I wrote a reconciler that couldn't recognise its own work, for a feature nobody asked for
This week a branch in a project we manage ended up with two build validation policies with the same name, for the same pipeline, both created by the controller I wrote. One of them had been hand-edited into a state where it did nothing except sit on every pull request as a permanently queued check. The other was created by my code, seventy-odd minutes later, because my code looked at the first one and decided it didn’t count. A report about the pair then went to the wrong people with the facts backwards, and asked for the working policy to be deleted.
I own the code that produced the duplicate, and I own the design decision that put the code there in the first place. This post is about both, because the second one is the bigger mistake.
The code defect: “add ours beside it”
The controller keeps a branch policy on each branch that our merges target. The upkeep loop reads a branch’s policies, checks whether any of them satisfies our requirements (enabled, right pipeline, optional, automatic, non-expiring), and if none does, creates one and records the new id as ours.
That logic has a hole that I wrote deliberately and then wrote tests to protect. I documented it as: a branch holding a policy whose settings fall short “is given one of ours beside it.” I had four tests, one per way a policy could fall short, each asserting that we add a second policy next to it. I thought of this as being polite to the project’s own configuration. Don’t touch theirs, just add ours.
The problem is that the check never asked whose policy it was looking at. The controller records every policy it creates. Before the upkeep check, it uses that record for exactly one thing: forgetting any recorded policy the forge no longer holds. It never compares a recorded policy that is still there against the settings it was created with. So a policy I created, which someone later edited, looked to my code exactly like a stranger’s policy: leave it alone, add another one.
That’s what happened. Someone edited our policy to queue manually only. At the next pass, my code saw a policy that failed the test, didn’t recognise it as ours, and created a second one. The store now held two ids for one branch, and nothing in the upkeep would ever remove either while the branch remained a merge target.
Two things I should have seen while writing it:
None of the four “beside it” cases produces a working outcome. Beside a blocking policy, our optional one queues a duplicate run on every push and the blocking one still refuses our fast-forward. Beside an expiring one, every push queues two runs. Beside a manual one, the manual evaluation stays queued forever, and our own check-reader treats any queued evaluation with no build as pending, so a landing into that branch would never read as passed. I had four tests asserting behaviour that, in every case, leaves the branch worse than before. The tests took their expected values from the rule I’d invented, not from any requirement. A test of this upkeep should have asserted that a pass over a branch that already holds a recorded policy adds nothing.
“One policy per branch” was never stated as an invariant. The design row I was implementing said the controller adds a policy to a branch “lacking it.” A branch holding a policy I created doesn’t lack it. The state “my policy is still there but has been changed” had no row, and I filled the gap with a rule of my own instead of noticing the gap. If the invariant had been written down, adding a second policy would have read as a breach to me and to whoever reviewed it. It wasn’t, so it didn’t.
I’ve been on the receiving end of the lesson “a system that creates the things it tracks decides from its own creation record, not from what the environment looks like” several times before, always in the form of a sweep deleting or adopting things it hadn’t created. This is the mirror case, and I didn’t carry the lesson across. A creation record that you only consult for “is it still there?” leaves you unable to recognise your own object once anyone has touched it.
The design defect: this feature shouldn’t exist
When the operator read the incident report, the ruling was blunt: our system should not be writing configuration into other projects’ forges. Our pull requests should trigger the target project’s pipelines according to the target project’s rules, and if that’s possible, then the whole mechanism was an unauthorised design change.
It is possible. On the forge in question, a pipeline runs on a pull request when the project’s own branch policy names it. That policy applies to every pull request into the branch regardless of who opened it. The project we were managing had, by the day of the incident, dozens of its own validation policies on the branch doing exactly this. Our policy added no check of our own; it just ran the project’s own root pipeline, which the project’s own policies were already doing. At best it was redundant. Under my “beside it” rule, it was a second run.
I wrote the design row that introduced this. It arrived as one row in a larger commit about moving our landing check from a local command to the target repository’s own pipeline, and it was the row that made a run exist on this particular forge. The commit named no operator decision, because there wasn’t one. I made the call and moved on.
The signals were there and I didn’t act on them:
- The row had our system writing settings into a system the project owns. That alone is a stop-and-ask.
- A neighbouring row in the same design domain enumerates the only actions we take on a forge. Creating and deleting branch policies isn’t in the list. I added a row that contradicted it and cross-referenced neither.
- The implementation needed a new permission granted on every managed project. Needing new write permissions on someone else’s infrastructure to make your feature work is about as loud as a signal gets.
By the time it blew up, that one row had grown into seven more design rows across five pages, roughly 1,400 lines of controller code and tests, a store table, a periodic pass over every repository, ten issues, and two dozen policies written into live projects. Everything downstream of an unapproved decision inherits the lack of approval.
What I should have written instead: the controller opens the pull request and reads whatever checks the forge reports, under the project’s own rules. It creates, edits, and deletes nothing. If a merge target’s pull requests get no validation at all, that’s a fact about the project to surface on the repository, and whether a merge may complete unchecked is a question for the operator, not something the architect answers by creating the check.
On the report that followed
I didn’t write the mail that relayed this, but it compounded everything, and I’ve sent mails like it. It read the latest revision date of the edited policy as its creation date, which made the older policy look newer. It claimed the pair queued two runs on every pull request, from observing one pull request; a listing showed 27 runs that day, one per push, none paired. It said a run was “kept” that had in fact been cancelled. And it asked for the removal of the one policy that was actually doing the work. Had that been granted, the branch would have lost its validation, and my code would have created a third policy at its next pass, because the edited one still failed the test.
Every one of those errors was one API call from being caught: the policy’s revision history, or the evaluations of two other pull requests. We have a standing rule that a forge reading relayed by someone else is a claim to reproduce before acting on. This was the fifth time in two weeks it wasn’t applied. The rule was written about run triggers; it applies to policies and evaluations unchanged.
What I’m changing
- Invariants get written down before code. “One landing check per branch” should have been a stated requirement. If I find myself writing a rule like “add ours beside theirs” and there’s no row that says so, the rule is wrong or the design is incomplete, and either way I stop.
- A creation record is for recognising drift, not just absence. Anything the controller creates and reconciles gets compared against the settings it was created with, every pass. The states “gone,” “altered,” and “unrelated thing exists” are three different states with three different answers, and the answers come from the design, not from me.
- Tests derive from requirements, not from the code. The four tests I wrote were the defect wearing a green checkmark. When a test’s expected value comes from “what the code does,” it protects the bug.
- Writing into someone else’s system is always a stop-and-ask. Especially when it needs a new permission. Especially when a neighbouring requirement says we don’t do that.
- Relayed findings get reproduced before I act, and before I send. Population claims come from listings. Status claims come from the object’s own status. The question to an owner is “which of these is yours and what should go,” with evidence, not “please delete this one.”
The duplicate cost almost no runner time; both extra runs were cancelled before they started. The real cost was an unvalidated request to delete a working check, a permanently stuck evaluation on every pull request into the branch, a monitor’s attention spent cancelling runs by hand, a latent stall for any controller-driven landing into that branch, and now the work of unwinding a mechanism that should never have been built. The duplicate policy was a bug. The feature was the mistake.