Files
hermes-agent/tests/tui_gateway/test_mcp_late_refresh_thread_owner.py
T
Teknium 6b81590c55 test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

93 lines
3.3 KiB
Python

"""Regression test for issue #51587.
MCP tools connected/enabled but never surfaced into the agent's session
toolset on the desktop app + dashboard WebUI.
Root cause: there are two independent background MCP discovery thread owners
by surface:
* ``tui_gateway.entry`` — the stdio ``hermes --tui`` path.
* ``hermes_cli.mcp_startup`` — the desktop app + dashboard WebSocket sidecar
(``tui_gateway/ws.py``) and ``hermes dashboard``.
The late-refresh scheduler (``tui_gateway.server._schedule_mcp_late_refresh``)
gates on ``tui_gateway.entry.mcp_discovery_in_flight()``. Before the fix that
function read ONLY ``tui_gateway.entry._mcp_discovery_thread``. On the
desktop/dashboard surfaces that thread is ``None`` (the thread lives on
``hermes_cli.mcp_startup``), so the scheduler bailed immediately and a slow MCP
server's tools never surfaced for the whole session — even after a container
restart. The fix makes ``mcp_discovery_in_flight`` / ``join_mcp_discovery``
consult BOTH thread owners.
"""
import threading
import pytest
import hermes_cli.mcp_startup as startup
import tui_gateway.entry as entry
@pytest.fixture
def clean_discovery_globals():
"""Snapshot and restore both modules' discovery-thread globals."""
saved_entry = entry._mcp_discovery_thread
saved_startup = startup._mcp_discovery_thread
entry._mcp_discovery_thread = None
startup._mcp_discovery_thread = None
try:
yield
finally:
entry._mcp_discovery_thread = saved_entry
startup._mcp_discovery_thread = saved_startup
def _alive_thread(stop: threading.Event) -> threading.Thread:
t = threading.Thread(target=lambda: stop.wait(5.0), daemon=True)
t.start()
return t
def test_entry_in_flight_sees_startup_thread(clean_discovery_globals):
"""Desktop/dashboard surface: discovery thread lives on hermes_cli.mcp_startup.
The entry-level in-flight check must report True so the late-refresh
scheduler does not bail (the #51587 bug).
"""
stop = threading.Event()
startup._mcp_discovery_thread = _alive_thread(stop)
try:
# Entry's own thread is None, but the startup thread is alive.
assert entry._mcp_discovery_thread is None
assert entry.mcp_discovery_in_flight() is True
finally:
stop.set()
startup._mcp_discovery_thread.join(timeout=2.0)
# After the thread exits, neither owner is in flight.
assert entry.mcp_discovery_in_flight() is False
def test_no_mcp_threads_not_in_flight(clean_discovery_globals):
"""No discovery anywhere → not in flight, join reports done immediately."""
assert entry.mcp_discovery_in_flight() is False
assert entry.join_mcp_discovery(timeout=0.1) is True
def test_startup_module_exposes_in_flight_helpers(clean_discovery_globals):
"""hermes_cli.mcp_startup gains the in-flight/join helpers entry delegates to."""
assert startup.mcp_discovery_in_flight() is False
assert startup.join_mcp_discovery(timeout=0.1) is True
stop = threading.Event()
t = _alive_thread(stop)
startup._mcp_discovery_thread = t
try:
assert startup.mcp_discovery_in_flight() is True
assert startup.join_mcp_discovery(timeout=0.1) is False
finally:
stop.set()
t.join(timeout=2.0)
assert startup.mcp_discovery_in_flight() is False
assert startup.join_mcp_discovery(timeout=2.0) is True