free_tier_bootstrap.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. """Record of who carries inference, for ``setup.status`` and ``setup.ready``.
  2. It runs once per process on a daemon thread: at ``hermes serve`` boot, and on the gateway when the
  3. local Desktop first connects (``tui_gateway.methods_onboarding.begin_local_desktop_boot``). It never
  4. creates an identity: the desktop's welcome-trial claim (``hermes_cli.welcome_trial``) and the first
  5. hosted connector action are the only creators. For the desktop's local backend it first records a
  6. fresh install's onboarding decision (``onboarding_run.latch_first_install``) and settles a claim cut
  7. off before its marker write (``welcome_trial.settle_interrupted_claim``). Then it inventories
  8. credentials cheap-first, resolves which provider carries inference, records the answer in process
  9. memory, and tells every connected client with one ``setup.ready`` event. ``free_tier.status`` and
  10. ``setup.status`` read the record; provider resolution never reaches the portal.
  11. """
  12. from __future__ import annotations
  13. import logging
  14. import threading
  15. import time
  16. from dataclasses import asdict, dataclass, field, replace
  17. from typing import Any, Dict, Optional
  18. logger = logging.getLogger("hermes_cli.auth")
  19. # The desktop's first ``setup.status`` waits this long for the record before falling back to a live probe.
  20. SETUP_READY_WAIT_SECONDS = 8.0
  21. SETUP_READY_EVENT = "setup.ready"
  22. @dataclass(frozen=True)
  23. class SetupRecord:
  24. """What the bootstrap found. One shape for every reader; no version field (renderer and backend
  25. ship together)."""
  26. provider_configured: bool # some provider can carry inference (free tier included)
  27. inference_provider: str # ``resolve_provider("auto")``'s answer, "" when nothing resolves
  28. free_tier_account: bool # the identity that exists is a guest that carries inference here
  29. has_identity: bool # a Nous identity (free tier or account) is on disk
  30. other_providers: bool # the inventory found something usable BESIDES the free tier
  31. error: str = "" # why the last trial claim failed, when it did; "" otherwise
  32. # The mint memo's verdict, verbatim (``anon_auth.MintFailure.as_payload``):
  33. # ``{error, error_code, retryable, retry_after}`` after a failed claim, else ``{}``.
  34. # One wire shape: every status RPC spreads it as is.
  35. failure: dict[str, Any] = field(default_factory=dict)
  36. finished_at: float = field(default_factory=time.time)
  37. @property
  38. def free_tier_route(self) -> bool:
  39. return self.free_tier_account and self.inference_provider == "nous"
  40. def as_payload(self) -> dict[str, Any]:
  41. # The broadcast carries the failure block flat, the same shape ``setup.status`` spreads,
  42. # so a client keys on ``error_code`` identically whichever surface it read.
  43. payload = asdict(self)
  44. payload.update(payload.pop("failure"))
  45. payload["free_tier_route"] = self.free_tier_route
  46. return payload
  47. def failure_fields(self) -> dict[str, Any]:
  48. return dict(self.failure)
  49. _lock = threading.Lock()
  50. _record: Optional[SetupRecord] = None
  51. _done = threading.Event()
  52. _started = False
  53. _settled = False # the bootstrap recorded the install and settled the claim (``_settle_install``)
  54. _resettle_lock = threading.Lock()
  55. # ``(mtime_ns, size)`` of the files the inventory reads, taken by the inventory that built the
  56. # current record; ``reconcile_record`` re-inventories only when they moved.
  57. _inventory_stamp: Optional[tuple] = None
  58. _INVENTORY_FILES = ("config.yaml", ".env", "auth.json")
  59. def current_record() -> Optional[SetupRecord]:
  60. """The record, or None until the first bootstrap finishes."""
  61. return _record
  62. def wait_for_record(timeout: float = SETUP_READY_WAIT_SECONDS) -> Optional[SetupRecord]:
  63. """Block up to ``timeout`` seconds for a bootstrap that is IN FLIGHT, then return whatever it
  64. produced, reconciled with any provider configured since (:func:`reconcile_record`). Returns
  65. None at once when no bootstrap ever started in this process (a bare ``tui_gateway`` under
  66. test, an old serve without the boot hook): the caller falls back to its live probe instead of
  67. paying the wait for nothing."""
  68. if not _started:
  69. return None
  70. _done.wait(timeout)
  71. return reconcile_record()
  72. def reconcile_record() -> Optional[SetupRecord]:
  73. """Let a provider configured AFTER boot count: a record that says ``provider_configured:
  74. false`` is re-inventoried once ``config.yaml`` / ``.env`` / ``auth.json`` moved since the
  75. inventory that built it, and replaced (+ ``setup.ready``) when something now carries
  76. inference. The identity and failure block are kept as is. A record that already says ``True`` is never re-probed, so the answer
  77. only moves false -> true here. Every write path that assigns the main model (the Models page,
  78. a picker key save) calls this for the immediate broadcast; ``setup.status`` calls it for
  79. writes this process never saw (``hermes setup`` / ``hermes model`` from a shell, a hand edit).
  80. The record is the LAUNCH profile's: a call scoped to another profile's home (a dashboard
  81. write with ``?profile=B``) leaves it alone, or B's providers would open the launch gate."""
  82. global _record
  83. record = _record
  84. if record is None or record.provider_configured:
  85. return record
  86. from hermes_constants import get_process_hermes_home, hermes_home_key
  87. if hermes_home_key() != hermes_home_key(get_process_hermes_home()) or _inventory_stamp == _config_stamp():
  88. return record
  89. if not _inventory_other_providers():
  90. return _record
  91. refreshed = replace(record, provider_configured=True, other_providers=True,
  92. inference_provider=_resolve_inference(), finished_at=time.time())
  93. with _lock:
  94. if _record is not record: # a retry replaced it meanwhile; its inventory is newer
  95. return _record
  96. _record = refreshed
  97. _due_changed()
  98. _broadcast(refreshed)
  99. return refreshed
  100. def reset_for_tests() -> None:
  101. global _record, _started, _settled, _inventory_stamp
  102. with _lock:
  103. _record = None
  104. _started = False
  105. _settled = False
  106. _inventory_stamp = None
  107. _done.clear()
  108. def _config_stamp() -> tuple:
  109. from hermes_cli.config import get_hermes_home
  110. from utils import file_cache_version
  111. home = get_hermes_home()
  112. stamp = []
  113. for name in _INVENTORY_FILES:
  114. try:
  115. stamp.append(file_cache_version(home / name, always_digest=True))
  116. except OSError:
  117. stamp.append(None)
  118. return tuple(stamp)
  119. def _inventory_other_providers() -> bool:
  120. """Is anything usable configured BESIDES the free tier? Asks the resolver ladder itself (the
  121. thing that picks the provider for a turn) with the free-tier rung hidden: an explicit key, a
  122. config pin, a sign-in or a host credential answers; nothing else falls through to
  123. ``no_provider_configured``. Not ``_has_any_provider_configured``: that first-run guard also
  124. counts host credentials (gh auth, Claude Code) and a config pin, and it does not hide the
  125. free-tier rung.
  126. Stamps the config files BEFORE reading them, so a write that lands during the inventory is
  127. seen by the next :func:`reconcile_record`.
  128. """
  129. global _inventory_stamp
  130. from hermes_cli.auth import resolve_provider
  131. _inventory_stamp = _config_stamp()
  132. try:
  133. return resolve_provider("auto", skip_free_tier=True) != "nous"
  134. except Exception as exc:
  135. logger.debug("free tier bootstrap: nothing else carries inference (%s)", exc)
  136. return False
  137. def _resolve_inference() -> str:
  138. from hermes_cli.auth import resolve_provider
  139. try:
  140. return str(resolve_provider("auto") or "")
  141. except Exception:
  142. return ""
  143. def _build_record(*, other: bool) -> SetupRecord:
  144. """One inventory pass into a record. Reads only: no identity is created here."""
  145. from hermes_cli import anon_auth
  146. state: Optional[dict[str, Any]] = anon_auth.current_nous_state()
  147. failure: dict[str, Any] = {} if state else (anon_auth.last_mint_failure() or {})
  148. free_tier_account = anon_auth.free_inference_active(state) if state else False
  149. return SetupRecord(
  150. provider_configured=other or free_tier_account or (bool(state) and not anon_auth.is_guest_state(state)),
  151. inference_provider=_resolve_inference(),
  152. free_tier_account=free_tier_account,
  153. has_identity=bool(state),
  154. other_providers=other,
  155. error=str(failure.get("error") or ""),
  156. failure=failure,
  157. )
  158. def run_bootstrap(*, announce: bool = True) -> SetupRecord:
  159. """Settle an interrupted claim -> inventory -> resolve inference -> record ->
  160. broadcast. Idempotent per process: a second call returns the existing record. Never raises.
  161. ``announce=False`` skips the ``setup.ready`` event."""
  162. global _record, _started, _settled
  163. with _lock:
  164. if _record is not None:
  165. return _record
  166. if _started:
  167. _done.wait(SETUP_READY_WAIT_SECONDS)
  168. if _record is not None:
  169. return _record
  170. _started = True
  171. settled = _settle_install()
  172. record = _build_record(other=_inventory_other_providers())
  173. with _lock:
  174. _record, _settled = record, settled
  175. _done.set()
  176. if announce:
  177. _broadcast(record)
  178. return record
  179. def _settle_install() -> bool:
  180. """Record a fresh install and settle the guest's claim; True when neither step failed. A failure is
  181. logged: onboarding.state still answers live, and the record still says who carries inference."""
  182. settled = True
  183. try:
  184. from hermes_cli import anon_auth
  185. from hermes_cli.onboarding_run import latch_first_install
  186. if anon_auth.on_local_desktop_backend():
  187. latch_first_install()
  188. except Exception:
  189. settled = False
  190. logger.warning("onboarding: could not record the first-install decision", exc_info=True)
  191. try:
  192. from hermes_cli.welcome_trial import settle_interrupted_claim
  193. settle_interrupted_claim()
  194. except Exception:
  195. settled = False
  196. logger.warning("free tier: could not settle an interrupted claim", exc_info=True)
  197. return settled
  198. def resettle_after_failed_boot() -> None:
  199. """The local Desktop connected again to a process whose bootstrap could not settle the install:
  200. try those steps again and rebuild the record when they pass. Nothing before the bootstrap
  201. finished or once it settled."""
  202. global _settled
  203. with _lock:
  204. unsettled = _record is not None and not _settled
  205. if not unsettled or not _resettle_lock.acquire(blocking=False): # one retry at a time
  206. return
  207. try:
  208. if _settle_install():
  209. with _lock:
  210. _settled = True
  211. refresh_record()
  212. finally:
  213. _resettle_lock.release()
  214. def refresh_record(*, announce: bool = True) -> Optional[SetupRecord]:
  215. """Rebuild the launch profile's record after the identity changed (a trial claim), so
  216. ``setup.status`` and the ``setup.ready`` listeners move with it. None before the first bootstrap."""
  217. global _record
  218. if _record is None:
  219. return None
  220. record = _build_record(other=_inventory_other_providers())
  221. with _lock:
  222. _record = record
  223. _due_changed()
  224. if announce:
  225. _broadcast(record)
  226. return record
  227. def _due_changed() -> None:
  228. from hermes_cli import onboarding_due
  229. onboarding_due.invalidate()
  230. def _broadcast(record: SetupRecord) -> None:
  231. try:
  232. from tui_gateway.server import _broadcast_global_event
  233. _broadcast_global_event(SETUP_READY_EVENT, record.as_payload())
  234. except Exception as exc: # no serve process (plain CLI): nobody to tell
  235. logger.debug("setup.ready not broadcast: %s", exc)
  236. def start_background_bootstrap() -> threading.Thread:
  237. """Run on a daemon thread so the inventory never delays the socket."""
  238. from agent.memory_provider import spawn_context_thread
  239. thread = spawn_context_thread(run_bootstrap, name="free-tier-bootstrap")
  240. thread.start()
  241. return thread