Skip to content

A source sync reads an address's owner again just before it writes, so a document a person wrote during the fetch is left alone - #353

Merged
zlareb1 merged 5 commits into
mainfrom
fix/source-sync-write-time-owner
Nov 7, 2026
Merged

zlareb1 merged 5 commits into
mainfrom
fix/source-sync-write-time-owner

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 7, 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):

A knowledge source's sync no longer overwrites a document a person writes at the same address while the sync is fetching and converting that file.

Why

source_sync.py promises that "a document another writer or another source holds is never written or retired". sync_item kept that promise only at the start: it called _may_write(src, uri) before the fetch, then _fetch_and_write downloaded the file and sent it to the extractor (tens of seconds for a large PDF) and wrote with writes.handle_knowledge_upsert, which has no ownership guard (_require_source_tags_kept returns early when the prior row's actor is not the sync worker). A knowledge.add or knowledge.upsert at that address in between was superseded by the worker's copy, and the document's actor became worker:knowledge-source-sync. The same write step is shared by the Linear, Notion and recording kinds. retire already re-checks right before its delete; the write did not.

Just before each write the turn reads the source again (as before), then write_document reads the address's owner again with _may_write under sequential_reads() (so a replica that has not seen the person's write cannot answer that the address is free), then runs the pass's stop check, then upserts; it returns False, writing nothing, when another writer holds the address. A retirement reads the address's owner with sequential consistency too, so a person's document that replaced the worker's copy is not deleted when its item drops out of a full listing or moves. Each kind's write step passes that to counted, which counts the file skipped (as the check before the fetch does) or changed.

Scope and risk

  • The four sync kinds' write step. The window between the new read and the upsert is a single round trip instead of a whole fetch and conversion (the read is sequentially consistent, so replication lag does not widen it); a write that lands inside that round trip can still be overwritten, as with any read-then-write over ClickHouse.
  • One extra point read of loom.documents per written file.

Compatibility, rollout and rollback

  • Wire: none. A sync's detail counts such a file under skipped.
  • Operator steps: none.
  • Rollback: revert.

Tests

  • test_an_address_a_writer_takes_during_the_fetch_is_not_overwritten (folder): a person writes at the address while the file is downloaded; on main the worker's upsert goes through, here nothing is upserted, the person's document keeps its actor, and skipped == 1.
  • test_an_address_taken_during_the_fetch_is_counted_skipped (Linear): the same race during the document fetch.
  • test_a_page_whose_address_is_taken_during_the_fetch_is_counted_skipped (Notion): the same race while a page's blocks are read; the two database pages are still written.
  • test_a_transcript_whose_address_is_taken_during_the_fetch_is_counted_skipped (recordings): the same race while a transcript's entries are read.
  • test_the_write_time_owner_read_reads_with_sequential_consistency: the check before the fetch is a plain read, the one just before the write runs under sequential_reads().
  • test_the_retire_owner_read_reads_with_sequential_consistency: a retirement's read of the address runs under sequential_reads().
  • Each race test fails with the write-time re-read removed; each sequential test fails with only its sequential_reads() scope removed. test_knowledge_source_sync.py, test_knowledge_source_sync_linear.py, test_notion_source_sync.py, test_knowledge_source_recordings.py: 298 passed. ruff: 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 case and the four write paths were checked against the code.

  • 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 2 commits November 7, 2026 12:35
…o a document a person wrote during the fetch is left alone

sync_item checked that no other writer held the address before fetching and converting the file, seconds earlier; a knowledge.add or knowledge.upsert at that address in between was then superseded by the worker's copy, its author replaced by worker:knowledge-source-sync. write_document now reads the live document again after the pass's stop check and writes nothing when another writer holds it; each kind counts that file skipped, as the check before the fetch does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…eck runs right before the write; each kind tests the skip

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the fix/source-sync-write-time-owner branch from b2c67d1 to cf8ccb3 Compare November 7, 2026 12:40
zlareb1 and others added 3 commits November 7, 2026 12:58
… checks the write step leaves it

The Linear, Notion and recording tests no longer stub write_document: the
fetch answer saves a person's document at the address, and each test checks
that nothing is upserted, the person's document stays and the file is
counted skipped. The recording harness answers a transcript's entries from a
callable too. The recording _fetch_and_write docstring is refilled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…istency

On replicated ClickHouse the read just before a write could land on a replica
that has not seen a person's write during the fetch and answer that the
address is free. write_document now makes that read inside
sequential_reads(), as require_current does; the check before the fetch stays
a plain read. The module docstring says the address is read again before each
write and names counted among the helpers kind modules use.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…fore it retires the document there

retire read the owner with a plain read, so a replica that has not seen a
person's write at the address could answer that the document is still the
source's, and a retirement of an absent or moved item would delete the
person's document. The read now runs inside sequential_reads(), as the
write-time read does. The knowledge feature page and the module docstring
split the re-read sentence and state the skip rule once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 33d4b9f into main Nov 7, 2026
38 checks passed
@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