diff --git a/packages/runtime/src/git.ts b/packages/runtime/src/git.ts index b8287e7..2366862 100644 --- a/packages/runtime/src/git.ts +++ b/packages/runtime/src/git.ts @@ -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(); 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 { + 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 }); } diff --git a/packages/runtime/test/git-contract.test.ts b/packages/runtime/test/git-contract.test.ts index 51bee89..4de1aac 100644 --- a/packages/runtime/test/git-contract.test.ts +++ b/packages/runtime/test/git-contract.test.ts @@ -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(); diff --git a/tests/local-loop.test.ts b/tests/local-loop.test.ts index 35bae8e..a097435 100644 --- a/tests/local-loop.test.ts +++ b/tests/local-loop.test.ts @@ -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, 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')).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.