hermes-agent/tests/tui_gateway/test_fallback_chain_hot_reload.py
teknium1 064c847516 fix(tui_gateway,cli): per-turn fallback sync fails closed on a torn config; classic CLI syncs too (#95066)
Review follow-up. `_sync_agent_fallback_with_config` read the chain through `_load_cfg()`, which
fails open to `{}` on a torn/invalid config.yaml — `get_fallback_chain({}) == []` then WIPED the
live agent's chain (the messaging gateway's `_refresh_fallback_model` keeps its last known-good
chain). The sync now reads `load_user_config_effective(fail_closed=True)` and skips the apply when
the read fails, so only a config that actually loaded can remove a chain.

The wiring test drives `_prepare_turn_input` (turn admission) instead of string-matching the
prompt_turn source, and covers removal and the active-fallback cooldown skip.

Sibling surface: the classic interactive CLI holds one long-lived agent and read the chain once in
`HermesCLI.__init__`; `HermesCLI.chat` now applies the same fail-closed per-turn sync (and refreshes
`self._fallback_model` so a rebuilt agent starts from the fresh chain). Docs table caveat added.
2026-09-19 01:43:15 -07:00

83 lines
3.5 KiB
Python

"""Desktop/TUI sessions must adopt a ``fallback_providers`` chain added after the chat was opened.
Regression for #95066: ``_make_agent`` read the chain once, so a session born before ``hermes fallback
add`` kept an empty ``_fallback_chain`` forever and a Codex ``usage_limit_reached`` 429 ended in a
provider error instead of switching to the configured fallback.
Both tests drive ``_prepare_turn_input`` (turn admission, the production entry) up to the sync's
successor so the wiring — not just the helper — is pinned.
"""
from __future__ import annotations
import time
from types import SimpleNamespace
import pytest
from tui_gateway import server
FALLBACK = [{"provider": "xai-oauth", "model": "grok-4.6"}]
class _StopAfterSync(Exception):
pass
def _session(chain=None):
agent = SimpleNamespace(
_fallback_chain=list(chain or []), _fallback_model=(chain or [None])[0], _fallback_index=0,
_fallback_activated=False, _rate_limited_until=0, _unavailable_fallback_keys=set(),
)
return {"agent": agent, "session_key": "session-95066"}, agent
def _admit_turn(monkeypatch, tmp_path, session, config_text: str) -> None:
"""Run turn admission against ``config_text`` as the live config.yaml; stop right after the sync."""
cfg_path = tmp_path / "config.yaml"
cfg_path.write_text(config_text, encoding="utf-8")
monkeypatch.setattr(server, "_active_config_path", lambda: cfg_path)
monkeypatch.setattr(server, "_profile_runtime_scope_tokens", lambda profile_home: None)
monkeypatch.setattr(server, "_set_session_context", lambda *a, **k: [])
monkeypatch.setattr(server, "_wire_callbacks", lambda sid: None)
for name in ("_apply_pending_model_switch", "_sync_agent_model_with_config", "_sync_agent_compression_with_config"):
monkeypatch.setattr(server, name, lambda sid, session: None)
def stop(sid, session):
raise _StopAfterSync()
monkeypatch.setattr(server, "_sync_bot_capabilities", stop)
st = server._TurnRun(agent=None, one_turn_restore=None, terminal_callback=None, receipt_committed=False)
with pytest.raises(_StopAfterSync):
server._prepare_turn_input("sid", session, st, "hello", [])
def test_chain_added_after_open_reaches_the_live_agent_at_turn_admission(monkeypatch, tmp_path):
session, agent = _session()
_admit_turn(monkeypatch, tmp_path, session,
"fallback_providers:\n - provider: xai-oauth\n model: grok-4.6\n")
assert agent._fallback_chain == FALLBACK
assert agent._fallback_model == FALLBACK[0]
assert agent._fallback_index == 0
def test_torn_config_keeps_the_last_known_good_chain_but_removal_still_applies(monkeypatch, tmp_path):
session, agent = _session(FALLBACK)
# Torn mid-edit write: an unparsable config.yaml must NOT read as "chain removed".
_admit_turn(monkeypatch, tmp_path, session, "fallback_providers: [\n - provider: {{{\n")
assert agent._fallback_chain == FALLBACK
assert agent._fallback_model == FALLBACK[0]
# Control: a valid config with the chain removed clears it on the next turn.
_admit_turn(monkeypatch, tmp_path, session, "model:\n provider: openai\n")
assert agent._fallback_chain == []
assert agent._fallback_model is None
# While a cooldown holds the agent on an activated fallback, the sync leaves the chain alone.
agent._fallback_chain, agent._fallback_activated = list(FALLBACK), True
agent._rate_limited_until = time.monotonic() + 600
_admit_turn(monkeypatch, tmp_path, session, "model:\n provider: openai\n")
assert agent._fallback_chain == FALLBACK