Hook Matchers: Precision Targeting

Day 36 · Week 8 · Hooks — Guardrails

Slack Message — copy & paste

🤖 Tip #36 — `matcher` selects the tool, `if` selects the arguments. `"matcher": "Bash"` + `"if": "Bash(git push --force*)"` hits only force pushes.

#36 Claude Code

Matcher Syntax

  • `matcher` matches the tool NAME only — three ways it is read
    • › `""`, `"*"` or omitted — every tool
    • › Only letters, digits, `_`, `-`, spaces, `,` and `|` — exact names: `"Bash"`, `"Edit|Write"`
    • › Anything else — an unanchored JavaScript regex: `"mcp__.*figma.*"`, `"^Notebook"`
  • The trap: permission-rule syntax in `matcher`
    • › `"Bash(git commit*)"` contains `(`, `)` and `*`, so it is read as a regex — "Bash" followed by "git commi" — and no tool name contains that
    • › The hook never fires and nothing warns you. `claude plugin validate` accepts it too
    • › Our own branch guard shipped this way for months
  • The `if` field — argument filtering, permission rule syntax
    • › `"matcher": "Bash"` + `"if": "Bash(git commit*)"` — fires on Bash, spawns the script only for git commits
    • › `"matcher": "Edit"` + `"if": "Edit(**/.claude-plugin/**)"` — only validate plugin file edits
    • › Evaluated before the hook process spawns; one rule per handler
    • › Best-effort, and other tools (Copilot CLI) ignore it — so the script should re-check its input too
Matchers Wildcards Patterns
#36 Hooks — Guardrails

Matchers for Every Risk

  1. 1 A filter for every risk level (all with `"matcher": "Bash"` unless noted)
    • › `"if": "Bash(rm -rf*)"` — catch destructive deletions
    • › `"if": "Bash(git reset --hard*)"` — catch history destruction
    • › `"matcher": "Write"` + `"if": "Write(*.env*)"` — catch secret file creation
  2. 2 List the 3 most dangerous commands for your project — write an `if` rule for each and you have a safety net
  3. 3 Never put parentheses in `matcher` — the tool name goes there, the argument pattern goes in `if`
  4. 4 Test every guard by asking the AI to run the matched command — a hook you have not watched fire is a hook you do not have
Matchers Wildcards Patterns
Your screenshot here Optional — add a screenshot from your own workflow
KAI by Dragan Filipovic