Skip to content

The agent that grows with you

fix(cron): keep recovery journals until live firers can settle

David Dudok de Witfc4bd97dd6a7
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 1823442fcfd..ca345954db5 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -3237,10 +3237,16 @@ def _finish_completed_run(d: _RunDelivery, fire_owner: Optional[str], execution_
# Failure ping left the process (or had a configured target): mark the incident alerted.
_mark_incident_alerted(d.failure_incident_id)
from functools import partial
- from cron.executions import recover_receipted_execution
+ from cron.executions import get_execution, recover_receipted_execution
finish = partial(recover_receipted_execution, job_id=job['id']) if recovered else finish_execution
- finish(
+ finished = finish(
execution_id, success=d.success, error=d.error, delivery_outcome=delivery_outcome)
+ if recovered and finished is None:
+ current = get_execution(execution_id)
+ # A live foreign firer still owns this row. Keep its only recovery link until settlement.
+ # Legacy direct calls may have no execution row; an already-finished row is also safe.
+ if current is not None and current['status'] not in {'completed', 'failed'}:
+ return False
if job.get("last_delivery_queued"):
# A drain that settled before this run's own bookkeeping landed found nothing to fence on.
settle_quietly(job["id"], execution_id)
diff --git a/tests/cron/test_live_firer_receipt_recovery.py b/tests/cron/test_live_firer_receipt_recovery.py
new file mode 100644
index 00000000000..0409726fda1
--- /dev/null
+++ b/tests/cron/test_live_firer_receipt_recovery.py
@@ -0,0 +1,51 @@
+"""Receipt reconciliation keeps its journal while a live foreign firer still owns the ledger row."""
+from contextlib import asynccontextmanager
+import json
+import os
+import subprocess
+import sys
+
+from cron import executions, jobs, scheduler_authority
+
+
+def test_live_firer_journal_survives_until_its_execution_can_settle(tmp_path, monkeypatch):
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ with jobs.use_cron_store(tmp_path / 'cron'):
+ job = jobs.create_job(prompt='report', schedule='every 1h', deliver='local')
+ code = ('import sys; from cron.executions import create_execution, mark_execution_running; '
+ f'row=create_execution({job["id"]!r},source="receipt-fixture"); '
+ 'mark_execution_running(row["id"]); print(row["id"],flush=True); sys.stdin.read()')
+ child = subprocess.Popen([sys.executable, '-c', code], env=dict(os.environ),
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
+ try:
+ fire = child.stdout.readline().strip()
+ assert fire and child.poll() is None
+ assert executions.get_execution(fire)['status'] == 'running'
+ params = {'job_id': job['id'], 'request_id': fire, 'extra_prompt': None}
+ journal = scheduler_authority.journal_path(job['id'], fire)
+ journal.parent.mkdir(parents=True)
+ journal.write_text(json.dumps({'params': params, 'receipt': None}), encoding='utf-8')
+ class Peer:
+ async def rpc(self, method, **kwargs):
+ assert method == 'cron.recover' and kwargs == params
+ return {'status': 'terminal', 'job': job, 'result': [True, 'report', 'answer', None]}
+ @asynccontextmanager
+ async def connect():
+ yield Peer()
+ monkeypatch.setattr('hermes_cli.gateway_client.connect_gateway', connect)
+ scheduler_authority.reconcile_pending()
+ assert journal.exists(), 'the live firer left running execution without its recovery receipt'
+ assert executions.get_execution(fire)['status'] == 'running'
+ child.stdin.close()
+ child.wait(timeout=10)
+ scheduler_authority.reconcile_pending()
+ assert not journal.exists()
+ assert executions.get_execution(fire)['status'] == 'completed'
+ assert jobs.get_job(job['id'])['repeat']['completed'] == 1
+ finally:
+ if child.poll() is None:
+ child.kill()
+ child.wait(timeout=10)
+ if not child.stdin.closed:
+ child.stdin.close()
+ child.stdout.close()