The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor: colocate fallback handoff with client lifecycle
Tekniumd0081d9597bc
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 2b10036d90f..3c418c8e27c 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -1783,34 +1783,6 @@ def _should_skip_fallback_candidate(agent, fb: dict, fb_key: tuple, fb_provider:return False-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)- if fb_api_mode == "anthropic_messages":- from agent.anthropic_adapter import build_anthropic_client- from agent.anthropic_credentials import resolve_anthropic_token, _is_oauth_token- is_anthropic = fb_provider == "anthropic"- effective_key = fb_client.api_key or (resolve_anthropic_token() if is_anthropic else None) or ""- agent.api_key = agent._anthropic_api_key = effective_key- agent._anthropic_base_url = fb_base_url- agent._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 False- agent.client, agent._client_kwargs = None, {}- return- agent.api_key = fb_client.api_key- agent.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}- if fb_headers:- agent._client_kwargs["default_headers"] = dict(fb_headers)- if timeout is not None:- agent._client_kwargs["timeout"] = timeout- # Rebuild now so the timeout applies to the very next request, not only after a rotation rebuild.- agent._replace_primary_openai_client(reason="fallback_timeout_apply")--def _update_fallback_context_compressor(agent) -> None:"""Point compression limits at the fallback model's context window (not the primary's),respecting the explicit model.context_length config override."""@@ -1943,6 +1915,7 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> boolagent._fallback_activated = True_rebind_fallback_credential_pool(agent, fb_provider, fb_model)+ from agent.client_lifecycle import _swap_fallback_clients_swap_fallback_clients(agent, fb_client, fb_provider, fb_model, fb_base_url, fb_api_mode)from agent.agent_runtime_helpers import sync_credential_pool_entry_iddiff --git a/agent/client_lifecycle.py b/agent/client_lifecycle.pyindex 017f8da8ba4..7359c0a11d7 100644--- a/agent/client_lifecycle.py+++ b/agent/client_lifecycle.py@@ -64,6 +64,34 @@ def _valid_credential_pair(api_key: Any, base_url: Any) -> bool:return bool(isinstance(api_key, str) and api_key.strip() and isinstance(base_url, str) and base_url.strip())+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)+ if fb_api_mode == "anthropic_messages":+ from agent.anthropic_adapter import build_anthropic_client+ from agent.anthropic_credentials import resolve_anthropic_token, _is_oauth_token+ is_anthropic = fb_provider == "anthropic"+ effective_key = fb_client.api_key or (resolve_anthropic_token() if is_anthropic else None) or ""+ agent.api_key = agent._anthropic_api_key = effective_key+ agent._anthropic_base_url = fb_base_url+ agent._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 False+ agent.client, agent._client_kwargs = None, {}+ return+ agent.api_key = fb_client.api_key+ agent.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}+ if fb_headers:+ agent._client_kwargs["default_headers"] = dict(fb_headers)+ if timeout is not None:+ agent._client_kwargs["timeout"] = timeout+ # Rebuild now so the timeout applies to the very next request, not only after a rotation rebuild.+ agent._replace_primary_openai_client(reason="fallback_timeout_apply")++class ClientLifecycleMixin:def _close_task_resources(self, task_id: str) -> None:"""Release task resources without treating a shared environment as process ownership."""