重新造了一个早已存在的轮子
Rebuilt something that already existed
智能体没去找已有的函数、组件或服务,而是又写了一个,从此要同步维护两份。
适用于: Claude Code · Cursor · Codex · Copilot agent
症状
新出现一个 `formatCurrency`、`useDebounce` 或 `apiClient`,和代码库里已在用的那个并存,行为还略有不同。
为什么会这样
写新代码比找旧代码快,而智能体的上下文很少包含整个仓库。Kaplira 的第一步就是“检查智能体是否找到了现有实现”,并指出再写一份实现会扩大任务范围却没解决根因。久而久之,这就是 developertoolkit.ai 说的“侵蚀”。
示例
示意。
textexisting: src/lib/money.ts export function formatCurrency(cents, locale)
new: src/cart/utils.ts export const fmtPrice = (n) => "$" + n.toFixed(2)
复制即用的修复
按你用的智能体挑选文件。规则片段用英文写,放进任何仓库都能用。
规则
Claude Code:加入 CLAUDE.md
CLAUDE.md## Reuse before you write
- Before writing a helper, hook, component or client, search the repo for an existing one
(`rg -n "format.*currency|useDebounce|apiClient" src`) and report what you found.
- Use the existing implementation. If it doesn't fit, say why, and extend it rather than writing a parallel one.
- Keep a short "Where things live" section in this file for common utilities.
规则
Codex、Copilot 编码智能体等:加入 AGENTS.md
AGENTS.md## Reuse before you write
- Before writing a helper, hook, component or client, search the repo for an existing one
(`rg -n "format.*currency|useDebounce|apiClient" src`) and report what you found.
- Use the existing implementation. If it doesn't fit, say why, and extend it rather than writing a parallel one.
- Keep a short "Where things live" section in this file for common utilities.
Cursor 规则
Cursor:项目规则
.cursor/rules/duplicate-implementation.mdc---
description: "AgentFails: Rebuilt something that already existed"
alwaysApply: true
---
## Reuse before you write
- Before writing a helper, hook, component or client, search the repo for an existing one
(`rg -n "format.*currency|useDebounce|apiClient" src`) and report what you found.
- Use the existing implementation. If it doesn't fit, say why, and extend it rather than writing a parallel one.
- Keep a short "Where things live" section in this file for common utilities.
模板
CLAUDE.md / AGENTS.md 中的“东西放在哪”段落
CLAUDE.md## Where things live (use these, don't recreate them)
- Money/currency formatting: src/lib/money.ts
- HTTP client (auth, retries): src/lib/api.ts
- Date helpers: src/lib/dates.ts
- Shared UI components: src/components/ui/
来源与致谢
- Kaplira: When an AI agent changes unrelated files (investigate the cause first)
- developertoolkit.ai: failure taxonomy (erosion)
- @octavianslash on X: a cookie middleware with a bespoke dispatch layer duplicating the existing middleware system
- @paolocargnin on X: rejected code often reimplements something that already exists in the codebase
- @triskweline on X: preventing a third notion of "is this invoice paid"