Skip to content

Commit f6d4ecc

Browse files
committed
fix(cron): re-anchor every-job to edit time on a genuine interval change
Only inherit the previous cadence anchor for an unchanged-interval every re-save. A real everyMs change (UI omits anchorMs) now re-anchors to the edit time, matching the prior update semantics, while idempotent re-saves still preserve a due slot and phase. Adds a regression test for the interval-change path.
1 parent e967035 commit f6d4ecc

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

‎src/cron/service.update-recompute-drops-due-every.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,52 @@ describe("update() must not drop a due every-job's pending run", () => {
7373
cron.stop();
7474
});
7575

76+
it("re-anchors an every-job to the edit time when the interval actually changes", async () => {
77+
const store = await makeStorePath();
78+
const base = Date.parse("2025-12-13T00:00:00.000Z");
79+
80+
const finished = createFinishedBarrier();
81+
const cron = new CronService({
82+
storePath: store.storePath,
83+
cronEnabled: true,
84+
log: noopLogger,
85+
enqueueSystemEvent: vi.fn(),
86+
requestHeartbeat: vi.fn(),
87+
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" as const })),
88+
onEvent: finished.onEvent,
89+
});
90+
91+
await cron.start();
92+
93+
const job = await cron.add({
94+
name: "every 10s",
95+
enabled: true,
96+
schedule: { kind: "every", everyMs: 10_000 },
97+
sessionTarget: "isolated",
98+
wakeMode: "next-heartbeat",
99+
payload: { kind: "agentTurn", message: "tick" },
100+
});
101+
const jobId = job.id;
102+
expect(job.schedule).toMatchObject({ kind: "every", anchorMs: base });
103+
104+
// User edits the interval from 10s to 1h. The control UI omits the internal
105+
// anchorMs, so the new cadence must start from the edit time rather than
106+
// keeping the old phase; nextRunAtMs is one new interval from now.
107+
const editTime = base + 3_000;
108+
vi.setSystemTime(new Date(editTime));
109+
await cron.update(jobId, { schedule: { kind: "every", everyMs: 3_600_000 } });
110+
111+
const current = (await cron.list({ includeDisabled: true })).find((j) => j.id === jobId)!;
112+
expect(current.schedule).toMatchObject({
113+
kind: "every",
114+
everyMs: 3_600_000,
115+
anchorMs: editTime,
116+
});
117+
expect(current.state.nextRunAtMs).toBe(editTime + 3_600_000);
118+
119+
cron.stop();
120+
});
121+
76122
it("preserves a due cron-job nextRunAtMs on an idempotent schedule re-save", async () => {
77123
const store = await makeStorePath();
78124
vi.setSystemTime(new Date("2025-12-13T08:59:00.000Z"));

‎src/cron/service/ops.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,15 @@ export async function update(state: CronServiceState, id: string, patch: CronJob
525525
if (nextJob.schedule.kind === "every") {
526526
const anchor = nextJob.schedule.anchorMs;
527527
if (typeof anchor !== "number" || !Number.isFinite(anchor)) {
528-
// Inherit the previous cadence anchor when an `every` re-save omits it
529-
// (UIs resubmit the schedule without the internal anchorMs). Without
530-
// this the edit re-phases the job to now, shifting every future fire
531-
// time and skipping an already-due slot.
528+
// Inherit the previous cadence anchor only for an unchanged-interval
529+
// re-save (UIs resubmit the schedule without the internal anchorMs).
530+
// Without this an idempotent edit re-phases the job to now, shifting
531+
// every future fire time and skipping an already-due slot. A genuine
532+
// interval change still anchors to the edit time so the new cadence
533+
// starts now, matching the prior update semantics.
532534
const previousAnchorMs =
533535
job.schedule.kind === "every" &&
536+
job.schedule.everyMs === nextJob.schedule.everyMs &&
534537
typeof job.schedule.anchorMs === "number" &&
535538
Number.isFinite(job.schedule.anchorMs)
536539
? job.schedule.anchorMs

0 commit comments

Comments
 (0)