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(hooks): recognize env calls and skip heredocs [L3]
Corey Olson25f51eb32718
diff --git a/src/hooks.js b/src/hooks.jsindex 1253ff9e..b6a6ff9f 100644--- a/src/hooks.js+++ b/src/hooks.js@@ -283,14 +283,57 @@ function activeHooksDir(root) {return resolve(root, gitPath(root, 'hooks'));}-/** Split hook shell commands without treating quoted examples or comments as invocations. */+/** Read a here-document delimiter without treating its quoted spelling as executable code. */+function hookHeredoc(text, start) {+ let end = start + 2;+ const stripTabs = text[end] === '-';+ if (stripTabs) end++;+ while (text[end] === ' ' || text[end] === '\t') end++;+ const wordStart = end;+ let delimiter = '';+ let quote = '';+ while (end < text.length) {+ const char = text[end];+ if (!quote && /[\s;&|()<>]/u.test(char)) break;+ if (char === '\\' && quote !== "'") {+ if (end + 1 >= text.length || text[end + 1] === '\n') return null;+ delimiter += text[++end]; end++; continue;+ }+ if (quote) {+ if (char === quote) quote = '';+ else delimiter += char;+ } else if (char === "'" || char === '"') quote = char;+ else delimiter += char;+ end++;+ }+ return end > wordStart && !quote ? { end, delimiter, stripTabs } : null;+}++/** Skip here-document bodies in shell order; printed examples cannot establish hook wiring. */+function skipHookHeredocs(text, start, documents) {+ let cursor = start;+ for (const { delimiter, stripTabs } of documents) {+ while (cursor < text.length) {+ const newline = text.indexOf('\n', cursor);+ const line = text.slice(cursor, newline < 0 ? text.length : newline);+ cursor = newline < 0 ? text.length : newline + 1;+ if ((stripTabs ? line.replace(/^\t*/u, '') : line) === delimiter) break;+ }+ }+ return cursor;+}++/** Split hook shell commands without treating quoted examples, comments or heredocs as invocations. */function hookCommands(source) {+ const text = source.replace(/\\\r?\n/gu, '');const commands = [];+ const documents = [];let command = '';let quote = '';let escaped = false;let comment = false;- for (const char of source.replace(/\\\r?\n/gu, '')) {+ for (let index = 0; index < text.length; index++) {+ const char = text[index];if (comment) {if (char !== '\n') continue;comment = false;@@ -300,7 +343,20 @@ function hookCommands(source) {if (quote) { command += char; if (char === quote) quote = ''; continue; }if (char === '"' || char === "'") { quote = char; command += char; continue; }if (char === '#' && (!command || /\s$/u.test(command))) { comment = true; continue; }- if (/[\n;&|()]/u.test(char)) { commands.push(command.trim()); command = ''; continue; }+ if (char === '<' && text[index + 1] === '<' && text[index - 1] !== '<' && text[index + 2] !== '<') {+ const here = hookHeredoc(text, index);+ if (here) {+ documents.push(here); command += text.slice(index, here.end); index = here.end - 1; continue;+ }+ }+ if (/[\n;&|()]/u.test(char)) {+ commands.push(command.trim()); command = '';+ if (char === '\n' && documents.length) {+ index = skipHookHeredocs(text, index + 1, documents) - 1;+ documents.length = 0;+ }+ continue;+ }command += char;}commands.push(command.trim());@@ -313,7 +369,7 @@ function delegatesHook(source, hook) {const program = String.raw`(?:"(?:[^"]*/)?pullboard(?:\.js)?"|'(?:[^']*/)?pullboard(?:\.js)?'|(?:[^\s"';&|()]+/)?pullboard(?:\.js)?)`;const binBinding = new RegExp(`^(?:bin=|for\\s+bin\\s+in\\s+)${program}(?=\\s|$)`);const boundBin = commands.some(command => binBinding.test(command));- const call = new RegExp(`^(?:(?:then|do|else|exec|command|npx|node)\\s+)*(?:${program}${boundBin ? '|"?\\$bin"?' : ''})\\s+hook\\s+${hook}(?=\\s|$)`);+ const call = new RegExp(`^(?:(?:then|do|else|exec|command|npx|node|env)\\s+)*(?:${program}${boundBin ? '|"?\\$bin"?' : ''})\\s+hook\\s+${hook}(?=\\s|$)`);return commands.some(command => call.test(command));}diff --git a/test/doctor.test.js b/test/doctor.test.jsindex e7d0e879..099f6dea 100644--- a/test/doctor.test.js+++ b/test/doctor.test.js@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';import { execFileSync, spawnSync } from 'node:child_process';import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join, resolve } from 'node:path';+import { delimiter, join, resolve } from 'node:path';import { after, test } from 'node:test';import { DatabaseSync } from 'node:sqlite';import { SCHEMA_VERSION } from '../src/board.js';@@ -120,6 +120,84 @@ test('doctor names an unwired hook and resume gives the repair line [L3]', () =>assert.doesNotMatch(box.run('resume').stdout, /HOOK_UNWIRED|active Git hook/u);});++/** Quote one private fixture path as a shell word without expanding user text. */+function hookWord(value) { return "'" + value.replaceAll("'", "'\\''") + "'"; }++/** Install a real CLI wrapper whose marker proves whether Git actually invokes Pullboard. */+function executedHookBox() {+ const box = boardBox();+ const bin = join(box.dir, 'private-bin');+ const hooks = join(box.root, '.husky');+ const marker = join(box.dir, 'executed-hook');+ mkdirSync(bin); mkdirSync(hooks);+ writeFileSync(join(bin, 'pullboard'), '#!/bin/sh\nprintf \'%s\\n\' "$*" >> "$PULLBOARD_TEST_HOOK_MARKER"\nexec '+ + hookWord(process.execPath) + ' ' + hookWord(BIN) + ' "$@"\n');+ chmodSync(join(bin, 'pullboard'), 0o755);+ box.env.PATH = bin + delimiter + box.env.PATH;+ box.env.PULLBOARD_TEST_HOOK_MARKER = marker;+ box.git('config', 'core.hooksPath', '.husky');+ writeFileSync(join(hooks, 'pre-merge-commit'), '#!/bin/sh\npullboard hook pre-merge-commit "$@"\n');+ chmodSync(join(hooks, 'pre-merge-commit'), 0o755);+ return { ...box, hook: join(hooks, 'pre-commit'), marker };+}++test('doctor and resume accept env delegation that real Git executes [L3]', () => {+ const box = executedHookBox();+ for (const source of ['#!/bin/sh\nenv pullboard hook pre-commit "$@"\n',+ '#!/bin/sh\nexec env pullboard hook pre-commit "$@"\n']) {+ writeFileSync(box.hook, source); chmodSync(box.hook, 0o755);+ writeFileSync(box.marker, '');+ box.git('commit', '--allow-empty', '-q', '-m', 'chore: actual hook control');+ assert.match(readFileSync(box.marker, 'utf8'), /^hook pre-commit\n$/u,+ 'the real Git pre-commit hook invoked the actual submitted CLI once');+ writeFileSync(box.marker, '');+ const doctor = box.run('doctor');+ assert.equal(doctor.status, 0, 'an executed env delegation is wired');+ assert.equal(doctor.stdout, 'board is clean\n');+ const resume = box.run('resume');+ assert.equal(resume.status, 0);+ assert.doesNotMatch(resume.stdout, /active Git hook|HOOK_UNWIRED/u);+ assert.deepEqual(JSON.parse(box.run('doctor', '--json').stdout).problems, []);+ assert.deepEqual(JSON.parse(box.run('resume', '--json').stdout).hookProblems, []);+ assert.equal(readFileSync(box.marker, 'utf8'), '', 'diagnosis never executes the hook');+ assert.equal(readFileSync(box.hook, 'utf8'), source, 'diagnosis never edits the hook');+ }+});++test('doctor and resume reject printed heredocs that real Git never executes [L3]', () => {+ const box = executedHookBox();+ const message = 'active Git hook .husky/pre-commit does not call pullboard hook pre-commit';+ const delegation = 'pullboard hook pre-commit "$@"';+ const finding = { code: 'HOOK_UNWIRED', message, next: 'add this line: ' + delegation };+ for (const source of [+ '#!/bin/sh\ncat <<\'DOCUMENTATION\'\npullboard hook pre-commit "$@"\nDOCUMENTATION\n',+ '#!/bin/sh\ncat <<DOCUMENTATION\npullboard hook pre-commit "$@"\nDOCUMENTATION\n',+ '#!/bin/sh\ncat <<-\'DOCUMENTATION\'\n\tpullboard hook pre-commit "$@"\n\tDOCUMENTATION\n',+ '#!/bin/sh\ncat <<FIRST <<"SECOND"\nexample\nFIRST\npullboard hook pre-commit "$@"\nSECOND\n',+ ]) {+ writeFileSync(box.hook, source); chmodSync(box.hook, 0o755);+ writeFileSync(box.marker, '');+ box.git('commit', '--allow-empty', '-q', '-m', 'chore: documentation control');+ assert.equal(readFileSync(box.marker, 'utf8'), '', 'Git only printed documentation; it never called the CLI');+ const doctor = box.run('doctor');+ assert.equal(doctor.status, 1, 'printed heredoc text cannot wire the active hook');+ assert.ok(doctor.stdout.includes(message + '; repair: ' + finding.next));+ const resume = box.run('resume');+ assert.equal(resume.status, 0);+ assert.ok(resume.stdout.includes(message + '; ' + finding.next));+ assert.deepEqual(JSON.parse(box.run('doctor', '--json').stdout).problems, [finding]);+ assert.deepEqual(JSON.parse(box.run('resume', '--json').stdout).hookProblems, [finding]);+ assert.equal(readFileSync(box.marker, 'utf8'), '', 'diagnosis never executes the printed example');+ assert.equal(readFileSync(box.hook, 'utf8'), source, 'diagnosis leaves documentation untouched');+ writeFileSync(box.hook, source + delegation + '\n');+ box.git('commit', '--allow-empty', '-q', '-m', 'chore: exact repair control');+ assert.match(readFileSync(box.marker, 'utf8'), /^hook pre-commit\n$/u, 'the exact repair line really invokes the CLI');+ assert.equal(box.run('doctor').stdout, 'board is clean\n');+ assert.doesNotMatch(box.run('resume').stdout, /active Git hook|HOOK_UNWIRED/u);+ }+});+test('text HELD refusal ends with the same next step as JSON [L3]', () => {const box = boardBox({ lanes: { web: { owns: ['web/'] } } });const added = box.run('add', 'web', 'Held refusal control', '--criterion', 'Do not edit a claimed item', '--json');