diff --git a/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.next.test.ts b/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.next.test.ts new file mode 100644 index 0000000000..4206d6690c --- /dev/null +++ b/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.next.test.ts @@ -0,0 +1,37 @@ +/** + * Verifies that nextFire for interval triggers aligns with the dispatcher's clock + * (multiples of period counted from Unix epoch grid) rather than drifting with lastStart. + * + * Issue #2788 + */ +import { describe, expect, test } from "bun:test"; +import { nextFire, occurrencesBetween } from "./schedule.ts"; + +const interval = (minutes: number) => ({ type: "interval" as const, minutes, missed_run: "once" as const }); +const at = (iso: string) => new Date(iso); + +const printedNext = (minutes: number, lastRunIso: string, nowIso: string): string => + nextFire(interval(minutes), "any-lane", at(nowIso), Date.parse(lastRunIso), "/nonexistent")!.toISOString(); + +const dispatcherNext = (minutes: number, nowIso: string): string => { + const now = at(nowIso); + const found = occurrencesBetween(interval(minutes), now, new Date(now.getTime() + minutes * 2 * 60_000)); + return found[0].at.toISOString(); +}; + +describe("interval trigger: workflow next vs dispatcher clock", () => { + test("a 60-minute lane whose last run was off the grid: next matches dispatcher next epoch multiple", () => { + const now = "2026-11-12T23:20:00.000Z"; + expect(printedNext(60, "2026-11-12T22:45:00.000Z", now)).toBe(dispatcherNext(60, now)); + }); + + test("a run that starts a few seconds after its tick preserves epoch-grid alignment", () => { + const now = "2026-11-12T10:01:00.000Z"; + expect(printedNext(15, "2026-11-12T10:00:04.000Z", now)).toBe(dispatcherNext(15, now)); + }); + + test("control: they agree when last run started on the grid", () => { + const now = "2026-11-12T10:01:00.000Z"; + expect(printedNext(15, "2026-11-12T10:00:00.000Z", now)).toBe(dispatcherNext(15, now)); + }); +}); \ No newline at end of file diff --git a/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.ts b/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.ts index 0c7635433a..92e05ef438 100644 --- a/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.ts +++ b/LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.ts @@ -243,9 +243,8 @@ export function nextFire(trigger: TriggerV2, name: string, now: Date, lastStart: return nextScheduledAt(v1ScheduleProbe(trigger.cron, zoneOf(trigger)), now); case "interval": { const period = trigger.minutes * 60_000; - let due = Number.isFinite(lastStart) ? lastStart + period : now.getTime() + period; - while (due <= now.getTime()) due += period; - return new Date(due); + const found = occurrencesBetween(trigger, now, new Date(now.getTime() + period)); + return found[0]?.at ?? null; } case "once": return Date.parse(trigger.at) > now.getTime() && !isOnceConsumed(dir, name, trigger.at) ? new Date(trigger.at) : null;