backup_cron_prompts.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. """Post-update cron safety net for field-level loss: restore agent-job prompts an update
  2. collapsed to the job name while the job count stayed the same (issue #82990).
  3. Split out of ``hermes_cli.backup`` (which owns the quick snapshots this compares against and
  4. the count-based net); ``backup`` names are late-imported so its patch seams
  5. (``_sibling_profile_homes``) hold.
  6. """
  7. import json
  8. import logging
  9. from pathlib import Path
  10. from typing import Any, Optional
  11. from hermes_constants import get_hermes_home
  12. # Log-record parity with the origin module.
  13. logger = logging.getLogger("hermes_cli.backup")
  14. def _load_cron_jobs_doc(path: Path) -> Optional[Any]:
  15. """Parse ``path`` as the canonical ``{"jobs": [...]}`` doc (legacy bare list honoured).
  16. ``None`` = missing/unreadable/non-dict-with-list — same dialect rules as
  17. :func:`_count_cron_jobs` (utf-8-sig for Windows BOMs). Never raises.
  18. """
  19. if not path.is_file():
  20. return None
  21. try:
  22. with open(path, "r", encoding="utf-8-sig") as f:
  23. data = json.load(f)
  24. except (OSError, json.JSONDecodeError):
  25. return None
  26. if isinstance(data, dict):
  27. jobs = data.get("jobs", [])
  28. return data if isinstance(jobs, list) else None
  29. if isinstance(data, list):
  30. return data
  31. return None
  32. def _cron_jobs_list(doc: Any) -> list[Any]:
  33. """The job list out of either document shape. Empty when malformed."""
  34. if isinstance(doc, list):
  35. return doc
  36. if isinstance(doc, dict):
  37. jobs = doc.get("jobs", [])
  38. return jobs if isinstance(jobs, list) else []
  39. return []
  40. def _prompt_degraded(job: dict[str, Any]) -> bool:
  41. """True when an agent job's prompt field is unusable: blank, missing, or
  42. collapsed to the job's own name (a name is not a prompt)."""
  43. if job.get("no_agent"):
  44. return False
  45. prompt = job.get("prompt")
  46. if not isinstance(prompt, str) or not prompt.strip():
  47. return True
  48. return prompt.strip() == str(job.get("name", "")).strip()
  49. def _snapshot_prompts_by_id(snap_doc: Any) -> dict[str, str]:
  50. """Usable (non-blank) snapshot prompts keyed by job id."""
  51. prompts: dict[str, str] = {}
  52. for job in _cron_jobs_list(snap_doc):
  53. if not isinstance(job, dict):
  54. continue
  55. prompt = job.get("prompt")
  56. if isinstance(prompt, str) and prompt.strip():
  57. prompts[str(job.get("id", ""))] = prompt
  58. return prompts
  59. def _restore_degraded_prompts(
  60. jobs: list[Any], snap_prompts: dict[str, str], *, apply: bool
  61. ) -> list[str]:
  62. """Ids of jobs whose prompt is degraded AND the snapshot has a usable prompt for;
  63. with ``apply`` the prompt field (only) is restored in place."""
  64. restored: list[str] = []
  65. for job in jobs:
  66. if not isinstance(job, dict) or not _prompt_degraded(job):
  67. continue
  68. job_id = str(job.get("id", ""))
  69. snap_prompt = snap_prompts.get(job_id)
  70. if snap_prompt is None:
  71. continue
  72. if apply:
  73. job["prompt"] = snap_prompt
  74. restored.append(job_id)
  75. return restored
  76. def _restore_under_cron_lock(home: Path, snap_prompts: dict[str, str]) -> list[str]:
  77. """Re-read ``home``'s jobs under the canonical cron writer lock, restore the prompts that
  78. are STILL degraded, and publish through the locked, merge-aware cron save path.
  79. Publishing a document read before the lock would erase any cron edit committed in between
  80. (create, remove, schedule/prompt edit). ``use_cron_store`` pins both the lock file and the
  81. store to ``home`` so a sibling profile's recovery serializes with THAT profile's writers.
  82. Raises ``RuntimeError`` on an unreadable store and ``OSError`` on a failed write.
  83. """
  84. from cron import jobs as cron_jobs
  85. with cron_jobs.use_cron_store(home), cron_jobs._jobs_lock():
  86. jobs = cron_jobs.load_jobs()
  87. restored_ids = _restore_degraded_prompts(jobs, snap_prompts, apply=True)
  88. if restored_ids:
  89. cron_jobs.save_jobs(jobs)
  90. return restored_ids
  91. def restore_cron_prompt_fields_if_degraded(
  92. snapshot_id: str,
  93. hermes_home: Optional[Path] = None,
  94. ) -> Optional[dict[str, Any]]:
  95. """Safety net for field-level cron-job degradation across ``hermes update``.
  96. A writer active during the update's mutation window replaced every
  97. agent-job ``prompt`` with the job's own ``name`` while the job COUNT
  98. stayed identical, so the count-based net
  99. (:func:`restore_cron_jobs_if_emptied`) passed the loss undetected
  100. (issue #82990): 6 jobs before, 6 jobs after, every one of them with an
  101. empty prompt wearing its name.
  102. Mirrors the field-level pattern of
  103. :func:`restore_config_model_settings_if_rewritten`: compare the live
  104. file against the pre-update snapshot taken minutes earlier by this same
  105. update run, and restore ONLY the ``prompt`` field of a live agent job
  106. whose id matches a snapshot job — never the whole record, never jobs the
  107. snapshot does not know. Conservative on purpose:
  108. - a live prompt is only restored when it is blank/missing or exactly
  109. equal to the job's own name — a legitimate user edit that merely
  110. differs from the snapshot is never stomped;
  111. - ``no_agent`` script jobs are never touched (they have no prompt);
  112. - a blank snapshot prompt restores nothing (there is nothing better to
  113. put back).
  114. Args:
  115. snapshot_id: The pre-update quick-snapshot id (from
  116. :func:`create_quick_snapshot`).
  117. hermes_home: Override for the Hermes home directory (tests/siblings).
  118. Returns:
  119. ``None`` when no action was taken (the common, healthy path). On a
  120. successful restore, ``{"restored": True, "prompts": N,
  121. "snapshot_id": ...}`` so the caller can warn the user.
  122. """
  123. if not snapshot_id:
  124. return None
  125. from hermes_cli.backup import _CRON_JOBS_REL, _quick_snapshot_root
  126. home = hermes_home or get_hermes_home()
  127. live_path = home / _CRON_JOBS_REL
  128. snap_path = _quick_snapshot_root(home) / snapshot_id / _CRON_JOBS_REL
  129. snap_doc = _load_cron_jobs_doc(snap_path)
  130. if snap_doc is None:
  131. return None
  132. snap_prompts = _snapshot_prompts_by_id(snap_doc)
  133. # Unlocked pre-check keeps the healthy path free of lock/dir side effects. It only gates:
  134. # the patch itself is decided on a fresh read under the cron writer lock below.
  135. live_doc = _load_cron_jobs_doc(live_path)
  136. if live_doc is None or not _restore_degraded_prompts(
  137. _cron_jobs_list(live_doc), snap_prompts, apply=False
  138. ):
  139. return None
  140. try:
  141. restored_ids = _restore_under_cron_lock(home, snap_prompts)
  142. except (OSError, RuntimeError) as exc:
  143. logger.error(
  144. "Cron job prompts were degraded during update but auto-restore "
  145. "failed: %s",
  146. exc,
  147. )
  148. return None
  149. if not restored_ids:
  150. return None
  151. logger.warning(
  152. "Restored %d cron job prompt(s) from pre-update snapshot %s — job(s) "
  153. "%s had their prompt replaced by the job name (#82990)",
  154. len(restored_ids),
  155. snapshot_id,
  156. ", ".join(restored_ids),
  157. )
  158. return {
  159. "restored": True,
  160. "prompts": len(restored_ids),
  161. "job_ids": restored_ids,
  162. "snapshot_id": snapshot_id,
  163. }
  164. def restore_cron_prompt_fields_all_profiles(
  165. profile_snapshots: dict[str, str],
  166. invoking_home: Optional[Path] = None,
  167. ) -> list[dict[str, Any]]:
  168. """Run the cron prompt-field safety net for every sibling profile.
  169. Same contract as :func:`restore_cron_jobs_all_profiles`: each profile's
  170. live ``cron/jobs.json`` is compared against ITS OWN same-generation
  171. pre-update snapshot. Returns one result dict per restored profile, each
  172. with a ``profile`` key added. Never raises.
  173. """
  174. restored: list[dict[str, Any]] = []
  175. if not profile_snapshots:
  176. return restored
  177. from hermes_cli.backup import _sibling_profile_homes
  178. home = invoking_home or get_hermes_home()
  179. by_name = dict(_sibling_profile_homes(home))
  180. for name, snap_id in profile_snapshots.items():
  181. profile_home = by_name.get(name)
  182. if profile_home is None:
  183. continue
  184. try:
  185. result = restore_cron_prompt_fields_if_degraded(
  186. snap_id, hermes_home=profile_home
  187. )
  188. except Exception as exc:
  189. logger.debug(
  190. "Cron prompt-field restore check for profile %s failed: %s",
  191. name,
  192. exc,
  193. )
  194. continue
  195. if result:
  196. result["profile"] = name
  197. restored.append(result)
  198. return restored