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.

69.8 KB
Raw Blame
"""
Coverage tests for LibraryRAGService.

Focuses on logic paths not exercised by the existing test_library_rag_service.py:
- _get_index_hash edge cases
- _get_index_path cache directory details
- _get_or_create_rag_index new vs existing
- index_document chunk indexing, empty text, skip already indexed
- index_all_documents with progress callback, stores settings
"""

import hashlib
import json
import threading
import time
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.constants import (
    DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
    DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
    DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC,
    DEFAULT_LOCAL_SEARCH_INDEX_TYPE,
    DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS,
    DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
    DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS,
)
from local_deep_research.database.models.library import RAGIndexStatus

# ---------------------------------------------------------------------------
# Module-level patch path prefix
# ---------------------------------------------------------------------------
_MOD = "local_deep_research.research_library.services.library_rag_service"


def _make_service(**overrides):
    """Create a LibraryRAGService with all external deps mocked out."""
    with (
        patch(f"{_MOD}.LocalEmbeddingManager") as _lem,
        patch(f"{_MOD}.get_user_db_session"),
        patch(f"{_MOD}.FileIntegrityManager") as _fim,
        patch(f"{_MOD}.get_text_splitter") as _gts,
    ):
        _lem.return_value.embeddings = MagicMock()
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        defaults = dict(username="testuser", db_password="pw")
        defaults.update(overrides)
        svc = LibraryRAGService(**defaults)
    return svc


# =========================================================================
# _get_index_hash
# =========================================================================
class TestGetIndexHash:
    """Tests for _get_index_hash determinism and sensitivity."""

    def test_hash_is_deterministic(self):
        svc = _make_service()
        h1 = svc._get_index_hash("col_a", "model_x", "sentence_transformers")
        h2 = svc._get_index_hash("col_a", "model_x", "sentence_transformers")
        assert h1 == h2

    def test_hash_changes_with_collection_name(self):
        svc = _make_service()
        h1 = svc._get_index_hash("col_a", "model_x", "sentence_transformers")
        h2 = svc._get_index_hash("col_b", "model_x", "sentence_transformers")
        assert h1 != h2

    def test_hash_changes_with_model(self):
        svc = _make_service()
        h1 = svc._get_index_hash("col_a", "model_x", "sentence_transformers")
        h2 = svc._get_index_hash("col_a", "model_y", "sentence_transformers")
        assert h1 != h2

    def test_hash_changes_with_provider(self):
        svc = _make_service()
        h1 = svc._get_index_hash("col_a", "model_x", "sentence_transformers")
        h2 = svc._get_index_hash("col_a", "model_x", "ollama")
        assert h1 != h2

    def test_hash_is_sha256_hex(self):
        svc = _make_service()
        h = svc._get_index_hash("c", "m", "p")
        identity = {
            "chunk_overlap": DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
            "chunk_size": DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
            "collection_name": "c",
            "distance_metric": DEFAULT_LOCAL_SEARCH_DISTANCE_METRIC,
            "embedding_model": "m",
            "embedding_provider": "p",
            "index_type": DEFAULT_LOCAL_SEARCH_INDEX_TYPE,
            "normalize_vectors": DEFAULT_LOCAL_SEARCH_NORMALIZE_VECTORS,
            "splitter_type": DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
            "text_separators": DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS,
        }
        expected = hashlib.sha256(
            json.dumps(
                identity,
                ensure_ascii=False,
                separators=(",", ":"),
                sort_keys=True,
            ).encode()
        ).hexdigest()
        assert h == expected

    def test_hash_length_is_64_chars(self):
        svc = _make_service()
        h = svc._get_index_hash("x", "y", "z")
        assert len(h) == 64


# =========================================================================
# _get_index_path
# =========================================================================
class TestGetIndexPath:
    """Tests for _get_index_path."""

    @patch(f"{_MOD}.get_cache_directory")
    def test_path_under_per_user_rag_indices_subdir(
        self, mock_cache_dir, tmp_path
    ):
        # Layout: rag_indices/<sha256(username)[:16]>/<hash>.faiss — the
        # per-user scope dir keeps two users' identical collection/model
        # combos from sharing .faiss files.
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        p = svc._get_index_path("abc123")
        assert p.parent.parent.name == "rag_indices"
        expected_scope = hashlib.sha256(b"testuser").hexdigest()[:16]
        assert p.parent.name == expected_scope

    @patch(f"{_MOD}.get_cache_directory")
    def test_path_user_scope_differs_per_user(self, mock_cache_dir, tmp_path):
        mock_cache_dir.return_value = tmp_path
        p1 = _make_service()._get_index_path("abc123")
        p2 = _make_service(username="otheruser")._get_index_path("abc123")
        assert p1 != p2
        assert p1.name == p2.name

    @patch(f"{_MOD}.get_cache_directory")
    def test_path_filename_contains_hash(self, mock_cache_dir, tmp_path):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        p = svc._get_index_path("abc123")
        assert p.name == "abc123.faiss"

    @patch(f"{_MOD}.get_cache_directory")
    def test_path_creates_directory(self, mock_cache_dir, tmp_path):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        svc._get_index_path("abc123")
        assert (tmp_path / "rag_indices").is_dir()

    @patch(f"{_MOD}.get_cache_directory")
    def test_path_returns_path_object(self, mock_cache_dir, tmp_path):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        p = svc._get_index_path("somehash")
        assert isinstance(p, Path)


# =========================================================================
# _get_or_create_rag_index
# =========================================================================
class TestGetOrCreateRagIndex:
    """Tests for _get_or_create_rag_index."""

    @patch(f"{_MOD}.get_user_db_session")
    def test_returns_existing_index(self, mock_session_ctx):
        svc = _make_service()
        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        existing_index = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = existing_index

        result = svc._get_or_create_rag_index("coll-123")
        assert result is existing_index
        # Should NOT call session.add since index already existed
        mock_session.add.assert_not_called()

    @patch(f"{_MOD}.get_cache_directory")
    @patch(f"{_MOD}.get_user_db_session")
    def test_creates_new_index_when_none_exists(
        self, mock_session_ctx, mock_cache_dir, tmp_path
    ):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.return_value = [0.0] * 384

        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        # No existing index found
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        svc._get_or_create_rag_index("coll-456")

        # Should have called session.add for the new RAGIndex
        mock_session.add.assert_called_once()
        mock_session.commit.assert_called_once()
        mock_session.refresh.assert_called_once()

    @patch(f"{_MOD}.get_cache_directory")
    @patch(f"{_MOD}.get_user_db_session")
    def test_new_index_constructs_enum_status(
        self, mock_session_ctx, mock_cache_dir, tmp_path
    ):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.return_value = [0.0] * 384

        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        svc._get_or_create_rag_index("coll-status")

        created_index = mock_session.add.call_args.args[0]
        assert created_index.status is RAGIndexStatus.ACTIVE

    @patch(f"{_MOD}.get_cache_directory")
    @patch(f"{_MOD}.get_user_db_session")
    def test_embeds_test_string_for_dimension(
        self, mock_session_ctx, mock_cache_dir, tmp_path
    ):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.return_value = [0.1] * 768

        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        svc._get_or_create_rag_index("coll-789")

        svc.embedding_manager.embeddings.embed_query.assert_called_once_with(
            "test"
        )


