From 2b8d0be7299cc7137fb1cfb5c261a5c24b4287ce Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 24 Oct 2026 20:43:56 +0800 Subject: [PATCH] fix(ci): a verified pretest-cache HIT must also touch dist mtimes to now (#4548) --- scripts/ci-pretest-cache.mjs | 25 ++++++++++++++++++++- tests/ci-pretest-cache.test.ts | 40 +++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/scripts/ci-pretest-cache.mjs b/scripts/ci-pretest-cache.mjs index 33b155d760..0da08a4275 100644 --- a/scripts/ci-pretest-cache.mjs +++ b/scripts/ci-pretest-cache.mjs @@ -32,7 +32,7 @@ import { createHash } from "node:crypto"; import { execFileSync } from "node:child_process"; -import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, utimesSync, writeFileSync } from "node:fs"; import { join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; @@ -128,6 +128,28 @@ export function markerIsFresh(distDir, hash) { } } +/** + * A verified-fresh dist must also be MTIME-fresh (#4548): the actions/cache + * restore brings dist files back with their archived mtimes (older than this + * run's checkout), so every mtime-based freshness consumer — the #3325 + * golden-drive guard, tsup/esbuild incrementals, anything else — sees a stale + * dist even though the marker just proved its bytes fresh. Refresh the whole + * dist tree to "now" on every HIT; on MISS the real build already does the + * equivalent implicitly. + */ +function touchTree(dir) { + const now = new Date(); + const walk = (d) => { + for (const e of readdirSync(d, { withFileTypes: true })) { + const p = join(d, e.name); + if (e.isDirectory()) walk(p); + else if (e.isFile()) utimesSync(p, now, now); + } + utimesSync(d, now, now); + }; + walk(dir); +} + /** * The pretest loop. For each component: hash → fresh marker? HIT, skip : * MISS → run the real build → stamp the marker. `build`/`log` are test seams @@ -144,6 +166,7 @@ export function runPretestCache(opts = {}) { const hash = hashComponent(root, comp); const distDir = join(root, comp.dir, "dist"); if (markerIsFresh(distDir, hash)) { + touchTree(distDir); log(`pretest-cache: HIT (${comp.name}) — dist fresh for hash ${hash.slice(0, 12)}`); results.push({ name: comp.name, hit: true, ms: 0 }); continue; diff --git a/tests/ci-pretest-cache.test.ts b/tests/ci-pretest-cache.test.ts index cb76d9d917..8529a5db7c 100644 --- a/tests/ci-pretest-cache.test.ts +++ b/tests/ci-pretest-cache.test.ts @@ -1,6 +1,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { mkdtempSync, mkdirSync, readFileSync, statSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import * as cache from "../scripts/ci-pretest-cache.mjs"; @@ -163,6 +163,44 @@ test("a missing input is a hard error, never a silent HIT", () => { assert.throws(() => cache.hashComponent(root, broken), /does-not-exist\.ts/); }); +test("a HIT must also be mtime-fresh — a cache-restored dist with archived old timestamps gets touched to now (#4548)", () => { + // Reproduces the master-red incident: actions/cache restores dist bytes + // with their archived mtimes (older than this checkout), the #3325 + // golden-drive guard compares mtimes, and a verified-fresh-but-old dist + // is wrongly refused. The HIT path must refresh the whole dist tree. + const { root, write } = makeMiniRepo(); + const seen: string[] = []; + cache.runPretestCache({ root, build: fakeBuild(seen) }); + + // Simulate the actions/cache restore: dist bytes valid, mtimes archived + // in the past — and prove the precondition actually looks stale first. + write("kernel/src/index.ts", 'export const v = "kernel";\n'); // same bytes + const old = new Date(Date.now() - 3_600_000); + utimesSync(join(root, "kernel", "dist", "index.js"), old, old); + utimesSync(join(root, "kernel", "dist"), old, old); + utimesSync(join(root, "kernel", "dist", cache.MARKER_NAME), old, old); + // Model the checkout clock: sources checked out ~1s after the cache was + // archived — strictly newer than the restored dist, older than the touch. + const srcAt = new Date(Date.now() - 1_000); + utimesSync(join(root, "kernel", "src", "index.ts"), srcAt, srcAt); + assert.ok( + statSync(join(root, "kernel", "dist", "index.js")).mtimeMs < srcAt.getTime(), + "precondition: restored dist must look mtime-stale before the run", + ); + + const r = cache.runPretestCache({ root, build: fakeBuild(seen) }); + assert.equal(r.find((x) => x.name === "kernel")?.hit, true); + assert.equal(seen.length, 2, "HIT must not rebuild"); + assert.ok( + statSync(join(root, "kernel", "dist", "index.js")).mtimeMs > srcAt.getTime(), + "HIT must touch the dist tree past the sources (#3325 guard invariant)", + ); + assert.ok( + statSync(join(root, "kernel", "dist")).mtimeMs > srcAt.getTime(), + "the dist directory itself is touched too", + ); +}); + test("build failure propagates (CLI exit path) and stamps no marker", () => { const { root } = makeMiniRepo(); const boom = (): void => {