Skip to content

Commit afa617b

Browse files
ScottN-PVclaudejuliusmarminge
authored
fix(web): branch picker no longer interrupts a running turn (pingdotgg#15255)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent fb7e558 commit afa617b

4 files changed

Lines changed: 119 additions & 13 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function BranchPicker({
3232
isFetchingNextPage,
3333
onLoadNext,
3434
statusText,
35+
notice,
3536
originControl,
3637
popupProps,
3738
renderItem,
@@ -50,6 +51,7 @@ export function BranchPicker({
5051
isFetchingNextPage: boolean;
5152
onLoadNext: () => void;
5253
statusText: string | null;
54+
notice?: string | null | undefined;
5355
originControl?: { checked: boolean; onCheckedChange: (checked: boolean) => void } | undefined;
5456
popupProps: Omit<ComponentProps<typeof ComboboxPopup>, "children">;
5557
renderItem: (value: string, index: number) => ReactNode;
@@ -134,6 +136,7 @@ export function BranchPicker({
134136
keyExtractor={(item) => item}
135137
{...(getItemType ? { getItemType } : {})}
136138
renderItem={({ item, index }) => renderItem(item, index)}
139+
extraData={renderItem}
137140
estimatedItemSize={28}
138141
drawDistance={336}
139142
onScroll={updateBranchListScrollFades}
@@ -180,6 +183,7 @@ export function BranchPicker({
180183
</TooltipPopup>
181184
</Tooltip>
182185
) : null}
186+
{notice ? <ComboboxStatus>{notice}</ComboboxStatus> : null}
183187
{statusText ? <ComboboxStatus>{statusText}</ComboboxStatus> : null}
184188
</div>
185189
</ComboboxPopup>

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { type VcsRef } from "@t3tools/contracts";
1+
import { ProviderInstanceId, RunId, type VcsRef } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33
import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
44
import {
55
deriveLocalBranchNameFromRemoteRef,
6+
isWorktreeChangeBlocked,
67
resolveBranchSelectionTarget,
78
resolveDraftEnvModeAfterBranchChange,
89
resolveEffectiveEnvMode,
@@ -254,6 +255,35 @@ describe("resolveBranchSelectionTarget", () => {
254255
});
255256
});
256257

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+
257287
describe("shouldIncludeBranchPickerItem", () => {
258288
it("keeps the synthetic checkout PR item visible for gh pr checkout input", () => {
259289
expect(

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import type {
55
ProjectId,
66
WorktreeSubmodules,
77
} from "@t3tools/contracts";
8+
import type { ThreadStructureRuntime } from "@t3tools/client-runtime/state/shell";
89
import * as Schema from "effect/Schema";
910
import { sanitizeNewRefName } from "@t3tools/shared/git";
1011
import { workspaceRelativeFilePath } from "@t3tools/shared/path";
1112
import { toSortableTimestamp } from "@t3tools/client-runtime/state/thread-sort";
13+
import { deriveCanInterruptRunningThread } from "../session-logic";
1214
export { deriveLocalBranchNameFromRemoteRef, sanitizeNewRefName } from "@t3tools/shared/git";
1315

1416
export interface EnvironmentOption {
@@ -281,6 +283,17 @@ export function resolveBranchSelectionTarget(input: {
281283
};
282284
}
283285

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+
284297
export function shouldIncludeBranchPickerItem(input: {
285298
itemValue: string;
286299
normalizedQuery: string;

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

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
3232
import { readLocalApi } from "../localApi";
3333
import { useOpenPrLink } from "../lib/openPullRequestLink";
3434
import { usePaginatedBranches } from "../state/queries";
35-
import { useProject, useQuietThreadShell } from "../state/entities";
35+
import { readThreadShell, useProject, useQuietThreadShell } from "../state/entities";
3636
import { useEnvironmentQuery } from "../state/query";
3737
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
3838
import { threadEnvironment } from "../state/threads";
@@ -46,6 +46,7 @@ import { getSourceControlPresentation } from "../sourceControlPresentation";
4646
import { useComposerMenuProps } from "./chat/composerEventScope";
4747
import {
4848
deriveLocalBranchNameFromRemoteRef,
49+
isWorktreeChangeBlocked,
4950
resolveBranchTriggerLabel,
5051
resolveBranchToolbarPrBranch,
5152
resolveBranchSelectionTarget,
@@ -90,6 +91,8 @@ interface BranchToolbarBranchSelectorProps {
9091
onComposerFocusRequest?: () => void;
9192
}
9293

94+
const WORKTREE_CHANGE_BLOCKED_MESSAGE = "Stop the current turn to switch worktrees.";
95+
9396
function toBranchActionErrorMessage(error: unknown): string {
9497
return error instanceof Error ? error.message : "An error occurred.";
9598
}
@@ -153,7 +156,9 @@ export function BranchToolbarBranchSelector({
153156
: (serverThread?.branch ?? draftThread?.branch ?? null);
154157
const activeWorktreePath = forceNewWorktree
155158
? null
156-
: (serverThread?.worktreePath ?? draftThread?.worktreePath ?? null);
159+
: serverThread
160+
? serverThread.worktreePath
161+
: (draftThread?.worktreePath ?? null);
157162
const activeProjectCwd = activeProject?.workspaceRoot ?? null;
158163
const branchCwd = activeWorktreePath ?? activeProjectCwd;
159164
const hasServerThread = serverThread !== null;
@@ -167,6 +172,10 @@ export function BranchToolbarBranchSelector({
167172
draftThreadEnvMode: draftThread?.envMode,
168173
});
169174

175+
const readLatestThreadState = () =>
176+
readThreadShell(threadRef) ??
177+
serverThread ?? { runtime: null, worktreePath: activeWorktreePath };
178+
170179
const setThreadBranch = (
171180
branch: string | null,
172181
worktreePath: string | null,
@@ -177,8 +186,22 @@ export function BranchToolbarBranchSelector({
177186
!activeProject ||
178187
(hasServerThread && !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope))
179188
)
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) {
182205
void stopThreadSession({
183206
environmentId,
184207
input: { threadId: activeThreadId },
@@ -197,7 +220,7 @@ export function BranchToolbarBranchSelector({
197220
}
198221
if (hasServerThread) {
199222
onActiveThreadBranchOverrideChange?.(branch);
200-
return;
223+
return true;
201224
}
202225
const nextDraftEnvMode = resolveDraftEnvModeAfterBranchChange({
203226
nextWorktreePath: worktreePath,
@@ -211,6 +234,7 @@ export function BranchToolbarBranchSelector({
211234
environmentSelection: automatic ? (draftThread?.environmentSelection ?? "auto") : "manual",
212235
projectRef: scopeProjectRef(environmentId, activeProject.id),
213236
});
237+
return true;
214238
};
215239

216240
const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);
@@ -244,6 +268,20 @@ export function BranchToolbarBranchSelector({
244268
const prReference = parsePullRequestReference(trimmedBranchQuery);
245269
const isSelectingWorktreeBase =
246270
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+
});
247285
const checkoutPullRequestItemValue =
248286
canChangeThreadBranch && prReference && onCheckoutPullRequestRequest
249287
? `__checkout_pull_request__:${prReference}`
@@ -386,20 +424,30 @@ export function BranchToolbarBranchSelector({
386424
const selectBranch = (refName: VcsRef) => {
387425
if (!canUpdateThreadBranch || !branchCwd || !activeProjectCwd || isBranchActionPending) return;
388426

427+
const latest = readLatestThreadState();
389428
if (isSelectingWorktreeBase) {
429+
if (latest.worktreePath) return;
390430
setThreadBranch(refName.name, null);
391431
setIsBranchMenuOpen(false);
392432
onComposerFocusRequest?.();
393433
return;
394434
}
395-
396435
const selectionTarget = resolveBranchSelectionTarget({
397436
activeProjectCwd,
398437
activeProjectCheckoutRoot,
399-
activeWorktreePath,
438+
activeWorktreePath: latest.worktreePath,
400439
refName,
401440
});
402441

442+
if (
443+
isWorktreeChangeBlocked({
444+
runtime: latest.runtime,
445+
currentWorktreePath: latest.worktreePath,
446+
nextWorktreePath: selectionTarget.nextWorktreePath,
447+
})
448+
)
449+
return;
450+
403451
if (selectionTarget.reuseExistingWorktree) {
404452
setThreadBranch(refName.name, selectionTarget.nextWorktreePath);
405453
setIsBranchMenuOpen(false);
@@ -428,8 +476,11 @@ export function BranchToolbarBranchSelector({
428476
const nextBranchName = refName.isRemote
429477
? (checkoutResult.value.refName ?? selectedBranchName)
430478
: selectedBranchName;
431-
setOptimisticBranch(nextBranchName);
432-
setThreadBranch(nextBranchName, selectionTarget.nextWorktreePath);
479+
setOptimisticBranch(
480+
setThreadBranch(nextBranchName, selectionTarget.nextWorktreePath)
481+
? nextBranchName
482+
: previousBranch,
483+
);
433484
return;
434485
}
435486
setOptimisticBranch(previousBranch);
@@ -465,8 +516,10 @@ export function BranchToolbarBranchSelector({
465516
},
466517
});
467518
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+
);
470523
return;
471524
}
472525
setOptimisticBranch(previousBranch);
@@ -641,7 +694,8 @@ export function BranchToolbarBranchSelector({
641694
activeProjectCheckoutRoot,
642695
activeWorktreePath,
643696
refName,
644-
}).reuseExistingWorktree))
697+
}).reuseExistingWorktree)) ||
698+
isWorktreeChangeBlockedForRef(refName)
645699
}
646700
onClick={() => selectPickerItem(itemValue)}
647701
onContextMenu={(event) => handleBranchContextMenu(event, itemValue)}
@@ -663,6 +717,11 @@ export function BranchToolbarBranchSelector({
663717
isFetchingNextPage={isFetchingNextPage}
664718
onLoadNext={branchRefState.loadNext}
665719
statusText={branchStatusText}
720+
notice={
721+
refs.some((refName) => isWorktreeChangeBlockedForRef(refName))
722+
? WORKTREE_CHANGE_BLOCKED_MESSAGE
723+
: null
724+
}
666725
renderItem={renderPickerItem}
667726
getItemType={(item) =>
668727
item === checkoutPullRequestItemValue

0 commit comments

Comments
 (0)