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.
142 lines
5.4 KiB
Python
142 lines
5.4 KiB
Python
"""Regression tests for #41289: the Discord/Telegram ``/model`` slash command
|
|
must not run the blocking provider-listing on the gateway's async event loop.
|
|
|
|
``list_picker_providers`` / ``list_authenticated_providers`` are synchronous and
|
|
can fall through to a blocking ``urllib`` HTTP fetch when the on-disk provider
|
|
cache is stale. Running that directly on the event loop froze the gateway for
|
|
120-150s ("application did not respond" + delayed agent starts).
|
|
|
|
Fix (ported from #41304, which patched the old ``gateway/run.py`` location):
|
|
``_handle_model_command`` offloads BOTH provider-listing calls via
|
|
``asyncio.to_thread`` so the loop stays responsive:
|
|
|
|
* line ~1161 — picker path -> ``list_picker_providers``
|
|
* line ~1382 — text-fallback -> ``list_authenticated_providers``
|
|
|
|
These tests assert the *offload contract* at the real handler seam: each listing
|
|
function must be dispatched through ``asyncio.to_thread`` and must NOT be invoked
|
|
directly. Reverting either ``to_thread`` wrap (calling the sync fn inline again)
|
|
makes the corresponding test fail — i.e. the tests are mutation-survivable.
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
import pytest
|
|
|
|
import gateway.slash_commands as slash_commands
|
|
from gateway.config import Platform
|
|
from gateway.platforms.base import MessageEvent, MessageType
|
|
from gateway.run import GatewayRunner
|
|
from gateway.session import SessionSource
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Harness
|
|
# --------------------------------------------------------------------------- #
|
|
def _make_runner():
|
|
runner = object.__new__(GatewayRunner)
|
|
runner.adapters = {}
|
|
runner._voice_mode = {}
|
|
runner._session_model_overrides = {}
|
|
runner._running_agents = {}
|
|
return runner
|
|
|
|
|
|
def _make_event():
|
|
"""A bare ``/model`` (no args) — triggers the listing branch."""
|
|
return MessageEvent(
|
|
text="/model",
|
|
message_type=MessageType.TEXT,
|
|
source=SessionSource(platform=Platform.TELEGRAM, chat_id="12345", chat_type="dm"),
|
|
)
|
|
|
|
|
|
class _ToThreadSpy:
|
|
"""Wraps the real ``asyncio.to_thread`` and records what it was asked to run."""
|
|
|
|
def __init__(self):
|
|
self.calls = [] # list of (func, args, kwargs)
|
|
self._real = asyncio.to_thread
|
|
|
|
async def __call__(self, func, /, *args, **kwargs):
|
|
self.calls.append((func, args, kwargs))
|
|
return await self._real(func, *args, **kwargs)
|
|
|
|
def funcs_offloaded(self):
|
|
return [c[0] for c in self.calls]
|
|
|
|
|
|
@pytest.fixture
|
|
def _isolated_config(tmp_path, monkeypatch):
|
|
"""Point the handler at an empty isolated home so config loading is cheap
|
|
and deterministic (no real provider creds / network)."""
|
|
import gateway.run as gateway_run
|
|
|
|
hermes_home = tmp_path / ".hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text("model:\n default: gpt-x\n provider: openrouter\nproviders: {}\n", encoding="utf-8")
|
|
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
|
|
monkeypatch.setattr("agent.models_dev.fetch_models_dev", lambda: {})
|
|
return hermes_home
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Text-fallback path -> list_authenticated_providers
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Picker path -> list_picker_providers
|
|
# --------------------------------------------------------------------------- #
|
|
class _FakePickerResult:
|
|
success = True
|
|
|
|
|
|
class _FakePickerAdapter:
|
|
"""Adapter whose *type* exposes ``send_model_picker`` (the gate the handler
|
|
checks via ``getattr(type(adapter), 'send_model_picker', None)``)."""
|
|
|
|
async def send_model_picker(self, **kwargs):
|
|
return _FakePickerResult()
|
|
|
|
def _thread_metadata(self, *a, **k): # pragma: no cover - not exercised
|
|
return None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_picker_path_offloads_list_picker_providers(_isolated_config, monkeypatch):
|
|
"""A picker-capable adapter => handler takes the picker branch, which must
|
|
offload ``list_picker_providers`` to a worker thread."""
|
|
spy = _ToThreadSpy()
|
|
monkeypatch.setattr(slash_commands.asyncio, "to_thread", spy)
|
|
|
|
# Non-empty providers so the handler proceeds to send_model_picker (and
|
|
# returns None), proving we got past the offloaded listing call.
|
|
fake_providers = [{"slug": "openrouter", "name": "OpenRouter", "is_current": True,
|
|
"models": ["gpt-x"], "total_models": 1}]
|
|
|
|
def _fake_list_picker_providers(**kwargs):
|
|
return fake_providers
|
|
|
|
monkeypatch.setattr(
|
|
"hermes_cli.model_switch.list_picker_providers",
|
|
_fake_list_picker_providers,
|
|
)
|
|
|
|
runner = _make_runner()
|
|
runner.adapters = {Platform.TELEGRAM: _FakePickerAdapter()}
|
|
# Stub the metadata/anchor helpers the picker branch calls before sending.
|
|
monkeypatch.setattr(runner, "_thread_metadata_for_source", lambda *a, **k: None, raising=False)
|
|
monkeypatch.setattr(runner, "_reply_anchor_for_event", lambda *a, **k: None, raising=False)
|
|
|
|
result = await runner._handle_model_command(_make_event())
|
|
|
|
# Picker "sent" => handler returns None.
|
|
assert result is None
|
|
offloaded = spy.funcs_offloaded()
|
|
assert _fake_list_picker_providers in offloaded, (
|
|
"list_picker_providers must be dispatched via asyncio.to_thread "
|
|
"(it was called inline on the event loop instead)"
|
|
)
|
|
|
|
|