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): resume() 的 interval 任务按冷启动从 now 起算,不再沿用旧节奏 (#603)

Silas24604ae4bb3e
diff --git a/packages/maker-scheduler/src/__tests__/scheduler.test.ts b/packages/maker-scheduler/src/__tests__/scheduler.test.ts
index 3d7e8e1a72..67faf02ed4 100644
--- a/packages/maker-scheduler/src/__tests__/scheduler.test.ts
+++ b/packages/maker-scheduler/src/__tests__/scheduler.test.ts
@@ -737,6 +737,40 @@ describe('Scheduler', () => {
expect(after?.nextFireAt).toBe(Date.UTC(2026, 0, 1, 5, 10, 0));
});
+ it('resume() with intervalMs cold-starts from now even when the last run is recent', async () => {
+ // Regression: resume is documented as a cold start ("起新一轮 N 倒计时"), matching
+ // update()'s `now + intervalMs`. A schedule that finished 17:22:25 with a 1h
+ // interval, resumed at 17:40:00 (well within that hour), must re-arm at
+ // now + 1h = 18:40:00 — NOT lastFinishedAt + 1h = 18:22:25. The latter is
+ // start()/restart's "respect the original cadence" semantics, which must not
+ // leak into a user-initiated resume.
+ h.storage.schedules.set('hourly', {
+ id: 'hourly',
+ name: 'every hour',
+ prompt: 'p',
+ kind: 'cron',
+ cronExpr: '0 * * * *',
+ intervalMs: 60 * 60_000,
+ timezone: 'UTC',
+ recurring: true,
+ manual: false,
+ agentKind: 'claude-code',
+ workspaceKind: 'project',
+ useWorktree: false,
+ notify: { desktop: false, feishu: false },
+ status: 'paused',
+ createdAt: Date.UTC(2026, 0, 1, 16, 0, 0),
+ updatedAt: 0,
+ lastFiredAt: Date.UTC(2026, 0, 1, 17, 22, 18),
+ lastFinishedAt: Date.UTC(2026, 0, 1, 17, 22, 25),
+ nextFireAt: Date.UTC(2026, 0, 1, 18, 22, 25),
+ });
+ h.clock.setTo(Date.UTC(2026, 0, 1, 17, 40, 0));
+ await h.scheduler.resume('hourly');
+ const after = await h.storage.get('hourly');
+ expect(after?.nextFireAt).toBe(Date.UTC(2026, 0, 1, 18, 40, 0));
+ });
+
// ── update() 在用户编辑时是否立刻取消 pending fire ──
// 用户体感:"Next 13:50" 还有 9 分钟到,我把 Every 10min 改成 Every 5min,
// 应该看到 nextFireAt 立刻刷成 now+5min(取消旧的 13:50)。
diff --git a/packages/maker-scheduler/src/engine/scheduler.ts b/packages/maker-scheduler/src/engine/scheduler.ts
index bb360f864f..f1a4c8557e 100644
--- a/packages/maker-scheduler/src/engine/scheduler.ts
+++ b/packages/maker-scheduler/src/engine/scheduler.ts
@@ -1111,10 +1111,12 @@ export class Scheduler extends EventEmitter {
const existing = await this.storage.get(id);
if (!existing) throw new Error(`Schedule not found: ${id}`);
const now = this.clock.now();
- // resume 视作冷启动:interval 模式起新一轮 N 倒计时;cron 模式找下一个壁钟槽位。
+ // resume 视作冷启动:interval 模式起新一轮 N 倒计时(从 now 起算,与 update() 一致);
+ // cron 模式找下一个壁钟槽位。不要复用 nextIntervalFire —— 它按 lastFinishedAt+N 尊重原
+ // 节奏(restart 语义),会让「上次完成不到一个 N 就 resume」比冷启动更早触发。
const next =
existing.intervalMs !== undefined
- ? nextIntervalFire(existing.lastFinishedAt ?? existing.createdAt, existing.intervalMs, now)
+ ? now + existing.intervalMs
: nextCronOrMonthlyFire(existing.cronExpr, now, existing.timezone);
const updated = await this.storage.update(id, {
status: 'active',