# =========================================================================
# index_document
# =========================================================================
class TestIndexDocument:
    """Tests for index_document."""

    @patch(f"{_MOD}.get_user_db_session")
    def test_returns_error_when_document_not_found(self, mock_session_ctx):
        svc = _make_service()
        mock_session = MagicMock()
        # This unit fixture has no saved collection configuration.
        mock_session.get.return_value = MagicMock(embedding_model=None)
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)
        mock_session.query.return_value.filter_by.return_value.first.return_value = None

        result = svc.index_document("doc-1", "coll-1")
        assert result["status"] == "error"
        assert "not found" in result["error"]

    @patch(f"{_MOD}.ensure_in_collection")
    @patch(f"{_MOD}.get_user_db_session")
    def test_returns_error_when_no_text_content(
        self, mock_session_ctx, mock_ensure
    ):
        svc = _make_service()
        mock_session = MagicMock()
        # This unit fixture has no saved collection configuration.
        mock_session.get.return_value = MagicMock(embedding_model=None)
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        mock_document = MagicMock()
        mock_document.text_content = None

        # After resolving the generation, Document lookup; 2nd = prior-chunk probe -> None so the
        # empty-text branch returns error (no prior chunks to purge).
        mock_session.query.return_value.filter_by.return_value.first.side_effect = [
            None,  # No existing generation for the transaction lock.
            mock_document,
            None,
        ]

        mock_ensure.return_value = MagicMock(indexed=False, chunk_count=0)

        result = svc.index_document("doc-1", "coll-1")
        assert result["status"] == "error"
        assert "no text content" in result["error"]

    @patch(f"{_MOD}.ensure_in_collection")
    @patch(f"{_MOD}.get_user_db_session")
    def test_skips_already_indexed_document(
        self, mock_session_ctx, mock_ensure
    ):
        svc = _make_service()
        mock_session = MagicMock()
        # This unit fixture has no saved collection configuration.
        mock_session.get.return_value = MagicMock(embedding_model=None)
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        mock_document = MagicMock()
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_document

        mock_ensure.return_value = MagicMock(indexed=True, chunk_count=42)

        result = svc.index_document("doc-1", "coll-1", force_reindex=False)
        assert result["status"] == "skipped"
        assert result["chunk_count"] == 42


# =========================================================================
# index_all_documents (index_collection in the spec)
# =========================================================================
class TestIndexAllDocuments:
    """Tests for index_all_documents."""

    @patch(f"{_MOD}.get_user_db_session")
    def test_no_documents_returns_info(self, mock_session_ctx):
        svc = _make_service()
        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)
        mock_session.query.return_value.filter_by.return_value.filter_by.return_value.all.return_value = []

        result = svc.index_all_documents("coll-1")
        assert result["status"] == "info"
        assert result["successful"] == 0

    @patch(f"{_MOD}.get_user_db_session")
    def test_progress_callback_invoked(self, mock_session_ctx):
        svc = _make_service()
        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        mock_dc1 = MagicMock()
        mock_dc1.document_id = "doc-1"
        mock_dc2 = MagicMock()
        mock_dc2.document_id = "doc-2"

        mock_doc = MagicMock()
        mock_doc.title = "Test Doc"

        # filter_by(collection_id=...) -> filter_by(indexed=False) -> all()
        mock_session.query.return_value.filter_by.return_value.filter_by.return_value.all.return_value = [
            mock_dc1,
            mock_dc2,
        ]
        # query(Document).filter_by(id=...).first() for title lookup
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_doc

        # Mock index_document to return success
        svc.index_document = MagicMock(
            return_value={"status": "success", "chunk_count": 10}
        )

        callback = MagicMock()
        svc.index_all_documents("coll-1", progress_callback=callback)

        assert callback.call_count == 2
        # Check the callback was called with (idx, total, title, status)
        first_call = callback.call_args_list[0]
        assert first_call[0][0] == 1  # idx
        assert first_call[0][1] == 2  # total
        assert first_call[0][3] == "success"  # status

    @patch(f"{_MOD}.get_user_db_session")
    def test_counts_successful_skipped_failed(self, mock_session_ctx):
        svc = _make_service()
        mock_session = MagicMock()
        mock_session_ctx.return_value.__enter__ = MagicMock(
            return_value=mock_session
        )
        mock_session_ctx.return_value.__exit__ = MagicMock(return_value=None)

        mock_dc1 = MagicMock()
        mock_dc1.document_id = "doc-1"
        mock_dc2 = MagicMock()
        mock_dc2.document_id = "doc-2"
        mock_dc3 = MagicMock()
        mock_dc3.document_id = "doc-3"

        mock_doc = MagicMock()
        mock_doc.title = "Title"

        # With force_reindex=True the code does:
        #   query(DocumentCollection).filter_by(collection_id=...).all()
        # (no second filter_by for indexed=False)
        mock_query = MagicMock()
        mock_query.filter_by.return_value.all.return_value = [
            mock_dc1,
            mock_dc2,
            mock_dc3,
        ]
        # Document title lookup
        mock_query.filter_by.return_value.first.return_value = mock_doc
        mock_session.query.return_value = mock_query

        results_sequence = [
            {"status": "success", "chunk_count": 5},
            {
                "status": "skipped",
                "message": "already indexed",
                "chunk_count": 3,
            },
            {"status": "error", "error": "something broke"},
        ]
        svc.index_document = MagicMock(side_effect=results_sequence)

        result = svc.index_all_documents("coll-1", force_reindex=True)
        assert result["successful"] == 1
        assert result["skipped"] == 1
        assert result["failed"] == 1
        assert len(result["errors"]) == 1


# =========================================================================
# db_password property
# =========================================================================
class TestDbPasswordProperty:
    """Tests for the db_password property propagation."""

    def test_getter_returns_value(self):
        svc = _make_service()
        svc._db_password = "secret"
        assert svc.db_password == "secret"

    def test_setter_propagates_to_embedding_manager(self):
        svc = _make_service()
        svc.embedding_manager = MagicMock()
        svc.integrity_manager = MagicMock()
        svc.db_password = "new_pw"
        assert svc.embedding_manager.db_password == "new_pw"

    def test_setter_propagates_to_integrity_manager(self):
        svc = _make_service()
        svc.embedding_manager = MagicMock()
        svc.integrity_manager = MagicMock()
        svc.db_password = "new_pw"
        assert svc.integrity_manager.password == "new_pw"

    def test_setter_handles_none_managers(self):
        svc = _make_service()
        svc.embedding_manager = None
        svc.integrity_manager = None
        # Should not raise
        svc.db_password = "pw"
        assert svc._db_password == "pw"


# =========================================================================
# Context manager / close
# =========================================================================
class TestContextManager:
    """Tests for context manager protocol."""

    def test_enter_returns_self(self):
        svc = _make_service()
        assert svc.__enter__() is svc

    def test_exit_calls_close(self):
        svc = _make_service()
        svc.close = MagicMock()
        svc.__exit__(None, None, None)
        svc.close.assert_called_once()

    def test_exit_returns_false(self):
        svc = _make_service()
        result = svc.__exit__(None, None, None)
        assert result is False


# =========================================================================
# Quarantine helper + concurrent-write locks (#4197)
# =========================================================================


def _import_module():
    import local_deep_research.research_library.services.library_rag_service as m

    return m


