Skip to content

fix(memory): deduplicate active graph edges on re-extraction#1495

Merged
bug-ops merged 4 commits intomainfrom
graph-edge-dedup
Mar 9, 2026
Merged

fix(memory): deduplicate active graph edges on re-extraction#1495
bug-ops merged 4 commits intomainfrom
graph-edge-dedup

Conversation

@bug-ops
Copy link
Owner

@bug-ops bug-ops commented Mar 9, 2026

Closes #1471

Summary

  • graph_edges had no unique constraint on (source_entity_id, target_entity_id, relation), causing repeated LLM extraction from prior context messages to create duplicate rows
  • Added migration 029 to remove existing duplicates and enforce a partial unique index on active edges (WHERE valid_to IS NULL)
  • insert_edge now performs an upsert: if an active edge with the same triple exists, confidence is updated to MAX(stored, incoming) and the existing id is returned

Test plan

  • insert_edge_deduplicates_active_edge — verifies same (source, target, relation) returns same id, count stays at 1, confidence updated to max
  • insert_edge_different_relations_are_distinct — verifies different relations on same entity pair produce two separate edges
  • All existing insert_edge_* tests continue to pass
  • Full suite: 4934 tests passed

bug-ops added 3 commits March 9, 2026 21:56
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
@github-actions github-actions bot added documentation Improvements or additions to documentation memory zeph-memory crate (SQLite) rust Rust code changes bug Something isn't working size/M Medium PR (51-200 lines) labels Mar 9, 2026
@bug-ops bug-ops enabled auto-merge (squash) March 9, 2026 21:23
@bug-ops bug-ops merged commit f2df17d into main Mar 9, 2026
18 checks passed
@bug-ops bug-ops deleted the graph-edge-dedup branch March 9, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation memory zeph-memory crate (SQLite) rust Rust code changes size/M Medium PR (51-200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Graph memory: duplicate edges on repeated extraction

1 participant