@@ -650,53 +650,117 @@ describe("CodexAppServerTurnRouter", () => {
650650 route . release ( ) ;
651651 } ) ;
652652
653- it ( "consumes one native completion and clears stale completion when arming" , async ( ) => {
653+ it ( "consumes buffered exact native completions and clears them when arming" , async ( ) => {
654654 const harness = createHarness ( ) ;
655- const route = getCodexAppServerTurnRouter ( harness . client ) . reserveThread ( {
655+ const router = getCodexAppServerTurnRouter ( harness . client ) ;
656+ const route = router . reserveThread ( {
656657 threadId : "thread-native" ,
657- onNotification : vi . fn ( ) ,
658658 } ) ;
659659 harness . send ( {
660660 method : "turn/completed" ,
661661 params : { threadId : "thread-native" , turn : { id : "turn-native" , items : [ ] } } ,
662662 } ) ;
663663 await settleInput ( ) ;
664-
665- await expect ( route . waitForTurnCompletion ( { timeoutMs : 10 } ) ) . resolves . toBe ( true ) ;
666- await expect ( route . waitForTurnCompletion ( { timeoutMs : 1 } ) ) . resolves . toBe ( false ) ;
664+ await route . activate ( { onNotification : vi . fn ( ) } ) ;
665+ expect ( route . observedNativeTurnId ) . toBe ( "turn-native" ) ;
666+ const completion = ( turnId : string , timeoutMs : number ) =>
667+ router . watchNativeTurnCompletion ( { threadId : route . threadId , turnId, timeoutMs } ) . completion ;
668+ await expect ( completion ( "turn-native" , 10 ) ) . resolves . toBe ( true ) ;
669+ await expect ( completion ( "turn-native" , 1 ) ) . resolves . toBe ( false ) ;
667670
668671 harness . send ( {
669672 method : "turn/completed" ,
670673 params : { threadId : "thread-native" , turn : { id : "turn-stale" , items : [ ] } } ,
671674 } ) ;
672675 await settleInput ( ) ;
673676 route . armTurn ( ) ;
674- await expect ( route . waitForTurnCompletion ( { timeoutMs : 1 } ) ) . resolves . toBe ( false ) ;
677+ expect ( route . observedNativeTurnId ) . toBeUndefined ( ) ;
678+ await expect ( completion ( "turn-stale" , 1 ) ) . resolves . toBe ( false ) ;
675679 await route . cancelTurn ( ) ;
676680 } ) ;
677681
678- it ( "settles an active native-completion waiter on completion, abort, and release " , async ( ) => {
682+ it ( "keeps an observed active native turn exact across arm and stale completion " , async ( ) => {
679683 const harness = createHarness ( ) ;
680- const route = getCodexAppServerTurnRouter ( harness . client ) . reserveThread ( {
684+ const router = getCodexAppServerTurnRouter ( harness . client ) ;
685+ const route = router . reserveThread ( {
686+ threadId : "thread-native-active" ,
687+ onNotification : vi . fn ( ) ,
688+ } ) ;
689+ harness . send ( {
690+ method : "turn/started" ,
691+ params : {
692+ threadId : "thread-native-active" ,
693+ turn : { id : "turn-compact" , status : "inProgress" } ,
694+ } ,
695+ } ) ;
696+ await settleInput ( ) ;
697+
698+ route . armTurn ( ) ;
699+ expect ( route . observedNativeTurnId ) . toBe ( "turn-compact" ) ;
700+ harness . send ( {
701+ method : "turn/completed" ,
702+ params : { threadId : "thread-native-active" , turn : { id : "turn-stale" , items : [ ] } } ,
703+ } ) ;
704+ await settleInput ( ) ;
705+ expect ( route . observedNativeTurnId ) . toBe ( "turn-compact" ) ;
706+
707+ const completed = router . watchNativeTurnCompletion ( {
708+ threadId : "thread-native-active" ,
709+ turnId : "turn-compact" ,
710+ timeoutMs : 100 ,
711+ } ) ;
712+ harness . send ( {
713+ method : "turn/completed" ,
714+ params : { threadId : "thread-native-active" , turn : { id : "turn-compact" , items : [ ] } } ,
715+ } ) ;
716+ await expect ( completed . completion ) . resolves . toBe ( true ) ;
717+ await route . cancelTurn ( ) ;
718+ } ) ;
719+
720+ it ( "settles exact native-completion watchers on completion, abort, and route release" , async ( ) => {
721+ const harness = createHarness ( ) ;
722+ const router = getCodexAppServerTurnRouter ( harness . client ) ;
723+ const route = router . reserveThread ( {
681724 threadId : "thread-native-wait" ,
682725 onNotification : vi . fn ( ) ,
683726 } ) ;
684727
685- const completed = route . waitForTurnCompletion ( { timeoutMs : 100 } ) ;
728+ const completed = router . watchNativeTurnCompletion ( {
729+ threadId : "thread-native-wait" ,
730+ turnId : "turn-native" ,
731+ timeoutMs : 100 ,
732+ } ) ;
686733 harness . send ( {
687734 method : "turn/completed" ,
688735 params : { threadId : "thread-native-wait" , turn : { id : "turn-native" , items : [ ] } } ,
689736 } ) ;
690- await expect ( completed ) . resolves . toBe ( true ) ;
737+ await expect ( completed . completion ) . resolves . toBe ( true ) ;
691738
692739 const controller = new AbortController ( ) ;
693- const aborted = route . waitForTurnCompletion ( { timeoutMs : 100 , signal : controller . signal } ) ;
740+ const aborted = router . watchNativeTurnCompletion ( {
741+ threadId : "thread-native-wait" ,
742+ turnId : "turn-aborted" ,
743+ timeoutMs : 100 ,
744+ signal : controller . signal ,
745+ } ) ;
694746 controller . abort ( "test" ) ;
695- await expect ( aborted ) . resolves . toBe ( false ) ;
747+ await expect ( aborted . completion ) . resolves . toBe ( false ) ;
748+ const alreadyAborted = router . watchNativeTurnCompletion ( {
749+ threadId : "thread-native-wait" ,
750+ turnId : "turn-aborted" ,
751+ timeoutMs : 100 ,
752+ signal : controller . signal ,
753+ } ) ;
754+ await expect ( alreadyAborted . completion ) . resolves . toBe ( false ) ;
696755
697- const released = route . waitForTurnCompletion ( { timeoutMs : 100 } ) ;
756+ const released = router . watchNativeTurnCompletion ( {
757+ threadId : "thread-native-wait" ,
758+ turnId : "turn-released" ,
759+ timeoutMs : 100 ,
760+ signal : route . signal ,
761+ } ) ;
698762 route . release ( ) ;
699- await expect ( released ) . resolves . toBe ( false ) ;
763+ await expect ( released . completion ) . resolves . toBe ( false ) ;
700764 } ) ;
701765
702766 it ( "watches one exact native turn without reserving its thread" , async ( ) => {
0 commit comments