Skip to content

The agent that grows with you

refactor(models): one fallback marker for every catalog fetcher

kshitijk4poor7793c196607c
diff --git a/hermes_cli/codex_models.py b/hermes_cli/codex_models.py
index c53f14cb768..fa8594e35af 100644
--- a/hermes_cli/codex_models.py
+++ b/hermes_cli/codex_models.py
@@ -8,11 +8,10 @@ import os
from pathlib import Path
from typing import List, Optional
-logger = logging.getLogger(__name__)
+from hermes_cli.route_identity import normalize_route_base_url
+logger = logging.getLogger(__name__)
-class CodexFallbackModels(list[str]):
- """Compatibility hints after failed account discovery, not an account catalog."""
# Curated offline fallback (first-run, transient API failure). Only slugs the ChatGPT Codex
# OAuth backend actually accepts: the public API's "-pro" variants and the retired
@@ -111,9 +110,9 @@ def codex_catalog_credential_identity() -> str:
Access/refresh tokens rotate in place while the account-scoped catalog stays authoritative for
the same ChatGPT principal and route, so the key includes the principal and resolved base URL,
- not the rotating token. An
- expired token is its own state: ``_codex_catalog`` serves the static fallback for it, and that
- fallback must not outlive the refresh under the healthy principal's key. Opaque non-JWT tokens
+ not the rotating token. An expired token is its own state (per route): ``_codex_catalog`` serves
+ the static fallback for it, and that fallback must not outlive the refresh under the healthy
+ principal's key. Opaque non-JWT tokens
fall back to the token itself (the caller hashes every part before anything is persisted).
"""
from hermes_cli.auth import _codex_access_token_is_expiring, resolve_codex_runtime_credentials
@@ -121,8 +120,7 @@ def codex_catalog_credential_identity() -> str:
try:
creds = resolve_codex_runtime_credentials(read_only=True)
token = str(creds.get("api_key") or "")
- from hermes_cli.auth_codex import _codex_base_url
- route = str(creds.get("base_url") or _codex_base_url()).strip().rstrip("/")
+ route = normalize_route_base_url(str(creds.get("base_url") or "").strip())
except Exception: # AuthError (no/exhausted creds) or the pytest seat belt: no live catalog either way
token = ""
if not token:
@@ -223,7 +221,10 @@ def get_codex_model_ids(access_token: Optional[str] = None, base_url: Optional[s
api_models = _fetch_models_from_api(access_token, base_url=base_url)
if api_models:
return _finalize_codex_models(api_models)
+ # Late: models_catalog_static builds its codex table from this module at import time.
+ from hermes_cli.models_catalog_static import CuratedFallbackModels
+
default_model = _read_default_model(codex_home)
- return CodexFallbackModels(_finalize_codex_models(_drop_undiscovered_astra(_dedupe([
+ return CuratedFallbackModels(_finalize_codex_models(_drop_undiscovered_astra(_dedupe([
*([default_model] if default_model else []), *_read_cache_models(codex_home),
*DEFAULT_CODEX_MODELS]))))
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index c16ec76f2b4..9ec795bd7a9 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -31,6 +31,7 @@ from hermes_cli.route_identity import normalize_route_base_url
from hermes_cli.urllib_security import open_credentialed_url
from hermes_cli.version_info import get_version_info
from hermes_cli.models_catalog_static import (
+ CuratedFallbackModels,
CANONICAL_PROVIDERS,
OPENROUTER_MODELS,
PREFERRED_SILENT_DEFAULT_MODEL,
@@ -1320,7 +1321,7 @@ def _openai_discovery_base_url(provider: str) -> str:
def _codex_catalog(normalized: str, force_refresh: bool) -> list[str]:
- from hermes_cli.codex_models import CodexFallbackModels, get_codex_model_ids
+ from hermes_cli.codex_models import get_codex_model_ids
# Live OAuth token so the picker matches what ChatGPT lists for this account; hardcoded
# catalog without a token / when unreachable. Read-only (#68004): a picker never imports,
@@ -1338,10 +1339,9 @@ def _codex_catalog(normalized: str, force_refresh: bool) -> list[str]:
access_token = None
except Exception:
access_token = None
- models = get_codex_model_ids(access_token=access_token, base_url=base_url)
- # Account-gated rows such as Astra are absent from the offline hints. Do not
- # cache those hints as a successful account fetch or replace a verified row.
- return CuratedFallbackModels(models) if isinstance(models, CodexFallbackModels) else models
+ # A failed live fetch returns ``CuratedFallbackModels``: account-gated rows such as Astra are
+ # absent from the offline hints, so they must never replace a verified row.
+ return get_codex_model_ids(access_token=access_token, base_url=base_url)
_COPILOT_ACP_SESSION_MEMO_TTL = 300.0 # 5 min; SWR disk cache handles the rest
@@ -1372,11 +1372,6 @@ def _copilot_acp_session_models(force_refresh: bool) -> Optional[list[str]]:
return live
-class CuratedFallbackModels(list[str]):
- """A curated list served because the provider's live catalog was unavailable. The disk cache
- treats it as a placeholder, never as the account's real catalog (#107391)."""
-
-
def _copilot_catalog(normalized: str, force_refresh: bool) -> Optional[list[str]]:
if normalized == "copilot-acp" and (live := _copilot_acp_session_models(force_refresh)):
return live
diff --git a/hermes_cli/models_catalog_static.py b/hermes_cli/models_catalog_static.py
index c0684ee2aad..3a7953029c8 100644
--- a/hermes_cli/models_catalog_static.py
+++ b/hermes_cli/models_catalog_static.py
@@ -9,6 +9,11 @@ from __future__ import annotations
from typing import NamedTuple
+class CuratedFallbackModels(list[str]):
+ """A curated list served because the provider's live catalog was unavailable. The disk cache
+ treats it as a placeholder, never as the account's real catalog (#107391)."""
+
+
# Fallback OpenRouter snapshot used when the live catalog is unavailable, as
# ``(model_id, description shown in menus)``. ``:free`` SKUs are described "free".
_OPENROUTER_DESCRIPTIONS = {