@@ -344,6 +344,60 @@ describe("loadRecentTurns", () => {
344344 const loaded = await loadRecentTurns ( dir , 5 ) ;
345345 expect ( loaded . map ( ( t ) => ( t . content [ 0 ] as { text : string } ) . text ) ) . toEqual ( [ "a" , "b" ] ) ;
346346 } ) ;
347+
348+ test ( "skips a non-tail malformed line in the newest segment" , async ( ) => {
349+ const dir = tempDir ( ) ;
350+ fs . writeFileSync ( path . join ( dir , TURNS_FILE ) , jsonl ( [ turn ( "a" ) ] ) ) ;
351+ fs . writeFileSync (
352+ path . join ( dir , segmentFileName ( TURNS_FILE , 1 ) ) ,
353+ jsonl ( [ turn ( "b" ) ] ) + '{"role":"user","content":[{"type":"te\n' + jsonl ( [ turn ( "c" ) ] ) ,
354+ ) ;
355+
356+ const loaded = await loadRecentTurns ( dir , 5 ) ;
357+ expect ( loaded . map ( ( t ) => ( t . content [ 0 ] as { text : string } ) . text ) ) . toEqual ( [ "a" , "b" , "c" ] ) ;
358+ } ) ;
359+
360+ test ( "skips a malformed line in an older sealed segment" , async ( ) => {
361+ const dir = tempDir ( ) ;
362+ fs . writeFileSync ( path . join ( dir , TURNS_FILE ) , jsonl ( [ turn ( "a" ) ] ) ) ;
363+ fs . writeFileSync (
364+ path . join ( dir , segmentFileName ( TURNS_FILE , 1 ) ) ,
365+ jsonl ( [ turn ( "b" ) ] ) + '{"role":"user","content":[{"type":"te\n' + jsonl ( [ turn ( "c" ) ] ) ,
366+ ) ;
367+ fs . writeFileSync ( path . join ( dir , segmentFileName ( TURNS_FILE , 2 ) ) , jsonl ( [ turn ( "d" ) ] ) ) ;
368+
369+ const loaded = await loadRecentTurns ( dir , 5 ) ;
370+ expect ( loaded . map ( ( t ) => ( t . content [ 0 ] as { text : string } ) . text ) ) . toEqual ( [
371+ "a" ,
372+ "b" ,
373+ "c" ,
374+ "d" ,
375+ ] ) ;
376+ } ) ;
377+
378+ test ( "skips a line that parses as JSON but fails the turn schema" , async ( ) => {
379+ const dir = tempDir ( ) ;
380+ fs . writeFileSync ( path . join ( dir , TURNS_FILE ) , jsonl ( [ turn ( "a" ) ] ) ) ;
381+ const badTurn = JSON . stringify ( { role : "user" , content : "not-an-array" , timestamp : 1 } ) ;
382+ fs . writeFileSync (
383+ path . join ( dir , segmentFileName ( TURNS_FILE , 1 ) ) ,
384+ jsonl ( [ turn ( "b" ) ] ) + badTurn + "\n" + jsonl ( [ turn ( "c" ) ] ) ,
385+ ) ;
386+
387+ const loaded = await loadRecentTurns ( dir , 5 ) ;
388+ expect ( loaded . map ( ( t ) => ( t . content [ 0 ] as { text : string } ) . text ) ) . toEqual ( [ "a" , "b" , "c" ] ) ;
389+ } ) ;
390+
391+ test ( "the reactor's load() stays strict on the same corrupt fixture and names the segment" , async ( ) => {
392+ const dir = tempDir ( ) ;
393+ const store = await createOptimizedContextStore ( dir ) ;
394+ fs . writeFileSync (
395+ path . join ( dir , TURNS_FILE ) ,
396+ jsonl ( [ turn ( "a" ) ] ) + '{"role":"user","content":[{"type":"te\n' + jsonl ( [ turn ( "b" ) ] ) ,
397+ ) ;
398+
399+ await expect ( store . load ( ) ) . rejects . toThrow ( TURNS_FILE ) ;
400+ } ) ;
347401} ) ;
348402
349403describe ( "createOptimizedContextStore checkpoint" , ( ) => {
0 commit comments