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

157 lines
5.6 KiB
Python

"""Gateway regression: the outer finalisation path must not NameError on
the streaming-TTS consumer (#60671).
The original commit created ``_streaming_tts_consumer`` as a local inside
``run_sync`` (executor-thread) but the outer ``_run_agent_inner`` code
referenced it during interrupt/finalisation — a cross-scope ``NameError``
on ordinary gateway turns. The hardening moves the consumer into a
``streaming_tts_consumer_holder`` created on the event-loop thread.
This test exercises the real ``_run_agent`` → ``_run_agent_inner`` path
with a voice input message type so the streaming-TTS consumer setup
branch is entered. The fake agent returns synchronously, the executor
finishes, and the outer finalisation code runs — proving no NameError.
"""
import asyncio
import sys
import threading
import types
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import gateway.run as gateway_run
from gateway.config import Platform
from gateway.platforms.base import MessageEvent, MessageType
from gateway.session import SessionSource
class _NoopAgent:
"""Minimal agent stub that returns immediately without tool calls."""
def __init__(self, *args, **kwargs):
self.tools = []
self.model = kwargs.get("model", "test-model")
self.provider = kwargs.get("provider", "test-provider")
self.session_id = kwargs.get("session_id", "session-1")
self.context_compressor = None
self.is_interrupted = False
def run_conversation(self, user_message, conversation_history=None,
task_id=None, persist_user_message=None,
persist_user_timestamp=None):
return {
"final_response": "Hello from the agent.",
"messages": [],
"api_calls": 1,
"completed": True,
}
def _install_fake_agent(monkeypatch):
fake_run_agent = types.ModuleType("run_agent")
fake_run_agent.AIAgent = _NoopAgent
monkeypatch.setitem(sys.modules, "run_agent", fake_run_agent)
def _make_runner():
runner = object.__new__(gateway_run.GatewayRunner)
runner.adapters = {}
runner._ephemeral_system_prompt = ""
runner._prefill_messages = []
runner._reasoning_config = None
runner._service_tier = None
runner._provider_routing = {}
runner._fallback_model = None
runner._running_agents = {}
runner._pending_model_notes = {}
runner._session_db = None
runner._agent_cache = {}
runner._agent_cache_lock = threading.Lock()
runner._session_model_overrides = {}
runner.hooks = SimpleNamespace(loaded_hooks=False)
runner.config = SimpleNamespace(streaming=None, multiplex_profiles=False)
runner.session_store = SimpleNamespace(
get_or_create_session=lambda source: SimpleNamespace(session_id="session-1"),
load_transcript=lambda session_id: [],
)
runner._get_or_create_gateway_honcho = lambda session_key: (None, None)
runner._enrich_message_with_vision = AsyncMock(return_value="ENRICHED")
runner._gateway_loop = None
return runner
def _make_voice_source() -> SessionSource:
return SessionSource(
platform=Platform.TELEGRAM,
chat_id="12345",
chat_type="dm",
user_id="user-1",
)
def _setup_monkeypatches(monkeypatch, tmp_path):
_install_fake_agent(monkeypatch)
(tmp_path / "config.yaml").write_text("agent:\n model: test-model\n", encoding="utf-8")
monkeypatch.setattr(gateway_run, "_hermes_home", tmp_path)
monkeypatch.setattr(gateway_run, "_env_path", tmp_path / ".env")
monkeypatch.setattr(gateway_run, "load_dotenv", lambda *args, **kwargs: None)
monkeypatch.setattr(gateway_run, "_load_gateway_config", lambda: {})
monkeypatch.setattr(
gateway_run,
"_load_gateway_runtime_config",
lambda: {"agent": {"model": "test-model"}},
)
monkeypatch.setattr(gateway_run, "_resolve_gateway_model", lambda config=None: "test-model")
monkeypatch.setattr(
gateway_run,
"_resolve_runtime_agent_kwargs",
lambda: {
"provider": "openrouter",
"api_mode": "chat_completions",
"base_url": "https://openrouter.ai/api/v1",
"api_key": "***",
},
)
import hermes_cli.tools_config as tools_config
monkeypatch.setattr(tools_config, "_get_platform_tools", lambda user_config, platform_key: {"core"})
def test_run_agent_voice_turn_no_name_error(monkeypatch, tmp_path):
"""A voice-input turn must complete without a _streaming_tts_consumer NameError.
The streaming-TTS consumer setup is entered (voice input + auto-TTS),
but since no adapter is configured the consumer setup is skipped. The
outer finalisation path still runs and must not raise NameError when
reading ``streaming_tts_consumer_holder``.
"""
_setup_monkeypatches(monkeypatch, tmp_path)
runner = _make_runner()
# No adapter for this source → consumer setup skipped, but the outer
# finalisation path still runs with streaming_tts_consumer_holder[0]=None.
monkeypatch.setattr(
gateway_run.GatewayRunner,
"_adapter_for_source",
lambda self, source: None,
)
async def _run():
result = await runner._run_agent(
message="Hello Jarvis",
context_prompt="",
history=[],
source=_make_voice_source(),
session_id="session-1",
session_key="agent:main:telegram:dm:12345",
message_type=MessageType.VOICE,
)
return result
result = asyncio.new_event_loop().run_until_complete(_run())
assert result["final_response"] == "Hello from the agent."