Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/memory/__tests__/knowledge-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
16 changes: 14 additions & 2 deletions src/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down