28.6 Fail closed
- Approved by
- Panaversity ·
In everyday life. When the power fails, gates behave in two ways. A fire exit unlocks, so people can get out. A bank's vault stays locked, so nobody can get in. Neither choice is wrong. Each gate is built for what stands behind it. In this section, a guard that lets everything through when it breaks fails open, like the fire exit. A guard that refuses fails closed, like the vault. And money stands behind the payment guard.
What you will learn. You practice this in Lab 28, Task 3.
- What does "fail closed" mean?
- What does a broken hook do on the Agent SDK, and in Claude Code?
- How do you make a hook fail closed, and what does that setting not cover?
- What do the other guards do when they break?
- What does failing closed cost?
Why it matters. A guard that works on the day you test it can break the next week. Claude Code's own reference says what follows: "A policy hook with a wrong path or a crashing script then lets everything through."1
At work. Suppose the builder had left out onFailure, and a week later the staging connector moves to a new address. One line still sends the hook's call to get_approval, the connector's read-only tool that looks up an approval (28.4), to the old address. The next proposed payment reaches the hook, the call to get_approval fails, and the callback throws. On the Agent SDK, a callback that throws lets the call go on to the other checks.2 Only the connector refuses, because no approvals row matches. With onFailure: "block" set, the hook itself blocks the call, and the SDK's page says the tool result then includes failed; blocking because onFailure is "block".2
What does "fail closed" mean?
A guard that fails closed denies when it breaks. A guard that fails open lets the call through. Every guard that decides should fail closed, because a guard that fails open is only a guard on the days it works. Section 22.5 gave the default for one place: "A hook that fails does not block." (22.5).
What does a broken hook do on the Agent SDK, and in Claude Code?
On both, most failures let the call go ahead. They differ on a timeout. Claude Code's hooks are handlers in its settings files and plugins, and the common one is a command hook, a program Claude Code starts, which answers by its exit code and its output.
| What breaks | Agent SDK callback | Claude Code command, HTTP or MCP tool hook |
|---|---|---|
| It throws, or the command exits with a code other than 0 or 2 | The call goes ahead, and your app gets a warning tagged hook_callback_failed | The call goes ahead. It is a "non-blocking error" |
| It returns nothing, or output that fails validation | The call goes ahead. A path with no return counts as failing | The call goes ahead. Invalid JSON is a non-blocking error too |
| It runs past its time limit | The call is blocked | The call goes ahead |
| How to make it fail closed | onFailure: "block" on the matcher (TypeScript SDK 0.3.317 or later), on_failure="block" in Python (0.2.177 or later) | "onFailure": "block" on the hook (Claude Code 2.1.295 or later, and 2.1.328 or later on an MCP tool hook) |
The Agent SDK column comes from its hooks page,2 and the Claude Code column from its reference.1 The timeout row is the one to remember. "On PreToolUse, a timed-out command, http, or mcp_tool hook doesn't block the tool call", but "An Agent SDK callback hook that exceeds its timeout blocks the tool call."1
Exit codes in Claude Code have one more trap. Exit 2 blocks the call (28.7). Exit 1, the usual failure code in Unix, does not. And "when a PreToolUse hook exits 1 but prints JSON that passes validation, that JSON can still decide what happens."1 To deny on purpose, exit 2, or exit 0 with a deny in valid JSON.
How do you make a hook fail closed, and what does that setting not cover?
Set it to block on failure, then let its errors reach the SDK. With "block" set, a throw, a missing return, invalid output and a timeout all block the call. In Claude Code, so does an exit code other than 0 or 2, "even if it printed JSON that allows the action".1 If the SDK cannot honor the setting, "the query fails at startup and sends no prompt".2 That is failing closed too.
The setting cannot see a failure your own code hides. "A callback that catches its own error and returns {} isn't failing, so let the error propagate from a guard."2 A try and catch around get_approval that returns {} turns a broken guard into a hook that decides nothing. And on a path where the hook decides nothing on purpose, write return {}, because with "block" set a path with no return blocks.
Three more things stay open with the setting on:
- Some events. It "has no effect" on
Stop,SubagentStop,TaskCompletedandTeammateIdlehooks, or on command hooks that run in the background.1 - A hook that never runs. A matcher that misses, or a hook that is not loaded, has not failed, so nothing blocks (28.5).
- Another program that approves. In Claude Code, a mod, an installed extension that can answer the permission check, can sometimes approve a call a hook blocked. The conditions are in 28.7.
What do the other guards do when they break?
Each one has its own default, so check each one.
- The connector's check fails closed by design. If the
schedule_paymenthandler cannot readapprovals, it refuses. An error is never a "yes". - Claude Code's sandbox fails open by default. "By default, when the platform doesn't support the sandbox or the dependency check at startup fails, Claude Code runs commands without sandboxing." Setting
sandbox.failIfUnavailabletotruemakes it stop at startup instead.3 - A hosted "ask" fails closed, and can wait forever. With
always_ask, "The session waits indefinitely for a response."4 Nothing runs until a person answers (23.8).
What does failing closed cost?
When the guard breaks, real work stops. After the address mistake above, every proposed payment is blocked until someone fixes the line. That is the right failure for money, and the wrong one for a guard on a read tool. So set "block" on the guards in front of an effect, and watch the hook_callback_failed warning, so a broken guard is found in minutes. Then make the callback throw on purpose, and check that the hook blocked the call.
The operating rule: every guard in front of an effect must deny when it breaks. Set it to block on failure, let its errors through, and make it fail once on purpose to prove it.
Do this now. Ask your coding agent to break your own guard on purpose. Copy this line:
Find each hook or permission callback in this project that can deny a tool call. For each one, tell me what happens to the call if it throws, returns nothing, or runs past its time limit, and quote the setting that decides it. Then propose one test that makes it fail on purpose and checks that the call is blocked. Change no file until I agree.
Check yourself
Question 1 / 8
0 answered
What does it mean for a guard to fail closed?
Sources
- Owner
- Panaversity