The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): persist profile single-use OAuth credentials when root store is empty
0xAlyDevf383e66b3e58
diff --git a/agent/credential_pool.py b/agent/credential_pool.pyindex d6df1850b9f..c19a60632e4 100644--- a/agent/credential_pool.py+++ b/agent/credential_pool.py@@ -820,7 +820,8 @@ def _write_through_provider_state_to_global_root(def _singleton_target_for_entry(pool: "CredentialPool", entry: "PooledCredential") -> Optional[Path]:"""Root ``.anthropic_oauth.json`` when *entry* is a borrowed hermes_pkce row, else None."""- if entry.source != "hermes_pkce" or entry.id not in getattr(pool, "_borrowed_root_ids", ()):+ borrowed_ids = getattr(pool, "_borrowed_root_ids", None) or ()+ if entry.source != "hermes_pkce" or entry.id not in borrowed_ids:return Nonetry:from agent.anthropic_credentials import _root_hermes_oauth_file@@ -1010,7 +1011,7 @@ class CredentialPool(CredentialPoolAdminMixin, CredentialPoolModelCooldownMixin)self._current_id: Optional[str] = None# Ids of rows read via the global-root fallback (single-use OAuth# providers only); set by load_pool(), consumed by add_entry().- self._borrowed_root_ids: Set[str] = set()+ self._borrowed_root_ids: Optional[Set[str]] = Noneself._persisted_token_pairs: Dict[str, Tuple[Any, Any]] = {}self._strategy = get_pool_strategy(provider)# RLock: _replace_entry/_persist self-acquire it so the DEFERREDdiff --git a/agent/credential_pool_admin.py b/agent/credential_pool_admin.pyindex e773cc7cd47..a073adf3256 100644--- a/agent/credential_pool_admin.py+++ b/agent/credential_pool_admin.py@@ -115,19 +115,22 @@ class CredentialPoolAdminMixin:entry = replace(entry, priority=_next_priority(self._entries))self._entries.append(entry)borrowed_ids = getattr(self, "_borrowed_root_ids", None)- if borrowed_ids:+ if borrowed_ids is not None:# ``hermes -p <profile> auth add <single-use provider>``: the# profile claims its OWN credential. Persist only profile-owned- # rows — copying the borrowed root grant alongside would fork+ # rows — copying any borrowed root grant alongside would fork# its single-use refresh token (#100339). Once the profile owns# rows, the root fallback for this provider is shadowed.+ # When root has no rows yet (borrowed_ids is empty set), this+ # fresh grant must persist to the profile store rather than+ # routing to the update-only root merge (#103694).self._entries = [e for e in self._entries if e.id not in borrowed_ids]written = write_credential_pool(self.provider, [e.to_dict() for e in self._entries],token_bases=self._persisted_token_pairs,)self._persisted_token_pairs = auth_mod._token_pairs_by_id(written)- self._borrowed_root_ids = set()+ self._borrowed_root_ids = Noneelse:self._persist()return entrydiff --git a/tests/agent/test_credential_pool_profile_oauth_fork.py b/tests/agent/test_credential_pool_profile_oauth_fork.pyindex d776f609a8a..af007739d86 100644--- a/tests/agent/test_credential_pool_profile_oauth_fork.py+++ b/tests/agent/test_credential_pool_profile_oauth_fork.py@@ -367,6 +367,33 @@ def test_profile_auth_add_owns_only_its_own_rows(fleet):assert [e["id"] for e in fleet["rows"](fleet["root"])] == ["abc123"]+def test_profile_auth_add_on_empty_root_persists_to_profile(fleet):+ """Named profile adding a single-use credential when root store has no rows (#103694).++ When root has no rows for the single-use provider (e.g. fresh root or unconfigured+ provider), load_pool() records an empty borrowed_ids set. The profile's add_entry()+ must persist the newly added credential to the profile store instead of silently+ routing to update-only root merge.+ """+ from agent.credential_pool import AUTH_TYPE_OAUTH, PooledCredential, load_pool++ root = fleet["root"]+ (root / "auth.json").write_text(json.dumps({"version": 1, "providers": {}, "credential_pool": {}}))++ fresh_profile = _profile(fleet, "fresh_profile")+ fleet["use"](fresh_profile)+ pool = load_pool("anthropic")+ pool.add_entry(PooledCredential(+ provider="anthropic", id="fresh001", label="fresh_auth", auth_type=AUTH_TYPE_OAUTH,+ priority=0, source="manual:hermes_pkce", access_token="sk-ant-oat01-FRESH",+ refresh_token="rt-fresh",+ ))+ assert fleet["rows"](fresh_profile) is not None, "credential was dropped without persisting (#103694)"+ assert [e["id"] for e in fleet["rows"](fresh_profile)] == ["fresh001"]+ assert fleet["rows"](root) is None+++def test_classic_mode_persist_is_unchanged(fleet):from agent.credential_pool import load_pool