Skip to content

Commit 1ec570c

Browse files
JSap0914olaservo
andauthored
fix(memory): skip duplicate entities and relations within a single batch (#4383)
create_entities and create_relations only de-duplicated against the existing graph, so passing the same entity name (or identical relation) twice in one call persisted duplicate records. This contradicts the documented behavior ("Ignores entities with existing names" / "Skips duplicate relations") and breaks the implicit name-uniqueness invariant the rest of the manager relies on (e.g. addObservations/deleteEntities key on name). De-duplicate within the input batch as well, keeping the first occurrence. Co-authored-by: JSap0914 <JSap0914@users.noreply.github.com> Co-authored-by: olaservo <olahungerford@gmail.com>
1 parent f41b666 commit 1ec570c

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

‎src/memory/__tests__/knowledge-graph.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ describe('KnowledgeGraphManager', () => {
5959
const newEntities = await manager.createEntities([]);
6060
expect(newEntities).toHaveLength(0);
6161
});
62+
63+
it('should ignore duplicate entity names within a single batch', async () => {
64+
const entities: Entity[] = [
65+
{ name: 'Alice', entityType: 'person', observations: ['first'] },
66+
{ name: 'Alice', entityType: 'person', observations: ['second'] },
67+
];
68+
69+
const newEntities = await manager.createEntities(entities);
70+
expect(newEntities).toHaveLength(1);
71+
72+
const graph = await manager.readGraph();
73+
expect(graph.entities).toHaveLength(1);
74+
expect(graph.entities[0].name).toBe('Alice');
75+
});
6276
});
6377

6478
describe('createRelations', () => {
@@ -135,6 +149,24 @@ describe('KnowledgeGraphManager', () => {
135149
const newRelations = await manager.createRelations([]);
136150
expect(newRelations).toHaveLength(0);
137151
});
152+
153+
it('should skip duplicate relations within a single batch', async () => {
154+
await manager.createEntities([
155+
{ name: 'Alice', entityType: 'person', observations: [] },
156+
{ name: 'Bob', entityType: 'person', observations: [] },
157+
]);
158+
159+
const relations: Relation[] = [
160+
{ from: 'Alice', to: 'Bob', relationType: 'knows' },
161+
{ from: 'Alice', to: 'Bob', relationType: 'knows' },
162+
];
163+
164+
const newRelations = await manager.createRelations(relations);
165+
expect(newRelations).toHaveLength(1);
166+
167+
const graph = await manager.readGraph();
168+
expect(graph.relations).toHaveLength(1);
169+
});
138170
});
139171

140172
describe('addObservations', () => {

‎src/memory/index.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ export class KnowledgeGraphManager {
181181

182182
async createEntities(entities: Entity[]): Promise<Entity[]> {
183183
const graph = await this.loadGraph();
184-
const newEntities = entities.filter(e => !graph.entities.some(existingEntity => existingEntity.name === e.name));
184+
const newEntities = entities.filter((e, index) =>
185+
!graph.entities.some(existingEntity => existingEntity.name === e.name) &&
186+
// Also skip duplicates appearing earlier in this same batch
187+
!entities.slice(0, index).some(earlier => earlier.name === e.name)
188+
);
185189
graph.entities.push(...newEntities);
186190
await this.saveGraph(graph);
187191
return newEntities;
@@ -200,11 +204,15 @@ export class KnowledgeGraphManager {
200204
}
201205
});
202206

203-
const newRelations = relations.filter(r => !graph.relations.some(existingRelation =>
204-
existingRelation.from === r.from &&
205-
existingRelation.to === r.to &&
206-
existingRelation.relationType === r.relationType
207-
));
207+
const isSameRelation = (a: Relation, b: Relation) =>
208+
a.from === b.from &&
209+
a.to === b.to &&
210+
a.relationType === b.relationType;
211+
const newRelations = relations.filter((r, index) =>
212+
!graph.relations.some(existingRelation => isSameRelation(existingRelation, r)) &&
213+
// Also skip duplicates appearing earlier in this same batch
214+
!relations.slice(0, index).some(earlier => isSameRelation(earlier, r))
215+
);
208216
graph.relations.push(...newRelations);
209217
await this.saveGraph(graph);
210218
return newRelations;

0 commit comments

Comments
 (0)