Skip to content

The agent that grows with you

fix(auth): a profile's auth add never copies root's login into the profile

unsupportedpastelsbf23bccef38f
diff --git a/agent/credential_pool.py b/agent/credential_pool.py
index d6df1850b9f..9686776beb0 100644
--- a/agent/credential_pool.py
+++ b/agent/credential_pool.py
@@ -29,6 +29,7 @@ from agent.credential_persistence import (
sanitize_borrowed_credential_payload,
)
import hermes_cli.auth as auth_mod
+from hermes_cli.auth_oauth_grants import owned_profile_reads_root_state
from hermes_cli.auth import (
CODEX_ACCESS_TOKEN_REFRESH_SKEW_SECONDS,
PROVIDER_REGISTRY,
@@ -2639,14 +2640,8 @@ def _seed_anthropic_singletons(seed: _Seeder) -> None:
def _seed_nous_singleton(seed: _Seeder, auth_store: Dict[str, Any]) -> None:
state, source_path = _load_provider_state_with_source(auth_store, "nous")
- global_root = _global_auth_file_path()
- if (
- source_path is not None and global_root is not None and _same_path(source_path, global_root)
- and _store_owns_pool_provider(auth_store, "nous")
- ):
- # A profile that owns local nous rows (e.g. an agent_key-only row surviving a
- # fork strip/heal) must not re-seed root's single-use refresh token into its
- # own pool from the global-root fallback: that re-creates the fork.
+ if owned_profile_reads_root_state(auth_store, "nous", source_path):
+ # e.g. an agent_key-only row surviving a fork strip/heal
return
has_runtime_material = bool(
isinstance(state, dict)
@@ -2808,7 +2803,9 @@ def _seed_tokens_singleton(seed: _Seeder, auth_store: Dict[str, Any]) -> None:
Codex CLI / VS Code causes refresh_token_reused races. Adoption is an
explicit one-time prompt via `hermes auth openai-codex`.
"""
- state = _load_provider_state(auth_store, seed.provider)
+ state, source_path = _load_provider_state_with_source(auth_store, seed.provider)
+ if owned_profile_reads_root_state(auth_store, seed.provider, source_path):
+ return
tokens = state.get("tokens") if isinstance(state, dict) else None
if not (isinstance(tokens, dict) and tokens.get("access_token")):
return
diff --git a/agent/credential_pool_admin.py b/agent/credential_pool_admin.py
index 1d94ae9d0a2..2c14c81879b 100644
--- a/agent/credential_pool_admin.py
+++ b/agent/credential_pool_admin.py
@@ -135,13 +135,16 @@ class CredentialPoolAdminMixin:
if borrowed_ids or profile_borrows:
# ``hermes -p <profile> auth add <single-use provider>``: a fresh
# login inside the profile is the profile's OWN credential, so
- # it goes to the profile store, never to root. Borrowed root
- # rows stay out of it — a copy would fork their single-use
- # refresh token (#100339). Once the profile owns rows, the root
- # fallback for this provider is shadowed.
- self._entries = [e for e in self._entries if e.id not in borrowed_ids]
+ # it goes to the profile store, never to root. Only that row is
+ # written: the rest of this pool is root's (its rows, or its
+ # ``providers.<id>`` login seeded through the fallback), and a
+ # copy would fork that single-use refresh token (#100339). Rows
+ # the profile seeds from its own sources come back on the next
+ # load. Once the profile owns rows, the root fallback for this
+ # provider is shadowed.
+ self._entries = [entry]
written = write_credential_pool(
- self.provider, [e.to_dict() for e in self._entries],
+ self.provider, [entry.to_dict()],
token_bases=self._persisted_token_pairs,
)
self._persisted_token_pairs = auth_mod._token_pairs_by_id(written)
diff --git a/hermes_cli/auth_oauth_grants.py b/hermes_cli/auth_oauth_grants.py
index d3e32e71dc4..1d2dd982b7f 100644
--- a/hermes_cli/auth_oauth_grants.py
+++ b/hermes_cli/auth_oauth_grants.py
@@ -561,6 +561,19 @@ def _pool_rows(store: Dict[str, Any], provider_id: str) -> Tuple[Any, List[Any]]
return pool, rows if isinstance(rows, list) else []
+def owned_profile_reads_root_state(
+ auth_store: Dict[str, Any], provider_id: str, source_path: Optional[Path]) -> bool:
+ """True when a profile that owns its own pool rows would seed ``providers.<id>`` from ROOT.
+
+ Seeding that state re-copies root's single-use refresh token into the profile's pool, which
+ is the fork #100339 forbids; the singleton seeders skip it."""
+ from hermes_cli.auth import _global_auth_file_path, _same_path
+ global_root = _global_auth_file_path()
+ return bool(
+ source_path is not None and global_root is not None and _same_path(source_path, global_root)
+ and _pool_rows(auth_store, provider_id)[1])
+
+
def _adopt_if_fresher(
target: Dict[str, Any], candidate: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""*target* carrying *candidate*'s pair when the candidate rotated later, else None."""
diff --git a/tests/agent/test_credential_pool_profile_oauth_fork.py b/tests/agent/test_credential_pool_profile_oauth_fork.py
index e4640898ee1..5eeac562476 100644
--- a/tests/agent/test_credential_pool_profile_oauth_fork.py
+++ b/tests/agent/test_credential_pool_profile_oauth_fork.py
@@ -428,6 +428,36 @@ def test_anthropic_auth_add_survives_the_next_load_beside_a_root_singleton(fleet
assert singleton.read_bytes() == singleton_before
[email protected]("provider,root_login", [
+ ("nous", {"access_token": "at-root", "refresh_token": "rt-root", "agent_key": "ak-root"}),
+ ("openai-codex", {"tokens": {"access_token": "at-root", "refresh_token": "rt-root"}}),
+ ("xai-oauth", {"tokens": {"access_token": "at-root", "refresh_token": "rt-root"}}),
+], ids=["nous", "openai-codex", "xai-oauth"])
+def test_profile_auth_add_never_copies_roots_provider_login(fleet: dict, provider: str, root_login: dict) -> None:
+ """Root's login may live only in ``providers.<id>``, with no pool rows yet. A profile sees it
+ through the root fallback; neither the profile's first ``auth add`` nor a later load may write
+ that single-use refresh token into the profile's store."""
+ from agent.credential_pool import AUTH_TYPE_OAUTH, PooledCredential, load_pool
+
+ root_file = fleet["root"] / "auth.json"
+ store = json.loads(root_file.read_text(encoding="utf-8"))
+ store["credential_pool"].pop(provider, None)
+ store["providers"][provider] = root_login
+ root_file.write_text(json.dumps(store), encoding="utf-8")
+ kid = _profile(fleet, "kid")
+
+ fleet["use"](kid)
+ load_pool(provider).add_entry(PooledCredential(
+ provider=provider, id="mine01", label="mine", auth_type=AUTH_TYPE_OAUTH, priority=0,
+ source="manual:device_code", access_token="at-mine", refresh_token="rt-mine",
+ ))
+ assert "rt-root" not in (kid / "auth.json").read_text(encoding="utf-8")
+
+ fleet["use"](kid) # a later process
+ assert "mine01" in [e.id for e in load_pool(provider).entries()]
+ assert "rt-root" not in (kid / "auth.json").read_text(encoding="utf-8")
+
+
def test_auth_add_never_reports_added_for_a_row_the_store_did_not_keep(fleet, monkeypatch, capsys):
""""Added" is printed only for a credential the store holds afterwards."""
from argparse import Namespace