Skip to content

Commit 41e36ce

Browse files
t3dotggclaude
andcommitted
fix(scheduler): webhook deliveries judge a run by its own dispatch, not the stored row
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4767782 commit 41e36ce

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ export const layer = Layer.effect(
796796
if (trigger !== "scheduled") {
797797
return yield* taskError("Schedule task is already running.", { taskId: task.id });
798798
}
799-
return { task, threadId: null };
799+
return { task, threadId: null, dispatchFailed: false };
800800
}
801801

802802
return yield* Effect.gen(function* () {
@@ -811,15 +811,19 @@ export const layer = Layer.effect(
811811
if (trigger !== "scheduled") {
812812
return yield* taskError("Schedule task not found.", { taskId: task.id });
813813
}
814-
return { task, threadId: null };
814+
return { task, threadId: null, dispatchFailed: false };
815815
}
816816
if (
817817
webhook !== undefined &&
818818
(yield* sql`SELECT 1 FROM scheduled_task_webhook_pending WHERE delivery_id = ${webhook.deliveryId} AND state = 'completed'`.pipe(
819819
NodeSqliteClient.readOnly,
820820
)).length > 0
821821
)
822-
return { task: { ...active, lastRunStatus: "succeeded" as const }, threadId: null };
822+
return {
823+
task: { ...active, lastRunStatus: "succeeded" as const },
824+
threadId: null,
825+
dispatchFailed: false,
826+
};
823827
const parsedNextRunAt =
824828
active.nextRunAt === null ? Option.none() : DateTime.make(active.nextRunAt);
825829
if (
@@ -828,7 +832,7 @@ export const layer = Layer.effect(
828832
Option.isNone(parsedNextRunAt) ||
829833
DateTime.toEpochMillis(parsedNextRunAt.value) > DateTime.toEpochMillis(startedAt))
830834
) {
831-
return { task: active, threadId: null };
835+
return { task: active, threadId: null, dispatchFailed: false };
832836
}
833837
// Reject queued deliveries after pause, same-id recreation or trigger changes.
834838
if (webhook !== undefined) {
@@ -1008,7 +1012,8 @@ export const layer = Layer.effect(
10081012
),
10091013
);
10101014
if (written) yield* notifyChanged;
1011-
return { task: recorded, threadId };
1015+
// The stored row can belong to a replacement, so report this dispatch separately.
1016+
return { task: recorded, threadId, dispatchFailed: !runSucceeded };
10121017
}).pipe(
10131018
Effect.onError((cause) =>
10141019
releaseStuckRun(task, startedAtIso, errorMessage(cause), webhook?.deliveryId),
@@ -1751,8 +1756,8 @@ export const layer = Layer.effect(
17511756
]);
17521757
const dispatched = yield* Ref.make(false);
17531758
return yield* runTask(task, "webhook", { deliveryId, prompt, dispatched }).pipe(
1754-
Effect.flatMap(({ task: completed }) =>
1755-
completed.lastRunStatus === "failed"
1759+
Effect.flatMap(({ dispatchFailed }) =>
1760+
dispatchFailed
17561761
? runOutcome("failed").pipe(
17571762
Effect.andThen(markDeliveryFailed(deliveryId, "The run failed to start.")),
17581763
Effect.andThen(releaseFailedDelivery(deliveryId)),

‎apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,32 @@ it.effect("start failures release both replay keys and relay claims for redelive
11991199
),
12001200
);
12011201

1202+
it.effect("a start failure releases the relay claim when the task row is replaced mid-run", () =>
1203+
Effect.gen(function* () {
1204+
const gate = yield* Deferred.make<void>();
1205+
yield* withService(
1206+
({ service, sql, launches }) =>
1207+
Effect.gen(function* () {
1208+
const { task } = yield* service.upsert(yield* webhookTaskInput());
1209+
const request = requestFor(task, { relayDeliveryId: "replaced-mid-run" });
1210+
const first = yield* waitForSpan(
1211+
service.triggerWebhook(request),
1212+
"ScheduledTaskService.runWebhookDelivery",
1213+
).pipe(Effect.forkChild);
1214+
yield* Queue.take(launches);
1215+
// A same-id recreate while the launch is out: the stored row is no longer this run's.
1216+
yield* sql`UPDATE scheduled_tasks SET last_run_status = 'never', last_run_at = NULL`;
1217+
yield* Deferred.succeed(gate, undefined);
1218+
yield* Fiber.join(first);
1219+
const retry = yield* service.triggerWebhook(request);
1220+
assert.equal(retry._tag === "accepted" && retry.outcome, "accepted");
1221+
yield* Queue.take(launches);
1222+
}),
1223+
{ gate, failLaunches: { remaining: 1 } },
1224+
);
1225+
}),
1226+
);
1227+
12021228
it.effect("main retention keeps fifty runs and preserves filtered and dedupe buckets", () =>
12031229
withService(({ service, sql, launches }) =>
12041230
Effect.gen(function* () {

0 commit comments

Comments
 (0)