The best way to control Claude Code, Codex and more from any computer or phone
fix(web): picking the previous branch after an outside checkout switches to it (#36347)
Julius Marminge committed
Nov 13, 2026 at 12:24 UTC
b009582cc97f8af6888f0f821e29b8ffff4e9772
5 files changed
+71
-29
apps/server/src/vcs/GitVcsDriverCore.test.ts
+20
@@ -722,6 +722,26 @@ it.effect("reads a branch whose name starts with a parenthesis as attached", ()
722
).pipe(Effect.provide(layerTest)),
723
);
724
725
+it.effect("follows a checkout made outside the driver after refs were cached", () =>
726
+ Effect.scoped(
727
+ Effect.gen(function* () {
728
+ const cwd = yield* makeTmpDir();
729
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
730
+ yield* git(cwd, ["branch", "feature/outside"]);
731
+ const driver = yield* GitVcsDriver.GitVcsDriver;
732
+ yield* driver.listRefs({ cwd });
733
+
734
+ yield* git(cwd, ["switch", "feature/outside"]);
735
+
736
+ const refs = (yield* driver.listRefs({ cwd })).refs;
737
+ const previous = refs.find((ref) => ref.name === initialBranch);
738
+ assert.isTrue(refs.find((ref) => ref.name === "feature/outside")?.current);
739
+ assert.isFalse(previous?.current);
740
+ assert.isNull(previous?.worktreePath ?? null);
741
+ }),
742
+ ).pipe(Effect.provide(layerTest)),
743
+);
744
+
745
it.effect.each(["on a branch", "detached", "unborn"] as const)(
746
"reads only the repository root for review requests that ignore the branch when %s",
747
(state) =>
apps/server/src/vcs/GitVcsDriverCore.ts
+36
-27
@@ -1495,23 +1495,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1495
: null;
1496
});
1497
1498
+ const readCurrentBranch = (operation: string, cwd: string) =>
1499
+ executeGit(operation, cwd, ["symbolic-ref", "--quiet", "HEAD"], {
1500
+ timeoutMs: 5_000,
1501
+ allowNonZeroExit: true,
1502
+ }).pipe(
1503
+ Effect.map((result) => (result.exitCode === 0 ? branchNameFromHeadRef(result.stdout) : null)),
1504
+ );
1505
+
1506
const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* (
1507
cwd: string,
1508
) {
1509
const gitCommonDir = yield* readGitCommonDir(cwd);
1510
if (gitCommonDir === null) return null;
1503
- const [worktreeRoot, currentBranchResult] = yield* Effect.all(
1511
+ const [worktreeRoot, currentBranch] = yield* Effect.all(
1512
[
1513
readWorktreeRoot(cwd),
1506
- executeGit(
1507
- "GitVcsDriver.resolveRepositoryPaths.currentBranch",
1508
- cwd,
1509
- ["symbolic-ref", "--quiet", "HEAD"],
1510
- {
1511
- timeoutMs: 5_000,
1512
- allowNonZeroExit: true,
1513
- },
1514
- ),
1514
+ readCurrentBranch("GitVcsDriver.resolveRepositoryPaths.currentBranch", cwd),
1515
],
1516
{ concurrency: 2 },
1517
);
@@ -1523,8 +1523,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1523
Effect.map((physicalCwd) => path.relative(worktreeRoot, physicalCwd) === ""),
1524
Effect.orElseSucceed(() => undefined),
1525
);
1526
- const currentBranch =
1527
- currentBranchResult.exitCode === 0 ? branchNameFromHeadRef(currentBranchResult.stdout) : null;
1526
1527
return {
1528
gitCommonDir,
@@ -4453,22 +4451,33 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
4451
};
4452
}
4453
4456
- const snapshot = yield* resolveListRefsSnapshot(
4457
- repositoryPaths.gitCommonDir,
4458
- input.refresh === true,
4454
+ // Both caches outlive a checkout made outside T3 Code, so this checkout's branch is read
4455
+ // on every call and replaces whatever the cached snapshot claims for it.
4456
+ const [snapshot, currentBranch] = yield* Effect.all(
4457
+ [
4458
+ resolveListRefsSnapshot(repositoryPaths.gitCommonDir, input.refresh === true),
4459
+ readCurrentBranch("GitVcsDriver.listRefs.currentBranch", input.cwd).pipe(
4460
+ Effect.orElseSucceed(() => repositoryPaths.currentBranch),
4461
+ ),
4462
+ ],
4463
+ { concurrency: 2 },
4464
);
4460
- const hasCurrentWorktreeBranch =
4461
- repositoryPaths.worktreeRoot !== null &&
4462
- snapshot.localBranches.some((ref) => ref.worktreePath === repositoryPaths.worktreeRoot);
4463
- const localBranches = snapshot.localBranches.map((ref) => ({
4464
- ...ref,
4465
- worktreePath:
4466
- ref.worktreePath ??
4467
- (ref.name === repositoryPaths.currentBranch ? repositoryPaths.worktreeRoot : null),
4468
- current: hasCurrentWorktreeBranch
4469
- ? ref.worktreePath === repositoryPaths.worktreeRoot
4470
- : ref.name === repositoryPaths.currentBranch,
4471
- }));
4465
+ const { worktreeRoot } = repositoryPaths;
4466
+ const localBranches = snapshot.localBranches.map((ref) => {
4467
+ const current = ref.name === currentBranch;
4468
+ return {
4469
+ ...ref,
4470
+ worktreePath:
4471
+ worktreeRoot === null
4472
+ ? ref.worktreePath
4473
+ : current
4474
+ ? worktreeRoot
4475
+ : ref.worktreePath === worktreeRoot
4476
+ ? null
4477
+ : ref.worktreePath,
4478
+ current,
4479
+ };
4480
+ });
4481
const combinedBranches = input.includeMatchingRemoteRefs
4482
? [...localBranches, ...snapshot.remoteBranches]
4483
: dedupeRemoteBranchesWithLocalMatches([...localBranches, ...snapshot.remoteBranches]);
apps/web/src/components/BranchToolbar.logic.test.ts
+5
@@ -192,6 +192,7 @@ describe("resolveBranchSelectionTarget", () => {
192
checkoutCwd: "/repo",
193
nextWorktreePath: "/repo",
194
reuseExistingWorktree: true,
195
+ staysInActiveCheckout: false,
196
});
197
expect(
198
resolveBranchSelectionTarget({
@@ -206,6 +207,7 @@ describe("resolveBranchSelectionTarget", () => {
207
checkoutCwd: "/repo/.t3/worktrees/feature-b",
208
nextWorktreePath: "/repo/.t3/worktrees/feature-b",
209
reuseExistingWorktree: true,
210
+ staysInActiveCheckout: false,
211
});
212
});
213
@@ -233,6 +235,8 @@ describe("resolveBranchSelectionTarget", () => {
235
checkoutCwd: "/repo",
236
nextWorktreePath: null,
237
reuseExistingWorktree: true,
238
+ // A ref listed in the thread's own checkout may be stale, so it is checked out, not assumed.
239
+ staysInActiveCheckout: activeWorktreePath === null,
240
});
241
}
242
});
@@ -251,6 +255,7 @@ describe("resolveBranchSelectionTarget", () => {
255
checkoutCwd: "/repo/.t3/worktrees/feature-a",
256
nextWorktreePath: "/repo/.t3/worktrees/feature-a",
257
reuseExistingWorktree: false,
258
+ staysInActiveCheckout: true,
259
});
260
});
261
});
apps/web/src/components/BranchToolbar.logic.ts
+6
-1
@@ -257,6 +257,7 @@ export function resolveBranchSelectionTarget(input: {
257
checkoutCwd: string;
258
nextWorktreePath: string | null;
259
reuseExistingWorktree: boolean;
260
+ staysInActiveCheckout: boolean;
261
} {
262
const { activeProjectCwd, activeWorktreePath, refName } = input;
263
@@ -266,10 +267,13 @@ export function resolveBranchSelectionTarget(input: {
267
input.activeProjectCheckoutRoot === undefined
268
? workspaceRelativeFilePath(activeProjectCwd, refName.worktreePath) !== null
269
: workspaceRelativeFilePath(input.activeProjectCheckoutRoot, refName.worktreePath) === ".";
270
+ const nextWorktreePath = isProjectCheckout ? null : refName.worktreePath;
271
return {
272
checkoutCwd: refName.worktreePath,
271
- nextWorktreePath: isProjectCheckout ? null : refName.worktreePath,
273
+ nextWorktreePath,
274
reuseExistingWorktree: true,
275
+ // Listed refs can predate a checkout made outside the picker, so only Git can confirm this one.
276
+ staysInActiveCheckout: nextWorktreePath === activeWorktreePath,
277
};
278
}
279
@@ -280,6 +284,7 @@ export function resolveBranchSelectionTarget(input: {
284
checkoutCwd: nextWorktreePath ?? activeProjectCwd,
285
nextWorktreePath,
286
reuseExistingWorktree: false,
287
+ staysInActiveCheckout: nextWorktreePath === activeWorktreePath,
288
};
289
}
290
apps/web/src/components/BranchToolbarBranchSelector.tsx
+4
-1
@@ -445,7 +445,10 @@ export function BranchToolbarBranchSelector({
445
)
446
return;
447
448
- if (selectionTarget.reuseExistingWorktree) {
448
+ if (
449
+ selectionTarget.reuseExistingWorktree &&
450
+ !(selectionTarget.staysInActiveCheckout && canWriteSourceControl)
451
+ ) {
452
setThreadBranch(refName.name, selectionTarget.nextWorktreePath);
453
setIsBranchMenuOpen(false);
454
onComposerFocusRequest?.();