The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(approvals): resolve an unattended turn from unattended_mode when no notifier is registered
liuhao10248918717b1ad2
diff --git a/tests/tools/test_approval.py b/tests/tools/test_approval.pyindex ed6dff4b671..3500ede9394 100644--- a/tests/tools/test_approval.py+++ b/tests/tools/test_approval.py@@ -1013,6 +1013,94 @@ class TestWebhookApprovalExclusion:assert "approvals.unattended_mode" in result["message"]+class TestUnattendedPlatformExecAskLeak:+ """A gateway process sets HERMES_EXEC_ASK=1 for EVERY session it serves, so an+ api_server/webhook turn reads as "ask" in ``_presence()`` even though the platform was+ already excluded from gateway approval contexts. Without a registered notify callback+ (non-streaming /v1/chat/completions, webhook, msgraph_webhook) the ask branch parks the+ approval as ``pending_approval`` that no client on that endpoint can ever answer, and+ ``approvals.unattended_mode`` never runs — the unanswerable stall of #100532.++ Fix: ``_presence()`` drops ``is_ask`` for unattended-platform sessions without a notify+ callback, so the unattended branch resolves instantly from ``approvals.unattended_mode``.+ ``/v1/runs`` and streaming chat completions register a callback and keep the ask path.+ """++ def _isolate(self, monkeypatch, *, platform="api_server"):+ import tools.approval as approval_mod+ from tools import approval_context++ monkeypatch.setattr(approval_mod, "_YOLO_MODE_FROZEN", False)+ monkeypatch.setattr(approval_context, "_get_approval_mode", lambda: "smart")+ monkeypatch.delenv("HERMES_CRON_SESSION", raising=False)+ monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False)+ monkeypatch.delenv("HERMES_INTERACTIVE", raising=False)+ monkeypatch.setenv("HERMES_EXEC_ASK", "1")+ monkeypatch.setenv("HERMES_SESSION_PLATFORM", platform)+ monkeypatch.setenv("HERMES_SESSION_KEY", "test-unattended-ask-leak")+ approval_mod._session_approved.clear()+ approval_mod._pending.clear()+ approval_mod._denial_tally.clear()+ approval_mod._gateway_notify_cbs.pop("test-unattended-ask-leak", None)++ def test_escalated_smart_gate_denies_not_pending(self, monkeypatch):+ """The #100532 chain: guardian ESCALATE on a notifier-less api_server session.++ Deny-by-default (approvals.unattended_mode) resolves instantly — never a+ ``pending_approval`` nobody on that endpoint can answer — and never pays the+ guardian LLM call the escalation came from.+ """+ import tools.approval as approval_mod+ from tools.approval import check_all_command_guards++ self._isolate(monkeypatch)+ monkeypatch.setattr(approval_mod, "_smart_verdict",+ lambda *a, **k: pytest.fail("guardian ran after unattended resolution"))++ result = check_all_command_guards("pkill -9 -f soffice", "local")++ assert result["approved"] is False+ assert result.get("status") != "pending_approval"+ assert result.get("approval_pending") is not True+ assert "unattended platform" in result["message"]+ assert "approvals.unattended_mode" in result["message"]++ def test_unattended_mode_approve_is_honored(self, monkeypatch):+ """approvals.unattended_mode: approve auto-approves instead of stalling (#100532)."""+ from tools import approval_context+ from tools.approval import check_all_command_guards++ self._isolate(monkeypatch)+ monkeypatch.setattr(+ approval_context, "_get_unattended_approval_mode", lambda: "approve"+ )++ result = check_all_command_guards("pkill -9 -f soffice", "local")+ assert result["approved"] is True++ def test_registered_notifier_keeps_the_ask_path(self, monkeypatch):+ """/v1/runs and streaming chat register a notify callback: is_ask survives, so the+ approval bridge answers the card instead of the unattended mode short-circuit."""+ import tools.approval as approval_mod++ self._isolate(monkeypatch)+ approval_mod.register_gateway_notify("test-unattended-ask-leak", lambda data: None)+ try:+ _, _is_cli, _is_gateway, is_ask = approval_mod._presence()+ assert is_ask is True+ finally:+ approval_mod.unregister_gateway_notify("test-unattended-ask-leak")++ def test_chat_gateway_platform_keeps_ask_without_notifier(self, monkeypatch):+ """Messaging platforms (TurnRunner registers their notifier at turn time) are not+ unattended: is_ask survives without a callback too."""+ import tools.approval as approval_mod++ self._isolate(monkeypatch, platform="telegram")+ _, _is_cli, _is_gateway, is_ask = approval_mod._presence()+ assert is_ask is True++class TestNormalizationBypass:"""Obfuscation techniques must not bypass dangerous command detection."""diff --git a/tests/tools/test_cron_not_interactive.py b/tests/tools/test_cron_not_interactive.pyindex 0228c4ea480..317edb7cf6f 100644--- a/tests/tools/test_cron_not_interactive.py+++ b/tests/tools/test_cron_not_interactive.py@@ -3,8 +3,10 @@A gateway sets HERMES_EXEC_ASK=1 at startup and hands its environ to every external cronworker; interactive launches export HERMES_INTERACTIVE=1. Inside cron nobody can answer thecard, so ``_presence()`` must clear the trio and let the gate resolve from-``approvals.cron_mode``. Unattended platforms are NOT cleared: api_server answers via the-``/v1/runs`` approval bridge, which needs ``is_ask`` intact.+``approvals.cron_mode``. Unattended platforms keep ``is_ask`` only while their session has a+registered approval notifier: api_server answers via the ``/v1/runs`` approval bridge, which+registers one; without a notifier (non-streaming /v1/chat/completions) the card can never be+answered, so the gate resolves from ``approvals.unattended_mode`` instead (#100532)."""import pytest@@ -35,7 +37,15 @@ def test_interactive_session_keeps_presence(monkeypatch, leaked_presence):def test_api_server_platform_keeps_exec_ask_for_runs_approval_bridge(monkeypatch, leaked_presence):"""api_server resolves approvals via ``approval.request`` → ``POST /v1/runs/{id}/approval``;- clearing ``is_ask`` there would turn every dangerous command into an instant BLOCK."""+ that bridge registers a notify callback for the session, and only then does ``is_ask``+ survive — clearing it with a live bridge would turn every bridged dangerous command into+ an instant BLOCK. Without a notifier the gate falls to ``approvals.unattended_mode``+ (#100532; covered in test_approval.py)."""monkeypatch.setenv("HERMES_SESSION_PLATFORM", "api_server")- _, _, _, is_ask = approval_mod._presence()- assert is_ask is True+ monkeypatch.setenv("HERMES_SESSION_KEY", "test-runs-bridge-presence")+ approval_mod.register_gateway_notify("test-runs-bridge-presence", lambda data: None)+ try:+ _, _, _, is_ask = approval_mod._presence()+ assert is_ask is True+ finally:+ approval_mod.unregister_gateway_notify("test-runs-bridge-presence")diff --git a/tools/approval.py b/tools/approval.pyindex 6d4402db744..8df609aa3ac 100644--- a/tools/approval.py+++ b/tools/approval.py@@ -914,13 +914,24 @@ def _presence(approval_callback=None) -> tuple:HERMES_INTERACTIVE=1 for sudo prompts, and a gateway sets HERMES_EXEC_ASK=1 at startup andpasses its environ to every external cron worker (#110932) — in neither can a human answerthe card, so the gate must resolve from ``approvals.<ctx>_mode`` instead of parking on a- pending approval. Unattended *platforms* keep ``is_ask``: api_server relies on it for the- ``/v1/runs`` approval bridge (``approval.request`` → ``POST /v1/runs/{id}/approval``)."""+ pending approval. Unattended *platforms* keep ``is_ask`` only while the session has a+ registered approval notifier: ``/v1/runs`` and streaming chat completions bridge one+ (``approval.request`` → ``POST /v1/runs/{id}/approval``). Without a notifier no client on+ the platform can ever answer, so ``is_ask`` drops too and the unattended branch resolves+ from ``approvals.unattended_mode`` instead of parking an unanswerable card (#100532)."""approval_callback = _resolve_cli_approval_callback(approval_callback)is_cli, is_gateway = _is_interactive_cli(), _is_gateway_approval_context()is_ask = env_var_enabled("HERMES_EXEC_ASK")if _is_single_query_approval_context() or _is_cron_approval_context():is_cli = is_gateway = is_ask = False+ elif is_ask and _is_unattended_platform_approval_context() \+ and _gateway_notify_cb(get_current_session_key()) is None:+ # start_gateway() sets HERMES_EXEC_ASK process-wide, so an api_server/webhook turn+ # inside that gateway still reads as "ask" here even though _is_gateway_approval_context()+ # already excluded the platform. /v1/runs and streaming chat register their notify+ # callback before the turn runs; a session with none has no surface that can resolve an+ # approval — resolve from approvals.unattended_mode, never a pending no one can answer.+ is_ask = Falsereturn approval_callback, is_cli, is_gateway, is_ask