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(core): release red submit seats and pool comparisons [V4,C7]

Corey Olsonb3c8e0ff1d4d
diff --git a/src/cli.js b/src/cli.js
index 73bb103f..3eaabbe1 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -2006,6 +2006,9 @@ async function submitHere(ctx, id) {
const gate = await runSubmitGate(root, ctx.config, { base: claimHead, trunk: acceptedMain.commit, check: frozenCheck(held), onWait: gateWaitReporter(ctx.io) });
if (!gate.isGreen) {
const message = `the submission proof is red at ${commit.slice(0, 12)}; fix it, commit, submit again. ${submitGateReport(gate)}`;
+ // No submission can follow this verdict. Read-only main comparisons must not retain the seat's write lease.
+ CHECKOUT_LEASES.get(ctx.io)?.release();
+ CHECKOUT_LEASES.delete(ctx.io);
let comparison = { main: null, files: [], unavailable: 'main comparison could not be completed' };
try { comparison = await compareMainFailures(root, gate.profile, gateWaitReporter(ctx.io), gate.output); } catch { /* Keep the original gate refusal if main evidence is unavailable. */ }
const mainNote = withBoard(ctx, board => mainRedMessage(root, board, gate.profile, comparison));
diff --git a/src/gate.js b/src/gate.js
index ac6eaf6f..f0b4c36a 100644
--- a/src/gate.js
+++ b/src/gate.js
@@ -4,7 +4,7 @@
* tree's id in the git dir, so the same tree is never checked twice. An agent sees a digest of the
* run, not the run (V10): a passing suite's output costs tokens and says nothing.
*/
-import { spawnSync } from 'node:child_process';
+import { spawn, spawnSync } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
@@ -147,6 +147,40 @@ export function runProfiledShell(root, command, { waitMs = 0, artifactDirectory,
return { ...run, profile, profilePath, logPath };
}
+/** Run an independent focused comparison without blocking other holders of the machine pool.
+ * @param {string} root
+ * @param {string} command
+ * @param {{ waitMs?: number, gateSlotHeld?: boolean }} [options]
+ * @returns {Promise<object>} The unchanged shell verdict and its private timing profile.
+ */
+export async function runProfiledShellAsync(root, command, { waitMs = 0, gateSlotHeld = false } = {}) {
+ const started = Date.now();
+ const env = { ...gitChildEnv(root), ...(gateSlotHeld ? { [GATE_SLOT_HELD_ENV]: '1' } : {}) };
+ const child = spawn(`(\n${command}\n) 2>&1`, { cwd: root, env, shell: true, stdio: ['ignore', 'pipe', 'pipe'] });
+ const chunks = [];
+ let bytes = 0;
+ let overflow = false;
+ let failure = null;
+ /** Retain merged shell output within the same one-GiB limit as the synchronous gate runner. */
+ const capture = part => {
+ bytes += Buffer.byteLength(part);
+ if (bytes > 2 ** 30) { overflow = true; child.kill(); }
+ else chunks.push(part);
+ };
+ child.stdout.setEncoding('utf8').on('data', capture);
+ child.stderr.setEncoding('utf8').on('data', capture);
+ child.once('error', error => { failure = error; });
+ const status = await new Promise(resolveClose => child.once('close', resolveClose));
+ invalidateGitFacts();
+ const output = chunks.join('') + (overflow ? '\nFocused comparison output exceeded one GiB.\n' : '')
+ + (failure ? `\n${failure.message}\n` : '');
+ const wallMs = Math.max(0, Date.now() - started);
+ const profilePath = join(dirname(gitPath(root, LOG)), `pullboard-gate-${randomUUID()}.profile.json`);
+ const profile = writeTimingProfile(profilePath, { ...timingFromOutput(output, timingRunner(command)),
+ waitMs: Math.max(0, Math.round(waitMs)), wallMs });
+ return { isGreen: status === 0 && !overflow && !failure, output, seconds: Math.round(wallMs / 1000), profile, profilePath, logPath: null };
+}
+
/** Persist one sanitized timing profile as a private artifact.
* @param {string} path
* @param {{ files?: Array<object>, tests?: Array<object>, [key: string]: any }} profile
diff --git a/src/main-red.js b/src/main-red.js
index 9913327f..59238ad8 100644
--- a/src/main-red.js
+++ b/src/main-red.js
@@ -6,7 +6,7 @@ import { join, relative, resolve, sep } from 'node:path';
import { tmpdir } from 'node:os';
import { stripVTControlCharacters } from 'node:util';
import { fileURLToPath } from 'node:url';
-import { runProfiledShell, withGateSlot } from './gate.js';
+import { runProfiledShellAsync, withGateSlot } from './gate.js';
import { cleanGitEnvironment, mainCheckout } from './git.js';
import { mainPolicy, policyAt } from './trusted-policy.js';
import { parseSpec } from './spec.js';
@@ -112,7 +112,7 @@ function gitCall(cwd, args, env) {
}
/** Build one clean temporary checkout at the exact main commit and always remove it. */
-function withMainCheckout(root, commit, run) {
+async function withMainCheckout(root, commit, run) {
const directory = join(tmpdir(), `pullboard-main-red-${randomUUID()}`);
mkdirSync(directory, { mode: 0o700 });
const checkout = join(directory, 'checkout');
@@ -127,7 +127,7 @@ function withMainCheckout(root, commit, run) {
if (clean.status !== 0 || clean.stdout !== '' || head.status !== 0 || head.stdout.trim() !== commit) {
return { unavailable: 'the temporary main checkout was not clean at the captured commit' };
}
- return run(checkout);
+ return await run(checkout);
} finally {
rmSync(directory, { recursive: true, force: true });
}
@@ -158,31 +158,26 @@ export async function compareMainFailures(root, profile, onWait, output = '') {
agent: process.env.PULLBOARD_AGENT ?? `pid-${process.pid}`,
onWait,
});
- try { return await withGateSlot(root, () => {
+ try {
let cache = readCache(file);
- const results = [];
const missing = files.filter(path => !['red', 'green'].includes(cache.entries[main.commit]?.[path]?.result));
- if (missing.length) {
- const outcomes = {};
- for (const path of missing) {
- const measured = withMainCheckout(root, main.commit, checkout => {
- const command = `${policy.config.affectedTests.trimEnd()} ${shellWord('./' + path)}`;
- const run = runProfiledShell(checkout, command, { gateSlotHeld: true });
- return { result: run.isGreen ? 'green' : 'red' };
- });
- if (measured.unavailable) return { main: main.commit, files: [], unavailable: measured.unavailable };
- outcomes[path] = measured;
- }
+ const outcomes = await Promise.allSettled(missing.map(path => withGateSlot(root, async lease => {
+ const measured = await withMainCheckout(root, main.commit, async checkout => {
+ const command = `${policy.config.affectedTests.trimEnd()} ${shellWord('./' + path)}`;
+ const run = await runProfiledShellAsync(checkout, command, { gateSlotHeld: true, waitMs: lease.waitMs });
+ return { result: run.isGreen ? 'green' : 'red' };
+ });
+ if (measured.unavailable) throw new Error(measured.unavailable);
+ // The cache lease excludes other comparisons; synchronous persistence keeps each finished result.
cache = readCache(file);
- const prior = cache.entries[main.commit] && typeof cache.entries[main.commit] === 'object' && !Array.isArray(cache.entries[main.commit])
- ? cache.entries[main.commit] : {};
- cache.entries[main.commit] = { ...prior, ...outcomes };
- try { writeCache(file, cache); }
- catch { return { main: main.commit, files: [], unavailable: 'main results could not be saved to the machine-local cache' }; }
- }
- for (const path of files) results.push({ file: path, result: cache.entries[main.commit][path].result });
- return { main: main.commit, files: results };
- }, { onWait });
+ cache.entries[main.commit] ??= {};
+ cache.entries[main.commit][path] = measured;
+ writeCache(file, cache);
+ }, { onWait })));
+ const failed = outcomes.find(outcome => outcome.status === 'rejected');
+ if (failed) return { main: main.commit, files: [], unavailable: failed.reason?.message ?? 'main comparison could not be completed' };
+ cache = readCache(file);
+ return { main: main.commit, files: files.map(path => ({ file: path, result: cache.entries[main.commit][path].result })) };
} finally { cacheLease.release(); }
}
diff --git a/test/submit-compare.test.js b/test/submit-compare.test.js
new file mode 100644
index 00000000..f19684e2
--- /dev/null
+++ b/test/submit-compare.test.js
@@ -0,0 +1,213 @@
+/** Red submit diagnostics release the seat and share the machine pool [V4,C7,Q4,B7]. */
+import assert from 'node:assert/strict';
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { delimiter, dirname, join } from 'node:path';
+import { DatabaseSync } from 'node:sqlite';
+import { test } from 'node:test';
+import { AGENT_SHELL_MARKERS } from '../src/person.js';
+import { GATE_SLOT_HELD_ENV } from '../src/resources.js';
+import { runFixtureChild, runFixtureGit, startFixtureChild } from './fixture-child.js';
+
+const BIN = join(import.meta.dirname, '../bin/pullboard.js');
+
+/** Quote the current native CLI path for hooks in this fixture's private executable shim. */
+function shellWord(value) { return `'${value.replaceAll("'", "'\\''")}'`; }
+
+/** Write a committed fixture input while keeping all control files outside the checkout. */
+function put(root, path, text) {
+ mkdirSync(dirname(join(root, path)), { recursive: true });
+ writeFileSync(join(root, path), text);
+}
+
+/** Run the real CLI with a private machine home and explicit session identity. */
+function cli(box, root, ...args) {
+ return runFixtureChild(process.execPath, [BIN, ...args, '--json'], { cwd: root, env: box.env, encoding: 'utf8' });
+}
+
+/** Commit the exact fixture inputs using its isolated author identity. */
+function commit(box, root) {
+ runFixtureGit(['add', '-A'], { cwd: root, env: box.env });
+ runFixtureGit(['commit', '-q', '-m', 'test(core): comparison fixture [G1]'], { cwd: root, env: box.env });
+ return runFixtureGit(['rev-parse', 'HEAD'], { cwd: root, env: box.env });
+}
+
+/** Capture asynchronous CLI completion without a throughput deadline or synthetic CLI replacement. */
+function launch(box, ...args) {
+ const child = startFixtureChild(process.execPath, [BIN, ...args, '--json'], {
+ cwd: box.builder, env: box.env, detached: true, stdio: ['ignore', 'pipe', 'pipe'],
+ });
+ let out = '';
+ let err = '';
+ child.stdout.setEncoding('utf8').on('data', part => { out += part; });
+ child.stderr.setEncoding('utf8').on('data', part => { err += part; });
+ child.closed = new Promise(resolveClose => child.once('close', (code, signal) => resolveClose({ code, signal, out, err })));
+ box.children.push(child);
+ return child;
+}
+
+/** Wait for an actual handshake, using a hang ceiling rather than judging how fast work runs. */
+async function observed(predicate, label) {
+ const deadline = Date.now() + 20_000; // budget: hang ceiling for fixture file and queue handshakes, not a throughput expectation.
+ while (!predicate() && Date.now() < deadline) await new Promise(done => setTimeout(done, 10));
+ assert.ok(predicate(), label);
+}
+
+/** Read a private native queue or board without changing its state. */
+function rows(file, sql) {
+ if (!existsSync(file)) return [];
+ const db = new DatabaseSync(file, { readOnly: true });
+ try { db.exec('PRAGMA query_only=ON'); return db.prepare(sql).all(); }
+ finally { db.close(); }
+}
+
+/** Read complete append-only comparison observations. */
+function events(box) {
+ if (!existsSync(box.events)) return [];
+ return readFileSync(box.events, 'utf8').trim().split('\n').filter(Boolean).map(line => JSON.parse(line));
+}
+
+/** Release every comparison before waiting for children and removing this fixture. */
+async function cleanup(box) {
+ for (const name of box.names) writeFileSync(join(box.control, name + '.release'), 'release');
+ await Promise.all(box.children.map(child => child.closed));
+ rmSync(box.directory, { recursive: true, force: true });
+}
+
+/** Build a real joined board whose native red tests pause only in exact-main comparison clones. */
+function fixture(t, count, slots = 2) {
+ const directory = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-submit-compare-')));
+ const root = join(directory, 'repo');
+ const control = join(directory, 'control');
+ mkdirSync(root);
+ mkdirSync(control);
+ put(directory, 'bin/pullboard', `#!/bin/sh\nexec ${shellWord(process.execPath)} ${shellWord(BIN)} "$@"\n`);
+ chmodSync(join(directory, 'bin/pullboard'), 0o755);
+ const env = { ...process.env };
+ for (const key of Object.keys(env)) if (key.startsWith('GIT_') || AGENT_SHELL_MARKERS.includes(key)) delete env[key];
+ delete env.NODE_TEST_CONTEXT;
+ delete env.PULLBOARD_RELAY_TOKEN;
+ delete env[GATE_SLOT_HELD_ENV];
+ Object.assign(env, {
+ GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1',
+ GIT_AUTHOR_NAME: 'Comparison fixture', GIT_AUTHOR_EMAIL: '[email protected]',
+ GIT_COMMITTER_NAME: 'Comparison fixture', GIT_COMMITTER_EMAIL: '[email protected]',
+ PULLBOARD_HOME: join(directory, 'home'), PULLBOARD_MACHINE_HOME: join(directory, 'machine'),
+ PATH: [join(directory, 'bin'), process.env.PATH].filter(Boolean).join(delimiter),
+ CODEX_SESSION_ID: 'comparison-fixture-session', PULLBOARD_MODEL: 'Fixture Model',
+ COMPARE_CONTROL: control, COMPARE_EVENTS: join(directory, 'events.jsonl'),
+ });
+ const names = ['a', 'b', 'c'].slice(0, count);
+ const box = { directory, root, control, names, env, events: env.COMPARE_EVENTS, children: [] };
+ t.after(() => cleanup(box));
+ runFixtureGit(['init', '-q', '-b', 'main'], { cwd: root, env });
+ assert.equal(cli(box, root, 'init').status, 0);
+ put(root, 'package.json', '{"type":"module"}\n');
+ put(root, 'pullboard.json', JSON.stringify({ gate: 'node test/runner.js', affectedTests: 'node test/runner.js',
+ lanes: { core: { owns: ['test/'], specs: ['G'] } } }));
+ put(root, 'SPEC.md', '# Comparison fixture\n\n## G · Goals\n- G1 [approved, must] The comparison retains a red verdict. | gate: test\n');
+ for (const name of names) put(root, `test/${name}.test.js`,
+ `import assert from 'node:assert/strict';\nimport { test } from 'node:test';\ntest('${name} native comparison [G1]', () => assert.equal(1, ${name === 'b' ? 2 : 1}));\n`);
+ put(root, 'test/runner.js', [
+ "import { spawnSync } from 'node:child_process';",
+ "import { appendFileSync, existsSync, writeFileSync } from 'node:fs';",
+ "import { basename, join } from 'node:path';",
+ `const files = process.argv.slice(2).length ? process.argv.slice(2) : ${JSON.stringify(names.map(name => `test/${name}.test.js`))};`,
+ "const comparison = basename(process.cwd()) === 'checkout';",
+ "const name = basename(files[0], '.test.js');",
+ "/** Record each real start or finish, including its inherited pool marker. */",
+ "function record(phase) { appendFileSync(process.env.COMPARE_EVENTS, JSON.stringify({phase, name, cwd: process.cwd(), slot: process.env.PULLBOARD_GATE_SLOT_HELD}) + '\\n'); }",
+ "if (comparison) {",
+ " record('start');",
+ " writeFileSync(join(process.env.COMPARE_CONTROL, name + '.started'), 'started');",
+ " while (!existsSync(join(process.env.COMPARE_CONTROL, name + '.release'))) await new Promise(done => setTimeout(done, 10));",
+ "}",
+ "const run = spawnSync(process.execPath, ['--test', '--test-reporter=tap', ...files], { encoding: 'utf8', env: process.env });",
+ "process.stdout.write(run.stdout ?? ''); process.stderr.write(run.stderr ?? '');",
+ "if (comparison) record('finish');",
+ 'process.exitCode = run.status ?? 1;',
+ ].join('\n') + '\n');
+ box.main = commit(box, root);
+ assert.equal(cli(box, root, 'settings', 'gateSlots', String(slots)).status, 0);
+ const added = cli(box, root, 'add', 'core', 'Retain the native red verdict', '--specs', 'G1', '--route', 'mid',
+ '--criterion', 'A red test is refused', '--check', 'true', '--brief', 'Files: test/\nTest: native comparison');
+ assert.equal(added.status, 0, added.stderr);
+ box.item = JSON.parse(added.stdout).id;
+ const made = cli(box, root, 'worktree', 'core', '--model', 'Fixture Model');
+ assert.equal(made.status, 0, made.stderr);
+ box.builder = JSON.parse(made.stdout).path;
+ assert.equal(cli(box, box.builder, 'claim', String(box.item)).status, 0);
+ for (const name of names) put(box.builder, `test/${name}.test.js`,
+ `import assert from 'node:assert/strict';\nimport { test } from 'node:test';\ntest('${name} native comparison [G1]', () => assert.equal(1, 2));\n// Candidate-only input.\n`);
+ box.head = commit(box, box.builder);
+ box.cache = join(root, '.git/pullboard/main-red.json');
+ box.board = join(root, '.git/pullboard/board.sqlite');
+ box.repoQueue = join(root, '.git/pullboard/resources.sqlite');
+ box.machineQueue = join(env.PULLBOARD_MACHINE_HOME, 'resources.sqlite');
+ return box;
+}
+
+/** A comparison must preserve the refusal and both Git heads after all diagnostics finish. */
+async function assertRed(box, submission) {
+ const result = await submission.closed;
+ assert.equal(result.code, 1, result.out + result.err);
+ assert.equal(JSON.parse(result.out).error.code, 'GATE_RED');
+ assert.equal(rows(box.board, 'SELECT item_status FROM item')[0].item_status, 'claimed');
+ assert.equal(runFixtureGit(['rev-parse', 'HEAD'], { cwd: box.builder, env: box.env }), box.head);
+ assert.equal(runFixtureGit(['rev-parse', 'HEAD'], { cwd: box.root, env: box.env }), box.main);
+ for (const event of events(box)) assert.equal(existsSync(event.cwd), false, 'temporary main checkouts are removed');
+}
+
+test('a shout completes while a submit compares with main [V4,C7,B7]', async t => {
+ const box = fixture(t, 1);
+ const submission = launch(box, 'submit', String(box.item));
+ await observed(() => events(box).some(event => event.phase === 'start'), 'the real main comparison is waiting');
+ const shout = launch(box, 'shout', 'coordinator', 'Comparison is still running');
+ await observed(() => shout.exitCode !== null || rows(box.repoQueue,
+ "SELECT ticket FROM waiter WHERE name LIKE 'checkout-session:%'").length > 0, 'the shout lands or reaches the checkout guard');
+ assert.equal(shout.exitCode, 0, 'the same-seat shout completes before releasing the comparison');
+ assert.equal((await shout.closed).code, 0);
+ assert.equal(rows(box.board, "SELECT shout_text FROM shout WHERE shout_text='Comparison is still running'").length, 1);
+ const fact = cli(box, box.builder, 'fact', String(box.item), 'note', 'This fact lands during the comparison');
+ assert.equal(fact.status, 0, fact.stderr);
+ assert.equal(cli(box, box.builder, 'show', String(box.item)).status, 0, 'same-seat reads remain available');
+ assert.equal(submission.exitCode, null, 'diagnostics remain held until the explicit release');
+ assert.equal(events(box).filter(event => event.phase === 'finish').length, 0);
+ writeFileSync(join(box.control, 'a.release'), 'release');
+ await assertRed(box, submission);
+});
+
+test('a red submit reruns its failed files in parallel within the pool [V4,C7,Q4]', async t => {
+ const box = fixture(t, 3, 2);
+ const submission = launch(box, 'submit', String(box.item));
+ await observed(() => events(box).filter(event => event.phase === 'start').length >= 2,
+ 'two independent exact-main comparisons start before either is released');
+ assert.equal(rows(box.machineQueue, "SELECT token FROM holder WHERE name='gate'").length, 2,
+ 'both running comparisons hold actual pool slots');
+ await observed(() => rows(box.machineQueue, "SELECT ticket FROM waiter WHERE name='gate'").length === 1,
+ 'the third file waits in the actual machine pool');
+ const first = events(box).find(event => event.phase === 'start').name;
+ writeFileSync(join(box.control, first + '.release'), 'release');
+ await observed(() => events(box).filter(event => event.phase === 'start').length === 3,
+ 'the third file starts after a slot is released');
+ assert.equal(JSON.parse(readFileSync(box.cache, 'utf8')).entries[box.main][`test/${first}.test.js`].result,
+ first === 'b' ? 'red' : 'green', 'the completed result is saved while other files still wait');
+ for (const name of box.names) writeFileSync(join(box.control, name + '.release'), 'release');
+ await assertRed(box, submission);
+ let active = 0;
+ let maximum = 0;
+ for (const event of events(box)) {
+ assert.equal(event.slot, '1', 'each runner inherits a real parent-held slot');
+ active += event.phase === 'start' ? 1 : -1;
+ maximum = Math.max(maximum, active);
+ }
+ assert.equal(maximum, 2, 'comparisons overlap without exceeding the configured two slots');
+ assert.equal(active, 0);
+ assert.deepEqual(JSON.parse(readFileSync(box.cache, 'utf8')).entries[box.main], {
+ 'test/a.test.js': { result: 'green' }, 'test/b.test.js': { result: 'red' }, 'test/c.test.js': { result: 'green' },
+ });
+ assert.equal(rows(box.machineQueue, 'SELECT token FROM holder').length, 0, 'all machine leases are released');
+ const before = events(box).length;
+ await assertRed(box, launch(box, 'submit', String(box.item)));
+ assert.equal(events(box).length, before, 'a later red submission reuses every exact-main cached result');
+});