diff --git a/doc/idle-decision-effect-recovery.md b/doc/idle-decision-effect-recovery.md index e7a724e12f6..b486ae55433 100644 --- a/doc/idle-decision-effect-recovery.md +++ b/doc/idle-decision-effect-recovery.md @@ -12,13 +12,20 @@ The existing decision row is durable truth. A row with `status = decided` and supported; invalid or negative values retain the existing default. Reads do not move the timestamp. Open decisions and completed history create no recovery debt. -The existing `notifyDecisionChange` helper adds the recovery topic. Creation, -bundle creation, foreground running claims, effect completion, archive completion -and failure, cancellation, dismissal, expiry, and continuation acknowledgements -already pass through that boundary. Notifications are awaited before writes in -the existing transaction. Outer commit activates work; rollback does not publish -a claim. The additional topic does not wrap provider work or agent wakes in SQL -transactions and does not introduce a new queue table. +The existing `writeDecisionChange` transaction boundary signals effect recovery +for running claims, effect/archive settlement, cancellation, dismissal and expiry. +The generic `notifyDecisionChange` helper signals continuation and expiry only; +open-decision creation does not owe effect recovery. Notifications are awaited +before writes. Outer commit activates work; rollback does not publish a claim. +The additional topic does not wrap provider work or agent wakes in SQL transactions +and does not introduce a new queue table. + +Committed notification bursts coalesce for up to one second after a successful +pass. This bounds sequential tenant writes to at most one notification-driven +global pass per window, rather than only coalescing overlapping scans. A saved +recovery deadline can preempt that window. New obligations discovered by a signal +can wait up to one second; foreground effects still execute immediately. The +window alone never schedules work when there is no notification or saved debt. The supported writer model is the application DB signal owner and its child pools. Independent SQL clients and multiple active writers need a separate notification @@ -178,3 +185,21 @@ separate requirements; these results do not prove host suspension. The owning branch also passes all 129 cases across the four complete recovery, decision-service, expiry, and startup suites. Database-backed suites use separate migrated disposable databases. See `/tmp/sleep-effect-review-owner-acceptance.log`. + + +### Bounded notification review repair + +Ten sequential real cancellation transactions reproduced ten extra recovery +scans. After enabling the existing shared notification-coalescing capability, +the same transactions cause one follow-up scan and then return to SQL quiet. +A separate real-database case preserves an already loaded deadline 500 milliseconds +away. The initial deadline fixture observed the scheduler while its SQL pass was +still active; waiting for that read removed the fixture race before the baseline +was recorded. The cancellation amplification still failed before the production fix. + +All 131 tests in the four complete recovery, decision-service, expiry and startup +suites pass at the owning branch, as does server typechecking. The helper's optional +coalescing capability is promoted from the later plugin scheduling slice into this +PR; consumers that omit it keep their previous behavior. The integrated stack is +verified separately before publication. The original full-suite and external-wake +limitations remain. diff --git a/server/src/__tests__/decision-effect-recovery-work.test.ts b/server/src/__tests__/decision-effect-recovery-work.test.ts index 00ec33b51ae..13fae784094 100644 --- a/server/src/__tests__/decision-effect-recovery-work.test.ts +++ b/server/src/__tests__/decision-effect-recovery-work.test.ts @@ -118,6 +118,46 @@ const support = externalUrl ? { supported: true } : await getEmbeddedPostgresTes expect(sweeps).not.toHaveBeenCalled(); }); + it("coalesces sequential committed cancellations without an idle recovery poll", async () => { + const rows = []; + for (let i = 0; i < 10; i++) rows.push(await create()); + // Freeze wall time while real SQL and transaction settlement continue. + vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout", "setInterval", "clearInterval"], shouldAdvanceTime: false }); + await start(); + const scans = vi.mocked(recovery.recoverEffects); + expect(scans).toHaveBeenCalledTimes(1); + for (const row of rows) { + await service().cancel(row.id, { actorType: "user", actorId: userId }); + await vi.advanceTimersByTimeAsync(1); + await db.execute(sql`select pg_sleep(0.01)`); // Allow each old scan to finish, rather than only testing overlap coalescing. + } + expect(scans).toHaveBeenCalledTimes(1); + await vi.advanceTimersByTimeAsync(1000); + await vi.waitFor(() => expect(scans).toHaveBeenCalledTimes(2)); + await quiet(); + const count = scans.mock.calls.length; + await vi.advanceTimersByTimeAsync(3_600_000); + expect(scans).toHaveBeenCalledTimes(count); + expect(wake).toHaveBeenCalledTimes(10); + }); + + it("lets an already saved recovery deadline preempt notification coalescing", async () => { + const due = await create(), cancelled = await create(); + vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout", "setInterval", "clearInterval"], shouldAdvanceTime: false }); + const deadline = Date.now() + 500; + await claim(due.id, new Date(deadline - 60_000)); + await start(); + expect(coordinator!.nextWakeAt()).toBe(deadline); + await service().cancel(cancelled.id, { actorType: "user", actorId: userId }); + await vi.advanceTimersByTimeAsync(1); + await db.execute(sql`select pg_sleep(0.01)`); + expect(coordinator!.nextWakeAt()).toBe(deadline); + await vi.advanceTimersByTimeAsync(499); + await vi.waitFor(async () => expect(await comments()).toHaveLength(1)); + await settled(due.id); await quiet(); + expect(wake).toHaveBeenCalledTimes(2); + }); + it("retries an old failure while newer due claims keep filling pages", async () => { const failed = await create(), healthy = await create(); const at = Date.now() - 600_000; diff --git a/server/src/services/decision-effect-recovery-work.ts b/server/src/services/decision-effect-recovery-work.ts index 2db639049ec..c34ef251a7c 100644 --- a/server/src/services/decision-effect-recovery-work.ts +++ b/server/src/services/decision-effect-recovery-work.ts @@ -10,6 +10,7 @@ export function registerDecisionEffectRecoveryWork( ) { return registerDeadlineWork(coordinator, DELIVERY_QUEUES.decisionEffectRecovery, { retryMs: 30_000, canRun, + notificationCoalesceMs: 1000, run: signal => service.recoverEffects(new Date(), undefined, signal), next: service.nextEffectRecoveryAt, });