fix(memory): deduplicate active graph edges on re-extraction#1495
Merged
fix(memory): deduplicate active graph edges on re-extraction#1495
Conversation
graph_edges had no unique constraint on (source_entity_id, target_entity_id, relation), causing repeated extractions from prior context messages to create duplicate rows (e.g. 3× "Alice works_at Google" after 3 turns). Migration 029 removes existing duplicates (keeping the lowest id) and adds a partial unique index on the active-edge triple. insert_edge now performs an upsert: if an active edge with the same (source, target, relation) already exists, its confidence is updated to max(stored, incoming) and the existing id is returned, preventing DB growth and inflated /graph facts output. Closes #1471
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.
Closes #1471
Summary
graph_edgeshad no unique constraint on(source_entity_id, target_entity_id, relation), causing repeated LLM extraction from prior context messages to create duplicate rowsWHERE valid_to IS NULL)insert_edgenow performs an upsert: if an active edge with the same triple exists,confidenceis updated toMAX(stored, incoming)and the existing id is returnedTest plan
insert_edge_deduplicates_active_edge— verifies same(source, target, relation)returns same id, count stays at 1, confidence updated to maxinsert_edge_different_relations_are_distinct— verifies different relations on same entity pair produce two separate edgesinsert_edge_*tests continue to pass