Skip to content
TypeScript

fix(web): new thread shortcut works while a sent draft sets up its worktree (#14538)

Guillermo Casanovab459f2ad4e07
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 694e9cf3290..0d9808977db 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -325,7 +325,7 @@ import {
clearBackgroundDraftSubmissionByRef,
finalizePromotedDraftThreadByRef,
markPromotedDraftThreadByRef,
- restoreFailedBackgroundDraftThread,
+ restoreFailedDraftThread,
composerDraftHasUserContent,
type ComposerFileAttachment,
type ComposerImageAttachment,
@@ -6845,6 +6845,11 @@ export default function ChatView(props: ChatViewProps) {
}),
);
}
+ const sentDraftThreadRef = isLocalDraftThread
+ ? scopeThreadRef(environmentId, threadIdForSend)
+ : null;
+ // Keep new-thread flows from reusing this draft while attachments and worktree setup settle.
+ if (sentDraftThreadRef) markPromotedDraftThreadByRef(sentDraftThreadRef);
promptRef.current = "";
clearComposerDraftContent(composerDraftTarget);
composerRef.current?.resetCursorState();
@@ -6998,7 +7003,6 @@ export default function ChatView(props: ChatViewProps) {
},
});
if (backgroundThreadRef) {
- markPromotedDraftThreadByRef(backgroundThreadRef);
try {
backgroundDraftOpened = Boolean(
await handleNewThread(
@@ -7032,12 +7036,15 @@ export default function ChatView(props: ChatViewProps) {
releaseDraftAttachments(composerAttachmentsSnapshot);
}
acknowledgeActiveThreadWoke();
- if (backgroundThreadRef) {
+ // A draft still on screen finalizes once its thread route takes over.
+ if (sentDraftThreadRef) {
if (backgroundDraftOpened || currentRouteThreadKeyRef.current !== routeThreadKey) {
- finalizePromotedDraftThreadByRef(backgroundThreadRef);
+ finalizePromotedDraftThreadByRef(sentDraftThreadRef);
} else {
- clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
+ clearBackgroundDraftSubmissionByRef(sentDraftThreadRef);
}
+ }
+ if (backgroundThreadRef) {
if (backgroundDraftOpened) {
toastManager.add(
stackedThreadToast({
@@ -7066,8 +7073,8 @@ export default function ChatView(props: ChatViewProps) {
}
if (failure !== null) {
- if (submissionIntent === "background" && draftId && draftThread) {
- restoreFailedBackgroundDraftThread(
+ if (isLocalDraftThread && draftId && draftThread) {
+ restoreFailedDraftThread(
draftId,
draftThread,
wasBootstrapThreadDeleted(squashAtomCommandFailure(failure))
@@ -7077,20 +7084,9 @@ export default function ChatView(props: ChatViewProps) {
clearBackgroundDraftSubmissionByRef(scopeThreadRef(environmentId, threadIdForSend));
}
if (
- backgroundDraftOpened
- ? !composerDraftHasUserContent(
- useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),
- )
- : promptRef.current.length === 0 &&
- composerImagesRef.current.length === 0 &&
- composerFilesRef.current.length === 0 &&
- composerTerminalContextsRef.current.length === 0 &&
- (useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)
- ?.previewAnnotations.length ?? 0) === 0 &&
- (useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.reviewComments
- .length ?? 0) === 0 &&
- (useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.threadContexts
- .length ?? 0) === 0
+ !composerDraftHasUserContent(
+ useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),
+ )
) {
setOptimisticUserMessages((existing) => {
const removed = existing.filter((message) => message.id === messageIdForSend);
@@ -7100,11 +7096,7 @@ export default function ChatView(props: ChatViewProps) {
const next = existing.filter((message) => message.id !== messageIdForSend);
return next.length === existing.length ? existing : next;
});
- promptRef.current = messageTextForSend;
const retryComposerImages = composerImagesSnapshot.map(cloneComposerImageForRetry);
- composerImagesRef.current = retryComposerImages;
- composerFilesRef.current = composerFilesSnapshot;
- composerTerminalContextsRef.current = composerTerminalContextsSnapshot;
setComposerDraftPrompt(composerDraftTarget, messageTextForSend);
addComposerDraftImages(composerDraftTarget, retryComposerImages);
addComposerDraftFiles(composerDraftTarget, composerFilesSnapshot);
@@ -7112,11 +7104,17 @@ export default function ChatView(props: ChatViewProps) {
setComposerDraftPreviewAnnotations(composerDraftTarget, composerPreviewAnnotationsSnapshot);
setComposerDraftReviewComments(composerDraftTarget, composerReviewCommentsSnapshot);
setComposerDraftThreadContexts(composerDraftTarget, composerThreadContextsSnapshot);
- composerRef.current?.resetCursorState({
- cursor: collapseExpandedComposerCursor(messageTextForSend, messageTextForSend.length),
- prompt: messageTextForSend,
- detectTrigger: true,
- });
+ if (currentRouteThreadKeyRef.current === routeThreadKey) {
+ promptRef.current = messageTextForSend;
+ composerImagesRef.current = retryComposerImages;
+ composerFilesRef.current = composerFilesSnapshot;
+ composerTerminalContextsRef.current = composerTerminalContextsSnapshot;
+ composerRef.current?.resetCursorState({
+ cursor: collapseExpandedComposerCursor(messageTextForSend, messageTextForSend.length),
+ prompt: messageTextForSend,
+ detectTrigger: true,
+ });
+ }
}
if (!isAtomCommandInterrupted(failure)) {
const error = squashAtomCommandFailure(failure);
@@ -7124,11 +7122,17 @@ export default function ChatView(props: ChatViewProps) {
threadIdForSend,
error instanceof Error ? error.message : "Failed to send message.",
);
- if (backgroundDraftOpened && draftId) {
+ if (
+ draftId &&
+ (backgroundDraftOpened || currentRouteThreadKeyRef.current !== routeThreadKey)
+ ) {
toastManager.add(
stackedThreadToast({
type: "error",
- title: "Background task failed",
+ title:
+ submissionIntent === "background"
+ ? "Background task failed"
+ : "Could not start thread",
description: error instanceof Error ? error.message : "Failed to send message.",
actionProps: {
children: "Open draft",
diff --git a/apps/web/src/components/ThreadRouteView.tsx b/apps/web/src/components/ThreadRouteView.tsx
index 12fbd0d5503..6bbe65761fb 100644
--- a/apps/web/src/components/ThreadRouteView.tsx
+++ b/apps/web/src/components/ThreadRouteView.tsx
@@ -35,6 +35,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
draftId === null ? null : store.getDraftSession(draftId),
);
const threadRefs = useThreadRefs();
+ // Promotion can begin before the server creates the reserved thread.
const inferredThreadRef = draftSession
? (threadRefs.find(
(ref) =>
@@ -43,7 +44,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
) ?? null)
: null;
const serverThreadRef: ScopedThreadRef | null =
- target.kind === "server" ? target.threadRef : (draftSession?.promotedTo ?? inferredThreadRef);
+ target.kind === "server" ? target.threadRef : inferredThreadRef;
const serverThread = useQuietThreadShell(serverThreadRef);
const backgroundSubmissionPending = useBackgroundDraftSubmissionPending(
target.kind === "draft" ? serverThreadRef : null,
diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.ts
index 629adc76fe8..410aba2b39b 100644
--- a/apps/web/src/composerDraftStore.test.ts
+++ b/apps/web/src/composerDraftStore.test.ts
@@ -69,7 +69,7 @@ import {
clearBackgroundDraftSubmissionByRef,
finalizePromotedDraftThreadByRef,
markPromotedDraftThreadByRef,
- restoreFailedBackgroundDraftThread,
+ restoreFailedDraftThread,
type ComposerFileAttachment,
type ComposerImageAttachment,
composerFileNeedsReattach,
@@ -1643,7 +1643,7 @@ describe("composerDraftStore project draft thread mapping", () => {
expect(useComposerDraftStore.getState().getDraftSession(draftId)).toBeNull();
});
- it("restores a failed background draft without changing the fresh draft", () => {
+ it("restores a failed draft without changing the fresh draft", () => {
const store = useComposerDraftStore.getState();
store.setProjectDraftThreadId(projectRef, draftId, { threadId });
const sentDraft = useComposerDraftStore.getState().getDraftSession(draftId)!;
@@ -1651,12 +1651,14 @@ describe("composerDraftStore project draft thread mapping", () => {
const freshId = DraftId.make("fresh-background-draft");
store.setProjectDraftThreadId(projectRef, freshId, { threadId: ThreadId.make("fresh-thread") });
store.setPrompt(freshId, "new work");
- restoreFailedBackgroundDraftThread(draftId, sentDraft, ThreadId.make("retry-thread"));
+ store.setDraftThreadContext(draftId, { branch: "retry-base" });
+ restoreFailedDraftThread(draftId, sentDraft, ThreadId.make("retry-thread"));
store.setPrompt(draftId, "retry work");
expect(useComposerDraftStore.getState().getDraftSession(draftId)?.promotedTo).toBeNull();
expect(useComposerDraftStore.getState().getDraftSession(draftId)?.threadId).toBe(
"retry-thread",
);
+ expect(useComposerDraftStore.getState().getDraftSession(draftId)?.branch).toBe("retry-base");
expect(draftByKey(freshId)?.prompt).toBe("new work");
expect(draftByKey(draftId)?.prompt).toBe("retry work");
});
diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.ts
index 76b86ae4cd4..b93af955f32 100644
--- a/apps/web/src/composerDraftStore.ts
+++ b/apps/web/src/composerDraftStore.ts
@@ -4270,7 +4270,7 @@ export function markPromotedDraftThreadByRef(threadRef: ScopedThreadRef): void {
}
}
-export function restoreFailedBackgroundDraftThread(
+export function restoreFailedDraftThread(
draftId: DraftId,
draftThread: DraftThreadState,
threadId: ThreadId,
@@ -4279,8 +4279,9 @@ export function restoreFailedBackgroundDraftThread(
draftThreadsByThreadKey: {
...state.draftThreadsByThreadKey,
[draftId]: {
- ...draftThread,
+ ...(state.draftThreadsByThreadKey[draftId] ?? draftThread),
threadId,
+ ...(threadId !== draftThread.threadId ? { createdAt: new Date().toISOString() } : {}),
promotedTo: null,
},
},