Skip to content

The agent that grows with you

refactor(config_providers): keep main's self-resolve, read the config through the read-only loader

kshitijk4poorb729bc43bcfc
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index ced2ec17ce0..639ab4482ad 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -2090,12 +2090,8 @@ 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.
- #
- # 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.
+ # Not gated on custom_providers: callers that never load the route list pass None and the
+ # helper self-resolves it from config (#69807).
if base_url and model:
with contextlib.suppress(Exception): # fall through to probing
from hermes_cli.config import get_custom_provider_context_length
diff --git a/hermes_cli/config_providers.py b/hermes_cli/config_providers.py
index 58cffdb9e8e..f0d58254f66 100644
--- a/hermes_cli/config_providers.py
+++ b/hermes_cli/config_providers.py
@@ -535,20 +535,19 @@ def get_custom_provider_context_length(
Before this helper existed, the lookup was duplicated in ``run_agent.py``'s startup path only; every
other path (notably ``/model`` switch) fell back to the 128K default. See #15779.
"""
+ from hermes_cli.config import get_compatible_custom_providers, load_config_readonly
if not model or not base_url:
return 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_providers
+ if custom_providers is None:
try:
- custom_providers = get_compatible_custom_providers()
+ # Step 0c now runs for every route with a base_url; the read-only loader skips the
+ # per-call deepcopy load_config() pays (same pattern as get_custom_provider_model_capability).
+ custom_providers = get_compatible_custom_providers(load_config_readonly() if config is None else config)
except Exception:
- return None
+ if config is None:
+ return None
+ raw = config.get("custom_providers")
+ custom_providers = raw if isinstance(raw, list) else []
def _positive_int(raw: Any) -> Optional[int]:
try:
diff --git a/tests/hermes_cli/test_custom_provider_context_length.py b/tests/hermes_cli/test_custom_provider_context_length.py
index 2220262a174..5a34641dcf6 100644
--- a/tests/hermes_cli/test_custom_provider_context_length.py
+++ b/tests/hermes_cli/test_custom_provider_context_length.py
@@ -216,3 +216,28 @@ class TestContextProbeTiers:
assert a > b, f"tiers must strictly descend, got {a} then {b}"
# 128K is still a tier (users relying on it probe-down get there)
assert 128_000 in CONTEXT_PROBE_TIERS
+
+
+def test_override_honored_when_caller_passes_no_custom_providers(tmp_path, monkeypatch):
+ """Step 0c must not be gated on the caller having loaded the route list: aux fallback screening,
+ CLI/TUI context estimators and gateway /status pass ``custom_providers=None`` (#69807)."""
+ from pathlib import Path
+
+ base_url, model, override = "https://cp-ctx-selfresolve.invalid/v1", "router/auto", 999_999
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(Path, "home", lambda: tmp_path)
+ (tmp_path / "config.yaml").write_text(
+ "custom_providers:\n"
+ " - name: test-route\n"
+ f" base_url: {base_url}\n"
+ " key_env: TEST_FAKE_CONTEXT_ENV\n"
+ f" model: {model}\n"
+ " api_mode: chat_completions\n"
+ " models:\n"
+ f" {model}:\n"
+ f" context_length: {override}\n",
+ encoding="utf-8",
+ )
+ from agent.model_metadata import get_model_context_length
+
+ assert get_model_context_length(model, base_url=base_url, api_key="", provider="custom") == override
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.py
deleted file mode 100644
index 970ff5e3c69..00000000000
--- a/tests/hermes_cli/test_custom_provider_context_length_no_caller_route.py
+++ /dev/null
@@ -1,38 +0,0 @@
-"""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
-
-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
-
-
-def test_override_honored_when_caller_passes_no_custom_providers(tmp_path, monkeypatch):
- """The exact failing call shape (aux/CLI/TUI/gateway /status): override wins, probe-down default never reached."""
- monkeypatch.setenv("HERMES_HOME", str(tmp_path))
- monkeypatch.setattr(Path, "home", lambda: tmp_path)
- (tmp_path / "config.yaml").write_text(
- "custom_providers:\n"
- " - name: test-route\n"
- f" base_url: {BASE_URL}\n"
- " key_env: TEST_FAKE_CONTEXT_ENV\n"
- f" model: {MODEL}\n"
- " api_mode: chat_completions\n"
- " models:\n"
- f" {MODEL}:\n"
- f" context_length: {OVERRIDE}\n",
- encoding="utf-8",
- )
-
- from agent.model_metadata import get_model_context_length
-
- assert get_model_context_length(MODEL, base_url=BASE_URL, api_key="", provider="custom") == OVERRIDE