Diff tools are biased.
Additions render green. They're the new surface area, the thing the reviewer interrogates. Deletions render red: they're the cleanup, the simplification, the lines that weren't pulling their weight anymore.
That bias made sense when humans wrote and removed code. It makes less sense when AI is doing the refactoring, because AI doesn't know why a line existed. It only knows what the code does in the current context.
The local coherence problem
An AI assistant evaluating a function sees the code in front of it, not the history of why it was written.
If a validation step is redundant given the surrounding logic, the AI will remove it. If an authorization check is duplicated three layers up, the AI will simplify. If an error handler looks like boilerplate, it will be cleaned up.
Each of those decisions is locally coherent. The problem is that security controls often get added for non-local reasons: a specific incident, a threat model, an edge case that existed once and might exist again. The AI has no way to know any of that from the code alone.
Failure mode 1: the check that looked like dead code
Imagine a permissions check in a utility function. The function is only ever called from one place, and that place already checks permissions. The AI sees the check as redundant and removes it.
Then someone adds a second call site six months later.
The check was defensive programming, not dead code. The difference is invisible in the diff. The AI made the right local decision. It had no way to know about the future caller.
(This example is illustrative, not a specific real case.)
Failure mode 2: the error handler that was also a firewall
An error handler catches a specific exception and logs it, then returns a generic error response rather than the full exception detail.
The AI refactors it into a cleaner, broader exception handler. Less repetition, cleaner structure, same surface-level behavior.
The specific catch was intentional. It was logging an anomalous input pattern that security was using to detect brute-force probes. The generic catch still logs errors, but doesn't flag that specific pattern.
Happy-path tests pass. The refactor ships. The monitoring gap opens.
(Illustrative.)
Failure mode 3: the test nobody understood
A test checks a very specific, slightly bizarre behavior. It was written three years ago, the developer who wrote it has left, and nobody remembers why the behavior was considered important.
The AI sees it as testing an implementation detail rather than expected behavior. It removes it during a cleanup pass.
The test was documenting a security invariant from an incident postmortem. The behavior was weird because the fix was weird. The test was the only record that it had to stay that way.
(Illustrative.)
Where this bites hardest
These failure modes are worst at trust boundaries: the layer where unauthenticated requests become authenticated ones, where user input enters business logic, where API calls cross service boundaries.
Security controls at trust boundaries tend to be the most important and the least explained. They were added in response to something real, documented once in a postmortem or an issue, and slowly lost that context as the code aged around them.
When reviewing diffs in these areas, the question to ask about a deletion is not "does the remaining code work?" but "what was this handling, and does anything handle it now?"
A practical review pass for deletions
For security-sensitive areas, treating deletions as a separate review pass tends to surface things that a combined addition/deletion review misses. The specific questions:
- What condition did this code handle?
- Was that condition handled anywhere before this code, or anywhere after?
- If a downstream system could now send something this code was filtering, what happens?
It is slower than reviewing additions. It is also the part of the review that matters most for security.
The reframe
AI refactors optimize for legibility and local coherence. Security is often encoded in the incoherent parts: the check that seems redundant, the error handler that seems overcautious, the test that seems to be testing the wrong thing.
Deletions are feature removals with the commit message omitted.
The refactor looked right. The diff looked clean. The test suite passed. Sometimes that is not enough.
Do you review deletions differently from additions in security-sensitive code, and if so, what does that review actually look like?
Top comments (18)
The removed authorization check is the case that worries me most, because the diff looks like a tidy simplification and the tests usually still pass. A cheap guard: run git blame on every deleted line that touches auth, validation or error handling, and make the review show the commit message that added it. If the original reason is still true, the deletion needs a sentence explaining why it's safe now. Have you seen a tool surface deletions that way?
I haven't seen a tool do this particularly well yet. git blame is a good starting point, but the commit message alone might not tell the full story, especially if the original context lives in an incident or a ticket. Ideally, a review tool would surface the history behind each security-sensitive deletion and ask the reviewer to explicitly account for it. That would be much more useful than just flagging removed lines.
Agreed, asking the reviewer to account for each security sensitive deletion is the useful part. Even linking the deleted lines to the ticket or incident that added them would make that prompt answerable instead of a guess.
Exactly. Linking the deleted lines back to the ticket or incident gives the reviewer the context to make a real decision. Otherwise they're being asked to reconstruct the reasoning from a commit message, which is often not enough. I'd also want the review to flag when that original context is missing, rather than silently treating the deletion as safe.
Exactly. A deletion with a linked ticket is a decision the reviewer can check; a deletion with only a commit message is a guess they have to reconstruct.
Yeah, and that missing context should be a signal in itself. If a security-sensitive deletion has no traceable reason behind it, I'd rather have the tool flag it for extra scrutiny than let it pass as a routine cleanup. The absence of history doesn't mean the code was unnecessary.
Agreed, a missing reason is a signal. Flagging security-sensitive deletions that have no linked ticket for an extra reviewer is a cheap rule that catches exactly the risky ones.
One case I’ve seen worth paying attention to is tenant isolation in multi-tenant systems.
Imagine an API controller validates that a user belongs to a tenant, then queries documents by
document_id. An AI refactor might decide that addingtenant_idto the repository query is redundant because authorization already happens in the controller.But that repository might later be called by a background worker, a GraphQL resolver, or another internal service that doesn't pass through the same controller.
Now the original optimization has quietly removed an important security boundary. This is exactly how a seemingly harmless cleanup can introduce a Broken Object Level Authorization (BOLA) vulnerability.
I think deletion reviews should also trace who can call the remaining code, not just whether the current execution path still works. That's context a passing test suite might not cover.
Exactly. That's the part that makes this tricky. The controller might be safe today, but the repository shouldn't have to rely on every future caller remembering to enforce the same rule. I'd argue the tenant filter belongs as close to the data access as possible, even if it feels redundant. That's one of those checks I'd rather see twice than regret removing once.
The non-local reason is the whole problem, and it is a record-keeping gap before it is a review gap. A check added after an incident carries its reason only in someone's memory or a closed ticket. If the reason lives next to the code as a written decision, like this authorization check stays even though a caller already checks, a refactor that removes it can fail a rule instead of relying on the reviewer to remember. That is the layer we are building Mneme around.
That's a good point. The tricky part is making sure those decisions don't become another layer of documentation that gets outdated or ignored. Ideally, the context should be available right where the change happens, and the tooling should actually enforce it. Otherwise we're just moving the memory problem somewhere else.
Staleness is the right worry. Prose rots because nothing fails when it stops matching the code. A decision with a path scope and a check fails the day the file it describes moves or the exception stops applying, so someone has to update it or delete it. The rules that never fire in a quarter are your prune list.
That's an interesting approach. Making decisions scoped and checkable rather than leaving them as free-form documentation addresses a big part of the staleness problem. I especially like the idea of treating rules that never fire as candidates for pruning. The challenge is making sure pruning doesn't remove a rule that protects against a rare but serious scenario.
The non-local rationale point is the strongest part: a guard clause or error handler often exists because of an incident or threat model that never appears in the source, so an AI optimizing for local coherence will read it as dead weight. In an Electron main-process context this shows up the same way when a renderer-originated IPC argument validation looks redundant beside an existing allowlist, but removing it leaves the door open for a future channel. One practical guard is to annotate defensive checks with an explicit note on why they exist, and keep security-relevant deletions in a separate review gate that a refactor pass cannot touch.
That Electron IPC example is a good one. An allowlist protects the channels that exist today, while validating the arguments protects what those channels are allowed to do. They're different boundaries, even if the checks look redundant locally. I like the idea of keeping security-relevant deletions behind a separate review gate too. Comments help preserve the rationale, but the review gate makes sure it doesn't get lost during a refactor.
Failure mode 3 is the one a script can catch. My test gate takes a minimum test count set where the gate runs, outside the commit, so a cleanup pass can't lower it. I deleted one of two tests in a small Python project and the run failed with "the output reports 1 tests, fewer than GATE_MIN_TESTS=2". That check compares a total, so swapping the odd test for a trivial one passes, and a deleted permission check never touches the count. Those two still fall to the reviewer, whose prompt says nothing gets weakened without a stated reason.
That's a useful distinction. A test-count gate can catch accidental deletion, but it can't tell whether the deleted test was the one protecting the important invariant. Same with authorization checks. I think these need two different layers: automated checks for structural changes and a review step that understands what the removed code was actually protecting. One can't really replace the other.
说得很对,删除的代码往往藏着当时的取舍和教训,单独审一遍删除的部分这个做法很值得借鉴,谢谢分享。