"A Crashed PreToolUse Hook Leaves No Trace in the Model's Context; Telling It to Block Leaves Too Much"
Two readers left comments on yesterday's piece about Claude Code's PreToolUse hook exit codes that I couldn't answer honestly at the time. One asked what happens to stderr output from a crashing hook — does it leak into the model's context, or just disappear. The other asked about an onFailure: "block" setting I hadn't tested. Yesterday's article only established that a non-2 exit code lets the…
Two readers commented on last week's article about Claude Code's PreToolUse hook exit codes, asking about what happens to stderr output from a crashing hook and about an onFailure: block setting that hadn't been tested. The first reader's question was answered in the article, which established that a non-2 exit code still lets the guarded tool call through, and that the failure appears as a hook_non_blocking_error attachment.
However, the article did not examine what is actually in the model's context versus what is only visible in the raw session log.
To answer these questions, the author built an experiment using a throwaway .claude/settings.json file, a crash-prone hook script, and real headless Claude -p calls against the actual CLI. The crash_hook.py script logs its invocation time, writes a traceback to a file, and then raises a RuntimeError. The hooks configuration includes a PreToolUse hook for the Bash command, with a matcher and a command to run the crash_hook.py script.
When the command echo hello456 is asked to run, the full event stream is captured with the --output-format stream-json --verbose option. The raw stream contains a hook_response system event with the entire crash information. However, the tool-result message that the model receives does not contain any stderr output, exit code, or mention of the hook running. The model's final answer correctly reports that the command printed hello456 without mentioning any hook output or errors.
When the onFailure: block setting is added to the hook configuration, the result changes. The crash occurs, and the tool call does not go through. The tool-result message now indicates that the tool call was blocked due to the onFailure: block setting, and the model's final answer accurately describes the situation, including the crash details and the blocked command.
This demonstrates that the onFailure: block setting fails closed, while the default behavior fails open, allowing the guarded command to run even if the hook crashes.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.