Files
hermes-agent/tests/agent/test_reset_aware_primary_restore.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

341 lines
12 KiB
Python

"""Reset-aware primary restore — stay on fallback until the primary's
rate-limit window actually resets.
``restore_primary_runtime`` retries the primary at the top of every turn
once the 60s ``_rate_limited_until`` cooldown clears. For transient 429s
that is correct, but subscription-window limits (Claude Pro/Max 5-hour
windows, ChatGPT weekly caps) report reset times hours or days away. The
credential pool already knows that timestamp (``last_error_reset_at``),
and until it elapses every restore attempt is a guaranteed failure that
invalidates the prompt cache twice per turn (primary → fail → fallback).
The gate must FAIL OPEN: no pool, no reset info, or any error in the gate
falls through to the existing per-turn retry, so recovery can never happen
later than it does today.
"""
import time
from unittest.mock import MagicMock, patch
from run_agent import AIAgent
from agent.credential_pool import (
STATUS_DEAD,
STATUS_EXHAUSTED,
STATUS_OK,
CredentialPool,
PooledCredential,
)
# =============================================================================
# Helpers
# =============================================================================
def _entry(
provider="openrouter",
id="cred-1",
status=STATUS_OK,
reset_at=None,
status_at=None,
error_code=None,
):
return PooledCredential(
provider=provider,
id=id,
label=f"label-{id}",
auth_type="api_key",
priority=1,
source="manual",
access_token="sk-test-1234567890",
last_status=status,
last_status_at=status_at,
last_error_code=error_code,
last_error_reset_at=reset_at,
)
class _FakePool:
"""Minimal stand-in for CredentialPool in agent-level tests."""
def __init__(self, provider, next_at=None, available=False, raise_on_next=False):
self.provider = provider
self._next_at = next_at
self._available = available
self._raise = raise_on_next
self.next_available_calls = 0
def next_available_at(self):
self.next_available_calls += 1
if self._raise:
raise RuntimeError("boom")
return self._next_at
def has_credentials(self):
return True
def has_available(self):
return self._available
def select(self):
return None
def _make_tool_defs(*names):
return [
{
"type": "function",
"function": {
"name": n,
"description": f"{n} tool",
"parameters": {"type": "object", "properties": {}},
},
}
for n in names
]
def _make_agent(fallback_model=None):
with (
patch("model_tools.get_tool_definitions", return_value=_make_tool_defs("web_search")),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.OpenAI"),
):
agent = AIAgent(
api_key="test-key-12345678",
base_url="https://my-llm.example.com/v1",
provider="custom",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
fallback_model=fallback_model,
)
agent.client = MagicMock()
return agent
def _activate_fallback(agent):
mock_client = MagicMock()
mock_client.api_key = "fallback-key-1234"
mock_client.base_url = "https://openrouter.ai/api/v1"
with patch(
"agent.auxiliary_client.resolve_provider_client",
return_value=(mock_client, None),
):
assert agent._try_activate_fallback() is True
assert agent._fallback_activated is True
# =============================================================================
# CredentialPool.next_available_at()
# =============================================================================
class TestNextAvailableAt:
def test_all_exhausted_returns_earliest_reset(self):
now = time.time()
pool = CredentialPool(
"openrouter",
[
_entry(id="a", status=STATUS_EXHAUSTED, reset_at=now + 7200, error_code=429),
_entry(id="b", status=STATUS_EXHAUSTED, reset_at=now + 3600, error_code=429),
],
)
assert pool.next_available_at() == now + 3600
def test_available_entry_returns_none(self):
now = time.time()
pool = CredentialPool(
"openrouter",
[
_entry(id="a", status=STATUS_OK),
_entry(id="b", status=STATUS_EXHAUSTED, reset_at=now + 3600, error_code=429),
],
)
assert pool.next_available_at() is None
def test_elapsed_cooldown_counts_as_available(self):
"""An exhausted entry whose reset time has passed re-enters rotation,
so the pool reports available (None) even without clear_expired."""
now = time.time()
pool = CredentialPool(
"openrouter",
[_entry(id="a", status=STATUS_EXHAUSTED, reset_at=now - 10, error_code=429)],
)
assert pool.next_available_at() is None
def test_exhausted_without_timestamps_returns_none(self):
"""No reset info at all -> None (fail open), not a guess."""
pool = CredentialPool(
"openrouter",
[_entry(id="a", status=STATUS_EXHAUSTED, reset_at=None, status_at=None)],
)
assert pool.next_available_at() is None
def test_exhausted_with_status_at_uses_ttl(self):
"""Without an explicit reset_at, last_status_at + TTL is the estimate."""
now = time.time()
pool = CredentialPool(
"openrouter",
[_entry(id="a", status=STATUS_EXHAUSTED, status_at=now, error_code=429)],
)
result = pool.next_available_at()
assert result is not None
assert result > now
def test_dead_only_returns_none(self):
"""DEAD entries never re-enter via TTL; report no wait info."""
pool = CredentialPool(
"openrouter",
[_entry(id="a", status=STATUS_DEAD, status_at=time.time())],
)
assert pool.next_available_at() is None
def test_empty_pool_returns_none(self):
pool = CredentialPool("openrouter", [])
assert pool.next_available_at() is None
def test_runs_under_the_pool_lock(self):
"""next_available_at must hold self._lock like every other
_available_entries caller — a concurrent select()/rotation can
otherwise tear self._entries mid-iteration (see has_available)."""
pool = CredentialPool("openrouter", [])
held = {}
original = pool._available_entries
def _probe(**kwargs):
# self._lock is an RLock (deferred-refresh mutations self-lock),
# so a same-thread non-blocking acquire always succeeds; probe
# ownership from a helper thread instead.
import threading as _t
blocked = _t.Event()
def _try():
if not pool._lock.acquire(blocking=False):
blocked.set()
else:
pool._lock.release()
worker = _t.Thread(target=_try)
worker.start()
worker.join(timeout=5)
held["locked"] = blocked.is_set()
return original(**kwargs)
pool._available_entries = _probe
pool.next_available_at()
assert held["locked"], "next_available_at called _available_entries without self._lock"
# =============================================================================
# restore_primary_runtime() gate
# =============================================================================
class TestResetAwareRestoreGate:
FB = {"provider": "openrouter", "model": "anthropic/claude-sonnet-4"}
def test_stays_on_fallback_until_reset(self):
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = 0 # 60s transient cooldown already cleared
# Attached pool matches the primary provider and says: nobody can
# serve until an hour from now.
agent._credential_pool = _FakePool("custom", next_at=time.time() + 3600)
assert agent._restore_primary_runtime() is False
assert agent._fallback_activated is True
assert agent.provider == "openrouter"
assert agent.model == "anthropic/claude-sonnet-4"
def test_restores_once_reset_elapsed(self):
agent = _make_agent(fallback_model=self.FB)
original_model = agent.model
_activate_fallback(agent)
agent._rate_limited_until = 0
agent._credential_pool = _FakePool("custom", next_at=None)
with patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
assert agent._restore_primary_runtime() is True
assert agent._fallback_activated is False
assert agent.model == original_model
assert agent.provider == "custom"
def test_past_reset_time_does_not_block(self):
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = 0
agent._credential_pool = _FakePool("custom", next_at=time.time() - 5)
with patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
assert agent._restore_primary_runtime() is True
assert agent._fallback_activated is False
def test_fails_open_on_pool_error(self):
"""Any exception inside the gate must not break restore."""
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = 0
agent._credential_pool = _FakePool("custom", raise_on_next=True)
with patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
assert agent._restore_primary_runtime() is True
assert agent._fallback_activated is False
def test_cross_provider_fallback_loads_primary_pool(self):
"""After a cross-provider fallback the attached pool belongs to the
fallback provider; the gate must consult the PRIMARY's pool."""
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = 0
# Attached pool is the fallback provider's (mismatch with "custom").
agent._credential_pool = _FakePool("openrouter", next_at=None)
primary_pool = _FakePool("custom", next_at=time.time() + 3600)
with patch("agent.credential_pool.load_pool", return_value=primary_pool) as lp:
assert agent._restore_primary_runtime() is False
assert any(c.args == ("custom",) for c in lp.call_args_list)
assert primary_pool.next_available_calls == 1
assert agent._fallback_activated is True
def test_no_pool_info_falls_through(self):
"""Pool present but no reset info -> existing per-turn retry."""
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = 0
agent._credential_pool = _FakePool("custom", next_at=None)
with patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
assert agent._restore_primary_runtime() is True
def test_logs_wait_only_once(self, caplog):
import logging
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = 0
agent._credential_pool = _FakePool("custom", next_at=time.time() + 3600)
with caplog.at_level(logging.INFO, logger="agent.agent_runtime_helpers"):
assert agent._restore_primary_runtime() is False
assert agent._restore_primary_runtime() is False
waits = [r for r in caplog.records if "staying on fallback" in r.getMessage()]
assert len(waits) == 1
def test_transient_cooldown_still_respected(self):
"""The existing 60s monotonic gate fires before the reset-aware one."""
agent = _make_agent(fallback_model=self.FB)
_activate_fallback(agent)
agent._rate_limited_until = time.monotonic() + 60
pool = _FakePool("custom", next_at=None)
agent._credential_pool = pool
assert agent._restore_primary_runtime() is False
# Reset-aware gate never consulted — short-circuited by the 60s gate.
assert pool.next_available_calls == 0