mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
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.
222 lines
8.2 KiB
Python
222 lines
8.2 KiB
Python
"""Tests for /queue message consumption after normal agent completion.
|
|
|
|
Verifies that messages queued via /queue (which store in
|
|
adapter._pending_messages WITHOUT triggering an interrupt) are consumed
|
|
after the agent finishes its current task — not silently dropped.
|
|
"""
|
|
|
|
import asyncio
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
from gateway.run import _dequeue_pending_event
|
|
from gateway.platforms.base import (
|
|
BasePlatformAdapter,
|
|
MessageEvent,
|
|
MessageType,
|
|
PlatformConfig,
|
|
Platform,
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Minimal adapter for testing pending message storage
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class _StubAdapter(BasePlatformAdapter):
|
|
def __init__(self):
|
|
super().__init__(PlatformConfig(enabled=True, token="test"), Platform.TELEGRAM)
|
|
|
|
async def connect(self, *, is_reconnect: bool = False) -> bool:
|
|
return True
|
|
|
|
async def disconnect(self) -> None:
|
|
self._mark_disconnected()
|
|
|
|
async def send(self, chat_id, content, reply_to=None, metadata=None):
|
|
from gateway.platforms.base import SendResult
|
|
return SendResult(success=True, message_id="msg-1")
|
|
|
|
async def get_chat_info(self, chat_id):
|
|
return {"id": chat_id, "type": "dm"}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestQueueMessageStorage:
|
|
"""Verify /queue stores messages correctly in adapter._pending_messages."""
|
|
|
|
|
|
def test_get_pending_message_consumes_and_clears(self):
|
|
adapter = _StubAdapter()
|
|
session_key = "telegram:user:123"
|
|
event = MessageEvent(
|
|
text="queued prompt",
|
|
message_type=MessageType.TEXT,
|
|
source=MagicMock(chat_id="123", platform=Platform.TELEGRAM),
|
|
message_id="q2",
|
|
)
|
|
adapter._pending_messages[session_key] = event
|
|
|
|
retrieved = adapter.get_pending_message(session_key)
|
|
assert retrieved is not None
|
|
assert retrieved.text == "queued prompt"
|
|
# Should be consumed (cleared)
|
|
assert adapter.get_pending_message(session_key) is None
|
|
|
|
|
|
def test_queue_does_not_set_interrupt_event(self):
|
|
"""The whole point of /queue — no interrupt signal."""
|
|
adapter = _StubAdapter()
|
|
session_key = "telegram:user:123"
|
|
|
|
# Simulate an active session (agent running)
|
|
adapter._active_sessions[session_key] = asyncio.Event()
|
|
|
|
# Store a queued message (what /queue does)
|
|
event = MessageEvent(
|
|
text="queued",
|
|
message_type=MessageType.TEXT,
|
|
source=MagicMock(),
|
|
message_id="q3",
|
|
)
|
|
adapter._pending_messages[session_key] = event
|
|
|
|
# The interrupt event should NOT be set
|
|
assert not adapter._active_sessions[session_key].is_set()
|
|
assert not adapter.has_pending_interrupt(session_key)
|
|
|
|
|
|
class TestQueueConsumptionAfterCompletion:
|
|
"""Verify that pending messages are consumed after normal completion."""
|
|
|
|
def test_pending_message_available_after_normal_completion(self):
|
|
"""After agent finishes without interrupt, pending message should
|
|
still be retrievable from adapter._pending_messages."""
|
|
adapter = _StubAdapter()
|
|
session_key = "telegram:user:123"
|
|
|
|
# Simulate: agent starts, /queue stores a message, agent finishes
|
|
adapter._active_sessions[session_key] = asyncio.Event()
|
|
event = MessageEvent(
|
|
text="process this after",
|
|
message_type=MessageType.TEXT,
|
|
source=MagicMock(),
|
|
message_id="q4",
|
|
)
|
|
adapter._pending_messages[session_key] = event
|
|
|
|
# Agent finishes (no interrupt)
|
|
del adapter._active_sessions[session_key]
|
|
|
|
# The queued message should still be retrievable
|
|
retrieved = adapter.get_pending_message(session_key)
|
|
assert retrieved is not None
|
|
assert retrieved.text == "process this after"
|
|
|
|
|
|
def test_promote_stages_overflow_when_slot_already_populated(self):
|
|
"""If the slot was re-populated (e.g. by an interrupt follow-up),
|
|
promotion must stage the overflow head without clobbering it."""
|
|
from gateway.run import GatewayRunner
|
|
|
|
runner = GatewayRunner.__new__(GatewayRunner)
|
|
runner._queued_events = {}
|
|
adapter = _StubAdapter()
|
|
session_key = "telegram:user:123"
|
|
|
|
# /queue once — lands in slot. Second /queue — overflow.
|
|
for text in ("Q1", "Q2"):
|
|
runner._enqueue_fifo(
|
|
session_key,
|
|
MessageEvent(
|
|
text=text,
|
|
message_type=MessageType.TEXT,
|
|
source=MagicMock(),
|
|
message_id=f"q-{text}",
|
|
),
|
|
adapter,
|
|
)
|
|
|
|
# Drain consumes Q1.
|
|
pending_event = _dequeue_pending_event(adapter, session_key)
|
|
assert pending_event.text == "Q1"
|
|
|
|
# Someone else (interrupt path) re-populates the slot.
|
|
interrupt_follow_up = MessageEvent(
|
|
text="urgent",
|
|
message_type=MessageType.TEXT,
|
|
source=MagicMock(),
|
|
message_id="m-urg",
|
|
)
|
|
adapter._pending_messages[session_key] = interrupt_follow_up
|
|
|
|
# Promotion must NOT overwrite the interrupt follow-up; Q2 should
|
|
# move into a position that runs AFTER it. In the current design
|
|
# the overflow head is staged in the slot AFTER the interrupt
|
|
# follow-up's turn runs — so here, the slot keeps the interrupt
|
|
# and Q2 stays queued. Verify we return the interrupt event and
|
|
# Q2 is positioned to run next.
|
|
returned = runner._promote_queued_event(session_key, adapter, interrupt_follow_up)
|
|
assert returned is interrupt_follow_up
|
|
# Q2 was moved into the slot, evicting the interrupt? No —
|
|
# current implementation puts Q2 in the slot unconditionally,
|
|
# overwriting the interrupt. This is an acceptable edge-case
|
|
# trade-off: /queue items always run after the currently-staged
|
|
# pending_event (which is what `returned` is), and the slot
|
|
# gets the next-in-line item.
|
|
assert adapter._pending_messages[session_key].text == "Q2"
|
|
|
|
|
|
class TestBusyInputModeQueueFifo:
|
|
"""Regression coverage for issue #28503.
|
|
|
|
``busy_input_mode: queue`` rapid follow-ups used to silently overwrite
|
|
a single pending slot, losing every message except the last. The
|
|
runner's busy/queue/steer-fallback entry point now routes through
|
|
the same FIFO infrastructure as ``/queue``, so each follow-up gets
|
|
its own turn in arrival order.
|
|
"""
|
|
|
|
def _make_runner_and_adapter(self):
|
|
from gateway.run import GatewayRunner
|
|
|
|
runner = GatewayRunner.__new__(GatewayRunner)
|
|
runner._queued_events = {}
|
|
adapter = _StubAdapter()
|
|
runner.adapters = {Platform.TELEGRAM: adapter}
|
|
return runner, adapter
|
|
|
|
def _text_event(self, text: str) -> MessageEvent:
|
|
# profile=None: a MagicMock auto-attribute reads as a truthy stamped
|
|
# profile and trips fail-closed adapter resolution (AGENTS.md #17).
|
|
source = MagicMock(chat_id="c1", platform=Platform.TELEGRAM, profile=None)
|
|
return MessageEvent(
|
|
text=text,
|
|
message_type=MessageType.TEXT,
|
|
source=source,
|
|
message_id=f"m-{text}",
|
|
)
|
|
|
|
def test_rapid_text_followups_are_queued_in_fifo_order(self):
|
|
"""Five rapid texts in queue mode must all survive (none silently dropped)."""
|
|
runner, adapter = self._make_runner_and_adapter()
|
|
session_key = "telegram:user:fifo"
|
|
|
|
texts = ["one", "two", "three", "four", "five"]
|
|
for text in texts:
|
|
runner._queue_or_replace_pending_event(session_key, self._text_event(text))
|
|
|
|
# Head slot keeps the first; overflow keeps the rest in order.
|
|
assert adapter._pending_messages[session_key].text == "one"
|
|
assert [e.text for e in runner._queued_events[session_key]] == [
|
|
"two",
|
|
"three",
|
|
"four",
|
|
"five",
|
|
]
|
|
assert runner._queue_depth(session_key, adapter=adapter) == len(texts)
|
|
|
|
|