hermes-agent/tests/tui_gateway/test_pre_agent_fallback_notice.py
teknium1 e7cc3836e1 fix: TUI auth fallback no longer crashes on model: <id> config shorthand
`_resolve_agent_model_runtime` builds the pre-agent fallback notice from
`(_load_cfg().get("model") or {}).get("provider")`. `load_user_config_effective`
keeps the `model: <id>` string shorthand as a string (every other reader —
gateway/run.py, cron/scheduler.py, hermes_cli/main.py — accepts it), so a
primary AuthError on such a config raised `'str' object has no attribute
'get'` inside the fallback branch: `setup.runtime_check` answered ok=False
("'str' object has no attribute 'get'") and `_make_agent` failed, on a
backend whose configured fallback chain was perfectly usable.

Repro: `tests/tui_gateway/test_pre_agent_fallback_notice.py::
test_fallback_build_survives_model_string_shorthand` (red on base).
Surfaced as the cross-file leak `test_protocol.py::test_config_roundtrip`
(writes `model: test/model` into a home left bound on `server._hermes_home`)
→ `test_tui_gateway_server.py::test_setup_runtime_check_agrees_with_
session_fallback_chain`.
2026-09-23 21:10:27 -07:00

73 lines
3.6 KiB
Python

"""A TUI/Desktop agent built on a credential-resolution fallback (primary AuthError -> fallback_providers
entry) must carry the same one-shot switch notice the messaging gateway surfaces (#74349), and the
private notice key must never reach the AIAgent constructor. Drives the real ``_make_agent``."""
from unittest.mock import patch
from hermes_cli.auth import AuthError
class _StubAgent:
def __init__(self, **kwargs):
self.kwargs = kwargs
def _build(monkeypatch, tmp_path, *, primary_fails: bool):
from tui_gateway import server
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.setenv("HERMES_IGNORE_RULES", "1")
(tmp_path / "config.yaml").write_text("model:\n default: gpt-5.6-sol\n provider: openai-codex\n")
monkeypatch.setattr(server, "_hermes_home", tmp_path)
monkeypatch.setattr(server, "_get_db", lambda: None)
monkeypatch.setattr(server, "_load_fallback_model",
lambda: [{"provider": "anthropic", "model": "claude-sonnet-5", "api_key": "fb"}])
def _resolve(**kwargs):
if primary_fails and kwargs.get("requested") != "anthropic":
raise AuthError("expired")
return {"api_key": "k", "base_url": "https://example.invalid/v1",
"provider": kwargs.get("requested") or "openai-codex", "api_mode": "chat_completions"}
with patch("hermes_cli.runtime_provider.resolve_runtime_provider", side_effect=_resolve), \
patch("run_agent.AIAgent", _StubAgent):
return server._make_agent("sid", "key", context_cwd_is_launch_artifact=False)
def test_fallback_build_attaches_the_switch_notice_outside_agent_kwargs(monkeypatch, tmp_path):
agent = _build(monkeypatch, tmp_path, primary_fails=True)
assert (agent.kwargs["provider"], agent.kwargs["model"]) == ("anthropic", "claude-sonnet-5")
assert "_fallback_notice" not in agent.kwargs
notice = agent._pending_fallback_notice
assert "openai-codex/gpt-5.6-sol" in notice and "anthropic/claude-sonnet-5" in notice
def test_primary_build_carries_no_notice(monkeypatch, tmp_path):
agent = _build(monkeypatch, tmp_path, primary_fails=False)
assert agent.kwargs["provider"] == "openai-codex"
assert not hasattr(agent, "_pending_fallback_notice")
def test_fallback_build_survives_model_string_shorthand(monkeypatch, tmp_path):
"""``model: <id>`` (the string shorthand every other reader accepts) must not crash the fallback
build while it names the primary in the notice."""
from tui_gateway import server
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.setenv("HERMES_IGNORE_RULES", "1")
(tmp_path / "config.yaml").write_text("model: gpt-5.6-sol\n", encoding="utf-8")
monkeypatch.setattr(server, "_hermes_home", tmp_path)
monkeypatch.setattr(server, "_get_db", lambda: None)
monkeypatch.setattr(server, "_load_fallback_model",
lambda: [{"provider": "anthropic", "model": "claude-sonnet-5", "api_key": "fb"}])
def _resolve(**kwargs):
if kwargs.get("requested") != "anthropic":
raise AuthError("expired")
return {"api_key": "k", "base_url": "https://example.invalid/v1",
"provider": "anthropic", "api_mode": "chat_completions"}
with patch("hermes_cli.runtime_provider.resolve_runtime_provider", side_effect=_resolve), \
patch("run_agent.AIAgent", _StubAgent):
agent = server._make_agent("sid", "key", context_cwd_is_launch_artifact=False)
assert (agent.kwargs["provider"], agent.kwargs["model"]) == ("anthropic", "claude-sonnet-5")
assert "gpt-5.6-sol" in agent._pending_fallback_notice and "anthropic/claude-sonnet-5" in agent._pending_fallback_notice