Skip to content

The agent that grows with you

fix(free-tier): a claim cut off before its marker write is settled

alt-glitchf17a950b695d
diff --git a/hermes_cli/free_tier_bootstrap.py b/hermes_cli/free_tier_bootstrap.py
index 54c280c277a..8da859c8e86 100644
--- a/hermes_cli/free_tier_bootstrap.py
+++ b/hermes_cli/free_tier_bootstrap.py
@@ -3,7 +3,8 @@
``hermes serve`` runs this once at boot on a daemon thread. It never creates an identity: the
desktop's welcome-trial claim (``hermes_cli.welcome_trial``) and the first hosted connector action
are the only creators. On the desktop's local backend it first records a fresh install's onboarding
-decision (``onboarding_run.latch_first_install``), then keeps the trial for a guest that already ran
+decision (``onboarding_run.latch_first_install``), settles a claim cut off before its marker write
+(``welcome_trial.settle_interrupted_claim``), then keeps the trial for a guest that already ran
on the free model (``welcome_trial.grandfather_existing_guest``). Then it inventories
credentials cheap-first, resolves which provider carries inference, records the answer in process
memory, and tells every connected client with one ``setup.ready`` event. ``free_tier.status`` and
@@ -206,7 +207,8 @@ def run_bootstrap(*, announce: bool = True) -> SetupRecord:
except Exception: # the next boot tries again; onboarding.state still answers live
logger.warning("onboarding: could not record the first-install decision", exc_info=True)
try:
- from hermes_cli.welcome_trial import grandfather_existing_guest
+ from hermes_cli.welcome_trial import grandfather_existing_guest, settle_interrupted_claim
+ settle_interrupted_claim()
grandfather_existing_guest()
except Exception: # the record still says who carries inference without it
logger.warning("free tier: could not check for an existing free-model guest", exc_info=True)
diff --git a/hermes_cli/welcome_trial.py b/hermes_cli/welcome_trial.py
index d36ea9fb6d1..6cf1a77005b 100644
--- a/hermes_cli/welcome_trial.py
+++ b/hermes_cli/welcome_trial.py
@@ -91,6 +91,7 @@ def claim_desktop_free_tier(*, assume_fresh: bool = False, force: bool = False)
Returns the claimed state, or None when the trial is not available here. A failed mint raises
the coded ``AuthError`` from :func:`anon_auth.ensure_portal_identity` (memoised with its cooldown;
*force* is the user's own retry)."""
+ settle_interrupted_claim()
if reason := claim_blocker(assume_fresh=assume_fresh):
logger.debug("welcome trial not claimed: %s", reason)
return None
@@ -107,6 +108,23 @@ def claim_desktop_free_tier(*, assume_fresh: bool = False, force: bool = False)
return state
+def settle_interrupted_claim() -> bool:
+ """A claim cut off between its guest write and its marker write leaves the reservation behind.
+ The stamped guest is the claim, so the reservation goes: kept, it would let a second trial through
+ once that guest is reaped or deleted. Runs before every claim and at serve boot; True when it did."""
+ from hermes_cli.auth_nous import _read_shared_nous_state
+ from hermes_cli.install_state import mark_machine_used, trial_reserved
+
+ if not trial_reserved():
+ return False
+ stores = (anon_auth.current_nous_state(), _read_shared_nous_state())
+ if not any(anon_auth.is_guest_state(s) and s.get(anon_auth.WELCOME_CLAIM_FLAG) is True for s in stores):
+ return False
+ mark_machine_used()
+ logger.info("Nous free tier: an interrupted welcome-trial claim was settled")
+ return True
+
+
def grandfather_existing_guest() -> bool:
"""A guest from before the claim existed keeps the free model when this home already chatted on
it. Only a guest with no ``welcome_claim`` key qualifies: every guest minted since carries the
diff --git a/tests/hermes_cli/test_welcome_trial.py b/tests/hermes_cli/test_welcome_trial.py
index 39968efa8d6..6551f618ba7 100644
--- a/tests/hermes_cli/test_welcome_trial.py
+++ b/tests/hermes_cli/test_welcome_trial.py
@@ -274,6 +274,24 @@ def test_a_missed_first_launch_stays_due_and_keeps_the_trial(portal, monkeypatch
assert welcome_trial.claim_blocker() == "a Nous identity exists"
+def test_a_claim_cut_off_before_its_marker_write_never_claims_twice(portal, monkeypatch):
+ as_local_desktop_backend(monkeypatch)
+ _relaunch() # first boot: due, the trial reserved
+ mark = install_state.mark_machine_used
+
+ def die():
+ raise RuntimeError("the process died here")
+ monkeypatch.setattr(install_state, "mark_machine_used", die) # the stamped guest is saved, then the crash
+ with pytest.raises(RuntimeError):
+ welcome_trial.claim_desktop_free_tier()
+ monkeypatch.setattr(install_state, "mark_machine_used", mark)
+ _relaunch()
+ anon_auth.clear_dead_guest("anon_credential_dead") # the claimed guest is reaped later
+
+ assert welcome_trial.claim_desktop_free_tier() is None
+ assert portal.creates() == 1
+
+
def test_one_due_answer_walks_the_provider_ladder_once(portal, monkeypatch):
from hermes_cli import auth