Codex hook failed: what Codex does when a PreToolUse hook breaks

In Codex CLI 0.172 only exit code 2 or a JSON deny blocks a command. A PreToolUse hook that exits 1, times out, prints junk, answers ask, is missing or is not trusted yet lets the command run. What we tested and how to make a Codex hook fail closed.

· 6 min read

In Codex CLI 0.172.0, a PreToolUse hook blocks a command only when it exits with code 2 or prints a JSON answer with permissionDecision: "deny". Every other way a hook can go wrong lets the command run. We tested six: a hook that exits 1, one that sleeps past its timeout, one that prints text that is not JSON, one that answers “ask”, a hook whose script does not exist, and a hook that has not been trusted yet. In all six, Codex ran touch marker.txt, and the codex exec --json output said nothing about the hook.

If you use a hook as a gate, that means a broken gate is an open one. Here is what we ran, what each case did, and how to write a Codex hook that refuses when it fails.

What Codex did in each case

Setup: Codex CLI 0.172.0 on macOS, 25 November 2026. One hook in config.toml with matcher ".*" and timeout = 5, run through codex exec --json with --dangerously-bypass-hook-trust so the hook ran without being trusted first (except in the last case), and the prompt “Run exactly this shell command once: touch marker.txt”.

What Codex CLI 0.172.0 did with touch marker.txt for eight PreToolUse hook outcomes, tested 25 November 2026
OptionCommand ranWhat the output said
Exit 2, reason on stderrCommand ranNo, blockedWhat the output saidLogged "Command blocked by PreToolUse hook: <reason>. Command: touch marker.txt". The model was told and did not retry.
Prints permissionDecision "deny" in JSON, exit 0Command ranNo, blockedWhat the output saidLogged "Command blocked by PreToolUse hook: <reason>." The model was told and did not retry.
Exit 1, message on stderrCommand ranYesWhat the output saidNothing about the hook.
Sleeps past its 5 s timeout, then deniesCommand ranYesWhat the output saidNothing about the hook.
Prints text that is not JSON, exit 0Command ranYesWhat the output saidNothing about the hook.
Answers permissionDecision "ask"Command ranYesWhat the output saidNothing about the hook.
Script file does not existCommand ranYesWhat the output saidNothing about the hook.
Exit 2, but not trusted yet (run without the bypass flag)Command ranYesWhat the output saidNothing: the hook was skipped.

The bypass flag itself added a warning to the output: “--dangerously-bypass-hook-trust is enabled. Enabled hooks may run without review for this invocation.” That was the only hook-related line in the runs where the hook failed.

What “hook failed” means

Codex’s hook documentation says that when a PreToolUse hook returns fields Codex does not support, Codex marks that hook run as failed, reports the error and continues the tool call. So a “hook failed” report is not a refusal: the call it was about has already gone ahead, or is about to. The same documentation says an error, timeout or malformed response from a managed hook can let the tool run.

We tested codex exec, not the interactive terminal UI, so we cannot say what the UI shows for each case. What we can say is that in exec the failed hook left no trace in the JSON output at all, which matters for unattended runs: a log of an exec run will not tell you a gate was skipped.

An untrusted hook is skipped, and the skip is a yes

Codex runs a hook you wrote yourself only after you review and trust its exact definition under /hooks. Trust is tied to the definition, so a hook you have just added or edited is skipped until you trust it again. In our run without the bypass flag, a hook that would have blocked with exit 2 was skipped, and the command ran. Hooks can also come from hooks.json files and, for managed setups, from requirements.toml; project hooks in .codex/ load only for a trusted project.

Making a Codex hook fail closed

We found no switch in Codex that turns a failed hook into a refusal, like Cursor’s failClosed or Claude Code’s onFailure. The hook has to do it itself:

  • Turn every error into a refusal. Unreadable input, a check that crashes, no network, no key: catch each one, write the reason to stderr and exit 2, or print the JSON deny. Exit 1 is a yes.
  • Answer well inside the timeout, and set it. The default is 600 seconds. Give the hook its own deadline a good margin inside the timeout you configure, and refuse when it is reached. A hook that is still working when Codex stops waiting is a yes.
  • Never answer “ask”. Codex ran the command when our hook answered permissionDecision: "ask". Answer allow or deny.
  • Install the hook’s program. A missing script was a yes. A hook that starts through npx or another download fails the same way the first time the network does.
  • Trust it again after every edit. Open /hooks after changing the hook, or it is skipped.
  • Retest after upgrading Codex. Hook runners change often; run the three failure checks below again.

A gate script shaped like this refuses on every failure:

#!/bin/sh
# Every path out of this script is a refusal unless the check says yes.
input=$(cat) || { echo "gate: could not read the call" >&2; exit 2; }
# my-check stands for your own check; give it a deadline of its own,
# well inside the hook's timeout, and make it fail when that passes.
answer=$(printf '%s' "$input" | /usr/local/bin/my-check) \
  || { echo "gate: check failed or took too long" >&2; exit 2; }
[ "$answer" = "allow" ] && exit 0
echo "gate: not allowed ($answer)" >&2
exit 2

Several hooks that match the same call run at the same time, so one hook cannot stop another from starting. Do not rely on an earlier hook to guard a later one.

Test it yourself

The hook entry we used, with a short timeout for the test:

# ~/.codex/config.toml
[[hooks.PreToolUse]]
matcher = ".*"

[[hooks.PreToolUse.hooks]]
type = "command"
command = "/Users/you/hooks/gate.sh"
timeout = 5   # short for the test; the default is 600 seconds

A hook script with each failure in turn:

#!/bin/sh
# gate.sh: swap the last line between runs to test each failure.
cat > /dev/null            # read the call Codex sends on stdin

exit 1                                  # 1. crashes
# sleep 30; exit 2                      # 2. passes the 5 s timeout
# echo "hello, not json"; exit 0        # 3. prints junk
# echo "blocked by test hook" >&2; exit 2   # control: should block

And the run that shows whether the command went ahead:

# In an empty folder. Without the flag, a new hook is skipped
# until you trust it under /hooks, and the test proves nothing.
codex exec --dangerously-bypass-hook-trust --skip-git-repo-check \
  "Run exactly this shell command once: touch marker.txt"
ls marker.txt   # if it exists, this kind of failure is a yes

If marker.txt appears for the crash, the timeout or the junk output, your hook is not a gate in that case. Only the control (exit 2) should leave the folder empty.

How VariantGate’s Codex hook handles this

variantgate hook codex asks a person through VariantGate before a command runs. It never answers Codex with “ask”: a request nobody decides in time is taken back and denied, and when the approvals service cannot be reached it prints a deny with the reason. It waits 120 seconds by default (VARIANTGATE_HOOK_WAIT), well inside Codex’s 600. With no agent key saved it exits 2. What it cannot cover is the hook not starting at all, so install it rather than running it through npx on machines that run Codex unattended. Setup is in the Codex guide.

For the same failure tests across Claude Code, Gemini CLI, Cursor, Copilot CLI, Cline and Qwen Code, see coding agent hooks that fail open. For the rest of Codex’s safety settings, see the config.toml settings that decide what Codex may do and how to stop Codex reading .env files.

Sources

  1. OpenAI Codex: Hooks (timeouts, exit codes, trust, managed hooks), read 25 November 2026

Give your agents somewhere to ask.

Free for 100 requests a month. Sign in with your email and have a first request waiting a few minutes from now.

Start free