Skip to content
TypeScript

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

Tristan Knightdaebb8b506f1
diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.ts
index 088722b4996..8885f6e45eb 100644
--- a/apps/web/src/components/BranchToolbar.logic.test.ts
+++ b/apps/web/src/components/BranchToolbar.logic.test.ts
@@ -13,7 +13,7 @@ import {
resolveBranchToolbarPrBranch,
resolveBranchToolbarValue,
resolveLockedWorkspaceLabel,
- resolveLocalCheckoutBranchMismatch,
+ resolveCheckoutBranchMismatch,
resolvePreviousWorktreeLabel,
resolvePreviousWorktreeSeed,
sanitizeNewRefName,
@@ -301,10 +301,10 @@ describe("resolveBranchToolbarPrBranch", () => {
});
});
-describe("resolveLocalCheckoutBranchMismatch", () => {
+describe("resolveCheckoutBranchMismatch", () => {
it("detects when a local thread is associated with a different branch than the checkout", () => {
expect(
- resolveLocalCheckoutBranchMismatch({
+ resolveCheckoutBranchMismatch({
effectiveEnvMode: "local",
activeWorktreePath: null,
activeThreadBranch: "feature/thread",
@@ -313,12 +313,13 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
).toEqual({
threadBranch: "feature/thread",
currentBranch: "feature/current",
+ canRestoreThreadBranch: true,
});
});
it("ignores matching local checkout state", () => {
expect(
- resolveLocalCheckoutBranchMismatch({
+ resolveCheckoutBranchMismatch({
effectiveEnvMode: "local",
activeWorktreePath: null,
activeThreadBranch: "feature/thread",
@@ -327,20 +328,34 @@ describe("resolveLocalCheckoutBranchMismatch", () => {
).toBeNull();
});
- it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {
+ it("warns about worktree branch drift without restore, but ignores a pending rename status", () => {
+ const worktree = {
+ effectiveEnvMode: "worktree" as const,
+ activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
+ activeThreadBranch: "t3code/fix-sidebar",
+ };
+
expect(
- resolveLocalCheckoutBranchMismatch({
- effectiveEnvMode: "worktree",
- activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
- activeThreadBranch: "feature/thread",
- currentGitBranch: "feature/current",
+ resolveCheckoutBranchMismatch({
+ ...worktree,
+ currentGitBranch: "t3code/1a2b3c4d",
}),
).toBeNull();
+ expect(
+ resolveCheckoutBranchMismatch({
+ ...worktree,
+ currentGitBranch: "feature/checkout",
+ }),
+ ).toEqual({
+ threadBranch: worktree.activeThreadBranch,
+ currentBranch: "feature/checkout",
+ canRestoreThreadBranch: false,
+ });
});
it("ignores new-worktree base selection before a worktree exists", () => {
expect(
- resolveLocalCheckoutBranchMismatch({
+ resolveCheckoutBranchMismatch({
effectiveEnvMode: "worktree",
activeWorktreePath: null,
activeThreadBranch: "feature/base",
diff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.ts
index f83223f0ce3..4c644e71d10 100644
--- a/apps/web/src/components/BranchToolbar.logic.ts
+++ b/apps/web/src/components/BranchToolbar.logic.ts
@@ -6,7 +6,7 @@ import type {
WorktreeSubmodules,
} from "@t3tools/contracts";
import * as Schema from "effect/Schema";
-import { sanitizeNewRefName } from "@t3tools/shared/git";
+import { isTemporaryWorktreeBranch, sanitizeNewRefName } from "@t3tools/shared/git";
import { toSortableTimestamp } from "../lib/threadSort";
export {
dedupeRemoteBranchesWithLocalMatches,
@@ -231,20 +231,35 @@ export function resolveBranchToolbarPrBranch(input: {
return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;
}
-export function resolveLocalCheckoutBranchMismatch(input: {
+export function resolveCheckoutBranchMismatch(input: {
effectiveEnvMode: EnvMode;
activeWorktreePath: string | null;
activeThreadBranch: string | null;
currentGitBranch: string | null;
-}): { threadBranch: string; currentBranch: string } | null {
+}) {
const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
- if (effectiveEnvMode !== "local" || activeWorktreePath !== null) {
+ const isLocalCheckout = effectiveEnvMode === "local" && activeWorktreePath === null;
+ const isEstablishedWorktree = effectiveEnvMode === "worktree" && activeWorktreePath !== null;
+ if (!isLocalCheckout && !isEstablishedWorktree) {
return null;
}
if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {
return null;
}
- return { threadBranch: activeThreadBranch, currentBranch: currentGitBranch };
+ // Cached status can predate the automatic rename of a temporary worktree branch.
+ if (
+ isEstablishedWorktree &&
+ !isTemporaryWorktreeBranch(activeThreadBranch) &&
+ isTemporaryWorktreeBranch(currentGitBranch)
+ ) {
+ return null;
+ }
+ return {
+ threadBranch: activeThreadBranch,
+ currentBranch: currentGitBranch,
+ // Restoring a worktree checkout could switch the branch for sibling threads.
+ canRestoreThreadBranch: isLocalCheckout,
+ };
}
export function resolveBranchSelectionTarget(input: {
diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts
index ab1e9b7adc5..330ab771c3f 100644
--- a/apps/web/src/components/ChatView.logic.test.ts
+++ b/apps/web/src/components/ChatView.logic.test.ts
@@ -258,14 +258,14 @@ describe("resolveThreadMetadataUpdateForNextTurn", () => {
model: "gpt-5.4",
};
- it("updates a stale local thread branch to the active checkout", () => {
+ it("adopts the checkout branch without clearing the worktree attachment", () => {
expect(
resolveThreadMetadataUpdateForNextTurn({
currentModelSelection: modelSelection,
currentBranch: "feature/thread",
nextBranch: "feature/checkout",
}),
- ).toEqual({ branch: "feature/checkout", worktreePath: null });
+ ).toEqual({ branch: "feature/checkout" });
});
it("does not write metadata when the model and branch are unchanged", () => {
diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts
index 567ff7cc163..65807d965af 100644
--- a/apps/web/src/components/ChatView.logic.ts
+++ b/apps/web/src/components/ChatView.logic.ts
@@ -485,7 +485,6 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
}): {
modelSelection?: ModelSelection;
branch?: string;
- worktreePath?: null;
} | null {
const nextModelSelection = input.nextModelSelection;
const modelSelectionChanged =
@@ -500,7 +499,7 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {
}
return {
...(modelSelectionChanged ? { modelSelection: nextModelSelection } : {}),
- ...(branchChanged ? { branch: input.nextBranch, worktreePath: null } : {}),
+ ...(branchChanged ? { branch: input.nextBranch } : {}),
};
}
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 0e1b1d30a7e..5821766aa9a 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -433,7 +433,7 @@ import { ThreadDetailsPanel } from "./chat/ThreadDetailsPanel";
import { NoActiveThreadState } from "./NoActiveThreadState";
import {
resolveEffectiveEnvMode,
- resolveLocalCheckoutBranchMismatch,
+ resolveCheckoutBranchMismatch,
shouldShowComposerContextStrip,
shouldShowEnvironmentIndicator,
} from "./BranchToolbar.logic";
@@ -3376,26 +3376,6 @@ export default function ChatView(props: ChatViewProps) {
scheduleComposerFocus();
};
const insertArtifactTemplateStable = useStableCallback(insertArtifactTemplate);
- const sendAppMessage = async (text: string) => {
- if (!isServerThread || activeThreadId === null) {
- throw new Error("Messages from apps need a started thread.");
- }
- const result = await startThreadTurn({
- environmentId,
- input: {
- threadId: activeThreadId,
- message: { messageId: newMessageId(), role: "user", text, attachments: [] },
- runtimeMode,
- interactionMode,
- dispatchMode: "queue",
- },
- });
- if (result._tag === "Failure") {
- const error = squashAtomCommandFailure(result);
- throw error instanceof Error ? error : new Error("Could not send the app's message.");
- }
- };
- const sendAppMessageStable = useStableCallback(sendAppMessage);
const queuedRunsControlRef = useRef<QueuedRunsControlHandle>(null);
const addTerminalContextToDraft = (selection: TerminalContextSelection) => {
composerRef.current?.addTerminalContext(selection);
@@ -4685,8 +4665,8 @@ export default function ChatView(props: ChatViewProps) {
requestedEnvMode: envMode,
isGitRepo,
});
- const localCheckoutBranchMismatch = isServerThread
- ? resolveLocalCheckoutBranchMismatch({
+ const checkoutBranchMismatch = isServerThread
+ ? resolveCheckoutBranchMismatch({
effectiveEnvMode: envMode,
activeWorktreePath,
activeThreadBranch,
@@ -4966,10 +4946,10 @@ export default function ChatView(props: ChatViewProps) {
const nowMinute = useNowMinute();
const activeBranchMismatchKey = branchMismatchKey(
activeThread?.id ?? null,
- localCheckoutBranchMismatch,
+ checkoutBranchMismatch,
);
const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
- hasMismatch: localCheckoutBranchMismatch !== null,
+ hasMismatch: checkoutBranchMismatch !== null,
isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
composerHasContent: composerHasDraftContent,
wasShownForCurrentMismatch:
@@ -4988,7 +4968,7 @@ export default function ChatView(props: ChatViewProps) {
if (
!activeProjectCwd ||
!activeThread ||
- !localCheckoutBranchMismatch ||
+ !checkoutBranchMismatch?.canRestoreThreadBranch ||
isRestoringThreadBranch
) {
return;
@@ -4998,7 +4978,7 @@ export default function ChatView(props: ChatViewProps) {
environmentId,
input: {
cwd: activeProjectCwd,
- refName: localCheckoutBranchMismatch.threadBranch,
+ refName: checkoutBranchMismatch.threadBranch,
},
});
if (checkoutResult._tag === "Failure") {
@@ -5019,7 +4999,7 @@ export default function ChatView(props: ChatViewProps) {
setIsRestoringThreadBranch(false);
return;
}
- const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;
+ const nextBranch = checkoutResult.value.refName ?? checkoutBranchMismatch.threadBranch;
if (nextBranch !== activeThread.branch) {
const updateResult = await updateThreadMetadata({
environmentId,
@@ -5114,6 +5094,37 @@ export default function ChatView(props: ChatViewProps) {
};
})() as ComposerBannerStackItem | null;
+ const sendAppMessage = async (text: string) => {
+ if (!isServerThread || activeThreadId === null) {
+ throw new Error("Messages from apps need a started thread.");
+ }
+ const settingsResult = await persistThreadSettingsForNextTurn({
+ threadId: activeThreadId,
+ createdAt: new Date().toISOString(),
+ ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
+ runtimeMode,
+ interactionMode,
+ });
+ const result =
+ settingsResult._tag === "Failure"
+ ? settingsResult
+ : await startThreadTurn({
+ environmentId,
+ input: {
+ threadId: activeThreadId,
+ message: { messageId: newMessageId(), role: "user", text, attachments: [] },
+ runtimeMode,
+ interactionMode,
+ dispatchMode: "queue",
+ },
+ });
+ if (result._tag === "Failure") {
+ const error = squashAtomCommandFailure(result);
+ throw error instanceof Error ? error : new Error("Could not send the app's message.");
+ }
+ };
+ const sendAppMessageStable = useStableCallback(sendAppMessage);
+
const sendStandaloneCommand = async (text: string, failureMessage: string) => {
if (!activeThread || !clientSettingsHydrated || sendInFlightRef.current) return;
const context = composerRef.current?.getSendContext();
@@ -5142,7 +5153,7 @@ export default function ChatView(props: ChatViewProps) {
const settingsResult = await persistThreadSettingsForNextTurn({
threadId,
createdAt,
- ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
+ ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
runtimeMode,
interactionMode: context.interactionMode,
});
@@ -5418,7 +5429,7 @@ export default function ChatView(props: ChatViewProps) {
].filter((item) => item !== null);
const usageLimitsItems = usageLimitsBanner === null ? [] : [usageLimitsBanner];
const projectCloneItems = projectCloneBannerItem === null ? [] : [projectCloneBannerItem];
- if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
+ if (!checkoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
return [
...feedbackBannerItems,
...limitRecoveryItems,
@@ -5447,18 +5458,18 @@ export default function ChatView(props: ChatViewProps) {
<TooltipTrigger
render={
<code className="min-w-0 truncate font-medium text-foreground">
- {localCheckoutBranchMismatch.threadBranch}
+ {checkoutBranchMismatch.threadBranch}
</code>
}
/>
<TooltipPopup side="top">
- This thread last ran on {localCheckoutBranchMismatch.threadBranch}. Sending will
- continue on {localCheckoutBranchMismatch.currentBranch}.
+ This thread last ran on {checkoutBranchMismatch.threadBranch}. Sending will continue
+ on {checkoutBranchMismatch.currentBranch}.
</TooltipPopup>
</Tooltip>
</span>
),
- actions: (
+ actions: checkoutBranchMismatch.canRestoreThreadBranch ? (
<Button
size="xs"
variant="ghost"
@@ -5467,7 +5478,7 @@ export default function ChatView(props: ChatViewProps) {
>
{isRestoringThreadBranch ? "Restoring..." : "Restore branch"}
</Button>
- ),
+ ) : null,
dismissLabel: "Dismiss branch change notice",
onDismiss: () => {
dismissBranchMismatchForSession(activeBranchMismatchKey);
@@ -5945,9 +5956,7 @@ export default function ChatView(props: ChatViewProps) {
const settingsResult = await persistThreadSettingsForNextTurn({
threadId,
createdAt,
- ...(localCheckoutBranchMismatch
- ? { branch: localCheckoutBranchMismatch.currentBranch }
- : {}),
+ ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
runtimeMode,
interactionMode,
});
@@ -6922,9 +6931,7 @@ export default function ChatView(props: ChatViewProps) {
const settingsResult = await persistThreadSettingsForNextTurn({
threadId: threadIdForSend,
createdAt: messageCreatedAt,
- ...(localCheckoutBranchMismatch
- ? { branch: localCheckoutBranchMismatch.currentBranch }
- : {}),
+ ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
runtimeMode,
interactionMode: sendInteractionMode,
});
@@ -7278,7 +7285,7 @@ export default function ChatView(props: ChatViewProps) {
const settingsResult = await persistThreadSettingsForNextTurn({
threadId: threadIdForSend,
createdAt: messageCreatedAt,
- ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
+ ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
runtimeMode,
interactionMode: nextInteractionMode,
});
@@ -7398,7 +7405,7 @@ export default function ChatView(props: ChatViewProps) {
modelSelection: nextThreadModelSelection,
runtimeMode: defaultRuntimeMode,
interactionMode: "default",
- branch: activeThreadBranch,
+ branch: checkoutBranchMismatch?.currentBranch ?? activeThreadBranch,
worktreePath: activeThread.worktreePath,
createdAt,
},
@@ -8482,7 +8489,7 @@ export default function ChatView(props: ChatViewProps) {
<BranchRestoreConfirmHost
threadKey={activeThreadKey}
- threadBranch={localCheckoutBranchMismatch?.threadBranch ?? ""}
+ threadBranch={checkoutBranchMismatch?.threadBranch ?? ""}
canWriteSourceControl={canWriteSourceControl}
onConfirm={handleSwitchCheckoutToThreadStable}
/>
diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx
index 0d743bb2dc5..9a54f3a99e7 100644
--- a/apps/web/src/components/Sidebar.tsx
+++ b/apps/web/src/components/Sidebar.tsx
@@ -233,7 +233,7 @@ import {
type SidebarSection,
resolveSidebarRowThreadId,
} from "./Sidebar.logic";
-import { resolveLocalCheckoutBranchMismatch } from "./BranchToolbar.logic";
+import { resolveCheckoutBranchMismatch } from "./BranchToolbar.logic";
import {
createSidebarCollisionDetection,
createSidebarSortingStrategy,
@@ -1735,7 +1735,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
: null;
const isWokeStatus = topStatus?.icon === "woke";
- const branchMismatch = resolveLocalCheckoutBranchMismatch({
+ const branchMismatch = resolveCheckoutBranchMismatch({
effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
activeWorktreePath: thread.worktreePath,
activeThreadBranch: thread.branch,
@@ -2574,7 +2574,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
JSON.stringify([thread.environmentId, gitCwd]),
gitStatus.data,
);
- const branchMismatch = resolveLocalCheckoutBranchMismatch({
+ const branchMismatch = resolveCheckoutBranchMismatch({
effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
activeWorktreePath: thread.worktreePath,
activeThreadBranch: thread.branch,