@@ -252,10 +252,7 @@ export async function runCodex(opts: {
252252 }
253253
254254 abortController . abort ( ) ;
255- messageQueue . reset ( ) ;
256- permissionHandler . reset ( ) ;
257255 reasoningProcessor . abort ( ) ;
258- diffProcessor . reset ( ) ;
259256 logger . debug ( '[Codex] Abort completed - session remains active' ) ;
260257 } catch ( error ) {
261258 logger . debug ( '[Codex] Error during abort:' , error ) ;
@@ -292,6 +289,13 @@ export async function runCodex(opts: {
292289 await session . close ( ) ;
293290 }
294291
292+ // Force close Codex transport (best-effort) so we don't leave stray processes
293+ try {
294+ await client . forceCloseSession ( ) ;
295+ } catch ( e ) {
296+ logger . debug ( '[Codex] Error while force closing Codex session during termination' , e ) ;
297+ }
298+
295299 // Stop caffeinate
296300 stopCaffeinate ( ) ;
297301
@@ -713,11 +717,9 @@ export async function runCodex(opts: {
713717 if ( isAbortError ) {
714718 messageBuffer . addMessage ( 'Aborted by user' , 'status' ) ;
715719 session . sendSessionEvent ( { type : 'message' , message : 'Aborted by user' } ) ;
716- // Session was already stored in handleAbort(), no need to store again
717- // Mark session as not created to force proper resume on next message
718- wasCreated = false ;
719- currentModeHash = null ;
720- logger . debug ( '[Codex] Marked session as not created after abort for proper resume' ) ;
720+ // Abort cancels the current task/inference but keeps the Codex session alive.
721+ // Do not clear session state here; the next user message should continue on the
722+ // existing session if possible.
721723 } else {
722724 messageBuffer . addMessage ( 'Process exited unexpectedly' , 'status' ) ;
723725 session . sendSessionEvent ( { type : 'message' , message : 'Process exited unexpectedly' } ) ;
@@ -767,9 +769,9 @@ export async function runCodex(opts: {
767769 } catch ( e ) {
768770 logger . debug ( '[codex]: Error while closing session' , e ) ;
769771 }
770- logger . debug ( '[codex]: client.disconnect begin' ) ;
771- await client . disconnect ( ) ;
772- logger . debug ( '[codex]: client.disconnect done' ) ;
772+ logger . debug ( '[codex]: client.forceCloseSession begin' ) ;
773+ await client . forceCloseSession ( ) ;
774+ logger . debug ( '[codex]: client.forceCloseSession done' ) ;
773775 // Stop Happy MCP server
774776 logger . debug ( '[codex]: happyServer.stop' ) ;
775777 happyServer . stop ( ) ;
0 commit comments