这他妈简直就是一座纯金矿 几乎还没人在用的最强 Claude Code 技巧。 思路是:每次 Claude 试图结束时,都让一个 Stop 钩子运行测试,只要没通过,就让它回去继续干活。 Claude 再也不能把构建失败的东西说成“完成”了,因为只有钩子检查过工作成果,才允许结束这一轮。 你只需要在 .claude/settings.json 中注册一个钩子: "Stop" → .claude/hooks/check-done.sh 具体流程如下: → Claude 完成工作,尝试结束这一轮 → Stop 钩子运行你的测试套件 → 测试失败时以退出码 2 退出,错误直接反馈给 Claude → Claude 继续修复,直到测试全部通过 钩子只在三个时刻介入: → 每一轮结束时 → 每次测试失败时 → 连续阻止 8 次之后,Claude Code 无论如何都会结束这一轮,避免无限循环 Claude 负责写。测试负责评判。钩子负责决定。 全部规则就是:“完成”不是 Claude 说了算,而是由你的测试来确认。 完整结构 > .claude/settings.json 注册 Stop 钩子 > check-done-sh 运行你的测试命令 > exit 0 允许结束这一轮 > exit 2 将失败信息反馈给 Claude > stop_hook_active 告诉钩子当前已经是重试 > /goal 无需配置,就能在单次会话中实现同样的效果 把这个结构和下面的提示词粘贴到 Claude Code 中 ↓ “为这个项目添加一个 Stop 钩子: 1. 创建 .claude/hooks/check-done.sh,运行 [你的测试命令],并只输出失败的测试。 2. 测试失败时,以退出码 2 退出,并将失败信息写入 stderr。测试通过时,以退出码 0 退出。 3. 如果 stop_hook_active 为 true,且相同的测试仍然失败,则以退出码 0 退出并告知我,而不是继续循环。 4. 将其注册到 .claude/settings.json 的 "Stop" 下,然后向我展示 /hooks。 先以 diff 的形式向我展示每一处改动。在我说开始之前,不要做任何修改。” ↳ https://code.claude.com/docs/en/hooks 收藏起来,以后会派上用场。
看原文
THIS IS A PURE F*CKING GOLDMINE the best Claude Code trick almost nobody runs yet. The idea: a Stop hook runs your tests every time Claude tries to finish, and sends it back to work until they pass. Claude can't call a broken build "done" anymore, because the hook checks the work before the turn is allowed to end. You just register one hook in .claude/settings.json: "Stop" → .claude/hooks/check-done.sh here's the split: → Claude finishes and tries to end the turn → the Stop hook runs your test suite → a failure exits with code 2 and the error goes straight back to Claude → Claude keeps fixing until the tests are green The hook only steps in at three moments: → the end of every turn → every time a test fails → after 8 blocks in a row, when Claude Code ends the turn anyway so it can't loop forever Claude writes. Tests judge. The hook decides. That's the whole rule: "done" isn't something Claude says, it's something your tests confirm. the full tree > .claude/settings.json registers the Stop hook > check-done-sh runs your test command > exit 0 lets the turn end > exit 2 sends the failures back to Claude > stop_hook_active tells the hook it's already a retry > /goal does the same for one session with zero config paste the tree and this prompt into Claude Code ↓ "Add a Stop hook to this project: 1. Create .claude/hooks/check-done.sh that runs [your test command] and prints only the failing tests. 2. When tests fail, exit 2 with the failures on stderr. When they pass, exit 0. 3. If stop_hook_active is true and the same tests still fail, exit 0 and tell me instead of looping. 4. Register it under "Stop" in .claude/settings.json, then show me /hooks. Show me every change as a diff first. Nothing changes until I say go." ↳ https://code.claude.com/docs/en/hooks save it, it'll come in handy.