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): keep cron wrap in future across sub-hour DST (#6561)

guyong-zapo0f2337b3cd09
diff --git a/packages/maker-scheduler/src/__tests__/cron.test.ts b/packages/maker-scheduler/src/__tests__/cron.test.ts
index 6ee568f3c6..23bcb3dbef 100644
--- a/packages/maker-scheduler/src/__tests__/cron.test.ts
+++ b/packages/maker-scheduler/src/__tests__/cron.test.ts
@@ -219,6 +219,35 @@ describe('nextRun', () => {
expect(next).toBeGreaterThan(from);
});
+ // 测试价值
+ // - 生产入口与可观察结果:nextRun 返回下一个 cron 壁钟槽位;scheduler 会用它重排 nextFireAt。
+ // - Bug/规则依据:#5924;亚小时 DST 回拨时 nextRun 必须严格晚于 from。
+ // - 现有覆盖缺口:既有纽约整小时回拨只覆盖重复段内的单个分钟,未覆盖 Lord Howe 的 30 分钟回拨与分钟 wrap。
+ // - 最小失败输入:2026-04-05 Australia/Lord_Howe 回拨日,from 位于重复 01:xx 壁钟小时。
+ // - tier:unit;直接调用公开 nextRun,无外部 I/O。
+ // - 实测耗时:随现有 cron.test.ts 定向运行记录在 PR 验证项。
+ it('Lord Howe 亚小时回拨:分钟 wrap 保留未来重复段并严格向前', () => {
+ const tz = 'Australia/Lord_Howe';
+ const cases = [
+ ['23 1 * * *', Date.UTC(2026, 3, 4, 14, 23, 13), Date.UTC(2026, 3, 5, 14, 53, 0)],
+ ['0 * * * *', Date.UTC(2026, 3, 4, 14, 0, 21), Date.UTC(2026, 3, 4, 15, 30, 0)],
+ ['0,45 1 * * *', Date.UTC(2026, 3, 4, 14, 50, 30), Date.UTC(2026, 3, 4, 15, 15, 0)],
+ ['45 1 * * *', Date.UTC(2026, 3, 4, 14, 50, 30), Date.UTC(2026, 3, 4, 15, 15, 0)],
+ ] as const;
+ for (const [expr, from, expected] of cases) {
+ const next = nextRun(expr, from, tz);
+ expect(next, expr).toBe(expected);
+ expect(next, expr).toBeGreaterThan(from);
+ }
+ });
+
+ it('Lord Howe 亚小时跳时:不存在的整点直接前进到下一合法小时', () => {
+ const from = Date.UTC(2026, 9, 3, 15, 29, 0); // local 2026-10-04 01:59, next is 02:30
+ const next = nextRun('0 * * * *', from, 'Australia/Lord_Howe');
+ expect(next).toBe(Date.UTC(2026, 9, 3, 16, 0, 0)); // local 03:00 after the 30m gap
+ 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'))
diff --git a/packages/maker-scheduler/src/__tests__/scheduler.test.ts b/packages/maker-scheduler/src/__tests__/scheduler.test.ts
index b41dbf1a2d..aa38575c22 100644
--- a/packages/maker-scheduler/src/__tests__/scheduler.test.ts
+++ b/packages/maker-scheduler/src/__tests__/scheduler.test.ts
@@ -236,6 +236,37 @@ describe('Scheduler', () => {
expect(list).toHaveLength(1);
});
+ // 测试价值
+ // - 生产入口与可观察结果:Scheduler.tick 认领到期任务、运行 runner 并重排 nextFireAt;下一合法槽位前不得重复调用。
+ // - Bug/规则依据:#5924;重排结果必须严格晚于完成时刻。
+ // - 现有覆盖缺口:已有 tick/互斥测试未在 Lord Howe 亚小时回拨时区验证 cron 重排结果。
+ // - 最小失败输入:Lord Howe 回拨后的整点 cron,完成时刻 15:30Z。
+ // - tier:unit;内存 storage、假时钟、无副作用 runner。
+ // - 实测耗时:随现有 scheduler.test.ts 定向运行记录在 PR 验证项。
+ it('Lord Howe 回拨后真实 tick 重排到未来且下一合法槽前不重复', async () => {
+ const local = makeHarness();
+ local.clock.setTo(Date.UTC(2026, 3, 4, 14, 0, 21));
+ const schedule = await local.scheduler.create({
+ ...baseInput,
+ cronExpr: '0 * * * *',
+ timezone: 'Australia/Lord_Howe',
+ });
+ expect(schedule.nextFireAt).toBe(Date.UTC(2026, 3, 4, 15, 30, 0));
+
+ local.clock.setTo(Date.UTC(2026, 3, 4, 15, 30, 0));
+ await local.scheduler.tick();
+ expect(local.runner.fire).toHaveBeenCalledTimes(1);
+ const run = (await local.storage.listRuns(schedule.id))[0];
+ const updated = await local.storage.get(schedule.id);
+ expect(run?.finishedAt).toBeDefined();
+ expect(updated?.nextFireAt).toBeDefined();
+ expect(updated!.nextFireAt!).toBeGreaterThan(run!.finishedAt!);
+
+ local.clock.advance(30_000);
+ await local.scheduler.tick();
+ expect(local.runner.fire).toHaveBeenCalledTimes(1);
+ });
+
it('marks skipped child runs as zero-cost', async () => {
const childHarness = makeHarness({
runnerImpl: async (_schedule, ctx) => {
diff --git a/packages/maker-scheduler/src/engine/cron.ts b/packages/maker-scheduler/src/engine/cron.ts
index a9fa615b14..fe675efbd9 100644
--- a/packages/maker-scheduler/src/engine/cron.ts
+++ b/packages/maker-scheduler/src/engine/cron.ts
@@ -285,11 +285,38 @@ export function nextRun(expr: string, fromMs: number, tz: string): number {
if (next > w.mi) {
cur = fromWallClock(w.y, w.mo, w.d, w.h, next, tz);
} else {
- // wrap into next hour. +1h on UTC then re-derive wall components handles DST jumps:
- // spring-forward jumps the wall hour past the missing slot; fall-back may land us
- // back in the same wall hour, which the outer loop will then advance again.
- const soh = fromWallClock(w.y, w.mo, w.d, w.h, 0, tz) + 3600_000;
- const w2 = wallClock(soh, tz);
+ // A sub-hour fall-back can leave us in the same wall-clock hour after
+ // the UTC +1h hop below. Try every cron minute in the current wall
+ // hour only for this wrap case: fromWallClock resolves an overlap to
+ // its later occurrence, so a minute that wrapped numerically (for
+ // example 01:45 after 01:50) can still be future in the repeated
+ // segment. Gap conversion is accepted only when it round-trips to the
+ // requested wall minute; otherwise the same clamped instant would be
+ // selected on every iteration.
+ const sameHourCandidates = cron.minute
+ .map((minute) => fromWallClock(w.y, w.mo, w.d, w.h, minute, tz))
+ .filter(
+ (candidate, index) =>
+ candidate > fromMs &&
+ wallClockEquals(candidate, w.y, w.mo, w.d, w.h, cron.minute[index], tz),
+ );
+ if (sameHourCandidates.length > 0) {
+ cur = Math.min(...sameHourCandidates);
+ continue;
+ }
+
+ // Wrap into the next distinct wall-clock hour. A plain +1h UTC hop
+ // can remain inside a repeated hour after a 30-minute fall-back, so
+ // advance by at most four hourly probes until the wall hour changes.
+ // The bound covers the DST transitions supported by this walker while
+ // preventing an unbounded search inside a malformed schedule.
+ const nextHourStart = fromWallClock(w.y, w.mo, w.d, w.h, 0, tz) + 3600_000;
+ let probe = nextHourStart;
+ let w2 = wallClock(probe, tz);
+ for (let step = 0; step < 4 && sameWallHour(w2, w); step++) {
+ probe += 3600_000;
+ w2 = wallClock(probe, tz);
+ }
cur = fromWallClock(w2.y, w2.mo, w2.d, w2.h, next, tz);
}
continue;
@@ -314,6 +341,10 @@ function nextOrWrap(list: CronField, current: number): number {
return list[0];
}
+function sameWallHour(a: WallClock, b: WallClock): boolean {
+ return a.y === b.y && a.mo === b.mo && a.d === b.d && a.h === b.h;
+}
+
// ---------- cronToHuman ----------
//
// English-only by design. Phase 6 (renderer UI) wraps this with an i18n layer