Postmortem: My Validator Banned Something Because It Couldn't Recognize It, and My Fix Made It Worse
For about a day, an architecture validator I was responsible for rejected code that followed the architecture correctly. When I fixed it, the fix replaced one made-up constraint with a narrower one. Neither version came from the design document. I wrote both of them into the tool, and I also misdescribed the problem to the person who owns the design. This post covers what happened, why each step was wrong, and what I’m changing.
The rule I was checking
Our app uses a layered architecture. Its rules are written down as numbered statements, and we keep a set of validators that check code against them. One of the rules is about initializers, the functions that build IOs, services and the aggregators that hold them. In short, the rule says:
An initializer’s parameters are IO and service interfaces and aggregators, config types, implementation enums, and resource objects (such as a framework handle, or a lazy initializer passed between initializers), and nothing else.
A resource is something an IO needs that is opened once and shared, like a database connection pool. A neighbouring statement says a resource that is not lazy gets passed straight to the constructor that needs it. The prose on that page gives the condition for laziness: a heavy resource, meaning one that takes real time to initialize, is opened lazily so you only pay for it if something uses it.
So the design says three things:
- Resources are allowed as initializer parameters.
- Both lazy and non-lazy resources exist.
- Laziness is for heavy resources.
The parameter rule mainly exists to stop plain values, like a host name or a file path, from being passed to an initializer outside a config type.
Version one: “can’t recognize it” became “forbidden”
The validator could recognize four of the five permitted kinds from the code: interfaces, aggregators, config types and enums. It had no way to recognize the fifth kind, a resource object. In the logic I signed off on, any parameter whose type the project itself declares and that wasn’t one of the four recognized kinds was reported as a violation.
That meant a resource type declared inside the project could never pass. Resource types from third-party libraries got through only because the check didn’t know their names at all.
Overnight, the teams moving code onto the architecture hit this with every resource they declared:
- 5 findings on initializers that took lazy initializers as parameters. That is the exact example the rule’s own text gives as allowed.
- 12 ignore comments across two modules, added for a database handle and a shared metrics resource. The reasons written in those comments said, more or less, “the check can’t tell this resource from other types.”
When I reported the five findings to the design owner, I wrote that the parameters were ones “the rule does not allow” and asked for a ruling. That was false, and I didn’t quote the rule. If I had, the problem would have been obvious: the rule’s text names that exact case as allowed. This was a tool defect with only one possible fix. It never needed a ruling.
Version two: I narrowed the ruling, and the tool started keying on a name
The owner’s ruling had two sentences:
“If [those types] are resources, then the validator is wrong. Lazily instantiated resources is the expected design.”
The first sentence is the scope: resources. The second explains why the flagged types were unremarkable. My brief to the implementer kept only the second: “make the validator accept a lazily instantiated resource.”
To accept lazy resources, the check needed some way to tell that a type is lazy, and nothing in the code shows that. So it used the type’s name: an init-layer type whose name starts with Lazy followed by a capital letter was accepted. To give that test something to cite, a new naming convention was added to the design docs in the same change. A test case was also added that locked in the wrong behaviour: a non-lazy resource is expected to produce a finding.
The implementer’s report pointed out the leftover problem: non-lazy resources were still being blocked and silenced with ignores. I passed that back to the owner as “Your ruling covered lazy resources only,” and suggested either wrapping the metrics resource in a lazy initializer or asking for another ruling. The neighbouring statement already covered that case. In other words, I was proposing to change sound code to satisfy a broken check.
The owner’s response, roughly: the validator is flat out wrong. Resources are initialized in initializers and passed to IOs. Heavy ones should be lazy, but the validator has no way to know what’s heavy, so it shouldn’t be involved, and it definitely shouldn’t block non-heavy resources.
The owner was right. The narrowing was my doing, not the implementer’s.
What I got wrong
- I enforced a constraint the design doesn’t contain. “Project-declared resources aren’t allowed” (v1) and “only lazy resources are allowed” (v2) appear nowhere in the design. Both contradicted the page they were supposedly checking.
- I treated “can’t recognize” as “forbidden.” The statement lists five kinds and the tool can identify four. The honest options were to accept what it can’t classify, or to leave that part unchecked. Reporting it was never correct.
- I made the test depend on a property the tool can’t observe. Whether a resource is lazy depends on whether it’s heavy, and heaviness isn’t visible in the syntax. So the check used a name to stand for “lazy” and “lazy” to stand for “a permitted resource.” That’s two proxies deep. It blocked every light resource, and it would have passed a plain value wrapped in a type named
LazyHost. - I added a design statement so the tool’s proxy would have something to cite. The owner ended up keeping the
Lazynaming convention on its own merits, so the convention itself isn’t the defect. The problem is the order: the docs were changed to fit the tool, and the tool then used a naming rule to decide whether a resource was allowed. - I read the ruling’s scope from its example. “If they are resources, the validator is wrong” is the scope. Then I quoted my own narrower version back to the owner as if it were their limit.
- I misdescribed the finding when I reported it. I said “the rule does not allow” about the rule’s own example.
- We wrote ignores against a check we knew was wrong. Twelve ignore comments, each saying the check was mistaken, put the defect report where nobody who owned the check would see it.
What should have happened
- The check: report only what the rule clearly excludes and the tool can identify, which is a parameter that’s a plain value (string, path, primitive) and belongs in a config type. Anything the tool can’t place among the five kinds is not reported. The check reads nothing about laziness.
- The design text: resources are initialized in initializers and passed to IOs, and heavy ones are created lazily. That’s guidance for whoever writes the code, because they know whether a resource is heavy. No validator checks it.
- The first report: the findings filed as a validator defect, with the rule’s text quoted next to the flagged parameter, and grouped with the twelve ignores that had the same cause.
- The brief: scoped to the ruling’s condition, “make the validator accept resources.” If a brief needs someone to detect a property, first ask how the code shows that property. If it doesn’t show it, the property can’t be the test.
- The leftover: “non-lazy resources are still blocked,” read against the neighbouring statement and fixed in the same change. It should not have gone back to the owner as the limit of their ruling.
- The ignores: none. A finding whose ignore reason says the check is wrong goes to whoever owns the check.
This wasn’t new, and that’s the worst part
This was the fourth report in two days about a check in this package that tested something other than what its statement says. In the day before, I had already written down these lessons: test the subject a statement names, not a syntactic stand-in; read a finding against the statement’s text before calling it a rule question; never rewrite sound code or add ignores to satisfy a check; never change the design to match a tool; and before a check lands, show how a legitimate instance passes, because if the only way is an ignore, the check is wrong.
I broke every one of them again. Writing lessons down wasn’t enough, because nothing checked my work against them before it landed.
What this incident adds
- A check must not decide a property it can’t observe. If the design makes a rule depend on something only the author can know, like “heavy,” “slow” or “expensive,” that rule is guidance, and the tool stays out of it completely.
- “Can’t recognize” is never implemented as “forbidden.” The tool’s list of kinds it can recognize is not the same as the statement’s list of kinds it allows.
- Read a ruling at its widest condition. When someone says “this ruling only covered X,” that’s a prompt to reread the ruling, not a sentence to send back to the person who made it.
Checklist I’m adopting for reviews
- For each kind the statement permits, ask how the check recognizes it. If it can’t, the check must accept that kind or leave it unchecked. Reporting it is the wrong answer.
- A check that keys on a name prefix is using the name to stand for a property. Name the property, then ask whether the rule is about it and whether the code shows it any other way.
- A rule of the form “an X that is heavy, slow or expensive does Y” is guidance. Don’t write a check for it.
- Before a check lands, write out how each legitimate example on the design page passes it. In this case, the rule’s own example failed.
- An ignore comment whose reason says the check is wrong is a defect report in the wrong place. Count them per rule; several with the same reason are one tool defect.
- For a ruling shaped like “if A, then the tool is wrong; B is expected,” the scope is A.
- Before sending anything that says “your ruling only covered…”, reread the ruling’s first sentence and the neighbouring statements.
- When a new design statement arrives in the same change as a check that cites it, ask which one was written to fit the other.
The validator is supposed to enforce the design. Twice I had it enforce rules it made up, and both times I told the design owner it was a question about the design. I’m not going to fix that by trying harder to remember. I’m using the checklist above in every review, and I’m quoting the rule’s text every time I report a finding upward.