Skip to content

The agent that grows with you

refactor(hooks): share fail-closed approve logic; update sibling tests

Teknium20fcc11a0c92
diff --git a/hermes_cli/plugins.py b/hermes_cli/plugins.py
index 89da6db5417..9ef8e47d069 100644
--- a/hermes_cli/plugins.py
+++ b/hermes_cli/plugins.py
@@ -6141,6 +6141,29 @@ def resolve_pre_tool_block(
tool_call_id=tool_call_id, turn_id=turn_id,
api_request_id=api_request_id, middleware_trace=middleware_trace,
)
+ return _resolve_block_from_details(
+ details, tool_name,
+ turn_id=turn_id, tool_call_id=tool_call_id, session_id=session_id,
+ )
+
+
+def _resolve_block_from_details(
+ details: "_PreToolCallDirective",
+ tool_name: str,
+ *,
+ turn_id: str = "",
+ tool_call_id: str = "",
+ session_id: str = "",
+) -> Optional[str]:
+ """Resolve a fetched directive to a final block message (or ``None``).
+
+ Shared by :func:`resolve_pre_tool_block` and
+ :func:`_dispatch_pre_tool_call_hooks` so the security-critical
+ fail-closed approval logic lives in exactly ONE place: ``block``
+ blocks with its message; an ``approve`` directive whose gate errors,
+ denies, or times out is fail-closed to a block; anything else
+ proceeds.
+ """
if details.action == "block":
return details.message
if details.action == "approve":
@@ -6198,9 +6221,11 @@ def _dispatch_pre_tool_call_hooks(
Returns a ``(block_message, modified_args)`` tuple:
- ``block_message`` — the first block/approve directive's resolved message
- (or ``None`` when the call may proceed). Uses the same approval-gate
- logic as :func:`resolve_pre_tool_block`.
- - ``modified_args`` — merged args from the first ``modify`` directive
+ (or ``None`` when the call may proceed). Shares the exact fail-closed
+ approval-gate logic of :func:`resolve_pre_tool_block` via
+ :func:`_resolve_block_from_details`, including the observability
+ context set around the human-approval gate.
+ - ``modified_args`` — merged args from ``modify`` directives
(or ``None`` when no hook requested modification).
This is the single invocation point for ``pre_tool_call`` hooks.
@@ -6215,25 +6240,10 @@ def _dispatch_pre_tool_call_hooks(
tool_call_id=tool_call_id, turn_id=turn_id,
api_request_id=api_request_id, middleware_trace=middleware_trace,
)
- block_msg: Optional[str] = None
- if details.action == "block":
- block_msg = details.message
- elif details.action == "approve":
- try:
- from tools.approval import request_tool_approval
- result = request_tool_approval(
- tool_name,
- details.message or "",
- rule_key=details.rule_key or tool_name,
- )
- except Exception:
- block_msg = f"BLOCKED: plugin approval gate failed for {tool_name}"
- else:
- if not result.get("approved"):
- block_msg = str(
- result.get("message")
- or f"BLOCKED: plugin approval required for {tool_name}"
- )
+ block_msg = _resolve_block_from_details(
+ details, tool_name,
+ turn_id=turn_id, tool_call_id=tool_call_id, session_id=session_id,
+ )
return (block_msg, details.modified_args)
diff --git a/tests/run_agent/test_run_agent.py b/tests/run_agent/test_run_agent.py
index b5caf832333..0fafb0845a9 100644
--- a/tests/run_agent/test_run_agent.py
+++ b/tests/run_agent/test_run_agent.py
@@ -1994,8 +1994,8 @@ class TestConcurrentToolExecution:
lambda _name, args, callback, **_kwargs: callback(args),
)
monkeypatch.setattr(
- "hermes_cli.plugins.resolve_pre_tool_block",
- lambda *_args, **_kwargs: None,
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ lambda *_args, **_kwargs: (None, None),
)
monkeypatch.setattr(
"agent.tool_executor._begin_tool_execution",
@@ -2056,8 +2056,8 @@ class TestConcurrentToolExecution:
messages = []
monkeypatch.setattr(
- "hermes_cli.plugins.resolve_pre_tool_block",
- lambda *args, **kwargs: "Blocked by policy",
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ lambda *args, **kwargs: ("Blocked by policy", None),
)
agent._checkpoint_mgr.enabled = True
agent._checkpoint_mgr.ensure_checkpoint = MagicMock(
@@ -2146,8 +2146,8 @@ class TestConcurrentToolExecution:
"""Blocked memory tool should not reset the nudge counter."""
agent._turns_since_memory = 5
monkeypatch.setattr(
- "hermes_cli.plugins.resolve_pre_tool_block",
- lambda *args, **kwargs: "Blocked",
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ lambda *args, **kwargs: ("Blocked", None),
)
with patch("tools.memory_tool.memory_tool", side_effect=AssertionError("should not run")):
result = agent._invoke_tool(
@@ -2180,8 +2180,8 @@ class TestConcurrentToolExecution:
lambda _name, args, callback, **_kwargs: callback(args),
)
monkeypatch.setattr(
- "hermes_cli.plugins.resolve_pre_tool_block",
- lambda *_args, **_kwargs: None,
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ lambda *_args, **_kwargs: (None, None),
)
monkeypatch.setattr(tool_executor, "_begin_tool_execution", lambda *_a, **_k: None)
@@ -2235,8 +2235,8 @@ class TestConcurrentToolExecution:
lambda _name, args, callback, **_kwargs: callback(args),
)
monkeypatch.setattr(
- "hermes_cli.plugins.resolve_pre_tool_block",
- lambda *_args, **_kwargs: None,
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ lambda *_args, **_kwargs: (None, None),
)
monkeypatch.setattr(tool_executor, "_begin_tool_execution", lambda *_a, **_k: None)
@@ -2301,8 +2301,8 @@ class TestAgentRuntimePostHookOwnershipSync:
hook_calls = []
monkeypatch.setattr(
- "hermes_cli.plugins.resolve_pre_tool_block",
- lambda *args, **kwargs: None,
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ lambda *args, **kwargs: (None, None),
)
monkeypatch.setattr(
"hermes_cli.lifecycle.invoke_hook",
diff --git a/tests/run_agent/test_tool_call_guardrail_runtime.py b/tests/run_agent/test_tool_call_guardrail_runtime.py
index 51d3ea7594b..ca6e80aac4c 100644
--- a/tests/run_agent/test_tool_call_guardrail_runtime.py
+++ b/tests/run_agent/test_tool_call_guardrail_runtime.py
@@ -249,7 +249,7 @@ def test_relay_rewrite_precedes_sequential_policy_approval_checkpoint_and_dispat
def observe_plugin(name, args, **kwargs):
del kwargs
observed["plugin"].append((name, dict(args)))
- return None
+ return (None, None)
def observe_approval(name, args):
observed["approval"].append((name, dict(args)))
@@ -274,7 +274,7 @@ def test_relay_rewrite_precedes_sequential_policy_approval_checkpoint_and_dispat
with (
patch("agent.relay_tools.execute", side_effect=relay_execute),
patch(
- "hermes_cli.plugins.resolve_pre_tool_block",
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
side_effect=observe_plugin,
),
patch.object(agent._tool_guardrails, "before_call", side_effect=observe_guardrail),
@@ -331,7 +331,10 @@ def test_plugin_pre_tool_block_wins_without_counting_as_toolguard_block():
messages = []
with (
- patch("hermes_cli.plugins.resolve_pre_tool_block", return_value="plugin policy"),
+ patch(
+ "hermes_cli.plugins._dispatch_pre_tool_call_hooks",
+ return_value=("plugin policy", None),
+ ),
patch("run_agent.handle_function_call", return_value="SHOULD_NOT_RUN") as mock_hfc,
):
agent._execute_tool_calls_sequential(msg, messages, "task-1")