Skip to content

The agent that grows with you

fix(auxiliary): a cached custom client follows the main credential it borrowed

John Paul Soliva822ec315bdbe
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index f92a80572f6..b978c9228d6 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -5801,7 +5801,22 @@ def _client_cache_key(
api_key_key = _runtime_cache_discriminator("api_key", api_key or "")
# Profile home leads the key: callers that omit api_key (pool / Nous auth.json paths) would
# otherwise share one client across multiplex profiles holding different credentials.
- return (hermes_home_key(), provider, async_mode, base_url or "", api_key_key, api_mode or "", runtime_key, is_vision, task_key, pool_hint, model_key)
+ return (hermes_home_key(), provider, async_mode, base_url or "", api_key_key, api_mode or "", runtime_key, is_vision, task_key, pool_hint, model_key,
+ _borrowed_main_credential_key(provider, base_url, api_key, runtime))
+
+
+def _borrowed_main_credential_key(provider: str, base_url: Optional[str], api_key: Any, runtime: Dict[str, Any]) -> tuple:
+ """What a keyless ``custom`` route borrows from the main runtime when its client is built.
+
+ 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":
+ 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", "")))
def _current_event_loop() -> Any:
diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.py
index 6f0267478d3..ab66356ecdb 100644
--- a/tests/agent/test_auxiliary_client.py
+++ b/tests/agent/test_auxiliary_client.py
@@ -4843,6 +4843,32 @@ class TestCustomEndpointApiKeyInheritance:
assert client.api_key != "sk-main-config-key"
+ @pytest.mark.parametrize("aux_base_url", ["https://gw.example.com/v1", None],
+ ids=["explicit-base-url", "runtime-endpoint"])
+ def test_cached_client_follows_the_live_main_credential(self, monkeypatch, aux_base_url):
+ """Auxiliary requests go through the client cache: a client built while one runtime was
+ live must not be served, carrying that runtime's key, once the live runtime changed."""
+ 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)
+ finally:
+ aux.reset_runtime_main(token)
+ served.append(getattr(client, "api_key", None))
+ finally:
+ aux.shutdown_cached_clients()
+ aux.clear_runtime_main()
+
+ assert served[:2] == ["sk-first-session", "sk-second-session"]
+ assert served[2] not in ("sk-first-session", "sk-second-session")
+
class TestNoProgressTimeoutTaskConfigGating:
"""#108104: ``auxiliary.<task>.no_progress_timeout`` must only reach the request kwargs