The PR Was Too Clean to Merge: When AI Code Ignores the Repo's Hard-Won History
AI-generated code that passes all tests can still silently fork business-critical logic, creating regressions that surface months later during reconciliation. Teams leaning on AI coding assistants need explicit safeguards — like searching for existing implementations and hardcoding edge cases — to preserve the institutional knowledge embedded in legacy systems.
A GitHub reviewer recounted how a seemingly perfect AI-generated PR for a checkout page introduced a `formatMoneyCents()` function, complete with full types, clean tests, and green CI. Just before hitting approve, the reviewer noticed the new function duplicated an existing `formatCurrency()` in a shared package, which carried a banker's rounding rule signed off by finance after a real cent-inaccuracy incident years earlier. The AI never discovered the existing tool because it only looked within its local file context, hit a naming mismatch, and was rewarded for a self-contained change.
The article breaks down why the AI generated a duplicate: local context preference, naming mismatch, completion reward, and barrel exports that hid the existing function from the public interface. The author also notes that tests written by the same model that writes the implementation are weak at uncovering boundary cases, since the model won't imagine values like 0.005 or 2.675 where rounding rules diverge. To avoid these silent regressions, the author now searches for existing functions by verb before reading any AI implementation, and asks why old code looks the way it does.
The piece closes with practical advice: hardcode boundary values into a fixed test table rather than trusting the model to remember them, and recognize that AI code often lacks provenance — the historical context that makes certain code deliberately ugly or seemingly redundant. In this case, the reviewer deleted the new function and routed the change through the existing one, accepting a messier diff for correctness.
AI coding tools are optimized for self-contained, immediately green changes, which systematically conflicts with the long-lived, context-heavy decisions embedded in shared codebases.
The perceived 'cleanliness' of AI code often signals that it lacks the scars of production incidents; those redundant-looking lines are usually the most battle-tested parts of a system.
Preventing duplicate AI code is not a prompting problem. It requires structural changes like better retrieval (search existing APIs by intent) and test design that encodes organizational knowledge independent of the model.
As AI assistants write more code, the role of the human reviewer shifts from checking syntax and style to acting as a guardian of historical context — asking not 'does it work?' but 'does this know why the old code exists?'