@@ -427,7 +427,7 @@ describe("ProviderRuntimeIngestion", () => {
427427 it . each ( [
428428 { source : "the previous turn" , turnId : asTurnId ( "opencode-stopped-turn" ) } ,
429429 { source : "an unspecified turn" , turnId : undefined } ,
430- ] ) ( "keeps a new OpenCode turn running after an abort for $source" , async ( lateAbort ) => {
430+ ] ) ( "ignores late OpenCode aborts for $source across newer turns " , async ( lateAbort ) => {
431431 const harness = await createHarness ( {
432432 serverSettings : { enableLegacyTokenStreaming : true } ,
433433 } ) ;
@@ -487,6 +487,78 @@ describe("ProviderRuntimeIngestion", () => {
487487 streaming : true ,
488488 } ) ,
489489 ] ) ;
490+
491+ harness . emit ( {
492+ ...base ,
493+ type : "turn.completed" ,
494+ eventId : asEventId ( "opencode-next-completed" ) ,
495+ turnId : nextTurnId ,
496+ createdAt : "2026-01-01T00:00:02.000Z" ,
497+ payload : { state : "completed" } ,
498+ } ) ;
499+ await harness . drain ( ) ;
500+
501+ const pendingAt = "2026-01-01T00:00:03.000Z" ;
502+ for ( const hasPendingStart of [ false , true ] ) {
503+ if ( hasPendingStart ) {
504+ await harness . dispatch ( {
505+ type : "thread.turn.start" ,
506+ commandId : CommandId . make ( "opencode-pending-start" ) ,
507+ threadId,
508+ message : {
509+ messageId : asMessageId ( "opencode-pending-message" ) ,
510+ role : "user" ,
511+ text : "Start another turn." ,
512+ attachments : [ ] ,
513+ } ,
514+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
515+ runtimeMode : "approval-required" ,
516+ createdAt : pendingAt ,
517+ } ) ;
518+ harness . emit ( {
519+ ...base ,
520+ type : "session.state.changed" ,
521+ eventId : asEventId ( "opencode-pending-starting" ) ,
522+ createdAt : pendingAt ,
523+ payload : { state : "starting" } ,
524+ } ) ;
525+ }
526+ harness . emit ( {
527+ ...base ,
528+ type : "turn.aborted" ,
529+ eventId : asEventId ( `opencode-late-abort-after-completion-${ hasPendingStart } ` ) ,
530+ ...( lateAbort . turnId ? { turnId : lateAbort . turnId } : { } ) ,
531+ createdAt : "2026-01-01T00:00:04.000Z" ,
532+ payload : { reason : "Interrupted by user." } ,
533+ } ) ;
534+ await harness . drain ( ) ;
535+
536+ const completedThread = ( await harness . readModel ( ) ) . threads . find (
537+ ( entry ) => entry . id === threadId ,
538+ ) ;
539+ expect ( completedThread ?. session ) . toMatchObject ( {
540+ status : hasPendingStart ? "starting" : "ready" ,
541+ activeTurnId : null ,
542+ } ) ;
543+ expect ( completedThread ?. latestTurn ) . toMatchObject ( { turnId : nextTurnId , state : "completed" } ) ;
544+ }
545+
546+ harness . emit ( {
547+ ...base ,
548+ type : "turn.started" ,
549+ eventId : asEventId ( "opencode-pending-started" ) ,
550+ turnId : asTurnId ( "opencode-pending-turn" ) ,
551+ createdAt : "2026-01-01T00:00:05.000Z" ,
552+ } ) ;
553+ await harness . drain ( ) ;
554+ const startedThread = ( await harness . readModel ( ) ) . threads . find (
555+ ( entry ) => entry . id === threadId ,
556+ ) ;
557+ expect ( startedThread ?. latestTurn ) . toMatchObject ( {
558+ turnId : asTurnId ( "opencode-pending-turn" ) ,
559+ state : "running" ,
560+ requestedAt : pendingAt ,
561+ } ) ;
490562 } ) ;
491563
492564 it ( "applies provider session.state.changed transitions directly" , async ( ) => {
0 commit comments