Skip to content

The open-source app everyone uses to manage agents at work

Preserve saved delivery deadlines across clock ticks (#16589)

Nicky Leachd1463fb79ecd
diff --git a/server/src/__tests__/delivery-work-coordinator.test.ts b/server/src/__tests__/delivery-work-coordinator.test.ts
index b4406a7cd5..d3f7590a79 100644
--- a/server/src/__tests__/delivery-work-coordinator.test.ts
+++ b/server/src/__tests__/delivery-work-coordinator.test.ts
@@ -45,6 +45,18 @@ describe("delivery work coordinator", () => {
expect(vi.getTimerCount()).toBe(0);
expect(idleWorkSnapshot().active).toBe(0);
});
+ it("preserves a future deadline when the clock advances between scheduling reads", async () => {
+ const s = setup(), t = task();
+ let now = Date.now();
+ const due = now + 1000;
+ const clock = vi.spyOn(Date, "now").mockImplementation(() => now++);
+ try {
+ await s.coordinator.register(DELIVERY_QUEUES.chatGitHubPublications, { ...t, nextRunAt: () => due }).ready;
+ expect(s.coordinator.nextWakeAt()).toBe(due);
+ expect(t.run).toHaveBeenCalledTimes(1);
+ expect(s.onError).not.toHaveBeenCalled();
+ } finally { clock.mockRestore(); }
+ });
it("schedules maintenance without holding idle intent, and lets new work preempt it", async () => {
const s = setup(), t = task();
let next: number | null = Date.now() + 60_000;
diff --git a/server/src/services/delivery-work-coordinator.ts b/server/src/services/delivery-work-coordinator.ts
index c8c9a468bf..2efb832250 100644
--- a/server/src/services/delivery-work-coordinator.ts
+++ b/server/src/services/delivery-work-coordinator.ts
@@ -35,9 +35,9 @@ export function createDeliveryWorkCoordinator(input: {
let workerStopped = false;
let controller: AbortController | null = null;
let finishIntent: (() => void) | undefined;
- function schedule(delay: number) {
+ function schedule(delay: number, deadline?: number) {
if (workerStopped) return;
- const at = Date.now() + delay;
+ const at = deadline ?? Date.now() + delay;
if (scheduledAt !== null && scheduledAt <= at) return;
scheduledAt = at;
scheduler.schedule(queue, at, start);
@@ -58,7 +58,7 @@ export function createDeliveryWorkCoordinator(input: {
const next = task.nextRunAt?.();
if (next != null) {
const delay = Math.max(0, next - Date.now());
- schedule(delay === 0 && !input.canRun() ? task.retryMs : delay);
+ schedule(delay === 0 && !input.canRun() ? task.retryMs : delay, delay > 0 ? next : undefined);
}
}).catch(error => {
schedule(task.retryMs);