mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-06-03 07:21:54 +00:00
Remove unused imports (F401) and duplicate/shadowed import redefinitions (F811) across the codebase using ruff's safe autofixes. No behavioral changes -- imports only. - ~1400 safe autofixes applied across 644 files (net -1072 lines) - __init__.py re-exports preserved (excluded from F401 removal so public re-export surfaces stay intact) - Re-exports that are imported or monkeypatched by tests but look unused in their defining module are kept with explicit # noqa: F401 (gateway/run.py load_dotenv; run_agent re-exports from agent.message_sanitization, agent.context_compressor, agent.retry_utils, agent.prompt_builder, agent.process_bootstrap, agent.codex_responses_adapter) - Unsafe F841 (unused-variable) fixes deliberately skipped -- those can change behavior when the RHS has side effects - ruff lints remain disabled in pyproject.toml (only PLW1514 is selected); this is a one-time cleanup, not a config change Verification: - python -m compileall: clean - pytest --collect-only: all 27161 tests collect (zero import errors) - core entry points import clean (run_agent, model_tools, cli, toolsets, hermes_state, batch_runner, gateway) - static scan: every name any test imports directly from an edited module still resolves
89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
"""TelegramAdapter send-path health gating after reconnect storms.
|
|
|
|
After sustained Bad Gateway / TimedOut reconnect cycles, the PTB httpx client
|
|
can enter a wedged state where ``bot.send_message()`` returns a valid Message
|
|
but nothing reaches the recipient. ``_send_path_degraded`` short-circuits
|
|
``send()`` so cron's live-adapter branch falls through to standalone HTTP.
|
|
"""
|
|
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 gateway.platforms.telegram import TelegramAdapter # noqa: E402
|
|
|
|
|
|
def _make_adapter() -> TelegramAdapter:
|
|
adapter = TelegramAdapter(PlatformConfig(enabled=True, token="***"))
|
|
adapter._bot = MagicMock()
|
|
adapter._bot.send_message = AsyncMock(return_value=MagicMock(message_id=42))
|
|
return adapter
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_succeeds_when_path_healthy():
|
|
"""Healthy adapter delivers normally; send_message is called."""
|
|
adapter = _make_adapter()
|
|
assert adapter._send_path_degraded is False
|
|
|
|
result = await adapter.send("123", "hello")
|
|
|
|
assert result.success is True
|
|
adapter._bot.send_message.assert_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_short_circuits_when_path_degraded():
|
|
"""Degraded adapter returns failure WITHOUT calling send_message,
|
|
so cron's live-adapter branch falls through to standalone HTTP."""
|
|
adapter = _make_adapter()
|
|
adapter._send_path_degraded = True
|
|
|
|
result = await adapter.send("123", "hello")
|
|
|
|
assert result.success is False
|
|
assert result.error == "send_path_degraded"
|
|
assert result.retryable is True
|
|
adapter._bot.send_message.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reconnect_storm_sets_and_heartbeat_clears_flag(monkeypatch):
|
|
"""_handle_polling_network_error sets the flag; a successful heartbeat
|
|
probe in _verify_polling_after_reconnect clears it."""
|
|
adapter = _make_adapter()
|
|
adapter._app = MagicMock()
|
|
adapter._app.updater = MagicMock()
|
|
adapter._app.updater.running = True
|
|
adapter._app.updater.stop = AsyncMock()
|
|
adapter._app.updater.start_polling = AsyncMock()
|
|
adapter._app.bot = MagicMock()
|
|
adapter._app.bot.get_me = AsyncMock(return_value=MagicMock())
|
|
adapter._polling_error_callback_ref = AsyncMock()
|
|
monkeypatch.setattr(
|
|
"gateway.platforms.telegram.Update", MagicMock(ALL_TYPES=[])
|
|
)
|
|
|
|
await adapter._handle_polling_network_error(OSError("Bad Gateway"))
|
|
assert adapter._send_path_degraded is True
|
|
|
|
with patch("gateway.platforms.telegram.asyncio.sleep", new_callable=AsyncMock):
|
|
await adapter._verify_polling_after_reconnect()
|
|
assert adapter._send_path_degraded is False
|