Skip to content

memory.bulk_set identity dedup compares each key's current value, not a superseded version - #244

Merged
zlareb1 merged 6 commits into
mainfrom
fix/bulk-dedup-head-version
Nov 4, 2026
Merged

zlareb1 merged 6 commits into
mainfrom
fix/bulk-dedup-head-version

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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_set no 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. A bulk_set entry with value V matched that old row: the dedup query filtered content_sha256 IN {shas} in WHERE and then took argMax(..., 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 ReplacingMergeTree key 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 on content_sha256 still limits the scan.

Scope and risk

Only _bulk_identity_dupes in server/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

  • Wire: none.
  • Operator steps: none.
  • Rollback: reverting restores the old query; it leaves no rows or files behind.

Tests

  • tests/unit/test_bulk_identity_dedup.py runs the query on clickhouse local against the production loom.memory definition with two unmerged versions of a key. test_a_superseded_version_with_the_same_bytes_is_not_a_keeper failed 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_keeper covers the reverse revision.
  • The fakes in test_memory_import.py and test_fixedstring_hash_reads.py now model the fixed query (newest version per key, then the hash).
  • test_a_key_deleted_or_retired_since_is_not_a_keeper pins a key whose newest version is tombstoned or superseded; test_the_same_key_in_two_sessions_picks_one_keeper_every_time pins the keeper order (ORDER BY head_sha, memory_key, session_id) so a retry reinforces the same memory_id.
  • Read cost, on clickhouse local with the production loom.memory definition, 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 through idx_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 = 1 shows 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 typecheck pass.

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 local before 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 typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version.
  • The frontend builds and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 5 commits November 4, 2026 03:20
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]>
…s in its part

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the fix/bulk-dedup-head-version branch from 1dda23e to 0fbf075 Compare November 4, 2026 03:21
@zlareb1
zlareb1 merged commit a2cbfbe into main Nov 4, 2026
22 checks passed
@zlareb1
zlareb1 deleted the fix/bulk-dedup-head-version branch November 4, 2026 08:05
@pufit pufit mentioned this pull request Nov 4, 2026
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant