Skip to content

The agent that grows with you

fix: apply model.openai_runtime once, after the ladder; trim tests to two invariants

teknium18ec20648a101
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py
index 9c3ed44b080..49e1d1917d7 100644
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@ -263,7 +263,8 @@ def _api_key_provider_api_mode(provider: str, model_cfg: Dict[str, Any], api_key
def _maybe_apply_codex_app_server_runtime(*, provider: str, api_mode: str, model_cfg: Optional[Dict[str, Any]]) -> str:
"""Opt-in rewrite to "codex_app_server" via ``model.openai_runtime``; only ``openai`` /
- ``openai-codex`` are eligible. No-op when unset, "auto", or empty."""
+ ``openai-codex`` are eligible. No-op when unset, "auto", or empty. Applied once, on the
+ runtime ``resolve_runtime_provider`` picked — never inside an individual ladder rung."""
if model_cfg and provider in {"openai", "openai-codex"} and str(model_cfg.get("openai_runtime") or "").strip().lower() == "codex_app_server":
return "codex_app_server"
return api_mode
@@ -521,7 +522,6 @@ def _resolve_runtime_from_pool_entry(*, provider: str, entry: PooledCredential,
api_mode, base_url = _pool_entry_mode_and_url(provider, entry, model_cfg, _effective_model(model_cfg, target_model),
_pool_entry_base_url(entry).rstrip("/"))
base_url = _finalize_base_url(provider, api_mode, base_url)
- api_mode = _maybe_apply_codex_app_server_runtime(provider=provider, api_mode=api_mode, model_cfg=model_cfg)
return _runtime(provider, api_mode, base_url, _pool_entry_api_key(entry), source=getattr(entry, "source", "pool"),
credential_pool=pool, requested_provider=requested_provider)
@@ -715,10 +715,6 @@ def _resolve_oauth_runtime(provider, requested_provider, model_cfg, target_model
spec = _OAUTH_RUNTIME_PROVIDERS[provider]
creds = spec.resolve()
api_mode = spec.api_mode(_effective_model(model_cfg, target_model)) if callable(spec.api_mode) else spec.api_mode
- # model.openai_runtime opt-in (#115169): the OAuth rung receives model_cfg directly, so apply
- # the rewrite here too — ambient-config-only application at the resolve_runtime_provider
- # choke point would not cover direct callers of this rung. Idempotent with that application.
- api_mode = _maybe_apply_codex_app_server_runtime(provider=provider, api_mode=api_mode, model_cfg=model_cfg)
return _runtime(provider, api_mode, (creds.get("base_url") or "").rstrip("/") or spec.default_base_url,
creds.get("api_key", ""), source=creds.get("source", spec.default_source),
**{spec.expiry_key: creds.get(spec.expiry_key)}, requested_provider=requested_provider)
@@ -917,12 +913,11 @@ def resolve_runtime_provider(*, requested: Optional[str] = None, explicit_api_ke
_raise_if_local_alias_missing_endpoint(requested_provider, explicit_base_url)
runtime = next(r for r in _ladder_rungs(requested_provider, explicit_api_key, explicit_base_url, target_model) if r)
_raise_for_credentialless_bare_custom(requested_provider, runtime)
- # model.openai_runtime opt-in: rewrite api_mode at the single choke point so every
- # ladder rung (OAuth, explicit-key, pool, shortcuts) honors it, not just the pool
- # rung's in-situ application below (#115169). Idempotent with that application.
+ # model.openai_runtime is applied ONCE, after the ladder: every rung (pool, OAuth store,
+ # explicit --api-key/--base-url, env key) hardcodes the wire api_mode for openai/openai-codex,
+ # so applying the opt-in inside one rung left the others on codex_responses (#115169).
runtime["api_mode"] = _maybe_apply_codex_app_server_runtime(
- provider=runtime.get("provider", ""), api_mode=runtime.get("api_mode", ""),
- model_cfg=_get_model_config())
+ provider=runtime.get("provider", ""), api_mode=runtime.get("api_mode", ""), model_cfg=_get_model_config())
return runtime
diff --git a/tests/hermes_cli/test_openai_runtime_override.py b/tests/hermes_cli/test_openai_runtime_override.py
deleted file mode 100644
index fffe7c677b9..00000000000
--- a/tests/hermes_cli/test_openai_runtime_override.py
+++ /dev/null
@@ -1,65 +0,0 @@
-"""model.openai_runtime: codex_app_server must survive runtime resolution (#115169).
-
-The rewrite was only applied on the credential-pool rung; the OAuth path (the
-normal openai-codex route) and explicit-key paths silently dropped it.
-"""
-
-from __future__ import annotations
-
-from unittest.mock import patch
-
-from hermes_cli import runtime_provider as rp
-
-FAKE_CODEX_CREDS = {
- "base_url": "https://chatgpt.com/backend-api/codex",
- "api_key": "tok",
- "source": "hermes-auth-store",
- "last_refresh": 1,
-}
-
-
-def _model_cfg(runtime_value):
- cfg = {"provider": "openai-codex", "default": "gpt-5.6-luna"}
- if runtime_value is not None:
- cfg["openai_runtime"] = runtime_value
- return cfg
-
-
-class TestOAuthPathHonorsOpenaiRuntime:
- def test_codex_app_server_applied(self):
- with patch.object(rp, "resolve_codex_runtime_credentials", return_value=dict(FAKE_CODEX_CREDS)):
- rt = rp._resolve_oauth_runtime("openai-codex", "openai-codex",
- _model_cfg("codex_app_server"), None)
- assert rt["api_mode"] == "codex_app_server"
-
- def test_unset_stays_codex_responses(self):
- with patch.object(rp, "resolve_codex_runtime_credentials", return_value=dict(FAKE_CODEX_CREDS)):
- rt = rp._resolve_oauth_runtime("openai-codex", "openai-codex",
- _model_cfg(None), None)
- assert rt["api_mode"] == "codex_responses"
-
- def test_auto_stays_codex_responses(self):
- with patch.object(rp, "resolve_codex_runtime_credentials", return_value=dict(FAKE_CODEX_CREDS)):
- rt = rp._resolve_oauth_runtime("openai-codex", "openai-codex",
- _model_cfg("auto"), None)
- assert rt["api_mode"] == "codex_responses"
-
-
-class TestFullLadderHonorsOpenaiRuntime:
- def test_codex_app_server_survives_ladder(self):
- # The hermetic conftest points HERMES_HOME at an empty tempdir, so the
- # ambient config cannot carry the flag — inject model_cfg at the same
- # seam the rest of this suite uses (monkeypatch.setattr(rp, "_get_model_config", ...)).
- with patch.object(rp, "resolve_codex_runtime_credentials", return_value=dict(FAKE_CODEX_CREDS)), \
- patch.object(rp, "_get_model_config", return_value=_model_cfg("codex_app_server")):
- rt = rp.resolve_runtime_provider(requested="openai-codex")
- assert rt["provider"] == "openai-codex"
- assert rt["api_mode"] == "codex_app_server"
-
-
-class TestNonEligibleProvidersUnaffected:
- def test_actual_route_clobber_unchanged(self):
- # provider "actual" is not eligible for the openai_runtime rewrite; the
- # is_actual_route chat_completions normalization must keep working.
- rt = rp._runtime("actual", "codex_app_server", "https://api.actual.inc/v1", "k")
- assert rt["api_mode"] == "chat_completions"
diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.py
index 744bd7fe3f0..da297ab1312 100644
--- a/tests/hermes_cli/test_runtime_provider_resolution.py
+++ b/tests/hermes_cli/test_runtime_provider_resolution.py
@@ -1971,3 +1971,48 @@ def test_removed_keyless_free_provider_points_at_its_replacements(name):
assert excinfo.value.code == "invalid_provider"
message = str(excinfo.value)
assert "opencode-zen" in message and "opencode-go" in message
+
+
+# ── model.openai_runtime: codex_app_server on every ladder rung (#115169) ─────────────────
+
+_CODEX_STORE_CREDS = {"base_url": "https://chatgpt.com/backend-api/codex", "api_key": "tok",
+ "source": "hermes-auth-store", "last_refresh": 1}
+
+
+def _codex_rung(monkeypatch, rung: str) -> dict:
+ """Isolate one openai-codex ladder rung; returns the kwargs for resolve_runtime_provider."""
+ monkeypatch.setattr(rp, "resolve_codex_runtime_credentials", lambda: dict(_CODEX_STORE_CREDS))
+ if rung == "pool":
+ entry = SimpleNamespace(api_key="tok", runtime_api_key="tok", base_url="", source="pool")
+ monkeypatch.setattr(rp, "load_pool", lambda _p: SimpleNamespace(
+ has_credentials=lambda: True, select=lambda model=None: entry))
+ monkeypatch.setattr(rp, "credential_pool_matches_provider", lambda *a, **k: True)
+ return {}
+ monkeypatch.setattr(rp, "load_pool", lambda _p: SimpleNamespace(has_credentials=lambda: False))
+ return {"explicit_api_key": "sk-explicit"} if rung == "explicit" else {}
+
+
[email protected]("rung", ["pool", "oauth", "explicit"])
+def test_openai_runtime_codex_app_server_applies_on_every_rung(monkeypatch, rung):
+ """#115169: the opt-in was applied only inside the credential-pool rung, so the OAuth-store
+ and explicit --api-key/--base-url rungs silently resolved codex_responses."""
+ kwargs = _codex_rung(monkeypatch, rung)
+ monkeypatch.setattr(rp, "_get_model_config", lambda: {
+ "provider": "openai-codex", "default": "gpt-5.5", "openai_runtime": "codex_app_server"})
+
+ resolved = rp.resolve_runtime_provider(requested="openai-codex", **kwargs)
+
+ assert resolved["provider"] == "openai-codex"
+ assert resolved["api_mode"] == "codex_app_server"
+
+
[email protected]("rung", ["pool", "oauth", "explicit"])
[email protected]("openai_runtime", [None, "auto"])
+def test_openai_runtime_unset_keeps_wire_api_mode(monkeypatch, rung, openai_runtime):
+ kwargs = _codex_rung(monkeypatch, rung)
+ model_cfg = {"provider": "openai-codex", "default": "gpt-5.5"}
+ if openai_runtime is not None:
+ model_cfg["openai_runtime"] = openai_runtime
+ monkeypatch.setattr(rp, "_get_model_config", lambda: model_cfg)
+
+ assert rp.resolve_runtime_provider(requested="openai-codex", **kwargs)["api_mode"] == "codex_responses"