@devries-cameron20 / t3code-1 / commits / daebb8b506
Fork 0

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

fix(web): worktree threads warn when the checkout branch changes (#14883)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Tristan Knight committed Oct 19, 2026 at 22:19 UTC daebb8b506f1bc2a99cf6c327d9b6ad26e9fc2d6
6 files changed +103 -67
apps/web/src/components/BranchToolbar.logic.test.ts
+26 -11
@@ -13,7 +13,7 @@ import {
13 resolveBranchToolbarPrBranch,
14 resolveBranchToolbarValue,
15 resolveLockedWorkspaceLabel,
16 - resolveLocalCheckoutBranchMismatch,
16 + resolveCheckoutBranchMismatch,
17 resolvePreviousWorktreeLabel,
18 resolvePreviousWorktreeSeed,
19 sanitizeNewRefName,
@@ -301,10 +301,10 @@ describe("resolveBranchToolbarPrBranch", () => {
301 });
302 });
303
304 -describe("resolveLocalCheckoutBranchMismatch", () => {
304 +describe("resolveCheckoutBranchMismatch", () => {
305 it("detects when a local thread is associated with a different branch than the checkout", () => {
306 expect(
307 - resolveLocalCheckoutBranchMismatch({
307 + resolveCheckoutBranchMismatch({
308 effectiveEnvMode: "local",
309 activeWorktreePath: null,
310 activeThreadBranch: "feature/thread",
@@ -313,12 +313,13 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
313 ).toEqual({
314 threadBranch: "feature/thread",
315 currentBranch: "feature/current",
316 + canRestoreThreadBranch: true,
317 });
318 });
319
320 it("ignores matching local checkout state", () => {
321 expect(
321 - resolveLocalCheckoutBranchMismatch({
322 + resolveCheckoutBranchMismatch({
323 effectiveEnvMode: "local",
324 activeWorktreePath: null,
325 activeThreadBranch: "feature/thread",
@@ -327,20 +328,34 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
328 ).toBeNull();
329 });
330
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 +
338 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",
342 }),
343 ).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 + });
354 });
355
356 it("ignores new-worktree base selection before a worktree exists", () => {
357 expect(
343 - resolveLocalCheckoutBranchMismatch({
358 + resolveCheckoutBranchMismatch({
359 effectiveEnvMode: "worktree",
360 activeWorktreePath: null,
361 activeThreadBranch: "feature/base",
apps/web/src/components/BranchToolbar.logic.ts
+20 -5
@@ -6,7 +6,7 @@ import type {
6 WorktreeSubmodules,
7 } from "@t3tools/contracts";
8 import * as Schema from "effect/Schema";
9 -import { sanitizeNewRefName } from "@t3tools/shared/git";
9 +import { isTemporaryWorktreeBranch, sanitizeNewRefName } from "@t3tools/shared/git";
10 import { toSortableTimestamp } from "../lib/threadSort";
11 export {
12 dedupeRemoteBranchesWithLocalMatches,
@@ -231,20 +231,35 @@ export function resolveBranchToolbarPrBranch(input: {
231 return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;
232 }
233
234 -export function resolveLocalCheckoutBranchMismatch(input: {
234 +export function resolveCheckoutBranchMismatch(input: {
235 effectiveEnvMode: EnvMode;
236 activeWorktreePath: string | null;
237 activeThreadBranch: string | null;
238 currentGitBranch: string | null;
239 -}): { threadBranch: string; currentBranch: string } | null {
239 +}) {
240 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) {
244 return null;
245 }
246 if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {
247 return null;
248 }
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 + };
263 }
264
265 export function resolveBranchSelectionTarget(input: {
apps/web/src/components/ChatView.logic.test.ts
+2 -2
@@ -258,14 +258,14 @@ describe("resolveThreadMetadataUpdateForNextTurn", () => {
258 model: "gpt-5.4",
259 };
260
261 - it("updates a stale local thread branch to the active checkout", () => {
261 + it("adopts the checkout branch without clearing the worktree attachment", () => {
262 expect(
263 resolveThreadMetadataUpdateForNextTurn({
264 currentModelSelection: modelSelection,
265 currentBranch: "feature/thread",
266 nextBranch: "feature/checkout",
267 }),
268 - ).toEqual({ branch: "feature/checkout", worktreePath: null });
268 + ).toEqual({ branch: "feature/checkout" });
269 });
270
271 it("does not write metadata when the model and branch are unchanged", () => {
apps/web/src/components/ChatView.logic.ts
+1 -2
@@ -485,7 +485,6 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
485 }): {
486 modelSelection?: ModelSelection;
487 branch?: string;
488 - worktreePath?: null;
488 } | null {
489 const nextModelSelection = input.nextModelSelection;
490 const modelSelectionChanged =
@@ -500,7 +499,7 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
499 }
500 return {
501 ...(modelSelectionChanged ? { modelSelection: nextModelSelection } : {}),
503 - ...(branchChanged ? { branch: input.nextBranch, worktreePath: null } : {}),
502 + ...(branchChanged ? { branch: input.nextBranch } : {}),
503 };
504 }
505
apps/web/src/components/ChatView.tsx
+51 -44
@@ -433,7 +433,7 @@ import { ThreadDetailsPanel } from "./chat/ThreadDetailsPanel";
433 import { NoActiveThreadState } from "./NoActiveThreadState";
434 import {
435 resolveEffectiveEnvMode,
436 - resolveLocalCheckoutBranchMismatch,
436 + resolveCheckoutBranchMismatch,
437 shouldShowComposerContextStrip,
438 shouldShowEnvironmentIndicator,
439 } from "./BranchToolbar.logic";
@@ -3376,26 +3376,6 @@ export default function ChatView(props: ChatViewProps) {
3376 scheduleComposerFocus();
3377 };
3378 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);
3379 const queuedRunsControlRef = useRef<QueuedRunsControlHandle>(null);
3380 const addTerminalContextToDraft = (selection: TerminalContextSelection) => {
3381 composerRef.current?.addTerminalContext(selection);
@@ -4685,8 +4665,8 @@ export default function ChatView(props: ChatViewProps) {
4665 requestedEnvMode: envMode,
4666 isGitRepo,
4667 });
4688 - const localCheckoutBranchMismatch = isServerThread
4689 - ? resolveLocalCheckoutBranchMismatch({
4668 + const checkoutBranchMismatch = isServerThread
4669 + ? resolveCheckoutBranchMismatch({
4670 effectiveEnvMode: envMode,
4671 activeWorktreePath,
4672 activeThreadBranch,
@@ -4966,10 +4946,10 @@ export default function ChatView(props: ChatViewProps) {
4946 const nowMinute = useNowMinute();
4947 const activeBranchMismatchKey = branchMismatchKey(
4948 activeThread?.id ?? null,
4969 - localCheckoutBranchMismatch,
4949 + checkoutBranchMismatch,
4950 );
4951 const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
4972 - hasMismatch: localCheckoutBranchMismatch !== null,
4952 + hasMismatch: checkoutBranchMismatch !== null,
4953 isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
4954 composerHasContent: composerHasDraftContent,
4955 wasShownForCurrentMismatch:
@@ -4988,7 +4968,7 @@ export default function ChatView(props: ChatViewProps) {
4968 if (
4969 !activeProjectCwd ||
4970 !activeThread ||
4991 - !localCheckoutBranchMismatch ||
4971 + !checkoutBranchMismatch?.canRestoreThreadBranch ||
4972 isRestoringThreadBranch
4973 ) {
4974 return;
@@ -4998,7 +4978,7 @@ export default function ChatView(props: ChatViewProps) {
4978 environmentId,
4979 input: {
4980 cwd: activeProjectCwd,
5001 - refName: localCheckoutBranchMismatch.threadBranch,
4981 + refName: checkoutBranchMismatch.threadBranch,
4982 },
4983 });
4984 if (checkoutResult._tag === "Failure") {
@@ -5019,7 +4999,7 @@ export default function ChatView(props: ChatViewProps) {
4999 setIsRestoringThreadBranch(false);
5000 return;
5001 }
5022 - const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;
5002 + const nextBranch = checkoutResult.value.refName ?? checkoutBranchMismatch.threadBranch;
5003 if (nextBranch !== activeThread.branch) {
5004 const updateResult = await updateThreadMetadata({
5005 environmentId,
@@ -5114,6 +5094,37 @@ export default function ChatView(props: ChatViewProps) {
5094 };
5095 })() as ComposerBannerStackItem | null;
5096
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 +
5128 const sendStandaloneCommand = async (text: string, failureMessage: string) => {
5129 if (!activeThread || !clientSettingsHydrated || sendInFlightRef.current) return;
5130 const context = composerRef.current?.getSendContext();
@@ -5142,7 +5153,7 @@ export default function ChatView(props: ChatViewProps) {
5153 const settingsResult = await persistThreadSettingsForNextTurn({
5154 threadId,
5155 createdAt,
5145 - ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
5156 + ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
5157 runtimeMode,
5158 interactionMode: context.interactionMode,
5159 });
@@ -5418,7 +5429,7 @@ export default function ChatView(props: ChatViewProps) {
5429 ].filter((item) => item !== null);
5430 const usageLimitsItems = usageLimitsBanner === null ? [] : [usageLimitsBanner];
5431 const projectCloneItems = projectCloneBannerItem === null ? [] : [projectCloneBannerItem];
5421 - if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
5432 + if (!checkoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
5433 return [
5434 ...feedbackBannerItems,
5435 ...limitRecoveryItems,
@@ -5447,18 +5458,18 @@ export default function ChatView(props: ChatViewProps) {
5458 <TooltipTrigger
5459 render={
5460 <code className="min-w-0 truncate font-medium text-foreground">
5450 - {localCheckoutBranchMismatch.threadBranch}
5461 + {checkoutBranchMismatch.threadBranch}
5462 </code>
5463 }
5464 />
5465 <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}.
5468 </TooltipPopup>
5469 </Tooltip>
5470 </span>
5471 ),
5461 - actions: (
5472 + actions: checkoutBranchMismatch.canRestoreThreadBranch ? (
5473 <Button
5474 size="xs"
5475 variant="ghost"
@@ -5467,7 +5478,7 @@ export default function ChatView(props: ChatViewProps) {
5478 >
5479 {isRestoringThreadBranch ? "Restoring..." : "Restore branch"}
5480 </Button>
5470 - ),
5481 + ) : null,
5482 dismissLabel: "Dismiss branch change notice",
5483 onDismiss: () => {
5484 dismissBranchMismatchForSession(activeBranchMismatchKey);
@@ -5945,9 +5956,7 @@ export default function ChatView(props: ChatViewProps) {
5956 const settingsResult = await persistThreadSettingsForNextTurn({
5957 threadId,
5958 createdAt,
5948 - ...(localCheckoutBranchMismatch
5949 - ? { branch: localCheckoutBranchMismatch.currentBranch }
5950 - : {}),
5959 + ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
5960 runtimeMode,
5961 interactionMode,
5962 });
@@ -6922,9 +6931,7 @@ export default function ChatView(props: ChatViewProps) {
6931 const settingsResult = await persistThreadSettingsForNextTurn({
6932 threadId: threadIdForSend,
6933 createdAt: messageCreatedAt,
6925 - ...(localCheckoutBranchMismatch
6926 - ? { branch: localCheckoutBranchMismatch.currentBranch }
6927 - : {}),
6934 + ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
6935 runtimeMode,
6936 interactionMode: sendInteractionMode,
6937 });
@@ -7278,7 +7285,7 @@ export default function ChatView(props: ChatViewProps) {
7285 const settingsResult = await persistThreadSettingsForNextTurn({
7286 threadId: threadIdForSend,
7287 createdAt: messageCreatedAt,
7281 - ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
7288 + ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
7289 runtimeMode,
7290 interactionMode: nextInteractionMode,
7291 });
@@ -7398,7 +7405,7 @@ export default function ChatView(props: ChatViewProps) {
7405 modelSelection: nextThreadModelSelection,
7406 runtimeMode: defaultRuntimeMode,
7407 interactionMode: "default",
7401 - branch: activeThreadBranch,
7408 + branch: checkoutBranchMismatch?.currentBranch ?? activeThreadBranch,
7409 worktreePath: activeThread.worktreePath,
7410 createdAt,
7411 },
@@ -8482,7 +8489,7 @@ export default function ChatView(props: ChatViewProps) {
8489
8490 <BranchRestoreConfirmHost
8491 threadKey={activeThreadKey}
8485 - threadBranch={localCheckoutBranchMismatch?.threadBranch ?? ""}
8492 + threadBranch={checkoutBranchMismatch?.threadBranch ?? ""}
8493 canWriteSourceControl={canWriteSourceControl}
8494 onConfirm={handleSwitchCheckoutToThreadStable}
8495 />
apps/web/src/components/Sidebar.tsx
+3 -3
@@ -233,7 +233,7 @@ import {
233 type SidebarSection,
234 resolveSidebarRowThreadId,
235 } from "./Sidebar.logic";
236 -import { resolveLocalCheckoutBranchMismatch } from "./BranchToolbar.logic";
236 +import { resolveCheckoutBranchMismatch } from "./BranchToolbar.logic";
237 import {
238 createSidebarCollisionDetection,
239 createSidebarSortingStrategy,
@@ -1735,7 +1735,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
1735 : null;
1736 const isWokeStatus = topStatus?.icon === "woke";
1737
1738 - const branchMismatch = resolveLocalCheckoutBranchMismatch({
1738 + const branchMismatch = resolveCheckoutBranchMismatch({
1739 effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
1740 activeWorktreePath: thread.worktreePath,
1741 activeThreadBranch: thread.branch,
@@ -2574,7 +2574,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
2574 JSON.stringify([thread.environmentId, gitCwd]),
2575 gitStatus.data,
2576 );
2577 - const branchMismatch = resolveLocalCheckoutBranchMismatch({
2577 + const branchMismatch = resolveCheckoutBranchMismatch({
2578 effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
2579 activeWorktreePath: thread.worktreePath,
2580 activeThreadBranch: thread.branch,