diff --git a/src/memory/__tests__/knowledge-graph.test.ts b/src/memory/__tests__/knowledge-graph.test.ts index a65d527b64..82607f3597 100644 --- a/src/memory/__tests__/knowledge-graph.test.ts +++ b/src/memory/__tests__/knowledge-graph.test.ts @@ -360,6 +360,40 @@ describe('KnowledgeGraphManager', () => { }); }); + describe('loading entities with extra properties', () => { + it('should strip extra properties from entities when loading (issue #3144)', async () => { + // Write JSONL file directly with extra properties (simulating manual edits or legacy data) + const jsonlContent = [ + JSON.stringify({ type: "entity", name: "Test", entityType: "test", observations: ["obs1"], custom_id: "xyz-123", extra_field: true }), + JSON.stringify({ type: "relation", from: "Test", to: "Test", relationType: "self", extra_prop: "should be stripped" }) + ].join('\n'); + await fs.writeFile(testFilePath, jsonlContent); + + const graph = await manager.readGraph(); + + // Entities should only have the schema-defined properties + expect(graph.entities).toHaveLength(1); + expect(graph.entities[0]).toEqual({ + name: "Test", + entityType: "test", + observations: ["obs1"] + }); + expect(graph.entities[0]).not.toHaveProperty('type'); + expect(graph.entities[0]).not.toHaveProperty('custom_id'); + expect(graph.entities[0]).not.toHaveProperty('extra_field'); + + // Relations should only have the schema-defined properties + expect(graph.relations).toHaveLength(1); + expect(graph.relations[0]).toEqual({ + from: "Test", + to: "Test", + relationType: "self" + }); + expect(graph.relations[0]).not.toHaveProperty('type'); + expect(graph.relations[0]).not.toHaveProperty('extra_prop'); + }); + }); + describe('file persistence', () => { it('should persist data across manager instances', async () => { await manager.createEntities([ diff --git a/src/memory/index.ts b/src/memory/index.ts index c7d781d2c4..600a7edcc8 100644 --- a/src/memory/index.ts +++ b/src/memory/index.ts @@ -74,8 +74,20 @@ export class KnowledgeGraphManager { const lines = data.split("\n").filter(line => line.trim() !== ""); return lines.reduce((graph: KnowledgeGraph, line) => { const item = JSON.parse(line); - if (item.type === "entity") graph.entities.push(item as Entity); - if (item.type === "relation") graph.relations.push(item as Relation); + if (item.type === "entity") { + graph.entities.push({ + name: item.name, + entityType: item.entityType, + observations: item.observations + }); + } + if (item.type === "relation") { + graph.relations.push({ + from: item.from, + to: item.to, + relationType: item.relationType + }); + } return graph; }, { entities: [], relations: [] }); } catch (error) {