r/ClaudeCoder • • 1d ago

I turned "don't touch src/payments" from a CLAUDE.md line into a PreToolUse hook. Here's what it took to make it hold

Rules in CLAUDE.md work until a long session gets compacted and the rule gets summarised away. I wanted "don't edit these files" to be enforced, not just remembered. Here's the setup and the bypasses I had to close.

The hook

In .claude/settings.local.json:

"PreToolUse": [{
  "matcher": "Edit|Write|MultiEdit|NotebookEdit|Bash|PowerShell",
  "hooks": [{ "type": "command", "command": "tagteam guard --hook" }]
}]

It reads the tool call from stdin and checks the path against your rules. On a match it exits 2 with the reason on stderr: Claude Code refuses the call and Claude reads why.

What I learned

  • The message matters as much as the block. My first version said how to switch the guard off, and Claude tried exactly that. Now it says "stop and ask the user: only they can lift a rule". In real sessions it stops and asks.
  • Blocking Edit isn't enough. The next move is sed -i, >, tee, mv, rm. So the hook also parses Bash commands:

    • redirections, sed -i / perl -i, cp / mv / rm, git checkout --, PowerShell's Set-Content
    • rm -rf src when src/payments is inside it
    • heredoc bodies are skipped, and cd is followed
  • Case and symlinks. SRC/Payments/x.js is the same file on Windows and macOS, and a symlinked folder can reach a protected path. Both needed handling.
  • Protect the guard itself. The agent can't edit the settings file that runs the hook, and guard off refuses to run without a real terminal.
  • It's not airtight. A script or python -c can still write. So the same rule is also checked at handover (tagteam status against the diff) and in CI (a GitHub Action fails the PR).

Set it up

npm i -g tagteam-ai
tagteam install claude
tagteam constraint "Don't change the payment gateway" --paths "src/payments/**"

The same tool also saves a git-checked handoff on /compact and exit, and loads it back at the next SessionStart. That's what it was originally for.

Affiliation: I'm the author. Free, Apache-2.0, everything local. https://github.com/MHRabea/tagteam

If you write your own hooks: what bypass would you try next?

1 Upvotes

0 comments sorted by