From 4a6d3640b99b412238e710b6bbee56a227efab61 Mon Sep 17 00:00:00 2001 From: whirmill <5079591+whirmill@users.noreply.github.com> Date: Thu, 16 Jul 2026 10:46:52 +0200 Subject: [PATCH] fix(agent): default context lookup for empty model IDs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An empty/blank model id reaching get_model_context_length() can't be meaningfully resolved — and it's worse than a miss: the endpoint metadata fuzzy matcher ('model in key or key in model') is vacuously true for "", so it matches an ARBITRARY catalog entry from the live /v1/models response and returns whatever context length that entry happens to have, persisting it under a junk '@' cache key. This started failing CI on main when the Nous portal catalog changed: tests/run_agent/test_primary_runtime_restore.py constructs agents with model='' against the live portal URL, the arbitrary match now lands on a 32K entry, and init_agent raises the 64K-floor ValueError (test_allowed_for_nous_anthropic_messages, red on every PR's slice). Guard early: a blank model id falls back to DEFAULT_FALLBACK_CONTEXT immediately, before any cache write or network probe. Salvaged from #65515 by @whirmill (rebased onto current main; the guard now sits after the malformed-base_url normalization added since, and carries an explanatory comment for the fuzzy-match footgun). Fixes the red slice on #85444, #85452 and every other open PR. Co-authored-by: whirmill <5079591+whirmill@users.noreply.github.com> --- agent/model_metadata.py | 13 +++++++++++++ tests/agent/test_model_metadata.py | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/agent/model_metadata.py b/agent/model_metadata.py index c2da434467..bd974af5e8 100644 --- a/agent/model_metadata.py +++ b/agent/model_metadata.py @@ -2624,6 +2624,19 @@ def get_model_context_length( except ValueError: base_url = "" + # An empty/blank model id can't be meaningfully resolved: every probe + # below would either miss or — worse — fuzzy-match an arbitrary catalog + # entry (the endpoint matcher's `model in key` check is vacuously true + # for ""), returning whatever context length that random entry has and + # persisting it under a junk "@" cache key. Fall back to the + # default immediately instead. + if not str(model or "").strip(): + logger.info( + "No model id provided for context length resolution — defaulting to %s tokens.", + f"{DEFAULT_FALLBACK_CONTEXT:,}", + ) + return DEFAULT_FALLBACK_CONTEXT + # Normalise provider-prefixed model names (e.g. "local:model-name" → # "model-name") so cache lookups and server queries use the bare ID that # local servers actually know about. Ollama "model:tag" colons are preserved. diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py index 8fe08933d0..cb73c60839 100644 --- a/tests/agent/test_model_metadata.py +++ b/tests/agent/test_model_metadata.py @@ -285,6 +285,10 @@ class TestDefaultContextLengths: model, provider="kimi-coding", base_url=base_url ) == 1_048_576 + def test_empty_model_uses_fallback_context(self): + assert get_model_context_length("") == DEFAULT_FALLBACK_CONTEXT + assert get_model_context_length(None) == DEFAULT_FALLBACK_CONTEXT # type: ignore[arg-type] + def test_xai_oauth_grok_build_uses_xai_models_dev_context(self): """xAI OAuth should share the xAI provider metadata path.