fix(rag): reuse per-file embeddings during indexing#1306
Conversation
|
Solid fix with the right invariant verified by the right tests. The algorithm is correct and the optimization is meaningful — the steady-state multi-document path now encodes each file exactly once instead of re-encoding the entire accumulated corpus on every addition. SummaryThe change removes two sources of redundant embedding work: (1) rebuilding embeddings for all previous chunks every time a new document is added, and (2) encoding the new file a second time to build the per-file index. Both the fresh-index and cache-load paths are updated consistently, and two regression tests nail the exact invariant. No security concerns. One correctness subtlety and a pair of style nits worth addressing. Issues Found🟡 Misleading variable name conceals in-place mutation (
|
|
The embedding work for multi-document indexing drops from O(total chunks) to O(new chunks) per addition — a real win that's already visible in the #1305 complaint. The implementation is correct and the two new tests give precise regression coverage for both indexing paths. Issues🟡 Broad
|
Summary
old_chunks + new_chunksevery time a document is addedWhy
The old indexing path did two expensive things in the normal multi-document flow:
The cache-load path had the same shape of duplication.
That made indexing cost grow much faster than it should as more documents were added, and it was directly visible in the code path behind #1305.
This change keeps the behavior the same from the outside, but switches the steady-state path to:
Closes #1305.
Testing
uv run pytest tests/test_rag.py -q -k 'indexing_two_documents_embeds_each_file_once or cache_load_embeds_each_cached_file_once or cache_load_tracks_access_times or cache_functionality or document_indexing'uv run pytest tests/test_rag.py -q -k 'query_uses_consistent_snapshot_during_state_swap or remove_document_preserves_state_when_rebuild_fails'uv run pytest tests/test_rag.py -quv run python -m compileall src/gaia/rag/sdk.py tests/test_rag.py