Skip to content

The agent that grows with you

fix(cron): canonical completion ids retire once nothing can replay them

Teknium0f2fe9ff5a4a
diff --git a/cron/jobs.py b/cron/jobs.py
index 1ef988ece24..7fed57ffba8 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -2469,8 +2469,9 @@ def mark_job_run(
return False
now = _hermes_now().isoformat()
_record_run_outcome(job, success, error, delivery_error, status, now)
- if execution_id is not None:
- job.setdefault("canonical_completions", []).append(execution_id)
+ if execution_id is not None: # retire ids recovery can no longer replay; latest stays last
+ from cron.scheduler_authority import replayable_completions as live
+ job["canonical_completions"] = [*live(job_id, job.get("canonical_completions")), execution_id]
_advance_after_run(job, now, ladder_rung=ladder_rung)
from cron import quota_hold
from cron.unreachable_retry import clear_state, plan_retry
diff --git a/cron/scheduler_authority.py b/cron/scheduler_authority.py
index 8628fae8a15..eea00d6598a 100644
--- a/cron/scheduler_authority.py
+++ b/cron/scheduler_authority.py
@@ -20,6 +20,12 @@ def _pause_identity(paused):
return {'paused_at': paused.get('paused_at'), 'reason': paused.get('paused_reason')} if paused else True
+def replayable_completions(job_id, completions):
+ """The recorded completion ids whose admission journal still exists: only journal recovery
+ can replay a completion into ``mark_job_run``, so an id without one is a dead tombstone."""
+ return [eid for eid in completions or () if journal_path(job_id, eid).exists()]
+
+
def pause_for_unknown(job_id, execution_id, reason):
"""The firer pauses the job for this fire's unverified admission and records that exact pause
on the fire's journal, so the receipt that later proves the run terminal can lift it."""
diff --git a/tests/cron/test_canonical_completion_retention.py b/tests/cron/test_canonical_completion_retention.py
new file mode 100644
index 00000000000..c22bb9ef9bf
--- /dev/null
+++ b/tests/cron/test_canonical_completion_retention.py
@@ -0,0 +1,26 @@
+"""A recurring canonical cron job's completion tombstones stay bounded: an id is retired once its
+admission journal is gone (nothing can replay it), never while recovery could still replay it."""
+from cron import jobs, scheduler_authority
+
+
+def _fire(job_id, eid, *, retire_journal=True):
+ journal = scheduler_authority.journal_path(job_id, eid)
+ journal.parent.mkdir(parents=True, exist_ok=True)
+ journal.write_text('{}', encoding='utf-8') # run_canonical_job writes it before admission
+ assert jobs.mark_job_run(job_id, True, execution_id=eid) is True
+ if retire_journal:
+ journal.unlink() # finish_completed_run's tail
+
+
+def test_completion_ids_retire_with_their_journal(tmp_path, monkeypatch):
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path / 'home'))
+ with jobs.use_cron_store(tmp_path / 'cron'):
+ job_id = jobs.create_job(prompt='report', schedule='every 1m', deliver='local')['id']
+ _fire(job_id, 'pending', retire_journal=False) # a tail that crashed before unlinking
+ for n in range(64):
+ _fire(job_id, f'run-{n}')
+ assert jobs.get_job(job_id)['canonical_completions'] == ['pending', 'run-63']
+ completed = jobs.get_job(job_id)['repeat']['completed']
+ # The still-journaled completion replayed by recovery is a no-op, not a second run.
+ assert jobs.mark_job_run(job_id, True, execution_id='pending') is True
+ assert jobs.get_job(job_id)['repeat']['completed'] == completed