mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +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.
220 lines
7.5 KiB
Python
220 lines
7.5 KiB
Python
"""Tests for gateway /reasoning command and hot reload behavior."""
|
|
|
|
import asyncio
|
|
import inspect
|
|
import sys
|
|
import types
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
import yaml
|
|
|
|
import gateway.run as gateway_run
|
|
from gateway.config import Platform
|
|
from gateway.platforms.base import MessageEvent
|
|
from gateway.session import SessionSource
|
|
|
|
|
|
def _make_event(text="/reasoning", platform=Platform.TELEGRAM, user_id="12345", chat_id="67890"):
|
|
"""Build a MessageEvent for testing."""
|
|
source = SessionSource(
|
|
platform=platform,
|
|
user_id=user_id,
|
|
chat_id=chat_id,
|
|
user_name="testuser",
|
|
)
|
|
return MessageEvent(text=text, source=source)
|
|
|
|
|
|
def _make_runner():
|
|
"""Create a bare GatewayRunner without calling __init__."""
|
|
runner = object.__new__(gateway_run.GatewayRunner)
|
|
runner.adapters = {}
|
|
runner._ephemeral_system_prompt = ""
|
|
runner._prefill_messages = []
|
|
runner._reasoning_config = None
|
|
runner._session_reasoning_overrides = {}
|
|
runner._show_reasoning = False
|
|
runner._provider_routing = {}
|
|
runner._fallback_model = None
|
|
runner._running_agents = {}
|
|
runner.hooks = MagicMock()
|
|
runner.hooks.emit = AsyncMock()
|
|
runner.hooks.loaded_hooks = []
|
|
runner._session_db = None
|
|
runner._get_or_create_gateway_honcho = lambda session_key: (None, None)
|
|
return runner
|
|
|
|
|
|
class _CapturingAgent:
|
|
"""Fake agent that records init kwargs for assertions."""
|
|
|
|
last_init = None
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
type(self).last_init = dict(kwargs)
|
|
self.tools = []
|
|
|
|
def run_conversation(self, user_message: str, conversation_history=None, task_id=None):
|
|
return {
|
|
"final_response": "ok",
|
|
"messages": [],
|
|
"api_calls": 1,
|
|
}
|
|
|
|
|
|
class TestReasoningCommand:
|
|
|
|
|
|
def test_parse_reasoning_command_args_accepts_ascii_and_smart_global_flags(self):
|
|
assert gateway_run.GatewayRunner._parse_reasoning_command_args("high --global") == ("high", True)
|
|
assert gateway_run.GatewayRunner._parse_reasoning_command_args("—global xhigh") == ("xhigh", True)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reasoning_command_reloads_current_state_from_config(self, tmp_path, monkeypatch):
|
|
hermes_home = tmp_path / "hermes"
|
|
hermes_home.mkdir()
|
|
config_path = hermes_home / "config.yaml"
|
|
config_path.write_text(
|
|
"agent:\n reasoning_effort: none\ndisplay:\n show_reasoning: true\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
|
|
|
|
runner = _make_runner()
|
|
runner._reasoning_config = {"enabled": True, "effort": "xhigh"}
|
|
runner._show_reasoning = False
|
|
|
|
result = await runner._handle_reasoning_command(_make_event("/reasoning"))
|
|
|
|
assert "**Effort:** `none (disabled)`" in result
|
|
assert "**Display:** on ✓" in result
|
|
assert runner._reasoning_config == {"enabled": False}
|
|
assert runner._show_reasoning is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("effort", ["max", "ultra"])
|
|
async def test_handle_reasoning_command_accepts_extended_efforts(
|
|
self, tmp_path, monkeypatch, effort
|
|
):
|
|
hermes_home = tmp_path / "hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text(
|
|
"agent:\n reasoning_effort: medium\n", encoding="utf-8"
|
|
)
|
|
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
|
|
|
|
runner = _make_runner()
|
|
event = _make_event(f"/reasoning {effort}")
|
|
session_key = runner._session_key_for_source(event.source)
|
|
|
|
await runner._handle_reasoning_command(event)
|
|
|
|
assert runner._session_reasoning_overrides[session_key] == {
|
|
"enabled": True,
|
|
"effort": effort,
|
|
}
|
|
|
|
|
|
def test_resolve_session_reasoning_prefers_session_override(self, tmp_path, monkeypatch):
|
|
hermes_home = tmp_path / "hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text("agent:\n reasoning_effort: low\n", encoding="utf-8")
|
|
|
|
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
|
|
|
|
runner = _make_runner()
|
|
source = _make_event("/reasoning").source
|
|
session_key = runner._session_key_for_source(source)
|
|
runner._session_reasoning_overrides[session_key] = {"enabled": True, "effort": "xhigh"}
|
|
|
|
assert runner._resolve_session_reasoning_config(source=source) == {"enabled": True, "effort": "xhigh"}
|
|
|
|
|
|
def test_run_agent_includes_enabled_mcp_servers_in_gateway_toolsets(self, tmp_path, monkeypatch):
|
|
hermes_home = tmp_path / "hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text(
|
|
"platform_toolsets:\n"
|
|
" cli: [web, memory]\n"
|
|
"mcp_servers:\n"
|
|
" exa:\n"
|
|
" url: https://mcp.exa.ai/mcp\n"
|
|
" web-search-prime:\n"
|
|
" url: https://api.z.ai/api/mcp/web_search_prime/mcp\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
|
|
monkeypatch.setattr(gateway_run, "_env_path", hermes_home / ".env")
|
|
monkeypatch.setattr(gateway_run, "load_dotenv", lambda *args, **kwargs: None)
|
|
monkeypatch.setattr(
|
|
gateway_run,
|
|
"_resolve_runtime_agent_kwargs",
|
|
lambda: {
|
|
"provider": "openrouter",
|
|
"api_mode": "chat_completions",
|
|
"base_url": "https://openrouter.ai/api/v1",
|
|
"api_key": "test-key",
|
|
},
|
|
)
|
|
fake_run_agent = types.ModuleType("run_agent")
|
|
fake_run_agent.AIAgent = _CapturingAgent
|
|
monkeypatch.setitem(sys.modules, "run_agent", fake_run_agent)
|
|
|
|
_CapturingAgent.last_init = None
|
|
runner = _make_runner()
|
|
|
|
source = SessionSource(
|
|
platform=Platform.LOCAL,
|
|
chat_id="cli",
|
|
chat_name="CLI",
|
|
chat_type="dm",
|
|
user_id="user-1",
|
|
)
|
|
|
|
result = asyncio.run(
|
|
runner._run_agent(
|
|
message="ping",
|
|
context_prompt="",
|
|
history=[],
|
|
source=source,
|
|
session_id="session-1",
|
|
session_key="agent:main:local:dm",
|
|
)
|
|
)
|
|
|
|
assert result["final_response"] == "ok"
|
|
assert _CapturingAgent.last_init is not None
|
|
enabled_toolsets = set(_CapturingAgent.last_init["enabled_toolsets"])
|
|
assert "web" in enabled_toolsets
|
|
assert "memory" in enabled_toolsets
|
|
assert "exa" in enabled_toolsets
|
|
assert "web-search-prime" in enabled_toolsets
|
|
|
|
|
|
class TestLoadShowReasoningCoercion:
|
|
"""Regression: display.show_reasoning must be coerced, not bool()'d."""
|
|
|
|
def _load_with_config(self, tmp_path, monkeypatch, yaml_body: str) -> bool:
|
|
hermes_home = tmp_path / "hermes"
|
|
hermes_home.mkdir()
|
|
(hermes_home / "config.yaml").write_text(yaml_body, encoding="utf-8")
|
|
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
|
|
return gateway_run.GatewayRunner._load_show_reasoning()
|
|
|
|
def test_quoted_false_is_false(self, tmp_path, monkeypatch):
|
|
assert self._load_with_config(
|
|
tmp_path, monkeypatch,
|
|
'display:\n show_reasoning: "false"\n',
|
|
) is False
|
|
|
|
|
|
def test_bare_true_is_true(self, tmp_path, monkeypatch):
|
|
assert self._load_with_config(
|
|
tmp_path, monkeypatch,
|
|
'display:\n show_reasoning: true\n',
|
|
) is True
|
|
|