Skip to content

A bulk_set batch writing a key twice owes a search document only for the row it embedded, and identity dedup finds a keeper ClickHouse is still owed - #446

Merged
zlareb1 merged 7 commits into
mainfrom
fix/bulk-key-twice-search-doc
Nov 9, 2026
Merged

zlareb1 merged 7 commits into
mainfrom
fix/bulk-key-twice-search-doc

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Fixes the Postgres memory-authority CI jobs on main, red since #433.

Changelog category (leave one):

  • Bug Fix

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):

Under LOOM_STORAGE_AUTHORITY=postgres, a memory.bulk_set that writes one key twice no longer holds the namespace's searches at their barrier (503 PROJECTION_BEHIND) until the batch's lease runs out, and a bulk write or import of a value the same identity wrote a moment ago is held by it as an exact duplicate.

Why

test_sensitivity_high_canary.py::test_one_bulk_batch_with_a_key_twice_keeps_a_vector_only_if_its_last_row_is_embedded (and, by the same cause, other writes into that shared namespace) failed in both Postgres memory-authority jobs from #433 on: the search after the batch answered 503 PROJECTION_BEHIND.

A batch that writes a key twice commits two revisions of the key; when only the last is embedded (the earlier row is 'high' or of an unembedded type), only the last has a search document. bulk_set._commit_batch gave the search-document target (SEARCH_DOC) by key, so both revisions owed a document. Before #433 the writer cleared that target by key too, so the earlier revision's target was cleared with the last's. The publication batch (pg_batch._landed) clears it only for the version a document was built from, which is right, so the earlier revision kept owing a document nobody sends, and every search of the namespace waited at its barrier for it until the batch's lease ran out and the delivery worker settled it.

Reproduced locally: right after such a batch, loom.memory_deliveries holds the first revision owing only SEARCH_DOC (targets = 2) under the writer's lease.

A second regression of the same kind showed on this PR's own Postgres 18 job: test_memory_import.py::test_a_pinned_memorys_own_text_is_held_by_it_and_competes_with_nothing held the imported copy as a merge instead of an identity_duplicate. memory.bulk_set's identity dedup (_bulk_identity_dupes) found its keepers in ClickHouse's loom.memory and then dropped the ones the authority no longer holds; since #433 a first write answers at its commit, before ClickHouse holds it, so a keeper written a moment ago was not found.

Design

  • Identity dedup keeps ClickHouse's read, which a bloom-filter index on content_sha256 narrows, and under Postgres first reads the keepers ClickHouse is still owed: the identity's revisions of the batch's hashes whose delivery still owes the memory row (pg_memory.unpublished_keepers, through the namespace's deliveries still owing the row, parked ones left out, in the caller's read scope; the connection's statement timeout bounds it). Read first, so a delivery that clears between the two reads was in ClickHouse before its read. Every keeper either read found is checked against its head on the authority (_keepers_on_the_authority), in ClickHouse's order, so one rewritten or deleted since is not one and a key rewritten a moment ago keeps its new bytes. Reading every head of the identity on the authority instead would scan them all (no index on memory_current.content_sha256, and a migration builds none on a large table).
  • _commit_batch gives SEARCH_DOC only to a row whose (identity key, version) has a search document in the batch, matching what the publication clears. An earlier row of a key written twice owes none unless it was embedded too (then both own a document and both owe one).

Scope and risk

  • Postgres authority only: one predicate in bulk_set._commit_batch, and one Postgres read in identity dedup over the namespace's deliveries still owing the memory row (parked ones left out; the statement timeout bounds it). The delivery worker builds a document only for a revision that owes one, so an earlier, unembedded revision of a key written twice now gets none built, as its head never serves it.

Compatibility, rollout and rollback

  • Wire: none. Operator steps: none. Rollback: revert.

Tests

  • tests/unit/test_pg_authority.py::test_a_bulk_batch_writing_a_key_twice_owes_a_document_only_for_its_embedded_rows (its first case fails on main): a batch of [k, k] with only the last row embedded commits MEMORY_ROW | AUDIT_ROW for the first revision and adds SEARCH_DOC only for the last; with both embedded, both owe one.
  • tests/integration/test_sensitivity_high_canary.py against a Postgres-authority server: fails on main (test_one_bulk_batch_with_a_key_twice_keeps_a_vector_only_if_its_last_row_is_embedded[False], 503 PROJECTION_BEHIND); 14 passed, three runs in a row, with this change.
  • tests/unit/test_bulk_identity_dedup.py::test_under_postgres_a_keeper_clickhouse_does_not_hold_yet_is_found: a keeper only the authority's undelivered revisions hold is found, read in the caller's scope, and a rewritten one is dropped by the head check.
  • test_pg_authority.py::test_a_bulk_write_of_bytes_another_key_holds_is_judged_on_the_authority (integration, 3 passed): an entry whose bytes a key holds whose delivery is staged undelivered for an hour (so ClickHouse shows its old value) is held; one whose keeper was deleted or rewritten since is written. The undelivered case fails on main.
  • test_bulk_identity_dedup.py::test_under_postgres_a_keeper_delivered_between_the_two_reads_is_found: the undelivered keepers are read before ClickHouse, so a delivery landing in between is found by one of the two reads.
  • test_bulk_identity_dedup.py::test_under_postgres_a_key_rewritten_a_moment_ago_keeps_its_new_bytes: ClickHouse shows a key's old bytes, its undelivered rewrite holds the new; the batch carrying both finds the key for the new bytes.
  • test_memory_import.py, test_sensitivity_high_canary.py, test_memory_bulk.py and test_bulk_conflicts.py against a Postgres-authority server: 43 passed, twice in a row.
  • make format-check lint, mypy on the changed files, and the whole unit suite (32,182 passed). The full Postgres-authority integration list is this PR's CI jobs: a local run was stopped by the host's memory pressure.

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. Checked: the failing CI job's log, a local reproduction against main, and the suites below.

  • 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, and this description names any operator steps and wire changes.
  • 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 4 commits November 9, 2026 13:13
…the row it embedded

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…wice owes one, and a test pins both cases

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… on their own lines

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ads, so a keeper written a moment ago is found

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1 zlareb1 changed the title A bulk_set batch writing a key twice owes a search document only for the row it embedded A bulk_set batch writing a key twice owes a search document only for the row it embedded, and its identity dedup reads the Postgres heads Nov 9, 2026
zlareb1 and others added 2 commits November 9, 2026 14:21
…till owed to it from the authority's undelivered revisions

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ecks every keeper either read found against its head

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1 zlareb1 changed the title A bulk_set batch writing a key twice owes a search document only for the row it embedded, and its identity dedup reads the Postgres heads A bulk_set batch writing a key twice owes a search document only for the row it embedded, and identity dedup finds a keeper ClickHouse is still owed Nov 9, 2026
…t bounds it; a test pins the read order

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit e725ec4 into main Nov 9, 2026
38 checks passed
@zlareb1
zlareb1 deleted the fix/bulk-key-twice-search-doc branch November 9, 2026 17:01
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 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