Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit 41615a7

Browse files
committed
fix(scheduler): prevent tasks firing immediately on daemon restart
when recalculateNextRuns() computed nextRun from lastRun and the daemon restarted after the scheduled window (e.g. morning-briefing at 7 AM, daemon restarted at 8 PM), nextRun landed in the past and checkAndRun() would fire the task immediately. now recalculates from 'now' when the computed nextRun is already past, so time-sensitive tasks are correctly scheduled for their next future occurrence.
1 parent d359b09 commit 41615a7

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/autonomous/scheduler.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,16 @@ export class Scheduler {
995995

996996
// Calculate from last run if available, otherwise from now
997997
const from = task.lastRun ? new Date(task.lastRun) : now;
998-
task.nextRun = parseCronExpression(task.cron, from) ?? undefined;
998+
let nextRun = parseCronExpression(task.cron, from) ?? undefined;
999+
1000+
// If the calculated nextRun is already in the past (e.g. daemon restarted
1001+
// after a scheduled task's window passed), recalculate from now so the
1002+
// task doesn't fire immediately on startup at the wrong time of day.
1003+
if (nextRun && nextRun <= now) {
1004+
nextRun = parseCronExpression(task.cron, now) ?? undefined;
1005+
}
1006+
1007+
task.nextRun = nextRun;
9991008
}
10001009
}
10011010

0 commit comments

Comments
 (0)