DEV Community

Every deletion in your diff had a reason to exist. AI doesn't know what it was.

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?

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.