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: pass collection_id to load_or_create_faiss_index in 3 callsites (#2204)

LearningCircuit436568d75464
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 6efc41c08f..2e82dd3a06 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
@@ -1191,9 +1191,14 @@ class LibraryRAGService:
source_id=str(file_path),
)
- # Load or create FAISS index
+ # Load or create FAISS index using default library collection
if self.faiss_index is None:
- self.faiss_index = self.load_or_create_faiss_index()
+ from ...database.library_init import get_default_library_id
+
+ default_collection_id = get_default_library_id(self.username)
+ self.faiss_index = self.load_or_create_faiss_index(
+ default_collection_id
+ )
# Filter out chunks that already exist in FAISS and deduplicate
if self.faiss_index is not None:
@@ -1305,7 +1310,11 @@ class LibraryRAGService:
# Load or create FAISS index for this collection
if self.faiss_index is None:
- self.faiss_index = self.load_or_create_faiss_index()
+ # Extract collection_id from collection_name (format: "collection_<uuid>")
+ collection_id = collection_name.removeprefix("collection_")
+ self.faiss_index = self.load_or_create_faiss_index(
+ collection_id
+ )
# If force_reindex, remove old chunks from FAISS before adding new ones
if force_reindex:
@@ -1429,7 +1438,11 @@ class LibraryRAGService:
# Load FAISS index if not already loaded
if self.faiss_index is None:
- self.faiss_index = self.load_or_create_faiss_index()
+ # Extract collection_id from collection_name (format: "collection_<uuid>")
+ collection_id = collection_name.removeprefix("collection_")
+ self.faiss_index = self.load_or_create_faiss_index(
+ collection_id
+ )
# Remove from FAISS index
if hasattr(self.faiss_index, "delete"):
diff --git a/tests/research_library/services/test_library_rag_service.py b/tests/research_library/services/test_library_rag_service.py
index 23d6961084..02b1e047f6 100644
--- a/tests/research_library/services/test_library_rag_service.py
+++ b/tests/research_library/services/test_library_rag_service.py
@@ -1099,6 +1099,205 @@ class TestRemoveCollectionFromIndex:
assert method is not None
+class TestLoadOrCreateFaissIndexCollectionId:
+ """Tests that load_or_create_faiss_index receives the correct collection_id.
+
+ Fixes a pre-existing bug where index_local_file, index_user_document,
+ and remove_collection_from_index called load_or_create_faiss_index()
+ without the required collection_id argument.
+ """
+
+ def _create_service(self, mocker):
+ """Create a minimally mocked LibraryRAGService."""
+ mock_session = MagicMock()
+ mock_session.__enter__ = Mock(return_value=mock_session)
+ mock_session.__exit__ = Mock(return_value=False)
+
+ mocker.patch(
+ "local_deep_research.research_library.services.library_rag_service.get_user_db_session",
+ return_value=mock_session,
+ )
+
+ mock_embedding_manager = Mock()
+ mock_embedding_manager.embeddings = Mock()
+ mock_embedding_manager.embeddings.embed_query.return_value = [0.1] * 384
+
+ mock_splitter = Mock()
+ mocker.patch(
+ "local_deep_research.research_library.services.library_rag_service.get_text_splitter",
+ return_value=mock_splitter,
+ )
+
+ mock_integrity = Mock()
+ mocker.patch(
+ "local_deep_research.research_library.services.library_rag_service.FileIntegrityManager",
+ return_value=mock_integrity,
+ )
+
+ from local_deep_research.research_library.services.library_rag_service import (
+ LibraryRAGService,
+ )
+
+ service = LibraryRAGService(
+ username="test_user",
+ embedding_manager=mock_embedding_manager,
+ )
+ return service
+
+ def test_index_local_file_passes_default_library_id(self, mocker, tmp_path):
+ """index_local_file passes the default library collection_id."""
+ service = self._create_service(mocker)
+
+ # Create a real temp file
+ test_file = tmp_path / "test.txt"
+ test_file.write_text("This is test content for indexing purposes.")
+
+ # Mock the FAISS index (simulate faiss_index being None)
+ service.faiss_index = None
+ mock_faiss_index = MagicMock()
+ service.load_or_create_faiss_index = Mock(return_value=mock_faiss_index)
+
+ # Mock _store_chunks_to_db
+ service.embedding_manager._store_chunks_to_db = Mock(
+ return_value=["id-1", "id-2"]
+ )
+
+ # Mock text_splitter to return chunks
+ from langchain_core.documents import Document as LangchainDocument
+
+ mock_chunks = [
+ LangchainDocument(page_content="chunk 1"),
+ LangchainDocument(page_content="chunk 2"),
+ ]
+ service.text_splitter.split_documents.return_value = mock_chunks
+
+ # Mock get_default_library_id
+ mocker.patch(
+ "local_deep_research.database.library_init.get_default_library_id",
+ return_value="default-lib-uuid",
+ )
+
+ # Mock rag_index_record for save path
+ service.rag_index_record = None
+
+ service.index_local_file(str(test_file))
+
+ # Verify load_or_create_faiss_index was called with the default library id
+ service.load_or_create_faiss_index.assert_called_once_with(
+ "default-lib-uuid"
+ )
+
+ def test_index_user_document_passes_extracted_collection_id(self, mocker):
+ """index_user_document extracts collection_id from collection_name."""
+ service = self._create_service(mocker)
+
+ # Create mock user document
+ mock_user_doc = Mock()
+ mock_user_doc.id = "user-doc-123"
+ mock_user_doc.text_content = (
+ "Enough content for indexing test purposes."
+ )
+ mock_user_doc.filename = "test.pdf"
+ mock_user_doc.file_type = "pdf"
+ mock_user_doc.file_size = 1024
+
+ # Mock FAISS index as None to trigger load
+ service.faiss_index = None
+ mock_faiss_index = MagicMock()
+ mock_faiss_index.docstore = Mock()
+ mock_faiss_index.docstore._dict = {}
+ service.load_or_create_faiss_index = Mock(return_value=mock_faiss_index)
+
+ # Mock _store_chunks_to_db
+ service.embedding_manager._store_chunks_to_db = Mock(
+ return_value=["chunk-1", "chunk-2"]
+ )
+
+ # Mock text_splitter
+ from langchain_core.documents import Document as LangchainDocument
+
+ mock_chunks = [
+ LangchainDocument(page_content="chunk 1"),
+ LangchainDocument(page_content="chunk 2"),
+ ]
+ service.text_splitter.split_documents.return_value = mock_chunks
+
+ # Mock rag_index_record for save path
+ service.rag_index_record = None
+
+ collection_name = "collection_abc-uuid-123"
+ service.index_user_document(mock_user_doc, collection_name)
+
+ # Verify collection_id was correctly extracted and passed
+ service.load_or_create_faiss_index.assert_called_once_with(
+ "abc-uuid-123"
+ )
+
+ def test_remove_collection_passes_extracted_collection_id(self, mocker):
+ """remove_collection_from_index extracts collection_id from collection_name."""
+ service = self._create_service(mocker)
+
+ # Mock DB session to return chunks — patch at source since method does local import
+ mock_session = MagicMock()
+ mock_session.__enter__ = Mock(return_value=mock_session)
+ mock_session.__exit__ = Mock(return_value=False)
+
+ mock_chunk = Mock()
+ mock_chunk.id = "chunk-1"
+ mock_session.query.return_value.filter_by.return_value.all.return_value = [
+ mock_chunk
+ ]
+
+ mocker.patch(
+ "local_deep_research.database.session_context.get_user_db_session",
+ return_value=mock_session,
+ )
+
+ # Mock FAISS index as None to trigger load
+ service.faiss_index = None
+ mock_faiss_index = MagicMock()
+ service.load_or_create_faiss_index = Mock(return_value=mock_faiss_index)
+
+ collection_name = "collection_xyz-uuid-456"
+ service.remove_collection_from_index(collection_name)
+
+ # Verify collection_id was correctly extracted and passed
+ service.load_or_create_faiss_index.assert_called_once_with(
+ "xyz-uuid-456"
+ )
+
+ def test_index_local_file_skips_load_when_faiss_already_set(
+ self, mocker, tmp_path
+ ):
+ """index_local_file does not call load_or_create_faiss_index when faiss_index is already set."""
+ service = self._create_service(mocker)
+
+ test_file = tmp_path / "test.txt"
+ test_file.write_text("This is test content for indexing purposes.")
+
+ # Set faiss_index to non-None (already loaded)
+ mock_faiss_index = MagicMock()
+ service.faiss_index = mock_faiss_index
+ service.load_or_create_faiss_index = Mock()
+
+ service.embedding_manager._store_chunks_to_db = Mock(
+ return_value=["id-1"]
+ )
+
+ from langchain_core.documents import Document as LangchainDocument
+
+ service.text_splitter.split_documents.return_value = [
+ LangchainDocument(page_content="chunk 1"),
+ ]
+
+ service.rag_index_record = None
+
+ service.index_local_file(str(test_file))
+
+ # load_or_create_faiss_index should NOT be called
+ service.load_or_create_faiss_index.assert_not_called()
+
+
class TestDeduplicateChunks:
"""Tests for _deduplicate_chunks static method.