# Hooks in Practice URL: https://stevekinney.com/courses/agentic-engineering-playbook/hooks-in-practice Canonical: https://stevekinney.com/courses/agentic-engineering-playbook/hooks-in-practice Author: Steve Kinney Language: en-US Modified: 2026-12-03T19:45:07.000Z Description: How hooks behave inside subagents, how to test and roll them out safely, the ways guards fail, and what real-world hooks look like in the wild. Course: Agentic Engineering Playbook Course URL: https://stevekinney.com/courses/agentic-engineering-playbook --- A hook that passes in your terminal can still fail where it matters: in a subagent, under a different permission mode, after a harness upgrade, or on the one input you didn't try. This lesson assumes you've read [Hooks]() and [Writing Good Hooks](). ## Hooks and subagents A _subagent_ is a helper the main agent starts with its own fresh context. Here's how hooks treat it: - **Settings hooks fire inside subagents, too**: By default, only exit `2` or a JSON decision blocks there, same as in [Hooks](). An old report claiming they didn't fire at all turned out to be a hook exiting `1`, which doesn't block by default. Since `2.1.295`, a hook that sets `"onFailure": "block"` does block when it fails that way. - **Agent frontmatter hooks are scoped to the agent**: They live only while it runs, and its `Stop` becomes `SubagentStop`. Agents loaded from a plugin ignore the `hooks` field entirely, so a plugin ships its hooks in `hooks/hooks.json` instead. - **`SubagentStart` can't block**: It's configured in settings and matched on the agent's name. It _can_ inject `additionalContext`, extra text for the conversation, before the subagent's first prompt. - **`SubagentStop` can block with a reason**: That keeps the _same_ worker going, so it's the cleanest way to enforce a report shape. - **`session_id` and `transcript_path` belong to the parent**: Use `agent_id` to detect a subagent. ## Testing and rolling out hooks - **Pick the earliest boundary with enough information**: Run cheap checks after each edit. Save expensive tests for the end of a task (the `Stop` event) or for when work is submitted, like a commit. - **Test in layers**: Pure policy, then the process boundary (stdin, stdout, exit code), then the real harness, then concurrency and injected failures. - **Prove a block harmlessly**: Point a disposable tool at a marker file and check that a denied call never creates it. - **Observe first**: Run new policies in observe-only mode, logging what the hook _would_ have blocked. Read the log before turning blocking on. - **Keep a compatibility manifest**: Record the harness versions you've tested and a set of sample payloads (fixtures). - **Make your inner deadline shorter than the hook's timeout**: If your script gives up first, it fails the way you chose, not the harness's way. The harness's way is rarely what you want: a `PreToolUse` hook that times out lets the call through unless it sets `"onFailure": "block"`. ### Re-run the fixtures after every upgrade A release can change what your configuration _means_ without touching a single file of yours. For a hook, that might be when it fires or what its payload holds. The clearest example isn't even a hook. It's a subagent setting, and the same thing can happen to a hook. Suppose a security-reviewer subagent sets `model: opus`, and a shell profile sets `CLAUDE_CODE_SUBAGENT_MODEL=haiku` to save money. Per the Claude Code changelog, before `2.1.251` that variable overrode everything, including the definition, so the reviewer quietly ran on Haiku. Version `2.1.251` made the variable the _default_, so the definition's `model:` wins. With zero edits, the reviewer silently started running on Opus. Checking that your settings file still parses won't catch that. Checking the _observed behavior_ will. A fixture here spawns the reviewer on a fixed prompt and records which model served it. Run it before and after an upgrade, and the difference shows up. For a hook, replay recorded payloads and compare the block or allow you observe. Worth it? For a security or spend control, yes. For a low-stakes personal setup, no. It only catches what you fixtured, so keep skimming the changelog for hooks, subagents, and permissions. ## Ways guards fail - **A stop hook with only a success path**: If the checks keep failing, a hook that always blocks the stop traps the agent. Add a repair counter (blocked attempts, stored in a file you manage) and a terminal-failure path that lets the stop through and reports the failure. - **Interpolating filenames into shell source**: A filename containing `$(…)` is all it takes. Pass filenames as arguments, or read them from the JSON on stdin. - **Assuming the sandbox contains hooks**: The sandbox is operating-system-level isolation that limits which files and network the agent's shell commands can reach. Hooks run on the host, outside it. Set `CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1` to remove recognized credentials from subprocess environments, hooks included. GitHub tokens, proxy credentials, and unrecognized secrets can remain, so launch with a clean environment and keep sensitive credentials outside the process. [Secrets]() covers those exclusions. - **Confusing bypass mode with asynchronous hooks**: `--dangerously-skip-permissions` skips permission prompts, but hooks still run synchronously by default and a `PreToolUse` denial still blocks the call. A command hook configured with `"async": true` runs in the background and can't block, in any permission mode. Keep enforcement hooks synchronous; the [background-hook reference](https://code.claude.com/docs/en/hooks#run-hooks-in-the-background) explains the distinction. - **Editing a Codex guard without re-trusting it**: Codex trusts each hook by a hash of its definition, so an edited guard gets skipped until you trust it again. `--dangerously-bypass-hook-trust` runs enabled hooks without persisted source trust. Their blocking decisions still apply; what you lose is the integrity check on which hook code may execute. Trust the reviewed definition instead of letting changed or unreviewed hook code run unattended. - **Running headless in an untrusted clone**: Hooks committed to a repository run without the folder-trust dialog in headless mode (`claude -p`, one non-interactive session that exits) and SDK mode (Claude run from your own code). Before you script `claude -p` over a repository you didn't write, review its `.claude/` settings files, start with `--bare`, or pass `--settings '{"disableAllHooks": true}'`. Repository-supplied settings are a real attack surface even with the dialog: several 2026 Claude Code CVEs (published security vulnerabilities) got around it in interactive sessions, including [CVE-2026-33068](https://advisories.gitlab.com/npm/@anthropic-ai/claude-code/CVE-2026-33068/), where a committed `.claude/settings.json` skipped the trust dialog by setting `permissions.defaultMode` to `bypassPermissions` (fixed in `2.1.53`). [Blast radius]() covers limiting what a compromised hook can reach. ## Advanced techniques - **Bounded continuation**: Claude Code overrides a `Stop` hook after 8 blocks in a row (`CLAUDE_CODE_STOP_HOOK_BLOCK_CAP` changes that). The count resets each time Claude calls a tool, so a loop that does work between blocks never hits the cap. Check `stop_hook_active`, which says you're already in a continuation, and keep your own retry budget. Codex documents no cap. - **A stop-hook loop versus a fresh-context Ralph loop**: A Ralph loop starts a fresh agent for each task. A stop-hook loop stays in one session, piles up context, and eventually fails through compaction, which replaces the conversation with a summary. Use the hook as a gate and fresh context for long iteration. See [The Ralph Loop](). - **Concurrency**: Skip this unless your hooks touch shared state. Hooks for one event run in parallel, and sessions can share a resource. Take an atomic lock on the resource, not the session, and use idempotency keys so a retried operation is recognized and skipped. When work has to outlive the agent process, use an outbox (a queue a separate worker drains) with fencing tokens: increasing numbers that let you reject writes from a stale worker. - **Classify failures before you retry**: If a hook's call to an outside service times out, ask the service whether it already did the work. Retry timeouts, not rejections. - **Across tools** (Codex, Gemini CLI, Cursor, Copilot): Keep one pure evaluator and a thin adapter per tool, because timeout units, failure behavior, and trust models differ. Codex's `PreCompact` and `PostCompact` hooks, for example, can't inject context, so use a `SessionStart` hook with the `compact` matcher, which fires only after a compaction. ## Hooks in the wild - **Formatter on edit**: A `PostToolUse` hook, as in [Boris Cherny's setup](https://x.com/bcherny/status/2007179832300581177). - **Protected paths and command classifiers**: Tools like [`nah`](https://nahguard.ai/) classify shell commands and guard protected paths. Block when work is submitted, like a commit or push, and only hint on edits. - **The stop-hook test gate**: The well-known bug here is a script that ends with `cat`, say, to print the test log. A shell script exits with its last command's status, and `cat` succeeds, so the gate exited `0` whatever the check found and never blocked anything. - **iTerm2's [`cc-status`](https://github.com/gnachman/iTerm2/blob/master/it2cli/Sources/it2core/Commands/HandleAgentHookCommand.swift)**: A cosmetic status hook that knows about background tasks. (`cc-status` is now a shim around `it2 handle-agent-hook`, which is where the link goes.) Its own failures never fail the hook, on purpose, because a status update must never block anything. That's a virtue here and a bug in a gate. - **Also out there**: Re-injecting context after compaction, token savings with [Trail-code](https://github.com/trailhq/Trail-Code), formerly Graft (its hooks add the matching files and functions to each prompt, so the agent greps less), registering a session with a [channel](https://code.claude.com/docs/en/channels) broker that relays outside events like pull request activity, and reflection hooks that write the lesson from a mistake into `CLAUDE.md`. The most useful hook is one you can explain in a sentence, test without invoking a model, and observe when it fails.