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.

9.7 KB
Raw Blame
"""Cache key provenance hardening for the shared research content cache.

Library keys (``/library/document/...``) in the run-wide cache are served
as the user's own library documents, so only the code paths that read the
user's library database may populate them:

* engines other than the library / collection engines never write
  library-keyed entries;
* library-keyed entries are served only when tagged with library
  provenance (the resolver and the engine fill path both check);
* the resolver checks for a user before it consults the cache.

Also pins the in-flight claim release in ``get_or_fetch``: after
``clear()``, a finishing owner must not drop a newer owner's claim.
"""

from __future__ import annotations

import threading
from unittest.mock import MagicMock, patch

from local_deep_research.utilities.research_content_cache import (
    LIBRARY_PROVENANCE,
    ResearchContentCache,
    content_cache_key,
    is_library_cache_key,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)

DOC_ID = "123e4567-e89b-12d3-a456-426614174000"
DOC_URL = f"/library/document/{DOC_ID}"
DB_TEXT = "text read from the user's library database"
OTHER_TEXT = "text supplied by a non-library engine " * 20


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

    def _get_full_content(self, items):
        return items


class _LibraryEngine(_NonLibraryEngine):
    writes_library_cache_keys = True


def _complete_item(link, text):
    return {
        "link": link,
        "title": "Some title",
        "full_content": text,
        "full_content_is_complete": True,
    }


def _patched_db(text=DB_TEXT):
    """Patch the user DB session so the resolver reads *text*."""
    from local_deep_research.database.models.library import Document

    doc = MagicMock()
    doc.id = DOC_ID
    doc.title = "Library title"
    doc.text_content = text

    session = MagicMock()

    def query_mock(model):
        q = MagicMock()
        if model == Document:
            q.filter_by.return_value.first.return_value = doc
        return q

    session.query.side_effect = query_mock
    session_cm = MagicMock()
    session_cm.__enter__.return_value = session
    session_cm.__exit__.return_value = False
    return session, patch(
        "local_deep_research.database.session_context.get_user_db_session",
        return_value=session_cm,
    )


class TestEngineWriteProvenance:
    def test_library_url_maps_to_library_key(self):
        assert is_library_cache_key(DOC_URL) is True
        assert is_library_cache_key("https://example.com/page") is False

    def test_only_library_engines_may_write_library_keys(self):
        from local_deep_research.web_search_engines.engines.search_engine_collection import (
            CollectionSearchEngine,
        )
        from local_deep_research.web_search_engines.engines.search_engine_library import (
            LibraryRAGSearchEngine,
        )

        assert BaseSearchEngine.writes_library_cache_keys is False
        assert LibraryRAGSearchEngine.writes_library_cache_keys is True
        assert CollectionSearchEngine.writes_library_cache_keys is True

    def test_non_library_engine_does_not_write_library_key(self):
        cache = ResearchContentCache()
        engine = _NonLibraryEngine(content_cache=cache)
        engine._store_full_content_in_cache(
            [_complete_item(DOC_URL, OTHER_TEXT)]
        )
        assert cache.get(DOC_URL) is None

    def test_non_library_engine_still_writes_web_keys(self):
        cache = ResearchContentCache()
        engine = _NonLibraryEngine(content_cache=cache)
        engine._is_cache_url_allowed = lambda link: True
        engine._store_full_content_in_cache(
            [_complete_item("https://example.com/page", "web body")]
        )
        hit = cache.get("https://example.com/page")
        assert hit is not None and hit["content"] == "web body"
        assert "provenance" not in hit

    def test_library_engine_writes_tagged_entry(self):
        cache = ResearchContentCache()
        engine = _LibraryEngine(content_cache=cache)
        engine._store_full_content_in_cache([_complete_item(DOC_URL, DB_TEXT)])
        hit = cache.get(DOC_URL)
        assert hit is not None
        assert hit["content"] == DB_TEXT
        assert hit["provenance"] == LIBRARY_PROVENANCE

    def test_non_library_result_not_served_as_library_document(self):
        """End to end: a non-library engine result carrying a library link
        must not become what the resolver returns for that document."""
        from local_deep_research.advanced_search_system.tools.fetch import (
            library_resolver as lr,
        )

        cache = ResearchContentCache()
        engine = _NonLibraryEngine(content_cache=cache)
        engine._store_full_content_in_cache(
            [_complete_item(DOC_URL, OTHER_TEXT)]
        )
        session, db_patch = _patched_db()
        with db_patch:
            result = lr.resolve_library_document(
                DOC_URL, "someone", content_cache=cache
            )
        assert result is not None
        assert result["content"] == DB_TEXT
        assert session.query.call_count > 0

    def test_engine_fill_ignores_untagged_library_entry(self):
        cache = ResearchContentCache()
        cache.put(DOC_URL, OTHER_TEXT, title="T", url_out=DOC_URL)
        engine = _LibraryEngine(content_cache=cache)
        items = [{"link": DOC_URL, "title": "T"}]
        cached, missing = engine._fill_full_content_from_cache(items)
        assert cached == []
        assert missing == items
        assert "full_content" not in items[0]


