onboarding_run.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """``onboarding.run``: whether the desktop first-run onboarding opens on the next launch.
  2. The flag lives in the ROOT profile's ``config.yaml`` (``get_default_hermes_root()``), never in the
  3. launch profile's: a pooled backend launched under ``-p work`` must read and write the same file as
  4. one launched under ``default``, because onboarding always targets the default profile.
  5. Unset means "only on a machine that never used Hermes"; ``true`` re-runs it once; ``false`` means done.
  6. """
  7. from __future__ import annotations
  8. from pathlib import Path
  9. from hermes_constants import get_default_hermes_root
  10. def _root_config() -> Path:
  11. return get_default_hermes_root() / "config.yaml"
  12. def should_run(*, assume_fresh: bool = False) -> bool:
  13. """The root config's explicit ``onboarding.run`` (managed overlay included) when it is a bool,
  14. else whether this machine has never used Hermes (``install_state``; *assume_fresh* is the
  15. unpackaged desktop's ``--fresh-install`` switch)."""
  16. value = _explicit_run()
  17. if value is not None:
  18. return value
  19. from hermes_cli.install_state import probe
  20. return assume_fresh or probe().fresh
  21. def _explicit_run() -> bool | None:
  22. from hermes_cli.config_effective import load_user_config_effective
  23. # Not load_config(): it resolves the launch profile and fills the default None for an unset flag.
  24. section = load_user_config_effective(_root_config()).get("onboarding")
  25. value = section.get("run") if isinstance(section, dict) else None
  26. return value if isinstance(value, bool) else None
  27. def latch_first_install() -> bool:
  28. """The desktop backend's first boot on a fresh install records the decision: ``onboarding.run:
  29. true`` and the trial reserved. A launch that misses the run (a late answer, the app closed, the
  30. user chatting first) then stays due until a run ends. True when it wrote them."""
  31. from hermes_cli.install_state import probe, reserve_trial
  32. if _explicit_run() is not None or not probe().fresh:
  33. return False
  34. set_run(True) # first: a crash before the reservation still leaves a fresh probe for the claim
  35. reserve_trial()
  36. return True
  37. def set_run(value: bool, *, mark_profile_offered: bool = False) -> None:
  38. """Persist ``onboarding.run`` (and, with *mark_profile_offered*, ``onboarding.seen.profile_build_offered``)
  39. in the root ``config.yaml`` as one write, keeping every other key and comment. A failed write raises."""
  40. from agent.onboarding import PROFILE_BUILD_FLAG
  41. from hermes_cli import config as config_mod
  42. path = _root_config()
  43. with config_mod._CONFIG_LOCK:
  44. config = config_mod.read_user_config_raw(path)
  45. section = config_mod._ensure_dict(config, "onboarding")
  46. section["run"] = value
  47. if mark_profile_offered:
  48. config_mod._ensure_dict(section, "seen")[PROFILE_BUILD_FLAG] = True
  49. config_mod.atomic_config_write(path, config)
  50. from hermes_cli import onboarding_due
  51. onboarding_due.invalidate()