🐛 Checkpoint a merge candidate again: its worktree is no agent workspace

Since the Git config of an agent workspace is reduced to an allowlist
before each checkpoint, every checkpoint read `<workspace>/.git/config`.
A merge candidate is a worktree of the integration cache, whose `.git`
is a file, so the read failed with ENOTDIR: whenever the target branch
moved while a run was working, its integration stopped on an
infrastructure wait, and continuing failed the same way. No test merged
onto a moved target without a conflict.

A GitWorkspace now remembers the worktrees it added from a repository
WishForge owns and leaves their config alone; no agent can reach it. A
workspace whose `.git` someone else turned into a pointer file is still
refused rather than followed. Tests merge onto a target that moved
meanwhile, end to end and at the Git contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
eust-w 2026-10-26 18:03:43 +08:00
parent 821c059081
commit b5b5c49ec3
3 changed files with 55 additions and 1 deletions

View File

@ -125,6 +125,11 @@ function validateLocalSshTransport(transport: LocalSshTransport): void {
export const CHECKPOINT_BUNDLE_LIMIT = 4 * 1024 * 1024;
export const CHECKPOINT_PATCH_LIMIT = 4 * 1024 * 1024;
export class GitWorkspace {
/**
* Worktrees this instance added from a repository WishForge owns. Their `.git` is a file that
* points into that repository, whose config no agent can reach, so it needs no hardening.
*/
private readonly ownWorktrees = new Set<string>();
constructor(
private readonly execute: CommandExecutor = executeCommand,
private readonly options: GitOptions = {},
@ -213,6 +218,7 @@ export class GitWorkspace {
* paused run's agent can plant again before it resumes.
*/
private async hardenWorkspaceConfig(workspace: string): Promise<void> {
if (this.ownWorktrees.has(path.resolve(workspace))) return;
const file = path.join(workspace, '.git', 'config');
const original = await readFile(file, 'utf8').catch(error => {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
@ -575,6 +581,7 @@ export class GitWorkspace {
if (await this.isAncestor(input.repository, actual, input.sourceCommit))
return { commit: input.sourceCommit, baseCommit: actual, fastForward: true };
await this.git(input.repository, ['worktree', 'add', '--detach', input.path, actual]);
this.ownWorktrees.add(path.resolve(input.path));
const merge = await this.execute(
this.command(input.path, [
'merge',
@ -598,6 +605,7 @@ export class GitWorkspace {
};
}
async removeWorktree(repository: string, worktree: string) {
this.ownWorktrees.delete(path.resolve(worktree));
await this.git(repository, ['worktree', 'remove', '--force', worktree]).catch(() => undefined);
await rm(worktree, { recursive: true, force: true });
}

View File

@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import path from 'node:path';
import os from 'node:os';
@ -267,6 +267,14 @@ describe('exact target branch and immutable checkpoint contracts', () => {
expect(merged.candidate.fastForward).toBe(false);
expect(merged.candidate.commit).not.toBe(second.checkpoint.sourceCommit);
expect(merged.candidate.baseCommit).toBe(first.checkpoint.sourceCommit);
// The merge is checkpointed in its worktree, whose .git is a file pointing into the integration cache.
const mergedCheckpoint = await work.checkpoint(
path.join(root, 'candidate-2'),
merged.candidate.baseCommit,
'second',
'test',
);
expect(mergedCheckpoint).toMatchObject({ sourceCommit: merged.candidate.commit, baseCommit: merged.expected });
// Someone else pushes meanwhile: a push based on the old tip is reported as a moved target.
const other = await change('other', 'c.txt', 'other\n');
await git(other.run.workspace, ['push', 'origin', 'HEAD:refs/heads/main']);
@ -489,6 +497,16 @@ describe('planted Git configuration cannot run commands during trusted export',
});
}
it('refuses a workspace whose .git was turned into a pointer file it did not make', async () => {
const f = await workspaceWith('');
const moved = path.join(f.root, 'moved-git');
await rename(path.join(f.prepared.workspace, '.git'), moved);
await writeFile(path.join(f.prepared.workspace, '.git'), `gitdir: ${moved}\n`);
await expect(f.work.checkpoint(f.prepared.workspace, f.prepared.baseCommit, 'run', 'codex')).rejects.toMatchObject({
code: 'ENOTDIR',
});
});
it('drops a planted url.insteadOf transport redirect but keeps the clone origin', async () => {
const root = await temporary();
const work = new GitWorkspace();

View File

@ -269,6 +269,17 @@ const writeGreeting =
await writeFile(path.join(start.workspace, 'greeting.txt'), `${text}\n`);
return summary;
};
/** A person commits straight to the target branch, as anyone with access to the repository can. */
async function pushToTarget(files: Record<string, string>, message: string) {
const person = path.join(root, 'pusher');
if (existsSync(person)) await git(person, 'pull', '--ff-only', 'origin', 'main');
else await git(root, 'clone', remote, person);
for (const [file, content] of Object.entries(files)) await writeFile(path.join(person, file), content);
await git(person, 'add', '.');
await git(person, 'commit', '-m', message);
await git(person, 'push', 'origin', 'HEAD:main');
return git(person, 'rev-parse', 'HEAD');
}
async function evidenceOf(runId: string) {
return store.transaction(TENANT, async tx => (await tx.list<Evidence>('evidence')).filter(e => e.runId === runId));
}
@ -519,6 +530,23 @@ describe('local main loop', () => {
expect(lessons[0]?.summary).toContain('Merging into the target branch conflicted in greeting.txt');
});
it('merges a change onto a target that moved meanwhile, and checks the merge', async () => {
const project = await createProject();
await pushToTarget({ 'greeting.txt': 'hello world\n' }, 'Greet the world');
const runId = await submit(project.id, 'Add notes', 'wish-moved-0001');
const session = await provision(runId, async start => {
await writeFile(path.join(start.workspace, 'notes.md'), 'Notes\n');
return 'Added the notes.';
});
expect(await session.executor.executeRun(session.input)).toEqual({ status: 'verify' });
const moved = await pushToTarget({ 'changelog.md': 'Changes\n' }, 'Start a changelog');
const checksBefore = checkRuns;
expect(await session.verifier.verifyRun(session.input)).toEqual({ status: 'complete' });
// The change on its own, then its merge with what the branch gained.
expect(checkRuns).toBe(checksBefore + 2);
expect(await git(remote, 'show', 'main:notes.md')).toBe('Notes');
await git(remote, 'merge-base', '--is-ancestor', moved, 'main');
});
it('verifies and merges a branch pushed from the IDE through a wish that starts from it', async () => {
const project = await createProject();
// The Git service's push hook: an IDE credential may update ide/ branches only.