Skip to content

The agent that grows with you

fix(aux): republish the legacy mirrors only from the runtime they were published from

kshitijk4poordd31854898f8
diff --git a/agent/auxiliary_key_rotation.py b/agent/auxiliary_key_rotation.py
index 6c1a83d2064..7bdb5a403cc 100644
--- a/agent/auxiliary_key_rotation.py
+++ b/agent/auxiliary_key_rotation.py
@@ -16,13 +16,15 @@ def rotate_runtime_main_api_key(old: Any, new: Any) -> None:
would stay invisible to the turn thread; the published dict is shared, so mutating it is not.
"""
from agent.auxiliary_client import (
- _MAIN_RUNTIME_FIELDS, _RUNTIME_MAIN_API_KEY, _RUNTIME_MAIN_CONTEXT, _normalize_api_key,
+ _RUNTIME_MAIN_COMPAT_SNAPSHOT, _MAIN_RUNTIME_FIELDS, _RUNTIME_MAIN_CONTEXT, _normalize_api_key,
_publish_runtime_main_mirrors,
)
runtime = _RUNTIME_MAIN_CONTEXT.get()
if isinstance(runtime, dict) and runtime.get("api_key") == old:
+ # Only the runtime the mirrors were published from may republish them: a scoped runtime (or another
+ # session's) that happens to share the old key must not overwrite them with its own route.
+ published = tuple(runtime.get(field, "") for field in _MAIN_RUNTIME_FIELDS) == _RUNTIME_MAIN_COMPAT_SNAPSHOT
runtime["api_key"] = _normalize_api_key(new)
- # A scoped runtime is a normalized copy (empty fields dropped) that must not reach the mirrors.
- if _RUNTIME_MAIN_API_KEY == old:
+ if published:
_publish_runtime_main_mirrors(tuple(runtime.get(field, "") for field in _MAIN_RUNTIME_FIELDS))
diff --git a/tests/agent/test_anthropic_third_party_oauth_guard.py b/tests/agent/test_anthropic_third_party_oauth_guard.py
index b93e67ed11e..c0a12db674c 100644
--- a/tests/agent/test_anthropic_third_party_oauth_guard.py
+++ b/tests/agent/test_anthropic_third_party_oauth_guard.py
@@ -210,6 +210,11 @@ class TestOAuthFlagOnRefresh:
rotate_runtime_main_api_key(old, new)
assert aux._RUNTIME_MAIN_CONTEXT.get()["api_key"] == new
assert (aux._RUNTIME_MAIN_MODEL, aux._RUNTIME_MAIN_API_KEY) == (agent.model, new)
+ # Even a scope sharing the still-published old key must not republish its own route.
+ aux.set_runtime_main("anthropic", agent.model, api_key=old, api_mode="anthropic_messages")
+ with aux.scoped_runtime_main({"provider": "anthropic", "api_key": old, "model": "m"}):
+ rotate_runtime_main_api_key(old, new)
+ assert (aux._RUNTIME_MAIN_MODEL, aux._RUNTIME_MAIN_API_KEY) == (agent.model, old)
finally:
aux.clear_runtime_main()