Sub topic 8
Allow, deny, or ask the user
This chapter assumes you can read the hook contract from the previous chapters: the event arrives as JSON on stdin, and the hook answers with an exit code and a message. Here you add a third outcome beyond block and pass.
Three decisions, not two
A pre-action hook can reach three outcomes:
| Decision | Meaning | Typical trigger |
|---|---|---|
| Allow | The action runs with no prompt. | Read-only commands such as `ls` or `git status` |
| Deny | The action never runs. | `rm -rf /`, writes to `.env` |
| Ask | The action waits for a person to approve or reject it. | `git push`, `npm publish`, edits under `infra/` |
Deny is for actions that are almost never right. Ask is for actions that are sometimes right and depend on intent. If you deny everything borderline, people disable the hook. If you allow everything borderline, the hook protects nothing.
What the exit code can express
The basic contract has only two clear signals. In the convention used by Claude Code and Kiro CLI, exit code 0 lets the action proceed and exit code 2 blocks it, with stderr passed back to the agent. Other nonzero codes are usually treated as a hook error that does not block. Check your agent's documentation for the exact behavior.
Exit codes cannot say "ask". Some agents add a structured form: the hook exits 0 and prints JSON on stdout that names the decision. Claude Code, for example, reads a permissionDecision field with the values allow, deny, or ask. If your agent has no such field, the closest option is to deny with a message telling the user how to approve the action another way.
A three-way hook
This script checks deny rules first, then ask rules. It uses the JSON form described above, so it only works on agents that support it. Treat the field names as an example.
#!/usr/bin/env python3
import json
import re
import sys
DENY = [r"\brm\s+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*\s+/(\s|$)"]
ASK = [r"\bgit\s+push\b", r"\bnpm\s+publish\b"]
try:
event = json.load(sys.stdin)
command = event["tool_input"]["command"]
except (json.JSONDecodeError, KeyError, TypeError):
print("Blocked: hook could not read the tool call.", file=sys.stderr)
sys.exit(2)
for pattern in DENY:
if re.search(pattern, command):
print("Blocked: this command can destroy the filesystem.", file=sys.stderr)
sys.exit(2)
for pattern in ASK:
if re.search(pattern, command):
print(json.dumps({
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "ask",
"permissionDecisionReason": "This command publishes changes outside your machine.",
}
}))
sys.exit(0)
sys.exit(0)Deny is checked first so a command matching both lists is blocked, not offered for approval. Unreadable input also ends in a block, which keeps the fail-closed rule from the earlier chapter.
Common mistakes
- Putting ask rules before deny rules. A person can approve something you meant to forbid.
- Asking too often. Frequent prompts train people to approve without reading. Reserve ask for actions with real consequences.
- Vague reasons. A prompt that says "Approve?" gives the user nothing to judge. Name the action and its effect.
- Assuming the prompt works in unattended runs. In a non-interactive session nobody can answer. Decide whether ask should fall back to deny there.
Exercise
Your hook must stop git push --force entirely, but only confirm with the user before a normal git push. In what order should you check the two rules, and which decision does each get?
Show answer
Check the force-push rule first and give it deny (exit code 2 with an explanation). Then check the plain git push rule and give it ask. Deny must come first, because otherwise a force push would match the ask rule and a person could approve it.