@devries-cameron20 / t3code Mirror · read-only / commits / b459f2ad4e
Fork 0

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 23, 01:45 UTC

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

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Guillermo Casanova committed Oct 19, 2026 at 15:44 UTC b459f2ad4e07bcedb88bd8e38038a642555a838f
4 files changed +46 -38
apps/web/src/components/ChatView.tsx
+36 -32
@@ -325,7 +325,7 @@ import {
325 clearBackgroundDraftSubmissionByRef,
326 finalizePromotedDraftThreadByRef,
327 markPromotedDraftThreadByRef,
328 - restoreFailedBackgroundDraftThread,
328 + restoreFailedDraftThread,
329 composerDraftHasUserContent,
330 type ComposerFileAttachment,
331 type ComposerImageAttachment,
@@ -6845,6 +6845,11 @@ export default function ChatView(props: ChatViewProps) {
6845 }),
6846 );
6847 }
6848 + const sentDraftThreadRef = isLocalDraftThread
6849 + ? scopeThreadRef(environmentId, threadIdForSend)
6850 + : null;
6851 + // Keep new-thread flows from reusing this draft while attachments and worktree setup settle.
6852 + if (sentDraftThreadRef) markPromotedDraftThreadByRef(sentDraftThreadRef);
6853 promptRef.current = "";
6854 clearComposerDraftContent(composerDraftTarget);
6855 composerRef.current?.resetCursorState();
@@ -6998,7 +7003,6 @@ export default function ChatView(props: ChatViewProps) {
7003 },
7004 });
7005 if (backgroundThreadRef) {
7001 - markPromotedDraftThreadByRef(backgroundThreadRef);
7006 try {
7007 backgroundDraftOpened = Boolean(
7008 await handleNewThread(
@@ -7032,12 +7036,15 @@ export default function ChatView(props: ChatViewProps) {
7036 releaseDraftAttachments(composerAttachmentsSnapshot);
7037 }
7038 acknowledgeActiveThreadWoke();
7035 - if (backgroundThreadRef) {
7039 + // A draft still on screen finalizes once its thread route takes over.
7040 + if (sentDraftThreadRef) {
7041 if (backgroundDraftOpened || currentRouteThreadKeyRef.current !== routeThreadKey) {
7037 - finalizePromotedDraftThreadByRef(backgroundThreadRef);
7042 + finalizePromotedDraftThreadByRef(sentDraftThreadRef);
7043 } else {
7039 - clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
7044 + clearBackgroundDraftSubmissionByRef(sentDraftThreadRef);
7045 }
7046 + }
7047 + if (backgroundThreadRef) {
7048 if (backgroundDraftOpened) {
7049 toastManager.add(
7050 stackedThreadToast({
@@ -7066,8 +7073,8 @@ export default function ChatView(props: ChatViewProps) {
7073 }
7074
7075 if (failure !== null) {
7069 - if (submissionIntent === "background" && draftId && draftThread) {
7070 - restoreFailedBackgroundDraftThread(
7076 + if (isLocalDraftThread && draftId && draftThread) {
7077 + restoreFailedDraftThread(
7078 draftId,
7079 draftThread,
7080 wasBootstrapThreadDeleted(squashAtomCommandFailure(failure))
@@ -7077,20 +7084,9 @@ export default function ChatView(props: ChatViewProps) {
7084 clearBackgroundDraftSubmissionByRef(scopeThreadRef(environmentId, threadIdForSend));
7085 }
7086 if (
7080 - backgroundDraftOpened
7081 - ? !composerDraftHasUserContent(
7082 - useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),
7083 - )
7084 - : promptRef.current.length === 0 &&
7085 - composerImagesRef.current.length === 0 &&
7086 - composerFilesRef.current.length === 0 &&
7087 - composerTerminalContextsRef.current.length === 0 &&
7088 - (useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)
7089 - ?.previewAnnotations.length ?? 0) === 0 &&
7090 - (useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.reviewComments
7091 - .length ?? 0) === 0 &&
7092 - (useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.threadContexts
7093 - .length ?? 0) === 0
7087 + !composerDraftHasUserContent(
7088 + useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),
7089 + )
7090 ) {
7091 setOptimisticUserMessages((existing) => {
7092 const removed = existing.filter((message) => message.id === messageIdForSend);
@@ -7100,11 +7096,7 @@ export default function ChatView(props: ChatViewProps) {
7096 const next = existing.filter((message) => message.id !== messageIdForSend);
7097 return next.length === existing.length ? existing : next;
7098 });
7103 - promptRef.current = messageTextForSend;
7099 const retryComposerImages = composerImagesSnapshot.map(cloneComposerImageForRetry);
7105 - composerImagesRef.current = retryComposerImages;
7106 - composerFilesRef.current = composerFilesSnapshot;
7107 - composerTerminalContextsRef.current = composerTerminalContextsSnapshot;
7100 setComposerDraftPrompt(composerDraftTarget, messageTextForSend);
7101 addComposerDraftImages(composerDraftTarget, retryComposerImages);
7102 addComposerDraftFiles(composerDraftTarget, composerFilesSnapshot);
@@ -7112,11 +7104,17 @@ export default function ChatView(props: ChatViewProps) {
7104 setComposerDraftPreviewAnnotations(composerDraftTarget, composerPreviewAnnotationsSnapshot);
7105 setComposerDraftReviewComments(composerDraftTarget, composerReviewCommentsSnapshot);
7106 setComposerDraftThreadContexts(composerDraftTarget, composerThreadContextsSnapshot);
7115 - composerRef.current?.resetCursorState({
7116 - cursor: collapseExpandedComposerCursor(messageTextForSend, messageTextForSend.length),
7117 - prompt: messageTextForSend,
7118 - detectTrigger: true,
7119 - });
7107 + if (currentRouteThreadKeyRef.current === routeThreadKey) {
7108 + promptRef.current = messageTextForSend;
7109 + composerImagesRef.current = retryComposerImages;
7110 + composerFilesRef.current = composerFilesSnapshot;
7111 + composerTerminalContextsRef.current = composerTerminalContextsSnapshot;
7112 + composerRef.current?.resetCursorState({
7113 + cursor: collapseExpandedComposerCursor(messageTextForSend, messageTextForSend.length),
7114 + prompt: messageTextForSend,
7115 + detectTrigger: true,
7116 + });
7117 + }
7118 }
7119 if (!isAtomCommandInterrupted(failure)) {
7120 const error = squashAtomCommandFailure(failure);
@@ -7124,11 +7122,17 @@ export default function ChatView(props: ChatViewProps) {
7122 threadIdForSend,
7123 error instanceof Error ? error.message : "Failed to send message.",
7124 );
7127 - if (backgroundDraftOpened && draftId) {
7125 + if (
7126 + draftId &&
7127 + (backgroundDraftOpened || currentRouteThreadKeyRef.current !== routeThreadKey)
7128 + ) {
7129 toastManager.add(
7130 stackedThreadToast({
7131 type: "error",
7131 - title: "Background task failed",
7132 + title:
7133 + submissionIntent === "background"
7134 + ? "Background task failed"
7135 + : "Could not start thread",
7136 description: error instanceof Error ? error.message : "Failed to send message.",
7137 actionProps: {
7138 children: "Open draft",
apps/web/src/components/ThreadRouteView.tsx
+2 -1
@@ -35,6 +35,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
35 draftId === null ? null : store.getDraftSession(draftId),
36 );
37 const threadRefs = useThreadRefs();
38 + // Promotion can begin before the server creates the reserved thread.
39 const inferredThreadRef = draftSession
40 ? (threadRefs.find(
41 (ref) =>
@@ -43,7 +44,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
44 ) ?? null)
45 : null;
46 const serverThreadRef: ScopedThreadRef | null =
46 - target.kind === "server" ? target.threadRef : (draftSession?.promotedTo ?? inferredThreadRef);
47 + target.kind === "server" ? target.threadRef : inferredThreadRef;
48 const serverThread = useQuietThreadShell(serverThreadRef);
49 const backgroundSubmissionPending = useBackgroundDraftSubmissionPending(
50 target.kind === "draft" ? serverThreadRef : null,
apps/web/src/composerDraftStore.test.ts
+5 -3
@@ -69,7 +69,7 @@ import {
69 clearBackgroundDraftSubmissionByRef,
70 finalizePromotedDraftThreadByRef,
71 markPromotedDraftThreadByRef,
72 - restoreFailedBackgroundDraftThread,
72 + restoreFailedDraftThread,
73 type ComposerFileAttachment,
74 type ComposerImageAttachment,
75 composerFileNeedsReattach,
@@ -1643,7 +1643,7 @@ describe("composerDraftStore project draft thread mapping", () => {
1643 expect(useComposerDraftStore.getState().getDraftSession(draftId)).toBeNull();
1644 });
1645
1646 - it("restores a failed background draft without changing the fresh draft", () => {
1646 + it("restores a failed draft without changing the fresh draft", () => {
1647 const store = useComposerDraftStore.getState();
1648 store.setProjectDraftThreadId(projectRef, draftId, { threadId });
1649 const sentDraft = useComposerDraftStore.getState().getDraftSession(draftId)!;
@@ -1651,12 +1651,14 @@ describe("composerDraftStore project draft thread mapping", () => {
1651 const freshId = DraftId.make("fresh-background-draft");
1652 store.setProjectDraftThreadId(projectRef, freshId, { threadId: ThreadId.make("fresh-thread") });
1653 store.setPrompt(freshId, "new work");
1654 - restoreFailedBackgroundDraftThread(draftId, sentDraft, ThreadId.make("retry-thread"));
1654 + store.setDraftThreadContext(draftId, { branch: "retry-base" });
1655 + restoreFailedDraftThread(draftId, sentDraft, ThreadId.make("retry-thread"));
1656 store.setPrompt(draftId, "retry work");
1657 expect(useComposerDraftStore.getState().getDraftSession(draftId)?.promotedTo).toBeNull();
1658 expect(useComposerDraftStore.getState().getDraftSession(draftId)?.threadId).toBe(
1659 "retry-thread",
1660 );
1661 + expect(useComposerDraftStore.getState().getDraftSession(draftId)?.branch).toBe("retry-base");
1662 expect(draftByKey(freshId)?.prompt).toBe("new work");
1663 expect(draftByKey(draftId)?.prompt).toBe("retry work");
1664 });
apps/web/src/composerDraftStore.ts
+3 -2
@@ -4270,7 +4270,7 @@ export function markPromotedDraftThreadByRef(threadRef: ScopedThreadRef): void {
4270 }
4271 }
4272
4273 -export function restoreFailedBackgroundDraftThread(
4273 +export function restoreFailedDraftThread(
4274 draftId: DraftId,
4275 draftThread: DraftThreadState,
4276 threadId: ThreadId,
@@ -4279,8 +4279,9 @@ export function restoreFailedBackgroundDraftThread(
4279 draftThreadsByThreadKey: {
4280 ...state.draftThreadsByThreadKey,
4281 [draftId]: {
4282 - ...draftThread,
4282 + ...(state.draftThreadsByThreadKey[draftId] ?? draftThread),
4283 threadId,
4284 + ...(threadId !== draftThread.threadId ? { createdAt: new Date().toISOString() } : {}),
4285 promotedTo: null,
4286 },
4287 },