Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/scheduler/__tests__/scheduler-hardening.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,40 @@ describe("Phase 2.5 scheduler fixes", () => {
expect(after?.lastDeliveryStatus).toContain("ECONNREFUSED");
expect(after?.lastRunStatus).toBe("ok");
});

test("a pauseJob() that lands mid-run is honored, not stomped by the write-back (#148)", async () => {
const runtime = createMockRuntime();
const scheduler = new Scheduler({ db, runtime: runtime as never });

const job = scheduler.createJob({
name: "Pause Mid Run",
schedule: { kind: "every", intervalMs: 60_000 },
task: "x",
});

// Simulate a pauseJob() landing while handleMessage is in flight: the
// status flips to 'paused' after executeJob captured its 'active'
// snapshot at pickup. Before the fix, the unconditional write-back
// stomped this back to 'active' and the next fire ran anyway.
runtime.handleMessage.mockImplementation(async () => {
db.run("UPDATE scheduled_jobs SET status = 'paused' WHERE id = ?", [job.id]);
return {
text: "Mock response",
sessionId: "mock-session",
cost: { totalUsd: 0, inputTokens: 0, outputTokens: 0, modelUsage: {} },
durationMs: 10,
};
});

await scheduler.runJobNow(job.id);

const after = scheduler.getJob(job.id);
// The concurrent pause must survive: status stays 'paused' and the
// recurring job gets no next fire scheduled. resumeJob() recomputes
// next_run_at when the operator un-pauses.
expect(after?.status).toBe("paused");
expect(after?.nextRunAt).toBeNull();
});
});

// ---------- M1: non-blocking missed-job recovery ----------
Expand Down
17 changes: 17 additions & 0 deletions src/scheduler/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ export async function executeJob(job: ScheduledJob, ctx: ExecutorContext): Promi
});
}

// handleMessage for an agent task runs for minutes, and pauseJob() can land
// during that window. We computed newStatus from the job snapshot taken at
// pickup, so an unconditional write-back would silently stomp a concurrent
// pause. Re-read the live status and honor an external pause. Only the
// 'active' continuation case is at risk: 'completed' and 'failed' are this
// run's terminal outcomes and must win. resumeJob() recomputes next_run_at,
// so clearing it here is safe.
if (newStatus === "active") {
const live = ctx.db.query("SELECT status FROM scheduled_jobs WHERE id = ?").get(job.id) as {
status: string;
} | null;
if (live?.status === "paused") {
newStatus = "paused";
nextRunAt = null;
}
}

// Runtime safety net for OOS#4.
if (!JOB_STATUS_VALUES.includes(newStatus)) {
throw new Error(`refusing to write invalid status '${newStatus}' for job ${job.id}`);
Expand Down
Loading