Skip to content
86% off$349 $48.86code BF2026

Guides

Claude Code agent deleted the PreToolUse hook's marker file

An agent can delete the marker file your PreToolUse hook checks. Fail closed, deny with exit code 2, keep the switch out of reach, and protect the branch.

GuideUpdated 7 min read

On this page

You set up a hook to stop gh pr create, gh pr merge and git push, and the agent deleted the marker file that turned the block on. That is the case in a thread on r/ClaudeCode (last seen September 2026), and people there found more holes. This guide explains why it happens and how to build a gate that survives it.

Why the agent got past the hook

A hook runs on your Mac as your user, and the agent works in a checkout it can write to. Anything inside that checkout, or owned by your user, is within the agent's reach. Commenters in the thread listed these routes (we have not re-tested each one):

  • rm on the marker, find -delete, or a glob that matches it.
  • A symlink that makes the marker path point somewhere else.
  • A disableAllHooks setting accepted in .claude/settings.local.json inside the checkout.

A second thread asked how Claude ran git when every command was set to ask. Users there answered that commands on a safe list skip prompts, and the agent can write a script and run that instead.

All of these share one cause: the gate, or the switch that controls it, lives where the agent can edit it.

Weak pointSymptomFix
Marker file means "blocked"Agent deletes it, gate opensFail closed (Fix 1)
Switch in the checkout or your home folderAgent edits itMove it out of reach (Fix 2)
Allow rules and safe listsCommand runs with no promptHard deny with exit code 2 (Fix 3)
Settings file in the checkoutHooks switched offFreeze it, but don't rely on it (Fix 4)
Your own token in the agent's environmentAgent pushes anywayServer-side protection (Fix 5)

Fix 1: Make the hook fail closed

Block unless a human-created token exists. Then deleting files buys the agent nothing, because absence means deny.

Claude Code passes the tool call to hooks as JSON on stdin. Confirm the exact fields, how to register the hook, and how exit codes are treated in the Claude Code hooks documentation for your version. This is a sketch:

bash
#!/bin/bash
# PreToolUse gate: deny pushes and PR actions unless a human created the token.
input=$(cat)
token=/usr/local/etc/agent-gate/allow
if echo "$input" | grep -Eq 'git push|gh pr (create|merge)'; then
  if [ ! -f "$token" ]; then
    echo "Blocked: git push and gh pr commands need a human." >&2
    exit 2
  fi
fi
exit 0

Matching text in raw JSON is crude, and the thread shows how many ways an agent can phrase a command. Treat this as one layer.

Fix 2: Put the off switch where the agent can't write

Where the switch livesCan the agent change it?
A marker file in the checkout or worktreeYes
A file in your home folderYes, same user
A file in a folder owned by rootNot without sudo
Approval state held by another process that a human answersNot by editing a file in the checkout
A credential the agent's environment doesn't haveNothing to switch

For the root-owned option, create the folder once and keep the hook script there too, referenced by absolute path:

bash
sudo mkdir -p /usr/local/etc/agent-gate

When you decide the agent may push, create the token, and remove it afterward:

bash
sudo touch /usr/local/etc/agent-gate/allow
sudo rm /usr/local/etc/agent-gate/allow

Fix 3: Deny with exit code 2, not with a prompt

The thread's answer to "how did it run git when everything was set to ask" was that standing allow rules and safe lists can approve a command before a prompt appears. According to the thread, a hook that exits with code 2 is a deny those allow rules don't override. Don't assume other non-zero codes deny; check the hooks documentation for your version.

Use a hard deny for actions you never want unattended (push to main, merging, publishing). Keep "ask" for things you want to review case by case. For skipping prompts entirely, see a safer alternative to --dangerously-skip-permissions.

Fix 4: Freeze the settings files, knowing the limits

disableAllHooks in a settings file inside the checkout is the hole this fix tries to close. You can mark the file immutable:

bash
chflags uchg .claude/settings.local.json

Undo it with chflags nouchg .claude/settings.local.json. This is a speed bump: the same user can clear the flag, it can break tools that need to write that file, and if the folder is writable the agent can create or replace other project-level settings files. Fix 5 matters more.

Fix 5: Take the credential and the branch away from the agent

