Skip to content

The agent that grows with you

fix(auxiliary): the client cache borrows on the same terms as the client build

John Paul Soliva85b54bde72a6
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index b978c9228d6..6302abf1614 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -5811,12 +5811,16 @@ def _borrowed_main_credential_key(provider: str, base_url: Optional[str], api_ke
The client keeps that credential for its lifetime, so it joins the cache key: otherwise a
later runtime (another session, a ``/model`` switch) is served the earlier one's key.
"""
- if api_key or _normalize_aux_provider(provider) != "custom":
+ if _normalize_aux_provider(provider) != "custom":
return ()
- if base_url:
- borrowed = _read_main_api_key_if_same_host(_to_openai_base_url(base_url).strip())
- return (_runtime_cache_discriminator("api_key", borrowed),)
- return (runtime.get("base_url", ""), _runtime_cache_discriminator("api_key", runtime.get("api_key", "")))
+ if not base_url:
+ # This shape takes the runtime's endpoint and key even when an explicit key was passed.
+ return (runtime.get("base_url", ""), _runtime_cache_discriminator("api_key", runtime.get("api_key", "")))
+ # 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())
+ return (_runtime_cache_discriminator("api_key", borrowed),)
def _current_event_loop() -> Any:
diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.py
index ab66356ecdb..cc0a689af10 100644
--- a/tests/agent/test_auxiliary_client.py
+++ b/tests/agent/test_auxiliary_client.py
@@ -4869,6 +4869,32 @@ class TestCustomEndpointApiKeyInheritance:
assert served[:2] == ["sk-first-session", "sk-second-session"]
assert served[2] not in ("sk-first-session", "sk-second-session")
+ @pytest.mark.parametrize("aux_base_url,explicit_key", [
+ ("https://gw.example.com/v1", " "), (None, " "), (None, "sk-explicit-aux"),
+ ], ids=["explicit-base-url-blank-key", "runtime-endpoint-blank-key", "runtime-endpoint-explicit-key"])
+ def test_cache_separates_runtimes_whenever_the_build_borrows(self, monkeypatch, aux_base_url, explicit_key):
+ """The cache and the client build must agree on when the main credential is borrowed: a
+ blank explicit key is keyless, and the runtime-endpoint shape never uses the explicit key."""
+ import agent.auxiliary_client as aux
+
+ monkeypatch.delenv("OPENAI_API_KEY", raising=False)
+ monkeypatch.delenv("OPENAI_BASE_URL", raising=False)
+ aux.shutdown_cached_clients()
+ served = []
+ try:
+ for live_key in ("sk-first-session", "sk-second-session"):
+ token = aux.set_runtime_main("custom", "main-model", base_url="https://gw.example.com/v1", api_key=live_key)
+ try:
+ client, _ = aux._get_cached_client("custom", "aux-model", base_url=aux_base_url, api_key=explicit_key)
+ finally:
+ aux.reset_runtime_main(token)
+ served.append(getattr(client, "api_key", None))
+ finally:
+ aux.shutdown_cached_clients()
+ aux.clear_runtime_main()
+
+ assert served == ["sk-first-session", "sk-second-session"]
+
class TestNoProgressTimeoutTaskConfigGating:
"""#108104: ``auxiliary.<task>.no_progress_timeout`` must only reach the request kwargs