Skip to content

The agent that grows with you

fix(state): recover bare-custom CLI session routes from the billing endpoint

liuhao1024466c6f75bb38
diff --git a/hermes_state_gateway.py b/hermes_state_gateway.py
index f5411f40132..0cd57878349 100644
--- a/hermes_state_gateway.py
+++ b/hermes_state_gateway.py
@@ -829,7 +829,9 @@ class SessionGatewayMixin:
JSON string or parsed dict). Precedence: nested ``gateway_runtime`` (gateway sync /
CLI ``/model``), then top-level ``provider``/``base_url``/``api_mode`` (TUI), with
``billing_provider`` filling a missing provider so sessions that never ran ``/model``
- still restore the provider that served them. Empty dict on parse failure — resume uses ambient config."""
+ still restore the provider that served them; a bare ``custom`` billing class falls back
+ to its ``billing_base_url`` endpoint for the readers' bare-custom heal (#136323).
+ Empty dict on parse failure — resume uses ambient config."""
from hermes_state import _BARE_BILLING_PROVIDERS
raw = (session_meta or {}).get("model_config")
if isinstance(raw, str):
@@ -847,10 +849,21 @@ class SessionGatewayMixin:
top_level = {key: raw.get(key) for key in ("provider", "base_url", "api_mode") if raw.get(key)}
# billing_provider is COALESCE-written on the first accounted API call — the only durable
# record for sessions that never ran /model. Bare buckets ("auto"/"custom") are not
- # routable identities; filter them so resume falls back to the ambient default.
+ # routable identities; "auto" is filtered so resume falls back to the ambient default.
billing_provider = str((session_meta or {}).get("billing_provider") or "").strip()
if billing_provider and billing_provider.lower() not in _BARE_BILLING_PROVIDERS:
top_level.setdefault("provider", billing_provider)
+ elif billing_provider.lower() == "custom" and not top_level.get("provider"):
+ # Plain CLI turns persist only the billing class — "custom" — plus the endpoint they
+ # served (billing_base_url, first-accounted overwrite on the row). Surface the pair so
+ # the readers' bare-custom heal (canonical_custom_identity) can recover custom:<name>
+ # from that endpoint instead of resume recombining the stored model with the ambient
+ # default (#136323); an endpoint no longer configured still heals to nothing. "auto"
+ # bills through the router with no attributable endpoint and stays filtered.
+ billing_base_url = str((session_meta or {}).get("billing_base_url") or "").strip()
+ if billing_base_url:
+ top_level.setdefault("base_url", billing_base_url)
+ top_level["provider"] = "custom"
if top_level:
return top_level
if not isinstance(runtime, dict):
diff --git a/tests/hermes_cli/test_resume_model_restore.py b/tests/hermes_cli/test_resume_model_restore.py
index 0e75afba629..a696275719a 100644
--- a/tests/hermes_cli/test_resume_model_restore.py
+++ b/tests/hermes_cli/test_resume_model_restore.py
@@ -303,6 +303,44 @@ def test_restore_session_model_heals_bare_custom_stored_rows(monkeypatch):
assert stub.provider == "openrouter"
+def test_restore_session_model_recovers_route_from_billing_columns(monkeypatch):
+ """#136323 end-to-end: a CLI session that never ran /model has only the model column plus the
+ billing class/endpoint. Resume must run it on the custom entry that endpoint belongs to, not
+ the ambient default."""
+ import hermes_cli.runtime_provider as rp
+ monkeypatch.setattr(rp, "canonical_custom_identity",
+ lambda base_url=None, model=None: "custom:glm")
+ monkeypatch.setattr(
+ "hermes_cli.runtime_provider.resolve_runtime_provider",
+ lambda **kw: {"base_url": "http://127.0.0.1:1234/v1", "api_key": "sk-glm"})
+ meta = _row(model="GLM-5.3")
+ meta["billing_provider"] = "custom"
+ meta["billing_base_url"] = "http://127.0.0.1:1234/v1"
+ stub = _make_stub()
+ stub._restore_session_model(meta)
+ assert stub.model == "GLM-5.3"
+ assert stub.provider == "custom:glm"
+ assert stub.requested_provider == "custom:glm"
+ assert stub.base_url == "http://127.0.0.1:1234/v1"
+ assert stub.api_key == "sk-glm"
+
+
+def test_restore_session_model_billing_custom_without_configured_entry_keeps_ambient(monkeypatch):
+ """The billing endpoint no longer matches any configured entry — the heal resolves nothing, so
+ the stored model is restored but the route stays ambient (the first turn surfaces the error)."""
+ import hermes_cli.runtime_provider as rp
+ monkeypatch.setattr(rp, "canonical_custom_identity",
+ lambda base_url=None, model=None: None)
+ meta = _row(model="GLM-5.3")
+ meta["billing_provider"] = "custom"
+ meta["billing_base_url"] = "http://gone-endpoint/v1"
+ stub = _make_stub()
+ stub._restore_session_model(meta)
+ assert stub.model == "GLM-5.3"
+ assert stub.provider == "openrouter"
+ assert stub.base_url == "https://openrouter.ai/api/v1"
+
+
def test_restore_session_model_rederives_per_model_wire_for_opencode_rows(monkeypatch):
"""A row persisted while an opencode-go session ran an anthropic_messages model (MiniMax) must not
pin that wire onto a chat_completions model on resume — api_mode and the relay URL follow the
@@ -429,6 +467,28 @@ def test_session_gateway_runtime_billing_provider_fills_top_level_route_without_
assert SessionDB.session_gateway_runtime(meta) == {"base_url": "https://f/v1", "api_mode": "chat_completions"}
+def test_session_gateway_runtime_bare_custom_falls_back_to_billing_base_url():
+ """#136323: plain CLI turns persist only the billing class + the endpoint they served. The
+ reader surfaces both so the bare-custom heal can recover custom:<name> instead of resume
+ recombining the stored model with the ambient default."""
+ meta = _row()
+ meta["billing_provider"] = "custom"
+ meta["billing_base_url"] = "http://127.0.0.1:1234/v1"
+ assert SessionDB.session_gateway_runtime(meta) == {
+ "provider": "custom", "base_url": "http://127.0.0.1:1234/v1"}
+ # Without an endpoint there is nothing to attribute — stays filtered (ambient resume).
+ del meta["billing_base_url"]
+ assert SessionDB.session_gateway_runtime(meta) == {}
+
+
+def test_session_gateway_runtime_bare_auto_ignores_billing_base_url():
+ """auto bills through the router — no attributable endpoint, stays filtered."""
+ meta = _row()
+ meta["billing_provider"] = "auto"
+ meta["billing_base_url"] = "http://127.0.0.1:1234/v1"
+ assert SessionDB.session_gateway_runtime(meta) == {}
+
+
def test_restore_session_model_restores_billing_provider_fallback():
"""End-to-end: _restore_session_model uses billing_provider fallback."""
stub = _make_stub()