From 3be313f634dfcd3dcf35248e404120f6dd070424 Mon Sep 17 00:00:00 2001 From: Will Jones Date: Thu, 17 Sep 2026 13:45:35 -0700 Subject: [PATCH] test(node): fix flaky optimize cleanup test (#4217) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backports the test fix from #4183 (commit `21ecafe9a` on `main`) to `release/v0.39`. ## Root cause `when optimizing a dataset > cleanups old versions` took its cutoff with `new Date()`. A JS `Date` has millisecond precision; Lance stores version timestamps with nanosecond precision. If the cutoff landed in the same millisecond as the preceding commit, truncating to milliseconds put the cutoff *before* that commit, so the version was not eligible for pruning and `stats.prune.oldVersionsRemoved` came back as `1` instead of `2`. This became possible on this branch with #4160, which changed `cleanupOlderThan` to be passed to the native binding as an absolute timestamp. It is timing- and machine-dependent, which is why it shows up on the fast Linux runners and not locally. ## Fix Wait for the clock to tick over to the next millisecond before capturing the cutoff, so the cutoff is strictly later than every commit the test made. Same change that is already on `main`. ## Observed failures - https://github.com/lancedb/lancedb/actions/runs/34945208311/job/104323950514 — scheduled NPM Publish on `main` at `ffe94a65a`, before #4183 landed. - https://github.com/lancedb/lancedb/actions/runs/35257750543 — #4216 (`update-lance-12.0.0-v0.39`), which targets this branch and so does not have the fix. ## Verification This is a flake, so there is no deterministic local repro to show red-then-green. The change is byte-identical to the one merged in #4183; `main` has had three green NPM Publish runs since it landed, and no recurrence of this test on `main`. Note: `delete unverified`, immediately below, still uses `new Date()` for its cutoff. It asserts `toBeGreaterThan(1)` rather than an exact count, so it has one version of slack, but it is the same latent race. Left alone here to keep this a faithful backport — happy to harden it on `main` and backport that too. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5 (1M context) --- nodejs/__test__/table.test.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index ac74c65ef..852bdec80 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -2766,6 +2766,15 @@ describe("when dealing with tags", () => { }); }); +/** Returns a Date strictly later than every instant observed before the call. */ +async function nextMillisecond(): Promise { + const start = Date.now(); + while (Date.now() <= start) { + await new Promise((resolve) => setTimeout(resolve, 1)); + } + return new Date(); +} + describe("when optimizing a dataset", () => { let tmpDir: tmp.DirResult; let table: Table; @@ -2788,7 +2797,12 @@ describe("when optimizing a dataset", () => { }); it("cleanups old versions", async () => { - const stats = await table.optimize({ cleanupOlderThan: new Date() }); + // Lance stores version timestamps with nanosecond precision while a JS + // Date only has millisecond precision. A cutoff captured in the same + // millisecond as the last commit would truncate to *before* that commit + // and leave it in place, so wait for the clock to tick over first. + const cutoff = await nextMillisecond(); + const stats = await table.optimize({ cleanupOlderThan: cutoff }); expect(stats.prune.bytesRemoved).toBeGreaterThan(0); expect(stats.prune.oldVersionsRemoved).toBe(2); });