class TestCorruptionQuarantine:
    """Verify the verify-failure and load-failure paths preserve
    corrupted bytes instead of deleting them.
    """

    def _make_idx(self, tmp_path, name="idx"):
        idx = tmp_path / f"{name}.faiss"
        pkl = tmp_path / f"{name}.pkl"
        idx.write_bytes(b"faiss-bytes")
        pkl.write_bytes(b"pkl-bytes")
        return idx, pkl

    def test_quarantines_faiss_but_deletes_legacy_plaintext_pkl(self, tmp_path):
        svc = _make_service()
        idx, pkl = self._make_idx(tmp_path)
        svc._quarantine_corrupt_index(idx, "test")
        # Original files are gone from their paths
        assert not idx.exists()
        assert not pkl.exists()
        # The .faiss is preserved for recovery (renamed to .corrupt-*)...
        corrupted_faiss = list(tmp_path.glob("idx.faiss.corrupt-*"))
        assert len(corrupted_faiss) == 1
        assert corrupted_faiss[0].read_bytes() == b"faiss-bytes"
        # ...but the legacy plaintext .pkl is DELETED, not preserved: its text
        # lives in the encrypted DB, so renaming it aside would needlessly keep
        # plaintext chunk text on disk.
        assert list(tmp_path.glob("idx.pkl.corrupt-*")) == []

    def test_skips_pkl_when_missing(self, tmp_path):
        svc = _make_service()
        idx = tmp_path / "idx.faiss"
        idx.write_bytes(b"faiss-bytes")
        # No .pkl present
        svc._quarantine_corrupt_index(idx, "test")
        assert not idx.exists()
        corrupted = list(tmp_path.glob("idx.faiss.corrupt-*"))
        assert len(corrupted) == 1
        # No orphan pkl created
        assert not any(tmp_path.glob("idx.pkl.corrupt-*"))

    def test_rename_oserror_re_raises(self, tmp_path):
        """Disk-full / permission failure must propagate, not be swallowed."""
        svc = _make_service()
        idx, _pkl = self._make_idx(tmp_path)
        with patch.object(Path, "rename", side_effect=OSError("EROFS")):
            with pytest.raises(OSError, match="EROFS"):
                svc._quarantine_corrupt_index(idx, "test")

    def test_already_quarantined_by_another_process_does_not_raise(
        self, tmp_path
    ):
        """A concurrent worker PROCESS can quarantine the same corrupt index
        first; our rename then fails because the source is already gone. That
        is the objective already met, not a failure — return gracefully (so the
        caller rebuilds fresh) and still purge any lingering plaintext .pkl."""
        svc = _make_service()
        idx, pkl = self._make_idx(tmp_path)

        def _steal_then_fail(*args, **kwargs):
            # Simulate the other process having already moved the .faiss aside.
            idx.unlink()
            raise FileNotFoundError(2, "No such file or directory")

        with patch.object(Path, "rename", side_effect=_steal_then_fail):
            # Must NOT raise — the corrupt file is already gone.
            svc._quarantine_corrupt_index(idx, "test")

        assert not idx.exists()
        # The lingering plaintext .pkl is purged even on the race path.
        assert not pkl.exists()

    def test_collision_increments_suffix(self, tmp_path):
        """Pre-existing quarantine path → loop increments to -1, -2, ..."""
        svc = _make_service()
        idx, pkl = self._make_idx(tmp_path)
        # Force a specific timestamp so we can predict collision targets
        with patch.object(
            _import_module().time,
            "time_ns",
            return_value=12345,
        ):
            # Pre-create the base collision target
            (tmp_path / "idx.faiss.corrupt-12345").write_bytes(b"old1")
            svc._quarantine_corrupt_index(idx, "test")
        # Original moved out
        assert not idx.exists()
        # New corrupt-12345-1 created (the -1 increment)
        moved = tmp_path / "idx.faiss.corrupt-12345-1"
        assert moved.exists()
        assert moved.read_bytes() == b"faiss-bytes"

    def test_collision_cap_raises(self, tmp_path):
        """If 32 collisions in a row, surface an OSError, don't hang."""
        svc = _make_service()
        mod = _import_module()
        idx, pkl = self._make_idx(tmp_path)
        with patch.object(mod.time, "time_ns", return_value=999):
            # Pre-create base + 32 collisions
            (tmp_path / "idx.faiss.corrupt-999").write_bytes(b"")
            for n in range(1, mod._QUARANTINE_SUFFIX_RETRY_CAP + 1):
                (tmp_path / f"idx.faiss.corrupt-999-{n}").write_bytes(b"")
            with pytest.raises(OSError, match="collisions exceeded"):
                svc._quarantine_corrupt_index(idx, "test")


class TestQuarantineRetention:
    """Retention sweep keeps the rag_indices/ directory from filling
    with old .corrupt-* files on systems that experience recurring
    corruption.
    """

    def _make_idx(self, tmp_path, name="idx"):
        idx = tmp_path / f"{name}.faiss"
        pkl = tmp_path / f"{name}.pkl"
        idx.write_bytes(b"faiss-bytes")
        pkl.write_bytes(b"pkl-bytes")
        return idx, pkl

    def _make_quarantined(self, tmp_path, name, ns, mtime):
        """Create a paired (.faiss, .pkl) quarantined-style file at
        the given ns suffix and stamp it with ``mtime`` so the
        retention sweep's age sort is deterministic.
        """
        faiss_f = tmp_path / f"{name}.faiss.corrupt-{ns}"
        pkl_f = tmp_path / f"{name}.pkl.corrupt-{ns}"
        faiss_f.write_bytes(b"")
        pkl_f.write_bytes(b"")
        import os

        os.utime(faiss_f, (mtime, mtime))
        os.utime(pkl_f, (mtime, mtime))
        return faiss_f, pkl_f

    def test_prune_keeps_most_recent_drops_older(self, tmp_path):
        """After N+2 simulated past corruption events, pruning leaves
        exactly N (most recent by mtime) and removes the oldest 2.
        """
        svc = _make_service()
        mod = _import_module()
        keep = mod._QUARANTINE_KEEP_RECENT

        # Pre-seed keep+2 quarantined pairs with increasing mtimes
        # (later ns → newer mtime).
        for i in range(keep + 2):
            self._make_quarantined(
                tmp_path, "idx", ns=1000 + i, mtime=1000.0 + i
            )

        # Trigger pruning via the public quarantine call (which now
        # invokes the sweep at the end).
        idx, _pkl = self._make_idx(tmp_path)
        svc._quarantine_corrupt_index(idx, "test")

        faiss_corrupts = sorted(tmp_path.glob("idx.faiss.corrupt-*"))
        pkl_corrupts = sorted(tmp_path.glob("idx.pkl.corrupt-*"))
        # keep pre-existing + 1 fresh = keep total on each side
        assert len(faiss_corrupts) == keep
        assert len(pkl_corrupts) == keep

        # Oldest two pre-existing pairs (ns 1000, 1001) must be gone.
        for stale_ns in (1000, 1001):
            assert not (tmp_path / f"idx.faiss.corrupt-{stale_ns}").exists()
            assert not (tmp_path / f"idx.pkl.corrupt-{stale_ns}").exists()

    def test_prune_skipped_when_under_threshold(self, tmp_path):
        """With fewer than the retention cap, no pruning happens."""
        svc = _make_service()
        mod = _import_module()
        keep = mod._QUARANTINE_KEEP_RECENT

        # Pre-seed (keep - 2) quarantined pairs. After this call we'll
        # add 1 more via the quarantine, ending at keep - 1 — still
        # under the cap.
        for i in range(keep - 2):
            self._make_quarantined(
                tmp_path, "idx", ns=2000 + i, mtime=2000.0 + i
            )

        idx, _pkl = self._make_idx(tmp_path)
        svc._quarantine_corrupt_index(idx, "test")

        # All pre-existing + the new one survive.
        assert len(list(tmp_path.glob("idx.faiss.corrupt-*"))) == keep - 1

    def test_prune_only_targets_same_base(self, tmp_path):
        """Pruning of ``idx.faiss.corrupt-*`` must NOT touch
        ``other.faiss.corrupt-*`` — different base paths are
        independent.
        """
        svc = _make_service()
        mod = _import_module()
        keep = mod._QUARANTINE_KEEP_RECENT

        # Many old corrupts for the other base — these MUST be untouched.
        for i in range(keep + 3):
            self._make_quarantined(
                tmp_path, "other", ns=3000 + i, mtime=3000.0 + i
            )
        # Trigger many corrupts for our base so the sweep runs.
        for i in range(keep + 2):
            self._make_quarantined(
                tmp_path, "idx", ns=4000 + i, mtime=4000.0 + i
            )

        idx, _pkl = self._make_idx(tmp_path)
        svc._quarantine_corrupt_index(idx, "test")

        # Other base untouched
        other_corrupts = list(tmp_path.glob("other.faiss.corrupt-*"))
        assert len(other_corrupts) == keep + 3
        # Our base pruned
        idx_corrupts = list(tmp_path.glob("idx.faiss.corrupt-*"))
        assert len(idx_corrupts) == keep

    def test_prune_failure_does_not_propagate(self, tmp_path):
        """If unlink raises during the sweep, the quarantine call
        still succeeds — retention is best-effort.
        """
        svc = _make_service()
        mod = _import_module()
        keep = mod._QUARANTINE_KEEP_RECENT

        # Seed enough corrupts to trigger pruning.
        for i in range(keep + 2):
            self._make_quarantined(
                tmp_path, "idx", ns=5000 + i, mtime=5000.0 + i
            )

        before_count = len(list(tmp_path.glob("idx.faiss.corrupt-*")))
        idx, _pkl = self._make_idx(tmp_path)

        # Allow the quarantine rename to succeed, but make unlink raise
        # for the prune sweep. The quarantine itself uses rename, not
        # unlink, so this only affects the retention path.
        real_unlink = Path.unlink

        def selective_unlink(self):
            if ".corrupt-" in self.name:
                raise PermissionError(f"denied: {self}")
            return real_unlink(self)

        with patch.object(Path, "unlink", selective_unlink):
            # Must not raise — sweep is best-effort.
            svc._quarantine_corrupt_index(idx, "test")

        # Quarantine still produced a new pair even though the sweep
        # was unable to delete anything (count went up, not down).
        after_count = len(list(tmp_path.glob("idx.faiss.corrupt-*")))
        assert after_count == before_count + 1, (
            f"expected one new corrupt file; before={before_count}, "
            f"after={after_count}"
        )

    def test_retention_sort_uses_filename_ns_not_mtime(self, tmp_path):
        """On low-resolution filesystems (FAT32, SMB, ext3) mtime can
        round to whole seconds, making same-second quarantines sort
        non-deterministically by ``st_mtime``. The embedded
        ``-<ns>`` suffix is the authoritative ordering — confirm
        retention drops the file with the *lowest* ns even when
        its mtime would mark it as the *newest*.
        """
        svc = _make_service()
        mod = _import_module()
        keep = mod._QUARANTINE_KEEP_RECENT
        import os

        # Seed exactly `keep` corrupts. The OLDEST ns gets the NEWEST
        # mtime; any retention that sorts by mtime would keep it. A
        # sort by ns must prune it once one more quarantine pushes us
        # over the cap.
        ns_values = list(range(6000, 6000 + keep))
        mtimes = list(reversed([7000.0 + i for i in range(keep)]))
        for ns, mtime in zip(ns_values, mtimes):
            self._make_quarantined(tmp_path, "idx", ns=ns, mtime=mtime)

        # Add one more quarantine via the real helper. Its ns is
        # `time.time_ns()` which is far larger than 6000+, so by ns
        # it's the freshest. Force its mtime to ancient so any
        # mtime-based sort would prune *it* instead of the seeded
        # lowest-ns file — that's the regression this test guards.
        idx, _pkl = self._make_idx(tmp_path)
        original_rename = Path.rename

        def rename_then_age(self, target):
            result = original_rename(self, target)
            os.utime(target, (1.0, 1.0))
            return result

        with patch.object(Path, "rename", rename_then_age):
            svc._quarantine_corrupt_index(idx, "test")

        # The lowest-ns seeded file (ns=6000) must be the one pruned.
        # The other seeded files (ns=6001..6004) plus the brand-new
        # one (highest ns) survive.
        assert not (tmp_path / f"idx.faiss.corrupt-{ns_values[0]}").exists()
        for ns in ns_values[1:]:
            assert (tmp_path / f"idx.faiss.corrupt-{ns}").exists(), (
                f"ns={ns} should still exist after retention sort"
            )


