The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(models): remember a Codex catalog probe that found nothing for 5 minutes
kshitijk4poore22905a696b4
diff --git a/agent/model_metadata.py b/agent/model_metadata.pyindex d87e2bef82e..e33e2b0c0b2 100644--- a/agent/model_metadata.py+++ b/agent/model_metadata.py@@ -1782,6 +1782,7 @@ _codex_oauth_context_cache: Dict[str, Tuple[Dict[str, int], float]] = {}# opted-in ``-900k`` bump reads it (#105443); a catalog without the field leaves the entry empty._codex_oauth_max_context_cache: Dict[str, Dict[str, int]] = {}_CODEX_OAUTH_CONTEXT_CACHE_TTL = 3600 # 1 hour+_CODEX_OAUTH_CONTEXT_NEGATIVE_TTL = 300 # a probe that found no catalog is retried after 5 minutes# The Codex models endpoint reads ``client_version`` as a Codex CLI compatibility version and# hides models whose ``minimal_client_version`` is newer. "0.0.0" used to be the ungated sentinel# returning the whole account catalog, but since the GPT-6 Sol/Luna rollout it returns a FROZEN@@ -1850,6 +1851,19 @@ def _codex_oauth_token_fingerprint(access_token: str, base_url: str = "") -> strreturn hashlib.sha256(f"{access_token}\n{(base_url or '').strip().rstrip('/')}".encode("utf-8")).hexdigest()[:16]+def _remember_no_codex_catalog(cache_key: str) -> None:+ """A probe that found no catalog (down, non-200, not Codex-shaped) is remembered briefly: context+ resolution runs on init, /status, aux and @-reference turns, and each miss can block ~30s. The entry is+ stamped as if cached ``TTL - NEGATIVE_TTL`` ago, so the shared freshness check expires it after+ ``_CODEX_OAUTH_CONTEXT_NEGATIVE_TTL``. A fresh catalog a concurrent probe just stored is kept, and the+ max cache is left alone so an observed cap survives."""+ now = time.time()+ current = _codex_oauth_context_cache.get(cache_key)+ if current is not None and current[0] and now - current[1] < _CODEX_OAUTH_CONTEXT_CACHE_TTL:+ return+ _codex_oauth_context_cache[cache_key] = ({}, now - _CODEX_OAUTH_CONTEXT_CACHE_TTL + _CODEX_OAUTH_CONTEXT_NEGATIVE_TTL)++def _fetch_codex_oauth_context_lengths_with_source(access_token: str, base_url: str = "") -> Tuple[Dict[str, int], bool]:"""Codex catalogue ``{slug: context_window}`` plus whether it came from HTTP. Cached per tokenfingerprint (windows vary by entitlement); ``max_context_window`` lands in@@ -1873,9 +1887,11 @@ def _fetch_codex_oauth_context_lengths_with_source(access_token: str, base_url:)if status != 200:logger.debug("Codex /models probe returned HTTP %s; falling back to hardcoded defaults", status)+ _remember_no_codex_catalog(cache_key)return {}, Falseexcept Exception as exc:logger.debug("Codex /models probe failed: %s", exc)+ _remember_no_codex_catalog(cache_key)return {}, Falseresult: Dict[str, int] = {}max_result: Dict[str, int] = {}@@ -1885,10 +1901,12 @@ def _fetch_codex_oauth_context_lengths_with_source(access_token: str, base_url:result[slug.strip()] = ctxif isinstance(max_ctx, int) and max_ctx > 0:max_result[slug.strip()] = max_ctx- if result:- # Max first: a reader that sees the fresh context entry must also see its cap.- _codex_oauth_max_context_cache[cache_key] = max_result- _codex_oauth_context_cache[cache_key] = (result, now)+ if not result:+ _remember_no_codex_catalog(cache_key)+ return {}, False+ # Max first: a reader that sees the fresh context entry must also see its cap.+ _codex_oauth_max_context_cache[cache_key] = max_result+ _codex_oauth_context_cache[cache_key] = (result, now)return result, Truediff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.pyindex 28fd5dddf8a..95ac67d1b21 100644--- a/tests/agent/test_model_metadata.py+++ b/tests/agent/test_model_metadata.py@@ -698,6 +698,31 @@ class TestCodexOAuthContextLength:patch("agent.model_metadata.time.time", return_value=time.time() + mm._CODEX_OAUTH_CONTEXT_CACHE_TTL + 1):assert mm.get_model_context_length(model="gpt-5.6-luna-900k", **route) == 600_000+ def test_a_probe_that_finds_no_catalog_is_not_repeated_within_the_negative_ttl(self):+ """Every context resolution (init, /status, aux, @-reference turns) used to re-probe a dead or+ non-Codex-shaped catalog; one miss is now remembered for the short negative TTL, then retried."""+ import agent.model_metadata as mm++ calls = []++ def down(url, **_kw):+ calls.append(url)+ raise OSError("catalog down")++ route = dict(base_url="https://chatgpt.com/backend-api/codex", api_key=_codex_jwt("fake-token"), provider="openai-codex")+ with patch.object(mm, "_codex_oauth_context_cache", {}), patch.object(mm, "_codex_oauth_max_context_cache", {}), \+ patch("agent.model_metadata.model_metadata_http.get", side_effect=down), \+ patch("agent.model_metadata.get_cached_context_length", return_value=None), \+ patch("agent.model_metadata.save_context_length"):+ assert mm.get_model_context_length(model="gpt-5.6-sol", **route) == 272_000+ per_probe = len(calls)+ mm.get_model_context_length(model="gpt-5.6-sol", **route)+ mm.get_model_context_length(model="gpt-5.6-sol-900k", **route)+ assert per_probe and len(calls) == per_probe+ with patch("agent.model_metadata.time.time", return_value=time.time() + mm._CODEX_OAUTH_CONTEXT_NEGATIVE_TTL + 1):+ mm.get_model_context_length(model="gpt-5.6-sol", **route)+ assert len(calls) == 2 * per_probe+def test_catalog_max_is_published_before_the_context_entry(self):"""A reader that sees a fresh context entry must already be able to see its cap, or aconcurrent -900k resolution between the two writes skips the catalog max."""