← 返回 Skills 市场
lanyasheng

Agent Hooks

作者 _silhouette · GitHub ↗ · v1.1.0 · MIT-0
cross-platform ✓ 安全检测通过
123
总下载
0
收藏
0
当前安装
2
版本数
在 OpenClaw 中安装
/install agent-hooks
功能描述
Claude Code Stop/PreToolUse/PostToolUse hook 脚本集。当 agent 提前停止、工具反复失败、或以投机语言完成任务时自动干预。每个脚本是独立的 bash hook,配到 settings.json 即可生效。不用于设计决策(用 harness-design-patter...
使用说明 (SKILL.md)

Agent Hooks

即插即用的 Claude Code hook 脚本。解决 3 类问题:agent 提前停止、工具重试死循环、投机性完成。

When to Use

  • Agent 在复杂任务中只完成一部分就停了 → 用 Ralph hooks
  • 工具反复失败但 agent 一直重试同一个命令 → 用 tool-error hooks
  • Agent 说"可能""大概"就声称完成了 → 用 doubt-gate hook
  • 编辑后想立即知道有没有引入错误 → 用 post-edit-check hook

When NOT to Use

  • Headless -p 模式(Stop hook 不触发,用 --max-turns
  • 设计多 agent 系统架构 → 用 harness-design-patterns
  • 监控运行中的 session、处理限速 → 用 agent-ops

安装

将需要的 hook 加入 ~/.claude/settings.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/\x3Cconfig-dir>/shared-context/sessions/\x3Csession-id>/(路径可通过 $HOME 环境变量配置)。

Scripts

脚本 Hook 类型 功能 详情
ralph-init.sh \x3Cid> [max] CLI 调用 初始化持续执行状态(支持 crash 恢复) 详情
ralph-stop-hook.sh Stop 阻止提前停止,4 个安全阀保底 详情
ralph-cancel.sh \x3Cid> [reason] CLI 调用 发送 30s TTL 取消信号 详情
tool-error-tracker.sh PostToolUseFailure 追踪连续失败,3 次软提示 / 5 次强制换方案 详情
tool-error-advisor.sh PreToolUse 5 次失败后 block 同一命令 详情
doubt-gate.sh Stop 检测投机语言,强制提供证据 详情
post-edit-check.sh PostToolUse (Write|Edit) 编辑后即时跑 linter/type checker 详情

Hook 协议

所有 hook 脚本遵循 Claude Code hook 协议:

  • 输入:stdin 接收 JSON(包含 tool_name, tool_input, session_id 等)
  • 输出:stdout 输出 JSON 决策
{"continue": true}                                    // 放行
{"decision": "block", "reason": "..."}                 // 阻止(Stop hook)
{"hookSpecificOutput": {"additionalContext": "..."}}   // 注入上下文

安全阀

Ralph stop hook 在以下条件下 MUST 放行,NEVER block:

  1. 认证失败(401/403,从 stop_reason 检测)
  2. Cancel 信号存在且未过期
  3. 闲置超时 > 2 小时
  4. 达到 max_iterations

注:Context usage >= 95% 安全阀未实现——Claude Code 不在 hook 输入或 transcript 中暴露 context_window_size。Claude Code 自身的 reactive compaction 机制会独立处理 context 溢出。

条件判断规则

  • 如果 headless -p 模式 → 不要用 Ralph,而是用 --max-turns
  • 如果任务 \x3C 5 分钟 → 不需要 Ralph
  • 如果不确定 → 先不启用,观察 agent 是否提前停止再决定
安全使用建议
What to check before installing: - Confirm the session directory and settings file: the SKILL.md example mentions ~/.claude/settings.json but the scripts use $HOME/.openclaw/shared-context/sessions. Decide which path you will use and update scripts/settings consistently. - Provide NC_SESSION at runtime (NC_SESSION is required by the scripts but not declared in metadata). Without it hooks will often permissively allow stop or exit silently. - Inspect the scripts yourself (they're plain bash and included here). They only read stdin and write session files, and they do not contact external servers, but they will run local linters/commands if present — make sure those tools are safe in your environment. - Be cautious when enabling Stop hooks: they intentionally block agent termination to force continued work. Confirm you trust the hook logic and its safety valves (auth/cancel/stale/max iterations) before enabling in production or with sensitive agents. - Consider testing in an isolated environment (temporary HOME) first. The repo includes unit tests that simulate behavior; run them in a sandbox to verify behavior with your platform's jq/md5/python tool availability. - If you want the metadata to match runtime needs, ask the maintainer to declare NC_SESSION in requires.env and to clarify the config paths in SKILL.md/metadata.
功能分析
Type: OpenClaw Skill Name: agent-hooks Version: 1.1.0 The 'agent-hooks' bundle is a collection of utility scripts designed to enhance the reliability and persistence of AI agents (specifically Claude Code). The scripts implement common agentic patterns such as persistent execution loops ('Ralph' mode in ralph-stop-hook.sh), speculative language detection (doubt-gate.sh), and automated diagnostics (post-edit-check.sh). The code uses standard tools like jq for safe JSON handling and maintains state locally within the user's home directory (~/.openclaw/). No indicators of data exfiltration, malicious execution, or unauthorized persistence were found; the instructions provided to the agent are aligned with the stated goal of ensuring task completion and verifying results.
能力评估
Purpose & Capability
The scripts implement Stop/PreToolUse/PostToolUse hooks and only require local session state under $HOME; this matches the 'agent-hooks' description. Minor inconsistency: SKILL.md and the scripts expect an NC_SESSION env var and write state to $HOME/.openclaw/shared-context/sessions, but the registry metadata lists no required env vars and the install example references ~/.claude/settings.json — the differing config paths and the undocumented NC_SESSION requirement should be clarified.
Instruction Scope
Instructions are narrowly scoped to hook behavior: read hook JSON from stdin, write/read per-session state files under $HOME/.openclaw/shared-context/sessions, and optionally run local linters/diagnostics (ruff, pyright, tsc, cargo, go, shellcheck) if present. They do not call external network endpoints or request unrelated system credentials. Concerns: the SKILL.md example shows adding commands to ~/.claude/settings.json while the scripts actually use ~/.openclaw for session state — this mismatch could cause user misconfiguration. Also the skill expects NC_SESSION (session id) to be present at runtime but that env var is not declared in registry metadata.
Install Mechanism
There is no automated install spec (instruction-only deployment). The package includes bash scripts and tests; nothing is downloaded from external URLs and no archives are extracted. This is low risk, but installing requires placing scripts on disk and wiring them into your settings.json.
Credentials
The skill does not request API keys or secrets. It does rely on NC_SESSION and $HOME for session identification and storage; NC_SESSION is used by every script but is not declared in the registry metadata. The scripts also use common utilities (jq, md5/md5sum/shasum, optionally python3) and may call local developer tools (pyright, ruff, npx, cargo, go, shellcheck) if available — these are reasonable for diagnostics but you should be aware linters/tools will be invoked on file paths provided by the agent.
Persistence & Privilege
Skill does not request 'always: true' and is user-invocable. Scripts create and update per-session files under $HOME/.openclaw/shared-context/sessions (state, cancel signals, error trackers). This file-write persistence is expected for a hook system, but combined with Stop hook behavior it means a hooked agent can be prevented from stopping until safety valves trigger — review the safety valve implementations and ensure you trust the scripts before enabling Stop hooks on live/high-privilege agents.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install agent-hooks
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /agent-hooks 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.1.0
Fix hook event count (26→27), test count (37→42), harden stdin reading, add jq dependency note
v1.0.0
Initial release: Ralph persistent execution, doubt gate, tool error escalation, post-edit diagnostics
元数据
Slug agent-hooks
版本 1.1.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 2
常见问题

Agent Hooks 是什么?

Claude Code Stop/PreToolUse/PostToolUse hook 脚本集。当 agent 提前停止、工具反复失败、或以投机语言完成任务时自动干预。每个脚本是独立的 bash hook,配到 settings.json 即可生效。不用于设计决策(用 harness-design-patter... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 123 次。

如何安装 Agent Hooks?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install agent-hooks」即可一键安装,无需额外配置。

Agent Hooks 是免费的吗?

是的,Agent Hooks 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Agent Hooks 支持哪些平台?

Agent Hooks 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Agent Hooks?

由 _silhouette(@lanyasheng)开发并维护,当前版本 v1.1.0。

💬 留言讨论