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(gate): limit pipefail to repo gates [V16,C7]

Corey Olsond858b8ce9a8f
diff --git a/src/gate.js b/src/gate.js
index 0a73a7ae..746d4c19 100644
--- a/src/gate.js
+++ b/src/gate.js
@@ -93,16 +93,35 @@ export function isStampedGreen(root) {
*
* @param {string} root
* @param {string} command
+ * @param {{ pipefail?: boolean }} [options] - Enable every-stage failures only for the configured repo gate.
* @returns {{ isGreen: boolean, output: string, seconds: number }}
*/
-export function runShell(root, command) {
+export function runShell(root, command, { pipefail = false } = {}) {
const started = Date.now();
+ const options = { cwd: root, env: gitChildEnv(root), shell: true, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', maxBuffer: 2 ** 30 };
+ let prefix = '';
+ if (pipefail) {
+ // Probe the same shell as the command, with no load-sensitive deadline or alternate shell.
+ const probe = spawnSync('set -o pipefail', options);
+ if (probe.status === 0) prefix = 'set -o pipefail\n';
+ else if (hasPipeline(command)) {
+ throw new Refused('PIPEFAIL_UNAVAILABLE', 'the repo gate shell cannot enable pipefail; rewrite the gate without a pipe');
+ }
+ }
// Newlines, not spaces, around the command, so a trailing comment in it cannot swallow the `)`.
- const result = spawnSync(`(\n${command}\n) 2>&1`, { cwd: root, env: gitChildEnv(root), shell: true, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', maxBuffer: 2 ** 30 });
+ const result = spawnSync(`${prefix}(\n${command}\n) 2>&1`, options);
invalidateGitFacts();
return { isGreen: result.status === 0, output: `${result.stdout ?? ''}${result.stderr ?? ''}`, seconds: Math.round((Date.now() - started) / 1000) };
}
+/** Conservatively detect a pipe when the configured gate shell cannot enforce every stage's status. */
+function hasPipeline(command) {
+ for (let index = 0; index < command.length; index += 1) {
+ if (command[index] === '|' && command[index + 1] !== '|' && command[index - 1] !== '|') return true;
+ }
+ return false;
+}
+
/** Run work under the machine gate queue, keeping landing gates ahead of ordinary gates and item checks last.
*
* @param {string} root - Checkout used to locate the machine resource database.
@@ -156,7 +175,7 @@ export async function runGate(root, config, { trustStamp = true, onWait, landing
function fullGateRun(root, config, trustStamp = false) {
if (trustStamp && isStampedGreen(root)) return { isGreen: true, isCached: true, output: '', seconds: 0, log: '' };
const before = committedTree(root);
- const { isGreen, output, seconds } = runShell(root, config.gate);
+ const { isGreen, output, seconds } = runShell(root, config.gate, { pipefail: true });
const log = gitPath(root, LOG);
writeFileSync(log, output);
if (isGreen && before !== null && committedTree(root) === before) writeFileSync(gitPath(root, STAMP), `${before}\n`);
diff --git a/test/gate.test.js b/test/gate.test.js
index 0b6d76c8..95c568df 100644
--- a/test/gate.test.js
+++ b/test/gate.test.js
@@ -10,6 +10,8 @@ import { join, resolve } from 'node:path';
import { after, test } from 'node:test';
import { digestOf } from '../src/gate.js';
+const PRIVATE_WORKER = resolve(import.meta.dirname, '../src/private-check-worker.js');
+
const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');
const dirs = [];
after(() => {
@@ -21,8 +23,9 @@ after(() => {
* to runs.log beside the repo, so a test can count the runs.
*
* @param {string} gate
+ * @param {string} [check]
*/
-function repoWithGate(gate) {
+function repoWithGate(gate, check = '') {
const dir = realpathSync(mkdtempSync(join(tmpdir(), 'pullboard-gate-')));
dirs.push(dir);
const repo = join(dir, 'repo');
@@ -44,7 +47,8 @@ function repoWithGate(gate) {
writeFileSync(join(repo, 'SPEC.md'), '# Demo\n\n## G · Goals\n- G1 [approved, must] It works. | gate: test\n');
git('add', '-A');
git('commit', '-q', '-m', 'chore: a spec');
- assert.equal(run('add', 'coordinator', 'Work').status, 0);
+ const added = run('add', 'coordinator', 'Work', ...(check ? ['--check', check, '--wait'] : []));
+ assert.equal(added.status, 0, `${added.stdout}${added.stderr}`);
assert.equal(run('claim', '1').status, 0);
const runs = () => {
try {
@@ -53,7 +57,7 @@ function repoWithGate(gate) {
return 0;
}
};
- return { repo, git, run, runs };
+ return { dir, repo, env, git, run, runs };
}
test('a digest keeps the failures and the end within its cap, however long one line is [V10]', () => {
@@ -139,3 +143,72 @@ test('submit refuses a tree its gate left changed: a new commit, or an edited tr
assert.equal(box.runs(), 1, moves);
}
});
+
+
+/** Quote a test executable or path as one shell word. */
+function shellWord(value) {
+ return "'" + String(value).replaceAll("'", "'\\''") + "'";
+}
+
+/** Run the production private-check worker with a real shell and durable output file. */
+function privateCheck(box, command) {
+ const logPath = join(box.dir, 'private-command.log');
+ const pidFile = logPath + '.pid';
+ const run = spawnSync(process.execPath, [PRIVATE_WORKER], {
+ cwd: box.repo, env: box.env, encoding: 'utf8',
+ input: JSON.stringify({ command, timeout: 30_000, pidFile, logPath }), timeout: 35_000,
+ });
+ rmSync(pidFile, { force: true });
+ assert.equal(run.status, 0, `${run.stdout}${run.stderr}`);
+ return JSON.parse(run.stdout);
+}
+
+test('a piped gate fails on any red stage without changing its existing shell [V16,C7]', () => {
+ const supported = spawnSync('set -o pipefail', { shell: true }).status === 0;
+ const failing = repoWithGate('sh -c "exit 7" | tail -1');
+ const red = failing.run('gate', '--json');
+ assert.equal(red.status, 1, 'a failing producer cannot leave the gate green');
+ if (supported) assert.equal(JSON.parse(red.stdout).green, false);
+ else {
+ const error = JSON.parse(red.stdout).error;
+ assert.equal(error.code, 'PIPEFAIL_UNAVAILABLE');
+ assert.match(error.message, /rewrite the gate without a pipe/u);
+ assert.equal(failing.runs(), 0, 'an unsupported gate never starts');
+ }
+ const plain = repoWithGate('sh -c "exit 7"');
+ assert.equal(plain.run('gate').status, 1, 'plain failure remains red');
+ const passing = repoWithGate("printf 'pipeline-ok\\n' | grep -q pipeline-ok");
+ const green = passing.run('gate', '--json');
+ if (supported) assert.equal(green.status, 0, `${green.stdout}${green.stderr}`);
+ else assert.equal(JSON.parse(green.stdout).error.code, 'PIPEFAIL_UNAVAILABLE');
+ const ordinary = repoWithGate('true');
+ assert.equal(ordinary.run('gate').status, 0, 'a plain passing gate needs no pipefail capability');
+});
+
+test('a piped gate fails safely while named-test item checks keep last-stage semantics [V16,C7]', () => {
+ const box = repoWithGate('true');
+ const fixture = join(box.dir, 'named-output.test.js');
+ writeFileSync(fixture, `import {test} from 'node:test';
+import {setTimeout} from 'node:timers/promises';
+test('passing named item check', () => {});
+test('output after the match', async () => {
+ await setTimeout(25);
+ process.stdout.write('x'.repeat(1024 * 1024));
+});
+`);
+ const runner = resolve(import.meta.dirname, '../bin/run-tests.js');
+ const named = `${shellWord(process.execPath)} ${shellWord(runner)} ${shellWord(fixture)} 2>&1 | grep -Eq '^ok [0-9]+ - passing named item check'`;
+ for (const command of [named, 'yes | grep -q y', 'sh -c "exit 7" | tail -1']) {
+ const item = repoWithGate('true', command);
+ const baseline = JSON.parse(item.run('show', '1', '--json').stdout).item_check_baseline;
+ assert.equal(baseline.result, 'green', 'background baselines preserve last-stage semantics');
+ const checked = item.run('check', '1', '--yes');
+ assert.equal(checked.status, 0, `${checked.stdout}${checked.stderr}`);
+ const submitted = item.run('submit', '1', '--json');
+ assert.equal(submitted.status, 0, `${submitted.stdout}${submitted.stderr}`);
+ assert.equal(JSON.parse(submitted.stdout).gate.check.green, true, 'submission uses the same check semantics');
+ const verified = privateCheck(item, command);
+ assert.equal(verified.status, 0, JSON.stringify(verified));
+ assert.equal(verified.error, null, 'the private verifier keeps its original shell behavior');
+ }
+});