class TestFaissWriteLock:
    """Lock infrastructure: identity, parallelism across keys,
    cleanup on user-close.
    """

    def _reset_locks(self):
        mod = _import_module()
        with mod._faiss_write_locks_lock:
            mod._faiss_write_locks.clear()

    def test_same_key_returns_same_lock(self, tmp_path):
        self._reset_locks()
        mod = _import_module()
        p = str(tmp_path / "shared.faiss")
        a = mod._get_faiss_write_lock("u1", p)
        b = mod._get_faiss_write_lock("u1", p)
        assert a is b

    def test_different_key_returns_different_lock(self, tmp_path):
        self._reset_locks()
        mod = _import_module()
        a = mod._get_faiss_write_lock("u1", str(tmp_path / "a.faiss"))
        b = mod._get_faiss_write_lock("u1", str(tmp_path / "b.faiss"))
        c = mod._get_faiss_write_lock("u2", str(tmp_path / "a.faiss"))
        assert a is not b
        assert a is not c

    def test_pop_removes_only_target_user(self, tmp_path):
        self._reset_locks()
        mod = _import_module()
        u1_lock = mod._get_faiss_write_lock("u1", str(tmp_path / "a.faiss"))
        mod._get_faiss_write_lock("u1", str(tmp_path / "b.faiss"))
        u2_lock = mod._get_faiss_write_lock("u2", str(tmp_path / "a.faiss"))

        mod.pop_faiss_locks_for_user("u1")

        # u1 locks gone (new lookup yields a fresh lock object)
        new_u1 = mod._get_faiss_write_lock("u1", str(tmp_path / "a.faiss"))
        assert new_u1 is not u1_lock
        # u2 lock untouched
        same_u2 = mod._get_faiss_write_lock("u2", str(tmp_path / "a.faiss"))
        assert same_u2 is u2_lock

    def test_pop_does_not_evict_a_held_lock(self, tmp_path):
        # A lock held by an in-flight writer must NOT be evicted — otherwise the
        # next writer would create a fresh lock and race the holder on the same
        # file. An unheld lock IS evicted.
        self._reset_locks()
        mod = _import_module()
        held = mod._get_faiss_write_lock("u1", str(tmp_path / "a.faiss"))
        unheld = mod._get_faiss_write_lock("u1", str(tmp_path / "b.faiss"))

        held.acquire()
        try:
            mod.pop_faiss_locks_for_user("u1")
        finally:
            held.release()

        # Held lock survived: same object -> mutual exclusion preserved.
        assert (
            mod._get_faiss_write_lock("u1", str(tmp_path / "a.faiss")) is held
        )
        # Unheld lock was evicted: fresh object.
        assert (
            mod._get_faiss_write_lock("u1", str(tmp_path / "b.faiss"))
            is not unheld
        )

    def test_concurrent_holders_serialised_for_same_key(self, tmp_path):
        """Two threads acquiring ``_get_faiss_write_lock`` for the same
        ``(username, index_path)`` MUST observe non-overlapping critical
        sections. Regression for the #4197 race.
        """
        self._reset_locks()
        mod = _import_module()
        index_path = tmp_path / "idx.faiss"

        in_flight = 0
        max_in_flight = 0
        observed_overlap = False
        lock_for_counters = threading.Lock()
        first_inside = threading.Event()
        first_can_finish = threading.Event()
        first_holder = [True]  # mutable flag to mark which thread enters first

        def worker():
            with mod._get_faiss_write_lock("u1", str(index_path)):
                nonlocal in_flight, max_in_flight, observed_overlap
                with lock_for_counters:
                    in_flight += 1
                    if in_flight > max_in_flight:
                        max_in_flight = in_flight
                    if in_flight > 1:
                        observed_overlap = True
                    am_first = first_holder[0]
                    first_holder[0] = False
                if am_first:
                    first_inside.set()
                    # Hold the critical section open so the second
                    # thread blocks behind us, not races us.
                    first_can_finish.wait(timeout=2.0)
                with lock_for_counters:
                    in_flight -= 1

        t1 = threading.Thread(target=worker)
        t2 = threading.Thread(target=worker)
        t1.start()
        assert first_inside.wait(timeout=2.0)
        t2.start()
        first_can_finish.set()
        t1.join(timeout=3.0)
        t2.join(timeout=3.0)

        assert not observed_overlap, (
            "Two threads held the same FAISS write lock concurrently."
        )
        assert max_in_flight == 1

    def test_different_keys_run_in_parallel(self, tmp_path):
        """Two threads acquiring locks for different
        ``(username, index_path)`` keys must run simultaneously.
        """
        self._reset_locks()
        mod = _import_module()
        barrier = threading.Barrier(2, timeout=2.0)
        idx1 = tmp_path / "a.faiss"
        idx2 = tmp_path / "b.faiss"

        def worker(index_path):
            with mod._get_faiss_write_lock("u1", str(index_path)):
                # If the lock blocked us, barrier.wait would time out
                barrier.wait()

        t1 = threading.Thread(target=worker, args=(idx1,))
        t2 = threading.Thread(target=worker, args=(idx2,))
        t1.start()
        t2.start()
        t1.join(timeout=3.0)
        t2.join(timeout=3.0)
        # If barrier broke (timeout), one of the threads would be alive
        assert not t1.is_alive()
        assert not t2.is_alive()


