hermes-agent/tests/gateway/test_run_cleanup_progress.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

297 lines
10 KiB
Python

"""Tests for opt-in cleanup of temporary progress bubbles.
When ``display.platforms.<plat>.cleanup_progress: true`` is set for a
platform whose adapter supports message deletion (e.g. Telegram), the
tool-progress bubble, "⏳ Working — N min" heartbeats, and status-callback
messages sent during a run are deleted after the final response is
delivered.
Failed runs skip cleanup so the bubbles remain as breadcrumbs.
Adapters without ``delete_message`` silently no-op.
"""
import asyncio
import importlib
import inspect as _inspect
import sys
import time
import types
from types import SimpleNamespace
import pytest
from gateway.config import Platform, PlatformConfig
async def _fire_post_delivery_cb(cb):
"""Invoke a popped post-delivery callback, awaiting if it's async.
Chained registrations return an async wrapper; single registrations
return the raw sync callable. Either way, await any awaitable result.
"""
result = cb()
if _inspect.isawaitable(result):
await result
from gateway.platforms.base import BasePlatformAdapter, SendResult
from gateway.session import SessionSource
# ---------------------------------------------------------------------------
# Test fakes — mirror those in test_run_progress_topics.py but add a
# delete_message implementation that records ids instead of hitting a bot.
# ---------------------------------------------------------------------------
class CleanupCaptureAdapter(BasePlatformAdapter):
"""Adapter that records every delete_message call for inspection."""
_next_mid = 100
def __init__(self, platform=Platform.TELEGRAM):
super().__init__(PlatformConfig(enabled=True, token="***"), platform)
self.sent = []
self.edits = []
self.deleted = []
async def connect(self, *, is_reconnect: bool = False) -> bool:
return True
async def disconnect(self) -> None:
return None
def _mint_id(self) -> str:
CleanupCaptureAdapter._next_mid += 1
return str(CleanupCaptureAdapter._next_mid)
async def send(self, chat_id, content, reply_to=None, metadata=None) -> SendResult:
mid = self._mint_id()
self.sent.append(
{"chat_id": chat_id, "content": content, "message_id": mid, "metadata": metadata}
)
return SendResult(success=True, message_id=mid)
async def edit_message(self, chat_id, message_id, content) -> SendResult:
self.edits.append({"chat_id": chat_id, "message_id": message_id, "content": content})
return SendResult(success=True, message_id=message_id)
async def delete_message(self, chat_id, message_id) -> bool:
self.deleted.append({"chat_id": chat_id, "message_id": str(message_id)})
return True
async def send_typing(self, chat_id, metadata=None) -> None:
return None
async def stop_typing(self, chat_id) -> None:
return None
async def get_chat_info(self, chat_id: str):
return {"id": chat_id}
class NoDeleteAdapter(CleanupCaptureAdapter):
"""Adapter that inherits the base no-op delete_message (used to prove
the cleanup path skips adapters without deletion support)."""
async def delete_message(self, chat_id, message_id) -> bool: # type: ignore[override]
# Pretend to be an adapter whose platform doesn't support deletion:
# match the base class behavior exactly. gateway/run.py checks
# ``type(adapter).delete_message is BasePlatformAdapter.delete_message``
# to detect this, so we re-assign at class body level below.
raise AssertionError("should not be called — cleanup must skip this adapter")
# Re-bind so the class's delete_message identity equals the base's.
NoDeleteAdapter.delete_message = BasePlatformAdapter.delete_message
class ProgressAgent:
"""Emits two tool-progress events and returns a normal final response."""
def __init__(self, **kwargs):
self.tool_progress_callback = kwargs.get("tool_progress_callback")
self.tools = []
def run_conversation(self, message, conversation_history=None, task_id=None):
cb = self.tool_progress_callback
if cb is not None:
cb("tool.started", "terminal", "pwd", {})
time.sleep(0.2)
cb("tool.started", "terminal", "ls", {})
time.sleep(0.2)
return {"final_response": "done", "messages": [], "api_calls": 1}
class FailingAgent:
def __init__(self, **kwargs):
self.tool_progress_callback = kwargs.get("tool_progress_callback")
self.tools = []
def run_conversation(self, message, conversation_history=None, task_id=None):
cb = self.tool_progress_callback
if cb is not None:
cb("tool.started", "terminal", "pwd", {})
time.sleep(0.2)
# Empty final_response + failed=True is the shape the gateway
# actually returns on provider errors (see gateway/run.py where
# failed keys are only propagated when final_response is empty).
return {
"final_response": "",
"messages": [],
"api_calls": 1,
"failed": True,
"error": "simulated provider failure",
}
def _make_runner(adapter):
gateway_run = importlib.import_module("gateway.run")
GatewayRunner = gateway_run.GatewayRunner
runner = object.__new__(GatewayRunner)
runner.adapters = {adapter.platform: adapter}
runner._voice_mode = {}
runner._prefill_messages = []
runner._ephemeral_system_prompt = ""
runner._reasoning_config = None
runner._provider_routing = {}
runner._fallback_model = None
runner._session_db = None
runner._running_agents = {}
runner._session_run_generation = {}
runner.hooks = SimpleNamespace(loaded_hooks=False)
runner.config = SimpleNamespace(
thread_sessions_per_user=False,
group_sessions_per_user=False,
stt_enabled=False,
)
return runner
def _install_fakes(
monkeypatch,
agent_cls,
*,
cleanup_on: bool,
cleanup_platform: Platform = Platform.TELEGRAM,
):
"""Wire up the module stubs every _run_agent test needs."""
monkeypatch.setenv("HERMES_TOOL_PROGRESS_MODE", "all")
fake_dotenv = types.ModuleType("dotenv")
fake_dotenv.load_dotenv = lambda *a, **k: None
monkeypatch.setitem(sys.modules, "dotenv", fake_dotenv)
fake_run_agent = types.ModuleType("run_agent")
fake_run_agent.AIAgent = agent_cls
monkeypatch.setitem(sys.modules, "run_agent", fake_run_agent)
import tools.terminal_tool # noqa: F401 — register tool emoji
gateway_run = importlib.import_module("gateway.run")
monkeypatch.setattr(gateway_run, "_resolve_runtime_agent_kwargs", lambda: {"api_key": "fake"})
# Wire the per-platform cleanup_progress flag via the config loader the
# gateway actually reads (``_load_gateway_config`` returns user config).
cfg = {
"display": {
"platforms": {
cleanup_platform.value: {"cleanup_progress": True},
}
}
} if cleanup_on else {}
monkeypatch.setattr(gateway_run, "_load_gateway_config", lambda: cfg)
return gateway_run
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_messaging_agent_forwards_checkpoint_config(monkeypatch, tmp_path):
"""Writable gateway agents must receive the configured checkpoint limits."""
captured = {}
class CheckpointCaptureAgent(ProgressAgent):
def __init__(self, **kwargs):
captured.update(kwargs)
super().__init__(**kwargs)
adapter = CleanupCaptureAdapter()
runner = _make_runner(adapter)
gateway_run = _install_fakes(
monkeypatch, CheckpointCaptureAgent, cleanup_on=False,
)
monkeypatch.setattr(gateway_run, "_hermes_home", tmp_path)
monkeypatch.setattr(
gateway_run,
"_load_gateway_config",
lambda: {
"checkpoints": {
"enabled": True,
"max_snapshots": 9,
"max_total_size_mb": 444,
"max_file_size_mb": 6,
}
},
)
source = SessionSource(platform=Platform.TELEGRAM, chat_id="-1001")
result = await runner._run_agent(
message="hello",
context_prompt="",
history=[],
source=source,
session_id="sess-checkpoints",
session_key="agent:main:telegram:group:-1001",
)
assert result["final_response"] == "done"
assert captured["checkpoints_enabled"] is True
assert captured["checkpoint_max_snapshots"] == 9
assert captured["checkpoint_max_total_size_mb"] == 444
assert captured["checkpoint_max_file_size_mb"] == 6
@pytest.mark.asyncio
async def test_cleanup_chains_with_existing_callback(monkeypatch, tmp_path):
"""When a bg-review-style callback is already registered, the cleanup
callback chains with it — both fire, neither clobbers the other."""
adapter = CleanupCaptureAdapter()
runner = _make_runner(adapter)
gateway_run = _install_fakes(monkeypatch, ProgressAgent, cleanup_on=True)
monkeypatch.setattr(gateway_run, "_hermes_home", tmp_path)
source = SessionSource(platform=Platform.TELEGRAM, chat_id="-1001")
session_key = "agent:main:telegram:group:-1001"
pre_existing_fired = []
def _preexisting_callback() -> None:
pre_existing_fired.append(True)
# Pre-register a callback with the same generation the run will use
# (run_generation=None in this test path — matches the default slot).
adapter.register_post_delivery_callback(session_key, _preexisting_callback)
result = await runner._run_agent(
message="hello",
context_prompt="",
history=[],
source=source,
session_id="sess-1",
session_key=session_key,
)
assert result["final_response"] == "done"
cb = adapter.pop_post_delivery_callback(session_key)
assert callable(cb)
await _fire_post_delivery_cb(cb)
for _ in range(20):
await asyncio.sleep(0.01)
if adapter.deleted:
break
# Both effects land: the pre-existing callback fires AND the cleanup
# deletes at least one progress bubble.
assert pre_existing_fired == [True]
assert len(adapter.deleted) >= 1