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): select tests across merged changes [V4,C7]

Corey Olsonac540601b4e9
diff --git a/src/affected-tests.js b/src/affected-tests.js
index 3baec9f1..5ad4f514 100644
--- a/src/affected-tests.js
+++ b/src/affected-tests.js
@@ -62,10 +62,12 @@ function graphAt(root, commit) {
const imports = importsIn([...modules.values()]);
const reverse = new Map();
const unknown = new Map();
+ const always = new Set();
paths.forEach((path, index) => {
for (const target of imports[index]) {
if (target === 'node:module') { unknown.set(path, 'a reached module may create CommonJS imports'); continue; }
if (target.startsWith('node:')) continue;
+ if (target === '(computed)' && TEST.test(path)) { always.add(path); continue; }
if (!target.startsWith('.')) {
unknown.set(path, target === '(computed)' ? 'non-literal dynamic import' : 'unresolved package import');
continue;
@@ -76,7 +78,7 @@ function graphAt(root, commit) {
reverse.get(dependency).add(path);
}
});
- return { paths, reverse, unknown };
+ return { paths, reverse, unknown, always };
}
/**
@@ -84,12 +86,18 @@ function graphAt(root, commit) {
* deletions and renames; process launches and filesystem reads are deliberately not import edges.
*
* @param {string} root
- * @param {{base:string, commit?:string, changed?:string[]}} options
+ * @param {{base:string|null, trunk?:string, commit?:string, changed?:string[]}} options
* @returns {{full:boolean, reason:string, files:string[]}}
*/
-export function selectAffectedTests(root, { base, commit = 'HEAD', changed }) {
+export function selectAffectedTests(root, { base, trunk, commit = 'HEAD', changed }) {
let files = [];
try {
+ if (!base) return { full: true, reason: 'no claim head recorded; the change cannot be selected', files };
+ // The merge base includes topic parents and merge resolutions that receipt history omits.
+ if (trunk) {
+ base = selectionGit(root, ['merge-base', trunk, commit]).trim();
+ changed = undefined;
+ }
changed ??= selectionGit(root, ['diff', '--no-renames', '--name-only', '-z', base, commit]).split('\0').filter(Boolean);
const after = graphAt(root, commit);
files = after.paths.filter(path => TEST.test(path)).sort();
@@ -110,7 +118,7 @@ export function selectAffectedTests(root, { base, commit = 'HEAD', changed }) {
}
}
}
- const selected = files.filter(path => reached.has(path));
+ const selected = files.filter(path => reached.has(path) || before.always.has(path) || after.always.has(path));
if (selected.length === files.length) return { full: true, reason: 'the change reaches every test file', files };
return { full: false, reason: '', files: selected };
} catch (error) {
diff --git a/src/cli.js b/src/cli.js
index b7f09ca7..93b65370 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -1581,7 +1581,7 @@ async function submitHere(ctx, id) {
requireTrunkMerge(root, commit);
// Every submit proves its frozen check and reachable tests anew on this exact commit (V4,V16).
// A full-gate stamp never substitutes for either proof; only an actual full fallback may stamp.
- const gate = await runSubmitGate(root, ctx.config, { base: claimHead, changed: filesSince(root, id, claimHead, commit), check: frozenCheck(held), onWait: gateWaitReporter(ctx.io) });
+ const gate = await runSubmitGate(root, ctx.config, { base: claimHead, trunk: acceptedMain.commit, check: frozenCheck(held), onWait: gateWaitReporter(ctx.io) });
if (!gate.isGreen) throw new Refused('GATE_RED', `the submission proof is red at ${commit.slice(0, 12)}; fix it, commit, submit again. ${submitGateReport(gate)}`);
if (headCommit(root) !== commit || !isClean(root)) {
throw new Refused(
diff --git a/src/gate.js b/src/gate.js
index 1abea688..4ea571f5 100644
--- a/src/gate.js
+++ b/src/gate.js
@@ -173,12 +173,12 @@ function commandWord(value) {
*
* @param {string} root
* @param {any} config
- * @param {{base:string, changed:string[], check:string, onWait?:(state:object)=>void}} options
+ * @param {{base:string|null, trunk?:string, changed?:string[], check:string, onWait?:(state:object)=>void}} options
* @returns {Promise<any>}
*/
-export async function runSubmitGate(root, config, { base, changed, check, onWait }) {
+export async function runSubmitGate(root, config, { base, trunk, changed, check, onWait }) {
if (!config.gate.trim()) throw new Refused('NO_GATE', 'no gate configured; set "gate" in pullboard.json, e.g. "npm test"');
- const selection = selectAffectedTests(root, { base, changed });
+ const selection = selectAffectedTests(root, { base, trunk, changed });
return await withGateSlot(root, () => {
const criterion = check ? runShell(root, check) : { isGreen: true, output: '', seconds: 0 };
const receipt = { command: check, green: criterion.isGreen, seconds: criterion.seconds, checked: Boolean(check) };
diff --git a/test/submit-gate.test.js b/test/submit-gate.test.js
index 77c260f9..93d51bae 100644
--- a/test/submit-gate.test.js
+++ b/test/submit-gate.test.js
@@ -6,7 +6,7 @@ import assert from 'node:assert/strict';
import { execFileSync, spawnSync } from 'node:child_process';
import { appendFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
-import { dirname, join } from 'node:path';
+import { dirname, join, resolve } from 'node:path';
import { after, test } from 'node:test';
import { selectAffectedTests } from '../src/affected-tests.js';
@@ -60,6 +60,25 @@ function privateRepo(t) {
return { dir, root, env, git, run };
}
+/** Create a real joined builder while the coordinator stays on its unchanged trunk. */
+function builderRepo(box) {
+ const config = JSON.parse(readFileSync(join(box.root, 'pullboard.json'), 'utf8'));
+ config.lanes = { build: { owns: ['src/', 'test/'], specs: ['G'] } };
+ put(box.root, 'pullboard.json', JSON.stringify(config));
+ commitAll(box);
+ const made = box.run('worktree', 'build', '--json');
+ assert.equal(made.code, 0, made.err);
+ const root = JSON.parse(made.out).path;
+ /** Run Git only in the joined builder. */
+ const git = (...args) => execFileSync('git', args, { cwd: root, env: box.env, encoding: 'utf8', stdio: 'pipe' }).trim();
+ /** Run the real CLI with the joined builder identity. */
+ const run = (...args) => {
+ const result = spawnSync(process.execPath, [BIN, ...args], { cwd: root, env: box.env, encoding: 'utf8', timeout: 90_000 });
+ return { code: result.status, out: result.stdout ?? '', err: result.stderr ?? '' };
+ };
+ return { ...box, root, git, run, coordinator: box.run };
+}
+
/** Write a fixture file, creating its parent directory when needed. */
function put(root, path, text) {
const file = join(root, path);
@@ -140,7 +159,7 @@ test('unknown imports and broad inputs fall back explicitly, while an all-test c
});
test('submit runs the frozen check and affected test only; landing and explicit gate remain full [V4,C7,V16]', (t) => {
- const box = privateRepo(t);
+ let box = privateRepo(t);
const checkCommand = 'node -e "require(\'node:fs\').appendFileSync(process.env.ITEM_CHECK_MARKER, \'check\')"';
const checkMarker = join(box.dir, 'item-check.log');
box.env.ITEM_CHECK_MARKER = checkMarker;
@@ -159,7 +178,8 @@ test('submit runs the frozen check and affected test only; landing and explicit
"assert.fail('independent test must not block this submit');",
].join('\n'));
commitAll(box);
- const added = box.run('add', 'coordinator', 'Source change', '--check', checkCommand);
+ box = builderRepo(box);
+ const added = box.coordinator('add', 'build', 'Source change', '--check', checkCommand);
assert.equal(added.code, 0, added.err);
assert.equal(box.run('claim', '1').code, 0);
@@ -168,7 +188,7 @@ test('submit runs the frozen check and affected test only; landing and explicit
const marker = join(box.dir, 'affected-ran.log');
box.env.AFFECTED_MARKER = marker;
rmSync(checkMarker, { force: true });
- const stamp = join(box.root, box.git('rev-parse', '--git-path', 'pullboard-gate-green'));
+ const stamp = resolve(box.root, box.git('rev-parse', '--git-path', 'pullboard-gate-green'));
writeFileSync(stamp, 'forged-stamp-must-not-change\n');
const submitted = box.run('submit', '1', '--json');
@@ -195,20 +215,21 @@ test('submit runs the frozen check and affected test only; landing and explicit
const remote = join(box.dir, 'remote.git');
box.git('init', '-q', '--bare', remote);
box.git('remote', 'add', 'origin', remote);
- const landing = spawnSync('git', ['push', '-q', 'origin', 'main'], { cwd: box.root, env: box.env, encoding: 'utf8', timeout: 90_000 });
+ const landing = spawnSync('git', ['push', '-q', 'origin', 'HEAD:main'], { cwd: box.root, env: box.env, encoding: 'utf8', timeout: 90_000 });
assert.notEqual(landing.status, 0, 'the landing pre-push hook still runs the full gate');
assert.match(`${landing.stdout ?? ''}\n${landing.stderr ?? ''}`, /gate is red|not ok 2/);
assert.equal(existsSync(stamp), false, 'neither subset nor red full gate leaves a green full-gate stamp');
});
test('submit without a frozen check reports no item check and still runs affected tests [V4,C7,V16]', (t) => {
- const box = privateRepo(t);
+ let box = privateRepo(t);
put(box.root, 'pullboard.json', JSON.stringify({ gate: 'node --test' }));
put(box.root, 'src/a.js', 'export const value = 1;\n');
put(box.root, 'test/a.test.js', "import '../src/a.js'; import { appendFileSync } from 'node:fs'; appendFileSync(process.env.AFFECTED_MARKER, 'ran');\n");
put(box.root, 'test/b.test.js', "import assert from 'node:assert/strict'; assert.fail('unrelated');\n");
commitAll(box);
- assert.equal(box.run('add', 'coordinator', 'No frozen command').code, 0);
+ box = builderRepo(box);
+ assert.equal(box.coordinator('add', 'build', 'No frozen command').code, 0);
assert.equal(box.run('claim', '1').code, 0);
put(box.root, 'src/a.js', 'export const value = 2;\n');
commitAll(box, 'feat(core): update selected source [G1]');
@@ -226,10 +247,11 @@ test('submit without a frozen check reports no item check and still runs affecte
});
test('a red frozen item check refuses submit before affected tests run [V4,C7,V16]', (t) => {
- const box = privateRepo(t);
+ let box = privateRepo(t);
put(box.root, 'pullboard.json', JSON.stringify({ gate: 'node --test' }));
seedGraph(box);
- const added = box.run('add', 'coordinator', 'Frozen check', '--check', 'node -e "process.exit(process.env.CHECK_REFUSE ? 1 : 0)"');
+ box = builderRepo(box);
+ const added = box.coordinator('add', 'build', 'Frozen check', '--check', 'node -e "process.exit(process.env.CHECK_REFUSE ? 1 : 0)"');
assert.equal(added.code, 0, added.err);
assert.equal(box.run('claim', '1').code, 0);
put(box.root, 'src/a.js', 'export const value = 2;\n');
@@ -274,3 +296,110 @@ test('CommonJS and nested package scopes fall back instead of silently missing r
assert.equal(loader.full, true, 'a reached CommonJS loader also makes the ESM import graph incomplete');
assert.match(loader.reason, /CommonJS/u);
});
+
+
+for (const mode of ['topic parent', 'merge commit']) {
+ test(`affected tests include source changes carried by a ${mode} [V4,C7,V16]`, (t) => {
+ let box = privateRepo(t);
+ put(box.root, 'pullboard.json', JSON.stringify({ gate: 'node --test' }));
+ seedGraph(box);
+ put(box.root, 'test/a.test.js', [
+ "import assert from 'node:assert/strict';",
+ "import { appendFileSync } from 'node:fs';",
+ "import { value } from '../src/middle.js';",
+ "appendFileSync(process.env.AFFECTED_MARKER, 'ran\\n');",
+ 'assert.equal(value, 1);',
+ ].join('\n'));
+ box = builderRepo(box);
+ assert.equal(box.coordinator('add', 'build', 'Merged source', '--check', 'true').code, 0);
+ assert.equal(box.run('claim', '1').code, 0);
+ const branch = box.git('branch', '--show-current');
+ box.git('checkout', '-q', '-b', 'topic');
+ put(box.root, mode === 'topic parent' ? 'src/a.js' : 'src/notes.js', 'export const value = 2;\n');
+ commitAll(box, 'feat(core): topic source [G1]');
+ box.git('checkout', '-q', branch);
+ if (mode === 'merge commit') {
+ put(box.root, 'src/other.js', 'export const other = false;\n');
+ commitAll(box, 'feat(core): unrelated source [G1]');
+ box.git('merge', '--no-ff', '--no-commit', 'topic');
+ put(box.root, 'src/a.js', 'export const value = 2;\n');
+ commitAll(box, 'feat(core): resolve merged source [G1]');
+ } else box.git('merge', '--no-ff', '-q', 'topic', '-m', 'feat(core): merge topic source [G1]');
+ assert.equal(box.git('rev-list', '--parents', '-n', '1', 'HEAD').split(' ').length, 3, 'the candidate really has two parents');
+ const marker = join(box.dir, 'merged-test-ran.log');
+ box.env.AFFECTED_MARKER = marker;
+ const standalone = spawnSync(process.execPath, ['--test', 'test/a.test.js'], { cwd: box.root, env: box.env, encoding: 'utf8' });
+ assert.equal(standalone.status, 1, 'the unchanged importer detects the merged source regression');
+ assert.match(standalone.stdout, /ERR_ASSERTION/u);
+ rmSync(marker);
+ const refused = box.run('submit', '1');
+ assert.equal(refused.code, 1, refused.out || refused.err);
+ assert.match(refused.err, /GATE_RED/u);
+ assert.match(refused.err, /affected tests:.*test\/a\.test\.js/u);
+ assert.match(refused.err, /ERR_ASSERTION/u);
+ assert.equal(readFileSync(marker, 'utf8'), 'ran\n', 'submit actually executed the transitive importer');
+ });
+}
+
+test('computed imports in test files are always selected in both graphs without forcing full [V4,C7]', (t) => {
+ const box = privateRepo(t);
+ const original = seedGraph(box);
+ put(box.root, 'test/c.test.js', "export const load = path => import(path);\n");
+ const base = commitAll(box);
+ const added = selectAffectedTests(box.root, { base: original, commit: base, changed: ['src/a.js'] });
+ assert.equal(added.full, false);
+ assert.deepEqual(added.files, ['test/a.test.js', 'test/c.test.js'], 'the new graph also always selects its computed-import test');
+ put(box.root, 'src/a.js', 'export const value = 2;\n');
+ let commit = commitAll(box, 'feat(core): source change [G1]');
+ let selection = selectAffectedTests(box.root, { base, commit });
+ assert.equal(selection.full, false);
+ assert.deepEqual(selection.files, ['test/a.test.js', 'test/c.test.js'], 'an unrelated computed-import test is always selected');
+ selection = selectAffectedTests(box.root, { base, commit, changed: ['test/c.test.js'] });
+ assert.equal(selection.full, false, 'reaching a computed-import test does not force full');
+ assert.deepEqual(selection.files, ['test/c.test.js']);
+ put(box.root, 'test/c.test.js', "import 'node:assert/strict';\n");
+ commit = commitAll(box, 'test(core): remove computed loader [G1]');
+ selection = selectAffectedTests(box.root, { base, commit, changed: ['src/a.js'] });
+ assert.equal(selection.full, false);
+ assert.deepEqual(selection.files, ['test/a.test.js', 'test/c.test.js'], 'the old graph keeps its computed-import test selected');
+});
+
+test('affected selection says explicitly when no claim head was recorded [V4,C7]', (t) => {
+ const box = privateRepo(t);
+ seedGraph(box);
+ const selection = selectAffectedTests(box.root, { base: null, trunk: 'refs/heads/main' });
+ assert.equal(selection.full, true);
+ assert.match(selection.reason, /no claim head recorded/u);
+});
+
+
+test('affected selection reads old edges and all changes at the trunk merge base [V4,C7]', (t) => {
+ const box = privateRepo(t);
+ const trunk = seedGraph(box);
+ box.git('rm', 'src/a.js');
+ const claim = commitAll(box, 'refactor(core): remove old source [G1]');
+ // A claim made after the deletion and an empty receipt must not hide the trunk-relative change.
+ const selection = selectAffectedTests(box.root, { base: claim, trunk, changed: [] });
+ assert.equal(selection.full, false);
+ assert.deepEqual(selection.files, ['test/a.test.js']);
+});
+
+
+test('affected submit includes branch changes made before the item claim [V4,C7,V16]', (t) => {
+ let box = privateRepo(t);
+ put(box.root, 'pullboard.json', JSON.stringify({ gate: 'node --test' }));
+ seedGraph(box);
+ put(box.root, 'test/a.test.js', "import assert from 'node:assert/strict'; import { value } from '../src/middle.js'; assert.equal(value, 1);\n");
+ box = builderRepo(box);
+ put(box.root, 'src/a.js', 'export const value = 2;\n');
+ commitAll(box, 'feat(core): source before claim [G1]');
+ assert.equal(box.coordinator('add', 'build', 'Existing source change', '--check', 'true').code, 0);
+ assert.equal(box.run('claim', '1').code, 0);
+ put(box.root, 'src/other.js', 'export const other = false;\n');
+ commitAll(box, 'feat(core): source after claim [G1]');
+ const refused = box.run('submit', '1');
+ assert.equal(refused.code, 1, refused.out || refused.err);
+ assert.match(refused.err, /GATE_RED/u);
+ assert.match(refused.err, /affected tests:.*test\/a\.test\.js/u);
+ assert.match(refused.err, /ERR_ASSERTION/u);
+});