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.

8.7 KB
Raw Blame
"""Real local retrieval contracts: snapshots must preserve complete chunks."""

import hashlib
import threading

import pytest
from langchain_core.embeddings import Embeddings
from langchain_core.language_models.fake import FakeListLLM
from sqlalchemy import create_engine, event, select
from sqlalchemy.orm import Session

from local_deep_research.advanced_search_system.strategies.source_based_strategy import (
    SourceBasedSearchStrategy,
)
from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentChunk,
    EmbeddingProvider,
    SourceType,
)
from local_deep_research.database.thread_local_session import (
    thread_session_manager,
)
from local_deep_research.mcp.server import _execute_search
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)
from local_deep_research.settings.manager import SettingsManager
from local_deep_research.utilities.thread_context import (
    clear_search_context,
    get_search_context,
    set_search_context,
)
from local_deep_research.web_search_engines.search_engine_factory import (
    create_search_engine,
    get_search,
)
from local_deep_research.web_search_engines.search_engines_config import (
    invalidate_collection_engines_cache,
)

from .test_search_settings_contracts import snapshot as snapshot


USERNAME = "settings-contract-user"
COLLECTION_ID = "22222222-2222-4222-8222-222222222222"
DOCUMENT_ID = "11111111-1111-4111-8111-111111111111"
CHUNK_TEXT = "Detailed local research evidence. " * 40 + "END-OF-COMPLETE-CHUNK"
pytestmark = pytest.mark.usefixtures("loguru_caplog")


class OfflineEmbeddings(Embeddings):
    """Replace the model boundary; FAISS and DB chunk hydration stay real."""

    def embed_documents(self, texts):
        return [[0.2, 0.4, 0.8] for _ in texts]

    def embed_query(self, text):
        return [0.2, 0.4, 0.8]


@pytest.fixture
def indexed_library(tmp_path, monkeypatch):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path / "data"))
    engine = create_engine(f"sqlite:///{tmp_path / 'library.db'}")

    @event.listens_for(engine, "connect")
    def sqlite_transactions(connection, record):
        connection.isolation_level = None
        connection.execute("PRAGMA journal_mode=WAL")

    @event.listens_for(engine, "begin")
    def begin(connection):
        connection.exec_driver_sql("BEGIN")

    Base.metadata.create_all(engine)
    per_thread = threading.local()
    sessions = []

    def discover_session(username, password):
        assert username == USERNAME
        if not hasattr(per_thread, "session"):
            per_thread.session = Session(engine)
            sessions.append(per_thread.session)
        return per_thread.session

    # Retain the production scope-depth, nested transaction and cleanup
    # behavior. Only encrypted-engine discovery points at isolated SQLite.
    monkeypatch.setattr(
        "local_deep_research.database.thread_local_session.get_metrics_session",
        discover_session,
    )
    monkeypatch.setattr(
        "local_deep_research.embeddings.get_embeddings",
        lambda **kwargs: OfflineEmbeddings(),
    )
    previous_context = get_search_context()
    set_search_context(
        {"username": USERNAME, "user_password": "contract-password"}
    )
    invalidate_collection_engines_cache(USERNAME)

    try:
        with Session(engine) as session:
            settings = SettingsManager(session)
            for key, value in {
                "policy.egress_scope": "private_only",
                "search.tool": "library",
                "local_search_embedding_provider": "ollama",
                "local_search_embedding_model": "contract-offline-model",
            }.items():
                assert settings.set_setting(key, value)
            session.add_all(
                [
                    SourceType(
                        id="upload", name="user_upload", display_name="Upload"
                    ),
                    Collection(
                        id=COLLECTION_ID,
                        name="Contract evidence",
                        is_public=False,
                        embedding_model="contract-offline-model",
                        embedding_model_type=EmbeddingProvider.OLLAMA,
                        chunk_size=10000,
                        chunk_overlap=0,
                    ),
                    Document(
                        id=DOCUMENT_ID,
                        source_type_id="upload",
                        title="Complete evidence",
                        filename="evidence.txt",
                        text_content=CHUNK_TEXT,
                        file_size=len(CHUNK_TEXT.encode()),
                        file_type="txt",
                        storage_mode="none",
                        document_hash=hashlib.sha256(
                            CHUNK_TEXT.encode()
                        ).hexdigest(),
                    ),
                ]
            )
            session.commit()

        with LibraryRAGService(
            username=USERNAME,
            db_password="contract-password",
            embedding_model="contract-offline-model",
            embedding_provider="ollama",
            chunk_size=10000,
            chunk_overlap=0,
        ) as service:
            result = service.index_document(DOCUMENT_ID, COLLECTION_ID)
            assert result["status"] == "success", result
        with Session(engine) as session:
            chunks = session.scalars(select(DocumentChunk)).all()
            assert len(chunks) == 1
            assert chunks[0].chunk_text == CHUNK_TEXT
        yield engine
    finally:
        if previous_context is None:
            clear_search_context()
        else:
            set_search_context(previous_context)
        invalidate_collection_engines_cache(USERNAME)
        thread_session_manager.cleanup_thread()
        for session in sessions:
            session.close()
        engine.dispose()


