The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(model_metadata): honor per-model context_length when the caller doesn't pass custom_providers
Stephen Cuppettf4ffd48274f8
diff --git a/agent/model_metadata.py b/agent/model_metadata.pyindex ce71143af36..ced2ec17ce0 100644--- a/agent/model_metadata.py+++ b/agent/model_metadata.py@@ -2090,7 +2090,13 @@ def _config_override_context_length(model: str, base_url: str, provider: str, cu# 0c. custom_providers per-model override — check before any probe. This closes the gap where /model# switch and display paths used to fall back to 128K despite the user having a per-model context_length# set. See #15779.- if custom_providers and base_url and model:+ #+ # Gate on (base_url AND model) only — NOT on the caller-supplied custom_providers. Callers that+ # never load the route list (aux fallback screening, CLI/TUI context-reference estimators,+ # gateway /status) pass custom_providers=None and would skip 0c entirely, falling to the+ # 256K/272K defaults despite a user-set models.<id>.context_length. get_custom_provider_context_length+ # self-resolves from config when the list was not supplied, mirroring _resolve_moa_context_length.+ if base_url and model:with contextlib.suppress(Exception): # fall through to probingfrom hermes_cli.config import get_custom_provider_context_lengthcp_ctx = get_custom_provider_context_length(model=model, base_url=base_url, custom_providers=custom_providers)diff --git a/hermes_cli/config_providers.py b/hermes_cli/config_providers.pyindex 10401602642..58cffdb9e8e 100644--- a/hermes_cli/config_providers.py+++ b/hermes_cli/config_providers.py@@ -535,17 +535,20 @@ def get_custom_provider_context_length(Before this helper existed, the lookup was duplicated in ``run_agent.py``'s startup path only; everyother path (notably ``/model`` switch) fell back to the 128K default. See #15779."""- from hermes_cli.config import get_compatible_custom_providersif not model or not base_url:return None- if custom_providers is None:+ # Self-resolve the route from config when the caller never loaded the provider list (aux+ # fallback screening, CLI/TUI context-reference estimators, gateway /status, vision auto-detect).+ # Without this, step 0c of get_model_context_length skips the per-model override on those paths+ # and falls to the 256K/272K defaults, while run_agent's startup path — which DOES pass the list —+ # honours the same setting. Same self-resolution pattern as _resolve_moa_context_length.+ # load_config() is cached on the config file signature, so the hot-path cost is minimal.+ if custom_providers is None and config is None:+ from hermes_cli.config import get_compatible_custom_providerstry:- custom_providers = get_compatible_custom_providers(config)+ custom_providers = get_compatible_custom_providers()except Exception:- if config is None:- return None- raw = config.get("custom_providers")- custom_providers = raw if isinstance(raw, list) else []+ return Nonedef _positive_int(raw: Any) -> Optional[int]:try:diff --git a/tests/hermes_cli/test_custom_provider_context_length_no_caller_route.py b/tests/hermes_cli/test_custom_provider_context_length_no_caller_route.pynew file mode 100644index 00000000000..ee08d3e8f11--- /dev/null+++ b/tests/hermes_cli/test_custom_provider_context_length_no_caller_route.py@@ -0,0 +1,78 @@+"""Per-model ``context_length`` override must be honored even when the caller does not+supply ``custom_providers`` (self-resolved from config).++Regression: step 0c of agent.model_metadata.get_model_context_length was gated on the+caller having pre-loaded the provider list, so auxiliary fallback screening, CLI/TUI+context-reference estimators, gateway /status and vision auto-detect — which all pass+custom_providers=None — skipped the per-model override and fell to the 256K/272K+probe-down defaults, while the startup path honored the same setting.+"""+from __future__ import annotations++from pathlib import Path+++def _write_custom_provider(home: str, base_url: str, model: str, context_length: int) -> None:+ cfg = (+ ("custom_providers:\n"+ f" - name: test-route\n"+ f" base_url: {base_url}\n"+ f" key_env: TEST_FAKE_CONTEXT_ENV\n"+ f" model: {model}\n"+ " api_mode: chat_completions\n"+ f" models:\n"+ f" {model}:\n"+ f" context_length: {context_length}\n")+ )+ (Path(home) / "config.yaml").write_text(cfg, encoding="utf-8")+++BASE_URL = "https://cp-ctx-selfresolve.invalid/v1"+MODEL = "router/auto"+OVERRIDE = 999_999 # non-power-of-two so it cannot be a catalog value+++class TestGetModelContextLengthSelfResolvesConfigOverride:+ def test_honored_when_caller_passes_no_custom_providers(self, tmp_path, monkeypatch):+ """The exact failing call shape (aux/CLI/TUI/gateway /status): override wins, probe-down default never reached."""+ import os+ from pathlib import Path as _P++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.setattr(_P, "home", lambda: tmp_path)+ _write_custom_provider(str(tmp_path), BASE_URL, MODEL, OVERRIDE)++ from agent.model_metadata import get_model_context_length+ from hermes_constants import get_hermes_home, display_hermes_home # noqa: F401 (cache isolation sanity)++ value = get_model_context_length(+ MODEL, base_url=BASE_URL, api_key="", provider="custom"+ )+ assert value == OVERRIDE++ def test_helper_self_resolves_without_any_args(self, tmp_path, monkeypatch):+ """get_custom_provider_context_length with neither list nor config dict."""+ from pathlib import Path as _P++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.setattr(_P, "home", lambda: tmp_path)+ _write_custom_provider(str(tmp_path), BASE_URL, MODEL, OVERRIDE)++ from hermes_cli.config_providers import get_custom_provider_context_length++ value = get_custom_provider_context_length(MODEL, BASE_URL)+ assert value == OVERRIDE+ value_slash = get_custom_provider_context_length(MODEL, BASE_URL + "/")+ assert value_slash == OVERRIDE++ def test_unknown_model_on_self_resolved_route_still_returns_none(self, tmp_path, monkeypatch):+ """A model with no per-model entry gets None from the helper — resolution continues downstream."""+ from pathlib import Path as _P++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.setattr(_P, "home", lambda: tmp_path)+ _write_custom_provider(str(tmp_path), BASE_URL, MODEL, OVERRIDE)++ from hermes_cli.config_providers import get_custom_provider_context_length++ assert get_custom_provider_context_length("some/other-model", BASE_URL) is None