Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): stop advancing disabled routine timers
Chrisad8188721028
diff --git a/docs/product-rules/bot-routines.md b/docs/product-rules/bot-routines.mdindex 5bd0e5ee5e..24943dfba1 100644--- a/docs/product-rules/bot-routines.md+++ b/docs/product-rules/bot-routines.md@@ -27,6 +27,8 @@伙伴忙碌导致的 deferred 保留同一待执行批次,30 秒后重试;宿主通过 runNow 的内部 deferToCaller 选项领取重试责任,底层不设置重试时间,不能由旧 backing schedule 另行重放旧事件。普通手动调度保持原有忙碌失败语义。已开始的执行失败不会自动重做,避免重复外部副作用。暂停或删除取消等待项并中止当前运行;修改规则取消旧版本等待项,下一次事件按新规则匹配。+停用规则时清除 cron/interval 定时游标,空闲 tick 不再因该规则写盘或广播;启动时一并清理旧文件中停用规则的残留游标,保留配置和历史。重新启用从当前时间安排下一次触发,不补跑停用期间的触发;停用状态仍可显式手动执行,且不会因此恢复定时游标。+执行已结束但终态落盘失败时,保留执行结果和该规则的运行锁;后续触发只能排队,不能再次执行。每 30 秒只重试保存原结果,成功后才释放队列;若在此期间重启,沿用 interrupted 恢复,不重放已执行项。其它规则不因单条终态保存失败而被停用。伙伴暂停时阻止新触发、取消等待项并中止运行,保留每条规则自身的启停选择;恢复只重新安排原本启用的规则,不补跑暂停期间的触发。永久删除先停止执行、删除 backing schedule,再清除规则和包含事件数据的历史;清理失败时保留伙伴档案供重试。启动按当前伙伴状态恢复,清除已不存在伙伴的规则并阻止暂停伙伴执行。diff --git a/packages/maker-scheduler/src/__tests__/routines.test.ts b/packages/maker-scheduler/src/__tests__/routines.test.tsindex ae47f32c43..a4a373d1d5 100644--- a/packages/maker-scheduler/src/__tests__/routines.test.ts+++ b/packages/maker-scheduler/src/__tests__/routines.test.ts@@ -33,6 +33,7 @@ async function fixture(let now = 1000;let id = 0;const onError = vi.fn();+ const changed = vi.fn();const engine = new RoutineEngine({load: async () => structuredClone(snapshot),save: async (state) => {@@ -42,7 +43,7 @@ async function fixture(execute,id: () => `id-${++id}`,now: () => now,- changed: vi.fn(),+ changed,onError,});await engine.start();@@ -56,6 +57,7 @@ async function fixture(engine,execute,onError,+ changed,snapshot: () => snapshot,advance: (ms: number) => {now += ms;@@ -64,6 +66,79 @@ async function fixture(}describe("Routine event admission and execution", () => {+ it.each([+ { id: 'timer', kind: 'interval' as const, intervalMs: 60_000 },+ { id: 'timer', kind: 'cron' as const, expression: '* * * * *', timezone: 'UTC' },+ ])('does not write or broadcast disabled $kind timers and resumes from re-enable time', async (trigger) => {+ const persist = vi.fn(async () => {});+ const execute = vi.fn(async () => ({}));+ const f = await fixture(execute, null, persist);+ const enabledInput = { ...input, triggers: [trigger] };+ try {+ const routine = await f.engine.put('bot', enabledInput);+ await f.engine.put('bot', { ...enabledInput, enabled: false }, routine.id);+ // Creating an already-disabled rule must also remain idle.+ const disabled = await f.engine.put('bot', { ...enabledInput, enabled: false });+ expect(f.snapshot()?.next).toEqual({});+ persist.mockClear();+ f.changed.mockClear();+ for (let minute = 0; minute < 3; minute++) {+ f.advance(60_000);+ await f.engine.tick();+ }+ expect(persist).not.toHaveBeenCalled();+ expect(f.changed).not.toHaveBeenCalled();+ expect(execute).not.toHaveBeenCalled();++ await f.engine.put('bot', enabledInput, routine.id);+ persist.mockClear();+ f.changed.mockClear();+ await f.engine.tick();+ expect(persist).not.toHaveBeenCalled();+ expect(f.changed).not.toHaveBeenCalled();+ expect(execute).not.toHaveBeenCalled();+ f.advance(60_000);+ await f.engine.tick();+ await vi.waitFor(() => expect(f.engine.history(routine.id)[0]?.status).toBe('success'));+ expect(execute).toHaveBeenCalledOnce();+ expect(f.engine.history(disabled.id)).toEqual([]);+ expect(f.snapshot()?.next[`${disabled.id}:timer`]).toBeUndefined();+ } finally {+ await f.engine.stop();+ }+ });++ it('cleans legacy disabled timer cursors on restart without shifting enabled timers or history', async () => {+ const first = await fixture();+ const disabled = await first.engine.put('bot', { ...input, enabled: false });+ const enabled = await first.engine.put('bot', input);+ await first.engine.runNow('bot', disabled.id);+ await vi.waitFor(() => expect(first.engine.history(disabled.id)[0]?.status).toBe('success'));+ await first.engine.stop();+ const saved = structuredClone(first.snapshot()!);+ saved.next[`${disabled.id}:fallback`] = 1;+ const enabledNext = saved.next[`${enabled.id}:fallback`];+ const history = structuredClone(saved.runs);+ const persist = vi.fn(async () => {});+ const execute = vi.fn(async () => ({}));+ const restored = await fixture(execute, saved, persist);+ try {+ expect(restored.snapshot()?.next).toEqual({ [`${enabled.id}:fallback`]: enabledNext });+ expect(restored.snapshot()?.runs).toEqual(history);+ persist.mockClear();+ restored.changed.mockClear();+ for (let minute = 0; minute < 3; minute++) {+ restored.advance(60_000);+ await restored.engine.tick();+ }+ expect(persist).not.toHaveBeenCalled();+ expect(restored.changed).not.toHaveBeenCalled();+ expect(execute).not.toHaveBeenCalled();+ } finally {+ await restored.engine.stop();+ }+ });+it('still dispatches an explicit manual run of a disabled routine', async () => {const execute = vi.fn<RoutineEngineDeps['execute']>(async (_routine, _run, _signal, canDispatch) => {expect(canDispatch()).toBe(true);@@ -76,6 +151,7 @@ describe("Routine event admission and execution", () => {await vi.waitFor(() => expect(f.engine.history(routine.id)[0].status).toBe('success'));expect(execute).toHaveBeenCalledOnce();expect(f.engine.list('bot')[0].enabled).toBe(false);+ expect(f.snapshot()?.next).toEqual({});} finally {await f.engine.stop();}diff --git a/packages/maker-scheduler/src/routine-engine.ts b/packages/maker-scheduler/src/routine-engine.tsindex 0b1f5f8499..0e2b71236b 100644--- a/packages/maker-scheduler/src/routine-engine.ts+++ b/packages/maker-scheduler/src/routine-engine.ts@@ -103,6 +103,13 @@ export class RoutineEngine {this.state.receipts = compactRoutineReceipts(this.state.receipts, this.deps.now());for (const [botId, status] of botStates ?? []) this.applyBotState(this.state, botId, status);for (const botId of this.state.pausedBotIds ?? []) this.blockedBots.add(botId);+ // Older versions retained timer cursors for disabled rules. Remove them in+ // the existing startup save so an idle tick cannot keep rewriting history.+ for (const routine of this.state.routines) {+ if (routine.enabled) continue;+ for (const key of Object.keys(this.state.next))+ if (key.startsWith(`${routine.id}:`)) delete this.state.next[key];+ }// Removed rules have no history entry point; do not retain their event payloads.const retainedIds = new Set(this.state.routines.map((routine) => routine.id));this.state.runs = this.state.runs.filter((run) => retainedIds.has(run.routineId));@@ -218,7 +225,7 @@ export class RoutineEngine {];for (const key of Object.keys(state.next))if (key.startsWith(`${routine.id}:`)) delete state.next[key];- for (const trigger of routine.triggers) {+ for (const trigger of routine.enabled ? routine.triggers : []) {const oldTrigger = existing?.triggers.find((item) => item.id === trigger.id,);@@ -388,11 +395,11 @@ export class RoutineEngine {if (!Object.values(this.state.next).some((time) => time <= now)) return;await this.change((state) => {for (const routine of state.routines) {- if (this.blockedBots.has(routine.botId) || this.removing.has(routine.id)) continue;+ if (!routine.enabled || this.blockedBots.has(routine.botId) || this.removing.has(routine.id)) continue;for (const trigger of routine.triggers) {const key = `${routine.id}:${trigger.id}`;if (state.next[key] === undefined || state.next[key] > now) continue;- if (routine.enabled) this.enqueue(state, routine, [trigger.id]);+ this.enqueue(state, routine, [trigger.id]);const next = nextRoutineTriggerAt(trigger, now);if (next !== undefined) state.next[key] = next;}