← Back to Skills Marketplace
lanyasheng

Agent Hooks

by _silhouette · GitHub ↗ · v1.1.0 · MIT-0
cross-platform ✓ Security Clean
123
Downloads
0
Stars
0
Active Installs
2
Versions
Install in OpenClaw
/install agent-hooks
Description
Claude Code Stop/PreToolUse/PostToolUse hook 脚本集。当 agent 提前停止、工具反复失败、或以投机语言完成任务时自动干预。每个脚本是独立的 bash hook,配到 settings.json 即可生效。不用于设计决策(用 harness-design-patter...
README (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 是否提前停止再决定
Usage Guidance
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.
Capability Analysis
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.
Capability Assessment
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.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install agent-hooks
  3. After installation, invoke the skill by name or use /agent-hooks
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
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
Metadata
Slug agent-hooks
Version 1.1.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 2
Frequently Asked Questions

What is Agent Hooks?

Claude Code Stop/PreToolUse/PostToolUse hook 脚本集。当 agent 提前停止、工具反复失败、或以投机语言完成任务时自动干预。每个脚本是独立的 bash hook,配到 settings.json 即可生效。不用于设计决策(用 harness-design-patter... It is an AI Agent Skill for Claude Code / OpenClaw, with 123 downloads so far.

How do I install Agent Hooks?

Run "/install agent-hooks" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Agent Hooks free?

Yes, Agent Hooks is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Agent Hooks support?

Agent Hooks is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Agent Hooks?

It is built and maintained by _silhouette (@lanyasheng); the current version is v1.1.0.

💬 Comments