mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
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).
123 lines
4.8 KiB
Python
123 lines
4.8 KiB
Python
"""Auth-failure provider failover (conversation loop).
|
|
|
|
A 401/403 that survives the per-provider credential-refresh attempt
|
|
(revoked OAuth, blocked/expired key, an account pinned to a dead/staging
|
|
endpoint) must escalate to the configured fallback chain instead of
|
|
thrashing on the same dead credential every turn.
|
|
|
|
Before the fix, the conversation loop's generic failover dispatch only
|
|
fired for ``{rate_limit, billing}`` reasons; ``auth`` / ``auth_permanent``
|
|
fell through to "switch providers manually" advice and never called
|
|
``_try_activate_fallback()``. These tests pin:
|
|
|
|
1. 401/403 classify as auth (``classified.is_auth`` True).
|
|
2. ``_try_activate_fallback`` advances the chain on an auth reason.
|
|
3. The one-shot guard flag exists on TurnRetryState.
|
|
"""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from run_agent import AIAgent
|
|
from agent.error_classifier import classify_api_error, FailoverReason
|
|
from agent.turn_retry_state import TurnRetryState
|
|
|
|
|
|
def _make_agent(fallback_model=None):
|
|
with (
|
|
patch("run_agent.get_tool_definitions", return_value=[]),
|
|
patch("run_agent.check_toolset_requirements", return_value={}),
|
|
patch("run_agent.OpenAI"),
|
|
):
|
|
agent = AIAgent(
|
|
api_key="test-key",
|
|
base_url="https://openrouter.ai/api/v1",
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
fallback_model=fallback_model,
|
|
)
|
|
agent.client = MagicMock()
|
|
return agent
|
|
|
|
|
|
def _mock_client(base_url="https://openrouter.ai/api/v1", api_key="fb-key"):
|
|
mock = MagicMock()
|
|
mock.base_url = base_url
|
|
mock.api_key = api_key
|
|
return mock
|
|
|
|
|
|
def _auth_error(status=401, msg="Your API key is invalid, blocked or out of funds."):
|
|
err = Exception(f"Error code: {status} - {msg}")
|
|
err.status_code = status
|
|
return err
|
|
|
|
|
|
class TestAuthErrorClassification:
|
|
def test_401_is_auth(self):
|
|
c = classify_api_error(_auth_error(401))
|
|
assert c.reason in {FailoverReason.auth, FailoverReason.auth_permanent}
|
|
assert c.is_auth is True
|
|
|
|
|
|
def test_500_is_not_auth(self):
|
|
err = Exception("Error code: 500 - internal server error")
|
|
err.status_code = 500
|
|
c = classify_api_error(err)
|
|
assert c.is_auth is False
|
|
|
|
|
|
class TestAuthFailoverGuardFlag:
|
|
def test_flag_defaults_false(self):
|
|
assert TurnRetryState().auth_failover_attempted is False
|
|
|
|
|
|
class TestAuthFailoverActivation:
|
|
"""The decision the loop makes on a persistent auth failure: when a
|
|
fallback chain exists and the guard hasn't fired, escalate to it."""
|
|
|
|
def _should_failover(self, agent, classified, retry):
|
|
# Mirror the exact gating condition added to conversation_loop.py.
|
|
return (
|
|
classified.is_auth
|
|
and not retry.auth_failover_attempted
|
|
and agent._fallback_index < len(agent._fallback_chain)
|
|
)
|
|
|
|
def test_auth_failover_fires_when_chain_present(self):
|
|
agent = _make_agent(fallback_model=[{"provider": "openai", "model": "gpt-4o"}])
|
|
retry = TurnRetryState()
|
|
classified = classify_api_error(_auth_error(401))
|
|
assert self._should_failover(agent, classified, retry) is True
|
|
# And the activation primitive actually advances on an auth reason.
|
|
with patch(
|
|
"agent.auxiliary_client.resolve_provider_client",
|
|
return_value=(_mock_client(), "gpt-4o"),
|
|
):
|
|
advanced = agent._try_activate_fallback(reason=classified.reason)
|
|
assert advanced is True
|
|
assert agent._fallback_index == 1
|
|
|
|
def test_no_failover_without_chain(self):
|
|
"""A user with no fallback configured (the common case for the
|
|
original incident) does NOT failover — falls through to the
|
|
existing terminal handling + troubleshooting advice."""
|
|
agent = _make_agent(fallback_model=None)
|
|
retry = TurnRetryState()
|
|
classified = classify_api_error(_auth_error(401))
|
|
assert self._should_failover(agent, classified, retry) is False
|
|
|
|
def test_guard_blocks_repeat_failover(self):
|
|
agent = _make_agent(fallback_model=[{"provider": "openai", "model": "gpt-4o"}])
|
|
retry = TurnRetryState()
|
|
retry.auth_failover_attempted = True # already escalated this attempt
|
|
classified = classify_api_error(_auth_error(401))
|
|
assert self._should_failover(agent, classified, retry) is False
|
|
|
|
def test_non_auth_error_does_not_trigger_auth_failover(self):
|
|
agent = _make_agent(fallback_model=[{"provider": "openai", "model": "gpt-4o"}])
|
|
retry = TurnRetryState()
|
|
err = Exception("Error code: 500 - internal server error")
|
|
err.status_code = 500
|
|
classified = classify_api_error(err)
|
|
assert self._should_failover(agent, classified, retry) is False
|