Skip to content

Commit 2ba272b

Browse files
authored
improve(ci): warm import-heavy Vitest graphs (#110557)
* perf(ci): broaden Vitest transform cache seed * fix(ci): declare cache slot clone helper
1 parent 608bb3b commit 2ba272b

5 files changed

Lines changed: 88 additions & 5 deletions

File tree

‎.github/workflows/vitest-cache-warm.yml‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,33 @@ jobs:
4545
import { appendFileSync } from "node:fs";
4646
import { createNodeTestShards } from "./scripts/lib/ci-node-test-plan.mjs";
4747
48-
// The unit-fast graph is striped across jobs; warm the union of its
49-
// configs as whole suites so the seed covers every stripe's imports.
50-
const shards = createNodeTestShards().filter((candidate) =>
48+
// Warm whole configs for the striped unit-fast graph plus the import-
49+
// bound graphs that remained cold in measured protected-cache readers.
50+
const additionalShardNames = new Set([
51+
"agentic-agents-embedded",
52+
"agentic-gateway-methods",
53+
"auto-reply-reply-commands-3",
54+
]);
55+
const allShards = createNodeTestShards();
56+
const coreShards = allShards.filter((candidate) =>
5157
candidate.shardName.startsWith("core-unit-fast"),
5258
);
53-
if (shards.length === 0) {
59+
if (coreShards.length === 0) {
5460
throw new Error("core-unit-fast cache seed shards are missing");
5561
}
62+
const additionalShards = allShards.filter((candidate) =>
63+
additionalShardNames.has(candidate.shardName),
64+
);
65+
const foundAdditionalShardNames = new Set(
66+
additionalShards.map((shard) => shard.shardName),
67+
);
68+
const missingShardNames = [...additionalShardNames].filter(
69+
(name) => !foundAdditionalShardNames.has(name),
70+
);
71+
if (missingShardNames.length > 0) {
72+
throw new Error(`cache seed shards are missing: ${missingShardNames.join(", ")}`);
73+
}
74+
const shards = [...coreShards, ...additionalShards];
5675
const configs = [...new Set(shards.flatMap((shard) => shard.configs))];
5776
appendFileSync(
5877
process.env.GITHUB_ENV,

‎scripts/ci-run-node-test-shard.d.mts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export function buildChildEnv(
3535

3636
export function pruneFsModuleCache(root: string, maxBytes?: number): FsModuleCachePruneResult;
3737

38+
export function clonePersistentCacheSlots(root: string | undefined, concurrency: number): number;
39+
3840
export function resolveShardChildCommand(
3941
args: string[],
4042
nodeExecPath?: string,

‎scripts/ci-run-node-test-shard.mjs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
// list of packed group plans. Extracted from .github/workflows/ci.yml so the
33
// execution policy is unit-testable and plans can run concurrently.
44
import { spawn } from "node:child_process";
5-
import { existsSync, mkdtempSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
5+
import {
6+
constants,
7+
cpSync,
8+
existsSync,
9+
mkdtempSync,
10+
readdirSync,
11+
rmSync,
12+
statSync,
13+
unlinkSync,
14+
writeFileSync,
15+
} from "node:fs";
616
import { tmpdir } from "node:os";
717
import { join } from "node:path";
818
import { StringDecoder } from "node:string_decoder";
@@ -150,6 +160,30 @@ export function pruneFsModuleCache(root, maxBytes = FS_MODULE_CACHE_MAX_BYTES) {
150160
return { beforeBytes, afterBytes: totalBytes, removedFiles };
151161
}
152162

163+
export function clonePersistentCacheSlots(root, concurrency) {
164+
if (!root || concurrency <= 1) {
165+
return 0;
166+
}
167+
const seed = join(root, "vitest-cache-0");
168+
if (!existsSync(seed)) {
169+
return 0;
170+
}
171+
172+
let clonedSlots = 0;
173+
for (let cacheSlot = 1; cacheSlot < concurrency; cacheSlot += 1) {
174+
const destination = join(root, `vitest-cache-${cacheSlot}`);
175+
rmSync(destination, { force: true, recursive: true });
176+
// Clone before workers start. Reflinks make the common Linux path cheap;
177+
// unsupported filesystems transparently fall back to a regular copy.
178+
cpSync(seed, destination, {
179+
mode: constants.COPYFILE_FICLONE,
180+
recursive: true,
181+
});
182+
clonedSlots += 1;
183+
}
184+
return clonedSlots;
185+
}
186+
153187
const MAX_PENDING_LINE_CHARS = 1_000_000;
154188

155189
function relayChildStream(stream, label) {
@@ -227,6 +261,12 @@ export async function runShardPlans(plans, options = {}) {
227261
const scratchDir = options.scratchDir ?? mkdtempSync(join(tmpdir(), "openclaw-node-shard-"));
228262
const persistentCacheRoot = baseEnv[FS_MODULE_CACHE_PATH_ENV_KEY]?.trim();
229263
const nodeCompileCacheRoot = baseEnv[NODE_COMPILE_CACHE_PATH_ENV_KEY]?.trim();
264+
const clonedCacheSlots = clonePersistentCacheSlots(persistentCacheRoot, concurrency);
265+
if (clonedCacheSlots > 0) {
266+
process.stdout.write(
267+
`[shard:cache] cloned restored Vitest seed into ${clonedCacheSlots} isolated lane(s)\n`,
268+
);
269+
}
230270

231271
let nextIndex = 0;
232272
let exitCode = 0;

‎test/scripts/ci-run-node-test-shard.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import path from "node:path";
1414
import { afterEach, describe, expect, it } from "vitest";
1515
import {
1616
buildChildEnv,
17+
clonePersistentCacheSlots,
1718
pruneFsModuleCache,
1819
resolveShardChildCommand,
1920
resolveShardPlans,
@@ -218,6 +219,24 @@ describe("scripts/ci-run-node-test-shard.mjs", () => {
218219
]);
219220
});
220221

222+
it("clones a restored persistent seed into every concurrent cache slot", () => {
223+
const persistentRoot = makeScratchDir();
224+
const seed = path.join(persistentRoot, "vitest-cache-0");
225+
mkdirSync(seed, { recursive: true });
226+
writeFileSync(path.join(seed, "transform"), "cached", "utf8");
227+
const staleSlot = path.join(persistentRoot, "vitest-cache-1");
228+
mkdirSync(staleSlot, { recursive: true });
229+
writeFileSync(path.join(staleSlot, "stale"), "old", "utf8");
230+
231+
expect(clonePersistentCacheSlots(persistentRoot, 3)).toBe(2);
232+
for (const cacheSlot of [1, 2]) {
233+
expect(
234+
readFileSync(path.join(persistentRoot, `vitest-cache-${cacheSlot}`, "transform"), "utf8"),
235+
).toBe("cached");
236+
}
237+
expect(existsSync(path.join(staleSlot, "stale"))).toBe(false);
238+
});
239+
221240
it("prunes oldest transform entries while preserving Vitest metadata", () => {
222241
const persistentRoot = makeScratchDir();
223242
const slot = path.join(persistentRoot, "vitest-cache-0");

‎test/scripts/ci-workflow-guards.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,9 @@ describe("ci workflow guards", () => {
25272527
expect(checkoutStep.with).toBeUndefined();
25282528
expect(warmerSource).toContain('cron: "17 8 * * *"');
25292529
expect(warmerSource).toContain('candidate.shardName.startsWith("core-unit-fast")');
2530+
expect(warmerSource).toContain('"agentic-agents-embedded"');
2531+
expect(warmerSource).toContain('"agentic-gateway-methods"');
2532+
expect(warmerSource).toContain('"auto-reply-reply-commands-3"');
25302533
expect(warmerSetup.with).toMatchObject({
25312534
"node-compile-cache-scope": "test",
25322535
"save-actions-cache": "true",

0 commit comments

Comments
 (0)