Skip to content

The agent that grows with you

fix(free-tier): grandfathering never overwrites a shared sign-in

alt-glitch5a67c860be83
diff --git a/hermes_cli/anon_auth.py b/hermes_cli/anon_auth.py
index d323140285b..f553383a563 100644
--- a/hermes_cli/anon_auth.py
+++ b/hermes_cli/anon_auth.py
@@ -33,6 +33,7 @@ import os
import time
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone, UTC
+from pathlib import Path
from typing import Any, Callable, Dict, Optional
from agent.retry_utils import parse_retry_after_seconds
@@ -980,26 +981,48 @@ def mark_guest_notice_shown() -> bool:
def set_guest_flag(flag: str) -> Optional[dict[str, Any]]:
"""Set *flag* on the guest's ``providers.nous`` state in whichever store holds it, and return the
updated state; None when there is no guest."""
- from hermes_cli.auth import (
- _auth_file_path, _load_auth_store, _provider_state_transaction, _same_path, _save_auth_store,
- _store_section)
+ from hermes_cli.auth import _provider_state_transaction
with _provider_state_transaction("nous") as (auth_store, state, source_path):
if not is_guest_state(state) or source_path is None:
return None
if state.get(flag):
return state
- state = dict(state)
- state[flag] = True
- if _same_path(source_path, _auth_file_path()):
- _store_section(auth_store, "providers")["nous"] = state
- _save_auth_store(auth_store)
- else:
- source_store = _load_auth_store(source_path)
- _store_section(source_store, "providers")["nous"] = state
- _save_auth_store(source_store, target_path=source_path)
+ state = {**state, flag: True}
+ _save_guest_state(auth_store, state, source_path)
return state
+def stamp_legacy_guest_claim() -> Optional[dict[str, Any]]:
+ """Grandfathering's write (``welcome_trial.grandfather_existing_guest``): stamp the welcome claim on
+ a guest minted before the key existed and publish it to the shared store, in one transaction under
+ the auth-then-shared locks. Nothing is written (None) unless, inside the locks, the guest still has
+ no key and the shared store is empty or holds that same guest: a sign-in another profile made is
+ never overwritten, whenever it landed."""
+ from hermes_cli.auth import _provider_state_transaction
+ from hermes_cli.auth_nous import _nous_shared_store_lock, _read_shared_nous_state, _write_shared_nous_state
+ with _provider_state_transaction("nous") as (auth_store, state, source_path), _nous_shared_store_lock():
+ shared = _read_shared_nous_state()
+ if (not is_guest_state(state) or source_path is None or WELCOME_CLAIM_FLAG in state
+ or (shared and _shared_identity_key(shared) != _shared_identity_key(state))):
+ return None
+ state = {**state, WELCOME_CLAIM_FLAG: True}
+ _save_guest_state(auth_store, state, source_path)
+ _write_shared_nous_state(state)
+ return state
+
+
+def _save_guest_state(auth_store: dict[str, Any], state: dict[str, Any], source_path: Path) -> None:
+ """Write the guest's ``providers.nous`` back to the store it was read from (the caller holds its lock)."""
+ from hermes_cli.auth import _auth_file_path, _load_auth_store, _same_path, _save_auth_store, _store_section
+ if _same_path(source_path, _auth_file_path()):
+ _store_section(auth_store, "providers")["nous"] = state
+ _save_auth_store(auth_store)
+ else:
+ source_store = _load_auth_store(source_path)
+ _store_section(source_store, "providers")["nous"] = state
+ _save_auth_store(source_store, target_path=source_path)
+
+
# --- ``hermes auth upgrade``: sign the guest into a real Nous account, keeping its connectors ---------
#
# Wire: the normal device-code flow, with a promotion intent registered on NAS BETWEEN the code
diff --git a/hermes_cli/welcome_trial.py b/hermes_cli/welcome_trial.py
index b3f7145a81d..0a52005bfff 100644
--- a/hermes_cli/welcome_trial.py
+++ b/hermes_cli/welcome_trial.py
@@ -143,11 +143,10 @@ def grandfather_existing_guest() -> bool:
homes = dict.fromkeys((get_hermes_home(), get_default_hermes_root()))
if not any(_chatted_on_welcome(home / "state.db") for home in homes):
return False
- from hermes_cli.auth_nous import _write_shared_nous_state
from hermes_cli.install_state import mark_machine_used
- if (stamped := anon_auth.set_guest_flag(anon_auth.WELCOME_CLAIM_FLAG)) is not None:
- _write_shared_nous_state(stamped)
+ if anon_auth.stamp_legacy_guest_claim() is None: # re-checked under the locks, against shared auth
+ return False
mark_machine_used()
logger.info("Nous free tier: existing free-model guest kept its trial")
return True
diff --git a/tests/hermes_cli/test_welcome_trial.py b/tests/hermes_cli/test_welcome_trial.py
index 52fc9a1fb88..02d23fcf4e8 100644
--- a/tests/hermes_cli/test_welcome_trial.py
+++ b/tests/hermes_cli/test_welcome_trial.py
@@ -111,6 +111,22 @@ def test_a_guest_that_chatted_on_the_free_model_keeps_it(portal, monkeypatch, mo
assert anon_auth.free_inference_active() is kept
+def test_grandfathering_never_overwrites_a_sign_in_from_another_profile(portal, monkeypatch):
+ from hermes_cli.auth_nous import _read_shared_nous_state, _write_shared_nous_state
+ from hermes_constants import get_hermes_home
+ from tests.hermes_cli.anon_portal import PORTAL
+
+ as_local_desktop_backend(monkeypatch)
+ _legacy_guest() # this profile still holds the old guest
+ _chat(get_hermes_home() / "state.db", anon_auth.GUEST_MODEL)
+ _write_shared_nous_state({"auth_method": "oauth_device_code", "portal_base_url": PORTAL,
+ "access_token": "at-signed-in", "refresh_token": "rt-signed-in"})
+
+ assert welcome_trial.grandfather_existing_guest() is False
+ assert _read_shared_nous_state()["refresh_token"] == "rt-signed-in"
+ assert anon_auth.WELCOME_CLAIM_FLAG not in anon_auth.current_nous_state()
+
+
def test_a_lost_claim_is_not_replaced(portal, monkeypatch):
claimed_trial(monkeypatch)
anon_auth.clear_dead_guest("anon_credential_dead")