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.

11.9 KB
Raw Blame
"""Failure-path coverage for phase-2 legacy index re-keying.

These tests isolate retries and quarantine decisions so transient failures never
turn into destructive reindexing or a completion marker hiding plaintext.
"""

import threading
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock

from pytest_mock import MockerFixture

import pytest

import local_deep_research.vector_stores.legacy_rekey as rekey


@contextmanager
def _session_context(session: MagicMock) -> Iterator[MagicMock]:
    yield session


def _one_index_harness(
    mocker: MockerFixture, tmp_path: Path, *, sidecar: bool = True
) -> tuple[Path, Path, MagicMock, MagicMock]:
    faiss_path = tmp_path / "legacy.faiss"
    sidecar_path = tmp_path / "legacy.idmap.json"
    if sidecar:
        sidecar_path.write_text('{"0": "a"}', encoding="utf-8")

    rag_index = MagicMock()
    rag_index.id = 17
    rag_index.index_path = str(faiss_path)
    rag_index.collection_name = "collection_edge"
    rag_index.embedding_dimension = 2
    rag_index.index_type = "flat"
    rag_index.distance_metric = "l2"
    rag_index.normalize_vectors = False

    settings = MagicMock()
    settings.get_bool_setting.return_value = False
    session = MagicMock()
    session.query.return_value.all.return_value = [rag_index]
    mocker.patch.object(
        rekey,
        "get_user_db_session",
        # One fresh context manager per call: the orchestrator opens a
        # second session when persisting rekeyed config (legacy_rekey.py:146).
        side_effect=lambda *args, **kwargs: _session_context(session),
    )
    mocker.patch.object(rekey, "get_settings_manager", return_value=settings)
    mocker.patch(
        "local_deep_research.research_library.services.library_rag_service."
        "_get_faiss_write_lock",
        # Head returns a _TrackedRLock (an RLock wrapper); RLock() here
        # matches its reentrancy instead of a plain, non-reentrant Lock().
        return_value=threading.RLock(),
    )
    return faiss_path, sidecar_path, rag_index, settings


def test_persist_rekeyed_config_ignores_deleted_rag_index(
    mocker: MockerFixture,
) -> None:
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = None
    mocker.patch.object(
        rekey,
        "get_user_db_session",
        return_value=_session_context(session),
    )

    rekey._persist_rekeyed_config("alice", "password", 12, {})

    # Pin the query path the guard depends on, not just its outcome.
    session.query.return_value.filter_by.assert_called_once_with(id=12)
    session.commit.assert_not_called()


def test_persist_rekeyed_config_skips_commit_and_preserves_fields_when_unchanged(
    mocker: MockerFixture,
) -> None:
    """Pins the `if changed:` gate (legacy_rekey.py:160-161) independently of
    the `row is None` guard above: the row IS found, but every column is
    already non-NULL, so `changed` never flips True. A revert that commits
    unconditionally would only be caught here, and a revert that stopped
    checking each field individually before assigning would be caught by the
    sentinel values below staying untouched (never override an explicit
    stored value, per _persist_rekeyed_config's docstring)."""
    row = MagicMock()
    row.index_type = "flat"
    row.distance_metric = "l2"
    row.normalize_vectors = False
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = row
    mocker.patch.object(
        rekey,
        "get_user_db_session",
        return_value=_session_context(session),
    )

    rekey._persist_rekeyed_config(
        "alice",
        "password",
        12,
        {"index_type": "hnsw", "metric": "cosine", "normalize": True},
    )

    session.commit.assert_not_called()
    assert row.index_type == "flat"
    assert row.distance_metric == "l2"
    assert row.normalize_vectors is False


