mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-14 14:12:44 +00:00
feat(plugins): pass approve rule keys to approval gate
This commit is contained in:
parent
f304f41266
commit
36308f0667
9 changed files with 762 additions and 103 deletions
|
|
@ -859,6 +859,171 @@ class TestPreToolCallBlocking:
|
|||
assert get_pre_tool_call_block_message("terminal", {}) == "first blocker"
|
||||
|
||||
|
||||
class TestPreToolCallDirective:
|
||||
"""Tests for the extended (block | approve) directive helper."""
|
||||
|
||||
def test_approve_directive_returned(self, monkeypatch):
|
||||
from hermes_cli.plugins import get_pre_tool_call_directive
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [
|
||||
{"action": "approve", "message": "needs human ok"}
|
||||
],
|
||||
)
|
||||
assert get_pre_tool_call_directive("write_file", {}) == (
|
||||
"approve", "needs human ok")
|
||||
|
||||
def test_approve_without_message_is_valid(self, monkeypatch):
|
||||
"""approve may omit a message (block may not)."""
|
||||
from hermes_cli.plugins import get_pre_tool_call_directive
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [{"action": "approve"}],
|
||||
)
|
||||
assert get_pre_tool_call_directive("write_file", {}) == ("approve", None)
|
||||
|
||||
def test_block_still_requires_message(self, monkeypatch):
|
||||
from hermes_cli.plugins import get_pre_tool_call_directive
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [{"action": "block"}],
|
||||
)
|
||||
assert get_pre_tool_call_directive("terminal", {}) == (None, None)
|
||||
|
||||
def test_first_directive_wins_across_actions(self, monkeypatch):
|
||||
from hermes_cli.plugins import get_pre_tool_call_directive
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [
|
||||
{"action": "approve", "message": "gate first"},
|
||||
{"action": "block", "message": "block second"},
|
||||
],
|
||||
)
|
||||
assert get_pre_tool_call_directive("terminal", {}) == (
|
||||
"approve", "gate first")
|
||||
|
||||
def test_shim_ignores_approve(self, monkeypatch):
|
||||
"""Back-compat shim only reports block, never approve."""
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [
|
||||
{"action": "approve", "message": "gate"}
|
||||
],
|
||||
)
|
||||
assert get_pre_tool_call_block_message("write_file", {}) is None
|
||||
|
||||
|
||||
class TestResolvePreToolBlock:
|
||||
"""Tests for the single dispatch-site chokepoint that resolves a
|
||||
directive (incl. the approve→gate escalation) to a block message."""
|
||||
|
||||
def test_block_returns_message(self, monkeypatch):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [{"action": "block", "message": "no"}],
|
||||
)
|
||||
assert resolve_pre_tool_block("terminal", {}) == "no"
|
||||
|
||||
def test_no_directive_returns_none(self, monkeypatch):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook", lambda hook_name, **kwargs: [])
|
||||
assert resolve_pre_tool_block("terminal", {}) is None
|
||||
|
||||
def test_approve_denied_blocks(self, monkeypatch):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [{"action": "approve", "message": "why"}],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"tools.approval.request_tool_approval",
|
||||
lambda *a, **k: {"approved": False, "message": "user denied it"},
|
||||
)
|
||||
assert resolve_pre_tool_block("write_file", {}) == "user denied it"
|
||||
|
||||
def test_approve_granted_allows(self, monkeypatch):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [{"action": "approve", "message": "why"}],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"tools.approval.request_tool_approval",
|
||||
lambda *a, **k: {"approved": True, "message": None},
|
||||
)
|
||||
assert resolve_pre_tool_block("write_file", {}) is None
|
||||
|
||||
def test_approve_passes_plugin_rule_key_to_gate(self, monkeypatch):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
|
||||
seen = {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [
|
||||
{
|
||||
"action": "approve",
|
||||
"message": "why",
|
||||
"rule_key": "write_file:ssh",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
def _approve(tool_name, reason, **kwargs):
|
||||
seen["tool_name"] = tool_name
|
||||
seen["reason"] = reason
|
||||
seen["rule_key"] = kwargs.get("rule_key")
|
||||
return {"approved": True, "message": None}
|
||||
|
||||
monkeypatch.setattr("tools.approval.request_tool_approval", _approve)
|
||||
|
||||
assert resolve_pre_tool_block("write_file", {}) is None
|
||||
assert seen == {
|
||||
"tool_name": "write_file",
|
||||
"reason": "why",
|
||||
"rule_key": "write_file:ssh",
|
||||
}
|
||||
|
||||
@pytest.mark.parametrize("rule_key", [None, "", " ", 123, object()])
|
||||
def test_approve_falls_back_to_tool_name_without_valid_rule_key(
|
||||
self, monkeypatch, rule_key
|
||||
):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
|
||||
seen = {}
|
||||
directive = {"action": "approve", "message": "why"}
|
||||
if rule_key is not None:
|
||||
directive["rule_key"] = rule_key
|
||||
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [directive],
|
||||
)
|
||||
|
||||
def _approve(tool_name, reason, **kwargs):
|
||||
seen["rule_key"] = kwargs.get("rule_key")
|
||||
return {"approved": True, "message": None}
|
||||
|
||||
monkeypatch.setattr("tools.approval.request_tool_approval", _approve)
|
||||
|
||||
assert resolve_pre_tool_block("write_file", {}) is None
|
||||
assert seen["rule_key"] == "write_file"
|
||||
|
||||
def test_approve_gate_exception_fails_closed(self, monkeypatch):
|
||||
from hermes_cli.plugins import resolve_pre_tool_block
|
||||
monkeypatch.setattr(
|
||||
"hermes_cli.plugins.invoke_hook",
|
||||
lambda hook_name, **kwargs: [{"action": "approve", "message": "why"}],
|
||||
)
|
||||
def _boom(*a, **k):
|
||||
raise RuntimeError("gate crashed")
|
||||
monkeypatch.setattr("tools.approval.request_tool_approval", _boom)
|
||||
msg = resolve_pre_tool_block("terminal", {})
|
||||
assert msg is not None and "gate failed" in msg # fail-closed
|
||||
|
||||
|
||||
class TestGetPreVerifyContinueMessage:
|
||||
"""`pre_verify` directive aggregation — mirrors the pre_tool_call block path."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue