_update_takeover.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. """Absolute-path bootstrap for historical updater takeover; no app imports yet."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. from pathlib import Path
  6. import re
  7. import subprocess
  8. import sys
  9. import time
  10. _SHA = re.compile(r"[0-9a-f]{40}|[0-9a-f]{64}")
  11. def prepare(request: dict) -> tuple[Path, dict[str, str]]:
  12. """Provision the new graph before entering its application interpreter."""
  13. root = Path(request["root"])
  14. sys.path.insert(0, str(root))
  15. # The old shim's UI has been frozen since the pull; from here the new
  16. # tree can publish stages (and pop the panel the old shim couldn't).
  17. from hermes_cli.update_stage import ensure_panel, publish_stage
  18. ensure_panel(root)
  19. # An older updater hands off here instead of update_completion._prepare.
  20. from hermes_cli.gitlock import convert_treeless_checkout_first
  21. convert_treeless_checkout_first(root)
  22. publish_stage("Updating Python dependencies (PM)")
  23. from pm import receipt
  24. from pm.client import ensure_tools_for_sync, sync_venv, venv_is_current
  25. from pm.environments import activation_environment, install_state_dir, runtime_facts_path
  26. from hermes_cli._launchers import resolve_store_python
  27. from hermes_cli.venv_sync import (
  28. arm_completion, collect_superseded_generations, publish_launchers, refuse_foreign_owned_venv)
  29. # The historical updater already moved the tree: owe the tail and the fleet restart before the
  30. # first slow step, exactly like a current updater's commit point, so a kill from here on leaves
  31. # both for the next launch / `hermes update` instead of nothing.
  32. refuse_foreign_owned_venv(root)
  33. arm_completion(root)
  34. _arm_fleet_obligation(root)
  35. correlation = request["update_id"]
  36. with receipt.worker_context(correlation):
  37. # A pre-PM installation has no required-tool facts. A current Python
  38. # generation alone does not prove its Node/Git/tool closure is ready.
  39. ensure_tools_for_sync()
  40. from pm.extras import legacy_selection
  41. extras = legacy_selection(root) if not runtime_facts_path(root).is_file() else None
  42. repair_marker = install_state_dir(root) / ".repair-incomplete"
  43. # Repair preserves the old stamp. Changed source inputs instead need
  44. # an ordinary sync, which builds and validates a fresh generation too.
  45. repair = repair_marker.is_file() and venv_is_current(project_root=root)
  46. # An update never fails because of a plugin: misfits are disabled and reported.
  47. # The old venv's extras are carried, not asked for: none of them undoes a `--without`, and
  48. # one beyond [all] that no longer builds is left out.
  49. carried = {"carried": extras[1:]} if not repair and extras and len(extras) > 1 else {}
  50. sync_venv(None if repair else extras, explicit=True, project_root=root, repair=repair,
  51. evict_incompatible_plugins=not repair, **carried)
  52. collect_superseded_generations(root)
  53. request["pm_receipt"] = receipt.last_for_update(correlation)
  54. publish_launchers(root)
  55. repair_marker.unlink(missing_ok=True)
  56. for name in (".update-incomplete", ".lazy-refresh-incomplete"):
  57. (root / name).unlink(missing_ok=True)
  58. python = resolve_store_python(root)
  59. if python is None:
  60. raise RuntimeError("updated installation has no managed interpreter")
  61. return python, activation_environment(root)
  62. def _arm_fleet_obligation(root: Path) -> None:
  63. """Owe the fleet restart for the moved tree. Never raises: the update is already committed, and
  64. ``main`` would report any exception here as a failed update."""
  65. # This runs in the HISTORICAL interpreter, before PM syncs the new dependencies: only stdlib-only
  66. # modules here (``update_cmd_fleet``'s writer imports ``update_cmd`` -> config -> ruamel, which a
  67. # release older than ruamel does not have). Same arm and per-home fallback as the commit point's;
  68. # its False (a debt other profiles cannot see) is already said out loud, and the tree has moved.
  69. from hermes_cli.update_host_obligation import PROFILE_MARKER_NAME, arm_host_obligation
  70. from hermes_constants import get_hermes_home
  71. # A git-less archive root has no SHA: an SHA-less record still owes the restart; its readers hold
  72. # the fleet to the checkout instead of a named pull.
  73. arm_host_obligation(get_hermes_home() / PROFILE_MARKER_NAME, expected_sha=_head_sha(root))
  74. def _head_sha(root: Path) -> str:
  75. """HEAD of ``root``, or ``''``. Git may be off PATH (only PM's store copy) or broken here, so a
  76. failed ``rev-parse`` falls back to reading the ref files git itself would read."""
  77. from hermes_cli._early_recovery import _git_dir, _git_executable
  78. from hermes_cli.update_lock import _git_common_dir
  79. try:
  80. head = subprocess.run([_git_executable(), "-C", str(root), "rev-parse", "HEAD"], capture_output=True,
  81. text=True, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, timeout=60, check=False)
  82. if head.returncode == 0 and _SHA.fullmatch(head.stdout.strip()):
  83. return head.stdout.strip()
  84. except (OSError, subprocess.SubprocessError):
  85. pass # no runnable git: the ref files below are the whole of the evidence
  86. try:
  87. git_dir = _git_dir(root) # a linked worktree's own dir holds HEAD; its branch refs, the common dir
  88. common = _git_common_dir(root) or git_dir
  89. head = (git_dir / "HEAD").read_text(encoding="utf-8-sig").strip()
  90. if head.startswith("ref:"):
  91. ref = head.removeprefix("ref:").strip()
  92. loose = common / ref
  93. if loose.is_file():
  94. head = loose.read_text(encoding="utf-8-sig").strip()
  95. else:
  96. packed = (common / "packed-refs").read_text(encoding="utf-8-sig").splitlines()
  97. head = next((line.split(" ", 1)[0] for line in packed if line.endswith(f" {ref}")), "")
  98. except (OSError, UnicodeDecodeError):
  99. return ""
  100. return head if _SHA.fullmatch(head) else ""
  101. def _publish_gateway_exit_code(code: int) -> None:
  102. """Temp + rename with the stdlib only (the takeover runs before app imports are safe): the
  103. gateway watcher polls ``.update_exit_code`` and reads an empty file as exit 1."""
  104. from hermes_constants import get_hermes_home
  105. marker = get_hermes_home() / ".update_exit_code"
  106. temporary = marker.with_name(marker.name + ".tmp")
  107. temporary.write_text(f"{code}\n", encoding="utf-8")
  108. os.replace(temporary, marker)
  109. def _record_failure(request: dict, result: Path, code: int, detail: str) -> None:
  110. from hermes_cli import update_receipt
  111. update_receipt.record_step("historical_takeover", False, detail)
  112. saved = update_receipt.finalize_pending_update_receipt(code, detail)
  113. if request.get("gateway_mode"):
  114. _publish_gateway_exit_code(code)
  115. result.write_text(json.dumps({"receipt_handled": saved is not None, "resume_handled": False}), encoding="utf-8")
  116. def main() -> int:
  117. context, result = map(Path, sys.argv[1:3])
  118. request = json.loads(context.read_text(encoding="utf-8-sig"))
  119. sys.path.insert(0, request["root"])
  120. if "stopped_serves" in request:
  121. # Historical atexit cleanup may run after the update's result is fixed.
  122. # It must reuse that installation, never start a second update/repair.
  123. from hermes_cli._launchers import resolve_store_python
  124. from pm.environments import activation_environment
  125. root = Path(request["root"])
  126. python = resolve_store_python(root)
  127. if python is None:
  128. print("Cannot resume stopped backends: no selected Hermes interpreter", file=sys.stderr)
  129. return 1
  130. return subprocess.run(
  131. [str(python), "-I", "-B", "-X", "utf8", str(root / "hermes_cli/update_serve_resume.py"),
  132. str(context), str(result)], cwd=root, env=activation_environment(root),
  133. check=False,
  134. ).returncode
  135. from hermes_cli import update_receipt
  136. from hermes_cli.update_lock import UpdateLock, describe_holder
  137. # The takeover syncs dependencies and its update_finish child builds the checkout: hold
  138. # (or join, inherited from the old updater) the checkout lock, not just the marker (R2).
  139. lock = UpdateLock(install_root=Path(request["root"]))
  140. if not lock.acquire():
  141. print(describe_holder(lock.holder), file=sys.stderr)
  142. return 2
  143. old_receipt = request.get("receipt") or {}
  144. update_receipt.begin_update_receipt(previous=old_receipt, correlation_id=old_receipt.get("update_id"))
  145. request["update_id"] = update_receipt.current_correlation_id()
  146. try:
  147. try:
  148. python, env = prepare(request)
  149. request["receipt"] = update_receipt._current.get().data
  150. context.write_text(json.dumps(request), encoding="utf-8")
  151. except Exception as exc: # health: allow BLE001 -- pre-commit boundary: any preparation failure is the takeover's recorded failure
  152. print(f"Update preparation failed: {exc}", file=sys.stderr, flush=True)
  153. _record_failure(request, result, 1, f"historical takeover preparation failed: {exc}")
  154. return 1
  155. try:
  156. return _run_finish_child(request, context, result, python, env)
  157. except Exception as exc: # health: allow BLE001 -- post-commit boundary: the tree moved and the tail is armed
  158. _record_owed_finish(request, result, f"the update finish child could not run ({exc})")
  159. return 0
  160. finally:
  161. lock.release()
  162. def _run_finish_child(request: dict, context: Path, result: Path, python: Path, env: dict) -> int:
  163. # This file is new too. Direct execution bypasses normal launch-time
  164. # update liveness checks while the waiting parent still holds its lock.
  165. command = [str(python), "-I", "-B", "-X", "utf8", str(Path(request["root"]) / "hermes_cli/update_finish.py"),
  166. str(context), str(result)]
  167. from hermes_cli.update_custody import popen_post_commit
  168. # update_finish builds the checkout: POSIX lock fd; Windows bound to the job (or leased).
  169. with popen_post_commit(command, label="update finish child", cwd=request["root"], env=env) as child:
  170. try:
  171. code = child.wait()
  172. except BaseException:
  173. child.kill()
  174. raise
  175. if code != 0 and not result.is_file():
  176. _record_failure(request, result, code, f"completion child exited {code} without acknowledgement")
  177. return code
  178. def _record_owed_finish(request: dict, result: Path, detail: str) -> None:
  179. """The historical updater already moved the tree and ``prepare`` armed the completion tail, so a
  180. finish child that could not be started or resumed is an owed follow-up, never a failed update:
  181. the next launch runs the tail. Receipt finalized with exit 0. Never raises."""
  182. from contextlib import suppress
  183. print(f" ⚠ The update is installed, but its finishing steps did not run: {detail}. "
  184. "The next `hermes` launch finishes them.", file=sys.stderr, flush=True)
  185. saved = None
  186. # Receipt/exit-code writes are best effort: a committed update exits 0 whatever they hit.
  187. with suppress(Exception): # health: allow BLE001 -- post-commit boundary: a receipt error never fails the update
  188. from hermes_cli import update_receipt
  189. update_receipt.record_step("historical_takeover", False, f"owed: {detail}")
  190. saved = update_receipt.finalize_pending_update_receipt(0, f"committed; finishing steps owed: {detail}")
  191. if request.get("gateway_mode"):
  192. with suppress(Exception): # health: allow BLE001 -- post-commit boundary: see above
  193. _publish_gateway_exit_code(0)
  194. with suppress(OSError): # no result file: the old updater keeps its own receipt and resumes
  195. result.write_text(json.dumps({"receipt_handled": saved is not None, "resume_handled": False}),
  196. encoding="utf-8")
  197. if __name__ == "__main__":
  198. raise SystemExit(main())