Docs Extend
Hooks
Run your own commands before and after every tool call to enforce a policy, block a dangerous call, force a question, or tidy up after edits, with examples.
Hooks are commands you configure that run around every tool call. They let a rule you care about (no rm -rf, no secrets in output, format after every edit) hold whatever the model remembers.
Where hooks live
Write hooks.json in the app’s data folder:
~/Library/Application Support/dev.quad.harness/hooks.json
It is read at the start of each turn, so an edit applies to the next turn without restarting anything. A missing or empty file means no hooks.
Hooks are only read from the data folder, never from a project. A folder you open must never be able to bring a command of its own.
There is no editor for hooks in the window. Edit the file.
The format
hooks.json is a list of hooks:
[
{
"id": "no-force-push",
"event": "pre_tool",
"match": "bash",
"command": "~/.config/quad/guard.sh",
"timeoutSeconds": 10
},
{
"event": "post_tool",
"match": "write_file|edit_file",
"command": "cargo fmt --quiet"
}
]
| Field | Meaning |
|---|---|
id |
Names the hook in refusals and records. Defaults to hook1, hook2 and so on by position. Must be unique. |
event |
pre_tool (before the tool runs) or post_tool (after it ran) |
match |
Which tools: * (the default), a tool name, a prefix ending in * (such as mcp__fs__*), or several joined with | |
command |
Run with sh -c in the session’s workspace folder |
timeoutSeconds |
1 to 600 (default 30). When it runs out, the hook’s whole process group is ended. |
Unknown fields are an error, so a misspelled timeoutSeconds is not silently ignored.
What a hook is told
One JSON object on standard input:
{"event": "pre_tool", "sessionId": "s_…", "workspace": "/Users/you/code/my-app", "turn": 3,
"callId": "call_…", "tool": "bash", "args": {"command": "git push --force"}}
A post_tool hook also gets output (the result as the model would see it, after any earlier post_tool hook changed it) and isError.
The same facts are in the environment: QUAD_HOOK_EVENT, QUAD_HOOK_ID, QUAD_SESSION_ID, QUAD_WORKSPACE and QUAD_TOOL.
What a hook may answer
By exit status
| Exit status | Meaning |
|---|---|
0 |
No objection |
2 |
Deny (pre_tool) or block the result (post_tool). The first 500 characters of standard error are the reason. |
| Anything else | Treated as a failure (see below) |
By JSON on standard output
If standard output starts with {, it is read as an answer:
| Event | Field | Effect |
|---|---|---|
pre_tool |
"decision": "deny" and "reason" |
The tool does not run. The model sees Blocked by hook <id>: <reason>. |
pre_tool |
"decision": "ask" |
You are asked first, even if the permission mode would not ask. |
pre_tool |
"decision": "allow" |
Skip the question Standard mode would have asked. |
post_tool |
"decision": "block" and "reason" |
The result is replaced by an error saying so. |
post_tool |
"replace": "text" |
The result becomes this text. |
| both | "context": "text" |
Added after the result as [Added by hooks], never inside it. |
Plain text on standard output is ignored, so a hook may log freely.
The rules
- A denial cannot be overridden. The first
pre_toolhook to deny ends the chain. No later hook, and noallow, changes that. Precedence among answers is deny, then ask, then allow. - Allow never lifts Plan mode. Plan mode is decided first. A call Plan mode blocks does not run its hooks at all.
- Always allow rules come first, hooks after. A rule skips the question, but a hook can still ask or deny.
- It fails closed. A
pre_toolhook that cannot start, times out, exits with a status other than 0 or 2, or prints JSON that cannot be read, denies the call. Ahooks.jsonthat cannot be read denies every tool call, with the error as the reason, until you fix or remove it. - A failing
post_toolhook only adds a note, since the tool already ran. - Order is fixed. Hooks of an event run one at a time, in file order.
- Hooks see only tools that were going to run.
post_toolhooks do not run for a call that was denied, blocked or refused. - Subagents’ tool calls go through the same hooks.
- A stopped turn ends the hook. Its whole process group is killed.
Every tool result records which hooks ran, their outcome (ok, deny, ask, allow, block, replace or error) and how long each took.
Examples
Refuse force pushes
~/.config/quad/guard.sh (make it executable with chmod +x):
#!/bin/sh
# Refuse git push --force and its short form.
cmd=$(cat | sed -n 's/.*"command": *"\([^"]*\)".*/\1/p')
case "$cmd" in
*"git push"*"--force"*|*"git push"*" -f"*)
echo "Force pushes are not allowed here. Push normally or ask the person." >&2
exit 2 ;;
esac
exit 0
[
{ "id": "no-force-push", "event": "pre_tool", "match": "bash", "command": "~/.config/quad/guard.sh" }
]
For anything beyond a quick check, parse the JSON properly, for example with jq:
#!/bin/sh
cmd=$(jq -r '.args.command // ""')
Always ask before touching production config
[
{
"id": "ask-prod",
"event": "pre_tool",
"match": "write_file|edit_file|apply_patch",
"command": "jq -e '.args.path | test(\"config/prod\")' >/dev/null && echo '{\"decision\":\"ask\"}' || true"
}
]
Format after every edit
[
{ "event": "post_tool", "match": "write_file|edit_file|apply_patch", "command": "cargo fmt --quiet" }
]
Keep secrets out of results
A post_tool hook that replaces anything that looks like a key:
#!/bin/sh
out=$(jq -r '.output')
clean=$(printf '%s' "$out" | sed -E 's/(sk-[A-Za-z0-9]{8})[A-Za-z0-9]+/\1…/g')
if [ "$out" != "$clean" ]; then
jq -n --arg t "$clean" '{replace: $t}'
fi
Limits
Only pre_tool and post_tool exist today. Hooks cannot change a call’s arguments, and there are no hooks for other stages of a turn.
Warning. Hooks are commands run with your account’s permissions. A hook is as trusted as the file that names it.