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): isolate child Git environments [C3,C5]
Corey Olson3ad9c8c11788
diff --git a/src/gate.js b/src/gate.jsindex b04725ae..a8cf2ee3 100644--- a/src/gate.js+++ b/src/gate.js@@ -6,7 +6,7 @@*/import { spawnSync } from 'node:child_process';import { existsSync, readFileSync, writeFileSync } from 'node:fs';-import { gitPath, headTree, isClean, untracked } from './git.js';+import { gitChildEnv, gitPath, headTree, isClean, untracked } from './git.js';import { Refused } from './refused.js';const STAMP = 'pullboard-gate-green';@@ -94,7 +94,7 @@ export function isStampedGreen(root) {export function runShell(root, command) {const started = Date.now();// Newlines, not spaces, around the command, so a trailing comment in it cannot swallow the `)`.- const result = spawnSync(`(\n${command}\n) 2>&1`, { cwd: root, shell: true, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', maxBuffer: 2 ** 30 });+ const result = spawnSync(`(\n${command}\n) 2>&1`, { cwd: root, env: gitChildEnv(root), shell: true, stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8', maxBuffer: 2 ** 30 });return { isGreen: result.status === 0, output: `${result.stdout ?? ''}${result.stderr ?? ''}`, seconds: Math.round((Date.now() - started) / 1000) };}diff --git a/src/git.js b/src/git.jsindex 824df135..f1bacd7b 100644--- a/src/git.js+++ b/src/git.js@@ -8,6 +8,20 @@ import { Refused } from './refused.js';const GIT_FLAGS = ['-c', 'core.quotepath=false'];+/**+ * The environment for a gate or fixer: keep the user's settings, but remove Git's repository-local+ * variables so a child that changes folders cannot act on the hook's repository by accident.+ * Pullboard's own Git calls retain those variables, including a hook's staged index.+ *+ * @param {string} root+ * @returns {NodeJS.ProcessEnv}+ */+export function gitChildEnv(root) {+ const env = { ...process.env };+ for (const name of git(root, ['rev-parse', '--local-env-vars']).split('\n')) delete env[name];+ return env;+}+/*** Run git in `cwd` and return its trimmed output, or throw when git fails.*diff --git a/src/hooks.js b/src/hooks.jsindex 30436bc2..3f53e2b3 100644--- a/src/hooks.js+++ b/src/hooks.js@@ -6,7 +6,7 @@import { spawnSync } from 'node:child_process';import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';import { join } from 'node:path';-import { git, tryGit } from './git.js';+import { git, gitChildEnv, tryGit } from './git.js';import { outOfLane } from './lanes.js';import { citedIds, deletedIds, idProblems } from './spec.js';@@ -248,6 +248,7 @@ export function applyFixers(root, fixers) {if (!files.length) continue;const result = spawnSync(`${fixer.run} ${files.map(shellQuote).join(' ')}`, {cwd: root,+ env: gitChildEnv(root),shell: true,encoding: 'utf8',timeout: 120_000,diff --git a/test/e2e.test.js b/test/e2e.test.jsindex 1e16c45f..5e7dc058 100644--- a/test/e2e.test.js+++ b/test/e2e.test.js@@ -483,6 +483,68 @@ test('pre-push runs the gate once per tree and pushes only the checked-out commiassert.match(notHere.stderr, /is not checked out/);});+/**+ * A gate/fixer that runs Git in two other folders. Even a failed ordinary-repo probe ends by+ * initializing the bare probe, so removing isolation visibly corrupts only this private fixture.+ */+function foreignGitScript(box, name, { fixer = false } = {}) {+ const file = join(box.dir, `${name}.cjs`);+ const ordinary = join(box.dir, `${name}-repo`);+ const bare = join(box.dir, `${name}-bare.git`);+ writeFileSync(file, [+ "const fs = require('node:fs'); const cp = require('node:child_process');",+ `const ordinary = ${JSON.stringify(ordinary)}; const bare = ${JSON.stringify(bare)};`,+ "fs.mkdirSync(ordinary); fs.mkdirSync(bare);",+ "let failed = false;",+ "const run = (cwd, args) => { const r = cp.spawnSync('git', args, { cwd, encoding: 'utf8', timeout: 10000 }); if (r.status !== 0) { failed = true; console.error(r.stderr); } };",+ "run(ordinary, ['init', '-q', '-b', 'main']);",+ "fs.writeFileSync(ordinary + '/probe.txt', 'its own repository\\n');",+ "run(ordinary, ['add', 'probe.txt']); run(ordinary, ['commit', '-q', '-m', 'chore: foreign probe']);",+ "run(bare, ['init', '-q', '--bare']);",+ ...(fixer ? ["if (!failed) for (const file of process.argv.slice(2)) fs.writeFileSync(file, fs.readFileSync(file, 'utf8').trimEnd() + '\\n');"] : []),+ "process.exitCode = failed ? 1 : 0;",+ ].join('\n'));+ return { file, ordinary, bare };+}++test('a linked-worktree push isolates the gate from Git hook variables and leaves both repos intact [C3]', () => {+ const box = project();+ const probe = foreignGitScript(box, 'gate');+ writeFileSync(join(box.repo, 'pullboard.json'), JSON.stringify({ ...CONFIG, gate: `node ${JSON.stringify(probe.file)}` }));+ box.git(box.repo, 'commit', '-qam', 'chore: a gate with foreign git commands');+ box.git(box.web, 'merge', '-q', '--ff-only', 'main');+ const remote = join(box.dir, 'remote.git');+ box.git(box.dir, 'init', '-q', '--bare', remote);+ box.git(box.repo, 'remote', 'add', 'origin', remote);+ const head = box.git(box.web, 'rev-parse', 'HEAD');+ const pushed = box.tryGit(box.web, 'push', '-q', 'origin', 'web/one');+ assert.equal(box.git(box.repo, 'config', '--get', 'core.bare'), 'false', 'the real hook repo stays non-bare; without isolation this is true');+ assert.equal(pushed.status, 0, `${pushed.stdout}${pushed.stderr}`);+ assert.equal(box.git(remote, 'rev-parse', 'refs/heads/web/one'), head, 'the checked-out commit reached the private local remote');+ assert.equal(box.git(probe.bare, 'rev-parse', '--is-bare-repository'), 'true');+ assert.equal(box.git(probe.ordinary, 'log', '-1', '--format=%s'), 'chore: foreign probe');+ assert.equal(readFileSync(join(probe.ordinary, 'probe.txt'), 'utf8'), 'its own repository\n');+ assert.equal(box.git(box.web, 'status', '--porcelain'), '');+});++test('a linked-worktree commit isolates fixers while its own staged index still works [C5]', () => {+ const box = project();+ const probe = foreignGitScript(box, 'fixer', { fixer: true });+ writeFileSync(join(box.repo, 'pullboard.json'), JSON.stringify({ ...CONFIG, fix: [{ run: `node ${JSON.stringify(probe.file)}`, files: ['*.js'] }] }));+ box.git(box.repo, 'commit', '-qam', 'chore: a fixer with foreign git commands');+ box.git(box.web, 'merge', '-q', '--ff-only', 'main');+ mkdirSync(join(box.web, 'web'), { recursive: true });+ writeFileSync(join(box.web, 'web', 'probe.js'), 'const value = 1; \n');+ box.git(box.web, 'add', 'web/probe.js');+ const committed = box.tryGit(box.web, 'commit', '-q', '-m', 'feat(web): a formatted probe [G1]');+ assert.equal(box.git(box.repo, 'config', '--get', 'core.bare'), 'false');+ assert.equal(committed.status, 0, committed.stderr);+ assert.equal(box.git(probe.bare, 'rev-parse', '--is-bare-repository'), 'true');+ assert.equal(box.git(probe.ordinary, 'log', '-1', '--format=%s'), 'chore: foreign probe');+ assert.equal(box.git(box.web, 'show', 'HEAD:web/probe.js'), 'const value = 1;', 'the parent hook restaged the formatted file through its own index');+ assert.equal(box.git(box.web, 'status', '--porcelain'), '');+});+/*** Run `pullboard claim` from two worktrees at once, so both race for the same lock.*/