Skip to content

The agent that grows with you

fix(auth): return the post-persist entry from _adopt; give the adoption guard test teeth

kshitijk4poor9e66e405cfbf
diff --git a/agent/credential_pool.py b/agent/credential_pool.py
index 8d90bb8ca84..6f86519d76e 100644
--- a/agent/credential_pool.py
+++ b/agent/credential_pool.py
@@ -1135,13 +1135,14 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin)
for index, entry in enumerate(self._entries):
row = rows.get(entry.id)
pair = self._persisted_token_pairs.get(entry.id, (None, None))
+ # Reference-only rows are intentionally secret-free on disk; never dehydrate
+ # their live in-memory credential while adopting a concurrent generation.
if row is None or not any(pair):
continue
# Adopt only rows the store overrode with a peer's newer pair; re-hydrating an
- # unchanged row would drop in-memory-only runtime fields to_dict() omits.
+ # unchanged row would replace the live object (and pull peer cooldown state
+ # merged into the written row) on every ordinary flush.
if pair != (entry.access_token, entry.refresh_token):
- # Reference-only rows are intentionally secret-free on disk; never dehydrate
- # their live in-memory credential while adopting a concurrent generation.
self._entries[index] = PooledCredential.from_dict(self.provider, row)
def _adopt(self, entry: PooledCredential, *, persist: bool = True, **updates: Any) -> PooledCredential:
@@ -1150,6 +1151,9 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin)
self._replace_entry(entry, updated)
if persist:
self._persist()
+ # _persist may have swapped in a peer's newer token generation; hand callers
+ # the live entry so they don't rebind the client to the stale pair.
+ return self._find(lambda e: e.id == updated.id) or updated
return updated
def _quarantine_sources(self, entry: PooledCredential, sources: Set[str]) -> None:
diff --git a/tests/agent/test_credential_pool_profile_oauth_fork.py b/tests/agent/test_credential_pool_profile_oauth_fork.py
index 08416cd7b72..f762ade0129 100644
--- a/tests/agent/test_credential_pool_profile_oauth_fork.py
+++ b/tests/agent/test_credential_pool_profile_oauth_fork.py
@@ -767,7 +767,15 @@ def test_stale_terminal_verdict_cannot_kill_peer_token_generation(fleet, borrowe
fleet["use"](_profile(fleet, "stale-dead") if borrowed else fleet["root"])
stale, stale_billing, peer = load_pool("anthropic"), load_pool("anthropic"), load_pool("anthropic")
+ stale_adopter = load_pool("anthropic")
assert peer.try_refresh_matching(credential_id="abc123").refresh_token == "sk-ant-ort-RT1"
+ # An ordinary flush whose pair already matches disk must not re-hydrate the live object.
+ live = peer._entries[0]
+ peer._persist()
+ assert peer._entries[0] is live
+ # A stale writer's _adopt hands back the post-persist entry carrying the peer's pair.
+ adopted = stale_adopter._adopt(stale_adopter._entries[0], last_status=None)
+ assert adopted.refresh_token == "sk-ant-ort-RT1" and adopted is stale_adopter._entries[0]
stale.mark_exhausted_and_rotate(
status_code=401, credential_id="abc123",