From dc4548232347b2bbb638928e4e773523ab10cdbd Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 26 Oct 2026 21:07:37 -0500 Subject: [PATCH] Verify recovery joins foreground effects independently of scan timing Observe committed-claim discovery before advancing its deadline. Assert pending recovery, one effect lock attempt, one result/wake and eventual quiet; verify the test rejects missing flight joining. Co-Authored-By: Paperclip --- doc/idle-decision-effect-recovery.md | 29 ++++++++++++ .../decision-effect-recovery-work.test.ts | 47 ++++++++++++++++--- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/doc/idle-decision-effect-recovery.md b/doc/idle-decision-effect-recovery.md index dfdcfeb3fd2..1bed1a3ae8f 100644 --- a/doc/idle-decision-effect-recovery.md +++ b/doc/idle-decision-effect-recovery.md @@ -104,3 +104,32 @@ text for cursor comparisons, leaving business grace deadlines unchanged. The invalid claim remains durable debt and is revisited after paging wraps; the valid claim progresses without repeating its completed effect. All 18 effect-recovery lifecycle cases and server typechecking pass after repair. + +### Foreground join test timing + +CI exposed a timing assumption in the foreground/recovery overlap test: it +expected exactly two lifetime recovery sweeps. A controlled PostgreSQL replay +waited for the committed claim's deadline discovery before advancing time. It +recorded two completed, zero-resumption scans before the deadline (startup and +claim notification), followed by the legitimate deadline scan. The old assertion +then failed deterministically with three calls. No recurring idle poll or duplicate +effect was found in that sequence. + +The test now waits for discovery, measures the deadline recovery relative to that +point, and proves that its promise stays pending while foreground execution holds +the effect lock. Advancing through several retry periods cannot start another +sweep or effect. After release, recovery completes, one comment and one continuation +wake exist, and the worker performs no SQL through the existing hour-long quiet +check. Counting actual lock attempts also distinguishes joining from a second +execution blocked on the same PostgreSQL advisory lock. A temporary mutation that +removed flight joining made this stronger assertion fail; production code was +restored before final verification. No runtime code, retry policy, timeout, or +business deadline changed. + +All 123 tests across the four complete effect-recovery, decision-service, expiry, +and startup suites passed, followed by server typechecking. Evidence: +`/tmp/sleep-effect-join-red.log`, `/tmp/sleep-effect-join-green.log`, +`/tmp/sleep-effect-join-mutation.log`, `/tmp/sleep-effect-join-owning-tests.log`, +and `/tmp/sleep-effect-join-root-types.log` with their status files. The initial +failure and mutation failure are expected evidence, not passing runs. Remaining +review findings and fresh stack CI remain separate acceptance requirements. diff --git a/server/src/__tests__/decision-effect-recovery-work.test.ts b/server/src/__tests__/decision-effect-recovery-work.test.ts index 058780b4bf3..03d88143860 100644 --- a/server/src/__tests__/decision-effect-recovery-work.test.ts +++ b/server/src/__tests__/decision-effect-recovery-work.test.ts @@ -136,23 +136,56 @@ const support = externalUrl ? { supported: true } : await getEmbeddedPostgresTes it("joins active foreground effects when their recovery deadline arrives", async () => { const row = await create(); await start(); + const effectKey = `decision-effect:${row.id}:0`; + const previousDebug = db.$client.options.debug; + let effectLockAttempts = 0; + db.$client.options.debug = (_connection, query, parameters) => { + if (query.includes("pg_advisory_xact_lock") && parameters.includes(effectKey)) effectLockAttempts++; + }; let release!: () => void, locked!: () => void; const gate = new Promise(resolve => { release = resolve; }); const acquired = new Promise(resolve => { locked = resolve; }); const lock = db.transaction(async tx => { - await tx.execute(sql`select pg_advisory_xact_lock(hashtextextended(${`decision-effect:${row.id}:0`}, 0))`); + await tx.execute(sql`select pg_advisory_xact_lock(hashtextextended(${effectKey}, 0))`); locked(); await gate; }); - await acquired; - const deciding = decide(row.id); + let deciding: ReturnType | undefined; + let recovering: ReturnType | undefined; try { + await acquired; + deciding = decide(row.id); await vi.waitFor(async () => expect((await service().get(row.id))?.executionStatus).toBe("running")); + const claimed = (await service().get(row.id))!; + const deadline = claimed.updatedAt.getTime() + 60_000; + // Let the committed-claim discovery finish before advancing to its deadline. + // Startup and notification scans are separate from the recovery we test. + await vi.waitFor(() => expect(coordinator!.nextWakeAt()).toBe(deadline)); + await vi.waitFor(() => expect(effectLockAttempts).toBe(2)); // Test gate + foreground execution. + const sweeps = vi.mocked(recovery.recoverEffects); + const beforeDeadline = sweeps.mock.calls.length; + await vi.advanceTimersByTimeAsync(Math.max(0, deadline - Date.now())); + await vi.waitFor(() => expect(sweeps).toHaveBeenCalledTimes(beforeDeadline + 1)); + let recoverySettled = false; + recovering = Promise.resolve(sweeps.mock.results[beforeDeadline].value).then(result => { + recoverySettled = true; + return result; + }); await vi.advanceTimersByTimeAsync(120_000); expect(await comments()).toHaveLength(0); expect(wake).not.toHaveBeenCalled(); - await vi.waitFor(() => expect(recovery.recoverEffects).toHaveBeenCalledTimes(2)); - } finally { release(); } - await lock; await deciding; await quiet(); - expect(await comments()).toHaveLength(1); expect(wake).toHaveBeenCalledTimes(1); + expect(recoverySettled).toBe(false); + expect(sweeps).toHaveBeenCalledTimes(beforeDeadline + 1); + expect(effectLockAttempts).toBe(2); + release(); + await lock; await deciding; + await expect(recovering).resolves.toEqual({ resumed: 1 }); + await quiet(); + expect(effectLockAttempts).toBe(2); + expect(await comments()).toHaveLength(1); expect(wake).toHaveBeenCalledTimes(1); + } finally { + release(); + await Promise.allSettled([lock, deciding, recovering]); + db.$client.options.debug = previousDebug; + } }); it("defers an outer transaction until commit and ignores rollback", async () => {