writing-hookify-rules
它能做什么
它为你的智能体提供编写 hookify 规则的语法和模式。这些小文件用于对某些操作发出警告或加以阻止。适合想为 Claude Code 设置防护规则的开发者。
适用场景
- ✓当你想要一条在某个命令或编辑发生时发出警告的规则的时候。
- ✓当你想要一条阻止某个危险操作运行的规则的时候。
- ✓当你想修复或理解现有的 hookify 规则文件的时候。
技能链接
https://github.com/anthropics/claude-plugins-official/blob/main/plugins/hookify/skills/writing-rules/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我写一条 hookify 规则,在任何命令用 rm 删除文件之前警告我?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会编写一个规则文件,包括名称、开关、监视的事件和匹配模式。根据你的需要,它会把动作设为警告或阻止。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Writing Hookify Rules
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 .claude/hookify.{rule-name}.local.md files.
Rule File Format
Basic Structure
---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: regex-pattern-here
---
Message to show Claude when this rule triggers.
Can include markdown formatting, warnings, suggestions, etc.
Frontmatter Fields
name (required): Unique identifier for the rule
- Use kebab-case:
warn-dangerous-rm,block-console-log - Be descriptive and action-oriented
- Start with verb: warn, prevent, block, require, check
enabled (required): Boolean to activate/deactivate
true: Rule is activefalse: Rule is disabled (won't trigger)- Can toggle without deleting rule
event (required): Which hook event to trigger on
bash: Bash tool commandsfile: Edit, Write, MultiEdit toolsstop: When agent wants to stopprompt: When user submits a promptall: All events
action (optional): What to do when rule matches
warn: Show message but allow operation (default)block: Prevent operation (PreToolUse) or stop session (Stop events)- If omitted, defaults to
warn
pattern (simple format): Regex pattern to match
- Used for simple single-condition rules
- Matches against command (bash) or new_text (file)
- Python regex syntax
Example:
event: bash
pattern: rm\s+-rf
Advanced Format (Multiple Conditions)
For complex rules with multiple conditions:
---
name: warn-env-file-edits
enabled: true
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.env$
- field: new_text
operator: contains
pattern: API_KEY
---
You're adding an API key to a .env file. Ensure this file is in .gitignore!
Condition fields:
field: Which field to check- For bash:
command - For file:
file_path,new_text,old_text,content
- For bash:
operator: How to matchregex_match: Regex pattern matchingcontains: Substring checkequals: Exact matchnot_contains: Substring must NOT be presentstarts_with: Prefix checkends_with: Suffix check
pattern: Pattern or string to match
All conditions must match for rule to trigger.
Message Body
The markdown content after frontmatter is shown to Claude when the rule triggers.
Good messages:
- Explain what was detected
- Explain why it's problematic
- Suggest alternatives or best practices
- Use formatting for clarity (bold, lists, etc.)
Example:
⚠️ **Console.log detected!**
You're adding console.log to production code.
**Why this matters:**
- Debug logs shouldn't ship to production
- Console.log can expose sensitive data
- Impacts browser performance
**Alternatives:**
- Use a proper logging library
- Remove before committing
- Use conditional debug builds
Event Type Guide
bash Events
Match Bash command patterns:
---
event: bash
pattern: sudo\s+|rm\s+-rf|chmod\s+777
---
Dangerous command detected!
Common patterns:
- Dangerous commands:
rm\s+-rf,dd\s+if=,mkfs - Privilege escalation:
sudo\s+,su\s+ - Permission issues:
chmod\s+777,chown\s+root
file Events
Match Edit/Write/MultiEdit operations:
---
event: file
pattern: console\.log\(|eval\(|innerHTML\s*=
---
Potentially problematic code pattern detected!
Match on different fields:
---
event: file
conditions:
- field: file_path
operator: regex_match
pattern: \.tsx?$
- field: new_text
operator: regex_match
pattern: console\.log\(
---
Console.log in TypeScript file!
Common patterns:
- Debug code:
console\.log\(,debugger,print\( - Security risks:
eval\(,innerHTML\s*=,dangerouslySetInnerHTML - Sensitive files:
\.env$,credentials,\.pem$ - Generated files:
node_modules/,dist/,build/
stop Events
Match when agent wants to stop (completion checks):
---
event: stop
pattern: .*
---
Before stopping, verify:
- [ ] Tests were run
- [ ] Build succeeded
- [ ] Documentation updated
Use for:
- Reminders about required steps
- Completion checklists
- Process enforcement
prompt Events
Match user prompt content (advanced):
---
event: prompt
conditions:
- field: user_prompt
常见问题
规则存放在哪里?
技能说明,规则以本地 Markdown 文件的形式存放在项目的 .claude 文件夹中。
规则可以监视哪些事件?
技能列出了 Bash 命令、文件编辑、停止事件、用户提示,以及所有事件。
我能否关闭一条规则而不删除它?
可以。每条规则都有一个 enabled 设置,可以关闭。
详情
作者的原始描述
This skill should be used when the user asks to "create a hookify rule", "write a hook rule", "configure hookify", "add a hookify rule", or needs guidance on hookify rule syntax and patterns.
热度
- GitHub 星标
- 46k
- 排名
- #6336
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/2
评论
- …