Skip to content

The agent that grows with you

fix(auth): carry pool-row lineage into the provider-block heal

Teknium13c580422c0b
diff --git a/hermes_cli/auth_oauth_grants.py b/hermes_cli/auth_oauth_grants.py
index 2b4ec8a38fe..71932322cdc 100644
--- a/hermes_cli/auth_oauth_grants.py
+++ b/hermes_cli/auth_oauth_grants.py
@@ -259,9 +259,15 @@ def heal_forked_single_use_oauth_grants(provider_id: str) -> Optional[Dict[str,
def _heal_forked_provider_block(
- profile_store: Dict[str, Any], root_store: Dict[str, Any], provider_id: str) -> Optional[bool]:
+ profile_store: Dict[str, Any], root_store: Dict[str, Any], provider_id: str,
+ lineage_proven: bool = False) -> Optional[bool]:
"""Consolidate a forked ``providers.<id>`` device-code block into root.
+ *lineage_proven* carries the pool-row verdict: a profile row that matched root by copied id
+ or shared tokens proves the fork even after both sides rotated past token equality. Root's
+ ``load_pool()`` re-seeds its ``device_code`` row FROM this block, so leaving root's block on
+ the spent pair would undo the pool-row heal on the next root load.
+
Returns None when nothing matched, False when the profile copy was dropped (root already
newest), True when the profile copy was fresher and was adopted into root.
"""
@@ -278,8 +284,9 @@ def _heal_forked_provider_block(
p_flat, r_flat = _flat(p_block), _flat(r_block)
# Provider blocks have no stable pool-row ID. Without a shared token pair
- # component, a common account is insufficient evidence of a copied grant.
- if not any(
+ # component (or lineage proven by the pool rows), a common account is
+ # insufficient evidence of a copied grant.
+ if not lineage_proven and not any(
p_flat.get(key) and p_flat.get(key) == r_flat.get(key)
for key in ("access_token", "refresh_token")
):
@@ -321,6 +328,7 @@ class _HealPass:
self.summary: Dict[str, Any] = {
"adopted": False, "stripped_ids": [], "files": [], "providers_block": False}
self.profile_changed = self.root_changed = False
+ self.lineage_proven = False # a profile pool row matched root by copied id / shared tokens
self.p_pool, self.p_rows = _pool_rows(profile_store, provider_id)
self.r_pool, self.r_rows = _pool_rows(root_store, provider_id)
self.r_oauth = [r for r in self.r_rows if _is_oauth_pool_payload(r)]
@@ -349,6 +357,7 @@ class _HealPass:
continue
match_idx = _find_root_counterpart(row, self.r_rows)
if match_idx is not None:
+ self.lineage_proven = True
self._adopt_root_row(match_idx, row)
# No root pool counterpart. Root's grant may live only in its .anthropic_oauth.json
# (the ``hermes auth`` PKCE shape); a profile hermes_pkce-family row is its copy.
@@ -371,7 +380,7 @@ class _HealPass:
if self.provider_id not in _DEVICE_CODE_BLOCK_PROVIDERS:
return
block_result = _heal_forked_provider_block(
- self.profile_store, self.root_store, self.provider_id)
+ self.profile_store, self.root_store, self.provider_id, self.lineage_proven)
if block_result is not None:
self.profile_changed = self.summary["providers_block"] = True
if block_result:
diff --git a/tests/agent/test_credential_pool_profile_oauth_fork.py b/tests/agent/test_credential_pool_profile_oauth_fork.py
index cf612f1064e..0c96c4431e4 100644
--- a/tests/agent/test_credential_pool_profile_oauth_fork.py
+++ b/tests/agent/test_credential_pool_profile_oauth_fork.py
@@ -466,6 +466,40 @@ def test_heal_preserves_independent_grants_for_same_account(fleet, shape, claims
assert root.read_bytes() == before[0]
+def test_heal_rotated_fork_moves_provider_block_with_the_pool_row(fleet):
+ """Copied pool-row id proves the fork even after both sides rotated past token equality.
+
+ Root's load_pool() re-seeds its device_code row FROM providers.<id>.tokens, so root's block
+ must carry the fresher pair too or the next root load resurrects the spent one.
+ """
+ from agent.credential_pool import load_pool
+ from hermes_cli.auth import heal_forked_single_use_oauth_grants
+
+ def store(tag, exp_off):
+ tokens = {"access_token": "at-" + tag, "refresh_token": "rt-" + tag}
+ issued = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(time.time() + exp_off - 3600))
+ return {"version": 1,
+ "providers": {"openai-codex": {"tokens": tokens, "last_refresh": issued}},
+ "credential_pool": {"openai-codex": [{
+ "id": "shared-id", "auth_type": "oauth", "source": "device_code", **tokens,
+ "expires_at_ms": int((time.time() + exp_off) * 1000)}]}}
+
+ root = fleet["root"] / "auth.json"
+ kid = _profile(fleet, "rotated")
+ kid.mkdir(parents=True, exist_ok=True)
+ root.write_text(json.dumps(store("old", 100)))
+ (kid / "auth.json").write_text(json.dumps(store("new", 3600)))
+ fleet["use"](kid)
+ assert heal_forked_single_use_oauth_grants("openai-codex")["adopted"] is True
+ r, p = json.loads(root.read_text()), json.loads((kid / "auth.json").read_text())
+ assert r["credential_pool"]["openai-codex"][0]["refresh_token"] == "rt-new"
+ assert r["providers"]["openai-codex"]["tokens"]["refresh_token"] == "rt-new"
+ assert "openai-codex" not in p["providers"]
+ assert "openai-codex" not in p.get("credential_pool", {})
+ fleet["use"](fleet["root"])
+ assert {e.refresh_token for e in load_pool("openai-codex").entries()} == {"rt-new"}
+
+
def test_heal_leaves_a_different_account_alone(fleet):
"""A profile row whose JWT identity names ANOTHER account is not root's grant."""
import base64