The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: retain rotating credentials during fallback handoff
Tekniumb4464f6fac78
diff --git a/agent/client_lifecycle.py b/agent/client_lifecycle.pyindex 7359c0a11d7..6ef18a0407a 100644--- a/agent/client_lifecycle.py+++ b/agent/client_lifecycle.py@@ -67,23 +67,26 @@ def _valid_credential_pair(api_key: Any, base_url: Any) -> bool:def _swap_fallback_clients(agent, fb_client, fb_provider: str, fb_model: str, fb_base_url: str, fb_api_mode: str) -> None:"""Install the fallback client(s) in place, honoring request_timeout_seconds (None = SDK default)."""timeout = get_provider_request_timeout(fb_provider, fb_model)+ # The SDK exposes an empty/stale api_key when a rotating source is installed.+ key_provider = getattr(fb_client, "_api_key_provider", None)+ credential = key_provider if callable(key_provider) else fb_client.api_keyif fb_api_mode == "anthropic_messages":from agent.anthropic_adapter import build_anthropic_clientfrom agent.anthropic_credentials import resolve_anthropic_token, _is_oauth_tokenis_anthropic = fb_provider == "anthropic"- effective_key = fb_client.api_key or (resolve_anthropic_token() if is_anthropic else None) or ""+ effective_key = credential or (resolve_anthropic_token() if is_anthropic else None) or ""agent.api_key = agent._anthropic_api_key = effective_keyagent._anthropic_base_url = fb_base_urlagent._anthropic_client = build_anthropic_client(effective_key, fb_base_url, timeout=timeout)agent._is_anthropic_oauth = _is_oauth_token(effective_key) if is_anthropic else Falseagent.client, agent._client_kwargs = None, {}return- agent.api_key = fb_client.api_key+ agent.api_key = credentialagent.client = fb_client# Keep provider headers resolve_provider_client() baked into fb_client (SDK: _custom_headers), else# later request-client rebuilds drop them and User-Agent-sentinel providers (Kimi Coding) 403.fb_headers = getattr(fb_client, "_custom_headers", None) or getattr(fb_client, "default_headers", None)- agent._client_kwargs = {"api_key": fb_client.api_key, "base_url": fb_base_url}+ agent._client_kwargs = {"api_key": credential, "base_url": fb_base_url}if fb_headers:agent._client_kwargs["default_headers"] = dict(fb_headers)if timeout is not None:diff --git a/tests/agent/test_fallback_dynamic_credential.py b/tests/agent/test_fallback_dynamic_credential.pynew file mode 100644index 00000000000..7996dbc91c2--- /dev/null+++ b/tests/agent/test_fallback_dynamic_credential.py@@ -0,0 +1,35 @@+"""Fallback rebuilds retain the credential source, not its last token."""+from types import SimpleNamespace++import httpx+import pytest+from openai import OpenAI++from agent.client_lifecycle import _swap_fallback_clients++[email protected]("mode", ["chat_completions", "codex_responses"])[email protected]("dynamic", [False, True])+def test_fallback_rebuild_preserves_rotating_and_static_credentials(mode, dynamic):+ token = ["first-token"]+ source = (lambda: token[0]) if dynamic else token[0]+ captured = []++ def respond(request):+ captured.append(request.headers.get("Authorization"))+ return httpx.Response(200, json={"id": "fixture", "choices": []})++ client = OpenAI(api_key=source, base_url="http://localhost:1234/v1")+ agent = SimpleNamespace()+ _swap_fallback_clients(agent, client, "fixture", "fixture", str(client.base_url), mode)+ rebuilt = OpenAI(**agent._client_kwargs, http_client=httpx.Client(transport=httpx.MockTransport(respond)))+ try:+ for value in ("first-token", "rotated-token"):+ token[0] = value+ rebuilt.chat.completions.create(model="fixture", messages=[])+ expected = ["Bearer first-token", "Bearer rotated-token" if dynamic else "Bearer first-token"]+ assert captured == expected+ assert agent.api_key is source+ finally:+ client.close()+ rebuilt.close()