@@ -250,6 +250,53 @@ test("broker keeps a resumed thread subscribed until its final client closes", a
250250 await waitForUnsubscribes ( broker . statePath , [ threadId ] ) ;
251251} ) ;
252252
253+ test ( "broker serializes a resume behind an in-flight unsubscribe" , async ( t ) => {
254+ const broker = startBroker ( "unsubscribe-delayed" ) ;
255+ t . after ( ( ) => broker . stop ( ) ) ;
256+ assert . equal ( await broker . listening ( ) , true , `broker never listened: ${ broker . stderr ( ) } ` ) ;
257+
258+ const firstClient = await connectClient ( broker . socketPath ) ;
259+ const threadId = ( await firstClient . request ( "thread/start" , { cwd : process . cwd ( ) , ephemeral : false } ) ) . thread . id ;
260+ await firstClient . end ( ) ;
261+ const unsubscribeStarted = await waitFor ( ( ) =>
262+ readState ( broker . statePath ) ?. unsubscribeRequests ?. includes ( threadId )
263+ ) ;
264+ assert . equal ( unsubscribeStarted , true , "unsubscribe request was not observed" ) ;
265+
266+ const secondClient = await connectClient ( broker . socketPath ) ;
267+ await secondClient . request ( "thread/resume" , { threadId } ) ;
268+ assert . deepEqual ( readState ( broker . statePath ) . requestOrder , [ "unsubscribe:response" , "thread/resume" ] ) ;
269+ assert . deepEqual ( readState ( broker . statePath ) . subscriptions , [ threadId ] ) ;
270+
271+ await secondClient . end ( ) ;
272+ await waitForUnsubscribes ( broker . statePath , [ threadId , threadId ] ) ;
273+ assert . deepEqual ( readState ( broker . statePath ) . subscriptions , [ ] ) ;
274+ } ) ;
275+
276+ test ( "broker cancels a retry when a client reacquires the thread" , async ( t ) => {
277+ const broker = startBroker ( "unsubscribe-fails-once" ) ;
278+ t . after ( ( ) => broker . stop ( ) ) ;
279+ assert . equal ( await broker . listening ( ) , true , `broker never listened: ${ broker . stderr ( ) } ` ) ;
280+
281+ const firstClient = await connectClient ( broker . socketPath ) ;
282+ const threadId = ( await firstClient . request ( "thread/start" , { cwd : process . cwd ( ) , ephemeral : false } ) ) . thread . id ;
283+ await firstClient . end ( ) ;
284+ const firstAttemptObserved = await waitFor (
285+ ( ) => readState ( broker . statePath ) ?. unsubscribeRequests ?. length === 1
286+ ) ;
287+ assert . equal ( firstAttemptObserved , true , "initial unsubscribe request was not observed" ) ;
288+
289+ const secondClient = await connectClient ( broker . socketPath ) ;
290+ await secondClient . request ( "thread/resume" , { threadId } ) ;
291+ await delay ( 3000 ) ;
292+ assert . deepEqual ( readState ( broker . statePath ) . subscriptions , [ threadId ] ) ;
293+
294+ await secondClient . end ( ) ;
295+ const unsubscribed = await waitFor ( ( ) => readState ( broker . statePath ) ?. subscriptions ?. length === 0 ) ;
296+ assert . equal ( unsubscribed , true , "reacquired thread was not unsubscribed after its final owner closed" ) ;
297+ assert . equal ( readState ( broker . statePath ) . unsubscribeRequests . at ( - 1 ) , threadId ) ;
298+ } ) ;
299+
253300test ( "broker unsubscribes source and detached review threads" , async ( t ) => {
254301 const broker = startBroker ( ) ;
255302 t . after ( ( ) => broker . stop ( ) ) ;
@@ -448,6 +495,40 @@ test("broker serializes subscription requests from one downstream client", async
448495 await waitForUnsubscribes ( broker . statePath , [ threadId ] ) ;
449496} ) ;
450497
498+ test ( "broker replies to a failed resume even when the upstream unsubscribe hangs" , async ( t ) => {
499+ const broker = startBroker ( "resume-fails-unsubscribe-hangs" ) ;
500+ t . after ( ( ) => broker . stop ( ) ) ;
501+ assert . equal ( await broker . listening ( ) , true , `broker never listened: ${ broker . stderr ( ) } ` ) ;
502+
503+ // Nobody owns this thread, so the failed provisional claim releases it upstream.
504+ const threadId = "thr_unowned" ;
505+ const firstClient = await connectClient ( broker . socketPath ) ;
506+ const failedResume = await waitWithTimeout (
507+ firstClient . request ( "thread/resume" , { threadId, persistFullHistory : true } ) . then (
508+ ( ) => ( { status : "fulfilled" } ) ,
509+ ( error ) => ( { status : "rejected" , error } )
510+ ) ,
511+ 2000
512+ ) ;
513+ assert . notEqual ( failedResume , null , "failed resume did not receive a response" ) ;
514+ assert . equal ( failedResume . status , "rejected" ) ;
515+ assert . match ( failedResume . error . message , / f o r c e d r e s u m e f a i l u r e / ) ;
516+
517+ const secondClient = await connectClient ( broker . socketPath ) ;
518+ const secondStarted = await waitWithTimeout (
519+ secondClient . request ( "thread/start" , { cwd : process . cwd ( ) , ephemeral : false } ) ,
520+ 2000
521+ ) ;
522+ assert . notEqual ( secondStarted , null , "broker remained busy after the failed resume" ) ;
523+
524+ const unsubscribeStarted = await waitFor ( ( ) =>
525+ readState ( broker . statePath ) ?. unsubscribeRequests ?. includes ( threadId )
526+ ) ;
527+ assert . equal ( unsubscribeStarted , true , "the released thread was not unsubscribed upstream" ) ;
528+ await secondClient . end ( ) ;
529+ await firstClient . end ( ) ;
530+ } ) ;
531+
451532test ( "broker keeps shared upstream subscriptions when one client explicitly unsubscribes" , async ( t ) => {
452533 const broker = startBroker ( ) ;
453534 t . after ( ( ) => broker . stop ( ) ) ;
@@ -503,6 +584,13 @@ test("broker logs upstream unsubscribe failures", async (t) => {
503584 ( ) => broker . stderr ( ) . includes ( `Failed to unsubscribe Codex thread ${ threadId } : thread unsubscribe failed` )
504585 ) ;
505586 assert . equal ( warningObserved , true , "unsubscribe failure was not logged" ) ;
587+ const retryBoundReached = await waitFor (
588+ ( ) => readState ( broker . statePath ) ?. unsubscribeRequests ?. length === 4 ,
589+ { timeoutMs : 6000 }
590+ ) ;
591+ assert . equal ( retryBoundReached , true , "unsubscribe retries did not reach the expected bound" ) ;
592+ await delay ( 500 ) ;
593+ assert . equal ( readState ( broker . statePath ) . unsubscribeRequests . length , 4 ) ;
506594 assert . deepEqual ( readState ( broker . statePath ) . subscriptions , [ threadId ] ) ;
507595} ) ;
508596
@@ -523,3 +611,33 @@ test("broker retries a transient upstream unsubscribe failure", async (t) => {
523611 assert . deepEqual ( readState ( broker . statePath ) . unsubscribeRequests , [ threadId , threadId ] ) ;
524612 assert . match ( broker . stderr ( ) , new RegExp ( `Failed to unsubscribe Codex thread ${ threadId } ` ) ) ;
525613} ) ;
614+
615+ test ( "broker bounds the wait for a hung unsubscribe before a resume proceeds" , async ( t ) => {
616+ const broker = startBroker ( "resume-fails-unsubscribe-hangs" ) ;
617+ t . after ( ( ) => broker . stop ( ) ) ;
618+ assert . equal ( await broker . listening ( ) , true , `broker never listened: ${ broker . stderr ( ) } ` ) ;
619+
620+ const firstClient = await connectClient ( broker . socketPath ) ;
621+ const threadId = ( await firstClient . request ( "thread/start" , { cwd : process . cwd ( ) , ephemeral : false } ) ) . thread . id ;
622+ await firstClient . end ( ) ;
623+ const unsubscribeStarted = await waitFor ( ( ) =>
624+ readState ( broker . statePath ) ?. unsubscribeRequests ?. includes ( threadId )
625+ ) ;
626+ assert . equal ( unsubscribeStarted , true , "unsubscribe request was not observed" ) ;
627+
628+ const secondClient = await connectClient ( broker . socketPath ) ;
629+ const startedAt = Date . now ( ) ;
630+ const resumed = await waitWithTimeout ( secondClient . request ( "thread/resume" , { threadId } ) , 8000 ) ;
631+ assert . notEqual ( resumed , null , "resume never completed while the upstream unsubscribe hung" ) ;
632+ assert . ok ( Date . now ( ) - startedAt >= 4000 , "resume did not wait for the in-flight unsubscribe" ) ;
633+ assert . deepEqual ( readState ( broker . statePath ) . subscriptions , [ threadId ] ) ;
634+
635+ const thirdClient = await connectClient ( broker . socketPath ) ;
636+ const thirdStarted = await waitWithTimeout (
637+ thirdClient . request ( "thread/start" , { cwd : process . cwd ( ) , ephemeral : false } ) ,
638+ 2000
639+ ) ;
640+ assert . notEqual ( thirdStarted , null , "broker remained busy after the bounded wait" ) ;
641+ await thirdClient . end ( ) ;
642+ await secondClient . end ( ) ;
643+ } ) ;
0 commit comments