update_finish.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. """Post-checkout completion shared by current and historical update callers."""
  2. from __future__ import annotations
  3. import json
  4. from pathlib import Path
  5. import sys
  6. def finish_update(*, root, assume_yes, gateway_mode, pre_update_snapshot_id,
  7. had_desktop_app_before_update, pre_update_version,
  8. plan, windows_resume, followups=None) -> None:
  9. """Finish the selected checkout; never fetch, switch branches or restore a stash.
  10. Same contract as the current completion (C3): the code is committed, so a failed build,
  11. maintenance step or fleet restart/verification is owed work, never a failed update.
  12. ``followups`` carries the steps that already failed (the product build); while any is owed
  13. the install stamp is withheld, the source-update tail stays pending for the next launch, and
  14. the gateway topology is left alone. An unsafe SQLite runtime is reported by maintenance and
  15. vetoes migration only.
  16. """
  17. from hermes_cli.update_cmd import (
  18. _run_post_update_maintenance,
  19. _restart_gateway_fleet_after_update, _verify_fleet_after_update,
  20. _write_gateway_update_exit_code, _resume_windows_gateways_and_merge_outcome,
  21. )
  22. from hermes_cli import update_receipt
  23. from hermes_cli.update_receipt import TAIL_FOLLOWUPS, record_build_stage, record_followup
  24. owed = followups if followups is not None else []
  25. runtime_safe = False
  26. try:
  27. runtime_safe = _run_post_update_maintenance(
  28. assume_yes=assume_yes, gateway_mode=gateway_mode,
  29. pre_update_snapshot_id=pre_update_snapshot_id,
  30. had_desktop_app_before_update=had_desktop_app_before_update,
  31. pre_update_version=pre_update_version, followups=owed,
  32. )
  33. except (Exception, SystemExit) as exc: # health: allow BLE001 -- the code is committed; retry later
  34. reason = str(exc) or type(exc).__name__
  35. record_followup("maintenance", reason)
  36. owed.append(("maintenance", reason))
  37. tail_owed = any(step in TAIL_FOLLOWUPS for step, _ in owed)
  38. record_build_stage(owed)
  39. if not tail_owed:
  40. from hermes_cli.source_stamp import write_source_stamp
  41. from hermes_cli.venv_sync import clear_completion
  42. try:
  43. write_source_stamp(Path(root))
  44. except (OSError, ValueError) as exc:
  45. print(f"⚠ Source update completed, but the install stamp could not be written: {exc}",
  46. file=sys.stderr)
  47. clear_completion(Path(root))
  48. # Restart can kill this process's gateway cgroup; record its result first. The code is
  49. # committed, so the watcher sees success; owed work is on the receipt.
  50. if gateway_mode:
  51. _write_gateway_update_exit_code(True)
  52. run = update_receipt._current.get()
  53. update_id = run.data.get("update_id") if run is not None else None
  54. try:
  55. restarted = _restart_gateway_fleet_after_update(plan, gateway_mode)
  56. _resume_windows_gateways_and_merge_outcome(restarted, windows_resume, gateway_mode)
  57. _verify_fleet_after_update(restarted, _pre_update_plan=plan, _windows_gateway_resume=windows_resume,
  58. update_complete=bool(runtime_safe) and not tail_owed)
  59. except (Exception, SystemExit) as exc: # health: allow BLE001 -- the code is committed (C3): the fleet obligation stays armed
  60. update_receipt.owe_followup(update_id, "gateway_restart", str(exc) or type(exc).__name__)
  61. def _restore_plan(data):
  62. if not data:
  63. return None
  64. from dataclasses import fields
  65. from hermes_cli.update_inventory import RuntimeRecord, UpdatePlan
  66. values = {field.name: data[field.name] for field in fields(UpdatePlan) if field.name in data}
  67. names = {field.name for field in fields(RuntimeRecord)}
  68. values["runtimes"] = [RuntimeRecord(**{key: value for key, value in row.items() if key in names})
  69. for row in data.get("runtimes", [])]
  70. return UpdatePlan(**values)
  71. def main(context: Path, result: Path) -> int:
  72. request = json.loads(context.read_text(encoding="utf-8-sig"))
  73. root = Path(request["root"])
  74. sys.path.insert(0, str(root))
  75. from hermes_cli import update_receipt
  76. token = request.get("windows_resume")
  77. resume = None
  78. restarting = request.get("restart_update", False)
  79. cli_started = False
  80. begun = False
  81. code = 1
  82. try:
  83. if not restarting:
  84. update_receipt.begin_update_receipt(previous=request.get("receipt"), correlation_id=request["update_id"])
  85. begun = update_receipt._current.get() is not None
  86. if not begun:
  87. raise RuntimeError("cannot create takeover receipt")
  88. from pm import receipt
  89. receipt.accept_worker_receipt(request.get("pm_receipt"), request["update_id"])
  90. update_receipt.record_step("historical_takeover", True, "continuing in the selected interpreter")
  91. # Preparation has already committed the dependency generation and
  92. # selected store Python. Bootstrap must run before any app imports;
  93. # its ordinary currency check is now a no-op, not another update.
  94. sys.argv = list(request["argv"]) if restarting else [str(root / "hermes"), "update"]
  95. import hermes_bootstrap
  96. # Import failures are update failures too: keep the original receipt
  97. # open before importing the application graph from the new checkout.
  98. from hermes_cli import main as cli
  99. from hermes_cli.source_build import build_update_products
  100. from hermes_cli.update_lock import UpdateLock, describe_holder
  101. from hermes_cli.update_cmd_windows import _resume_windows_gateways_after_update
  102. from hermes_cli.venv_sync import arm_completion
  103. cli.PROJECT_ROOT = root
  104. if restarting:
  105. # A known pre-pull capture has no completion worklist yet. The
  106. # current CLI owns its plan, receipt and lock; don't start a nested
  107. # receipt or execute the finish-only path against the old checkout.
  108. cli_started = True
  109. cli.main()
  110. else:
  111. plan = _restore_plan(request.get("plan"))
  112. with UpdateLock(install_root=root) as lock:
  113. if not lock.acquired and lock.holder is not None:
  114. print(describe_holder(lock.holder), file=sys.stderr)
  115. code = 2
  116. return code
  117. resume = _resume_windows_gateways_after_update
  118. desktop = request.get("desktop")
  119. if desktop is None:
  120. # Historical hooks can precede Desktop detection. Resolve
  121. # only that unknown state, in the freshly bootstrapped app.
  122. from hermes_cli.main_desktop import _desktop_dist_exists, _desktop_packaged_executable
  123. desktop_dir = root / "apps" / "desktop"
  124. desktop = (_desktop_packaged_executable(desktop_dir) is not None
  125. or _desktop_dist_exists(desktop_dir))
  126. # The tail is owed from here until it finishes: a failure or a kill leaves the
  127. # pending marker, so the next launch finishes it (same as the current completion).
  128. try:
  129. arm_completion(root)
  130. except OSError as exc: # stale dependencies still trigger the next launch's sync
  131. print(f" ⚠ Could not record the owed source-update tail: {exc}")
  132. owed: list[tuple[str, str]] = []
  133. try:
  134. build_update_products(root, desktop=desktop)
  135. except (Exception, SystemExit) as exc: # health: allow BLE001 -- committed code: owed, not failed
  136. reason = str(exc) or type(exc).__name__
  137. update_receipt.record_followup("build", reason)
  138. owed.append(("build", reason))
  139. finish_update(
  140. root=root, assume_yes=request["assume_yes"], gateway_mode=request["gateway_mode"],
  141. pre_update_snapshot_id=request.get("pre_update_snapshot_id"),
  142. had_desktop_app_before_update=desktop,
  143. pre_update_version=request.get("pre_update_version"),
  144. plan=plan, windows_resume=token, followups=owed,
  145. )
  146. code = 0
  147. except SystemExit as exc:
  148. code = exc.code if isinstance(exc.code, int) else 1
  149. except Exception as exc:
  150. update_receipt.record_step("historical_completion", False, str(exc))
  151. print(f"Update completion failed: {exc}", file=sys.stderr, flush=True)
  152. finally:
  153. terminal = update_receipt.finalized_receipt(request["update_id"]) or {}
  154. if code and request.get("gateway_mode"):
  155. # Even an application import failure must wake the gateway watcher. A run that already
  156. # closed as a success (an interrupt after verification) stays 0 (review regression 3).
  157. from hermes_constants import get_hermes_home
  158. from hermes_cli.runtime_state import _atomic_bytes
  159. _atomic_bytes(get_hermes_home() / ".update_exit_code",
  160. b"0" if terminal.get("outcome") == "success" else b"1")
  161. if restarting and not cli_started:
  162. # Startup failed before the replacement command could own a
  163. # receipt. Preserve the original handoff, just like preparation.
  164. update_receipt.begin_update_receipt(previous=request.get("receipt"), correlation_id=request["update_id"])
  165. begun = update_receipt._current.get() is not None
  166. attempted = False
  167. if resume is not None:
  168. try:
  169. resume(token)
  170. except Exception as exc:
  171. attempted = True
  172. update_receipt.owe_followup(request["update_id"], "windows_resume", f"Windows gateway recovery failed: {exc}")
  173. handled = cli_started or (begun and update_receipt._current.get() is None)
  174. written = update_receipt.finalize_pending_update_receipt(code, "historical takeover completion")
  175. result.write_text(json.dumps({
  176. # An attempt that failed is reported here: the historical parent must not replay it.
  177. "resume_handled": attempted or not token or not token.get("resume_needed"),
  178. "receipt_handled": handled or written is not None,
  179. }), encoding="utf-8")
  180. return code
  181. if __name__ == "__main__":
  182. raise SystemExit(main(Path(sys.argv[1]), Path(sys.argv[2])))