TypeScript
revert: "fix(web): worktree threads warn when the checkout branch changes" (#14883) (#22225)
Theo Browne90c3f8350d77
diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.tsindex 5d68ff76c2d..33dd09d60c0 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,- resolveCheckoutBranchMismatch,+ resolveLocalCheckoutBranchMismatch,resolvePreviousWorktreeLabel,resolvePreviousWorktreeSeed,sanitizeNewRefName,@@ -301,10 +301,10 @@ describe("resolveBranchToolbarPrBranch", () => {});});-describe("resolveCheckoutBranchMismatch", () => {+describe("resolveLocalCheckoutBranchMismatch", () => {it("detects when a local thread is associated with a different branch than the checkout", () => {expect(- resolveCheckoutBranchMismatch({+ resolveLocalCheckoutBranchMismatch({effectiveEnvMode: "local",activeWorktreePath: null,activeThreadBranch: "feature/thread",@@ -313,13 +313,12 @@ describe("resolveCheckoutBranchMismatch", () => {).toEqual({threadBranch: "feature/thread",currentBranch: "feature/current",- canRestoreThreadBranch: true,});});it("ignores matching local checkout state", () => {expect(- resolveCheckoutBranchMismatch({+ resolveLocalCheckoutBranchMismatch({effectiveEnvMode: "local",activeWorktreePath: null,activeThreadBranch: "feature/thread",@@ -328,34 +327,20 @@ describe("resolveCheckoutBranchMismatch", () => {).toBeNull();});- 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",- };-+ it("ignores dedicated worktrees because their checkout is already thread-scoped", () => {expect(- resolveCheckoutBranchMismatch({- ...worktree,- currentGitBranch: "t3code/1a2b3c4d",+ resolveLocalCheckoutBranchMismatch({+ effectiveEnvMode: "worktree",+ activeWorktreePath: "/repo/.t3/worktrees/feature-thread",+ activeThreadBranch: "feature/thread",+ currentGitBranch: "feature/current",}),).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(- resolveCheckoutBranchMismatch({+ resolveLocalCheckoutBranchMismatch({effectiveEnvMode: "worktree",activeWorktreePath: null,activeThreadBranch: "feature/base",diff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.tsindex 13775843b64..55ac56e0996 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 { isTemporaryWorktreeBranch, sanitizeNewRefName } from "@t3tools/shared/git";+import { sanitizeNewRefName } from "@t3tools/shared/git";import { toSortableTimestamp } from "../lib/threadSort";export {dedupeRemoteBranchesWithLocalMatches,@@ -238,35 +238,20 @@ export function resolveBranchToolbarPrBranch(input: {return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;}-export function resolveCheckoutBranchMismatch(input: {+export function resolveLocalCheckoutBranchMismatch(input: {effectiveEnvMode: EnvMode;activeWorktreePath: string | null;activeThreadBranch: string | null;currentGitBranch: string | null;-}) {+}): { threadBranch: string; currentBranch: string } | null {const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;- const isLocalCheckout = effectiveEnvMode === "local" && activeWorktreePath === null;- const isEstablishedWorktree = effectiveEnvMode === "worktree" && activeWorktreePath !== null;- if (!isLocalCheckout && !isEstablishedWorktree) {+ if (effectiveEnvMode !== "local" || activeWorktreePath !== null) {return null;}if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {return null;}- // 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,- };+ return { threadBranch: activeThreadBranch, currentBranch: currentGitBranch };}export function resolveBranchSelectionTarget(input: {diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.tsindex 63d2fd1ca61..70f346df14f 100644--- a/apps/web/src/components/ChatView.logic.test.ts+++ b/apps/web/src/components/ChatView.logic.test.ts@@ -261,14 +261,14 @@ describe("resolveThreadMetadataUpdateForNextTurn", () => {model: "gpt-5.4",};- it("adopts the checkout branch without clearing the worktree attachment", () => {+ it("updates a stale local thread branch to the active checkout", () => {expect(resolveThreadMetadataUpdateForNextTurn({currentModelSelection: modelSelection,currentBranch: "feature/thread",nextBranch: "feature/checkout",}),- ).toEqual({ branch: "feature/checkout" });+ ).toEqual({ branch: "feature/checkout", worktreePath: null });});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.tsindex 03a29ceb740..c132c6689ff 100644--- a/apps/web/src/components/ChatView.logic.ts+++ b/apps/web/src/components/ChatView.logic.ts@@ -474,6 +474,7 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {}): {modelSelection?: ModelSelection;branch?: string;+ worktreePath?: null;} | null {const nextModelSelection = input.nextModelSelection;const modelSelectionChanged =@@ -488,7 +489,7 @@ export function resolveThreadMetadataUpdateForNextTurn(input: {}return {...(modelSelectionChanged ? { modelSelection: nextModelSelection } : {}),- ...(branchChanged ? { branch: input.nextBranch } : {}),+ ...(branchChanged ? { branch: input.nextBranch, worktreePath: null } : {}),};}diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 0122db67b06..8c12286a241 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -441,7 +441,7 @@ import { ThreadDetailsPanel } from "./chat/ThreadDetailsPanel";import { NoActiveThreadState } from "./NoActiveThreadState";import {resolveEffectiveEnvMode,- resolveCheckoutBranchMismatch,+ resolveLocalCheckoutBranchMismatch,shouldShowComposerContextStrip,shouldShowEnvironmentIndicator,} from "./BranchToolbar.logic";@@ -3456,6 +3456,26 @@ 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);@@ -4769,8 +4789,8 @@ export default function ChatView(props: ChatViewProps) {requestedEnvMode: envMode,isGitRepo,});- const checkoutBranchMismatch = isServerThread- ? resolveCheckoutBranchMismatch({+ const localCheckoutBranchMismatch = isServerThread+ ? resolveLocalCheckoutBranchMismatch({effectiveEnvMode: envMode,activeWorktreePath,activeThreadBranch,@@ -5099,10 +5119,10 @@ export default function ChatView(props: ChatViewProps) {const nowMinute = useNowMinute();const activeBranchMismatchKey = branchMismatchKey(activeThread?.id ?? null,- checkoutBranchMismatch,+ localCheckoutBranchMismatch,);const showBranchMismatchBanner = shouldShowBranchMismatchBanner({- hasMismatch: checkoutBranchMismatch !== null,+ hasMismatch: localCheckoutBranchMismatch !== null,isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),composerHasContent: composerHasDraftContent,wasShownForCurrentMismatch:@@ -5121,7 +5141,7 @@ export default function ChatView(props: ChatViewProps) {if (!activeProjectCwd ||!activeThread ||- !checkoutBranchMismatch?.canRestoreThreadBranch ||+ !localCheckoutBranchMismatch ||isRestoringThreadBranch) {return;@@ -5131,7 +5151,7 @@ export default function ChatView(props: ChatViewProps) {environmentId,input: {cwd: activeProjectCwd,- refName: checkoutBranchMismatch.threadBranch,+ refName: localCheckoutBranchMismatch.threadBranch,},});if (checkoutResult._tag === "Failure") {@@ -5152,7 +5172,7 @@ export default function ChatView(props: ChatViewProps) {setIsRestoringThreadBranch(false);return;}- const nextBranch = checkoutResult.value.refName ?? checkoutBranchMismatch.threadBranch;+ const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;if (nextBranch !== activeThread.branch) {const updateResult = await updateThreadMetadata({environmentId,@@ -5269,37 +5289,6 @@ 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();@@ -5328,7 +5317,7 @@ export default function ChatView(props: ChatViewProps) {const settingsResult = await persistThreadSettingsForNextTurn({threadId,createdAt,- ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),+ ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),runtimeMode,interactionMode: context.interactionMode,});@@ -5612,7 +5601,7 @@ export default function ChatView(props: ChatViewProps) {].filter((item) => item !== null);const usageLimitsItems = usageLimitsBanner === null ? [] : [usageLimitsBanner];const projectCloneItems = projectCloneBannerItem === null ? [] : [projectCloneBannerItem];- if (!checkoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {+ if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {return [...feedbackBannerItems,...limitRecoveryItems,@@ -5641,18 +5630,18 @@ export default function ChatView(props: ChatViewProps) {<TooltipTriggerrender={<code className="min-w-0 truncate font-medium text-foreground">- {checkoutBranchMismatch.threadBranch}+ {localCheckoutBranchMismatch.threadBranch}</code>}/><TooltipPopup side="top">- This thread last ran on {checkoutBranchMismatch.threadBranch}. Sending will continue- on {checkoutBranchMismatch.currentBranch}.+ This thread last ran on {localCheckoutBranchMismatch.threadBranch}. Sending will+ continue on {localCheckoutBranchMismatch.currentBranch}.</TooltipPopup></Tooltip></span>),- actions: checkoutBranchMismatch.canRestoreThreadBranch ? (+ actions: (<Buttonsize="xs"variant="ghost"@@ -5661,7 +5650,7 @@ export default function ChatView(props: ChatViewProps) {>{isRestoringThreadBranch ? "Restoring..." : "Restore branch"}</Button>- ) : null,+ ),dismissLabel: "Dismiss branch change notice",onDismiss: () => {dismissBranchMismatchForSession(activeBranchMismatchKey);@@ -6128,7 +6117,9 @@ export default function ChatView(props: ChatViewProps) {const settingsResult = await persistThreadSettingsForNextTurn({threadId,createdAt,- ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),+ ...(localCheckoutBranchMismatch+ ? { branch: localCheckoutBranchMismatch.currentBranch }+ : {}),runtimeMode,interactionMode,});@@ -7103,7 +7094,9 @@ export default function ChatView(props: ChatViewProps) {const settingsResult = await persistThreadSettingsForNextTurn({threadId: threadIdForSend,createdAt: messageCreatedAt,- ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),+ ...(localCheckoutBranchMismatch+ ? { branch: localCheckoutBranchMismatch.currentBranch }+ : {}),runtimeMode,interactionMode: sendInteractionMode,});@@ -7457,7 +7450,7 @@ export default function ChatView(props: ChatViewProps) {const settingsResult = await persistThreadSettingsForNextTurn({threadId: threadIdForSend,createdAt: messageCreatedAt,- ...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),+ ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),runtimeMode,interactionMode: nextInteractionMode,});@@ -7577,7 +7570,7 @@ export default function ChatView(props: ChatViewProps) {modelSelection: nextThreadModelSelection,runtimeMode: defaultRuntimeMode,interactionMode: "default",- branch: checkoutBranchMismatch?.currentBranch ?? activeThreadBranch,+ branch: activeThreadBranch,worktreePath: activeThread.worktreePath,createdAt,},@@ -8669,7 +8662,7 @@ export default function ChatView(props: ChatViewProps) {<BranchRestoreConfirmHostthreadKey={activeThreadKey}- threadBranch={checkoutBranchMismatch?.threadBranch ?? ""}+ threadBranch={localCheckoutBranchMismatch?.threadBranch ?? ""}canWriteSourceControl={canWriteSourceControl}onConfirm={handleSwitchCheckoutToThreadStable}/>diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsxindex b2c355bda42..22910c76d5f 100644--- a/apps/web/src/components/Sidebar.tsx+++ b/apps/web/src/components/Sidebar.tsx@@ -234,7 +234,7 @@ import {type SidebarSection,resolveSidebarRowThreadId,} from "./Sidebar.logic";-import { resolveCheckoutBranchMismatch } from "./BranchToolbar.logic";+import { resolveLocalCheckoutBranchMismatch } from "./BranchToolbar.logic";import {createSidebarCollisionDetection,createSidebarSortingStrategy,@@ -1736,7 +1736,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP: null;const isWokeStatus = topStatus?.icon === "woke";- const branchMismatch = resolveCheckoutBranchMismatch({+ const branchMismatch = resolveLocalCheckoutBranchMismatch({effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",activeWorktreePath: thread.worktreePath,activeThreadBranch: thread.branch,@@ -2573,7 +2573,7 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {JSON.stringify([thread.environmentId, gitCwd]),gitStatus.data,);- const branchMismatch = resolveCheckoutBranchMismatch({+ const branchMismatch = resolveLocalCheckoutBranchMismatch({effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",activeWorktreePath: thread.worktreePath,activeThreadBranch: thread.branch,