diff --git a/hermes_cli/auth_nous.py b/hermes_cli/auth_nous.py index 6ec4eb6aaf..3dd320421f 100644 --- a/hermes_cli/auth_nous.py +++ b/hermes_cli/auth_nous.py @@ -426,6 +426,13 @@ def _nous_state_order(a: dict[str, Any], b: dict[str, Any]) -> Optional[int]: return oauth_pair_order(a, b) +def _same_known_nous_identity(a: dict[str, Any], b: dict[str, Any]) -> bool: + """True when *a* and *b* are provably the same identity. An account whose access token carries no + JWT ``sub`` is UNKNOWN, never "the same" as another unknown one.""" + identity = _nous_identity(a) + return identity[1] not in (None, "") and identity == _nous_identity(b) + + def _merge_shared_nous_oauth_state(state: dict[str, Any]) -> bool: """Adopt the shared store's OAuth tokens into a profile-local Nous state when they are NEWER. @@ -444,9 +451,11 @@ def _merge_shared_nous_oauth_state(state: dict[str, Any]) -> bool: return False # a free-tier identity has no refresh token; nothing to merge into an OAuth state refresh_changed = shared_refresh.strip() != str(state.get("refresh_token") or "").strip() order = _nous_state_order(shared, state) + same = _same_known_nous_identity(shared, state) # Unstamped on both sides: no evidence either way, keep the legacy "a different token wins". - if not (order == 1 or (order is None and refresh_changed)): - if order == -1 and refresh_changed and _nous_identity(shared) == _nous_identity(state): + # Never across identities: a profile signed in as one account keeps it (profiles are islands). + if not (same and (order == 1 or (order is None and refresh_changed))): + if order == -1 and refresh_changed and same: _oauth_trace( "nous_shared_store_older_than_profile", shared_refresh_token_fp=_token_fingerprint(shared_refresh), @@ -494,7 +503,7 @@ def _shared_copy_is_newer(held: Optional[dict[str, Any]], state: dict[str, Any], if held is None or is_guest_state(state): return False copy = held if same else held.get(_SHARED_RENEWAL_KEY) - if not isinstance(copy, dict) or _nous_identity(copy) != _nous_identity(state): + if not isinstance(copy, dict) or not _same_known_nous_identity(copy, state): return False return _nous_state_order(copy, state) == 1 diff --git a/tests/hermes_cli/test_nous_shared_store_freshness.py b/tests/hermes_cli/test_nous_shared_store_freshness.py index 1de23f385a..f9b8ad72c9 100644 --- a/tests/hermes_cli/test_nous_shared_store_freshness.py +++ b/tests/hermes_cli/test_nous_shared_store_freshness.py @@ -140,3 +140,22 @@ def test_a_later_rotation_with_a_shorter_ttl_is_still_the_newer_pair(homes, monk assert auth_nous._read_shared_nous_state()["refresh_token"] == "RT3" # never rolled back to RT2 auth_mod.resolve_nous_runtime_credentials(force_refresh=True) assert posted[-1] == "RT3" + + +def test_unknown_identities_never_push_over_the_shared_store(homes): + hermes_home, _ = homes + # No JWT ``sub`` on either side: unknown identities, so a newer profile is not pushed over shared. + newer, older = (_state("A", "RTA", issued=-10, token="opaqueA"), _state("B", "RTB", issued=-600, token="opaqueB")) + _seed(hermes_home, newer, older) + with auth_nous._nous_shared_store_lock(): + assert auth_nous._merge_shared_nous_oauth_state(dict(newer)) is False + assert auth_nous._read_shared_nous_state()["refresh_token"] == "RTB" + + +def test_a_profile_never_adopts_another_accounts_newer_shared_login(homes): + """Profiles are islands: account B's shared login is not account A's next refresh token.""" + hermes_home, _ = homes + _seed(hermes_home, _state("KA", "RTA", issued=-600, sub="uA"), _state("KB", "RTB", issued=-10, sub="uB")) + auth_mod.resolve_nous_access_token() + assert _profile_refresh_token(hermes_home) == "RTA" + assert auth_nous._read_shared_nous_state()["refresh_token"] == "RTB"