I run Claude Code unattended a lot: claude -p jobs while I'm away from the PC, overnight refactors, scheduled runs. The model is careful most of the time. "Most of the time" is not what you want between an agent and git reset --hard.

Claude Code has a hook system that runs your own script before a tool call executes. If the script exits with code 2, the call is blocked, and whatever the script printed to stderr goes back to Claude as feedback so it can find another way. That's all you need to build a guard rail.

The hook

Save this as ~/.claude/hooks/guard-bash.mjs:

#!/usr/bin/env node // PreToolUse hook for the Bash tool. Exit 0 = allow, exit 2 = block. // Claude Code sends the pending tool call as JSON on stdin. const BLOCK = [ [/\brm\s+-[a-z]*r[a-z]*f?[a-z]*\s+(\/|~|\$HOME|\*)(\s|$)/i, "recursive delete of /, ~ or *"], [/\brm\s+-[a-z]*f[a-z]*r[a-z]*\s+(\/|~|\$HOME|\*)(\s|$)/i, "recursive delete of /, ~ or *"], [/\bgit\s+push\b.*(--force\b|--force-with-lease\b|\s-f\b).*\b(main|master|production|prod)\b/i, "force-push to a protected branch"], [/\bgit\s+push\b.*\b(main|master|production|prod)\b.*(--force\b|\s-f\b)/i, "force-push to a protected branch"], [/\bgit\s+reset\s+--hard\b/i, "git reset --hard discards uncommitted work"], [/\bdrop\s+(table|database|schema)\b/i, "DROP TABLE/DATABASE/SCHEMA"], [/\bdelete\s+from\s+\w+\s*(;|$)/i, "DELETE without a WHERE clause"], [/\b(curl|wget)\b[^|]*\|\s*(sudo\s+)?(sh|bash|zsh|node|python3?)\b/i, "piping a download into a shell"], ]; let raw = ""; process.stdin.setEncoding("utf8"); process.stdin.on("data", (c) => (raw += c)); process.stdin.on("end", () => { let cmd = ""; try { cmd = String(JSON.parse(raw || "{}")?.tool_input?.command ?? ""); } catch { process.exit(0); // unreadable input: don't get in the way } for (const [re, why] of BLOCK) { if (re.test(cmd)) { process.stderr.write(`Blocked by guard-bash: ${why}.\nFind a safer way, or ask the user to run it.\n`); process.exit(2); } } process.exit(0); }); 

Wire it up

Add this to ~/.claude/settings.json (merge it with whatever is already there):

{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "node ~/.claude/hooks/guard-bash.mjs", "timeout": 10 } ] } ] } } 

On Windows, use the full path (node C:/Users/you/.claude/hooks/guard-bash.mjs), because ~ isn't expanded everywhere.

Test it before you trust it

You can test the hook without Claude at all by piping fake tool calls into it:

t(){ printf '%s' "{\"tool_input\":{\"command\":\"$1\"}}" | node ~/.claude/hooks/guard-bash.mjs 2>/dev/null; echo "exit=$? :: $1"; } t "rm -rf /" # exit=2 t "git push --force origin main" # exit=2 t "git reset --hard HEAD~1" # exit=2 t "DELETE FROM users;" # exit=2 t "rm -rf node_modules" # exit=0 t "DELETE FROM users WHERE id=1;" # exit=0 t "git push origin feature/x" # exit=0 

Then, in a real session, ask Claude to run git reset --hard. It should get blocked, read your message, and suggest git stash or ask you instead.

It's a regex list, not a sandbox. It catches the obvious foot-guns; it won't stop a determined python -c that deletes files. Treat it as a seatbelt, not a cage.

The other half: rules for when you're not there

The hook stops the worst commands. The rest is behaviour, and that goes in CLAUDE.md. This is the block I add to any project I run headless:

## Unattended runs  When a prompt says I'm away, or there's no one to answer: - Never stop to ask. Make routine decisions yourself and record them. - Never do these unattended: deploy, push to main, delete data, run destructive migrations, send messages to real people, spend money, change credentials. Log them under NEEDS YOU with your recommended answer and move on. - Log as you go in RUN-LOG.md (append): DONE / DECIDED / SKIPPED / NEEDS YOU / STATE. - Leave it building. If your last change broke the build and you can't fix it, revert that change and log why. - Finish with three lines: what's done, what needs me, where the log is. 

The difference is coming back to a log and finished work, instead of coming back to a question it asked at 1 a.m. and then waited on all night.

A third one worth adding

A notification when Claude finishes, so you don't have to keep checking. A Stop hook that POSTs to ntfy.sh (free, no account) pings your phone. Point a "Stop" entry in the same hooks block at a script that does fetch("https://ntfy.sh/<your-secret-topic>", { method: "POST", body: "Done" }).

If you want the whole setup

I packaged the full versions into a small kit: the guard hook above with a longer pattern list (git clean -f, chmod 777, mkfs, npm publish, fork bombs) tested against 15 commands, the ntfy Stop hook, five skills (verify-ui, de-slop, unattended-run, ship-check, sharpen-prompt) and two CLAUDE.md templates. It's $7 here: https://ko-fi.com/s/ec132522d2

Full disclosure: that's my own product. Everything in this post works on its own without it.