main_web_build.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """Dashboard build freshness/serialization and checkout bytecode sweep.
  2. Split out of ``hermes_cli/main.py``. Names that still live in main (``PROJECT_ROOT``, ...)
  3. are imported lazily inside the functions that use them (avoids an import cycle).
  4. """
  5. import logging
  6. import subprocess
  7. import sys
  8. from pathlib import Path
  9. # Log-record parity with the origin module.
  10. logger = logging.getLogger("hermes_cli.main")
  11. # Checkout fingerprint the bytecode cache was last validated against. Lives next
  12. # to the checkout (NOT in HERMES_HOME): __pycache__ is per-checkout state shared
  13. # by every profile.
  14. _BYTECODE_FINGERPRINT_FILE = ".bytecode-fingerprint"
  15. def _record_bytecode_fingerprint() -> None:
  16. """Persist the current checkout fingerprint after a bytecode sweep. Never raises."""
  17. from hermes_cli.main import PROJECT_ROOT, _read_git_revision_fingerprint
  18. try:
  19. fingerprint = _read_git_revision_fingerprint(PROJECT_ROOT)
  20. if not fingerprint:
  21. return
  22. stamp_path = PROJECT_ROOT / _BYTECODE_FINGERPRINT_FILE
  23. tmp_path = stamp_path.with_name(stamp_path.name + ".tmp")
  24. tmp_path.write_text(fingerprint, encoding="utf-8")
  25. tmp_path.replace(stamp_path)
  26. except OSError as exc:
  27. logger.debug("Could not record bytecode fingerprint: %s", exc)
  28. def _sweep_stale_bytecode_if_checkout_changed() -> None:
  29. """Clear ``__pycache__`` at launch when the checkout fingerprint changed since the last sweep.
  30. Update-time clears can't close the stale-bytecode class: ``hermes update`` runs
  31. the PRE-pull updater code and manual pulls never run it. Cheap file reads, no
  32. git subprocess. Never raises.
  33. The stale-bytecode bug class (issues #6207, #60242; Dhruv's WhatsApp ``cannot import name
  34. 'parse_model_flags_detailed'`` report) has one shared shape: the checkout's ``.py`` files change (git
  35. pull inside ``hermes update``, a manual ``git pull``, a ZIP update, a file-sync restore) while
  36. ``__pycache__`` retains bytecode from the previous revision, and a later process trusts the stale
  37. ``.pyc`` instead of the fresh source.
  38. """
  39. from hermes_cli.main import PROJECT_ROOT, _clear_bytecode_cache, _read_git_revision_fingerprint
  40. try:
  41. fingerprint = _read_git_revision_fingerprint(PROJECT_ROOT)
  42. if not fingerprint:
  43. return # non-git install — the ZIP update path clears explicitly
  44. stamp_path = PROJECT_ROOT / _BYTECODE_FINGERPRINT_FILE
  45. try:
  46. recorded = stamp_path.read_text(encoding="utf-8-sig").strip()
  47. except OSError:
  48. recorded = ""
  49. if recorded == fingerprint:
  50. return
  51. removed = _clear_bytecode_cache(PROJECT_ROOT)
  52. if removed:
  53. logger.info(
  54. "Checkout changed since last launch (%s -> %s): cleared %d stale __pycache__ director%s",
  55. recorded or "unknown", fingerprint, removed, "y" if removed == 1 else "ies",
  56. )
  57. _record_bytecode_fingerprint()
  58. except Exception as exc:
  59. logger.debug("Stale-bytecode launch sweep failed: %s", exc)
  60. def _web_project_root(web_dir: Path) -> Path:
  61. """Repo root for a frontend dir (``web/`` or ``apps/<name>/``)."""
  62. return web_dir.parent.parent if web_dir.parent.name == "apps" else web_dir.parent
  63. def _web_dist_dir(web_dir: Path) -> Path:
  64. """Vite outputs to ``hermes_cli/web_dist/`` (vite.config.ts outDir), NOT ``web/dist/``."""
  65. return _web_project_root(web_dir) / "hermes_cli" / "web_dist"
  66. def _web_ui_build_needed(web_dir: Path) -> bool:
  67. from hermes_cli.source_build import source_product_current
  68. return not source_product_current(_web_project_root(web_dir), "web", _web_dist_dir(web_dir))
  69. def _write_web_ui_build_stamp(project_root: Path, web_dir: Path) -> None:
  70. """Historical updater entrypoint; current builders publish their own receipts."""
  71. from hermes_cli._old_updater import stop_for_relaunch
  72. stop_for_relaunch()
  73. def _console_print(text: str) -> None:
  74. """print() that survives cp1252-style consoles (arrow/check glyphs) via errors="replace"."""
  75. try:
  76. print(text)
  77. except UnicodeEncodeError:
  78. encoding = getattr(sys.stdout, "encoding", None) or "ascii"
  79. print(text.encode(encoding, errors="replace").decode(encoding, errors="replace"))
  80. def _run_with_idle_timeout(
  81. cmd: list[str], cwd: Path, *, idle_timeout_seconds: int = 180, indent: str = " ",
  82. env: dict[str, str] | None = None) -> subprocess.CompletedProcess:
  83. """Stop an old updater instead of running the retired build path."""
  84. from hermes_cli._old_updater import stop_for_relaunch
  85. stop_for_relaunch()
  86. def _nixos_build_env() -> dict[str, str] | None:
  87. """Stop an old updater instead of running the retired build path."""
  88. from hermes_cli._old_updater import stop_for_relaunch
  89. stop_for_relaunch()
  90. def _run_npm_install_deterministic(
  91. npm: str, cwd: Path, *, extra_args: tuple[str, ...] = (), capture_output: bool = True,
  92. env: dict[str, str] | None = None) -> subprocess.CompletedProcess:
  93. """Stop an old updater instead of running the retired build path."""
  94. from hermes_cli._old_updater import stop_for_relaunch
  95. stop_for_relaunch()
  96. def _build_web_ui(web_dir: Path, *, fatal: bool = False) -> bool:
  97. """Serialize dashboard rebuilds, checking freshness only after acquiring the lock."""
  98. from hermes_cli.runtime_state import _lock
  99. if not (web_dir / "package.json").exists():
  100. return True
  101. try:
  102. with (_web_project_root(web_dir) / ".web_ui_build.lock").open("ab") as lock_file:
  103. _lock(lock_file.fileno(), wait=True)
  104. return _do_build_web_ui(web_dir, fatal=fatal)
  105. except OSError as exc:
  106. _console_print(f" ✗ Could not lock the web UI build: {exc}")
  107. return False
  108. def _do_build_web_ui(web_dir: Path, *, fatal: bool = False) -> bool:
  109. """Build stale dashboard sources; failure is never reported as a usable build."""
  110. from hermes_cli.source_build import build_source_web, prepare_launch_dependencies, source_build_env
  111. if not (web_dir / "package.json").exists() or not _web_ui_build_needed(web_dir):
  112. return True
  113. project_root = _web_project_root(web_dir)
  114. _console_print("→ Building web UI...")
  115. try:
  116. env = source_build_env()
  117. prepare_launch_dependencies(project_root, env=env)
  118. build_source_web(project_root, env=env)
  119. except (OSError, subprocess.SubprocessError, RuntimeError) as exc:
  120. _console_print(f" {'✗' if fatal else '⚠'} Web UI build failed: {exc}")
  121. return False
  122. _console_print(" ✓ Web UI built")
  123. return True