@@ -470,6 +470,154 @@ describe("startSession", () => {
470470 manager . stopAll ( ) ;
471471 }
472472 } ) ;
473+
474+ it ( "disposes an existing session before starting a replacement for the same thread" , async ( ) => {
475+ const manager = new CodexAppServerManager ( ) ;
476+ const existingContext = {
477+ session : {
478+ provider : "codex" ,
479+ status : "ready" ,
480+ threadId : asThreadId ( "thread-1" ) ,
481+ runtimeMode : "full-access" ,
482+ createdAt : "2026-02-10T00:00:00.000Z" ,
483+ updatedAt : "2026-02-10T00:00:00.000Z" ,
484+ } ,
485+ } ;
486+
487+ (
488+ manager as unknown as {
489+ sessions : Map < ThreadId , typeof existingContext > ;
490+ }
491+ ) . sessions . set ( asThreadId ( "thread-1" ) , existingContext ) ;
492+
493+ const disposeSession = vi
494+ . spyOn (
495+ manager as unknown as {
496+ disposeSession : (
497+ context : typeof existingContext ,
498+ options ?: { readonly emitLifecycleEvent ?: boolean } ,
499+ ) => void ;
500+ } ,
501+ "disposeSession" ,
502+ )
503+ . mockImplementation ( ( ) => { } ) ;
504+ const assertSupportedCodexCliVersion = vi
505+ . spyOn (
506+ manager as unknown as {
507+ assertSupportedCodexCliVersion : ( input : {
508+ binaryPath : string ;
509+ cwd : string ;
510+ homePath ?: string ;
511+ } ) => void ;
512+ } ,
513+ "assertSupportedCodexCliVersion" ,
514+ )
515+ . mockImplementation ( ( ) => { } ) ;
516+ const processCwd = vi . spyOn ( process , "cwd" ) . mockImplementation ( ( ) => {
517+ throw new Error ( "cwd missing" ) ;
518+ } ) ;
519+
520+ try {
521+ await expect (
522+ manager . startSession ( {
523+ threadId : asThreadId ( "thread-1" ) ,
524+ provider : "codex" ,
525+ binaryPath : "codex" ,
526+ runtimeMode : "full-access" ,
527+ } ) ,
528+ ) . rejects . toThrow ( "cwd missing" ) ;
529+
530+ expect ( disposeSession ) . toHaveBeenCalledWith ( existingContext , {
531+ emitLifecycleEvent : false ,
532+ } ) ;
533+ expect ( assertSupportedCodexCliVersion ) . not . toHaveBeenCalled ( ) ;
534+ } finally {
535+ disposeSession . mockRestore ( ) ;
536+ assertSupportedCodexCliVersion . mockRestore ( ) ;
537+ processCwd . mockRestore ( ) ;
538+ (
539+ manager as unknown as {
540+ sessions : Map < ThreadId , typeof existingContext > ;
541+ }
542+ ) . sessions . clear ( ) ;
543+ manager . stopAll ( ) ;
544+ }
545+ } ) ;
546+
547+ it ( "continues replacement start when existing session disposal fails" , async ( ) => {
548+ const manager = new CodexAppServerManager ( ) ;
549+ const existingContext = {
550+ session : {
551+ provider : "codex" ,
552+ status : "ready" ,
553+ threadId : asThreadId ( "thread-1" ) ,
554+ runtimeMode : "full-access" ,
555+ createdAt : "2026-02-10T00:00:00.000Z" ,
556+ updatedAt : "2026-02-10T00:00:00.000Z" ,
557+ } ,
558+ } ;
559+
560+ (
561+ manager as unknown as {
562+ sessions : Map < ThreadId , typeof existingContext > ;
563+ }
564+ ) . sessions . set ( asThreadId ( "thread-1" ) , existingContext ) ;
565+
566+ const disposeSession = vi
567+ . spyOn (
568+ manager as unknown as {
569+ disposeSession : (
570+ context : typeof existingContext ,
571+ options ?: { readonly emitLifecycleEvent ?: boolean } ,
572+ ) => void ;
573+ } ,
574+ "disposeSession" ,
575+ )
576+ . mockImplementation ( ( ) => {
577+ throw new Error ( "dispose failed" ) ;
578+ } ) ;
579+ const assertSupportedCodexCliVersion = vi
580+ . spyOn (
581+ manager as unknown as {
582+ assertSupportedCodexCliVersion : ( input : {
583+ binaryPath : string ;
584+ cwd : string ;
585+ homePath ?: string ;
586+ } ) => void ;
587+ } ,
588+ "assertSupportedCodexCliVersion" ,
589+ )
590+ . mockImplementation ( ( ) => { } ) ;
591+ const processCwd = vi . spyOn ( process , "cwd" ) . mockImplementation ( ( ) => {
592+ throw new Error ( "cwd missing" ) ;
593+ } ) ;
594+
595+ try {
596+ await expect (
597+ manager . startSession ( {
598+ threadId : asThreadId ( "thread-1" ) ,
599+ provider : "codex" ,
600+ binaryPath : "codex" ,
601+ runtimeMode : "full-access" ,
602+ } ) ,
603+ ) . rejects . toThrow ( "cwd missing" ) ;
604+
605+ expect ( disposeSession ) . toHaveBeenCalledWith ( existingContext , {
606+ emitLifecycleEvent : false ,
607+ } ) ;
608+ expect ( assertSupportedCodexCliVersion ) . not . toHaveBeenCalled ( ) ;
609+ } finally {
610+ disposeSession . mockRestore ( ) ;
611+ assertSupportedCodexCliVersion . mockRestore ( ) ;
612+ processCwd . mockRestore ( ) ;
613+ (
614+ manager as unknown as {
615+ sessions : Map < ThreadId , typeof existingContext > ;
616+ }
617+ ) . sessions . clear ( ) ;
618+ manager . stopAll ( ) ;
619+ }
620+ } ) ;
473621} ) ;
474622
475623describe ( "sendTurn" , ( ) => {
0 commit comments