Hooks and sandboxing
Hooks intercept every call before and after it runs; a sandbox bounds what a shell command can reach, whatever the rules allowed.
Check yourself ↓ three questions and what to remember, at the end
Loading the animation…
Concept
Permission rules (chapter 5) are one checkpoint. A harness has two more. Hooks are the user's own code, run by the harness at fixed points: a pre-tool hook sees every call before it runs and can block it or rewrite it; a post-tool hook sees the result and can add to it. A sandbox is a boundary the operating system enforces around the commands the agent runs: whatever the rules allowed, a sandboxed command cannot write outside the workspace or reach the network.
The task is the failing test again, after which the scripted model wants to install a coverage plugin (pip install pytest-cov), clear the build directory (rm -rf build) and clear a cache in the home directory (rm -rf ~/.cache/pytest). Three hooks are on offer: no-rm blocks any rm; quiet-tests rewrites pytest to pytest -q; lint appends a lint result after every edit. Follow one call through the checkpoints in the animation, then change the sandbox:
- No sandbox, no hooks: every call runs. On a real machine,
rm -rf ~/.cache/pytestwould have deleted part of your home directory. - Workspace sandbox: writes inside the repository and no network. The install and the home-directory delete are stopped (2 calls), while
rm -rf build, inside the repository, still runs. - Read-only sandbox: no writes at all; 3 shell calls are stopped,
rm -rf buildamong them. The edit that fixes the test still happens: it is a file tool, not a shell command, and the sandbox wraps the shell only. That is how the public sandboxes work too (see the table below), and why permission rules still matter inside one. - With the hooks:
no-rmblocks both deletes before the sandbox is reached (2 calls blocked), so in workspace mode the sandbox stops only the install. The rewrite and the lint note change what the model sees, at no risk.
Order matters. A hook runs before the tool; the sandbox applies while it runs. A hook can stop a call the sandbox would have allowed (deleting the build directory), and the sandbox stops what no rule or hook thought of (a path outside the workspace, written some other way). Neither replaces the other.
Maths
Write a call's fate as three predicates, checked in order: the permission decision (with a human's answer if the rules asked), the pre-tool hooks' , and for a shell command the sandbox's :
The first two are the harness's decisions, made before anything runs, from what the call says. The third is the operating system's, made while it runs, from what the call does. A rule or a hook that misreads a command (a shell alias, a script that deletes files) lets it through; the sandbox does not read commands at all. Hooks change the run's time as well: here the three hooks take it from 13.9 s to 12.5 s, because the two blocked deletes never run.
Code
A pre-tool hook, cut from the vendored engine (harness.ts): a block ends the call before it reaches the tool.
if (h.action === "block") {
this.emit("hook", agent, this.t, { call: cid, hook: h.name, phase: "pre", action: "block", result: "blocked" });
outcome = { cid, call: c, args, outcome: "blocked", text: `Blocked by hook ${h.name}: ${h.message ?? "not allowed"}` };
break;
}
And the sandbox's check on a shell command (tools.ts):
if ((NETWORK_COMMANDS.includes(cmd) || installs) && !(sandbox.network ?? false))
return `${cmd}: network access is blocked by the sandbox`;
In production. The same block as a Claude Code PreToolUse hook. In .claude/settings.json, a matcher picks the Bash tool and an if condition narrows it to rm commands, so the script only starts when both match:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"if": "Bash(rm *)",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh",
"args": []
}
]
}
]
}
}
The script reads the call as JSON on its standard input and answers with a decision; printing nothing and exiting 0 leaves the call to the normal permission rules:
#!/bin/bash
# .claude/hooks/block-rm.sh: Claude Code passes the call as JSON on stdin
COMMAND=$(jq -r '.tool_input.command')
if echo "$COMMAND" | grep -q 'rm -rf'; then
jq -n '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: "Destructive command blocked by hook"
}
}'
else
exit 0 # no decision: the normal permission flow applies
fi
From the hooks reference (accessed 2026-10-16). The script was run here on two sample inputs (rm -rf ~/.cache/pytest is denied; pytest -q prints nothing); the settings file needs Claude Code itself to test. A hook that exits with code 2 blocks the call too, with its standard error as the reason.
The sandbox in the public harnesses
| Harness | Hooks and sandbox (from their docs, accessed 2026-10-07) |
|---|---|
| Claude Code | PreToolUse hooks can block a call or replace its arguments (updatedInput); PostToolUse can replace the result (hooks). The Bash sandbox "is off by default"; when on, commands write to the working directory and reach the network through allowed domains; "The sandbox covers shell commands only" (sandboxing) |
| Codex CLI | PreToolUse can deny a call or return updatedInput; PostToolUse can add context (hooks). Sandbox modes read-only, workspace-write and danger-full-access, with the network off by default in workspace-write (approvals and security) |
| OpenHands | PreToolUse hooks can stop a tool call (hooks); a Docker sandbox, the recommended default, runs the agent's commands in a container (sandboxes) |
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: Yes: the edit is a file tool, and the sandbox wraps the shell
Yes. The sandbox here, like Claude Code's, covers shell commands only; the edit is a file tool, so it runs. That is why permission rules still matter inside a sandbox.
Pick one, then reveal.
Reveal the answer
Answer: 2 (counted right within 10%)
The install (it needs the network) and the delete in the home directory
(outside the workspace). rm -rf build is inside the repository, so it
still runs.
Commit to a number, then reveal.
Reveal the answer
Answer: The hook
The hook. A pre-tool hook runs before the tool; the sandbox applies only while it runs, so the call never reaches it. With the hooks the sandbox stops only the install.
Pick one, then reveal.