@@ -12,6 +12,7 @@ import {
1212} from "@t3tools/contracts" ;
1313import {
1414 CommandId ,
15+ CheckpointRef ,
1516 DEFAULT_PROVIDER_INTERACTION_MODE ,
1617 EventId ,
1718 MessageId ,
@@ -667,6 +668,167 @@ describe("CheckpointReactor", () => {
667668 } ) ,
668669 ) ;
669670
671+ effectIt . effect . each ( [ "turn.completed" , "turn.aborted" ] as const ) (
672+ "captures every edit after a mid-turn diff update on %s" ,
673+ ( terminalEventType ) =>
674+ Effect . gen ( function * ( ) {
675+ const harness = yield * Effect . promise ( ( ) =>
676+ createHarness ( { seedFilesystemCheckpoints : false } ) ,
677+ ) ;
678+ const threadId = ThreadId . make ( "thread-1" ) ;
679+ const turnId = asTurnId ( "turn-1" ) ;
680+ const assistantMessageId = MessageId . make ( "assistant:mid-turn" ) ;
681+ const createdAt = "2026-01-01T00:00:00.000Z" ;
682+ yield * harness . engine . dispatch ( {
683+ type : "thread.session.set" ,
684+ commandId : CommandId . make ( "cmd-mid-turn-running" ) ,
685+ threadId,
686+ session : {
687+ threadId,
688+ status : "running" ,
689+ providerName : "codex" ,
690+ runtimeMode : "approval-required" ,
691+ activeTurnId : turnId ,
692+ lastError : null ,
693+ updatedAt : createdAt ,
694+ } ,
695+ createdAt,
696+ } ) ;
697+ harness . provider . emit ( {
698+ type : "turn.started" ,
699+ eventId : EventId . make ( "evt-mid-turn-start" ) ,
700+ provider : ProviderDriverKind . make ( "codex" ) ,
701+ createdAt,
702+ threadId,
703+ turnId,
704+ } ) ;
705+ expect ( yield * harness . nextReceipt ) . toMatchObject ( {
706+ type : "checkpoint.baseline.captured" ,
707+ } ) ;
708+
709+ NodeFS . writeFileSync ( NodePath . join ( harness . cwd , "early.ts" ) , "export const early = 1;\n" ) ;
710+ yield * harness . engine . dispatch ( {
711+ type : "thread.turn.diff.complete" ,
712+ commandId : CommandId . make ( "cmd-mid-turn-diff" ) ,
713+ threadId,
714+ turnId,
715+ completedAt : createdAt ,
716+ checkpointRef : CheckpointRef . make ( "provider-diff:mid-turn" ) ,
717+ assistantMessageId,
718+ status : "missing" ,
719+ files : [ ] ,
720+ checkpointTurnCount : 1 ,
721+ createdAt,
722+ } ) ;
723+ yield * Effect . promise ( harness . drain ) ;
724+
725+ NodeFS . writeFileSync ( NodePath . join ( harness . cwd , "late.ts" ) , "export const late = 2;\n" ) ;
726+ yield * harness . engine . dispatch ( {
727+ type : "thread.session.set" ,
728+ commandId : CommandId . make ( "cmd-mid-turn-settled" ) ,
729+ threadId,
730+ session : {
731+ threadId,
732+ status : terminalEventType === "turn.aborted" ? "interrupted" : "ready" ,
733+ providerName : "codex" ,
734+ runtimeMode : "approval-required" ,
735+ activeTurnId : null ,
736+ lastError : null ,
737+ updatedAt : createdAt ,
738+ } ,
739+ createdAt,
740+ } ) ;
741+ harness . provider . emit ( {
742+ eventId : EventId . make ( "evt-mid-turn-complete" ) ,
743+ provider : ProviderDriverKind . make ( "codex" ) ,
744+ createdAt,
745+ threadId,
746+ turnId,
747+ ...( terminalEventType === "turn.completed"
748+ ? { type : "turn.completed" , payload : { state : "completed" } }
749+ : { type : "turn.aborted" , payload : { reason : "Interrupted by user." } } ) ,
750+ } ) ;
751+ yield * Effect . promise ( harness . drain ) ;
752+ expect ( gitRefExists ( harness . cwd , checkpointRefForThreadTurn ( threadId , 1 ) ) ) . toBe ( true ) ;
753+ expect ( yield * harness . nextReceipt ) . toMatchObject ( {
754+ type : "checkpoint.diff.finalized" ,
755+ turnId,
756+ checkpointTurnCount : 1 ,
757+ } ) ;
758+ expect ( yield * harness . nextReceipt ) . toMatchObject ( {
759+ type : "turn.processing.quiesced" ,
760+ turnId,
761+ } ) ;
762+ yield * Effect . promise ( harness . drain ) ;
763+ const thread = ( yield * Effect . promise ( harness . readModel ) ) . threads . find (
764+ ( entry ) => entry . id === threadId ,
765+ ) ;
766+ expect ( thread ?. checkpoints ) . toHaveLength ( 1 ) ;
767+ expect ( thread ?. checkpoints [ 0 ] ?. status ) . toBe ( "ready" ) ;
768+ expect ( thread ?. latestTurn ?. state ) . toBe (
769+ terminalEventType === "turn.aborted" ? "interrupted" : "completed" ,
770+ ) ;
771+ expect ( thread ?. checkpoints [ 0 ] ?. assistantMessageId ) . toBe ( assistantMessageId ) ;
772+ expect ( thread ?. checkpoints [ 0 ] ?. files . map ( ( file ) => file . path ) ) . toEqual ( [
773+ "early.ts" ,
774+ "late.ts" ,
775+ ] ) ;
776+ expect (
777+ gitShowFileAtRef ( harness . cwd , checkpointRefForThreadTurn ( threadId , 1 ) , "late.ts" ) ,
778+ ) . toBe ( "export const late = 2;\n" ) ;
779+
780+ const followUpTurnId = asTurnId ( "turn-2" ) ;
781+ harness . provider . emit ( {
782+ type : "turn.started" ,
783+ eventId : EventId . make ( "evt-follow-up-start" ) ,
784+ provider : ProviderDriverKind . make ( "codex" ) ,
785+ createdAt,
786+ threadId,
787+ turnId : followUpTurnId ,
788+ } ) ;
789+ harness . provider . emit ( {
790+ type : "turn.completed" ,
791+ eventId : EventId . make ( "evt-follow-up-complete" ) ,
792+ provider : ProviderDriverKind . make ( "codex" ) ,
793+ createdAt,
794+ threadId,
795+ turnId : followUpTurnId ,
796+ payload : { state : "completed" } ,
797+ } ) ;
798+ expect ( yield * harness . nextReceipt ) . toMatchObject ( {
799+ type : "checkpoint.diff.finalized" ,
800+ turnId : followUpTurnId ,
801+ checkpointTurnCount : 2 ,
802+ } ) ;
803+ const followUp = ( yield * Effect . promise ( harness . readModel ) ) . threads . find (
804+ ( entry ) => entry . id === threadId ,
805+ ) ;
806+ expect (
807+ followUp ?. checkpoints . find ( ( checkpoint ) => checkpoint . turnId === followUpTurnId ) ,
808+ ) . toMatchObject ( { checkpointTurnCount : 2 , files : [ ] } ) ;
809+ } ) ,
810+ ) ;
811+
812+ it ( "does not capture an aborted turn without a matching start or active session" , async ( ) => {
813+ const harness = await createHarness ( { seedFilesystemCheckpoints : false } ) ;
814+ harness . provider . emit ( {
815+ type : "turn.aborted" ,
816+ eventId : EventId . make ( "evt-untracked-abort" ) ,
817+ provider : ProviderDriverKind . make ( "codex" ) ,
818+ createdAt : "2026-01-01T00:00:00.000Z" ,
819+ threadId : ThreadId . make ( "thread-1" ) ,
820+ turnId : asTurnId ( "turn-untracked" ) ,
821+ payload : { reason : "Interrupted before the turn started." } ,
822+ } ) ;
823+ await harness . drain ( ) ;
824+
825+ const thread = ( await harness . readModel ( ) ) . threads . find ( ( entry ) => entry . id === "thread-1" ) ;
826+ expect ( thread ?. checkpoints ) . toEqual ( [ ] ) ;
827+ expect (
828+ gitRefExists ( harness . cwd , checkpointRefForThreadTurn ( ThreadId . make ( "thread-1" ) , 1 ) ) ,
829+ ) . toBe ( false ) ;
830+ } ) ;
831+
670832 it ( "refreshes local git status state on turn completion using the session cwd" , async ( ) => {
671833 const gitStatusRefreshCalls : string [ ] = [ ] ;
672834 const harness = await createHarness ( {
0 commit comments