Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

feat(gate): reuse a test file green result while its inputs match [C7]

Corey Olson01ab7b6d9645
diff --git a/bin/run-tests.js b/bin/run-tests.js
index 98b9d5bd..e07b843e 100644
--- a/bin/run-tests.js
+++ b/bin/run-tests.js
@@ -1245,7 +1245,6 @@ function optionTakesValue(argument) {
if (signature.includes('=') && !signature.includes('[=')) nodeOptionsWithValues.add(option[0]);
}
}
-
}
return nodeOptionsWithValues.has(argument.replaceAll('_', '-'));
}
@@ -1442,6 +1441,9 @@ async function main(args) {
const { testsNeedBrowser } = await import('../src/browser-admission.js');
browser = await testsNeedBrowser(process.cwd(), explicitTestFiles(args));
}
+ // A test file whose inputs did not change since a green run is reused, not run again (#1045).
+ const actions = await (await import('../src/test-action-cache.js')).planTestActions(process.cwd(), args, { fullSuite, landing: process.env[LANDING_RUN_ENV] === '1', listFiles: () => fullSuite ? trackedDefaultTestFiles(process.cwd(), process.env) : explicitTestFiles(args) });
+ if (actions.done) return;
let signal = null;
/** Keep sandbox lifetime inside the held lease, including the complete child process. */
const shardLabel = process.env[LANDING_SHARD_ENV];
@@ -1451,7 +1453,7 @@ async function main(args) {
const fileTimes = shardLabel ? join(sandbox, 'landing-file-times.jsonl') : '';
if (fileTimes) process.env[LANDING_FILE_TIMES_ENV] = fileTimes;
try {
- const result = await executeTests(args, sandbox, gateMarkers, fullSuite, concurrency);
+ const result = await executeTests(actions.args, sandbox, gateMarkers, fullSuite && !actions.files, concurrency);
signal = result.signal;
process.exitCode = result.status ?? 1;
if (fileTimes) (await import('../src/landing-shards.js')).printShardFileTimes(fileTimes);
@@ -1466,7 +1468,7 @@ async function main(args) {
const { landingShardCount } = await import('../src/gate.js');
const count = landingShardCount(process.cwd());
if (count > 1) {
- const files = isGitWorkTree(process.cwd(), process.env) ? trackedDefaultTestFiles(process.cwd(), process.env) : [];
+ const files = actions.files ?? (isGitWorkTree(process.cwd(), process.env) ? trackedDefaultTestFiles(process.cwd(), process.env) : []);
if (!files.length) throw new Error('a sharded landing gate needs tracked Node test files; commit them or set landingShards to 1');
const inherited = scopedMarker ? inheritedGateSlot({ root: process.cwd(), browser }) : null;
const { runLandingShards } = await import('../src/landing-shards.js');
@@ -1500,7 +1502,7 @@ async function main(args) {
const { runOnRunner } = await import('../src/runners.js');
/** Quote the unchanged runner arguments for the remote command shell. */
const word = value => `'${String(value).replaceAll("'", "'\\''")}'`;
- const command = ['node', `./${runnerPath}`, ...args].map(word).join(' ');
+ const command = ['node', `./${runnerPath}`, ...actions.args].map(word).join(' ');
const result = runOnRunner(process.cwd(), command, { runner: lease.runner, source: lease.source,
browser, metadata: lease.runMetadata });
process.stdout.write(result.output);
@@ -1531,6 +1533,7 @@ if (process.argv[1] && resolve(process.argv[1]) === runner) {
await main(process.argv.slice(2));
} else if (process.argv[1]) {
process.env.PULLBOARD_TEST_FILE = process.argv[1];
+ if (process.env.PULLBOARD_TEST_ACTIONS) (await import('../src/test-action-cache.js')).recordTestAction(process.argv[1]);
const fileTimes = process.env[LANDING_FILE_TIMES_ENV];
// Only this test file reports; anything it starts is not one of the shard's files.
delete process.env[LANDING_FILE_TIMES_ENV];
diff --git a/src/affected-tests.js b/src/affected-tests.js
index ab2e84f7..d006f780 100644
--- a/src/affected-tests.js
+++ b/src/affected-tests.js
@@ -125,7 +125,7 @@ function selectionGit(root, args) {
return result.stdout;
}
-/** Read all committed module blobs in one Git batch, preserving exact names and source bytes. */
+/** Read all committed module blobs in one Git batch, preserving exact names and source bytes; `blobs` maps every tracked path to its object id. */
function modulesAt(root, commit) {
const tracked = selectionGit(root, ['ls-tree', '-r', '-z', commit]).split('\0').filter(Boolean).map(record => {
const tab = record.indexOf('\t');
@@ -143,7 +143,8 @@ function modulesAt(root, commit) {
if (entries.some(entry => entry.type !== 'blob' || entry.mode === '120000' || entry.path.endsWith('.cjs'))) {
throw new Error('a tracked module uses an unsupported file mode or CommonJS');
}
- if (!entries.length) return new Map();
+ const blobs = new Map(tracked.map(entry => [entry.path, entry.hash]));
+ if (!entries.length) return { modules: new Map(), blobs };
const hashes = [...new Set(entries.map(entry => entry.hash))];
const result = spawnSync('git', ['--no-replace-objects', 'cat-file', '--batch'], {
cwd: root, env: cleanGitEnvironment(), input: hashes.join('\n') + '\n', maxBuffer: 128 * 1024 * 1024,
@@ -160,7 +161,7 @@ function modulesAt(root, commit) {
sources.set(hash, result.stdout.subarray(at, at + size).toString('utf8'));
at += size + 1;
}
- return new Map(entries.map(entry => [entry.path, sources.get(entry.hash)]));
+ return { modules: new Map(entries.map(entry => [entry.path, sources.get(entry.hash)])), blobs };
}
/**
@@ -259,7 +260,7 @@ function knownBuiltinSynchronizers(sources, imports) {
/** Compile this tree's import edges, plus the literal child-CLI dependency used by fixture modules. */
function graphAt(root, commit, changedDocs = []) {
- const modules = modulesAt(root, commit);
+ const { modules, blobs } = modulesAt(root, commit);
const paths = [...modules.keys()];
const sources = [...modules.values()];
const imports = importsIn(sources);
@@ -334,7 +335,43 @@ function graphAt(root, commit, changedDocs = []) {
if (!reverse.has(module)) reverse.set(module, new Set());
reverse.get(module).add(reader);
}
- return { paths, reverse, unknown, undeclared, testTreeReaders, sourceTreeReaders, docInputs };
+ return { paths, reverse, unknown, undeclared, testTreeReaders, sourceTreeReaders, docInputs, modules, blobs };
+}
+
+/**
+ * Each committed test file's transitive import closure: the test, every module it reaches through the same edges
+ * selection uses (imports, declared loads, the CLI entry, command and engine discovery), sorted. A closure that reaches
+ * an import the graph cannot resolve, or a test whose computed import declares nothing, has `files: null` and a reason,
+ * so a caller that keys results on it runs that file rather than trust an incomplete closure [C7].
+ *
+ * @param {string} root
+ * @param {string} [commit]
+ * @returns {{ closures: Map<string, { files: string[] | null, reason: string }>, modules: Map<string, string>, blobs: Map<string, string> }}
+ */
+export function testClosures(root, commit = 'HEAD') {
+ const graph = graphAt(root, commit);
+ const forward = new Map();
+ for (const [dependency, importers] of graph.reverse) {
+ for (const importer of importers) {
+ if (!forward.has(importer)) forward.set(importer, new Set());
+ forward.get(importer).add(dependency);
+ }
+ }
+ const closures = new Map();
+ for (const test of graph.paths.filter(path => TEST.test(path))) {
+ let reason = graph.undeclared.get(test) ?? '';
+ const reached = new Set([test]);
+ const queue = [test];
+ for (let at = 0; at < queue.length && !reason; at += 1) {
+ const path = queue[at];
+ if (graph.unknown.has(path)) { reason = `${graph.unknown.get(path)} in reached file ${path}`; break; }
+ for (const dependency of forward.get(path) ?? []) {
+ if (!reached.has(dependency)) { reached.add(dependency); queue.push(dependency); }
+ }
+ }
+ closures.set(test, reason ? { files: null, reason } : { files: [...reached].sort(), reason: '' });
+ }
+ return { closures, modules: graph.modules, blobs: graph.blobs };
}
/**
diff --git a/src/gate.js b/src/gate.js
index ead1127d..7cdabe2a 100644
--- a/src/gate.js
+++ b/src/gate.js
@@ -326,6 +326,8 @@ export function runProfiledShell(root, command, { waitMs = 0, artifactDirectory,
...(gateLease && !gateLease.runner ? gateSlotEnvironment(gateLease) : gateSlotHeld ? { [GATE_SLOT_HELD_ENV]: '1' } : {}),
[LANDING_RUN_ENV]: landing ? '1' : '0',
[LANDING_WAIVERS_ENV]: landing ? JSON.stringify(waivers) : '[]',
+ // A reused test result names the stage that ran it (#1045).
+ PULLBOARD_TEST_STAGE: gateLease?.receiptKind ?? (landing ? 'land' : 'gate'),
};
// The registered worker returns the same complete stage/component accounting as a local run.
gateLease?.tracker?.mark('runAt');
@@ -369,6 +371,7 @@ export async function runProfiledShellAsync(root, command, { waitMs = 0, gateSlo
const inherited = {
...(gateLease ? gateSlotEnvironment(gateLease) : gateSlotHeld ? { [GATE_SLOT_HELD_ENV]: '1' } : {}),
[LANDING_RUN_ENV]: landing ? '1' : '0',
+ PULLBOARD_TEST_STAGE: gateLease?.receiptKind ?? (landing ? 'land' : 'gate'),
};
const env = { ...gitChildEnv(root), ...inherited };
const child = spawn(`(\n${command}\n) 2>&1`, { cwd: root, env, shell: true, stdio: ['ignore', 'pipe', 'pipe'] });
diff --git a/src/test-action-cache.js b/src/test-action-cache.js
new file mode 100644
index 00000000..b41e2341
--- /dev/null
+++ b/src/test-action-cache.js
@@ -0,0 +1,328 @@
+/**
+ * Per-test-file results keyed by their inputs: a green result whose inputs did not change is reused, not run again [C7].
+ *
+ * A test file's action key hashes the file, its transitive import closure (src/affected-tests.js), the non-JS files it
+ * declares or names, bin/run-tests.js, package.json, the Node version and the declared environment. The store sits in
+ * the repository's common git dir, so every worktree on the machine shares it, and every write is a rename, so
+ * concurrent runs never read half an entry. Only a green result is reused; a red one marks its key for good, since the
+ * same inputs once failing makes a later pass a flake. A missing or unreadable entry runs the file.
+ *
+ * Children record their own outcome as they exit (recordTestAction) into a run folder; the parent run promotes those
+ * results once it exits, and for a landing only when the landing itself was green, since a landing can fail a file
+ * whose process exited cleanly.
+ */
+import { createHash, randomUUID } from 'node:crypto';
+import { mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
+import { join, posix, relative, resolve, sep } from 'node:path';
+import { spawnSync } from 'node:child_process';
+
+export const TEST_ACTIONS_ENV = 'PULLBOARD_TEST_ACTIONS';
+export const TEST_STAGE_ENV = 'PULLBOARD_TEST_STAGE';
+const VERSION = 1;
+const MODULE = /\.[cm]?js$/u;
+const RUNNER_INPUTS = ['bin/run-tests.js', 'package.json'];
+// The environment a test's verdict may depend on; anything else in the environment is per-run noise.
+const DECLARED_ENVIRONMENT = ['NODE_OPTIONS', 'PULLBOARD_HOSTED_SHAPES', 'PULLBOARD_TEST_TIMEOUT_MS', 'PULLBOARD_TEST_FILE_TIMEOUT_MS', 'TZ', 'LANG', 'LC_ALL'];
+// Options that choose how a run reports or splits files, never what a file proves.
+const NEUTRAL_OPTION = /^--(?:test[-_]concurrency|test[-_]reporter(?:[-_]destination)?|test[-_]shard)(?:=|$)/u;
+// A run that selects tests inside a file proves only those tests, so it neither reuses nor records.
+const FILTER_OPTION = /^--test[-_](?:name|skip)[-_]pattern(?:=|$)|^--test[-_]only$/u;
+// Release notes are prose: src reads them for headlines that tests prove against their own fixture copies.
+const PROSE = new Set(['CHANGELOG.md']);
+// Whole trees are inputs only by declaration (`@pullboard-test-input`), never because a fixture names `src`.
+const TREE_DECLARATIONS = [['test-tree', ['test/']], ['source-tree', ['src/', 'bin/']], ['docs-tree', ['docs/']]];
+const TOP_TREES = new Set(['', '.', 'src', 'bin', 'test', 'docs']);
+const URL_LITERAL = /new\s+URL\(\s*(['"`])([^'"`\\$\r\n]+)\1\s*,\s*import\.meta\.url\s*\)/gu;
+const STRING = /(['"])(?:\\[\s\S]|(?!\1)[^\\\r\n])*\1|`(?:\\[\s\S]|[^`\\])*`/uy;
+
+/** Run git in the checkout with a private environment, returning trimmed stdout or throwing its error. */
+function git(root, args, env) {
+ const result = spawnSync('git', ['--no-replace-objects', ...args], { cwd: root, env, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
+ if (result.status !== 0) throw new Error((result.stderr || `git ${args[0]} failed`).trim());
+ return result.stdout;
+}
+
+/** The sha256 of a value's canonical JSON, as hex. */
+function digest(value) {
+ return createHash('sha256').update(JSON.stringify(value)).digest('hex');
+}
+
+/** Write a JSON file by rename, so a concurrent reader sees the old entry, the new one, or none, never half of one. */
+function writeAtomic(path, value) {
+ const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
+ writeFileSync(temporary, `${JSON.stringify(value)}\n`, { mode: 0o600 });
+ renameSync(temporary, path);
+}
+
+/** Every string literal's text, trying each quote as a start, so a quote inside a comment or pattern never hides one. */
+function stringLiterals(source) {
+ const values = new Set();
+ for (const quote of source.matchAll(/['"`]/gu)) {
+ STRING.lastIndex = quote.index;
+ const match = STRING.exec(source);
+ if (match) values.add(match[0].slice(1, -1).split('${', 1)[0]);
+ }
+ return values;
+}
+
+/**
+ * The tracked files one module names: for a test-side module (under test/, or the test itself), every file or folder a
+ * string names from the root, from the module's folder, or by a file's own name; for any module, every file or folder
+ * it opens through new URL('<literal>', import.meta.url). A folder means every file under it, except a whole
+ * top-level tree, which counts only by declaration. Over-selection only costs a rerun.
+ */
+function namedFiles(file, source, testSide, index) {
+ const named = new Set();
+ /** Add a tracked file, or every file under a tracked folder. */
+ const add = (path) => {
+ const normal = posix.normalize(path).replace(/\/$/u, '');
+ if (normal.startsWith('../') || normal.startsWith('/')) return;
+ if (index.files.has(normal)) named.add(normal);
+ else if (index.folders.has(normal) && !TOP_TREES.has(normal)) for (const each of index.under(normal)) named.add(each);
+ };
+ const folder = posix.dirname(file);
+ if (testSide) {
+ for (const literal of stringLiterals(source)) {
+ const value = literal.split(/[?#]/u)[0];
+ if (!value || value.length > 300) continue;
+ add(value);
+ add(posix.join(folder, value));
+ for (const path of index.byName.get(value) ?? []) named.add(path);
+ }
+ }
+ for (const match of source.matchAll(URL_LITERAL)) add(posix.join(folder, match[2]));
+ return [...named].filter(path => !MODULE.test(path) && !PROSE.has(path));
+}
+
+/** The non-module files a test's closure reads: the trees its test declares and every module's named files. */
+function nonModuleInputs(test, files, modules, index) {
+ const inputs = new Set();
+ const header = (modules.get(test) ?? '').split(/\r?\n/u);
+ for (const [name, prefixes] of TREE_DECLARATIONS) {
+ if (!header.includes(`/** @pullboard-test-input ${name} */`)) continue;
+ for (const prefix of prefixes) for (const path of index.under(prefix)) if (!MODULE.test(path) && !PROSE.has(path)) inputs.add(path);
+ }
+ for (const file of files) {
+ const testSide = file.startsWith('test/') || file === test;
+ const memo = `${testSide ? 't' : 's'}:${file}`;
+ if (!index.named.has(memo)) index.named.set(memo, namedFiles(file, modules.get(file) ?? '', testSide, index));
+ for (const path of index.named.get(memo)) inputs.add(path);
+ }
+ return [...inputs].sort();
+}
+
+/** Index the tracked tree: every file, every folder, files by their own name, and the files under any folder. */
+function treeIndex(blobs) {
+ const files = new Set(blobs.keys());
+ const byName = new Map();
+ const folders = new Set();
+ for (const path of files) {
+ const name = posix.basename(path);
+ if (!byName.has(name)) byName.set(name, []);
+ byName.get(name).push(path);
+ for (let at = path.indexOf('/'); at !== -1; at = path.indexOf('/', at + 1)) folders.add(path.slice(0, at));
+ }
+ const sorted = [...files].sort();
+ const cache = new Map();
+ /** The tracked files under a folder, sorted; computed once per folder. */
+ const under = (folder) => {
+ const prefix = folder.endsWith('/') ? folder : `${folder}/`;
+ if (!cache.has(prefix)) cache.set(prefix, sorted.filter(path => path.startsWith(prefix)));
+ return cache.get(prefix);
+ };
+ return { files, folders, byName, under, named: new Map() };
+}
+
+/**
+ * Every test file's action key at a commit, or null with a reason when its closure is incomplete. The key hashes the
+ * file and its closure, its non-module inputs, the runner and package manifest, Node, the platform, the declared
+ * environment and the run's options; the blobs are git object ids, so the key is a pure function of committed bytes.
+ *
+ * @param {string} root
+ * @param {{ commit?: string, options?: string[], env?: NodeJS.ProcessEnv }} [context]
+ * @returns {Promise<Map<string, { key: string | null, reason: string }>>}
+ */
+export async function testActionKeys(root, { commit = 'HEAD', options = [], env = process.env } = {}) {
+ const { testClosures } = await import('./affected-tests.js');
+ const { closures, modules, blobs } = testClosures(root, commit);
+ const index = treeIndex(blobs);
+ const shared = {
+ version: VERSION,
+ runner: RUNNER_INPUTS.map(path => [path, blobs.get(path) ?? null]),
+ node: process.version, platform: process.platform, arch: process.arch,
+ environment: DECLARED_ENVIRONMENT.map(name => [name, env[name] ?? null]),
+ options: options.filter(option => !NEUTRAL_OPTION.test(option)),
+ };
+ const keys = new Map();
+ for (const [test, closure] of closures) {
+ if (!closure.files) { keys.set(test, { key: null, reason: closure.reason }); continue; }
+ const inputs = nonModuleInputs(test, closure.files, modules, index);
+ keys.set(test, { key: digest({ ...shared, test,
+ closure: closure.files.map(path => [path, blobs.get(path) ?? null]),
+ inputs: inputs.map(path => [path, blobs.get(path)]) }), reason: '' });
+ }
+ return keys;
+}
+
+/** The store folder for this repository, shared by every worktree through the common git dir. */
+function storeOf(root, env) {
+ const common = git(root, ['rev-parse', '--path-format=absolute', '--git-common-dir'], env).trim();
+ return join(common, 'pullboard', 'test-actions');
+}
+
+/**
+ * The reusable green entry for a key, or null: a missing, unreadable, mismatched or red-marked entry is never reused.
+ *
+ * @param {string} store
+ * @param {string | null} key
+ * @returns {{ key: string, file: string, stage: string, commit: string } | null}
+ */
+export function reusableResult(store, key) {
+ if (!key) return null;
+ try { statSync(join(store, 'red', key)); return null; } catch { /* No red mark: the key never failed. */ }
+ try {
+ const entry = JSON.parse(readFileSync(join(store, 'green', `${key}.json`), 'utf8'));
+ return entry?.version === VERSION && entry.key === key && entry.status === 'pass' && typeof entry.commit === 'string' ? entry : null;
+ } catch { return null; }
+}
+
+/**
+ * The one rule for whether per-file results, fresh or reused, prove the whole suite: every file of the suite has a
+ * result, every result passed, and every reused result was stored under the key the file has now. Pure: it reads
+ * nothing but its arguments.
+ *
+ * @param {string[]} files every test file of the suite
+ * @param {Map<string, { status: string, source: 'fresh' | 'reused', key?: string | null, currentKey?: string | null }>} results
+ * @returns {{ proven: boolean, missing: string[], failed: string[], stale: string[] }}
+ */
+export function suiteProven(files, results) {
+ const missing = files.filter(file => !results.has(file));
+ const failed = files.filter(file => results.has(file) && results.get(file).status !== 'pass');
+ const stale = files.filter(file => results.get(file)?.source === 'reused' && (!results.get(file).key || results.get(file).key !== results.get(file).currentKey));
+ return { proven: files.length > 0 && !missing.length && !failed.length && !stale.length, missing, failed, stale };
+}
+
+/** The repository path of a file selector, or null when it is not a path inside the root. */
+function repoPath(root, selector) {
+ try {
+ const path = relative(root, realpathSync(resolve(root, selector)));
+ return path && !path.startsWith('..') ? path.split(sep).join('/') : null;
+ } catch { return null; }
+}
+
+/** Remove run folders a killed parent never settled, a day after they started. */
+function pruneRuns(store) {
+ const runs = join(store, 'runs');
+ let names = [];
+ try { names = readdirSync(runs); } catch { return; }
+ for (const name of names) {
+ try { if (Date.now() - statSync(join(runs, name)).mtimeMs > 86_400_000) rmSync(join(runs, name), { recursive: true, force: true }); }
+ catch { /* A folder another run removed first needs nothing more. */ }
+ }
+}
+
+/**
+ * Decide which selected test files must run. A file whose key has a reusable green result is left out and its receipt
+ * says `reused <key> from <stage> <commit>`; when that leaves nothing, the run is done. A checkout that differs from
+ * its commit, a filtered run, a selector that is not a tracked test file, or a shard of a planned run keeps every
+ * file, and so does any failure to compute keys: reuse is an optimization, never a reason to skip.
+ *
+ * @param {string} root
+ * @param {string[]} args the runner's arguments
+ * @param {{ fullSuite: boolean, landing?: boolean, listFiles: () => string[], env?: NodeJS.ProcessEnv, write?: (text: string) => void }} options
+ * @returns {Promise<{ args: string[], files: string[] | null, done: boolean }>}
+ */
+export async function planTestActions(root, args, { fullSuite, landing = false, listFiles, env = process.env, write = text => process.stdout.write(text) }) {
+ const keep = { args, files: null, done: false };
+ if (env[TEST_ACTIONS_ENV] || env.PULLBOARD_TEST_REUSE === '0' || args.some(arg => FILTER_OPTION.test(arg))) return keep;
+ let store, commit, keys, selectors;
+ try {
+ root = realpathSync(root);
+ const gitEnv = Object.fromEntries(Object.entries(env).filter(([name]) => !name.startsWith('GIT_')));
+ if (git(root, ['status', '--porcelain', '-z', '--untracked-files=all'], gitEnv)) return keep;
+ commit = git(root, ['rev-parse', '--verify', 'HEAD^{commit}'], gitEnv).trim();
+ selectors = listFiles();
+ const paths = selectors.map(selector => repoPath(root, selector));
+ const fileSet = new Set(selectors);
+ keys = await testActionKeys(root, { commit, options: args.filter(arg => !fileSet.has(arg)), env });
+ if (!paths.length || paths.some(path => !path || !keys.has(path))) return keep;
+ store = storeOf(root, gitEnv);
+ selectors = selectors.map((selector, at) => ({ selector, path: paths[at], key: keys.get(paths[at]).key }));
+ } catch { return keep; }
+ const stage = env[TEST_STAGE_ENV] || (landing ? 'land' : fullSuite ? 'gate' : 'focused');
+ const results = new Map();
+ const run = [];
+ for (const file of selectors) {
+ const entry = reusableResult(store, file.key);
+ if (!entry) { run.push(file); continue; }
+ results.set(file.path, { status: 'pass', source: 'reused', key: entry.key, currentKey: file.key });
+ write(`# reused ${entry.key} from ${entry.stage} ${entry.commit} (${file.path})\n`);
+ }
+ if (!run.length && suiteProven(selectors.map(file => file.path), results).proven) {
+ write(`# all ${selectors.length} test files reused; none ran\n`);
+ return { args, files: [], done: true };
+ }
+ const runDir = join(store, 'runs', randomUUID());
+ mkdirSync(join(runDir, 'green'), { recursive: true, mode: 0o700 });
+ mkdirSync(join(store, 'green'), { recursive: true, mode: 0o700 });
+ mkdirSync(join(store, 'red'), { recursive: true, mode: 0o700 });
+ pruneRuns(store);
+ writeAtomic(join(runDir, 'keys.json'), { root, store, files: Object.fromEntries(run.filter(file => file.key).map(file => [file.path, file.key])) });
+ env[TEST_ACTIONS_ENV] = runDir;
+ process.once('exit', code => settleTestActions(runDir, { store, stage, commit, landing, code }));
+ const reused = new Set(selectors.filter(file => !run.includes(file)).map(file => file.selector));
+ if (fullSuite) return { args: [...args, ...run.map(file => file.selector)], files: run.map(file => file.selector), done: false };
+ return { args: args.filter(arg => !reused.has(arg)), files: null, done: false };
+}
+
+/**
+ * Promote the greens a run's children recorded into the shared store, then remove the run folder. A landing that
+ * failed promotes nothing: it can fail a file whose own process exited cleanly.
+ *
+ * @param {string} runDir
+ * @param {{ store: string, stage: string, commit: string, landing: boolean, code: number }} run
+ */
+export function settleTestActions(runDir, { store, stage, commit, landing, code }) {
+ try {
+ if (!landing || code === 0) {
+ for (const name of readdirSync(join(runDir, 'green'))) {
+ let staged;
+ try { staged = JSON.parse(readFileSync(join(runDir, 'green', name), 'utf8')); } catch { continue; }
+ if (typeof staged?.key !== 'string' || !/^[0-9a-f]{64}$/u.test(staged.key)) continue;
+ try { statSync(join(store, 'red', staged.key)); continue; } catch { /* Never red: the green may be stored. */ }
+ writeAtomic(join(store, 'green', `${staged.key}.json`), { version: VERSION, key: staged.key, file: staged.file,
+ status: 'pass', stage, commit, node: process.version, at: new Date().toISOString() });
+ }
+ }
+ } catch (error) {
+ process.stderr.write(`run-tests: test results were not stored: ${error.message}\n`);
+ } finally {
+ rmSync(runDir, { recursive: true, force: true });
+ }
+}
+
+/**
+ * In a test file's own process: record its outcome when it exits. Exit 0 stages a green for the parent to promote; any
+ * other exit marks the key red at once, so it is never reused. A process killed for a timeout records nothing, so its
+ * file runs again. The marker leaves the environment here, so nothing this file starts records as it.
+ *
+ * @param {string} file the test file's path, as the process received it
+ */
+export function recordTestAction(file) {
+ const runDir = process.env[TEST_ACTIONS_ENV];
+ delete process.env[TEST_ACTIONS_ENV];
+ if (!runDir || process.env.NODE_TEST_CONTEXT !== 'child-v8') return;
+ let plan, path;
+ try {
+ plan = JSON.parse(readFileSync(join(runDir, 'keys.json'), 'utf8'));
+ path = relative(plan.root, realpathSync(file)).split(sep).join('/');
+ } catch { return; }
+ const key = plan.files?.[path];
+ if (!key) return;
+ process.once('exit', (code) => {
+ try {
+ if (code === 0) writeAtomic(join(runDir, 'green', `${digest(path)}.json`), { key, file: path });
+ else writeAtomic(join(plan.store, 'red', key), { key, file: path, code });
+ } catch (error) { process.stderr.write(`run-tests: the result of ${path} was not recorded: ${error.message}\n`); }
+ });
+}
diff --git a/test/test-action-cache.test.js b/test/test-action-cache.test.js
new file mode 100644
index 00000000..26b8db20
--- /dev/null
+++ b/test/test-action-cache.test.js
@@ -0,0 +1,113 @@
+/** @pullboard-test-input source-tree */
+/** A test file whose inputs did not change reuses its green result; a changed import, a red result or a bad entry runs it [C7]. */
+import assert from 'node:assert/strict';
+import { copyFileSync, cpSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { test } from 'node:test';
+import { runFixtureChild, runFixtureGit } from './fixture-child.js';
+import { suiteProven } from '../src/test-action-cache.js';
+
+const runner = fileURLToPath(new URL('../bin/run-tests.js', import.meta.url));
+const source = fileURLToPath(new URL('../src', import.meta.url));
+
+/** A fixture test that records its own name, then passes when its module says so. */
+function fixtureTest(name, module) {
+ return `import { appendFileSync } from 'node:fs';
+import { test } from 'node:test';
+import { value } from '../lib/${module}.js';
+/** Record that this file ran, then check its module. */
+function check() { appendFileSync(process.env.PULLBOARD_ACTION_CAPTURE, '${name}\\n'); if (value !== 'green') throw new Error('${name} is red'); }
+test('${name} [C7]', check);
+`;
+}
+
+/** A committed repository with the real runner, three tests and their modules: A imports shared, B and C stand alone. */
+function makeFixture(t) {
+ const root = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-test-actions-')));
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ for (const folder of ['bin', 'lib', 'test']) mkdirSync(join(root, folder));
+ copyFileSync(runner, join(root, 'bin', 'run-tests.js'));
+ cpSync(source, join(root, 'src'), { recursive: true });
+ writeFileSync(join(root, '.gitignore'), 'bin/\nsrc/\nmachine/\n');
+ writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));
+ writeFileSync(join(root, 'lib', 'shared.js'), "export const shared = 'green';\n");
+ writeFileSync(join(root, 'lib', 'a.js'), "import { shared } from './shared.js';\nexport const value = shared;\n");
+ writeFileSync(join(root, 'lib', 'b.js'), "export const value = 'green';\n");
+ writeFileSync(join(root, 'lib', 'c.js'), "export const value = 'red';\n");
+ for (const name of ['a', 'b', 'c']) writeFileSync(join(root, 'test', `${name}.test.js`), fixtureTest(name, name));
+ const capture = join(root, 'machine', 'ran.txt');
+ const env = { ...process.env, PULLBOARD_MACHINE_HOME: join(root, 'machine'), PULLBOARD_ACTION_CAPTURE: capture, PULLBOARD_LANDING_RUN: '0',
+ GIT_AUTHOR_NAME: 'Action Fixture', GIT_AUTHOR_EMAIL: '[email protected]', GIT_COMMITTER_NAME: 'Action Fixture', GIT_COMMITTER_EMAIL: '[email protected]' };
+ for (const key of ['PULLBOARD_GATE_SLOT_HELD', 'PULLBOARD_GATE_SLOT_POOL', 'PULLBOARD_GATE_SLOT_TOKEN', 'PULLBOARD_BROWSER_SLOT_HELD', 'PULLBOARD_TEST_STAGE', 'PULLBOARD_TEST_ACTIONS']) delete env[key];
+ mkdirSync(join(root, 'machine'));
+ /** Run one fixture git command under the private environment. */
+ const git = (...args) => runFixtureGit(args, { cwd: root, env });
+ git('init', '-q', '-b', 'main');
+ git('add', '.gitignore', 'package.json', 'lib', 'test');
+ git('commit', '-q', '-m', 'fixture');
+ /** Commit one module's new text. */
+ const change = (path, text) => { writeFileSync(join(root, path), text); git('commit', '-q', '-m', `change ${path}`, '--', path); };
+ /** Run the whole suite through the real runner; return its status, output and the files that ran. */
+ const run = () => {
+ writeFileSync(capture, '');
+ const result = runFixtureChild(process.execPath, ['bin/run-tests.js'], { cwd: root, env, encoding: 'utf8', timeout: 25_000 });
+ const ran = readFileSync(capture, 'utf8').trim().split('\n').filter(Boolean).sort();
+ return { status: result.status, output: `${result.stdout}\n${result.stderr}`, ran, context: result.context };
+ };
+ const store = join(root, '.git', 'pullboard', 'test-actions');
+ return { root, change, run, store };
+}
+
+test('an unchanged closure reuses its green result and a changed import reruns it [C7]', (t) => {
+ const fixture = makeFixture(t);
+ const first = fixture.run();
+ assert.equal(first.status, 1, `C is red, so the first run fails\n${first.output}`);
+ assert.deepEqual(first.ran, ['a', 'b', 'c'], 'every file runs when nothing is stored');
+
+ // 1. A module only B imports changes: B runs, A is reused, C runs because a red result is never reused (3).
+ fixture.change('lib/b.js', "export const value = 'green'; // edited\n");
+ const second = fixture.run();
+ assert.deepEqual(second.ran, ['b', 'c'], second.output);
+ assert.match(second.output, /^# reused [0-9a-f]{64} from \S+ [0-9a-f]{40} \(test\/a\.test\.js\)$/mu);
+ assert.doesNotMatch(second.output, /reused .*\(test\/(?:b|c)\.test\.js\)/u);
+
+ // 2. A module A imports changes: A runs and B is reused; C still runs.
+ fixture.change('lib/shared.js', "export const shared = 'green'; // edited\n");
+ const third = fixture.run();
+ assert.deepEqual(third.ran, ['a', 'c'], third.output);
+ assert.match(third.output, /^# reused [0-9a-f]{64} from \S+ [0-9a-f]{40} \(test\/b\.test\.js\)$/mu);
+ assert.equal(readdirSync(join(fixture.store, 'red')).length, 1, 'C has one red key, written once and kept');
+
+ // 4. A missing entry (A) and a corrupt entry (B) both run their file; C turns green under a new key and runs.
+ const entries = readdirSync(join(fixture.store, 'green'))
+ .map(name => [name, JSON.parse(readFileSync(join(fixture.store, 'green', name), 'utf8')).file]);
+ assert.deepEqual([...new Set(entries.map(([, file]) => file))].sort(), ['test/a.test.js', 'test/b.test.js'], 'only green files are stored');
+ for (const [name, file] of entries) {
+ if (file === 'test/a.test.js') rmSync(join(fixture.store, 'green', name));
+ else writeFileSync(join(fixture.store, 'green', name), '{"version":1,"key":');
+ }
+ fixture.change('lib/c.js', "export const value = 'green';\n");
+ const fourth = fixture.run();
+ assert.equal(fourth.status, 0, fourth.output);
+ assert.deepEqual(fourth.ran, ['a', 'b', 'c'], fourth.output);
+
+ // Nothing changed since a green run: no file runs and the suite is proven from reused results alone.
+ const fifth = fixture.run();
+ assert.equal(fifth.status, 0, fifth.output);
+ assert.deepEqual(fifth.ran, [], fifth.output);
+ assert.equal(fifth.output.match(/^# reused /gmu)?.length, 3, fifth.output);
+ assert.match(fifth.output, /^# all 3 test files reused; none ran$/mu);
+});
+
+test('only passing results under the current key prove the whole suite [C7]', () => {
+ const files = ['test/a.test.js', 'test/b.test.js'];
+ const fresh = { status: 'pass', source: 'fresh' };
+ const reused = { status: 'pass', source: 'reused', key: 'k1', currentKey: 'k1' };
+ assert.equal(suiteProven(files, new Map([[files[0], fresh], [files[1], reused]])).proven, true);
+ assert.deepEqual(suiteProven(files, new Map([[files[0], fresh]])).missing, [files[1]]);
+ assert.deepEqual(suiteProven(files, new Map([[files[0], fresh], [files[1], { ...fresh, status: 'fail' }]])).failed, [files[1]]);
+ assert.deepEqual(suiteProven(files, new Map([[files[0], fresh], [files[1], { ...reused, currentKey: 'k2' }]])).stale, [files[1]]);
+ assert.equal(suiteProven([], new Map()).proven, false, 'an empty suite proves nothing');
+});