From 9914160b25bd30e89d0567be07835bad83ac7420 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 06:17:57 +0000 Subject: [PATCH 1/5] fix(web): threads whose worktree was removed can move to another checkout When a thread's worktree outside T3's managed directory is removed, git status for that path reports no repository, so the branch picker hid and listed refs from the deleted path. Turn start keeps failing with no way to rebind the thread. A thread checkout Git no longer recognises inside a project it still does now keeps the git controls, and the branch picker lists and checks out refs in the project checkout. Picking a ref rebinds the thread there through the existing metadata update, keeping its history. Co-Authored-By: Claude Opus 5.5 --- .../components/BranchToolbar.logic.test.ts | 25 +++++++++++++++++++ .../web/src/components/BranchToolbar.logic.ts | 12 +++++++++ .../BranchToolbarBranchSelector.tsx | 24 ++++++++++++------ apps/web/src/components/ChatView.tsx | 18 ++++++++++++- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.ts index 99ae0bda81bb..ededbffcf7a0 100644 --- a/apps/web/src/components/BranchToolbar.logic.test.ts +++ b/apps/web/src/components/BranchToolbar.logic.test.ts @@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test"; import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; import { deriveLocalBranchNameFromRemoteRef, + isThreadCheckoutMissing, isWorktreeChangeBlocked, resolveBranchSelectionTarget, resolveDraftEnvModeAfterBranchChange, @@ -281,6 +282,30 @@ describe("resolveBranchSelectionTarget", () => { }); }); +describe("isThreadCheckoutMissing", () => { + it.each([ + { worktreePath: "/external-wt", worktreeIsRepo: false, projectIsRepo: true, missing: true }, + { worktreePath: "/external-wt", worktreeIsRepo: true, projectIsRepo: true, missing: false }, + // Unknown until both statuses load, and a project that is not a repo has nowhere to move to. + { + worktreePath: "/external-wt", + worktreeIsRepo: undefined, + projectIsRepo: true, + missing: false, + }, + { + worktreePath: "/external-wt", + worktreeIsRepo: false, + projectIsRepo: undefined, + missing: false, + }, + { worktreePath: "/external-wt", worktreeIsRepo: false, projectIsRepo: false, missing: false }, + { worktreePath: null, worktreeIsRepo: false, projectIsRepo: true, missing: false }, + ])("$worktreePath, worktree repo $worktreeIsRepo, project repo $projectIsRepo", (input) => { + expect(isThreadCheckoutMissing(input)).toBe(input.missing); + }); +}); + describe("isWorktreeChangeBlocked", () => { const runtime = (status: "preparing" | "running" | "completed") => ({ status, diff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.ts index 9f2550b06499..9bdc96dbec25 100644 --- a/apps/web/src/components/BranchToolbar.logic.ts +++ b/apps/web/src/components/BranchToolbar.logic.ts @@ -83,6 +83,18 @@ export function shouldShowComposerContextStrip(input: { ); } +// A thread checkout Git no longer recognises inside a project it still does was removed, so git +// controls work from the project checkout and picking a ref there rebinds the thread. +export function isThreadCheckoutMissing(input: { + worktreePath: string | null; + worktreeIsRepo: boolean | undefined; + projectIsRepo: boolean | undefined; +}): boolean { + return ( + input.worktreePath !== null && input.worktreeIsRepo === false && input.projectIsRepo === true + ); +} + // Hysteresis prevents labels and icons from flapping at the width boundary. const CONTEXT_STRIP_COMPACT_EXPAND_HYSTERESIS_PX = 16; diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx index 591f7c5ab969..141440d942c5 100644 --- a/apps/web/src/components/BranchToolbarBranchSelector.tsx +++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx @@ -58,6 +58,7 @@ import { resolveBranchTriggerLabel, resolveBranchToolbarPrBranch, resolveBranchSelectionTarget, + isThreadCheckoutMissing, resolveBranchToolbarValue, resolveConfirmedSelectedRef, resolveDefaultWorktreeBaseBranch, @@ -174,7 +175,7 @@ export function BranchToolbarBranchSelector({ ? serverThread.worktreePath : (draftThread?.worktreePath ?? null); const activeProjectCwd = activeProject?.workspaceRoot ?? null; - const branchCwd = activeWorktreePath ?? activeProjectCwd; + const statusCwd = activeWorktreePath ?? activeProjectCwd; const hasServerThread = serverThread !== null; const canUpdateThreadBranch = !hasServerThread || canOperateThread; const canChangeThreadBranch = canWriteSourceControl && canUpdateThreadBranch; @@ -258,11 +259,11 @@ export function BranchToolbarBranchSelector({ const [branchQuery, setBranchQuery] = useState(""); const branchStatusQuery = useEnvironmentQuery( - branchCwd === null + statusCwd === null ? null : vcsEnvironment.status({ environmentId, - input: { cwd: branchCwd }, + input: { cwd: statusCwd }, }), ); const projectStatusQuery = useEnvironmentQuery( @@ -270,6 +271,15 @@ export function BranchToolbarBranchSelector({ ? null : vcsEnvironment.status({ environmentId, input: { cwd: activeProjectCwd } }), ); + const checkoutMissing = isThreadCheckoutMissing({ + worktreePath: activeWorktreePath, + worktreeIsRepo: branchStatusQuery.data?.isRepo, + projectIsRepo: projectStatusQuery.data?.isRepo, + }); + const branchCwd = checkoutMissing ? activeProjectCwd : statusCwd; + // Refs resolve against the checkout the thread can move to, not one that is gone. + const selectableWorktreePath = (worktreePath: string | null) => + checkoutMissing ? null : worktreePath; const activeProjectCheckoutRoot = projectStatusQuery.data?.workingTree.rootPath; const trimmedBranchQuery = branchQuery.trim(); const branchRefState = usePaginatedBranches( @@ -298,7 +308,7 @@ export function BranchToolbarBranchSelector({ : resolveBranchSelectionTarget({ activeProjectCwd, activeProjectCheckoutRoot, - activeWorktreePath, + activeWorktreePath: selectableWorktreePath(activeWorktreePath), refName, }).nextWorktreePath, }); @@ -482,7 +492,7 @@ export function BranchToolbarBranchSelector({ const selectionTarget = resolveBranchSelectionTarget({ activeProjectCwd, activeProjectCheckoutRoot, - activeWorktreePath: latest.worktreePath, + activeWorktreePath: selectableWorktreePath(latest.worktreePath), refName, }); @@ -589,7 +599,7 @@ export function BranchToolbarBranchSelector({ }); if (createBranchResult._tag === "Success") { const nextBranchName = createBranchResult.value.refName; - const applied = setThreadBranch(nextBranchName, activeWorktreePath); + const applied = setThreadBranch(nextBranchName, selectableWorktreePath(activeWorktreePath)); if (applied) { setBranchHold({ branch: nextBranchName, @@ -794,7 +804,7 @@ export function BranchToolbarBranchSelector({ !resolveBranchSelectionTarget({ activeProjectCwd, activeProjectCheckoutRoot, - activeWorktreePath, + activeWorktreePath: selectableWorktreePath(activeWorktreePath), refName, }).reuseExistingWorktree)) || isWorktreeChangeBlockedForRef(refName) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index a731a5798e19..e0273bac8c22 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -321,6 +321,7 @@ import { NoActiveThreadState } from "./NoActiveThreadState"; import { resolveEffectiveEnvMode, resolveLocalCheckoutBranchMismatch, + isThreadCheckoutMissing, shouldShowComposerContextStrip, shouldShowEnvironmentIndicator, } from "./BranchToolbar.logic"; @@ -1774,7 +1775,22 @@ export default function ChatView(props: ChatViewProps) { rememberCheckoutIsRepo(environmentId, gitStatusCwd, liveIsGitRepo); } }, [environmentId, gitStatusCwd, liveIsGitRepo]); - const isGitRepo = liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd) ?? true; + // A removed thread checkout keeps git controls so the thread can move to another checkout. + const projectGitStatus = useAtomValue( + liveIsGitRepo === false && activeThreadWorktreePath !== null && activeProjectCwd !== null + ? chatGitStatusAtom( + vcsEnvironment.status({ environmentId, input: { cwd: activeProjectCwd } }), + ) + : NO_CHAT_GIT_STATUS_ATOM, + ); + const threadCheckoutMissing = isThreadCheckoutMissing({ + worktreePath: activeThreadWorktreePath, + worktreeIsRepo: liveIsGitRepo, + projectIsRepo: projectGitStatus?.isRepo, + }); + const isGitRepo = + threadCheckoutMissing || + (liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd) ?? true); // Provider-native subagents cannot take messages; approvals and questions go to the top-level parent. const showProviderSubagentBar = isProviderSubagent; const composerMounted = !showProviderSubagentBar; From 6d07c6af6a21da5fc71ed0da6ce49217a3784044 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 06:35:03 +0000 Subject: [PATCH 2/5] fix(mobile): threads whose worktree was removed can move to another checkout Mobile disabled Branches & worktrees once the thread's checkout reported no repository, and its branch actions kept running in the deleted folder and writing its path back to the thread. The removed-checkout decision moves to client-runtime so web and mobile share it. Mobile's git sheets and actions resolve the checkout through useSelectedThreadGitCheckout, which falls back to the project checkout and no worktree, so picking a branch rebinds the thread there. Co-Authored-By: Claude Opus 5.5 --- .../git/GitBranchesSheet.occupancy.test.tsx | 23 +++++++++++-- .../threads/git/GitBranchesSheet.test.tsx | 3 +- .../features/threads/git/GitBranchesSheet.tsx | 4 +-- .../features/threads/git/GitOverviewSheet.tsx | 8 +++-- .../use-selected-thread-git-actions.test.ts | 3 +- .../state/use-selected-thread-git-actions.ts | 4 +-- .../src/state/use-selected-thread-worktree.ts | 32 +++++++++++++++++++ .../components/BranchToolbar.logic.test.ts | 25 --------------- .../web/src/components/BranchToolbar.logic.ts | 12 ------- .../BranchToolbarBranchSelector.tsx | 2 +- apps/web/src/components/ChatView.tsx | 2 +- packages/client-runtime/package.json | 4 +++ .../client-runtime/src/threadCheckout.test.ts | 27 ++++++++++++++++ packages/client-runtime/src/threadCheckout.ts | 11 +++++++ 14 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 packages/client-runtime/src/threadCheckout.test.ts create mode 100644 packages/client-runtime/src/threadCheckout.ts diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx index 436fb4dd0b40..f310315541db 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx @@ -5,6 +5,7 @@ import { expect, it, vi } from "vite-plus/test"; const checkout = vi.hoisted(() => ({ projectRoot: "", worktreePath: null as string | null, + removedPath: null as string | null, branches: [] as Array<{ name: string; worktreePath: string | null }>, })); @@ -51,8 +52,18 @@ vi.mock("../../../state/use-selected-thread-git-state", () => ({ vi.mock("../../../state/use-selected-thread-git-actions", () => ({ useSelectedThreadGitActions: () => ({ canChangeThreadBranch: true }), })); -vi.mock("../../../state/query", () => ({ useEnvironmentQuery: () => ({ data: null }) })); -vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { branch: () => null } })); +vi.mock("../../../state/query", () => ({ + useEnvironmentQuery: (query: { statusCwd?: string } | null) => ({ + data: + query?.statusCwd === undefined ? null : { isRepo: query.statusCwd !== checkout.removedPath }, + }), +})); +vi.mock("../../../state/vcs", () => ({ + vcsEnvironment: { + branch: () => null, + status: ({ input }: { input: { cwd: string } }) => ({ statusCwd: input.cwd }), + }, +})); import { GitBranchesSheet } from "./GitBranchesSheet"; @@ -96,6 +107,14 @@ it("recognizes both the main checkout and a linked worktree as this thread's che { name: "feature/linked", subtitle: "Checked out in this thread", disabled: false }, { name: "main", subtitle: "Checked out in another worktree", disabled: true }, ]); + + // A removed worktree leaves the project checkout as the one the thread can move to. + checkout.removedPath = linked; + expect(rows()).toEqual([ + { name: "feature/available", subtitle: "Local branch", disabled: false }, + { name: "feature/linked", subtitle: "Checked out in another worktree", disabled: true }, + { name: "main", subtitle: "Checked out in this thread", disabled: false }, + ]); } finally { act(() => root.unmount()); } diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx index fef45b5c8800..7386d98ac21d 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx @@ -42,9 +42,10 @@ vi.mock("../../../state/use-thread-selection", () => ({ }), })); vi.mock("../../../state/use-selected-thread-worktree", () => ({ - useSelectedThreadWorktree: () => ({ + useSelectedThreadGitCheckout: () => ({ selectedThreadCwd: "/repo", selectedThreadWorktreePath: null, + checkoutMissing: false, }), })); vi.mock("../../../state/use-selected-thread-git-state", () => ({ diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx index 7c610a445667..5b5f86ef020d 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx @@ -12,7 +12,7 @@ import { useEnvironmentQuery } from "../../../state/query"; import { useThreadSelection } from "../../../state/use-thread-selection"; import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions"; import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state"; -import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree"; +import { useSelectedThreadGitCheckout } from "../../../state/use-selected-thread-worktree"; import { vcsEnvironment } from "../../../state/vcs"; import { SheetActionButton } from "./gitSheetComponents"; @@ -25,7 +25,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { const gt = useGT(); const navigation = useNavigation(); const { selectedThread } = useThreadSelection(); - const { selectedThreadCwd } = useSelectedThreadWorktree(); + const { selectedThreadCwd } = useSelectedThreadGitCheckout(); const gitState = useSelectedThreadGitState(); const gitActions = useSelectedThreadGitActions(); const { canChangeThreadBranch } = gitActions; diff --git a/apps/mobile/src/features/threads/git/GitOverviewSheet.tsx b/apps/mobile/src/features/threads/git/GitOverviewSheet.tsx index f84ea9d0ccc2..53577bcebb61 100644 --- a/apps/mobile/src/features/threads/git/GitOverviewSheet.tsx +++ b/apps/mobile/src/features/threads/git/GitOverviewSheet.tsx @@ -33,7 +33,10 @@ import { useEnvironmentQuery } from "../../../state/query"; import { useThreadSelection } from "../../../state/use-thread-selection"; import { useSelectedThreadGitActions } from "../../../state/use-selected-thread-git-actions"; import { useSelectedThreadGitState } from "../../../state/use-selected-thread-git-state"; -import { useSelectedThreadWorktree } from "../../../state/use-selected-thread-worktree"; +import { + useSelectedThreadGitCheckout, + useSelectedThreadWorktree, +} from "../../../state/use-selected-thread-worktree"; import { vcsEnvironment } from "../../../state/vcs"; import { menuItemIconName, statusSummary } from "./gitSheetComponents"; import { @@ -81,6 +84,7 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) { context: selectedThread ?? undefined, }); const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree(); + const { checkoutMissing } = useSelectedThreadGitCheckout(); const linkedPrLines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime); const linkedPrActions = useLinkedPullRequestActions({ thread: selectedThread, @@ -331,7 +335,7 @@ function GitOverviewSheetContent(props: GitOverviewSheetProps) { ? gt("Switch branch, create branch, or move to a worktree") : gt("View branches and worktrees") } - disabled={busy || !isRepo} + disabled={busy || (!isRepo && !checkoutMissing)} onPress={() => presentRoute(() => navigation.navigate("GitBranches", { diff --git a/apps/mobile/src/state/use-selected-thread-git-actions.test.ts b/apps/mobile/src/state/use-selected-thread-git-actions.test.ts index 548ae2d3ce6b..d1f6f98ed40d 100644 --- a/apps/mobile/src/state/use-selected-thread-git-actions.test.ts +++ b/apps/mobile/src/state/use-selected-thread-git-actions.test.ts @@ -44,9 +44,10 @@ vi.mock("./use-thread-selection", () => ({ }), })); vi.mock("./use-selected-thread-worktree", () => ({ - useSelectedThreadWorktree: () => ({ + useSelectedThreadGitCheckout: () => ({ selectedThreadCwd: "/repo", selectedThreadWorktreePath: null, + checkoutMissing: false, }), })); vi.mock("./queries", () => ({ diff --git a/apps/mobile/src/state/use-selected-thread-git-actions.ts b/apps/mobile/src/state/use-selected-thread-git-actions.ts index 42336835cc44..1e8df5b3d48d 100644 --- a/apps/mobile/src/state/use-selected-thread-git-actions.ts +++ b/apps/mobile/src/state/use-selected-thread-git-actions.ts @@ -37,7 +37,7 @@ import { import { useAtomCommand } from "./use-atom-command"; import { showGitActionResult } from "./use-vcs-action-state"; import { useThreadSelection } from "./use-thread-selection"; -import { useSelectedThreadWorktree } from "./use-selected-thread-worktree"; +import { useSelectedThreadGitCheckout } from "./use-selected-thread-worktree"; function refreshFailureMessage(cause: Cause.Cause): string { const error = Cause.squash(cause); @@ -75,7 +75,7 @@ export function useSelectedThreadGitActions() { AuthOrchestrationOperateScope, ); const canChangeThreadBranch = canWriteSourceControl && canOperateThread; - const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree(); + const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadGitCheckout(); const runStackedAction = useAtomCommand( vcsActionManager.runStackedAction({ environmentId: selectedThread?.environmentId ?? null, diff --git a/apps/mobile/src/state/use-selected-thread-worktree.ts b/apps/mobile/src/state/use-selected-thread-worktree.ts index 74d45cc83f8c..72903be24102 100644 --- a/apps/mobile/src/state/use-selected-thread-worktree.ts +++ b/apps/mobile/src/state/use-selected-thread-worktree.ts @@ -1,5 +1,9 @@ +import { isThreadCheckoutMissing } from "@t3tools/client-runtime/thread-checkout"; + +import { useEnvironmentQuery } from "./query"; import { useSelectedThreadWorktreePath } from "./use-thread-detail"; import { useThreadSelection } from "./use-thread-selection"; +import { vcsEnvironment } from "./vcs"; import { resolvePreferredThreadWorktreePath } from "../features/terminal/terminalLaunchContext"; export function useSelectedThreadWorktree() { @@ -16,3 +20,31 @@ export function useSelectedThreadWorktree() { selectedThreadCwd: selectedThreadWorktreePath ?? selectedThreadProject?.workspaceRoot ?? null, }; } + +/** The checkout git actions use: the project checkout once the thread's own was removed. */ +export function useSelectedThreadGitCheckout() { + const { selectedThread, selectedThreadProject } = useThreadSelection(); + const worktree = useSelectedThreadWorktree(); + const environmentId = selectedThread?.environmentId ?? null; + const worktreePath = worktree.selectedThreadWorktreePath; + const projectCwd = selectedThreadProject?.workspaceRoot ?? null; + const worktreeStatus = useEnvironmentQuery( + environmentId !== null && worktreePath !== null + ? vcsEnvironment.status({ environmentId, input: { cwd: worktreePath } }) + : null, + ); + const worktreeIsRepo = worktreeStatus.data?.isRepo; + const projectStatus = useEnvironmentQuery( + environmentId !== null && worktreeIsRepo === false && projectCwd !== null + ? vcsEnvironment.status({ environmentId, input: { cwd: projectCwd } }) + : null, + ); + const checkoutMissing = isThreadCheckoutMissing({ + worktreePath, + worktreeIsRepo, + projectIsRepo: projectStatus.data?.isRepo, + }); + return checkoutMissing + ? { selectedThreadWorktreePath: null, selectedThreadCwd: projectCwd, checkoutMissing } + : { ...worktree, checkoutMissing }; +} diff --git a/apps/web/src/components/BranchToolbar.logic.test.ts b/apps/web/src/components/BranchToolbar.logic.test.ts index ededbffcf7a0..99ae0bda81bb 100644 --- a/apps/web/src/components/BranchToolbar.logic.test.ts +++ b/apps/web/src/components/BranchToolbar.logic.test.ts @@ -3,7 +3,6 @@ import { describe, expect, it } from "vite-plus/test"; import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; import { deriveLocalBranchNameFromRemoteRef, - isThreadCheckoutMissing, isWorktreeChangeBlocked, resolveBranchSelectionTarget, resolveDraftEnvModeAfterBranchChange, @@ -282,30 +281,6 @@ describe("resolveBranchSelectionTarget", () => { }); }); -describe("isThreadCheckoutMissing", () => { - it.each([ - { worktreePath: "/external-wt", worktreeIsRepo: false, projectIsRepo: true, missing: true }, - { worktreePath: "/external-wt", worktreeIsRepo: true, projectIsRepo: true, missing: false }, - // Unknown until both statuses load, and a project that is not a repo has nowhere to move to. - { - worktreePath: "/external-wt", - worktreeIsRepo: undefined, - projectIsRepo: true, - missing: false, - }, - { - worktreePath: "/external-wt", - worktreeIsRepo: false, - projectIsRepo: undefined, - missing: false, - }, - { worktreePath: "/external-wt", worktreeIsRepo: false, projectIsRepo: false, missing: false }, - { worktreePath: null, worktreeIsRepo: false, projectIsRepo: true, missing: false }, - ])("$worktreePath, worktree repo $worktreeIsRepo, project repo $projectIsRepo", (input) => { - expect(isThreadCheckoutMissing(input)).toBe(input.missing); - }); -}); - describe("isWorktreeChangeBlocked", () => { const runtime = (status: "preparing" | "running" | "completed") => ({ status, diff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.ts index 9bdc96dbec25..9f2550b06499 100644 --- a/apps/web/src/components/BranchToolbar.logic.ts +++ b/apps/web/src/components/BranchToolbar.logic.ts @@ -83,18 +83,6 @@ export function shouldShowComposerContextStrip(input: { ); } -// A thread checkout Git no longer recognises inside a project it still does was removed, so git -// controls work from the project checkout and picking a ref there rebinds the thread. -export function isThreadCheckoutMissing(input: { - worktreePath: string | null; - worktreeIsRepo: boolean | undefined; - projectIsRepo: boolean | undefined; -}): boolean { - return ( - input.worktreePath !== null && input.worktreeIsRepo === false && input.projectIsRepo === true - ); -} - // Hysteresis prevents labels and icons from flapping at the width boundary. const CONTEXT_STRIP_COMPACT_EXPAND_HYSTERESIS_PX = 16; diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx index 141440d942c5..66dd976caace 100644 --- a/apps/web/src/components/BranchToolbarBranchSelector.tsx +++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx @@ -1,3 +1,4 @@ +import { isThreadCheckoutMissing } from "@t3tools/client-runtime/thread-checkout"; import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; import { ThreadDetailsControl } from "./chat/ThreadDetailsControl"; import { ComposerContextLabel } from "./ComposerContextLabel"; @@ -58,7 +59,6 @@ import { resolveBranchTriggerLabel, resolveBranchToolbarPrBranch, resolveBranchSelectionTarget, - isThreadCheckoutMissing, resolveBranchToolbarValue, resolveConfirmedSelectedRef, resolveDefaultWorktreeBaseBranch, diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index e0273bac8c22..5a18520ade9e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1,3 +1,4 @@ +import { isThreadCheckoutMissing } from "@t3tools/client-runtime/thread-checkout"; import { m } from "../i18n/messages"; import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; import { useGT, useMessages } from "gt-react"; @@ -321,7 +322,6 @@ import { NoActiveThreadState } from "./NoActiveThreadState"; import { resolveEffectiveEnvMode, resolveLocalCheckoutBranchMismatch, - isThreadCheckoutMissing, shouldShowComposerContextStrip, shouldShowEnvironmentIndicator, } from "./BranchToolbar.logic"; diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index c09deef7e4b3..825159a27172 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -79,6 +79,10 @@ "types": "./src/worktreeSetup.ts", "default": "./src/worktreeSetup.ts" }, + "./thread-checkout": { + "types": "./src/threadCheckout.ts", + "default": "./src/threadCheckout.ts" + }, "./load-balancing": { "types": "./src/load-balancing.ts", "default": "./src/load-balancing.ts" diff --git a/packages/client-runtime/src/threadCheckout.test.ts b/packages/client-runtime/src/threadCheckout.test.ts new file mode 100644 index 000000000000..d8f70b5b309f --- /dev/null +++ b/packages/client-runtime/src/threadCheckout.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { isThreadCheckoutMissing } from "./threadCheckout.ts"; + +describe("isThreadCheckoutMissing", () => { + it.each([ + { worktreePath: "/external-wt", worktreeIsRepo: false, projectIsRepo: true, missing: true }, + { worktreePath: "/external-wt", worktreeIsRepo: true, projectIsRepo: true, missing: false }, + // Unknown until both statuses load, and a project that is not a repo has nowhere to move to. + { + worktreePath: "/external-wt", + worktreeIsRepo: undefined, + projectIsRepo: true, + missing: false, + }, + { + worktreePath: "/external-wt", + worktreeIsRepo: false, + projectIsRepo: undefined, + missing: false, + }, + { worktreePath: "/external-wt", worktreeIsRepo: false, projectIsRepo: false, missing: false }, + { worktreePath: null, worktreeIsRepo: false, projectIsRepo: true, missing: false }, + ])("$worktreePath, worktree repo $worktreeIsRepo, project repo $projectIsRepo", (input) => { + expect(isThreadCheckoutMissing(input)).toBe(input.missing); + }); +}); diff --git a/packages/client-runtime/src/threadCheckout.ts b/packages/client-runtime/src/threadCheckout.ts new file mode 100644 index 000000000000..92e419d7559d --- /dev/null +++ b/packages/client-runtime/src/threadCheckout.ts @@ -0,0 +1,11 @@ +// A thread checkout Git no longer recognises inside a project it still does was removed, so git +// controls work from the project checkout and picking a ref there rebinds the thread. +export function isThreadCheckoutMissing(input: { + worktreePath: string | null; + worktreeIsRepo: boolean | undefined; + projectIsRepo: boolean | undefined; +}): boolean { + return ( + input.worktreePath !== null && input.worktreeIsRepo === false && input.projectIsRepo === true + ); +} From 4b7f252459078cda35e514505a397b7c1156de34 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 06:49:19 +0000 Subject: [PATCH 3/5] fix(mobile): list branches from the project checkout once the thread's is removed Branches & worktrees read its branch list from useSelectedThreadGitState, which still listed refs in the deleted folder and so found none. It now resolves the checkout through useSelectedThreadGitCheckout like the sheet and its actions. Co-Authored-By: Claude Opus 5.5 --- .../git/GitBranchesSheet.occupancy.test.tsx | 16 +++++++++++----- .../state/use-selected-thread-git-state.test.tsx | 2 +- .../src/state/use-selected-thread-git-state.ts | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx index f310315541db..040af6f7c946 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.occupancy.test.tsx @@ -42,13 +42,19 @@ vi.mock("../../../state/use-thread-selection", () => ({ vi.mock("../../../state/use-thread-detail", () => ({ useSelectedThreadWorktreePath: () => checkout.worktreePath, })); -vi.mock("../../../state/use-selected-thread-git-state", () => ({ - useSelectedThreadGitState: () => ({ - selectedThreadBranches: checkout.branches, - selectedThreadBranchesLoading: false, - gitOperationLabel: null, +// A removed checkout is not a repository, so listing refs there finds none. +vi.mock("../../../state/queries", () => ({ + useBranches: ({ cwd }: { cwd: string | null }) => ({ + data: { refs: cwd === checkout.removedPath ? [] : checkout.branches }, + isPending: false, }), })); +vi.mock("../../../state/use-vcs-action-state", () => ({ + useVcsActionState: () => ({ currentLabel: null, hookName: null }), +})); +vi.mock("../../../state/sourceControl", () => ({ + sourceControlEnvironment: { discovery: () => null }, +})); vi.mock("../../../state/use-selected-thread-git-actions", () => ({ useSelectedThreadGitActions: () => ({ canChangeThreadBranch: true }), })); diff --git a/apps/mobile/src/state/use-selected-thread-git-state.test.tsx b/apps/mobile/src/state/use-selected-thread-git-state.test.tsx index 73c3b22d02ab..3318c981ff82 100644 --- a/apps/mobile/src/state/use-selected-thread-git-state.test.tsx +++ b/apps/mobile/src/state/use-selected-thread-git-state.test.tsx @@ -18,7 +18,7 @@ vi.mock("./use-thread-selection", () => ({ }), })); vi.mock("./use-selected-thread-worktree", () => ({ - useSelectedThreadWorktree: () => ({ selectedThreadCwd: harness.cwd }), + useSelectedThreadGitCheckout: () => ({ selectedThreadCwd: harness.cwd }), })); vi.mock("./use-vcs-action-state", () => ({ useVcsActionState: () => ({ currentLabel: null }), diff --git a/apps/mobile/src/state/use-selected-thread-git-state.ts b/apps/mobile/src/state/use-selected-thread-git-state.ts index b48429e8fe28..1c5ec0a5dcc5 100644 --- a/apps/mobile/src/state/use-selected-thread-git-state.ts +++ b/apps/mobile/src/state/use-selected-thread-git-state.ts @@ -9,12 +9,12 @@ import { useEnvironmentQuery } from "./query"; import { sourceControlEnvironment } from "./sourceControl"; import { useVcsActionState } from "./use-vcs-action-state"; import { useThreadSelection } from "./use-thread-selection"; -import { useSelectedThreadWorktree } from "./use-selected-thread-worktree"; +import { useSelectedThreadGitCheckout } from "./use-selected-thread-worktree"; export function useSelectedThreadGitState() { const messages = useMessages(); const { selectedThread } = useThreadSelection(); - const { selectedThreadCwd } = useSelectedThreadWorktree(); + const { selectedThreadCwd } = useSelectedThreadGitCheckout(); const selectedThreadGitTarget = { environmentId: selectedThread?.environmentId ?? null, From 0ccd6174710bd4610ac8e508dd065be4625bb73a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 07:24:21 +0000 Subject: [PATCH 4/5] fix(web): refuse a branch creation that cannot rebind a running thread Creating a branch from a removed checkout switched the project checkout before the worktree-change guard ran, so a running thread stayed bound to the missing folder. The guard now runs before the checkout changes. Co-Authored-By: Claude Opus 5.5 --- .../src/components/BranchToolbarBranchSelector.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx index 66dd976caace..1c2e761d5488 100644 --- a/apps/web/src/components/BranchToolbarBranchSelector.tsx +++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx @@ -581,6 +581,20 @@ export function BranchToolbarBranchSelector({ if (!canChangeThreadBranch) return; const name = sanitizeNewRefName(rawName); if (!branchCwd || !name || isBranchActionPending) return; + const latest = readLatestThreadState(); + // Refuse before switching the checkout, or a running thread stays bound to the missing one. + if ( + isWorktreeChangeBlocked({ + runtime: latest.runtime, + currentWorktreePath: latest.worktreePath, + nextWorktreePath: selectableWorktreePath(activeWorktreePath), + }) + ) { + toastManager.add( + stackedThreadToast({ type: "warning", title: messages(WORKTREE_CHANGE_BLOCKED_MESSAGE) }), + ); + return; + } latestSelectionIdRef.current++; setIsBranchMenuOpen(false); From ed3fd3b706d78755eb331d3012068453f8a7027b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 08:54:50 +0000 Subject: [PATCH 5/5] fix(web): hide git actions for a removed thread checkout until it is rebound The thread details panel offered 'Initialize Git' for the deleted folder next to the recovery branch picker. Git actions for that folder have nothing to act on, so the panel hides them until the thread is moved to another checkout. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/components/ChatView.tsx | 1 + .../src/components/chat/ThreadDetailsPanel.browser.test.tsx | 1 + apps/web/src/components/chat/ThreadDetailsPanel.tsx | 3 +++ 3 files changed, 5 insertions(+) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 5a18520ade9e..d65f474e8906 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3976,6 +3976,7 @@ export default function ChatView(props: ChatViewProps) { gitCwd={gitCwd} workspaceMutationId={workspaceMutationId} isGitRepo={isGitRepo} + threadCheckoutMissing={threadCheckoutMissing} envLocked={envLocked} availableEnvironments={logicalProjectEnvironments} autoEnvironmentLabel={autoEnvironmentLabel} diff --git a/apps/web/src/components/chat/ThreadDetailsPanel.browser.test.tsx b/apps/web/src/components/chat/ThreadDetailsPanel.browser.test.tsx index 52e5033d58d4..a888ab2c3faa 100644 --- a/apps/web/src/components/chat/ThreadDetailsPanel.browser.test.tsx +++ b/apps/web/src/components/chat/ThreadDetailsPanel.browser.test.tsx @@ -161,6 +161,7 @@ function Panel({ fixture }: { fixture: ReturnType }) { gitCwd={state.folder} workspaceMutationId={null} isGitRepo={false} + threadCheckoutMissing={false} envLocked availableEnvironments={[]} onEnvModeChange={noop} diff --git a/apps/web/src/components/chat/ThreadDetailsPanel.tsx b/apps/web/src/components/chat/ThreadDetailsPanel.tsx index d7c309074200..707376c1544e 100644 --- a/apps/web/src/components/chat/ThreadDetailsPanel.tsx +++ b/apps/web/src/components/chat/ThreadDetailsPanel.tsx @@ -70,6 +70,8 @@ interface ThreadDetailsPanelProps extends Pick< gitCwd: string | null; workspaceMutationId: string | null; isGitRepo: boolean; + // The thread's folder was removed: its git actions have nothing to act on until it is rebound. + threadCheckoutMissing: boolean; envLocked: boolean; availableEnvironments: readonly EnvironmentOption[]; autoEnvironmentLabel?: string | undefined; @@ -176,6 +178,7 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) { pullRequests: !(offersGit && props.isGitRepo) && visible.branch, git: offersGit && + !props.threadCheckoutMissing && props.activeProjectName !== undefined && (visible.gitActions || (visible.changes && props.isGitRepo)), };