def test_purge_redundant_pkl_returns_false_when_unlink_fails(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    faiss_path = tmp_path / "legacy.faiss"
    pkl_path = faiss_path.with_suffix(".pkl")
    pkl_path.write_bytes(b"plaintext")

    def _fail_unlink(path, *args, **kwargs):
        if path == pkl_path:
            raise OSError("locked")
        return original_unlink(path, *args, **kwargs)

    original_unlink = Path.unlink
    monkeypatch.setattr(Path, "unlink", _fail_unlink)

    assert rekey._purge_redundant_pkl(faiss_path, "password") is False
    assert pkl_path.exists()


def test_quarantine_renames_pair_and_deletes_plaintext(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path = tmp_path / "legacy.faiss"
    sidecar_path = tmp_path / "legacy.idmap.json"
    pkl_path = tmp_path / "legacy.pkl"
    faiss_path.write_bytes(b"index")
    sidecar_path.write_text("{}", encoding="utf-8")
    pkl_path.write_bytes(b"plaintext")
    rag_index = MagicMock(id=7, collection_name="collection_edge")
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        MagicMock(is_current=False)
    )
    mocker.patch.object(
        rekey,
        "get_user_db_session",
        return_value=_session_context(session),
    )

    rekey._quarantine_and_reset(
        "alice",
        "password",
        rag_index,
        faiss_path,
        sidecar_path,
        threading.Lock(),
    )

    assert not faiss_path.exists()
    assert not sidecar_path.exists()
    assert not pkl_path.exists()
    assert len(list(tmp_path.glob("*.corrupt-*"))) == 2


def test_old_format_without_sidecar_quarantines_when_phase_one_gave_up(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, _sidecar, _row, settings = _one_index_harness(
        mocker, tmp_path, sidecar=False
    )
    faiss_path.write_bytes(b"old index")
    mocker.patch.object(rekey, "_is_idmap2", return_value=False)
    quarantine = mocker.patch.object(rekey, "_quarantine_and_reset")

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["quarantined"] == 1
    quarantine.assert_called_once()
    settings.set_setting.assert_called_once_with(
        rekey.REKEY_MARKER_SETTING, True
    )


def test_failed_quarantine_withholds_marker_for_old_format_orphan(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, _sidecar, _row, settings = _one_index_harness(
        mocker, tmp_path, sidecar=False
    )
    faiss_path.write_bytes(b"old index")
    mocker.patch.object(rekey, "_is_idmap2", return_value=False)
    mocker.patch.object(
        rekey, "_quarantine_and_reset", side_effect=OSError("full")
    )

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["quarantined"] == 0
    settings.set_setting.assert_not_called()


def test_mixed_batch_one_unresolved_index_withholds_marker_for_whole_batch(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    """`all_resolved` (legacy_rekey.py:342) is an accumulator over the WHOLE
    per-user sweep, not a per-index flag: it is initialised once before the
    `for rag_index in indexes:` loop and only ever flipped False, so one
    unresolved index withholds the marker even when a LATER index in the
    same call resolves cleanly. A revert that re-initialises
    `all_resolved = True` inside the loop body (resetting it every
    iteration) would let this second, successful index silently erase the
    first index's failure and set the marker anyway -- caught below by
    `settings.set_setting.assert_not_called()`.
    """
    unresolved_path = tmp_path / "legacy_unresolved.faiss"
    unresolved_path.write_bytes(b"old index")
    resolved_path = tmp_path / "legacy_resolved.faiss"
    resolved_path.write_bytes(b"old index")

    unresolved_row = MagicMock(id=1, index_path=str(unresolved_path))
    resolved_row = MagicMock(id=2, index_path=str(resolved_path))

    settings = MagicMock()
    settings.get_bool_setting.return_value = False
    session = MagicMock()
    session.query.return_value.all.return_value = [
        unresolved_row,
        resolved_row,
    ]
    mocker.patch.object(
        rekey,
        "get_user_db_session",
        side_effect=lambda *args, **kwargs: _session_context(session),
    )
    mocker.patch.object(rekey, "get_settings_manager", return_value=settings)
    mocker.patch(
        "local_deep_research.research_library.services.library_rag_service."
        "_get_faiss_write_lock",
        return_value=threading.RLock(),
    )
    mocker.patch.object(rekey, "_is_idmap2", return_value=False)
    quarantine = mocker.patch.object(
        rekey,
        "_quarantine_and_reset",
        # First index (unresolved): quarantine itself fails. Second index
        # (resolved): quarantine succeeds. Order matters -- the failure must
        # come first so a per-iteration reset would be masked by the later
        # success.
        side_effect=[OSError("full"), None],
    )

    stats = rekey.rekey_user_indexes("alice", "password")

    assert quarantine.call_count == 2
    assert stats["quarantined"] == 1
    settings.set_setting.assert_not_called()


def test_orphan_sidecar_unlink_failure_withholds_marker(
    tmp_path: Path, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
    _faiss, sidecar, _row, settings = _one_index_harness(mocker, tmp_path)
    mocker.patch.object(rekey, "_purge_redundant_pkl", return_value=True)

    original_unlink = Path.unlink

    def _fail_sidecar_unlink(path, *args, **kwargs):
        if path == sidecar:
            raise OSError("locked")
        return original_unlink(path, *args, **kwargs)

    monkeypatch.setattr(Path, "unlink", _fail_sidecar_unlink)

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["skipped_no_faiss"] == 1
    assert sidecar.exists()
    settings.set_setting.assert_not_called()


@pytest.mark.parametrize("failure_site", ["persist", "resolve"])
def test_transient_pre_rekey_failures_do_not_quarantine(
    tmp_path: Path, mocker: MockerFixture, failure_site: str
) -> None:
    faiss_path, _sidecar, _row, settings = _one_index_harness(mocker, tmp_path)
    faiss_path.write_bytes(b"old index")
    quarantine = mocker.patch.object(rekey, "_quarantine_and_reset")
    if failure_site == "persist":
        mocker.patch.object(
            rekey, "_persist_rekeyed_config", side_effect=OSError("db")
        )
    else:
        mocker.patch.object(
            rekey, "_load_uuid_to_id", side_effect=OSError("db")
        )

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["quarantined"] == 0
    quarantine.assert_not_called()
    settings.set_setting.assert_not_called()


def test_rekey_failure_with_failed_quarantine_withholds_marker(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, _sidecar, _row, settings = _one_index_harness(mocker, tmp_path)
    faiss_path.write_bytes(b"old index")
    mocker.patch.object(rekey, "_load_uuid_to_id", return_value={"a": 1})
    mocker.patch.object(
        rekey, "rekey_index_file", side_effect=ValueError("corrupt")
    )
    mocker.patch.object(
        rekey, "_quarantine_and_reset", side_effect=OSError("full")
    )

    stats = rekey.rekey_user_indexes("alice", "password")

    assert stats["quarantined"] == 0
    settings.set_setting.assert_not_called()


def test_finalization_failure_leaves_sidecar_and_withholds_marker(
    tmp_path: Path, mocker: MockerFixture
) -> None:
    faiss_path, sidecar, _row, settings = _one_index_harness(mocker, tmp_path)
    faiss_path.write_bytes(b"old index")
    mocker.patch.object(rekey, "_load_uuid_to_id", return_value={"a": 1})
    mocker.patch.object(rekey, "rekey_index_file")
    manager = MagicMock()
    manager.record_file.side_effect = OSError("integrity database unavailable")
    mocker.patch.object(rekey, "FileIntegrityManager", return_value=manager)

    stats = rekey.rekey_user_indexes("alice", "password")

    manager.record_file.assert_called_once()
    assert stats["rekeyed"] == 0
    assert sidecar.exists()
    settings.set_setting.assert_not_called()