Skip to content

The agent that grows with you

refactor(auxiliary): compare origins before reading the main key; name the helper for what it checks

kshitijk4poor2345688c583b
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 6302abf1614..8d2d3569a37 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -2508,7 +2508,7 @@ def _read_main_model_for_aux() -> str:
return model
-def _read_main_api_key_if_same_host(aux_base_url: str) -> Any:
+def _read_main_api_key_if_same_origin(aux_base_url: str) -> Union[str, Callable[[], str]]:
"""Main api_key only when *aux_base_url* has the main base_url's exact origin.
Unconditional inheritance would leak the credential to any misconfigured host; mismatch keeps ``no-key-required`` → 401.
@@ -2516,16 +2516,20 @@ def _read_main_api_key_if_same_host(aux_base_url: str) -> Any:
Anchor and key come from ONE source: the live runtime a turn bound, else config.yaml. The
per-field readers fall back to config field by field, so a keyless or key_cmd live main would
pair its own base_url with config's key and send that key to the live endpoint.
+ Origins are compared before any key is read: the client cache calls this on every keyless
+ ``custom`` lookup, and a mismatch must not pay for a config.yaml key read.
"""
aux_origin = base_url_origin(aux_base_url)
- live_base, live_key = _runtime_main_value("base_url"), _runtime_main_value("api_key")
- if live_base or live_key:
- main_base, main_key = live_base, _normalize_api_key(live_key)
- else:
- main_base, main_key = _read_main_base_url(), _read_main_api_key()
- if not aux_origin[1] or aux_origin != base_url_origin(main_base):
+ if not aux_origin[1]:
+ return ""
+ live = _normalize_main_runtime(None)
+ if live.get("base_url") or live.get("api_key"):
+ if aux_origin != base_url_origin(live.get("base_url", "")):
+ return ""
+ return _normalize_api_key(live.get("api_key", ""))
+ if aux_origin != base_url_origin(_read_main_base_url()):
return ""
- return main_key
+ return _read_main_api_key()
# Compatibility mirrors for older readers/tests; the ContextVar below is
@@ -5086,7 +5090,7 @@ def _resolve_custom_branch(req: _ResolveRequest) -> _ResolveResult:
custom_key = (
_normalize_api_key(req.explicit_api_key)
or _scoped_key_env("OPENAI_API_KEY")
- or _read_main_api_key_if_same_host(custom_base)
+ or _read_main_api_key_if_same_origin(custom_base)
or "no-key-required" # local servers don't need auth
)
if not custom_base:
@@ -5819,7 +5823,7 @@ def _borrowed_main_credential_key(provider: str, base_url: Optional[str], api_ke
# Same normalization as the client build, which treats a blank explicit key as keyless.
if _normalize_api_key(api_key):
return ()
- borrowed = _read_main_api_key_if_same_host(_to_openai_base_url(base_url).strip())
+ borrowed = _read_main_api_key_if_same_origin(_to_openai_base_url(base_url).strip())
return (_runtime_cache_discriminator("api_key", borrowed),)
diff --git a/tests/agent/test_auxiliary_ollama_local_provider.py b/tests/agent/test_auxiliary_ollama_local_provider.py
index 6d25253a8cb..e6dcb8187b0 100644
--- a/tests/agent/test_auxiliary_ollama_local_provider.py
+++ b/tests/agent/test_auxiliary_ollama_local_provider.py
@@ -22,7 +22,7 @@ def _keyless_environment(monkeypatch):
monkeypatch.delenv(key, raising=False)
# The key ladder inherits the main config's key on a host match; the placeholder
# assertions below need that rung empty.
- with patch("agent.auxiliary_client._read_main_api_key_if_same_host", return_value=None):
+ with patch("agent.auxiliary_client._read_main_api_key_if_same_origin", return_value=None):
yield
@@ -57,7 +57,7 @@ def test_ollama_lane_with_bare_local_host_resolves_custom_client_under_v1(
if env_openai_key:
monkeypatch.setenv("OPENAI_API_KEY", env_openai_key)
- monkeypatch.setattr(ac, "_read_main_api_key_if_same_host", lambda _base: "sk-MAIN-SECRET")
+ monkeypatch.setattr(ac, "_read_main_api_key_if_same_origin", lambda _base: "sk-MAIN-SECRET")
lane = {"provider": "ollama", "model": "qwen3.8:27b", "base_url": "http://127.0.0.1:11434", "api_key": api_key}
monkeypatch.setattr(ac, "_get_auxiliary_task_config", lambda task: dict(lane) if task == "title_generation" else {})
provider, model, base_url, key, _mode = ac._resolve_task_provider_model("title_generation")