@@ -336,4 +336,60 @@ describe("Codex app-server main thread cleanup", () => {
336336 ) ;
337337 await expect ( readCodexAppServerBinding ( sessionFile ) ) . resolves . toBeUndefined ( ) ;
338338 } ) ;
339+
340+ it ( "retires a Codex client when a failed turn cannot unsubscribe its thread" , async ( ) => {
341+ const sessionFile = path . join ( tempDir , "session.jsonl" ) ;
342+ const workspaceDir = path . join ( tempDir , "workspace" ) ;
343+ const sessionKey = "agent:main:dashboard:incognito-failed-unsubscribe" ;
344+ const requests : string [ ] = [ ] ;
345+ const request = vi . fn ( async ( method : string ) => {
346+ requests . push ( method ) ;
347+ if ( method === "thread/start" ) {
348+ return threadStartResult ( ) ;
349+ }
350+ if ( method === "turn/start" ) {
351+ throw new Error ( "turn start exploded" ) ;
352+ }
353+ if ( method === "thread/unsubscribe" ) {
354+ throw new Error ( "thread unsubscribe failed" ) ;
355+ }
356+ return { } ;
357+ } ) ;
358+ const createClient = vi . fn ( async ( ) => {
359+ return {
360+ ...mockClientRuntimeMethods ( ) ,
361+ request,
362+ close : vi . fn ( ) ,
363+ addNotificationHandler : ( ) => ( ) => undefined ,
364+ addRequestHandler : ( ) => ( ) => undefined ,
365+ addCloseHandler : ( ) => ( ) => undefined ,
366+ } as never ;
367+ } ) ;
368+ const clientFactory : CodexAppServerClientFactory = multiplexedClientFactory ( createClient ) ;
369+
370+ await expect (
371+ runCodexAppServerAttempt ( createParams ( sessionFile , workspaceDir , sessionKey ) , {
372+ bindingStore : testCodexAppServerBindingStore ,
373+ clientFactory,
374+ } ) ,
375+ ) . rejects . toThrow ( "turn start exploded" ) ;
376+
377+ await expect (
378+ runCodexAppServerAttempt ( createParams ( sessionFile , workspaceDir , sessionKey ) , {
379+ bindingStore : testCodexAppServerBindingStore ,
380+ clientFactory,
381+ } ) ,
382+ ) . rejects . toThrow ( "turn start exploded" ) ;
383+
384+ expect ( requests ) . toEqual ( [
385+ "thread/start" ,
386+ "turn/start" ,
387+ "thread/unsubscribe" ,
388+ "thread/start" ,
389+ "turn/start" ,
390+ "thread/unsubscribe" ,
391+ ] ) ;
392+ expect ( createClient ) . toHaveBeenCalledTimes ( 2 ) ;
393+ await expect ( readCodexAppServerBinding ( sessionFile ) ) . resolves . toBeUndefined ( ) ;
394+ } ) ;
339395} ) ;
0 commit comments