Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.next.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
5 changes: 2 additions & 3 deletions LifeOS/install/LIFEOS/WORKFLOWS/engine/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down