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.
147 lines
5.5 KiB
Python
147 lines
5.5 KiB
Python
"""Tests for confirmed-dead delivery-target short-circuiting (deleted Telegram
|
|
groups, blocked/kicked bots, deactivated users).
|
|
|
|
Covers the full lifecycle through the real ``DeliveryRouter.deliver()`` path:
|
|
forbidden send -> target marked dead
|
|
next delivery -> short-circuited (adapter never called)
|
|
successful send -> dead flag cleared (self-healing)
|
|
|
|
and the standalone ``DeadTargetRegistry`` persistence/classification contract.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from gateway.config import GatewayConfig, Platform
|
|
from gateway.delivery import DeliveryRouter, DeliveryTarget
|
|
from gateway.dead_targets import DeadTargetRegistry
|
|
|
|
|
|
class ForbiddenThenOkAdapter:
|
|
"""First send raises a deleted-group Forbidden; subsequent sends succeed."""
|
|
|
|
def __init__(self, fail_times=1):
|
|
self.calls = []
|
|
self._fail_times = fail_times
|
|
|
|
async def send(self, chat_id, content, metadata=None):
|
|
self.calls.append(chat_id)
|
|
if len(self.calls) <= self._fail_times:
|
|
raise RuntimeError("Forbidden: the group chat was deleted")
|
|
return {"success": True}
|
|
|
|
|
|
class TransientFailAdapter:
|
|
async def send(self, chat_id, content, metadata=None):
|
|
raise RuntimeError("httpx.ReadTimeout: connection timed out")
|
|
|
|
|
|
@pytest.fixture
|
|
def isolate(tmp_path, monkeypatch):
|
|
monkeypatch.setattr("gateway.delivery.get_hermes_home", lambda: tmp_path)
|
|
monkeypatch.setattr("gateway.dead_targets.get_hermes_home", lambda: tmp_path)
|
|
return tmp_path
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# DeadTargetRegistry unit contract
|
|
# --------------------------------------------------------------------------
|
|
|
|
class TestDeadTargetRegistry:
|
|
def test_mark_is_dead_clear_roundtrip(self, isolate):
|
|
reg = DeadTargetRegistry()
|
|
assert reg.is_dead("telegram", "123") is False
|
|
assert reg.mark_dead("telegram", "123", "forbidden") is True
|
|
assert reg.is_dead("telegram", "123") is True
|
|
# idempotent: second mark returns False (already present)
|
|
assert reg.mark_dead("telegram", "123", "forbidden") is False
|
|
assert reg.clear("telegram", "123") is True
|
|
assert reg.is_dead("telegram", "123") is False
|
|
|
|
def test_persists_across_instances(self, isolate):
|
|
reg = DeadTargetRegistry()
|
|
reg.mark_dead("telegram", "999", "deleted group")
|
|
# New instance reads the same on-disk store under tmp HERMES_HOME.
|
|
reg2 = DeadTargetRegistry()
|
|
assert reg2.is_dead("telegram", "999") is True
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# DeliveryRouter end-to-end lifecycle
|
|
# --------------------------------------------------------------------------
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_forbidden_marks_target_dead_then_short_circuits(isolate):
|
|
adapter = ForbiddenThenOkAdapter(fail_times=99)
|
|
router = DeliveryRouter(GatewayConfig(), adapters={Platform.TELEGRAM: adapter})
|
|
target = DeliveryTarget.parse("telegram:42")
|
|
|
|
# First delivery: send raises Forbidden -> failure + target recorded dead.
|
|
res1 = await router.deliver("hi", [target])
|
|
assert res1["telegram:42"]["success"] is False
|
|
assert router.dead_targets.is_dead("telegram", "42") is True
|
|
assert adapter.calls == ["42"] # adapter was invoked once
|
|
|
|
# Second delivery: short-circuited, adapter NOT called again.
|
|
res2 = await router.deliver("hi again", [target])
|
|
assert res2["telegram:42"]["skipped"] == "dead_target"
|
|
assert res2["telegram:42"]["success"] is False
|
|
assert adapter.calls == ["42"] # still only the original call
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_shared_registry_is_used_when_injected(isolate):
|
|
shared = DeadTargetRegistry()
|
|
shared.mark_dead("telegram", "500", "pre-existing")
|
|
adapter = ForbiddenThenOkAdapter(fail_times=0)
|
|
router = DeliveryRouter(
|
|
GatewayConfig(),
|
|
adapters={Platform.TELEGRAM: adapter},
|
|
dead_targets=shared,
|
|
)
|
|
target = DeliveryTarget.parse("telegram:500")
|
|
res = await router.deliver("hi", [target])
|
|
# Injected registry's pre-existing flag short-circuits before any send.
|
|
assert res["telegram:500"]["skipped"] == "dead_target"
|
|
assert adapter.calls == []
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# not_found blast radius: chat-level kills the chat, thread/message-level must not
|
|
# --------------------------------------------------------------------------
|
|
|
|
class RaisingAdapter:
|
|
"""Raises a fixed error message on every send."""
|
|
|
|
def __init__(self, message):
|
|
self.message = message
|
|
self.calls = []
|
|
|
|
async def send(self, chat_id, content, metadata=None):
|
|
self.calls.append(chat_id)
|
|
raise RuntimeError(self.message)
|
|
|
|
|
|
_SUBCHAT_NOT_FOUND_MESSAGES = [
|
|
"Bad Request: message thread not found",
|
|
"Bad Request: TOPIC_DELETED",
|
|
"Bad Request: message to edit not found",
|
|
"Bad Request: message to reply not found",
|
|
"Bad Request: MESSAGE_ID_INVALID",
|
|
]
|
|
|
|
|
|
class TestNotFoundBlastRadius:
|
|
|
|
@pytest.mark.parametrize("message", _SUBCHAT_NOT_FOUND_MESSAGES)
|
|
def test_is_chat_level_not_found_subchat(self, message):
|
|
from gateway.platforms.base import is_chat_level_not_found
|
|
|
|
assert is_chat_level_not_found(error_text=message) is False
|
|
|
|
def test_subchat_marker_wins_when_both_present(self):
|
|
from gateway.platforms.base import is_chat_level_not_found
|
|
|
|
# Conservative: if a sub-chat marker is present, never kill the whole chat.
|
|
assert is_chat_level_not_found(error_text="chat not found; message thread not found") is False
|
|
|
|
|