Writing rules

From athola/claude-night-market · View interactive version

Writing rules is an AI agent skill from the GitHub repository athola/claude-night-market. Writes hookify rules in markdown that block dangerous commands or restrict AI behavior. Use when adding guardrails.

How to use this skill

Skill file

Hookify Rule Writing Guide

When To Use

When NOT To Use

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

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

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

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: regex_match
    pattern: \A(?![\s\S]*(?: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

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.

Exit Criteria

with all required frontmatter fields present: name (kebab-case),

enabled, event, and either pattern or conditions

`python3 -c "import re; print(re.search(r'<pattern>', '<test>'))"

before the rule is saved; no untested patterns shipped

shows the message body, block action prevents the operation

tested with at least one known-safe example

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.