diff --git a/packages/db/src/work-signals.test.ts b/packages/db/src/work-signals.test.ts index aabf0529911..b46e2c74b08 100644 --- a/packages/db/src/work-signals.test.ts +++ b/packages/db/src/work-signals.test.ts @@ -472,6 +472,22 @@ describe("database work scope identity", () => { } finally { await other.close(); } }); + it("scopes an unhinted signal alongside a hinted one to the hinted set, locally and in another process", async () => { + const other = receiving(["alongside"]); + const local: Array = []; + const stop = subscribeDatabaseWork(db, "alongside", (event, hints) => { if (event === "settled") local.push(hints ? [...hints] : null); }); + try { + const result = await run(root => root.transaction(async tx => { + await signalDatabaseWork(tx, "alongside"); + await signalDatabaseWork(tx, "alongside", "company-1"); + })); + expect(local).toEqual([["company-1"]]); + expect(result.payloads.map(payload => JSON.parse(payload).hints)).toEqual([["company-1"]]); + other.deliver(result.payloads); + expect(other.wakes).toEqual([["alongside", ["company-1"]]]); + } finally { stop(); await other.close(); } + }); + it("splits many hints into payloads an older receiver still reads, within the per-topic byte cap", async () => { const hints = Array.from({ length: 80 }, (_, index) => JSON.stringify({ companyId: `company-${String(index).padStart(4, "0")}` })); const other = receiving(["many"]); diff --git a/server/src/__tests__/delivery-work-coordinator.test.ts b/server/src/__tests__/delivery-work-coordinator.test.ts index 6fb1ca3471f..7cacd3f681f 100644 --- a/server/src/__tests__/delivery-work-coordinator.test.ts +++ b/server/src/__tests__/delivery-work-coordinator.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { installDatabaseWorkSignals } from "../../../packages/db/src/work-signals.js"; +import { installDatabaseWorkSignals, publishDatabaseWorkSettled } from "../../../packages/db/src/work-signals.js"; import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js"; import { notifyDeliveryWork, DELIVERY_QUEUES } from "../services/delivery-work-notifications.js"; import { beginIdleTrackedWork, idleWorkSnapshot, isIdleTaskDrainActive, readTaskDrain, startTaskDrain, stopTaskDrain } from "../services/task-admission.js"; @@ -399,3 +399,67 @@ it("invalidates owner reconciliation synchronously before awaiting an active sto finish(); await worker.ready; await stopping; expect(stopped).toBe(true); }); + +describe("settled hints a task cannot apply", () => { + function settledFixture(onSettled: (hints?: ReadonlySet) => void) { + const s = setup(); + const t = { ...task(), onSettled: vi.fn(onSettled) }; + const worker = s.coordinator.register(DELIVERY_QUEUES.feedback, t); + return { s, t, worker }; + } + const received = (mock: ReturnType) => mock.mock.calls.map(([hints]) => hints === undefined ? null : [...hints as ReadonlySet]); + + it("still wakes the task, unscoped, and logs a warning when it rejects a hint", async () => { + const warn = vi.spyOn(process, "emitWarning").mockImplementation(() => {}); + try { + const { s, t, worker } = settledFixture(hints => { + if (hints && [...hints].some(hint => !hint.startsWith("company:"))) throw new Error("unknown hint shape"); + }); + await worker.ready; + t.run.mockClear(); + publishDatabaseWorkSettled(s.owner, DELIVERY_QUEUES.feedback, "{\"newShape\":1}"); + await vi.advanceTimersByTimeAsync(1); + expect(received(t.onSettled)).toEqual([["{\"newShape\":1}"], null]); + expect(t.run).toHaveBeenCalledOnce(); + expect(warn).toHaveBeenCalledOnce(); + expect(String(warn.mock.calls[0]![0])).toContain(`Delivery settlement could not be applied for ${DELIVERY_QUEUES.feedback}`); + expect(s.onError).not.toHaveBeenCalled(); + } finally { warn.mockRestore(); } + }); + + it("wakes the task once without retrying when even the unscoped settlement throws", async () => { + const warn = vi.spyOn(process, "emitWarning").mockImplementation(() => {}); + try { + const { s, t, worker } = settledFixture(() => { throw new Error("broken"); }); + await worker.ready; + t.run.mockClear(); + publishDatabaseWorkSettled(s.owner, DELIVERY_QUEUES.feedback, "company:1"); + await vi.advanceTimersByTimeAsync(1); + expect(received(t.onSettled)).toEqual([["company:1"], null]); + expect(t.run).toHaveBeenCalledOnce(); + expect(warn).toHaveBeenCalledTimes(2); + t.onSettled.mockClear(); warn.mockClear(); + publishDatabaseWorkSettled(s.owner, DELIVERY_QUEUES.feedback); + await vi.advanceTimersByTimeAsync(1); + expect(received(t.onSettled)).toEqual([null]); + expect(t.run).toHaveBeenCalledTimes(2); + expect(warn).toHaveBeenCalledOnce(); + await vi.advanceTimersByTimeAsync(60_000); + expect(t.run).toHaveBeenCalledTimes(2); + } finally { warn.mockRestore(); } + }); + + it("passes recognised hints through once, without a warning", async () => { + const warn = vi.spyOn(process, "emitWarning").mockImplementation(() => {}); + try { + const { s, t, worker } = settledFixture(() => {}); + await worker.ready; + t.run.mockClear(); + publishDatabaseWorkSettled(s.owner, DELIVERY_QUEUES.feedback, "company:1"); + await vi.advanceTimersByTimeAsync(1); + expect(received(t.onSettled)).toEqual([["company:1"]]); + expect(t.run).toHaveBeenCalledOnce(); + expect(warn).not.toHaveBeenCalled(); + } finally { warn.mockRestore(); } + }); +}); diff --git a/server/src/services/delivery-work-coordinator.ts b/server/src/services/delivery-work-coordinator.ts index 7512f3768fd..a9bd83e5cd0 100644 --- a/server/src/services/delivery-work-coordinator.ts +++ b/server/src/services/delivery-work-coordinator.ts @@ -110,6 +110,12 @@ export function createDeliveryWorkCoordinator(input: { dirty = true; if (!running) schedule(task.coalesceMs ?? 0); } + function applySettled(hints?: ReadonlySet) { + for (const attempt of hints === undefined ? [undefined] : [hints, undefined]) { + try { task.onSettled?.(attempt); return; } + catch (error) { process.emitWarning(`Delivery settlement could not be applied for ${queue}: ${String(error)}`); } + } + } const unsubscribe = subscribeDatabaseWork(input.owner, queue, (event, hints) => { if (workerStopped) return; if (databaseWorkPending(input.owner, queue)) { @@ -118,7 +124,7 @@ export function createDeliveryWorkCoordinator(input: { // replaces this deadline with an immediate wake on the normal path. if (!running) schedule(task.retryMs); } - if (event === "settled") { task.onSettled?.(hints); wake(); } + if (event === "settled") { applySettled(hints); wake(); } }); const worker = { wake, async stop() { workerStopped = true;