AgentSkillsCN

writing-rules

创建 Hookify 规则以预防不良 AI 行为的指南。当用户请求创建、编写或配置 Hookify 规则时,请使用此技能。

SKILL.md
--- frontmatter
name: writing-rules
description: >
  Guide for creating hookify rules to prevent unwanted AI behaviors.
  Use when user asks to create, write, or configure hookify rules.

Writing Hookify Rules

When to Use

  • User asks to create, write, or configure hookify rules
  • User wants to prevent specific behaviors in Claude Code

Process

  1. Identify the behavior to prevent
  2. Choose the event type (bash, file, stop, prompt, all)
  3. Write the pattern (regex or conditions)
  4. Create the rule file in .claude/hookify.{name}.local.md
  5. Test immediately — rules take effect on next tool use

Rule File Format

markdown
---
name: rule-identifier
enabled: true
event: bash|file|stop|prompt|all
pattern: regex-pattern-here
---

Message shown to Claude when rule triggers.

Event Types

EventMatches AgainstUse For
bashCommand textDangerous commands, privilege escalation
fileFile path + contentDebug code, sensitive files, security risks
stopAlways (use .*)Completion checklists, required steps
promptUser prompt textDeployment gates, process enforcement
allAll eventsCross-cutting concerns

Actions

  • warn (default): Show message, allow operation
  • block: Prevent operation entirely

Advanced: Multiple Conditions

yaml
conditions:
  - field: file_path
    operator: regex_match
    pattern: \.env$
  - field: new_text
    operator: contains
    pattern: API_KEY

Operators: regex_match, contains, equals, not_contains, starts_with, ends_with

Fields: bash → command | file → file_path, new_text, old_text, content | prompt → user_prompt

All conditions must match for the rule to trigger.

File Organization

  • Location: .claude/hookify.{descriptive-name}.local.md
  • Gitignore: Add .claude/*.local.md to .gitignore
  • Naming: Start with verb: warn-dangerous-rm, block-console-log, require-tests

See references/ for regex patterns, examples, and common pitfalls.