Sub topic 9
Telling the agent why it was blocked
This chapter assumes you can write a pre-action hook that reads a tool-call event from stdin and exits with code 2 to block it. It also assumes you know how to match a shell command against a deny pattern. Here you will improve what the hook says when it blocks.
Why the message matters
When a hook exits with code 2, the agent usually receives whatever the hook wrote to stderr as the result of the failed tool call. The agent has no other view of your rules. It cannot read your script, so the message is its only evidence of what went wrong.
A vague message gives the agent nothing to act on, and it may do one of these things:
- Retry the same command unchanged.
- Rephrase the command until it slips past your pattern.
- Give up and tell the user the task is impossible.
A specific message lets the agent pick a safe alternative and continue.
What a useful message contains
A good block message answers three questions, in this order:
- What was blocked?
- Why is it blocked?
- What can the agent do instead?
| Weak message | Strong message |
|---|---|
| `Blocked.` | `Blocked: git push --force. Force pushes can overwrite teammates' commits. Use git push --force-with-lease instead.` |
| `Not allowed` | `Blocked: write to .env. This file holds secrets and is protected. Edit .env.example and ask the user to update .env.` |
| `Policy violation 17` | `Blocked: rm -rf on /. Delete only specific paths inside the project directory.` |
The strong messages name the rule in plain words. They also point to an action that the hook itself would allow.
A helper that enforces the format
Put the message format in one function so every rule produces it. This also keeps the exit code in one place.
#!/usr/bin/env python3
import json
import re
import sys
def block(what, why, instead):
message = f"Blocked: {what}. {why} Instead: {instead}"
print(message, file=sys.stderr)
sys.exit(2)
event = json.load(sys.stdin)
command = event.get("tool_input", {}).get("command", "")
if re.search(r"\bgit\s+push\b.*--force(?!-with-lease)", command):
block(
"git push --force",
"Force pushes can overwrite other people's commits.",
"use git push --force-with-lease, or ask the user to confirm.",
)
sys.exit(0)The pattern --force(?!-with-lease) uses a negative lookahead. It matches a bare --force but lets the safer --force-with-lease through, which is the alternative the message recommends. If the message suggested something your own rule then blocked, the agent would loop.
The feedback loop
The diagram shows the exchange when the agent follows the advice.
Keep messages safe and short
Also avoid wording that invites the agent to bypass the rule, such as "this check only looks for the word force". That sentence tells the agent how to evade the check. Describe the intent of the rule, not how it is implemented.
Test the message
Run the hook with a sample event, as in the testing chapter, and read the stderr output as if you were the agent. Ask whether you could act on it with no other context.
echo '{"tool_name":"shell","tool_input":{"command":"git push --force origin main"}}' | python3 guard.py
echo "exit code: $?"Exercise
A hook blocks rm -rf build/../.. and prints only "Denied". Rewrite the message so the agent can recover, and name the three things it should contain.
Show answer
The message should say what was blocked, why, and what to do instead. For example: "Blocked: rm -rf with a path that leaves the project directory. Deleting outside the project is not allowed. Instead: delete a specific folder inside the project, such as rm -rf build." The agent can then retry with a safe command.