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(tests): a runner preload is known only by its exact source [C7]
Corey Olson320e9fa24c9c
diff --git a/src/child-journal-preload.js b/src/child-journal-preload.jsnew file mode 100644index 00000000..72eb7098--- /dev/null+++ b/src/child-journal-preload.js@@ -0,0 +1,66 @@+/** The SSH runner's spawn-journal preload, built from its directory alone so its content proves what it is [C7]. */+import { join } from 'node:path';++/**+ * Source of the preload an SSH runner puts in NODE_OPTIONS for a job: it records only child identity and a one-way+ * command digest. It depends on nothing but its directory, so a classifier can rebuild it and compare byte for byte.+ *+ * @param {string} directory the job directory that holds child-spawn-preload.mjs and child-journal/+ * @returns {string}+ */+export function childJournalPreloadSource(directory) {+ const journalDirectory = join(directory, 'child-journal');+ const overflowPath = join(journalDirectory, 'overflow');+ return `+import diagnostics from 'node:diagnostics_channel';+import { closeSync, openSync, statSync, writeSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { createHash, randomUUID } from 'node:crypto';+const directory = ${JSON.stringify(journalDirectory)};+const overflowPath = ${JSON.stringify(overflowPath)};+const instance = randomUUID();+process.env.PULLBOARD_RUNNER_INSTANCE = instance;+const file = join(directory, 'children-' + process.pid + '.jsonl');+const fd = openSync(file, 'a', 0o600);+let bytes = statSync(file).size;+let overflow = false;+const limit = 131072;+function record(value) {+ if (overflow) return;+ try {+ const line = JSON.stringify(value) + '\\n';+ const size = Buffer.byteLength(line);+ if (bytes + size > limit) {+ overflow = true;+ try { writeFileSync(overflowPath, 'journal limit reached\\n', { flag: 'wx', mode: 0o600 }); } catch {}+ return;+ }+ writeSync(fd, line);+ bytes += size;+ } catch {+ try { writeFileSync(overflowPath, 'journal write failed\\n', { flag: 'wx', mode: 0o600 }); } catch {}+ overflow = true;+ }+}+record({ type: 'boot', pid: process.pid, ppid: process.ppid, instance });+const channel = diagnostics.channel('child_process');+const seen = new WeakSet();+channel.subscribe(message => {+ try {+ const child = message?.process;+ if (!child || typeof child.once !== 'function' || seen.has(child)) return;+ seen.add(child);+ 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 });+ };+ if (Number.isSafeInteger(child.pid)) writeReceipt();+ else child.once('spawn', writeReceipt);+ } catch {+ try { writeFileSync(overflowPath, 'child receipt could not be recorded\\n', { flag: 'wx', mode: 0o600 }); } catch {}+ }+});+process.once('exit', () => { try { closeSync(fd); } catch {} });+`;+}diff --git a/src/runner-worker.js b/src/runner-worker.jsindex 8dcc770f..9fd6e85e 100644--- a/src/runner-worker.js+++ b/src/runner-worker.js@@ -38,6 +38,7 @@ import {} from 'node:url';import * as resourceApi from './resources.js';import { parseProcessCensus } from './process-census.js';+import { childJournalPreloadSource } from './child-journal-preload.js';import {gateStages,stagedScript,@@ -202,7 +203,7 @@ async function readSpec() {tagNames.add(tag.name);if (tag.chain.length ? tag.chain[0] !== tag.object : tag.object !== tag.peeledCommit) throw new Error('invalid tag manifest');}- const runtimeNames = ['resources.js', 'git.js', 'machine-home.js', 'refused.js', 'process-census.js', 'queue-settings.js', 'queue-order.js', 'sandbox-denial.js', 'gate-stages.js'];+ const runtimeNames = ['resources.js', 'git.js', 'machine-home.js', 'refused.js', 'process-census.js', 'queue-settings.js', 'queue-order.js', 'sandbox-denial.js', 'gate-stages.js', 'child-journal-preload.js'];if (Object.keys(value.runtime.files).length !== runtimeNames.length|| runtimeNames.some(name => typeof value.runtime.files[name] !== 'string')) throw new Error('invalid work specification');if (createHash('sha256').update(JSON.stringify(value.runtime.files)).digest('hex') !== value.runtime.sha256) throw new Error('invalid trusted runtime');@@ -310,58 +311,7 @@ function writeChildJournalPreload(directory) {const journalDirectory = join(directory, 'child-journal');mkdirSync(journalDirectory, { mode: 0o700 });const overflowPath = join(journalDirectory, 'overflow');- const source = `-import diagnostics from 'node:diagnostics_channel';-import { closeSync, openSync, statSync, writeSync, writeFileSync } from 'node:fs';-import { join } from 'node:path';-import { createHash, randomUUID } from 'node:crypto';-const directory = ${JSON.stringify(journalDirectory)};-const overflowPath = ${JSON.stringify(overflowPath)};-const instance = randomUUID();-process.env.PULLBOARD_RUNNER_INSTANCE = instance;-const file = join(directory, 'children-' + process.pid + '.jsonl');-const fd = openSync(file, 'a', 0o600);-let bytes = statSync(file).size;-let overflow = false;-const limit = 131072;-function record(value) {- if (overflow) return;- try {- const line = JSON.stringify(value) + '\\n';- const size = Buffer.byteLength(line);- if (bytes + size > limit) {- overflow = true;- try { writeFileSync(overflowPath, 'journal limit reached\\n', { flag: 'wx', mode: 0o600 }); } catch {}- return;- }- writeSync(fd, line);- bytes += size;- } catch {- try { writeFileSync(overflowPath, 'journal write failed\\n', { flag: 'wx', mode: 0o600 }); } catch {}- overflow = true;- }-}-record({ type: 'boot', pid: process.pid, ppid: process.ppid, instance });-const channel = diagnostics.channel('child_process');-const seen = new WeakSet();-channel.subscribe(message => {- try {- const child = message?.process;- if (!child || typeof child.once !== 'function' || seen.has(child)) return;- seen.add(child);- 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 });- };- if (Number.isSafeInteger(child.pid)) writeReceipt();- else child.once('spawn', writeReceipt);- } catch {- try { writeFileSync(overflowPath, 'child receipt could not be recorded\\n', { flag: 'wx', mode: 0o600 }); } catch {}- }-});-process.once('exit', () => { try { closeSync(fd); } catch {} });-`;+ const source = childJournalPreloadSource(directory);const preload = join(directory, 'child-spawn-preload.mjs');writeFileSync(preload, source, { mode: 0o600, flag: 'wx' });return { preload, journalDirectory, overflowPath, nodeOptions: `--import=${pathToFileURL(preload).href}` };diff --git a/src/runners.js b/src/runners.jsindex a86f6a21..81f52f38 100644--- a/src/runners.js+++ b/src/runners.js@@ -63,7 +63,7 @@ const MAX_BLOB_BYTES = 8 * 1024 ** 2;const MAX_HISTORY_TAGS = 1_024;const MAX_TAG_CHAIN = 8;const TOOL_ROOT = fileURLToPath(new URL('../', import.meta.url));-const RUNTIME_NAMES = ['resources.js', 'git.js', 'machine-home.js', 'refused.js', 'process-census.js', 'queue-settings.js', 'queue-order.js', 'sandbox-denial.js', 'gate-stages.js'];+const RUNTIME_NAMES = ['resources.js', 'git.js', 'machine-home.js', 'refused.js', 'process-census.js', 'queue-settings.js', 'queue-order.js', 'sandbox-denial.js', 'gate-stages.js', 'child-journal-preload.js'];/** Validate an SSH destination as one argument, never a command or an SSH option. */export function validateRunner(value) {diff --git a/src/test-isolation.js b/src/test-isolation.jsindex 5a0e0c19..2fb2a1a0 100644--- a/src/test-isolation.js+++ b/src/test-isolation.js@@ -5,8 +5,10 @@* gate.shortSlots, registered runners), the account's resources.sqlite, a CI marker a runner sets, or the* instrumentation an SSH runner loads into every Node process. bin/run-tests.js applies this to each test run.*/-import { mkdirSync } from 'node:fs';-import { join } from 'node:path';+import { mkdirSync, readFileSync } from 'node:fs';+import { dirname, join } from 'node:path';+import { fileURLToPath } from 'node:url';+import { childJournalPreloadSource } from './child-journal-preload.js';/** Stands in for the OS account home when machine state falls back to it (src/machine-home.js). */export const ACCOUNT_HOME_ENV = 'PULLBOARD_ACCOUNT_HOME';@@ -17,8 +19,27 @@ export const RUNNER_PRELOAD_ENV = 'PULLBOARD_RUNNER_PRELOAD';/** The launch guard a browser-free test file passes to its Node children (gate.browserByLaunch). */const SLOT_GUARD_IMPORT = `--import=${new URL('./browser-slot-guard.js', import.meta.url).href}`;-/** The shape of the runner's own journal preload; a declared value of any other shape is not trusted. */-const RUNNER_PRELOAD = /^--import=file:\/\/\S+\/child-spawn-preload\.mjs$/u;+/** The import a runner declares; its file must hold exactly the runner's journal source for that directory. */+const RUNNER_PRELOAD = /^--import=(file:\/\/\S+\/child-spawn-preload\.mjs)$/u;++/**+ * Whether a declared runner preload is the runner's own: the file's bytes must equal the source the runner writes+ * for that directory. A name or a declaration alone proves nothing, so any other content stays foreign.+ *+ * @param {string | undefined} declared PULLBOARD_RUNNER_PRELOAD+ * @returns {boolean}+ */+function runnerPreload(declared) {+ const url = RUNNER_PRELOAD.exec(declared ?? '')?.[1];+ if (!url) return false;+ try {+ const file = fileURLToPath(url);+ return readFileSync(file, 'utf8') === childJournalPreloadSource(dirname(file));+ } catch (error) {+ if (['ENOENT', 'EACCES', 'EISDIR', 'ERR_INVALID_URL', 'ERR_INVALID_FILE_URL_PATH'].includes(error.code)) return false;+ throw error;+ }+}/*** Point every machine-state fallback of a test run at its sandbox and drop host markers tests model explicitly.@@ -39,8 +60,8 @@ export function isolateTestMachine(env, sandbox) {}/**- * Whether NODE_OPTIONS may load a module the classifier has not seen. This checkout's launch guard and the preload- * an SSH runner declares are known; anything else is foreign, so the run is not classified as short or browser-free.+ * Whether NODE_OPTIONS may load a module the classifier has not seen. This checkout's launch guard and an SSH+ * runner's proven journal preload are known; anything else is foreign, so the run is not short or browser-free.** @param {string | undefined} value NODE_OPTIONS* @param {NodeJS.ProcessEnv} [env] where a runner's declared preload is read@@ -49,6 +70,6 @@ export function isolateTestMachine(env, sandbox) {export function foreignNodeOptions(value, env = process.env) {const declared = env[RUNNER_PRELOAD_ENV];const known = new Set([SLOT_GUARD_IMPORT]);- if (declared && RUNNER_PRELOAD.test(declared)) known.add(declared);+ if (runnerPreload(declared)) known.add(declared);return String(value ?? '').split(/\s+/u).filter(Boolean).some(token => !known.has(token));}diff --git a/test/test-isolation.test.js b/test/test-isolation.test.jsindex 1faa0c5d..172bcd4d 100644--- a/test/test-isolation.test.js+++ b/test/test-isolation.test.js@@ -1,4 +1,4 @@-/** @pullboard-test-loads bin/run-tests.js src/test-isolation.js src/machine-home.js src/settings.js src/resources.js src/view-port.js */+/** @pullboard-test-loads bin/run-tests.js src/test-isolation.js src/child-journal-preload.js src/machine-home.js src/settings.js src/resources.js src/view-port.js *//** A test's verdict never depends on the machine it runs on: settings, resources, runners, host markers, ports [C7]. */import assert from 'node:assert/strict';import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';@@ -8,6 +8,8 @@ import { join, resolve } from 'node:path';import { test } from 'node:test';import { pathToFileURL } from 'node:url';import { runFixtureChildAsync } from './fixture-child.js';+import { childJournalPreloadSource } from '../src/child-journal-preload.js';+import { foreignNodeOptions } from '../src/test-isolation.js';import { takeResource } from '../src/resources.js';import { DEFAULT_VIEW_PORT } from '../src/view-port.js';import { GATE_SLOT_HELD_ENV, GATE_SLOT_POOL_ENV, GATE_SLOT_TOKEN_ENV } from '../src/resources.js';@@ -82,9 +84,10 @@ test('a test reads no machine state outside its own private home [C7]', async (tconst holder = createServer();await new Promise((done) => { holder.once('error', done); holder.listen(DEFAULT_VIEW_PORT, '127.0.0.1', done); });cleanups.push(() => holder.listening ? new Promise((done) => holder.close(done)) : undefined);+ // The runner's own spawn-journal preload, exactly as an SSH runner writes it for a job.const preload = join(dir, 'runner', 'child-spawn-preload.mjs');- mkdirSync(join(dir, 'runner'));- writeFileSync(preload, 'export {};\n');+ mkdirSync(join(dir, 'runner', 'child-journal'), { recursive: true });+ writeFileSync(preload, childJournalPreloadSource(join(dir, 'runner')));const root = probeSuite(dir);const base = { ...process.env, PULLBOARD_MACHINE_HOME: join(dir, 'outer-machine'), ISOLATION_HOSTILE_HOME: hostile };@@ -109,3 +112,21 @@ test('a test reads no machine state outside its own private home [C7]', async (tconst seen = verdicts(exposed.stdout);for (const index of [0, 1, 2]) assert.match(seen[index], /^not ok /u, `${PROBES[index]} catches the machine it reads`);});++test('a forged runner preload of the same name stays foreign [C7]', (t) => {+ const dir = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-forged-preload-')));+ t.after(() => rmSync(dir, { recursive: true, force: true }));+ const preload = join(dir, 'child-spawn-preload.mjs');+ const declared = `--import=${pathToFileURL(preload).href}`;+ const env = { PULLBOARD_RUNNER_PRELOAD: declared };+ writeFileSync(preload, childJournalPreloadSource(dir));+ assert.equal(foreignNodeOptions(declared, env), false, 'the runner\'s own journal source is known');+ assert.equal(foreignNodeOptions(declared, {}), true, 'undeclared, even the real preload is foreign');+ writeFileSync(preload, `${childJournalPreloadSource(dir)}\nimport 'node:child_process';\n`);+ assert.equal(foreignNodeOptions(declared, env), true, 'one added line makes the declared file foreign');+ writeFileSync(preload, childJournalPreloadSource(join(dir, 'elsewhere')));+ assert.equal(foreignNodeOptions(declared, env), true, 'the source for another directory is foreign');+ rmSync(preload);+ assert.equal(foreignNodeOptions(declared, env), true, 'a missing file is foreign');+ assert.equal(foreignNodeOptions('--import=file:///elsewhere/unclassified.js', env), true, 'an undeclared import is foreign');+});