Get the FREE Ultimate OpenClaw Setup Guide →

writing-rules

npx machina-cli add skill athola/claude-night-market/writing-rules --openclaw
Files (1)
SKILL.md
5.8 KB

Table of Contents

Hookify Rule Writing Guide

When To Use

  • Creating behavioral rules to prevent unwanted actions
  • Defining persistent guardrails for Claude Code sessions

When NOT To Use

  • Complex multi-step workflows - use agents instead
  • One-time operations that do not need persistent behavioral 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.

Quick Start

Create .claude/hookify.dangerous-rm.local.md:

---
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 --help flag to verify availability.

The rule activates immediately - no restart needed!

Rule File Format

Frontmatter Fields

name (required): Unique identifier (kebab-case) enabled (required): true or false event (required): bash, file, stop, prompt, or all action (optional): warn (default) or block pattern (simple): Regex pattern to match

Event Types

  • bash: Bash tool commands
  • file: Edit, Write, MultiEdit tools
  • stop: When agent wants to stop
  • prompt: User prompt submission
  • all: All events

Advanced Conditions

For multiple field checks:

---
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.

Operators

  • regex_match: Pattern matching
  • contains: Substring check
  • equals: Exact match
  • not_contains: Must NOT contain
  • starts_with: Prefix check
  • ends_with: Suffix check

Field Reference

bash events: command file events: file_path, new_text, old_text, content prompt events: user_prompt stop events: transcript

Pattern Writing

Regex Basics

  • \s - whitespace
  • \d - digit
  • \w - word character
  • . - any character (use \. for literal dot)
  • + - one or more
  • * - zero or more
  • | - OR

Examples

rm\s+-rf          → rm -rf
console\.log\(    → console.log(
chmod\s+777       → chmod 777

Test Patterns

python3 -c "import re; print(re.search(r'pattern', 'text'))"

Example Rules

Block Destructive Commands

---
name: block-destructive
enabled: true
event: bash
pattern: rm\s+-rf|dd\s+if=|mkfs
action: block
---

šŸ›‘ **Destructive operation blocked!**
Can cause data loss.

Warn About Debug Code

---
name: warn-debug
enabled: true
event: file
pattern: console\.log\(|debugger;
action: warn
---

šŸ› **Debug code detected!**
Remove before committing.

Require Tests

---
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.

Protect Production Files

---
name: protect-prod
enabled: true
event: file
action: block
conditions:
  - field: file_path
    operator: regex_match
    pattern: /production/|\.prod\.
---

🚨 **Production file!**
Requires review.

Management

Enable/Disable: Edit .local.md file: enabled: false

Delete:

rm .claude/hookify.my-rule.local.md

List:

/hookify:list

Related Skills

  • abstract:hook-scope-guide - Hook placement decisions
  • abstract:hook-authoring - SDK hook development
  • abstract:hooks-eval - Hook evaluation

Best Practices

  1. Start with simple patterns
  2. Test regex thoroughly
  3. Use clear, helpful messages
  4. Prefer warnings over blocks initially
  5. Name rules descriptively
  6. Document intent in messages

Troubleshooting

Common Issues

If a rule doesn't trigger, verify that the event type matches the tool being used (e.g., use bash for command line tools). Check that the regex pattern 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 .claude/, ensure that the directory is writable by your user.

Source

git clone https://github.com/athola/claude-night-market/blob/master/plugins/hookify/skills/writing-rules/SKILL.mdView on GitHub

Overview

Hookify rules are Markdown files with YAML frontmatter that define patterns to watch for and messages to show when those patterns match. They live in .claude/hookify.{rule-name}.local.md and provide persistent guardrails for Claude Code sessions.

How This Skill Works

Each rule specifies an event type (bash, file, stop, prompt, or all) and an action (warn or block). When a rule's pattern matches, Claude triggers the configured response and may include advanced conditions with multiple field checks to refine matches.

When to Use It

  • Creating behavioral rules to prevent dangerous commands
  • Blocking destructive actions like rm -rf
  • Blocking debug commits or code
  • Enforcing repository conventions and safety checks
  • Establishing persistent guardrails across Claude sessions

Quick Start

  1. Step 1: Create .claude/hookify.dangerous-rm.local.md with YAML frontmatter
  2. Step 2: Define name, enabled, event, pattern, action, and optional conditions
  3. Step 3: Verify activation (rules take effect immediately; no restart needed) and test with sample commands

Best Practices

  • Start with narrow patterns tied to a specific event to minimize false positives
  • Use multi-field conditions when possible to refine matches
  • Test rules in a safe environment before broad deployment
  • Avoid overly broad patterns that disrupt workflow
  • Version-control rule files and manage rollout with the enabled flag

Example Use Cases

  • Block dangerous rm -rf command in bash events
  • Warn when API keys appear in .env files
  • Protect production files by blocking edits
  • Warn about including debug code in commits or PRs
  • Require tests for changes to enforce quality

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers ↗