@@ -49,6 +49,8 @@ export const WEBHOOK_ROUTE_PREFIX = "/api/hooks"; |
49 | 49 | const WEBHOOK_DELIVERY_RETENTION = 50; |
50 | 50 | /** Body text kept in the delivery log. Larger bodies are cut and flagged. */ |
51 | 51 | const WEBHOOK_DELIVERY_LOG_BODY_LIMIT = 64 * 1024; |
| 52 | +/** Deliveries one task may hold at once, running or waiting their turn. */ |
| 53 | +const WEBHOOK_MAX_QUEUED_PER_TASK = 20; |
52 | 54 | /** Accepted deliveries per task per minute, enforced here as well as on the relay because the tunnel hostname is public too. */ |
53 | 55 | const WEBHOOK_RATE_LIMIT_PER_MINUTE = 60; |
54 | 56 |
|
@@ -346,6 +348,7 @@ export const layer = Layer.effect( |
346 | 348 | const webhookPermits = yield* Ref.make<ReadonlyMap<ScheduledTaskId, Semaphore.Semaphore>>( |
347 | 349 | new Map(), |
348 | 350 | ); |
| 351 | + const webhookQueued = yield* Ref.make<ReadonlyMap<ScheduledTaskId, number>>(new Map()); |
349 | 352 | const webhookRateWindows = yield* Ref.make<ReadonlyMap<ScheduledTaskId, RateWindow>>(new Map()); |
350 | 353 | const activeRuns = yield* Ref.make<ReadonlySet<ScheduledTaskId>>(new Set()); |
351 | 354 | // Sliding(1) coalesces the dirty-signal: every notification triggers a |
@@ -462,7 +465,12 @@ export const layer = Layer.effect( |
462 | 465 | const saveTask = ( |
463 | 466 | task: ScheduledTask, |
464 | 467 | requireExisting: boolean, |
465 | | - webhook: { readonly token: string | null; readonly secret: string | null }, |
| 468 | + webhook: { |
| 469 | + readonly token: string | null; |
| 470 | + readonly secret: string | null; |
| 471 | + /** False when the save carried no new secret, so a concurrent change survives. */ |
| 472 | + readonly secretChanged: boolean; |
| 473 | + }, |
466 | 474 | ) => |
467 | 475 | sql<{ task_id: string }>` |
468 | 476 | INSERT INTO scheduled_tasks ( |
@@ -535,7 +543,10 @@ export const layer = Layer.effect( |
535 | 543 | WHEN excluded.webhook_token IS NULL THEN NULL |
536 | 544 | ELSE COALESCE(scheduled_tasks.webhook_token, excluded.webhook_token) |
537 | 545 | END, |
538 | | - webhook_secret = excluded.webhook_secret |
| 546 | + webhook_secret = CASE |
| 547 | + WHEN ${webhook.secretChanged ? 1 : 0} = 1 THEN excluded.webhook_secret |
| 548 | + ELSE scheduled_tasks.webhook_secret |
| 549 | + END |
539 | 550 | RETURNING task_id |
540 | 551 | `.pipe( |
541 | 552 | Effect.mapError((cause) => |
@@ -565,17 +576,22 @@ export const layer = Layer.effect( |
565 | 576 | // a completing run cannot resurrect a deleted task or clobber concurrent |
566 | 577 | // edits to the task definition. |
567 | 578 | const markRunning = (id: ScheduledTaskId, startedAtIso: string) => |
568 | | - sql` |
| 579 | + sql<{ task_id: string }>` |
569 | 580 | UPDATE scheduled_tasks |
570 | 581 | SET updated_at = ${startedAtIso}, |
571 | 582 | last_run_at = ${startedAtIso}, |
572 | 583 | last_run_status = 'running', |
573 | 584 | last_run_error = NULL |
574 | 585 | WHERE task_id = ${id} |
| 586 | + RETURNING task_id |
575 | 587 | `.pipe( |
576 | 588 | Effect.mapError((cause) => |
577 | 589 | taskError("Could not mark schedule task as running.", { taskId: id, cause }), |
578 | 590 | ), |
| 591 | + // A task deleted after the re-read must not be dispatched from the stale snapshot. |
| 592 | + Effect.flatMap((rows) => |
| 593 | + rows.length > 0 ? Effect.void : taskError("Schedule task not found.", { taskId: id }), |
| 594 | + ), |
579 | 595 | ); |
580 | 596 |
|
581 | 597 | const markCompleted = (input: { |
@@ -959,14 +975,16 @@ export const layer = Layer.effect( |
959 | 975 | input.schedule.type === "webhook" ? input.schedule.signature : null; |
960 | 976 | const secret = |
961 | 977 | signature == null ? null : (signature.secret ?? existing?.secret ?? null); |
| 978 | + const secretChanged = |
| 979 | + signature == null || signature.secret !== undefined || existing === null; |
962 | 980 | if (signature != null && secret === null) { |
963 | 981 | return yield* taskError("A webhook signature check needs a signing secret.", { |
964 | 982 | taskId: id, |
965 | 983 | }); |
966 | 984 | } |
967 | | - return { token, secret }; |
| 985 | + return { token, secret, secretChanged }; |
968 | 986 | }) |
969 | | - : { token: null, secret: null }; |
| 987 | + : { token: null, secret: null, secretChanged: true }; |
970 | 988 | const scheduleUnchanged = |
971 | 989 | existingTask !== null && |
972 | 990 | existingTask.enabled === input.enabled && |
@@ -1152,7 +1170,10 @@ export const layer = Layer.effect( |
1152 | 1170 | readonly missing: ReadonlyArray<string>; |
1153 | 1171 | readonly renderedPrompt: string | null; |
1154 | 1172 | }) => { |
1155 | | - const truncated = input.request.bodyText.length > WEBHOOK_DELIVERY_LOG_BODY_LIMIT; |
| 1173 | + const truncated = input.request.body.byteLength > WEBHOOK_DELIVERY_LOG_BODY_LIMIT; |
| 1174 | + const loggedBody = truncated |
| 1175 | + ? new TextDecoder().decode(input.request.body.subarray(0, WEBHOOK_DELIVERY_LOG_BODY_LIMIT)) |
| 1176 | + : input.request.bodyText; |
1156 | 1177 | return sql |
1157 | 1178 | .withTransaction( |
1158 | 1179 | Effect.gen(function* () { |
@@ -1167,7 +1188,7 @@ export const layer = Layer.effect( |
1167 | 1188 | SELECT |
1168 | 1189 | ${input.id}, ${input.taskId}, ${input.receivedAt}, ${input.request.method}, |
1169 | 1190 | ${input.request.query}, ${encodeHeadersJson(redactHeaders(input.request.headers))}, |
1170 | | - ${truncated ? input.request.bodyText.slice(0, WEBHOOK_DELIVERY_LOG_BODY_LIMIT) : input.request.bodyText}, |
| 1191 | + ${loggedBody}, |
1171 | 1192 | ${input.request.body.byteLength}, ${truncated ? 1 : 0}, ${input.outcome}, |
1172 | 1193 | ${input.signatureVerified ? 1 : 0}, ${encodeMissingFieldsJson(input.missing)}, |
1173 | 1194 | ${input.renderedPrompt}, NULL |
@@ -1325,6 +1346,25 @@ export const layer = Layer.effect( |
1325 | 1346 | missing: rendered.missing, |
1326 | 1347 | renderedPrompt: rendered.prompt, |
1327 | 1348 | }); |
| 1349 | + // Bound the deliveries waiting behind a slow run, so steady traffic to |
| 1350 | + // a stuck task cannot pile up parked fibers. |
| 1351 | + const queued = yield* Ref.modify(webhookQueued, (counts) => { |
| 1352 | + const count = counts.get(task.id) ?? 0; |
| 1353 | + return count >= WEBHOOK_MAX_QUEUED_PER_TASK |
| 1354 | + ? ([false, counts] as const) |
| 1355 | + : ([true, new Map(counts).set(task.id, count + 1)] as const); |
| 1356 | + }); |
| 1357 | + if (!queued) { |
| 1358 | + yield* markDeliveryFailed(deliveryId, "Too many deliveries are waiting for this task."); |
| 1359 | + return { _tag: "rate_limited" as const }; |
| 1360 | + } |
| 1361 | + const release = Ref.update(webhookQueued, (counts) => { |
| 1362 | + const next = new Map(counts); |
| 1363 | + const count = (next.get(task.id) ?? 1) - 1; |
| 1364 | + if (count <= 0) next.delete(task.id); |
| 1365 | + else next.set(task.id, count); |
| 1366 | + return next; |
| 1367 | + }); |
1328 | 1368 | const permit = yield* webhookPermit(task.id); |
1329 | 1369 | yield* runTask(task, "webhook", { deliveryId, prompt: rendered.prompt }).pipe( |
1330 | 1370 | Effect.flatMap((completed) => |
@@ -1340,6 +1380,7 @@ export const layer = Layer.effect( |
1340 | 1380 | ), |
1341 | 1381 | ), |
1342 | 1382 | permit.withPermits(1), |
| 1383 | + Effect.ensuring(release), |
1343 | 1384 | Effect.forkIn(serviceScope), |
1344 | 1385 | ); |
1345 | 1386 | return { _tag: "accepted" as const, deliveryId }; |
|