@@ -458,6 +458,76 @@ describe('KnowledgeGraphManager', () => {
458458 expect ( JSON . parse ( lines [ 1 ] ) ) . toHaveProperty ( 'type' , 'relation' ) ;
459459 } ) ;
460460
461+ it ( 'should write a trailing newline to produce valid JSONL' , async ( ) => {
462+ await manager . createEntities ( [
463+ { name : 'Alice' , entityType : 'person' , observations : [ 'test' ] } ,
464+ ] ) ;
465+
466+ const fileContent = await fs . readFile ( testFilePath , 'utf-8' ) ;
467+ expect ( fileContent . endsWith ( '\n' ) ) . toBe ( true ) ;
468+ } ) ;
469+
470+ it ( 'should produce a file where every line is individually valid JSON' , async ( ) => {
471+ // This test catches the bug where saveGraph wrote lines.join("\n")
472+ // without a trailing newline. When the file was later appended to
473+ // (e.g. by a concurrent process or external tool), the last JSON
474+ // object and the new first JSON object ended up on the same line,
475+ // producing invalid JSONL like:
476+ // {"type":"entity","name":"Alice"}{"type":"relation","from":"Alice",...}
477+ // which fails with: "Unexpected non-whitespace character after JSON
478+ // at position N"
479+ await manager . createEntities ( [
480+ { name : 'Alice' , entityType : 'person' , observations : [ 'test' ] } ,
481+ { name : 'Bob' , entityType : 'person' , observations : [ ] } ,
482+ ] ) ;
483+ await manager . createRelations ( [
484+ { from : 'Alice' , to : 'Bob' , relationType : 'knows' } ,
485+ ] ) ;
486+
487+ const fileContent = await fs . readFile ( testFilePath , 'utf-8' ) ;
488+ const allLines = fileContent . split ( '\n' ) ;
489+
490+ // Every non-empty line must be valid JSON on its own
491+ for ( const line of allLines ) {
492+ if ( line . trim ( ) === '' ) continue ;
493+ expect ( ( ) => JSON . parse ( line ) ) . not . toThrow ( ) ;
494+ }
495+ } ) ;
496+
497+ it ( 'should not corrupt JSONL when content is appended to the file externally' , async ( ) => {
498+ // Simulate the real-world corruption scenario:
499+ // 1. saveGraph writes entities to the file
500+ // 2. An external process appends a new JSON line to the file
501+ // 3. loadGraph must still parse the file without errors
502+ //
503+ // Without a trailing newline on step 1, the appended content in
504+ // step 2 lands on the same line as the last entity, producing
505+ // invalid JSONL that breaks loadGraph.
506+ await manager . createEntities ( [
507+ { name : 'Alice' , entityType : 'person' , observations : [ 'original' ] } ,
508+ ] ) ;
509+
510+ // Simulate an external append (e.g. another process, a script, or
511+ // a crash-recovery replay). This is what triggers the bug: without
512+ // a trailing newline, this JSON object concatenates onto line 1.
513+ const externalLine = JSON . stringify ( {
514+ type : 'entity' ,
515+ name : 'External' ,
516+ entityType : 'person' ,
517+ observations : [ 'appended externally' ] ,
518+ } ) ;
519+ await fs . appendFile ( testFilePath , externalLine + '\n' ) ;
520+
521+ // A new manager instance forces a fresh loadGraph from disk
522+ const manager2 = new KnowledgeGraphManager ( testFilePath ) ;
523+ const graph = await manager2 . readGraph ( ) ;
524+
525+ // Both entities must load without a JSON parse error
526+ expect ( graph . entities ) . toHaveLength ( 2 ) ;
527+ expect ( graph . entities . map ( e => e . name ) ) . toContain ( 'Alice' ) ;
528+ expect ( graph . entities . map ( e => e . name ) ) . toContain ( 'External' ) ;
529+ } ) ;
530+
461531 it ( 'should strip type field from entities when loading from file' , async ( ) => {
462532 // Create entities and relations (these get saved with type field)
463533 await manager . createEntities ( [
0 commit comments