Skip to content

The agent that grows with you

fix(cron): a firer-booked terminal row retires its admission journal

Teknium3a41994f452f
diff --git a/cron/scheduler_authority.py b/cron/scheduler_authority.py
index cbd9fdb60ce..f2bb545e3f1 100644
--- a/cron/scheduler_authority.py
+++ b/cron/scheduler_authority.py
@@ -118,7 +118,7 @@ def reconcile_pending(*, allow_connect=True):
from gateway.session_cron import owner_for_home, operation
from hermes_constants import get_hermes_home
from hermes_cli.gateway_client import connect_gateway
- from cron.executions import execution_owner_live
+ from cron.executions import execution_owner_live, get_execution
from cron.jobs import pause_job
from cron.scheduler import (_RunDelivery, _FireOwnership, _apply_agent_failure_marker,
_save_compose_deliver)
@@ -137,6 +137,12 @@ def reconcile_pending(*, allow_connect=True):
# Its firer (this process included) still owns the row and its tail: re-saving the
# output or re-marking the job on every tick would duplicate its side effects.
continue
+ row = get_execution(params['request_id'])
+ if row is not None and row['job_id'] == params['job_id'] and row['status'] in {'completed', 'failed'}:
+ # The firer booked this attempt's terminal outcome itself (ownership lost, shutdown
+ # interrupt, a crashed tail): the receipt is not a result to save or deliver again.
+ journal.unlink(missing_ok=True)
+ continue
if 'refusal' in record:
state = {'status': 'terminal', 'result': record['refusal'], 'job': record['job']}
else:
diff --git a/tests/cron/test_cron_journal_terminal_exits.py b/tests/cron/test_cron_journal_terminal_exits.py
new file mode 100644
index 00000000000..80573d79da0
--- /dev/null
+++ b/tests/cron/test_cron_journal_terminal_exits.py
@@ -0,0 +1,65 @@
+"""Bug class: a canonical cron firer books its own terminal ledger row on a non-completion exit
+(ownership lost, shutdown interrupt, crashed tail) but leaves the admission journal behind;
+receipt recovery then replays the owner's result: a second output save, delivery and run mark.
+"""
+from contextlib import asynccontextmanager
+import json
+
+import pytest
+
+from cron import executions, jobs, scheduler, scheduler_authority
+from cron.scheduler_bookkeeping import finish_interrupted_run
+from hermes_cli import gateway_client
+
+
+def _ownership_lost(job, fire, journal):
+ scheduler._record_fire_ownership_lost(job, None, fire)
+
+
+def _shutdown_interrupted(job, fire, journal):
+ finish_interrupted_run(job, fire, None)
+
+
+def _crashed_tail(job, fire, journal):
+ # run_one_job's generic exception tail: it books the row failed itself.
+ assert scheduler._run_one_job_body(dict(job, execution_id=fire)) is False
+
+
[email protected]('firer_exit', [_ownership_lost, _shutdown_interrupted, _crashed_tail],
+ ids=['ownership-lost', 'shutdown-interrupted', 'crashed-tail'])
+def test_firer_booked_exit_is_not_replayed_by_receipt_recovery(tmp_path, monkeypatch, firer_exit):
+ calls = []
+
+ class Peer:
+ async def rpc(self, method, **params):
+ calls.append(method)
+ return {'status': 'terminal', 'job': job, 'result': [True, 'document', 'answer', None]}
+
+ @asynccontextmanager
+ async def connected():
+ yield Peer()
+ monkeypatch.setattr(gateway_client, 'connect_gateway', connected)
+
+ def run_job(job, **kwargs):
+ raise RuntimeError('the firing tail crashed after the owner finished')
+ monkeypatch.setattr(scheduler, 'run_job', run_job)
+ with jobs.use_cron_store(tmp_path / 'cron'):
+ job = jobs.create_job(prompt='report', schedule='every 1h', deliver='local')
+ fire = executions.create_execution(job['id'], source='test')['id']
+ if firer_exit is not _crashed_tail:
+ executions.mark_execution_running(fire)
+ journal = scheduler_authority.journal_path(job['id'], fire)
+ journal.parent.mkdir(parents=True, exist_ok=True)
+ journal.write_text(json.dumps({'params': {'job_id': job['id'], 'request_id': fire, 'extra_prompt': None},
+ 'receipt': {'session_id': 's', 'admission_id': 'a'}}))
+ firer_exit(dict(jobs.get_job(job['id'])), fire, journal)
+ assert executions.get_execution(fire)['status'] == 'failed'
+ booked = jobs.get_job(job['id'])
+ scheduler_authority.reconcile_pending()
+ after = jobs.get_job(job['id'])
+ assert calls == []
+ assert not list(jobs._job_output_dir(job['id']).glob('*.md'))
+ assert (after['last_run_at'], after['last_status'], after['repeat']) == (
+ booked['last_run_at'], booked['last_status'], booked['repeat'])
+ assert executions.get_execution(fire)['status'] == 'failed'
+ assert not journal.exists()