From dc4548232347b2bbb638928e4e773523ab10cdbd Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 26 Oct 2026 21:07:37 -0500 Subject: [PATCH 1/2] 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 () => { From 6b9874f9b8045f7d1c8463eb2547fe5e056910fb Mon Sep 17 00:00:00 2001 From: Dotta Date: Mon, 26 Oct 2026 21:10:06 -0500 Subject: [PATCH 2/2] Carry login cleanup fixture settlement through the whole stack Backport the existing deadline-aware settlement helper so earlier PRs no longer leave fake-time cleanup work paused beyond their initial clock jump. All sixteen login lifecycle cases pass. Co-Authored-By: Paperclip --- doc/idle-login-cleanup.md | 17 +++++++++++++++++ server/src/__tests__/login-cleanup-work.test.ts | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/idle-login-cleanup.md b/doc/idle-login-cleanup.md index 813cf34eb85..5bc3ebf62ac 100644 --- a/doc/idle-login-cleanup.md +++ b/doc/idle-login-cleanup.md @@ -95,3 +95,20 @@ cover the surrounding login contracts. The fake-clock database fixture warms its connections and disables pool idle/lifetime expiry so time jumps test the worker rather than racing socket connection timers. Database suspension and reconnect behavior require separate qualification. + +## CI fixture settlement across the migration stack + +The setup-token consume fixture can finish its initial fake-clock jump while +cleanup is still performing database work. A write during that pass can request +another generation whose coalesced deadline lies beyond the jump. Waiting for +zero tracked work without advancing to that registered deadline then times out, +even though cleanup has a valid wake and can finish. + +The existing fixture repair from `59f8c8b3e5` is now carried at the earliest stack +branch so every intermediate PR receives it. The helper advances to registered +deadlines only while work remains tracked, preserving its existing five-second +observation limit and all production behavior. All 16 login-cleanup cases passed +on this branch against a dedicated PostgreSQL database; the prior later-branch +verification and failed CI history remain documented in the GitHub review-check +migration audit. No runtime change or weakened quiet assertion is included. +Evidence: `/tmp/sleep-login-settlement-owning-tests.log` and its zero status file. diff --git a/server/src/__tests__/login-cleanup-work.test.ts b/server/src/__tests__/login-cleanup-work.test.ts index c21d04c97d3..081fc6eece6 100644 --- a/server/src/__tests__/login-cleanup-work.test.ts +++ b/server/src/__tests__/login-cleanup-work.test.ts @@ -116,7 +116,13 @@ describe.skipIf(!support.supported)("durable login cleanup", () => { }); async function settle(ms = 30_000) { await vi.advanceTimersByTimeAsync(ms); - await vi.waitFor(() => expect(idleWorkSnapshot().active).toBe(0), { timeout: 5000 }); + await vi.waitFor(async () => { + if (idleWorkSnapshot().active > 0) { + const deadlines = coordinators.flatMap(c => c.nextWakeAt() === null ? [] : [c.nextWakeAt()!]); + if (deadlines.length) await vi.advanceTimersByTimeAsync(Math.max(0, Math.min(...deadlines) - Date.now())); + } + expect(idleWorkSnapshot().active).toBe(0); + }, { timeout: 5000 }); await vi.advanceTimersByTimeAsync(1); await vi.waitFor(() => expect(idleWorkSnapshot().active).toBe(0), { timeout: 5000 }); expect(errors).toEqual([]);