Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(library): do not promote an index created on a read path (#6321)

Sebastian Cao61c2eca49c2f
diff --git a/changelog.d/5745.bugfix.md b/changelog.d/5745.bugfix.md
new file mode 100644
index 0000000000..e34210bc71
--- /dev/null
+++ b/changelog.d/5745.bugfix.md
@@ -0,0 +1 @@
+A library or collection search that runs under an index configuration the collection was not indexed with no longer hijacks the collection's current index: the empty index row such a search creates now stays non-current, so the real index keeps serving every other search until an actual indexing run switches it.
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.py
index 7df533fc2f..6d761f1041 100644
--- a/src/local_deep_research/research_library/services/library_rag_service.py
+++ b/src/local_deep_research/research_library/services/library_rag_service.py
@@ -817,12 +817,13 @@ class LibraryRAGService:
) -> RAGIndex:
"""Get or create RAGIndex record for the current configuration.
- ``promote_current`` (default True) controls whether reusing an existing,
- not-current row re-promotes it to ``is_current`` (demoting others). Read
- paths (search) pass False: a mere search under a stale/different config
- must NOT silently flip which index the collection considers current —
- only an actual write (index) should. Creating a brand-new index still
- promotes it regardless (it's the collection's only index for that config).
+ ``promote_current`` (default True) controls whether the resolved row,
+ reused or just created, is promoted to ``is_current`` (demoting
+ others). Read paths (search) pass False: a mere search under a
+ stale/different config must NOT silently flip which index the
+ collection considers current; only an actual write (index) should.
+ A row created on a read path is still persisted, but stays
+ non-current until an indexing run promotes it.
"""
if db_session is None:
with get_user_db_session(
@@ -905,8 +906,12 @@ class LibraryRAGService:
created = True
logger.info(f"Created new RAG index: {index_hash}")
- mutated = created or (promote_current and not rag_index.is_current)
- if mutated:
+ # Only a write path (promote_current=True) may move the collection's
+ # is_current pointer. A read path that had to create the row leaves
+ # it non-current: the collection's real index must keep serving every
+ # other caller until an actual indexing run switches over.
+ promoted = promote_current and not rag_index.is_current
+ if promoted:
db_session.query(RAGIndex).filter(
RAGIndex.collection_name == collection_name,
RAGIndex.is_current.is_(True),
@@ -918,7 +923,7 @@ class LibraryRAGService:
# A pure read-through of an existing current index must stay a no-op
# so it can't consume a caller's pending write (e.g. the fault the
# index-finalize self-heal backstop guards against).
- if commit and mutated:
+ if commit and (created or promoted):
db_session.commit()
db_session.refresh(rag_index)
return rag_index
diff --git a/tests/web/routers/test_rag_indexing_pipeline.py b/tests/web/routers/test_rag_indexing_pipeline.py
index 32bc4429be..c6c43adb39 100644
--- a/tests/web/routers/test_rag_indexing_pipeline.py
+++ b/tests/web/routers/test_rag_indexing_pipeline.py
@@ -634,6 +634,89 @@ class TestChunkingSettingsAtQueryTime:
"is_current -- the row both search engines look up"
)
+ @pytest.mark.parametrize(
+ ("promote_current", "expect_created_current"),
+ [(False, False), (True, True)],
+ ids=["read-path", "write-path"],
+ )
+ def test_a_row_created_on_a_read_path_is_not_promoted(
+ self,
+ world,
+ indexed_with_nondefault_separators,
+ promote_current,
+ expect_created_current,
+ ):
+ """``promote_current`` gates the promotion of a row
+ ``_get_or_create_rag_index`` had to CREATE, not only of a reused one.
+
+ The write-path case is the control: an indexing run under a new
+ configuration still promotes its fresh row (demoting the old one)
+ exactly as before, so the gate is what keeps a read path from moving
+ the pointer -- the second half of #5745."""
+ session = world
+ collection_id, _ = indexed_with_nondefault_separators
+ collection_name = f"collection_{collection_id}"
+ original_id = _sole_index_row(session, collection_id).id
+
+ # Default separators: a configuration the collection was never
+ # indexed with, so the row must be created rather than reused.
+ created = _make_service()._get_or_create_rag_index(
+ collection_id, promote_current=promote_current
+ )
+
+ session.expire_all()
+ assert created.id != original_id
+ assert created.chunk_count == 0
+ current_ids = [
+ row.id
+ for row in session.query(RAGIndex)
+ .filter_by(collection_name=collection_name)
+ .all()
+ if row.is_current
+ ]
+ expected_current = created.id if expect_created_current else original_id
+ assert current_ids == [expected_current], (
+ "a row created under promote_current=False was promoted to "
+ "is_current, demoting the collection's real index"
+ )
+
+ def test_mismatched_search_leaves_the_real_index_serving(
+ self, world, indexed_with_nondefault_separators
+ ):
+ """The end-to-end shape of the hijack in #5745: a search under a
+ configuration the collection was not indexed with (a stand-in for the
+ next identity field a caller forgets to thread) may create its own
+ empty index, but a correctly-configured search afterwards must still
+ reach the content -- the mismatch is not permanent any more."""
+ session = world
+ collection_id, _ = indexed_with_nondefault_separators
+ collection_name = f"collection_{collection_id}"
+ original_id = _sole_index_row(session, collection_id).id
+
+ mismatched_hits = _make_service().search(QUERY, collection_id, 5)
+ assert not any(SECRET in h.text for h in mismatched_hits)
+
+ session.expire_all()
+ current_ids = [
+ row.id
+ for row in session.query(RAGIndex)
+ .filter_by(collection_name=collection_name)
+ .all()
+ if row.is_current
+ ]
+ assert current_ids == [original_id], (
+ "one mismatched search flipped the collection's current index "
+ "to the empty row it created"
+ )
+
+ hits = _make_service(text_separators=NON_DEFAULT_SEPARATORS).search(
+ QUERY, collection_id, 5
+ )
+ assert any(SECRET in h.text for h in hits), (
+ "after a mismatched search the correctly-configured search no "
+ "longer finds the indexed content"
+ )
+
# ---------------------------------------------------------------------------
# (3) CWE-209: backend failure detail reaches the index-status endpoint