Skip to content

Commit c87a2c8

Browse files
fix(web): retain worktree attachment when adopting checkout branches
1 parent cbf86d9 commit c87a2c8

4 files changed

Lines changed: 52 additions & 47 deletions

File tree

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -328,30 +328,29 @@ describe("resolveCheckoutBranchMismatch", () => {
328328
).toBeNull();
329329
});
330330

331-
it("detects a worktree checked out on another branch without offering a restore", () => {
332-
expect(
333-
resolveCheckoutBranchMismatch({
334-
effectiveEnvMode: "worktree",
335-
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
336-
activeThreadBranch: "feature/thread",
337-
currentGitBranch: "feature/current",
338-
}),
339-
).toEqual({
340-
threadBranch: "feature/thread",
341-
currentBranch: "feature/current",
342-
canRestoreThreadBranch: false,
343-
});
344-
});
331+
it("warns about worktree branch drift without restore, but ignores a pending rename status", () => {
332+
const worktree = {
333+
effectiveEnvMode: "worktree" as const,
334+
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
335+
activeThreadBranch: "t3code/fix-sidebar",
336+
};
345337

346-
it("ignores a worktree status read from before its temporary branch was renamed", () => {
347338
expect(
348339
resolveCheckoutBranchMismatch({
349-
effectiveEnvMode: "worktree",
350-
activeWorktreePath: "/repo/.t3/worktrees/feature-thread",
351-
activeThreadBranch: "t3code/fix-sidebar",
340+
...worktree,
352341
currentGitBranch: "t3code/1a2b3c4d",
353342
}),
354343
).toBeNull();
344+
expect(
345+
resolveCheckoutBranchMismatch({
346+
...worktree,
347+
currentGitBranch: "feature/checkout",
348+
}),
349+
).toEqual({
350+
threadBranch: worktree.activeThreadBranch,
351+
currentBranch: "feature/checkout",
352+
canRestoreThreadBranch: false,
353+
});
355354
});
356355

357356
it("ignores new-worktree base selection before a worktree exists", () => {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,12 @@ export function resolveBranchToolbarPrBranch(input: {
231231
return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;
232232
}
233233

234-
// A thread's recorded branch can fall behind its checkout: the user or an agent
235-
// switches branches, or a sibling thread sharing the worktree does. Only a local
236-
// checkout offers a restore; switching a worktree back could pull the branch
237-
// out from under a sibling thread.
238234
export function resolveCheckoutBranchMismatch(input: {
239235
effectiveEnvMode: EnvMode;
240236
activeWorktreePath: string | null;
241237
activeThreadBranch: string | null;
242238
currentGitBranch: string | null;
243-
}): { threadBranch: string; currentBranch: string; canRestoreThreadBranch: boolean } | null {
239+
}) {
244240
const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
245241
const isLocalCheckout = effectiveEnvMode === "local" && activeWorktreePath === null;
246242
const isEstablishedWorktree = effectiveEnvMode === "worktree" && activeWorktreePath !== null;
@@ -250,8 +246,7 @@ export function resolveCheckoutBranchMismatch(input: {
250246
if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {
251247
return null;
252248
}
253-
// A new worktree's temporary branch is renamed in the background, so a status
254-
// read from before the rename is not drift.
249+
// Cached status can predate the automatic rename of a temporary worktree branch.
255250
if (
256251
isEstablishedWorktree &&
257252
!isTemporaryWorktreeBranch(activeThreadBranch) &&
@@ -262,6 +257,7 @@ export function resolveCheckoutBranchMismatch(input: {
262257
return {
263258
threadBranch: activeThreadBranch,
264259
currentBranch: currentGitBranch,
260+
// Restoring a worktree checkout could switch the branch for sibling threads.
265261
canRestoreThreadBranch: isLocalCheckout,
266262
};
267263
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ describe("resolveThreadMetadataUpdateForNextTurn", () => {
258258
model: "gpt-5.4",
259259
};
260260

261-
it("updates a stale thread branch to the active checkout without moving the thread", () => {
261+
it("adopts the checkout branch without clearing the worktree attachment", () => {
262262
expect(
263263
resolveThreadMetadataUpdateForNextTurn({
264264
currentModelSelection: modelSelection,

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3342,26 +3342,6 @@ export default function ChatView(props: ChatViewProps) {
33423342
scheduleComposerFocus();
33433343
};
33443344
const insertArtifactTemplateStable = useStableCallback(insertArtifactTemplate);
3345-
const sendAppMessage = async (text: string) => {
3346-
if (!isServerThread || activeThreadId === null) {
3347-
throw new Error("Messages from apps need a started thread.");
3348-
}
3349-
const result = await startThreadTurn({
3350-
environmentId,
3351-
input: {
3352-
threadId: activeThreadId,
3353-
message: { messageId: newMessageId(), role: "user", text, attachments: [] },
3354-
runtimeMode,
3355-
interactionMode,
3356-
dispatchMode: "queue",
3357-
},
3358-
});
3359-
if (result._tag === "Failure") {
3360-
const error = squashAtomCommandFailure(result);
3361-
throw error instanceof Error ? error : new Error("Could not send the app's message.");
3362-
}
3363-
};
3364-
const sendAppMessageStable = useStableCallback(sendAppMessage);
33653345
const queuedRunsControlRef = useRef<QueuedRunsControlHandle>(null);
33663346
const addTerminalContextToDraft = (selection: TerminalContextSelection) => {
33673347
composerRef.current?.addTerminalContext(selection);
@@ -5074,6 +5054,37 @@ export default function ChatView(props: ChatViewProps) {
50745054
};
50755055
})() as ComposerBannerStackItem | null;
50765056

5057+
const sendAppMessage = async (text: string) => {
5058+
if (!isServerThread || activeThreadId === null) {
5059+
throw new Error("Messages from apps need a started thread.");
5060+
}
5061+
const settingsResult = await persistThreadSettingsForNextTurn({
5062+
threadId: activeThreadId,
5063+
createdAt: new Date().toISOString(),
5064+
...(checkoutBranchMismatch ? { branch: checkoutBranchMismatch.currentBranch } : {}),
5065+
runtimeMode,
5066+
interactionMode,
5067+
});
5068+
const result =
5069+
settingsResult._tag === "Failure"
5070+
? settingsResult
5071+
: await startThreadTurn({
5072+
environmentId,
5073+
input: {
5074+
threadId: activeThreadId,
5075+
message: { messageId: newMessageId(), role: "user", text, attachments: [] },
5076+
runtimeMode,
5077+
interactionMode,
5078+
dispatchMode: "queue",
5079+
},
5080+
});
5081+
if (result._tag === "Failure") {
5082+
const error = squashAtomCommandFailure(result);
5083+
throw error instanceof Error ? error : new Error("Could not send the app's message.");
5084+
}
5085+
};
5086+
const sendAppMessageStable = useStableCallback(sendAppMessage);
5087+
50775088
const sendStandaloneCommand = async (text: string, failureMessage: string) => {
50785089
if (!activeThread || !clientSettingsHydrated || sendInFlightRef.current) return;
50795090
const context = composerRef.current?.getSendContext();
@@ -7337,7 +7348,6 @@ export default function ChatView(props: ChatViewProps) {
73377348
modelSelection: nextThreadModelSelection,
73387349
runtimeMode: defaultRuntimeMode,
73397350
interactionMode: "default",
7340-
// The new thread runs in the same checkout, so it starts on its live branch.
73417351
branch: checkoutBranchMismatch?.currentBranch ?? activeThreadBranch,
73427352
worktreePath: activeThread.worktreePath,
73437353
createdAt,

0 commit comments

Comments
 (0)