def _local_snapshot(snapshot, name, snippets_only):
    settings = snapshot(
        name,
        **{
            "policy.egress_scope": "private_only",
            "search.engine.library.enabled": True,
            "search.snippets_only": snippets_only,
            "search.rag.enable_relevance_filter": False,
            "local_search_embedding_provider": "ollama",
            "local_search_embedding_model": "contract-offline-model",
            "local_search_chunk_size": 10000,
            "local_search_chunk_overlap": 0,
        },
    )
    settings["_username"] = USERNAME
    return settings


def _assert_chunk(results, snippets_only):
    assert len(results) == 1
    assert results[0]["snippet"] != CHUNK_TEXT
    assert "END-OF-COMPLETE-CHUNK" not in results[0]["snippet"]
    if snippets_only:
        assert "full_content" not in results[0]
    else:
        assert results[0]["full_content"] == CHUNK_TEXT


@pytest.mark.parametrize("name", ["library", f"collection_{COLLECTION_ID}"])
@pytest.mark.parametrize("snippets_only", [False, True])
@pytest.mark.parametrize("caller", ["get_search", "mcp"])
def test_local_snapshot_controls_real_chunk_hydration(
    indexed_library, snapshot, name, snippets_only, caller
):
    settings = _local_snapshot(snapshot, name, snippets_only)
    if caller == "mcp":
        result = _execute_search("local research", name, 3, settings)
        assert result["status"] == "success", result
        results = result["results"]
    else:
        engine = get_search(
            name, None, settings_snapshot=settings, programmatic_mode=True
        )
        assert engine is not None
        try:
            results = engine.run("local research")
        finally:
            engine.close()
    _assert_chunk(results, snippets_only)


@pytest.mark.parametrize("include_text_content", [False, True])
def test_strategy_changes_the_branch_used_by_real_engine_execution(
    indexed_library, snapshot, include_text_content
):
    settings = _local_snapshot(snapshot, "library", include_text_content)
    engine = create_search_engine(
        "library", settings_snapshot=settings, programmatic_mode=True
    )
    assert engine is not None
    try:
        strategy = SourceBasedSearchStrategy(
            search=engine,
            model=FakeListLLM(responses=["Unused model response."]),
            include_text_content=include_text_content,
            settings_snapshot=settings,
        )
        # Before strategy construction the engine has the opposite mode.
        # Updating only include_full_content (the old dead attribute) must
        # fail on the actual BaseSearchEngine.run() branch and chunk output.
        results = strategy.search.run("local research")
        _assert_chunk(results, not include_text_content)
    finally:
        engine.close()