Skip to content

The agent that grows with you

fix(auth): scope only terminal DEAD verdicts to the stale token generation

kshitijk4poorbadd8f56e449
diff --git a/agent/credential_pool.py b/agent/credential_pool.py
index de9e868b3ac..585b2c27c91 100644
--- a/agent/credential_pool.py
+++ b/agent/credential_pool.py
@@ -1126,7 +1126,7 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin)
self.provider, payloads,
removed_ids=removed_ids,
status_cleared_ids=status_cleared_ids,
- token_bases=getattr(self, "_persisted_token_pairs", {}),
+ token_bases=self._persisted_token_pairs,
)
if written is None:
return
@@ -1135,7 +1135,11 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin)
self._persisted_token_pairs = {row_id: pair for row_id, pair in pairs.items() if any(pair)}
for index, entry in enumerate(self._entries):
row = rows.get(entry.id)
- if row is not None and any(pairs[entry.id]):
+ if row is None or not any(pairs[entry.id]):
+ 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.
+ if pairs[entry.id] != (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)
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.py
index 2057722bcfc..82316bcd28b 100644
--- a/hermes_cli/auth.py
+++ b/hermes_cli/auth.py
@@ -901,6 +901,9 @@ _POOL_TOKEN_GENERATION_FIELDS = (
"access_token", "refresh_token", "expires_at", "expires_at_ms", "expires_in", "obtained_at",
"last_refresh", "agent_key", "agent_key_expires_at", "agent_key_expires_in", "agent_key_id",
"agent_key_obtained_at", "agent_key_reused",
+ # Refresh-coupled metadata: a Nous refresh rewrites scope and the validated
+ # inference route together with the new pair, so they travel with it.
+ "scope", "inference_base_url",
)
@@ -918,7 +921,14 @@ def _merge_pool_row_generation(
base_pair: Optional[Tuple[Any, Any]] = None,
status_cleared: bool = False,
) -> Dict[str, Any]:
- """Keep a newer on-disk token generation authoritative during stale writes."""
+ """Keep a newer on-disk token generation authoritative during stale writes.
+
+ Only a terminal auth verdict (``last_status == dead``) is scoped to the token pair
+ it was observed on; account-wide cooldowns (402 billing, 429 throttle) from the
+ stale writer still apply to the rotated pair and go through the ordinary recency
+ merge in ``_merge_disk_cooldown_state``."""
+ from agent.credential_pool import STATUS_DEAD
+
merge_disk = None if status_cleared else disk_entry
if not isinstance(disk_entry, dict) or base_pair is None:
return _merge_disk_cooldown_state(entry, merge_disk, provider_id)
@@ -932,7 +942,7 @@ def _merge_pool_row_generation(
merged[field] = disk_entry[field]
else:
merged.pop(field, None)
- if not status_cleared:
+ if not status_cleared and entry.get("last_status") == STATUS_DEAD:
for field in _POOL_STATUS_FIELDS:
if field in disk_entry:
merged[field] = disk_entry[field]
diff --git a/tests/agent/test_credential_pool_profile_oauth_fork.py b/tests/agent/test_credential_pool_profile_oauth_fork.py
index 694ec47d548..556741dc9ba 100644
--- a/tests/agent/test_credential_pool_profile_oauth_fork.py
+++ b/tests/agent/test_credential_pool_profile_oauth_fork.py
@@ -761,25 +761,9 @@ def test_persisted_mark_still_re_heals_when_the_root_store_gains_a_grant(fleet):
# ── G. token generation is the authority boundary (#120815) ─────────────
[email protected]("borrowed", [False, True], ids=["owned-rows", "borrowed-root-rows"])
-def test_stale_pool_cannot_roll_back_peer_token_generation(fleet, borrowed):
- from agent.credential_pool import load_pool
-
- fleet["use"](_profile(fleet, "stale-write") if borrowed else fleet["root"])
- stale, peer = load_pool("anthropic"), load_pool("anthropic")
- assert peer.try_refresh_matching(credential_id="abc123").refresh_token == "sk-ant-ort-RT1"
-
- stale.mark_exhausted_and_rotate(status_code=429, credential_id="abc123")
-
- row = fleet["rows"](fleet["root"])[0]
- assert row["refresh_token"] == "sk-ant-ort-RT1"
- selected = stale.select()
- assert selected is not None and selected.refresh_token == "sk-ant-ort-RT1"
-
-
@pytest.mark.parametrize("borrowed", [False, True], ids=["owned-rows", "borrowed-root-rows"])
def test_stale_terminal_verdict_cannot_kill_peer_token_generation(fleet, borrowed):
- from agent.credential_pool import STATUS_DEAD, load_pool
+ from agent.credential_pool import STATUS_DEAD, STATUS_EXHAUSTED, load_pool
fleet["use"](_profile(fleet, "stale-dead") if borrowed else fleet["root"])
stale, peer = load_pool("anthropic"), load_pool("anthropic")
@@ -796,6 +780,13 @@ def test_stale_terminal_verdict_cannot_kill_peer_token_generation(fleet, borrowe
selected = stale.select()
assert selected is not None and selected.refresh_token == "sk-ant-ort-RT1"
+ # Only the terminal verdict was scoped to the old pair: a later account-wide
+ # billing 402 from the same stale writer still applies to the rotated pair.
+ stale.mark_exhausted_and_rotate(status_code=402, credential_id="abc123")
+ row = fleet["rows"](fleet["root"])[0]
+ assert row["refresh_token"] == "sk-ant-ort-RT1"
+ assert row["last_status"] == STATUS_EXHAUSTED
+
def test_terminal_verdict_for_current_token_generation_still_persists(fleet):
from agent.credential_pool import STATUS_DEAD, load_pool