fix(kg): symmetric inversion guard on invalidate (#1371) - #1409
Closed
arnoldwender wants to merge 1 commit into
Closed
fix(kg): symmetric inversion guard on invalidate (#1371)#1409arnoldwender wants to merge 1 commit into
arnoldwender wants to merge 1 commit into
Conversation
MemPalace#1214 added an inverted-interval guard to KnowledgeGraph.add_triple() so a row with valid_to < valid_from cannot be persisted on the create path. The same foot-gun was reachable through invalidate(): a caller passing ended="2020-01-01" against an open row with valid_from="2026-01-01" silently produced the same inverted state — invisible to every query_entity / query_relationship / timeline call, durable in SQLite forever. Mirror the add_triple() guard inside invalidate(). Before the UPDATE, SELECT the matched rows' valid_from values and reject with ValueError if ended is strictly before any of them. Equality is allowed (point-in-time fact). Open intervals (valid_from IS NULL) pass through unchanged. The SELECT and UPDATE share one transaction (`with conn:`) so a concurrent insert cannot race a row past the guard. Tests: - test_invalidate_rejects_ended_before_valid_from: rejection + asserts the row stays open after the failed call. - test_invalidate_accepts_ended_equal_to_valid_from: same-day close. - test_invalidate_passes_through_open_intervals: non-regression for the original invalidate() contract on rows without valid_from. Closes MemPalace#1371. Surfaced by @igorls during review of MemPalace#1214.
arnoldwender
requested review from
bensig,
igorls and
milla-jovovich
as code owners
May 7, 2026 19:42
This was referenced May 7, 2026
Contributor
Author
|
Closing — superseded by @fatkobra's #1432 (commit Thanks to @igorls for surfacing the symmetry concern in #1371; happy this got addressed in a more general form. Closes #1371 from this PR's side. |
Contributor
Author
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.
What and Why
#1214 added an inverted-interval guard to
KnowledgeGraph.add_triple()so a row withvalid_to < valid_fromcannot be persisted on the create path. The same foot-gun was reachable throughinvalidate(): a caller passingended=\"2020-01-01\"against an open row withvalid_from=\"2026-01-01\"silently produced the same inverted state — invisible to everyquery_entity/query_relationship/timelinecall (because theas_offiltervalid_from <= ? AND valid_to >= ?can never be satisfied), durable in SQLite forever.@igorls flagged this in #1371 during review of #1214.
Root Cause
mempalace/knowledge_graph.py:232-245(pre-fix) —invalidate()issued anUPDATE triples SET valid_to=?with no comparison against the existingvalid_from. The asymmetry withadd_triple()'s line-177 guard meant the same invariant was enforced on creation but not on closure.Reproduction
Change Summary
mempalace/knowledge_graph.py:232-264—invalidate()nowSELECTsvalid_fromfor all matched open rows before theUPDATE. Ifended < valid_fromfor any matched row, raiseValueErrorwith the same message format asadd_triple(). TheSELECTandUPDATEshare onewith conn:transaction so a concurrent insert cannot race a row past the guard.ended == valid_fromis allowed (point-in-time fact, mirrorstest_add_triple_accepts_equal_dates).valid_from IS NULL(open intervals) pass through — the originalinvalidate()contract for date-less triples is preserved.Test Plan
test_invalidate_rejects_ended_before_valid_from— exercises the rejection path and asserts the row stays open after the failed call.test_invalidate_accepts_ended_equal_to_valid_from— same-day point-in-time close still works.test_invalidate_passes_through_open_intervals— non-regression: rows withoutvalid_frominvalidate normally.test_invalidate_sets_valid_to(seeded_kg hasvalid_from=\"2024-06-01\", ended\"2026-01-01\"— guard does not fire),test_invalidated_triple_allows_re_add,test_add_triple_allows_only_one_bound.tests/test_knowledge_graph.py: 27 passed (24 existing + 3 new).ruff check .andruff format --check .clean on both files.Closes #1371. Surfaced by @igorls during review of #1214.