Skip to content

Commit f827e4a

Browse files
ScottN-PVclaude
andcommitted
fix(web): branch picker no longer stops a running turn
Moving a thread to another worktree detaches its provider session, and on V2 the server does that itself for any worktree change. Picking a ref from another worktree mid-run therefore interrupted the run with no warning. While Stop is offered for the thread, the picker disables refs that would move it to another worktree and shows why. Enter and a run that starts during a checkout are rechecked against the latest thread state. Refs #14245 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 43bd667 commit f827e4a

4 files changed

Lines changed: 161 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function BranchPicker({
4343
isFetchingNextPage,
4444
onLoadNext,
4545
statusText,
46+
notice,
47+
extraData,
4648
originControl,
4749
popupProps,
4850
renderItem,
@@ -62,6 +64,9 @@ export function BranchPicker({
6264
isFetchingNextPage: boolean;
6365
onLoadNext: () => void;
6466
statusText: string | null;
67+
notice?: string | null | undefined;
68+
/** Rows re-render only when their item or this value changes. */
69+
extraData?: unknown;
6570
originControl?: { checked: boolean; onCheckedChange: (checked: boolean) => void } | undefined;
6671
popupProps: Omit<ComponentProps<typeof ComboboxPopup>, "children">;
6772
renderItem: (value: string, index: number) => ReactNode;
@@ -203,6 +208,7 @@ export function BranchPicker({
203208
keyExtractor={(item) => item}
204209
{...(getItemType ? { getItemType } : {})}
205210
renderItem={({ item, index }) => renderItem(item, index)}
211+
extraData={extraData}
206212
estimatedItemSize={28}
207213
drawDistance={336}
208214
onLayout={() => {
@@ -252,6 +258,7 @@ export function BranchPicker({
252258
</TooltipPopup>
253259
</Tooltip>
254260
) : null}
261+
{notice ? <ComboboxStatus>{notice}</ComboboxStatus> : null}
255262
{statusText ? <ComboboxStatus>{statusText}</ComboboxStatus> : null}
256263
</div>
257264
</ComboboxPopup>
@@ -264,13 +271,15 @@ export function BranchPickerRefItem({
264271
projectCwd: activeProjectCwd,
265272
index,
266273
value,
274+
disabled,
267275
onClick,
268276
onContextMenu,
269277
}: {
270278
branch: VcsRef;
271279
projectCwd: string | null;
272280
index: number;
273281
value?: string;
282+
disabled?: boolean | undefined;
274283
onClick: ComponentProps<typeof ComboboxItem>["onClick"];
275284
onContextMenu?: ComponentProps<typeof ComboboxItem>["onContextMenu"];
276285
}) {
@@ -292,6 +301,7 @@ export function BranchPickerRefItem({
292301
key={itemValue}
293302
index={index}
294303
value={value ?? itemValue}
304+
disabled={disabled}
295305
onClick={onClick}
296306
onContextMenu={onContextMenu}
297307
>

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

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { EnvironmentId, type VcsRef } from "@t3tools/contracts";
1+
import type { ThreadRuntimeSummary } from "@t3tools/client-runtime/state/shell";
2+
import { EnvironmentId, ProviderInstanceId, RunId, type VcsRef } from "@t3tools/contracts";
23
import { describe, expect, it } from "vite-plus/test";
34
import {
45
dedupeRemoteBranchesWithLocalMatches,
56
deriveLocalBranchNameFromRemoteRef,
7+
isWorktreeChangeBlocked,
68
resolveEnvironmentOptionLabel,
79
resolveBranchSelectionTarget,
810
resolveCurrentWorkspaceLabel,
@@ -896,3 +898,88 @@ describe("sanitizeNewRefName", () => {
896898
expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
897899
});
898900
});
901+
902+
describe("isWorktreeChangeBlocked", () => {
903+
const runtime = (
904+
status: ThreadRuntimeSummary["status"],
905+
activeRunId: string | null = "run-1",
906+
): ThreadRuntimeSummary => ({
907+
status,
908+
activeRunId: activeRunId === null ? null : RunId.make(activeRunId),
909+
providerInstanceId: ProviderInstanceId.make("codex"),
910+
providerName: null,
911+
lastError: null,
912+
updatedAt: "2026-10-02T00:00:00.000Z",
913+
});
914+
const secondaryWorktree = "/repo/.t3/worktrees/feature-a";
915+
916+
it.each(["preparing", "starting", "running", "waiting"] as const)(
917+
"blocks a worktree change while the run is %s",
918+
(status) => {
919+
expect(
920+
isWorktreeChangeBlocked({
921+
runtime: runtime(status),
922+
currentWorktreePath: null,
923+
nextWorktreePath: secondaryWorktree,
924+
}),
925+
).toBe(true);
926+
},
927+
);
928+
929+
it("blocks a worktree change while a queued run waits on an attached run", () => {
930+
expect(
931+
isWorktreeChangeBlocked({
932+
runtime: runtime("queued"),
933+
currentWorktreePath: null,
934+
nextWorktreePath: secondaryWorktree,
935+
}),
936+
).toBe(true);
937+
});
938+
939+
it.each([
940+
["the thread has no runtime", null],
941+
["the thread is idle", runtime("idle", null)],
942+
["the last run completed", runtime("completed", null)],
943+
["the last run was interrupted", runtime("interrupted", null)],
944+
["only a queued run remains", runtime("queued", null)],
945+
])("allows a worktree change when %s", (_label, threadRuntime) => {
946+
expect(
947+
isWorktreeChangeBlocked({
948+
runtime: threadRuntime,
949+
currentWorktreePath: null,
950+
nextWorktreePath: secondaryWorktree,
951+
}),
952+
).toBe(false);
953+
});
954+
955+
it.each([
956+
["the main checkout", null],
957+
["a secondary worktree", secondaryWorktree],
958+
])("allows staying in %s mid-run", (_label, worktreePath) => {
959+
expect(
960+
isWorktreeChangeBlocked({
961+
runtime: runtime("running"),
962+
currentWorktreePath: worktreePath,
963+
nextWorktreePath: worktreePath,
964+
}),
965+
).toBe(false);
966+
});
967+
968+
it.each([
969+
["a ref checked out in the main repo", { isDefault: false, worktreePath: "/repo" }],
970+
["the default ref with no checkout", { isDefault: true, worktreePath: null }],
971+
])("blocks returning to the main checkout via %s mid-run", (_label, refName) => {
972+
const { nextWorktreePath } = resolveBranchSelectionTarget({
973+
activeProjectCwd: "/repo",
974+
activeWorktreePath: secondaryWorktree,
975+
refName,
976+
});
977+
expect(
978+
isWorktreeChangeBlocked({
979+
runtime: runtime("running"),
980+
currentWorktreePath: secondaryWorktree,
981+
nextWorktreePath,
982+
}),
983+
).toBe(true);
984+
});
985+
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import type {
55
ProjectId,
66
WorktreeSubmodules,
77
} from "@t3tools/contracts";
8+
import type { ThreadRuntimeSummary } from "@t3tools/client-runtime/state/shell";
89
import * as Schema from "effect/Schema";
910
import { sanitizeNewRefName } from "@t3tools/shared/git";
1011
import { toSortableTimestamp } from "../lib/threadSort";
12+
import { deriveCanInterruptRunningThread } from "../session-logic";
1113
export {
1214
dedupeRemoteBranchesWithLocalMatches,
1315
deriveLocalBranchNameFromRemoteRef,
@@ -298,6 +300,19 @@ export function resolveBranchSelectionTarget(input: {
298300
};
299301
}
300302

303+
// The server detaches the provider session when a thread's worktree changes, which
304+
// interrupts the run in progress. Block that while the user could still Stop the run.
305+
export function isWorktreeChangeBlocked(input: {
306+
runtime: ThreadRuntimeSummary | null;
307+
currentWorktreePath: string | null;
308+
nextWorktreePath: string | null;
309+
}): boolean {
310+
return (
311+
input.nextWorktreePath !== input.currentWorktreePath &&
312+
deriveCanInterruptRunningThread(true, input.runtime)
313+
);
314+
}
315+
301316
export function shouldIncludeBranchPickerItem(input: {
302317
itemValue: string;
303318
normalizedQuery: string;

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
2828
import { readLocalApi } from "../localApi";
2929
import { useOpenPrLink } from "../lib/openPullRequestLink";
3030
import { usePaginatedBranches } from "../state/queries";
31-
import { useProject, useThreadShell } from "../state/entities";
31+
import { readThreadShell, useProject, useThreadShell } from "../state/entities";
3232
import { useEnvironmentQuery } from "../state/query";
3333
import { threadEnvironment } from "../state/threads";
3434
import { useAtomCommand } from "../state/use-atom-command";
@@ -44,6 +44,7 @@ import { getSourceControlPresentation } from "../sourceControlPresentation";
4444
import { useComposerMenuProps } from "./chat/composerEventScope";
4545
import {
4646
deriveLocalBranchNameFromRemoteRef,
47+
isWorktreeChangeBlocked,
4748
resolveBranchTriggerLabel,
4849
resolveBranchToolbarPrBranch,
4950
resolveBranchSelectionTarget,
@@ -88,6 +89,8 @@ interface BranchToolbarBranchSelectorProps {
8889
onComposerFocusRequest?: () => void;
8990
}
9091

92+
const WORKTREE_CHANGE_BLOCKED_MESSAGE = "Stop the current turn to switch worktrees.";
93+
9194
function toBranchActionErrorMessage(error: unknown): string {
9295
return error instanceof Error ? error.message : "An error occurred.";
9396
}
@@ -272,6 +275,18 @@ export function BranchToolbarBranchSelector({
272275
const prReference = parsePullRequestReference(trimmedBranchQuery);
273276
const isSelectingWorktreeBase =
274277
effectiveEnvMode === "worktree" && !envLocked && !activeWorktreePath;
278+
const isWorktreeChangeBlockedForRef = (refName: VcsRef, runtime = serverSession) =>
279+
activeProjectCwd !== null &&
280+
!isSelectingWorktreeBase &&
281+
isWorktreeChangeBlocked({
282+
runtime,
283+
currentWorktreePath: activeWorktreePath,
284+
nextWorktreePath: resolveBranchSelectionTarget({
285+
activeProjectCwd,
286+
activeWorktreePath,
287+
refName,
288+
}).nextWorktreePath,
289+
});
275290
const checkoutPullRequestItemValue =
276291
prReference && onCheckoutPullRequestRequest ? `__checkout_pull_request__:${prReference}` : null;
277292
const canCreateBranch = !isSelectingWorktreeBase && trimmedBranchQuery.length > 0;
@@ -409,6 +424,11 @@ export function BranchToolbarBranchSelector({
409424
return;
410425
}
411426

427+
// Enter selects the highlighted value directly, so disabled rows need this too.
428+
// A run can start after the render this handler came from, so read the runtime again.
429+
const latestRuntime = readThreadShell(threadRef)?.runtime ?? null;
430+
if (isWorktreeChangeBlockedForRef(refName, latestRuntime)) return;
431+
412432
const selectionTarget = resolveBranchSelectionTarget({
413433
activeProjectCwd,
414434
activeWorktreePath,
@@ -440,6 +460,25 @@ export function BranchToolbarBranchSelector({
440460
},
441461
});
442462
if (checkoutResult._tag === "Success") {
463+
// A run can start while the checkout runs. Leave the thread where it is.
464+
const latestThread = readThreadShell(threadRef);
465+
if (
466+
isWorktreeChangeBlocked({
467+
runtime: latestThread?.runtime ?? null,
468+
currentWorktreePath: latestThread?.worktreePath ?? activeWorktreePath,
469+
nextWorktreePath: selectionTarget.nextWorktreePath,
470+
})
471+
) {
472+
setOptimisticBranch(previousBranch);
473+
toastManager.add(
474+
stackedThreadToast({
475+
type: "warning",
476+
title: WORKTREE_CHANGE_BLOCKED_MESSAGE,
477+
description: `Checked out ${refName.name}, but this thread stayed in its current worktree.`,
478+
}),
479+
);
480+
return;
481+
}
443482
const nextBranchName = refName.isRemote
444483
? (checkoutResult.value.refName ?? selectedBranchName)
445484
: selectedBranchName;
@@ -647,6 +686,7 @@ export function BranchToolbarBranchSelector({
647686
projectCwd={activeProjectCwd}
648687
index={index}
649688
value={itemValue}
689+
disabled={isWorktreeChangeBlockedForRef(refName)}
650690
onClick={() => selectPickerItem(itemValue)}
651691
onContextMenu={(event) => handleBranchContextMenu(event, itemValue)}
652692
/>
@@ -668,6 +708,13 @@ export function BranchToolbarBranchSelector({
668708
isFetchingNextPage={isFetchingNextPage}
669709
onLoadNext={branchRefState.loadNext}
670710
statusText={branchStatusText}
711+
notice={
712+
refs.some((refName) => isWorktreeChangeBlockedForRef(refName))
713+
? WORKTREE_CHANGE_BLOCKED_MESSAGE
714+
: null
715+
}
716+
// A row's disabled state depends on the run status and the current worktree.
717+
extraData={`${serverSession?.status ?? ""}:${serverSession?.activeRunId ?? ""}:${activeWorktreePath ?? ""}`}
671718
renderItem={renderPickerItem}
672719
getItemType={(item) =>
673720
item === checkoutPullRequestItemValue

0 commit comments

Comments
 (0)