Skip to content

The agent that grows with you

fix(approval): protected-instruction gate hangs in `hermes chat -q` sessions

Daniel Green2b525ced8429
diff --git a/tests/tools/test_protected_instruction_cli_channel.py b/tests/tools/test_protected_instruction_cli_channel.py
new file mode 100644
index 00000000000..172f8f35770
--- /dev/null
+++ b/tests/tools/test_protected_instruction_cli_channel.py
@@ -0,0 +1,164 @@
+"""Protected-instruction approval must be answerable on an interactive CLI
+session, and must NOT pretend a human channel exists in a headless one-shot
+(``hermes chat -q``) session.
+
+Defect (kanban t_9691f08d): every write to a protected agent-instruction file
+from a kanban worker failed with
+
+ BLOCKED: ... approval prompt timed out without a user response.
+ Silence is not consent.
+
+which reads as "a human ignored the prompt". In fact those workers are spawned
+as ``hermes chat -q``, which never builds a prompt_toolkit Application — but
+``HermesCLI.__init__`` had already registered ``_approval_callback`` as the
+thread-local CLI approval callback. ``_request_protected_instruction_approval``
+therefore saw a callback, believed a human channel existed, pushed a modal into
+a layout that is never rendered, and blocked on a response queue no key binding
+could ever fill until the approval timeout expired.
+
+``_run_approval_gate`` learned about this context in #86878 via
+``HERMES_SINGLE_QUERY_SESSION``; the protected-instruction gate deliberately
+bypasses that gate (one-operation approval every time, no allowlist, no yolo)
+and so never got the fix. These tests lock three invariants:
+
+1. Interactive CLI: a registered approval callback that answers ``once`` lets
+ the write through, and ``deny`` blocks it. The channel works.
+2. Headless one-shot: even with a callback registered, the gate fails closed
+ *immediately* with the honest "no interactive user or gateway is present"
+ message instead of manufacturing a timeout.
+3. Single-query never auto-approves this gate, whatever
+ ``approvals.single_query_mode`` says — that config governs
+ ``_run_approval_gate``, not this one.
+
+The real CLI modal round trip is covered by tests/hermes_cli/test_cli_approval_ui.py.
+"""
+
+import json
+import time
+
+import pytest
+
+
[email protected](autouse=True)
+def _gate_on(monkeypatch):
+ import tools.file_tools_write_guards as ft
+ monkeypatch.setattr(ft, "_protected_instruction_config", lambda: (True, []))
+ yield
+
+
[email protected](autouse=True)
+def _clean_callbacks(monkeypatch):
+ from tools.terminal_tool import set_approval_callback
+ # Default every test to a NON single-query context; the headless tests
+ # opt in explicitly.
+ monkeypatch.delenv("HERMES_SINGLE_QUERY_SESSION", raising=False)
+ set_approval_callback(None)
+ yield
+ set_approval_callback(None)
+
+
+def _write(path, content="content"):
+ from tools.file_tools import write_file_tool
+ return json.loads(write_file_tool(str(path), content))
+
+
+class TestInteractiveCliChannelDelivers:
+ """An interactive CLI session can actually answer the prompt."""
+
+ def test_interactive_approval_allows_write(self, tmp_path):
+ from tools.terminal_tool import set_approval_callback
+
+ seen = []
+
+ def cb(command, description, **kwargs):
+ seen.append((command, description, kwargs))
+ return "once"
+
+ set_approval_callback(cb)
+ target = tmp_path / "SOUL.md"
+ res = _write(target, "approved by a live human")
+
+ assert not res.get("error"), res
+ assert target.read_text(encoding="utf-8") == "approved by a live human"
+ assert len(seen) == 1, "the human channel was never used"
+ assert "SOUL.md" in seen[0][0]
+ # One-operation only: no session/permanent scope is offered.
+ assert seen[0][2].get("allow_permanent") is False
+
+ def test_interactive_denial_blocks_write(self, tmp_path):
+ from tools.terminal_tool import set_approval_callback
+ set_approval_callback(lambda c, d, **k: "deny")
+
+ target = tmp_path / "SOUL.md"
+ res = _write(target)
+ assert res.get("error") and "BLOCKED" in res["error"]
+ assert "denied by the user" in res["error"]
+ assert not target.exists()
+
+
+class TestHeadlessOneShotFailsClosedHonestly:
+ """`hermes chat -q` must not claim a human channel it does not have."""
+
+ def test_no_callback_reports_no_human_not_timeout(self, tmp_path):
+ target = tmp_path / "SOUL.md"
+ res = _write(target)
+ assert res.get("error") and "BLOCKED" in res["error"]
+ assert "no interactive user or gateway is present" in res["error"]
+ assert "timed out" not in res["error"], (
+ "headless run reported a timeout, implying a human ignored a "
+ "prompt that was never rendered"
+ )
+ assert not target.exists()
+
+ def test_single_query_ignores_the_unreachable_modal_callback(
+ self, tmp_path, monkeypatch):
+ """The reported defect, verbatim.
+
+ A callback IS registered (HermesCLI.__init__ always registers one),
+ but there is no Application to render it. The gate must not call it
+ and must not report a timeout.
+ """
+ from tools.terminal_tool import set_approval_callback
+
+ called = []
+
+ def never_answers(command, description, **kwargs):
+ called.append(command)
+ time.sleep(30) # what the unrendered modal effectively did
+ return "timeout"
+
+ set_approval_callback(never_answers)
+ monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
+
+ target = tmp_path / "SOUL.md"
+ started = time.time()
+ res = _write(target)
+ elapsed = time.time() - started
+
+ assert not called, (
+ "single-query session dispatched to a modal nothing can render")
+ assert elapsed < 5, f"gate blocked for {elapsed:.1f}s with no human"
+ assert res.get("error") and "BLOCKED" in res["error"]
+ assert "no interactive user or gateway is present" in res["error"]
+ assert "timed out" not in res["error"]
+ assert not target.exists()
+
+ @pytest.mark.parametrize("mode", ["approve", "deny"])
+ def test_single_query_mode_never_auto_approves_this_gate(
+ self, tmp_path, monkeypatch, mode):
+ """``approvals.single_query_mode`` governs _run_approval_gate only.
+
+ Protected-instruction writes always need a live human, so even
+ ``single_query_mode: approve`` must not let one through.
+ """
+ from tools import approval_context
+ from tools.terminal_tool import set_approval_callback
+
+ monkeypatch.setenv("HERMES_SINGLE_QUERY_SESSION", "1")
+ monkeypatch.setattr(approval_context, "_get_single_query_approval_mode", lambda: mode)
+ set_approval_callback(lambda c, d, **k: "once")
+
+ target = tmp_path / "SOUL.md"
+ res = _write(target)
+ assert res.get("error") and "BLOCKED" in res["error"]
+ assert not target.exists()
diff --git a/tools/file_tools_write_guards.py b/tools/file_tools_write_guards.py
index 36f4569d3cc..43d2cd191f5 100644
--- a/tools/file_tools_write_guards.py
+++ b/tools/file_tools_write_guards.py
@@ -319,6 +319,14 @@ def _request_protected_instruction_approval(reasons: list[str], task_id: str = "
# No human channel (script, cron, background thread): fail closed —
# auto-approving here would recreate the persistence vector.
return blocked.format(why=_NO_HUMAN)
+ # ...but a one-shot ``hermes chat -q`` session — which is exactly how the kanban dispatcher spawns
+ # every worker — registers that callback in ``HermesCLI.__init__`` and never builds a prompt_toolkit
+ # Application, so the modal is pushed into a layout nothing renders and the gate burned the whole
+ # approvals timeout, then reported "timed out" for a prompt no human saw. Detect that context the same
+ # way ``_run_approval_gate`` does (#86878) and fail closed immediately with the honest reason. Single-query
+ # runs are NEVER auto-approved here, whatever ``approvals.single_query_mode`` says.
+ if _approval._is_single_query_approval_context():
+ return blocked.format(why=_NO_HUMAN)
# Same observer payload as the gateway branch (#131876), fired like the
# dangerous-command CLI prompt in tools/approval.py.
hook_kwargs = dict(command=display, description=description, pattern_key="protected_instruction_file",