Skip to content

The agent that grows with you

fix(free-tier): one welcome-trial claim at a time per OS user

alt-glitch207b61fe9681
diff --git a/hermes_cli/install_state.py b/hermes_cli/install_state.py
index db470bd4485..0116f507527 100644
--- a/hermes_cli/install_state.py
+++ b/hermes_cli/install_state.py
@@ -17,6 +17,7 @@ import json
import os
import sqlite3
import sys
+import threading
from collections.abc import Callable, Iterable, Iterator
from dataclasses import dataclass
from functools import cache, cached_property
@@ -111,6 +112,26 @@ def mark_machine_used() -> None:
_TRIAL_RESERVED = {"trial": "reserved"}
+# Covers a claim's checks, its mint (bounded by its own auth lock and HTTP timeouts) and the marker write.
+CLAIM_LOCK_TIMEOUT_SECONDS = 30.0
+_claim_lock_holder = threading.local()
+
+
+def claim_lock() -> Iterator[None]:
+ """One welcome-trial claim at a time for this OS user, whichever Hermes data root it runs from
+ (stable and canary started together hold different auth locks). Beside the marker it guards, and
+ taken before any auth lock."""
+ places = machine_places()
+ if not places.markers:
+ yield
+ return
+ from hermes_cli.auth import _file_lock
+
+ lock_path = places.markers[0].with_name(places.markers[0].name + ".lock")
+ with _file_lock(lock_path, _claim_lock_holder, CLAIM_LOCK_TIMEOUT_SECONDS,
+ f"Timed out waiting for the welcome-trial claim lock ({lock_path})"):
+ yield
def reserve_trial() -> None:
diff --git a/hermes_cli/welcome_trial.py b/hermes_cli/welcome_trial.py
index 6cf1a77005b..b3f7145a81d 100644
--- a/hermes_cli/welcome_trial.py
+++ b/hermes_cli/welcome_trial.py
@@ -90,20 +90,24 @@ def claim_desktop_free_tier(*, assume_fresh: bool = False, force: bool = False)
"""Claim the trial when nothing blocks it: mint the guest, stamp the claim, mark the machine.
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
- state = anon_auth.ensure_portal_identity(explicit=True, force=force, welcome_claim=True)
- if state is None: # still inside the cooldown of the last failure
- failure = anon_auth.last_mint_failure() or {}
- raise anon_auth._anon_err(str(failure.get("error") or anon_auth.ANON_FAILURE_COPY[anon_auth.ANON_SERVER_ERROR]),
- str(failure.get("error_code") or anon_auth.ANON_SERVER_ERROR),
- retry_after=failure.get("retry_after"))
- from hermes_cli.install_state import mark_machine_used
-
- mark_machine_used()
+ *force* is the user's own retry). Held under the OS user's claim lock from the checks to the
+ marker write, so two installs starting together claim once."""
+ from hermes_cli import install_state
+
+ with install_state.claim_lock():
+ install_state.probe.cache_clear() # another install may have claimed since this process looked
+ settle_interrupted_claim()
+ if reason := claim_blocker(assume_fresh=assume_fresh):
+ logger.debug("welcome trial not claimed: %s", reason)
+ return None
+ state = anon_auth.ensure_portal_identity(explicit=True, force=force, welcome_claim=True)
+ if state is None: # still inside the cooldown of the last failure
+ failure = anon_auth.last_mint_failure() or {}
+ raise anon_auth._anon_err(
+ str(failure.get("error") or anon_auth.ANON_FAILURE_COPY[anon_auth.ANON_SERVER_ERROR]),
+ str(failure.get("error_code") or anon_auth.ANON_SERVER_ERROR),
+ retry_after=failure.get("retry_after"))
+ install_state.mark_machine_used()
logger.info("Nous free tier: welcome trial claimed on this machine")
return state
diff --git a/tests/hermes_cli/test_welcome_trial.py b/tests/hermes_cli/test_welcome_trial.py
index 6551f618ba7..52fc9a1fb88 100644
--- a/tests/hermes_cli/test_welcome_trial.py
+++ b/tests/hermes_cli/test_welcome_trial.py
@@ -292,6 +292,46 @@ def test_a_claim_cut_off_before_its_marker_write_never_claims_twice(portal, monk
assert portal.creates() == 1
+def test_two_installs_claiming_at_once_mint_one_trial(portal, monkeypatch, tmp_path):
+ """Stable and canary started together: two data roots (each its own auth and shared store, so
+ their auth locks never meet), one OS user, one marker. The canary claims while stable mints."""
+ import threading
+
+ from hermes_cli import auth_nous
+ from hermes_constants import get_hermes_home, reset_hermes_home_override, set_hermes_home_override
+
+ as_local_desktop_backend(monkeypatch)
+ _relaunch() # the trial is reserved for this OS user
+ monkeypatch.setattr(auth_nous, "_nous_shared_auth_dir", lambda: get_hermes_home() / "shared-nous")
+ canary_root = tmp_path / "hermes-canary"
+ canary_root.mkdir()
+ canary: dict = {}
+ canary_done = threading.Event()
+
+ def canary_claims():
+ token = set_hermes_home_override(canary_root)
+ try:
+ canary["state"] = welcome_trial.claim_desktop_free_tier()
+ finally:
+ reset_hermes_home_override(token)
+ canary_done.set()
+
+ stable, canary_thread = threading.current_thread(), threading.Thread(target=canary_claims)
+ real_mint = anon_auth.ensure_portal_identity
+
+ def stable_mints(**kwargs):
+ if threading.current_thread() is stable and not canary_thread.is_alive() and not canary_done.is_set():
+ canary_thread.start()
+ canary_done.wait(timeout=2.0) # unguarded, the canary claims in this gap
+ return real_mint(**kwargs)
+ monkeypatch.setattr(anon_auth, "ensure_portal_identity", stable_mints)
+
+ assert welcome_trial.claim_desktop_free_tier() is not None
+ canary_thread.join(timeout=30.0)
+ assert canary == {"state": None}
+ assert portal.creates() == 1
+
+
def test_one_due_answer_walks_the_provider_ladder_once(portal, monkeypatch):
from hermes_cli import auth