ยท Jonathan Mondaut

Claude Code hooks and deny rules: stop a bad command before it runs

An instruction asks the agent to behave. A hook or a deny rule makes it. If your CLAUDE.md says "use yarn" and the agent runs npm install anyway, the instruction did its best. A deny rule on npm would have refused the command before it ran.

Claude Code gives you two ways to do that: permission rules and hooks.

Deny rules: the simplest guard

Permission rules live in .claude/settings.json. A rule names a tool and a pattern; deny refuses, ask asks you first, allow lets it run without a question. A deny rule wins over an allow rule.

React's repository keeps its agents on yarn with six deny rules. Four of them:

{
  "permissions": {
    "deny": ["Bash(npm:*)", "Bash(pnpm:*)", "Bash(bun:*)", "Bash(npx:*)"]
  }
}

Rules are not only for shell commands. Supabase's repository refuses edits to generated files, so the agent changes the source and regenerates instead of patching the output:

{
  "permissions": {
    "deny": ["Edit(**/__generated__/**)", "Edit(**/routeTree.gen.ts)"]
  }
}

An Edit rule covers every editing tool, so a Write to the same path is refused too.

Hooks: a guard that can read the command

A deny rule matches a pattern. When the decision needs more than a pattern, use a PreToolUse hook: a script that Claude Code runs before a tool call. It receives the call as JSON on standard input. If it exits with code 2, the call is refused and what the script printed on standard error is shown to the agent, so it knows why and can try the right way.

A minimal hook that refuses npm:

// .claude/hooks/no-npm.mjs
let input = "";
process.stdin.on("data", (chunk) => (input += chunk));
process.stdin.on("end", () => {
  const { tool_input } = JSON.parse(input);
  if (/^\s*npm\b/.test(tool_input.command || "")) {
    console.error("This repository uses yarn, not npm.");
    process.exit(2);
  }
});

And the line that registers it, next to the permission rules:

{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Bash", "hooks": [{ "type": "command", "command": "node .claude/hooks/no-npm.mjs" }] }
    ]
  }
}

Bun's repository uses this kind of hook for decisions a pattern cannot make: its shell guard reads the whole command line, so it refuses bun bd wrapped in timeout and updating snapshots with a test name filter, while cargo fmt and the debug build testing one file go through. Its PostToolUse hook formats each file after an edit.

A guard you have not tested is a guess

Hooks fail quietly. A script that cannot find the field it expects usually exits 0, and exit 0 means allow. The guard is installed, it runs on every call, and it protects nothing.

So a guard needs cases: the calls it must block and the ones it must let through. Baudrier drafts them for you from the rules it can read:

baudrier selftest --init ./my-harness   # a block case per deny rule, and one call that must pass
baudrier selftest . --runtime claude    # replay every case with the payload Claude Code sends

For a hook, a tool cannot guess what the script means to block, so selftest --init leaves a short prompt for your coding agent to write those cases. React's seven cases and Bun's nine all pass in Claude Code.

The same guards in Codex and Copilot

Copy a Claude Code hook to another agent and it may keep running and stop blocking. Codex edits files with apply_patch, so an edit guard that reads file_path finds nothing and allows. Copilot names its arguments differently again.

When baudrier install writes a harness for Codex or GitHub Copilot, the hooks go through an adapter that hands each script the call shape it was written for, and deny rules on edits become hooks that refuse a patch touching a covered file. Then run the same cases in each agent:

baudrier selftest . --runtime codex
baudrier selftest . --runtime copilot

Some pieces have no equivalent. Codex rules cover shell commands only, so a rule that refuses to read ~/.ssh has nothing to become there. The harness page lists it as lost and says to set that limit in Codex's sandbox instead.

Start from guards that already hold

You do not have to write these from scratch. React, Bun, Supabase and AgentMob's harnesses on the Harness Hub come with their cases and the publisher's run in Claude Code, Codex and Copilot.

uv tool install baudrier
baudrier install react --dry

Look at what it would write, then install it and run baudrier selftest in your repository to see the guards block for yourself.