即插即用的 Claude Code hook 脚本。解决 3 类问题:agent 提前停止、工具重试死循环、投机性完成。
-p 模式(Stop hook 不触发,用 --max-turns)harness-design-patternsagent-ops将需要的 hook 加入 ~/.claude/settings.json:
json
{
"hooks": {
"Stop": [{
"hooks": [
{"type": "command", "command": "bash /path/to/agent-hooks/scripts/ralph-stop-hook.sh"},
{"type": "command", "command": "bash /path/to/agent-hooks/scripts/doubt-gate.sh"}
]
}],
"PostToolUseFailure": [{
"hooks": [
{"type": "command", "command": "bash /path/to/agent-hooks/scripts/tool-error-tracker.sh", "async": true}
]
}],
"PreToolUse": [{
"hooks": [
{"type": "command", "command": "bash /path/to/agent-hooks/scripts/tool-error-advisor.sh"}
]
}],
"PostToolUse": [{
"matcher": "Write|Edit",
"hooks": [
{"type": "command", "command": "bash /path/to/agent-hooks/scripts/post-edit-check.sh", "async": true}
]
}]
}
}
所有脚本需要环境变量 NC_SESSION(session ID)。状态写入 $HOME/<config-dir>/shared-context/sessions/<session-id>/(路径可通过 $HOME 环境变量配置)。
脚本Hook 类型功能详情ralph-init.sh <id> [max]CLI 调用初始化持续执行状态(支持 crash 恢复)详情ralph-stop-hook.shStop阻止提前停止,4 个安全阀保底详情ralph-cancel.sh <id> [reason]CLI 调用发送 30s TTL 取消信号详情tool-error-tracker.shPostToolUseFailure追踪连续失败,3 次软提示 / 5 次强制换方案详情tool-error-advisor.shPreToolUse5 次失败后 block 同一命令详情doubt-gate.shStop检测投机语言,强制提供证据详情post-edit-check.shPostToolUse (Write|Edit)编辑后即时跑 linter/type checker详情
所有 hook 脚本遵循 Claude Code hook 协议:
json
{"continue": true} // 放行
{"decision": "block", "reason": "..."} // 阻止(Stop hook)
{"hookSpecificOutput": {"additionalContext": "..."}} // 注入上下文
Ralph stop hook 在以下条件下 MUST 放行,NEVER block:
注:Context usage >= 95% 安全阀 未实现——Claude Code 不在 hook 输入或 transcript 中暴露 context_window_size。Claude Code 自身的 reactive compaction 机制会独立处理 context 溢出。
-p 模式 → 不要用 Ralph,而是用 --max-turns