|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { setupCronServiceSuite, writeCronStoreSnapshot } from "./service.test-harness.js"; |
| 3 | +import { createCronServiceState } from "./service/state.js"; |
| 4 | +import { onTimer } from "./service/timer.test-support.js"; |
| 5 | +import type { CronJob } from "./types.js"; |
| 6 | + |
| 7 | +const sqliteTransactionLabels = vi.hoisted(() => [] as string[]); |
| 8 | + |
| 9 | +vi.mock("../state/openclaw-state-db.js", async (importOriginal) => { |
| 10 | + const actual = await importOriginal<typeof import("../state/openclaw-state-db.js")>(); |
| 11 | + const runOpenClawStateWriteTransaction: typeof actual.runOpenClawStateWriteTransaction = ( |
| 12 | + operation, |
| 13 | + options, |
| 14 | + transactionOptions, |
| 15 | + ) => { |
| 16 | + sqliteTransactionLabels.push(transactionOptions?.operationLabel ?? "state.write"); |
| 17 | + return actual.runOpenClawStateWriteTransaction(operation, options, transactionOptions); |
| 18 | + }; |
| 19 | + return { ...actual, runOpenClawStateWriteTransaction }; |
| 20 | +}); |
| 21 | + |
| 22 | +const { logger, makeStorePath } = setupCronServiceSuite({ |
| 23 | + prefix: "cron-timer-maintenance-", |
| 24 | + baseTimeIso: "2026-08-30T12:00:00.000Z", |
| 25 | +}); |
| 26 | + |
| 27 | +function job( |
| 28 | + id: string, |
| 29 | + nowMs: number, |
| 30 | + schedule: CronJob["schedule"], |
| 31 | + state: CronJob["state"], |
| 32 | +): CronJob { |
| 33 | + return { |
| 34 | + id, |
| 35 | + name: id, |
| 36 | + enabled: true, |
| 37 | + createdAtMs: nowMs - 60_000, |
| 38 | + updatedAtMs: nowMs - 60_000, |
| 39 | + schedule, |
| 40 | + sessionTarget: "isolated", |
| 41 | + wakeMode: "next-heartbeat", |
| 42 | + payload: { kind: "agentTurn", message: id }, |
| 43 | + state, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +async function runTimer(jobs: CronJob[], nowMs: number) { |
| 48 | + const store = await makeStorePath(); |
| 49 | + await writeCronStoreSnapshot({ storePath: store.storePath, jobs }); |
| 50 | + const state = createCronServiceState({ |
| 51 | + storePath: store.storePath, |
| 52 | + cronEnabled: true, |
| 53 | + log: logger, |
| 54 | + nowMs: () => nowMs, |
| 55 | + enqueueSystemEvent: vi.fn(), |
| 56 | + requestHeartbeat: vi.fn(), |
| 57 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })), |
| 58 | + }); |
| 59 | + state.schedulerStarted = true; |
| 60 | + sqliteTransactionLabels.length = 0; |
| 61 | + await onTimer(state); |
| 62 | + if (state.timer) { |
| 63 | + clearTimeout(state.timer); |
| 64 | + state.timer = null; |
| 65 | + } |
| 66 | + return { |
| 67 | + jobs: state.store?.jobs ?? [], |
| 68 | + maintenanceCount: sqliteTransactionLabels.filter((label) => label === "cron.schedule-unowned") |
| 69 | + .length, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +describe("cron timer maintenance admission", () => { |
| 74 | + it.each([ |
| 75 | + { |
| 76 | + name: "stable future schedule", |
| 77 | + create: (nowMs: number) => |
| 78 | + job( |
| 79 | + "future", |
| 80 | + nowMs, |
| 81 | + { kind: "every", everyMs: 60_000, anchorMs: nowMs }, |
| 82 | + { nextRunAtMs: nowMs + 60_000 }, |
| 83 | + ), |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "valid future cron slot", |
| 87 | + create: (nowMs: number) => |
| 88 | + job( |
| 89 | + "future-cron", |
| 90 | + nowMs, |
| 91 | + { kind: "cron", expr: "0 * * * * *", tz: "UTC", staggerMs: 0 }, |
| 92 | + { nextRunAtMs: Math.floor(nowMs / 60_000) * 60_000 + 60_000 }, |
| 93 | + ), |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "event-driven schedule without a timer slot", |
| 97 | + create: (nowMs: number) => |
| 98 | + job( |
| 99 | + "stream", |
| 100 | + nowMs, |
| 101 | + { kind: "stream", command: ["true"] }, |
| 102 | + { |
| 103 | + streamSourceIdentity: "source", |
| 104 | + }, |
| 105 | + ), |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "active due schedule", |
| 109 | + create: (nowMs: number) => |
| 110 | + job( |
| 111 | + "active", |
| 112 | + nowMs, |
| 113 | + { kind: "every", everyMs: 60_000, anchorMs: nowMs - 120_000 }, |
| 114 | + { nextRunAtMs: nowMs - 60_000, runningAtMs: nowMs }, |
| 115 | + ), |
| 116 | + }, |
| 117 | + ])("skips a write sweep for $name", async ({ create }) => { |
| 118 | + const nowMs = Date.now(); |
| 119 | + const result = await runTimer([create(nowMs)], nowMs); |
| 120 | + expect(result.maintenanceCount).toBe(0); |
| 121 | + }); |
| 122 | + |
| 123 | + it("runs one sweep for a stale backoff slot", async () => { |
| 124 | + const nowMs = Date.now(); |
| 125 | + const nextRunAtMs = nowMs - 20_000; |
| 126 | + const result = await runTimer( |
| 127 | + [ |
| 128 | + job( |
| 129 | + "stale-backoff", |
| 130 | + nowMs, |
| 131 | + { kind: "every", everyMs: 60_000, anchorMs: nowMs - 120_000 }, |
| 132 | + { |
| 133 | + nextRunAtMs, |
| 134 | + lastRunAtMs: nowMs - 10_000, |
| 135 | + lastRunStatus: "error", |
| 136 | + consecutiveErrors: 1, |
| 137 | + }, |
| 138 | + ), |
| 139 | + ], |
| 140 | + nowMs, |
| 141 | + ); |
| 142 | + expect(result.maintenanceCount).toBe(1); |
| 143 | + expect(result.jobs[0]?.state.nextRunAtMs).toBeGreaterThan(nowMs); |
| 144 | + }); |
| 145 | + |
| 146 | + it("repairs a stale future cron slot with one sweep", async () => { |
| 147 | + const nowMs = Date.now(); |
| 148 | + const expected = Math.floor(nowMs / 60_000) * 60_000 + 60_000; |
| 149 | + const stale = job( |
| 150 | + "stale-future", |
| 151 | + nowMs, |
| 152 | + { kind: "cron", expr: "0 * * * * *", tz: "UTC", staggerMs: 0 }, |
| 153 | + { nextRunAtMs: nowMs + 7 * 24 * 60 * 60_000 + 30_000 }, |
| 154 | + ); |
| 155 | + stale.payload = { kind: "systemEvent", text: "repair stale future slot" }; |
| 156 | + |
| 157 | + const result = await runTimer([stale], nowMs); |
| 158 | + expect(result.maintenanceCount).toBe(1); |
| 159 | + expect(result.jobs[0]?.state.nextRunAtMs).toBe(expected); |
| 160 | + }); |
| 161 | + |
| 162 | + it("keeps retrying malformed timed schedules until the third failure disables them", async () => { |
| 163 | + const nowMs = Date.now(); |
| 164 | + let current = job( |
| 165 | + "malformed", |
| 166 | + nowMs, |
| 167 | + { kind: "cron", expr: "0 7 * * *", tz: "Invalid/Timezone" }, |
| 168 | + {}, |
| 169 | + ); |
| 170 | + |
| 171 | + for (let attempt = 1; attempt <= 3; attempt += 1) { |
| 172 | + const result = await runTimer([current], nowMs); |
| 173 | + expect(result.maintenanceCount).toBe(1); |
| 174 | + current = result.jobs[0]!; |
| 175 | + expect(current.state.scheduleErrorCount).toBe(attempt); |
| 176 | + expect(current.enabled).toBe(attempt < 3); |
| 177 | + } |
| 178 | + }); |
| 179 | +}); |
0 commit comments