Repository navigation
Repo: embed before writing, batch the first index, one lock across processes - #11
Merged
Merged
Conversation
…cache A push used to chunk and embed every changed file inside one transaction that held the source's row lock, and new vectors were saved to the embedding cache only after it committed. If a job was killed partway (job time limit, crash), all of that embedding work was lost, so the retry embedded everything again. - Every changed file is now chunked and embedded first, with no transaction open. The transaction then only writes the rows and moves indexed_commit, so search still switches to the new commit all at once. - Every 256 new vectors are written to embedding_cache on a second connection while the push runs. A retry finds them there and does not embed those chunks again. With no free connection (a one-connection pool), they are written after the commit, as before. - A file that looked unchanged during the embedding phase is checked again under the row lock, and prepared again if another writer changed it in between. - Deleting a source or tenant no longer waits for a running push to finish embedding. The push then finds its source gone and writes nothing. The two tests that expected the delete to wait now test this instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ery index run - A repo's first index commits every LORE_FIRST_INDEX_BATCH files (default 200) in its own transaction. The repo is searchable as it goes, and a run that dies keeps its batches: the retry finds those files stored with the same hash and skips them. indexed_commit is set only by the last transaction. Later indexes stay one transaction, so search never mixes two commits. - list_sources (MCP) gains `partial`: a repo with documents but no indexed commit yet. The admin page shows such a repo as "indexing (partial)". - Server pushes and syncs now take the per-source advisory lock that `lore index` already held. Embedding now runs before the row lock, so without it a server and a Job would embed the same repo at once. A push meeting a running index gets 409 index_in_progress; the poller skips the repo quietly. - Each write transaction locks the source row and checks the indexed commit (lock_source), for a process without the advisory lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… fresh source - The advisory lock connects with the pool's own options, not LORE_DATABASE_URL. Advisory locks are per database: the server's lock landed in another database than the one it wrote to (tests passed locally only because both URLs pointed at the same server; CI would fail). Its connect and lock query are bounded by the query timeout. - Push takes the advisory lock before reading the source; sync re-reads the source once it holds the lock and stands down (Busy) if another process indexed meanwhile. Busy is not recorded as a sync error. - A later index holds at most LORE_MAX_HELD_CHUNKS (50k, about 100 MB) prepared chunks for its one transaction; files past it are embedded first (warming the cache) and prepared again in the transaction. - Fresh vectors are published to the cache after a failed run too: they are what the retry needs. - The first-index and later-index paths are explicit branches; Job::delete_doc is gone. - The cli_index tests of a push queued behind a Job now expect 409 index_in_progress: both hold the same advisory lock now. Their old pass relied on the lock landing in another database. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Why
Indexing glassflow/tares took 108 minutes, and 105 of them were embedding. All of it ran inside one transaction, and new vectors reached
embedding_cacheonly after the commit. If the index Job hit its time limit or crashed, everything rolled back, so the retry started from zero.What changes
Every index run
embedding_cacheon a second connection, and the rest are written at the end, even if the run fails. A retry finds them and skips that embedding.A repo's first index (no
indexed_commityet)LORE_FIRST_INDEX_BATCHfiles (default 200) commit in their own transaction, so the repo is searchable within minutes.indexed_commitis set only by the last transaction.list_sourcestool returnspartial: trueuntil the last batch commits. The admin page shows "indexing (partial)".Later indexes
LORE_MAX_HELD_CHUNKS(default 50k, about 100 MB) prepared chunks wait for that transaction. Files past it are embedded first, which warms the cache, then prepared again from the cache inside the transaction.One lock across processes
lore indexalready held.index_in_progress. The poller skips that repo quietly and doesn't record an error.Behaviour changes
index_in_progressinstead of queueing behind it.Review
A second model reviewed the change independently. Its findings that this PR fixes:
LORE_DATABASE_URL, not the pool's database. This would have failed CI, which runs Postgres on 5432.indexed_commit.Known and not fixed here:
Testing
cargo fmt --check,cargo clippy --all-targets -- -D warningsandcargo test --all-targetspass against Postgres. New tests cover:The
cli_indextests of a push queued behind a Job now expect 409. They used to pass only because the two locks were in different databases.🤖 Generated with Claude Code