Skip to content

Commit daebb8b

Browse files
fix(web): worktree threads warn when the checkout branch changes (#14883)
Co-authored-by: Julius Marminge <[email protected]>
1 parent f7ad7f2 commit daebb8b

6 files changed

Lines changed: 103 additions & 67 deletions

File tree

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
resolveBranchToolbarPrBranch,
1414
resolveBranchToolbarValue,
1515
resolveLockedWorkspaceLabel,
16-
resolveLocalCheckoutBranchMismatch,
16+
resolveCheckoutBranchMismatch,
1717
resolvePreviousWorktreeLabel,
1818
resolvePreviousWorktreeSeed,
1919
sanitizeNewRefName,
@@ -301,10 +301,10 @@ describe("resolveBranchToolbarPrBranch", () => {
301301
});
302302
});
303303

304-
describe("resolveLocalCheckoutBranchMismatch", () => {
304+
describe("resolveCheckoutBranchMismatch", () => {
305305
it("detects when a local thread is associated with a different branch than the checkout", () => {
306306
expect(
307-
resolveLocalCheckoutBranchMismatch({
307+
resolveCheckoutBranchMismatch({
308308
effectiveEnvMode: "local",
309309
activeWorktreePath: null,
310310
activeThreadBranch: "feature/thread",
@@ -313,12 +313,13 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
313313
).toEqual({
314314
threadBranch: "feature/thread",
315315
currentBranch: "feature/current",
316+
canRestoreThreadBranch: true,
316317
});
317318
});
318319

319320
it("ignores matching local checkout state", () => {
320321
expect(
321-
resolveLocalCheckoutBranchMismatch({
322+
resolveCheckoutBranchMismatch({
322323
effectiveEnvMode: "local",
323324
activeWorktreePath: null,
324325
activeThreadBranch: "feature/thread",
@@ -327,20 +328,34 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
327328
).toBeNull();
328329
});
329330

330-
it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
331+
it("warns about worktree branch drift without restore, but ignores a pending rename status", () => {
332+
const worktree = {
333+
effectiveEnvMode: "worktree" as const,
334+
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
335+
activeThreadBranch: "t3code/fix-sidebar",
336+
};
337+
331338
expect(
332-
resolveLocalCheckoutBranchMismatch({
333-
effectiveEnvMode: "worktree",
334-
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
335-
activeThreadBranch: "feature/thread",
336-
currentGitBranch: "feature/current",
339+
resolveCheckoutBranchMismatch({
340+
...worktree,
341+
currentGitBranch: "t3code/1a2b3c4d",
337342
}),
338343
).toBeNull();
344+
expect(
345+
resolveCheckoutBranchMismatch({
346+
...worktree,
347+
currentGitBranch: "feature/checkout",
348+
}),
349+
).toEqual({
350+
threadBranch: worktree.activeThreadBranch,
351+
currentBranch: "feature/checkout",
352+
canRestoreThreadBranch: false,
353+
});
339354
});
340355

341356
it("ignores new-worktree base selection before a worktree exists", () => {
342357
expect(
343-
resolveLocalCheckoutBranchMismatch({
358+
resolveCheckoutBranchMismatch({
344359
effectiveEnvMode: "worktree",
345360
activeWorktreePath: null,
346361
activeThreadBranch: "feature/base",

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

Lines changed: 20 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,
@@ -231,20 +231,35 @@ export function resolveBranchToolbarPrBranch(input: {
231231
return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;
232232
}
233233

234-
export function resolveLocalCheckoutBranchMismatch(input: {
234+
export function resolveCheckoutBranchMismatch(input: {
235235
effectiveEnvMode: EnvMode;
236236
activeWorktreePath: string | null;
237237
activeThreadBranch: string | null;
238238
currentGitBranch: string | null;
239-
}): { threadBranch: string; currentBranch: string } | null {
239+
}) {
240240
const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
241-
if (effectiveEnvMode !== "local" || activeWorktreePath !== null) {
241+
const isLocalCheckout = effectiveEnvMode === "local" && activeWorktreePath === null;
242+
const isEstablishedWorktree = effectiveEnvMode === "worktree" && activeWorktreePath !== null;
243+
if (!isLocalCheckout && !isEstablishedWorktree) {
242244
return null;
243245
}
244246
if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {
245247
return null;
246248
}
247-
return { threadBranch: activeThreadBranch, currentBranch: currentGitBranch };
249+
// Cached status can predate the automatic rename of a temporary worktree branch.
250+
if (
251+
isEstablishedWorktree &&
252+
!isTemporaryWorktreeBranch(activeThreadBranch) &&
253+
isTemporaryWorktreeBranch(currentGitBranch)
254+
) {
255+
return null;
256+
}
257+
return {
258+
threadBranch: activeThreadBranch,
259+
currentBranch: currentGitBranch,
260+
// Restoring a worktree checkout could switch the branch for sibling threads.
261+
canRestoreThreadBranch: isLocalCheckout,
262+
};
248263
}
249264

250265
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
@@ -258,14 +258,14 @@ describe("resolveThreadMetadataUpdateForNextTurn", () => {
258258
model: "gpt-5.4",
259259
};
260260

261-
it("updates a stale local thread branch to the active checkout", () => {
261+
it("adopts the checkout branch without clearing the worktree attachment", () => {
262262
expect(
263263
resolveThreadMetadataUpdateForNextTurn({
264264
currentModelSelection: modelSelection,
265265
currentBranch: "feature/thread",
266266
nextBranch: "feature/checkout",
267267
}),
268-
).toEqual({ branch: "feature/checkout", worktreePath: null });
268+
).toEqual({ branch: "feature/checkout" });
269269
});
270270

271271
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
@@ -485,7 +485,6 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
485485
}): {
486486
modelSelection?: ModelSelection;
487487
branch?: string;
488-
worktreePath?: null;
489488
} | null {
490489
const nextModelSelection = input.nextModelSelection;
491490
const modelSelectionChanged =
@@ -500,7 +499,7 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
500499
}
501500
return {
502501
...(modelSelectionChanged ? { modelSelection: nextModelSelection } : {}),
503-
...(branchChanged ? { branch: input.nextBranch, worktreePath: null } : {}),
502+
...(branchChanged ? { branch: input.nextBranch } : {}),
504503
};
505504
}
506505

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

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ import { ThreadDetailsPanel } from "./chat/ThreadDetailsPanel";
433433
import { NoActiveThreadState } from "./NoActiveThreadState";
434434
import {
435435
resolveEffectiveEnvMode,
436-
resolveLocalCheckoutBranchMismatch,
436+
resolveCheckoutBranchMismatch,
437437
shouldShowComposerContextStrip,
438438
shouldShowEnvironmentIndicator,
439439
} from "./BranchToolbar.logic";
@@ -3376,26 +3376,6 @@ export default function ChatView(props: ChatViewProps) {
33763376
scheduleComposerFocus();
33773377
};
33783378
const insertArtifactTemplateStable = useStableCallback(insertArtifactTemplate);
3379-
const sendAppMessage = async (text: string) => {
3380-
if (!isServerThread || activeThreadId === null) {
3381-
throw new Error("Messages from apps need a started thread.");
3382-
}
3383-
const result = await startThreadTurn({
3384-
environmentId,
3385-
input: {
3386-
threadId: activeThreadId,
3387-
message: { messageId: newMessageId(), role: "user", text, attachments: [] },
3388-
runtimeMode,
3389-
interactionMode,
3390-
dispatchMode: "queue",
3391-
},
3392-
});
3393-
if (result._tag === "Failure") {
3394-
const error = squashAtomCommandFailure(result);
3395-
throw error instanceof Error ? error : new Error("Could not send the app's message.");
3396-
}
3397-
};
3398-
const sendAppMessageStable = useStableCallback(sendAppMessage);
33993379
const queuedRunsControlRef = useRef<QueuedRunsControlHandle>(null);
34003380
const addTerminalContextToDraft = (selection: TerminalContextSelection) => {
34013381
composerRef.current?.addTerminalContext(selection);
@@ -4685,8 +4665,8 @@ export default function ChatView(props: ChatViewProps) {
46854665
requestedEnvMode: envMode,
46864666
isGitRepo,
46874667
});
4688-
const localCheckoutBranchMismatch = isServerThread
4689-
? resolveLocalCheckoutBranchMismatch({
4668+
const checkoutBranchMismatch = isServerThread
4669+
? resolveCheckoutBranchMismatch({
46904670
effectiveEnvMode: envMode,
46914671
activeWorktreePath,
46924672
activeThreadBranch,
@@ -4966,10 +4946,10 @@ export default function ChatView(props: ChatViewProps) {
49664946
const nowMinute = useNowMinute();
49674947
const activeBranchMismatchKey = branchMismatchKey(
49684948
activeThread?.id ?? null,
4969-
localCheckoutBranchMismatch,
4949+
checkoutBranchMismatch,
49704950
);
49714951
const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
4972-
hasMismatch: localCheckoutBranchMismatch !== null,
4952+
hasMismatch: checkoutBranchMismatch !== null,
49734953
isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
49744954
composerHasContent: composerHasDraftContent,
49754955
wasShownForCurrentMismatch:
@@ -4988,7 +4968,7 @@ export default function ChatView(props: ChatViewProps) {
49884968
if (
49894969
!activeProjectCwd ||
49904970
!activeThread ||
4991-
!localCheckoutBranchMismatch ||
4971+
!checkoutBranchMismatch?.canRestoreThreadBranch ||
49924972
isRestoringThreadBranch
49934973
) {
49944974
return;
@@ -4998,7 +4978,7 @@ export default function ChatView(props: ChatViewProps) {
49984978
environmentId,
49994979
input: {
50004980
cwd: activeProjectCwd,
5001-
refName: localCheckoutBranchMismatch.threadBranch,
4981+
refName: checkoutBranchMismatch.threadBranch,
50024982
},
50034983
});
50044984
if (checkoutResult._tag === "Failure") {
@@ -5019,7 +4999,7 @@ export default function ChatView(props: ChatViewProps) {
50194999
setIsRestoringThreadBranch(false);
50205000
return;
50215001
}
5022-
const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;
5002+
const nextBranch = checkoutResult.value.refName ?? checkoutBranchMismatch.threadBranch;
50235003
if (nextBranch !== activeThread.branch) {
50245004
const updateResult = await updateThreadMetadata({
50255005
environmentId,
@@ -5114,6 +5094,37 @@ export default function ChatView(props: ChatViewProps) {
51145094
};
51155095
})() as ComposerBannerStackItem | null;
51165096

5097+
const sendAppMessage = async (text: string) => {
5098+
if (!isServerThread || activeThreadId === null) {
5099+
throw new Error("Messages from apps need a started thread.");
5100+
}
5101+
const settingsResult = await persistThreadSettingsForNextTurn({
5102+
threadId: activeThreadId,
5103+
createdAt: new Date().toISOString(),
5104+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
5105+
runtimeMode,
5106+
interactionMode,
5107+
});
5108+
const result =
5109+
settingsResult._tag === "Failure"
5110+
? settingsResult
5111+
: await startThreadTurn({
5112+
environmentId,
5113+
input: {
5114+
threadId: activeThreadId,
5115+
message: { messageId: newMessageId(), role: "user", text, attachments: [] },
5116+
runtimeMode,
5117+
interactionMode,
5118+
dispatchMode: "queue",
5119+
},
5120+
});
5121+
if (result._tag === "Failure") {
5122+
const error = squashAtomCommandFailure(result);
5123+
throw error instanceof Error ? error : new Error("Could not send the app's message.");
5124+
}
5125+
};
5126+
const sendAppMessageStable = useStableCallback(sendAppMessage);
5127+
51175128
const sendStandaloneCommand = async (text: string, failureMessage: string) => {
51185129
if (!activeThread || !clientSettingsHydrated || sendInFlightRef.current) return;
51195130
const context = composerRef.current?.getSendContext();
@@ -5142,7 +5153,7 @@ export default function ChatView(props: ChatViewProps) {
51425153
const settingsResult = await persistThreadSettingsForNextTurn({
51435154
threadId,
51445155
createdAt,
5145-
...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
5156+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
51465157
runtimeMode,
51475158
interactionMode: context.interactionMode,
51485159
});
@@ -5418,7 +5429,7 @@ export default function ChatView(props: ChatViewProps) {
54185429
].filter((item) => item !== null);
54195430
const usageLimitsItems = usageLimitsBanner === null ? [] : [usageLimitsBanner];
54205431
const projectCloneItems = projectCloneBannerItem === null ? [] : [projectCloneBannerItem];
5421-
if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
5432+
if (!checkoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
54225433
return [
54235434
...feedbackBannerItems,
54245435
...limitRecoveryItems,
@@ -5447,18 +5458,18 @@ export default function ChatView(props: ChatViewProps) {
54475458
<TooltipTrigger
54485459
render={
54495460
<code className="min-w-0 truncate font-medium text-foreground">
5450-
{localCheckoutBranchMismatch.threadBranch}
5461+
{checkoutBranchMismatch.threadBranch}
54515462
</code>
54525463
}
54535464
/>
54545465
<TooltipPopup side="top">
5455-
This thread last ran on {localCheckoutBranchMismatch.threadBranch}. Sending will
5456-
continue on {localCheckoutBranchMismatch.currentBranch}.
5466+
This thread last ran on {checkoutBranchMismatch.threadBranch}. Sending will continue
5467+
on {checkoutBranchMismatch.currentBranch}.
54575468
</TooltipPopup>
54585469
</Tooltip>
54595470
</span>
54605471
),
5461-
actions: (
5472+
actions: checkoutBranchMismatch.canRestoreThreadBranch ? (
54625473
<Button
54635474
size="xs"
54645475
variant="ghost"
@@ -5467,7 +5478,7 @@ export default function ChatView(props: ChatViewProps) {
54675478
>
54685479
{isRestoringThreadBranch ? "Restoring..." : "Restore branch"}
54695480
</Button>
5470-
),
5481+
) : null,
54715482
dismissLabel: "Dismiss branch change notice",
54725483
onDismiss: () => {
54735484
dismissBranchMismatchForSession(activeBranchMismatchKey);
@@ -5945,9 +5956,7 @@ export default function ChatView(props: ChatViewProps) {
59455956
const settingsResult = await persistThreadSettingsForNextTurn({
59465957
threadId,
59475958
createdAt,
5948-
...(localCheckoutBranchMismatch
5949-
? { branch: localCheckoutBranchMismatch.currentBranch }
5950-
: {}),
5959+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
59515960
runtimeMode,
59525961
interactionMode,
59535962
});
@@ -6922,9 +6931,7 @@ export default function ChatView(props: ChatViewProps) {
69226931
const settingsResult = await persistThreadSettingsForNextTurn({
69236932
threadId: threadIdForSend,
69246933
createdAt: messageCreatedAt,
6925-
...(localCheckoutBranchMismatch
6926-
? { branch: localCheckoutBranchMismatch.currentBranch }
6927-
: {}),
6934+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
69286935
runtimeMode,
69296936
interactionMode: sendInteractionMode,
69306937
});
@@ -7278,7 +7285,7 @@ export default function ChatView(props: ChatViewProps) {
72787285
const settingsResult = await persistThreadSettingsForNextTurn({
72797286
threadId: threadIdForSend,
72807287
createdAt: messageCreatedAt,
7281-
...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
7288+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
72827289
runtimeMode,
72837290
interactionMode: nextInteractionMode,
72847291
});
@@ -7398,7 +7405,7 @@ export default function ChatView(props: ChatViewProps) {
73987405
modelSelection: nextThreadModelSelection,
73997406
runtimeMode: defaultRuntimeMode,
74007407
interactionMode: "default",
7401-
branch: activeThreadBranch,
7408+
branch: checkoutBranchMismatch?.currentBranch ?? activeThreadBranch,
74027409
worktreePath: activeThread.worktreePath,
74037410
createdAt,
74047411
},
@@ -8482,7 +8489,7 @@ export default function ChatView(props: ChatViewProps) {
84828489

84838490
<BranchRestoreConfirmHost
84848491
threadKey={activeThreadKey}
8485-
threadBranch={localCheckoutBranchMismatch?.threadBranch ?? ""}
8492+
threadBranch={checkoutBranchMismatch?.threadBranch ?? ""}
84868493
canWriteSourceControl={canWriteSourceControl}
84878494
onConfirm={handleSwitchCheckoutToThreadStable}
84888495
/>

0 commit comments

Comments
 (0)