Pattern 7
A cheap hook beats a careful agent
Problem. An agent that has misread the task can overwrite your .env, delete a record, or force-push. "Be more careful" does not scale.
Pattern. A PreToolUse hook intercepts file writes and shell commands against a blocklist (sensitive paths, destructive verbs, pushes to protected branches, exec-hijacking env-var prefixes like GIT_SSH_COMMAND='…' git fetch) and blocks them before they run. It fails open: a bug in the guard must never wedge the session.
Why this beats the obvious. Trusting the model to never err is a hope, not a control. A ten-line deterministic check catches the large majority of accidental damage for almost nothing.
Cost. Occasional false positives (a legitimately named file that matches a protected substring), best resolved by naming around them rather than widening the gap.
Where it lives: samples/scripts/security/check_bash_command.py and the hook config in samples/.claude/settings.example.json.
Seen in 4 of 8 teardowns · Practise: learn/04