hermes-agent/tests/gateway/test_plaintext_approval_routing.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

135 lines
4.7 KiB
Python

"""Tests for #46866: plain-text approval responses must resolve a blocking
dangerous-command approval instead of being steered/queued.
When the agent is blocked inside tools/approval.py waiting for a dangerous
command to be approved, a messaging user who replies "yes" / "approve" /
"deny" (without the leading slash) must have that response routed to the
approval handler. Previously the bare-word reply fell through to the
steer/queue/interrupt logic in _handle_active_session_busy_message — the
approval never resolved, timed out, and auto-denied.
Slash forms (/approve, /deny) already bypass at the base-adapter guard;
this covers the bare-word forms Signal/SMS users naturally type.
"""
import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from gateway.config import GatewayConfig, Platform, PlatformConfig
from gateway.platforms.base import MessageEvent, MessageType
from gateway.session import SessionSource
def _make_source() -> SessionSource:
return SessionSource(
platform=Platform.TELEGRAM,
user_id="u1",
chat_id="c1",
user_name="tester",
chat_type="dm",
)
def _make_event(text: str) -> MessageEvent:
return MessageEvent(
text=text,
message_type=MessageType.TEXT,
source=_make_source(),
message_id="m1",
)
def _clear_approval_state():
from tools import approval as mod
mod._gateway_queues.clear()
mod._gateway_notify_cbs.clear()
mod._session_approved.clear()
mod._permanent_approved.clear()
mod._pending.clear()
def _make_runner():
"""Minimal GatewayRunner that exercises the real busy-session handler."""
from gateway.run import GatewayRunner
runner = object.__new__(GatewayRunner)
runner.config = GatewayConfig(
platforms={Platform.TELEGRAM: PlatformConfig(enabled=True, token="***")}
)
adapter = MagicMock()
adapter.send = AsyncMock()
adapter._send_with_retry = AsyncMock(
return_value=SimpleNamespace(success=True, message_id="reply1")
)
# _unwrap_ephemeral is a real base-adapter method; emulate its contract.
adapter._unwrap_ephemeral = lambda r: (r, 0) if isinstance(r, str) else (None, 0)
runner.adapters = {Platform.TELEGRAM: adapter}
runner._running_agents = {}
runner._running_agents_ts = {}
runner._pending_messages = {}
runner._pending_approvals = {}
runner._busy_ack_ts = {}
runner._draining = False
runner.session_store = None
runner._is_user_authorized = lambda _source: True
# _handle_active_session_busy_message uses these only on the
# non-approval fall-through path; harmless to stub.
runner._busy_input_mode = "interrupt"
runner._busy_text_mode = "interrupt"
return runner, adapter
def _register_blocking_approval(runner):
"""Register a real blocking approval entry for the runner's session."""
from tools.approval import _ApprovalEntry, _gateway_queues
source = _make_source()
session_key = runner._session_key_for_source(source)
entry = _ApprovalEntry({"command": "rm -rf /tmp/test"})
_gateway_queues.setdefault(session_key, []).append(entry)
return session_key, entry
@pytest.mark.parametrize("reply", ["yes", "approve", "ok", "y", "confirm"])
def test_plaintext_yes_resolves_approval(reply):
_clear_approval_state()
runner, adapter = _make_runner()
session_key, entry = _register_blocking_approval(runner)
handled = asyncio.run(
runner._handle_active_session_busy_message(_make_event(reply), session_key)
)
assert handled is True
assert entry.event.is_set()
assert entry.result == "once"
# The user gets a confirmation reply, not silence.
adapter._send_with_retry.assert_awaited()
_clear_approval_state()
def test_no_pending_approval_does_not_consume_conversational_yes():
"""A bare 'yes' with NO blocking approval must NOT be treated as an
approval — it falls through to normal busy handling (design intent:
'yes' in conversation must not execute a dangerous command)."""
_clear_approval_state()
runner, adapter = _make_runner()
source = _make_source()
session_key = runner._session_key_for_source(source)
# No approval registered.
handled = asyncio.run(
runner._handle_active_session_busy_message(_make_event("yes"), session_key)
)
# No approval existed, so nothing was resolved — the "yes" is treated
# as ordinary text, not as a dangerous-command approval (design intent).
# (It still flows through normal busy handling, which may send a busy
# ack; the contract here is only that no approval was consumed.)
from tools.approval import _gateway_queues
assert session_key not in _gateway_queues
_clear_approval_state()