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(check): pipefail counts only when its set always runs [C7,V4]
Corey Olson656081659d10
diff --git a/src/check-mask.js b/src/check-mask.jsindex fbb94b0c..c114fd39 100644--- a/src/check-mask.js+++ b/src/check-mask.js@@ -6,8 +6,9 @@ import { Refused } from './refused.js';/*** Keep only the check's top-level shell code, blanking everything inside quotes and $( ) substitutions while- * keeping each offset. A $( ) inside double quotes opens its own code context, so "$(printf '%s' "$out" | grep x)"- * stays one quoted word; a plain ( ) subshell is top-level code, since it exits with its last stage [C7,V4].+ * keeping each offset; a # comment is blanked too. A $( ) inside double quotes opens its own code context, so+ * "$(printf '%s' "$out" | grep x)" stays one quoted word; a plain ( ) subshell is top-level code, since it exits with+ * its last stage [C7,V4].** @param {string} command* @returns {string}@@ -29,6 +30,12 @@ function topLevel(command) {if (char === '"') stack.pop();else if (char === '$' && command[i + 1] === '(') { stack.push('$('); out += ' '; i++; }out += ' ';+ } else if (char === '#' && (i === 0 || /[\s;&|()]/u.test(command[i - 1]))) {+ // A shell comment runs to the end of its line and is never code.+ const end = command.indexOf('\n', i);+ const stop = end < 0 ? command.length : end;+ out += ' '.repeat(stop - i);+ i = stop - 1;} else if (char === "'" || char === '"') { stack.push(char); out += top ? char : ' '; }else if (char === '$' && command[i + 1] === '(') { stack.push('$('); out += top ? '$(' : ' '; i++; }else if (char === '(' && context === '$(') { stack.push('$('); out += ' '; }@@ -40,46 +47,53 @@ function topLevel(command) {/*** Split a check into its pipelines at ;, &&, ||, &, newlines and ( ) group edges in its top-level code; pipes inside- * quotes or a $( ) substitution are not the check's own [C7,V4].+ * quotes, comments or a $( ) substitution are not the check's own. Each pipeline keeps the separator before it and+ * its ( ) group depth, so a later reader knows whether it always runs [C7,V4].** @param {string} command- * @returns {{ start: number, end: number, pipes: number[] }[]}+ * @returns {{ start: number, end: number, pipes: number[], after: string, depth: number }[]}*/function pipelines(command) {const shell = topLevel(command);const found = [];- let current = { start: 0, pipes: [] };- const cut = (at, next) => { found.push({ ...current, end: at }); current = { start: next, pipes: [] }; };+ let depth = 0;+ let current = { start: 0, pipes: [], after: 'start', depth };+ const cut = (at, next, separator) => { found.push({ ...current, end: at }); current = { start: next, pipes: [], after: separator, depth }; };for (let i = 0; i < shell.length; i++) {const char = shell[i];if (char === '$' && shell[i + 1] === '(') { i = shell.indexOf(')', i); if (i < 0) break; }- else if (char === '(' || char === ')') cut(i, i + 1);- else if (char === '|' && shell[i + 1] === '|') { cut(i, i + 2); i++; }- else if (char === '&' && shell[i + 1] === '&') { cut(i, i + 2); i++; }+ else if (char === '(') { depth++; cut(i, i + 1, '('); }+ else if (char === ')') { depth = Math.max(0, depth - 1); cut(i, i + 1, ')'); }+ else if (char === '|' && shell[i + 1] === '|') { cut(i, i + 2, '||'); i++; }+ else if (char === '&' && shell[i + 1] === '&') { cut(i, i + 2, '&&'); i++; }else if (char === '|') current.pipes.push(i);- else if ((char === '&' && shell[i - 1] !== '>' && shell[i + 1] !== '>') || char === ';' || char === '\n') cut(i, i + 1);+ else if ((char === '&' && shell[i - 1] !== '>' && shell[i + 1] !== '>') || char === ';' || char === '\n') cut(i, i + 1, char);}- cut(shell.length, shell.length);+ cut(shell.length, shell.length, 'end');return found;}/*** Name the final stage of the first pipeline that would hide a check's exit status, or null when none does. A- * pipeline is exempt only when the check sets pipefail as a real command (set -o pipefail or set -euo pipefail), or- * when its first stage prints a value captured earlier with NAME=$(...), the out=$(...) form this board uses; an- * untested capture excuses nothing [C7,V4].+ * pipeline is exempt only when pipefail is on as it runs, or when its first stage prints a value captured earlier+ * with NAME=$(...), the out=$(...) form this board uses; an untested capture excuses nothing. Pipefail is on only+ * after a set -o pipefail (or set -euo pipefail) that always runs: it starts a statement outside any ( ) subshell+ * and follows no && or ||. A later set +o pipefail turns it off again [C7,V4].** @param {string} command* @returns {string | null}*/function maskingStage(command) {- if (/(?:^|[;&(\n])\s*set\s+(?:-\S+\s+)*-\S*o\s+pipefail\b/u.test(topLevel(command))) return null;const captured = new Set();+ let pipefail = false;for (const pipeline of pipelines(command)) {const text = command.slice(pipeline.start, pipeline.end);const assignment = /^\s*(\w+)="?\$\(/u.exec(text);if (assignment) captured.add(assignment[1]);- if (!pipeline.pipes.length) continue;+ if (/^\s*set\s+(?:[-+]\S+\s+)*\+\S*o\s+pipefail\b/u.test(text)) pipefail = false;+ else if (/^\s*set\s+(?:[-+]\S+\s+)*-\S*o\s+pipefail\b/u.test(text) && !pipeline.pipes.length+ && pipeline.depth === 0 && ['start', ';', '\n', ')'].includes(pipeline.after)) pipefail = true;+ if (!pipeline.pipes.length || pipefail) continue;const first = command.slice(pipeline.start, pipeline.pipes[0]);const tested = /^\s*(?:printf|echo)\b/u.test(first)&& [...first.matchAll(/\$\{?(\w+)\}?/gu)].some((match) => captured.has(match[1]));diff --git a/test/check.test.js b/test/check.test.jsindex c671d311..e438377e 100644--- a/test/check.test.js+++ b/test/check.test.js@@ -360,3 +360,17 @@ test("the board's own captured-output form is accepted [C7,V4]", () => {assert.equal(maskRefusal(`out=$(node bin/run-tests.js test/x.test.js 2>&1); printf "%s\\n" "$out"; printf "%s\\n" "$out" | grep -Eq "^# fail 0$" && printf "%s\\n" "$out" | grep -Eq "ok"`), null);assert.equal(maskRefusal(`out=$(node -e "process.exit(1)" | tail -5); echo "$out" | grep -q ok`), null, 'a pipe inside the capture is not a check pipeline');});++test('pipefail counts only when its set always runs and is still on [C7,V4]', () => {+ const masked = `node -e "console.log('proof-red'); process.exit(7)" | tail -n1`;+ // /bin/sh never runs this set, since false fails first, so the pipe still exits 0 (#812 review).+ assert.equal(maskRefusal(`false && set -o pipefail; ${masked}`), 'CHECK_MASKS_EXIT');+ assert.equal(maskRefusal(`set -o pipefail; set +o pipefail; ${masked}`), 'CHECK_MASKS_EXIT', 'a later set +o pipefail turns it off');+ assert.equal(maskRefusal(`(set -o pipefail); ${masked}`), 'CHECK_MASKS_EXIT', 'a subshell keeps its pipefail to itself');+ assert.equal(maskRefusal(`set -euo pipefail\n${masked}`), null, 'a set that always runs keeps the producer exit');+});++test('a pipe inside a shell comment is not a pipeline [C7,V4]', () => {+ assert.equal(maskRefusal('printf proof-green # example only: false | tail'), null);+ assert.equal(maskRefusal('echo a#b | tail -n1'), 'CHECK_MASKS_EXIT', 'a # inside a word starts no comment');+});