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(land): a failing pipeline producer is never waived [B3,R3,C7]
Corey Olsona6c9a98a2cac
diff --git a/src/gate.js b/src/gate.jsindex d8e9e6a0..07e31681 100644--- a/src/gate.js+++ b/src/gate.js@@ -6,9 +6,9 @@*/import { spawn, spawnSync } from 'node:child_process';import { randomUUID } from 'node:crypto';-import { existsSync, mkdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';-import { availableParallelism } from 'node:os';+import { availableParallelism, tmpdir } from 'node:os';import { fileURLToPath } from 'node:url';import { gitChildEnv, gitPath, headTree, invalidateGitFacts, isClean, untracked } from './git.js';import { secretsIn } from './hooks.js';@@ -149,49 +149,67 @@ export function isStampedGreen(root) {}/**- * Split a gate command into its top-level stages, the commands joined by `&&`, `||`, `;` or a newline.- * Anything the split cannot read for certain (a subshell, a group, command substitution, a heredoc,- * a comment, a background job or an open quote) returns null, so no stage is ever guessed [B3,R3].+ * Split a gate command into its top-level stages, the commands joined by `&&`, `||`, `;` or a newline,+ * and each stage into its pipeline components, the commands joined by `|`. Anything the split cannot+ * read for certain (a subshell, a group, command substitution, a heredoc, a comment, a background job,+ * `|&` or an open quote) returns null, so no stage or component is ever guessed [B3,R3].** @param {string} command- * @returns {Array<{ text: string, op: string }> | null}+ * @returns {Array<{ text: string, op: string, pipeline: string[] }> | null}*/export function gateStages(command) {const source = String(command).trim();const stages = [];let current = '';+ let segment = '';+ let pipeline = [];let quote = null;let variable = 0;+ /** Append text to both the stage and its current pipeline component. */+ const add = (text) => { current += text; segment += text; };+ /** Close the current pipeline component; an empty or line-continued component is unreadable. */+ const pipe = () => {+ if (!segment.trim() || /\\$/u.test(segment.trim())) return false;+ pipeline.push(segment.trim());+ segment = '';+ return true;+ };/** Close the current stage with the operator that follows it; an empty stage is unreadable. */const close = (op) => {- if (!current.trim() || /\\$/u.test(current.trim())) return false;- stages.push({ text: current.trim(), op });+ if (!current.trim() || /\\$/u.test(current.trim()) || !pipe()) return false;+ stages.push({ text: current.trim(), op, pipeline });current = '';+ pipeline = [];return true;};for (let at = 0; at < source.length; at += 1) {const char = source[at];const next = source[at + 1];- if (quote === "'") { current += char; if (char === "'") quote = null; continue; }- if (char === '\\') { current += next === '\n' ? ' ' : char + (next ?? ''); at += 1; continue; }+ if (quote === "'") { add(char); if (char === "'") quote = null; continue; }+ if (char === '\\') { add(next === '\n' ? ' ' : char + (next ?? '')); at += 1; continue; }if (char === '`' || (char === '$' && next === '(')) return null;- if (quote === '"') { current += char; if (char === '"') quote = null; continue; }- if (char === "'" || char === '"') { quote = char; current += char; continue; }- if (char === '$' && next === '{') { variable += 1; current += '${'; at += 1; continue; }- if (char === '}' && variable) { variable -= 1; current += char; continue; }+ if (quote === '"') { add(char); if (char === '"') quote = null; continue; }+ if (char === "'" || char === '"') { quote = char; add(char); continue; }+ if (char === '$' && next === '{') { variable += 1; add('${'); at += 1; continue; }+ if (char === '}' && variable) { variable -= 1; add(char); continue; }if ('(){}#'.includes(char) || (char === '<' && (next === '<' || next === '('))) return null;if (char === '&') {if (next === '&') { if (!close('&&')) return null; at += 1; continue; }- if (next === '>' || source[at - 1] === '>' || source[at - 1] === '<') { current += char; continue; }+ if (next === '>' || source[at - 1] === '>' || source[at - 1] === '<') { add(char); continue; }return null;}if (char === '|' && next === '|') { if (!close('||')) return null; at += 1; continue; }+ if (char === '|') {+ if (source[at - 1] === '>') { add(char); continue; }+ if (next === '&' || variable || !pipe()) return null;+ current += char;+ continue;+ }if (char === ';') { if (next === ';' || !close(';')) return null; continue; }if (char === '\n') { if (current.trim() && !close(';')) return null; continue; }- current += char;+ add(char);}- if (quote || variable || !current.trim() || /\\$/u.test(current.trim())) return null;- stages.push({ text: current.trim(), op: '' });+ if (quote || variable || !current.trim() || /\\$/u.test(current.trim()) || !close('')) return null;return stages;}@@ -199,10 +217,35 @@ export function gateStages(command) {* Wrap each stage so the shell prints its start and its exit status between private marker lines.* Control flow is unchanged: each wrapper exits with its stage's status, and an `exit` inside a* stage still ends the run, which leaves that stage without an end marker and so unaccounted.+ * A pipeline's components each append their own exit status to a private file instead of the+ * output, since a marker written into a pipe would land in the next component's input [B3,R3].+ */+function stagedScript(stages, marker, pipes) {+ const quoted = `'${String(pipes).replaceAll("'", "'\\''")}'`;+ /** One component that records its own status, then exits with it so the pipeline is unchanged. */+ const component = (text, index, at) => `{ {\n${text}\n}; pullboard_pipe_status=$?; `+ + `printf '%s %s %s\\n' ${index} ${at} "$pullboard_pipe_status" >> ${quoted}; (exit "$pullboard_pipe_status"); }`;+ return stages.map((stage, index) => {+ const body = stage.pipeline.length > 1 ? stage.pipeline.map((text, at) => component(text, index, at)).join(' | ') : stage.text;+ return `{ printf '\\n${marker} begin ${index}\\n'; {\n${body}\n}; pullboard_stage_status=$?; `+ + `printf '\\n${marker} end ${index} %s\\n' "$pullboard_stage_status"; (exit "$pullboard_stage_status"); } ${stage.op}`;+ }).join('\n');+}++/**+ * Read each pipeline component's recorded status. A component that recorded none, or more than one,+ * stays null, so a line forged into the private file can only make its stage unaccounted [B3,R3].*/-function stagedScript(stages, marker) {- return stages.map((stage, index) => `{ printf '\\n${marker} begin ${index}\\n'; {\n${stage.text}\n}; pullboard_stage_status=$?; `- + `printf '\\n${marker} end ${index} %s\\n' "$pullboard_stage_status"; (exit "$pullboard_stage_status"); } ${stage.op}`).join('\n');+function readPipes(split, pipes) {+ const records = new Map();+ const text = existsSync(pipes) ? readFileSync(pipes, 'utf8') : '';+ for (const line of text.split('\n')) {+ const found = /^(\d+) (\d+) (\d+)$/u.exec(line);+ if (found) records.set(`${found[1]} ${found[2]}`, [...(records.get(`${found[1]} ${found[2]}`) ?? []), Number(found[3])]);+ }+ return split.map((stage, index) => (stage.pipeline.length > 1+ ? stage.pipeline.map((_, at) => { const seen = records.get(`${index} ${at}`) ?? []; return seen.length === 1 ? seen[0] : null; })+ : null));}/** Cut a staged run's output back to the command's own lines and each stage's lines and status. */@@ -235,7 +278,7 @@ function readStages(output, marker, count) {* @param {string} root* @param {string} command* @param {{ pipefail?: boolean, env?: NodeJS.ProcessEnv, stages?: boolean }} [options] - Gate-only pipefail, additional child environment values and per-stage accounting.- * @returns {{ isGreen: boolean, output: string, seconds: number, sandboxBlocked: string|null, stages?: Array<{ status: number|null, output: string }> | null }}+ * @returns {{ isGreen: boolean, output: string, seconds: number, sandboxBlocked: string|null, stages?: Array<{ status: number|null, output: string, pipeline?: Array<number|null> }> | null }}*/export function runShell(root, command, { pipefail = false, env = {}, stages = false } = {}) {const started = Date.now();@@ -251,16 +294,22 @@ export function runShell(root, command, { pipefail = false, env = {}, stages = f}const split = stages ? gateStages(command) : null;const marker = `PULLBOARD_STAGE_${randomUUID().replaceAll('-', '')}`;+ const pipeDirectory = split?.some((stage) => stage.pipeline.length > 1) ? mkdtempSync(join(tmpdir(), 'pullboard-pipes-')) : null;+ const pipes = pipeDirectory ? join(pipeDirectory, 'status') : null;// Newlines, not spaces, around the command, so a trailing comment in it cannot swallow the `)`.- const result = spawnSync(`${prefix}(\n${split ? stagedScript(split, marker) : command}\n) 2>&1`, options);+ const result = spawnSync(`${prefix}(\n${split ? stagedScript(split, marker, pipes) : command}\n) 2>&1`, options);invalidateGitFacts();+ const pipeStatuses = pipes ? readPipes(split, pipes) : null;+ if (pipeDirectory) rmSync(pipeDirectory, { recursive: true, force: true });const raw = `${result.stdout ?? ''}${result.stderr ?? ''}`;const seconds = Math.round((Date.now() - started) / 1000);const blocked = (output) => (result.status === 0 ? null : sandboxBlockedOperation(root, output));if (!stages) return { isGreen: result.status === 0, output: raw, seconds, sandboxBlocked: blocked(raw) };if (!split) return { isGreen: result.status === 0, output: raw, seconds, stages: null, sandboxBlocked: blocked(raw) };const staged = readStages(raw, marker, split.length);- return { isGreen: result.status === 0, seconds, ...staged, sandboxBlocked: blocked(staged.output) };+ // Each pipeline component's own status rides on its stage, so no failing producer hides behind a later one.+ const withPipes = staged.stages.map((stage, index) => (pipeStatuses?.[index] ? { ...stage, pipeline: pipeStatuses[index] } : stage));+ return { isGreen: result.status === 0, seconds, ...staged, stages: withPipes, sandboxBlocked: blocked(staged.output) };}/** Resolve existing path prefixes so a symlink cannot hide an outside-worktree operation. */diff --git a/src/land.js b/src/land.jsindex 73b4c7f4..eb0124b7 100644--- a/src/land.js+++ b/src/land.js@@ -276,14 +276,17 @@ export function tapFailures(output, root) {/*** The failures that explain a red run, or null when anything else may have failed: the command- * could not be split into stages, a stage did not finish, or a stage exited other than 0 without- * exactly 1 and a complete TAP account of its failures. Waivers only ever subtract these [B3,R3].+ * could not be split into stages, a stage did not finish, a stage exited other than 0 without+ * exactly 1 and a complete TAP account of its failures, or a pipeline in a stage had a component+ * that left no status or a second failing component. Waivers only ever subtract these [B3,R3].*/export function accountedFailures(gate, root) {if (gate.isGreen || !Array.isArray(gate.stages) || !gate.stages.length) return null;const failures = [];for (const stage of gate.stages) {if (!Number.isInteger(stage.status)) return null;+ // In a pipeline every component's own status counts: at most one may fail, and only with exit 1 [B3,R3].+ if (stage.pipeline !== undefined && !pipelineAccounted(stage.pipeline)) return null;if (stage.status === 0) continue;const own = stage.status === 1 ? tapFailures(stage.output ?? '', root) : null;if (!own) return null;@@ -292,6 +295,16 @@ export function accountedFailures(gate, root) {return failures.length ? failures : null;}+/**+ * True when a stage's pipeline statuses leave one place a failure can come from. A producer that+ * fails beside a failing TAP consumer is an independent red no waiver names, so it voids the run.+ */+function pipelineAccounted(statuses) {+ if (!Array.isArray(statuses) || statuses.some((status) => !Number.isInteger(status))) return false;+ const failed = statuses.filter((status) => status !== 0);+ return failed.length === 0 || (failed.length === 1 && failed[0] === 1);+}+/*** Pair every named failure of a red run with the live person waiver covering it, or return null.* One unnamed, unfiled or unwaived failure voids the whole run, so a waiver never hides another red [B3,R3].diff --git a/test/land.test.js b/test/land.test.jsindex 3eee7b81..2c00e95e 100644--- a/test/land.test.js+++ b/test/land.test.js@@ -859,6 +859,35 @@ test("a person's waiver lets a submit and a landing gate through only the waivedassert.equal(passingStage.code, 0, `${passingStage.err}\n${passingStage.out}`);assert.match(passingStage.out, line, 'a passing stage leaves only the waived test to subtract');+ // A pipeline counts each component's own status: a healthy producer leaves only the waived test,+ // and a failing producer piped into the same waived TAP is its own unwaived red, at landing and at submit.+ const consumer = join(box.dir, 'pipeline-consumer.cjs');+ writeFileSync(consumer, `process.stdin.resume(); process.stdin.on('end', () => { const child = require('node:child_process').spawnSync(${JSON.stringify(gateCommand)}, { shell: true, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8' }); process.stdout.write(child.stdout || ''); process.stderr.write(child.stderr || ''); process.exitCode = child.status ?? 125; });\n`);+ setGate(`node ${JSON.stringify(setup)} | node ${JSON.stringify(consumer)}`, 'chore: pipe a setup producer into the tests');+ const gateLog = () => readFileSync(join(box.repo, box.git(box.repo, 'rev-parse', '--git-path', 'pullboard-gate.log')), 'utf8');+ const healthyPipe = box.run(box.repo, 'gate', '--landing');+ assert.equal(healthyPipe.code, 0, `${healthyPipe.err}\n${healthyPipe.out}`);+ assert.match(healthyPipe.out, line, 'a healthy producer leaves only the waived test to subtract');+ writeFileSync(broken, 'on\n');+ const producerRuns = readFileSync(runs, 'utf8').length;+ const failingPipe = box.run(box.repo, 'gate', '--landing', '--json');+ assert.equal(readFileSync(runs, 'utf8').length, producerRuns + 1, 'the failing producer really ran');+ assert.match(gateLog(), /Error: separate setup stage failed/u, 'the producer failed on its own');+ assert.match(gateLog(), /^# fail 1$/mu, 'the waived TAP after it is complete');+ assert.equal(failingPipe.code, 1, 'a failing producer piped into the waived tests refuses the landing gate');+ assert.equal(JSON.parse(failingPipe.out).waived, undefined, 'a refused pipeline names no waived failures');+ const pipeItem = itemWorktree(box, 3);+ assert.equal(commitFile(box, pipeItem.path, pipeItem.file, 'PASS\n', `feat(web): build land fixture ${pipeItem.item} [G1]`).status, 0);+ const failingPipeSubmit = box.run(pipeItem.path, 'submit', String(pipeItem.item), '--json');+ assert.equal(JSON.parse(failingPipeSubmit.out).error?.code, 'GATE_RED', 'a failing producer piped into the waived tests refuses the submission');+ assert.equal(readFileSync(runs, 'utf8').length, producerRuns + 2, 'the submission ran the failing producer');+ assert.ok(!events(box).some((event) => event.event_kind === 'fact' && event.item_id === pipeItem.item), 'a refused pipeline submission records nothing as waived');+ rmSync(broken);+ const healthyPipeSubmit = box.run(pipeItem.path, 'submit', String(pipeItem.item));+ assert.equal(healthyPipeSubmit.code, 0, `${healthyPipeSubmit.err}\n${healthyPipeSubmit.out}`);+ assert.match(healthyPipeSubmit.out, line, 'a healthy producer leaves only the waived test at submit');+ assert.match(healthyPipeSubmit.out, /waived, not green/u);+// A real landing run does not stop early on a waived failure: every other file still runs, then the waiver subtracts it.const laterRan = join(box.dir, 'later-file-ran');const waivedFile = 'test/landing-waived.test.cjs';