Skip to content

The agent that grows with you

fix(cron): run prompt recovery inside the cron writer lock

Tekniumc4d1ba0e2fab
diff --git a/hermes_cli/backup_cron_prompts.py b/hermes_cli/backup_cron_prompts.py
index c354fa73d42..afd807fe4d4 100644
--- a/hermes_cli/backup_cron_prompts.py
+++ b/hermes_cli/backup_cron_prompts.py
@@ -9,7 +9,7 @@ the count-based net); ``backup`` names are late-imported so its patch seams
import json
import logging
from pathlib import Path
-from typing import Any, Dict, Optional
+from typing import Any, Optional
from hermes_constants import get_hermes_home
@@ -48,7 +48,7 @@ def _cron_jobs_list(doc: Any) -> list[Any]:
return []
-def _prompt_degraded(job: Dict[str, Any]) -> bool:
+def _prompt_degraded(job: dict[str, Any]) -> bool:
"""True when an agent job's prompt field is unusable: blank, missing, or
collapsed to the job's own name (a name is not a prompt)."""
if job.get("no_agent"):
@@ -59,10 +59,60 @@ def _prompt_degraded(job: Dict[str, Any]) -> bool:
return prompt.strip() == str(job.get("name", "")).strip()
+def _snapshot_prompts_by_id(snap_doc: Any) -> dict[str, str]:
+ """Usable (non-blank) snapshot prompts keyed by job id."""
+ prompts: dict[str, str] = {}
+ for job in _cron_jobs_list(snap_doc):
+ if not isinstance(job, dict):
+ continue
+ prompt = job.get("prompt")
+ if isinstance(prompt, str) and prompt.strip():
+ prompts[str(job.get("id", ""))] = prompt
+ return prompts
+
+
+def _restore_degraded_prompts(
+ jobs: list[Any], snap_prompts: dict[str, str], *, apply: bool
+) -> list[str]:
+ """Ids of jobs whose prompt is degraded AND the snapshot has a usable prompt for;
+ with ``apply`` the prompt field (only) is restored in place."""
+ restored: list[str] = []
+ for job in jobs:
+ if not isinstance(job, dict) or not _prompt_degraded(job):
+ continue
+ job_id = str(job.get("id", ""))
+ snap_prompt = snap_prompts.get(job_id)
+ if snap_prompt is None:
+ continue
+ if apply:
+ job["prompt"] = snap_prompt
+ restored.append(job_id)
+ return restored
+
+
+def _restore_under_cron_lock(home: Path, snap_prompts: dict[str, str]) -> list[str]:
+ """Re-read ``home``'s jobs under the canonical cron writer lock, restore the prompts that
+ are STILL degraded, and publish through the locked, merge-aware cron save path.
+
+ Publishing a document read before the lock would erase any cron edit committed in between
+ (create, remove, schedule/prompt edit). ``use_cron_store`` pins both the lock file and the
+ store to ``home`` so a sibling profile's recovery serializes with THAT profile's writers.
+ Raises ``RuntimeError`` on an unreadable store and ``OSError`` on a failed write.
+ """
+ from cron import jobs as cron_jobs
+
+ with cron_jobs.use_cron_store(home), cron_jobs._jobs_lock():
+ jobs = cron_jobs.load_jobs()
+ restored_ids = _restore_degraded_prompts(jobs, snap_prompts, apply=True)
+ if restored_ids:
+ cron_jobs.save_jobs(jobs)
+ return restored_ids
+
+
def restore_cron_prompt_fields_if_degraded(
snapshot_id: str,
hermes_home: Optional[Path] = None,
-) -> Optional[Dict[str, Any]]:
+) -> Optional[dict[str, Any]]:
"""Safety net for field-level cron-job degradation across ``hermes update``.
A writer active during the update's mutation window replaced every
@@ -99,56 +149,35 @@ def restore_cron_prompt_fields_if_degraded(
if not snapshot_id:
return None
- from hermes_cli.backup import _CRON_JOBS_REL, _atomic_output_path, _quick_snapshot_root
+ from hermes_cli.backup import _CRON_JOBS_REL, _quick_snapshot_root
home = hermes_home or get_hermes_home()
live_path = home / _CRON_JOBS_REL
snap_path = _quick_snapshot_root(home) / snapshot_id / _CRON_JOBS_REL
- live_doc = _load_cron_jobs_doc(live_path)
- if live_doc is None:
- return None
snap_doc = _load_cron_jobs_doc(snap_path)
if snap_doc is None:
return None
-
- snap_by_id: Dict[str, Dict[str, Any]] = {}
- for job in _cron_jobs_list(snap_doc):
- if isinstance(job, dict):
- snap_by_id[str(job.get("id", ""))] = job
-
- restored_ids: list[str] = []
- live_jobs = _cron_jobs_list(live_doc)
- for job in live_jobs:
- if not isinstance(job, dict):
- continue
- snap_job = snap_by_id.get(str(job.get("id", "")))
- if snap_job is None:
- continue
- if not _prompt_degraded(job):
- continue
- snap_prompt = snap_job.get("prompt")
- if not isinstance(snap_prompt, str) or not snap_prompt.strip():
- continue
- job["prompt"] = snap_prompt
- restored_ids.append(str(job.get("id", "")))
-
- if not restored_ids:
+ snap_prompts = _snapshot_prompts_by_id(snap_doc)
+ # Unlocked pre-check keeps the healthy path free of lock/dir side effects. It only gates:
+ # the patch itself is decided on a fresh read under the cron writer lock below.
+ live_doc = _load_cron_jobs_doc(live_path)
+ if live_doc is None or not _restore_degraded_prompts(
+ _cron_jobs_list(live_doc), snap_prompts, apply=False
+ ):
return None
try:
- live_path.parent.mkdir(parents=True, exist_ok=True)
- with _atomic_output_path(live_path) as tmp_path:
- with open(tmp_path, "w", encoding="utf-8") as f:
- json.dump(live_doc, f, indent=2)
- f.write("\n")
- except (OSError, PermissionError) as exc:
+ restored_ids = _restore_under_cron_lock(home, snap_prompts)
+ except (OSError, RuntimeError) as exc:
logger.error(
"Cron job prompts were degraded during update but auto-restore "
"failed: %s",
exc,
)
return None
+ if not restored_ids:
+ return None
logger.warning(
"Restored %d cron job prompt(s) from pre-update snapshot %s — job(s) "
@@ -166,9 +195,9 @@ def restore_cron_prompt_fields_if_degraded(
def restore_cron_prompt_fields_all_profiles(
- profile_snapshots: Dict[str, str],
+ profile_snapshots: dict[str, str],
invoking_home: Optional[Path] = None,
-) -> list[Dict[str, Any]]:
+) -> list[dict[str, Any]]:
"""Run the cron prompt-field safety net for every sibling profile.
Same contract as :func:`restore_cron_jobs_all_profiles`: each profile's
@@ -176,7 +205,7 @@ def restore_cron_prompt_fields_all_profiles(
pre-update snapshot. Returns one result dict per restored profile, each
with a ``profile`` key added. Never raises.
"""
- restored: list[Dict[str, Any]] = []
+ restored: list[dict[str, Any]] = []
if not profile_snapshots:
return restored
from hermes_cli.backup import _sibling_profile_homes
diff --git a/tests/hermes_cli/test_backup_cron_prompts_lock.py b/tests/hermes_cli/test_backup_cron_prompts_lock.py
new file mode 100644
index 00000000000..a9a376f36c9
--- /dev/null
+++ b/tests/hermes_cli/test_backup_cron_prompts_lock.py
@@ -0,0 +1,61 @@
+"""Cron prompt recovery (#82990) must serialize with cron writers: it re-reads jobs.json under
+the canonical cron lock and publishes via the locked, merge-aware save path, so cron edits
+committed while recovery runs are never erased by a stale whole-document write."""
+
+import json
+from pathlib import Path
+
+
+def _seed_jobs(path: Path, jobs):
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(json.dumps({"jobs": jobs}))
+
+
+def test_concurrent_cron_edits_survive_recovery(tmp_path, monkeypatch):
+ """Recovery must publish under the cron writer lock from a fresh read: cron edits
+ committed after recovery first read jobs.json (create, remove, schedule edit, a
+ legitimate prompt edit) all survive; only the still-degraded prompt is restored."""
+ import cron.jobs as cron_jobs
+ from hermes_cli.backup import create_quick_snapshot
+ import hermes_cli.backup_cron_prompts as bcp
+ hermes_home = tmp_path / ".hermes"
+ jobs_path = hermes_home / "cron" / "jobs.json"
+ sched = {"kind": "interval", "minutes": 60, "display": "every 60m"}
+
+ def job(job_id, prompt):
+ return {"id": job_id, "name": job_id.upper(), "prompt": prompt,
+ "schedule": sched, "enabled": True}
+
+ _seed_jobs(jobs_path, [job("a", "A real prompt."), job("b", "B real prompt."),
+ job("c", "C real prompt.")])
+ snap_id = create_quick_snapshot(label="pre-update", hermes_home=hermes_home, keep=5)
+ assert snap_id
+ _seed_jobs(jobs_path, [job("a", "A"), job("b", "B"), job("c", "C real prompt.")])
+
+ real_load = bcp._load_cron_jobs_doc
+ fired = []
+
+ def load_then_race(path):
+ doc = real_load(path)
+ if Path(path) == jobs_path and not fired:
+ fired.append(True)
+ with cron_jobs.use_cron_store(hermes_home):
+ new_id = cron_jobs.create_job(prompt="New prompt.", schedule="every 1h",
+ name="new")["id"]
+ fired.append(new_id)
+ assert cron_jobs.remove_job("c")
+ cron_jobs.update_job("a", {"schedule": cron_jobs.parse_schedule("every 2h")})
+ cron_jobs.update_job("b", {"prompt": "User rewrote b."})
+ return doc
+
+ monkeypatch.setattr(bcp, "_load_cron_jobs_doc", load_then_race)
+ result = bcp.restore_cron_prompt_fields_if_degraded(snap_id, hermes_home=hermes_home)
+
+ assert len(fired) == 2
+ assert result is not None and result["job_ids"] == ["a"]
+ final = {j["id"]: j for j in json.loads(jobs_path.read_text())["jobs"]}
+ assert set(final) == {"a", "b", fired[1]}
+ assert final["a"]["prompt"] == "A real prompt."
+ assert final["a"]["schedule"]["minutes"] == 120
+ assert final["b"]["prompt"] == "User rewrote b."
+ assert final[fired[1]]["prompt"] == "New prompt."