@@ -530,6 +530,142 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
530530 ) . toBe ( true ) ;
531531 } ) ;
532532
533+ it ( "does not clear local dispatch while the session is running a newer turn than latestTurn" , ( ) => {
534+ const localDispatch = createLocalDispatchSnapshot ( {
535+ id : ThreadId . make ( "thread-1" ) ,
536+ environmentId : localEnvironmentId ,
537+ codexThreadId : null ,
538+ projectId,
539+ title : "Thread" ,
540+ modelSelection : { provider : "codex" , model : "gpt-5.4" } ,
541+ runtimeMode : "full-access" ,
542+ interactionMode : "default" ,
543+ session : previousSession ,
544+ messages : [ ] ,
545+ proposedPlans : [ ] ,
546+ error : null ,
547+ createdAt : "2026-03-29T00:00:00.000Z" ,
548+ archivedAt : null ,
549+ updatedAt : "2026-03-29T00:00:10.000Z" ,
550+ latestTurn : previousLatestTurn ,
551+ branch : null ,
552+ worktreePath : null ,
553+ turnDiffSummaries : [ ] ,
554+ activities : [ ] ,
555+ } ) ;
556+
557+ expect (
558+ hasServerAcknowledgedLocalDispatch ( {
559+ localDispatch,
560+ phase : "running" ,
561+ latestTurn : previousLatestTurn ,
562+ session : {
563+ ...previousSession ,
564+ status : "running" ,
565+ orchestrationStatus : "running" ,
566+ activeTurnId : TurnId . make ( "turn-2" ) ,
567+ updatedAt : "2026-03-29T00:01:00.000Z" ,
568+ } ,
569+ hasPendingApproval : false ,
570+ hasPendingUserInput : false ,
571+ threadError : null ,
572+ } ) ,
573+ ) . toBe ( false ) ;
574+ } ) ;
575+
576+ it ( "does not clear local dispatch while the session is running but latestTurn has not advanced yet" , ( ) => {
577+ const localDispatch = createLocalDispatchSnapshot ( {
578+ id : ThreadId . make ( "thread-1" ) ,
579+ environmentId : localEnvironmentId ,
580+ codexThreadId : null ,
581+ projectId,
582+ title : "Thread" ,
583+ modelSelection : { provider : "codex" , model : "gpt-5.4" } ,
584+ runtimeMode : "full-access" ,
585+ interactionMode : "default" ,
586+ session : previousSession ,
587+ messages : [ ] ,
588+ proposedPlans : [ ] ,
589+ error : null ,
590+ createdAt : "2026-03-29T00:00:00.000Z" ,
591+ archivedAt : null ,
592+ updatedAt : "2026-03-29T00:00:10.000Z" ,
593+ latestTurn : previousLatestTurn ,
594+ branch : null ,
595+ worktreePath : null ,
596+ turnDiffSummaries : [ ] ,
597+ activities : [ ] ,
598+ } ) ;
599+
600+ expect (
601+ hasServerAcknowledgedLocalDispatch ( {
602+ localDispatch,
603+ phase : "running" ,
604+ latestTurn : previousLatestTurn ,
605+ session : {
606+ ...previousSession ,
607+ status : "running" ,
608+ orchestrationStatus : "running" ,
609+ activeTurnId : undefined ,
610+ updatedAt : "2026-03-29T00:01:00.000Z" ,
611+ } ,
612+ hasPendingApproval : false ,
613+ hasPendingUserInput : false ,
614+ threadError : null ,
615+ } ) ,
616+ ) . toBe ( false ) ;
617+ } ) ;
618+
619+ it ( "clears local dispatch once the running latestTurn matches the active session turn" , ( ) => {
620+ const localDispatch = createLocalDispatchSnapshot ( {
621+ id : ThreadId . make ( "thread-1" ) ,
622+ environmentId : localEnvironmentId ,
623+ codexThreadId : null ,
624+ projectId,
625+ title : "Thread" ,
626+ modelSelection : { provider : "codex" , model : "gpt-5.4" } ,
627+ runtimeMode : "full-access" ,
628+ interactionMode : "default" ,
629+ session : previousSession ,
630+ messages : [ ] ,
631+ proposedPlans : [ ] ,
632+ error : null ,
633+ createdAt : "2026-03-29T00:00:00.000Z" ,
634+ archivedAt : null ,
635+ updatedAt : "2026-03-29T00:00:10.000Z" ,
636+ latestTurn : previousLatestTurn ,
637+ branch : null ,
638+ worktreePath : null ,
639+ turnDiffSummaries : [ ] ,
640+ activities : [ ] ,
641+ } ) ;
642+
643+ expect (
644+ hasServerAcknowledgedLocalDispatch ( {
645+ localDispatch,
646+ phase : "running" ,
647+ latestTurn : {
648+ ...previousLatestTurn ,
649+ turnId : TurnId . make ( "turn-2" ) ,
650+ state : "running" ,
651+ requestedAt : "2026-03-29T00:01:00.000Z" ,
652+ startedAt : "2026-03-29T00:01:01.000Z" ,
653+ completedAt : null ,
654+ } ,
655+ session : {
656+ ...previousSession ,
657+ status : "running" ,
658+ orchestrationStatus : "running" ,
659+ activeTurnId : TurnId . make ( "turn-2" ) ,
660+ updatedAt : "2026-03-29T00:01:01.000Z" ,
661+ } ,
662+ hasPendingApproval : false ,
663+ hasPendingUserInput : false ,
664+ threadError : null ,
665+ } ) ,
666+ ) . toBe ( true ) ;
667+ } ) ;
668+
533669 it ( "clears local dispatch when the session changes without an observed running phase" , ( ) => {
534670 const localDispatch = createLocalDispatchSnapshot ( {
535671 id : ThreadId . make ( "thread-1" ) ,
0 commit comments