The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): evict rotated auxiliary provider clients
KoNit-K10684127cda6
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.pyindex 22b69e3e309..075249783e1 100644--- a/agent/auxiliary_client.py+++ b/agent/auxiliary_client.py@@ -3300,7 +3300,8 @@ def _evict_cached_clients(provider: str) -> None:"""Drop cached auxiliary clients for a provider so fresh creds are used."""normalized = _normalize_aux_provider(provider)with _client_cache_lock:- for key in [key for key in _client_cache if _normalize_aux_provider(str(key[0])) == normalized]:+ # Cache keys begin with the profile home; the provider is the second component.+ for key in [key for key in _client_cache if _normalize_aux_provider(str(key[1])) == normalized]:client = _client_cache.get(key, (None, None, None))[0]if client is not None:_close_cached_client(client)diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.pyindex 1d6beb1506a..7e5c7d9b353 100644--- a/tests/agent/test_auxiliary_client.py+++ b/tests/agent/test_auxiliary_client.py@@ -2862,7 +2862,8 @@ class TestAuxiliaryAuthRefreshRetry:def test_refresh_provider_credentials_force_refreshes_anthropic_oauth_and_evicts_cache(self, monkeypatch):stale_client = MagicMock()- cache_key = ("anthropic", False, None, None, None)+ from agent.auxiliary_client import _client_cache_key+ cache_key = _client_cache_key("anthropic", async_mode=False)monkeypatch.setenv("ANTHROPIC_TOKEN", "")monkeypatch.setenv("CLAUDE_CODE_OAUTH_TOKEN", "")@@ -2903,7 +2904,8 @@ class TestAuxiliaryAuthRefreshRetry:through to the final `return False` and the stale client (and itsdead token) stayed cached until process restart."""stale_client = MagicMock()- cache_key = ("vertex", False, None, None, None)+ from agent.auxiliary_client import _client_cache_key+ cache_key = _client_cache_key("vertex", async_mode=False)with (patch("agent.auxiliary_client._client_cache", {cache_key: (stale_client, "google/gemini-3-flash-preview", None)}),diff --git a/tests/agent/test_auxiliary_client_cache_eviction.py b/tests/agent/test_auxiliary_client_cache_eviction.pynew file mode 100644index 00000000000..d945c750255--- /dev/null+++ b/tests/agent/test_auxiliary_client_cache_eviction.py@@ -0,0 +1,55 @@+"""Regression coverage for provider-scoped auxiliary cache eviction (#113022)."""++from types import SimpleNamespace+from unittest.mock import MagicMock, patch++import agent.auxiliary_client as aux+++def _cache_entry() -> tuple[MagicMock, str, None]:+ return (MagicMock(), "test-model", None)+++def test_evict_cached_clients_matches_provider_after_profile_home_key(monkeypatch):+ """OAuth rotation must evict both sync and async entries for its provider."""+ anthropic_sync = _cache_entry()+ anthropic_async = _cache_entry()+ other_provider = _cache_entry()+ sync_key = aux._client_cache_key("anthropic", async_mode=False)+ async_key = aux._client_cache_key("anthropic", async_mode=True)+ other_key = aux._client_cache_key("openai-codex", async_mode=False)+ monkeypatch.setattr(aux, "_client_cache", {+ sync_key: anthropic_sync,+ async_key: anthropic_async,+ other_key: other_provider,+ })++ aux._evict_cached_clients("anthropic")++ assert sync_key not in aux._client_cache+ assert async_key not in aux._client_cache+ assert other_key in aux._client_cache+ anthropic_sync[0].close.assert_called_once()+ anthropic_async[0].close.assert_called_once()+ other_provider[0].close.assert_not_called()+++def test_pool_rotation_evicts_client_built_with_revoked_credential(monkeypatch):+ """A 401 pool rotation drops the old provider client before retry/fallback."""+ stale_entry = _cache_entry()+ stale_key = aux._client_cache_key("anthropic", async_mode=False)+ monkeypatch.setattr(aux, "_client_cache", {stale_key: stale_entry})++ pool = MagicMock()+ pool.has_credentials.return_value = True+ pool.try_refresh_current.return_value = None+ pool.mark_exhausted_and_rotate.return_value = SimpleNamespace(id="fresh-oauth-entry")+ auth_error = Exception("revoked OAuth token")+ auth_error.status_code = 401++ with patch("agent.auxiliary_client.load_pool", return_value=pool):+ assert aux._recover_provider_pool("anthropic", auth_error, failed_api_key="revoked-token") is True++ assert stale_key not in aux._client_cache+ stale_entry[0].close.assert_called_once()+ pool.mark_exhausted_and_rotate.assert_called_once()