feat(hooks): pre_tool_use veto — block tool calls from a hook#243
Merged
emal-avala merged 1 commit intomainfrom Apr 24, 2026
Merged
feat(hooks): pre_tool_use veto — block tool calls from a hook#243emal-avala merged 1 commit intomainfrom
emal-avala merged 1 commit intomainfrom
Conversation
Promotes PreToolUse from a notify-only signal to a gate. When a
pre_tool_use hook exits non-zero, the tool call is vetoed — the
tool does not execute, and the model receives a synthetic error
ToolResult carrying the hook's stderr (or stdout, or a generic
message) as the reason.
Rationale: operators have needed to plug in policy guards (content
scanning, destructive-command blocks, compliance checks) without
extending the permission rule grammar. The pre_tool_use hook
surface was almost sufficient — it fired — but the return was
ignored so hooks couldn't actually stop anything.
Example:
[[hooks]]
event = "pre_tool_use"
tool_name = "Bash"
action = { type = "shell", command = """
if echo "$TOOL_INPUT" | grep -q 'rm -rf /'; then
echo "refusing destructive root delete" >&2
exit 1
fi
""" }
Implementation:
- `HookResult` gains a dedicated `stderr` field so hook authors can
signal block reasons on stderr (shell convention) and have them
surfaced verbatim without mixing with stdout. Existing callers
continue to work; the added field defaults.
- query::run_turn_with_sink collects per-call hook results and
builds a `vetoed_ids` set. Vetoed calls skip execution and
produce a synthetic error result.
- Vetoes are also recorded on the existing DenialTracker — so the
`permission_denied` hook, `/permissions` UI, and denial reports
see the block uniformly with rule-based and user denials.
- Reason priority: stderr (trimmed) > stdout (trimmed) > generic
"blocked by pre-tool-use hook". First line only, so audit
records stay scannable.
Tests (3 new dispatcher tests for the veto preconditions):
- Hook exiting non-zero sets `success=false`
- Stderr and stdout are captured into distinct fields, no cross-mix
- Exit-zero hook with stderr output still counts as success (not
a veto — success is tied to exit status only)
All 13 hook dispatcher tests pass. Clippy clean.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Promotes
pre_tool_usefrom a notify-only signal to a gate. When a hook exits non-zero, the tool call is vetoed — the tool does not execute and the model receives a synthetic error ToolResult carrying the hook's stderr (or stdout, or a generic message) as the reason.Problem
The
pre_tool_useevent fired, but its return value was ignored. Operators who wanted a policy guard (content scanning, destructive-command blocks, compliance checks) had to stretch the permission rule grammar to match arbitrary predicates — or wait for one. A hook that can both inspect the tool input and veto the call closes that gap cleanly.Example
When this fires and exits 1, the Bash call is blocked and the model sees:
Implementation
HookResultgains astderrfield (default empty). Shell hooks populate it from subprocess stderr; HTTP hooks leave it empty. Existing call sites continue to work — the struct isDefault + Cloneand no external crate constructsHookResultdirectly.query::run_turn_with_sink: per-call hook results are collected into avetoed_idsset. Vetoed calls skipexecute_tool_callsand produce a syntheticToolResult::error.DenialTracker, so thepermission_deniedhook,/permissionsUI, and denial reports see the block uniformly with rule-based and user denials.Backwards compatibility
pre_tool_usewas documented as "can block/modify" in the schema rustdoc — this PR makes the "can block" half real; the "modify" half is not claimed here.Tests (3 new)
success=falseAll 13
hooks::dispatcher tests pass. Clippy clean under-D warnings.Test plan
cargo test -p agent-code-lib --lib hooks::cargo clippy --workspace --tests --no-deps -- -D warningscargo fmt --all --check