Skip to content

refactor(core): document upsert/update as embed-then-commit - #1350

Merged
ajianaz merged 1 commit into
developfrom
refactor/document-upsert-embed-then-commit
Oct 7, 2026
Merged

ajianaz merged 1 commit into
developfrom
refactor/document-upsert-embed-then-commit

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

What

Item B of the architecture review (#1341, tracked in #1345; builds on #1347/#1348).

  • doc_upsert_with_parent and doc_update embed all chunks first — no index lock, no open transaction — then write the document row and every chunk in one SQLite transaction (Store::upsert_document_with_chunks, Store::update_document_with_chunks; upsert_document is split into a begin/commit wrapper and a transaction body), then mirror the result into the vector index through index_sync (remove_unsaved old chunk: keys, insert_unsaved new ones, one persist).
  • doc_delete removes chunk vectors with the shared remove_ids policy and no longer swallows the save error (let _ = index.save()).
  • New embed_document_chunks / sync_document_chunks_to_index helpers replace three copies of the chunk loop.

Why

Previously the document row was committed first, then each chunk was embedded while holding the embedder mutex and the index write lock, and chunks were inserted one by one outside a transaction. An embed failure at chunk 50 of 200 left the new document row with 49 chunks (old chunks already deleted) and blocked recall for the whole embedding time; chunk index inserts were warn-and-continue.

Behaviour changes (intended)

  • A failed embed (or no embedder) now fails the call before anything is written: previous document, chunks and index are intact. Before, doc_upsert without an embedder committed the row and then errored.
  • Recall/search are blocked only during the in-memory index mirror, not during embedding.

Testing

  • 4 new tests with an injected embedder that can run out of budget mid-document: upsert failing mid-document keeps content/version/chunks/index; update failing mid-document keeps content/chunks; no embedder writes no row; success replaces chunks in SQLite and index (index len == new chunk count).
  • Validated against the old implementation: 3 of the 4 fail there (mid-upsert, mid-update, no-embedder) and all pass now; the success test passes on both, as it should.
  • cargo fmt --check, cargo clippy --workspace --all-targets clean; cargo test --workspace green (core 602); vecq-only core build green (600).

Found on the way (not fixed here)

Changing a document's title fails with "database corruption" (FTS5 external-content update trigger) — filed as #1349. The new tests keep the title constant so they isolate embed failure.

Refs #1341

🤖 Generated with Claude Code

Embed all chunks first (no index lock, no transaction), write the document and
its chunks in one SQLite transaction (upsert_document_with_chunks /
update_document_with_chunks), then mirror into the vector index through
index_sync. A failure while embedding now leaves the previous document, chunks
and index untouched; recall is no longer blocked for the whole embedding time.
doc_delete uses the shared removal policy.

Refs #1341

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@ajianaz ajianaz added enhancement New feature or request scope:core Core engine work labels Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 6d0b778 into develop Oct 7, 2026
15 checks passed
ajianaz added a commit that referenced this pull request Oct 7, 2026
…#1361)

#1350 made document upsert/update embed-then-commit, but
embed_document_chunks still held the embedder mutex across the whole chunk
loop. recall/remember embed their query through the same mutex, so a long
document made every other embed wait for all of its chunks. Take the lock per
chunk instead.

Co-authored-by: Aji Anaz <[email protected]>
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
@ajianaz
ajianaz deleted the refactor/document-upsert-embed-then-commit branch October 13, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request scope:core Core engine work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant