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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(pool): reserve FIFO tokens before file dispatch [C7]
Corey Olson80f1f12da76e
diff --git a/bin/run-tests.js b/bin/run-tests.jsindex b1422924..dcd00a45 100644--- a/bin/run-tests.js+++ b/bin/run-tests.js@@ -24,6 +24,8 @@ import { AGENT_SHELL_MARKERS, SSH_SHELL_MARKERS } from '../src/person.js';import { isLandingWorkerIdentityTimeout, landingWorkerIdentityQuery, parseLandingWorkerIdentity } from '../src/landing-worker-identity.js';import { foreignNodeOptions, isolateTestMachine } from '../src/test-isolation.js';import { TEST_FILE_POOL_ENV, longestFirst, takeTestFileToken, testFilePoolValue } from '../src/test-file-pool.js';+import { optionTakesValue } from '../src/test-file-dispatch.js';+import { knownNodeOption } from '../src/test-file-dispatch.js';const runner = fileURLToPath(import.meta.url);const LANDING_RUN_ENV = 'PULLBOARD_LANDING_RUN';const LANDING_WORKER_STARTS_ENV = 'PULLBOARD_LANDING_WORKER_STARTS';@@ -265,208 +267,26 @@ function testEnvironment(sandbox, gateMarkers = {}) {}/**- * Start the Node test runner and return its result to the caller for faithful exit handling.+ * Run ordinary mode through admitted files and return its result for faithful exit handling.** @param {string[]} args* @param {NodeJS.ProcessEnv} env- * @returns {import('node:child_process').SpawnSyncReturns<string>}+ * @returns {Promise<{status:number,signal:null}>}*/-function runTests(args, env, concurrency) {- let files = concurrency.files;- // Node reads an option only before the files, so every option is placed ahead of them, wherever it was written (#931).- const placed = [];- const selectors = [];- for (let at = 0; at < args.length; at += 1) {- const argument = args[at];- if (argument === '--') { selectors.push(...args.slice(at)); break; }- if (/^--test[-_]name[-_]pattern(?:=|$)/u.test(argument)) {- const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];- if (value === undefined) throw new Error('--test-name-pattern requires a value');- placed.push(`--test-name-pattern=${value}`);- } else if (/^--test[-_]skip[-_]pattern(?:=|$)/u.test(argument)) {- const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];- if (value === undefined) throw new Error('--test-skip-pattern requires a value');- placed.push(`--test-skip-pattern=${value}`);- } else if (/^--test[-_]concurrency(?:=|$)/u.test(argument)) {- const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];- if (!/^[1-9][0-9]*$/u.test(value ?? '') || !Number.isSafeInteger(Number(value))) {- throw new Error('--test-concurrency must be a positive integer');- }- files = Math.min(files, Number(value));- } else if (/^--(?:experimental[-_])?test[-_]isolation(?:=none|$)/u.test(argument) &&- (argument.endsWith('=none') || args[at + 1] === 'none')) {- throw new Error('bounded browser test files require process isolation; remove --test-isolation=none');- } else if (argument === '--test-only') {- // Node would apply it only to cases marked only, so a run would silently skip the rest of the file (#931).- throw new Error('unknown option --test-only; run-tests does not apply it, so select cases with --test-name-pattern');- } else if (argument.startsWith('-')) {- // An option Node does not know would be ignored; name it instead of running without it (#931).- if (!knownNodeOption(argument)) throw new Error(`unknown option ${argument.split('=', 1)[0]}; check its spelling, or drop it`);- placed.push(argument);- if (!argument.includes('=') && optionTakesValue(argument)) {- if (args[at + 1] === undefined) throw new Error(`${argument} requires a value`);- placed.push(args[++at]);- }- } else selectors.push(argument);- }- const reporter = args.some((argument) => argument === '--test-reporter' || argument.startsWith('--test-reporter='));- const selectionReporter = [];- if (env.PULLBOARD_TEST_SELECTION) {- const reporters = placed.filter(argument => /^--test-reporter(?:=|$)/u.test(argument)).length;- const destinations = placed.filter(argument => /^--test-reporter-destination(?:=|$)/u.test(argument)).length;- if (destinations && destinations !== (reporters || 1)) throw new Error('each --test-reporter requires a matching --test-reporter-destination');- if (reporter && !destinations) for (let index = 0; index < reporters; index++) selectionReporter.push('--test-reporter-destination=stdout');- if (!reporter && !env.PULLBOARD_TEST_ACTIONS && !destinations) selectionReporter.push('--test-reporter-destination=stdout');- selectionReporter.push(`--test-reporter=${fileURLToPath(new URL('../src/test-selection.js', import.meta.url))}`, `--test-reporter-destination=${devNull}`);- }- const nodeArgs = [- '--disable-warning=ExperimentalWarning',- '--test',- `--test-concurrency=${files}`,- ...(!reporter ? ['--test-reporter=tap', ...(env.PULLBOARD_TEST_ACTIONS ? ['--test-reporter-destination=stdout', `--test-reporter=${fileURLToPath(new URL('../src/test-action-reporter.js', import.meta.url))}`, `--test-reporter-destination=${devNull}`] : [])] : []),- '--import',- runner,- ...placed,- ...selectionReporter,- ...selectors,- ];- return spawnSync(process.execPath, nodeArgs, { env, stdio: 'inherit' });+async function runTests(args, env, concurrency) {+ const { nativeFileArguments, reportDispatchedTests, legacyFileMode, legacyNativeFileRun } = await import('../src/test-file-dispatch.js');+ if (legacyFileMode(args)) return legacyNativeFileRun(args, env, concurrency, { runner, knownNodeOption, optionTakesValue });+ const normalized = nativeFileArguments(args, concurrency, knownNodeOption, optionTakesValue);+ const { options } = await landingTestOptions(normalized.args, env, normalized.concurrency);+ if (env.PULLBOARD_TEST_ACTIONS) options.reporters.push({ reporter: fileURLToPath(new URL('../src/test-action-reporter.js', import.meta.url)), destination: devNull });+ if (env.PULLBOARD_TEST_SELECTION) options.reporters.push({ reporter: fileURLToPath(new URL('../src/test-selection.js', import.meta.url)), destination: devNull });+ return reportDispatchedTests(options, env);}/** Convert the Node test CLI subset to the supported node:test run() options. */async function landingTestOptions(args, env, concurrency) {- const options = { concurrency: true, isolation: 'process', execArgv: ['--disable-warning=ExperimentalWarning', '--import', runner] };- const reporters = [];- const selectors = [];- const patterns = [];- const skipPatterns = [];- const coverageExcludeGlobs = [];- const coverageIncludeGlobs = [];- const execArgv = [];- let expectValue = null;- for (let index = 0; index < args.length; index += 1) {- let argument = args[index];- if (expectValue) {- expectValue(argument);- expectValue = null;- continue;- }- if (argument === '--') { selectors.push(...args.slice(index + 1)); break; }- const valueOption = (name, take) => {- if (argument === name) { expectValue = take; return true; }- if (argument.startsWith(`${name}=`)) { take(argument.slice(name.length + 1)); return true; }- return false;- };- if (valueOption('--test-concurrency', value => {- if (value === 'true') options.concurrency = true;- else if (value === 'false') options.concurrency = false;- else if (/^[1-9][0-9]*$/u.test(value)) {- const requested = Number(value);- // Validate before clamping: Node's run() accepts a positive uint32, not Infinity or larger integers.- if (!Number.isSafeInteger(requested) || requested > 4_294_967_295) {- throw new Error(`invalid --test-concurrency value: ${value}`);- }- options.concurrency = requested;- } else throw new Error(`invalid --test-concurrency value: ${value}`);- })) continue;- if (valueOption('--test-name-pattern', value => patterns.push(value))) continue;- if (valueOption('--test-skip-pattern', value => skipPatterns.push(value))) continue;- if (valueOption('--test-timeout', value => { options.timeout = Number(value); })) continue;- if (valueOption('--test-reporter', value => reporters.push(value))) continue;- if (valueOption('--test-reporter-destination', value => { (options.reporterDestinations ??= []).push(value); })) continue;- if (valueOption('--test-coverage-include', value => coverageIncludeGlobs.push(value))) continue;- if (valueOption('--test-coverage-exclude', value => coverageExcludeGlobs.push(value))) continue;- if (valueOption('--test-coverage-lines', value => { options.lineCoverage = Number(value); })) continue;- if (valueOption('--test-coverage-branches', value => { options.branchCoverage = Number(value); })) continue;- if (valueOption('--test-coverage-functions', value => { options.functionCoverage = Number(value); })) continue;- // Parsed but never applied, so a run would pass silently without the cases it claims: refuse it by name (#931).- if (argument === '--test-only') throw new Error('unknown option --test-only; run-tests does not apply it, so select cases with --test-name-pattern');- if (argument === '--experimental-test-coverage') { options.coverage = true; continue; }- if (argument.startsWith('--experimental-test-isolation=')) {- options.isolation = argument.slice(argument.indexOf('=') + 1);- continue;- }- if (argument.startsWith('--test-isolation=')) {- options.isolation = argument.slice(argument.indexOf('=') + 1);- continue;- }- if (argument === '--test-isolation') {- expectValue = value => { options.isolation = value; };- continue;- }- if (argument === '--experimental-test-isolation') {- expectValue = value => { options.isolation = value; };- continue;- }- if (argument === '--test-shard') { index += 1; continue; }- if (argument.startsWith('--test-shard=')) continue;- if (argument.startsWith('-')) {- // An option Node does not know would be forwarded and ignored after the files; refuse it by name wherever it appears (#931).- const name = argument.split('=', 1)[0].replaceAll('_', '-');- optionTakesValue(name);- if (!knownNodeOptions.has(name)) throw new Error(`unknown option ${name}; check its spelling, or drop it`);- execArgv.push(argument);- if (!argument.includes('=') && optionTakesValue(argument)) {- if (args[index + 1] === undefined) throw new Error(`${argument} requires a value`);- execArgv.push(args[++index]);- }- continue;- }- selectors.push(argument);- }- if (expectValue) throw new Error('missing value for a Node test runner option');- options.execArgv = [...options.execArgv, ...execArgv];- // Keep native run() from treating true/default or a larger explicit value as unbounded.- if (options.concurrency !== false) {- const requested = options.concurrency === true ? concurrency.files : options.concurrency;- options.concurrency = Math.min(concurrency.files, requested);- }- if (selectors.length) {- const { glob } = await import('node:fs/promises');- const files = [];- for (const selector of selectors) {- if (!/[?*[{]/u.test(selector)) {- files.push(resolve(selector));- continue;- }- const matches = [];- for await (const match of glob(selector, { cwd: process.cwd() })) matches.push(resolve(match));- if (!matches.length) throw new Error(`test file pattern matched no files: ${selector}`);- files.push(...matches.sort());- }- options.files = [...new Set(files)];- }- if (patterns.length) options.testNamePatterns = patterns;- if (skipPatterns.length) options.testSkipPatterns = skipPatterns;- if (coverageExcludeGlobs.length) options.coverageExcludeGlobs = coverageExcludeGlobs;- if (coverageIncludeGlobs.length) options.coverageIncludeGlobs = coverageIncludeGlobs;- if (args.includes('--test-shard') || args.some(argument => argument.startsWith('--test-shard='))) {- const shardArgument = args.find(argument => argument === '--test-shard' || argument.startsWith('--test-shard='));- const shard = shardArgument === '--test-shard' ? args[args.indexOf(shardArgument) + 1] : shardArgument.slice('--test-shard='.length);- const match = shard?.match(/^([1-9][0-9]*)\/([1-9][0-9]*)$/u);- if (!match) throw new Error(`invalid --test-shard value: ${shard ?? '(missing)'}`);- options.shard = { index: Number(match[1]), total: Number(match[2]) };- }- let selectedCandidates = options.files;- if (!selectedCandidates) {- const { glob } = await import('node:fs/promises');- const defaults = ['**/*.test.{cjs,mjs,js}', '**/*-test.{cjs,mjs,js}', '**/*_test.{cjs,mjs,js}',- '**/test-*.{cjs,mjs,js}', '**/test.{cjs,mjs,js}', '**/test/**/*.{cjs,mjs,js}'];- const matches = new Set();- for (const pattern of defaults) {- for await (const file of glob(pattern, { cwd: process.cwd() })) {- if (!file.split(/[\\/]/u).includes('node_modules')) matches.add(resolve(file));- }- }- selectedCandidates = [...matches].sort();- }- options.selectedFiles = selectedCandidates.filter((_file, index) => !options.shard || index % options.shard.total === options.shard.index - 1);- if (!reporters.length) reporters.push('tap');- const destinations = options.reporterDestinations ?? (reporters.length === 1 ? ['stdout'] : []);- if (destinations.length !== reporters.length) throw new Error('each --test-reporter requires a matching --test-reporter-destination');- options.reporters = reporters.map((reporter, index) => ({ reporter, destination: destinations[index] }));- delete options.reporterDestinations;- return { options, env };+ const { testFileOptions } = await import('../src/test-file-dispatch.js');+ return testFileOptions(args, env, concurrency, { runner, optionTakesValue, knownNodeOption });}/** Parse a POSIX process census into PID, group, and start identities used for safe cleanup. */@@ -889,7 +709,9 @@ async function runLandingTests(args, env, concurrency) {const retainedFailure = landingFailureReport();const waivers = landingWaiverProjection(process.env[LANDING_WAIVERS_ENV]);const waivedFiles = new Map();+ const { dispatchTestFiles, legacyFileMode } = await import('../src/test-file-dispatch.js');const { run } = await import('node:test');+ const legacy = legacyFileMode(args);const { PassThrough } = await import('node:stream');const { createWriteStream } = await import('node:fs');const { pathToFileURL } = await import('node:url');@@ -908,6 +730,7 @@ async function runLandingTests(args, env, concurrency) {testEnv[LANDING_ATTEMPT_ENV] = attempt;watchLandingChildSpawns(workerStartsPath, attempt);const selectedFiles = options.selectedFiles;+ if (!legacy) { options.files = selectedFiles; delete options.shard; }delete options.selectedFiles;const reporterEntries = options.reporters;if (process.env.PULLBOARD_TEST_ACTIONS) reporterEntries.push({ reporter: fileURLToPath(new URL('../src/test-action-reporter.js', import.meta.url)), destination: devNull });@@ -1145,7 +968,8 @@ async function runLandingTests(args, env, concurrency) {for (const key of Object.keys(process.env)) delete process.env[key];Object.assign(process.env, testEnv, { [LANDING_RUN_ENV]: '0' });try {- const stream = run({ ...options, signal: controller.signal,+ const execute = legacy ? run : input => dispatchTestFiles(input, testEnv);+ const stream = execute({ ...options, signal: controller.signal,setup(tests) {tests.on('test:complete', data => {const { file, name, details } = data;@@ -1255,40 +1079,6 @@ async function runLandingTests(args, env, concurrency) {return { status: successful ? 0 : 1, signal: null };}-let nodeOptionsWithValues;-let knownNodeOptions;--/** Read Node option arity from this runtime so supported option values cannot bypass the machine pool. */-function optionTakesValue(argument) {- if (!nodeOptionsWithValues) {- nodeOptionsWithValues = new Set();- knownNodeOptions = new Set();- const help = execFileSync(process.execPath, ['--help'], { encoding: 'utf8' });- for (const line of help.split(/\r?\n/u)) {- const signature = line.match(/^ (-\S.*?)(?: {2,}|$)/u)?.[1];- if (!signature) continue;- for (const option of signature.matchAll(/--?[A-Za-z][A-Za-z0-9-]*/gu)) {- knownNodeOptions.add(option[0]);- if (signature.includes('=') && !signature.includes('[=')) nodeOptionsWithValues.add(option[0]);- }- }- }- return nodeOptionsWithValues.has(argument.replaceAll('_', '-'));-}--/**- * Whether Node's own CLI knows an option, so run-tests can forward it; any other dash option is a typo or a flag- * this runner cannot apply.- *- * @param {string} argument the option as written, with its value after an equals sign if any- * @returns {boolean}- */-function knownNodeOption(argument) {- const name = argument.split('=', 1)[0].replaceAll('_', '-');- optionTakesValue(name);- return knownNodeOptions.has(name);-}-/** Distinguish file selection from option values so filtered discovery still takes a full-suite slot. */function hasTestFiles(args) {let takesValue = false;@@ -1465,7 +1255,7 @@ async function executeTests(args, sandbox, gateMarkers, fullSuite, concurrency)selectedArgs = await longestFilesFirst(selectedArgs);const result = process.env[LANDING_RUN_ENV] === '1'? await runLandingTests(selectedArgs, env, concurrency)- : runTests(selectedArgs, env, concurrency);+ : await runTests(selectedArgs, env, concurrency);if (result.error) throw result.error;return selectedTestResult(result, env.PULLBOARD_TEST_SELECTION, patterns, explicitTestFiles(selectedArgs));}diff --git a/src/test-file-dispatch.js b/src/test-file-dispatch.jsnew file mode 100644index 00000000..0c1037f5--- /dev/null+++ b/src/test-file-dispatch.js@@ -0,0 +1,558 @@+/** Parent-controlled file admission and one structured result stream [C7]. */+/** @pullboard-module-loads ** */+// Reporter modules are caller-selected, just as in bin/run-tests.js.+import { spawnSync } from 'node:child_process';+import { devNull } from 'node:os';+import { fileURLToPath } from 'node:url';+import { execFileSync } from 'node:child_process';+import { channel } from 'node:diagnostics_channel';+import { performance } from 'node:perf_hooks';+import { createWriteStream } from 'node:fs';+import { availableParallelism } from 'node:os';+import { resolve } from 'node:path';+import { PassThrough } from 'node:stream';+import { run } from 'node:test';+import { pathToFileURL } from 'node:url';+import { testsNeedBrowser } from './browser-admission.js';+import { takeResource } from './resources.js';+import { reserveTestFileToken } from './test-file-pool.js';+import { TEST_FILE_POOL_ENV } from './test-file-pool.js';++/** Counter trailers are identified by their structured root-plan boundary, never by parsing test stdout/TAP. */+function trailer(event, afterPlan) {+ return afterPlan && event.type === 'test:diagnostic' && event.data.nesting === 0+ && /^(?:tests|suites|pass|fail|cancelled|skipped|todo|duration_ms) [0-9]/u.test(event.data.message);+}++/** One parent FIFO reservation at a time; transferred actual workers may remain live concurrently. */+function acquisitionQueue() {+ let tail = Promise.resolve();+ return async () => {+ const prior = tail;+ let release;+ tail = new Promise(done => { release = done; });+ await prior;+ return release;+ };+}++/** Feed one event to named live listeners, while ordered reporter data is published separately. */+function notify(stream, event) {+ stream.emit(event.type, event.data);+}++/** Set only this actual worker's legitimately reserved admission before its ordinary runner preload. */+function admissionImport(value, browser, runner) {+ const source = `process.env[${JSON.stringify(TEST_FILE_POOL_ENV)}]=${JSON.stringify(value)};`+ + (browser ? `process.env.PULLBOARD_TEST_BROWSER_OWNER_PID=${JSON.stringify(String(process.pid))};`+ + "if(process.env.PULLBOARD_TEST_BROWSER_BY_LAUNCH==='1')process.env.PULLBOARD_TEST_BROWSER_SLOT='held';"+ + 'delete process.env.PULLBOARD_TEST_BROWSER_BY_LAUNCH;' : '')+ + `await import(${JSON.stringify(pathToFileURL(runner).href)});`;+ return `data:text/javascript,${encodeURIComponent(source)}`;+}++/** Observe the actual native ChildProcess without changing spawn; its opaque bootstrap binds one file. */+function fileExitObserver(bootstrap) {+ const records = [];+ const source = channel('child_process');+ const observe = ({ process: child }) => {+ if (!child || typeof child.once !== 'function') return;+ records.push({ child, closed: new Promise(done => child.once('close', done)) });+ };+ source.subscribe(observe);+ let closing;+ return () => closing ??= (async () => {+ source.unsubscribe(observe);+ const owned = records.filter(({ child }) => child.spawnfile === process.execPath+ && child.spawnargs?.includes(bootstrap) && Number.isInteger(child.pid) && child.pid > 0);+ if (owned.length > 1) throw new Error('one file admission started more than one actual worker');+ await Promise.all(owned.map(record => record.closed));+ return owned.length;+ })();+}++/**+ * Dispatch each selected file only after its FIFO token, preserving browser-before-CPU admission and+ * the existing per-run concurrency ceiling. Public run() supplies real structured events and errors.+ * Reporter factories consume this single stream once; file stdout is never interpreted as a report.+ */+export function dispatchTestFiles(options, env) {+ const stream = new PassThrough({ objectMode: true });+ const controller = new AbortController();+ const abort = () => controller.abort(options.signal?.reason);+ if (options.signal?.aborted) abort();+ else options.signal?.addEventListener('abort', abort, { once: true });+ const signal = controller.signal;+ const files = options.selectedFiles ?? options.files ?? [];+ const limit = options.concurrency === false ? 1 : options.concurrency === true+ ? Math.max(1, availableParallelism() - 1) : options.concurrency ?? 1;+ const acquire = acquisitionQueue();+ const started = performance.now();+ const blocks = new Map();+ const summaries = [];+ let nextFile = 0;+ let nextBlock = 0;+ let topLevel = 0;+ options.setup?.(stream);++ /** Publish complete file blocks in discovery order without changing nested numbers or Error objects. */+ function flush() {+ while (blocks.has(nextBlock)) {+ const { events, summary } = blocks.get(nextBlock);+ blocks.delete(nextBlock++);+ for (const event of events) {+ const data = event.data;+ stream.write(data.nesting === 0 && Number.isInteger(data.testNumber)+ ? { ...event, data: { ...data, testNumber: data.testNumber + topLevel } } : event);+ }+ topLevel += summary?.counts?.topLevel ?? 0;+ }+ }++ /** Browser waits happen before reserving CPU, and their real lease lasts through the file's exit. */+ async function browserLease(file) {+ if (!env.PULLBOARD_TEST_BROWSER_RESOURCE) return null;+ const byLaunch = env.PULLBOARD_TEST_BROWSER_BY_LAUNCH === '1';+ if (!await testsNeedBrowser(process.cwd(), [file], { byLaunch })) return null;+ signal.throwIfAborted();+ return takeResource({ name: env.PULLBOARD_TEST_BROWSER_RESOURCE,+ capacity: Number(env.PULLBOARD_TEST_BROWSER_CAPACITY), kind: 'test-browser-file', root: process.cwd(),+ onWait() { signal.throwIfAborted(); } });+ }++ /** Start and consume one real file; returning requires its actual process to have exited. */+ async function fileRun(file, index) {+ let browser;+ let token;+ let unlock;+ let drained;+ let fileExited;+ try {+ browser = await browserLease(file);+ signal.throwIfAborted();+ unlock = await acquire();+ signal.throwIfAborted();+ token = await reserveTestFileToken(env, signal);+ signal.throwIfAborted();+ const execArgv = [...options.execArgv];+ const preload = execArgv.indexOf('--import');+ if (preload < 0 || !execArgv[preload + 1]) throw new Error('test-file dispatch requires its ordinary runner preload');+ execArgv[preload + 1] = admissionImport(token.value, browser, execArgv[preload + 1]);+ fileExited = fileExitObserver(execArgv[preload + 1]);+ const source = run({ ...options, files: [file], shard: undefined, concurrency: 1, signal,+ setup: undefined, execArgv });+ source.once('error', error => controller.abort(error));+ const events = [];+ let summary;+ let afterPlan = false;+ drained = (async () => {+ for await (const event of source) {+ if (event.type === 'test:plan' && event.data.nesting === 0) { afterPlan = true; continue; }+ if (event.type === 'test:summary' && !event.data.file) { summary = event.data; continue; }+ if (trailer(event, afterPlan)) continue;+ notify(stream, event);+ events.push(event);+ }+ if (!summary) throw new Error(`test file ended without a native summary: ${file}`);+ const actualWorkers = await fileExited();+ if (summary.success && actualWorkers !== 1) throw new Error(`test file has no actual worker exit receipt: ${file}`);+ summaries.push(summary);+ blocks.set(index, { events, summary });+ flush();+ })();+ // A failed import can exit before adopting; cleanup of the still-reserved place also unblocks transfer.+ const completed = drained.finally(async () => { await fileExited(); await token.drained(); token.release(); });+ completed.catch(() => {});+ await Promise.race([token.transferred(), completed]);+ unlock();+ unlock = null;+ await completed;+ } finally {+ try { if (drained) await drained; }+ finally {+ try { await fileExited?.(); await token?.drained(); token?.release(); }+ finally { unlock?.(); browser?.release(); }+ }+ }+ }++ /** Keep at most the configured number of files active; aborted queued files are never spawned. */+ async function lane() {+ while (!signal.aborted && nextFile < files.length) {+ const index = nextFile++;+ await fileRun(files[index], index);+ }+ }++ /** Emit one combined native-style summary after every actual file and borrowed descendant has drained. */+ async function finish() {+ try {+ const work = Array.from({ length: Math.min(limit, files.length) }, () => lane().catch(error => {+ controller.abort(error);+ throw error;+ }));+ const outcomes = await Promise.allSettled(work);+ const error = outcomes.find(outcome => outcome.status === 'rejected' && outcome.reason?.name !== 'AbortError');+ if (error) throw error.reason;+ const counts = { tests: 0, suites: 0, passed: 0, failed: 0, cancelled: 0, skipped: 0, todo: 0, topLevel: 0 };+ for (const summary of summaries) for (const key of Object.keys(counts)) counts[key] += summary.counts[key] ?? 0;+ const duration = performance.now() - started;+ const summary = { counts, duration_ms: duration, success: !signal.aborted && summaries.every(value => value.success) };+ const final = [{ type: 'test:plan', data: { nesting: 0, count: counts.topLevel } }];+ for (const [name, key] of [['tests', 'tests'], ['suites', 'suites'], ['pass', 'passed'], ['fail', 'failed'],+ ['cancelled', 'cancelled'], ['skipped', 'skipped'], ['todo', 'todo']]) {+ final.push({ type: 'test:diagnostic', data: { nesting: 0, message: `${name} ${counts[key]}` } });+ }+ final.push({ type: 'test:diagnostic', data: { nesting: 0, message: `duration_ms ${duration}` } });+ final.push({ type: 'test:summary', data: summary });+ for (const event of final) { notify(stream, event); stream.write(event); }+ stream.end();+ } catch (error) { stream.destroy(error); }+ finally { options.signal?.removeEventListener('abort', abort); }+ }+ queueMicrotask(finish);+ return stream;+}++/** Native mode keeps running after a test failure and initializes each public/internal reporter exactly once. */+export async function reportDispatchedTests(options, env) {+ const controller = new AbortController();+ const inputs = [];+ const ends = [];+ let reporterError;+ let successful = false;+ const originalEnv = { ...process.env };+ /** Reporter failures stop new dispatch while existing actual workers drain through their normal signal path. */+ const failed = error => { reporterError ??= error; controller.abort(error); };+ try {+ for (const entry of options.reporters) {+ const builtin = ['tap', 'spec', 'dot', 'junit', 'lcov'].includes(entry.reporter);+ const reporter = builtin ? (await import('node:test/reporters'))[entry.reporter]+ : (await import(entry.reporter.startsWith('.') || entry.reporter.startsWith('/')+ ? pathToFileURL(resolve(entry.reporter)).href : entry.reporter)).default;+ if (typeof reporter !== 'function') throw new Error(`unknown test reporter: ${entry.reporter}`);+ const input = new PassThrough({ objectMode: true });+ const output = input.compose(reporter);+ const terminal = entry.destination === 'stdout' || entry.destination === 'stderr';+ const destination = entry.destination === 'stdout' ? process.stdout+ : entry.destination === 'stderr' ? process.stderr : createWriteStream(resolve(entry.destination));+ const readable = new Promise(done => { output.once('end', done); output.once('close', done); });+ const writable = terminal ? Promise.resolve() : new Promise(done => {+ destination.once('finish', done);+ destination.once('error', error => { failed(error); done(); });+ });+ output.once('error', failed);+ destination.once('error', failed);+ output.pipe(destination);+ ends.push(Promise.all([readable, writable]).finally(() => destination.off('error', failed)));+ inputs.push(input);+ }+ for (const key of Object.keys(process.env)) delete process.env[key];+ Object.assign(process.env, env);+ const source = dispatchTestFiles({ ...options, signal: controller.signal,+ setup(tests) {+ tests.on('test:summary', summary => { if (!summary.file) successful = summary.success; });+ } }, env);+ try {+ for await (const event of source) for (const input of inputs) input.write(event);+ } catch (error) { failed(error); }+ } finally {+ for (const input of inputs) input.end();+ await Promise.all(ends);+ for (const key of Object.keys(process.env)) delete process.env[key];+ Object.assign(process.env, originalEnv);+ }+ if (reporterError) throw reporterError;+ return { status: successful ? 0 : 1, signal: null };+}++/** Preserve native CLI option placement, aliases and validation before public dispatch. */+export function nativeFileArguments(args, concurrency, knownNodeOption, optionTakesValue) {+ let files = concurrency.files;+ // Node reads an option only before the files, so every option is placed ahead of them, wherever it was written (#931).+ const placed = [];+ const selectors = [];+ for (let at = 0; at < args.length; at += 1) {+ const argument = args[at];+ if (argument === '--') { selectors.push(...args.slice(at)); break; }+ if (/^--test[-_]name[-_]pattern(?:=|$)/u.test(argument)) {+ const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];+ if (value === undefined) throw new Error('--test-name-pattern requires a value');+ placed.push(`--test-name-pattern=${value}`);+ } else if (/^--test[-_]skip[-_]pattern(?:=|$)/u.test(argument)) {+ const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];+ if (value === undefined) throw new Error('--test-skip-pattern requires a value');+ placed.push(`--test-skip-pattern=${value}`);+ } else if (/^--test[-_]concurrency(?:=|$)/u.test(argument)) {+ const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];+ if (!/^[1-9][0-9]*$/u.test(value ?? '') || !Number.isSafeInteger(Number(value))) {+ throw new Error('--test-concurrency must be a positive integer');+ }+ files = Math.min(files, Number(value));+ } else if (/^--(?:experimental[-_])?test[-_]isolation(?:=none|$)/u.test(argument) &&+ (argument.endsWith('=none') || args[at + 1] === 'none')) {+ throw new Error('bounded browser test files require process isolation; remove --test-isolation=none');+ } else if (argument === '--test-only') {+ // Node would apply it only to cases marked only, so a run would silently skip the rest of the file (#931).+ throw new Error('unknown option --test-only; run-tests does not apply it, so select cases with --test-name-pattern');+ } else if (argument.startsWith('-')) {+ // An option Node does not know would be ignored; name it instead of running without it (#931).+ if (!knownNodeOption(argument)) throw new Error(`unknown option ${argument.split('=', 1)[0]}; check its spelling, or drop it`);+ placed.push(argument);+ if (!argument.includes('=') && optionTakesValue(argument)) {+ if (args[at + 1] === undefined) throw new Error(`${argument} requires a value`);+ placed.push(args[++at]);+ }+ } else selectors.push(argument);+ }+ return { args: [...placed, ...selectors], concurrency: { ...concurrency, files } };+}++/** Convert the validated test CLI into public run() options and one shared reporter configuration. */+export async function testFileOptions(args, env, concurrency, { runner, optionTakesValue, knownNodeOption }) {+ const options = { concurrency: true, isolation: 'process', execArgv: ['--disable-warning=ExperimentalWarning', '--import', runner] };+ const reporters = [];+ const selectors = [];+ const patterns = [];+ const skipPatterns = [];+ const coverageExcludeGlobs = [];+ const coverageIncludeGlobs = [];+ const execArgv = [];+ let expectValue = null;+ for (let index = 0; index < args.length; index += 1) {+ let argument = args[index];+ if (expectValue) {+ expectValue(argument);+ expectValue = null;+ continue;+ }+ if (argument === '--') { selectors.push(...args.slice(index + 1)); break; }+ const valueOption = (name, take) => {+ if (argument === name) { expectValue = take; return true; }+ if (argument.startsWith(`${name}=`)) { take(argument.slice(name.length + 1)); return true; }+ return false;+ };+ if (valueOption('--test-concurrency', value => {+ if (value === 'true') options.concurrency = true;+ else if (value === 'false') options.concurrency = false;+ else if (/^[1-9][0-9]*$/u.test(value)) {+ const requested = Number(value);+ // Validate before clamping: Node's run() accepts a positive uint32, not Infinity or larger integers.+ if (!Number.isSafeInteger(requested) || requested > 4_294_967_295) {+ throw new Error(`invalid --test-concurrency value: ${value}`);+ }+ options.concurrency = requested;+ } else throw new Error(`invalid --test-concurrency value: ${value}`);+ })) continue;+ if (valueOption('--test-name-pattern', value => patterns.push(value))) continue;+ if (valueOption('--test-skip-pattern', value => skipPatterns.push(value))) continue;+ if (valueOption('--test-timeout', value => { options.timeout = Number(value); })) continue;+ if (valueOption('--test-reporter', value => reporters.push(value))) continue;+ if (valueOption('--test-reporter-destination', value => { (options.reporterDestinations ??= []).push(value); })) continue;+ if (valueOption('--test-coverage-include', value => coverageIncludeGlobs.push(value))) continue;+ if (valueOption('--test-coverage-exclude', value => coverageExcludeGlobs.push(value))) continue;+ if (valueOption('--test-coverage-lines', value => { options.lineCoverage = Number(value); })) continue;+ if (valueOption('--test-coverage-branches', value => { options.branchCoverage = Number(value); })) continue;+ if (valueOption('--test-coverage-functions', value => { options.functionCoverage = Number(value); })) continue;+ // Parsed but never applied, so a run would pass silently without the cases it claims: refuse it by name (#931).+ if (argument === '--test-only') throw new Error('unknown option --test-only; run-tests does not apply it, so select cases with --test-name-pattern');+ if (argument === '--experimental-test-coverage') { options.coverage = true; continue; }+ if (argument.startsWith('--experimental-test-isolation=')) {+ options.isolation = argument.slice(argument.indexOf('=') + 1);+ continue;+ }+ if (argument.startsWith('--test-isolation=')) {+ options.isolation = argument.slice(argument.indexOf('=') + 1);+ continue;+ }+ if (argument === '--test-isolation') {+ expectValue = value => { options.isolation = value; };+ continue;+ }+ if (argument === '--experimental-test-isolation') {+ expectValue = value => { options.isolation = value; };+ continue;+ }+ if (argument === '--test-shard') { index += 1; continue; }+ if (argument.startsWith('--test-shard=')) continue;+ if (argument.startsWith('-')) {+ // An option Node does not know would be forwarded and ignored after the files; refuse it by name wherever it appears (#931).+ const name = argument.split('=', 1)[0].replaceAll('_', '-');+ optionTakesValue(name);+ if (!knownNodeOption(name)) throw new Error(`unknown option ${name}; check its spelling, or drop it`);+ execArgv.push(argument);+ if (!argument.includes('=') && optionTakesValue(argument)) {+ if (args[index + 1] === undefined) throw new Error(`${argument} requires a value`);+ execArgv.push(args[++index]);+ }+ continue;+ }+ selectors.push(argument);+ }+ if (expectValue) throw new Error('missing value for a Node test runner option');+ options.execArgv = [...options.execArgv, ...execArgv];+ // Keep native run() from treating true/default or a larger explicit value as unbounded.+ if (options.concurrency !== false) {+ const requested = options.concurrency === true ? concurrency.files : options.concurrency;+ options.concurrency = Math.min(concurrency.files, requested);+ }+ if (selectors.length) {+ const { glob } = await import('node:fs/promises');+ const files = [];+ for (const selector of selectors) {+ if (!/[?*[{]/u.test(selector)) {+ files.push(resolve(selector));+ continue;+ }+ const matches = [];+ for await (const match of glob(selector, { cwd: process.cwd() })) matches.push(resolve(match));+ if (!matches.length) throw new Error(`test file pattern matched no files: ${selector}`);+ files.push(...matches.sort());+ }+ options.files = [...new Set(files)];+ }+ if (patterns.length) options.testNamePatterns = patterns;+ if (skipPatterns.length) options.testSkipPatterns = skipPatterns;+ if (coverageExcludeGlobs.length) options.coverageExcludeGlobs = coverageExcludeGlobs;+ if (coverageIncludeGlobs.length) options.coverageIncludeGlobs = coverageIncludeGlobs;+ if (args.includes('--test-shard') || args.some(argument => argument.startsWith('--test-shard='))) {+ const shardArgument = args.find(argument => argument === '--test-shard' || argument.startsWith('--test-shard='));+ const shard = shardArgument === '--test-shard' ? args[args.indexOf(shardArgument) + 1] : shardArgument.slice('--test-shard='.length);+ const match = shard?.match(/^([1-9][0-9]*)\/([1-9][0-9]*)$/u);+ if (!match) throw new Error(`invalid --test-shard value: ${shard ?? '(missing)'}`);+ options.shard = { index: Number(match[1]), total: Number(match[2]) };+ }+ let selectedCandidates = options.files;+ if (!selectedCandidates) {+ const { glob } = await import('node:fs/promises');+ const defaults = ['**/*.test.{cjs,mjs,js}', '**/*-test.{cjs,mjs,js}', '**/*_test.{cjs,mjs,js}',+ '**/test-*.{cjs,mjs,js}', '**/test.{cjs,mjs,js}', '**/test/**/*.{cjs,mjs,js}'];+ const matches = new Set();+ for (const pattern of defaults) {+ for await (const file of glob(pattern, { cwd: process.cwd() })) {+ if (!file.split(/[\\/]/u).includes('node_modules')) matches.add(resolve(file));+ }+ }+ selectedCandidates = [...matches].sort();+ }+ options.selectedFiles = selectedCandidates.filter((_file, index) => !options.shard || index % options.shard.total === options.shard.index - 1);+ if (!reporters.length) reporters.push('tap');+ const destinations = options.reporterDestinations ?? (reporters.length === 1 ? ['stdout'] : []);+ if (destinations.length !== reporters.length) throw new Error('each --test-reporter requires a matching --test-reporter-destination');+ options.reporters = reporters.map((reporter, index) => ({ reporter, destination: destinations[index] }));+ delete options.reporterDestinations;+ return { options, env };+}+++let nodeOptionsWithValues;+let knownNodeOptions;++/** Read Node option arity from this runtime so supported option values cannot bypass the machine pool. */+export function optionTakesValue(argument) {+ if (!nodeOptionsWithValues) {+ nodeOptionsWithValues = new Set();+ knownNodeOptions = new Set();+ const help = execFileSync(process.execPath, ['--help'], { encoding: 'utf8' });+ for (const line of help.split(/\r?\n/u)) {+ const signature = line.match(/^ (-\S.*?)(?: {2,}|$)/u)?.[1];+ if (!signature) continue;+ for (const option of signature.matchAll(/--?[A-Za-z][A-Za-z0-9-]*/gu)) {+ knownNodeOptions.add(option[0]);+ if (signature.includes('=') && !signature.includes('[=')) nodeOptionsWithValues.add(option[0]);+ }+ }+ }+ return nodeOptionsWithValues.has(argument.replaceAll('_', '-'));+}++/**+ * Whether Node's own CLI knows an option, so run-tests can forward it; any other dash option is a typo or a flag+ * this runner cannot apply.+ *+ * @param {string} argument the option as written, with its value after an equals sign if any+ * @returns {boolean}+ */+export function knownNodeOption(argument) {+ const name = argument.split('=', 1)[0].replaceAll('_', '-');+ optionTakesValue(name);+ return knownNodeOptions.has(name);+}+++/** Special native modes keep their existing single native orchestrator and aggregate coverage/watch behavior. */+export function legacyFileMode(args) {+ for (let index = 0; index < args.length; index++) {+ const argument = args[index];+ if (argument === '--') break;+ if (!argument.startsWith('-')) continue;+ const normalized = argument.replaceAll('_', '-');+ if (/^--(?:watch(?:-|=|$)|experimental-test-coverage(?:=|$)|test-coverage(?:-|=|$))/u.test(normalized)) return true;+ if (!argument.includes('=') && optionTakesValue(argument)) index++;+ }+ return false;+}++/** Retained native CLI path for watch and coverage; normal file runs use parent dispatch. */+export function legacyNativeFileRun(args, env, concurrency, { runner, knownNodeOption, optionTakesValue }) {+ let files = concurrency.files;+ // Node reads an option only before the files, so every option is placed ahead of them, wherever it was written (#931).+ const placed = [];+ const selectors = [];+ for (let at = 0; at < args.length; at += 1) {+ const argument = args[at];+ if (argument === '--') { selectors.push(...args.slice(at)); break; }+ if (/^--test[-_]name[-_]pattern(?:=|$)/u.test(argument)) {+ const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];+ if (value === undefined) throw new Error('--test-name-pattern requires a value');+ placed.push(`--test-name-pattern=${value}`);+ } else if (/^--test[-_]skip[-_]pattern(?:=|$)/u.test(argument)) {+ const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];+ if (value === undefined) throw new Error('--test-skip-pattern requires a value');+ placed.push(`--test-skip-pattern=${value}`);+ } else if (/^--test[-_]concurrency(?:=|$)/u.test(argument)) {+ const value = argument.includes('=') ? argument.slice(argument.indexOf('=') + 1) : args[++at];+ if (!/^[1-9][0-9]*$/u.test(value ?? '') || !Number.isSafeInteger(Number(value))) {+ throw new Error('--test-concurrency must be a positive integer');+ }+ files = Math.min(files, Number(value));+ } else if (/^--(?:experimental[-_])?test[-_]isolation(?:=none|$)/u.test(argument) &&+ (argument.endsWith('=none') || args[at + 1] === 'none')) {+ throw new Error('bounded browser test files require process isolation; remove --test-isolation=none');+ } else if (argument === '--test-only') {+ // Node would apply it only to cases marked only, so a run would silently skip the rest of the file (#931).+ throw new Error('unknown option --test-only; run-tests does not apply it, so select cases with --test-name-pattern');+ } else if (argument.startsWith('-')) {+ // An option Node does not know would be ignored; name it instead of running without it (#931).+ if (!knownNodeOption(argument)) throw new Error(`unknown option ${argument.split('=', 1)[0]}; check its spelling, or drop it`);+ placed.push(argument);+ if (!argument.includes('=') && optionTakesValue(argument)) {+ if (args[at + 1] === undefined) throw new Error(`${argument} requires a value`);+ placed.push(args[++at]);+ }+ } else selectors.push(argument);+ }+ const reporter = args.some((argument) => argument === '--test-reporter' || argument.startsWith('--test-reporter='));+ const selectionReporter = [];+ if (env.PULLBOARD_TEST_SELECTION) {+ const reporters = placed.filter(argument => /^--test-reporter(?:=|$)/u.test(argument)).length;+ const destinations = placed.filter(argument => /^--test-reporter-destination(?:=|$)/u.test(argument)).length;+ if (destinations && destinations !== (reporters || 1)) throw new Error('each --test-reporter requires a matching --test-reporter-destination');+ if (reporter && !destinations) for (let index = 0; index < reporters; index++) selectionReporter.push('--test-reporter-destination=stdout');+ if (!reporter && !env.PULLBOARD_TEST_ACTIONS && !destinations) selectionReporter.push('--test-reporter-destination=stdout');+ selectionReporter.push(`--test-reporter=${fileURLToPath(new URL('../src/test-selection.js', import.meta.url))}`, `--test-reporter-destination=${devNull}`);+ }+ const nodeArgs = [+ '--disable-warning=ExperimentalWarning',+ '--test',+ `--test-concurrency=${files}`,+ ...(!reporter ? ['--test-reporter=tap', ...(env.PULLBOARD_TEST_ACTIONS ? ['--test-reporter-destination=stdout', `--test-reporter=${fileURLToPath(new URL('../src/test-action-reporter.js', import.meta.url))}`, `--test-reporter-destination=${devNull}`] : [])] : []),+ '--import',+ runner,+ ...placed,+ ...selectionReporter,+ ...selectors,+ ];+ return spawnSync(process.execPath, nodeArgs, { env, stdio: 'inherit' });+}+diff --git a/src/test-file-pool.js b/src/test-file-pool.jsindex 78af2603..25fe49e6 100644--- a/src/test-file-pool.js+++ b/src/test-file-pool.js@@ -113,6 +113,7 @@ async function openPool(home) {addColumn('file_call', 'closing', 'INTEGER NOT NULL DEFAULT 0');addColumn('file_call', 'path', 'TEXT');addColumn('file_holder', 'path', 'TEXT');+ addColumn('file_holder', 'dispatch', 'TEXT');return db;}@@ -216,11 +217,24 @@ export async function takeTestFileToken(env = process.env) {if (!pool) return 0;const asked = Date.now();const db = await openPool(pool.home);- immediate(db, () => db.prepare('INSERT OR IGNORE INTO file_waiter(pid) VALUES (?)').run(process.pid));+ const dispatched = pool.dispatch;+ if (dispatched) {+ if (!Number.isSafeInteger(dispatched.parent) || dispatched.parent !== process.ppid+ || typeof dispatched.nonce !== 'string' || !dispatched.nonce) {+ db.close();+ throw new Error('test-file admission does not name this worker parent');+ }+ const adopted = immediate(db, () => db.prepare('UPDATE file_holder SET pid = ? WHERE pid = ? AND dispatch = ?')+ .run(process.pid, dispatched.parent, dispatched.nonce).changes);+ if (adopted !== 1) {+ db.close();+ throw new Error('test-file admission was lost before worker startup');+ }+ } else immediate(db, () => db.prepare('INSERT OR IGNORE INTO file_waiter(pid) VALUES (?)').run(process.pid));let pruned = 0;let reported = false;let path;- while (true) {+ while (!dispatched) {const { taken, ahead } = standing(db);// A dead holder only goes in the write path, so look there at least once a second even when nothing looks free.if (pool.call || ahead < pool.cores - taken || Date.now() - pruned >= PRUNE_MS) {@@ -235,8 +249,13 @@ export async function takeTestFileToken(env = process.env) {}await new Promise(done => setTimeout(done, POLL_MS));}+ if (dispatched) path = db.prepare('SELECT path FROM file_holder WHERE pid = ?').get(process.pid)?.path ?? '';process.once('exit', () => {- try { db.prepare('DELETE FROM file_holder WHERE pid = ?').run(process.pid); db.close(); }+ try {+ // The dispatcher's row stays charged through every exit listener until the actual process has exited.+ if (!dispatched) db.prepare('DELETE FROM file_holder WHERE pid = ?').run(process.pid);+ db.close();+ }catch { /* A row left behind is pruned once this process is gone. */ }});env[TEST_FILE_POOL_ENV] = JSON.stringify({ home: pool.home, cores: pool.cores });@@ -246,6 +265,67 @@ export async function takeTestFileToken(env = process.env) {return Date.now() - asked;}+/**+ * Reserve one FIFO place before the dispatcher starts a file. A dispatcher has only one pending reservation;+ * its actual file worker atomically adopts that existing row, preserving the lender/call/path and charge.+ * This uses the same holder/waiter tables, not a second capacity pool or run registry.+ */+export async function reserveTestFileToken(env, signal) {+ if (ownsFileToken) throw new Error('a test file must start a separate runner before dispatching nested files');+ const pool = JSON.parse(testFilePoolValue(env));+ const db = await openPool(pool.home);+ const nonce = randomUUID();+ let reserved = false;+ try {+ signal?.throwIfAborted();+ immediate(db, () => db.prepare('INSERT INTO file_waiter(pid) VALUES (?)').run(process.pid));+ while (!reserved) {+ signal?.throwIfAborted();+ reserved = admit(db, pool.cores, pool.lender, pool.call) !== false;+ if (!reserved) await new Promise(done => setTimeout(done, POLL_MS));+ }+ db.prepare('UPDATE file_holder SET dispatch = ? WHERE pid = ?').run(nonce, process.pid);+ const owned = () => db.prepare('SELECT pid FROM file_holder WHERE dispatch = ?').get(nonce);+ let closed = false;+ return {+ value: JSON.stringify({ ...pool, dispatch: { parent: process.pid, nonce } }),+ /** Do not acquire another place under this PID until the actual worker has adopted the first. */+ async transferred() {+ while (!closed && owned()?.pid === process.pid) {+ signal?.throwIfAborted();+ await new Promise(done => setTimeout(done, POLL_MS));+ }+ },+ /** A stream error does not authorize returning a token that its worker still owns. */+ async drained() {+ while (!closed) {+ const holder = owned();+ if (!holder || holder.pid === process.pid || !alive(holder.pid)) return;+ await new Promise(done => setTimeout(done, POLL_MS));+ }+ },+ /** Release only after the actual worker exits, or before any file was started. */+ release() {+ if (closed) return;+ const holder = owned();+ if (holder && holder.pid !== process.pid && alive(holder.pid)) {+ throw new Error('cannot release a test-file token while its actual worker is alive');+ }+ db.prepare('DELETE FROM file_holder WHERE dispatch = ?').run(nonce);+ db.close();+ closed = true;+ },+ };+ } catch (error) {+ immediate(db, () => {+ db.prepare('DELETE FROM file_waiter WHERE pid = ?').run(process.pid);+ if (reserved) db.prepare('DELETE FROM file_holder WHERE pid = ?').run(process.pid);+ });+ db.close();+ throw error;+ }+}+/** Block this thread for a moment without spinning: the caller is inside a synchronous call anyway. */function pauseSync(ms) {Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);diff --git a/test/run-tests-reporter.test.js b/test/run-tests-reporter.test.jsindex 62924b77..f07e3461 100644--- a/test/run-tests-reporter.test.js+++ b/test/run-tests-reporter.test.js@@ -1,7 +1,9 @@/** Keep the production runner's report stable across Node versions and caller options [C7]. */import assert from 'node:assert/strict';import { runFixtureChild as spawnSync } from './fixture-child.js';-import { mkdtempSync, rmSync, writeFileSync, readFileSync } from 'node:fs';+import { runFixtureChildAsync } from './fixture-child.js';+import { awaitNestedRun } from '../src/test-file-pool.js';+import { mkdtempSync, rmSync, writeFileSync, readFileSync, existsSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { fileURLToPath } from 'node:url';@@ -14,22 +16,12 @@ function runReporterFixture(t, reporterArgs = [], extraEnv = {}) {const root = mkdtempSync(join(tmpdir(), 'pullboard-run-tests-reporter-'));t.after(() => rmSync(root, { recursive: true, force: true }));const fixture = join(root, 'reporter.test.js');- const preload = join(root, 'capture-args.mjs');const capture = join(root, 'child-args.json');writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));- writeFileSync(fixture, "import { test } from 'node:test';\ntest('reporter fixture [C7]', () => {});\n");- writeFileSync(preload, `import cp from 'node:child_process';-import fs from 'node:fs';-import { syncBuiltinESMExports } from 'node:module';-const original = cp.spawnSync;-/** Record the runner's actual child argv before allowing the test process to start. */-cp.spawnSync = function captureNodeTestArgs(file, args, options) {- if (String(file) === process.execPath && args?.includes('--test')) {- fs.writeFileSync(process.env.PULLBOARD_REPORTER_CAPTURE_FILE, JSON.stringify(args));- }- return original.call(this, file, args, options);-};-syncBuiltinESMExports();+ writeFileSync(fixture, `import { test } from 'node:test';+import { writeFileSync } from 'node:fs';+writeFileSync(process.env.PULLBOARD_REPORTER_CAPTURE_FILE, JSON.stringify(process.execArgv));+test('reporter fixture [C7]', () => {});`);const result = spawnSync(process.execPath, [runner, ...reporterArgs, fixture], {@@ -37,7 +29,6 @@ syncBuiltinESMExports();env: {...process.env,...extraEnv,- NODE_OPTIONS: `--import="${preload}"`,PULLBOARD_REPORTER_CAPTURE_FILE: capture,},encoding: 'utf8',@@ -52,20 +43,21 @@ test('run-tests prints TAP on every Node version and keeps a caller reporter [C7assert.equal(defaultReporter.result.status, 0, defaultReporter.output);assert.match(defaultReporter.output, /^TAP version \d+/mu);assert.match(defaultReporter.output, /^ok \d+ - reporter fixture \[C7\]/mu);- assert.ok(defaultReporter.childArgs.includes('--test-reporter=tap'), 'the production child receives the explicit TAP reporter');+ assert.ok(!defaultReporter.childArgs.some(arg => arg.startsWith('--test-reporter')),+ 'the parent owns the TAP reporter rather than restarting it in each file');const callerReporter = runReporterFixture(t, ['--test-reporter=spec']);assert.equal(callerReporter.result.status, 0, callerReporter.output);- assert.ok(callerReporter.childArgs.includes('--test-reporter=spec'), 'the caller option reaches the production child');+ assert.ok(!callerReporter.childArgs.some(arg => arg.startsWith('--test-reporter')),+ 'the selected spec reporter belongs to the parent');assert.ok(!callerReporter.childArgs.includes('--test-reporter=tap'), 'the default does not override a caller reporter');assert.doesNotMatch(callerReporter.output, /^TAP version \d+/mu);assert.match(callerReporter.output, /reporter fixture \[C7\]/u);const splitCallerReporter = runReporterFixture(t, ['--test-reporter', 'spec']);assert.equal(splitCallerReporter.result.status, 0, splitCallerReporter.output);- const splitReporterIndex = splitCallerReporter.childArgs.indexOf('--test-reporter');- assert.ok(splitReporterIndex >= 0, 'the separate reporter flag reaches the child');- assert.equal(splitCallerReporter.childArgs[splitReporterIndex + 1], 'spec');+ assert.ok(!splitCallerReporter.childArgs.some(arg => arg.startsWith('--test-reporter')),+ 'the separate-value reporter option selects the parent reporter');assert.ok(!splitCallerReporter.childArgs.includes('--test-reporter=tap'), 'the default does not override the separate caller option');assert.doesNotMatch(splitCallerReporter.output, /^TAP version \d+/mu);});@@ -79,9 +71,147 @@ test('run-tests never injects timing reporters beside caller reporter options [Cconst run = runReporterFixture(t, reporterArgs, { PULLBOARD_TEST_TIMING_PROFILE: join(tmpdir(), 'must-not-inject') });assert.equal(run.result.status, 0, run.output);const preload = run.childArgs.indexOf('--import');- assert.equal(run.childArgs[preload + 1], runner, 'the production watchdog preload reaches Node');- assert.deepEqual(run.childArgs.slice(preload + 2, -1), reporterArgs, 'only the exact caller reporter flags reach Node');+ const bootstrap = decodeURIComponent(run.childArgs[preload + 1]);+ assert.ok(bootstrap.includes(new URL('../bin/run-tests.js', import.meta.url).href),+ 'the actual worker admission bootstrap imports the ordinary production watchdog');+ assert.ok(!run.childArgs.some(arg => arg.startsWith('--test-reporter')),+ 'caller reporters run once in the parent, without injected worker timing reporters');assert.match(run.output, /ℹ tests 1/u);assert.doesNotMatch(run.output, /gate-profile-reporter|"files":/u);}});++test('multiple dispatched files share one reporter factory and one truthful summary [C7,V10]', t => {+ const root = mkdtempSync(join(tmpdir(), 'pullboard-dispatch-reporters-'));+ t.after(() => rmSync(root, { recursive: true, force: true }));+ writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));+ writeFileSync(join(root, 'one.test.js'), `import { test } from 'node:test';+test('outer suite', async t => {+ await t.test('nested case', () => { console.log('TAP version 13\\n# pass 999\\n# fail 999'); });+});+test.skip('skipped case', () => {});+test.todo('todo case');+`);+ writeFileSync(join(root, 'two.test.js'), "import { test } from 'node:test';\ntest('other file case', () => {});\n");+ const calls = join(root, 'reporter-calls.jsonl');+ writeFileSync(join(root, 'custom.mjs'), `import { appendFileSync } from 'node:fs';+/** Observe the public structured stream, without manufacturing test outcomes or interpreting stdout. */+export default async function* observe(events) {+ appendFileSync(${JSON.stringify(calls)}, JSON.stringify({ type: 'start', pid: process.pid }) + '\\n');+ for await (const event of events) {+ if (event.type === 'test:pass') yield event.data.name + '\\n';+ if (event.type === 'test:summary' && !event.data.file) {+ appendFileSync(${JSON.stringify(calls)}, JSON.stringify({ type: 'summary', ...event.data }) + '\\n');+ }+ }+ appendFileSync(${JSON.stringify(calls)}, JSON.stringify({ type: 'end' }) + '\\n');+}+`);+ const output = join(root, 'custom.txt');+ const result = spawnSync(process.execPath, [runner, '--test-reporter=tap', '--test-reporter=./custom.mjs',+ '--test-reporter-destination=stdout', `--test-reporter-destination=${output}`, 'one.test.js', 'two.test.js'],+ { cwd: root, encoding: 'utf8' });+ assert.equal(result.status, 0, result.context);+ for (const line of ['# pass 3', '# fail 0', '# skipped 1', '# todo 1']) {+ assert.equal(result.stdout.split('\n').filter(value => value === line).length, 1, `one native aggregate ${line}`);+ }+ assert.match(result.stdout, /pass 999/u, 'embedded TAP remains visible test stdout');+ assert.match(result.stdout, /^ok 4 - other file case$/mu, 'root numbering continues across files');+ assert.match(result.stdout, /^\s+ok 1 - nested case$/mu, 'nested numbering stays local to its suite');+ const events = readFileSync(calls, 'utf8').trim().split('\n').map(line => JSON.parse(line));+ assert.equal(events.filter(event => event.type === 'start').length, 1, 'custom reporter starts once');+ assert.equal(events.filter(event => event.type === 'end').length, 1, 'custom reporter finalizes once');+ const summaries = events.filter(event => event.type === 'summary');+ assert.equal(summaries.length, 1, 'custom reporter receives one aggregate summary');+ assert.deepEqual(summaries[0].counts, { tests: 5, suites: 0, passed: 3, failed: 0,+ cancelled: 0, skipped: 1, todo: 1, topLevel: 4 });+ assert.equal(readFileSync(output, 'utf8').split('\n').filter(name => name === 'other file case').length, 1);+});++/** Record the actual legacy native invocation; watch additionally publishes its own real PID/parent binding. */+function legacyObserver(root, capture, watch) {+ const path = join(root, 'observe-native.mjs');+ writeFileSync(path, `import cp from 'node:child_process';+import { writeFileSync } from 'node:fs';+import { syncBuiltinESMExports } from 'node:module';+if (process.argv[1] === ${JSON.stringify(runner)}) {+ const original = cp.spawnSync;+ cp.spawnSync = function observe(command, args, options) {+ if (command === process.execPath && args?.includes('--test')) {+ writeFileSync(${JSON.stringify(capture)}, JSON.stringify({ pid: process.pid, args }));+ }+ return original.call(this, command, args, options);+ };+ syncBuiltinESMExports();+} else if (process.execArgv.includes('--watch')) {+ writeFileSync(${JSON.stringify(watch)}, JSON.stringify({ pid: process.pid, parentPid: process.ppid }));+}+`);+ return path;+}++test('watch and aggregate coverage retain their single native orchestrator [C7]', async t => {+ const root = mkdtempSync(join(tmpdir(), 'pullboard-native-special-modes-'));+ t.after(() => rmSync(root, { recursive: true, force: true }));+ writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));+ const capture = join(root, 'native.json');+ const watch = join(root, 'watch.json');+ const preload = legacyObserver(root, capture, watch);+ const env = { ...process.env, NODE_OPTIONS: `--import="${preload}"` };+ await t.test('coverage is merged before applying its threshold', () => {+ writeFileSync(join(root, 'shared.mjs'), 'export const alpha = () => 1;\nexport const beta = () => 2;\n');+ for (const name of ['alpha', 'beta']) writeFileSync(join(root, `${name}.test.mjs`),+ `import { test } from 'node:test';\nimport assert from 'node:assert/strict';\nimport { ${name} } from './shared.mjs';\ntest('${name}', () => assert.equal(${name}(), ${name === 'alpha' ? 1 : 2}));\n`);+ const result = spawnSync(process.execPath, [runner, '--experimental-test-coverage',+ '--test-coverage-functions=100', '--test-coverage-include=shared.mjs', 'alpha.test.mjs', 'beta.test.mjs'],+ { cwd: root, env, encoding: 'utf8' });+ assert.equal(result.status, 0, result.context);+ const observed = JSON.parse(readFileSync(capture, 'utf8'));+ assert.ok(observed.args.includes('--test') && observed.args.includes('--test-coverage-functions=100'));+ assert.ok(observed.args.includes('alpha.test.mjs') && observed.args.includes('beta.test.mjs'),+ 'one native invocation receives both files before the aggregate threshold');+ assert.match(result.stdout, /^# pass 2$/mu);+ assert.match(result.stdout, /shared\.mjs/u);+ });+ await t.test('watch executes its first real case and stops through its observed owner', async () => {+ writeFileSync(join(root, 'watch.test.mjs'), "import { test } from 'node:test';\ntest('watch cell passed', () => {});\n");+ const run = runFixtureChildAsync(process.execPath, [runner, '--watch', 'watch.test.mjs'], { cwd: root, env });+ let stopped = false;+ /** Signal only the observed watcher while its current parent is this exact owned live runner. */+ const stop = () => {+ if (stopped || !existsSync(watch)) return;+ const owned = JSON.parse(readFileSync(watch, 'utf8'));+ assert.equal(owned.parentPid, run.child.pid);+ const current = spawnSync('ps', ['-p', String(owned.pid), '-o', 'ppid=,command='], { encoding: 'utf8' });+ if (current.status !== 0) return;+ assert.match(current.stdout, new RegExp(`^\\s*${run.child.pid}\\s+`), 'the watcher still belongs to our runner');+ assert.match(current.stdout, /--watch/u);+ process.kill(owned.pid, 'SIGTERM');+ stopped = true;+ };+ let output = '';+ const control = new Promise((resolve, reject) => {+ const ceiling = setTimeout(() => { try { stop(); } catch (error) { reject(error); } reject(new Error('watch fixture did not publish its first native green summary')); }, 10_000); // budget: owned watch fixture setup ceiling, not a performance bar.+ run.child.stdout.on('data', chunk => {+ output += chunk;+ if (!/^# pass 1$/mu.test(output)) return;+ try { stop(); clearTimeout(ceiling); resolve(); }+ catch (error) { clearTimeout(ceiling); reject(error); }+ });+ run.child.once('close', () => {+ clearTimeout(ceiling);+ if (!stopped) reject(new Error('watch ended before the owned first-pass control'));+ });+ });+ control.catch(() => {});+ const result = await awaitNestedRun(run);+ await control;+ assert.ok(stopped, 'normal termination targeted the observed native watcher');+ assert.ok(result.status === 0 || result.signal === 'SIGTERM', 'only normal owned watch termination is accepted');+ assert.match(result.stdout, /watch cell passed/u);+ const observed = JSON.parse(readFileSync(capture, 'utf8'));+ assert.equal(observed.pid, run.child.pid);+ assert.ok(observed.args.includes('--watch') && observed.args.includes('--test'));+ assert.equal(observed.args.filter(arg => arg === 'watch.test.mjs').length, 1);+ });+});