Skip to content

The agent that grows with you

test(approvals): fold the unattended-presence cases into two invariants; document the api_server bridge exception

Tekniume760a20d8920
diff --git a/tests/tools/test_approval.py b/tests/tools/test_approval.py
index 1b2f395c74a..ed6dff4b671 100644
--- a/tests/tools/test_approval.py
+++ b/tests/tools/test_approval.py
@@ -1013,139 +1013,6 @@ 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 surface that can answer the
- card — non-streaming /v1/chat/completions registers no notify callback, and
- webhook/msgraph_webhook register one on every turn yet render no approval surface and
- have no reader — the ask branch blocks for the full approval timeout with no human who
- can resolve it, and ``approvals.unattended_mode`` never runs (#100532).
-
- Fix: ``_presence()`` drops ``is_ask`` for unattended-platform sessions that no client can
- answer — no notify callback on api_server, or a notifier-blind platform (webhook,
- msgraph_webhook) where the TurnRunner lane registers one unconditionally — 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
-
- def test_webhook_registered_notifier_still_resolves_unattended(self, monkeypatch):
- """webhook/msgraph_webhook residual of #100532: the generic TurnRunner lane registers a
- notify callback for *every* inbound turn (``_run_conversation_with_approval`` registers
- unconditionally), yet the WebhookAdapter renders no ``send_exec_approval``/``/approve``
- surface and the inbound lane is ``POST -> 202`` with no reader — so a registered
- notifier there does not mean anyone can answer. The ask leak must resolve from
- ``approvals.unattended_mode`` instead of blocking the full approval timeout."""
- import tools.approval as approval_mod
- from tools.approval import check_all_command_guards
-
- self._isolate(monkeypatch, platform="webhook")
- monkeypatch.setattr(approval_mod, "_smart_verdict",
- lambda *a, **k: pytest.fail("guardian ran after unattended resolution"))
-
- def _no_human(data):
- pytest.fail("approval card delivered to a platform with no approval transport")
-
- approval_mod.register_gateway_notify("test-unattended-ask-leak", _no_human)
- try:
- result = check_all_command_guards("pkill -9 -f soffice", "local")
- finally:
- approval_mod.unregister_gateway_notify("test-unattended-ask-leak")
-
- 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_msgraph_webhook_registered_notifier_drops_ask(self, monkeypatch):
- """Same shape at the ``_presence()`` level for msgraph_webhook: the TurnRunner-registered
- notifier must not keep the ask branch alive on a notifier-blind platform."""
- import tools.approval as approval_mod
-
- self._isolate(monkeypatch, platform="msgraph_webhook")
- 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 False
- finally:
- approval_mod.unregister_gateway_notify("test-unattended-ask-leak")
-
-
class TestNormalizationBypass:
"""Obfuscation techniques must not bypass dangerous command detection."""
diff --git a/tests/tools/test_approval_unattended_presence.py b/tests/tools/test_approval_unattended_presence.py
new file mode 100644
index 00000000000..6050ec19a50
--- /dev/null
+++ b/tests/tools/test_approval_unattended_presence.py
@@ -0,0 +1,47 @@
+"""Unattended-platform runs resolve flagged commands from ``approvals.unattended_mode`` unless a
+client can actually answer the card. The gateway exports ``HERMES_EXEC_ASK=1`` to every session
+it runs, so without this a webhook run posted its card to the route's delivery chat (where
+``/approve`` resolves a different session) and a non-streaming api_server chat parked on a
+``pending_approval`` its client never sees; both hung for the full approval timeout (#100532)."""
+
+import pytest
+
+import tools.approval as approval_mod
+from tools import approval_context
+
+SESSION = "test-unattended-presence"
+
+
+def unattended_session(monkeypatch):
+ monkeypatch.setattr(approval_mod, "_YOLO_MODE_FROZEN", False)
+ monkeypatch.setattr(approval_context, "_get_approval_mode", lambda: "manual")
+ for var in ("HERMES_CRON_SESSION", "HERMES_GATEWAY_SESSION", "HERMES_INTERACTIVE"):
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setenv("HERMES_EXEC_ASK", "1")
+ monkeypatch.setenv("HERMES_SESSION_KEY", SESSION)
+ monkeypatch.setattr(approval_mod, "_human_decision",
+ lambda *a, **k: pytest.fail("unanswerable run reached the human approval gate"))
+ approval_mod._session_approved.pop(SESSION, None)
+ yield lambda platform: monkeypatch.setenv("HERMES_SESSION_PLATFORM", platform)
+ approval_mod.unregister_gateway_notify(SESSION)
+
+
[email protected]("platform", ["webhook", "msgraph_webhook"])
+def test_webhook_denies_instantly_even_with_turn_runner_notifier(unattended_session, platform):
+ # The generic TurnRunner lane registers a notifier on every turn; these adapters still cannot answer it.
+ unattended_session(platform)
+ approval_mod.register_gateway_notify(SESSION, lambda data: pytest.fail("card sent to a platform nobody answers"))
+ result = approval_mod.check_all_command_guards("sudo systemctl restart nginx", "local")
+ assert result["approved"] is False
+ assert "approvals.unattended_mode" in result["message"]
+
+
+def test_api_server_asks_only_when_a_client_bridge_is_registered(unattended_session):
+ unattended_session("api_server")
+ result = approval_mod.check_all_command_guards("sudo systemctl restart nginx", "local")
+ assert result["approved"] is False and "approvals.unattended_mode" in result["message"]
+
+ # /v1/runs and streaming chat completions register a notifier: the approval bridge keeps the card.
+ approval_mod.register_gateway_notify(SESSION, lambda data: None)
+ assert approval_mod._presence()[3] is True
diff --git a/website/docs/user-guide/security.md b/website/docs/user-guide/security.md
index 56c4142d665..3e420e58a8f 100644
--- a/website/docs/user-guide/security.md
+++ b/website/docs/user-guide/security.md
@@ -48,7 +48,7 @@ The full set of keys:
| `timeout` | `300` | Seconds Hermes waits for an approval reply before timing out. |
| `cron_mode` | `deny` | How [cron jobs](./features/cron.md) behave headlessly when they trigger a dangerous-command prompt. `deny` blocks the command (the agent must find another path); `approve` auto-approves everything in cron context. |
| `single_query_mode` | `deny` | How one-shot [`hermes chat -q`](./cli.md) sessions behave when they trigger a dangerous-command prompt. A `-q` session runs a single turn and exits with no user waiting to answer prompts; `deny` blocks the command (the agent must find another path), `approve` auto-approves everything in single-query context. Mirrors `cron_mode`. |
-| `unattended_mode` | `deny` | How sessions on unattended programmatic platforms (webhook, msgraph_webhook, api_server) behave when they trigger a dangerous-command prompt. These surfaces have no human who can answer `/approve`, so instead of blocking for the full approval timeout, `deny` blocks the command instantly (the agent must find another path) and `approve` auto-approves everything in unattended context. Mirrors `cron_mode`. |
+| `unattended_mode` | `deny` | How sessions on unattended programmatic platforms (webhook, msgraph_webhook, api_server) behave when they trigger a dangerous-command prompt. These surfaces have no human who can answer `/approve`, so instead of blocking for the full approval timeout, `deny` blocks the command instantly (the agent must find another path) and `approve` auto-approves everything in unattended context. Exception: an api_server session whose client can answer the card (`/v1/runs` and streaming chat completions, via `POST /v1/runs/{id}/approval`) still gets the approval request. Mirrors `cron_mode`. |
| `mcp_reload_confirm` | `true` | When true, `/reload-mcp` asks before rebuilding the MCP tool set. Rebuilding invalidates the provider prompt cache (tool schemas live in the system prompt), so the next message re-sends full input tokens. Users who click **Always Approve** flip this key to `false`. |
| `destructive_slash_confirm` | `true` | When true, destructive session slash commands (`/clear`, `/new`, `/reset`, `/undo`) prompt before discarding conversation state. Three-option dialog (Approve Once / Always Approve / Cancel) routed through native yes/no buttons on Telegram, Discord, and Slack; text fallback elsewhere. Users who click **Always Approve** flip this key to `false`. The TUI also honors this setting for its `/clear`, `/new`, and `/reset` modal; `HERMES_TUI_NO_CONFIRM=1` force-skips that modal regardless of the configured value. |