From bc4be04d523df28d024b94cfea599da807ce30c3 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 5 Sep 2026 22:29:21 +0800 Subject: [PATCH] test: poll instead of fixed sleeps for debounced persist writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI Windows runners intermittently sweep %TEMP% mid-test: the debounced persist write hits a transient ENOENT on rename (tmp file or dir gone). acp-kernel (PR 201) now retries the whole write cycle on transient ENOENT/ENOTDIR, which can land the write well past the 5ms debounce — so tests must poll with a deadline instead of asserting right after a fixed settle()/sleep. - proxy-persist.test.ts: waitFor() helper; debounce test polls loadSync (deadline 5s, covers the kernel retry ladder ~1.5s with headroom). - e2e-export-handoff.test.ts: poll listSessions until the session shows the compression block (deadline 10s). Polling on length alone raced the first pre-compression snapshot and failed 'blocks >= 1'. Fixes the recurring windows-22 flakes: - proxy-persist.test.ts:109 'Cannot read properties of null' - e2e-export-handoff.test.ts:174 'expected 1 persisted session, got 0' --- tests/e2e-export-handoff.test.ts | 16 ++++++++++------ tests/proxy-persist.test.ts | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/tests/e2e-export-handoff.test.ts b/tests/e2e-export-handoff.test.ts index 10c40a7b4b..04e26807c1 100644 --- a/tests/e2e-export-handoff.test.ts +++ b/tests/e2e-export-handoff.test.ts @@ -165,12 +165,16 @@ test("e2e export: real proxy compresses via real tool call, block persists, bili `compression result not observed: bodies=${bodies.length} final=${finalText.slice(0, 120)}`, ); - // debounceMs: 0 — the debounced persistence write lands within a tick. - await new Promise((r) => setTimeout(r, 100)); - - // `bili export` reads the SAME dir with a fresh store — proving the - // on-disk file is self-sufficient, not an artifact of the live session. - const sessions = await listSessions({ dir }); + // debounceMs: 0 — the debounced write lands within a tick, BUT a + // transient ENOENT (CI Windows temp sweep) defers the flush into the + // kernel's retry ladder (~1.5s) or the spill. Poll with headroom + // instead of a fixed 100ms sleep. + const deadline = Date.now() + 10_000; + let sessions = await listSessions({ dir }); + while ((sessions.length < 1 || (sessions[0]?.blocks ?? 0) < 1) && Date.now() < deadline) { + await new Promise((r) => setTimeout(r, 25)); + sessions = await listSessions({ dir }); + } assert.equal(sessions.length, 1, `expected 1 persisted session, got ${sessions.length}: ${sessions.map((s) => s.id).join(",")}`); assert.ok(sessions[0]!.blocks >= 1, "real compression block did not persist"); diff --git a/tests/proxy-persist.test.ts b/tests/proxy-persist.test.ts index bd26f51b03..8acaa04daa 100644 --- a/tests/proxy-persist.test.ts +++ b/tests/proxy-persist.test.ts @@ -52,6 +52,21 @@ async function settle(ms = 30): Promise { await new Promise((r) => setTimeout(r, ms)); } +/** Poll probe() until it returns non-nullish, failing after deadlineMs. + * Used where a write can legitimately land late: acp-kernel retries + * transient ENOENT/ENOTDIR (CI Windows temp sweeps) with backoff, so a + * debounced flush may surface seconds after its 5ms timer — a fixed + * sleep would flake even though the data is safe. */ +async function waitFor(probe: () => T | null | undefined, deadlineMs: number, what: string): Promise { + const start = Date.now(); + for (;;) { + const v = probe(); + if (v != null) return v; + if (Date.now() - start > deadlineMs) throw new Error(`timed out after ${deadlineMs}ms waiting for ${what}`); + await settle(10); + } +} + await withTempStore("writeNow round-trips state + blockContents", async (store, dir) => { const s = makeSession("sess-1"); s.stats.requests = 42; @@ -103,9 +118,12 @@ await withTempStore("scheduleSave debounces and eventually writes", async (store store.scheduleSave(s); s.stats.requests = 3; store.scheduleSave(s); - await settle(); + // Poll rather than one fixed settle(): if the flush hits a transient + // ENOENT (temp sweep) the kernel retries with backoff, so the write can + // land well past the 5ms debounce. Deadline covers the retry ladder + // (~1.5s) with headroom. + const loaded = await waitFor(() => store.loadSync("debounce-1"), 5000, "debounced write to land"); assert.equal(readdirSync(dir).length, 1, "exactly one top-level entry (the _unknown subdir) happened"); - const loaded = store.loadSync("debounce-1"); assert.equal(loaded!.stats.requests, 3, "latest value persisted"); });