class TestTrackedRLockReleaseRace:
    """Regression tests for the release+discard race fixed in PR #5235
    (review comment 5085604502).

    The previous implementation did:
        self._lock.release()
        if not self._lock._is_owned():
            with _faiss_write_locks_lock:
                _faiss_active_lock_keys.discard(self._key)

    Between ``release()`` and the ``_is_owned()`` check, a thread blocked in
    ``acquire()`` could unblock, acquire the lock, and add its key — and the
    then-continuing outer thread would discard that fresh key. With
    ``pop_faiss_locks_for_user`` evicting a "free" lock, the next writer would
    create a fresh lock for the same ``(username, index_path)`` and race the
    holder on the FAISS file. The fix holds ``_faiss_write_locks_lock`` across
    the inner release + depth check + discard.
    """

    def _reset_locks(self):
        mod = _import_module()
        with mod._faiss_write_locks_lock:
            mod._faiss_write_locks.clear()
            mod._faiss_active_lock_keys.clear()

    def test_release_does_not_discard_concurrent_acquire_key(self, tmp_path):
        """The release path must NOT erase a key that another thread just
        added via ``acquire()`` while we were releasing.
        """
        self._reset_locks()
        mod = _import_module()
        p = str(tmp_path / "racy.faiss")
        lock = mod._get_faiss_write_lock("u1", p)

        a_inside = threading.Event()
        a_can_release = threading.Event()
        b_inside = threading.Event()
        b_can_release = threading.Event()

        def holder_a():
            with lock:
                a_inside.set()
                a_can_release.wait(timeout=2.0)
            # Once A returns, its key must have been removed.
            with mod._faiss_write_locks_lock:
                assert lock._key not in mod._faiss_active_lock_keys

        def holder_b():
            a_inside.wait(timeout=2.0)
            with lock:  # blocks until A releases
                b_inside.set()
                # While B holds the lock, its key MUST be in the active set.
                # With the pre-fix code, A's release path could erase the
                # key B just added.
                with mod._faiss_write_locks_lock:
                    assert lock._key in mod._faiss_active_lock_keys, (
                        "Race: A discarded B's key from _faiss_active_lock_keys"
                    )
                b_can_release.set()

        ta = threading.Thread(target=holder_a)
        tb = threading.Thread(target=holder_b)
        ta.start()
        tb.start()
        assert a_inside.wait(timeout=2.0)
        # Give B a moment to enter ``acquire`` and block.
        deadline = time.monotonic() + 2.0
        while time.monotonic() < deadline:
            with mod._faiss_write_locks_lock:
                if lock._key in mod._faiss_active_lock_keys:
                    break
            time.sleep(0.01)
        a_can_release.set()
        assert b_inside.wait(timeout=2.0)
        b_can_release.set()
        ta.join(timeout=3.0)
        tb.join(timeout=3.0)

        with mod._faiss_write_locks_lock:
            assert lock._key not in mod._faiss_active_lock_keys

    def test_reentrant_release_keeps_key_until_final_release(self, tmp_path):
        """Re-entry (acquire twice, release once) must NOT discard the key.
        Regression for the depth-tracking rewrite that replaced the
        CPython-private ``RLock._is_owned()`` accessor.
        """
        self._reset_locks()
        mod = _import_module()
        p = str(tmp_path / "reentrant.faiss")
        lock = mod._get_faiss_write_lock("u1", p)

        # Acquire twice via the explicit method so we can release each
        # step independently and observe the key state in between.
        lock.acquire()
        lock.acquire()
        with mod._faiss_write_locks_lock:
            assert lock._key in mod._faiss_active_lock_keys

        lock.release()  # depth 2 → 1; key MUST still be present
        with mod._faiss_write_locks_lock:
            assert lock._key in mod._faiss_active_lock_keys, (
                "Key was discarded on inner release — re-entry depth "
                "tracking broken"
            )

        lock.release()  # depth 1 → 0; key MUST be discarded
        with mod._faiss_write_locks_lock:
            assert lock._key not in mod._faiss_active_lock_keys

    def test_pop_does_not_evict_lock_after_concurrent_handoff(self, tmp_path):
        """End-to-end: A releases → B acquires, ``pop_faiss_locks_for_user``
        runs while B holds the lock, the lock must NOT be evicted.
        """
        self._reset_locks()
        mod = _import_module()
        p = str(tmp_path / "handoff.faiss")
        lock = mod._get_faiss_write_lock("u1", p)

        a_inside = threading.Event()
        b_inside = threading.Event()
        pop_done = threading.Event()

        def holder_a():
            with lock:
                a_inside.set()
                b_inside.wait(timeout=2.0)
                pop_done.wait(timeout=2.0)

        def holder_b():
            a_inside.wait(timeout=2.0)
            with lock:
                b_inside.set()
                mod.pop_faiss_locks_for_user("u1")
                pop_done.set()
                # After pop_faiss_locks_for_user, the held lock must STILL
                # be the one returned by subsequent lookups.
                same = mod._get_faiss_write_lock("u1", p)
                assert same is lock, (
                    "Held lock was evicted — next writer would race B on "
                    "the same file"
                )

        ta = threading.Thread(target=holder_a)
        tb = threading.Thread(target=holder_b)
        ta.start()
        tb.start()
        ta.join(timeout=3.0)
        tb.join(timeout=3.0)
        assert not ta.is_alive()
        assert not tb.is_alive()

    def test_lock_does_not_use_cpython_private_is_owned(self, tmp_path):
        """Defensive: the wrapper must not call ``threading.RLock._is_owned``.

        ``_is_owned`` is a CPython private API and is absent on PyPy and other
        implementations. Pinning against its use here so a future refactor
        does not silently re-introduce a PyPy-broken shortcut. The check
        only inspects method bodies (the class's docstring mentions the
        API by name in the explanation of the rewrite).
        """
        import inspect

        mod = _import_module()
        for name, member in vars(mod._TrackedRLock).items():
            if name.startswith("__") and name.endswith("__"):
                continue
            try:
                src = inspect.getsource(member)
            except (TypeError, OSError):
                continue
            assert "_is_owned" not in src, (
                f"_TrackedRLock.{name} must not rely on the CPython-private "
                "threading.RLock._is_owned() API"
            )

    def test_failed_non_blocking_acquire_does_not_corrupt_tracking(
        self, tmp_path
    ):
        """A non-blocking acquire() attempt that fails must not set owner or depth."""
        self._reset_locks()
        mod = _import_module()
        p = str(tmp_path / "nonblocking.faiss")
        lock = mod._get_faiss_write_lock("u1", p)

        acquired = lock.acquire()
        assert acquired is True

        failed_acquire = None

        def attempt_b():
            nonlocal failed_acquire
            failed_acquire = lock.acquire(blocking=False)

        tb = threading.Thread(target=attempt_b)
        tb.start()
        tb.join(timeout=2.0)

        assert failed_acquire is False
        # Thread B must not have corrupted lock._owner or depth
        assert lock.locked() is True
        assert lock._owner == threading.get_ident()
        assert lock._depth == 1

        lock.release()
        assert lock._depth == 0
        assert lock._owner is None


