@devries-cameron20 / t3code Mirror · read-only / commits / 7c67d196c5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

fix(server): review diff previews reuse the cached repository layout (#38451)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Vallone committed Nov 17, 2026 at 23:17 UTC 7c67d196c57f55b0897f450bfb693fc36e1a426e
2 files changed +58 -28
apps/server/src/vcs/GitVcsDriverCore.test.ts
+36 -15
@@ -766,7 +766,7 @@ it.effect.each(["an existing", "a new"] as const)(
766 );
767
768 it.effect.each(["on a branch", "detached", "unborn"] as const)(
769 - "reads only the repository root for review requests that ignore the branch when %s",
769 + "reuses the cached repository layout for review requests when %s",
770 (state) =>
771 Effect.scoped(
772 Effect.gen(function* () {
@@ -780,15 +780,18 @@ it.effect.each(["on a branch", "detached", "unborn"] as const)(
780 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
781 );
782 const discoveries = () => {
783 - const commands = spawnedArgs.filter(
784 - (args) =>
785 - args.includes("--show-toplevel") ||
786 - args.includes("--git-common-dir") ||
787 - (args.includes("symbolic-ref") && args.includes("HEAD")),
788 - );
783 + const spawned = (matches: (args: ReadonlyArray<string>) => boolean) =>
784 + spawnedArgs.filter(matches).length;
785 + const counts = {
786 + commonDir: spawned((args) => args.includes("--git-common-dir")),
787 + worktreeRoot: spawned((args) => args.includes("--show-toplevel")),
788 + branch: spawned((args) => args.includes("symbolic-ref") && args.includes("HEAD")),
789 + };
790 spawnedArgs.length = 0;
790 - return commands.length;
791 + return counts;
792 };
793 + const warm = { commonDir: 0, worktreeRoot: 0, branch: 0 };
794 + const branchRead = { ...warm, branch: 1 };
795 const cwd = yield* makeTmpDir();
796 if (state === "unborn") {
797 yield* git(cwd, ["init", "-b", "main"]);
@@ -802,36 +805,54 @@ it.effect.each(["on a branch", "detached", "unborn"] as const)(
805 yield* writeTextFile(cwd, "new.txt", "new\n");
806 spawnedArgs.length = 0;
807
805 - const workingTree = yield* driver.getReviewDiffPreview({
808 + const workingTreeFile = {
809 cwd,
810 file: { path: "new.txt", previousPath: null, sourceKind: "working-tree" },
808 - });
809 - assert.equal(discoveries(), 1);
811 + } as const;
812 + const workingTree = yield* driver.getReviewDiffPreview(workingTreeFile);
813 + assert.deepStrictEqual(discoveries(), { commonDir: 1, worktreeRoot: 1, branch: 1 });
814 assert.deepStrictEqual(
815 workingTree.sources
816 .find((source) => source.kind === "working-tree")!
817 .files!.map((file) => file.path),
818 ["new.txt"],
819 );
820 + yield* driver.getReviewDiffPreview(workingTreeFile);
821 + assert.deepStrictEqual(discoveries(), warm);
822
823 if (state !== "unborn") {
824 const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]);
819 - spawnedArgs.length = 0;
825 const commit = yield* driver.getReviewDiffPreview({
826 cwd,
827 commitSha,
828 file: { path: "committed.txt", previousPath: null, sourceKind: "commit" },
829 });
825 - assert.equal(discoveries(), 1);
830 + assert.deepStrictEqual(discoveries(), warm);
831 assert.include(commit.sources[0]!.diff, "+committed");
832 }
833
834 + const headRef = (preview: Effect.Success<ReturnType<typeof driver.getReviewDiffPreview>>) =>
835 + preview.sources.find((entry) => entry.kind === "branch-range")!.headRef;
836 const general = yield* driver.getReviewDiffPreview({ cwd });
830 - assert.equal(discoveries(), 3);
837 + assert.deepStrictEqual(discoveries(), branchRead);
838 assert.equal(
832 - general.sources.find((source) => source.kind === "branch-range")!.headRef,
839 + headRef(general),
840 state === "detached" ? "HEAD" : yield* git(cwd, ["branch", "--show-current"]),
841 );
842 +
843 + // A checkout made outside the driver is visible to the next request.
844 + if (state !== "detached") yield* git(cwd, ["checkout", "-b", "outside-checkout"]);
845 + const afterCheckout = yield* driver.getReviewDiffPreview({ cwd });
846 + assert.deepStrictEqual(discoveries(), branchRead);
847 + if (state !== "detached") assert.equal(headRef(afterCheckout), "outside-checkout");
848 +
849 + yield* driver.getReviewBranchCommits({ cwd });
850 + assert.deepStrictEqual(discoveries(), branchRead);
851 +
852 + // A cached layout must not outlive a removed cwd.
853 + yield* (yield* FileSystem.FileSystem).remove(cwd, { recursive: true });
854 + assert.isEmpty((yield* driver.getReviewDiffPreview(workingTreeFile)).sources);
855 + assert.isEmpty((yield* driver.getReviewDiffPreview({ cwd })).sources);
856 }),
857 ).pipe(Effect.provide(layerTest)),
858 );
apps/server/src/vcs/GitVcsDriverCore.ts
+22 -13
@@ -1620,13 +1620,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1620 } satisfies GitRepositoryPaths;
1621 });
1622
1623 - const resolveReviewWorktreeRoot = Effect.fn("resolveReviewWorktreeRoot")(function* (cwd: string) {
1624 - const worktreeRoot = yield* readWorktreeRoot(cwd);
1625 - if (worktreeRoot !== null) return { worktreeRoot, currentBranch: null };
1626 - // Full discovery tells bare and non-repository directories apart from real Git failures.
1627 - return yield* resolveRepositoryPathsUncached(cwd);
1628 - });
1629 -
1623 const repositoryPathsCache = yield* Cache.makeWith(
1624 (cwd: string) => resolveRepositoryPathsUncached(cwd),
1625 {
@@ -1660,6 +1653,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1653 return Cache.get(refresh ? repositoryPathsRefreshCache : repositoryPathsCache, cacheKey);
1654 };
1655
1656 + const resolveReviewRepository = Effect.fn("resolveReviewRepository")(function* (
1657 + cwd: string,
1658 + needsCurrentBranch: boolean,
1659 + ) {
1660 + // A cached layout outlives a removed cwd, which must still read as a missing cwd.
1661 + if (
1662 + !needsCurrentBranch &&
1663 + !(yield* fileSystem.exists(cwd).pipe(Effect.orElseSucceed(() => true)))
1664 + ) {
1665 + return null;
1666 + }
1667 + const repository = yield* resolveRepositoryPaths(cwd);
1668 + if (repository?.worktreeRoot == null) return null;
1669 + // The cached branch outlives a checkout made outside T3 Code, so it is read fresh.
1670 + const currentBranch = needsCurrentBranch
1671 + ? yield* readCurrentBranch("GitVcsDriver.review.currentBranch", cwd)
1672 + : null;
1673 + return { worktreeRoot: repository.worktreeRoot, currentBranch };
1674 + });
1675 +
1676 const defaultBranchCache = yield* Cache.makeWith(
1677 (gitCommonDir: string) =>
1678 Effect.gen(function* () {
@@ -4064,7 +4077,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4077 const getReviewBranchCommits = Effect.fn("getReviewBranchCommits")(function* (
4078 input: ReviewBranchCommitsInput,
4079 ) {
4067 - const repository = yield* resolveRepositoryPathsUncached(input.cwd);
4080 + const repository = yield* resolveReviewRepository(input.cwd, true);
4081 if (!repository?.worktreeRoot) return { commits: [], truncated: false };
4082 const cwd = repository.worktreeRoot;
4083 const head = yield* executeGit(
@@ -4150,11 +4163,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4163 input.commitSha === undefined &&
4164 input.file?.sourceKind !== "commit" &&
4165 sourceKind !== "working-tree";
4153 - const repository = yield* (
4154 - needsCurrentBranch
4155 - ? resolveRepositoryPathsUncached(input.cwd)
4156 - : resolveReviewWorktreeRoot(input.cwd)
4157 - ).pipe(
4166 + const repository = yield* resolveReviewRepository(input.cwd, needsCurrentBranch).pipe(
4167 Effect.catchTags({
4168 GitCommandError: (error) =>
4169 isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),