fix(memory): skip duplicate entities and relations within a single batch#4383
Open
JSap0914 wants to merge 1 commit into
Open
fix(memory): skip duplicate entities and relations within a single batch#4383JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
create_entitiesandcreate_relationsonly de-duplicated against the existing graph, never against the items in the incoming request. So passing the same entity name (or an identical relation) twice in a single call persisted duplicate records tomemory.jsonl.This contradicts the documented behavior in the README — "Ignores entities with existing names" and "Skips duplicate relations" — and breaks the implicit name-uniqueness invariant the rest of the manager relies on (
addObservations/deleteEntities/deleteObservationsall key onname, andfind()only returns the first match).The fix de-duplicates within the input batch as well, keeping the first occurrence. Cross-call de-duplication is unchanged.
Server Details
KnowledgeGraphManager.createEntities,KnowledgeGraphManager.createRelations)Motivation and Context
Reproduction (before this change):
After the change each of the above creates a single record, matching the documented "ignores existing names" / "skips duplicate relations" contract.
How Has This Been Tested?
Added two regression tests to
src/memory/__tests__/knowledge-graph.test.ts(within-batch duplicate entities, within-batch duplicate relations) and ran the existing vitest suite:Both new tests fail on
main(the manager returns/persists 2 records) and pass with this change.npx tsc --noEmitis clean.Breaking Changes
None. Behavior only changes for inputs that were already malformed (duplicate items in one request); valid de-duplicating callers are unaffected.
Types of changes
Checklist