class TestNoWalCheckpointInIndexingPath:
    """Regression guard: the per-document PRAGMA wal_checkpoint(FULL)
    has been removed (#4197 secondary fix from PR #3539). Re-adding it
    would re-introduce 'database is locked' errors under concurrent
    bulk indexing.
    """

    def test_module_source_has_no_executable_wal_checkpoint(self):
        import inspect

        mod = _import_module()
        src = inspect.getsource(mod)
        # Strip out comment / docstring lines so the regression test
        # tolerates explanatory references like 'PRAGMA wal_checkpoint'
        # in comments, but fails if any code line re-adds the call.
        for lineno, line in enumerate(src.splitlines(), 1):
            stripped = line.strip()
            if not stripped:
                continue
            if stripped.startswith("#"):
                continue
            assert "wal_checkpoint(" not in stripped, (
                f"library_rag_service.py:{lineno} re-introduced "
                f"a wal_checkpoint() call: {line!r}"
            )


class TestPurgeDocumentVectors:
    """purge_document_vectors: FAISS-side cleanup for deleted documents
    (note deletion). Replace-on-reindex can never fire for a deleted id,
    and collection search serves snippets straight from the docstore, so
    without this the deleted content surfaced in search indefinitely."""

    def _reset_locks(self):
        mod = _import_module()
        with mod._faiss_write_locks_lock:
            mod._faiss_write_locks.clear()

    def _make_session_patch(self, mod, rag_index_row):
        from contextlib import contextmanager

        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            rag_index_row
        )
        # The merge's ownership query: no foreign-owned rows.
        session.query.return_value.filter.return_value = []
        # The merge's shared-text survivor query streams member texts via
        # query().join().filter().yield_per(); no other members → empty, so
        # everything stays purgeable.
        (
            session.query.return_value.join.return_value.filter.return_value.yield_per
        ).return_value = []

        @contextmanager
        def fake_session(*args, **kwargs):
            yield session

        return patch.object(mod, "get_user_db_session", fake_session)

    def test_no_rag_index_row_returns_zero(self):
        """A collection never indexed with this configuration has nothing
        to purge — and the deletion path must not create index records."""
        self._reset_locks()
        mod = _import_module()
        svc = _make_service()
        with self._make_session_patch(mod, None):
            assert svc.purge_document_vectors("doc-1", "coll-1") == 0

    def test_missing_index_file_returns_zero(self, tmp_path):
        self._reset_locks()
        mod = _import_module()
        svc = _make_service()
        rag_row = MagicMock(id=7, index_hash="deadbeef")
        svc._get_index_path = MagicMock(return_value=tmp_path / "nope.faiss")
        with self._make_session_patch(mod, rag_row):
            assert svc.purge_document_vectors("doc-1", "coll-1") == 0

    def test_unverified_index_skips_purge(self, tmp_path):
        """An index failing integrity verification has nothing safely
        purgeable; quarantine/rebuild decisions belong to indexing paths."""
        self._reset_locks()
        mod = _import_module()
        index_path = tmp_path / "idx.faiss"
        index_path.touch()

        svc = _make_service()
        svc.integrity_manager = MagicMock()
        svc.integrity_manager.verify_file.return_value = (False, "checksum")
        rag_row = MagicMock(id=7, index_hash="deadbeef")
        svc._get_index_path = MagicMock(return_value=index_path)

        with self._make_session_patch(mod, rag_row):
            assert svc.purge_document_vectors("note-1", "coll-1") == 0


class TestLazyEmbeddings:
    """_LazyEmbeddings defers the (expensive) embedding-backend construction
    until embed_* is actually called, so the delete/purge path — which never
    embeds — pays nothing for the model load."""

    def test_construction_does_not_touch_manager_embeddings(self):
        """Building the proxy must NOT access ``manager.embeddings`` (the lazy
        property that loads a SentenceTransformer / opens Ollama clients)."""
        mod = _import_module()

        class _Boom:
            @property
            def embeddings(self):
                raise AssertionError(
                    "manager.embeddings accessed at construction — the model "
                    "would load on a delete-only path"
                )

        # Constructing the proxy must not raise: it never touches .embeddings.
        proxy = mod._LazyEmbeddings(_Boom())
        assert proxy is not None

    def test_embed_calls_delegate_to_real_backend(self):
        mod = _import_module()
        backend = MagicMock()
        backend.embed_documents.return_value = [[0.1, 0.2]]
        backend.embed_query.return_value = [0.3, 0.4]

        accessed = {"count": 0}

        class _Manager:
            @property
            def embeddings(self):
                accessed["count"] += 1
                return backend

        proxy = mod._LazyEmbeddings(_Manager())
        # Still nothing accessed until we actually embed.
        assert accessed["count"] == 0

        assert proxy.embed_documents(["a"]) == [[0.1, 0.2]]
        assert proxy.embed_query("q") == [0.3, 0.4]
        backend.embed_documents.assert_called_once_with(["a"])
        backend.embed_query.assert_called_once_with("q")
        assert accessed["count"] == 2


# =========================================================================
# _migrate_legacy_index_files  (pre-per-user-scoping upgrade migration)
# =========================================================================
class TestMigrateLegacyIndexFiles:
    """The per-user path scoping must not strand indexes built under the
    old shared cache/rag_indices/ layout: load_or_create_faiss_index
    recomputes the path and ignores the stored one, so without the
    migration an upgrading user's semantic search silently returns
    nothing while the DB still claims everything is indexed."""

    @patch(f"{_MOD}.get_user_db_session")
    @patch(f"{_MOD}.get_cache_directory")
    def test_moves_legacy_files_and_updates_record(
        self, mock_cache_dir, mock_session_ctx, tmp_path
    ):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        legacy_root = tmp_path / "rag_indices"
        legacy_root.mkdir(parents=True, exist_ok=True)
        legacy = legacy_root / "hash1.faiss"
        legacy.write_bytes(b"faiss-bytes")
        (legacy_root / "hash1.pkl").write_bytes(b"pkl-bytes")

        index_path = svc._get_index_path("hash1")
        assert not index_path.exists()

        session = mock_session_ctx.return_value.__enter__.return_value
        db_row = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            db_row
        )

        rag_index = MagicMock(id=3, index_hash="hash1")
        svc._migrate_legacy_index_files(legacy, index_path, rag_index)

        # The .faiss is relocated (moved) into the per-user dir...
        assert index_path.read_bytes() == b"faiss-bytes"
        assert not legacy.exists()
        # ...but the legacy plaintext .pkl is DELETED, not relocated (the new
        # store writes no .pkl; moving it would just carry plaintext along).
        assert not index_path.with_suffix(".pkl").exists()
        assert not (legacy_root / "hash1.pkl").exists()
        # DB row repointed + integrity record refreshed for the new path.
        assert db_row.index_path == str(index_path)
        session.commit.assert_called()
        svc.integrity_manager.record_file.assert_called_once()
        rec_path = svc.integrity_manager.record_file.call_args[0][0]
        assert rec_path == index_path

    @patch(f"{_MOD}.get_user_db_session")
    @patch(f"{_MOD}.get_cache_directory")
    def test_refuses_stored_path_outside_legacy_root(
        self, mock_cache_dir, mock_session_ctx, tmp_path
    ):
        # Stored paths are not followed blindly — only files directly in
        # the known legacy shared dir are migrated.
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        elsewhere = tmp_path / "elsewhere"
        elsewhere.mkdir()
        legacy = elsewhere / "hash1.faiss"
        legacy.write_bytes(b"faiss-bytes")

        index_path = svc._get_index_path("hash1")
        svc._migrate_legacy_index_files(
            legacy, index_path, MagicMock(id=3, index_hash="hash1")
        )

        assert legacy.exists()
        assert not index_path.exists()
        mock_session_ctx.assert_not_called()

    @patch(f"{_MOD}.get_user_db_session")
    @patch(f"{_MOD}.get_cache_directory")
    def test_refuses_filename_mismatch(
        self, mock_cache_dir, mock_session_ctx, tmp_path
    ):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        legacy_root = tmp_path / "rag_indices"
        legacy_root.mkdir(parents=True, exist_ok=True)
        legacy = legacy_root / "otherhash.faiss"
        legacy.write_bytes(b"faiss-bytes")

        index_path = svc._get_index_path("hash1")
        svc._migrate_legacy_index_files(
            legacy, index_path, MagicMock(id=3, index_hash="hash1")
        )

        assert legacy.exists()
        assert not index_path.exists()
        mock_session_ctx.assert_not_called()

    @patch(f"{_MOD}.get_user_db_session")
    @patch(f"{_MOD}.get_cache_directory")
    def test_noop_when_legacy_file_missing(
        self, mock_cache_dir, mock_session_ctx, tmp_path
    ):
        mock_cache_dir.return_value = tmp_path
        svc = _make_service()
        legacy = tmp_path / "rag_indices" / "hash1.faiss"  # never created

        index_path = svc._get_index_path("hash1")
        svc._migrate_legacy_index_files(
            legacy, index_path, MagicMock(id=3, index_hash="hash1")
        )

        assert not index_path.exists()
        mock_session_ctx.assert_not_called()


