OmniscioGuidesA Stop hook that runs tests
A Claude Code Stop hook that runs the tests first
“Done” is a claim, and a test suite is the cheapest way to check it. A Stop hook turns that check into something the loop cannot skip silently: a red turn is sent back once with the failure, instead of reaching you as a finished task.
Checked against Anthropic’s Claude Code hooks documentation on November 25, 2026. By the Omniscio team. Updated .
The problem
Asked to fix a bug, Claude can make a change that reads well, explain it convincingly, and break two tests. If you are watching, you notice. If you are running six sessions, you find out at the end of the day.
The shape of the fix is a hook that runs at the one moment Claude is about to hand control back. Anthropic gives Stop for that moment, and gives it a way to refuse: blocking it prevents Claude from stopping and continues the conversation, with your message as the reason to keep going.
The script
Save it as .claude/hooks/run-tests-before-stop.sh in the project, create the folder if needed, and make it executable with chmod +x. It needs bash and jq, and it assumes npm test runs your suite. On Windows, run it through Git Bash, or port the guard to a .js or .ps1 hook.
#!/bin/bash # .claude/hooks/run-tests-before-stop.sh input=$(cat) command -v jq >/dev/null || exit 0 if [ "$(jq -r '.stop_hook_active' <<<"$input")" = "true" ]; then exit 0 fi output=$(npm test --silent 2>&1) status=$? if [ $status -ne 0 ]; then echo "npm test failed in $PWD. Re-run it, fix the failures, then finish." >&2 printf '%s\n' "$output" | tail -n 20 >&2 exit 2 fi exit 0
The command -v jq line is the fail-open guard: without jq the next line cannot read stop_hook_active, the test for "true" never matches, and the hook would block on every continuation until the cap. Exiting 0 when jq is absent means a broken dependency costs you the check rather than an eight-turn loop.
Register it in .claude/settings.json at the project root, merging the Stop array into any hooks object already there. Stop takes no matcher, so there is no event filter to get wrong — only the settings file and a timeout long enough for the suite.
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/run-tests-before-stop.sh",
"timeout": 300
}
]
}
]
}
}Two notes on that block. $CLAUDE_PROJECT_DIR is the documented placeholder for the project root, quoted in shell form so a path with spaces survives. And 300 is below the ten-minute default: if your suite can take longer, raise it, because a timed-out hook is cancelled and its output discarded, which for this hook means the turn simply ends.
Exit 2, or the JSON block decision
Exit 2 is the simplest route: the message on stderr becomes the reason Claude is told to keep working. The JSON route does the same job with a field instead, and it is the one Anthropic documents as the structured form: decision set to "block" and a reason, which is required whenever the decision is block.
{
"decision": "block",
"reason": "npm test is failing. Run it, fix the failures, then finish."
}The practical difference is in the transcript. The JSON route shows the refusal as a hook error, while the exit-2 route puts your stderr where Claude reads it. For guidance you expect Claude to follow rather than a failure you are reporting, Anthropic offers hookSpecificOutput.additionalContext on the same event: it keeps the conversation going through the same loop protections, but the transcript labels it Stop hook feedback and shows no hook error. Whatever you print, the object must be the only thing on stdout.
To use the JSON route, replace the tail of the script rather than adding to it: after status=$?, print the one object with jq -c or a here-doc and exit 0. A script that prints the object and still exits 2 leaves Claude Code deciding between two answers.
What each line does
| Line | Job |
|---|---|
input=$(cat) | Reads the Stop event JSON from stdin. |
command -v jq || exit 0 | Fails open when jq is missing, so a broken dependency cannot turn the hook into a loop. |
.stop_hook_active | True when Claude Code is already continuing because of a Stop hook. Exiting 0 there is what makes this a one-shot nudge. |
npm test --silent | Runs the project’s suite. Use the command your CI runs, so the hook and CI agree on what passing means. |
status=$? | Captured immediately after the run, before anything else runs. |
echo … >&2 | The reason travels on stderr, names the command and the directory, and tells Claude what to do next. |
tail -n 20 | The last twenty lines of output, so Claude gets the failure without the whole log. Keep it short: this text becomes prompt context. |
exit 2 | On Stop, exit 2 prevents Claude from stopping and continues the conversation. |
timeout | Seconds, defaulting to 600. A timed-out hook is cancelled and its output discarded, so set it past your slowest run. |
Why stop_hook_active is not optional
A blocking Stop hook creates a new turn, and that turn ends in another Stop event. Without a guard, the hook blocks again, and again, on the very failure it reported the first time. Anthropic caps this: after eight consecutive continuations Claude Code overrides the next block and ends the turn, and the count resets every time Claude calls a tool. The cap is a backstop, not a design. A hook that leans on it spends eight turns saying the same thing.
Reading stop_hook_active and exiting 0 when it is true is the documented fix, and it costs one line. Raise the cap with CLAUDE_CODE_STOP_HOOK_BLOCK_CAP only when a hook genuinely needs more than eight attempts to converge, which a test suite should not.
When you want a wall instead of a nudge
Removing the guard turns the hook into a gate: it blocks on every continuation while the suite is red, and Claude Code’s eight-continuation cap is what finally ends the turn. That is harsher and more expensive, so choose it deliberately — and only for a suite that can actually go green without you, or the cap is the only thing that stops it.
How to test that it fires
- Prove the script’s three exits by hand. Run it with sample JSON in a project where
npm testpasses, then with a deliberately failing test, then withstop_hook_activetrue:echo '{"hook_event_name":"Stop","stop_hook_active":false}' \ | .claude/hooks/run-tests-before-stop.sh echo $?Expect 0 with a green suite, 2 with a red one, and 0 whenstop_hook_activeis true. - Break a test, then ask Claude for something small. When it tries to finish, the turn continues instead, and the transcript shows the message you wrote. That is the moment the hook did its job.
- Watch what the continuation does. Because of the guard, the next attempt is allowed to finish even if the suite is still red. If you expected a wall, that is the behaviour to change, not a bug.
- Use the debug log for anything else.
claude --debug-file /tmp/claude.logrecords the hook’s run, and/debugturns logging on mid-session.
No Claude Code session was configured to run this hook for the page, so the three exit codes above describe what the script is written to return and the commands are there for you to run.
The common failures
1. The hook blocks and Claude never stops
The guard is missing, misspelled, or comparing against the wrong string. jq -r prints true and false in lower case, so compare with those, and keep the guard as the first thing the script does, before the suite runs.
2. The hook is pointed at a path that does not exist
A hook that cannot start never reaches its own exit 2: the shell exits with a code like 127 and Claude Code treats that as a non-blocking error, so the gate opens. Anthropic documents a separate exception for this event — a hook that exits 2 with nothing on stdout whose stderr says a file is missing, such as No such file or directory, is also treated as non-blocking — which is why a hook that seems to run proves nothing until you watch it block. Test the script by hand before you trust it.
3. Every answer runs the suite
Stop fires whenever Claude finishes responding, not only at the end of a task, so a five-minute suite runs after every short answer. Give the hook a condition of its own — a marker file, a changed-file check, or a fast subset of the suite — or accept the delay knowingly.
4. JSON printed next to other output
On the JSON route the object has to be the whole of stdout. An echo from a shell profile, or your own debug print, turns it into plain text and the decision is ignored, with no error in the transcript. The debug log names it: Hook output does not start with {.
5. A failing suite that is really an environment problem
The hook runs in the session’s working directory with its environment, so a suite that needs a database, a port or a variable the session does not have will fail every time. A test command that is not installed is this case too: npm test fails with its own message, the script still exits 2, and Claude spends turns chasing a green that is not reachable. Run the same command in the same directory by hand first.
Where Omniscio fits
This gate belongs in the repository, so commit the script and the settings file and every clone inherits it. Omniscio’s Hooks panel takes the session-level angle: it composes the hooks it ships, and any you define, into every session it spawns, with a global switch and per-project overrides, kept in its own database rather than written into your global Claude configuration. The Claude Code hooks guide covers that panel, and the hooks builder assembles the block from the event, matcher and handler you pick.
Omniscio runs on Windows, macOS and Linux, and the pricing page covers the plans.
How this page was written
The event, its input fields, the block decision, the eight-continuation cap, the file-missing exception and the no-matcher rule were read from Anthropic’s Claude Code hooks reference on November 25, 2026. The cap warning and the stop_hook_active fix come from Anthropic’s hooks quickstart, read the same day.
No Claude Code session was configured to run this hook for the page, so the page describes what the script is written to do and where the documentation says Claude Code will act on it.
Sources
- Anthropic’s Hooks reference, for the
Stopevent, its input, decision control, the continuation cap, the exit-code table and the JSON rules, read November 25, 2026. - Anthropic’s Automate actions with hooks, for the block-cap troubleshooting section, the
stop_hook_activesnippet and the shell-profile note, read November 25, 2026.
Common questions
How do I make Claude Code run tests before it finishes?
Use a Stop hook. It runs when the main agent has finished responding, and a hook that exits 2, or returns a block decision, prevents Claude from stopping and sends it your reason so it keeps working. Read stop_hook_active first if you want the hook to nudge once rather than hold the turn open.
Why does my Stop hook loop?
The hook blocks every time it runs, including the turns it caused itself. Claude Code stops regardless after eight consecutive continuations, and the cap can be raised with CLAUDE_CODE_STOP_HOOK_BLOCK_CAP. Read stop_hook_active and exit 0 when it is true when you want one nudge instead of a wall.
Does a Stop hook run after every answer?
Yes. Anthropic documents that Stop hooks fire whenever Claude finishes responding, not only when a task is complete, and that they do not fire on a user interrupt. A hook that runs a slow suite therefore needs a condition of its own.
Why did my Stop hook exit 2 without blocking?
Anthropic documents an exception: on Stop and SubagentStop, when the hook exits 2 with nothing on stdout and its stderr says a file is missing, such as No such file or directory, the run is treated as a non-blocking error. A hook whose own script path does not exist never reaches exit 2 at all: the shell exits 127 and that is a non-blocking error too.
Is there a built-in way to do this without writing hook configuration?
Anthropic points at the /goal command as a built-in shortcut for a session-scoped prompt-based Stop hook, for keeping Claude working toward a condition without writing hook configuration. It is a prompt hook evaluated by a model rather than the command hook this page teaches, so the two are alternatives rather than the same thing.
A gate that holds in every session
Omniscio runs your Claude Code sessions side by side and composes the hooks it ships, and any you define, into every one it spawns, so the test gate is not missing from the session you started in a hurry.
The features page covers what else it does, pricing covers the plans, and you can download it for Windows, macOS or Linux.