The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(anthropic): republish the same-turn aux runtime after a token refresh
kshitijk4poor44c269f1eba7
diff --git a/agent/client_lifecycle.py b/agent/client_lifecycle.pyindex 1981adb844c..45dc77e97d9 100644--- a/agent/client_lifecycle.py+++ b/agent/client_lifecycle.py@@ -926,6 +926,12 @@ class ClientLifecycleMixin:for k in ("api_key", "anthropic_api_key", "compressor_api_key"):if rt.get(k) == old_token:rt[k] = new_token+ # The turn's aux runtime was published before the first request triggered this refresh, so+ # same-turn `auto` aux calls (approvals, goal judge, plugin llm) would still send the revoked token.+ from agent.auxiliary_client import _normalize_main_runtime+ if _normalize_main_runtime(None).get("api_key") == old_token:+ from agent.turn_context import _publish_runtime_main+ _publish_runtime_main(self)return True# ------------------------------------------------------------------ route-derived client configdiff --git a/tests/agent/test_anthropic_third_party_oauth_guard.py b/tests/agent/test_anthropic_third_party_oauth_guard.pyindex 3e3a57395c0..93c6a34b6d9 100644--- a/tests/agent/test_anthropic_third_party_oauth_guard.py+++ b/tests/agent/test_anthropic_third_party_oauth_guard.py@@ -143,6 +143,45 @@ class TestOAuthFlagOnRefresh:assert agent._anthropic_api_key == agent.api_key == agent.context_compressor.api_key == newassert set(agent._primary_runtime.values()) == {new}+ def test_auxiliary_main_route_uses_refreshed_token(self, agent):+ """Production order: the turn publishes its aux runtime BEFORE the first request triggers+ the silent refresh, so same-turn `auto` aux calls must still pick up the new token."""+ from agent import auxiliary_client as aux+ from agent.turn_context import _publish_runtime_main++ old, new = "sk-ant...aaaa", "sk-ant...bbbb"+ agent.api_mode, agent.provider, agent.model = "anthropic_messages", "anthropic", "claude-opus-4-6"+ agent.base_url = agent._anthropic_base_url = "https://api.anthropic.com"+ agent.api_key = agent._anthropic_api_key = old+ agent._anthropic_client = MagicMock()+ agent._is_anthropic_oauth = True+ seen = {}++ def fake_resolve(provider, model, explicit_api_key=None, **kwargs):+ seen["api_key"] = explicit_api_key+ return MagicMock(), model++ try:+ _publish_runtime_main(agent)+ with (+ patch("agent.anthropic_credentials.resolve_anthropic_token", return_value=new),+ patch("agent.anthropic_adapter.build_anthropic_client", return_value=MagicMock()),+ ):+ assert agent._try_refresh_anthropic_client_credentials() is True+ runtime = aux._normalize_main_runtime(None)+ assert runtime.get("api_key") == new+ with (+ patch.object(aux, "resolve_provider_client", side_effect=fake_resolve),+ patch.object(aux, "_is_provider_unhealthy", return_value=False),+ ):+ aux._try_main_provider_route(+ "anthropic", agent.model, runtime.get("base_url", ""), runtime.get("api_key"), "anthropic_messages",+ )+ assert seen["api_key"] == new+ finally:+ aux.clear_runtime_main()++class TestOAuthFlagOnCredentialSwap:"""Site 4 — _swap_credential (credential pool rotation)."""