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.
96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
"""Regression tests: the shutdown teardown loop must not hang on a wedged adapter.
|
|
|
|
`GatewayRunner._stop_impl()` tears down every adapter by awaiting
|
|
`cancel_background_tasks()` then `disconnect()`. Both calls can block
|
|
indefinitely when a platform's network state is half-dead (e.g. a wedged
|
|
Feishu/Lark WebSocket thread waiting on I/O). An unbounded await stalls the
|
|
whole shutdown past systemd's TimeoutStopSec; the resulting SIGKILL skips
|
|
atexit PID-file cleanup, so the next start dies with "PID file race lost"
|
|
(#14128).
|
|
|
|
The fix routes both teardown loops through `_bounded_adapter_teardown`,
|
|
which wraps each await in the existing per-adapter timeout budget
|
|
(HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUT) and always returns.
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from gateway.config import Platform
|
|
from gateway.run import GatewayRunner
|
|
|
|
|
|
@pytest.fixture
|
|
def bare_runner():
|
|
"""A GatewayRunner shell that only needs _bounded_adapter_teardown."""
|
|
return object.__new__(GatewayRunner)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_teardown_bounds_hanging_cancel(bare_runner, monkeypatch, caplog):
|
|
"""A wedged cancel_background_tasks() must time out, then disconnect runs."""
|
|
monkeypatch.setenv("HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUT", "0.01")
|
|
adapter = MagicMock()
|
|
|
|
async def hang():
|
|
await asyncio.sleep(0.2)
|
|
|
|
adapter.cancel_background_tasks = AsyncMock(side_effect=hang)
|
|
adapter.disconnect = AsyncMock(return_value=None)
|
|
|
|
with caplog.at_level(logging.WARNING, logger="gateway.run"):
|
|
await asyncio.wait_for(
|
|
bare_runner._bounded_adapter_teardown(adapter, Platform.FEISHU),
|
|
timeout=5.0,
|
|
)
|
|
|
|
assert "feishu background-task cancel timed out" in caplog.text
|
|
# disconnect still attempted after the cancel timeout — forward progress.
|
|
adapter.disconnect.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_teardown_continues_after_cancellation_swallowing_background_cancel(
|
|
bare_runner, monkeypatch, caplog
|
|
):
|
|
"""A stuck cancellation handler cannot prevent adapter disconnect.
|
|
|
|
This models a platform task that catches ``CancelledError`` while it is
|
|
unwinding. The teardown deadline must release runner ownership promptly,
|
|
then proceed to disconnect instead of waiting for that old task forever.
|
|
"""
|
|
monkeypatch.setenv("HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUT", "0.01")
|
|
adapter = MagicMock()
|
|
started = asyncio.Event()
|
|
release = asyncio.Event()
|
|
finished = asyncio.Event()
|
|
|
|
async def swallow_cancellation():
|
|
started.set()
|
|
while not release.is_set():
|
|
try:
|
|
await release.wait()
|
|
except asyncio.CancelledError:
|
|
continue
|
|
finished.set()
|
|
|
|
adapter.cancel_background_tasks = AsyncMock(side_effect=swallow_cancellation)
|
|
adapter.disconnect = AsyncMock(return_value=None)
|
|
operation = asyncio.create_task(
|
|
bare_runner._bounded_adapter_teardown(adapter, Platform.FEISHU)
|
|
)
|
|
await started.wait()
|
|
done, _pending = await asyncio.wait({operation}, timeout=0.2)
|
|
try:
|
|
assert operation in done
|
|
adapter.disconnect.assert_awaited_once()
|
|
assert "feishu background-task cancel timed out" in caplog.text
|
|
finally:
|
|
release.set()
|
|
await asyncio.wait({operation}, timeout=0.2)
|
|
await asyncio.wait_for(finished.wait(), timeout=0.2)
|
|
|
|
|