refactor: simplify pass (trekexecute)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-10-26 20:39:40 +01:00
commit 52c382c198
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
8 changed files with 45 additions and 52 deletions

View file

@ -68,10 +68,13 @@ const DIFF = [
'',
].join('\n');
/** The CLI run with `--cwd <cwd>` (the repo by default) on `diff` written into the repo's git dir. */
function run(dir, diff = DIFF, cwd = dir) {
/**
* The CLI run with `--cwd <cwd>` (the repo by default) on `diff` written into the repo's git dir.
* With `env`, the child gets that environment; without it, it inherits this process's.
*/
function run(dir, diff = DIFF, cwd = dir, env) {
writeFileSync(join(dir, '.git', 'upward.diff'), diff);
return spawnSync(process.execPath, [CLI, '--diff', join(dir, '.git', 'upward.diff'), '--step', '1', '--attempt', '1', '--cwd', cwd], { encoding: 'utf8' });
return spawnSync(process.execPath, [CLI, '--diff', join(dir, '.git', 'upward.diff'), '--step', '1', '--attempt', '1', '--cwd', cwd], { encoding: 'utf8', ...(env ? { env } : {}) });
}
// Parked outside the work tree, in the git dir, so a later `git ls-files --others` never lists them.
@ -399,8 +402,7 @@ test('[M7] a failing HEAD read exits 2 and moves nothing', async () => {
const ctl = spawnSync('git', ['ls-tree', 'HEAD'], { cwd: dir, encoding: 'utf8', env });
assert.equal(ctl.status, 128, `control: the fake git must fail ls-tree: ${ctl.stderr}`);
assert.equal(spawnSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8', env }).status, 0, 'control: the fake git runs every other call');
writeFileSync(join(dir, '.git', 'upward.diff'), DIFF);
const r = spawnSync(process.execPath, [CLI, '--diff', join(dir, '.git', 'upward.diff'), '--step', '1', '--attempt', '1', '--cwd', dir], { encoding: 'utf8', env });
const r = run(dir, DIFF, dir, env);
assert.equal(r.status, 2, `a HEAD read that failed must be exit 2: ${r.stdout}${r.stderr}`);
assert.match(r.stderr, /ls-tree/, 'the message does not name the git call that failed');
const listed = git(dir, 'ls-files').split('\n');
@ -437,12 +439,6 @@ test('[M7] a HEAD path missing from the index exits 2 and moves nothing', () =>
}
});
/** The CLI run on `diff` written into the repo's git dir, its child given childEnv(). */
function runIsolated(dir, diff = DIFF) {
writeFileSync(join(dir, '.git', 'upward.diff'), diff);
return spawnSync(process.execPath, [CLI, '--diff', join(dir, '.git', 'upward.diff'), '--step', '1', '--attempt', '1', '--cwd', dir], { encoding: 'utf8', env: childEnv() });
}
test('[M7] a corrupt branch ref is a failed HEAD read: exit 2, nothing unindexed or moved', () => {
const dir = repo();
try {
@ -454,7 +450,7 @@ test('[M7] a corrupt branch ref is a failed HEAD read: exit 2, nothing unindexed
const ctl = spawnSync('git', ['rev-parse', '--verify', '--quiet', 'HEAD'], { cwd: dir, encoding: 'utf8', env: childEnv() });
assert.equal(ctl.status, 1, `control: the corrupt ref reads like an unborn HEAD (exit 1): ${ctl.stderr}`);
assert.equal(ctl.stdout.trim(), '', 'control: and prints nothing');
const r = runIsolated(dir);
const r = run(dir, DIFF, dir, childEnv());
assert.equal(r.status, 2, `a corrupt branch ref must be exit 2: ${r.stdout}${r.stderr}`);
const listed = git(dir, 'ls-files').split('\n');
assert.ok(listed.includes('tests/batch.sh'), 'the committed file was taken out of the index although HEAD could not be read');
@ -476,7 +472,7 @@ test('[M7r] an unborn HEAD parks the attempt\'s files', () => {
assert.match(git(dir, 'ls-files'), /tests\/batch\.sh/, 'control: the intent-to-add file is in the index');
const ctl = spawnSync('git', ['rev-parse', '--verify', '--quiet', 'HEAD'], { cwd: dir, encoding: 'utf8', env: childEnv() });
assert.equal(ctl.status, 1, 'control: HEAD is unborn');
const r = runIsolated(dir);
const r = run(dir, DIFF, dir, childEnv());
assert.equal(r.status, 0, `an unborn HEAD must park: ${r.stdout}${r.stderr}`);
assert.deepEqual(JSON.parse(r.stdout).parked, ['tests/batch.sh', 'tests/cli-stdin.test.mjs']);
assert.doesNotMatch(git(dir, 'ls-files'), /tests\/batch\.sh/, 'the intent-to-add entry is still in the index');

View file

@ -70,11 +70,7 @@ function finding(id, rule_key, extra = {}) {
function repoWith(reviews) {
const dir = mkdtempSync(join(tmpdir(), 'recurrence-'));
reviews.forEach((r, i) => {
const p = join(dir, '.claude', 'projects', `2026-10-0${i + 1}-${r.slug}`);
mkdirSync(p, { recursive: true });
writeFileSync(join(p, 'review.md'), reviewText(r));
});
reviewsInto(join(dir, '.claude'), reviews);
return dir;
}

View file

@ -323,10 +323,11 @@ function gateRepo({ sub = '', claude = '# Project\n', claudeLink = false, object
g(repo, 'add', '-A');
g(repo, 'commit', '-q', '-m', msg);
};
const gate = ({ verdict = 'ALLOW', sha = reviewed } = {}) => {
// `env` is added to the child's environment; `repo` is the --repo given (the project dir by default).
const gate = ({ verdict = 'ALLOW', sha = reviewed, env = {}, repo: at = dir } = {}) => {
const p = join(base, 'review.md');
writeFileSync(p, review({ verdict, severities: [verdict === 'BLOCK' ? 'BLOCKER' : 'MINOR'], scopeShaEnd: sha }));
return spawnSync(process.execPath, [GATE, '--json', p, '--repo', dir], { encoding: 'utf-8', env: childEnv() });
return spawnSync(process.execPath, [GATE, '--json', p, '--repo', at], { encoding: 'utf-8', env: childEnv(env) });
};
return {
base, repo, dir, reviewed, put, commit, gate,
@ -715,13 +716,6 @@ function failingGit(sub) {
};
}
/** The gate on an ALLOW review of the fixture's reviewed commit, with `env` and `repo` given. */
function gateWith(fx, { env = {}, repo = fx.dir } = {}) {
const p = join(fx.base, 'review.md');
writeFileSync(p, review({ verdict: 'ALLOW', severities: ['MINOR'], scopeShaEnd: fx.reviewed }));
return spawnSync(process.execPath, [GATE, '--json', p, '--repo', repo], { encoding: 'utf-8', env: childEnv(env) });
}
function assertNotMeasured(r, re) {
assert.equal(r.status, 2, r.stdout + r.stderr);
assert.doesNotMatch(r.stdout, /review-stale/);
@ -734,7 +728,7 @@ test('[M4] a git that cannot run rev-parse is not measured', () => {
const fx = gateRepo();
const git = failingGit('rev-parse');
try {
assertNotMeasured(gateWith(fx, { env: { PATH: git.path } }), /rev-parse/);
assertNotMeasured(fx.gate({ env: { PATH: git.path } }), /rev-parse/);
} finally { git.cleanup(); fx.cleanup(); }
});
@ -744,9 +738,9 @@ test('[M4] a git that fails rev-list after the commit resolves is not measured',
try {
lessonsCommit(fx);
// Control: with the real git the same review opens the gate, so the run below gets past rev-parse.
const control = gateWith(fx);
const control = fx.gate();
assert.equal(control.status, 0, control.stdout + control.stderr);
assertNotMeasured(gateWith(fx, { env: { PATH: git.path } }), /rev-list/);
assertNotMeasured(fx.gate({ env: { PATH: git.path } }), /rev-list/);
} finally { git.cleanup(); fx.cleanup(); }
});
@ -755,16 +749,16 @@ test('[M4] a git that fails diff after the commit resolves is not measured', ()
const git = failingGit('diff');
try {
lessonsCommit(fx);
const control = gateWith(fx);
const control = fx.gate();
assert.equal(control.status, 0, control.stdout + control.stderr);
assertNotMeasured(gateWith(fx, { env: { PATH: git.path } }), /diff/);
assertNotMeasured(fx.gate({ env: { PATH: git.path } }), /diff/);
} finally { git.cleanup(); fx.cleanup(); }
});
test('[M4] --repo with a missing path is not measured', () => {
const fx = gateRepo();
try {
assertNotMeasured(gateWith(fx, { repo: join(fx.base, 'no-such-dir') }), /no-such-dir/);
assertNotMeasured(fx.gate({ repo: join(fx.base, 'no-such-dir') }), /no-such-dir/);
} finally { fx.cleanup(); }
});
@ -774,7 +768,7 @@ test('[M4] --repo that is no repository is not measured', () => {
const plain = join(fx.base, 'plain');
mkdirSync(plain);
// The ceiling stops git from finding a repository above the fixture.
assertNotMeasured(gateWith(fx, { repo: plain, env: { GIT_CEILING_DIRECTORIES: fx.base } }), /not a git repository/);
assertNotMeasured(fx.gate({ repo: plain, env: { GIT_CEILING_DIRECTORIES: fx.base } }), /not a git repository/);
} finally { fx.cleanup(); }
});