# =========================================================================
# _reset_index_state_for_rebuild
# =========================================================================
class TestResetIndexStateForRebuild:
    """Reaching the fresh-build branch while DB rows still claim indexed
    content means the on-disk index was lost. Without the reset,
    index_document/index_collection skip everything (indexed=True), so
    re-indexing no-ops while search returns nothing."""

    def _model_aware_session(self, idx, status_count, membership_count):
        from local_deep_research.database.models.library import (
            DocumentCollection,
            RAGIndex,
            RagDocumentStatus,
        )

        rag_q = MagicMock()
        rag_q.filter_by.return_value.first.return_value = idx
        status_q = MagicMock()
        status_q.filter_by.return_value.count.return_value = status_count
        dc_q = MagicMock()
        dc_q.filter_by.return_value.count.return_value = membership_count

        session = MagicMock()
        session.query.side_effect = lambda model: {
            RAGIndex: rag_q,
            RagDocumentStatus: status_q,
            DocumentCollection: dc_q,
        }[model]
        return session, status_q, dc_q

    @patch(f"{_MOD}.get_user_db_session")
    def test_resets_state_when_row_claims_content(self, mock_session_ctx):
        svc = _make_service()
        idx = MagicMock(chunk_count=5, total_documents=2)
        session, status_q, dc_q = self._model_aware_session(idx, 3, 2)
        mock_session_ctx.return_value.__enter__.return_value = session

        svc._reset_index_state_for_rebuild("col-1", MagicMock(id=9))

        assert idx.chunk_count == 0
        assert idx.total_documents == 0
        status_q.filter_by.return_value.delete.assert_called_once()
        dc_q.filter_by.return_value.update.assert_called_once_with(
            {"indexed": False, "chunk_count": 0}
        )
        session.commit.assert_called_once()

    @patch(f"{_MOD}.get_user_db_session")
    def test_resets_when_only_memberships_stale(self, mock_session_ctx):
        # Covers the embedding-model-switch case: a brand-new RAGIndex row
        # (zero counts) but DocumentCollection rows still flagged indexed
        # from the previous index.
        svc = _make_service()
        idx = MagicMock(chunk_count=0, total_documents=0)
        session, status_q, dc_q = self._model_aware_session(idx, 0, 4)
        mock_session_ctx.return_value.__enter__.return_value = session

        svc._reset_index_state_for_rebuild("col-1", MagicMock(id=9))

        dc_q.filter_by.return_value.update.assert_called_once()
        session.commit.assert_called_once()

    @patch(f"{_MOD}.get_user_db_session")
    def test_noop_for_genuinely_new_index(self, mock_session_ctx):
        svc = _make_service()
        idx = MagicMock(chunk_count=0, total_documents=0)
        session, status_q, dc_q = self._model_aware_session(idx, 0, 0)
        mock_session_ctx.return_value.__enter__.return_value = session

        svc._reset_index_state_for_rebuild("col-1", MagicMock(id=9))

        status_q.filter_by.return_value.delete.assert_not_called()
        dc_q.filter_by.return_value.update.assert_not_called()
        session.commit.assert_not_called()


# =========================================================================
# _preflight_index_path — missing-file self-heal must not wipe healthy state
# =========================================================================
class TestPreflightSelfHeal:
    """When the new per-user index path is absent, the self-heal reset must fire
    ONLY when the bytes are genuinely gone — not when a transient one-time
    legacy-path migration failed and the bytes still sit at the stored path
    (which would wipe a healthy collection's indexed state, even on a search)."""

    def _rag_index(self, legacy):
        return MagicMock(
            index_hash="hash",
            index_path=str(legacy),
            chunk_count=5,
            total_documents=1,
        )

    def test_no_reset_when_legacy_bytes_survive(self, tmp_path):
        svc = _make_service()
        new_path = tmp_path / "user" / "hash.faiss"  # migration target, absent
        legacy = tmp_path / "hash.faiss"
        legacy.write_bytes(b"legacy-faiss")  # bytes survive at the old path
        svc._get_index_path = MagicMock(return_value=new_path)
        # Simulate a transient relocation failure: no-op, legacy stays put.
        svc._migrate_legacy_index_files = MagicMock()
        svc._reset_index_state_for_rebuild = MagicMock()

        # Read path (reset_stale_state=False) — must NOT reset.
        svc._preflight_index_path(
            "col-1", self._rag_index(legacy), reset_stale_state=False
        )
        svc._reset_index_state_for_rebuild.assert_not_called()

    def test_resets_when_index_gone_at_both_locations(self, tmp_path):
        svc = _make_service()
        new_path = tmp_path / "user" / "hash.faiss"  # absent
        legacy = tmp_path / "hash.faiss"  # ALSO absent (genuinely gone)
        svc._get_index_path = MagicMock(return_value=new_path)
        svc._migrate_legacy_index_files = MagicMock()
        svc._reset_index_state_for_rebuild = MagicMock()

        svc._preflight_index_path(
            "col-1", self._rag_index(legacy), reset_stale_state=False
        )
        svc._reset_index_state_for_rebuild.assert_called_once()


