Skip to content

The agent that grows with you

fix(cron): the admission journal survives power loss; status reads back off

Teknium1889bc732aa3
diff --git a/cron/scheduler_authority.py b/cron/scheduler_authority.py
index cb9d08a8c35..7c5a97c4c0e 100644
--- a/cron/scheduler_authority.py
+++ b/cron/scheduler_authority.py
@@ -36,7 +36,8 @@ def run_canonical_job(job, *, extra_prompt=None, cancel_event=None, execution_id
def save():
root.mkdir(parents=True, exist_ok=True)
- atomic_json_write(journal, record, mode=0o600)
+ # The journal is the only evidence a lost reply was admitted: survive power loss.
+ atomic_json_write(journal, record, mode=0o600, fsync_dir=True)
async def refused(call, exc):
# A lost reply may still have been admitted. Only the owner's own verdict (a bounded
@@ -59,6 +60,8 @@ def run_canonical_job(job, *, extra_prompt=None, cancel_event=None, execution_id
raise
record['receipt'] = receipt
save()
+ # Cron turns run for minutes: back the status read off to 2 s instead of 10 reads/s.
+ delay = .1
while True:
if cancel_event is not None and cancel_event.is_set():
await call('cancel', receipt)
@@ -68,7 +71,8 @@ def run_canonical_job(job, *, extra_prompt=None, cancel_event=None, execution_id
return tuple(state['result'])
if state['status'] == 'unknown':
raise CronExecutionUnknown('unknown_execution: cron admission was not replayed')
- await asyncio.sleep(.1)
+ await asyncio.sleep(delay)
+ delay = min(delay * 1.5, 2.0)
async def remote():
async with connect_gateway() as client:
diff --git a/tests/cron/test_cron_recovery.py b/tests/cron/test_cron_recovery.py
index 1d82c6a3879..4b3e635e8cc 100644
--- a/tests/cron/test_cron_recovery.py
+++ b/tests/cron/test_cron_recovery.py
@@ -155,3 +155,33 @@ def test_owner_refused_fire_is_booked_failed_and_retires_its_journal(tmp_path, m
finally:
db.close()
assert jobs.get_job(job['id'])['state'] != 'paused'
+
+
+def test_admission_journal_survives_power_loss(tmp_path, monkeypatch):
+ """The journal is the only evidence that a lost reply may have been admitted, so its rename
+ is made durable (directory fsync), not left in the page cache until the next flush."""
+ import utils
+ from cron import scheduler_authority
+ synced = []
+ real = utils.fsync_directory
+ monkeypatch.setattr(utils, 'fsync_directory', lambda path: (synced.append(str(path)), real(path)))
+ workdir = tmp_path / 'work'
+ workdir.mkdir()
+ with jobs.use_cron_store(tmp_path / 'cron'):
+ job = jobs.create_job(prompt='original', schedule='every 1h', deliver='local', workdir=str(workdir))
+ workdir.rmdir()
+ authority, db = _owner(tmp_path, monkeypatch)
+
+ async def probe():
+ session_cron.bind_owner(authority)
+ try:
+ await asyncio.to_thread(
+ scheduler_authority.run_canonical_job, jobs.get_job(job['id']), execution_id='fire')
+ finally:
+ session_cron.unbind_owner(authority)
+ try:
+ asyncio.run(probe())
+ finally:
+ db.close()
+ journal_dir = str(scheduler_authority.journal_path(job['id'], 'fire').parent)
+ assert journal_dir in synced