39975613b1
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.
130 lines
4.4 KiB
Python
130 lines
4.4 KiB
Python
"""Regression test: internal synthetic events must never interrupt a busy session.
|
|
|
|
Reported by @Heeervas (June 2026): an ``async_delegation`` completion from a
|
|
``delegate_task(background=true)`` subagent re-enters the originating gateway
|
|
session as an internal ``MessageEvent``. When that session was busy running a
|
|
turn, the completion was treated exactly like a user TEXT message and hit the
|
|
default ``busy_input_mode='interrupt'`` path — calling
|
|
``running_agent.interrupt()`` and aborting the active turn, plus sending a
|
|
"⚡ Interrupting current task" ack. The same shape affects background-process
|
|
completions (terminal ``notify_on_complete``), which also re-enter as internal
|
|
events.
|
|
|
|
The fix: ``_handle_active_session_busy_message`` returns ``False`` early for any
|
|
event with ``internal=True``, so the base adapter queues it silently (no
|
|
interrupt, no ack) and it cascades as a new turn after the current one finishes.
|
|
This preserves strict message-role alternation and the design invariant that a
|
|
completion surfaces as a NEW turn only when idle, never spliced into a running
|
|
turn.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import threading
|
|
import types
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
# Minimal telegram stubs so gateway imports cleanly (mirrors sibling tests).
|
|
_tg = types.ModuleType("telegram")
|
|
_tg.constants = types.ModuleType("telegram.constants")
|
|
_ct = MagicMock()
|
|
_ct.SUPERGROUP = "supergroup"
|
|
_ct.GROUP = "group"
|
|
_ct.PRIVATE = "private"
|
|
_tg.constants.ChatType = _ct
|
|
sys.modules.setdefault("telegram", _tg)
|
|
sys.modules.setdefault("telegram.constants", _tg.constants)
|
|
sys.modules.setdefault("telegram.ext", types.ModuleType("telegram.ext"))
|
|
|
|
from gateway.platforms.base import ( # noqa: E402
|
|
MessageEvent,
|
|
MessageType,
|
|
SessionSource,
|
|
build_session_key,
|
|
)
|
|
from gateway.run import GatewayRunner # noqa: E402
|
|
|
|
|
|
def _make_internal_event(text: str = "[async delegation completed]") -> MessageEvent:
|
|
source = SessionSource(
|
|
platform=MagicMock(value="telegram"),
|
|
chat_id="123",
|
|
chat_type="private",
|
|
user_id="user1",
|
|
)
|
|
return MessageEvent(
|
|
text=text,
|
|
message_type=MessageType.TEXT,
|
|
source=source,
|
|
message_id="msg1",
|
|
internal=True,
|
|
)
|
|
|
|
|
|
def _make_runner() -> GatewayRunner:
|
|
runner = object.__new__(GatewayRunner)
|
|
runner._running_agents = {}
|
|
runner._running_agents_ts = {}
|
|
runner._pending_messages = {}
|
|
runner._busy_ack_ts = {}
|
|
runner._draining = False
|
|
runner.adapters = {}
|
|
runner.config = MagicMock()
|
|
runner.session_store = None
|
|
runner.hooks = MagicMock()
|
|
runner.hooks.emit = AsyncMock()
|
|
runner.pairing_store = MagicMock()
|
|
runner.pairing_store.is_approved.return_value = True
|
|
runner._is_user_authorized = lambda _source: True
|
|
return runner
|
|
|
|
|
|
def _make_adapter() -> MagicMock:
|
|
adapter = MagicMock()
|
|
adapter._pending_messages = {}
|
|
adapter._send_with_retry = AsyncMock()
|
|
adapter.config = MagicMock()
|
|
adapter.config.extra = {}
|
|
adapter.platform = MagicMock(value="telegram")
|
|
return adapter
|
|
|
|
|
|
def _make_running_parent() -> MagicMock:
|
|
parent = MagicMock()
|
|
parent._active_children = [] # no active subagents at completion time
|
|
parent._active_children_lock = threading.Lock()
|
|
parent.get_activity_summary.return_value = {
|
|
"api_call_count": 4,
|
|
"max_iterations": 60,
|
|
"current_tool": "terminal",
|
|
}
|
|
return parent
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_internal_event_does_not_interrupt_busy_session() -> None:
|
|
"""The async-delegation completion must not abort the active turn."""
|
|
runner = _make_runner()
|
|
runner._busy_input_mode = "interrupt" # the default that caused the bug
|
|
adapter = _make_adapter()
|
|
event = _make_internal_event()
|
|
sk = build_session_key(event.source)
|
|
parent = _make_running_parent()
|
|
runner._running_agents[sk] = parent
|
|
runner.adapters[event.source.platform] = adapter
|
|
|
|
handled = await runner._handle_active_session_busy_message(event, sk)
|
|
|
|
# Returns False so the base adapter silently queues the internal event
|
|
# as a cascading next turn — it must NOT be handled-with-interrupt here.
|
|
assert handled is False
|
|
# The active turn must survive.
|
|
parent.interrupt.assert_not_called()
|
|
# No "⚡ Interrupting current task" (or any) ack for a synthetic event.
|
|
adapter._send_with_retry.assert_not_called()
|
|
|
|
|