From b1bc63a60c03f27fdf8d63202d055803c324bf19 Mon Sep 17 00:00:00 2001 From: Alex Vallone <39172404+amv146@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:49:22 -0800 Subject: [PATCH 1/2] fix(server): review diff previews reuse the cached repository layout Review previews re-ran Git repository discovery on every per-file request. They now read the common dir and worktree root from the existing layout cache like listRefs, and still read the current branch fresh when a request needs it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 63 +++++++++++++++----- apps/server/src/vcs/GitVcsDriverCore.ts | 35 +++++++---- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 6cb91ad2dd042..5d3fb37db1d73 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -766,7 +766,7 @@ it.effect.each(["an existing", "a new"] as const)( ); it.effect.each(["on a branch", "detached", "unborn"] as const)( - "reads only the repository root for review requests that ignore the branch when %s", + "reuses the cached repository layout for review requests when %s", (state) => Effect.scoped( Effect.gen(function* () { @@ -780,15 +780,18 @@ it.effect.each(["on a branch", "detached", "unborn"] as const)( Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), ); const discoveries = () => { - const commands = spawnedArgs.filter( - (args) => - args.includes("--show-toplevel") || - args.includes("--git-common-dir") || - (args.includes("symbolic-ref") && args.includes("HEAD")), - ); + const spawned = (matches: (args: ReadonlyArray) => boolean) => + spawnedArgs.filter(matches).length; + const counts = { + commonDir: spawned((args) => args.includes("--git-common-dir")), + worktreeRoot: spawned((args) => args.includes("--show-toplevel")), + branch: spawned((args) => args.includes("symbolic-ref") && args.includes("HEAD")), + }; spawnedArgs.length = 0; - return commands.length; + return counts; }; + const warm = { commonDir: 0, worktreeRoot: 0, branch: 0 }; + const branchRead = { ...warm, branch: 1 }; const cwd = yield* makeTmpDir(); if (state === "unborn") { yield* git(cwd, ["init", "-b", "main"]); @@ -802,36 +805,66 @@ it.effect.each(["on a branch", "detached", "unborn"] as const)( yield* writeTextFile(cwd, "new.txt", "new\n"); spawnedArgs.length = 0; - const workingTree = yield* driver.getReviewDiffPreview({ + const workingTreeFile = { cwd, file: { path: "new.txt", previousPath: null, sourceKind: "working-tree" }, - }); - assert.equal(discoveries(), 1); + } as const; + const workingTree = yield* driver.getReviewDiffPreview(workingTreeFile); + assert.deepStrictEqual(discoveries(), { commonDir: 1, worktreeRoot: 1, branch: 1 }); assert.deepStrictEqual( workingTree.sources .find((source) => source.kind === "working-tree")! .files!.map((file) => file.path), ["new.txt"], ); + yield* driver.getReviewDiffPreview(workingTreeFile); + assert.deepStrictEqual(discoveries(), warm); if (state !== "unborn") { const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]); - spawnedArgs.length = 0; const commit = yield* driver.getReviewDiffPreview({ cwd, commitSha, file: { path: "committed.txt", previousPath: null, sourceKind: "commit" }, }); - assert.equal(discoveries(), 1); + assert.deepStrictEqual(discoveries(), warm); assert.include(commit.sources[0]!.diff, "+committed"); + + // Creating a worktree drops the cached layout. + const path = yield* Path.Path; + yield* driver.createWorktree({ + cwd, + path: path.join(yield* makeTmpDir("git-worktrees-"), "review"), + refName: commitSha, + newRefName: "feature/review", + }); + discoveries(); + yield* driver.getReviewDiffPreview(workingTreeFile); + assert.deepStrictEqual(discoveries(), { commonDir: 1, worktreeRoot: 1, branch: 1 }); } + const headRef = (preview: Effect.Success>) => + preview.sources.find((entry) => entry.kind === "branch-range")!.headRef; const general = yield* driver.getReviewDiffPreview({ cwd }); - assert.equal(discoveries(), 3); + assert.deepStrictEqual(discoveries(), branchRead); assert.equal( - general.sources.find((source) => source.kind === "branch-range")!.headRef, + headRef(general), state === "detached" ? "HEAD" : yield* git(cwd, ["branch", "--show-current"]), ); + + // A checkout made outside the driver is visible to the next request. + if (state !== "detached") yield* git(cwd, ["checkout", "-b", "outside-checkout"]); + const afterCheckout = yield* driver.getReviewDiffPreview({ cwd }); + assert.deepStrictEqual(discoveries(), branchRead); + if (state !== "detached") assert.equal(headRef(afterCheckout), "outside-checkout"); + + yield* driver.getReviewBranchCommits({ cwd }); + assert.deepStrictEqual(discoveries(), branchRead); + + // A cached layout must not outlive a removed cwd. + yield* (yield* FileSystem.FileSystem).remove(cwd, { recursive: true }); + assert.isEmpty((yield* driver.getReviewDiffPreview(workingTreeFile)).sources); + assert.isEmpty((yield* driver.getReviewDiffPreview({ cwd })).sources); }), ).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 48664332e760c..0d6ef4631327d 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1620,13 +1620,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } satisfies GitRepositoryPaths; }); - const resolveReviewWorktreeRoot = Effect.fn("resolveReviewWorktreeRoot")(function* (cwd: string) { - const worktreeRoot = yield* readWorktreeRoot(cwd); - if (worktreeRoot !== null) return { worktreeRoot, currentBranch: null }; - // Full discovery tells bare and non-repository directories apart from real Git failures. - return yield* resolveRepositoryPathsUncached(cwd); - }); - const repositoryPathsCache = yield* Cache.makeWith( (cwd: string) => resolveRepositoryPathsUncached(cwd), { @@ -1660,6 +1653,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* return Cache.get(refresh ? repositoryPathsRefreshCache : repositoryPathsCache, cacheKey); }; + const resolveReviewRepository = Effect.fn("resolveReviewRepository")(function* ( + cwd: string, + needsCurrentBranch: boolean, + ) { + // A cached layout outlives a removed cwd, which must still read as a missing cwd. + if ( + !needsCurrentBranch && + !(yield* fileSystem.exists(cwd).pipe(Effect.orElseSucceed(() => true))) + ) { + return null; + } + const repository = yield* resolveRepositoryPaths(cwd); + if (repository?.worktreeRoot == null) return null; + // The cached branch outlives a checkout made outside T3 Code, so it is read fresh. + const currentBranch = needsCurrentBranch + ? yield* readCurrentBranch("GitVcsDriver.review.currentBranch", cwd) + : null; + return { worktreeRoot: repository.worktreeRoot, currentBranch }; + }); + const defaultBranchCache = yield* Cache.makeWith( (gitCommonDir: string) => Effect.gen(function* () { @@ -4064,7 +4077,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const getReviewBranchCommits = Effect.fn("getReviewBranchCommits")(function* ( input: ReviewBranchCommitsInput, ) { - const repository = yield* resolveRepositoryPathsUncached(input.cwd); + const repository = yield* resolveReviewRepository(input.cwd, true); if (!repository?.worktreeRoot) return { commits: [], truncated: false }; const cwd = repository.worktreeRoot; const head = yield* executeGit( @@ -4150,11 +4163,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* input.commitSha === undefined && input.file?.sourceKind !== "commit" && sourceKind !== "working-tree"; - const repository = yield* ( - needsCurrentBranch - ? resolveRepositoryPathsUncached(input.cwd) - : resolveReviewWorktreeRoot(input.cwd) - ).pipe( + const repository = yield* resolveReviewRepository(input.cwd, needsCurrentBranch).pipe( Effect.catchTags({ GitCommandError: (error) => isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error), From 1faf8db77d15e2a32cc9a213f0b5b0afc060cbb4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 06:22:19 +0000 Subject: [PATCH 2/2] test(server): drop worktree invalidation case already covered elsewhere Co-Authored-By: Claude Opus 5.5 --- apps/server/src/vcs/GitVcsDriverCore.test.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 5d3fb37db1d73..f4eb10810503e 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -829,18 +829,6 @@ it.effect.each(["on a branch", "detached", "unborn"] as const)( }); assert.deepStrictEqual(discoveries(), warm); assert.include(commit.sources[0]!.diff, "+committed"); - - // Creating a worktree drops the cached layout. - const path = yield* Path.Path; - yield* driver.createWorktree({ - cwd, - path: path.join(yield* makeTmpDir("git-worktrees-"), "review"), - refName: commitSha, - newRefName: "feature/review", - }); - discoveries(); - yield* driver.getReviewDiffPreview(workingTreeFile); - assert.deepStrictEqual(discoveries(), { commonDir: 1, worktreeRoot: 1, branch: 1 }); } const headRef = (preview: Effect.Success>) =>