|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + createAbortAwareIsolatedRunner, |
| 4 | + createIsolatedRegressionJob, |
| 5 | + noopLogger, |
| 6 | + setupCronIssueRegressionFixtures, |
| 7 | + writeCronJobs, |
| 8 | +} from "./service.issue-regressions.test-helpers.js"; |
| 9 | +import { createCronServiceState } from "./service/state.js"; |
| 10 | +import { executeJobCore, executeJobCoreWithTimeout, onTimer } from "./service/timer.js"; |
| 11 | + |
| 12 | +describe("Cron timeout and abort fixes", () => { |
| 13 | + const { makeStorePath } = setupCronIssueRegressionFixtures(); |
| 14 | + |
| 15 | + describe("#41783 — deferred execution timeout", () => { |
| 16 | + it("does not fire the timeout during queue wait (onExecutionStart defers the clock)", async () => { |
| 17 | + vi.useRealTimers(); |
| 18 | + const store = makeStorePath(); |
| 19 | + const scheduledAt = Date.parse("2026-03-10T12:00:00.000Z"); |
| 20 | + const cronJob = createIsolatedRegressionJob({ |
| 21 | + id: "deferred-timeout", |
| 22 | + name: "deferred timeout", |
| 23 | + scheduledAt, |
| 24 | + schedule: { kind: "at", at: new Date(scheduledAt).toISOString() }, |
| 25 | + // Very short timeout (2.5ms) — if the safety backstop fires at |
| 26 | + // the old moment (before deferred arm), the job would time out |
| 27 | + // during the simulated queue wait. |
| 28 | + payload: { kind: "agentTurn", message: "work", timeoutSeconds: 0.05 }, |
| 29 | + state: { nextRunAtMs: scheduledAt }, |
| 30 | + }); |
| 31 | + await writeCronJobs(store.storePath, [cronJob]); |
| 32 | + |
| 33 | + let resolveWork: ((v: { status: "ok"; summary: string }) => void) | undefined; |
| 34 | + const workPromise = new Promise<{ status: "ok"; summary: string }>((resolve) => { |
| 35 | + resolveWork = resolve; |
| 36 | + }); |
| 37 | + |
| 38 | + let now = scheduledAt; |
| 39 | + const state = createCronServiceState({ |
| 40 | + cronEnabled: true, |
| 41 | + storePath: store.storePath, |
| 42 | + log: noopLogger, |
| 43 | + nowMs: () => now, |
| 44 | + enqueueSystemEvent: vi.fn(), |
| 45 | + requestHeartbeatNow: vi.fn(), |
| 46 | + runIsolatedAgentJob: vi.fn(async () => { |
| 47 | + // Simulate completing quickly once actually running. |
| 48 | + // The deferred timeout gives us the full 50ms budget |
| 49 | + // from when execution starts, not from enqueue. |
| 50 | + resolveWork!({ status: "ok", summary: "done" }); |
| 51 | + return workPromise; |
| 52 | + }), |
| 53 | + }); |
| 54 | + |
| 55 | + const timerPromise = onTimer(state); |
| 56 | + |
| 57 | + // Resolve the work immediately. |
| 58 | + resolveWork!({ status: "ok", summary: "done" }); |
| 59 | + await timerPromise; |
| 60 | + |
| 61 | + const job = state.store?.jobs.find((j) => j.id === "deferred-timeout"); |
| 62 | + expect(job?.state.lastStatus).toBe("ok"); |
| 63 | + expect(job?.state.lastError).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("executeJobCore calls onExecutionStart before running isolated jobs", async () => { |
| 67 | + vi.useRealTimers(); |
| 68 | + const store = makeStorePath(); |
| 69 | + const scheduledAt = Date.parse("2026-03-10T12:00:00.000Z"); |
| 70 | + const cronJob = createIsolatedRegressionJob({ |
| 71 | + id: "arm-callback-test", |
| 72 | + name: "arm callback", |
| 73 | + scheduledAt, |
| 74 | + schedule: { kind: "at", at: new Date(scheduledAt).toISOString() }, |
| 75 | + payload: { kind: "agentTurn", message: "test" }, |
| 76 | + state: { nextRunAtMs: scheduledAt }, |
| 77 | + }); |
| 78 | + await writeCronJobs(store.storePath, [cronJob]); |
| 79 | + |
| 80 | + const onExecutionStart = vi.fn(); |
| 81 | + let now = scheduledAt; |
| 82 | + const state = createCronServiceState({ |
| 83 | + cronEnabled: true, |
| 84 | + storePath: store.storePath, |
| 85 | + log: noopLogger, |
| 86 | + nowMs: () => now, |
| 87 | + enqueueSystemEvent: vi.fn(), |
| 88 | + requestHeartbeatNow: vi.fn(), |
| 89 | + runIsolatedAgentJob: vi.fn(async () => { |
| 90 | + // Verify onExecutionStart was called before we run. |
| 91 | + expect(onExecutionStart).toHaveBeenCalledTimes(1); |
| 92 | + return { status: "ok" as const, summary: "done" }; |
| 93 | + }), |
| 94 | + }); |
| 95 | + |
| 96 | + state.running = true; |
| 97 | + state.store = { version: 1, jobs: [cronJob] }; |
| 98 | + |
| 99 | + await executeJobCore(state, cronJob, undefined, onExecutionStart); |
| 100 | + |
| 101 | + expect(onExecutionStart).toHaveBeenCalledTimes(1); |
| 102 | + }); |
| 103 | + |
| 104 | + it("executeJobCore calls onExecutionStart for main session jobs", async () => { |
| 105 | + vi.useRealTimers(); |
| 106 | + const store = makeStorePath(); |
| 107 | + const scheduledAt = Date.parse("2026-03-10T12:00:00.000Z"); |
| 108 | + const mainJob = { |
| 109 | + id: "main-arm-test", |
| 110 | + name: "main arm", |
| 111 | + enabled: true, |
| 112 | + createdAtMs: scheduledAt, |
| 113 | + updatedAtMs: scheduledAt, |
| 114 | + schedule: { kind: "at" as const, at: new Date(scheduledAt).toISOString() }, |
| 115 | + sessionTarget: "main" as const, |
| 116 | + wakeMode: "next-heartbeat" as const, |
| 117 | + payload: { kind: "systemEvent" as const, text: "ping" }, |
| 118 | + state: { nextRunAtMs: scheduledAt }, |
| 119 | + }; |
| 120 | + |
| 121 | + const onExecutionStart = vi.fn(); |
| 122 | + const state = createCronServiceState({ |
| 123 | + cronEnabled: true, |
| 124 | + storePath: store.storePath, |
| 125 | + log: noopLogger, |
| 126 | + nowMs: () => scheduledAt, |
| 127 | + enqueueSystemEvent: vi.fn(), |
| 128 | + requestHeartbeatNow: vi.fn(), |
| 129 | + runIsolatedAgentJob: vi.fn().mockResolvedValue({ status: "ok" }), |
| 130 | + }); |
| 131 | + state.running = true; |
| 132 | + state.store = { version: 1, jobs: [mainJob] }; |
| 133 | + |
| 134 | + await executeJobCore(state, mainJob, undefined, onExecutionStart); |
| 135 | + |
| 136 | + expect(onExecutionStart).toHaveBeenCalledTimes(1); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe("#37505 — shared AbortController kills fallback chain", () => { |
| 141 | + it("abort signal is passed to runIsolatedAgentJob and fires on timeout", async () => { |
| 142 | + vi.useRealTimers(); |
| 143 | + const store = makeStorePath(); |
| 144 | + const scheduledAt = Date.parse("2026-03-10T12:00:00.000Z"); |
| 145 | + const cronJob = createIsolatedRegressionJob({ |
| 146 | + id: "abort-signal-test", |
| 147 | + name: "abort signal", |
| 148 | + scheduledAt, |
| 149 | + schedule: { kind: "at", at: new Date(scheduledAt).toISOString() }, |
| 150 | + payload: { kind: "agentTurn", message: "work", timeoutSeconds: 0.005 }, |
| 151 | + state: { nextRunAtMs: scheduledAt }, |
| 152 | + }); |
| 153 | + await writeCronJobs(store.storePath, [cronJob]); |
| 154 | + |
| 155 | + const abortAwareRunner = createAbortAwareIsolatedRunner(); |
| 156 | + let now = scheduledAt; |
| 157 | + const state = createCronServiceState({ |
| 158 | + cronEnabled: true, |
| 159 | + storePath: store.storePath, |
| 160 | + log: noopLogger, |
| 161 | + nowMs: () => now, |
| 162 | + enqueueSystemEvent: vi.fn(), |
| 163 | + requestHeartbeatNow: vi.fn(), |
| 164 | + runIsolatedAgentJob: vi.fn(async (params) => { |
| 165 | + const result = await abortAwareRunner.runIsolatedAgentJob(params); |
| 166 | + now += 5; |
| 167 | + return result; |
| 168 | + }), |
| 169 | + }); |
| 170 | + |
| 171 | + await onTimer(state); |
| 172 | + |
| 173 | + expect(abortAwareRunner.getObservedAbortSignal()).toBeDefined(); |
| 174 | + expect(abortAwareRunner.getObservedAbortSignal()?.aborted).toBe(true); |
| 175 | + |
| 176 | + const job = state.store?.jobs.find((j) => j.id === "abort-signal-test"); |
| 177 | + expect(job?.state.lastStatus).toBe("error"); |
| 178 | + expect(job?.state.lastError).toContain("timed out"); |
| 179 | + }); |
| 180 | + |
| 181 | + it("executeJobCoreWithTimeout still times out correctly with deferred start", async () => { |
| 182 | + vi.useRealTimers(); |
| 183 | + const store = makeStorePath(); |
| 184 | + const scheduledAt = Date.parse("2026-03-10T12:00:00.000Z"); |
| 185 | + const cronJob = createIsolatedRegressionJob({ |
| 186 | + id: "timeout-still-works", |
| 187 | + name: "timeout works", |
| 188 | + scheduledAt, |
| 189 | + schedule: { kind: "at", at: new Date(scheduledAt).toISOString() }, |
| 190 | + payload: { kind: "agentTurn", message: "slow", timeoutSeconds: 0.005 }, |
| 191 | + state: { nextRunAtMs: scheduledAt }, |
| 192 | + }); |
| 193 | + await writeCronJobs(store.storePath, [cronJob]); |
| 194 | + |
| 195 | + const abortAwareRunner = createAbortAwareIsolatedRunner(); |
| 196 | + let now = scheduledAt; |
| 197 | + const state = createCronServiceState({ |
| 198 | + cronEnabled: true, |
| 199 | + storePath: store.storePath, |
| 200 | + log: noopLogger, |
| 201 | + nowMs: () => now, |
| 202 | + enqueueSystemEvent: vi.fn(), |
| 203 | + requestHeartbeatNow: vi.fn(), |
| 204 | + runIsolatedAgentJob: abortAwareRunner.runIsolatedAgentJob, |
| 205 | + }); |
| 206 | + state.running = true; |
| 207 | + state.store = { version: 1, jobs: [cronJob] }; |
| 208 | + |
| 209 | + // executeJobCoreWithTimeout rejects the Promise.race when timeout |
| 210 | + // fires, which surfaces as a thrown error. The caller (executeJob/ |
| 211 | + // ops.ts) catches this and records it as an error status. |
| 212 | + await expect(executeJobCoreWithTimeout(state, cronJob)).rejects.toThrow("timed out"); |
| 213 | + expect(abortAwareRunner.getObservedAbortSignal()?.aborted).toBe(true); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments