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
feat(hooks): lanes, secrets, cited ids and the pre-push gate [L3,C1,C3]
Corey Olson4309069c1037
diff --git a/src/gate.js b/src/gate.jsnew file mode 100644index 00000000..9799b836--- /dev/null+++ b/src/gate.js@@ -0,0 +1,58 @@+/**+ * The gate (V4, C3): the repo's own check command, run before submit and before every push. A green+ * run over a committed tree, with nothing untracked and nothing changed while it ran, leaves that+ * tree's id in the git dir, so the same tree is never checked twice.+ */+import { spawnSync } from 'node:child_process';+import { existsSync, readFileSync, writeFileSync } from 'node:fs';+import { gitPath, headTree, isClean, untracked } from './git.js';+import { Refused } from './refused.js';++const STAMP = 'pullboard-gate-green';++/**+ * The tree a gate run would check, or null when the working copy differs from HEAD or holds+ * untracked files, since a pass over it would vouch for something no commit holds.+ *+ * @param {string} root+ * @returns {string | null}+ */+export function committedTree(root) {+ if (!isClean(root) || untracked(root).length) return null;+ return headTree(root);+}++/**+ * True when the gate already passed on exactly this committed tree.+ *+ * @param {string} root+ * @returns {boolean}+ */+export function isStampedGreen(root) {+ const tree = committedTree(root);+ const file = gitPath(root, STAMP);+ return tree !== null && existsSync(file) && readFileSync(file, 'utf8').trim() === tree;+}++/**+ * Run the configured gate in the repo, unless this exact tree already passed.+ *+ * @param {string} root+ * @param {any} config+ * @param {{ stdio?: 'inherit' | 'pipe' }} [options]+ * @returns {{ isGreen: boolean, isCached: boolean, output: string }}+ */+export function runGate(root, config, { stdio = 'inherit' } = {}) {+ if (!config.gate.trim()) {+ throw new Refused('NO_GATE', 'no gate configured; set "gate" in pullboard.json, e.g. "npm test"');+ }+ if (isStampedGreen(root)) return { isGreen: true, isCached: true, output: '' };+ const before = committedTree(root);+ const result = spawnSync(config.gate, { cwd: root, shell: true, stdio, encoding: 'utf8' });+ const isGreen = result.status === 0;+ const output = stdio === 'pipe' ? `${result.stdout ?? ''}${result.stderr ?? ''}` : '';+ if (isGreen && before !== null && committedTree(root) === before) {+ writeFileSync(gitPath(root, STAMP), `${before}\n`);+ }+ return { isGreen, isCached: false, output };+}diff --git a/src/hooks.js b/src/hooks.jsnew file mode 100644index 00000000..d5275743--- /dev/null+++ b/src/hooks.js@@ -0,0 +1,286 @@+/**+ * The checks git runs (C1–C3, L3, L4): the commit message cites the spec, a lane's worktree stays in+ * its folders, no secret or env file is committed, and a push sends exactly what the gate checked.+ * Each check returns its problems; an empty list lets git go on.+ */+import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { git, tryGit } from './git.js';+import { outOfLane } from './lanes.js';+import { citedIds, idProblems } from './spec.js';++export const HOOKS = ['pre-commit', 'commit-msg', 'pre-push'];+export const HOOKS_DIR = '.githooks';+export const HOOK_MARK = 'Installed by pullboard';++const EMOJI_RE = /\p{Extended_Pictographic}|\p{Regional_Indicator}|⃣|️/u;+const EXEMPT_RE = /^(Merge |Revert "|fixup! |squash! |amend! )/;+const ZERO_SHA = /^0+$/;++/**+ * Secrets by kind, most specific first, so a line is named once by what it most likely is.+ */+const SECRETS = [+ ['Anthropic key', /sk-ant-[A-Za-z0-9_-]{20,}/],+ ['OpenAI key', /sk-(proj-)?[A-Za-z0-9_-]{32,}/],+ ['AWS access key', /AKIA[0-9A-Z]{16}/],+ ['private key', /-----BEGIN [A-Z ]*PRIVATE KEY-----/],+ ['GitHub token', /gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,}/],+ ['Slack token or webhook', /xox[abprs]-[A-Za-z0-9-]{10,}|hooks\.slack\.com\/services\/[A-Za-z0-9/]+/],+ ['Stripe live key', /(sk|rk)_live_[A-Za-z0-9]{20,}/],+ ['Cloudflare token', /cfut_[A-Za-z0-9_-]{20,}/],+ ['npm token', /npm_[A-Za-z0-9]{36}/],+ ['assigned secret', /(api[_-]?key|secret|token|password)['"]?\s*[:=]\s*['"][A-Za-z0-9/+_=-]{20,}['"]/i],+ ['env-style secret', /^\s*(export\s+)?[A-Z][A-Z0-9_]*(SECRET|KEY|TOKEN|PASSWORD)[A-Z0-9_]*\s*=\s*[A-Za-z0-9/+_=-]{20,}\s*$/],+];++/**+ * Problems with a commit message's header: format, length, case, trailing period.+ *+ * @param {string} header+ * @param {any} rules+ * @returns {{ problems: string[], type: string }}+ */+function headerProblems(header, rules) {+ const typeRe = new RegExp(`^(${rules.types.join('|')})(\\([a-z0-9._/-]+\\))?(!)?: (.+)$`);+ const match = typeRe.exec(header);+ if (!match) {+ return {+ problems: [`header is "type(scope): subject [ids]"; types: ${rules.types.join(', ')}`],+ type: '',+ };+ }+ const subject = match[4].replace(/\s*\[[^\]]*\]\s*$/, '');+ const problems = [];+ if (header.length > rules.maxHeader) {+ problems.push(`header is ${header.length} characters; at most ${rules.maxHeader}`);+ }+ if (/^[A-Z]/.test(subject)) problems.push('the subject starts lowercase');+ if (subject.endsWith('.')) problems.push('the subject has no trailing period');+ return { problems, type: match[1] };+}++/**+ * Every rule a commit message breaks (C1, C2). Merges, reverts and fixups git wrote are exempt.+ *+ * @param {string} raw - The message as git hands it, comments included.+ * @param {{ rules: any, spec: any }} context+ * @returns {string[]}+ */+export function commitMsgProblems(raw, { rules, spec }) {+ const lines = raw.split('\n').filter((line) => !line.startsWith('#'));+ while (lines.length && !lines.at(-1)?.trim()) lines.pop();+ const [header = '', second, ...body] = lines;+ if (EXEMPT_RE.test(header)) return [];+ const { problems, type } = headerProblems(header, rules);+ const ids = citedIds(header);+ problems.push(...idProblems(spec, ids).map((problem) => `cited id: ${problem}`));+ if (type && rules.requireIds.includes(type) && !ids.length) {+ problems.push(`${type} commits cite the spec ids they serve: end the header with [ID,...]`);+ }+ if (second !== undefined && second.trim()) problems.push('leave a blank line after the header');+ const all = [header, ...body].join('\n');+ const words = new Set(all.toLowerCase().match(/[a-z]+/g) ?? []);+ for (const word of rules.banned) {+ if (words.has(word.toLowerCase())) problems.push(`filler word: "${word}"`);+ }+ if (rules.noEmoji && EMOJI_RE.test(all)) problems.push('no emojis');+ if (rules.noCoAuthor && /^\s*co-authored-by\s*:/im.test(all)) problems.push('no Co-Authored-By trailer');+ return problems;+}++/**+ * Staged paths: every path the commit touches, and the ones it writes. Deletions and both sides of+ * a rename count as touched, so a lane cannot delete or move a file it does not own (L3).+ *+ * @param {string} root+ * @returns {{ touched: string[], written: string[] }}+ */+export function stagedPaths(root) {+ const fields = git(root, ['diff', '--cached', '--name-status', '-z']).split('\0');+ const touched = [];+ const written = [];+ let index = 0;+ while (index < fields.length - 1) {+ const status = fields[index] ?? '';+ const isMove = /^[RC]/.test(status);+ const paths = fields.slice(index + 1, index + (isMove ? 3 : 2));+ touched.push(...paths);+ if (!status.startsWith('D')) written.push(paths.at(-1) ?? '');+ index += isMove ? 3 : 2;+ }+ return { touched, written };+}++/**+ * The lines a staged diff adds, each with its file and line number.+ *+ * @param {string} diff - A zero-context unified diff.+ * @returns {{ path: string, line: number, text: string }[]}+ */+export function addedLines(diff) {+ const added = [];+ let path = '';+ let line = 0;+ for (const diffLine of diff.split('\n')) {+ if (diffLine.startsWith('+++ b/')) path = diffLine.slice(6);+ else if (diffLine.startsWith('@@ ')) line = Number(/\+(\d+)/.exec(diffLine)?.[1] ?? 0);+ else if (diffLine.startsWith('+') && !diffLine.startsWith('+++')) {+ added.push({ path, line, text: diffLine.slice(1) });+ line += 1;+ }+ }+ return added;+}++/**+ * Added lines that look like secrets, named by kind and place. Values are never echoed back.+ *+ * @param {{ path: string, line: number, text: string }[]} added+ * @returns {string[]}+ */+export function secretsIn(added) {+ return added.flatMap((entry) => {+ const found = SECRETS.find(([, pattern]) => pattern.test(entry.text));+ return found ? [`${found[0]} at ${entry.path}:${entry.line}`] : [];+ });+}++/**+ * True when a file pattern matches a path: patterns with a slash match the whole path, others the+ * file name. `*` matches within one name.+ *+ * @param {string} pattern+ * @param {string} path+ * @returns {boolean}+ */+export function matchesPattern(pattern, path) {+ const target = pattern.includes('/') ? path : (path.split('/').at(-1) ?? '');+ const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^/]*');+ return new RegExp(`^${escaped}$`).test(target);+}++/**+ * Written paths that must never be committed: env files and whatever else the repo blocks.+ *+ * @param {string[]} paths+ * @param {{ blocked: string[], allowed: string[] }} protect+ * @returns {string[]}+ */+export function blockedPaths(paths, protect) {+ return paths.filter(+ (path) =>+ protect.blocked.some((pattern) => matchesPattern(pattern, path)) &&+ !protect.allowed.some((pattern) => matchesPattern(pattern, path)),+ );+}++/**+ * Every rule the staged change breaks (L3, L4): blocked files, secrets, and, in a lane's worktree,+ * changes outside its folders. The main checkout is the coordinator's and may change anything; any+ * other worktree must have joined a lane.+ *+ * @param {{ root: string, isMain: boolean, config: any, agent: any }} context+ * @returns {string[]}+ */+export function preCommitProblems({ root, isMain, config, agent }) {+ const { touched, written } = stagedPaths(root);+ const problems = blockedPaths(written, config.protect).map(+ (path) => `${path} is a blocked file (env or secrets); keep it out of git`,+ );+ if (config.protect.secrets) {+ const diff = git(root, ['diff', '--cached', '--text', '--no-ext-diff', '--no-textconv', '--no-color', '-U0']);+ problems.push(...secretsIn(addedLines(diff)).map((where) => `possible secret: ${where}`));+ }+ if (isMain) return problems;+ if (!agent) {+ problems.push('this worktree has not joined a lane: pullboard join <lane>');+ return problems;+ }+ const foreign = outOfLane(config, agent.agent_lane, touched);+ problems.push(+ ...foreign.map((path) => `outside the ${agent.agent_lane} lane: ${path}; shout its owner instead`),+ );+ return problems;+}++/**+ * Problems with a push (C3): uncommitted changes, or a ref that is not the checked-out commit, so+ * the gate that runs next checks exactly what leaves this machine.+ *+ * @param {string} root+ * @param {string} refsText - What git writes on the hook's stdin: one ref update per line.+ * @returns {string[]}+ */+export function prePushProblems(root, refsText) {+ if (tryGit(root, ['diff', '--quiet', 'HEAD']).status !== 0) {+ return ['commit or stash your changes first; the gate must check what you push'];+ }+ const head = git(root, ['rev-parse', 'HEAD']);+ const problems = [];+ for (const line of refsText.split('\n').filter((entry) => entry.trim())) {+ const [localRef = '', localSha = ''] = line.split(' ');+ if (ZERO_SHA.test(localSha)) continue;+ const commit = git(root, ['rev-parse', `${localSha}^{commit}`]);+ if (commit !== head) problems.push(`${localRef} is not checked out; switch to it and push from there`);+ }+ return problems;+}++/**+ * The shell script for one hook: it runs pullboard from the repo's own install when there is one,+ * else from the PATH.+ *+ * @param {string} hook+ * @returns {string}+ */+export function hookScript(hook) {+ const args = hook === 'commit-msg' ? ' "$1"' : '';+ return [+ '#!/bin/sh',+ `# ${HOOK_MARK}: runs the pullboard ${hook} checks. Edit pullboard.json, not this file.`,+ 'root=$(git rev-parse --show-toplevel)',+ `if [ -x "$root/node_modules/.bin/pullboard" ]; then exec "$root/node_modules/.bin/pullboard" hook ${hook}${args}; fi`,+ `if command -v pullboard >/dev/null 2>&1; then exec pullboard hook ${hook}${args}; fi`,+ 'echo "pullboard is not installed; npm i -D @pullboard/local, or npm i -g @pullboard/local" >&2',+ 'exit 1',+ '',+ ].join('\n');+}++/**+ * Write the hook scripts and point git at them. A hook file pullboard did not write is left alone+ * and reported, so init never clobbers a repo's own hooks (I2).+ *+ * @param {string} root+ * @returns {string[]} What happened, one line per hook.+ */+export function installHooks(root) {+ const dir = join(root, HOOKS_DIR);+ mkdirSync(dir, { recursive: true });+ const notes = [];+ for (const hook of HOOKS) {+ const file = join(dir, hook);+ const existing = existsSync(file) ? readFileSync(file, 'utf8') : null;+ if (existing !== null && !existing.includes(HOOK_MARK)) {+ const isWired = existing.includes(`hook ${hook}`) && existing.includes('pullboard');+ notes.push(+ isWired+ ? `kept your ${HOOKS_DIR}/${hook} (it already runs pullboard)`+ : `kept your ${HOOKS_DIR}/${hook}; add a line to it: pullboard hook ${hook}`,+ );+ continue;+ }+ writeFileSync(file, hookScript(hook));+ chmodSync(file, 0o755);+ notes.push(`wrote ${HOOKS_DIR}/${hook}`);+ }+ const current = tryGit(root, ['config', '--get', 'core.hooksPath']).stdout;+ if (current && current !== HOOKS_DIR) {+ notes.push(`core.hooksPath is ${current}; left as is. Call pullboard hook <name> from those hooks`);+ } else {+ git(root, ['config', 'core.hooksPath', HOOKS_DIR]);+ notes.push(`set core.hooksPath to ${HOOKS_DIR}`);+ }+ return notes;+}