Skip to content
mentoor

Topic 5 of 6 · 6 min read

Debugging hooks that fail, stall or loop

Goal: Read a hook's debug output and find why it failed silently, stalled the session or trapped the agent in a loop.

When a hook misbehaves, run it by hand with a sample input, read its exit code, and then turn on the harness debug output; most hook bugs are an exit code that means something else, a wrong path, or a stop hook that never says "done".

Check yourself · 1 of 3

A hook meant to block an edit exits with code 1. What happens in Claude Code?

Choose one answer

Why it matters

A broken hook is hard to see. A formatter that never runs just means untidy code. A blocking hook that crashes may block everything or nothing. A hook that waits for input hangs the session. And a hook that keeps the agent working can burn tokens for an hour.

How to debug

  1. Run it by hand. Save a sample of the real input JSON, then run cat sample.json | ./hook.sh; echo "exit $?". This shows output and exit code without the agent.
  2. Know the exit codes. In Claude Code, 0 means success, and output may be added as context. 2 means a blocking error: the call is stopped and standard error goes to the model. Any other non-zero code is a non-blocking error: the session continues and the failure shows only in the log. A script that crashes with code 1 while you meant to block does not block.
  3. Turn on debug output. Start the agent with its debug flag (in Claude Code, claude --debug) and read which hooks matched, what they printed and how long they took.
  4. Check the basics. Is the script executable? Is the command path absolute or based on the project folder variable, not on the current directory? Is jq installed? Is the JSON valid? A trailing comma silently disables a settings file.
  5. Check the timeout. Hooks have a time limit. A hook that waits on a network call or a prompt will be cut off. Set a short timeout and fail fast.

The stop-hook loop

A Stop hook that says "tests failed, keep going" sends the agent back to work. If the tests still fail, it runs again. Without a guard, this repeats. The input includes a flag, stop_hook_active, that is true when the agent is already continuing because of a stop hook. Check it and exit 0 on the second pass, or count attempts.

Before and after

Before: the stop hook always exits 2 when any test fails. The agent edits, stops, is sent back, and repeats 30 times on a flaky test.

After: the hook reads stop_hook_active. If true, it lets the agent finish and reports the failing test to you.

What goes wrong

  • Hooks that print to standard output when they should use standard error, so the model never sees the reason.
  • Slow checks run on every event, which looks like the agent being slow.
  • Fixing the hook while the old settings are still loaded. Restart the session or reload the config.

Examples

Run it by hand

cat sample-input.json | .claude/hooks/check.sh
echo "exit code: $?"

If this prints the right message and exits 2, the script is fine, and the fault is in the registration: a wrong matcher, a bad path or invalid JSON in the settings.

A stop hook with a guard

#!/usr/bin/env bash
input=$(cat)
if [ "$(echo "$input" | jq -r '.stop_hook_active')" = "true" ]; then
  exit 0   # already retried once: let the agent stop
fi
npm run test:unit >/tmp/test.log 2>&1 || {
  tail -n 15 /tmp/test.log >&2
  exit 2
}

It gives the agent one more chance, then stops.

Key points

  • Reproduce a hook outside the agent: feed it a saved sample of the input and read its exit code.
  • Exit 2 blocks and sends standard error to the model; other non-zero codes only log an error and the action continues.
  • Use the harness debug mode to see which hooks matched, what they printed and how long they took.
  • Give every hook a timeout and make it fail fast instead of waiting on the network or on input.
  • Guard stop hooks with stop_hook_active or an attempt counter so they cannot loop forever.

Common mistakes

  • Exiting 1 when the intent was to block, so the action still runs.
  • Using a relative path that works in your shell but not from the agent's working directory.
  • Writing a stop hook with no loop guard.
  • Guessing at the cause instead of running the hook by hand with a saved input.

Check yourself · 2 of 3

What is the quickest first step when a hook seems not to run at all?

Choose one answer

Check yourself · 3 of 3

A stop hook sends the agent back to work whenever tests fail, and the agent loops on one flaky test. What should the hook check?

Choose one answer

Practice items, not counted towards your mastery.

Start here