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.

fix(view): a test finds its runner by its pid, not its argv [C7]

Corey Olsona76dfa8f8efc
diff --git a/test/chrome-fixture.js b/test/chrome-fixture.js
index 6283a4c6..b3aec42b 100644
--- a/test/chrome-fixture.js
+++ b/test/chrome-fixture.js
@@ -169,44 +169,41 @@ setInterval(() => {
let runnerAncestor;
+/** The environment variable bin/run-tests.js sets to its own pid for every test it runs (#896). */
+export const RUNNER_PID_ENV = 'PULLBOARD_TEST_RUNNER_PID';
+
/**
- * Whether a process command line is the test runner itself, bin/run-tests.js with any options, and not one of its
- * workers: a worker carries bin/run-tests.js only as its --import value, or runs under a standalone --test flag.
- * Options such as --test-concurrency=1 belong to the runner and never make it a worker (#896). Paths may hold spaces.
+ * Find the test runner above this process: the pid bin/run-tests.js left in this process's environment, kept
+ * only while it is a real ancestor of this process. Why not read command lines: ps prints argv without quoting,
+ * so no parser tells a runner started with `--import data:…` or a path with spaces from one of its workers (#896).
+ * The walk reads parent pids only, which macOS and Linux both print; no runner, or a stale pid from another tree,
+ * answers null, and only the owner is guarded.
*
- * @param {string} command - The command line as ps prints it.
- * @returns {boolean}
+ * @param {{ env?: NodeJS.ProcessEnv, parentOf?: (pid: number) => number | null }} [options]
+ * @returns {number | null}
*/
-export function isRunnerCommand(command) {
- const text = String(command);
- if (!/(?:^|[\s/])bin\/run-tests\.js(?:\s|$)/u.test(text)) return false;
- if (/(?:^|\s)--test(?:\s|$)/u.test(text)) return false;
- // Each import value runs to the first module file name that ends it, so a folder with spaces stays inside the value
- // while `--import ./instrumentation.mjs bin/run-tests.js` still names the runner as the script (#896).
- const imports = [...text.matchAll(/(?:^|\s)--import[=\s](.*?\.(?:[mc]?js|ts))(?=\s|$)/gu)].map((match) => match[1]);
- return !imports.some((value) => /(?:^|[\s/:])bin\/run-tests\.js$/u.test(value));
+export function findRunnerAncestor({ env = process.env, parentOf = parentPid } = {}) {
+ if (env === process.env && runnerAncestor !== undefined) return runnerAncestor;
+ let found = null;
+ const runner = Number(env[RUNNER_PID_ENV]);
+ if (Number.isSafeInteger(runner) && runner > 1) {
+ let pid = process.pid;
+ for (let depth = 0; depth < 64 && pid > 1; depth += 1) {
+ pid = parentOf(pid);
+ if (pid === null) break;
+ if (pid === runner) { found = runner; break; }
+ }
+ }
+ if (env === process.env) runnerAncestor = found;
+ return found;
}
-/**
- * Find the test runner above this process: the nearest ancestor whose command runs bin/run-tests.js itself.
- * The walk uses `ps -o ppid=,command= -p <pid>`, which macOS and Linux share, runs once per process,
- * and answers null for a direct `node --test`, where only the owner is guarded.
- */
-export function findRunnerAncestor() {
- if (runnerAncestor !== undefined) return runnerAncestor;
- runnerAncestor = null;
+/** A process's parent pid from ps, or null when it cannot be read. */
+function parentPid(pid) {
try {
- let pid = process.pid;
- for (let depth = 0; depth < 32 && pid > 1; depth += 1) {
- const row = execFileSync('ps', ['-o', 'ppid=,command=', '-p', String(pid)], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
- const match = /^(\d+)\s+(.*)$/su.exec(row);
- if (!match) break;
- pid = Number(match[1]);
- const ancestor = execFileSync('ps', ['-o', 'command=', '-p', String(pid)], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
- if (isRunnerCommand(ancestor.trim())) { runnerAncestor = pid; break; }
- }
- } catch { /* No readable ancestry means the guardian watches the owner alone. */ }
- return runnerAncestor;
+ const value = Number(execFileSync('ps', ['-o', 'ppid=', '-p', String(pid)], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim());
+ return Number.isSafeInteger(value) && value > 0 ? value : null;
+ } catch { return null; /* An ended process has no parent to read. */ }
}
/**
diff --git a/test/cockpit/stable.test.js b/test/cockpit/stable.test.js
index b80e90f6..b2b6615b 100644
--- a/test/cockpit/stable.test.js
+++ b/test/cockpit/stable.test.js
@@ -6,7 +6,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
-import { findChromeExecutable, isRunnerCommand } from '../chrome-fixture.js';
+import { RUNNER_PID_ENV, findChromeExecutable, findRunnerAncestor } from '../chrome-fixture.js';
const ROOT = fileURLToPath(new URL('../../', import.meta.url));
const PROBE = join(ROOT, 'test/cockpit/chrome-owner-probe.mjs');
@@ -57,14 +57,14 @@ function stopRecorded(profile, pids) {
for (const pid of pids) { try { process.kill(pid, 'SIGKILL'); } catch { /* Already gone. */ } }
}
-test('the runner is bin/run-tests.js with any options, and its workers are not [C7]', () => {
- const at = '/work/Code Space/repo';
- for (const command of [`node bin/run-tests.js test/a.test.js`, `node bin/run-tests.js --test-concurrency=1 test/a.test.js`,
- `/usr/local/bin/node ${at}/bin/run-tests.js --test-concurrency=1 ${at}/test/a.test.js`,
- `node --import ./instrumentation.mjs bin/run-tests.js test/a.test.js`, `node --import=./instrumentation.mjs ${at}/bin/run-tests.js`]) assert.equal(isRunnerCommand(command), true, command);
- for (const command of [`node --disable-warning=ExperimentalWarning --import ${at}/bin/run-tests.js ${at}/test/a.test.js`,
- `node --import=${at}/bin/run-tests.js ${at}/test/a.test.js`, `node --import ./trace.mjs --import ${at}/bin/run-tests.js ${at}/test/a.test.js`,
- `node --test test/a.test.js bin/run-tests.js`, `node test/a.test.js`]) assert.equal(isRunnerCommand(command), false, command);
+test('a test finds its runner by the pid the runner gave it, never by its command line [C7]', () => {
+ // A parent chain this process -> 700 -> 600 -> 500 -> 1, as ps would report it.
+ const chain = new Map([[process.pid, 700], [700, 600], [600, 500], [500, 1]]);
+ const parentOf = (pid) => chain.get(pid) ?? null;
+ assert.equal(findRunnerAncestor({ env: { [RUNNER_PID_ENV]: '600' }, parentOf }), 600, 'the named runner is found up the chain');
+ assert.equal(findRunnerAncestor({ env: { [RUNNER_PID_ENV]: '900' }, parentOf }), null, 'a pid that is not an ancestor is never guarded');
+ assert.equal(findRunnerAncestor({ env: {}, parentOf }), null, 'with no runner, only the owner is guarded');
+ assert.equal(findRunnerAncestor({ env: { [RUNNER_PID_ENV]: '1' }, parentOf }), null, 'init is never a runner');
});
/** How each killed process starts the probe: the owner directly, or the runner, with and without its own options. */
@@ -72,6 +72,8 @@ const VICTIMS = {
owner: [],
runner: ['bin/run-tests.js'],
'runner started with --test-concurrency=1': ['bin/run-tests.js', '--test-concurrency=1'],
+ // A data: import before the runner, which no reading of ps text tells from a worker (#896).
+ 'runner started after a data: import': ['--import', 'data:text/javascript,globalThis.__probe=1', 'bin/run-tests.js'],
};
for (const [launcher, name] of Object.entries(LAUNCHERS)) {