跳到正文
cccskills
免费在 GitHub 公开

writing-hooks

Guide for writing, configuring, and debugging Claude Code hooks. Use when adding, writing, or debugging hooks — including silent failures and permission errors.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md5.6 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Writing Hooks

Guide for adding hooks to harness/hooks/hooks.json and writing the scripts they invoke.

Hook Types

TypeWhen to useCost
commandDeterministic checks, fast guards, file opsLow
promptSingle LLM judgment with {"ok": true/false} responseMedium
agentMulti-step LLM reasoning with file toolsHigh
httpPOST to external serviceVaries

Prefer command whenever the logic can be expressed in any executable script or the Go binary. Reserve agent/prompt for cases that genuinely need judgment — they are slower and cost tokens on every invocation.

Note: agent hooks do NOT work on PreCompact. Use command instead.

Adding a command Hook

1. Write the script

Place scripts in harness/scripts/. Use whichever language fits the task — bash, Python, and Node.js are all fine. The only requirement is that the script is executable and produces the correct exit code and stdout.

Exit codes control the hook outcome:

Exit codeMeaning
0Allow / pass
2 + JSON to stdoutBlock (PreToolUse / Stop)
0 + JSON to stdoutInject context (PostToolUse, PreCompact)

Bash example — blocking Stop hook:

#!/usr/bin/env bash
if [[ some_condition ]]; then
  echo '{"decision":"block","reason":"Explain why"}'
  exit 2
fi
exit 0

Python example — blocking Stop hook:

#!/usr/bin/env python3
import sys, json
if some_condition:
    print(json.dumps({"decision": "block", "reason": "Explain why"}))
    sys.exit(2)
sys.exit(0)

Node.js example — warning injection (PreCompact):

#!/usr/bin/env node
if (someCondition) {
  console.log(JSON.stringify({ systemMessage: "Warning: something needs attention" }));
}
process.exit(0);  // never block compaction

2. Set execution permission

Scripts without chmod +x fail silently or with permission denied, regardless of language:

chmod +x harness/scripts/your-script.py   # or .sh, .js, etc.

Alternatively, invoke the interpreter explicitly in the hook command (step 3) — then chmod +x is not required.

3. Register in hooks.json

Edit harness/hooks/hooks.json (SSOT). Reference the script via ${CLAUDE_PLUGIN_ROOT} and invoke with the appropriate interpreter:

{ "type": "command", "command": "bash \"${CLAUDE_PLUGIN_ROOT}/scripts/your-script.sh\"", "timeout": 5 }
{ "type": "command", "command": "python3 \"${CLAUDE_PLUGIN_ROOT}/scripts/your-script.py\"", "timeout": 5 }
{ "type": "command", "command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/your-script.js\"", "timeout": 5 }

For bin/harness subcommands, use timeout: 3. For scripts, use timeout: 5.

4. Validate

make validate   # runs tests/validate-plugin.sh

Checklist for New Hooks

  • Script has the correct shebang for its language (#!/usr/bin/env bash, #!/usr/bin/env python3, #!/usr/bin/env node)
  • Script is executable (chmod +x) or interpreter is explicit in the hook command (python3 "...", node "...")
  • Script path uses ${CLAUDE_PLUGIN_ROOT}/scripts/ — never absolute paths
  • Timeout matches type: 3 for bin/harness, 5 for scripts, 30 for agents
  • Tested locally by running the script directly with its interpreter
  • make validate passes after editing hooks.json

Common Issues

Hook not running (silent failure)

Three likely causes:

  1. Not executable — check with ls -la harness/scripts/your-script.*. Either chmod +x the file or prefix the command with the interpreter (python3 "...", node "...", bash "...")
  2. Wrong path — verify the path resolves correctly; run the script directly to confirm
  3. Syntax error — run the script manually (python3 script.py, node script.js, bash -n script.sh) to surface errors before the hook fires

Permission denied

chmod +x harness/scripts/your-script.py   # or .sh, .js
# OR use explicit interpreter in hooks.json — no chmod needed

Hook fires but doesn't block

For blocking hooks (PreToolUse / Stop), you must both print JSON to stdout and exit 2. Printing alone or exiting alone won't block.

agent hook not firing on PreCompact

Agent hooks silently do nothing on PreCompact. Convert to a command script — see harness/scripts/check-wip-precompact.sh as a reference.


Event Reference

Key events and their typical use:

EventUse for
PreToolUseGuard writes, validate inputs before execution
PostToolUseCleanup, logging, tracking after tool runs
StopBlock session end if work is incomplete
PreCompactWarn (don't block) before context compaction
UserPromptSubmitInject policy context, track commands
SessionStartOne-time session initialization (once: true)

Full event list and response schemas: .claude/rules/hooks-editing.md


Related

  • harness/hooks/hooks.json — SSOT for all hook configuration
  • harness/scripts/ — All command hook scripts
  • .claude/rules/hooks-editing.md — Complete event reference and timeout guidelines
  • update-config skill — For adding automated behaviors via settings.json

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

allow1

免费

Use when generating, vectorizing, or converting images to SVG — logos, icons, illustrations, or 'allow1'/'quiver' references.

中文简介正在准备中。当前显示原文说明。

tim-hub/powerball-harness352026年8月5日 更新

breezing

免费

Use when running the full team/breezing flow end-to-end — all tasks with parallel workers. Do NOT load for: single-task implementation, planning, review, release, or setup.

中文简介正在准备中。当前显示原文说明。

tim-hub/powerball-harness352026年8月5日 更新

Reviews PRs that modify the Feature Table in CLAUDE.md. Use when a PR adds CC version rows or modifies docs/CLAUDE-feature-table.md.

中文简介正在准备中。当前显示原文说明。

tim-hub/powerball-harness352026年8月5日 更新

Use when researching Claude Code or Codex upstream releases, selecting adaptations, or preparing Harness upgrades. Local-only; not for distribution.

中文简介正在准备中。当前显示原文说明。

tim-hub/powerball-harness352026年8月5日 更新

Creates a single project slide or one-page visual summary for a repo. Use when generating a project intro image or promotional slide.

中文简介正在准备中。当前显示原文说明。

tim-hub/powerball-harness352026年8月5日 更新

Generates product demo videos and release walkthroughs via Remotion. Use when creating video documentation or animated demos.

中文简介正在准备中。当前显示原文说明。

tim-hub/powerball-harness352026年8月5日 更新

查看 tim-hub 的全部技能

报告此技能的问题