Skip to main content

Permissions and Hooks: What Claude Code May Do Here

Why This Matters

A rule in CLAUDE.md is context: Claude Code treats it as guidance, not enforced configuration. A permission or a hook is enforcement. For anything that must never happen in the TestPilot repository, you want enforcement.

Permissions

Permissions in .claude/settings.json decide which tool calls run without asking, which ask first, and which are refused:

{
  "permissions": {
    "allow": ["Bash(npm run check)", "Bash(npm test)"],
    "ask": ["Bash(git push:*)"],
    "deny": ["Read(./.env)", "Read(./.env.*)"]
  }
}

Committed in project settings, these apply to everyone. Remember from lesson 42: a deny in any scope wins.

/permissions in Claude Code shows the result, one tab per kind of rule. The Allow tab for TestPilot: the two check commands and the three read-only testpilot tools, which Claude Code now runs without asking:

Claude Code’s /permissions panel, Allow tab: Bash(npm run check), Bash(npm test), mcp__testpilot__get_test_history, mcp__testpilot__read_log, mcp__testpilot__search_issues

The Ask tab: pushing, and the three scripts that call the Claude API and cost money. Claude Code may run them, but only after you confirm each time:

Claude Code’s /permissions panel, Ask tab: Bash(git push:), Bash(npm run eval:), Bash(npm run team:), Bash(npm run triage:)

And the Deny tab: .env can never be read, whatever you approve in the moment, and a deny anywhere beats an allow anywhere else:

Claude Code’s /permissions panel, Deny tab: Read(./.env.*) and Read(./.env)

What that looks like when someone asks for it. Claude Code did not use the Read tool at all: it tried a shell command that would only show the variable names and the length of each value. The project’s deny rule stopped that too, and Claude did not look for a third way:

Asked to show .env, Claude Code tries a masked view with sed and awk; the call is not run: “permission denied · rule from shared project settings”. It explains the block is deliberate and suggests checking the file outside the session

Two things to take from this. A well-meant workaround (a masked view) is exactly what a deny rule must also catch, so write deny rules for the file, not for one tool. And the rule lives in the shared project settings, so it protects every developer who clones the repository, not only the one who thought of it.

Hooks

Hooks are commands that run at fixed moments in Claude Code’s lifecycle, configured in the same settings files. The two you will use most:

  • PreToolUse runs before a tool call. Exiting with code 2 blocks the call and shows your message (stderr) to Claude, so it can adjust.
  • PostToolUse runs after a tool call succeeded. It cannot block (the action already happened), but its message is shown to Claude.

Any other non-zero exit code is reported as a hook error and does not block.

Protect the golden set

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [{ "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-golden.sh" }]
      }
    ]
  }
}
#!/usr/bin/env bash
# .claude/hooks/protect-golden.sh: the tool call arrives as JSON on stdin
file=$(jq -r '.tool_input.file_path // empty')
if [[ "$file" == *"fixtures/golden/"* ]] && [[ ! -f "$CLAUDE_PROJECT_DIR/.golden-change-reason" ]]; then
  echo "Golden labels are protected. Write the reason for this change to .golden-change-reason first." >&2
  exit 2
fi
exit 0

Make the script executable (chmod +x).

In practice the hook rarely has to fire, because Claude Code knows it is there. Asked to change a golden label, it did not even try the edit. It named the hook and the golden-set rule, refused to invent a reason, proposed the related changes to the case, and pointed out that npm run eval costs tokens:

Asked to change golden-003 to test_bug, Claude Code asks for a reason first: the hook blocks golden edits without .golden-change-reason, and the rule requires a reason in the commit message

Told to try anyway, it sent the edit, and the hook stopped it before the tool ran. Note the Loaded .claude/rules/golden.md line as well: the path rule arrived with the first touch of the golden set:

The Update of fixtures/golden/cases.json fails with “PreToolUse:Edit hook error: Golden labels are protected. Write the reason for this change to .golden-change-reason first.”; Claude Code reports the file is unchanged and explains that the hook runs before the tool

The file was never touched, and the block message came back to Claude as feedback, so it could explain what the hook wants instead of retrying blindly. This was in auto mode, where Claude Code approves most tool calls on its own: hooks still run, because they are code, not a question to the user.

That is the layering working: the CLAUDE.md line and the path rule make the right behaviour likely; the hook makes the wrong one impossible, for the session where the instructions are missed.

A hole in this hook. It only checks that .golden-change-reason exists, and Claude could write that file itself. A reviewer agent found that in lesson 52. The version on the solution branch closes it: Claude may never write the reason file (the hook blocks it, and a deny rule on Edit, which covers every file-writing tool, backs that up; a Write(path) rule would match nothing), the hook also covers Bash commands that touch the golden set, and it fails closed when it cannot read its input. Start with the short version above to understand the idea; use the full one in a real project.

Check every edit

A PostToolUse hook that runs the typecheck after every edit gives Claude immediate feedback instead of a surprise at the end:

"PostToolUse": [
  { "matcher": "Edit|Write", "hooks": [{ "type": "command", "command": "npx tsc --noEmit >&2 || exit 2" }] }
]

Rule, permission or hook?

RequirementMechanism
“Prefer small functions”CLAUDE.md (guidance)
“Never read .env”Permission (deny)
“Never edit golden labels without a reason”PreToolUse hook (custom logic)
“Typecheck after every edit”PostToolUse hook

The same idea in your own agents

The Agent SDK supports hooks around tool calls too. In module 7 you use them in TestPilot itself to log, block or change a tool call before it runs.

Exercise

Add the permissions, the protect-golden hook and the typecheck hook to TestPilot. Ask Claude Code to change a golden label and watch the hook stop it.

Compare with solution/lesson-52-claude-code, which has the Claude Code setup for lessons 42–52. Its docs/claude-code-demo.md walks you through all of module 6 in one session.