# =========================================================================
# _preflight_index_path — embedding-dimension-mismatch rebuild (7d9930e84
# regression family: a reintroduced partial reset must not resurface)
# =========================================================================
class TestPreflightDimensionMismatch:
    """When the stored ``embedding_dimension`` differs from what the current
    embedding model returns, the write path must delete the stale index AND
    fully reset DB state via ``_reset_index_state_for_rebuild`` (not just bump
    the dimension) — a partial reset leaves DocumentCollection.indexed=True,
    so index_document/index_collection skip everything and search returns []
    forever while get_rag_stats still reports 100% indexed.
    """

    def _make_present_index(self, tmp_path, stored_dim=384):
        index_path = tmp_path / "hash.faiss"
        index_path.write_bytes(b"faiss-bytes")
        rag_index = MagicMock(
            id=42,
            index_hash="hash",
            index_path=str(index_path),
            embedding_dimension=stored_dim,
            chunk_count=5,
            total_documents=1,
        )
        return index_path, rag_index

    @patch(f"{_MOD}.get_user_db_session")
    def test_preflight_dimension_mismatch_rebuilds_and_updates_dimension_on_write_path(
        self, mock_session_ctx, tmp_path
    ):
        svc = _make_service()
        svc._get_index_path = MagicMock(return_value=tmp_path / "hash.faiss")
        index_path, rag_index = self._make_present_index(
            tmp_path, stored_dim=384
        )

        svc.integrity_manager.verify_file.return_value = (True, None)
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.return_value = [0.1] * 768

        session = MagicMock()
        mock_session_ctx.return_value.__enter__.return_value = session
        db_row = MagicMock(embedding_dimension=384)
        session.query.return_value.filter_by.return_value.first.return_value = (
            db_row
        )

        svc._reset_index_state_for_rebuild = MagicMock()

        svc._preflight_index_path("col-1", rag_index, reset_stale_state=True)

        # Old bytes are gone.
        assert not index_path.exists()
        # DB-side dimension bumped to the current model dim.
        assert db_row.embedding_dimension == 768
        session.commit.assert_called()
        # Full state reset (not a hand-rolled partial one) fires. Note: since
        # unlink() actually removes the file, the immediately-following
        # missing-file self-heal block (index_path no longer exists) also
        # sees the passed-in rag_index's stale in-memory chunk_count/
        # total_documents (this mock isn't refreshed from the DB) and fires a
        # SECOND, harmless, idempotent reset call — a real DB-backed
        # RAGIndex row would already read back as zeroed by then, making
        # that second call a no-op. We assert it fired at least once with
        # the right args rather than pin the exact count.
        svc._reset_index_state_for_rebuild.assert_any_call("col-1", rag_index)
        # The write path is the ONLY path that probes the model dimension.
        svc.embedding_manager.embeddings.embed_query.assert_called_once_with(
            "dimension_check"
        )
        # In-memory record reflects the new dimension too.
        assert rag_index.embedding_dimension == 768

    @patch(f"{_MOD}.get_user_db_session")
    @patch(f"{_MOD}.VectorIndex")
    def test_search_skips_dimension_probe_on_read_path(
        self, mock_vector_index, mock_session_ctx, tmp_path
    ):
        # Performance: a read-only search() must NOT burn an embedding
        # round-trip probing the model dimension. A wrong-dimension query
        # is rejected downstream by the vector store's ``_prepare`` (see
        # FaissVectorStore), so the read path stays fail-closed without
        # the proactive probe. Only WRITE paths (reset_stale_state=True)
        # probe so they can rebuild on drift.
        svc = _make_service()
        svc._get_index_path = MagicMock(return_value=tmp_path / "hash.faiss")
        index_path, rag_index = self._make_present_index(
            tmp_path, stored_dim=384
        )

        # The real _preflight_index_path still runs through search() ->
        # _get_vector_index(reset_stale_state=False); only
        # _get_or_create_rag_index (DB) and the VectorIndex facade are
        # short-circuited, so this asserts ONLY the preflight probe behaviour.
        svc._get_or_create_rag_index = MagicMock(return_value=rag_index)
        svc.integrity_manager.verify_file.return_value = (True, None)
        svc.embedding_manager = MagicMock()
        # If preflight probed, this 768-vector would mismatch the stored
        # 384 and raise on the read path (the old behaviour).
        svc.embedding_manager.embeddings.embed_query.return_value = [0.1] * 768

        # The patched facade stands in for the real VectorIndex: its search
        # returns [] and never embeds (the real downstream query embedding
        # is the facade's job, not preflight's), so any call to the backend
        # embed_query mock below is attributable ONLY to the preflight probe.
        mock_vector_index.return_value.search.return_value = []

        svc._reset_index_state_for_rebuild = MagicMock()

        # search() leaves reset_stale_state at its default False -> read path.
        result = svc.search("query", "col-1", 3)

        # The facade delegated the query embedding downstream; preflight did NOT.
        assert result == []
        mock_vector_index.return_value.search.assert_called_once_with(
            "query", 3
        )
        svc.embedding_manager.embeddings.embed_query.assert_not_called()
        # A read must not destroy a healthy collection or wipe DB state.
        assert index_path.exists()
        assert rag_index.embedding_dimension == 384
        mock_session_ctx.assert_not_called()
        svc._reset_index_state_for_rebuild.assert_not_called()

    @patch(f"{_MOD}.get_user_db_session")
    def test_preflight_embed_provider_failure_propagates_without_quarantine_or_rebuild(
        self, mock_session_ctx, tmp_path
    ):
        svc = _make_service()
        svc._get_index_path = MagicMock(return_value=tmp_path / "hash.faiss")
        index_path, rag_index = self._make_present_index(
            tmp_path, stored_dim=384
        )

        svc.integrity_manager.verify_file.return_value = (True, None)
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.side_effect = RuntimeError(
            "connection refused"
        )

        svc._reset_index_state_for_rebuild = MagicMock()

        with pytest.raises(RuntimeError, match="connection refused"):
            svc._preflight_index_path(
                "col-1", rag_index, reset_stale_state=True
            )

        # The probe failure must NOT be mistaken for a dimension mismatch:
        # no unlink, no DB dimension bump, no state reset.
        assert index_path.exists()
        assert rag_index.embedding_dimension == 384
        mock_session_ctx.assert_not_called()
        svc._reset_index_state_for_rebuild.assert_not_called()

    @patch(f"{_MOD}.get_user_db_session")
    def test_preflight_dimension_mismatch_removes_manifest_despite_foreign_segment_file(
        self, mock_session_ctx, tmp_path
    ):
        """A refused foreign file must not leave a manifest pointing at
        deleted segments while the DB row is reset."""
        from local_deep_research.vector_stores.segment_files import (
            segment_directory,
        )

        svc = _make_service()
        svc._get_index_path = MagicMock(return_value=tmp_path / "hash.faiss")
        index_path, rag_index = self._make_present_index(
            tmp_path, stored_dim=384
        )
        directory = segment_directory(index_path)
        directory.mkdir()
        segment = directory / ("a" * 32 + ".faiss")
        segment.write_bytes(b"vectors")
        (directory / ".DS_Store").write_bytes(b"foreign")

        svc.integrity_manager.verify_file.return_value = (True, None)
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.return_value = [0.1] * 768

        session = MagicMock()
        mock_session_ctx.return_value.__enter__.return_value = session
        db_row = MagicMock(embedding_dimension=384)
        session.query.return_value.filter_by.return_value.first.return_value = (
            db_row
        )

        svc._reset_index_state_for_rebuild = MagicMock()

        svc._preflight_index_path("col-1", rag_index, reset_stale_state=True)

        assert not segment.exists()
        assert not index_path.exists()
        assert db_row.embedding_dimension == 768

    @patch(f"{_MOD}.get_user_db_session")
    def test_preflight_dimension_mismatch_continues_rebuild_when_unlink_fails(
        self, mock_session_ctx, tmp_path
    ):
        svc = _make_service()
        svc._get_index_path = MagicMock(return_value=tmp_path / "hash.faiss")
        index_path, rag_index = self._make_present_index(
            tmp_path, stored_dim=384
        )

        svc.integrity_manager.verify_file.return_value = (True, None)
        svc.embedding_manager = MagicMock()
        svc.embedding_manager.embeddings.embed_query.return_value = [0.1] * 768

        session = MagicMock()
        mock_session_ctx.return_value.__enter__.return_value = session
        db_row = MagicMock(embedding_dimension=384)
        session.query.return_value.filter_by.return_value.first.return_value = (
            db_row
        )

        svc._reset_index_state_for_rebuild = MagicMock()

        with patch.object(Path, "unlink", side_effect=OSError("disk full")):
            svc._preflight_index_path(
                "col-1", rag_index, reset_stale_state=True
            )

        # The swallowed unlink failure must not abort the DB-side reset.
        assert db_row.embedding_dimension == 768
        svc._reset_index_state_for_rebuild.assert_called_once_with(
            "col-1", rag_index
        )
        assert rag_index.embedding_dimension == 768