Agent Hooks

v1.1.0

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

0· 126·0 current·0 all-time
by_silhouette@lanyasheng

Install

OpenClaw Prompt Flow

Install with OpenClaw

Best for remote or guided setup. Copy the exact prompt, then paste it into OpenClaw for lanyasheng/agent-hooks.

Previewing Install & Setup.
Prompt PreviewInstall & Setup
Install the skill "Agent Hooks" (lanyasheng/agent-hooks) from ClawHub.
Skill page: https://clawhub.ai/lanyasheng/agent-hooks
Keep the work scoped to this skill only.
After install, inspect the skill metadata and help me finish setup.
Use only the metadata you can verify from ClawHub; do not invent missing requirements.
Ask before making any broader environment changes.

Command Line

CLI Commands

Use the direct CLI path if you want to install manually and keep every step visible.

OpenClaw CLI

Bare skill slug

openclaw skills install agent-hooks

ClawHub CLI

Package manager switcher

npx clawhub@latest install agent-hooks
Security Scan
VirusTotalVirusTotal
Benign
View report →
OpenClawOpenClaw
Benign
high confidence
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.
Assessment
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.

Like a lobster shell, security has layers — review code before you run it.

latestvk97drxsdctykfd35drwfwzw5gx84b7s2
126downloads
0stars
2versions
Updated 3w ago
v1.1.0
MIT-0

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/<config-dir>/shared-context/sessions/<session-id>/(路径可通过 $HOME 环境变量配置)。

Scripts

脚本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 协议

所有 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
  • 如果任务 < 5 分钟 → 不需要 Ralph
  • 如果不确定 → 先不启用,观察 agent 是否提前停止再决定

Comments

Loading comments...