source_build.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. """Source launch/update composition over the shared JavaScript builders."""
  2. import os
  3. from pathlib import Path
  4. import shutil
  5. import subprocess
  6. import sys
  7. def source_product_current(project_root: Path, product: str, out: Path) -> bool:
  8. """Read the compiler's receipt without acquiring tools or dependencies."""
  9. from pm import env_for, installed_package
  10. installed = installed_package("node")
  11. if installed is None or installed.binary is None:
  12. return False # Only PM's Node may run the receipt reader; never the user's PATH copy.
  13. node, env = str(installed.binary), env_for("node")
  14. try:
  15. result = subprocess.run(
  16. [node, str(project_root / "scripts/build/freshness.mjs"),
  17. "--source", str(project_root), "--product", product, "--out", str(out)],
  18. cwd=project_root, env=env, capture_output=True, text=True, encoding="utf-8", errors="replace", check=True,
  19. )
  20. return result.stdout.strip() == "true"
  21. except (OSError, subprocess.SubprocessError):
  22. return False
  23. def source_build_env(base_env: dict | None = None, *, explicit: bool = False, verify: bool = True) -> dict[str, str]:
  24. from pm import ensure
  25. from pm.environments import project_python, running_from_selected_environment
  26. from pm.paths import repo_root
  27. from hermes_constants import get_hermes_home
  28. # The historical update runs on store Python with the selected environment
  29. # activated in-process. Icon generation starts an isolated child, which needs
  30. # the selected venv executable rather than the store interpreter.
  31. root = repo_root()
  32. python = str(project_python(root)) if running_from_selected_environment(root) else sys.executable
  33. env = {**os.environ, **(base_env or {}), "CI": "1", "HERMES_PYTHON": python,
  34. "PYTHON": python}
  35. # Electron's installer unpacks its binary with a native module that imports
  36. # VCRUNTIME140.dll, which a fresh Windows lacks. Our Python ships one, and
  37. # scripts/build/electron-runtime.mjs copies it beside that module.
  38. vc_runtime = Path(sys.base_prefix) / "vcruntime140.dll"
  39. if vc_runtime.is_file():
  40. env["HERMES_VC_RUNTIME"] = str(vc_runtime)
  41. env.pop("ESBUILD_BINARY_PATH", None)
  42. npmrc = get_hermes_home() / "npmrc"
  43. if npmrc.is_file():
  44. env.setdefault("NPM_CONFIG_USERCONFIG", str(npmrc))
  45. return ensure("npm", base_env=env, explicit=explicit, verify=verify).env
  46. def run_in_custody(project_root: Path, command: list, label: str, **kwargs):
  47. """``pm.progress.run_contained`` for a build command that writes the checkout (node, npm):
  48. it and everything it starts stay in the update's custody (POSIX: the checkout lock fd, the
  49. caller's process group, and every descendant killed when it exits; Windows: the owner's
  50. kill-on-close job), so the checkout is never handed to a contender while one of them still
  51. writes. Outside an update it is ``run_contained`` as is."""
  52. from pm.progress import run_contained
  53. from hermes_cli.update_custody import contained_command
  54. with contained_command(command, root=project_root) as (argv, custody):
  55. return run_contained(argv, label, **kwargs, **custody)
  56. def run_source_script(project_root: Path, script: str, *args: str, env: dict, label: str) -> None:
  57. command = [shutil.which("node", path=env["PATH"]), str(project_root / script), *args]
  58. # npm's deprecation warnings are the loudest lines and never actionable
  59. # here; they still land in the failure tail.
  60. run_in_custody(project_root, command, label, hide=lambda line: line.lower().startswith("npm warn"),
  61. indent=" ", cwd=project_root, env=env)
  62. def _install_args(explicit: bool) -> tuple[str, ...]:
  63. from pm import lazy_installs_allowed
  64. return () if explicit or lazy_installs_allowed() else ("--no-install",)
  65. def prepare_source_dependencies(project_root: Path, workspaces: tuple[str, ...], *, env: dict,
  66. explicit: bool = False) -> None:
  67. run_source_script(
  68. project_root, "scripts/build/node-deps.mjs", "--source", str(project_root), "--reuse",
  69. *_install_args(explicit),
  70. *(arg for workspace in workspaces for arg in ("--workspace", workspace)), env=env,
  71. label="Preparing Node dependencies",
  72. )
  73. def prepare_launch_dependencies(project_root: Path, *, env: dict) -> None:
  74. """A launch rebuild must not prune another installed source frontend."""
  75. from hermes_cli.main_desktop import _desktop_dist_exists, _desktop_packaged_executable
  76. desktop_dir = project_root / "apps/desktop"
  77. desktop = _desktop_dist_exists(desktop_dir) or _desktop_packaged_executable(desktop_dir) is not None
  78. workspaces = ("ui-tui", "web") + (("apps/desktop",) if desktop else ())
  79. prepare_source_dependencies(project_root, workspaces, env=env)
  80. def build_source_tui(project_root: Path, *, env: dict) -> None:
  81. run_source_script(project_root, "scripts/build/tui.mjs", env=env, label="Building the TUI")
  82. def build_source_web(project_root: Path, *, env: dict, icons: Path | None = None) -> None:
  83. # Bounded build (#63338): the Vite/Rolldown dashboard build saturates small
  84. # hosts; cap the V8 heap and the native bundler's rayon thread pool.
  85. from hermes_cli.web_build_limits import apply_web_build_limits
  86. apply_web_build_limits(env)
  87. # Default-brand icons are committed; installs never render them.
  88. icons = icons or project_root
  89. run_source_script(project_root, "scripts/build/web.mjs", "--source", str(project_root),
  90. "--icons", str(icons), "--out", str(project_root / "hermes_cli/web_dist"), env=env,
  91. label="Building the web UI")
  92. def build_source_webapp(project_root: Path, *, env: dict, explicit: bool = False) -> None:
  93. """Browser renderer; its dependencies live in a private workspace, never the checkout's."""
  94. run_source_script(project_root, "apps/desktop/scripts/build-webapp.mjs", *_install_args(explicit),
  95. env=env, label="Building the Webapp renderer")
  96. def source_frontends(project_root: Path) -> tuple[str, ...]:
  97. """The frontend workspaces this checkout carries. A source slice without them
  98. (python-only installs, the installer's acceptance fixture) has no products
  99. to build; it still publishes commands and runs the maintenance tail."""
  100. return tuple(name for name in ("ui-tui", "web") if (project_root / name / "package.json").is_file())
  101. class ProductBuildError(RuntimeError):
  102. """One or more products failed; every independent product was still attempted."""
  103. def __init__(self, failures: list[tuple[str, BaseException]]):
  104. self.failures = failures
  105. super().__init__("; ".join(f"{name}: {_failure_text(exc)}" for name, exc in failures))
  106. def _failure_text(exc: BaseException) -> str:
  107. if isinstance(exc, subprocess.CalledProcessError):
  108. return f"{' '.join(map(str, exc.cmd)) if isinstance(exc.cmd, (list, tuple)) else exc.cmd} exited {exc.returncode}"
  109. return f"{type(exc).__name__}: {exc}" if str(exc) else type(exc).__name__
  110. def _headless_linux() -> bool:
  111. return sys.platform.startswith("linux") and not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"))
  112. def build_update_products(project_root: Path, *, desktop: bool) -> None:
  113. """Prepare the selected union once, attempting every independent product.
  114. A failed product never skips the next one (a broken web build must not leave the TUI or the
  115. desktop app stale); the failures are raised together at the end as ``ProductBuildError``.
  116. """
  117. # Both current updates and historical takeover reach this in a fresh target
  118. # interpreter, never in the updater's pre-sync import graph.
  119. from hermes_cli.main_install_repair import _install_configured_features_missing_deps
  120. from hermes_cli.update_stage import publish_stage
  121. failures: list[tuple[str, BaseException]] = []
  122. publish_stage("Building the apps")
  123. def attempt(name: str, step) -> bool:
  124. try:
  125. step()
  126. return True
  127. except Exception as exc: # health: allow BLE001 -- collected and raised after the independent steps
  128. print(f" ⚠ {name} failed: {_failure_text(exc)}")
  129. failures.append((name, exc))
  130. return False
  131. attempt("feature dependencies", lambda: _install_configured_features_missing_deps(project_root))
  132. frontends = source_frontends(project_root)
  133. if frontends:
  134. desktop_built = False
  135. env: dict = {}
  136. workspaces = frontends + (("apps/desktop",) if desktop else ())
  137. publish_stage("Updating Node dependencies")
  138. def node_dependencies(selected: tuple[str, ...]) -> None:
  139. # Acquiring npm is part of this step: its failure must be reported like the install's.
  140. # A recorded install is trusted as at startup (re-hashing node+npm costs ~2 s per
  141. # tail); a missing one is still installed explicitly.
  142. env.update(source_build_env(explicit=True, verify=False))
  143. prepare_source_dependencies(project_root, selected, env=env, explicit=True)
  144. # Every product compiles from these node_modules: without them there is nothing to build.
  145. prepared = attempt("Node dependencies", lambda: node_dependencies(workspaces))
  146. desktop_skipped = False
  147. if not prepared and desktop and env:
  148. # Only apps/desktop brings native addons (node-pty, electron) into the union: a host
  149. # whose compiler cannot build them must still get the TUI and web UI it runs.
  150. print(" → Retrying Node dependencies without the desktop app")
  151. failed = failures.pop()
  152. prepared = desktop_skipped = attempt("Node dependencies", lambda: node_dependencies(frontends))
  153. if desktop_skipped:
  154. failures.append(("desktop dependencies", failed[1]))
  155. desktop_reason = failures[-1][0] if failures else ""
  156. if desktop and (desktop_skipped or _headless_linux()):
  157. print(" ⓘ If this host never runs the desktop app, `hermes uninstall --gui` removes it "
  158. "and future updates stop building it.")
  159. if prepared:
  160. # An update that changed no TUI/web input reuses the receipted output, as the
  161. # launch path already does; recompiling it produces the same bytes. Desktop
  162. # additionally needs the packaged app to name HEAD (its baked stamp carries the
  163. # commit), so it is reused only when HEAD did not move: "Already up to date",
  164. # a retried tail, a takeover re-entry.
  165. if "ui-tui" in frontends:
  166. if source_product_current(project_root, "tui", project_root / "ui-tui/dist"):
  167. print(" ✓ TUI is up to date")
  168. else:
  169. publish_stage("Building the TUI")
  170. attempt("TUI build", lambda: build_source_tui(project_root, env=env))
  171. if "web" in frontends:
  172. if source_product_current(project_root, "web", project_root / "hermes_cli/web_dist"):
  173. print(" ✓ Web UI is up to date")
  174. else:
  175. publish_stage("Building the web UI")
  176. attempt("web UI build", lambda: build_source_web(project_root, env=env))
  177. if desktop and not desktop_skipped:
  178. desktop_built = attempt("desktop app build", lambda: _build_desktop_product(project_root, env, publish_stage))
  179. desktop_reason = failures[-1][0] if not desktop_built else ""
  180. if desktop and not desktop_built:
  181. # One whole line the Desktop hand-off scripts match: the follow-up text is truncated
  182. # and names whichever products failed first, so it cannot say if THIS app was rebuilt.
  183. print(f" Desktop app build owed: {desktop_reason} failed")
  184. if (project_root / "apps/desktop/scripts/build-webapp.mjs").is_file():
  185. # Its dependencies live in a private workspace, so it never waits on the union above.
  186. attempt("Webapp build", lambda: _rebuild_served_webapp(project_root, publish_stage))
  187. # A configured memory provider that no longer ships in core is installed from the
  188. # catalog for every profile home sharing this venv (config, data and tool names
  189. # unchanged). The update must finish even if the migration blows up.
  190. try:
  191. from hermes_cli.memory_provider_migration import migrate_all_homes
  192. migrate_all_homes()
  193. except Exception as exc: # health: allow BLE001 -- post-commit boundary: printed as ⚠, never fails the committed update; every later update/tail re-runs it
  194. print(f" ⚠ Memory provider migration skipped: {exc}")
  195. # Same for a gateway platform / toolset that left core (Home Assistant): every home that used
  196. # it gets its catalog plugin (hermes_cli/left_core_migration.py).
  197. try:
  198. from hermes_cli.left_core_migration import migrate_all_homes as migrate_left_core
  199. migrate_left_core()
  200. except Exception as exc: # health: allow BLE001 -- post-commit boundary: printed as ⚠, never fails the committed update; every later update/tail re-runs it
  201. print(f" ⚠ Plugin migration skipped: {exc}")
  202. if failures:
  203. raise ProductBuildError(failures)
  204. def _rebuild_served_webapp(project_root: Path, publish_stage) -> None:
  205. """The Webapp bundle (``apps/desktop/dist-webapp``), when a home sharing this install serves it
  206. (``webapp.enabled``): its gateway serves the bundle as built, so one older than the updated
  207. backend would talk to it with an outdated contract. Built with the function ``hermes webapp``
  208. uses, which reuses a current bundle. A failed build leaves the previous bundle on disk, marked
  209. out of date: its gateways report the Webapp unavailable until ``hermes webapp`` rebuilds it."""
  210. from gateway.webapp_listener import webapp_settings
  211. from hermes_cli.webapp import mark_webapp_stale, prepare_webapp_renderer
  212. from pm.plugins_state import dependency_homes
  213. if not any(webapp_settings(home).enabled for home in dependency_homes()):
  214. return
  215. publish_stage("Building the Webapp")
  216. try:
  217. prepare_webapp_renderer(project_root, explicit=True)
  218. except BaseException:
  219. try:
  220. if mark_webapp_stale(project_root):
  221. print(" ⓘ The Webapp stays unavailable until `hermes webapp` rebuilds it.")
  222. except OSError as exc:
  223. print(f" ⚠ Could not mark the Webapp out of date: {exc}")
  224. raise
  225. def _build_desktop_product(project_root: Path, env: dict, publish_stage) -> None:
  226. from hermes_cli.desktop_build_reuse import packaged_desktop_reusable
  227. from hermes_cli.main_desktop import (
  228. _packaged_desktop_current_for_head, _refresh_installed_desktop_apps, build_prepared_desktop)
  229. desktop_dir = project_root / "apps/desktop"
  230. if _packaged_desktop_current_for_head(desktop_dir, project_root):
  231. print(" ✓ Desktop app is up to date")
  232. elif built_from := packaged_desktop_reusable(desktop_dir, project_root, env):
  233. print(f" ✓ Desktop app is up to date: no desktop build input changed since {built_from[:12]}")
  234. else:
  235. publish_stage("Building the desktop app")
  236. # The desktop build mutates checkout-scoped node_modules and
  237. # apps/desktop/release; serialize it against a concurrent manual
  238. # `hermes desktop` (#93940). The update path waits rather than exits:
  239. # the in-flight build it queues behind produces the same fresh tree
  240. # this update needs.
  241. from hermes_cli.desktop_build_lock import DesktopBuildLock
  242. build_lock = DesktopBuildLock(project_root)
  243. build_lock.acquire(wait=True)
  244. try:
  245. build_prepared_desktop(
  246. desktop_dir, source_mode=False,
  247. npm=shutil.which("npm", path=env["PATH"]), env=env, icons=project_root,
  248. )
  249. finally:
  250. build_lock.release()
  251. # A current release/ can still sit beside a stale installed copy (an earlier
  252. # update rebuilt but never installed); healing must not wait for the next build.
  253. _refresh_installed_desktop_apps(desktop_dir)
  254. if __name__ == "__main__":
  255. import argparse
  256. parser = argparse.ArgumentParser(description="Build source-install frontends")
  257. parser.add_argument("--source", type=Path, required=True)
  258. parser.add_argument("--desktop", action="store_true")
  259. args = parser.parse_args()
  260. build_update_products(args.source.resolve(), desktop=args.desktop)