Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ concurrency:
jobs:
# Three-layer CI (#3295) plan: decide the matrix scope BEFORE paying for
# legs. One cheap ubuntu job picks one of three scopes:
# core — every PR (push) and master push: 6 test legs (ubuntu 22 full 3-shard
# suite, ubuntu 24 single-leg whole suite, windows 22 2-shard) + 2
# core — every PR (push) and master push: 9 test legs (all three combos at
# /3 shards: ubuntu 22, ubuntu 24, windows 22 — #4351 rebalance) + 2
# static legs (ubuntu 24, windows 22 — #3406 item 8: the windows
# static leg keeps typecheck/build + the kernel/pi-subagents
# suites covered on windows for EVERY PR, not only ci:full/nightly).
# The blocking layer; branch protection requires exactly
# these 6 test leg names.
# The blocking layer. master's required-checks list still carries
# the legacy 12 FULL-matrix leg names; core produces 9 of them (the
# win node-24 legs appear only in FULL scope), enforce_admins off.
# full — `ci:full` label on a PR, a release PR (#3688: bot-authored
# one-click fallback PRs, *_release-v* head branches, or a
# "[PR] release…"/"release vN…" title — no label needed),
Expand Down Expand Up @@ -110,13 +111,24 @@ jobs:
FULL_INPUT: ${{ inputs.full }}
run: |
set -euo pipefail
# #4351: the suite grew ~399 -> ~860 files in three weeks while the
# core shape stayed frozen at its ~650-file design point (#3295), so
# its heaviest legs became the wall-clock floor: the ubuntu-24
# single-leg whole suite measured ~21 min and the windows 2-shard
# halves ~13-17 min (up to ~35 min tails on flake re-runs). All three
# combos now shard /3: partitionAll stays total for any i/N, so
# union of shards == every file — zero coverage loss; wall drops to
# the slowest shard (~7-12 min). The win node-24 legs stay FULL-only.
CORE_TEST='{"include":[
{"os":"ubuntu-latest","node-version":22,"shard":1,"shardTotal":3},
{"os":"ubuntu-latest","node-version":22,"shard":2,"shardTotal":3},
{"os":"ubuntu-latest","node-version":22,"shard":3,"shardTotal":3},
{"os":"ubuntu-latest","node-version":24,"shard":1,"shardTotal":1},
{"os":"windows-latest","node-version":22,"shard":1,"shardTotal":2},
{"os":"windows-latest","node-version":22,"shard":2,"shardTotal":2}]}'
{"os":"ubuntu-latest","node-version":24,"shard":1,"shardTotal":3},
{"os":"ubuntu-latest","node-version":24,"shard":2,"shardTotal":3},
{"os":"ubuntu-latest","node-version":24,"shard":3,"shardTotal":3},
{"os":"windows-latest","node-version":22,"shard":1,"shardTotal":3},
{"os":"windows-latest","node-version":22,"shard":2,"shardTotal":3},
{"os":"windows-latest","node-version":22,"shard":3,"shardTotal":3}]}'
CORE_STATIC='{"include":[{"os":"ubuntu-latest","node-version":24},{"os":"windows-latest","node-version":22}]}'
FULL_TEST='{"include":[
{"os":"ubuntu-latest","node-version":22,"shard":1,"shardTotal":3},
Expand Down Expand Up @@ -212,9 +224,10 @@ jobs:
# across more legs: each runs a balanced slice (scripts/run-ci-tests.mjs partitions
# deterministically + totally, so union of shards == allFiles, nothing skipped,
# same tests/assertions/env, c=4 within each shard). Wall drops ~3x (~20 -> ~7min)
# with zero coverage loss. To go faster, add entries to the FULL_TEST include
# list in the plan job above (each entry carries its own shardTotal, so the
# legacy "bump the /3" coupling is gone — any denominator works).
# with zero coverage loss. To go faster, add entries to the include list of
# the scope you want faster in the plan job above (each entry carries its own
# shardTotal, so the legacy "bump the /3" coupling is gone — any denominator
# works; #4351 used exactly this knob on CORE_TEST).
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.plan.outputs.testMatrix) }}
Expand All @@ -225,6 +238,29 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: npm
# #4360 lever 1: pretest dist content-hash cache. kernel/ and
# pi-subagents/ dist are pure functions of (src tree, build config,
# manifests, lockfiles); the pretest hook (scripts/ci-pretest-cache.mjs)
# stamps each dist with a marker of that hash and skips the rebuild on
# a byte-exact match, saving the ~5-6.5 min Windows build per leg.
# Correctness never trusts the cache: marker mismatch => real rebuild,
# so a dirty/stale restore at worst wastes one build, never runs wrong
# artifacts. Per-OS keys (never share dists across platforms); no
# restore-keys — a near-miss restore is worthless because the marker
# check rejects it anyway. The post step saves automatically (on the
# exact primary key, job success only) AFTER the pretest build inside
# run-ci-tests.mjs below. The hashFiles patterns MUST stay in sync with
# the input tables in scripts/ci-pretest-cache.mjs.
- name: Restore kernel dist cache (#4360)
uses: actions/cache@v4
with:
path: kernel/dist
key: pretest-dist-kernel-v1-${{ runner.os }}-${{ hashFiles('kernel/src/**', 'kernel/tsup.config.ts', 'kernel/tsconfig.json', 'kernel/package.json', 'kernel/package-lock.json', 'package-lock.json') }}
- name: Restore pi-subagents dist cache (#4360)
uses: actions/cache@v4
with:
path: pi-subagents/dist
key: pretest-dist-pisub-v1-${{ runner.os }}-${{ hashFiles('pi-subagents/src/**', 'pi-subagents/tsup.config.ts', 'pi-subagents/tsconfig.json', 'pi-subagents/package.json', 'package-lock.json') }}
- run: npm install --ignore-scripts --no-fund --no-audit
# #2816: explicit file concurrency=4 for the whole suite. Baseline logs
# show the old default (availableParallelism()-1) already ran several
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"check:unused-exports": "node scripts/check-unused-exports.mjs",
"check:test-import-graph": "node scripts/check-test-import-graph.mjs",
"check:dist-thin": "node scripts/check-dist-thin-install.mjs",
"pretest": "node scripts/test-suite-lock.mjs gate && npm --prefix kernel run build && npm --prefix pi-subagents run build",
"pretest": "node scripts/test-suite-lock.mjs gate && node scripts/ci-pretest-cache.mjs",
"test": "node scripts/test-suite-lock.mjs run",
"start": "node dist/index.js",
"test:e2e": "node --import tsx --test tests/e2e/e2e-codex.test.ts",
Expand Down
22 changes: 22 additions & 0 deletions scripts/ci-pretest-cache.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
interface PretestCacheComponent {
name: string;
dir: string;
inputs: string[];
}

interface PretestCacheResult {
name: string;
hit: boolean;
ms: number;
}

export const MARKER_NAME: string;
export const COMPONENTS: PretestCacheComponent[];
export function hashComponent(root: string, comp: PretestCacheComponent, platform?: string): string;
export function markerIsFresh(distDir: string, hash: string): boolean;
export function runPretestCache(opts?: {
root?: string;
components?: PretestCacheComponent[];
log?: (line: string) => void;
build?: (comp: PretestCacheComponent, root: string) => void;
}): PretestCacheResult[];
197 changes: 197 additions & 0 deletions scripts/ci-pretest-cache.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#!/usr/bin/env node
// #4360 lever 1: content-hash cache for the pretest dist builds.
//
// Every CI test leg used to fully rebuild kernel/ and pi-subagents/ dist
// (~5-6.5 min on Windows each leg) inside `npm run pretest` before a single
// test ran — yet those dists are a PURE FUNCTION of the source tree, the
// build config (tsup/tsc), the manifests that carry the build script chains
// (+ CURRENT_VERSION, inlined by pi-subagents' tsup), and the lockfiles that
// pin the toolchain (tsup/esbuild/typescript). This script makes that
// function explicit: after a REAL build it stamps each dist with a marker
// holding the sha256 of its inputs; on later runs the marker is verified
// BYTE-EXACTLY against the live source hash and the rebuild is skipped only
// on a match.
//
// Correctness rule (#4360): NEVER trust the cache. The marker is the only
// freshness oracle and it gates on the LIVE tree, not on what a cache
// restore happened to bring in — so a dirty or stale restore at worst wastes
// ONE rebuild (marker mismatch → real rebuild), it can never run wrong
// artifacts. Symmetrically, an absent marker (fresh checkout, dist wiped,
// build interrupted mid-way) is a MISS, never a guess. Exits non-zero only
// on a real failure: a build command that failed, or an input tree so broken
// the hash itself cannot be computed (which means the build would fail too).
//
// The workflow side (actions/cache keyed on hashFiles over the SAME inputs,
// per runner.os) only accelerates the MISS path across legs; the marker
// check stands on its own, so local `npm test` warm runs benefit too.
//
// Parity note: pretest previously ran `npm --prefix kernel run build && npm
// --prefix pi-subagents run build`; this script runs exactly those commands
// on a miss (same resolution, same clean semantics — kernel's build wipes
// dist, so the marker is written only after a fully successful build).

import { createHash } from "node:crypto";
import { execFileSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = join(fileURLToPath(new URL(".", import.meta.url)), "..");

/** Marker file written inside each dist; gitignored together with dist/. */
export const MARKER_NAME = ".pretest-cache-marker";

/** Seed: bump when the hash recipe itself changes so old markers invalidate. */
const HASH_SEED_PREFIX = "bili-pretest-cache-v1";

/**
* Shared inputs hashed relative to the repo root for BOTH components: the
* root lockfile pins the toolchain versions (tsup/esbuild/typescript) that
* the `file:` devDependencies resolve through, so a toolchain bump
* invalidates both caches even though no component source changed.
*/
const ROOT_INPUTS = ["package-lock.json"];

/**
* The two pretest components and the exact input set that determines their
* dist bytes. `inputs` are relative to the component dir; each entry is a
* file (hashed by content) or a directory (walked recursively, hashed by
* relative path + content). Keep this table in sync with the hashFiles()
* patterns in .github/workflows/ci.yml — the workflow README-comment there
* points back here.
*/
export const COMPONENTS = [
{
name: "kernel",
dir: "kernel",
inputs: ["src", "tsup.config.ts", "tsconfig.json", "package.json", "package-lock.json"],
},
{
name: "pi-subagents",
dir: "pi-subagents",
// package.json is a BUILD INPUT here, not just metadata: tsup.config.ts
// inlines CURRENT_VERSION from it into the bundle.
inputs: ["src", "tsup.config.ts", "tsconfig.json", "package.json"],
},
];

/** Never hashed inside a walk — build outputs and deps are not inputs. */
const SKIP_NAMES = new Set(["node_modules", "dist", "tmp", ".tmp", ".git"]);

function hashFile(h, abs, rel) {
h.update(rel);
h.update("\0");
h.update(readFileSync(abs));
h.update("\0");
}

function hashDir(h, absDir, relDir) {
const entries = readdirSync(absDir, { withFileTypes: true });
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
for (const e of entries) {
if (SKIP_NAMES.has(e.name)) continue;
const rel = `${relDir}/${e.name}`;
if (e.isDirectory()) hashDir(h, join(absDir, e.name), rel);
else if (e.isFile()) hashFile(h, join(absDir, e.name), rel);
// symlinks/other node types are skipped — deterministic and inert
}
}

/**
* The content hash for one component: sha256 over the platform plus every
* input's normalized relative path and bytes. Platform rides the hash
* because a cache restore crossing OSes (possible with a future key typo)
* must never satisfy the marker check — same never-trust rule as everything
* else here. Pure: no writes, no builds.
*/
export function hashComponent(root, comp, platform = process.platform) {
const h = createHash("sha256");
h.update(`${HASH_SEED_PREFIX} platform=${platform}\0`);
const rels = [...ROOT_INPUTS, ...comp.inputs.map((i) => `${comp.dir}/${i}`)].sort();
for (const rel of rels) {
const abs = join(root, rel);
if (!existsSync(abs)) {
throw new Error(`input "${rel}" not found under ${root} — cannot establish dist freshness`);
}
if (statSync(abs).isDirectory()) hashDir(h, abs, rel);
else hashFile(h, abs, rel);
}
return h.digest("hex");
}

/** Marker freshness: byte-exact hash comparison. Absent/corrupt marker = MISS. */
export function markerIsFresh(distDir, hash) {
try {
return readFileSync(join(distDir, MARKER_NAME), "utf-8").trim() === hash;
} catch {
return false;
}
}

/**
* The pretest loop. For each component: hash → fresh marker? HIT, skip :
* MISS → run the real build → stamp the marker. `build`/`log` are test seams
* (the CLI passes defaults); build failures propagate so the CLI exits 1 —
* non-zero exit means a REAL build failure, never a cache decision.
*/
export function runPretestCache(opts = {}) {
const root = opts.root ?? ROOT;
const components = opts.components ?? COMPONENTS;
const log = opts.log ?? ((line) => console.log(line));
const build = opts.build ?? defaultBuild;
const results = [];
for (const comp of components) {
const hash = hashComponent(root, comp);
const distDir = join(root, comp.dir, "dist");
if (markerIsFresh(distDir, hash)) {
log(`pretest-cache: HIT (${comp.name}) — dist fresh for hash ${hash.slice(0, 12)}`);
results.push({ name: comp.name, hit: true, ms: 0 });
continue;
}
log(`pretest-cache: MISS (${comp.name}) → rebuilding`);
const t0 = Date.now();
build(comp, root);
mkdirSync(distDir, { recursive: true });
writeFileSync(join(distDir, MARKER_NAME), `${hash}\n`);
const ms = Date.now() - t0;
log(`pretest-cache: built (${comp.name}) in ${(ms / 1000).toFixed(1)}s — marker stamped (${hash.slice(0, 12)})`);
results.push({ name: comp.name, hit: false, ms });
}
return results;
}

function defaultBuild(comp, root) {
// Same invocation pretest used verbatim (`npm --prefix <dir> run build`).
// On Windows npm is a .cmd batch file CreateProcess cannot exec directly
// (EINVAL) — same shell:true dance as scripts/run-ci-tests.mjs; args are
// fixed literals, so there is no quoting surface.
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
execFileSync(npmCmd, ["--prefix", comp.dir, "run", "build"], {
cwd: root,
stdio: "inherit",
shell: process.platform === "win32",
});
}

function main() {
try {
runPretestCache();
return 0;
} catch (e) {
console.error(`pretest-cache: FAILED — ${e?.message ?? e}`);
return 1;
}
}

// Run only when invoked directly (not when imported by the regression test).
const INVOKED_DIRECTLY = (() => {
if (!process.argv[1]) return false;
try {
return resolve(process.argv[1]) === fileURLToPath(import.meta.url);
} catch {
return false;
}
})();
if (INVOKED_DIRECTLY) {
process.exit(main());
}
Loading
Loading