Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/cli/src/commands/review/agent-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ interface PlanReport {
prNumber?: unknown;
ownerRepo?: unknown;
worktreePath?: unknown;
/** The PR head sha fetch-pr recorded — the probe's identity anchor. */
fetchedSha?: unknown;
mergeBaseSha?: unknown;
host?: unknown;
repositoryContext?: unknown;
Expand Down Expand Up @@ -1322,7 +1324,15 @@ function repositoryContextBlock(context: RepositoryContext): string[] {
function worktreeResidueOf(report: PlanReport): WorktreeResidue {
const wt = report.worktreePath;
if (typeof wt !== 'string' || !wt) return { paths: [], total: 0 };
return worktreeResidue(resolve(wt));
// Hand over the sha fetch-pr recorded: it is the one identity element a
// forge planted at the worktree cannot reproduce, and with it the probe
// refuses a forged admin entry (see worktreeResidue).
const sha = report.fetchedSha;
return worktreeResidue(
resolve(wt),
12,
typeof sha === 'string' && /^[0-9a-f]{40}$/i.test(sha) ? sha : undefined,
);
}

/**
Expand Down
275 changes: 275 additions & 0 deletions packages/cli/src/commands/review/lib/worktree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { execFileSync } from 'node:child_process';
import {
appendFileSync,
chmodSync,
copyFileSync,
existsSync,
lstatSync,
mkdirSync,
Expand Down Expand Up @@ -185,6 +186,280 @@ describe('worktreeResidue', () => {
expect(worktreeResidue(tree).unmeasured).toBeUndefined();
});

it('says UNMEASURED for a gitfile swapped at a repo that answers for this path', () => {
// The identity gate reads `--show-toplevel`, which prints the directory the
// `.git` FILE sits in — whatever that file points at. A repository whose
// `core.worktree` names this tree answers with this path, so the gate saw
// itself while every command after it measured the plant's index, which
// already holds the contamination as committed content. Measured: through
// discovery the swap reports a clean tree with the mutant on disk.
writeFileSync(join(tree, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(tree, '__probe__.test.ts'), 'probe');
// Genuine first, so the fixture is known to be measurable at all.
expect(worktreeResidue(tree).paths.sort()).toEqual([
'__probe__.test.ts',
'a.ts',
]);

const forge = join(repo, 'forge');
mkdirSync(forge);
const fgit = (...args: string[]) =>
execFileSync(
'git',
[
'-c',
'user.email=t@t.t',
'-c',
'user.name=t',
'-c',
'commit.gpgsign=false',
...args,
],
{ cwd: forge, encoding: 'utf8' },
);
fgit('init', '-q', '-b', 'main', '--template=', '.');
writeFileSync(join(forge, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(forge, '__probe__.test.ts'), 'probe');
fgit('add', '-A');
fgit(
'commit',
'-qm',
'the mutant, as if it were the commit',
'--no-verify',
);
fgit('config', 'core.worktree', tree);
writeFileSync(join(tree, '.git'), `gitdir: ${join(forge, '.git')}\n`);

const got = worktreeResidue(tree);

expect(got.paths).toEqual([]);
// The shape with NO admin entry gets its own reason: there is no
// `gitdir` file here to "not point back", and the triager hunting one
// is the confusion the distinct message exists to spare.
expect(got.unmeasured).toContain('no admin entry');
});

it('says UNMEASURED for a forged admin entry when the caller pins the expected head', () => {
// The round trip proves only that the admin entry the gitfile names SAYS
// this tree is its worktree — and a same-user planter writes both halves
// of the pair: a repo carrying the contamination as committed content,
// and an admin entry whose `gitdir` file is hand-written to name this
// tree (four small writes). The gate then passes end-to-end and the pin
// measures the forge's index. Measured: without the caller's anchor the
// swap below answers clean with the mutant on disk. The anchor is the
// one thing the forge cannot reproduce — committing the contamination
// moves its HEAD off the fetched sha.
writeFileSync(join(tree, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(tree, '__probe__.test.ts'), 'probe');
const expected = git('rev-parse', 'HEAD');
// A genuine tree with the right sha still measures: the anchor must not
// become a refusal of its own.
expect(worktreeResidue(tree, 12, expected).paths.sort()).toEqual([
'__probe__.test.ts',
'a.ts',
]);

const forge = join(repo, 'forge');
mkdirSync(forge);
const fgit = (...args: string[]) =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred to the next round under the ~8-finding batch bound: this round implemented the ten findings that share the identity-gate fix cluster (they cannot be shipped coherently apart). The restructure already removed two of the four fgit copies — the gitfile-swap and forged-admin fixtures no longer build an external forge, they reuse the real repository — leaving two copies (the two redirect tests), which makes the extraction smaller and safer to land on its own.

中文说明

按每轮约 8 条发现的批次上限延后到下一轮:本轮实现了共享同一身份门修复簇的 10 条发现(它们无法被拆开独立交付)。本轮的重构已消除了四份 fgit 拷贝中的两份——gitfile 掉包与伪造 admin 条目夹具不再搭建外部伪造仓库,而是直接复用真实仓库——仅剩两份拷贝(两个重定向测试),抽取因此更小、也更安全,适合单独提交。

execFileSync(
'git',
[
'-c',
'user.email=t@t.t',
'-c',
'user.name=t',
'-c',
'commit.gpgsign=false',
...args,
],
{ cwd: forge, encoding: 'utf8' },
);
fgit('init', '-q', '-b', 'main', '--template=', '.');
writeFileSync(join(forge, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(forge, '__probe__.test.ts'), 'probe');
fgit('add', '-A');
fgit(
'commit',
'-qm',
'the mutant, as if it were the commit',
'--no-verify',
);
const admin = join(forge, '.git', 'worktrees', 'evil');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred to the next round (batch bound), same as the round-2 thread. Progress this round: two of the four fgit copies are gone — the gitfile-swap fixture now points the gitfile at the enclosing repository's own git dir, and the forged-admin fixture hand-writes its entry inside the real repository's .git/worktrees/, so neither builds an external forge any more. The remaining two copies live in the redirect tests; the helper extraction is smaller now and lands next round.

中文说明

与第 2 轮线程相同,按批次上限延后到下一轮。本轮已有进展:四份 fgit 拷贝中已消除两份——gitfile 掉包夹具现在把 gitfile 指向外层仓库自身的 git 目录,伪造 admin 条目夹具直接在真实仓库的 .git/worktrees/ 内手写条目,两者都不再搭建外部伪造仓库。剩余两份拷贝位于重定向测试中;抽取现在更小,将在下一轮提交。

mkdirSync(admin, { recursive: true });
writeFileSync(join(admin, 'gitdir'), `${join(tree, '.git')}\n`);
writeFileSync(join(admin, 'commondir'), '../..\n');
copyFileSync(join(forge, '.git', 'HEAD'), join(admin, 'HEAD'));
copyFileSync(join(forge, '.git', 'index'), join(admin, 'index'));
writeFileSync(join(tree, '.git'), `gitdir: ${admin}\n`);

const got = worktreeResidue(tree, 12, expected);

expect(got.paths).toEqual([]);
expect(got.unmeasured).toContain('not the fetched PR head');
});

it('says UNMEASURED for a gitfile borrowing a SIBLING worktree’s admin entry', () => {
// The mismatch arm of the round trip: a real admin entry — a sibling's —
// whose `gitdir` file names the sibling's `.git`, not this tree's.
// `--show-toplevel` prints the directory the gitfile sits in, so the
// self-equality passes while the round trip catches the borrow. The arm
// needs its own witness: negating the comparison ships green without this
// test — measured, the gate then passes and certifies a tree measured
// against the sibling's index.
const sibling = join(repo, '.qwen', 'tmp', 'sibling-wt');
gitRepo('worktree', 'add', '--detach', '-q', sibling, 'HEAD');
const admin = readFileSync(join(sibling, '.git'), 'utf8')
.trim()
.replace(/^gitdir:\s*/, '');
writeFileSync(join(tree, '.git'), `gitdir: ${admin}\n`);

const got = worktreeResidue(tree);

expect(got.paths).toEqual([]);
expect(got.unmeasured).toContain('does not point back');
});

