@@ -31,6 +31,12 @@ const state = vi.hoisted(() => ({
3131 manager : null as unknown as ReturnType <
3232 typeof import ( "./thread-outbox-manager" ) . createThreadOutboxManager
3333 > ,
34+ // Encoded records, as the app's storage holds them across restarts.
35+ stored : new Map < string , unknown > ( ) ,
36+ beforeWrite : null as ( ( ) => Promise < void > ) | null ,
37+ createManager : null as unknown as ( ) => ReturnType <
38+ typeof import ( "./thread-outbox-manager" ) . createThreadOutboxManager
39+ > ,
3440 thread : {
3541 environmentId : "secondary" ,
3642 id : "thread" ,
@@ -109,17 +115,33 @@ vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => "abcd" })
109115vi . mock ( "../lib/attachmentUpload" , ( ) => ( { prepareTurnAttachments : state . prepare } ) ) ;
110116vi . mock ( "./thread-outbox" , async ( ) => {
111117 const { createThreadOutboxManager } = await import ( "./thread-outbox-manager" ) ;
118+ const { decodeQueuedThreadMessage, encodeQueuedThreadMessage } =
119+ await import ( "./thread-outbox-model" ) ;
112120 const { appAtomRegistry } = await import ( "./atom-registry" ) ;
113- state . manager = createThreadOutboxManager ( {
114- registry : appAtomRegistry ,
115- storage : {
116- load : async ( ) => ( { messages : [ ] , errors : [ ] } ) ,
117- write : async ( ) => { } ,
118- remove : async ( ) => { } ,
119- } ,
120- } ) ;
121+ state . createManager = ( ) =>
122+ createThreadOutboxManager ( {
123+ registry : appAtomRegistry ,
124+ storage : {
125+ load : async ( ) => ( {
126+ messages : [ ...state . stored . values ( ) ] . map ( ( record ) => decodeQueuedThreadMessage ( record ) ) ,
127+ errors : [ ] ,
128+ } ) ,
129+ write : async ( message ) => {
130+ await state . beforeWrite ?.( ) ;
131+ state . stored . set ( message . messageId , encodeQueuedThreadMessage ( message ) ) ;
132+ } ,
133+ remove : async ( message ) => {
134+ state . stored . delete ( message . messageId ) ;
135+ } ,
136+ } ,
137+ } ) ;
138+ state . manager = state . createManager ( ) ;
121139 return {
122- threadOutboxManager : state . manager ,
140+ // A restart swaps the manager; delivery keeps its module-level reference.
141+ threadOutboxManager : new Proxy (
142+ { } ,
143+ { get : ( _target , key ) => state . manager [ key as keyof typeof state . manager ] } ,
144+ ) ,
123145 threadOutboxRevision : ( id : MessageId ) => state . manager . revisionOf ( id ) ,
124146 confirmThreadOutboxMessageQueued : ( message : QueuedThreadMessage ) =>
125147 state . manager . confirmQueued ( message ) ,
@@ -212,6 +234,8 @@ beforeEach(() => {
212234 setOperateGrant ( "primary" , true ) ;
213235 setOperateGrant ( "secondary" , false ) ;
214236 state . draftText = "Keep this draft separate" ;
237+ state . stored . clear ( ) ;
238+ state . beforeWrite = null ;
215239 state . config . shellThreadFamilies = false ;
216240 state . modelUnavailable . mockReset ( ) . mockReturnValue ( false ) ;
217241 state . familyMount . mockReset ( ) . mockReturnValue ( ( ) => { } ) ;
@@ -553,25 +577,37 @@ describe("compaction before a queued send", () => {
553577} ) ;
554578
555579describe ( "a new task whose launch reply was lost" , ( ) => {
580+ const queuedCreation = ( ) =>
581+ message ( {
582+ threadId : ThreadId . make ( "new-thread" ) ,
583+ modelSelection : { instanceId : ProviderInstanceId . make ( "codex" ) , model : "gpt-5.4" } ,
584+ creation : {
585+ projectId : ProjectId . make ( "project" ) ,
586+ projectCwd : "/repo" ,
587+ workspaceMode : "local" ,
588+ branch : null ,
589+ worktreePath : null ,
590+ } ,
591+ } ) ;
592+ const lostReply = ( ) => AsyncResult . failure ( Cause . fail ( new Error ( "Socket is not connected" ) ) ) ;
593+
556594 it . each ( [
557- { change : "unchanged" , edit : null , recovered : null } ,
558- { change : "edited" , edit : { text : "Edited after launch" } , recovered : "Edited after launch" } ,
595+ { change : "unchanged" , edit : null , relaunch : "accepted" , starts : 2 , kept : 0 , recovered : null } ,
596+ // The shell can exist without the first message: the server commits them separately.
597+ { change : "unchanged" , edit : null , relaunch : "lost" , starts : 2 , kept : 1 , recovered : null } ,
598+ {
599+ change : "edited" ,
600+ edit : { text : "Edited after launch" } ,
601+ relaunch : null ,
602+ starts : 1 ,
603+ kept : 0 ,
604+ recovered : "Edited after launch" ,
605+ } ,
559606 ] ) (
560- "drops the launched task once its thread appears, recovering only edits ($change)" ,
561- async ( { edit, recovered } ) => {
607+ "after a restart, settles a created thread only on an accepted launch ($change, relaunch $relaunch )" ,
608+ async ( { edit, relaunch , starts , kept , recovered } ) => {
562609 setOperateGrant ( "secondary" , true ) ;
563- const queued = message ( {
564- threadId : ThreadId . make ( "new-thread" ) ,
565- modelSelection : { instanceId : ProviderInstanceId . make ( "codex" ) , model : "gpt-5.4" } ,
566- creation : {
567- projectId : ProjectId . make ( "project" ) ,
568- projectCwd : "/repo" ,
569- workspaceMode : "local" ,
570- branch : null ,
571- worktreePath : null ,
572- } ,
573- } ) ;
574- await state . manager . enqueue ( queued ) ;
610+ await state . manager . enqueue ( queuedCreation ( ) ) ;
575611 const started = Promise . withResolvers < void > ( ) ;
576612 const reply = Promise . withResolvers < AsyncResult . AsyncResult < unknown , Error > > ( ) ;
577613 state . start . mockImplementationOnce ( ( ) => {
@@ -580,27 +616,61 @@ describe("a new task whose launch reply was lost", () => {
580616 } ) ;
581617 const drained = runDrain ( ) ;
582618 await started . promise ;
583- // The launch committed, so its thread reaches the shell, but the reply never arrives.
584619 appAtomRegistry . set ( state . threadShells , [
585620 state . thread ,
586621 { ...state . thread , id : "new-thread" } ,
587622 ] ) ;
588- reply . resolve ( AsyncResult . failure ( Cause . fail ( new Error ( "Socket is not connected" ) ) ) ) ;
623+ reply . resolve ( lostReply ( ) ) ;
589624 await drained ;
590- expect ( remaining ( ) ) . toHaveLength ( 1 ) ;
591625 if ( edit !== null ) {
592- await state . manager . update ( { ...remaining ( ) [ 0 ] ! , ...edit } ) ;
626+ expect ( await state . manager . update ( { ...remaining ( ) [ 0 ] ! , ...edit } ) ) . toBe ( true ) ;
593627 }
594628
595- // A relaunched app finds the thread already created .
629+ // A relaunched app hydrates a fresh manager from the stored records .
596630 state . cleanups . splice ( 0 ) . forEach ( ( cleanup ) => cleanup ( ) ) ;
631+ state . manager = state . createManager ( ) ;
632+ expect ( await state . manager . load ( ) ) . toBe ( true ) ;
633+ expect ( remaining ( ) ) . toHaveLength ( 1 ) ;
634+ expect ( remaining ( ) [ 0 ] ! . dispatchedContentKey ) . toBeDefined ( ) ;
635+ if ( relaunch === "lost" ) state . start . mockResolvedValueOnce ( lostReply ( ) ) ;
597636 await runDrain ( ) ;
598637
599- expect ( state . start ) . toHaveBeenCalledOnce ( ) ;
600- expect ( remaining ( ) ) . toEqual ( [ ] ) ;
638+ expect ( state . start ) . toHaveBeenCalledTimes ( starts ) ;
639+ if ( starts === 2 ) {
640+ expect ( state . start . mock . calls [ 1 ] ! [ 0 ] . input ) . toMatchObject ( {
641+ commandId : "command" ,
642+ message : { messageId : "message" , text : "Keep this queued" } ,
643+ } ) ;
644+ }
645+ expect ( remaining ( ) ) . toHaveLength ( kept ) ;
646+ expect ( state . stored . size ) . toBe ( kept ) ;
601647 expect ( state . draftText ) . toBe (
602648 recovered === null ? "Keep this draft separate" : `Keep this draft separate\n${ recovered } ` ,
603649 ) ;
604650 } ,
605651 ) ;
652+
653+ it . each ( [
654+ {
655+ change : "a deletion holds the task" ,
656+ apply : ( ) => appAtomRegistry . set ( editingQueuedMessageIdsAtom , { message : true } ) ,
657+ } ,
658+ { change : "operation access is revoked" , apply : ( ) => setOperateGrant ( "secondary" , false ) } ,
659+ ] ) ( "does not launch when $change while the launch is being recorded" , async ( { apply } ) => {
660+ setOperateGrant ( "secondary" , true ) ;
661+ await state . manager . enqueue ( queuedCreation ( ) ) ;
662+ const writing = Promise . withResolvers < void > ( ) ;
663+ const write = Promise . withResolvers < void > ( ) ;
664+ state . beforeWrite = ( ) => {
665+ writing . resolve ( ) ;
666+ return write . promise ;
667+ } ;
668+ const drained = runDrain ( ) ;
669+ await writing . promise ;
670+ apply ( ) ;
671+ write . resolve ( ) ;
672+ await drained ;
673+ expect ( state . start ) . not . toHaveBeenCalled ( ) ;
674+ expect ( remaining ( ) ) . toHaveLength ( 1 ) ;
675+ } ) ;
606676} ) ;
0 commit comments