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(runner): adopt a process only on exact proof [C7]
Corey Olson9577836d61af
diff --git a/src/process-census.js b/src/process-census.jsindex 2c919c94..a3df00ec 100644--- a/src/process-census.js+++ b/src/process-census.js@@ -28,22 +28,3 @@ export function parseProcessCensus(stdout) {}return rows.size ? rows : null;}--const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];--/**- * Whether a census row's process started at the given moment, from a census read with TZ=UTC. The census gives- * whole seconds, and a process starts at most a moment before the record of it, so the window is one second before- * that second to three after, for a loaded host. Hosts hand out pids in order, so a reused pid cannot start inside it.- *- * @param {{started: string}} row - A row from parseProcessCensus.- * @param {number} ms - When the job's own record says the process started, in epoch milliseconds.- * @returns {boolean}- */-export function bornNear(row, ms) {- const match = /^[A-Z][a-z]{2} ([A-Z][a-z]{2}) (\d{1,2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/u.exec(row?.started ?? '');- const month = match ? MONTHS.indexOf(match[1]) : -1;- if (month < 0 || !Number.isFinite(ms)) return false;- const second = Date.UTC(Number(match[6]), month, Number(match[2]), Number(match[3]), Number(match[4]), Number(match[5]));- return second - 1_000 <= ms && ms < second + 3_000;-}diff --git a/src/runner-custody.js b/src/runner-custody.jsindex e21844cc..2ca57c0f 100644--- a/src/runner-custody.js+++ b/src/runner-custody.js@@ -17,7 +17,6 @@ import {join,} from 'node:path';import { pathToFileURL } from 'node:url';-import { bornNear } from './process-census.js';import { parseProcessCensus } from './process-census.js';const safePath = [dirname(process.execPath), process.env.PATH || '/usr/local/bin:/usr/bin:/bin'].join(delimiter);@@ -85,7 +84,7 @@ function record(value) {overflow = true;}}-record({ type: 'boot', pid: process.pid, ppid: process.ppid, instance, started: Math.round(performance.timeOrigin) });+record({ type: 'boot', pid: process.pid, ppid: process.ppid, instance });const channel = diagnostics.channel('child_process');const seen = new WeakSet();channel.subscribe(message => {@@ -96,7 +95,7 @@ channel.subscribe(message => {const writeReceipt = () => {if (!Number.isSafeInteger(child.pid) || child.pid < 1) return;const commandSha256 = createHash('sha256').update(JSON.stringify({ command: child.spawnfile ?? '', args: child.spawnargs ?? [] })).digest('hex');- record({ type: 'spawn', pid: child.pid, ppid: process.pid, instance, commandSha256, at: Date.now() });+ record({ type: 'spawn', pid: child.pid, ppid: process.pid, instance, commandSha256 });};if (Number.isSafeInteger(child.pid)) writeReceipt();else child.once('spawn', writeReceipt);@@ -137,8 +136,6 @@ export function createDescendantTracker(directory, { census: processCensusHook =const offsets = new Map();const partial = new Map();const instancePids = new Map();- // Each job Node process's own boot record: its pid and start time, written into this job's private journal.- const boots = new Map();const overflowPath = journal.overflowPath;let current = baseline;let uncertain = false;@@ -158,30 +155,25 @@ export function createDescendantTracker(directory, { census: processCensusHook =}/**- * Take custody of a live process proven to be this job's. A group lives inside one session, and a session holds- * only descendants of the job process that made it, so while that process lives its group is the job's too: record- * the group with its leader's start time. A group whose leader is already gone has no start time to check a reused- * number against, so it gets no custody; nor does a group led by a process that was here before the job (#1063).- * `byGroup` marks a process whose only proof is its group, which never proves the group in turn.+ * Take custody of a live member of a group this census proves is the job's (groupProven). A group lives inside one+ * session, and a session holds only descendants of the job process that made it, and the kernel never reuses a+ * group's number while a member lives, so the member is the job's. It is marked `byGroup`: membership is its only+ * proof, so it never proves the group in turn (#1063).*/- function adopt(row, commandSha256, ownedPids, byGroup = false) {- known.set(row.pid, { ...row, commandSha256, root: false, group: row.pgid, ...(byGroup ? { byGroup: true } : {}) });- const leader = current.get(row.pgid);- const foreign = leader && baseline.get(row.pgid)?.birth === leader.birth;- if (!detachedGroups.has(row.pgid) && leader && !foreign && !/^Z/u.test(leader.state)) detachedGroups.set(row.pgid, leader.birth);+ function adoptMember(row, ownedPids) {+ known.set(row.pid, { ...row, commandSha256: '', root: false, group: row.pgid, byGroup: true });pendingIdentities.delete(row.pid);ownedPids.add(row.pid);}/**- * Whether this census still proves group `pgid` is the job's: its recorded leader is alive with its recorded start- * time, or a member proven by its own receipt, boot record or ancestry is. Group membership alone proves nothing.+ * Whether this census proves group `pgid` is the job's: a live member is a process the job proved itself, as the+ * runner's own launch, a spawn receipt matched while its parent was the job's, or live ancestry. A recorded start+ * time never proves a group, since a census reads start times only to the second (#1063).*/function groupProven(pgid) {- const leaderStarted = detachedGroups.get(pgid);for (const row of current.values()) {if (row.pgid !== pgid || /^Z/u.test(row.state)) continue;- if (row.pid === pgid && leaderStarted && row.birth === leaderStarted) return true;const identity = known.get(row.pid);if (identity && !identity.byGroup && identity.birth === row.birth && !retired.has(`${row.pid}:${row.birth}`)) return true;}@@ -225,8 +217,6 @@ export function createDescendantTracker(directory, { census: processCensusHook =try { record = JSON.parse(line); } catch { markUncertain('child spawn journal record is invalid'); continue; }if (record.type === 'boot' && Number.isSafeInteger(record.pid) && record.pid > 0 && typeof record.instance === 'string') {instancePids.set(record.instance, record.pid);- // A boot record without its start time still binds receipts; it only proves nothing by itself.- if (Number.isFinite(record.started)) boots.set(record.pid, [...(boots.get(record.pid) ?? []), record.started].slice(-4));} else if (record.type === 'spawn' && Number.isSafeInteger(record.pid) && record.pid > 0&& Number.isSafeInteger(record.ppid) && record.ppid > 0&& typeof record.instance === 'string' && /^[a-f0-9]{64}$/u.test(record.commandSha256)) {@@ -300,13 +290,6 @@ export function createDescendantTracker(directory, { census: processCensusHook =if (retired.has(`${row.pid}:${row.birth}`)) { markUncertain('a retired process identity reappeared'); continue; }if (prior && prior.birth === row.birth) { consumedEvents.add(event.eventId); ownedPids.add(row.pid); continue; }if (!prior && baseline.get(row.pid)?.birth === row.birth) { consumedEvents.add(event.eventId); continue; }- // The receipt and the census both carry a start time. A process born when a job process spawned it is that- // child, even if its parent exited before any census saw the two together; a reused pid starts later (#1063).- if ((row.ppid !== event.ppid || !ownedPids.has(event.ppid)) && bornNear(row, event.at)) {- adopt(row, event.commandSha256, ownedPids);- consumedEvents.add(event.eventId);- continue;- }// A spawn receipt must be observed while its exact parent instance still owns it.// A same-numbered PID first seen after reparenting is ambiguous and never adopted.if (row.ppid !== event.ppid) {@@ -340,16 +323,6 @@ export function createDescendantTracker(directory, { census: processCensusHook =}}- // A Node process that wrote its own boot record into this job's private journal is the job's, once the- // census shows it born when it said it was (#1063).- for (const [pid, starts] of boots) {- const row = current.get(pid);- if (!row || /^Z/u.test(row.state) || ownedPids.has(pid) || retired.has(`${pid}:${row.birth}`)) continue;- if (baseline.get(pid)?.birth === row.birth) continue;- if (known.get(pid)?.birth === row.birth) ownedPids.add(pid);- else if (starts.some(started => bornNear(row, started))) adopt(row, '', ownedPids);- }-// Process ancestry captures non-Node children; journal receipts retain ownership after reparenting.let changed = true;while (changed) {@@ -402,7 +375,7 @@ export function createDescendantTracker(directory, { census: processCensusHook =if (retired.has(`${row.pid}:${row.birth}`)) { markUncertain('a retired process identity reappeared'); continue; }if (prior && prior.birth === row.birth) { ownedPids.add(row.pid); continue; }if (baseline.get(row.pid)?.birth === row.birth) continue;- adopt(row, '', ownedPids, true);+ adoptMember(row, ownedPids);}// A recorded PGID does not prove an unseen member belongs to this job. Keep// the lease fail-closed when a root group contains a process lacking identity evidence.diff --git a/test/runner-orphan-fixture.js b/test/runner-orphan-fixture.jsindex 6a809812..013836b0 100644--- a/test/runner-orphan-fixture.js+++ b/test/runner-orphan-fixture.js@@ -21,8 +21,8 @@ const preload = option ? fileURLToPath(option[1] || option[2] || option[3]) : ''if (!preload.startsWith(remoteRoot)) { writeFileSync(join(out, mode + '-local'), 'local\\n'); console.log('ran locally'); process.exit(0); }writeFileSync(join(out, mode + '-remote'), 'remote\\n');if (mode === 'overflow') { writeFileSync(join(dirname(preload), 'child-journal', 'overflow'), 'fixture overflow\\n'); process.exit(0); }-// A launcher that exits as soon as it spawns, so no census sees the detached child beside its parent.-spawn(process.execPath, ['-e', "require('node:child_process').spawn(process.execPath, [process.argv[1], process.argv[2]], { detached: true, stdio: 'ignore' }).unref(); process.exit(0);",+// A launcher that starts a detached child, lives long enough for the runner to see the two together, then exits.+spawn(process.execPath, ['-e', "require('node:child_process').spawn(process.execPath, [process.argv[1], process.argv[2]], { detached: true, stdio: 'ignore' }).unref(); setTimeout(() => process.exit(0), 1500);",fileURLToPath(new URL('./orphan-child.mjs', import.meta.url)), out], { stdio: 'ignore' });const deadline = Date.now() + 10_000;while (!existsSync(join(out, 'grandchild.pid')) && Date.now() < deadline) await new Promise(done => setTimeout(done, 20));@@ -32,6 +32,9 @@ const CHILD = `import { spawn } from 'node:child_process';import { renameSync, writeFileSync } from 'node:fs';import { join } from 'node:path';const [out] = process.argv.slice(2);+// Only once its launcher is gone, so the grandchild's parent is an orphan the runner must still own.+const launcher = process.ppid;+while (process.ppid === launcher) await new Promise(done => setTimeout(done, 20));const grandchild = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { detached: true, stdio: 'ignore' });grandchild.unref();writeFileSync(join(out, 'child.pid'), String(process.pid));@@ -43,8 +46,8 @@ setInterval(() => {}, 1000);/*** Run the gate twice on a stand-in SSH runner while the only local slot is held. The first job leaves a detached- * child and grandchild that no census sees beside their exited launcher; the runner must still end both, verify- * that, and return green. The second job makes its cleanup unverifiable; the run must rerun locally, not fail.+ * child whose launcher exits, and that orphan then starts a detached grandchild; the runner must still end both,+ * verify that from its own record, and return green. The second job makes its cleanup unverifiable; the run must rerun locally, not fail.*/export async function runnerOrphanFixture(t, tools) {const { cli, git, installSshStandIn, observe, startToolingCli, processIsAlive, HOST } = tools;@@ -151,37 +154,74 @@ function censusLine(pid, ppid, pgid, ms) {}/**- * Drive the runner's custody through a scripted census and a recording kill. A job orphan is proven by its own boot- * record while its group's leader is already gone; it exits, and a foreign leader and member then take that group- * number. The runner must never adopt or signal the foreign group (#1063). Pids sit above any real pid_max.+ * Drive the runner's custody through a scripted census and a recording kill; pids sit above any real pid_max. The+ * runner adopts only what it can prove exactly, so two lookalikes must never be signalled (#1063):+ * - a job process spawned a child and exited, and a foreign process holds that pid, born the same second;+ * - a job group's members all exited, and a foreign leader holds that group number, born the same second as the old one.*/-export async function reusedGroupFixture(t, { createDescendantTracker, parseProcessCensus }) {- const dir = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-runner-reused-group-')));- t.after(() => rmSync(dir, { recursive: true, force: true }));+export async function unprovenProcessFixture(t, { createDescendantTracker, parseProcessCensus }) {+ const dir = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-runner-unproven-')));const self = 9_000_001;- const group = 9_000_100;- const orphan = 9_000_101;- const foreign = [group, 9_000_102];+ const root = 9_000_010;+ const parent = 9_000_050;+ const reusedPid = 9_000_060;+ const leader = 9_000_100;+ const member = 9_000_101;+ const foreignMember = 9_000_102;const now = Date.now();+ const line = (pid, ppid, pgid) => censusLine(pid, ppid, pgid, now);let rows = [censusLine(self, 1, self, now - 60_000)];const kills = [];- const census = () => parseProcessCensus(rows.join('\n'));const kill = (pid, signal) => {kills.push({ pid, signal });// A signalled process leaves the census, as a killed one would.- rows = rows.filter(line => { const [rowPid, , rowGroup] = line.split(' ').map(Number); return pid < 0 ? rowGroup !== -pid : rowPid !== pid; });+ rows = rows.filter(entry => { const [rowPid, , rowGroup] = entry.split(' ').map(Number); return pid < 0 ? rowGroup !== -pid : rowPid !== pid; });};- const tracker = createDescendantTracker(dir, { census, kill, selfPid: self, intervalMs: 3_600_000 });- t.after(() => tracker.stopMonitoring());- writeFileSync(join(dir, 'child-journal', `children-${orphan}.jsonl`), JSON.stringify({ type: 'boot', pid: orphan, ppid: 1, instance: 'fixture-orphan', started: now }) + '\n');- rows = [...rows, censusLine(orphan, 1, group, now)];+ // A lookalike leaves custody uncertain, so the stop loop waits for the cleanup budget; end that budget with the test.+ let budgetSpent = false;+ const tracker = createDescendantTracker(dir, { census: () => parseProcessCensus(rows.join('\n')), kill, selfPid: self,+ timedOut: () => budgetSpent, intervalMs: 3_600_000 });+ // End the budget and let the stop loop return before its journal directory goes.+ t.after(async () => {+ budgetSpent = true;+ let timer;+ await Promise.race([tracker.stopDescendants(), new Promise(done => { timer = setTimeout(done, 3_000); })]); // budget: one census pass after the budget ends.+ clearTimeout(timer);+ tracker.stopMonitoring();+ rmSync(dir, { recursive: true, force: true });+ });+ /** Stop descendants within a bound, then report what was signalled and what is in custody. */+ const stop = async () => {+ let timer;+ await Promise.race([tracker.stopDescendants(), new Promise(done => { timer = setTimeout(done, 3_000); })]); // budget: nothing to stop returns at once.+ clearTimeout(timer);+ return { kills: [...kills], live: tracker.cleanupState().live.map(entry => entry.pid) };+ };++ rows = [...rows, line(root, self, root)];+ tracker.registerRoot(root);+ // The job's own parent records that it spawned a child, then exits; a foreign process born that second holds the pid.+ writeFileSync(join(dir, 'child-journal', `children-${parent}.jsonl`), [+ { type: 'boot', pid: parent, ppid: root, instance: 'fixture-parent' },+ { type: 'spawn', pid: reusedPid, ppid: parent, instance: 'fixture-parent', commandSha256: 'a'.repeat(64), at: now },+ ].map(entry => JSON.stringify(entry)).join('\n') + '\n');+ rows = [...rows, line(reusedPid, 1, reusedPid)];+ tracker.observe();+ assert.deepEqual(await stop().then(state => state.kills), [], 'a receipt and a start second never prove a reparented pid');+ assert.deepEqual(tracker.cleanupState().pending.map(entry => entry.pid), [reusedPid], 'the lookalike stays unidentified, so cleanup cannot verify');+ rows = rows.filter(entry => Number(entry.split(' ')[0]) !== reusedPid);+ tracker.observe();++ // A detached group the job proved by ancestry; its leader exits, then its last member.+ rows = [...rows, line(leader, root, leader), line(member, leader, leader)];+ tracker.observe();+ assert.deepEqual(tracker.cleanupState().live.map(entry => entry.pid).sort(), [leader, member], 'live ancestry proves the job group');+ rows = rows.filter(entry => Number(entry.split(' ')[0]) !== leader).map(entry => Number(entry.split(' ')[0]) === member ? line(member, 1, leader) : entry);tracker.observe();- assert.deepEqual(tracker.cleanupState().live.map(row => row.pid), [orphan], 'the orphan is the job\'s by its own boot record');- rows = [rows[0], censusLine(foreign[0], 1, group, now + 5_000), censusLine(foreign[1], foreign[0], group, now + 5_000)];+ assert.deepEqual(tracker.cleanupState().live.map(entry => entry.pid), [member], 'a proven member keeps its group after the leader exits');+ rows = [...rows.filter(entry => Number(entry.split(' ')[0]) !== member), line(leader, 1, leader), line(foreignMember, leader, leader)];tracker.observe();- let timer;- await Promise.race([tracker.stopDescendants(), new Promise(done => { timer = setTimeout(done, 3_000); })]); // budget: a stop that adopts nothing returns at once.- clearTimeout(timer);- assert.deepEqual(kills.filter(({ pid }) => pid === -group || foreign.includes(pid)), [], 'the runner never signals a group it cannot prove is the job\'s');- assert.deepEqual(tracker.cleanupState().live, [], 'no foreign process is taken into custody');+ const after = await stop();+ assert.deepEqual(after.kills, [], 'the runner never signals a group it cannot prove is the job\'s');+ assert.deepEqual(after.live, [], 'no foreign process is taken into custody');}diff --git a/test/runners.test.js b/test/runners.test.jsindex 8c22027f..0343377d 100644--- a/test/runners.test.js+++ b/test/runners.test.js@@ -16,7 +16,7 @@ import { readRemoteGateHolders, waitForCondition } from './runner-fixture-state.import { assertAttemptDeadlineReset, assertRedispatchDeadlineAccounting, runPairedRerun, startRunnerCpuStandIn } from './runner-paired-rerun-fixture.js';import { assertFixtureGroupEpermControls, stopGroup } from './runner-process-group.js';import { parentDeathDrainFixture } from './runner-parent-death-fixture.js';-import { reusedGroupFixture, runnerOrphanFixture } from './runner-orphan-fixture.js';+import { runnerOrphanFixture, unprovenProcessFixture } from './runner-orphan-fixture.js';import { createDescendantTracker } from '../src/runner-custody.js';import { createE2eHelpers } from './e2e-helpers.js';import { refuseSandboxBlocked } from '../src/gate.js';@@ -1508,8 +1508,8 @@ test('a queued run executes on a registered runner and its result comes back [C7const orphanTools = { cli, git, installSshStandIn, observe, startToolingCli, processIsAlive, HOST };test('a runner job leaves no process behind, detached ones included [C7]',{ timeout: 150_000 }, t => runnerOrphanFixture(t, orphanTools)); // budget: one plain runner round trip, then the declared 30-second remote cleanup timeout and a local rerun.-test('a runner job leaves no process behind, and never signals a group whose number was reused [C7]',- { timeout: 30_000 }, t => reusedGroupFixture(t, { createDescendantTracker, parseProcessCensus })); // budget: scripted census, no real processes.+test('a runner job leaves no process behind, and never signals a process it cannot prove is the job\'s [C7]',+ { timeout: 30_000 }, t => unprovenProcessFixture(t, { createDescendantTracker, parseProcessCensus })); // budget: scripted census, no real processes./** Read one machine pool's browser row through the public resource API, selecting that machine's home. */function browserPoolAt(machineHome, root) {