The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(anthropic): rotate the published aux key in place so the turn thread sees it
kshitijk4poord79480f12868
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.pyindex 7404bccb1d0..27b34bd470a 100644--- a/agent/auxiliary_client.py+++ b/agent/auxiliary_client.py@@ -2786,6 +2786,18 @@ def set_runtime_main(return token+def rotate_runtime_main_api_key(old: Any, new: Any) -> None:+ """Swap a revoked main key for its replacement in the published runtime, IN PLACE.++ A refresh can run inside a request worker's copied Context, where rebinding the ContextVar+ would stay invisible to the turn thread; the published dict is shared, so mutating it is not.+ """+ runtime = _RUNTIME_MAIN_CONTEXT.get()+ if isinstance(runtime, dict) and runtime.get("api_key") == old:+ runtime["api_key"] = _normalize_api_key(new)+ _publish_runtime_main_mirrors(tuple(runtime[field] for field in _MAIN_RUNTIME_FIELDS))++def reset_runtime_main(token: contextvars.Token) -> None:"""Restore the runtime binding that preceded one scoped turn."""if token is None:diff --git a/agent/client_lifecycle.py b/agent/client_lifecycle.pyindex 45dc77e97d9..6aa9d72ff10 100644--- a/agent/client_lifecycle.py+++ b/agent/client_lifecycle.py@@ -928,10 +928,9 @@ class ClientLifecycleMixin: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)+ # Mutated in place: the refresh may run in a request worker's copied Context.+ from agent.auxiliary_client import rotate_runtime_main_api_key+ rotate_runtime_main_api_key(old_token, new_token)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 a313d27d637..409a54b30eb 100644--- a/tests/agent/test_anthropic_third_party_oauth_guard.py+++ b/tests/agent/test_anthropic_third_party_oauth_guard.py@@ -18,6 +18,7 @@ This test class covers all FIVE sites that assign ``_is_anthropic_oauth``:from __future__ import annotations+import threadingfrom unittest.mock import MagicMock, patchimport pytest@@ -147,6 +148,7 @@ class TestOAuthFlagOnRefresh:"""Production order: the turn publishes its aux runtime BEFORE the first request triggersthe silent refresh, so same-turn `auto` aux calls must still pick up the new token."""from agent import auxiliary_client as aux+ from agent.chat_completion_helpers import _context_thread_targetfrom agent.turn_context import _publish_runtime_mainold, new = "sk-ant...aaaa", "sk-ant...bbbb"@@ -161,13 +163,19 @@ class TestOAuthFlagOnRefresh:seen["api_key"] = explicit_api_keyreturn MagicMock(), model+ refreshed = []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+ # The refresh runs in the request worker's copied Context; the turn thread reads after.+ worker = threading.Thread(target=_context_thread_target(+ lambda: refreshed.append(agent._try_refresh_anthropic_client_credentials())))+ worker.start()+ worker.join()+ assert refreshed == [True]runtime = aux._normalize_main_runtime(None)assert runtime.get("api_key") == newwith (