@devries-cameron20 / t3code Mirror · read-only / commits / cffcb76986
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 04:15 UTC

fix(relay): stop held webhook deliveries when the inbox is cleared (#18838)

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com>

Alex Southwell committed Oct 19, 2026 at 06:17 UTC cffcb76986d5005c9da31227c83be90059335ba2
2 files changed +51
infra/relay/src/hooks/HookInboxStore.test.ts
+37
@@ -2,8 +2,10 @@ import { describe, expect, it } from "@effect/vitest";
2 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Clock from "effect/Clock";
4 import * as DateTime from "effect/DateTime";
5 +import * as Deferred from "effect/Deferred";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 +import * as Fiber from "effect/Fiber";
9 import * as SqlClient from "effect/sql/SqlClient";
10 import * as TestClock from "effect/testing/TestClock";
11
@@ -42,6 +44,41 @@ const withInbox = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
44 );
45
46 describe("HookInboxStore", () => {
47 + it.effect.each(["delivered", "busy", "unreachable"] as const)(
48 + "stops a prefetched batch cleared during a %s send",
49 + (outcome) =>
50 + withInbox(
51 + Effect.gen(function* () {
52 + for (const id of ["first", "second", "third"]) {
53 + yield* HookInboxStore.hold(yield* hook(id), BASE_URL);
54 + }
55 + const started = yield* Deferred.make<void>();
56 + const released = yield* Deferred.make<void>();
57 + const sent: Array<string> = [];
58 + const run = yield* HookInboxStore.deliverDue((_baseUrl, held) =>
59 + Effect.gen(function* () {
60 + sent.push(held.id);
61 + if (held.id === "first") {
62 + yield* Deferred.succeed(started, undefined);
63 + yield* Deferred.await(released);
64 + }
65 + return outcome;
66 + }),
67 + ).pipe(Effect.forkChild);
68 + yield* Deferred.await(started);
69 + yield* HookInboxStore.clear;
70 + yield* Deferred.succeed(released, undefined);
71 + const nextAt = yield* Fiber.join(run);
72 + expect(sent).toEqual(["first"]);
73 + // The alarm must not be rescheduled, even if the in-flight send failed.
74 + expect(nextAt).toBeNull();
75 + const sql = yield* SqlClient.SqlClient;
76 + expect(yield* sql`SELECT id FROM held_hooks`).toEqual([]);
77 + expect(yield* sql`SELECT id FROM held_hooks_target`).toEqual([]);
78 + }),
79 + ),
80 + );
81 +
82 it.effect("delivers held requests oldest first, byte for byte", () =>
83 withInbox(
84 Effect.gen(function* () {
infra/relay/src/hooks/HookInboxStore.ts
+14
@@ -293,6 +293,10 @@ export const deliverDue = Effect.fn("HookInboxStore.deliverDue")(function* <R>(
293 "relay.inbox.held_bytes": backlog.bytes,
294 });
295 });
296 + const stillHeld = (seq: number) =>
297 + sql<{ readonly seq: number }>`SELECT seq FROM held_hooks WHERE seq = ${seq}`.pipe(
298 + Effect.map((rows) => rows.length > 0),
299 + );
300 // Pages run out only once every non-busy request has been tried, so a
301 // backlog behind busy hooks never hides another hook's requests.
302 pages: while (batch.length > 0) {
@@ -300,6 +304,11 @@ export const deliverDue = Effect.fn("HookInboxStore.deliverDue")(function* <R>(
304 if (sent === DELIVERIES_PER_RUN) break pages;
305
306 if (busyHooks.has(row.hook_key)) continue;
307 + // Clear can discard this prefetched page while an upstream send is in flight.
308 + if (!(yield* stillHeld(row.seq))) {
309 + yield* annotateRun("cleared");
310 + return null;
311 + }
312 const headers = decodeHeaders(row.headers);
313 if (Option.isNone(headers)) {
314 // Unreadable: it can never be delivered, and must not block the rest.
@@ -319,6 +328,11 @@ export const deliverDue = Effect.fn("HookInboxStore.deliverDue")(function* <R>(
328 headers: headers.value,
329 body: row.body,
330 });
331 + // A cleared request must neither advance this run nor schedule a retry.
332 + if (!(yield* stillHeld(row.seq))) {
333 + yield* annotateRun("cleared");
334 + return null;
335 + }
336 if (outcome === "unreachable") {
337 failures = yield* countFailure;
338 yield* annotateRun("unreachable");