cron: pending actions are lost if the merged store cannot be saved
Problem
CronService._merge_action() clears action.jsonl before calling _save_store(). If the store write fails (for example, ENOSPC), the old jobs.json remains intact but the accepted actions have already been removed from disk.
The failed service retains a dirty in-memory snapshot, which can support an in-process retry. That does not protect a restart before the retry succeeds: additions disappear, edits revert, and deleted jobs reappear.
Reproduction
Confirmed on main at f62e0da9 with deterministic fault injection, without model calls:
- Persist an initial cron store.
- Use a separate, stopped
CronServiceto enqueue an add, update, or delete through its public API. These operations append toaction.jsonl. - Let a running service load and merge the pending actions, with its
_atomic_writepatched to raiseOSError(errno.ENOSPC, "simulated disk full"). - Inspect the files after the exception:
jobs.jsonstill contains the original snapshot, whileaction.jsonlis empty.
The same regression test fails for all three operations:
test_action_log_survives_failed_store_save[add] FAILED
test_action_log_survives_failed_store_save[update] FAILED
test_action_log_survives_failed_store_save[delete] FAILED
assert action_path.read_bytes() == pending_actions
E assert b'' == <previously accepted action>A fresh service instance can only recover the old snapshot because neither file contains the pending operation anymore.
Expected behavior / proposed fix
Keep the action log until the merged store has been successfully saved, under the existing file lock. The existing ID-keyed add/update/delete replay can recover if saving fails or if execution is interrupted between saving the snapshot and clearing the log.
I am preparing a narrowly scoped fix with restart-recovery tests; no store format or scheduling changes are needed.
Source: HKUDS/nanobot