Skip to content

Commit 4eb9915

Browse files
fix(server): rotation and the delivery cap respect recreated tasks
- Rotating a token matches the task's created_at, so it cannot land on a task deleted and recreated under the same id. - Deleting a task clears its queued-delivery count. - A request refused by the queue cap is not logged, so it cannot push real deliveries out of the log. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d26f14c commit 4eb9915

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,12 @@ export const layer = Layer.effect(
10611061
next.delete(input.id);
10621062
return next;
10631063
}),
1064+
// Old deliveries still finishing must not fill a recreated task's queue.
1065+
Ref.update(webhookQueued, (queued) => {
1066+
const next = new Map(queued);
1067+
next.delete(input.id);
1068+
return next;
1069+
}),
10641070
]),
10651071
),
10661072
Effect.andThen(notifyChanged),
@@ -1092,15 +1098,21 @@ export const layer = Layer.effect(
10921098
}
10931099
const token = yield* newWebhookToken;
10941100
const now = yield* localNow;
1095-
yield* sql`
1101+
// Matching created_at keeps a rotation from landing on a task deleted
1102+
// and recreated under the same id since it was loaded.
1103+
const updated = yield* sql<{ task_id: string }>`
10961104
UPDATE scheduled_tasks
10971105
SET webhook_token = ${token}, updated_at = ${iso(now)}
1098-
WHERE task_id = ${input.id}
1106+
WHERE task_id = ${input.id} AND created_at = ${task.createdAt}
1107+
RETURNING task_id
10991108
`.pipe(
11001109
Effect.mapError((cause) =>
11011110
taskError("Could not rotate webhook token.", { taskId: input.id, cause }),
11021111
),
11031112
);
1113+
if (updated.length === 0) {
1114+
return yield* taskError("Schedule task was deleted or replaced.", { taskId: input.id });
1115+
}
11041116
yield* notifyChanged;
11051117
return { task: yield* loadTask(input.id) };
11061118
});
@@ -1341,30 +1353,28 @@ export const layer = Layer.effect(
13411353
}
13421354

13431355
const rendered = renderWebhookPrompt(task.prompt, request);
1344-
yield* log("accepted", {
1345-
signatureVerified: signature !== null,
1346-
missing: rendered.missing,
1347-
renderedPrompt: rendered.prompt,
1348-
});
1349-
// Bound the deliveries waiting behind a slow run, so steady traffic to
1350-
// a stuck task cannot pile up parked fibers.
1356+
// Bound the deliveries one task holds, so steady traffic to a stuck
1357+
// task cannot pile up parked fibers. A refused request is not logged,
1358+
// so it cannot push real deliveries out of the log.
13511359
const queued = yield* Ref.modify(webhookQueued, (counts) => {
13521360
const count = counts.get(task.id) ?? 0;
13531361
return count >= WEBHOOK_MAX_QUEUED_PER_TASK
13541362
? ([false, counts] as const)
13551363
: ([true, new Map(counts).set(task.id, count + 1)] as const);
13561364
});
1357-
if (!queued) {
1358-
yield* markDeliveryFailed(deliveryId, "Too many deliveries are waiting for this task.");
1359-
return { _tag: "rate_limited" as const };
1360-
}
1365+
if (!queued) return { _tag: "rate_limited" as const };
13611366
const release = Ref.update(webhookQueued, (counts) => {
13621367
const next = new Map(counts);
13631368
const count = (next.get(task.id) ?? 1) - 1;
13641369
if (count <= 0) next.delete(task.id);
13651370
else next.set(task.id, count);
13661371
return next;
13671372
});
1373+
yield* log("accepted", {
1374+
signatureVerified: signature !== null,
1375+
missing: rendered.missing,
1376+
renderedPrompt: rendered.prompt,
1377+
}).pipe(Effect.onError(() => release));
13681378
const permit = yield* webhookPermit(task.id);
13691379
yield* runTask(task, "webhook", { deliveryId, prompt: rendered.prompt }).pipe(
13701380
Effect.flatMap((completed) =>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Effect from "effect/Effect";
88
import * as Layer from "effect/Layer";
99
import * as Queue from "effect/Queue";
1010
import * as Schema from "effect/Schema";
11+
import * as TestClock from "effect/testing/TestClock";
1112

1213
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
1314
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
@@ -402,6 +403,9 @@ it.effect("caps deliveries waiting behind a stuck run", () =>
402403
);
403404
assert.equal(waiting.filter((result) => result._tag === "accepted").length, 19);
404405
assert.equal(waiting.at(-1)?._tag, "rate_limited");
406+
// The refused request is not logged.
407+
const logged = (yield* service.listWebhookDeliveries({ id: task.id })).deliveries;
408+
assert.equal(logged.length, 20);
405409
yield* Deferred.succeed(gate, undefined);
406410
}),
407411
{ gate },

0 commit comments

Comments
 (0)