The strongest gate is not having the key.

  • Branch protection on the server. Protect main on GitHub so changes need a pull request and review. If the agent might use your own token and you are a repo admin, check the rule's setting for administrators so it applies to you too.
  • A separate actor holds the credential. The agent works and commits locally. A human, or a CI job with its own token, pushes and merges. Don't run gh auth login in the environment the agent uses.
  • A container or VM. Run the agent in a throwaway environment that has the repo and nothing else. See Try risky software safely in a throwaway Mac VM for the VM route.

What Sledbot covers, and what it doesn't

Sledbot covers part of this, and only for Claude Code. It launches Claude Code with per-launch hooks (including PreToolUse and PermissionRequest) and holds an action until you answer an approval card. Your global Claude config is not changed, and the block doesn't depend on a marker file in your worktree. Agents running in Sledbot terminals also can't make the core calls that change secrets, rules or machines.

Rules work like this: if an allow rule and an ask rule both match, ask wins, and a shell line is split into simple commands so any part that asks makes the whole line ask. Built-in ask rules cover git push to protected branches (main, master, release/*, production), merging a pull request, deploys, package publishing, sudo, and rm -r or file writes outside the worktree. A plain push to another branch and gh pr create are not on that list. To gate them, open Settings ▸ Approvals, choose Add rule under Your rules, set the effect to Ask first, enter the command (the field's example is git push * main) and click Save.

What it does not do:

  • It is not a sandbox. Sledbot is not sandboxed, and agents run as your macOS user. We don't claim approval state is tamper-proof against a determined agent.
  • Other CLI agents can't be stopped. For Codex, Gemini CLI and the rest, Sledbot mirrors the agent's own on-screen prompt as a card and answers it by typing keys. It can't stop an action the agent runs without asking.
  • The built-in lists can't be edited. The protected-branch list and production hosts have no UI.
  • disableAllHooks is unverified. Our notes don't say whether that setting in a project file affects the hooks Sledbot injects. Don't rely on Sledbot surviving it.
  • Scripts. Rules match commands. We don't claim they see inside a script the agent writes and then runs.
  • Your GitHub login. Sledbot's GitHub features use your gh login, and we don't claim it is kept from agents in your terminals. If the credential must be out of reach, use Fix 5.

Details are in Needs you and approvals. Sledbot is part of MacSuite, and you can try it free for 7 days.

Checklist before you trust the gate

Use a scratch repo with no real remote and no real credentials. Ask the agent to try each of these and confirm every one stays blocked:

  • Push or merge with the gate in place.
  • Delete the token or marker, by name and with a glob.
  • Replace a path with a symlink.
  • Add disableAllHooks to a settings file in the checkout.
  • Write a shell script that runs the blocked command, then run the script.

If any gets through, the switch or the credential is still within the agent's reach.

If it doesn't work

  • The hook never fires. Check it is registered for the tool the agent uses (Bash, for shell commands) and that the settings file you edited is the one in effect. The agent may use a different tool for the same job.
  • The command goes through anyway. Your script probably exited with something other than 2. Print to stderr and exit 2 explicitly.
  • Legitimate commands get blocked. The pattern is too broad. Narrow it, or have a human create the token for a short window.
  • The agent works around the gate with a script. String matching can't catch this. Use branch protection or remove the credential (Fix 5).

If the behavior looks like a Claude Code bug, contact Anthropic's support with something like this:

text
Claude Code version: (output of claude --version)
Issue: a project-level .claude/settings.local.json containing disableAllHooks
is accepted and switches off my PreToolUse hook.
Steps: (1) register a PreToolUse hook that exits 2, (2) add disableAllHooks
to the settings file in the checkout, (3) run a blocked command.
Expected: the hook still runs. Actual: the command goes through.

Common questions

Is disableAllHooks a Claude Code bug?

The thread reports it being accepted from a settings file inside the checkout. Whether that is intended is a question for Anthropic. Either way, assume anything the agent can write can switch the gate off.

Does Sledbot make a hook tamper-proof?

No. It keeps approval state out of your worktree and lets you answer from a card, but agents run as your user without a sandbox.

What is the single most useful change?

Remove the credential from the agent's environment and protect the branch on the server. The local hook then becomes a convenience, not the only barrier.

Every app.
One purchase.