用途:编写持久化的行为规则,防止 Claude Code 执行不需要的操作,为会话定义安全护栏。复杂多步工作流应改用 agents;一次性操作无需此类持久规则。
主要能力:规则文件为带 YAML frontmatter 的 markdown,存放于 .claude/hookify.{rule-name}.local.md。frontmatter 字段包括 name(必填,kebab-case 唯一标识)、enabled(必填,true/false)、event(必填:bash、file、stop、prompt 或 all)、action(可选:warn 默认 或 block)、pattern(简单正则)。多字段检查用 conditions,含 field、operator、pattern;operator 支持 regex_match、contains、equals、not_contains、starts_with、ends_with。字段引用:bash 事件用 command;file 事件用 file_path、new_text、old_text、content;prompt 事件用 user_prompt;stop 事件用 transcript。
使用方式:创建规则文件后立即生效,无需重启。示例正则:rm\s+-rf、console.log(、chmod\s+777。测试模式可运行 python3 -c "import re; print(re.search(r'pattern', 'text'))"。启用/禁用通过编辑文件 enabled 字段;删除用 rm .claude/hookify.my-rule.local.md;列出用 /hookify:list。
适用场景:阻止破坏性命令(rm -rf、dd if=、mkfs)、提示调试代码(console.log(、debugger;)、要求运行测试、保护生产文件等。
限制:规则不匹配指定 event 类型时不会触发;正则需先经测试验证,避免误报;在 .claude/ 下创建文件需该目录可写。建议从简单模式开始、优先使用 warn 再考虑 block、命名具描述性并在消息中说明意图。