Skip to content

The agent that grows with you

refactor(aux): move rotate_runtime_main_api_key into auxiliary_key_rotation to keep auxiliary_client.py under its size cap

kshitijk4poor5018787e7b1d
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 32510883702..7404bccb1d0 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -2786,20 +2786,6 @@ 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)
- # A scoped runtime is a normalized copy (empty fields dropped) that must not reach the mirrors.
- if _RUNTIME_MAIN_API_KEY == old:
- _publish_runtime_main_mirrors(tuple(runtime.get(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/auxiliary_key_rotation.py b/agent/auxiliary_key_rotation.py
new file mode 100644
index 00000000000..6c1a83d2064
--- /dev/null
+++ b/agent/auxiliary_key_rotation.py
@@ -0,0 +1,28 @@
+"""Rotate the published auxiliary main-runtime key after a credential refresh.
+
+Split from ``agent.auxiliary_client`` (facade size cap). It reads the facade's runtime-main state
+at call time, so the late import below always sees the current legacy mirror values.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+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.
+ """
+ from agent.auxiliary_client import (
+ _MAIN_RUNTIME_FIELDS, _RUNTIME_MAIN_API_KEY, _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:
+ 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:
+ _publish_runtime_main_mirrors(tuple(runtime.get(field, "") for field in _MAIN_RUNTIME_FIELDS))
diff --git a/agent/client_lifecycle.py b/agent/client_lifecycle.py
index 6aa9d72ff10..aeac152fcea 100644
--- a/agent/client_lifecycle.py
+++ b/agent/client_lifecycle.py
@@ -929,7 +929,7 @@ class ClientLifecycleMixin:
# 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.
# Mutated in place: the refresh may run in a request worker's copied Context.
- from agent.auxiliary_client import rotate_runtime_main_api_key
+ from agent.auxiliary_key_rotation import rotate_runtime_main_api_key
rotate_runtime_main_api_key(old_token, new_token)
return True