fix(rag-worker): use deterministic embedding point ids to avoid orphaned duplicate vectors#622
Open
serhiizghama wants to merge 2 commits into
Conversation
Derive Qdrant point IDs from the document id and chunk index via an RFC 4122 v5 UUID instead of a random UUID per run. This is a pure addition; call sites are updated in the next commit.
…cates The file, scrape and text RAG pipelines generated a fresh random UUID for every Qdrant point on each run. When a job is retried after the Qdrant upsert succeeded but a later step (e.g. the MongoDB status update) failed, the retry wrote a brand-new set of points while the originals stayed in the collection, leaving orphaned duplicate vectors that degrade retrieval and are no longer tracked by the app. Derive each point id from the document id and chunk index so retries upsert over the same points and stay idempotent. Fixes rowboatlabs#603
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.
Problem
Closes #603.
The RAG worker writes embeddings to Qdrant and then updates the document record in MongoDB as two separate network calls. Point IDs are generated with
crypto.randomUUID()on every run:If the Qdrant upsert succeeds but a later step fails (MongoDB timeout, connection drop, version conflict), the worker throws and the document becomes eligible for retry. On retry, fresh random UUIDs are generated, so a new set of points is inserted while the originals remain in the collection — the app no longer holds any reference to them. Over time this accumulates orphaned duplicate vectors that:
The same pattern is present in all three pipelines:
runProcessFilePipeline,runScrapePipeline, andrunProcessTextPipeline.Solution
Derive each point ID deterministically from the document id and chunk index instead of generating a random UUID per run:
embeddingPointId()produces an RFC 4122 v5 (name-based) UUID from${docId}-chunk-${chunkIndex}under a fixed namespace. Because the same chunk always maps to the same id, a retry upserts over the existing points rather than inserting duplicates — the operation becomes idempotent. The return value is a valid UUID, whichEmbeddingRecord.id(z.string().uuid()) requires.The helper is dependency-free (uses the already-imported Node
cryptomodule), so no new packages are added. Deletion (runDeletionPipeline) filters by thedocIdpayload field rather than by point id, so it is unaffected.Testing
The repo has no test harness, so I verified the helper's behavior in isolation:
embeddingPointId("doc123", 0)returns the same id across calls.^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$, satisfyingEmbeddingRecord.Re-running any pipeline for the same document now overwrites the existing points in Qdrant instead of adding a duplicate set.