hermes-agent/tests/tui_gateway/test_resume_switched_provider_endpoint.py
teknium1 33c6ab002d fix(tui_gateway): resume on the route in model_config.gateway_runtime (#125942, salvage #125956)
tui_gateway/server.py::_stored_session_runtime_overrides read only the top-level model_config
keys and then fell back to billing_provider (frozen at the first accounted call), never the
nested gateway_runtime dict gateway/run_turn.py persists per turn — so a Telegram-driven session
resumed on Desktop paired the current model with the first-call provider and 404'd. Route the
Desktop/TUI reader through the canonical SessionDB.session_gateway_runtime (nested gateway_runtime,
then top-level TUI keys, then billing_provider), the same reader CLI --resume uses; the reader now
fills a missing top-level provider from billing_provider so the pre-existing TUI contract
(top-level base_url/api_mode + billing_provider) is unchanged. Redone slim from #125956, which
kept a second hand-rolled reader and inverted the precedence.

red-on-base: tests/tui_gateway/test_resume_switched_provider_endpoint.py::
test_resume_honors_gateway_runtime_over_first_call_billing_provider and
test_resume_prefers_gateway_runtime_over_stale_top_level_keys fail on origin/main with
`assert ('anthropic', None, None) == ('openai-codex', None, 'codex_responses')`; green-on-head.
2026-09-28 03:13:19 -07:00

56 lines
3.3 KiB
Python

"""A chat switched to another provider must resume on THAT provider's endpoint.
The messaging gateway's /model wrote only ``model``/``provider`` over a row the Desktop had persisted
with the Nous Portal route, so resuming a chat switched to openai-codex sent the Codex slug to the
Portal's chat/completions ("Model 'gpt-6-luna-900k' isn't available on ChatGPT or Codex").
"""
import json
import pytest
from hermes_cli.cli_model_switch_mixin import stored_session_route
from hermes_state import SessionDB
from tui_gateway.server import _stored_session_runtime_overrides
NOUS_ROUTE = {"base_url": "https://inference-api.nousresearch.com/v1", "api_mode": "chat_completions"}
@pytest.mark.parametrize("row_origin", ["gateway_model_switch", "row_written_by_older_build"])
def test_resume_drops_previous_providers_endpoint(tmp_path, row_origin):
db = SessionDB(db_path=tmp_path / "state.db")
db.create_session("s1", source="telegram", model="openai/gpt-6-luna")
if row_origin == "gateway_model_switch":
db.update_session_meta("s1", json.dumps({"model": "openai/gpt-6-luna", "provider": "nous", **NOUS_ROUTE}),
"openai/gpt-6-luna")
db.update_session_model("s1", "gpt-6-luna-900k", provider="openai-codex")
else:
db.update_session_meta("s1", json.dumps({"model": "gpt-6-luna-900k", "provider": "openai-codex", **NOUS_ROUTE}),
"gpt-6-luna-900k")
row = db.get_session("s1")
desktop = _stored_session_runtime_overrides(row)["model_override"]
assert (desktop["provider"], desktop["base_url"], desktop["api_mode"]) == ("openai-codex", None, None)
assert stored_session_route(row, current_model="openai/gpt-6-luna", current_provider="nous") == (
"gpt-6-luna-900k", "openai-codex", None, None, True)
def test_resume_honors_gateway_runtime_over_first_call_billing_provider():
"""The messaging gateway persists the route it last ran only in ``model_config.gateway_runtime``;
``billing_provider`` is frozen at the first accounted call. Desktop resume must read the nested
route like CLI ``--resume`` does, not recombine the current model with the stale bucket (#125942)."""
row = {"model": "gpt-6-sol", "billing_provider": "anthropic", "model_config": json.dumps(
{"gateway_runtime": {"provider": "openai-codex", "base_url": None, "api_mode": "codex_responses"}})}
desktop = _stored_session_runtime_overrides(row)["model_override"]
assert (desktop["provider"], desktop["base_url"], desktop["api_mode"]) == ("openai-codex", None, "codex_responses")
assert stored_session_route(row, current_model="gpt-6-sol", current_provider="anthropic")[1:4] == (
"openai-codex", None, "codex_responses")
def test_resume_prefers_gateway_runtime_over_stale_top_level_keys():
"""A gateway turn after a Desktop pick rewrites only ``gateway_runtime``, so the nested route is the
newest one — same precedence as ``SessionDB.session_gateway_runtime``."""
row = {"model": "gpt-6-sol", "model_config": json.dumps(
{"provider": "anthropic", **NOUS_ROUTE, "gateway_runtime": {"provider": "openai-codex", "api_mode": "codex_responses"}})}
desktop = _stored_session_runtime_overrides(row)["model_override"]
assert (desktop["provider"], desktop["base_url"], desktop["api_mode"]) == ("openai-codex", None, "codex_responses")