These are notes on the hook script behind a short list of rules I gave coding agents in a shared workspace: no force pushes, no skipped git hooks or commit signing, no merging pull requests, and no pushing to a project's main. Why those rules got a hook while everything else stayed as written instructions is in the post about sorting agent rules, so this one is only about the script, how it's wired into two editors, how it fails, and how much of a shell parser you end up writing once you try to read commands properly.

Wiring it into both editors

The script is one file, scripts/guard-command.mjs, with no dependencies, and both editors run it before an agent's shell command. Claude Code runs it as a PreToolUse hook on the Bash tool:

{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "node \"$CLAUDE_PROJECT_DIR/scripts/guard-command.mjs\"" } ] } ] } } 

and Cursor runs the same file as a beforeShellExecution hook in .cursor/hooks.json:

{ "version": 1, "hooks": { "beforeShellExecution": [ { "command": "node scripts/guard-command.mjs", "matcher": "git|gh", "timeout": 10, "failClosed": true } ] } } 

Both send the command as JSON on stdin, but in different shapes, and they expect different answers, so the script looks at the input to decide how to reply (Cursor can also load Claude Code's hooks, which is why it goes by the shape of the input and not by which config called it):

export function respond(input, { decision, reason }) { if (input && typeof input === 'object' && 'tool_input' in input) { // An explicit allow would skip Claude Code's own permission prompt, so no objection is silence. if (decision === 'none') return ''; return JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: decision, permissionDecisionReason: reason }, }); } if (decision === 'none') return JSON.stringify({ permission: 'allow' }); return JSON.stringify({ permission: decision, user_message: reason, agent_message: reason }); } 

In Claude Code, "no objection" has to be silence. A hook that answers allow makes Claude Code skip its normal permission prompt, so a guard that says "allow" whenever it doesn't object grants more than the user's own settings would. Printing nothing lets the usual permission check run.

Cursor is the odd one, because the guard always answers it in JSON, so "no objection" goes out as allow, and I couldn't tell from Cursor's docs whether that allow also skips Cursor's own approval prompt. That's why the Cursor hook only matches commands that contain git or gh, which keeps the unknown to the commands the guard is about anyway, and not every rm an agent runs.

It's cheap to run. The review measured about 30 ms per command and about 50 ms for a push, nearly all of it Node starting up, plus two quick git calls for a push to find the repository root and the current branch.

What it denies and what it asks about

It denies force pushes, --no-verify and skipped signing, gh pr merge and auto-merge (also through gh api and GraphQL), and a push to main from inside a project. It asks before an agent files or changes a GitHub issue, posts review comments or closes a PR, and whenever it can't tell which branch a push goes to. The force part of the push check gives a good idea of how each rule looks:

if (a.startsWith('--')) { const flag = a.split('=')[0]; if (['--force', '--force-with-lease', '--force-if-includes', '--mirror'].includes(flag)) deny(REASONS.force); } else if (/^-[A-Za-z0-9]+$/.test(a)) { for (const ch of a.slice(1)) { if (ch === 'f') deny(REASONS.force); if (ch === 'o') break; // -o takes a value, so the rest of the cluster isn't flags } } // ...and later, any refspec starting with + is a force push too 

The workspace repository itself is the one place where pushing main is fine, since workspace docs get committed there, and it only counts as the workspace when it has a workspace.json, so somebody copying the guard into a single repository keeps main protected. The reasons are written for the agent as much as for the person, because the agent reads them and changes course:

Agents push feature branches only. Pushing to main in storefront is blocked; open a PR with /pre-pr instead. 

Reading the command like a shell

The obvious first version is a few regular expressions over the command string, which falls over as soon as an agent writes a commit message, because commit messages talk about the thing they changed:

git commit -m "Explain why we never git push --force here" 

That command has to pass, so the guard splits the command into words and operators the way a shell would (quotes, $'...', here-documents, &&, ;, pipes, subshells, bash -c "...") and checks each simple command on its own. It also has to know where each command runs, so it keeps track of the current directory, the previous one for cd -, and a pushd stack, it treats subshells and background jobs as not moving the shell, and when it can't work out the directory (a cd in the last element of a pipeline, say) it marks it unknown. An unknown directory never gets the workspace exemption, so an explicit main is denied there and a bare push asks. A few decisions, from a project on a feature branch (the loop from the workspace root):

git commit -m "never git push --force" => none git push origin main => deny cd .. && cd - && git push origin main => deny git push origin $'--force' feat/x => deny for p in a b; do git -C projects/$p push origin main; done => deny 

Testing this kind of guard is mostly testing the shell, which is easy to underestimate when the rules are all about git. Tests written up front cover the git flags well, and miss the shapes around them, like a command after if, do, { or !, cd - landing back in the exempt workspace, or a here-document commit message with an apostrophe in it (all three got past my first version). What found those was breaking each documented rule in the script on purpose and checking that some test failed, which showed about half the rules had nothing watching them (49 of 50 of those mutants survived). The other half of it is a list of everyday commands that must get no objection, with old and new decisions compared on it after every fix, since fixes to a parser like this one tend to open something they didn't touch, and that comparison is how we caught a fix round that let through a handful of commands the old guard had denied.

How it fails

The two editors behave differently when the hook can't do its job, so it's worth deciding each case on purpose:

  • Claude Code blocks a command when a hook exits with 2, so a guard that exits 2 on bad input blocks every command. The guard exits 1 instead, which Claude Code treats as a non-blocking error. The same goes for a crash, a timeout, or node missing from the editor's PATH (easy to hit with volta or nvm and an editor started from the Dock), so in Claude Code a guard that can't run means no guard, and the command goes on to the normal permission prompt.
  • An error inside the guard's own parsing is caught and answers ask, and so does a command that ends inside an open quote or here-document, so a confusing command goes to a person.
  • Cursor, with failClosed and a 10 second timeout, blocks the command when the hook fails. That's from Cursor's docs, and I've only fed the script Cursor-shaped input, not run it inside a live Cursor window.

failClosed turned up one bug right away. The guard read stdin with readFileSync(0), which throws EAGAIN when stdin is a non-blocking pipe and the input hasn't arrived yet. Before, that meant the guard reported "not JSON" and did nothing, without anyone noticing, and with failClosed on it would have blocked every git command in Cursor. Reading it as a stream fixed it:

import { text } from 'node:stream/consumers'; const input = JSON.parse(await text(process.stdin)); 

Gotchas around it

The guard reads the branch before the command runs, which goes wrong in both directions. git checkout -b feat/x && git push -u origin HEAD from main is denied, because when the hook runs the branch is still main (naming the branch in the push works). The other direction is worse, since git checkout main && git push from a feature branch gets no objection, because the guard still thinks it's on the feature branch. The fix is to follow checkouts in the location model and treat the branch as unknown after any checkout it can't follow. Until then, the remote's branch protection is what catches that one.

An ask rule on Edit doesn't cover the shell. An agent could switch the guard off by editing it, so Claude Code's settings have ask rules for Edit on the script and the hook configs. With Claude Code 2.1.296 in accept-edits mode, the Edit was held for a prompt, but echo ... >> scripts/guard-command.mjs ran without one, and only a deny rule blocked the redirect. I kept ask so a person can still approve a change to the guard. Cursor has no equivalent rule at all, so there it rests on the written rule that editing the guard to get past an objection is never the agent's call.

Cursor's ask may not stop anything in Auto-review mode. That comes from forum reports, which I couldn't confirm, so on Cursor I'd count only on the deny rules.

What a text check can't see

Run from a feature branch, git push origin $(echo main) gets no objection, because a substitution the guard doesn't recognise ends the word it's reading, so it sees a plain git push origin of the current branch (on main the same command is denied, but only for that reason). It does resolve the read-only forms agents use for the current branch, like $(git branch --show-current), and $(git rev-parse --show-toplevel) for the repository, since those are the ones that show up in practice. A spelling like g''it push -f gets past Cursor's git|gh matcher before the guard even sees it. Aliases, scripts, and API calls made with curl instead of gh are out of reach too, and there's no fixing that from command text, because you'd need to run the command to know what it does.

So the guard ends up as a default in front of the remote's protection. It covers the command shapes we could think of and test, and gives the agent a reason it can act on, while branch protection and required reviews on the remote are what stop a push to main or a merge nobody reviewed. The parts that held up are the wiring, choosing on purpose what each editor does when the hook fails, and testing it in shell shapes like loops, blocks, here-documents and cd -, not only git flags, with a list of everyday commands that must stay silent.