fix(memory): prevent unique index violation in insert_or_supersede#3639
Merged
fix(memory): prevent unique index violation in insert_or_supersede#3639
Conversation
SQLite enforces unique indexes at statement level, not at transaction commit. When insert_or_supersede_with_metrics had a prior active head, insert_new_edge ran before invalidate_prior_head, momentarily leaving two active rows for the same (source_entity_id, target_entity_id, canonical_relation, edge_type) tuple and triggering SQLITE_CONSTRAINT_UNIQUE on uq_graph_edges_active_head. Split invalidate_prior_head into two helpers: - expire_prior_head: sets valid_to and expired_at before the INSERT - set_superseded_by: back-fills superseded_by after the INSERT returns new_id Both run inside the same transaction, preserving atomicity. Closes #3635
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.
Summary
insert_or_supersede_with_metricsviolateduq_graph_edges_active_head(partial unique index on(source_entity_id, target_entity_id, canonical_relation, edge_type) WHERE valid_to IS NULL AND expired_at IS NULL) becauseinsert_new_edgeran before the prior head was deactivatedinvalidate_prior_headintoexpire_prior_head(setsvalid_to/expired_atbefore INSERT) andset_superseded_by(back-fillssuperseded_byafter INSERT), both within the same transactionRoot cause
Test plan
cargo nextest run -p zeph-memory --lib— 1272 passed, regression testinsert_or_supersede_same_tuple_no_unique_index_violationcovers the exact bug scenariocargo +nightly fmt --check— CLEANcargo clippy -p zeph-memory --tests -- -D warnings— CLEANCloses #3635