How to stop AI coding agents duplicating code that already exists
2026-11-07 · ai agents · operations · reliability
The argument in 48 seconds, with music. One audit and one timed edit, each on one project. The name check it shows went live on 7 November 2026, and I deleted it on 11 November, for the reason below.
Five changes that stop AI coding agents rebuilding code that already exists
Don’t count on anyone remembering to search firstAn audit of one project found 3 duplicates that review had missed.
Detect an event from the record it leaves, not the words typedMy merge hook missed a whole batch of merges typed two other ways, three deleted tests among them.
Catch copies by their code, not their nameMy name check was wrong 34 times in 39 and never fired on a renamed copy. A cross-file checker caught 2 of 3.
Read a generated API index instead of searching the repoSearching the repo took 6.1 seconds and timed out. Reading the index took about 45 ms.
Put a check at each moment, and make each one read evidenceMine run at the brief, at “done” and at review.
The numbers are mine: one project’s audit, one timed edit, and two later studies of how my checks did.
Don’t count on anyone remembering to search first
This is for you if you run AI coding agents and rely on review to catch code that already exists. I run mine with Rigby, my own harness for several of them at once, and an audit of one project found three duplicates review had missed.
Review missed three duplicates, so I tried telling the agent the moment it wrote one
Before: found by an audit, after it landed
2 cost-query APIs, under different names
2 definitions of one command API
2 copies of the API contract document
Then: at the moment it was written
+ class LLMCostQueryAPI:
Already “LlmCostQueryAPI”. Extend that owner.
Before: one audit of one project. Then: the name check’s real message, shortened. It didn’t last, for the reason below.
My agents start each task fresh. They know the task, not the codebase, so code that exists under another name looks like new work.
Each guard I had worked only if someone remembered to look first
The brief said “search first”An agent deep in a long task forgets, or searches for the wrong name
The reviewer read the diffThe other copy isn’t in the diff, so the duplicate looks clean
An audit found itAfter it landed, when callers already depended on both
Review rarely catches a duplicate, so I moved the check into hooks, scripts that run automatically whether or not anyone remembers. Two of them looked at the wrong thing.
Read the record, not the words typed
My merge hook reminds an agent to audit each merge. A whole batch went past it, three deleted tests among them.
The hook recognised a merge by how it was typed, so other spellings never happened
What the agent typed
Seen?
git merge …
Yes
env -u GIT_INDEX_FILE git merge …
No
$G merge …
No
Real spellings from one project’s merges, with branch names left out.
The first fix widened the pattern. I threw it out, because every new spelling would need another one, and a missed merge is silent.
Now: read what records a merge, simplified
# after every shell call, whatever was typed
if receipt_in(output) or reflog_moved(repo, since=last_seen,
actions=("merge", "pull", "cherry-pick", "rebase")):
remind_to_audit()
The hook now ignores the command and checks git’s reflog after every shell call. Every spelling leaves the same record.
Don’t detect an event from the words someone typed. Read the thing that records it
A mergegit’s reflog, or the merge tool’s receipt
A finished taskThe commit and its report, not the agent saying “done”
Code that already existsThe API index and the code, not a reviewer’s memory
So whenever a hook has to notice something, I ask what records it and read that. For code that already exists, the record is the API index, a generated list of every public function, route and command, with its owner.
Catch copies by their code, not their name
A duplicate is cheapest to fix while the agent still has it open and nothing calls it. So on 7 November I added a hook that looked up each new name as it was written.
When an agent wrote something new, the hook looked it up and spoke only on a match
The agent writesA new function, class, route or database write
The hook reads itThe new name, and the table it writes to
It looks it upIn the project’s generated API index, ignoring case and word order
On a match, it says soWho owns it, and to extend that instead
What the agent saw, real output from two projects, paths shortened
Before building a second one, check the existing owner:
- "contact_create" (new in "src/x/b.py"): already
"AccountantContactCommandAPI.create_contact"
in "src/…/contacts.py".
Extend that owner instead of adding a second one.
- table "saved_feed" is already written by
"src/…/saved_feeds.py:153", ...
One writer per table: write through that owner.
Case and word order didn’t matter, so LLMCostQueryAPI matched LlmCostQueryAPI. Then I tested it, and names turned out to be the wrong thing to match.
Most name warnings were false alarms, and a renamed copy never set one off
34 of 39
warnings that were false alarms
4 of 4
runs where a copy under a different name got through
2 of 3
renamed copies that jscpd, a cross-file checker, caught
Its 39 warnings, recovered from transcripts. Four runs of a task whose copy had a new name. Three renamed copies among 14 pairs of functions.
Most false alarms were classes overriding one method, which share a name by design. Twice a warning did make an agent drop its copy, but the hook never fired on a renamed one. I deleted it on 11 November.
One lesson outlived the name check. Its first version searched the whole repo on every edit, and went silent.
Searching the repo took 6.1 seconds and timed out. Reading the API index took about 45 ms
Search the whole repo6.1 spast the limit, so silent
Search fewer files1.2 smatched “save” to an unrelated file
Read the API index≈45 msreal owners only
One edit, timed on one project, whose index lists 28 Python facades and 71 Rust crates. The hook gave up after 2 seconds.
My projects already generate that index for agents. On one with 438 owners and 1,859 public methods, one file read still answers “does this exist?”
Now each brief carries the part of the index its task touches. If you don’t have an index, generate it first.
Put a check at each moment
There were four checks: at the brief, at each edit, at “done” and at review. With the name check at each edit gone, three remain, and each reads evidence instead of memory.
Three checks, from the brief to the review, and each reads evidence
The briefCarries the part of the API index the task touches
“Done”Refused without a Reused: line, then the duplicate-code gate runs
The reviewChecks ownership and design, not just the diff
“Done” is the command an agent runs to hand back its work.
How far to trust it
The name check didn’t hold up, and the code check runs on one project so far
The name check didn’t hold upMost of its warnings were false alarms, and it never fired on a renamed copy. Both studies, in full
jscpd on one project so farOnly one project’s done gate runs it. My other projects move next
The reflog doesn’t say who mergedAnother agent’s merge in the same checkout gets reported on my next command
Hooks remind, they don’t blockAn agent can read past one. The gate at “done” is the backstop
One project’s auditThe three duplicates are from one codebase, not a rate
What to copy
Give your own agent this brief to build the same checks.
A brief for duplicate checks
Add checks that catch code which already exists, and make each one read evidence.
Index: generate a file that lists every public function, route and command, with its owner. Read it instead of searching the repo.
Brief: give each task the part of the index it touches.
Done: refuse the hand-back without a "Reused:" line, then run a cross-file copy checker such as jscpd over every file. Fail only on copies the change adds. Report near-copies, don't block them.
Don't warn when a new name matches an old one: most matches share a name by design, and a renamed copy never matches.
If a hook has to notice a merge, read git's reflog or the merge tool's receipt, never the command text.
How it works
Catch copies by their code, and make every check read evidence
Generate an indexEvery public function, route and command, with its owner
Brief from itEach task gets the part of the index it touches
Check the code at doneA cross-file checker fails copies a change adds
Read the recordReflog and receipts, never the command text
Next, jscpd moves into my other projects’ done gates.