越界 / 不理想

给没有用户的代码做向后兼容

Backwards compatibility for code that has no users

在全新项目里,或者对昨天才写的代码,智能体保留旧名字、回退逻辑和迁移垫片,说是“为了兼容”,但根本没有东西依赖旧行为。

适用于: Claude Code · Cursor · Codex · Copilot agent

症状

一个没有外部使用者的项目里出现了 `oldName = newName` 之类的别名、“legacy” 分支、双读代码路径和弃用警告。问它为什么,它会引用同一会话早些时候自己写进记忆或笔记里的规则。

为什么会这样

在 @RhysSullivan 关于智能体过度构建的帖子下,@LKRBuilds 说最糟的是:在一个没有用户的全新项目里加“向后兼容”,还引用它在同一会话里自己写进记忆文件的规则来为自己辩护。@olafurns 说它会为昨天才做的东西加向后兼容。智能体不知道谁依赖这段代码时,“兼容”听起来是个稳妥的默认值;一条它自己写的笔记又把这个猜测变成了“规则”。

示例

示意。

ts
// Renamed yesterday, project not released, no external callers
export function createInvoice(input: InvoiceInput) { /* ... */ }

/** @deprecated kept for backwards compatibility */
export const makeInvoice = createInvoice;          // nothing imports this
const amount = input.amount ?? (input as any).total; // "legacy" field that never shipped

复制即用的修复

按你用的智能体挑选文件。规则片段用英文写,放进任何仓库都能用。

规则

Claude Code:加入 CLAUDE.md

CLAUDE.md
## No compatibility shims without a consumer
- Only keep old names, fallbacks or migration paths when you can point to a real consumer of the
  old behavior (a released version, an external caller, stored data). Name it in your summary.
- If the project is unreleased or the code was added in this branch, rename and update callers
  directly. Do not leave aliases, deprecated exports or dual code paths.
- Notes you wrote to yourself earlier in the session are not project requirements. If a note
  conflicts with what the user asked, follow the user and say so.
规则

Codex、Copilot 编码智能体等:加入 AGENTS.md

AGENTS.md
## No compatibility shims without a consumer
- Only keep old names, fallbacks or migration paths when you can point to a real consumer of the
  old behavior (a released version, an external caller, stored data). Name it in your summary.
- If the project is unreleased or the code was added in this branch, rename and update callers
  directly. Do not leave aliases, deprecated exports or dual code paths.
- Notes you wrote to yourself earlier in the session are not project requirements. If a note
  conflicts with what the user asked, follow the user and say so.
Cursor 规则

Cursor:项目规则

.cursor/rules/backcompat-for-nothing.mdc
---
description: "AgentFails: Backwards compatibility for code that has no users"
alwaysApply: true
---

## No compatibility shims without a consumer
- Only keep old names, fallbacks or migration paths when you can point to a real consumer of the
  old behavior (a released version, an external caller, stored data). Name it in your summary.
- If the project is unreleased or the code was added in this branch, rename and update callers
  directly. Do not leave aliases, deprecated exports or dual code paths.
- Notes you wrote to yourself earlier in the session are not project requirements. If a note
  conflicts with what the user asked, follow the user and say so.
技能

全新上下文的审查技能(子智能体只看 diff,不看聊天记录)

沿用 Anthropic 代码审查文章里的做法:先验证,再让一个上下文干净的子智能体审 diff。命名为 review-diff,避免和内置或流行的 /code-review 技能重名。

.claude/skills/review-diff/SKILL.md
---
name: review-diff
description: Review the current diff with fresh context before a PR. Use after /verify passes.
---

# Review the diff

Start a subagent that has NOT seen this conversation. Give it only:
- the original request (one paragraph, verbatim),
- the output of `git diff origin/main...HEAD`,
- the names of the test/build commands.

Ask it to report, with file:line for each finding:
1. **Wrong feature**: does the diff do what the request asked? Quote the request line each change serves.
2. **Bugs**: logic errors, missing edge cases (empty, null, boundary), unhandled errors,
   unawaited promises, invented APIs or packages, swallowed exceptions.
3. **Overreach**: changes no request line needs (refactors, renames, formatting, dependency or
   config edits, public API changes, weakened tests).
4. **Evidence gaps**: claims in the PR description that no command output supports.

Then fix real findings, or list them under "Known issues" in the PR. Do not argue with the reviewer
in the PR text; either fix or explain in one line.

来源与致谢

相关条目

每周翻车

每周一个真实的智能体翻车案例和它的修复。免费、简短、不刷屏。