Files
hermes-agent/tests/agent/test_stream_stale_breaker_reset.py
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

188 lines
6.6 KiB
Python

"""Follow-up for the cross-turn stream-stale circuit breaker (#58962).
The breaker latches: once ``_consecutive_stale_streams`` reaches the give-up
threshold, ``interruptible_streaming_api_call`` raises BEFORE any stream is
attempted — so the "reset on successful stream" path can never run again on
its own. The breaker's error message tells the user to "switch models …
then retry", and the provider-fallback chain swaps providers on the same
agent object, so BOTH swap paths must clear the streak or a healthy new
provider would keep short-circuiting forever:
- ``switch_model()`` (user-initiated /model swap)
- ``try_activate_fallback()`` (automatic provider fallback)
- ``restore_primary_runtime()`` (turn-start restore back to the primary)
The non-streaming sibling ``interruptible_api_call`` shares the same
breaker (guard at entry, bump on stale_call_kill, reset on success) —
quiet-mode / subagent sessions take that path and had the identical
infinite stale-retry class.
"""
from unittest.mock import MagicMock, patch
import pytest
from run_agent import AIAgent
def _make_agent_openrouter():
"""Minimal openrouter agent (skips __init__), mirroring
tests/agent/test_switch_model_rollback.py."""
agent = AIAgent.__new__(AIAgent)
agent.provider = "openrouter"
agent.model = "x-ai/grok-4"
agent.base_url = "https://openrouter.ai/api/v1"
agent.api_key = "or-key-original"
agent.api_mode = "chat_completions"
agent.client = MagicMock(name="OriginalClient")
agent._client_kwargs = {
"api_key": "or-key-original",
"base_url": "https://openrouter.ai/api/v1",
}
agent.context_compressor = None
agent._anthropic_api_key = ""
agent._anthropic_base_url = None
agent._anthropic_client = None
agent._is_anthropic_oauth = False
agent._cached_system_prompt = "cached"
agent._primary_runtime = {}
agent._fallback_activated = False
agent._fallback_index = 0
agent._fallback_chain = []
agent._fallback_model = None
agent._config_context_length = None
return agent
def _make_fallback_agent(fallback_model):
"""Full-constructor agent for the fallback path, mirroring
tests/agent/test_fallback_exhaustion_cooldown.py."""
with (
patch("model_tools.get_tool_definitions", return_value=[]),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.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 test_switch_model_resets_stale_streak():
"""A user-initiated /model swap must clear the latched streak so the new
provider gets a real stream attempt instead of an instant short-circuit."""
agent = _make_agent_openrouter()
agent._consecutive_stale_streams = 7 # past any reasonable threshold
agent._create_openai_client = MagicMock(return_value=MagicMock(name="NewClient"))
with patch("hermes_cli.timeouts.get_provider_request_timeout", return_value=None):
agent.switch_model(
new_model="openai/gpt-5",
new_provider="openrouter",
api_key="or-key-new",
base_url="https://openrouter.ai/api/v1",
api_mode="chat_completions",
)
assert agent._consecutive_stale_streams == 0
def test_switch_model_failure_does_not_reset_streak():
"""A failed swap rolls back — the agent is still on the wedged provider,
so the breaker must stay latched (reset happens after the rebuild)."""
agent = _make_agent_openrouter()
agent._consecutive_stale_streams = 7
def boom(*_a, **_kw):
raise RuntimeError("simulated client build failure")
agent._create_openai_client = boom
with patch("hermes_cli.timeouts.get_provider_request_timeout", return_value=None):
try:
agent.switch_model(
new_model="openai/gpt-5",
new_provider="openrouter",
api_key="or-key-new",
base_url="https://openrouter.ai/api/v1",
api_mode="chat_completions",
)
except RuntimeError:
pass
assert agent._consecutive_stale_streams == 7
def test_fallback_activation_resets_stale_streak():
"""Automatic provider fallback swaps to a different backend; the streak
measured the OLD provider and must not wedge the new one."""
fbs = [{"provider": "openai", "model": "gpt-4o"}]
agent = _make_fallback_agent(fallback_model=fbs)
agent._consecutive_stale_streams = 7
with patch(
"agent.auxiliary_client.resolve_provider_client",
return_value=(_mock_client(), "resolved"),
):
assert agent._try_activate_fallback() is True
assert agent._consecutive_stale_streams == 0
def test_fallback_exhaustion_keeps_stale_streak():
"""When the chain is exhausted (no swap happened), the streak stays
latched — the session is still wedged on the same provider."""
agent = _make_fallback_agent(fallback_model=[])
agent._consecutive_stale_streams = 7
assert agent._try_activate_fallback() is False
assert agent._consecutive_stale_streams == 7
class TestNonStreamingSibling:
"""interruptible_api_call carries the same breaker (#58962)."""
def test_non_streaming_short_circuits_at_threshold(self, monkeypatch):
monkeypatch.setenv("HERMES_STREAM_STALE_GIVEUP", "3")
agent = _make_fallback_agent(fallback_model=[])
agent._consecutive_stale_streams = 3
with pytest.raises(RuntimeError, match="unresponsive"):
agent._interruptible_api_call({})
# The client is never touched on the short-circuit path.
agent.client.chat.completions.create.assert_not_called()
assert agent._consecutive_stale_streams == 3
def test_non_streaming_success_resets_streak(self, monkeypatch):
monkeypatch.setenv("HERMES_STREAM_STALE_GIVEUP", "3")
agent = _make_fallback_agent(fallback_model=[])
agent._consecutive_stale_streams = 2 # below threshold
agent.client.chat.completions.create.return_value = MagicMock(
name="resp", choices=[MagicMock()]
)
resp = agent._interruptible_api_call({"model": "m", "messages": []})
assert resp is not None
assert agent._consecutive_stale_streams == 0