Part IV · Safety, Policy, and Extensibility
Chapter 13
The Hook System: Intercepting Every Lifecycle Event
Layer: Layer 4 (Safety, Policy, Sandbox & Lifecycle Hooks)
Prerequisites: Chapter 02, Chapter 06, Chapter 07, Chapter 11, Chapter 12
Primary Source Files:
types/hooks.ts— Zod response envelopes (syncHookResponseSchema,hookJSONOutputSchema), prompt-elicitation schemas, and internalHookCallback/HookResulttypesschemas/hooks.ts— cycle-breaking Zod schemas for the 4 persisted hook backends (command,prompt,http,agent) andifrule filtersentrypoints/sdk/coreTypes.ts— runtimeHOOK_EVENTStuple (27 events); re-exports generated SDK hook input/output types (coreTypes.generated.tsis absent from this snapshot, though its source Zod schemas live inentrypoints/sdk/coreSchemas.ts)utils/hooks.ts— 5,022-line core hook engine: trust gate, matcher/dedup/if-filter pipeline,execCommandHook(),executeHooks(),executeHooksOutsideREPL(), and all 27 event entrypointsutils/hooks/execHttpHook.ts&utils/hooks/ssrfGuard.ts— HTTP webhook runner, header env-var allowlisting/CRLF sanitization, sandbox/env proxy routing, and DNS-rebinding-safe SSRF lookup guardutils/hooks/execPromptHook.ts,utils/hooks/execAgentHook.ts&utils/hooks/hookHelpers.ts— single-shot structured-JSON LLM hooks and multi-turn verifier-agent hooksutils/hooks/hooksConfigSnapshot.ts,utils/hooks/hooksConfigManager.ts&utils/hooks/hooksSettings.ts— startup hook snapshotting, enterprise policy precedence, and/hooksUI aggregationutils/hooks/sessionHooks.ts,utils/hooks/registerFrontmatterHooks.ts&utils/hooks/registerSkillHooks.ts— ephemeral per-session/per-agent hooks,FunctionHookcallbacks, andonce: trueself-removalutils/hooks/AsyncHookRegistry.ts,utils/hooks/hookEvents.ts&utils/hooks/fileChangedWatcher.ts— non-blocking background hook polling, out-of-band progress event bus, andCwdChanged/FileChangedenv watchersservices/tools/toolHooks.ts—PreToolUse/PostToolUse/PostToolUseFailureadapters and theresolveHookPermissionDecision()rule-preservation guard
Snapshot boundaries: Two type modules imported across the hook subsystem are absent from this checkout:
types/message.ts(observed at call sites forMessage,AssistantMessage,AttachmentMessage,ProgressMessage, andHookResultMessage) andentrypoints/sdk/coreTypes.generated.ts(re-exported byentrypoints/sdk/coreTypes.ts:19forHookInput/HookJSONOutputtypes, whose authoritative Zod schemas are present inentrypoints/sdk/coreSchemas.ts:355-795).
Why This Layer Exists
By the end of Chapter 12, the agent harness can enforce static permission rules (allow/deny/ask), run an auto-mode security classifier, and isolate shell processes inside an OS sandbox. Static policy covers what the agent may touch, but real engineering teams quickly need programmable interception across the entire lifecycle:
- Rewriting or blocking dangerous tool invocations before they reach the permission dialog (
PreToolUse). - Running a formatter, linter, or MCP response scrubber immediately after a tool finishes (
PostToolUse). - Answering permission prompts from an external enterprise policy server (
PermissionRequest). - Forcing the model to keep working if tests are still red or a structured output tool was skipped (
Stop,SubagentStop). - Reloading
direnv/virtualenv exports when directories or.envrcfiles change (CwdChanged,FileChanged), delegating worktrees to Mercurial or Jujutsu (WorktreeCreate), or auditing mid-session config edits (ConfigChange).
Yet letting .claude/settings.json execute arbitrary shell commands, HTTP webhooks, or nested LLM queries inside an agent harness opens a minefield of failure modes: pre-trust remote code execution on untrusted repos, SSRF into cloud metadata endpoints (169.254.169.254), infinite recursion when a prompt hook submits a prompt, hidden mid-session hook tampering, subagent hook leakage, and permissive hooks punching holes through enterprise deny rules. Every abstraction in utils/hooks/ and services/tools/toolHooks.ts exists to tame one of those hazards without turning every tool call into a latency cliff.
1. The 27-Event Lifecycle Bus, Matching Pipeline, and Dual Dispatch Paths
The Problem
A single hook engine has to serve two wildly different runtime worlds. Inside the interactive query() generator loop, hooks need progress spinners, abort propagation, model-visible attachments, and structured permission/input overrides. Outside the REPL — during process shutdown (SessionEnd), chokidar file-watch events (ConfigChange, FileChanged), compaction (PreCompact), or MCP elicitations — there is no active turn stream or ToolUseContext at all. Worse, on the hot path (PreToolUse / PostToolUse), 99% of sessions have either zero user hooks or only internal analytics callbacks; allocating JSON.stringify payloads or spawning shells just to inspect tool_name would stall every tool batch.
The Real Implementation
entrypoints/sdk/coreTypes.ts:25-53 defines the canonical 27-member HOOK_EVENTS tuple (mirrored with Zod input schemas in entrypoints/sdk/coreSchemas.ts:355-795 and UI descriptions in utils/hooks/hooksConfigManager.ts:26-267):
| Lifecycle Family | Events (HOOK_EVENTS) |
Primary matchQuery Field (utils/hooks.ts:1616-1670) |
Dispatch Path |
|---|---|---|---|
| Tools & Permissions | PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest, PermissionDenied |
tool_name |
executeHooks() |
| Turn & Session | SessionStart, Setup, UserPromptSubmit, Stop, StopFailure, SessionEnd, Notification |
source / trigger / — / error / reason / notification_type |
Mixed (StopFailure, SessionEnd, Notification outside REPL) |
| Context & Compaction | PreCompact, PostCompact, InstructionsLoaded |
trigger / load_reason |
executeHooksOutsideREPL() |
| Subagents & Swarms | SubagentStart, SubagentStop, TeammateIdle, TaskCreated, TaskCompleted |
agent_type / — |
executeHooks() |
| MCP & Workspace | Elicitation, ElicitationResult, ConfigChange, WorktreeCreate, WorktreeRemove, CwdChanged, FileChanged |
mcp_server_name / source / — / basename(file_path) |
executeHooksOutsideREPL() |
Two additional settings-configured command runners — StatusLine and FileSuggestion (utils/hooks.ts:4584-4738) — bypass event matching but reuse execCommandHook(), the trust gate, and managed-only policy. Separately, utils/hooks/postSamplingHooks.ts maintains a programmatic-only registry (registerPostSamplingHook) not exposed in settings.json; for example, utils/hooks/skillImprovement.ts:175-182 registers a post-sampling LLM hook via utils/hooks/apiQueryHookHelper.ts behind feature('SKILL_IMPROVEMENT') and GrowthBook flag tengu_copper_panda.
To break a circular dependency between utils/settings/types.ts and utils/plugins/schemas.ts, the persisted hook schemas live in schemas/hooks.ts, while the response envelopes live in types/hooks.ts with a compile-time type-fest lockstep guard:
// types/hooks.ts:195-200
// Compile-time assertion that SDK and Zod types match
import type { IsEqual } from 'type-fest'
type Assert<T extends true> = T
type _assertSDKTypesMatch = Assert<
IsEqual<SchemaHookJSONOutput, HookJSONOutput>
>
Before any hook runs — inside or outside the REPL — executeHooks() (utils/hooks.ts:1978-1999) and executeHooksOutsideREPL() (utils/hooks.ts:3016-3036) enforce three centralized gates in order:
shouldDisableAllHooksIncludingManaged()— enterprisepolicySettings.disableAllHooks === truekills everything.isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE)— simple/minimal mode disables all hooks.shouldSkipHookDueToTrust()(utils/hooks.ts:286-296) — in interactive mode, every hook requirescheckHasTrustDialogAccepted(); in non-interactive SDK mode, trust is implicit.
On hot paths (PreToolUse, PostToolUseFailure, PermissionDenied, Stop, StopFailure, UserPromptSubmit), callers first probe hasHookForEvent() (utils/hooks.ts:1582-1593), which intentionally over-approximates across all three hook sources so unconfigured sessions skip createBaseHookInput() path joins and matching completely. If only internal callbacks (hook.type === 'callback' && hook.internal === true, such as file-access analytics or commit attribution) match, executeHooks() takes a zero-overhead fast path (utils/hooks.ts:2036-2067) that skips OTEL spans, progress messages, createCombinedAbortSignal, and JSON processing — cutting PostToolUse overhead from 6.01µs to ~1.8µs (-70%).
When user hooks do exist, getMatchingHooks() (utils/hooks.ts:1603-1874) executes a four-stage funnel:
- Matcher filtering (
matchesPattern,utils/hooks.ts:1346-1381): Empty string or*matches all; pure alphanumeric/pipe strings (Write|Edit) run fast exact checks withnormalizeLegacyToolName(); anything else compiles aRegExptested against bothmatchQueryandgetLegacyToolNames(matchQuery)so legacy patterns like^Task$keep matchingAgent. - Source-namespaced deduplication (
hookDedupKey,utils/hooks.ts:1453-1455, 1735-1806): Settings-file hooks share the''prefix so identical commands across user/project/local collapse (last-merged scope wins vianew Map), while plugin and skill hooks prefix withpluginRootorskillRootso two plugins referencing${CLAUDE_PLUGIN_ROOT}/hook.shnever collide (gh-29724). Identity also folds inshell(defaulting to'bash') and theifcondition. - Pre-spawn
ifcondition filtering (prepareIfConditionMatcher,utils/hooks.ts:1390-1421): Hooks onPreToolUse,PostToolUse,PostToolUseFailure, andPermissionRequestmay declare aniffield in permission-rule syntax (e.g.,"Bash(git *)","Read(*.ts)"). Expensive work — tool lookup, Zod input parsing, and Chapter 12'stool.preparePermissionMatcher()(which invokesparseForSecurity()for Bash) — runs once per event batch, returning a closure evaluated per hook before spawning a single subprocess. - Event-specific transport exclusions (
utils/hooks.ts:1850-1864): HTTP hooks are stripped fromSessionStartandSetup.
// utils/hooks.ts:1390-1421
async function prepareIfConditionMatcher(
hookInput: HookInput,
tools: Tools | undefined,
): Promise<IfConditionMatcher | undefined> {
if (
hookInput.hook_event_name !== 'PreToolUse' &&
hookInput.hook_event_name !== 'PostToolUse' &&
hookInput.hook_event_name !== 'PostToolUseFailure' &&
hookInput.hook_event_name !== 'PermissionRequest'
) {
return undefined
}
const toolName = normalizeLegacyToolName(hookInput.tool_name)
const tool = tools && findToolByName(tools, hookInput.tool_name)
const input = tool?.inputSchema.safeParse(hookInput.tool_input)
const patternMatcher =
input?.success && tool?.preparePermissionMatcher
? await tool.preparePermissionMatcher(input.data)
: undefined
return ifCondition => {
const parsed = permissionRuleValueFromString(ifCondition)
if (normalizeLegacyToolName(parsed.toolName) !== toolName) {
return false
}
if (!parsed.ruleContent) {
return true
}
return patternMatcher ? patternMatcher(parsed.ruleContent) : false
}
}
Once matched, executeHooks() lazily stringifies hookInput at most once per batch (getJsonInput(), utils/hooks.ts:2124-2140), yields a hook_progress message for every matched hook, and runs all hooks concurrently via Chapter 01's all(hookPromises) (utils/hooks.ts:2744). Independently of the REPL message stream, utils/hooks/hookEvents.ts broadcasts started, 1-second .unref()-ed progress, and response events for SDK consumers — always emitting SessionStart and Setup (ALWAYS_EMITTED_HOOK_EVENTS), buffering up to MAX_PENDING_EVENTS = 100 events until registerHookEventHandler() attaches, and unlocking all 27 events when setAllHookEventsEnabled(true) is set (SDK includeHookEvents or CLAUDE_CODE_REMOTE).
Hidden Invariants & Failure Modes
The pre-trust RCE scar (
utils/hooks.ts:268-296):captureHooksConfigSnapshot()runs insetup.ts:166before the workspace trust dialog appears. Two historical bugs —SessionEndfiring when a user declined trust andSubagentStopfiring before trust — forcedshouldSkipHookDueToTrust()to be centralized insideexecuteHooks(),executeHooksOutsideREPL(),executeStatusLineCommand(), andexecuteFileSuggestionCommand().
- Never add Zod
.transform()toschemas/hooks.ts(schemas/hooks.ts:130-137,gh-24920/CC-79):parseSettingsFile()usesHooksSchema, andupdateSettingsForSource()round-trips parsed objects throughJSON.stringify. An earlier.transform()wrappingAgentHookSchema.promptinto a closure silently vanished on stringify, deleting users' prompts fromsettings.json. - Headless HTTP deadlock (
utils/hooks.ts:1850-1864): HTTP hooks are filtered out onSessionStartandSetupbecause in headless mode the sandbox network ask callback deadlocks before thestructuredInputconsumer loop has started. - Session-ID alignment on
StopFailure(utils/hooks.ts:3600-3604):executeHooksOutsideREPL()always queries hooks under the maingetSessionId(), soexecuteStopFailureHooks()deliberately gates withgetSessionId()rather thanagentIdto prevent passing the gate and failing execution. - Unblockable enterprise policy audits (
utils/hooks.ts:4232-4236):executeConfigChangeHooks()runs hooks whenpolicy_settingschanges so admins get audit logs, then overwritesblocked: falseon every result so local hooks can never veto enterprise policy.
2. The Four Hook Execution Backends (Plus In-Process Callbacks & Functions)
The Problem
Not every hook can — or should — shell out to /bin/sh. Teams want local shell scripts with cross-platform Windows support, remote HTTPS policy webhooks that cannot be abused for SSRF or secret exfiltration, cheap single-shot LLM judges for semantic guardrails, and full multi-turn verifier agents that can inspect the repo and conversation transcript before letting the main agent finish. Each backend requires a different isolation boundary.
The Real Implementation
HookCommandSchema (schemas/hooks.ts:176-189) defines four persisted backends discriminated on type: 'command', 'http', 'prompt', and 'agent'. Two additional in-memory backends — 'callback' (types/hooks.ts:211-226) and 'function' (utils/hooks/sessionHooks.ts:24-31) — serve SDK control hooks, internal telemetry hooks, and ephemeral validators.
Backend 1: Shell Command (type: 'command') — execCommandHook()
utils/hooks.ts:747-1335 runs shell hooks with a default 10-minute timeout (TOOL_HOOK_EXECUTION_TIMEOUT_MS, overridden to 1,500ms or CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS for SessionEnd, utils/hooks.ts:166-182). It builds directly on Chapter 12's shell primitives:
- Dual shell dispatch (
utils/hooks.ts:790-811, 957-984): Resolveshook.shell ?? DEFAULT_HOOK_SHELL('bash'vs'powershell'). Bash hooks on Windows run viafindGitBashPath()(Cygwin/Git Bash, notcmd.exe), convert all injected paths withwindowsPathToPosixPath(), auto-prependbashfor.shscripts, and applyCLAUDE_CODE_SHELL_PREFIXviaformatShellPrefixCommand(). PowerShell hooks spawngetCachedPowerShellPath()withbuildPowerShellArgs()(-NoProfile -NonInteractive -Command), keep native Windows paths, and intentionally skip.shauto-prepend,SHELL_PREFIX, andCLAUDE_ENV_FILE(since PowerShell writes$env:FOOsyntax that bash cannot source). - Ordered template substitution & missing-plugin pre-check (
utils/hooks.ts:818-857): For plugin hooks,execCommandHook()checksawait pathExists(pluginRoot)before spawning. Without that guard,python3 <missing>.pyexits with code 2 — identical to the hook protocol's blocking code, which would brickUserPromptSubmitorStopuntil restart! It then substitutes${CLAUDE_PLUGIN_ROOT}and${CLAUDE_PLUGIN_DATA}using function-form.replace(..., () => path)(avoiding$-pattern mangling on Windows UNC shares like\\server\c$\plugin) beforesubstituteUserConfigVariables(), so user-entered config values containing${CLAUDE_PLUGIN_ROOT}are treated as opaque literals. Plugin options are also exported asCLAUDE_PLUGIN_OPTION_<KEY>. - Stable root, safe cwd, and
CLAUDE_ENV_FILE(utils/hooks.ts:816, 882-938):CLAUDE_PROJECT_DIRpins togetProjectRoot()(stable even inside a mid-session worktree), whilecwdfalls back fromgetCwd()togetOriginalCwd()if a worktree directory was deleted out from under AsyncLocalStorage. ForSessionStart,Setup,CwdChanged, andFileChanged,CLAUDE_ENV_FILEpoints to a per-hook.shfile whose exports are sourced into subsequentBashToolcommands. - Pipe-mode
wrapSpawn(), newline-terminated stdin, and bidirectional prompt elicitation (utils/hooks.ts:986-1282): UnlikeBashTool(which uses file-fd output), hooks usewrapSpawn()in pipe mode so JS inspectsstdoutincrementally:- Line 1 is sniffed for
{"async": true}(utils/hooks.ts:1117-1164) to background immediately without blocking if the process emits more lines quickly. - When
requestPromptis passed,stdoutis parsed line-by-line againstpromptRequestSchema(types/hooks.ts:28-47), serialized throughpromptChain, and answered over openstdinas{ prompt_response, selected }\n; processed prompt lines are recorded inprocessedPromptLinesand stripped by content match on close (utils/hooks.ts:1243-1249) so elicitation frames never leak intoparseHookOutput(). - Input JSON is always written with a trailing
\n(jsonInput + '\n',utils/hooks.ts:1006, 1210) — a production scar (gh-30509/CC-161) because bashread -r lineexits 1 on EOF without a newline!
- Line 1 is sniffed for
Backend 2: HTTP Webhook (type: 'http') — execHttpHook() + ssrfGuard.ts
utils/hooks/execHttpHook.ts POSTs jsonInput to hook.url with layered defenses against project-configured exfiltration and SSRF:
- URL allowlist before any I/O (
execHttpHook.ts:137-145): ReadsallowedHttpHookUrlsfromgetInitialSettings()(undefined= unrestricted,[]= block all, non-empty = wildcard*match). - Double-gated env-var interpolation & CRLF sanitization (
execHttpHook.ts:76-108, 161-172): Header values may reference$VARor${VAR}, but only variables in the intersection ofhook.allowedEnvVarsand enterprisesettings.httpHookAllowedEnvVarsresolve; everything else becomes'', andsanitizeHeaderValue()strips\r,\n, and\x00to prevent header injection from poisoned env vars. - DNS-rebinding-proof SSRF guard (
utils/hooks/ssrfGuard.ts:42-283): Rather than pre-resolving DNS and letting axios resolve again (a classic TOCTOU DNS-rebinding window),execHttpHook()passesssrfGuardedLookupdirectly into axios'slookupoption, alongsidemaxRedirects: 0:
// utils/hooks/ssrfGuard.ts:245-268 (trimmed)
dnsLookup(hostname, { all: true }, (err, addresses) => {
if (err) {
callback(err, '')
return
}
for (const { address } of addresses) {
if (isBlockedAddress(address)) {
callback(ssrfError(hostname, address), '')
return
}
}
const first = addresses[0]
if (!first) {
callback(Object.assign(new Error(`ENOTFOUND ${hostname}`), {
code: 'ENOTFOUND', hostname,
}), '')
return
}
// ... returns validated address(es) directly to the socket
})
isBlockedAddress() rejects 0.0.0.0/8, 10.0.0.0/8, 100.64.0.0/10 (RFC 6598 CGNAT — used by Alibaba Cloud metadata 100.100.100.200), 169.254.0.0/16 (AWS/GCP/Azure link-local metadata), 172.16.0.0/12, 192.168.0.0/16, IPv6 ::, fc00::/7, fe80::/10, AND IPv4-mapped IPv6 in any representation — expandIPv6Groups() + extractMappedIPv4() (ssrfGuard.ts:133-204) unpack hex-form addresses like ::ffff:a9fe:a9fe (169.254.169.254) before delegating to isBlockedV4(). Loopback (127.0.0.0/8, ::1) is intentionally allowed so developers can run local policy servers.
- Proxy handoff (
execHttpHook.ts:21-41, 176-217):getSandboxProxyConfig()dynamically importsSandboxManager(breaking a static cyclesandbox-adapter → settings → ... → hooks → execHttpHook) and awaitsSandboxManager.waitForNetworkInitialization(). When either the sandbox proxy or an env-var proxy (HTTP_PROXY/HTTPS_PROXY, respectingNO_PROXY) is active,lookupis set toundefined— because the proxy resolves the target's DNS, and runningssrfGuardedLookupwould mistakenly block a corporate proxy hosted on10.0.0.1:3128!
HTTP hooks must return valid JSON (parseHttpHookOutput, utils/hooks.ts:453-487; empty body normalizes to {}). Non-2xx statuses, network errors, and schema failures all yield non_blocking_error.
Backend 3: Single-Shot LLM Prompt Hook (type: 'prompt') — execPromptHook()
utils/hooks/execPromptHook.ts turns a natural-language check into a single structured API call (queryModelWithoutStreaming(), querySource: 'hook_prompt', default 30s timeout, model: hook.model ?? getSmallFastModel(), thinkingConfig: { type: 'disabled' }):
- Expands
$ARGUMENTSviaaddArgumentsToPrompt()and buildscreateUserMessage()directly — deliberately skippingprocessUserInput()so aUserPromptSubmitprompt hook never triggers infinite recursion (execPromptHook.ts:40-42). - Prepends conversation
messageswhen available (e.g., onStop), constrains output viaoutputFormat: { type: 'json_schema' }to{ ok: boolean, reason?: string }, and validates withhookResponseSchema(). { ok: false, reason }returnsoutcome: 'blocking',blockingError,preventContinuation: true, andstopReason: reason. Malformed JSON or API failures degrade safely tonon_blocking_error.
Backend 4: Multi-Turn Agent Hook (type: 'agent') — execAgentHook()
utils/hooks/execAgentHook.ts escalates verification to a full multi-turn query() loop (querySource: 'hook_agent', default 60s timeout, MAX_AGENT_TURNS = 50, model: hook.model ?? getSmallFastModel(), thinkingConfig: { type: 'disabled' }). Running an agent inside a hook demands four explicit containment boundaries:
// utils/hooks/execAgentHook.ts:100-160 (trimmed)
const tools: Tool[] = [
...filteredTools.filter(
tool => !ALL_AGENT_DISALLOWED_TOOLS.has(tool.name),
),
structuredOutputTool,
]
// ...
const hookAgentId = asAgentId(`hook-agent-${randomUUID()}`)
const agentToolUseContext: ToolUseContext = {
...toolUseContext,
agentId: hookAgentId,
abortController: hookAbortController,
// ...
getAppState() {
const appState = toolUseContext.getAppState()
const existingSessionRules =
appState.toolPermissionContext.alwaysAllowRules.session ?? []
return {
...appState,
toolPermissionContext: {
...appState.toolPermissionContext,
mode: 'dontAsk' as const,
alwaysAllowRules: {
...appState.toolPermissionContext.alwaysAllowRules,
session: [...existingSessionRules, `Read(/${transcriptPath})`],
},
},
}
},
}
registerStructuredOutputEnforcement(toolUseContext.setAppState, hookAgentId)
- Tool filtering: Removes any parent
StructuredOutputtool (avoiding duplicate-schema clashes under--json-schema) and stripsALL_AGENT_DISALLOWED_TOOLSso aStopverifier cannot spawn recursive subagents or enter plan mode. - Non-interactive transcript permission: Overrides
getAppState()to forcemode: 'dontAsk'while injecting a scoped session allow ruleRead(/${transcriptPath})so the verifier can inspect the conversation log without hanging on a dialog. - Self-bootstrapped structured output enforcement: Calls
registerStructuredOutputEnforcement()(utils/hooks/hookHelpers.ts:70-83), which registers a session-scopedStopfunction hook underhookAgentIdverifyinghasSuccessfulToolCall(messages, SYNTHETIC_OUTPUT_TOOL_NAME). The moment thestructured_outputattachment arrives,execAgentHook()abortshookAbortController, breaks out offor await, and callsclearSessionHooks(toolUseContext.setAppState, hookAgentId)(execAgentHook.ts:212-233). - Fail-open exhaustion (
execAgentHook.ts:236-268): HittingMAX_AGENT_TURNSor exiting without structured output logs telemetry (tengu_agent_stop_hook_max_turns/tengu_agent_stop_hook_error) and returnsoutcome: 'cancelled'rather than trapping the user in an unbreakable stop loop.
Hidden Invariants & Failure Modes
- Outside-REPL backend limits (
utils/hooks.ts:3152-3186):executeHooksOutsideREPL()only executescommand,http, andcallbackhooks;prompt,agent, andfunctionhooks return non-blocking errors because noToolUseContextormessagesarray exists outside the query loop. - No timeout double-stacking on HTTP hooks (
utils/hooks.ts:2299-2307):executeHooks()passes the raw parentsignal(not the wrapper'sabortSignal) toexecHttpHook(), becauseexecHttpHook()constructs its own combined timeout signal internally.
3. Exit Codes, JSON Decision Envelopes, and Safe Permission Composition
The Problem
Hooks speak through two channels — legacy POSIX exit codes and structured JSON envelopes — and multiple hooks run concurrently for the same event. That creates three subtle composition hazards:
- Conflicting decisions from parallel
PreToolUsehooks (one says'allow', another says'deny'). - A hook returning
{ permissionDecision: "allow", updatedInput: ... }attempting to bypass a user or enterprisesettings.jsondeny/askrule (the exact class of bug flagged asinc-4788in source comments). Stophooks needing to either continue the turn with corrective feedback (exit 2 / blocking error) or halt the entire turn immediately (continue: false).
The Real Implementation
Exit Codes vs. syncHookResponseSchema
For command hooks, executeHooks() checks parseHookOutput(result.stdout) first (utils/hooks.ts:2500-2697). If stdout.trim() starts with {, it validates against hookJSONOutputSchema() (types/hooks.ts:50-176) and dispatches through processHookJSONOutput() (utils/hooks.ts:489-737). Otherwise, it falls back to three exit-code semantics:
| Exit Code / Envelope | Outcome | Effect on Execution |
|---|---|---|
Exit 0 (plain text) |
'success' |
stdout becomes hook_success attachment content (model-visible on UserPromptSubmit, SessionStart, Setup, SubagentStart; transcript-visible on PostToolUse). |
Exit 2 (plain text) |
'blocking' |
stderr becomes blockingError, blocking tool call (PreToolUse), prompt (UserPromptSubmit), stop (Stop/SubagentStop), task creation/completion, or config change (ConfigChange). |
| Other non-zero exit | 'non_blocking_error' |
stderr shown to user only; execution proceeds. |
JSON continue: false |
preventContinuation: true |
Halts turn continuation with optional stopReason (takes precedence over blockingErrors in Stop hooks). |
JSON decision: 'approve' | 'block' |
Legacy top-level decision | Maps to permissionBehavior: 'allow' or 'deny' + blockingError. |
JSON hookSpecificOutput |
Event-scoped structured output | Validated against expectedHookEvent (utils/hooks.ts:583-590); carries permissionDecision, updatedInput, additionalContext, updatedMCPToolOutput, permissionRequestResult, retry, watchPaths, worktreePath, or elicitation actions. |
Notice two UI/transcript deduplication scars in this layer: JSON-output hooks create their hook_success attachment with content: '' (utils/hooks.ts:725-729) so messages.ts suppresses noisy "X hook success: Success" reminders (context travels via additionalContext instead), and services/tools/toolHooks.ts:90-103 skips hook_blocking_error messages already yielded alongside result.blockingError on PostToolUse / PostToolUseFailure (#31301).
Parallel Precedence and Three-Way updatedInput
As concurrent hooks settle via all(hookPromises), executeHooks() aggregates permissionBehavior monotonically with strict precedence deny > ask > allow (utils/hooks.ts:2820-2847): once 'deny' is observed, neither 'ask' nor 'allow' can downgrade it; 'ask' overrides 'allow'; 'passthrough' makes no decision.
Crucially, PreToolUse hooks can rewrite updatedInput in three distinct ways:
- Paired with
permissionDecision: 'allow'(utils/hooks.ts:2850-2868). - Paired with
permissionDecision: 'ask'(rewritten input shown in the prompt dialog). - Passthrough — returning
updatedInputwith nopermissionDecision(utils/hooks.ts:2873-2880→toolHooks.ts:558-563→toolExecution.ts:834-838), rewritingprocessedInputwhile letting normal permission evaluation run untouched.
The inc-4788 Invariant: resolveHookPermissionDecision()
After runPreToolUseHooks() finishes in services/tools/toolExecution.ts:800-862, toolExecution.ts:921 hands hookPermissionResult to resolveHookPermissionDecision() in services/tools/toolHooks.ts:332-433 (shared with REPLTool/toolWrappers.ts so REPL inner calls enforce identical policy):
// services/tools/toolHooks.ts:347-406
if (hookPermissionResult?.behavior === 'allow') {
const hookInput = hookPermissionResult.updatedInput ?? input
// Hook provided updatedInput for an interactive tool — the hook IS the
// user interaction (e.g. headless wrapper that collected AskUserQuestion
// answers). Treat as non-interactive for the rule-check path.
const interactionSatisfied =
requiresInteraction && hookPermissionResult.updatedInput !== undefined
if ((requiresInteraction && !interactionSatisfied) || requireCanUseTool) {
return {
decision: await canUseTool(
tool, hookInput, toolUseContext, assistantMessage, toolUseID,
),
input: hookInput,
}
}
// Hook allow skips the interactive prompt, but deny/ask rules still apply.
const ruleCheck = await checkRuleBasedPermissions(
tool,
hookInput,
toolUseContext,
)
if (ruleCheck === null) {
return { decision: hookPermissionResult, input: hookInput }
}
if (ruleCheck.behavior === 'deny') {
return { decision: ruleCheck, input: hookInput }
}
// ask rule — dialog required despite hook approval
return {
decision: await canUseTool(
tool, hookInput, toolUseContext, assistantMessage, toolUseID,
),
input: hookInput,
}
}
Hook
'allow'never bypassessettings.jsondenyoraskrules.resolveHookPermissionDecision()runscheckRuleBasedPermissions()against the post-rewritehookInput, closing the escalation vector where a hook rewrites a benign command into a denied command and simultaneously approves it. Only whenruleCheck === null(and neitherrequireCanUseToolnor an unsatisfiedrequiresUserInteraction()applies) does hook'allow'skip the interactive prompt.
When hookPermissionResult is 'ask', resolveHookPermissionDecision() passes it as forceDecision to canUseTool() (toolHooks.ts:415-432) so the permission dialog displays the hook's explanation and updatedInput. Before tool.call() executes (toolExecution.ts:1181-1205), if a hook modified or schema-parsed processedInput from a backfillObservableInput clone while leaving file_path equal to the expanded backfill value, toolExecution.ts restores the model's original file_path string so transcript and VCR hashes stay stable.
PermissionRequest, PermissionDenied, PostToolUse, and Stop
PermissionRequest(hooks/toolPermission/PermissionContext.ts:216-263): Fires inside the permission dialog handler when interactive approval would be shown. Returning{ behavior: 'allow', updatedInput?, updatedPermissions? }persists permanent permission updates viahandleHookAllow()and approves without prompting; returning{ behavior: 'deny', message?, interrupt: true }rejects and callstoolUseContext.abortController.abort().PermissionDenied(services/tools/toolExecution.ts:1075-1101, gated onfeature('TRANSCRIPT_CLASSIFIER')): Fires after an auto-mode classifier denial; if any hook yieldsretry: true,toolExecution.tsappends anisMeta: trueuser message notifying the model it may retry.PostToolUse(services/tools/toolHooks.ts:39-191): Can appendadditionalContext, stop continuation (continue: false), or replace an MCP tool's output viaupdatedMCPToolOutput(strictly guarded byisMcpTool(tool)attoolHooks.ts:146, so hooks cannot spoof built-in tool output shapes).Stop/SubagentStop(query/stopHooks.ts:180-455):executeStopHooks()switches to'SubagentStop'wheneversubagentIdis present and passesstop_hook_activeso hooks can detect re-entry and avoid infinite loops. Blocking errors becomeisMeta: trueuser messages that keepquery()iterating — unless any hook setspreventContinuation: true, which short-circuits and dropsblockingErrors(query/stopHooks.ts:325-327). For swarm teammates,TaskCompleted(for each in-progress owned task) andTeammateIdleexecute right afterStoppasses (query/stopHooks.ts:335-453).
Hidden Invariants & Failure Modes
- Interactive-tool override (
toolHooks.ts:350-355): For tools declaringrequiresUserInteraction()(likeAskUserQuestion), a hook returningallowwithoutupdatedInputstill falls intocanUseTool; returningallowwithupdatedInputsetsinteractionSatisfied = truebecause the hook itself collected the user's answer. - No double
hook_blocking_error(toolHooks.ts:90-103, 245-255): Exit-code-2 hooks only yield{ blockingError }, whereas JSON{ decision: 'block' }hooks yield both{ blockingError }and{ message: hook_blocking_error }.runPostToolUseHooksandrunPostToolUseFailureHooksfilter out the duplicate attachment message explicitly.
4. Consistency Across Time, Subagents, and Background Execution
The Problem
Three operational dynamics threaten hook reliability in long-running sessions:
- Mid-session settings drift: Re-reading
.claude/settings.jsonon every tool call would let an agent (or rogue script) silently install a hook mid-session; yet legitimate/hooksedits,--worktreeswitches, and enterprise policy pushes do need controlled reloads. - Subagent and skill isolation: Skills and subagents declare temporary frontmatter hooks, and
StructuredOutputregisters temporaryStopfunction hooks. Cross-session leakage breaks parent turns, while naive immutable copies ofAppStatetrigger O(N²) overhead and UI listener storms under parallel workflow agents. - Blocking latency vs. background wakeups: Long verification scripts cannot block every tool call synchronously, but backgrounding naively drops stderr or fails to notify an idle agent when a background check fails.
The Real Implementation
Startup Snapshots & Three-Source Merging (hooksConfigSnapshot.ts, hooksConfigManager.ts)
setup.ts:166 calls captureHooksConfigSnapshot() immediately after setCwd(cwd) (and re-calls updateHooksConfigSnapshot() at setup.ts:284 after --worktree changes directories, at ExitWorktreeTool.ts:140 on worktree exit, and in applySettingsChange.ts:42 on approved settings reloads). Inside utils/hooks/hooksConfigSnapshot.ts:18-53, getHooksFromAllowedSources() enforces a four-tier enterprise precedence ladder:
policySettings.disableAllHooks === true→{}(all hooks disabled, including managed).policySettings.allowManagedHooksOnly === true→policySettings.hooks ?? {}(ignores user, project, local, plugin, and session hooks).isRestrictedToPluginOnly('hooks')→policySettings.hooks ?? {}(blocks user/project/local settings hooks, while plugin hooks and admin-trusted agent frontmatter hooks remain permitted — gated at registration time intools/AgentTool/runAgent.ts:564-568whereagentDefinition.sourceis known).- Non-managed
mergedSettings.disableAllHooks === true→policySettings.hooks ?? {}(non-managed settings cannot disable enterprise managed hooks).
updateHooksConfigSnapshot() calls resetSettingsCache() first (hooksConfigSnapshot.ts:104-112) so external edits followed immediately by /hooks never read stale cached settings before chokidar's stability threshold elapses.
At execution time, getHooksConfig() (utils/hooks.ts:1492-1566) assembles matchers from three independent registries:
- Snapshot settings hooks (
getHooksConfigFromSnapshot()). - Process-level registered hooks (
getRegisteredHooks()inbootstrap/state.ts:1419-1465— plugin native hooks carryingpluginRoot, SDK control callbacks, and internal hooks). WhenshouldAllowManagedHooksOnly()is true, entries with'pluginRoot' in matcherare skipped (utils/hooks.ts:1524). - Ephemeral session hooks (
appState.sessionHooks, skipped entirely undermanagedOnlyso agent/skill frontmatter cannot bypass enterprise lock-down).
For the /hooks management UI, utils/hooks/hooksConfigManager.ts and utils/hooks/hooksSettings.ts group hooks by event and matcher, deduplicate identical resolved file paths (when running from $HOME, userSettings and projectSettings both resolve to ~/.claude/settings.json, hooksSettings.ts:109-122), hide all settings hooks when allowManagedHooksOnly is active, and expose [ANT-ONLY] Built-in Hook entries only under process.env.USER_TYPE === 'ant' (hooksConfigManager.ts:346-359).
Subagent & Skill Scoping and the Map Identity Scar (sessionHooks.ts)
utils/hooks/sessionHooks.ts scopes every temporary hook to a sessionId — either the main session ID or a subagent's agentId (utils/hooks.ts:2003). Three helpers build on it:
registerFrontmatterHooks()(called fromtools/AgentTool/runAgent.ts:568withisAgent = true, cleaned up viaclearSessionHooks(rootSetAppState, agentId)atrunAgent.ts:821): automatically convertsStophooks toSubagentStopbecause subagents fireSubagentStop(registerFrontmatterHooks.ts:40-45).registerSkillHooks()(called fromutils/processUserInput/processSlashCommand.tsx:877): preservesskillRoot(exported asCLAUDE_PLUGIN_ROOTfor skill/plugin parity) and implementsonce: trueby registering anonHookSuccesscallback that invokesremoveSessionHook()after the firstoutcome === 'success'(utils/hooks.ts:2919-2927).addFunctionHook()(sessionHooks.ts:93-115): attaches in-memoryFunctionHookcallbacks (e.g.,registerStructuredOutputEnforcementandteammateInit.ts:98).
The container type itself carries a textbook performance scar:
// utils/hooks/sessionHooks.ts:48-62
/**
* Map (not Record) so .set/.delete don't change the container's identity.
* Mutator functions mutate the Map and return prev unchanged, letting
* store.ts's Object.is(next, prev) check short-circuit and skip listener
* notification. Session hooks are ephemeral per-agent runtime callbacks,
* never reactively read (only getAppState() snapshots in the query loop).
*
* This matters under high-concurrency workflows: parallel() with N
* schema-mode agents fires N addFunctionHook calls in one synchronous
* tick. With a Record + spread, each call cost O(N) to copy the growing
* map (O(N²) total) plus fired all ~30 store listeners. With Map: .set()
* is O(1), return prev means zero listener fires.
*/
export type SessionHooksState = Map<string, SessionStore>
Non-Blocking Async Hooks, asyncRewake, and Reactive Env Watchers
Command hooks can opt into background execution either statically (async: true or asyncRewake: true in BashCommandHookSchema) or dynamically by emitting {"async": true, "asyncTimeout": ...} on stdout line 1 (utils/hooks.ts:995-1030, 1117-1151, suppressed if forceSyncExecution is true). From there, executeInBackground() (utils/hooks.ts:184-265) splits into two distinct delivery engines:
- Standard Async (
AsyncHookRegistry.ts): CallsshellCommand.background(processId)and registers aPendingAsyncHook(default 15s timeout, 1s progress polling). Before each subsequent model turn,getAsyncHookResponseAttachments()(utils/attachments.ts:3464-3518) pollscheckForAsyncHookResponses()(AsyncHookRegistry.ts:113-268), which usesPromise.allSettled()so one throwing hook cannot orphan another's cleanup, scans completed stdout for the first non-async JSON line, marksresponseAttachmentSent = true, emitshookResponse, invalidates the session env cache if aSessionStarthook completed (AsyncHookRegistry.ts:257-262), and yieldsasync_hook_responseattachments. In headless/print mode,cli/print.ts:2674, 4134callsfinalizePendingAsyncHooks()to flush completed hooks and kill still-running stragglers on shutdown. - Wake-on-Failure (
asyncRewake: true,utils/hooks.ts:205-246): BypassesAsyncHookRegistryand deliberately skipsshellCommand.background()— becausebackground()invokestaskOutput.spillToDisk(), under whichgetStderr()returns''! Instead, theStreamWrappers keep piping into in-memoryTaskOutputbuffers; whenshellCommand.resultresolves, it yields one tick viaawait new Promise(resolve => setImmediate(resolve))so pending stdio'data'events drain after'exit', then on exit code2enqueues atask-notificationviaenqueuePendingNotification()to wake an idle model or inject mid-query.
Finally, utils/hooks/fileChangedWatcher.ts closes the loop for environment-sync hooks: initializeFileChangedWatcher(cwd) (setup.ts:172) watches pipe-separated filenames from FileChanged matchers (e.g., .envrc|.env) plus dynamic watchPaths returned by SessionStart, CwdChanged, or FileChanged hooks (chokidar with stabilityThreshold: 500). Whenever CwdChanged or FileChanged runs, executeEnvHooks() (utils/hooks.ts:4241-4258) invalidates the session environment cache so new CLAUDE_ENV_FILE exports immediately apply to future BashTool commands.
Hidden Invariants & Failure Modes
- Mirroring
managedOnlyin existence checks (utils/hooks.ts:4910-4920):hasWorktreeCreateHook()must skip plugin hooks whenshouldAllowManagedHooksOnly()is true — mirroringgetHooksConfig(). OtherwisehasWorktreeCreateHook()would returntrue, skip native git worktree setup, and thenexecuteWorktreeCreateHook()would find zero runnable hooks and throw. onHookSuccessscoping nuance (utils/hooks.ts:2907-2928): TheonHookSuccesslookup callsgetSessionId()(the main session ID) rather thantoolUseContext?.agentId, which works for main-sessionregisterSkillHooks(once: true) while subagent frontmatter hooks rely onclearSessionHooks(..., agentId)at teardown.- Post-exit stream drain (
utils/hooks.ts:219-222): InasyncRewake,shellCommand.resultresolves on process'exit'before final stdout/stderr'data'chunks finish flushing intoTaskOutput; thesetImmediateyield prevents truncating the very stderr message meant to wake the model.
Putting the Layer Together
First, the full hook configuration, matching, and multi-backend execution pipeline:
flowchart TD
S1["Snapshot Settings (hooksConfigSnapshot.ts)"] --> M["getHooksConfig()"]
S2["STATE.registeredHooks (plugins + SDK callbacks)"] --> M
S3["AppState.sessionHooks Map (skills + subagents + functions)"] --> M
M --> G["Central Gates: disableAllHooks / CLAUDE_CODE_SIMPLE / trust"]
G --> F["getMatchingHooks(): pattern -> dedup -> if-rule AST -> HTTP filter"]
F --> B1["command: execCommandHook() (bash / pwsh / pipe wrapSpawn)"]
F --> B2["http: execHttpHook() + ssrfGuardedLookup()"]
F --> B3["prompt: execPromptHook() (single-shot json_schema)"]
F --> B4["agent: execAgentHook() (50-turn dontAsk query())"]
F --> B5["callback / function (in-process fast path)"]
B1 -- "async / line-1 async" --> AR["AsyncHookRegistry -> attachments.ts poll"]
B1 -- "asyncRewake + exit 2" --> QN["enqueuePendingNotification() (task-notification)"]
B1 & B2 & B3 & B4 & B5 --> AGG["all() parallel merge: deny > ask > allow + updatedInput"]
AGG --> TH["toolHooks.ts / stopHooks.ts / PermissionContext.ts"]
And the exact tool-execution interception envelope (where hooks compose safely with Chapter 11's permission engine):
tool_use (Zod parsed + validateInput + backfillObservableInput)
│
▼
runPreToolUseHooks() ──► parallel hooks ──► precedence: deny > ask > allow
│ (+ updatedInput on passthrough)
▼
resolveHookPermissionDecision()
├─ hook 'deny' ─────────────────────────► tool_result (is_error: true)
├─ hook 'allow' ──► checkRuleBasedPermissions(tool, hookInput)
│ ├─ deny rule ─────► DENIED (inc-4788 invariant!)
│ ├─ ask / reqUI ───► canUseTool() ─► PermissionReq
│ └─ null ──────────► execute tool.call(callInput)
└─ 'ask' / none ──► canUseTool(forceDecision) ─► PermissionRequest hooks
│
┌───────────────────── denied (auto-mode) ◄────────┤ allowed
▼ ▼
PermissionDenied hooks (retry?) tool.call(callInput)
├─ ok ─► PostToolUse
│ (updatedMCPOut?)
└─ err ─► PostToolUseFail
How This Snaps Into the Next Layer
Chapter 13's hook snapshot and ConfigChange event form a tight bidirectional contract with Chapter 14 (Configuration, Settings Precedence, and Enterprise Policy):
- Hooks gate live settings reloads: In
utils/settings/changeDetector.ts:290-301andutils/settings/changeDetector.ts:343-353, every detected disk edit or deletion firesexecuteConfigChangeHooks()before callingfanOut(source). If any hook exits2or returns{ decision: 'block' }(hasBlockingResult()), the reload is vetoed — except forpolicy_settings, whereutils/hooks.ts:4234-4236forcesblocked: falseso enterprise policy always lands. - Settings reloads refresh the hook snapshot: Once
fanOut(source)passes,utils/settings/applySettingsChange.ts:42invokesupdateHooksConfigSnapshot()alongsideloadAllPermissionRulesFromDisk(), keeping both interactive (AppState.tsx) and headless (cli/print.ts) hook snapshots synchronized with disk. - Enterprise policy knobs live in the settings schema:
utils/settings/types.ts:459-499declaresdisableAllHooks,allowManagedHooksOnly,allowedHttpHookUrls, andhttpHookAllowedEnvVarsand importsHooksSchemafromschemas/hooks.ts— the exact multi-source settings hierarchy Chapter 14 dissects next.
Reading Exercises & Code Checkpoints
- Audit the
inc-4788permission-composition invariant andupdatedInputbackfill restoration: Runrg -n "checkRuleBasedPermissions|interactionSatisfied|hookUpdatedInput|backfilledClone" services/tools/toolHooks.ts services/tools/toolExecution.tsand trace whyresolveHookPermissionDecision()evaluatescheckRuleBasedPermissions()onhookInput(not the originalinput) and whytoolExecution.ts:1189-1205restorescallInput.file_pathwhen unchanged frombackfilledClone. - Verify the SSRF guard's IPv6 hex-mapped defense and proxy bypass:
Inspect
utils/hooks/ssrfGuard.ts:88-204andutils/hooks/execHttpHook.ts:176-217. Trace how::ffff:a9fe:a9feis decoded into169.254.169.254, why100.64.0.0/10is blocked while127.0.0.0/8is allowed, and whylookupbecomesundefinedundersandboxProxyorenvProxyActive. - Trace the three hook registries,
allowManagedHooksOnly, and theSessionHooksStateMaptrick: Runrg -n "getHooksConfigFromSnapshot|getRegisteredHooks|sessionHooks|shouldAllowManagedHooksOnly" utils/hooks.ts utils/hooks/hooksConfigSnapshot.ts utils/hooks/sessionHooks.tsand confirm whyhasWorktreeCreateHook()(utils/hooks.ts:4910-4920) must mirrorgetHooksConfig()'spluginRootfilter, and whysessionHooks.tsmutatesprev.sessionHooksin place and returnsprev. - Follow the Chapter 13 ↔ Chapter 14
ConfigChangefeedback loop: Runrg -n "executeConfigChangeHooks|hasBlockingResult|updateHooksConfigSnapshot|policy_settings" utils/settings/changeDetector.ts utils/settings/applySettingsChange.ts utils/hooks.tsand verify which config sources aConfigChangehook can block versus audit-only.
Manuscript chapters/13-the-hook-system.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.