How do you detect and sweep orphaned chunks in a RAG index, and prove that a full re-run of ingestion changes nothing?
answer
- store versus registry, both directions
- unknown owner, inactive generation, extra position
- leave in-flight writes alone
- zero writes on the second run
- a digest of the chunk set
basics
~20 sReconcile the store against the document registry: chunks with an unknown document, an inactive generation or an unowned ID are orphans, deleted after a grace period. Prove idempotence by re-running on unchanged sources: zero writes and an unchanged chunk-set digest.
solid answer
~50 sNormal runs leave orphans when something fails halfway: a crash between write and retire, a delete that errored, a bug in an old pipeline version, an ID scheme change. A sweep finds them by scanning the store's IDs and payloads, not by similarity search, and comparing with the registry: chunks with an unknown `doc_id`, a generation other than the active one, or an ID missing from the document's chunk list. It skips anything newer than a grace period so it never deletes a replacement in flight, reports in dry-run first, and has a size brake. The reverse check finds registry entries whose chunks are missing. To prove a re-run is a no-op, run the pipeline twice over unchanged sources: the second run must report zero embeddings, writes and deletes, and a digest over the sorted `(chunk_id, chunk_hash)` pairs must not change.
go deeper
Recall what an orphan is: a chunk no document claims, still retrievable. Say why a normal run never revisits it.
Explain the store-versus-registry comparison and the classes it finds, and why the second run of an idempotent pipeline writes nothing.
Show the grace period for in-flight generations, the reverse check for holes, the digest versus count point, and the sweep's own delete brake.
Decide what the platform measures continuously, such as orphan and hole counts and the double-run test in automated checks, so regressions surface before users quote stale text.
## Where orphans come from even in a good design A retrieval-augmented generation (RAG) index stores each source document as a set of **chunks**, and a well-built ingestion pipeline replaces and deletes those sets carefully: deterministic chunk IDs, a **document registry** recording which chunks each document owns, deletes for chunks a shrunken document no longer has, and a deletion diff for removed documents. Orphans still appear, because every one of those steps can fail halfway: - a crash after new chunks were written but before the old ones were deleted; - a delete call that errored or timed out and was never retried; - an earlier pipeline version with a bug, whose leftovers no current run looks at; - a change of ID scheme, which leaves every record under the old scheme unclaimed; - a manual backfill or hot-fix that wrote records outside the pipeline. An **orphan** is a chunk the registry does not account for. It is still retrievable, so it can surface in answers. A periodic **sweep** is the only thing that finds orphans no normal run will revisit. ## The reconciliation sweep The sweep compares two inventories: what the **store** holds and what the **registry** says should exist. 1. **Enumerate the store** by ID and payload. Many stores offer a paged scan or a list by metadata; this is a scan, not a similarity search, which only finds what happens to be near a query vector and can never prove completeness. If the store cannot list its records, keep an append-only log of every chunk ID the pipeline has ever written, and reconcile against that. 2. **Classify each chunk** against the registry: | Finding | Meaning | Action | |---|---|---| | `doc_id` not in the registry | Owner deleted or never recorded | Delete | | Generation older than the document's active one | A retired set whose delete failed | Delete | | Generation newer than active, older than the grace period | An abandoned replacement | Delete | | Generation newer than active, within the grace period | A replacement being written now | Leave alone | | ID not in the document's chunk list | A position the document no longer has | Delete | | Two chunks with the same document, generation and position | A duplicate write under different IDs | Keep the registered one | 3. **Check the reverse direction**: registry entries whose chunk IDs are missing from the store are **holes**, documents that answer nothing. Re-queue them. 4. **Guard the sweep** like any bulk delete: a dry-run report first, a brake on the share of a source it may delete in one pass, and per-source scope. Whether a chunk the sweep deleted has actually stopped appearing in results, and how a store reclaims deleted records internally, are the store's concerns. The sweep counts and deletes what the registry disowns. ## Proving a full re-run is a no-op Idempotence is the property every re-indexing design claims and few teams test. The test is direct: 1. Take a **frozen fixture** of sources, or a source window known to be unchanged. 2. Run the full pipeline once and record a **digest** of the chunk set: sort every `(chunk_id, chunk_hash)` pair, where `chunk_hash` covers the chunk text and its payload, and hash the sorted list. 3. Run it again over the same input. 4. Require **zero** embedding calls, **zero** writes, **zero** deletes, and an **identical digest**. A matching chunk *count* alone proves little: one duplicate plus one missing chunk leaves the count unchanged. The digest catches both. Alongside the digest, assert the structural invariants: - every document's chunk count in the store equals its count in the registry; - no chunk's `doc_id` is outside the registry; - no two live chunks share a document and position; - every document has exactly one active generation. Run the test in the pipeline's automated checks on a fixture corpus with edits, shrinks, deletions and an emptied page, and run the invariants in production as a scheduled job that publishes an **orphan count** and a **hole count**. A count that starts climbing is the early warning that a write path regressed. ## A worked scenario A documentation site re-indexed nightly has 8,000 pages and about 70,000 chunks (the scenario's figures). After a week in which one night's job was killed mid-run, the sweep reports 41 chunks with a retired generation and 3 under an unknown `doc_id`. The dry run is under the brake, so the sweep deletes all 44. The next night's double-run test returns zero writes and a matching digest, which is the evidence that the write path itself is sound and the orphans came from the killed run.
- Why can't the sweep use similarity search to enumerate the store?A nearest-neighbour query returns only records close to the query vector, and approximate search may skip some even then. No set of queries can show you have seen every record, so a sweep built on search can never prove there are no orphans. Use a scan by ID or metadata, or a log of every ID written.
- The double-run test passes on fixtures but production shows a growing orphan count. Where do you look?At paths the fixture does not exercise: partial failures and retries, concurrent runs over one document, a connector that writes outside the registry, or shapes the fixture lacks, such as a page emptied to zero chunks. Add a fixture case that reproduces whichever one it is.
- If each run succeeds, there can be no orphans to sweep.
- A matching total chunk count proves a re-run changed nothing.
- Delete every chunk whose generation is not the active one, immediately.
- Similarity search over random vectors is enough to enumerate the store.
- The job's exit status shows the re-run was a no-op.