Skip to content

The agent that grows with you

fix(auth): don't reseed root's nous grant into a profile that owns nous rows

kshitijk4poore7bab8eb18bf
diff --git a/agent/credential_pool.py b/agent/credential_pool.py
index 648afc92279..74d6e63d69d 100644
--- a/agent/credential_pool.py
+++ b/agent/credential_pool.py
@@ -837,10 +837,7 @@ def _profile_owns_pool_provider(provider: str) -> bool:
"""
# Classic mode (profile == root) has no root fallback, so the answer is always "owns";
# skip the per-call auth.json re-read on this hot load_pool path.
- try:
- if auth_mod._global_auth_file_path() is None:
- return True
- except Exception:
+ if auth_mod._global_auth_file_path() is None:
return True
try:
pool = _load_auth_store().get("credential_pool")
@@ -2605,7 +2602,16 @@ def _seed_anthropic_singletons(seed: _Seeder) -> None:
def _seed_nous_singleton(seed: _Seeder, auth_store: Dict[str, Any]) -> None:
- state = _load_provider_state(auth_store, "nous")
+ 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 _profile_owns_pool_provider("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.
+ return
has_runtime_material = bool(
isinstance(state, dict)
and (str(state.get("access_token") or "").strip() or str(state.get("agent_key") or "").strip())
diff --git a/tests/agent/test_credential_pool_profile_oauth_fork.py b/tests/agent/test_credential_pool_profile_oauth_fork.py
index c88cc061bea..17ecdd917f2 100644
--- a/tests/agent/test_credential_pool_profile_oauth_fork.py
+++ b/tests/agent/test_credential_pool_profile_oauth_fork.py
@@ -160,10 +160,13 @@ def test_strip_helper_drops_cloned_nous_refresh_grant(tmp_path, fleet):
agent_key-only nous rows carry no single-use grant and must survive both strip and heal.
"""
+ from agent.credential_pool import load_pool
from hermes_cli.auth import heal_forked_single_use_oauth_grants, strip_cloned_single_use_oauth_grants
- pdir = tmp_path / "p"
- pdir.mkdir()
grant = {"access_token": "AT1", "refresh_token": "RT1", "agent_key": "AK"}
+ root_store = json.loads((fleet["root"] / "auth.json").read_text())
+ root_store["providers"]["nous"] = dict(grant)
+ (fleet["root"] / "auth.json").write_text(json.dumps(root_store))
+ pdir = _profile(fleet, "nousstrip")
ak_row = {"id": "ak", "source": "device_code", "auth_type": "oauth", "agent_key": "PAK"}
(pdir / "auth.json").write_text(json.dumps({
"version": 1,
@@ -174,6 +177,12 @@ def test_strip_helper_drops_cloned_nous_refresh_grant(tmp_path, fleet):
store = json.loads((pdir / "auth.json").read_text())
assert (summary["pool"], summary["providers"]) == (["nous"], ["nous"])
assert store["credential_pool"]["nous"] == [ak_row] and "nous" not in store["providers"]
+ # Mixed shape: the surviving ak row keeps the profile "owning" nous, so the next load must
+ # not re-seed root's single-use RT from the global-root providers fallback (fork again).
+ fleet["use"](pdir)
+ load_pool("nous")
+ rows = json.loads((pdir / "auth.json").read_text())["credential_pool"]["nous"]
+ assert all(row.get("refresh_token") != "RT1" for row in rows), rows
# Heal: a fork living only in the providers block (flat nous tokens, shared RT) is dropped.
(fleet["root"] / "auth.json").write_text(json.dumps({"version": 1, "providers": {"nous": dict(grant)}}))