@@ -1036,6 +1036,92 @@ describe("session accessor file-backed seam", () => {
10361036 } ) ;
10371037 } ) ;
10381038
1039+ it ( "branches from the newest matching compaction checkpoint without sorting all checkpoints" , async ( ) => {
1040+ const sourceSessionId = "11111111-1111-4111-8111-111111111111" ;
1041+ const branchSessionId = "22222222-2222-4222-8222-222222222222" ;
1042+ const branchPath = path . join ( tempDir , "branch-newest.jsonl" ) ;
1043+ fs . writeFileSync ( branchPath , `{"type":"session","id":"${ branchSessionId } "}\n` , "utf8" ) ;
1044+ const oldMatchingCheckpoint = {
1045+ checkpointId : "checkpoint-1" ,
1046+ sessionKey : "agent:main:main" ,
1047+ sessionId : sourceSessionId ,
1048+ createdAt : 10 ,
1049+ reason : "manual" ,
1050+ preCompaction : {
1051+ sessionId : sourceSessionId ,
1052+ leafId : "old-leaf" ,
1053+ } ,
1054+ postCompaction : { sessionId : "33333333-3333-4333-8333-333333333333" } ,
1055+ } satisfies NonNullable < SessionEntry [ "compactionCheckpoints" ] > [ number ] ;
1056+ const newestMatchingCheckpoint = {
1057+ ...oldMatchingCheckpoint ,
1058+ createdAt : 20 ,
1059+ preCompaction : {
1060+ sessionId : sourceSessionId ,
1061+ leafId : "new-leaf" ,
1062+ } ,
1063+ postCompaction : { sessionId : "44444444-4444-4444-8444-444444444444" } ,
1064+ } satisfies NonNullable < SessionEntry [ "compactionCheckpoints" ] > [ number ] ;
1065+ const newestDifferentCheckpoint = {
1066+ ...oldMatchingCheckpoint ,
1067+ checkpointId : "checkpoint-2" ,
1068+ createdAt : 30 ,
1069+ preCompaction : {
1070+ sessionId : sourceSessionId ,
1071+ leafId : "different-leaf" ,
1072+ } ,
1073+ postCompaction : { sessionId : "55555555-5555-4555-8555-555555555555" } ,
1074+ } satisfies NonNullable < SessionEntry [ "compactionCheckpoints" ] > [ number ] ;
1075+ fs . writeFileSync (
1076+ storePath ,
1077+ JSON . stringify (
1078+ {
1079+ main : {
1080+ sessionId : sourceSessionId ,
1081+ updatedAt : 30 ,
1082+ compactionCheckpoints : [
1083+ oldMatchingCheckpoint ,
1084+ newestDifferentCheckpoint ,
1085+ newestMatchingCheckpoint ,
1086+ ] ,
1087+ } ,
1088+ } satisfies Record < string , SessionEntry > ,
1089+ null ,
1090+ 2 ,
1091+ ) ,
1092+ "utf8" ,
1093+ ) ;
1094+
1095+ const result = await branchSessionFromCompactionCheckpoint ( {
1096+ storePath,
1097+ sourceKey : "agent:main:main" ,
1098+ sourceStoreKey : "main" ,
1099+ nextKey : "agent:main:branch-newest" ,
1100+ checkpointId : "checkpoint-1" ,
1101+ forkTranscriptFromCheckpoint : async ( selectedCheckpoint ) => {
1102+ expect ( selectedCheckpoint ) . toEqual ( newestMatchingCheckpoint ) ;
1103+ return {
1104+ status : "created" ,
1105+ transcript : {
1106+ sessionFile : branchPath ,
1107+ sessionId : branchSessionId ,
1108+ } ,
1109+ } ;
1110+ } ,
1111+ buildEntry : ( { currentEntry, forkedTranscript } ) => ( {
1112+ ...currentEntry ,
1113+ sessionFile : forkedTranscript . sessionFile ,
1114+ sessionId : forkedTranscript . sessionId ,
1115+ } ) ,
1116+ } ) ;
1117+
1118+ expect ( result ) . toMatchObject ( {
1119+ status : "created" ,
1120+ key : "agent:main:branch-newest" ,
1121+ checkpoint : newestMatchingCheckpoint ,
1122+ } ) ;
1123+ } ) ;
1124+
10391125 it ( "does not persist checkpoint restores when the transcript boundary is missing" , async ( ) => {
10401126 fs . writeFileSync (
10411127 storePath ,
0 commit comments