writing-rules
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTable of Contents
目录
- Overview
- Quick Start
- Rule File Format
- Frontmatter Fields
- Event Types
- Advanced Conditions
- Operators
- Field Reference
- Pattern Writing
- Regex Basics
- Examples
- Test Patterns
- Example Rules
- Block Destructive Commands
- Warn About Debug Code
- Require Tests
- Protect Production Files
- Management
- Related Skills
- Best Practices
Hookify Rule Writing Guide
Hookify规则编写指南
When To Use
适用场景
- Creating behavioral rules to prevent unwanted actions
- Defining persistent guardrails for Claude Code sessions
- 创建行为规则以阻止不必要的操作
- 为Claude Code会话定义持久化的防护规则
When NOT To Use
不适用场景
- Complex multi-step workflows - use agents instead
- One-time operations that do not need persistent behavioral rules
- 复杂的多步骤工作流 - 请使用Agent替代
- 不需要持久化行为规则的一次性操作
Overview
概述
Hookify rules are markdown files with YAML frontmatter that define patterns to watch for and messages to show when those patterns match. Rules are stored in files.
.claude/hookify.{rule-name}.local.mdHookify规则是带有YAML前置元数据的Markdown文件,用于定义需要监控的匹配模式,以及模式匹配时显示的提示信息。规则存储在文件中。
.claude/hookify.{rule-name}.local.mdQuick Start
快速开始
Create :
.claude/hookify.dangerous-rm.local.mdyaml
---
name: dangerous-rm
enabled: true
event: bash
pattern: rm\s+-rf
action: block
---
🛑 **Dangerous rm command detected!**
This command could delete important files.Verification: Run the command with flag to verify availability.
--helpThe rule activates immediately - no restart needed!
创建文件:
.claude/hookify.dangerous-rm.local.mdyaml
---
name: dangerous-rm
enabled: true
event: bash
pattern: rm\s+-rf
action: block
---
🛑 **检测到危险的rm命令!**
该命令可能会删除重要文件。验证: 携带参数运行命令以验证可用性。
--help规则会立即生效 - 无需重启!
Rule File Format
规则文件格式
Frontmatter Fields
前置元数字段
name (required): Unique identifier (kebab-case)
enabled (required): or
event (required): , , , , or
action (optional): (default) or
pattern (simple): Regex pattern to match
truefalsebashfilestoppromptallwarnblockname(必填):唯一标识符(短横线命名法)
enabled(必填): 或
event(必填):、、、 或
action(可选):(默认)或
pattern(简单模式):用于匹配的正则表达式
truefalsebashfilestoppromptallwarnblockEvent Types
事件类型
- bash: Bash tool commands
- file: Edit, Write, MultiEdit tools
- stop: When agent wants to stop
- prompt: User prompt submission
- all: All events
- bash:Bash工具命令
- file:Edit、Write、MultiEdit工具操作
- stop:当Agent想要停止会话时
- prompt:用户提交提示时
- all:所有事件
Advanced Conditions
高级条件
For multiple field checks:
yaml
---
name: warn-env-edits
enabled: true
event: file
action: warn
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
🔐 **API key in .env file!**
Ensure file is in .gitignore.如需多字段检查:
yaml
---
name: warn-env-edits
enabled: true
event: file
action: warn
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
🔐 **检测到.env文件中包含API密钥!**
请确保该文件已添加到.gitignore中。Operators
操作符
- : Pattern matching
regex_match - : Substring check
contains - : Exact match
equals - : Must NOT contain
not_contains - : Prefix check
starts_with - : Suffix check
ends_with
- :模式匹配
regex_match - :子字符串检查
contains - :精确匹配
equals - :必须不包含
not_contains - :前缀检查
starts_with - :后缀检查
ends_with
Field Reference
字段参考
bash events:
file events: , , ,
prompt events:
stop events:
commandfile_pathnew_textold_textcontentuser_prompttranscriptbash事件:
file事件:、、、
prompt事件:
stop事件:
commandfile_pathnew_textold_textcontentuser_prompttranscriptPattern Writing
模式编写
Regex Basics
正则表达式基础
- - whitespace
\s - - digit
\d - - word character
\w - - any character (use
.for literal dot)\. - - one or more
+ - - zero or more
* - - OR
|
- - 空白字符
\s - - 数字
\d - - 单词字符
\w - - 任意字符(使用
.匹配字面量点)\. - - 一个或多个
+ - - 零个或多个
* - - 或
|
Examples
示例
rm\s+-rf → rm -rf
console\.log\( → console.log(
chmod\s+777 → chmod 777rm\s+-rf → rm -rf
console\.log\( → console.log(
chmod\s+777 → chmod 777Test Patterns
测试模式
bash
python3 -c "import re; print(re.search(r'pattern', 'text'))"bash
python3 -c "import re; print(re.search(r'pattern', 'text'))"Example Rules
规则示例
Block Destructive Commands
拦截破坏性命令
yaml
---
name: block-destructive
enabled: true
event: bash
pattern: rm\s+-rf|dd\s+if=|mkfs
action: block
---
🛑 **Destructive operation blocked!**
Can cause data loss.yaml
---
name: block-destructive
enabled: true
event: bash
pattern: rm\s+-rf|dd\s+if=|mkfs
action: block
---
🛑 **破坏性操作已被拦截!**
可能会导致数据丢失。Warn About Debug Code
调试代码警告
yaml
---
name: warn-debug
enabled: true
event: file
pattern: console\.log\(|debugger;
action: warn
---
🐛 **Debug code detected!**
Remove before committing.yaml
---
name: warn-debug
enabled: true
event: file
pattern: console\.log\(|debugger;
action: warn
---
🐛 **检测到调试代码!**
提交前请移除。Require Tests
要求执行测试
yaml
---
name: require-tests
enabled: true
event: stop
action: warn
conditions:
- field: transcript
operator: not_contains
pattern: pytest|npm test
---
⚠️ **Tests not run!**
Please verify changes.yaml
---
name: require-tests
enabled: true
event: stop
action: warn
conditions:
- field: transcript
operator: not_contains
pattern: pytest|npm test
---
⚠️ **未执行测试!**
请验证更改内容。Protect Production Files
保护生产文件
yaml
---
name: protect-prod
enabled: true
event: file
action: block
conditions:
- field: file_path
operator: regex_match
pattern: /production/|\.prod\.
---
🚨 **Production file!**
Requires review.yaml
---
name: protect-prod
enabled: true
event: file
action: block
conditions:
- field: file_path
operator: regex_match
pattern: /production/|\.prod\.
---
🚨 **生产环境文件!**
需要审核。Management
管理
Enable/Disable:
Edit file:
.local.mdenabled: falseDelete:
bash
rm .claude/hookify.my-rule.local.mdList:
bash
/hookify:list启用/禁用:
编辑文件:
.local.mdenabled: false删除:
bash
rm .claude/hookify.my-rule.local.md列出所有规则:
bash
/hookify:listRelated Skills
相关技能
- abstract:hook-scope-guide - Hook placement decisions
- abstract:hook-authoring - SDK hook development
- abstract:hooks-eval - Hook evaluation
- abstract:hook-scope-guide - Hook作用域决策
- abstract:hook-authoring - SDK Hook开发
- abstract:hooks-eval - Hook评估
Best Practices
最佳实践
- Start with simple patterns
- Test regex thoroughly
- Use clear, helpful messages
- Prefer warnings over blocks initially
- Name rules descriptively
- Document intent in messages
- 从简单模式开始
- 彻底测试正则表达式
- 使用清晰、有帮助的提示信息
- 初期优先使用警告而非拦截
- 为规则起描述性名称
- 在提示信息中说明规则意图
Troubleshooting
故障排除
Common Issues
常见问题
If a rule doesn't trigger, verify that the type matches the tool being used (e.g., use for command line tools). Check that the regex is valid and matches the target text by testing it with a short Python script. If you encounter permission errors when creating rule files in , ensure that the directory is writable by your user.
eventbashpattern.claude/如果规则未触发,请验证类型是否与所用工具匹配(例如,针对命令行工具使用类型)。通过简短的Python脚本测试正则表达式是否有效且能匹配目标文本。如果在目录中创建规则文件时遇到权限错误,请确保该目录对当前用户可写。
eventbashpattern.claude/