Skip to content

The agent that grows with you

fix(sessions): keep a resumed route off endpoints it doesn't own

emozilla7ea6ca730642
diff --git a/hermes_cli/cli_model_switch_mixin.py b/hermes_cli/cli_model_switch_mixin.py
index 3783a99ad89..fc5915703b3 100644
--- a/hermes_cli/cli_model_switch_mixin.py
+++ b/hermes_cli/cli_model_switch_mixin.py
@@ -58,11 +58,18 @@ def stored_session_route(session_meta, *, current_model, current_provider):
from hermes_state import SessionDB as _SessionDB
runtime = _SessionDB.session_gateway_runtime(session_meta)
base_url = runtime.get("base_url") or None
- provider = _heal_bare_custom_provider(runtime.get("provider") or None, base_url=base_url, model=stored_model)
+ api_mode = runtime.get("api_mode") or None
+ stored_provider = runtime.get("provider") or None
+ provider = _heal_bare_custom_provider(stored_provider, base_url=base_url, model=stored_model)
+ if provider != stored_provider:
+ # A healed entry resolves its own current endpoint. A route that didn't heal is gone with its
+ # endpoint and wire, which must not ride on the ambient provider.
+ base_url = None
+ if provider is None:
+ api_mode = None
provider_changed = bool(provider) and provider != current_provider
if stored_model == current_model and not provider_changed:
return None
- api_mode = runtime.get("api_mode") or None
from hermes_cli.runtime_provider import is_foreign_provider_endpoint
if is_foreign_provider_endpoint(provider, base_url):
# The endpoint and its wire belong to the provider this chat left; resolve the stored one's own.
diff --git a/tests/hermes_cli/test_resume_model_restore.py b/tests/hermes_cli/test_resume_model_restore.py
index a696275719a..49b52a8de54 100644
--- a/tests/hermes_cli/test_resume_model_restore.py
+++ b/tests/hermes_cli/test_resume_model_restore.py
@@ -481,6 +481,44 @@ def test_session_gateway_runtime_bare_custom_falls_back_to_billing_base_url():
assert SessionDB.session_gateway_runtime(meta) == {}
+GLM_OLD, GLM_NEW = "http://10.0.0.5:8000/v1", "http://10.0.0.6:8000/v1"
+
+
+def _billing_only_meta():
+ meta = _row(model="GLM-5.3", model_config={"api_mode": "chat_completions"})
+ meta["billing_provider"] = "custom"
+ meta["billing_base_url"] = GLM_OLD
+ return meta
+
+
+def _custom_config(monkeypatch, *entries):
+ import hermes_cli.runtime_provider as rp
+ config = {"model": {"provider": "anthropic", "default": "claude-opus-5-5"}, "custom_providers": list(entries)}
+ monkeypatch.setattr(rp, "load_config", lambda: config)
+ monkeypatch.setattr(rp, "_get_model_config", lambda: config["model"])
+
+
+def test_stored_session_route_follows_a_custom_entry_that_moved(monkeypatch):
+ """The entry serving the chat's model now lives at a new URL: resume runs on the entry, which
+ resolves its own endpoint, not on the URL the chat last billed."""
+ from hermes_cli.cli_model_switch_mixin import stored_session_route
+ _custom_config(monkeypatch, {"name": "glm", "base_url": GLM_NEW, "api_key": "glm-key", "model": "GLM-5.3"})
+
+ route = stored_session_route(_billing_only_meta(), current_model="claude-opus-5-5", current_provider="anthropic")
+
+ assert route[:3] == ("GLM-5.3", "custom:glm", None)
+ assert route[4] is True
+
+
+def test_stored_session_route_drops_an_endpoint_no_entry_serves(monkeypatch):
+ from hermes_cli.cli_model_switch_mixin import stored_session_route
+ _custom_config(monkeypatch)
+
+ route = stored_session_route(_billing_only_meta(), current_model="claude-opus-5-5", current_provider="anthropic")
+
+ assert route == ("GLM-5.3", None, None, None, False)
+
+
def test_session_gateway_runtime_bare_auto_ignores_billing_base_url():
"""auto bills through the router — no attributable endpoint, stays filtered."""
meta = _row()
diff --git a/tests/tui_gateway/test_local_model_session_identity.py b/tests/tui_gateway/test_local_model_session_identity.py
index 56cb85cfd05..28c78748750 100644
--- a/tests/tui_gateway/test_local_model_session_identity.py
+++ b/tests/tui_gateway/test_local_model_session_identity.py
@@ -135,3 +135,38 @@ def test_resume_recovers_a_local_chat_that_recorded_only_its_billing_endpoint(lo
assert overrides.get("provider_override") == "llamacpp"
assert restored_model == model
assert restored["base_url"] == route["base_url"]
+
+
+GLM_OLD, GLM_NEW = "http://10.0.0.5:8000/v1", "http://10.0.0.6:8000/v1"
+
+
+def _billing_only_row(base_url):
+ return {"model": "GLM-5.3", "model_config": {"max_iterations": 60, "api_mode": "chat_completions"},
+ "billing_provider": "custom", "billing_base_url": base_url}
+
+
+def _custom_config(monkeypatch, *entries):
+ config = {"model": {"provider": "anthropic", "default": "claude-opus-5-5"}, "custom_providers": list(entries)}
+ monkeypatch.setattr(rp, "load_config", lambda: config)
+ monkeypatch.setattr(rp, "_get_model_config", lambda: config["model"])
+
+
+def test_resume_follows_a_custom_entry_that_moved_since_the_chat(monkeypatch):
+ _custom_config(monkeypatch, {"name": "glm", "base_url": GLM_NEW, "api_key": "glm-key", "model": "GLM-5.3"})
+
+ overrides = server._stored_session_runtime_overrides(_billing_only_row(GLM_OLD))
+
+ assert overrides["provider_override"] == "custom:glm"
+ assert overrides["model_override"]["base_url"] is None
+
+
+def test_resume_never_sends_the_default_provider_to_an_endpoint_no_entry_serves(monkeypatch):
+ """The stored endpoint belonged to a route that no longer exists. Pinning it on the profile's
+ default provider would send that provider's credentials to it."""
+ _custom_config(monkeypatch)
+
+ overrides = server._stored_session_runtime_overrides(_billing_only_row(GLM_OLD))
+
+ assert "provider_override" not in overrides
+ assert overrides["model_override"]["base_url"] is None
+ assert overrides["model_override"]["api_mode"] is None
diff --git a/tui_gateway/agent_factory.py b/tui_gateway/agent_factory.py
index 2dd8e6ef305..84c279f9a50 100644
--- a/tui_gateway/agent_factory.py
+++ b/tui_gateway/agent_factory.py
@@ -162,7 +162,9 @@ def _stored_session_runtime_overrides(row: dict | None) -> dict:
provider = healed
base_url = "" # the healed identity owns a registered endpoint; the snapshot URL must not override it
else:
- provider = ""
+ # The route is gone with its endpoint and wire: on the default provider they would send
+ # that provider's credentials to the stored URL.
+ provider = base_url = api_mode = ""
if model:
# Same dict-shaped override live /model switches use, so a DB-restored session keeps custom endpoint
# metadata across resume and rebuilds (/new). Raw api_key is never persisted/restored.