update_cmd_maint.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. """Post-update maintenance for ``hermes update``: pre-update backup snapshot, state-db verify/restore, curator/FTS notices, FHS path guard, completion summary.
  2. Split out of ``update_cmd.py``, which re-imports every name so ``hermes_cli.update_cmd.<name>``
  3. still resolves/monkeypatches. Origin helpers are imported lazily per function (no cycle;
  4. test patches on ``update_cmd`` stay effective).
  5. """
  6. import logging
  7. from contextlib import suppress
  8. import os
  9. import subprocess
  10. import sys
  11. import time as _time
  12. from pathlib import Path
  13. from typing import Optional
  14. from hermes_cli.update_cmd_common import _best_effort
  15. from datetime import UTC
  16. # Log-record parity with the origin module.
  17. logger = logging.getLogger("hermes_cli.update_cmd")
  18. def _prepare_updated_checkout(project_root: Path, *, desktop: bool) -> None:
  19. """Historical updater hook: never complete inside the pre-swap interpreter."""
  20. from hermes_cli._old_updater import stop_for_relaunch
  21. stop_for_relaunch(incomplete=True)
  22. _PRE_UPDATE_SNAPSHOT_KEEP = 1
  23. # Per-file cap for the quick snapshot (larger files skipped with a warning): it protects
  24. # small hard-to-regenerate state, not a multi-GB state.db (24 GB cost ~60s + 24 GB/update).
  25. _PRE_UPDATE_SNAPSHOT_MAX_FILE_SIZE = 1 << 30 # 1 GiB
  26. #: Reinstalling through the official installer swaps in a Python whose SQLite is safe; the
  27. #: one-liner differs per OS (mirrors ``uninstall._REINSTALL_HINT``). windows -> command
  28. _REINSTALL_ONE_LINER = {
  29. True: "iex (irm https://hermes-agent.nousresearch.com/install.ps1)",
  30. False: "curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash",
  31. }
  32. def _sqlite_partial_completion_lines(sqlite_version: str) -> list[str]:
  33. """Shared ``⚠ Update partially complete`` wording for a vulnerable post-update SQLite, so the
  34. two completion banners cannot drift. The lead names the consequence, the second line the
  35. exact fix command."""
  36. from hermes_cli.update_cmd import _m
  37. return [
  38. f"⚠ Update partially complete — your Python's SQLite ({sqlite_version}) has a known "
  39. "corruption bug. Hermes works, but sessions could be damaged.",
  40. f" Fix: run the installer again ({_REINSTALL_ONE_LINER[bool(_m()._is_windows())]}) "
  41. "which installs a safe Python, then run `hermes doctor` to confirm.",
  42. ]
  43. def _load_updates_cfg() -> dict:
  44. """``updates`` section of config.yaml; ``{}`` on any failure."""
  45. from hermes_cli.config import load_config
  46. cfg = load_config() or {}
  47. updates = cfg.get("updates", {}) if isinstance(cfg, dict) else {}
  48. return updates if isinstance(updates, dict) else {}
  49. def _purge_stale_hermes_modules() -> None:
  50. """Historical updater hook; module-graph surgery cannot complete an update."""
  51. from hermes_cli._old_updater import stop_for_relaunch
  52. stop_for_relaunch(incomplete=True)
  53. def _reload_updated_runtime_modules() -> None:
  54. # Historical updater hook: dependency activation belongs to the next process.
  55. from hermes_cli._old_updater import stop_for_relaunch
  56. stop_for_relaunch()
  57. def _print_curator_first_run_notice() -> None:
  58. """Curator heads-up after update. Fires only when enabled AND never run — the window where
  59. the first pass (deferred one ``interval_hours``) is pending, so the user can preview or
  60. disable it first. Silent on steady state."""
  61. try:
  62. from agent import curator
  63. if not curator.is_enabled():
  64. return
  65. state = curator.load_state()
  66. except Exception:
  67. return
  68. if state.get("last_run_at"):
  69. return
  70. try:
  71. hours = curator.get_interval_hours()
  72. except Exception:
  73. hours = 24 * 7
  74. days = max(1, hours // 24)
  75. print()
  76. print("ℹ Skill curator")
  77. print(
  78. f" Background skill maintenance is enabled. First pass is deferred "
  79. f"~{days}d after installation; only agent-created skills are in "
  80. f"scope and nothing is ever auto-deleted (archive is recoverable)."
  81. )
  82. print(" Preview now: hermes curator run --dry-run")
  83. print(" Pause it: hermes curator pause")
  84. print(" Docs: https://hermes-agent.nousresearch.com/docs/user-guide/features/curator")
  85. def _print_fts_optimize_available_notice() -> None:
  86. """Advertise the opt-in FTS storage rebuild when state.db still needs one.
  87. ``sessions.fts_optimize_notice``: ``advise`` (default), ``require`` (firmer), ``off``.
  88. """
  89. try:
  90. from hermes_cli.config import load_config
  91. mode = str(((load_config() or {}).get("sessions") or {}).get("fts_optimize_notice", "advise")).strip().lower()
  92. except Exception:
  93. mode = "advise"
  94. if mode == "off":
  95. return
  96. try:
  97. from hermes_constants import get_hermes_home
  98. from hermes_state import SessionDB
  99. except Exception:
  100. return
  101. db_path = get_hermes_home() / "state.db"
  102. if not db_path.exists():
  103. return
  104. try:
  105. size_gb = db_path.stat().st_size / (1024 ** 3)
  106. except OSError:
  107. return
  108. # Small DBs: the win isn't worth the nag.
  109. if size_gb < 0.5:
  110. return
  111. db = None
  112. legacy_inline = False
  113. drift: list[str] = []
  114. try:
  115. db = SessionDB(db_path=db_path, read_only=True)
  116. # read_only opens skip schema init; probe the stored layout directly.
  117. from hermes_state_fts import fts_layout_drift
  118. drift = fts_layout_drift(db._conn)
  119. legacy_inline = db._db_has_legacy_inline_fts(db._conn)
  120. # Interrupted optimize-storage: v23 table shape but backfill markers / trash
  121. # tables remain. Re-running resumes it, so offer the command again.
  122. interrupted = bool(
  123. db._conn.execute(
  124. "SELECT 1 FROM state_meta "
  125. "WHERE key = 'fts_rebuild_high_water' LIMIT 1"
  126. ).fetchone()
  127. or db._conn.execute(
  128. "SELECT 1 FROM sqlite_master WHERE type = 'table' "
  129. "AND name LIKE 'fts\\_v22\\_trash\\_%' ESCAPE '\\' LIMIT 1"
  130. ).fetchone()
  131. or db._conn.execute(
  132. "SELECT 1 FROM state_meta WHERE key IN "
  133. "('fts_cjk_rebuild_high_water', 'fts_cjk_stale') LIMIT 1"
  134. ).fetchone()
  135. )
  136. except Exception:
  137. return
  138. finally:
  139. if db is not None:
  140. with suppress(Exception):
  141. db.close()
  142. if not drift and not interrupted:
  143. return # current layout already present (fresh/optimized)
  144. if interrupted:
  145. print()
  146. print("◆ Session database optimization incomplete")
  147. print(
  148. " A previous `hermes sessions optimize-storage` run was "
  149. "interrupted. Search still works; re-run the command to resume "
  150. "and finish reclaiming disk:"
  151. )
  152. print(" hermes sessions optimize-storage")
  153. return
  154. print()
  155. if legacy_inline:
  156. # The ~60% figure was measured for this transition only (inline copies of every message dropped).
  157. est_reclaim = size_gb * 0.6
  158. if mode == "require":
  159. print("◆ Session database upgrade required")
  160. print(
  161. f" Your search index uses the OLD storage layout and should be "
  162. f"upgraded. The new layout typically frees ~60% of state.db "
  163. f"(≈{est_reclaim:.1f} GB of your current {size_gb:.1f} GB) and is "
  164. f"required for continued optimal operation."
  165. )
  166. else:
  167. print("◆ Reclaim ~60% of your session database disk")
  168. print(
  169. f" Your search index uses the old storage layout. Upgrading it "
  170. f"typically frees ~60% of state.db — about {est_reclaim:.1f} GB "
  171. f"of your current {size_gb:.1f} GB."
  172. )
  173. else:
  174. if mode == "require":
  175. print("◆ Search index rebuild required by your notice policy")
  176. else:
  177. print("◆ Search index update available")
  178. print(
  179. " Your search index is on the compact layout from an earlier "
  180. "optimization, but a later release changed it. Running the "
  181. "command again rebuilds it in the current layout."
  182. )
  183. print(" Run when convenient: hermes sessions optimize-storage")
  184. print(
  185. " It runs in the foreground with a progress bar, is safe to "
  186. "interrupt/re-run, and never changes your conversations."
  187. )
  188. def _print_curator_recent_run_notice() -> None:
  189. """Print the latest background curator run summary (rename map) once, stamping
  190. ``last_run_summary_shown_at``. Silent when never run, already shown, or no rename info."""
  191. try:
  192. from agent import curator
  193. state = curator.load_state()
  194. except Exception:
  195. return
  196. last_run_at = state.get("last_run_at")
  197. if not last_run_at:
  198. return # no curator run yet — first-run notice handles this case
  199. if state.get("last_run_summary_shown_at") == last_run_at:
  200. return # already shown for this run
  201. summary = state.get("last_run_summary") or ""
  202. if not summary:
  203. return
  204. # Only a multi-line summary (rename map) is worth showing; still stamp it shown.
  205. if "\n" in summary:
  206. print()
  207. print(f"ℹ Skill curator — last run {_format_time_ago(last_run_at)}")
  208. for line in summary.splitlines():
  209. print(f" {line}")
  210. print(" (This message shows once per curator run. View anytime: hermes curator status)")
  211. with suppress(Exception):
  212. state["last_run_summary_shown_at"] = last_run_at
  213. curator.save_state(state)
  214. def _format_time_ago(iso_ts: str) -> str:
  215. """Render an ISO timestamp as `Xh ago` / `Xd ago` / `Xm ago`. Best effort."""
  216. try:
  217. from datetime import datetime, timezone
  218. ts = datetime.fromisoformat(iso_ts)
  219. if ts.tzinfo is None:
  220. ts = ts.replace(tzinfo=UTC)
  221. secs = int((datetime.now(UTC) - ts).total_seconds())
  222. if secs < 60:
  223. return "just now"
  224. if secs < 3600:
  225. return f"{secs // 60}m ago"
  226. if secs < 86400:
  227. return f"{secs // 3600}h ago"
  228. return f"{secs // 86400}d ago"
  229. except Exception:
  230. return "recently"
  231. def _reload_process_scan_modules() -> None:
  232. """Historical updater hook; scans now run only in fresh completion Python."""
  233. from hermes_cli._old_updater import stop_for_relaunch
  234. stop_for_relaunch(incomplete=True)
  235. def _finish_dashboard_update_cleanup(
  236. node_failures: list[str], already_restarted_units: set[str] | None = None
  237. ) -> None:
  238. """Historical updater hook; do not continue a pre-PM update after the swap."""
  239. from hermes_cli._old_updater import stop_for_relaunch
  240. stop_for_relaunch()
  241. def _refresh_dashboard_after_update(*, already_restarted_units: set[str] | None = None) -> set[int]:
  242. """Refresh managed dashboards or stop stale manual ones after an update; returns the PIDs it
  243. stopped and could not bring back, so the receipt records them ``failed`` (#109290).
  244. *already_restarted_units*: systemd unit names (no ``.service``) the fleet-restart loop
  245. already handled (restarted, declined, or failed; each already reported), so a Serve-only
  246. install isn't restarted a second time here and a declined or failed unit isn't retried (#141021).
  247. See #83595.
  248. """
  249. from hermes_cli.update_cmd import _m, _record_update_step
  250. from hermes_constants import get_hermes_home
  251. try:
  252. stop_result = _m()._kill_stale_dashboard_processes(
  253. restart_managed=True, already_restarted_units=already_restarted_units,
  254. scope_home=str(get_hermes_home()),
  255. )
  256. except Exception as exc:
  257. # Isolated like every sibling post-update step: a failure here (#112604) used to abort
  258. # the fleet matrix, reconciliation and the inner receipt finalize that follow it. A
  259. # dashboard/serve left on pre-update code is still caught by the survivor probe →
  260. # reconciliation (an owed gateway_restart follow-up).
  261. logger.warning("Post-update dashboard cleanup failed: %s", exc)
  262. _record_update_step("dashboard_cleanup", False, f"{type(exc).__name__}: {exc}")
  263. print()
  264. print(f"⚠ Could not refresh running dashboard/serve process(es): {exc}")
  265. print(" If one is still running, restart it so it serves the updated code:")
  266. print(" hermes dashboard --port <port> (or: systemctl --user restart hermes-dashboard)")
  267. return set()
  268. unrecovered = {int(pid) for pid in stop_result.get("unrecovered") or ()}
  269. if not unrecovered:
  270. return unrecovered
  271. print()
  272. print("⚠ A web dashboard/serve process was stopped during update and could not be auto-restarted.")
  273. print(" Re-launch it when you want the web UI back:")
  274. print(" hermes dashboard --port <port>")
  275. return unrecovered
  276. def _print_update_completion(message: str) -> None:
  277. """Print the outcome (with branch @ sha so drift is visible) plus, when launched by the
  278. dashboard with an action id, a receipt line the Desktop matches after restart.
  279. See #47359, #58764.
  280. """
  281. from hermes_cli.update_cmd import _branch_head_suffix
  282. print(f"{message}{_branch_head_suffix()}")
  283. action_id = os.environ.get("HERMES_ACTION_ID", "")
  284. if len(action_id) == 32 and all(char in "0123456789abcdef" for char in action_id):
  285. print(f"=== hermes-update completed {action_id} ===")
  286. def _read_project_version() -> str | None:
  287. """``version`` from the checkout's pyproject.toml (not importlib.metadata, which still
  288. describes the OLD version after a pull). None on any failure — cosmetic, never breaks."""
  289. from hermes_cli.update_cmd import _m
  290. try:
  291. import tomllib
  292. with open(_m().PROJECT_ROOT / "pyproject.toml", "rb") as fh: # windows-footgun: ok — binary mode, tomllib requires bytes
  293. version = tomllib.load(fh).get("project", {}).get("version")
  294. return str(version) if version else None
  295. except Exception:
  296. return None
  297. def _checkout_version() -> str | None:
  298. """The checkout's runtime identity, computed exactly as ``write_source_stamp`` publishes it.
  299. pyproject.toml is an inert 0.0.0 on source checkouts; the release a checkout
  300. runs is derived from its reachable tags.
  301. """
  302. from hermes_cli.update_cmd import _m
  303. from hermes_cli.version_info import _git_version_info
  304. info = _git_version_info(Path(_m().PROJECT_ROOT), include_untracked=True)
  305. return info.derived_version if info.commit else None
  306. def _update_complete_message(pre_version: str | None) -> str:
  307. """Completion line with ``vA → vB`` when known; plain when either side is unknown or
  308. the version did not change.
  309. Ported from PrimeIntellect-ai/prime-agent#630: after a successful self-update, show both versions
  310. (``v0.19.4 → v0.20.0``) so the user can see what they actually got. Falls back to the plain message when
  311. either side is unknown or the version did not change.
  312. """
  313. def shown(version: str) -> str:
  314. # A tagless checkout's identity is ``git.<sha>``, not a release number.
  315. return f"v{version}" if version[:1].isdigit() else version
  316. post_version = _checkout_version()
  317. if pre_version and post_version and pre_version != post_version:
  318. return f"✓ Update complete! ({shown(pre_version)} → {shown(post_version)})"
  319. if post_version:
  320. return f"✓ Update complete! ({shown(post_version)})"
  321. return "✓ Update complete!"
  322. def _post_update_sqlite_runtime_status():
  323. """Return whether the interpreter used after update has safe SQLite."""
  324. from hermes_cli.sqlite_runtime import probe_sqlite_runtime
  325. # Completion already runs on PM's selected Python, not the obsolete repo venv.
  326. info = probe_sqlite_runtime(Path(sys.executable))
  327. return info is not None and not info.wal_reset_vulnerable, info
  328. def _print_verified_update_completion(message: str) -> bool:
  329. """Print a success completion only after probing the next Hermes runtime."""
  330. from hermes_cli.update_cmd import _post_update_sqlite_runtime_status
  331. if not message.startswith("✓"):
  332. _print_update_completion(message)
  333. return False
  334. sqlite_runtime_ok, sqlite_info = _post_update_sqlite_runtime_status()
  335. if sqlite_info is None:
  336. # Grace path: an unprobeable interpreter (dev checkout, no probe subprocess) must not
  337. # fail the update — only a POSITIVE vulnerable probe withholds success.
  338. logger.debug("Post-update SQLite runtime probe unavailable; not blocking")
  339. if sqlite_info is None or sqlite_runtime_ok:
  340. _print_update_completion(message)
  341. return True
  342. print()
  343. for line in _sqlite_partial_completion_lines(sqlite_info.sqlite_version_string):
  344. print(line)
  345. return False
  346. def _print_update_summary(*, node_failures: list, desktop_build_ok: bool, pre_update_version: str | None) -> bool:
  347. """Historical updater hook; old soft-build results cannot establish completion."""
  348. from hermes_cli._old_updater import stop_for_relaunch
  349. stop_for_relaunch()
  350. def _restore_state_db_from_snapshot(state_path: Path, snap_state: Path) -> bool:
  351. """Restore through backup's authority reservation and pre-publication epoch floor.
  352. Use the single-database primitive, not whole-profile restore: post-update config
  353. and unrelated files must not roll back with a damaged transcript store.
  354. """
  355. from hermes_cli.backup import verify_sqlite_integrity
  356. from hermes_cli.backup_restore import _safe_restore_db
  357. snap_state.stat() # Preserve the caller's OSError reporting for missing snapshots.
  358. if not verify_sqlite_integrity(snap_state, check_header=True, run_pragma=True).get("valid"):
  359. return False
  360. if not _safe_restore_db(snap_state, state_path):
  361. print(" ✗ Auto-restore refused or failed; state.db was not restored.")
  362. print(" Stop the owning runtime and close database handles before retrying.")
  363. print(" If the canonical runtime epoch is unreadable, do not restore in place:")
  364. print(" use `hermes sessions recover --source <snapshot> --output <separate-path>`")
  365. print(" to salvage transcripts into a separate output.")
  366. return False
  367. restored = verify_sqlite_integrity(state_path, check_header=True, run_pragma=True)
  368. return bool(restored.get("valid"))
  369. def _verify_and_restore_one_state_db(home: Path, *, label: str) -> None:
  370. """Integrity check + auto-restore for ONE home's state.db from its newest valid snapshot.
  371. Never raises: a guard that crashes the update tail is worse than what it detects."""
  372. try:
  373. from hermes_cli.backup import _quick_snapshot_root, verify_sqlite_integrity
  374. state_path = home / "state.db"
  375. if not state_path.exists():
  376. return
  377. ok = verify_sqlite_integrity(state_path, check_header=True, run_pragma=True)
  378. if ok.get("valid"):
  379. logger.debug("Post-update state.db integrity OK (%s): %s", label, ok.get("message"))
  380. return
  381. print()
  382. print(f"⚠ state.db is corrupted after update ({label}): " + ok.get("message", "unknown error"))
  383. snap_root = _quick_snapshot_root(home)
  384. if not snap_root.exists():
  385. print(" ⚠ No pre-update snapshot for this home")
  386. return
  387. for snap_dir in sorted((d for d in snap_root.iterdir() if d.is_dir()), reverse=True):
  388. snap_state = snap_dir / "state.db"
  389. if not snap_state.exists():
  390. continue
  391. if not verify_sqlite_integrity(snap_state, check_header=True, run_pragma=True).get("valid"):
  392. continue
  393. try:
  394. if _restore_state_db_from_snapshot(state_path, snap_state):
  395. print(f" ✓ Auto-restored from snapshot {snap_dir.name} ({label})")
  396. else:
  397. print(" ✗ Auto-restore did not complete; see the restore diagnostic above.")
  398. except OSError as exc:
  399. print(f" ✗ Auto-restore file copy failed: {exc}")
  400. return
  401. print(" ⚠ No valid pre-update snapshot found for this home")
  402. except Exception as exc:
  403. logger.debug("Post-update state.db guard (%s) failed: %s", label, exc)
  404. def _verify_and_restore_state_dbs_post_update() -> None:
  405. """Integrity guard for the ROOT state.db AND every sibling profile's (the snapshot covers
  406. siblings, so the guard must too or a corrupt profile DB goes undetected).
  407. See #97994.
  408. """
  409. from hermes_cli.update_cmd import get_hermes_home
  410. home = get_hermes_home()
  411. _verify_and_restore_one_state_db(home, label="default home")
  412. with _best_effort('Sibling-profile state.db guard sweep failed: %s'):
  413. from hermes_cli.backup import _sibling_profile_homes
  414. for name, profile_home in _sibling_profile_homes(home):
  415. _verify_and_restore_one_state_db(profile_home, label=f"profile {name}")
  416. def _invalidate_live_plugin_catalog_caches() -> None:
  417. """Drop the cached live plugin catalog under the active home AND every sibling profile's.
  418. The checkout is shared across profiles, so an update's code swap changes every profile's
  419. catalog truth at once: a live snapshot cached before the swap would out-vote the newer
  420. in-tree catalog (the pin it just bumped, the entry it just added) for the rest of the cache
  421. TTL (#119340). Mirrors the state.db guard's home + siblings iteration. Never raises —
  422. :func:`plugin_catalog.invalidate_live_cache_for_home` is best-effort per home.
  423. """
  424. from hermes_cli.update_cmd import get_hermes_home
  425. home = get_hermes_home()
  426. from hermes_cli.plugin_catalog import invalidate_live_cache_for_home
  427. invalidate_live_cache_for_home(home)
  428. with suppress(Exception):
  429. from hermes_cli.backup import _sibling_profile_homes
  430. for _name, profile_home in _sibling_profile_homes(home):
  431. invalidate_live_cache_for_home(profile_home)
  432. def _report_catalog_plugin_updates() -> None:
  433. from hermes_cli.backup import _sibling_profile_homes
  434. from hermes_cli.plugins_auto_update import report_catalog_updates_after_hermes_update
  435. from hermes_cli.update_cmd import get_hermes_home
  436. home = get_hermes_home()
  437. report_catalog_updates_after_hermes_update(home, _sibling_profile_homes(home))
  438. def _print_bundled_skills_sync_report() -> None:
  439. """Run ``sync_skills`` (copies new, updates changed, respects user deletions) and print its summary."""
  440. from tools.skills_sync import sync_skills
  441. result = sync_skills(quiet=True)
  442. if result["copied"]:
  443. print(f" + {len(result['copied'])} new: {', '.join(result['copied'])}")
  444. if result.get("updated"):
  445. print(f" ↑ {len(result['updated'])} updated: {', '.join(result['updated'])}")
  446. if result.get("user_modified"):
  447. print(f" ~ {len(result['user_modified'])} user-modified (kept)")
  448. print(" → see them: hermes skills list-modified (diff/reset to resume updates)")
  449. if result.get("cleaned"):
  450. print(f" − {len(result['cleaned'])} removed from manifest")
  451. if result.get("relocated"):
  452. print(f" → {len(result['relocated'])} moved to new upstream paths: {', '.join(result['relocated'])}")
  453. if not result["copied"] and not result.get("updated"):
  454. print(" ✓ Skills are up to date")
  455. def _ensure_fhs_path_guard() -> None:
  456. """Ensure /usr/local/bin is on PATH for RHEL-family root non-login shells (su, sudo -s,
  457. tmux), where neither /etc/bashrc nor .bash_profile adds it. Mirrors install.sh. Idempotent;
  458. no-op on non-Linux/non-root/non-FHS or when ``bash -i -c 'command -v hermes'`` resolves."""
  459. from hermes_cli.update_cmd import _m
  460. if _m().sys.platform != "linux":
  461. return
  462. try:
  463. if os.geteuid() != 0: # windows-footgun: ok — Linux FHS helper, guarded by sys.platform == "linux" above + AttributeError catch
  464. return
  465. except AttributeError:
  466. return
  467. # Only for FHS-layout installs (link at /usr/local/bin/hermes).
  468. fhs_link = Path("/usr/local/bin/hermes")
  469. if not fhs_link.is_symlink() and not fhs_link.exists():
  470. return
  471. # ``bash -i -c`` sources ~/.bashrc but NOT ~/.bash_profile or /etc/profile — the exact
  472. # scenario where RHEL root loses /usr/local/bin.
  473. home = os.environ.get("HOME") or "/root"
  474. try:
  475. probe = subprocess.run(
  476. [
  477. "env",
  478. "-i",
  479. f"HOME={home}",
  480. f"TERM={os.environ.get('TERM', 'dumb')}",
  481. "bash",
  482. "-i",
  483. "-c",
  484. "command -v hermes",
  485. ],
  486. # Fallback: blunt systemctl restart. This is what the old code always did; we get here only when
  487. # the graceful path failed (unit missing SIGUSR1 wiring, drain exceeded the budget,
  488. # restart-policy mismatch). Always `reset-failed` first. If systemd's own auto-restart attempts
  489. # already parked the unit in a failed state (transient CHDIR / OOM / filesystem race after our
  490. # drain + exit-75), a plain `systemctl restart` can wedge against the RestartSec backoff and
  491. # leave the unit dead. Clearing the failed state first makes the restart idempotent. Mirrors the
  492. # recovery path in `hermes gateway restart` (`systemd_restart()`) as of PR #20949.
  493. capture_output=True,
  494. text=True, encoding="utf-8", errors="replace",
  495. timeout=10,
  496. check=False,
  497. )
  498. except (FileNotFoundError, subprocess.TimeoutExpired):
  499. return # no bash or probe hung — don't block update on this
  500. if probe.returncode == 0:
  501. return # already on PATH, nothing to do
  502. path_line = 'export PATH="/usr/local/bin:$PATH"'
  503. path_comment = "# Hermes Agent — ensure /usr/local/bin is on PATH (RHEL non-login shells)"
  504. wrote_any = False
  505. for candidate in (".bashrc", ".bash_profile"):
  506. cfg = Path(home) / candidate
  507. if not cfg.is_file():
  508. continue
  509. try:
  510. existing = cfg.read_text(errors="replace", encoding="utf-8-sig")
  511. except OSError:
  512. continue
  513. # Idempotency: any uncommented PATH line referencing /usr/local/bin (install.sh grep).
  514. if any(
  515. "/usr/local/bin" in line and "PATH" in line and not line.lstrip().startswith("#")
  516. for line in existing.splitlines()
  517. ):
  518. continue
  519. try:
  520. with cfg.open("a", encoding="utf-8") as f:
  521. f.write("\n" + path_comment + "\n" + path_line + "\n")
  522. except OSError as e:
  523. print(f" ⚠ Could not update {cfg}: {e}")
  524. continue
  525. print(f" ✓ Added /usr/local/bin to PATH in {cfg}")
  526. wrote_any = True
  527. if wrote_any:
  528. print(" (reload your shell or run 'source ~/.bashrc' to pick it up)")
  529. def _ensure_acp_launcher() -> None:
  530. """Historical export; launcher policy belongs to the launcher owner."""
  531. from hermes_cli import _launchers
  532. from hermes_cli.update_cmd import _m
  533. _launchers.expose_cli(_m().PROJECT_ROOT)
  534. _BACKUP_MODE_ALIASES = {
  535. "off": "off", "false": "off", "none": "off", "disabled": "off",
  536. "full": "full", "zip": "full", "true": "full",
  537. "quick": "quick",
  538. }
  539. def _resolve_pre_update_backup_mode(args) -> str:
  540. """Backup mode ``off``/``quick``/``full``. CLI flags win (``--no-backup`` beats ``--backup``);
  541. config accepts mode strings plus legacy booleans (true→full, false→off, which also disables
  542. the quick snapshot). Default ``quick``."""
  543. if getattr(args, "no_backup", False):
  544. return "off"
  545. if getattr(args, "backup", False):
  546. return "full"
  547. try:
  548. raw = _load_updates_cfg().get("pre_update_backup", "quick")
  549. except Exception as exc:
  550. logger.debug("Could not load config for pre-update backup: %s", exc)
  551. raw = "quick"
  552. if raw is True:
  553. return "full"
  554. if raw is False:
  555. return "off"
  556. mode = _BACKUP_MODE_ALIASES.get(str(raw).strip().lower())
  557. if mode is None:
  558. logger.warning("Unknown updates.pre_update_backup value %r — using 'quick'", raw)
  559. return "quick"
  560. return mode
  561. def _verify_state_db_after_snapshot(snapshot_id: str) -> None:
  562. """Verify live state.db after the snapshot: a concurrent process (antivirus, killed
  563. gateway, Windows filter driver) can corrupt it and we'd otherwise exit 0 silently."""
  564. from hermes_cli.backup import _quick_snapshot_root, verify_sqlite_integrity
  565. from hermes_cli.config import get_hermes_home
  566. _src_path = get_hermes_home() / "state.db"
  567. if not _src_path.exists():
  568. return
  569. _integrity = verify_sqlite_integrity(
  570. _src_path, check_header=True, run_pragma=True, max_bytes=_PRE_UPDATE_SNAPSHOT_MAX_FILE_SIZE,
  571. )
  572. if _integrity.get("valid"):
  573. return
  574. print(f" ⚠ state.db integrity check FAILED after snapshot: {_integrity.get('message', 'unknown error')}")
  575. _snap_state = _quick_snapshot_root(get_hermes_home()) / snapshot_id / "state.db"
  576. if not _snap_state.exists():
  577. print(" ⚠ Snapshot does not contain state.db (was skipped or too large).")
  578. elif verify_sqlite_integrity(_snap_state, check_header=True, run_pragma=True).get("valid"):
  579. print(" ✓ Snapshot copy is valid — continuing update.")
  580. print(" If state.db is lost after update it will be auto-restored.")
  581. else:
  582. print(" ✗ Snapshot copy ALSO failed integrity — the source was already corrupted before the backup.")
  583. print()
  584. def _run_quick_snapshots() -> Optional[str]:
  585. """Quick snapshot of the root home plus every sibling profile; returns the root snapshot id."""
  586. from hermes_cli.update_cmd import _record_update_step
  587. from hermes_cli.backup import create_quick_snapshot
  588. snapshot_id = create_quick_snapshot(
  589. label="pre-update", keep=_PRE_UPDATE_SNAPSHOT_KEEP, max_file_size=_PRE_UPDATE_SNAPSHOT_MAX_FILE_SIZE,
  590. )
  591. if snapshot_id:
  592. _verify_state_db_after_snapshot(snapshot_id)
  593. print(f"◆ Pre-update snapshot: {snapshot_id}")
  594. # The code swap + fleet restart touch EVERY profile, so each gets the same snapshot
  595. # under its own state-snapshots/. Best-effort per profile.
  596. with _best_effort('Sibling profile snapshots failed: %s'):
  597. from hermes_cli.backup import create_pre_update_snapshots_all_profiles
  598. _sibling_snaps = create_pre_update_snapshots_all_profiles(
  599. keep=_PRE_UPDATE_SNAPSHOT_KEEP, max_file_size=_PRE_UPDATE_SNAPSHOT_MAX_FILE_SIZE,
  600. )
  601. if _sibling_snaps:
  602. print("◆ Sibling profile snapshot(s): " + ", ".join(sorted(_sibling_snaps)))
  603. _record_update_step(
  604. "sibling_profile_snapshots",
  605. True,
  606. ", ".join(f"{k}={v}" for k, v in sorted(_sibling_snaps.items())),
  607. )
  608. import hermes_cli.update_cmd_config as _cfg
  609. # The reader lives in update_cmd_config; write ITS module global, not ours.
  610. _cfg._LAST_SIBLING_SNAPSHOTS = _sibling_snaps
  611. return snapshot_id
  612. def _run_full_backup() -> None:
  613. """Zip HERMES_HOME under ``backups/`` (restorable via ``hermes import``). Never raises."""
  614. try:
  615. from hermes_cli.backup import create_pre_update_backup
  616. except Exception as exc:
  617. print(f"⚠ Pre-update backup: could not load backup module ({exc}); continuing update.")
  618. print()
  619. return
  620. try:
  621. _keep = _load_updates_cfg().get("backup_keep", 5)
  622. except Exception:
  623. _keep = 5
  624. print("◆ Creating pre-update backup...")
  625. t0 = _time.monotonic()
  626. try:
  627. out_path = create_pre_update_backup(keep=int(_keep))
  628. except Exception as exc: # defensive — helper already swallows, but just in case
  629. print(f" ⚠ Backup failed: {exc}")
  630. print(" Continuing with update.")
  631. print()
  632. return
  633. elapsed = _time.monotonic() - t0
  634. if out_path is None:
  635. print(" ⚠ Backup skipped (no files found or write failed); continuing update.")
  636. print()
  637. return
  638. try:
  639. size_bytes = out_path.stat().st_size
  640. except OSError:
  641. size_bytes = 0
  642. from hermes_cli.sizefmt import format_bytes
  643. # display_hermes_home so the user sees ~/.hermes/...
  644. try:
  645. from hermes_constants import get_hermes_home, display_hermes_home
  646. display_path = f"{display_hermes_home()}/{out_path.relative_to(get_hermes_home())}"
  647. except Exception:
  648. display_path = str(out_path)
  649. print(f" Saved: {display_path} ({format_bytes(size_bytes)}, {elapsed:.1f}s)")
  650. print(f" Restore: hermes import {out_path}")
  651. print(" Disable: set updates.pre_update_backup: quick (or off) in config.yaml")
  652. print()
  653. def _run_pre_update_backup(args) -> Optional[str]:
  654. """Run the pre-update backup; return the quick-snapshot id (None when off/failed). Never raises.
  655. ``off`` — nothing. ``quick`` (default) — snapshot of critical small files under
  656. ``state-snapshots/``, files over 1 GiB skipped so a bloated state.db can't stall the update.
  657. ``full`` — quick snapshot PLUS a zip of HERMES_HOME under ``backups/`` (``hermes import``).
  658. Explicit user opt-out is honored fully. See #34600.
  659. """
  660. mode = _resolve_pre_update_backup_mode(args)
  661. if mode == "off":
  662. if getattr(args, "no_backup", False):
  663. print("◆ Pre-update backup: skipped (--no-backup)")
  664. print()
  665. # Config-level off is silent: the user opted out.
  666. return None
  667. snapshot_id = None
  668. try:
  669. snapshot_id = _run_quick_snapshots()
  670. except Exception as exc:
  671. logger.warning("Pre-update snapshot failed: %s", exc)
  672. snapshot_detail = f" ({exc})"
  673. else:
  674. snapshot_detail = ""
  675. if not snapshot_id:
  676. # Best-effort by design (8ed599dc054: a broken backup never blocks the update), but a
  677. # swallowed failure is how a user discovers post-hoc that the receipt says
  678. # ``ok: false`` and nothing was there to restore (#114592). Say it on stdout, once,
  679. # before any code moves.
  680. print(f" ⚠ Pre-update snapshot FAILED — no recovery point was saved{snapshot_detail}.")
  681. print(" Continuing with update (set updates.pre_update_backup: off to silence this).")
  682. print()
  683. if mode != "full":
  684. if snapshot_id:
  685. print()
  686. return snapshot_id
  687. _run_full_backup()
  688. return snapshot_id
  689. def _sweep_bytecode_after_update(branch: str) -> None:
  690. """Clear stale ``__pycache__`` (else gateway restart ImportErrors on names absent from old
  691. bytecode), re-stamp the fingerprint, refresh the bootstrap cache scripts.
  692. Never fails the committed update: a failed sweep is a ``bytecode_sweep`` follow-up and the
  693. fingerprint stays un-stamped, so the launch-time sweep (fingerprint mismatch) retries it.
  694. """
  695. from hermes_cli.update_cmd import _m
  696. from hermes_cli.update_receipt import record_followup
  697. # Timestamp-based .pyc validation can accept old bytecode after the source swap.
  698. try:
  699. removed = _m()._clear_bytecode_cache(_m().PROJECT_ROOT)
  700. if removed:
  701. print(f" ✓ Cleared {removed} stale __pycache__ director{'y' if removed == 1 else 'ies'}")
  702. _m()._record_bytecode_fingerprint()
  703. except Exception as exc: # health: allow BLE001 -- owed to the next launch's fingerprint sweep
  704. record_followup("bytecode_sweep", str(exc) or type(exc).__name__)
  705. try:
  706. _m()._refresh_bootstrap_cache_scripts(branch)
  707. except Exception as exc: # health: allow BLE001 -- refreshed again by the next update
  708. record_followup("bootstrap_scripts", str(exc) or type(exc).__name__,
  709. retry="the next `hermes update` refreshes them")
  710. def _profile_skill_sync_status(r) -> str:
  711. if r and r.get("skipped_opt_out"):
  712. return "opted out (--no-skills)"
  713. if not r:
  714. return "sync failed"
  715. parts = []
  716. for key, fmt in (("copied", "+{} new"), ("updated", "↑{} updated"), ("user_modified", "~{} user-modified")):
  717. count = len(r.get(key, []))
  718. if count:
  719. parts.append(fmt.format(count))
  720. return ", ".join(parts) if parts else "up to date"
  721. def _sync_profiles_after_update() -> None:
  722. """Best-effort per-profile syncs: bundled skills, ``.env`` backfill, Honcho profiles."""
  723. # All profiles incl. the active one: seed_profile_skills() subprocesses with an explicit
  724. # HERMES_HOME, so sync_skills()'s module-level HERMES_HOME cache can't skew it.
  725. with suppress(Exception):
  726. from hermes_cli.profiles import list_profiles, seed_profile_skills
  727. all_profiles = list_profiles()
  728. if all_profiles:
  729. print()
  730. print("→ Syncing bundled skills to all profiles...")
  731. for p in all_profiles:
  732. try:
  733. print(f" {p.name}: {_profile_skill_sync_status(seed_profile_skills(p.path, quiet=True))}")
  734. except Exception as pe:
  735. print(f" {p.name}: error ({pe})")
  736. # Seed the placeholder .env into any named profile lacking one (never a copy of the default's).
  737. with suppress(Exception):
  738. # See #44792.
  739. from hermes_cli.profiles import backfill_profile_envs
  740. backfilled = backfill_profile_envs(quiet=True)
  741. if backfilled:
  742. print()
  743. print(f"→ Seeded .env for {len(backfilled)} profile(s) (empty placeholder): {', '.join(backfilled)}")
  744. with suppress(Exception):
  745. from plugins.memory import import_provider_module
  746. synced = import_provider_module("honcho", "cli").sync_honcho_profiles_quiet()
  747. if synced:
  748. print(f"\n-> Honcho: synced {synced} profile(s)")
  749. def _refresh_cua_driver_after_update() -> None:
  750. """Reconcile an installed optional package, never a user-selected external binary."""
  751. import pm
  752. if not _load_updates_cfg().get("refresh_cua_driver", True):
  753. return
  754. if os.environ.get("HERMES_CUA_DRIVER_CMD", "").strip():
  755. return
  756. if pm.installed_package("cua-driver", allow_outdated=True) is None:
  757. return
  758. if sys.platform == "win32":
  759. # The scheduled task targets a versioned binary. Selecting a new pin
  760. # without re-registering leaves it stale; registration requires UAC.
  761. print("\n→ Windows cua-driver refresh deferred (autostart registration requires UAC).")
  762. print(" Run `hermes computer-use install --upgrade` in an interactive terminal.")
  763. return
  764. print("\n→ Preparing pinned cua-driver (Computer Use)...")
  765. if sys.platform == "darwin":
  766. # PM preserves the signed app; setup validates and registers its new path
  767. # with LaunchServices. This path never requests permissions or elevation.
  768. from hermes_cli.tools_config_cua import install_cua_driver
  769. install_cua_driver(show_installer_progress=False)
  770. else:
  771. pm.ensure("cua-driver", explicit=True)
  772. def _install_default_tools_after_update() -> None:
  773. """Give the install its optional default tools: the PM defaults (Chromium,
  774. cua-driver). browser_exec's engine (browser-harness) is a venv dependency.
  775. Runs at the end of both the installers (via the source completion) and
  776. ``hermes update``: a source update re-syncs only the venv, so a tool that became
  777. a default after this install was created would never arrive otherwise. Declined
  778. packages stay declined (pm/defaults.py). A failed download warns and never fails.
  779. """
  780. import pm
  781. from pm.defaults import default_packages
  782. from pm.install import lazy_installs_allowed, sealed
  783. from pm.lock import Lockfile
  784. from pm.paths import lockfile_path
  785. # Sealed payloads ship their tools; the lazy-install policy (config or the
  786. # Docker/test bridge) means the user asked Hermes not to fetch on its own.
  787. if sealed() or not lazy_installs_allowed():
  788. return
  789. for name in default_packages(Lockfile(lockfile_path()).names()):
  790. if pm.installed_package(name) is not None:
  791. continue
  792. print(f"\n→ Installing {name} (default tool; opt out with `hermes pm install --without {name}`)...")
  793. try:
  794. pm.ensure(name, explicit=True)
  795. except (pm.InstallError, OSError) as exc:
  796. print(f" ⚠ {name} was not installed: {exc}")
  797. print(f" Retry with: hermes pm install {name}")
  798. def _print_checkpoint_footprint_notice() -> None:
  799. """Surface a GB-scale /rollback store the user may not know is on (see the helper's docstring)."""
  800. from tools.checkpoint_maintenance import checkpoint_footprint_notice
  801. notice = checkpoint_footprint_notice()
  802. if notice:
  803. print(f"\n\033[1;33mℹ {notice}\033[0m")
  804. def _print_post_update_notices_and_self_heals() -> None:
  805. """Best-effort notices (FTS optimize, curator) and self-heals (FHS PATH, ACP launcher,
  806. Windows bin launchers, cua-driver refresh) that run after the summary."""
  807. from hermes_cli.update_cmd import _m, _print_curator_first_run_notice, _print_curator_recent_run_notice
  808. from hermes_cli import _launchers
  809. def _migrate_windows_bin_path() -> None:
  810. # Windows launchers into the managed bin dir: in-checkout launchers were swept by the
  811. # autostash (--include-untracked) and updates never run install.ps1. No-op on POSIX.
  812. from hermes_cli._install_repair import migrate_windows_bin_path
  813. migrate_windows_bin_path(_m().PROJECT_ROOT)
  814. for message, step in (
  815. # v23 FTS layout is opt-in (existing indexes untouched); surface the command here.
  816. ('FTS optimize notice failed: %s', _print_fts_optimize_available_notice),
  817. ('Curator first-run notice failed: %s', _print_curator_first_run_notice),
  818. ('Curator recent-run notice failed: %s', _print_curator_recent_run_notice),
  819. ('FHS PATH guard check failed: %s', _ensure_fhs_path_guard),
  820. ('CLI launcher exposure failed: %s', lambda: _launchers.expose_cli(_m().PROJECT_ROOT)),
  821. ('Windows bin launcher migration failed: %s', _migrate_windows_bin_path),
  822. ('cua-driver refresh failed: %s', _refresh_cua_driver_after_update),
  823. ('Default PM tool install failed: %s', _install_default_tools_after_update),
  824. ('Checkpoint footprint notice failed: %s', _print_checkpoint_footprint_notice),
  825. # Legacy HERMES_NEMO_RELAY_ATIF_*/ATOF_* vars produce no traces since the Relay cutover;
  826. # generate each profile's relay-plugins.toml instead of leaving exports silently dead.
  827. ('Relay exporter migration failed: %s', _migrate_relay_exporter_env),
  828. ):
  829. with _best_effort(message):
  830. step()
  831. def _migrate_relay_exporter_env() -> None:
  832. from hermes_cli.relay_plugin_migrate import run_relay_migration_after_update
  833. run_relay_migration_after_update()
  834. def _run_post_update_maintenance(
  835. *, assume_yes, gateway_mode, pre_update_snapshot_id, had_desktop_app_before_update,
  836. pre_update_version, completion_message=None, followups=None,
  837. ) -> bool:
  838. """Post-build housekeeping and completion, returning the SQLite runtime verdict.
  839. Ancillary repairs and notices are best-effort. The profile sync and the config migration run
  840. after a failed build too and never fail the committed update. A config-migration failure is
  841. printed as ``⚠``, recorded as a receipt follow-up and appended to ``followups`` so the tail
  842. stays owed. Profile sync is best-effort per profile (``_sync_profiles_after_update`` prints
  843. that profile's error and carries on); only a sync that escapes the step is owed. An unsafe
  844. runtime withholds success (and is reported) but is not tail work.
  845. """
  846. from hermes_cli.update_receipt import record_followup
  847. def owed_step(name, run):
  848. try:
  849. run()
  850. except (Exception, SystemExit) as exc: # health: allow BLE001 -- the code is committed; retry later
  851. reason = str(exc) or type(exc).__name__
  852. record_followup(name, reason)
  853. if followups is not None:
  854. followups.append((name, reason))
  855. from hermes_cli.update_cmd import _check_and_apply_config_migration, _m
  856. from hermes_cli.update_windows_acl import repair_windows_home_acl
  857. owed_step("windows_home_acl", repair_windows_home_acl)
  858. # macOS TCC: Desktop bundles are re-signed each update, so old grants can go stale
  859. # (toggle ON, yet macOS re-prompts with no Allow button). Tell users how to re-grant.
  860. # With the post-#73681 identifier-pinned DR, new grants survive rebuilds — but a grant made to a pre-fix
  861. # binary stays stale: the System Settings toggle shows ON while macOS re-prompts on every capture, and
  862. # the modern prompt has no Allow button, so users loop. One line of guidance after update tells affected
  863. # users how to complete the one-time re-grant.
  864. if sys.platform == "darwin" and had_desktop_app_before_update:
  865. print()
  866. print(
  867. " ℹ macOS: if Hermes re-prompts for permissions you already "
  868. "granted (toggle shows ON), the stored grant is stale — run "
  869. "`tccutil reset ScreenCapture com.nousresearch.hermes` (repeat "
  870. "per affected service), toggle it ON in System Settings, then "
  871. "fully quit & relaunch once."
  872. )
  873. # macOS TCC interpreter anchor; boot-gated — a failed probe leaves the venv untouched.
  874. try:
  875. # See #95596.
  876. from hermes_cli.macos_tcc_anchor import ensure_tcc_anchor
  877. ensure_tcc_anchor()
  878. except Exception:
  879. logger.debug("macOS TCC anchor refresh skipped", exc_info=True)
  880. # state.db integrity guard for root home AND every profile; restore from own snapshot.
  881. with _best_effort('Post-update state.db integrity check failed: %s'):
  882. _verify_and_restore_state_dbs_post_update()
  883. # Both shallow history and missing tags can hide the release identity.
  884. # Refresh them before the completion line and install stamp read it.
  885. try:
  886. from hermes_cli.gitlock import fetch_full_commit_graph
  887. from hermes_cli.update_cmd import _no_prompt_git_kwargs
  888. if fetch_full_commit_graph(Path(_m().PROJECT_ROOT), **_no_prompt_git_kwargs()):
  889. print(" ✓ Fetched release history (commits only) for version identity")
  890. except (OSError, subprocess.SubprocessError) as exc:
  891. detail = (getattr(exc, "stderr", None) or str(exc)).strip().splitlines()[-1:] or [type(exc).__name__]
  892. print(f" ⚠ Could not refresh release history ({detail[0]}); the version label may be stale or unknown until the next update")
  893. # Keeps every later version lookup off a full history walk; detached, so it never delays this run.
  894. with _best_effort('Commit-graph refresh failed to start: %s'):
  895. from hermes_cli.git_commit_graph import spawn_commit_graph_refresh
  896. spawn_commit_graph_refresh(Path(_m().PROJECT_ROOT))
  897. # Seed the model-catalog cache from the checkout instead of a bot-gated, flaky fetch.
  898. with _best_effort('Model catalog seed during update failed: %s'):
  899. from hermes_cli.model_catalog import seed_cache_from_checkout
  900. if seed_cache_from_checkout(_m().PROJECT_ROOT):
  901. print(" ✓ Model catalog cache refreshed from checkout")
  902. # Drop the cached live plugin catalog under every profile: the checkout is shared, so a
  903. # pre-update snapshot must not out-vote the pins/entries this update just installed for the
  904. # rest of the cache TTL (#119340).
  905. with _best_effort('Live plugin catalog cache invalidation failed: %s'):
  906. _invalidate_live_plugin_catalog_caches()
  907. # New catalog pins are not applied by this update: name them per profile and re-arm each
  908. # profile's plugin check so an opted-in (plugins.auto_apply) gateway applies them next tick.
  909. with _best_effort('Plugin catalog update notice failed: %s'):
  910. _report_catalog_plugin_updates()
  911. with _best_effort('Skills sync during update failed: %s'):
  912. print()
  913. print("→ Syncing bundled skills...")
  914. _print_bundled_skills_sync_report()
  915. owed_step("profile_sync", _sync_profiles_after_update)
  916. owed_step("config_migration", lambda: _check_and_apply_config_migration(
  917. assume_yes=assume_yes, gateway_mode=gateway_mode, pre_update_snapshot_id=pre_update_snapshot_id,
  918. ))
  919. print()
  920. update_complete = _print_verified_update_completion(completion_message or _update_complete_message(pre_update_version))
  921. # A multi-profile host whose gateway came back standalone on a guard says so here too — the
  922. # update summary is the one line operators read (the boot log under s6 is not).
  923. with suppress(Exception):
  924. from hermes_cli.gateway_multiplex_mode import consume_rewritten_notice, recorded_standalone_warning_lines
  925. for line in [*consume_rewritten_notice(), *recorded_standalone_warning_lines()]:
  926. print(line)
  927. with _best_effort('Post-update notices failed: %s'):
  928. _print_post_update_notices_and_self_heals()
  929. # A non-✓ completion message (parked local changes) withholds success on its own; only a
  930. # ✓ message that still came back False is the SQLite verdict.
  931. if not update_complete and (completion_message or "✓").startswith("✓"):
  932. record_followup("sqlite_runtime", "the selected Python links an unsafe SQLite runtime",
  933. retry="run the installer again")
  934. return update_complete