@@ -44,7 +44,11 @@ import {
4444 updateThreadOutboxMessage ,
4545 type QueuedThreadMessage ,
4646} from "../../state/thread-outbox" ;
47- import { setEditingQueuedMessageId , useThreadOutboxMessages } from "../../state/use-thread-outbox" ;
47+ import {
48+ clearEditingQueuedMessageId ,
49+ setEditingQueuedMessageId ,
50+ useThreadOutboxMessages ,
51+ } from "../../state/use-thread-outbox" ;
4852import {
4953 setPendingConnectionError ,
5054 useSavedRemoteConnections ,
@@ -58,6 +62,11 @@ function pendingTaskDraftKey(messageId: string): string {
5862 return `pending-task:${ messageId } ` ;
5963}
6064
65+ // Bumped on every beginEditingPendingTask, across provider instances. An
66+ // in-flight flush from a dismissed session compares against it so it never
67+ // clears the draft or drain lock out from under a newer editing session.
68+ let editingSessionGeneration = 0 ;
69+
6170function findQueuedPendingTask ( messageId : string ) : QueuedThreadMessage | null {
6271 const message = flattenQueuedThreadMessages (
6372 appAtomRegistry . get ( threadOutboxManager . queuedMessagesByThreadKeyAtom ) ,
@@ -579,6 +588,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
579588 }
580589 setSelectedEnvironmentId ( message . environmentId ) ;
581590 setSelectedProjectKey ( scopedProjectKey ( message . environmentId , message . creation . projectId ) ) ;
591+ editingSessionGeneration += 1 ;
582592 editingPendingTaskRef . current = message ;
583593 setEditingPendingTask ( message ) ;
584594 // Hold the outbox drain off this task while it is open in the editor.
@@ -599,6 +609,17 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
599609 }
600610 const workspaceSelection = draft . workspaceSelection ;
601611 const mode = workspaceSelection ?. mode ?? "local" ;
612+ // When the selection is the stand-in built from the queued snapshot,
613+ // persist the original (possibly absent) snapshot values — the
614+ // stand-in's placeholder title/workspaceRoot must never be written back
615+ // as if they were real project metadata.
616+ const usingPendingSnapshot = selectedProject === editingPendingProject ;
617+ const projectTitle = usingPendingSnapshot
618+ ? editingPendingTask ?. creation ?. projectTitle
619+ : selectedProject . title ;
620+ const projectCwd = usingPendingSnapshot
621+ ? editingPendingTask ?. creation ?. projectCwd
622+ : selectedProject . workspaceRoot ;
602623 return {
603624 environmentId : selectedProject . environmentId ,
604625 threadId : ThreadId . make ( metadata . threadId ) ,
@@ -611,8 +632,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
611632 interactionMode : draft . interactionMode ?? DEFAULT_PROVIDER_INTERACTION_MODE ,
612633 creation : {
613634 projectId : selectedProject . id ,
614- projectTitle : selectedProject . title ,
615- projectCwd : selectedProject . workspaceRoot ,
635+ ... ( projectTitle !== undefined ? { projectTitle } : { } ) ,
636+ ... ( projectCwd !== undefined ? { projectCwd } : { } ) ,
616637 workspaceMode : mode ,
617638 branch : workspaceSelection ?. branch ?? null ,
618639 worktreePath : mode === "worktree" ? null : ( workspaceSelection ?. worktreePath ?? null ) ,
@@ -621,7 +642,13 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
621642 createdAt : metadata . createdAt ,
622643 } ;
623644 } ,
624- [ selectedModel , selectedProject , selectedProjectDraftKey ] ,
645+ [
646+ editingPendingProject ,
647+ editingPendingTask ,
648+ selectedModel ,
649+ selectedProject ,
650+ selectedProjectDraftKey ,
651+ ] ,
625652 ) ;
626653
627654 const finishEditingPendingTask = useCallback ( ( ) => {
@@ -671,26 +698,32 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
671698 createdAt : editing . createdAt ,
672699 } ) ;
673700
674- if ( message ) {
675- // update() rewrites the task only if it is still queued — a concurrent
676- // delete or delivery wins, so the flush cannot resurrect it. The drain
677- // lock is released only once the updated payload is durable, and the
678- // editing draft is discarded only once the save succeeded (a failed
679- // save keeps it around so the edits rehydrate on the next open).
680- void updateThreadOutboxMessage ( message )
681- . then ( ( ) => {
682- clearComposerDraft ( pendingTaskDraftKey ( editing . messageId ) ) ;
683- } )
684- . catch ( ( error ) => {
685- console . warn ( "[new-task] failed to save edited pending task" , error ) ;
686- } )
687- . finally ( ( ) => {
688- setEditingQueuedMessageId ( null ) ;
689- } ) ;
690- } else {
691- clearComposerDraft ( pendingTaskDraftKey ( editing . messageId ) ) ;
692- setEditingQueuedMessageId ( null ) ;
701+ if ( ! message ) {
702+ // The edits are currently unsendable (e.g. the prompt was cleared).
703+ // Keep both the draft and the drain lock: the stale queued payload
704+ // must not auto-send content the user just removed, and reopening the
705+ // task resumes from the saved draft.
706+ return ;
693707 }
708+
709+ // update() rewrites the task only if it is still queued — a concurrent
710+ // delete or delivery wins, so the flush cannot resurrect it.
711+ const flushGeneration = editingSessionGeneration ;
712+ void updateThreadOutboxMessage ( message )
713+ . then ( ( ) => {
714+ // If any editing session started (possibly in a fresh provider)
715+ // while the save was in flight, it owns the draft and drain lock.
716+ if ( editingSessionGeneration !== flushGeneration ) {
717+ return ;
718+ }
719+ clearComposerDraft ( pendingTaskDraftKey ( editing . messageId ) ) ;
720+ clearEditingQueuedMessageId ( editing . messageId ) ;
721+ } )
722+ . catch ( ( error ) => {
723+ // Keep the drain lock and the draft: delivering the stale payload
724+ // would silently drop the newer edits. Reopening the task retries.
725+ console . warn ( "[new-task] failed to save edited pending task" , error ) ;
726+ } ) ;
694727 } ;
695728 } , [ buildPendingTaskMessage ] ) ;
696729 const cancelEditingPendingTask = useCallback ( ( ) => {
0 commit comments