@@ -45,8 +45,8 @@ import {
4545 type QueuedThreadMessage ,
4646} from "../../state/thread-outbox" ;
4747import {
48- clearEditingQueuedMessageId ,
49- setEditingQueuedMessageId ,
48+ holdEditingQueuedMessage ,
49+ releaseEditingQueuedMessage ,
5050 useThreadOutboxMessages ,
5151} from "../../state/use-thread-outbox" ;
5252import {
@@ -62,10 +62,11 @@ function pendingTaskDraftKey(messageId: string): string {
6262 return `pending-task:${ messageId } ` ;
6363}
6464
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 ;
65+ // The message id owned by the currently active editing session, tracked
66+ // across provider instances. An in-flight flush from a dismissed session
67+ // consults it so it never drops the draft or releases the drain lock out from
68+ // under a newer session editing the same task.
69+ let activeEditingMessageId : string | null = null ;
6970
7071function findQueuedPendingTask ( messageId : string ) : QueuedThreadMessage | null {
7172 const message = flattenQueuedThreadMessages (
@@ -219,9 +220,15 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
219220 setSubmitting ( false ) ;
220221 setBranchQuery ( "" ) ;
221222 setExpandedProvider ( null ) ;
223+ const editing = editingPendingTaskRef . current ;
222224 editingPendingTaskRef . current = null ;
223225 setEditingPendingTask ( null ) ;
224- setEditingQueuedMessageId ( null ) ;
226+ if ( editing ) {
227+ if ( activeEditingMessageId === editing . messageId ) {
228+ activeEditingMessageId = null ;
229+ }
230+ releaseEditingQueuedMessage ( editing . messageId ) ;
231+ }
225232 } , [ ] ) ;
226233
227234 const environments = useMemo (
@@ -279,7 +286,10 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
279286 environmentId : editingPendingTask . environmentId ,
280287 id : creation . projectId ,
281288 title : creation . projectTitle ?? "Unknown project" ,
282- workspaceRoot : creation . projectCwd ?? String ( creation . projectId ) ,
289+ // Deliberately empty when the snapshot has no cwd — downstream consumers
290+ // (branch queries, worktree bootstrap) must skip it, not receive a
291+ // fabricated path.
292+ workspaceRoot : creation . projectCwd ?? "" ,
283293 repositoryIdentity : null ,
284294 defaultModelSelection : editingPendingTask . modelSelection ?? null ,
285295 scripts : [ ] ,
@@ -435,7 +445,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
435445 const branchTarget = useMemo (
436446 ( ) => ( {
437447 environmentId : selectedProject ?. environmentId ?? null ,
438- cwd : selectedProject ?. workspaceRoot ?? null ,
448+ // `|| null` also skips the stand-in project's empty workspaceRoot.
449+ cwd : selectedProject ?. workspaceRoot || null ,
439450 query : null ,
440451 } ) ,
441452 [ selectedProject ?. environmentId , selectedProject ?. workspaceRoot ] ,
@@ -588,11 +599,11 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
588599 }
589600 setSelectedEnvironmentId ( message . environmentId ) ;
590601 setSelectedProjectKey ( scopedProjectKey ( message . environmentId , message . creation . projectId ) ) ;
591- editingSessionGeneration += 1 ;
602+ activeEditingMessageId = message . messageId ;
592603 editingPendingTaskRef . current = message ;
593604 setEditingPendingTask ( message ) ;
594605 // Hold the outbox drain off this task while it is open in the editor.
595- setEditingQueuedMessageId ( message . messageId ) ;
606+ holdEditingQueuedMessage ( message . messageId ) ;
596607 return true ;
597608 } , [ ] ) ;
598609
@@ -655,10 +666,13 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
655666 const editing = editingPendingTaskRef . current ;
656667 editingPendingTaskRef . current = null ;
657668 if ( editing ) {
669+ if ( activeEditingMessageId === editing . messageId ) {
670+ activeEditingMessageId = null ;
671+ }
658672 clearComposerDraft ( pendingTaskDraftKey ( editing . messageId ) ) ;
673+ releaseEditingQueuedMessage ( editing . messageId ) ;
659674 }
660675 setEditingPendingTask ( null ) ;
661- setEditingQueuedMessageId ( null ) ;
662676 } , [ ] ) ;
663677
664678 // If the queued task disappears mid-edit (deleted from the list, or
@@ -690,6 +704,9 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
690704 }
691705 editingPendingTaskRef . current = null ;
692706 setEditingPendingTask ( null ) ;
707+ if ( activeEditingMessageId === editing . messageId ) {
708+ activeEditingMessageId = null ;
709+ }
693710
694711 const message = buildPendingTaskMessage ( {
695712 threadId : editing . threadId ,
@@ -708,16 +725,15 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
708725
709726 // update() rewrites the task only if it is still queued — a concurrent
710727 // delete or delivery wins, so the flush cannot resurrect it.
711- const flushGeneration = editingSessionGeneration ;
712728 void updateThreadOutboxMessage ( message )
713729 . 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 ) {
730+ // If this task was reopened (possibly in a fresh provider) while
731+ // the save was in flight, that session owns the draft and the lock.
732+ if ( activeEditingMessageId === editing . messageId ) {
717733 return ;
718734 }
719735 clearComposerDraft ( pendingTaskDraftKey ( editing . messageId ) ) ;
720- clearEditingQueuedMessageId ( editing . messageId ) ;
736+ releaseEditingQueuedMessage ( editing . messageId ) ;
721737 } )
722738 . catch ( ( error ) => {
723739 // Keep the drain lock and the draft: delivering the stale payload
0 commit comments