Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(scheduler): resolve DST overlap/gap wall times in cron engine (#4972)

billythegod25a437cc0418
diff --git a/packages/maker-scheduler/src/__tests__/cron.test.ts b/packages/maker-scheduler/src/__tests__/cron.test.ts
index 71c0ef58d4..6ee568f3c6 100644
--- a/packages/maker-scheduler/src/__tests__/cron.test.ts
+++ b/packages/maker-scheduler/src/__tests__/cron.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { parseCron, nextRun, cronToHuman } from '../engine/cron.js';
+import { parseCron, nextRun, cronToHuman, fromWallClock } from '../engine/cron.js';
describe('parseCron', () => {
it('expands */5 in minute field', () => {
@@ -197,6 +197,70 @@ describe('nextRun', () => {
const next = nextRun('0 12 * * *', from, 'America/New_York');
expect(next).toBe(Date.UTC(2026, 10, 1, 17, 0, 0));
});
+
+ it('DST fall-back overlap: from inside the repeated hour never yields a past instant', () => {
+ // 2026-11-01 06:31:30Z = 01:31:30 EST — the SECOND pass of the repeated
+ // 01:xx hour (02:00 EDT → 01:00 EST at 06:00Z). Wall 01:45 occurs at both
+ // 05:45Z (EDT) and 06:45Z (EST); resolving ambiguity to the earlier
+ // occurrence returned 05:45Z — 46min in the past — which the scheduler
+ // treated as due and re-fired the finished schedule every tick.
+ const from = Date.UTC(2026, 10, 1, 6, 31, 30);
+ const next = nextRun('45 1 * * *', from, 'America/New_York');
+ expect(next).toBe(Date.UTC(2026, 10, 1, 6, 45, 0));
+ expect(next).toBeGreaterThan(from);
+ });
+
+ it('DST fall-back overlap: minute-wrap branch also stays in the future', () => {
+ // Same geometry, but the current minute (31) is past the target (10), so
+ // nextRun takes the wrap-into-next-hour branch through wall 02:00.
+ const from = Date.UTC(2026, 10, 1, 6, 31, 30);
+ const next = nextRun('10 1 * * *', from, 'America/New_York');
+ expect(next).toBe(Date.UTC(2026, 10, 2, 6, 10, 0));
+ expect(next).toBeGreaterThan(from);
+ });
+
+ it('DST fall-back overlap: ambiguous wall time resolves to the later occurrence', () => {
+ // 01:00 on 2026-11-01 in NY exists twice (05:00Z EDT and 06:00Z EST).
+ expect(fromWallClock(2026, 11, 1, 1, 0, 'America/New_York'))
+ .toBe(Date.UTC(2026, 10, 1, 6, 0, 0));
+ // Southern-hemisphere geometry (guess lands post-transition):
+ // 2026-04-05 02:30 in Sydney exists twice (15:30Z AEDT and 16:30Z AEST).
+ expect(fromWallClock(2026, 4, 5, 2, 30, 'Australia/Sydney'))
+ .toBe(Date.UTC(2026, 3, 4, 16, 30, 0));
+ });
+
+ it('DST spring-forward gap: nonexistent wall time clamps past the transition', () => {
+ // 02:30 on 2026-03-08 in NY does not exist (02:00 EST → 03:00 EDT).
+ // The post-transition reading is 03:30 EDT = 07:30Z.
+ expect(fromWallClock(2026, 3, 8, 2, 30, 'America/New_York'))
+ .toBe(Date.UTC(2026, 2, 8, 7, 30, 0));
+ // Sydney gap: 2026-10-04 02:30 AEST doesn't exist (→ 03:00 AEDT); the
+ // post-transition reading is 03:30 AEDT = Oct 3 16:30Z.
+ expect(fromWallClock(2026, 10, 4, 2, 30, 'Australia/Sydney'))
+ .toBe(Date.UTC(2026, 9, 3, 16, 30, 0));
+ });
+
+ it('DST spring-forward gap: hourly cron terminates instead of exhausting the iteration limit', () => {
+ // cron "0 2 * * *" can never match wall 02:00 on 2026-03-08 in NY. Before
+ // the gap clamp this looped MAX_ITERATIONS times (~4.4s of blocking Intl
+ // calls) and threw, quarantining the schedule. The missing slot is skipped:
+ // the fire lands on the next day's 02:00 EDT.
+ const next = nextRun('0 2 * * *', Date.UTC(2026, 2, 8, 5, 0, 0), 'America/New_York');
+ expect(next).toBe(Date.UTC(2026, 2, 9, 6, 0, 0));
+ const nextHalf = nextRun('30 2 * * *', Date.UTC(2026, 2, 8, 4, 0, 0), 'America/New_York');
+ expect(nextHalf).toBe(Date.UTC(2026, 2, 9, 6, 30, 0));
+ });
+
+ it('every-minute cron never moves backwards across both 2026 NY transition days', () => {
+ for (const dayStart of [Date.UTC(2026, 2, 8), Date.UTC(2026, 10, 1)]) {
+ let cur = dayStart;
+ for (let i = 0; i < 2000; i++) {
+ const next = nextRun('* * * * *', cur, 'America/New_York');
+ expect(next).toBeGreaterThan(cur);
+ cur = next;
+ }
+ }
+ });
});
describe('cronToHuman', () => {
diff --git a/packages/maker-scheduler/src/__tests__/monthlyClamp.test.ts b/packages/maker-scheduler/src/__tests__/monthlyClamp.test.ts
index 56e6a3d7ed..89dd139a87 100644
--- a/packages/maker-scheduler/src/__tests__/monthlyClamp.test.ts
+++ b/packages/maker-scheduler/src/__tests__/monthlyClamp.test.ts
@@ -134,4 +134,14 @@ describe('nextCronOrMonthlyFire (dispatch)', () => {
// Standard cron: Feb has no 31, Feb 1 already past → next is March 1 09:00 CST
expect(result).toBe(Date.UTC(2026, 2, 1, 1, 0));
});
+
+ it('monthly preset in the DST fall-back overlap does not skip the month', () => {
+ // 2026-11-01 06:31Z = 01:31 EST (second pass of the repeated 01:xx hour).
+ // `45 1 1 * *` fires at wall 01:45 on the 1st; the earlier occurrence
+ // (05:45Z) is in the past, which used to push the fire to Dec 1 and skip
+ // November entirely.
+ const from = Date.UTC(2026, 10, 1, 6, 31, 0);
+ const result = nextCronOrMonthlyFire('45 1 1 * *', from, 'America/New_York');
+ expect(result).toBe(Date.UTC(2026, 10, 1, 6, 45, 0));
+ });
});
diff --git a/packages/maker-scheduler/src/engine/cron.ts b/packages/maker-scheduler/src/engine/cron.ts
index a76a8b2da6..a9fa615b14 100644
--- a/packages/maker-scheduler/src/engine/cron.ts
+++ b/packages/maker-scheduler/src/engine/cron.ts
@@ -184,15 +184,57 @@ function tzOffsetMs(epochMs: number, tz: string): number {
return wallAsUtc - epochSec;
}
-// Convert wall-clock (y,mo,d,h,mi) in tz back to epoch ms. Handles DST edges by re-checking offset.
+// Whether the epoch instant reads exactly (y,mo,d,h,mi) on the wall clock in tz.
+function wallClockEquals(
+ epochMs: number,
+ y: number,
+ mo: number,
+ d: number,
+ h: number,
+ mi: number,
+ tz: string,
+): boolean {
+ const w = wallClock(epochMs, tz);
+ return w.y === y && w.mo === mo && w.d === d && w.h === h && w.mi === mi;
+}
+
+// Convert wall-clock (y,mo,d,h,mi) in tz back to epoch ms.
+//
+// DST edges:
+// - Overlap (fall-back, wall time occurs twice): resolves to the LATER
+// occurrence. nextRun/nextMonthlyFire compare candidates against a strictly
+// increasing "from" instant; the earlier occurrence can lie in the past when
+// `from` is already inside the repeated hour, which made the scheduler
+// re-fire a finished schedule every tick until the wall clock left the
+// repeated hour.
+// - Gap (spring-forward, wall time never occurs): returns the instant the wall
+// clock reads the requested time plus the gap size (the post-transition
+// interpretation). Returning the pre-transition side made nextRun re-derive
+// the missing slot forever and exhaust MAX_ITERATIONS (~4.4s of blocking
+// Intl calls), quarantining the schedule and stalling the host tick loop.
// Exported for sibling use (see wallClock comment above).
export function fromWallClock(y: number, mo: number, d: number, h: number, mi: number, tz: string): number {
const guess = Date.UTC(y, mo - 1, d, h, mi, 0);
const off1 = tzOffsetMs(guess, tz);
const candidate = guess - off1;
const off2 = tzOffsetMs(candidate, tz);
- if (off2 === off1) return candidate;
- return guess - off2;
+ if (off2 === off1) {
+ // Offsets agree, but a fall-back transition within the next hour can make
+ // this wall time occur a second time; prefer that later occurrence.
+ const offAfter = tzOffsetMs(candidate + 3600_000, tz);
+ if (offAfter !== off1) {
+ const later = guess - offAfter;
+ if (wallClockEquals(later, y, mo, d, h, mi, tz)) return later;
+ }
+ return candidate;
+ }
+ const corrected = guess - off2;
+ if (wallClockEquals(corrected, y, mo, d, h, mi, tz)) return corrected;
+ // Nonexistent wall time: spring-forward gaps shrink the day, so the offset
+ // that applies after the jump is the larger one — clamping there guarantees
+ // the returned instant is at/after the transition, letting walkers exit the
+ // gap instead of re-entering it.
+ return guess - Math.min(off1, off2);
}
// ---------- nextRun ----------