@@ -6824,6 +6824,11 @@ export default function ChatView(props: ChatViewProps) {
68246824 } ) ,
68256825 ) ;
68266826 }
6827+ const sentDraftThreadRef = isLocalDraftThread
6828+ ? scopeThreadRef ( environmentId , threadIdForSend )
6829+ : null ;
6830+ // Keep new-thread flows from reusing this draft while attachments and worktree setup settle.
6831+ if ( sentDraftThreadRef ) markPromotedDraftThreadByRef ( sentDraftThreadRef ) ;
68276832 promptRef . current = "" ;
68286833 clearComposerDraftContent ( composerDraftTarget ) ;
68296834 composerRef . current ?. resetCursorState ( ) ;
@@ -6985,15 +6990,6 @@ export default function ChatView(props: ChatViewProps) {
69856990 createdAt : messageCreatedAt ,
69866991 } ,
69876992 } ) ;
6988- // The composer is already cleared, so until the draft is marked promoting
6989- // new-thread flows would treat it as an empty draft and reuse it while
6990- // the worktree is still being prepared.
6991- const sentDraftThreadRef = isLocalDraftThread
6992- ? scopeThreadRef ( environmentId , threadIdForSend )
6993- : null ;
6994- if ( sentDraftThreadRef ) {
6995- markPromotedDraftThreadByRef ( sentDraftThreadRef ) ;
6996- }
69976993 if ( backgroundThreadRef ) {
69986994 try {
69996995 backgroundDraftOpened = Boolean (
@@ -7069,29 +7065,16 @@ export default function ChatView(props: ChatViewProps) {
70697065 restoreFailedDraftThread (
70707066 draftId ,
70717067 draftThread ,
7072- // Foreground sends rotate a deleted thread's id below, with a fresh createdAt.
7073- submissionIntent === "background" &&
7074- wasBootstrapThreadDeleted ( squashAtomCommandFailure ( failure ) )
7068+ wasBootstrapThreadDeleted ( squashAtomCommandFailure ( failure ) )
70757069 ? newThreadId ( )
70767070 : threadIdForSend ,
70777071 ) ;
70787072 clearBackgroundDraftSubmissionByRef ( scopeThreadRef ( environmentId , threadIdForSend ) ) ;
70797073 }
70807074 if (
7081- backgroundDraftOpened
7082- ? ! composerDraftHasUserContent (
7083- useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget ) ,
7084- )
7085- : promptRef . current . length === 0 &&
7086- composerImagesRef . current . length === 0 &&
7087- composerFilesRef . current . length === 0 &&
7088- composerTerminalContextsRef . current . length === 0 &&
7089- ( useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget )
7090- ?. previewAnnotations . length ?? 0 ) === 0 &&
7091- ( useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget ) ?. reviewComments
7092- . length ?? 0 ) === 0 &&
7093- ( useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget ) ?. threadContexts
7094- . length ?? 0 ) === 0
7075+ ! composerDraftHasUserContent (
7076+ useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget ) ,
7077+ )
70957078 ) {
70967079 setOptimisticUserMessages ( ( existing ) => {
70977080 const removed = existing . filter ( ( message ) => message . id === messageIdForSend ) ;
@@ -7101,35 +7084,43 @@ export default function ChatView(props: ChatViewProps) {
71017084 const next = existing . filter ( ( message ) => message . id !== messageIdForSend ) ;
71027085 return next . length === existing . length ? existing : next ;
71037086 } ) ;
7104- promptRef . current = messageTextForSend ;
71057087 const retryComposerImages = composerImagesSnapshot . map ( cloneComposerImageForRetry ) ;
7106- composerImagesRef . current = retryComposerImages ;
7107- composerFilesRef . current = composerFilesSnapshot ;
7108- composerTerminalContextsRef . current = composerTerminalContextsSnapshot ;
71097088 setComposerDraftPrompt ( composerDraftTarget , messageTextForSend ) ;
71107089 addComposerDraftImages ( composerDraftTarget , retryComposerImages ) ;
71117090 addComposerDraftFiles ( composerDraftTarget , composerFilesSnapshot ) ;
71127091 setComposerDraftTerminalContexts ( composerDraftTarget , composerTerminalContextsSnapshot ) ;
71137092 setComposerDraftPreviewAnnotations ( composerDraftTarget , composerPreviewAnnotationsSnapshot ) ;
71147093 setComposerDraftReviewComments ( composerDraftTarget , composerReviewCommentsSnapshot ) ;
71157094 setComposerDraftThreadContexts ( composerDraftTarget , composerThreadContextsSnapshot ) ;
7116- composerRef . current ?. resetCursorState ( {
7117- cursor : collapseExpandedComposerCursor ( messageTextForSend , messageTextForSend . length ) ,
7118- prompt : messageTextForSend ,
7119- detectTrigger : true ,
7120- } ) ;
7095+ if ( currentRouteThreadKeyRef . current === routeThreadKey ) {
7096+ promptRef . current = messageTextForSend ;
7097+ composerImagesRef . current = retryComposerImages ;
7098+ composerFilesRef . current = composerFilesSnapshot ;
7099+ composerTerminalContextsRef . current = composerTerminalContextsSnapshot ;
7100+ composerRef . current ?. resetCursorState ( {
7101+ cursor : collapseExpandedComposerCursor ( messageTextForSend , messageTextForSend . length ) ,
7102+ prompt : messageTextForSend ,
7103+ detectTrigger : true ,
7104+ } ) ;
7105+ }
71217106 }
71227107 if ( ! isAtomCommandInterrupted ( failure ) ) {
71237108 const error = squashAtomCommandFailure ( failure ) ;
71247109 setThreadError (
71257110 threadIdForSend ,
71267111 error instanceof Error ? error . message : "Failed to send message." ,
71277112 ) ;
7128- if ( backgroundDraftOpened && draftId ) {
7113+ if (
7114+ draftId &&
7115+ ( backgroundDraftOpened || currentRouteThreadKeyRef . current !== routeThreadKey )
7116+ ) {
71297117 toastManager . add (
71307118 stackedThreadToast ( {
71317119 type : "error" ,
7132- title : "Background task failed" ,
7120+ title :
7121+ submissionIntent === "background"
7122+ ? "Background task failed"
7123+ : "Could not start thread" ,
71337124 description : error instanceof Error ? error . message : "Failed to send message." ,
71347125 actionProps : {
71357126 children : "Open draft" ,
0 commit comments