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.

4.2 KB
Raw Blame
"""Cache ownership and bounded waiter failure contracts."""

import threading
from unittest.mock import Mock
from uuid import uuid4

import pytest

from local_deep_research.search_system import AdvancedSearchSystem
from local_deep_research.utilities.research_content_cache import (
    ResearchContentCache,
    bind_cache_to_research,
    get_cache_for_research,
    unbind_cache_from_research,
)


@pytest.mark.parametrize("already_bound", [False, True])
def test_failed_system_construction_preserves_registry(
    monkeypatch, already_bound
):
    rid = str(uuid4())
    prior_cache = ResearchContentCache() if already_bound else None
    if prior_cache is not None:
        bind_cache_to_research(rid, prior_cache)

    def fail_strategy(**kwargs):
        raise RuntimeError("strategy initialization failed")

    monkeypatch.setattr(
        "local_deep_research.search_system_factory.create_strategy",
        fail_strategy,
    )
    try:
        with pytest.raises(
            RuntimeError, match="strategy initialization failed"
        ):
            AdvancedSearchSystem(
                llm=Mock(),
                search=Mock(),
                strategy_name="source-based",
                settings_snapshot={},
                research_id=rid,
            )
        assert get_cache_for_research(rid) is prior_cache
    finally:
        unbind_cache_from_research(rid)


def test_waiter_timeout_preserves_owner_and_its_later_result():
    cache = ResearchContentCache()
    started = threading.Event()
    release = threading.Event()

    def owner_fetch():
        started.set()
        assert release.wait(5)
        return {"content": "owner result"}

    thread = threading.Thread(
        target=lambda: cache.get_or_fetch("https://example.com/p", owner_fetch)
    )
    thread.start()
    try:
        assert started.wait(2)
        with pytest.raises(TimeoutError, match="Timed out waiting"):
            cache.get_or_fetch(
                "https://example.com/p",
                lambda: {"content": "fallback"},
                wait_timeout=0,
            )
    finally:
        release.set()
        thread.join(2)
        assert not thread.is_alive()

    fallback = Mock(side_effect=AssertionError("owner result was lost"))
    assert (
        cache.get_or_fetch("https://example.com/p", fallback)["content"]
        == "owner result"
    )
    fallback.assert_not_called()


def test_closing_older_system_preserves_replacement_binding():
    rid = str(uuid4())
    first = AdvancedSearchSystem(
        llm=Mock(),
        search=Mock(),
        strategy_name="source-based",
        settings_snapshot={},
        research_id=rid,
    )
    second = None
    try:
        second = AdvancedSearchSystem(
            llm=Mock(),
            search=Mock(),
            strategy_name="source-based",
            settings_snapshot={},
            research_id=rid,
        )
        first.close()
        assert get_cache_for_research(rid) is second.content_cache
    finally:
        first.close()
        if second is not None:
            second.close()
        unbind_cache_from_research(rid)
    assert get_cache_for_research(rid) is None


@pytest.mark.parametrize("primary", [" arxiv ", {"value": " arxiv "}])
def test_cache_uses_canonical_run_primary(primary):
    engine = _cache_engine({"search.tool": primary})
    assert engine._get_cache_egress_context().primary_engine == "arxiv"


@pytest.mark.parametrize("operator_scope", [False, True])
def test_strict_cache_without_saved_primary_refuses_web_hit(
    monkeypatch, operator_scope
):
    snapshot = {"policy.egress_scope": "strict"}
    if operator_scope:
        monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "strict")
        snapshot = {}
    engine = _cache_engine(snapshot)
    assert engine._get_cache_egress_context() is None
    assert engine._is_cache_url_allowed("http://93.184.216.34/article") is False


def _cache_engine(snapshot):
    from local_deep_research.web_search_engines.search_engine_base import (
        BaseSearchEngine,
    )

    class Engine(BaseSearchEngine):
        def _get_previews(self, query):
            return []

    engine = Engine(settings_snapshot=snapshot, programmatic_mode=True)
    engine._engine_name = "arxiv"
    return engine