Skip to content

Commit 4434ec6

Browse files
committed
fix(web): warn when a worktree thread's branch no longer matches its checkout
1 parent cc1e634 commit 4434ec6

6 files changed

Lines changed: 74 additions & 46 deletions

File tree

‎apps/web/src/components/BranchToolbar.logic.test.ts‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
resolveBranchToolbarValue,
1515
resolveLockedWorkspaceLabel,
1616
resolveWorkspaceDisplayName,
17-
resolveLocalCheckoutBranchMismatch,
17+
resolveCheckoutBranchMismatch,
1818
resolvePreviousWorktreeLabel,
1919
resolvePreviousWorktreeSeed,
2020
sanitizeNewRefName,
@@ -302,10 +302,10 @@ describe("resolveBranchToolbarPrBranch", () => {
302302
});
303303
});
304304

305-
describe("resolveLocalCheckoutBranchMismatch", () => {
305+
describe("resolveCheckoutBranchMismatch", () => {
306306
it("detects when a local thread is associated with a different branch than the checkout", () => {
307307
expect(
308-
resolveLocalCheckoutBranchMismatch({
308+
resolveCheckoutBranchMismatch({
309309
effectiveEnvMode: "local",
310310
activeWorktreePath: null,
311311
activeThreadBranch: "feature/thread",
@@ -314,12 +314,13 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
314314
).toEqual({
315315
threadBranch: "feature/thread",
316316
currentBranch: "feature/current",
317+
canRestoreThreadBranch: true,
317318
});
318319
});
319320

320321
it("ignores matching local checkout state", () => {
321322
expect(
322-
resolveLocalCheckoutBranchMismatch({
323+
resolveCheckoutBranchMismatch({
323324
effectiveEnvMode: "local",
324325
activeWorktreePath: null,
325326
activeThreadBranch: "feature/thread",
@@ -328,20 +329,35 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
328329
).toBeNull();
329330
});
330331

331-
it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
332+
it("detects a worktree checked out on another branch without offering a restore", () => {
332333
expect(
333-
resolveLocalCheckoutBranchMismatch({
334+
resolveCheckoutBranchMismatch({
334335
effectiveEnvMode: "worktree",
335336
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
336337
activeThreadBranch: "feature/thread",
337338
currentGitBranch: "feature/current",
338339
}),
340+
).toEqual({
341+
threadBranch: "feature/thread",
342+
currentBranch: "feature/current",
343+
canRestoreThreadBranch: false,
344+
});
345+
});
346+
347+
it("ignores a worktree status read from before its temporary branch was renamed", () => {
348+
expect(
349+
resolveCheckoutBranchMismatch({
350+
effectiveEnvMode: "worktree",
351+
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
352+
activeThreadBranch: "t3code/fix-sidebar",
353+
currentGitBranch: "t3code/1a2b3c4d",
354+
}),
339355
).toBeNull();
340356
});
341357

342358
it("ignores new-worktree base selection before a worktree exists", () => {
343359
expect(
344-
resolveLocalCheckoutBranchMismatch({
360+
resolveCheckoutBranchMismatch({
345361
effectiveEnvMode: "worktree",
346362
activeWorktreePath: null,
347363
activeThreadBranch: "feature/base",

‎apps/web/src/components/BranchToolbar.logic.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
WorktreeSubmodules,
77
} from "@t3tools/contracts";
88
import * as Schema from "effect/Schema";
9-
import { sanitizeNewRefName } from "@t3tools/shared/git";
9+
import { isTemporaryWorktreeBranch, sanitizeNewRefName } from "@t3tools/shared/git";
1010
import { toSortableTimestamp } from "../lib/threadSort";
1111
export {
1212
dedupeRemoteBranchesWithLocalMatches,
@@ -253,20 +253,39 @@ export function resolveBranchToolbarPrBranch(input: {
253253
return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;
254254
}
255255

256-
export function resolveLocalCheckoutBranchMismatch(input: {
256+
// A thread's recorded branch can fall behind its checkout: the user or an agent
257+
// switches branches, or a sibling thread sharing the worktree does. Only a local
258+
// checkout offers a restore; switching a worktree back could pull the branch
259+
// out from under a sibling thread.
260+
export function resolveCheckoutBranchMismatch(input: {
257261
effectiveEnvMode: EnvMode;
258262
activeWorktreePath: string | null;
259263
activeThreadBranch: string | null;
260264
currentGitBranch: string | null;
261-
}): { threadBranch: string; currentBranch: string } | null {
265+
}): { threadBranch: string; currentBranch: string; canRestoreThreadBranch: boolean } | null {
262266
const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
263-
if (effectiveEnvMode !== "local" || activeWorktreePath !== null) {
267+
const isLocalCheckout = effectiveEnvMode === "local" && activeWorktreePath === null;
268+
const isEstablishedWorktree = effectiveEnvMode === "worktree" && activeWorktreePath !== null;
269+
if (!isLocalCheckout && !isEstablishedWorktree) {
264270
return null;
265271
}
266272
if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {
267273
return null;
268274
}
269-
return { threadBranch: activeThreadBranch, currentBranch: currentGitBranch };
275+
// A new worktree's temporary branch is renamed in the background, so a status
276+
// read from before the rename is not drift.
277+
if (
278+
isEstablishedWorktree &&
279+
!isTemporaryWorktreeBranch(activeThreadBranch) &&
280+
isTemporaryWorktreeBranch(currentGitBranch)
281+
) {
282+
return null;
283+
}
284+
return {
285+
threadBranch: activeThreadBranch,
286+
currentBranch: currentGitBranch,
287+
canRestoreThreadBranch: isLocalCheckout,
288+
};
270289
}
271290

272291
export function resolveBranchSelectionTarget(input: {

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ describe("resolveThreadMetadataUpdateForNextTurn", () => {
244244
model: "gpt-5.4",
245245
};
246246

247-
it("updates a stale local thread branch to the active checkout", () => {
247+
it("updates a stale thread branch to the active checkout without moving the thread", () => {
248248
expect(
249249
resolveThreadMetadataUpdateForNextTurn({
250250
currentModelSelection: modelSelection,
251251
currentBranch: "feature/thread",
252252
nextBranch: "feature/checkout",
253253
}),
254-
).toEqual({ branch: "feature/checkout", worktreePath: null });
254+
).toEqual({ branch: "feature/checkout" });
255255
});
256256

257257
it("does not write metadata when the model and branch are unchanged", () => {

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
486486
}): {
487487
modelSelection?: ModelSelection;
488488
branch?: string;
489-
worktreePath?: null;
490489
} | null {
491490
const nextModelSelection = input.nextModelSelection;
492491
const modelSelectionChanged =
@@ -501,7 +500,7 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
501500
}
502501
return {
503502
...(modelSelectionChanged ? { modelSelection: nextModelSelection } : {}),
504-
...(branchChanged ? { branch: input.nextBranch, worktreePath: null } : {}),
503+
...(branchChanged ? { branch: input.nextBranch } : {}),
505504
};
506505
}
507506

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ import { NoActiveThreadState } from "./NoActiveThreadState";
434434
import {
435435
type EnvironmentOption,
436436
resolveEffectiveEnvMode,
437-
resolveLocalCheckoutBranchMismatch,
437+
resolveCheckoutBranchMismatch,
438438
shouldShowComposerContextStrip,
439439
shouldShowEnvironmentIndicator,
440440
} from "./BranchToolbar.logic";
@@ -6433,10 +6433,10 @@ export default function ChatView(props: ChatViewProps) {
64336433
requestedEnvMode: envMode,
64346434
isGitRepo,
64356435
});
6436-
const localCheckoutBranchMismatch = useMemo(
6436+
const checkoutBranchMismatch = useMemo(
64376437
() =>
64386438
isServerThread
6439-
? resolveLocalCheckoutBranchMismatch({
6439+
? resolveCheckoutBranchMismatch({
64406440
effectiveEnvMode: envMode,
64416441
activeWorktreePath,
64426442
activeThreadBranch,
@@ -6736,10 +6736,10 @@ export default function ChatView(props: ChatViewProps) {
67366736
const nowMinute = useNowMinute();
67376737
const activeBranchMismatchKey = branchMismatchKey(
67386738
activeThread?.id ?? null,
6739-
localCheckoutBranchMismatch,
6739+
checkoutBranchMismatch,
67406740
);
67416741
const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
6742-
hasMismatch: localCheckoutBranchMismatch !== null,
6742+
hasMismatch: checkoutBranchMismatch !== null,
67436743
isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
67446744
composerHasContent: composerHasDraftContent,
67456745
wasShownForCurrentMismatch:
@@ -6759,7 +6759,7 @@ export default function ChatView(props: ChatViewProps) {
67596759
if (
67606760
!activeProjectCwd ||
67616761
!activeThread ||
6762-
!localCheckoutBranchMismatch ||
6762+
!checkoutBranchMismatch?.canRestoreThreadBranch ||
67636763
isRestoringThreadBranch
67646764
) {
67656765
return;
@@ -6769,7 +6769,7 @@ export default function ChatView(props: ChatViewProps) {
67696769
environmentId,
67706770
input: {
67716771
cwd: activeProjectCwd,
6772-
refName: localCheckoutBranchMismatch.threadBranch,
6772+
refName: checkoutBranchMismatch.threadBranch,
67736773
},
67746774
});
67756775
if (checkoutResult._tag === "Failure") {
@@ -6786,7 +6786,7 @@ export default function ChatView(props: ChatViewProps) {
67866786
return;
67876787
}
67886788

6789-
const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;
6789+
const nextBranch = checkoutResult.value.refName ?? checkoutBranchMismatch.threadBranch;
67906790
if (nextBranch !== activeThread.branch) {
67916791
const updateResult = await updateThreadMetadata({
67926792
environmentId,
@@ -6816,7 +6816,7 @@ export default function ChatView(props: ChatViewProps) {
68166816
environmentId,
68176817
gitStatusQuery,
68186818
isRestoringThreadBranch,
6819-
localCheckoutBranchMismatch,
6819+
checkoutBranchMismatch,
68206820
scheduleComposerFocus,
68216821
switchGitRef,
68226822
updateThreadMetadata,
@@ -7132,7 +7132,7 @@ export default function ChatView(props: ChatViewProps) {
71327132
// The user asked for this one, so it leads the notice tier instead of trailing it.
71337133
const usageLimitsItems = usageLimitsBanner === null ? [] : [usageLimitsBanner];
71347134
const projectCloneItems = projectCloneBannerItem === null ? [] : [projectCloneBannerItem];
7135-
if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
7135+
if (!checkoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
71367136
return [
71377137
...feedbackBannerItems,
71387138
...limitRecoveryItems,
@@ -7165,18 +7165,18 @@ export default function ChatView(props: ChatViewProps) {
71657165
<TooltipTrigger
71667166
render={
71677167
<code className="min-w-0 truncate font-medium text-foreground">
7168-
{localCheckoutBranchMismatch.threadBranch}
7168+
{checkoutBranchMismatch.threadBranch}
71697169
</code>
71707170
}
71717171
/>
71727172
<TooltipPopup side="top">
7173-
This thread last ran on {localCheckoutBranchMismatch.threadBranch}. Sending will
7174-
continue on {localCheckoutBranchMismatch.currentBranch}.
7173+
This thread last ran on {checkoutBranchMismatch.threadBranch}. Sending will continue
7174+
on {checkoutBranchMismatch.currentBranch}.
71757175
</TooltipPopup>
71767176
</Tooltip>
71777177
</span>
71787178
),
7179-
actions: (
7179+
actions: checkoutBranchMismatch.canRestoreThreadBranch ? (
71807180
<Button
71817181
size="xs"
71827182
variant="ghost"
@@ -7185,7 +7185,7 @@ export default function ChatView(props: ChatViewProps) {
71857185
>
71867186
{isRestoringThreadBranch ? "Restoring..." : "Restore branch"}
71877187
</Button>
7188-
),
7188+
) : null,
71897189
dismissLabel: "Dismiss branch change notice",
71907190
onDismiss: () => {
71917191
dismissBranchMismatchForSession(activeBranchMismatchKey);
@@ -7203,7 +7203,7 @@ export default function ChatView(props: ChatViewProps) {
72037203
handleRestoreThreadBranch,
72047204
isRestoringThreadBranch,
72057205
backgroundWorkBannerItem,
7206-
localCheckoutBranchMismatch,
7206+
checkoutBranchMismatch,
72077207
parkedThreadBannerItem,
72087208
projectCloneBannerItem,
72097209
resumeCompactionBannerItem,
@@ -7930,9 +7930,7 @@ export default function ChatView(props: ChatViewProps) {
79307930
const settingsResult = await persistThreadSettingsForNextTurn({
79317931
threadId,
79327932
createdAt,
7933-
...(localCheckoutBranchMismatch
7934-
? { branch: localCheckoutBranchMismatch.currentBranch }
7935-
: {}),
7933+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
79367934
runtimeMode,
79377935
interactionMode: context.interactionMode,
79387936
});
@@ -7997,9 +7995,7 @@ export default function ChatView(props: ChatViewProps) {
79977995
const settingsResult = await persistThreadSettingsForNextTurn({
79987996
threadId,
79997997
createdAt,
8000-
...(localCheckoutBranchMismatch
8001-
? { branch: localCheckoutBranchMismatch.currentBranch }
8002-
: {}),
7998+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
80037999
runtimeMode,
80048000
interactionMode,
80058001
});
@@ -9115,9 +9111,7 @@ export default function ChatView(props: ChatViewProps) {
91159111
const settingsResult = await persistThreadSettingsForNextTurn({
91169112
threadId: threadIdForSend,
91179113
createdAt: messageCreatedAt,
9118-
...(localCheckoutBranchMismatch
9119-
? { branch: localCheckoutBranchMismatch.currentBranch }
9120-
: {}),
9114+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
91219115
runtimeMode,
91229116
interactionMode: sendInteractionMode,
91239117
});
@@ -9703,7 +9697,7 @@ export default function ChatView(props: ChatViewProps) {
97039697
const settingsResult = await persistThreadSettingsForNextTurn({
97049698
threadId: threadIdForSend,
97059699
createdAt: messageCreatedAt,
9706-
...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
9700+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
97079701
runtimeMode,
97089702
interactionMode: nextInteractionMode,
97099703
});
@@ -11068,7 +11062,7 @@ export default function ChatView(props: ChatViewProps) {
1106811062
<AlertDialogTitle>
1106911063
Switch to{" "}
1107011064
<code className="font-medium">
11071-
{localCheckoutBranchMismatch?.threadBranch ?? ""}
11065+
{checkoutBranchMismatch?.threadBranch ?? ""}
1107211066
</code>
1107311067
?
1107411068
</AlertDialogTitle>

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ import {
212212
type SidebarListMarker,
213213
type SidebarSection,
214214
} from "./Sidebar.logic";
215-
import { resolveLocalCheckoutBranchMismatch } from "./BranchToolbar.logic";
215+
import { resolveCheckoutBranchMismatch } from "./BranchToolbar.logic";
216216
import {
217217
createSidebarCollisionDetection,
218218
createSidebarSortingStrategy,
@@ -1316,7 +1316,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
13161316
: null;
13171317
const isWokeStatus = topStatus?.icon === "woke";
13181318

1319-
const branchMismatch = resolveLocalCheckoutBranchMismatch({
1319+
const branchMismatch = resolveCheckoutBranchMismatch({
13201320
effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
13211321
activeWorktreePath: thread.worktreePath,
13221322
activeThreadBranch: thread.branch,
@@ -2173,7 +2173,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
21732173
JSON.stringify([thread.environmentId, gitCwd]),
21742174
gitStatus.data,
21752175
);
2176-
const branchMismatch = resolveLocalCheckoutBranchMismatch({
2176+
const branchMismatch = resolveCheckoutBranchMismatch({
21772177
effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
21782178
activeWorktreePath: thread.worktreePath,
21792179
activeThreadBranch: thread.branch,

0 commit comments

Comments
 (0)