class TestCachePutProvenance:
    def test_untagged_write_does_not_replace_library_entry(self):
        cache = ResearchContentCache()
        cache.put(DOC_URL, DB_TEXT, provenance=LIBRARY_PROVENANCE)
        cache.put(DOC_URL, OTHER_TEXT)  # longer, but untagged
        hit = cache.get(DOC_URL)
        assert hit["content"] == DB_TEXT
        assert hit["provenance"] == LIBRARY_PROVENANCE

    def test_library_write_replaces_untagged_entry(self):
        cache = ResearchContentCache()
        cache.put(DOC_URL, OTHER_TEXT)
        cache.put(DOC_URL, "short", provenance=LIBRARY_PROVENANCE)
        hit = cache.get(DOC_URL)
        assert hit["content"] == "short"
        assert hit["provenance"] == LIBRARY_PROVENANCE

    def test_longest_wins_within_same_provenance(self):
        cache = ResearchContentCache()
        cache.put(DOC_URL, "longer text", provenance=LIBRARY_PROVENANCE)
        cache.put(DOC_URL, "short", provenance=LIBRARY_PROVENANCE)
        assert cache.get(DOC_URL)["content"] == "longer text"


class TestResolverProvenance:
    def test_no_username_never_serves_cache(self):
        from local_deep_research.advanced_search_system.tools.fetch import (
            library_resolver as lr,
        )

        cache = ResearchContentCache()
        cache.put(
            DOC_URL,
            DB_TEXT,
            title="T",
            url_out=DOC_URL,
            provenance=LIBRARY_PROVENANCE,
        )
        assert (
            lr.resolve_library_document(DOC_URL, None, content_cache=cache)
            is None
        )
        assert (
            lr.resolve_library_document(DOC_URL, "", content_cache=cache)
            is None
        )

    def test_untagged_entry_falls_through_to_db(self):
        from local_deep_research.advanced_search_system.tools.fetch import (
            library_resolver as lr,
        )

        cache = ResearchContentCache()
        cache.put(DOC_URL, OTHER_TEXT, title="T", url_out=DOC_URL)
        session, db_patch = _patched_db()
        with db_patch:
            result = lr.resolve_library_document(
                DOC_URL, "someone", content_cache=cache
            )
        assert result["content"] == DB_TEXT
        assert session.query.call_count > 0
        # The DB read replaces the untagged entry with a tagged one.
        hit = cache.get(DOC_URL)
        assert hit["content"] == DB_TEXT
        assert hit["provenance"] == LIBRARY_PROVENANCE

    def test_tagged_entry_served_without_db(self):
        from local_deep_research.advanced_search_system.tools.fetch import (
            library_resolver as lr,
        )

        cache = ResearchContentCache()
        cache.put(
            DOC_URL,
            DB_TEXT,
            title="T",
            url_out=DOC_URL,
            provenance=LIBRARY_PROVENANCE,
        )
        session, db_patch = _patched_db(text="unused")
        with db_patch:
            result = lr.resolve_library_document(
                DOC_URL, "someone", content_cache=cache
            )
        assert result["content"] == DB_TEXT
        assert session.query.call_count == 0


class TestInflightReleaseAfterClear:
    def test_finishing_owner_keeps_newer_claim(self):
        """After ``clear()`` a new owner may claim the same key; the old
        owner's release must not remove that newer claim."""
        cache = ResearchContentCache()
        url = "https://example.com/race"
        key = content_cache_key(url)
        newer_claim = threading.Event()

        def fetch():
            cache.clear()
            # A second caller elects itself owner of the cleared key.
            with cache._lock:
                assert key not in cache._inflight
                cache._inflight[key] = newer_claim
            return {"content": "body", "url": url}

        result = cache.get_or_fetch(url, fetch)
        assert result is not None and result["content"] == "body"
        assert cache._inflight.get(key) is newer_claim
        assert not newer_claim.is_set()

    def test_owner_still_releases_own_claim(self):
        cache = ResearchContentCache()
        url = "https://example.com/own"
        cache.get_or_fetch(url, lambda: {"content": "body", "url": url})
        assert cache._inflight == {}