The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 23:30 UTC
fix(web): branch picker no longer interrupts a running turn (#15255)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Scott Norteman committed
Nov 11, 2026 at 11:42 UTC
afa617b4655361ccdee49a6bfe21696188f0d5ce
4 files changed
+119
-13
apps/web/src/components/BranchPicker.tsx
+4
@@ -32,6 +32,7 @@ export function BranchPicker({
32
isFetchingNextPage,
33
onLoadNext,
34
statusText,
35
+ notice,
36
originControl,
37
popupProps,
38
renderItem,
@@ -50,6 +51,7 @@ export function BranchPicker({
51
isFetchingNextPage: boolean;
52
onLoadNext: () => void;
53
statusText: string | null;
54
+ notice?: string | null | undefined;
55
originControl?: { checked: boolean; onCheckedChange: (checked: boolean) => void } | undefined;
56
popupProps: Omit<ComponentProps<typeof ComboboxPopup>, "children">;
57
renderItem: (value: string, index: number) => ReactNode;
@@ -134,6 +136,7 @@ export function BranchPicker({
136
keyExtractor={(item) => item}
137
{...(getItemType ? { getItemType } : {})}
138
renderItem={({ item, index }) => renderItem(item, index)}
139
+ extraData={renderItem}
140
estimatedItemSize={28}
141
drawDistance={336}
142
onScroll={updateBranchListScrollFades}
@@ -180,6 +183,7 @@ export function BranchPicker({
183
</TooltipPopup>
184
</Tooltip>
185
) : null}
186
+ {notice ? <ComboboxStatus>{notice}</ComboboxStatus> : null}
187
{statusText ? <ComboboxStatus>{statusText}</ComboboxStatus> : null}
188
</div>
189
</ComboboxPopup>
apps/web/src/components/BranchToolbar.logic.test.ts
+31
-1
@@ -1,8 +1,9 @@
1
-import { type VcsRef } from "@t3tools/contracts";
1
+import { ProviderInstanceId, RunId, type VcsRef } from "@t3tools/contracts";
2
import { describe, expect, it } from "vite-plus/test";
3
import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
4
import {
5
deriveLocalBranchNameFromRemoteRef,
6
+ isWorktreeChangeBlocked,
7
resolveBranchSelectionTarget,
8
resolveDraftEnvModeAfterBranchChange,
9
resolveEffectiveEnvMode,
@@ -254,6 +255,35 @@ describe("resolveBranchSelectionTarget", () => {
255
});
256
});
257
258
+describe("isWorktreeChangeBlocked", () => {
259
+ const runtime = (status: "preparing" | "running" | "completed") => ({
260
+ status,
261
+ activeRunId: status === "completed" ? null : RunId.make("run"),
262
+ providerInstanceId: ProviderInstanceId.make("codex"),
263
+ providerName: null,
264
+ lastError: null,
265
+ });
266
+
267
+ it.each([
268
+ ["preparing", "/repo/wt-a", null, true],
269
+ ["running", "/repo/wt-a", "/repo/wt-b", true],
270
+ ["running", null, "/repo/wt-b", true],
271
+ ["running", "/repo/wt-a", "/repo/wt-a", false],
272
+ ["completed", "/repo/wt-a", null, false],
273
+ ] as const)(
274
+ "a %s turn moving %s -> %s is blocked: %s",
275
+ (status, currentWorktreePath, nextWorktreePath, blocked) => {
276
+ expect(
277
+ isWorktreeChangeBlocked({
278
+ runtime: runtime(status),
279
+ currentWorktreePath,
280
+ nextWorktreePath,
281
+ }),
282
+ ).toBe(blocked);
283
+ },
284
+ );
285
+});
286
+
287
describe("shouldIncludeBranchPickerItem", () => {
288
it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
289
expect(
apps/web/src/components/BranchToolbar.logic.ts
+13
@@ -5,10 +5,12 @@ import type {
5
ProjectId,
6
WorktreeSubmodules,
7
} from "@t3tools/contracts";
8
+import type { ThreadStructureRuntime } from "@t3tools/client-runtime/state/shell";
9
import * as Schema from "effect/Schema";
10
import { sanitizeNewRefName } from "@t3tools/shared/git";
11
import { workspaceRelativeFilePath } from "@t3tools/shared/path";
12
import { toSortableTimestamp } from "@t3tools/client-runtime/state/thread-sort";
13
+import { deriveCanInterruptRunningThread } from "../session-logic";
14
export { deriveLocalBranchNameFromRemoteRef, sanitizeNewRefName } from "@t3tools/shared/git";
15
16
export interface EnvironmentOption {
@@ -281,6 +283,17 @@ export function resolveBranchSelectionTarget(input: {
283
};
284
}
285
286
+export function isWorktreeChangeBlocked(input: {
287
+ runtime: ThreadStructureRuntime | null;
288
+ currentWorktreePath: string | null;
289
+ nextWorktreePath: string | null;
290
+}): boolean {
291
+ return (
292
+ input.nextWorktreePath !== input.currentWorktreePath &&
293
+ deriveCanInterruptRunningThread(true, input.runtime)
294
+ );
295
+}
296
+
297
export function shouldIncludeBranchPickerItem(input: {
298
itemValue: string;
299
normalizedQuery: string;
apps/web/src/components/BranchToolbarBranchSelector.tsx
+71
-12
@@ -32,7 +32,7 @@ import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
32
import { readLocalApi } from "../localApi";
33
import { useOpenPrLink } from "../lib/openPullRequestLink";
34
import { usePaginatedBranches } from "../state/queries";
35
-import { useProject, useQuietThreadShell } from "../state/entities";
35
+import { readThreadShell, useProject, useQuietThreadShell } from "../state/entities";
36
import { useEnvironmentQuery } from "../state/query";
37
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
38
import { threadEnvironment } from "../state/threads";
@@ -46,6 +46,7 @@ import { getSourceControlPresentation } from "../sourceControlPresentation";
46
import { useComposerMenuProps } from "./chat/composerEventScope";
47
import {
48
deriveLocalBranchNameFromRemoteRef,
49
+ isWorktreeChangeBlocked,
50
resolveBranchTriggerLabel,
51
resolveBranchToolbarPrBranch,
52
resolveBranchSelectionTarget,
@@ -90,6 +91,8 @@ interface BranchToolbarBranchSelectorProps {
91
onComposerFocusRequest?: () => void;
92
}
93
94
+const WORKTREE_CHANGE_BLOCKED_MESSAGE = "Stop the current turn to switch worktrees.";
95
+
96
function toBranchActionErrorMessage(error: unknown): string {
97
return error instanceof Error ? error.message : "An error occurred.";
98
}
@@ -153,7 +156,9 @@ export function BranchToolbarBranchSelector({
156
: (serverThread?.branch ?? draftThread?.branch ?? null);
157
const activeWorktreePath = forceNewWorktree
158
? null
156
- : (serverThread?.worktreePath ?? draftThread?.worktreePath ?? null);
159
+ : serverThread
160
+ ? serverThread.worktreePath
161
+ : (draftThread?.worktreePath ?? null);
162
const activeProjectCwd = activeProject?.workspaceRoot ?? null;
163
const branchCwd = activeWorktreePath ?? activeProjectCwd;
164
const hasServerThread = serverThread !== null;
@@ -167,6 +172,10 @@ export function BranchToolbarBranchSelector({
172
draftThreadEnvMode: draftThread?.envMode,
173
});
174
175
+ const readLatestThreadState = () =>
176
+ readThreadShell(threadRef) ??
177
+ serverThread ?? { runtime: null, worktreePath: activeWorktreePath };
178
+
179
const setThreadBranch = (
180
branch: string | null,
181
worktreePath: string | null,
@@ -177,8 +186,22 @@ export function BranchToolbarBranchSelector({
186
!activeProject ||
187
(hasServerThread && !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope))
188
)
180
- return;
181
- if (serverSession && worktreePath !== activeWorktreePath) {
189
+ return false;
190
+ const latest = readLatestThreadState();
191
+ if (automatic && (latest.runtime || latest.worktreePath)) return false;
192
+ if (
193
+ isWorktreeChangeBlocked({
194
+ runtime: latest.runtime,
195
+ currentWorktreePath: latest.worktreePath,
196
+ nextWorktreePath: worktreePath,
197
+ })
198
+ ) {
199
+ toastManager.add(
200
+ stackedThreadToast({ type: "warning", title: WORKTREE_CHANGE_BLOCKED_MESSAGE }),
201
+ );
202
+ return false;
203
+ }
204
+ if (latest.runtime && worktreePath !== latest.worktreePath) {
205
void stopThreadSession({
206
environmentId,
207
input: { threadId: activeThreadId },
@@ -197,7 +220,7 @@ export function BranchToolbarBranchSelector({
220
}
221
if (hasServerThread) {
222
onActiveThreadBranchOverrideChange?.(branch);
200
- return;
223
+ return true;
224
}
225
const nextDraftEnvMode = resolveDraftEnvModeAfterBranchChange({
226
nextWorktreePath: worktreePath,
@@ -211,6 +234,7 @@ export function BranchToolbarBranchSelector({
234
environmentSelection: automatic ? (draftThread?.environmentSelection ?? "auto") : "manual",
235
projectRef: scopeProjectRef(environmentId, activeProject.id),
236
});
237
+ return true;
238
};
239
240
const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);
@@ -244,6 +268,20 @@ export function BranchToolbarBranchSelector({
268
const prReference = parsePullRequestReference(trimmedBranchQuery);
269
const isSelectingWorktreeBase =
270
effectiveEnvMode === "worktree" && !envLocked && !activeWorktreePath;
271
+ const isWorktreeChangeBlockedForRef = (refName: VcsRef) =>
272
+ activeProjectCwd !== null &&
273
+ isWorktreeChangeBlocked({
274
+ runtime: serverSession,
275
+ currentWorktreePath: serverThread ? serverThread.worktreePath : activeWorktreePath,
276
+ nextWorktreePath: isSelectingWorktreeBase
277
+ ? null
278
+ : resolveBranchSelectionTarget({
279
+ activeProjectCwd,
280
+ activeProjectCheckoutRoot,
281
+ activeWorktreePath,
282
+ refName,
283
+ }).nextWorktreePath,
284
+ });
285
const checkoutPullRequestItemValue =
286
canChangeThreadBranch && prReference && onCheckoutPullRequestRequest
287
? `__checkout_pull_request__:${prReference}`
@@ -386,20 +424,30 @@ export function BranchToolbarBranchSelector({
424
const selectBranch = (refName: VcsRef) => {
425
if (!canUpdateThreadBranch || !branchCwd || !activeProjectCwd || isBranchActionPending) return;
426
427
+ const latest = readLatestThreadState();
428
if (isSelectingWorktreeBase) {
429
+ if (latest.worktreePath) return;
430
setThreadBranch(refName.name, null);
431
setIsBranchMenuOpen(false);
432
onComposerFocusRequest?.();
433
return;
434
}
395
-
435
const selectionTarget = resolveBranchSelectionTarget({
436
activeProjectCwd,
437
activeProjectCheckoutRoot,
399
- activeWorktreePath,
438
+ activeWorktreePath: latest.worktreePath,
439
refName,
440
});
441
442
+ if (
443
+ isWorktreeChangeBlocked({
444
+ runtime: latest.runtime,
445
+ currentWorktreePath: latest.worktreePath,
446
+ nextWorktreePath: selectionTarget.nextWorktreePath,
447
+ })
448
+ )
449
+ return;
450
+
451
if (selectionTarget.reuseExistingWorktree) {
452
setThreadBranch(refName.name, selectionTarget.nextWorktreePath);
453
setIsBranchMenuOpen(false);
@@ -428,8 +476,11 @@ export function BranchToolbarBranchSelector({
476
const nextBranchName = refName.isRemote
477
? (checkoutResult.value.refName ?? selectedBranchName)
478
: selectedBranchName;
431
- setOptimisticBranch(nextBranchName);
432
- setThreadBranch(nextBranchName, selectionTarget.nextWorktreePath);
479
+ setOptimisticBranch(
480
+ setThreadBranch(nextBranchName, selectionTarget.nextWorktreePath)
481
+ ? nextBranchName
482
+ : previousBranch,
483
+ );
484
return;
485
}
486
setOptimisticBranch(previousBranch);
@@ -465,8 +516,10 @@ export function BranchToolbarBranchSelector({
516
},
517
});
518
if (createBranchResult._tag === "Success") {
468
- setOptimisticBranch(createBranchResult.value.refName);
469
- setThreadBranch(createBranchResult.value.refName, activeWorktreePath);
519
+ const nextBranchName = createBranchResult.value.refName;
520
+ setOptimisticBranch(
521
+ setThreadBranch(nextBranchName, activeWorktreePath) ? nextBranchName : previousBranch,
522
+ );
523
return;
524
}
525
setOptimisticBranch(previousBranch);
@@ -641,7 +694,8 @@ export function BranchToolbarBranchSelector({
694
activeProjectCheckoutRoot,
695
activeWorktreePath,
696
refName,
644
- }).reuseExistingWorktree))
697
+ }).reuseExistingWorktree)) ||
698
+ isWorktreeChangeBlockedForRef(refName)
699
}
700
onClick={() => selectPickerItem(itemValue)}
701
onContextMenu={(event) => handleBranchContextMenu(event, itemValue)}
@@ -663,6 +717,11 @@ export function BranchToolbarBranchSelector({
717
isFetchingNextPage={isFetchingNextPage}
718
onLoadNext={branchRefState.loadNext}
719
statusText={branchStatusText}
720
+ notice={
721
+ refs.some((refName) => isWorktreeChangeBlockedForRef(refName))
722
+ ? WORKTREE_CHANGE_BLOCKED_MESSAGE
723
+ : null
724
+ }
725
renderItem={renderPickerItem}
726
getItemType={(item) =>
727
item === checkoutPullRequestItemValue