
Rules That Can't Bend Belong in Hooks, Not in Louder CLAUDE.md Lines
- كاتب موثّق
- Claude Code 2.x
محتويات المقال
db/migrations/0042_add_refunds.sql, which staging applied last week. The tests rebuild their database from scratch, so they pass. Staging never re-runs 0042, and its schema quietly drifts from what the code expects. The fix someone pushes is "IMPORTANT: NEVER EDIT AN EXISTING MIGRATION!!!". The next week Claude asks before creating any file in db/migrations/, even the new migration the task needs.
The common misconception
Claude does follow CLAUDE.md nearly every time, so the rare miss looks like a wording problem. Emphasis used to help. Older models sometimes needed IMPORTANT and capital letters to notice a line. is the documented home for project instructions and loads into every session, so it feels like configuration. Adding a word takes five seconds. Writing a script takes an afternoon, or so it seems.
What actually happens
Two kinds of instructions
PreToolUse hook instead.
Emphasis changes the weighting, not the guarantee
A hook runs before the action
PreToolUse hook fires after Claude has prepared a tool call and before Claude Code runs it. Claude Code pipes the call to your script as JSON on stdin: tool_name, and tool_input with fields such as file_path. Your exit code decides:
Exit 0: no objection. The call goes through the normal permission flow; exit 0 doesn't approve it. Exit 2: the call is blocked, and whatever you wrote to stderr reaches Claude as the tool's error, so it can change course instead of retrying blindly.
bypassPermissions, and hooks from settings files also fire for tool calls made inside subagents. Claude doesn't have to remember the rule, because Claude isn't the one applying it.
The same rule, as a line and as a hook
# Database- IMPORTANT: NEVER EDIT AN EXISTING MIGRATION!!!- ALWAYS add a new one instead. NO EXCEPTIONS.{ "hooks": { "PreToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-migrations.sh", "onFailure": "block" } ] } ] }}#!/bin/bash# PreToolUse hook: committed migrations are immutableset -e # a missing jq exits non-zero instead of allowing the editFILE_PATH=$(jq -r '.tool_input.file_path // empty')FILE_PATH="${FILE_PATH//\\//}" # normalize Windows separatorsif [[ "$FILE_PATH" == */db/migrations/* ]] && git -C "$CLAUDE_PROJECT_DIR" ls-files --error-unmatch \ "$FILE_PATH" >/dev/null 2>&1; then echo "Blocked: $FILE_PATH is committed and may be applied." >&2 echo "Add a new migration: pnpm db:migrate:new <name>" >&2 exit 2fiexit 0 # no decision: the normal permission flow applieschmod +x, then ask Claude to change the column in 0042. The edit never happens: Claude gets the "Blocked:" message as the tool's error and writes 0043 instead. CLAUDE.md keeps one plain line, "Schema changes go in a new migration: pnpm db:migrate:new <name>", so Claude does the right thing first and rarely meets the hook.
What a hook doesn't see
A matcher on Edit|Writenever fires forsed -ior a>redirect run through Bash. The docs suggest also matchingBash|PowerShelland listing changed files withgit status --porcelain, or aStophook that scans the working tree once per turn.Files you @-mention in a prompt are inserted without a tool call, so noPreToolUsehook fires for them. To keep a file out entirely, use aReaddeny rule.A PostToolUsehook can't undo anything, because the tool already ran. Blocking belongs inPreToolUse.A promptoragenthook asks another model to decide, so it isn't deterministic either. Real enforcement is acommandorhttphook.
Line, hook or deny rule
PreToolUse hook | |||
|---|---|---|---|
bypassPermissions | |||
How to apply it in your project
Find the shouting lines Search your CLAUDE.md files for IMPORTANT, NEVER, ALWAYS and capital letters. Each one is a rule someone found hard to get followed. Sort each line If the rule has exceptions, rewrite it plainly and leave it to Claude. If it has none, it needs enforcement. Pick the mechanism A path or command that's simply off-limits becomes a deny rule in .claude/settings.json. A rule that needs logic or a redirect message becomes aPreToolUsehook. A step that must follow every edit becomes aPostToolUsehook.Make it fail closed and prove it Add onFailure: "block", pipe sample JSON through the script, then ask Claude to break the rule and check the transcript withCtrl+O.Keep the enforced set small Every hook and deny rule will occasionally fire when it shouldn't, and someone maintains each one. The docs' rule of thumb: at forty of them, most are preferences Claude could have weighed itself.
Key takeaways
CLAUDE.md is context Claude weighs; capital letters change the weighting, not the guarantee. A PreToolUsehook runs before the tool call; exit 2 blocks it and sends your stderr to Claude.A hook's deny holds in every permission mode and inside subagents. Hooks see only what they match: cover Bash writes, and set onFailure: "block"so a broken hook fails closed.Enforce the few rules with zero exceptions; leave guidelines in CLAUDE.md, written plainly.
التعليقات (0)