@@ -762,6 +762,206 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
762762 } ) . pipe ( Effect . provide ( layerTest ) ) ,
763763) ;
764764
765+ it . effect ( "a compacted thread reads its recent runs, live runs, fresh starts and named runs" , ( ) =>
766+ Effect . gen ( function * ( ) {
767+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
768+ const threadId = yield * addRolledBackRecoveryCandidate ( "compacted" ) ;
769+ const now = yield * DateTime . now ;
770+ const nodeId = NodeId . make ( "node:compacted" ) ;
771+ const runId = ( ordinal : number ) => RunId . make ( `run:compacted:${ ordinal } ` ) ;
772+ const read = ( keepRunIds : ReadonlyArray < RunId > ) =>
773+ store . getThreadRecords ( threadId , [ "runs" , "messages" ] , {
774+ messageRoles : [ "user" ] ,
775+ sinceLastCompaction : { keepRunIds } ,
776+ } ) ;
777+ // Run 1 is the helper's rolled-back run. Run 2 is live and run 3 starts a fresh session.
778+ // Runs 6 and 9 are `/compact` runs that hold a compaction; run 8 is one that does not.
779+ for ( const [ ordinal , status ] of [
780+ [ 2 , "running" ] ,
781+ [ 3 , "completed" ] ,
782+ [ 4 , "completed" ] ,
783+ [ 5 , "completed" ] ,
784+ [ 6 , "completed" ] ,
785+ [ 7 , "completed" ] ,
786+ [ 8 , "completed" ] ,
787+ [ 9 , "completed" ] ,
788+ ] as const ) {
789+ yield * store . apply ( {
790+ id : EventId . make ( `event:compacted:run-${ ordinal } ` ) ,
791+ type : "run.created" ,
792+ threadId,
793+ runId : runId ( ordinal ) ,
794+ nodeId,
795+ driver,
796+ providerInstanceId,
797+ occurredAt : now ,
798+ payload : {
799+ id : runId ( ordinal ) ,
800+ threadId,
801+ ordinal,
802+ providerInstanceId,
803+ modelSelection,
804+ providerThreadId : null ,
805+ userMessageId : MessageId . make ( `message:compacted:${ ordinal } ` ) ,
806+ rootNodeId : nodeId ,
807+ activeAttemptId : null ,
808+ status,
809+ requestedAt : now ,
810+ startedAt : now ,
811+ completedAt : status === "running" ? null : now ,
812+ checkpointId : null ,
813+ contextHandoffId : null ,
814+ ...( ordinal === 3 ? { freshContext : true } : { } ) ,
815+ } ,
816+ } ) ;
817+ yield * store . apply ( {
818+ id : EventId . make ( `event:compacted:message-${ ordinal } ` ) ,
819+ type : "message.updated" ,
820+ threadId,
821+ runId : runId ( ordinal ) ,
822+ occurredAt : now ,
823+ payload : {
824+ id : MessageId . make ( `message:compacted:${ ordinal } ` ) ,
825+ threadId,
826+ runId : runId ( ordinal ) ,
827+ nodeId,
828+ createdBy : "user" ,
829+ creationSource : "web" ,
830+ role : "user" ,
831+ text : [ 6 , 8 , 9 ] . includes ( ordinal ) ? "/compact" : `Prompt ${ ordinal } ` ,
832+ attachments : [ ] ,
833+ streaming : false ,
834+ createdAt : now ,
835+ updatedAt : now ,
836+ } ,
837+ } ) ;
838+ }
839+ const rolledBackId = RunId . make ( "run:compacted:rolled-back" ) ;
840+ const ids = < T extends { readonly id : string } > ( rows : ReadonlyArray < T > ) =>
841+ rows . map ( ( row ) => row . id ) ;
842+ const compact = ( ordinal : number ) =>
843+ store . apply ( {
844+ id : EventId . make ( `event:compacted:compaction-${ ordinal } ` ) ,
845+ type : "turn-item.updated" ,
846+ threadId,
847+ runId : runId ( ordinal ) ,
848+ nodeId,
849+ driver,
850+ occurredAt : now ,
851+ payload : {
852+ id : TurnItemId . make ( `item:compacted:compaction-${ ordinal } ` ) ,
853+ threadId,
854+ runId : runId ( ordinal ) ,
855+ nodeId,
856+ providerThreadId : null ,
857+ providerTurnId : null ,
858+ nativeItemRef : null ,
859+ parentItemId : null ,
860+ ordinal,
861+ status : "completed" ,
862+ title : null ,
863+ startedAt : now ,
864+ completedAt : now ,
865+ updatedAt : now ,
866+ type : "compaction" ,
867+ driver,
868+ } ,
869+ } ) ;
870+ // Nothing is compacted yet, so the whole history is current.
871+ assert . lengthOf ( ( yield * read ( [ ] ) ) . runs , 9 ) ;
872+
873+ yield * compact ( 6 ) ;
874+ // Legacy rows can hold unreadable JSON; the fresh-context index must not reject them.
875+ const sql = yield * SqlClient . SqlClient ;
876+ yield * sql `
877+ INSERT INTO orchestration_v2_projection_runs
878+ (run_id, thread_id, ordinal, provider, status, requested_at, payload_json)
879+ VALUES ('run:compacted:legacy', ${ threadId } , 0, 'codex', 'completed', '', '{')
880+ ` ;
881+ const runIdsOf = ( ordinals : ReadonlyArray < number > ) => ordinals . map ( runId ) ;
882+ // The window starts at run 5, the prompt before the compaction.
883+ const recent = yield * read ( [ rolledBackId ] ) ;
884+ assert . deepEqual ( ids ( recent . runs ) , [ rolledBackId , ...runIdsOf ( [ 2 , 3 , 5 , 6 , 7 , 8 , 9 ] ) ] ) ;
885+ assert . deepEqual (
886+ ids ( recent . messages ) ,
887+ [ 2 , 3 , 5 , 6 , 7 , 8 , 9 ] . map ( ( ordinal ) => `message:compacted:${ ordinal } ` ) ,
888+ ) ;
889+ assert . deepEqual ( ids ( ( yield * read ( [ ] ) ) . runs ) , runIdsOf ( [ 2 , 3 , 5 , 6 , 7 , 8 , 9 ] ) ) ;
890+
891+ // Run 8 is a `/compact` run, so the model-switch baseline is the prompt before it, run 7.
892+ yield * compact ( 9 ) ;
893+ assert . deepEqual ( ids ( ( yield * read ( [ ] ) ) . runs ) , runIdsOf ( [ 2 , 3 , 7 , 8 , 9 ] ) ) ;
894+
895+ // The search for a prompt before the compaction may walk past an unreadable run.
896+ const legacyThreadId = yield * addRolledBackRecoveryCandidate ( "compacted-legacy" ) ;
897+ const legacyNodeId = NodeId . make ( "node:compacted-legacy" ) ;
898+ const compactionRunId = RunId . make ( "run:compacted-legacy:2" ) ;
899+ yield * sql `
900+ INSERT INTO orchestration_v2_projection_runs
901+ (run_id, thread_id, ordinal, provider, status, requested_at, payload_json)
902+ VALUES ('run:compacted-legacy:0', ${ legacyThreadId } , 0, 'codex', 'completed', '', '{')
903+ ` ;
904+ yield * store . apply ( {
905+ id : EventId . make ( "event:compacted-legacy:run" ) ,
906+ type : "run.created" ,
907+ threadId : legacyThreadId ,
908+ runId : compactionRunId ,
909+ nodeId : legacyNodeId ,
910+ driver,
911+ providerInstanceId,
912+ occurredAt : now ,
913+ payload : {
914+ id : compactionRunId ,
915+ threadId : legacyThreadId ,
916+ ordinal : 2 ,
917+ providerInstanceId,
918+ modelSelection,
919+ providerThreadId : null ,
920+ userMessageId : MessageId . make ( "message:compacted-legacy:2" ) ,
921+ rootNodeId : legacyNodeId ,
922+ activeAttemptId : null ,
923+ status : "completed" ,
924+ requestedAt : now ,
925+ startedAt : now ,
926+ completedAt : now ,
927+ checkpointId : null ,
928+ contextHandoffId : null ,
929+ } ,
930+ } ) ;
931+ yield * store . apply ( {
932+ id : EventId . make ( "event:compacted-legacy:compaction" ) ,
933+ type : "turn-item.updated" ,
934+ threadId : legacyThreadId ,
935+ runId : compactionRunId ,
936+ nodeId : legacyNodeId ,
937+ driver,
938+ occurredAt : now ,
939+ payload : {
940+ id : TurnItemId . make ( "item:compacted-legacy:compaction" ) ,
941+ threadId : legacyThreadId ,
942+ runId : compactionRunId ,
943+ nodeId : legacyNodeId ,
944+ providerThreadId : null ,
945+ providerTurnId : null ,
946+ nativeItemRef : null ,
947+ parentItemId : null ,
948+ ordinal : 2 ,
949+ status : "completed" ,
950+ title : null ,
951+ startedAt : now ,
952+ completedAt : now ,
953+ updatedAt : now ,
954+ type : "compaction" ,
955+ driver,
956+ } ,
957+ } ) ;
958+ const legacy = yield * store . getThreadRecords ( legacyThreadId , [ "runs" ] , {
959+ sinceLastCompaction : { keepRunIds : [ ] } ,
960+ } ) ;
961+ assert . deepEqual ( ids ( legacy . runs ) , [ compactionRunId ] ) ;
962+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
963+ ) ;
964+
765965it . effect ( "record reads filter notification sources without dropping matching message bodies" , ( ) =>
766966 Effect . gen ( function * ( ) {
767967 const store = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments