update_cmd_config.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. """Post-``hermes update`` config-schema migration for the active profile and every sibling.
  2. Names are re-imported by ``update_cmd`` (``hermes_cli.update_cmd.<name>`` resolves/monkeypatches);
  3. origin helpers are imported lazily."""
  4. import logging
  5. import sys
  6. from pathlib import Path
  7. from hermes_cli.update_cmd_common import _best_effort
  8. # Log-record parity with the origin module.
  9. logger = logging.getLogger("hermes_cli.update_cmd")
  10. def _reload_config_modules() -> None:
  11. """Historical updater hook; migration now belongs to fresh completion Python."""
  12. from hermes_cli._old_updater import stop_for_relaunch
  13. stop_for_relaunch(incomplete=True)
  14. def _run_config_check_fresh() -> tuple:
  15. from hermes_cli._old_updater import stop_for_relaunch
  16. stop_for_relaunch(incomplete=True)
  17. def _run_migrate_config_fresh(*, interactive: bool = False, quiet: bool = False) -> dict:
  18. from hermes_cli._old_updater import stop_for_relaunch
  19. stop_for_relaunch(incomplete=True)
  20. def _migrate_sibling_profile_configs(notes: dict[str, list[str]] | None = None) -> list[tuple[str, int, int]]:
  21. """Migrate every SIBLING profile's config.yaml (the shared checkout serves all profiles). Per
  22. sibling (active skipped): scope via the context-local HERMES_HOME override (never ``os.environ``)
  23. and run the NON-INTERACTIVE quiet migration — prompt-requiring settings wait for that profile's
  24. own session. Returns ``[(name, from_version, to_version), ...]``; never raises. *notes* receives
  25. each migrated sibling's display lines for what quiet mode muted (a reset/removed setting, a
  26. warning), as the active profile's quiet path re-prints them; the tuple shape stays for old updaters.
  27. 91277 Phase 2 (fleet-wide config migration; #20438/#54926/#79048): the shared checkout serves every
  28. profile, but ``hermes update`` historically migrated only the active profile's config — siblings drifted
  29. versions until their gateway hit a config the new code couldn't read.
  30. Enumeration matches pre-update snapshots (#66140): default lives at
  31. ``_get_default_hermes_home()``, not under ``profiles/``, so a named-profile
  32. ``hermes update`` still migrates it.
  33. """
  34. from hermes_cli.config import check_config_version, get_missing_config_fields, migrate_config
  35. migrated: list[tuple[str, int, int]] = []
  36. with _best_effort('Sibling profile enumeration failed: %s'):
  37. from hermes_constants import (
  38. get_process_hermes_home, reset_hermes_home_override, set_hermes_home_override)
  39. from hermes_cli.backup import _sibling_profile_homes
  40. active_home = Path(get_process_hermes_home())
  41. for name, profile_home in _sibling_profile_homes(active_home):
  42. if not (profile_home / "config.yaml").is_file(): # config-reader: ok — profile directory discovery marker
  43. continue # profile never configured — nothing to migrate
  44. token = set_hermes_home_override(profile_home)
  45. try:
  46. current_ver, latest_ver = check_config_version(raise_on_parse_error=True)
  47. if current_ver >= latest_ver:
  48. continue
  49. results = migrate_config(interactive=False, quiet=True)
  50. after_ver, _ = check_config_version(raise_on_parse_error=True)
  51. if after_ver > current_ver:
  52. migrated.append((name, current_ver, after_ver))
  53. if notes is not None:
  54. # config_added also lists the new default keys; those are not changes.
  55. new_keys = {field["key"] for field in get_missing_config_fields()}
  56. notes[name] = [f"ℹ {note}" for note in results["config_added"] if note not in new_keys]
  57. notes[name] += [f"⚠️ {warning}" for warning in results["warnings"]]
  58. except Exception as exc:
  59. logger.debug("Config migration for profile %s failed: %s", name, exc)
  60. finally:
  61. reset_hermes_home_override(token)
  62. return migrated
  63. def _restore_snapshot_safety_nets(pre_update_snapshot_id) -> None:
  64. """Post-migration safety nets (never break an otherwise-good update): restore cron jobs
  65. emptied by migrations/the desktop scheduler and protected model settings (model.provider /
  66. model.default / moa:) rewritten by Desktop repair cycles — for the active profile (from
  67. *pre_update_snapshot_id*) and every sibling profile (against ITS OWN snapshot)."""
  68. def _cron_line(r):
  69. return (
  70. f"cron/jobs.json lost jobs during this update — restored {r['job_count']} job(s) "
  71. f"from pre-update snapshot {r['snapshot_id']}.")
  72. def _prompt_line(r):
  73. return (
  74. f"cron/jobs.json had agent-job prompt(s) replaced by the job name during this "
  75. f"update — restored {r['prompts']} prompt(s) from pre-update snapshot "
  76. f"{r['snapshot_id']}.")
  77. def _cfg_line(r):
  78. return (
  79. f"config.yaml user model settings were rewritten during this update — restored "
  80. f"{', '.join(r['keys'])} from pre-update snapshot {r['snapshot_id']}.")
  81. with _best_effort("Cron jobs auto-restore check failed: %s"):
  82. # Safety net: config-version migrations have been observed to leave cron/jobs.json valid-but-empty,
  83. # silently dropping every scheduled job (issue #34600). The desktop scheduler can also overwrite
  84. # with its own small set, causing partial loss (issue #52144). If the live file now has fewer jobs
  85. # than the pre-update snapshot, restore it and warn loudly.
  86. from hermes_cli.backup import restore_cron_jobs_if_emptied
  87. cron_restore = restore_cron_jobs_if_emptied(pre_update_snapshot_id)
  88. if cron_restore:
  89. print()
  90. print(f" ⚠️ {_cron_line(cron_restore)}")
  91. with _best_effort("Cron prompt-field auto-restore check failed: %s"):
  92. # Safety net: a writer in the update's mutation window replaced agent-job prompts
  93. # with the job NAME while the count stayed identical, so the count-based net above
  94. # passed it undetected (issue #82990). Restore only the degraded prompt fields.
  95. from hermes_cli.backup_cron_prompts import restore_cron_prompt_fields_if_degraded
  96. prompt_restore = restore_cron_prompt_fields_if_degraded(pre_update_snapshot_id)
  97. if prompt_restore:
  98. print()
  99. print(f" ⚠️ {_prompt_line(prompt_restore)}")
  100. with _best_effort("Config model-settings auto-restore check failed: %s"):
  101. from hermes_cli.backup import restore_config_model_settings_if_rewritten
  102. cfg_restore = restore_config_model_settings_if_rewritten(pre_update_snapshot_id)
  103. if cfg_restore:
  104. print()
  105. print(f" ⚠️ {_cfg_line(cfg_restore)}")
  106. with _best_effort('Sibling cron auto-restore check failed: %s'):
  107. from hermes_cli.backup import restore_cron_jobs_all_profiles
  108. for _restored in restore_cron_jobs_all_profiles(_LAST_SIBLING_SNAPSHOTS):
  109. print()
  110. print(f" ⚠️ Profile '{_restored['profile']}': {_cron_line(_restored)}")
  111. with _best_effort('Sibling cron prompt-field auto-restore check failed: %s'):
  112. from hermes_cli.backup_cron_prompts import restore_cron_prompt_fields_all_profiles
  113. for _prompt_restored in restore_cron_prompt_fields_all_profiles(_LAST_SIBLING_SNAPSHOTS):
  114. print()
  115. print(f" ⚠️ Profile '{_prompt_restored['profile']}': {_prompt_line(_prompt_restored)}")
  116. with _best_effort('Sibling config auto-restore check failed: %s'):
  117. from hermes_cli.backup import restore_config_model_settings_all_profiles
  118. for _cfg_restored in restore_config_model_settings_all_profiles(_LAST_SIBLING_SNAPSHOTS):
  119. print()
  120. print(f" ⚠️ Profile '{_cfg_restored['profile']}': {_cfg_line(_cfg_restored)}")
  121. def _ask_configure_new_options(*, assume_yes: bool, gateway_mode: bool) -> str:
  122. """The yes/no answer for "configure new options now?": "y" under --yes, the messenger's reply in
  123. gateway mode, "auto" (safe migrations only) when non-interactive, else ``input()``."""
  124. from hermes_cli.update_cmd import _gateway_prompt
  125. if assume_yes:
  126. print(" ℹ --yes: auto-applying config migration (skipping API-key prompts).")
  127. return "y"
  128. if gateway_mode:
  129. return _gateway_prompt("Would you like to configure new options now? [Y/n]", "n").strip().lower()
  130. if not (sys.stdin.isatty() and sys.stdout.isatty()):
  131. print(" ℹ Non-interactive session — applying safe config migrations.")
  132. return "auto"
  133. try:
  134. return input("Would you like to configure them now? [Y/n]: ").strip().lower()
  135. except EOFError:
  136. return "n"
  137. except UnicodeDecodeError:
  138. # Non-UTF-8 locales / embedded terminals can make input() raise this.
  139. print(
  140. " ⚠ Could not read input (encoding issue). Skipping. "
  141. "Run 'hermes config migrate' manually to configure.")
  142. return "n"
  143. def _check_and_apply_config_migration(
  144. *, assume_yes: bool = False, gateway_mode: bool = False, pre_update_snapshot_id: str | None = None
  145. ) -> None:
  146. """Check/apply config migrations. Runs on EVERY completion path
  147. (post-pull, venv-repair, Node-deps repair on ``commit_count == 0``) so an interrupted update
  148. that already pulled code doesn't strand an old config version.
  149. See #91360.
  150. """
  151. from hermes_cli.update_cmd import _migrate_sibling_profile_configs
  152. from hermes_cli.config import check_config_version, migrate_config
  153. print()
  154. print("→ Checking configuration for new options...")
  155. from hermes_cli.config import get_missing_env_vars, get_missing_config_fields
  156. # A config-check failure must not break an otherwise-successful update.
  157. try:
  158. from hermes_cli.config import get_missing_env_vars, get_missing_config_fields
  159. # Log, point at the manual command, and return. See #91360.
  160. missing_env = get_missing_env_vars(required_only=True)
  161. missing_config = get_missing_config_fields()
  162. current_ver, latest_ver = check_config_version(raise_on_parse_error=True)
  163. except Exception as exc:
  164. logger.debug("Config check during update failed: %s", exc)
  165. print(" ⚠️ Could not check config version.")
  166. print(" Run 'hermes config migrate' to check manually.")
  167. return
  168. owed_error = None
  169. has_new_options = bool(missing_env or missing_config)
  170. version_bump_only = not has_new_options and current_ver < latest_ver
  171. needs_migration = has_new_options or current_ver < latest_ver
  172. if version_bump_only:
  173. # Only the format version changed (defaults merge transparently); prompting
  174. # would look like a no-op on yes — apply silently and say what happened.
  175. print()
  176. print(f" ℹ Updating config format (v{current_ver} → v{latest_ver})…")
  177. try:
  178. _mig_results = migrate_config(interactive=False, quiet=True)
  179. print(" ✓ Config format updated (no new settings to configure)")
  180. # quiet=True also mutes steps that RESET/REMOVE a setting; re-surface them so an
  181. # unattended update never silently changes config (config_added holds only mutations here).
  182. # In this branch missing_config is empty, so config_added can only contain migration-step
  183. # mutations, not missing-key listings. See #81946, #86656.
  184. for _note in _mig_results.get("config_added") or []:
  185. print(f" ℹ {_note}")
  186. for _warn in _mig_results.get("warnings") or []:
  187. print(f" ⚠️ {_warn}")
  188. except Exception as _mig_err:
  189. print(f" ⚠️ Config format update failed: {_mig_err}")
  190. print(" Run 'hermes config migrate' to retry.")
  191. # Not a silent success: raised after the sibling migration and snapshot safety nets
  192. # below (independent of the active write, and the snapshot id is only in hand now), so
  193. # the caller's owed-step guard records ``config_migration`` and keeps the tail pending.
  194. owed_error = RuntimeError(f"config format v{current_ver} → v{latest_ver} was not written: {_mig_err}")
  195. owed_error.__cause__ = _mig_err
  196. elif needs_migration:
  197. print()
  198. # Show WHAT changed, not just a count, for an informed yes/no.
  199. if missing_env:
  200. print(f" ⚠️ {len(missing_env)} new required setting(s) need configuration")
  201. _print_items(missing_env, "New settings", "name")
  202. if missing_config:
  203. print(f" ℹ️ {len(missing_config)} new config option(s) available")
  204. _print_items(missing_config, "New options", "key")
  205. print()
  206. response = _ask_configure_new_options(assume_yes=assume_yes, gateway_mode=gateway_mode)
  207. if response in {"", "y", "yes", "auto"}:
  208. print()
  209. # Gateway/--yes/non-interactive can't prompt for API keys; still run the
  210. # non-interactive pass so defaults and version bumps land before the gateway restarts.
  211. unattended = gateway_mode or assume_yes or response == "auto"
  212. results = migrate_config(interactive=not unattended, quiet=False)
  213. if results["env_added"] or results["config_added"]:
  214. print()
  215. print("✓ Configuration updated!")
  216. if unattended and missing_env:
  217. print(" ℹ API keys require manual entry: hermes config migrate")
  218. else:
  219. print()
  220. print("Skipped. Run 'hermes config migrate' later to configure.")
  221. else:
  222. print(" ✓ Configuration is up to date")
  223. # The migration above touched only the active profile; run the same NON-INTERACTIVE
  224. # migration per sibling home via the context-local HERMES_HOME override (never os.environ).
  225. with _best_effort('Sibling config migration failed: %s'):
  226. sibling_notes: dict[str, list[str]] = {}
  227. for _name, _from_ver, _to_ver in _migrate_sibling_profile_configs(sibling_notes):
  228. print(f" ✓ Profile '{_name}': config format updated (v{_from_ver} → v{_to_ver})")
  229. for _note in sibling_notes.get(_name, ()):
  230. print(f" {_note}")
  231. _restore_snapshot_safety_nets(pre_update_snapshot_id)
  232. if owed_error is not None:
  233. raise owed_error
  234. # {profile: snapshot_id} from this run's pre-update backup, consumed by the per-profile
  235. # safety nets. Module-level because snapshot and restore run far apart in _cmd_update_impl.
  236. _LAST_SIBLING_SNAPSHOTS: dict = {}
  237. def _print_items(items, label, key, fallback_key=None):
  238. if not items:
  239. return
  240. print(f" {label}:")
  241. shown = items[:8]
  242. for it in shown:
  243. # Defensive: some callers/mocks pass bare name strings.
  244. if isinstance(it, dict):
  245. name = it.get(key) or (fallback_key and it.get(fallback_key)) or "?"
  246. desc = (it.get("description") or "").strip()
  247. else:
  248. name, desc = str(it), ""
  249. print(f" • {name} — {desc}" if desc else f" • {name}")
  250. if len(items) > len(shown):
  251. print(f" … and {len(items) - len(shown)} more")