Skip to content

The agent that grows with you

fix(plugins): a pre_tool_call block outranks an earlier plugin's approve

teknium102ad41df3a33
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 23dff081afd..529ad7f61fc 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -1810,8 +1810,10 @@ def _get_pre_tool_call_directive_details(
) -> _PreToolCallDirective:
"""Check ``pre_tool_call`` hooks for ``{"action": "block", "message"}`` (veto; message becomes
the tool result) or ``{"action": "approve", "message", "rule_key"?}`` (escalate ANY tool to the
- human-approval gate; ``rule_key`` picks the ``[a]lways`` allowlist grain). First valid directive
- wins; irrelevant returns are ignored."""
+ human-approval gate; ``rule_key`` picks the ``[a]lways`` allowlist grain). Precedence is
+ ``block`` > ``approve`` > none, not registration order: any plugin's valid veto wins over an
+ earlier plugin's request for human confirmation (#87420); among approves the first valid one
+ wins. Irrelevant returns are ignored."""
allowed = getattr(_thread_tool_whitelist, "allowed", None)
if allowed is not None and tool_name not in allowed:
fmt = getattr(_thread_tool_whitelist, "fmt", "Tool '{tool_name}' denied")
@@ -1823,6 +1825,7 @@ def _get_pre_tool_call_directive_details(
api_request_id=api_request_id, middleware_trace=list(middleware_trace or []),
)
modified_args: Optional[Dict[str, Any]] = None
+ first_approve: Optional[Tuple[Optional[str], Optional[str]]] = None # (message, rule_key)
for result in hook_results:
if not isinstance(result, dict):
continue
@@ -1843,9 +1846,15 @@ def _get_pre_tool_call_directive_details(
# A block directive requires a message (it becomes the tool result); approve's is optional.
if action == "block" and not message:
continue
- rule_key = result.get("rule_key") if action == "approve" else None
- rule_key = (rule_key.strip() or None) if isinstance(rule_key, str) else None
- return _PreToolCallDirective(action=action, message=message, rule_key=rule_key, modified_args=modified_args)
+ if action == "block":
+ return _PreToolCallDirective(action="block", message=message, modified_args=modified_args)
+ # approve is held back until the whole list has been scanned for a veto.
+ if first_approve is None:
+ rule_key = result.get("rule_key")
+ first_approve = (message, (rule_key.strip() or None) if isinstance(rule_key, str) else None)
+ if first_approve is not None:
+ return _PreToolCallDirective(action="approve", message=first_approve[0], rule_key=first_approve[1],
+ modified_args=modified_args)
return _PreToolCallDirective(modified_args=modified_args)
diff --git a/tests/hermes_cli/test_plugins.py b/tests/hermes_cli/test_plugins.py
index 5440f40a148..2711512344d 100644
--- a/tests/hermes_cli/test_plugins.py
+++ b/tests/hermes_cli/test_plugins.py
@@ -1808,6 +1808,41 @@ class TestPreToolCallDirective:
)
assert get_pre_tool_call_directive("write_file", {}) == ("approve", None)
+ def test_later_block_outranks_earlier_approve(self, monkeypatch):
+ """Precedence is block > approve, not registration order: a security plugin's veto must
+ not be shadowed by an earlier plugin's approve (#87420). Under approvals.mode off an
+ approve means no prompt at all, so the veto would otherwise be dropped silently."""
+ from hermes_cli.plugins import _get_pre_tool_call_directive_details
+ monkeypatch.setattr(
+ "hermes_cli.plugins.invoke_hook",
+ lambda hook_name, **kwargs: [
+ {"action": "modify", "args": {"path": "/safe"}},
+ {"action": "approve", "message": "earlier plugin approves", "rule_key": "k"},
+ {"action": "block", "message": "later security plugin blocks"},
+ ],
+ )
+ details = _get_pre_tool_call_directive_details("write_file", {"path": "/unsafe"})
+ assert (details.action, details.message, details.rule_key) == (
+ "block", "later security plugin blocks", None)
+ assert details.modified_args == {"path": "/safe"} # modify before the veto stays visible
+
+ def test_first_approve_wins_among_approves_and_keeps_later_modify(self, monkeypatch):
+ """Holding approve back for a veto scan must not change which approve wins (first valid,
+ incl. its rule_key) and must keep accumulating modify directives that follow it."""
+ from hermes_cli.plugins import _get_pre_tool_call_directive_details
+ monkeypatch.setattr(
+ "hermes_cli.plugins.invoke_hook",
+ lambda hook_name, **kwargs: [
+ {"action": "block"}, # message-less block is invalid and ignored
+ {"action": "approve", "message": "first", "rule_key": " write_file:ssh "},
+ {"action": "modify", "args": {"content": "fixed"}},
+ {"action": "approve", "message": "second", "rule_key": "write_file:other"},
+ ],
+ )
+ details = _get_pre_tool_call_directive_details("write_file", {"path": "/p"})
+ assert (details.action, details.message, details.rule_key) == ("approve", "first", "write_file:ssh")
+ assert details.modified_args == {"path": "/p", "content": "fixed"}
+
class TestResolvePreToolBlock:
"""Tests for the single dispatch-site chokepoint that resolves a
diff --git a/website/docs/user-guide/features/hooks.md b/website/docs/user-guide/features/hooks.md
index d29dce466b9..7303ef3f03f 100644
--- a/website/docs/user-guide/features/hooks.md
+++ b/website/docs/user-guide/features/hooks.md
@@ -438,7 +438,7 @@ Payload fields below are the exact event-specific fields supplied by each call s
| Hook | Category | Exact timing and return behavior | Explicit payload fields | Privacy / sensitivity |
|---|---|---|---|---|
-| [`pre_tool_call`](#pre_tool_call) | Directive/control | Once before execution; first valid `block` or `approve` directive wins, and `modify` returns are shallow-merged into the tool arguments. | `tool_name`, `args`, `task_id`, `session_id`, `tool_call_id`, `turn_id`, `api_request_id`, `middleware_trace` | Raw arguments may contain user content, paths, commands, or secrets. |
+| [`pre_tool_call`](#pre_tool_call) | Directive/control | Once before execution; any valid `block` wins over any `approve` (then the first valid `approve`), and `modify` returns are shallow-merged into the tool arguments. | `tool_name`, `args`, `task_id`, `session_id`, `tool_call_id`, `turn_id`, `api_request_id`, `middleware_trace` | Raw arguments may contain user content, paths, commands, or secrets. |
| `post_tool_call` | Observer | After blocked, error, or successful result; return ignored. | `tool_name`, `args`, `result`, `task_id`, `session_id`, `tool_call_id`, `turn_id`, `api_request_id`, `duration_ms`, `status`, `error_type`, `error_message`, `middleware_trace` | Result/error text may contain arbitrary tool or user content and secrets. |
| `transform_tool_result` | Transform | After `post_tool_call`, before conversation append; first string replaces the result. | `tool_name`, `args`, `result`, `task_id`, `session_id`, `tool_call_id`, `turn_id`, `api_request_id`, `duration_ms`, `status`, `error_type`, `error_message` | Exposes the full model-bound result and arguments. |
| `transform_terminal_output` | Transform | After bounded foreground process capture, before final output limiting; first string replaces output. | `command`, `output`, `returncode`, `task_id`, `env_type` | Command/output may contain credentials. |
@@ -554,7 +554,7 @@ return {"action": "block", "message": "Reason the tool call was blocked"}
return {"action": "approve", "message": "Why approval is required", "rule_key": "optional:scope"}
```
-The first valid directive wins (Python plugins registered first, then shell hooks). `block` requires a non-empty `message` and short-circuits the tool with that text as the error returned to the model. `approve` escalates the call to the existing human-approval gate; `message` and `rule_key` are optional, and denial, timeout, or gate error fails closed. Other return values are ignored, so existing observer-only callbacks keep working unchanged.
+Precedence is `block` > `approve` > no directive, regardless of registration order: any plugin's valid `block` wins over an earlier plugin's `approve`, and among `approve` directives the first valid one wins (Python plugins are registered first, then shell hooks). `block` requires a non-empty `message` and short-circuits the tool with that text as the error returned to the model. `approve` escalates the call to the existing human-approval gate; `message` and `rule_key` are optional, and denial, timeout, or gate error fails closed. Other return values are ignored, so existing observer-only callbacks keep working unchanged.
**Return value — rewrite the tool's arguments:**
@@ -1919,7 +1919,7 @@ Shell hooks run with **your full user credentials** — same trust boundary as a
### Ordering and precedence
-Both Python plugin hooks and shell hooks flow through the same `invoke_hook()` dispatcher. Python plugins are registered first (`discover_and_load()`), shell hooks second (`register_from_config()`), so Python `pre_tool_call` block decisions take precedence in tie cases. The first valid block wins — the aggregator returns as soon as any callback produces `{"action": "block", "message": str}` with a non-empty message.
+Both Python plugin hooks and shell hooks flow through the same `invoke_hook()` dispatcher. Python plugins are registered first (`discover_and_load()`), shell hooks second (`register_from_config()`), so Python `pre_tool_call` decisions take precedence in tie cases. The first valid block wins — the aggregator returns as soon as any callback produces `{"action": "block", "message": str}` with a non-empty message — and a block anywhere in the list outranks an `approve` returned earlier.
## Outbound Webhooks