From ab56393accc26ae14f8ee5de3ef8d1342efe872a Mon Sep 17 00:00:00 2001 From: Dotta Date: Tue, 27 Oct 2026 13:06:36 -0500 Subject: [PATCH] Keep healthy receivers moving after a slow failed batch Co-Authored-By: Paperclip --- ...-25-chat-receiver-discovery-sleep-audit.md | 23 +++++++++++++++++++ .../src/__tests__/chat-receiver-work.test.ts | 11 ++++++--- .../chat-channels/runtime/receiver-work.ts | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/doc/plans/2026-10-25-chat-receiver-discovery-sleep-audit.md b/doc/plans/2026-10-25-chat-receiver-discovery-sleep-audit.md index 34bd6d4dc65..a662168c458 100644 --- a/doc/plans/2026-10-25-chat-receiver-discovery-sleep-audit.md +++ b/doc/plans/2026-10-25-chat-receiver-discovery-sleep-audit.md @@ -248,3 +248,26 @@ full-refresh failure/backoff case. Server typecheck and build pass. Prior broad consumer/workspace verification remains recorded above; these 45 overlap that coverage and are not an additional unique total. Publication requires fresh remote CI and review. The full migration inventory remains outstanding. + +## Healthy receiver progress after a slow failed batch + +The receiver worker already stores a retry deadline for each failed receiver and +scope. It now supplies that deadline calculation through the coordinator's +`nextRunAtOnError` contract as well. A completed batch can take longer than the +one-second batching interval. When that batch contained an error, the coordinator +previously ignored an already-due healthy backlog and waited for its generic +30-second retry. The changed registration lets healthy due entries proceed while +the failed receiver retains its own exponential backoff. + +The existing 105-receiver PostgreSQL regression now runs with both an immediate +failure and a controlled 1,100 ms elapsed interval in the failed provider call. +The latter failed before the repair: its next wake was 30 seconds away although +healthy receivers were due. Afterward, all 105 are attempted before retrying the +failed receiver, which is attempted once during the healthy drain. Recovery then +settles and two former poll periods add no SQL. The assertion helper advances its +own fake clock; the test no longer adds an extra second on every assertion retry. + +All 20 receiver lifecycle cases and the server production typecheck pass on the +owning branch. This changes one coordinator registration and adds no provider +branch, queue, table, or independent timer. Current-head CI and combined-stack +verification remain separate gates. diff --git a/server/src/__tests__/chat-receiver-work.test.ts b/server/src/__tests__/chat-receiver-work.test.ts index 83aff022265..19279e91193 100644 --- a/server/src/__tests__/chat-receiver-work.test.ts +++ b/server/src/__tests__/chat-receiver-work.test.ts @@ -192,15 +192,20 @@ const support = external ? { supported: true } : await getEmbeddedPostgresTestSu await endpoint(); await settled(); expect(reconcile).toHaveBeenCalledOnce(); expect(errors.length).toBeGreaterThan(0); errors.length = 0; await quiet(); }); - it("loads inventory in bounded pages and drains a backlog despite one failed receiver", async () => { + it.each([0, 1100])("drains paged inventory despite a failed receiver after %i ms", async delay => { for (let i = 0; i < 105; i++) await endpoint(); const service = chatReceiverSchedule(db, { reconcile, stop }); const reads = vi.spyOn(db, "select"); const inventory = await service.load({ kind: "company", id: companyId }); expect(inventory).toHaveLength(105); expect(reads).toHaveBeenCalledTimes(2); reads.mockRestore(); const failedId = inventory[0].id; - reconcile.mockImplementation(async id => { if (id === failedId) throw new Error("bad credential"); return "owned"; }); + reconcile.mockImplementation(async id => { + if (id === failedId) { vi.setSystemTime(Date.now() + delay); throw new Error("bad credential"); } + return "owned"; + }); await start(service); - await vi.waitFor(async () => { await vi.advanceTimersByTimeAsync(1000); expect(new Set(reconcile.mock.calls.map(([id]) => id)).size).toBe(105); }, { timeout: 5000 }); + expect(coordinator!.nextWakeAt()).not.toBeNull(); + expect(coordinator!.nextWakeAt()!).toBeLessThanOrEqual(Date.now() + 1000); + await vi.waitFor(() => expect(new Set(reconcile.mock.calls.map(([id]) => id)).size).toBe(105), { timeout: 5000 }); expect(reconcile.mock.calls.filter(([id]) => id === failedId)).toHaveLength(1); reconcile.mockResolvedValue("owned"); await settled(); errors.length = 0; await quiet(); }); diff --git a/server/src/services/chat-channels/runtime/receiver-work.ts b/server/src/services/chat-channels/runtime/receiver-work.ts index ad62f0baa06..c7bb909b162 100644 --- a/server/src/services/chat-channels/runtime/receiver-work.ts +++ b/server/src/services/chat-channels/runtime/receiver-work.ts @@ -83,6 +83,7 @@ export function registerChatReceiverWork( // The coordinator independently fences unresolved database transactions. hasPending: async () => canRun() && dirty.size > 0, nextRunAt: next, + nextRunAtOnError: next, }); return { ready: worker.ready, changed(id: string) { dirty.add(receiverScopeHint({ kind: "endpoint", id })); worker.wake();