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(cli): diagnose unwired hooks and refusal next steps [L3]

Corey Olson7d691cd8a9f1
diff --git a/src/cli.js b/src/cli.js
index e9c51ddd..82ae6a52 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -25,6 +25,7 @@ import {
preCommitProblems,
prePushProblems,
pushesTrunk,
+ unwiredHookProblems,
} from './hooks.js';
import { initRepo } from './init.js';
import { lifecycleHelp, lifecycleMarkdown } from './machine.js';
@@ -53,7 +54,7 @@ import { runItems } from './run.js';
import { parseProblems, sweepItems } from './sweep.js';
import { renderSpecView } from './view.js';
import { tour } from './tour.js';
-import { commandOutput } from './json.js';
+import { commandOutput, refusalDocument } from './json.js';
import { forgetProject, registerProjectAndLink } from './projects.js';
import { milestoneRoadmap } from './roadmap.js';
import { listResources } from './resources.js';
@@ -1108,6 +1109,7 @@ function resumeHere(io) {
};
});
card.relayProblems = unlinkedRelayProjects();
+ card.hookProblems = unwiredHookProblems(root);
const { me } = card;
card.stale = staleFrozenItems(card.all, loadSpec(root, ctx.config).rows, ctx.doctrine.rows).map((item) => ({ ...item, ...staleItemFinding(item) }));
if (isMain) {
@@ -1118,6 +1120,7 @@ function resumeHere(io) {
}
const say = (line) => io.say(line);
for (const problem of card.relayProblems) say(problem.message);
+ for (const problem of card.hookProblems) say(`${problem.message}; ${problem.next}`);
const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
say(`resume: ${me.id}${me.family ? ` (${me.family})` : ''}, ${me.lane} lane${isMain ? ', the main checkout' : ''}, at ${root}`);
const dirty = dirtyFiles(root).length;
@@ -1999,7 +2002,9 @@ async function sweepHere(ctx, board, me, values) {
}
if (skipped.length) ctx.io.say(`already open: ${skipped.join(', ')}`);
if (blind.length) {
- ctx.io.err(`pullboard: [CHECK_CANNOT_FAIL] the check passes on ${blind.join(', ')} although the checker flags problems there, so it could never prove a fix; not filed. A check ending in a pipe takes its exit status from the last command`);
+ const refusal = new Refused('CHECK_CANNOT_FAIL', `the check passes on ${blind.join(', ')} although the checker flags problems there, so it could never prove a fix; not filed. A check ending in a pipe takes its exit status from the last command. Fix the check so flagged files make it fail, then run pullboard sweep again.`);
+ ctx.io.refusal?.(refusal);
+ ctx.io.err(`pullboard: ${refusal.message}`);
}
const filed = items.length - blind.length;
ctx.io.say(`${problems.length} problems in ${new Set(problems.map((problem) => problem.file)).size} files; ${values['dry-run'] ? 'would file' : 'filed'} ${filed}`);
@@ -2643,8 +2648,9 @@ async function runCommand(argv, io) {
try {
parsed = parseCommandArgs(argv);
} catch (error) {
- io.refusal?.(new Refused('USAGE', `${error.message}; run pullboard help`));
- io.err(`pullboard: ${error.message}\nRun pullboard help --all for the full list.`);
+ const refusal = new Refused('USAGE', `${error.message}; run pullboard help`);
+ io.refusal?.(refusal);
+ io.err(`pullboard: ${error.message}\nRun pullboard help --all for the full list.\nnext: ${refusalDocument(refusal).error.next}`);
return 2;
}
const { values, positionals } = parsed;
@@ -2761,7 +2767,7 @@ async function runCommand(argv, io) {
} catch (error) {
if (error instanceof Refused) {
io.refusal?.(error);
- io.err(`pullboard: ${error.message}`);
+ io.err(`pullboard: ${error.message}; next: ${refusalDocument(error).error.next}`);
return 1;
}
throw error;
@@ -2813,7 +2819,7 @@ async function runMain(argv, streams) {
} catch (error) {
if (!(error instanceof Refused)) throw error;
io.refusal(error);
- io.err(`pullboard: ${error.message}`);
+ io.err(`pullboard: ${error.message}; next: ${refusalDocument(error).error.next}`);
io.flush(1);
return 1;
}
@@ -2827,7 +2833,7 @@ async function runMain(argv, streams) {
}
catch (error) {
if (!(error instanceof Refused)) throw error;
- if (!['NOT_A_REPO', 'NO_REPO', 'NO_CONFIG', 'CORE_BARE'].includes(error.code)) io.err(`pullboard: ${error.message}`);
+ if (!['NOT_A_REPO', 'NO_REPO', 'NO_CONFIG', 'CORE_BARE'].includes(error.code)) io.err(`pullboard: ${error.message}; next: ${refusalDocument(error).error.next}`);
}
};
if (needsRepo) rememberTrunk(io);
@@ -2837,7 +2843,7 @@ async function runMain(argv, streams) {
try { await executePersonRequests(io.cwd, io, main); }
catch (error) {
if (!(error instanceof Refused)) throw error;
- if (!['NOT_A_REPO', 'NO_REPO', 'NO_CONFIG', 'CORE_BARE'].includes(error.code)) io.err(`pullboard: ${error.message}`);
+ if (!['NOT_A_REPO', 'NO_REPO', 'NO_CONFIG', 'CORE_BARE'].includes(error.code)) io.err(`pullboard: ${error.message}; next: ${refusalDocument(error).error.next}`);
}
}
const code = await runCommand(argv, io);
diff --git a/src/doctor.js b/src/doctor.js
index 26cd0250..4554ca00 100644
--- a/src/doctor.js
+++ b/src/doctor.js
@@ -11,7 +11,7 @@ import { readEventLogVersion, SCHEMA_VERSION } from './board.js';
import { staleFrozenItems, staleItemFinding } from './approved-rows.js';
import { loadSpec } from './spec.js';
import { loadDoctrine } from './doctrine.js';
-import { preMergeHookProblems } from './hooks.js';
+import { preMergeHookProblems, unwiredHookProblems } from './hooks.js';
const blankCharacters = new Set(BLANKS.map((point) => String.fromCodePoint(point)));
/** Offer a working tracked or untracked rename without rewriting the repo's doctrine [D1,D2]. */
@@ -46,6 +46,7 @@ export function doctorProblems(file, root, tryGit, config) {
const layout = layoutProblems(db);
return [
...preMergeHookProblems(root),
+ ...unwiredHookProblems(root),
...triggerProblems(db),
...layout.problems,
...(layout.itemFields ? itemProblems(db) : []),
diff --git a/src/hooks.js b/src/hooks.js
index ff464a06..1253ff9e 100644
--- a/src/hooks.js
+++ b/src/hooks.js
@@ -5,7 +5,7 @@
*/
import { spawnSync } from 'node:child_process';
import { chmodSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
-import { join, resolve } from 'node:path';
+import { join, relative, resolve } from 'node:path';
import { git, gitChildEnv, gitPath, gitConfig, invalidateGitFacts, mainCheckout, refuseGrafts, tryGit } from './git.js';
import { CONFIG_FILE, configFromSource, DOCTRINE_FILE, LEGACY_DOCTRINE_FILE } from './config.js';
import { Refused } from './refused.js';
@@ -274,6 +274,73 @@ export function preMergeHookProblems(root) {
return existsSync(file) ? [] : [{ code: 'HOOK_MISSING', message: `merge hook ${file} is missing`, next: 'run pullboard hooks to install pre-merge-commit, then commit the hook files' }];
}
+/** Resolve Git's effective hook directory, letting Git apply core.hooksPath itself.
+ *
+ * @param {string} root
+ * @returns {string}
+ */
+function activeHooksDir(root) {
+ return resolve(root, gitPath(root, 'hooks'));
+}
+
+/** Split hook shell commands without treating quoted examples or comments as invocations. */
+function hookCommands(source) {
+ const commands = [];
+ let command = '';
+ let quote = '';
+ let escaped = false;
+ let comment = false;
+ for (const char of source.replace(/\\\r?\n/gu, '')) {
+ if (comment) {
+ if (char !== '\n') continue;
+ comment = false;
+ }
+ if (escaped) { command += char; escaped = false; continue; }
+ if (char === '\\' && quote !== "'") { command += char; escaped = true; continue; }
+ 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; }
+ command += char;
+ }
+ commands.push(command.trim());
+ return commands.filter(Boolean);
+}
+
+/** Recognize direct, quoted-path and generated-variable calls without executing a hook. */
+function delegatesHook(source, hook) {
+ const commands = hookCommands(source);
+ 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|$)`);
+ return commands.some(command => call.test(command));
+}
+
+/** Report active Git hooks that do not delegate to Pullboard, without changing hook files [L3].
+ *
+ * @param {string} root
+ * @returns {{ code: string, message: string, next: string }[]}
+ */
+export function unwiredHookProblems(root) {
+ const dir = activeHooksDir(root);
+ return HOOKS.flatMap((hook) => {
+ const file = join(dir, hook);
+ let source;
+ try {
+ if ((statSync(file).mode & 0o111) === 0) return [];
+ source = readFileSync(file, 'utf8');
+ } catch { return []; }
+ if (delegatesHook(source, hook)) return [];
+ const path = relative(root, file);
+ return [{
+ code: 'HOOK_UNWIRED',
+ message: `active Git hook ${path} does not call pullboard hook ${hook}`,
+ next: `add this line: pullboard hook ${hook} "$@"`,
+ }];
+ });
+}
+
/**
* The commit being merged, only when it is already part of the primary checkout's history.
*
diff --git a/test/doctor.test.js b/test/doctor.test.js
index 13500d9a..e7d0e879 100644
--- a/test/doctor.test.js
+++ b/test/doctor.test.js
@@ -19,7 +19,7 @@ after(() => {
/**
* Create a committed repo with a board and isolated git identity for one integrity scenario.
*/
-function boardBox({ initialize = true, repoName = 'repo' } = {}) {
+function boardBox({ initialize = true, repoName = 'repo', lanes = {} } = {}) {
const dir = mkdtempSync(join(tmpdir(), 'pullboard-doctor-'));
sandboxes.push(dir);
const root = join(dir, repoName);
@@ -42,11 +42,11 @@ function boardBox({ initialize = true, repoName = 'repo' } = {}) {
mkdirSync(join(root, '.git', 'hooks'), { recursive: true });
writeFileSync(mergeHook, hookScript('pre-merge-commit'));
chmodSync(mergeHook, 0o755);
- writeFileSync(join(root, 'pullboard.json'), JSON.stringify({ gate: 'true', lanes: {} }, null, 2));
+ writeFileSync(join(root, 'pullboard.json'), JSON.stringify({ gate: 'true', lanes }, null, 2));
git('add', '-A');
git('commit', '-q', '-m', 'chore: setup');
const dbFile = join(root, '.git', 'pullboard', 'board.sqlite');
- if (initialize) assert.equal(run('status').status, 0);
+ if (initialize) { const result = run('status'); assert.equal(result.status, 0, result.stderr || result.stdout); }
else mkdirSync(join(root, '.git', 'pullboard'), { recursive: true });
return { dir, root, env, git, run, dbFile };
}
@@ -86,6 +86,70 @@ test('doctor reports a clean board in one line and leaves it unchanged [A6]', ()
assert.deepEqual(readFileSync(box.dbFile), before);
});
+test('doctor names an unwired hook and resume gives the repair line [L3]', () => {
+ const box = boardBox();
+ const hooks = join(box.root, '.husky');
+ mkdirSync(hooks);
+ box.git('config', 'core.hooksPath', '.husky');
+ writeFileSync(join(hooks, 'pre-commit'), '#!/bin/sh\nnpx lint-staged\n');
+ writeFileSync(join(hooks, 'pre-merge-commit'), '#!/bin/sh\npullboard hook pre-merge-commit "$@"\n');
+ chmodSync(join(hooks, 'pre-commit'), 0o755);
+ chmodSync(join(hooks, 'pre-merge-commit'), 0o755);
+ const expected = 'active Git hook .husky/pre-commit does not call pullboard hook pre-commit';
+
+ const doctor = box.run('doctor');
+ assert.equal(doctor.status, 1);
+ assert.equal(doctor.stdout.split('\n').filter((line) => line.includes(expected)).length, 1, doctor.stdout);
+ assert.ok(doctor.stdout.includes(`${expected}; repair: add this line: pullboard hook pre-commit "$@"`), doctor.stdout);
+ const resume = box.run('resume');
+ assert.equal(resume.status, 0, resume.stderr);
+ assert.equal(resume.stdout.split('\n').filter((line) => line.includes(expected)).length, 1, resume.stdout);
+ assert.ok(resume.stdout.includes(`${expected}; add this line: pullboard hook pre-commit "$@"`), resume.stdout);
+ const doctorJson = box.run('doctor', '--json');
+ assert.equal(doctorJson.status, 1);
+ const finding = { code: 'HOOK_UNWIRED', message: expected, next: 'add this line: pullboard hook pre-commit "$@"' };
+ assert.deepEqual(JSON.parse(doctorJson.stdout).problems, [finding]);
+ const resumeJson = box.run('resume', '--json');
+ assert.equal(resumeJson.status, 0, resumeJson.stderr);
+ assert.deepEqual(JSON.parse(resumeJson.stdout).hookProblems, [finding]);
+
+ writeFileSync(join(hooks, 'pre-commit'), '#!/bin/sh\npullboard hook pre-commit "$@"\n');
+ chmodSync(join(hooks, 'pre-commit'), 0o755);
+ box.git('config', 'core.hooksPath', hooks);
+ assert.doesNotMatch(box.run('doctor').stdout, /HOOK_UNWIRED|active Git hook/u);
+ assert.doesNotMatch(box.run('resume').stdout, /HOOK_UNWIRED|active Git hook/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');
+ assert.equal(added.status, 0, added.stderr || added.stdout);
+ const id = JSON.parse(added.stdout).item.item_id;
+ const created = box.run('worktree', 'web', '--json');
+ assert.equal(created.status, 0, created.stderr || created.stdout);
+ const agent = JSON.parse(created.stdout);
+ const claim = spawnSync(process.execPath, [BIN, 'claim', String(id), '--json'], { cwd: agent.path, env: box.env, encoding: 'utf8' });
+ assert.equal(claim.status, 0, claim.stderr || claim.stdout);
+ const text = box.run('edit', String(id), '--route', 'mid');
+ const json = box.run('edit', String(id), '--route', 'mid', '--json');
+ assert.equal(text.status, 1);
+ assert.equal(json.status, 1);
+ const refusal = JSON.parse(json.stdout).error;
+ assert.equal(refusal.code, 'HELD');
+ assert.ok(text.stderr.trimEnd().endsWith(`next: ${refusal.next}`), text.stderr);
+});
+
+test('text USAGE refusal ends with the same next step as JSON [L3]', () => {
+ const box = boardBox();
+ const text = box.run('status', '--not-a-real-flag');
+ const json = box.run('status', '--not-a-real-flag', '--json');
+ assert.equal(text.status, 2);
+ assert.equal(json.status, 2);
+ const refusal = JSON.parse(json.stdout).error;
+ assert.equal(refusal.code, 'USAGE');
+ assert.ok(text.stderr.trimEnd().endsWith(`next: ${refusal.next}`), text.stderr);
+});
+
test('commands and doctor explain how to repair core.bare without crossing a nested repo [A6, P4]', () => {
const box = boardBox({ repoName: "O'Brien project" });
const root = box.git('rev-parse', '--show-toplevel');
diff --git a/test/e2e-runner.test.js b/test/e2e-runner.test.js
index 5a9abee8..03ef4d93 100644
--- a/test/e2e-runner.test.js
+++ b/test/e2e-runner.test.js
@@ -191,6 +191,11 @@ test('sweep files one light item per flagged file, in its lane; a second sweep s
assert.equal(blind.code, 1);
assert.match(blind.err, /CHECK_CANNOT_FAIL\] the check passes on docs\/c.js although the checker flags problems there/);
assert.match(blind.out, /filed 0/);
+ const machineBlind = box.run(box.repo, 'sweep', '--run', `node ${join(box.dir, 'novar.mjs')} .`, '--check', `node ${join(box.dir, 'novar.mjs')} {file} | tail -5`, '--json');
+ assert.equal(machineBlind.code, 1);
+ const next = JSON.parse(machineBlind.out).error.next;
+ assert.equal(next, 'Fix the check so flagged files make it fail, then run pullboard sweep again.');
+ assert.ok(blind.err.trimEnd().endsWith(next), blind.err);
});
test('pre-commit runs the fixers on fully staged files and restages them; partly staged files are left alone [C5]', () => {
diff --git a/test/hooks.test.js b/test/hooks.test.js
index d8b24cb8..937fe22a 100644
--- a/test/hooks.test.js
+++ b/test/hooks.test.js
@@ -3,12 +3,12 @@
*/
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
-import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test } from 'node:test';
import { defaults } from '../src/config.js';
-import { addedLines, blockedPaths, commitCitationWarnings, commitMsgProblems, matchesPattern, preCommitProblems, secretsIn } from '../src/hooks.js';
+import { addedLines, blockedPaths, commitCitationWarnings, commitMsgProblems, hookScript, matchesPattern, preCommitProblems, secretsIn, unwiredHookProblems } from '../src/hooks.js';
import { parseSpec } from '../src/spec.js';
import { Refused } from '../src/refused.js';
@@ -36,6 +36,44 @@ function stagedProblems(root) {
*/
const check = (message, rules = RULES) => commitMsgProblems(message, { rules, spec: SPEC });
+test('hook wiring checks the configured path and accepts pullboard delegation [L3]', (t) => {
+ const box = stagedRepo(t);
+ const hooks = join(box.root, '.husky');
+ mkdirSync(hooks);
+ box.git('config', 'core.hooksPath', '.husky');
+ writeFileSync(join(hooks, 'pre-commit'), '#!/bin/sh\nnpx lint-staged\n');
+ chmodSync(join(hooks, 'pre-commit'), 0o755);
+ assert.deepEqual(unwiredHookProblems(box.root), [{
+ code: 'HOOK_UNWIRED',
+ message: 'active Git hook .husky/pre-commit does not call pullboard hook pre-commit',
+ next: 'add this line: pullboard hook pre-commit "$@"',
+ }]);
+ writeFileSync(join(hooks, 'pre-commit'), '#!/bin/sh\n# pullboard hook pre-commit "$@"\nnot-pullboard hook pre-commit\n');
+ chmodSync(join(hooks, 'pre-commit'), 0o755);
+ assert.equal(unwiredHookProblems(box.root).length, 1);
+ writeFileSync(join(hooks, 'pre-commit'), '#!/bin/sh\npullboard hook pre-commit "$@"\n');
+ chmodSync(join(hooks, 'pre-commit'), 0o755);
+ assert.deepEqual(unwiredHookProblems(box.root), []);
+ for (const source of [
+ `#!/bin/sh\necho 'pullboard hook pre-commit'\n`,
+ `#!/bin/sh\necho 'example; pullboard hook pre-commit'\n`,
+ '#!/bin/sh\nbin=some-other-tool; "$bin" hook pre-commit\n',
+ ]) {
+ writeFileSync(join(hooks, 'pre-commit'), source);
+ assert.equal(unwiredHookProblems(box.root).length, 1, source);
+ }
+ for (const source of [
+ '#!/bin/sh\nexec "$root/node_modules/.bin/pullboard" hook pre-commit\n',
+ '#!/bin/sh\nnode "./bin/pullboard.js" hook pre-commit\n',
+ ]) {
+ writeFileSync(join(hooks, 'pre-commit'), source);
+ assert.deepEqual(unwiredHookProblems(box.root), [], source);
+ }
+ writeFileSync(join(hooks, 'pre-commit'), hookScript('pre-commit'));
+ chmodSync(join(hooks, 'pre-commit'), 0o755);
+ assert.deepEqual(unwiredHookProblems(box.root), []);
+});
+
test('a clean header citing a live id passes [C1, C2]', () => {
assert.deepEqual(check('feat(web): add the page [G1]'), []);
assert.deepEqual(check('docs: explain lanes'), []);