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.
100 lines
4.1 KiB
Python
100 lines
4.1 KiB
Python
"""TelegramAdapter wedged-getUpdates detection via pending_update_count.
|
|
|
|
PTB can report ``updater.running == True`` while its long-poll consumer is
|
|
silently stuck (observed on WSL2), so DMs queue in the Bot API and never reach
|
|
handlers (#42909). ``get_me()`` stays healthy (general request path), so the
|
|
CLOSE-WAIT heartbeat is blind to it. ``_probe_pending_updates`` watches
|
|
``get_webhook_info().pending_update_count`` and escalates to the existing
|
|
network-error recovery ladder after two consecutive stuck probes.
|
|
|
|
The same probe also covers the harsher case where the updater has stopped
|
|
entirely (``running=False``) with no reconnect in flight — the long-poll task
|
|
is gone, so the gateway silently stops receiving messages while the process
|
|
stays alive (#55769) — and feeds it into the same recovery ladder.
|
|
"""
|
|
import sys
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from gateway.config import PlatformConfig
|
|
|
|
|
|
def _ensure_telegram_mock():
|
|
if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):
|
|
return
|
|
mod = MagicMock()
|
|
mod.error.NetworkError = type("NetworkError", (OSError,), {})
|
|
mod.error.TimedOut = type("TimedOut", (OSError,), {})
|
|
mod.error.BadRequest = type("BadRequest", (Exception,), {})
|
|
for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request"):
|
|
sys.modules.setdefault(name, mod)
|
|
sys.modules.setdefault("telegram.error", mod.error)
|
|
|
|
|
|
_ensure_telegram_mock()
|
|
|
|
from plugins.platforms.telegram.adapter import TelegramAdapter # noqa: E402
|
|
|
|
|
|
def _make_adapter(*, pending: int) -> TelegramAdapter:
|
|
adapter = TelegramAdapter(PlatformConfig(enabled=True, token="***"))
|
|
adapter._webhook_mode = False
|
|
adapter._app = MagicMock()
|
|
adapter._app.updater.running = True
|
|
bot = MagicMock()
|
|
bot.get_webhook_info = AsyncMock(
|
|
return_value=MagicMock(pending_update_count=pending)
|
|
)
|
|
adapter._app.bot = bot
|
|
adapter._bot = bot
|
|
return adapter
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_single_stopped_updater_probe_does_not_escalate():
|
|
"""One probe finding a stopped updater only increments the counter (#55769)."""
|
|
adapter = _make_adapter(pending=9)
|
|
adapter._app.updater.running = False
|
|
adapter._polling_pending_stuck_count = 1
|
|
with patch.object(adapter, "_handle_polling_network_error", new=AsyncMock()) as rec:
|
|
await adapter._probe_pending_updates(adapter._app.bot, 5)
|
|
# Stopped updater means no live consumer to query for a queue.
|
|
adapter._app.bot.get_webhook_info.assert_not_called()
|
|
assert adapter._polling_pending_stuck_count == 0
|
|
assert adapter._polling_not_running_count == 1
|
|
rec.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_two_stopped_updater_probes_trigger_recovery():
|
|
"""A stopped updater that stays stopped routes into recovery (#55769)."""
|
|
adapter = _make_adapter(pending=9)
|
|
adapter._app.updater.running = False
|
|
recovery = AsyncMock()
|
|
with patch.object(adapter, "_handle_polling_network_error", new=recovery):
|
|
await adapter._probe_pending_updates(adapter._app.bot, 5)
|
|
assert adapter._polling_not_running_count == 1
|
|
await adapter._probe_pending_updates(adapter._app.bot, 5)
|
|
task = adapter._polling_error_task
|
|
assert task is not None
|
|
await task
|
|
recovery.assert_awaited_once()
|
|
assert adapter._polling_not_running_count == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reconnect_in_flight_skips_stopped_updater_escalation():
|
|
"""A stopped updater during an in-flight reconnect must not re-escalate."""
|
|
adapter = _make_adapter(pending=9)
|
|
adapter._app.updater.running = False
|
|
adapter._polling_not_running_count = 1
|
|
inflight = MagicMock()
|
|
inflight.done.return_value = False
|
|
adapter._polling_error_task = inflight
|
|
with patch.object(adapter, "_handle_polling_network_error", new=AsyncMock()) as rec:
|
|
await adapter._probe_pending_updates(adapter._app.bot, 5)
|
|
# The in-flight reconnect owns recovery; the stopped-updater counter resets
|
|
# so the transient stop()->start_polling() window never trips a re-trigger.
|
|
assert adapter._polling_not_running_count == 0
|
|
rec.assert_not_called()
|