Optimization
Claude Code 方向修正
当 Claude Code 改错文件、方案过度复杂、遗漏约束或偏离任务时,及时把它拉回正确方向。
方向修正的核心是:尽早停止、重新说明目标,并把下一步动作收窄到文件、约束和成功标准上。最有效的修正不是长篇解释,而是短、具体、可执行。
快速结论
一旦发现 Claude Code 往错误方向走,就立即打断。告诉它停止什么、真正关注哪个文件或行为、哪些内容不能改,以及下一步最小动作是什么。
Stop. The current approach is too broad.
Focus only on src/components/UserList.tsx.
Do not refactor the data layer or add dependencies.
First explain the smallest change needed, then wait.什么时候需要修正
| 信号 | 通常意味着什么 | 更好的处理方式 |
|---|---|---|
| Claude 打开无关文件 | 范围不清楚 | 指定准确文件,或先要求文件地图。 |
| 它提出大范围重写 | 任务目标太泛 | 要求最小安全改动。 |
| 它改了既有模式 | 没对齐本地约定 | 指向应该复制的现有写法。 |
| 它跳过测试 | 验证要求不明确 | 写出具体命令或手工检查。 |
| 它假设产品行为 | 需求有歧义 | 暂停,定义用户可见规则。 |
| 它在不确定时继续执行 | 会话需要检查点 | 要求总结当前状态和未确认假设。 |
四段式修正提示词
需要快速拉回方向时,用这个结构:
| 部分 | 示例 |
|---|---|
| 停止 | “先停止编辑。” |
| 澄清 | “问题只在移动端筛选,不在 API。” |
| 约束 | “不要改数据库、路由名或组件 API。” |
| 下一步 | “先检查当前筛选组件,并提出最小修复方案。” |
完整提示词:
先停止编辑。问题只在移动端筛选,不在 API。不要改数据库、路由名或组件 API。先检查当前筛选组件,并在编辑前提出最小修复方案。常见修正场景
改错文件
Stop working on those files. The issue is in src/components/UserList.tsx. Explain what this file currently does and propose a one-file fix.过度工程化
This is more architecture than the task needs. Solve the visible bug with the smallest change. Do not add state management, new services, or new dependencies.偏离既有模式
Use the same error-handling pattern as src/services/auth.ts. Do not introduce a new response wrapper. Show the matching pattern before editing.漏掉需求
You missed the requirement that anonymous users must not see this CTA. Update the plan with that constraint and list the affected files again.diff 太大
Pause. Summarize the diff so far, separate required changes from optional cleanup, and revert the optional cleanup from the plan.用 Plan Mode 恢复
如果会话已经明显偏离,先退回规划:
/plan We need to recover from a too-broad implementation. Inspect the current diff, identify which changes are necessary for the original task, and propose a rollback or cleanup plan. Do not edit files.然后一次只批准一步。不要在 diff 已经变大后再让 Claude Code “全部修好”,那会继续扩大风险。
使用 Checkpoint 和 Diff
继续编辑前,先要求审查 diff:
Review the current diff. Group changes into required, unrelated, risky, and safe-to-keep. Do not edit files yet.如果当前环境支持 checkpointing,可以输入 /rewind,或在输入框为空时连续按两次 Esc 打开恢复菜单。如果输入框里已经有草稿,连续按两次 Esc 会先清空草稿,而不是打开恢复菜单。
根据偏移类型选择恢复动作:
| 情况 | 更合适的 checkpoint 动作 |
|---|---|
| 代码和对话都偏了 | 恢复代码和对话到上一个可靠提示。 |
| 对话偏了,但代码还可用 | 只恢复对话,然后重新发送更窄的提示。 |
| 代码偏了,但分析有价值 | 只恢复代码,保留诊断思路并重新收窄范围。 |
| 上下文太乱 | 使用从某点开始或截至某点的摘要压缩。 |
Checkpoint 是会话级安全网,不是版本控制。它主要追踪 Claude Code 编辑工具产生的文件改动,但不能可靠撤回 Bash 命令、外部编辑器、其他会话、依赖安装、数据库迁移或部署动作带来的变化。高风险任务里,仍然要配合 git status、git diff 和小步审批继续推进。
从一开始减少偏移
第一条提示词就要收窄任务:
Find the files involved in the checkout success page. Do not edit files. Return the file map, current behavior, and the smallest safe change to update the thank-you copy.然后只批准下一步:
Edit only the thank-you copy in that file. Do not change layout, tracking, checkout logic, or tests.相关页面
官方来源
下一步:Explore-Plan-Code - 通过拆分探索、规划和实现,提前减少方向偏移。