crush-hooks
它能做什么
这项技能帮助你为 Crush 这一编程工具编写钩子。钩子会在设定的节点(例如工具调用之前)运行你自己的命令,以允许、阻止或修改该调用。适合希望对工具的能力设定严格规则的用户。
适用场景
- ✓当你想在 Crush 中阻止或允许某些工具调用的时候。
- ✓当你想在工具运行前修改其输入,或为工具结果添加上下文的时候。
- ✓当 Crush 钩子的行为不符合预期,需要调试的时候。
技能链接
https://github.com/charmbracelet/crush/blob/main/internal/skills/builtin/crush-hooks/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一个 Crush 钩子,阻止任何删除我项目文件夹之外文件的 shell 命令。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会在 crush.json 中为正确的事件(例如 PreToolUse)编写钩子。钩子在权限检查之前运行,并且只针对主智能体的工具调用,不针对子智能体。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Crush Hooks
Hooks are user-defined commands in crush.json that fire at specific points
during execution, giving deterministic control over tool behavior. They run
before permission checks and only on the top-level agent's tool calls —
sub-agent calls (task tool, agentic_fetch, etc.) are not intercepted, though
the sub-agent tool call itself is.
For the full reference, see docs/hooks/README.md. This skill covers what you
need to author correct hooks.
Supported Events
Crush supports 13 hook events: PreToolUse, PostToolUse,
PostToolUseFailure, PermissionRequest, PermissionResult,
SessionStart, SessionEnd, TurnStart, TurnEnd, StopFailure,
Interrupt, PreCompact, PostCompact. Event names are case-insensitive
and accept snake_case (PreToolUse, pretooluse, pre_tool_use all work);
an unknown name is a config error. The five events that carry a tool call
(PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest,
PermissionResult) accept matcher; the rest run every configured hook and
reject matcher.
Only PreToolUse (allow/deny/halt/rewrite/context), PostToolUse
(halt/replace output/context/feedback), PostToolUseFailure
(halt/context), and PreCompact (block) honor output. The rest are
observe-only. Turn and tool events fire only for the top-level agent, never
for sub-agents (agentic_fetch, the task tool), so a TurnEnd hook runs
once per user prompt.
Per-event payload extras: PostToolUse has tool_response (the output
string), PostToolUseFailure and StopFailure have error, TurnEnd has
last_assistant_message, PermissionResult has decision
(granted/denied/cancelled), SessionStart has source
(startup/resume), SessionEnd has reason, and the compaction events
have trigger (auto/manual). Payloads also carry hook_event_name and
tool_use_id, and hooks see CLAUDE_PROJECT_DIR, so scripts written for
Claude Code usually work unchanged.
Configuration
{
"hooks": {
"PreToolUse": [
{
"matcher": "^bash$", // regex against tool name (optional; omit to match all)
"command": "./hooks/my-hook.sh", // required: shell command to run
"timeout": 10 // optional: seconds, default 30
}
]
}
}
Project-level hooks take precedence over global. Matching hooks are deduped by
command, run in parallel, and aggregated in config order (not finish order).
Language
command is a shell command, so hooks can be written in any language by
invoking the interpreter: node ./hooks/h.js, python3 ./hooks/h.py,
./hooks/h.sh, inline echo '…', etc. The rest of this skill shows bash, but
the input/output contract is identical regardless of language.
Input
Environment variables:
| Variable | Description |
|---|---|
CRUSH_EVENT |
Event name (e.g. PreToolUse) |
CRUSH_TOOL_NAME |
Tool being called (e.g. bash) |
CRUSH_SESSION_ID |
Current session ID |
CRUSH_CWD |
Working directory |
CRUSH_PROJECT_DIR |
Project root directory |
CLAUDE_PROJECT_DIR |
Same value, for Claude Code scripts |
CRUSH_TOOL_INPUT_COMMAND |
For bash calls: the shell command |
CRUSH_TOOL_INPUT_FILE_PATH |
For file tools: the target file path |
The CRUSH_TOOL_* variables are only set on events that carry a tool call.
JSON on stdin:
{
"event": "PreToolUse",
"hook_event_name": "PreToolUse",
"session_id": "313909e",
"cwd": "/home/user/project",
"tool_name": "bash",
"tool_input": {"command": "rm -rf /"},
"tool_use_id": "call_01"
}
Output
Communicate back via exit code (+ stderr) or JSON on stdout.
| Exit Code | Meaning |
|---|---|
| 0 | Success. Stdout is parsed as the JSON envelope below. |
| 2 | Block this tool call. Stderr becomes the deny reason. |
| 49 | Halt the whole turn. Stderr becomes the halt reason. |
| Other | Non-blocking error unless stdout holds a JSON object. |
Exit 2 blocks one tool call (agent sees the rea
常见问题
钩子可以使用哪些事件?
Crush 支持 13 种事件,例如 PreToolUse、PostToolUse 和 SessionStart。
钩子会对子智能体运行吗?
不会,钩子只对顶层智能体的工具调用运行,不对子智能体的调用运行。
钩子在哪里配置?
钩子在 crush.json 文件中设置。
详情
作者的原始描述
Use when the user wants to add, write, debug, or configure a Crush hook — gating or blocking tool calls, approving or rewriting tool input before execution, injecting context into tool results, or troubleshooting hook behavior in crush.json.
repo: charmbracelet/crush
热度
- GitHub 星标
- 29k
- 排名
- #9766
- 许可证
- —
- 更新时间
- 2026/11/2
评论
- …