Repository navigation
memory.bulk_set identity dedup compares each key's current value, not a superseded version - #244
Merged
Merged
Conversation
The dedup query filtered on content_sha256 before taking each key's newest version, so a superseded version not yet merged away could match: an entry holding a key's old value was reported as a copy of that key and not written. The query now takes the newest version per (session_id, memory_key) and compares its hash; the hash filter only narrows the keys. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…red keys Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…lter is bound Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…s in its part Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
zlareb1
force-pushed
the
fix/bulk-dedup-head-version
branch
from
November 4, 2026 03:21
1dda23e to
0fbf075
Compare
8 of 9 tasks
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.
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
memory.bulk_setno longer drops an entry as a copy of a key whose value has since changed. The identity dedup compares each entry with the current value of every key, not with any version that once held the same bytes.Why
Failing case: key K is written with value V, then revised to V2. Until ClickHouse merges the parts, the V version row is still in
loom.memory. Abulk_setentry with value V matched that old row: the dedup query filteredcontent_sha256 IN {shas}inWHEREand then tookargMax(..., version)per(content_sha256, memory_key), so the argMax only saw the versions that held the hash, never the key's newest version. The entry was reported as an identity duplicate of K and not written, while K's live value is V2, so V was lost. Whether it happened depended on merge timing.Pattern: a filter on a non-key column applied before the latest version of a
ReplacingMergeTreekey is chosen.Fix: the query groups by the table's key within the identity (
session_id, memory_key), takes each key's newest version, and compares that version's hash (HAVING argMax(content_sha256, version) IN {shas}), together with the existing tombstone and live-status checks. The hash filter stays only in a subquery that narrows which keys are read, so the bloom-filter index oncontent_sha256still limits the scan.Scope and risk
Only
_bulk_identity_dupesinserver/memory/app/bulk_set.py. The read scope predicate (own_session/own_user) applies to both the outer query and the key subquery. A lookup error still fails open and writes the entry. No wire, schema or setting change.Compatibility, rollout and rollback
Tests
tests/unit/test_bulk_identity_dedup.pyruns the query onclickhouse localagainst the productionloom.memorydefinition with two unmerged versions of a key.test_a_superseded_version_with_the_same_bytes_is_not_a_keeperfailed before the fix (the revised key was returned as the keeper) and passes now;test_a_key_whose_current_value_holds_the_bytes_is_the_keepercovers the reverse revision.test_memory_import.pyandtest_fixedstring_hash_reads.pynow model the fixed query (newest version per key, then the hash).test_a_key_deleted_or_retired_since_is_not_a_keeperpins a key whose newest version is tombstoned orsuperseded;test_the_same_key_in_two_sessions_picks_one_keeper_every_timepins the keeper order (ORDER BY head_sha, memory_key, session_id) so a retry reinforces the samememory_id.clickhouse localwith the productionloom.memorydefinition, 500,000 rows in two unmerged parts (50 users, 20 sessions, a quarter of the keys revised once), one identity and two hashes: the old query read 3 marks / 384 rows; the new one reads 5 marks / 640 rows (3 for the hash subquery throughidx_memory_content_sha, 2 for the outer read, whose(session_id, memory_key) IN (subquery)condition prunes the primary key to 2 of 3,907 granules).EXPLAIN indexes = 1shows both.pytest tests/unit/test_bulk_identity_dedup.py tests/unit/test_write_scope.py tests/unit/test_memory_import.py tests/unit/test_fixedstring_hash_reads.py tests/unit/test_memory_bulk_parity.py: all pass.make format lint typecheckpass.AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code. The failing test was run against real SQL on
clickhouse localbefore and after the fix.I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.main; no merged migration is edited.frontend/changed.docs/describe the system as it now is.git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code