it('accepts a backpointer spelled through a link that resolves at this tree', () => {
// The round trip's LEFT side is attacker-written, so its normalisation is
// load-bearing: a `gitdir` file spelled through a link that RESOLVES at
// this tree's `.git` does point back at this tree, and refusing it would
// fail closed on a shape that names the right tree. Measured: removing
// the realpathSync from the comparison flips this probe from clean to
// unmeasured — the witness that a spelling and a resolution are being
// compared, not two spellings.
const alias = join(repo, 'alias');
symlinkSync(tree, alias);
const admin = readFileSync(join(tree, '.git'), 'utf8')
.trim()
.replace(/^gitdir:\s*/, '');
writeFileSync(join(admin, 'gitdir'), `${join(alias, '.git')}\n`);

writeFileSync(join(tree, '__probe__.test.ts'), 'probe');
const got = worktreeResidue(tree);

expect(got.unmeasured).toBeUndefined();
expect(got.paths).toEqual(['__probe__.test.ts']);
});

it('says UNMEASURED when an ancestor of the tree is a symlink into forge territory', () => {
// A link planted at any ancestor below the checkout — here `.qwen/tmp`,
// the directory the pipeline itself names — redirects the chdir into
// territory holding a completely genuine `git init` + `worktree add`
// pair with the contamination COMMITTED: no forged admin entry, the
// round trip is real git state, and every check resolves THROUGH the
// link and agrees with itself. Measured: the redirect certified the
// mutant clean before the walk.
writeFileSync(join(tree, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(tree, '__probe__.test.ts'), 'probe');
expect(worktreeResidue(tree).paths.sort()).toEqual([
'__probe__.test.ts',
'a.ts',
]);

const outside = mkdtempSync(join(tmpdir(), 'qwen-redirect-'));
try {
const forgeRepo = join(outside, 'forge');
mkdirSync(forgeRepo);
const fgit = (...args: string[]) =>
execFileSync(
'git',
[
'-c',
'user.email=t@t.t',
'-c',
'user.name=t',
'-c',
'commit.gpgsign=false',
...args,
],
{ cwd: forgeRepo, encoding: 'utf8' },
);
fgit('init', '-q', '-b', 'main', '--template=', '.');
writeFileSync(join(forgeRepo, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(forgeRepo, '__probe__.test.ts'), 'probe');
fgit('add', '-A');
fgit('commit', '-qm', 'the mutant, committed', '--no-verify');
fgit(
'worktree',
'add',
'--detach',
'-q',
join(outside, 'review-wt'),
'HEAD',
);

// The attack: the ancestor becomes a link into that territory.
rmSync(dirname(tree), { recursive: true, force: true });
symlinkSync(outside, dirname(tree));

const got = worktreeResidue(tree);

expect(got.paths).toEqual([]);
expect(got.unmeasured).toContain('resolves through a symlink');
} finally {
rmSync(outside, { recursive: true, force: true });
}
});

it('says UNMEASURED when the tree path itself is a symlink into forge territory', () => {
// The same concealment one hop closer: the LEAF replaced by a link.
// `spawnSync` chdirs through it, `--show-toplevel` answers the physical
// forge path, and both sides of the self-equality resolve through the
// same link, while the pin would freeze the forge's identity. Measured:
// the redirect certified the mutant clean before the leaf check.
writeFileSync(join(tree, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(tree, '__probe__.test.ts'), 'probe');
expect(worktreeResidue(tree).paths.sort()).toEqual([
'__probe__.test.ts',
'a.ts',
]);

const outside = mkdtempSync(join(tmpdir(), 'qwen-redirect-'));
try {
const forgeRepo = join(outside, 'forge');
mkdirSync(forgeRepo);
const fgit = (...args: string[]) =>
execFileSync(
'git',
[
'-c',
'user.email=t@t.t',
'-c',
'user.name=t',
'-c',
'commit.gpgsign=false',
...args,
],
{ cwd: forgeRepo, encoding: 'utf8' },
);
fgit('init', '-q', '-b', 'main', '--template=', '.');
writeFileSync(join(forgeRepo, 'a.ts'), 'export const x = 2; // MUTANT\n');
writeFileSync(join(forgeRepo, '__probe__.test.ts'), 'probe');
fgit('add', '-A');
fgit('commit', '-qm', 'the mutant, committed', '--no-verify');
fgit(
'worktree',
'add',
'--detach',
'-q',
join(outside, 'leaf-wt'),
'HEAD',
);

// The attack: the leaf becomes a link into that territory.
rmSync(tree, { recursive: true, force: true });
symlinkSync(join(outside, 'leaf-wt'), tree);

const got = worktreeResidue(tree);

expect(got.paths).toEqual([]);
expect(got.unmeasured).toContain('resolves through a symlink');
} finally {
rmSync(outside, { recursive: true, force: true });
}
});

it('says UNMEASURED, not clean, when a repository is planted at the path', () => {
// The concealment: `rm .git && git init && git add -A && git commit` over
// the contamination answers a clean `git status` for a dirty tree, and no
Expand Down
Loading
Loading