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.

22.8 KB
Raw Blame
"""Contract tests for the shared ``utilities/`` layer.

Everything here is called from everywhere in the app, so a defect is
systemic rather than local. Each test below pins a behaviour that the
module's own docstrings/comments claim is impossible, or that two
callers of the same value disagree about.

Deliberately NOT re-covered here (already pinned elsewhere):

* ``db_utils.get_db_session`` raising ``RuntimeError`` for a
  username-less off-MainThread caller (filed separately).
* ``LRUCache(maxsize=10)`` evicting live Sessions without closing them
  (#5778) -- covered here only in its *cross-thread* form, which the
  filed issue does not describe.
* ``preserve_research_context`` capturing at decoration time
  (``tests/utilities/test_thread_context_semantics.py``).
* An int ``index`` raising inside ``extract_links_from_search_results``
  (``tests/utilities/test_search_utilities_extended.py``) -- covered
  here only as the *cross-module type disagreement* it creates.
"""

import gc
import queue
import threading
import weakref
from datetime import datetime, timezone
from types import SimpleNamespace

import pytest
from cachetools import LRUCache

from local_deep_research.utilities import log_utils
from local_deep_research.utilities.search_utilities import (
    extract_links_from_search_results,
    format_links_to_markdown,
)
from local_deep_research.utilities.threading_utils import (
    g_thread_local_store,
    thread_specific_cache,
)
from local_deep_research.utilities.url_utils import (
    is_safe_custom_llm_endpoint,
)
from local_deep_research.llm.providers.base import normalize_provider


class FakeSession:
    """Stand-in for the SQLAlchemy ``Session`` the real cache stores.

    Only records whether anybody closed it -- the point of the tests
    below is that nothing ever does.
    """

    def __init__(self, username):
        self.username = username
        self.closed = False

    def close(self):
        self.closed = True


@pytest.fixture
def thread_id_sandbox():
    """Save/restore this thread's ``thread_specific_cache`` identity.

    ``_key_func`` lazily stamps a uuid4 onto ``g_thread_local_store``.
    Overwriting it lets a single thread deterministically impersonate
    many threads against the REAL key function and the REAL cache --
    no sleeps, no scheduler dependence.
    """
    had = hasattr(g_thread_local_store, "thread_id")
    previous = getattr(g_thread_local_store, "thread_id", None)
    yield lambda tid: setattr(g_thread_local_store, "thread_id", tid)
    if had:
        g_thread_local_store.thread_id = previous
    elif hasattr(g_thread_local_store, "thread_id"):
        del g_thread_local_store.thread_id


class TestThreadSpecificCacheOwnership:
    """``utilities/threading_utils.thread_specific_cache``.

    The docstring justifies thread-keying as protecting a THREAD-AFFINE
    RESOURCE. It is silent on what happens to that resource once the
    owning thread is gone, and on the fact that the size bound is
    global while the key space is per-thread.
    """

    def test_cached_value_outlives_the_thread_that_created_it(self):
        """A dead thread's Session stays pinned by the shared cache.

        The key is a uuid4 minted per thread, so no future thread can
        ever produce that key again: the entry is unreachable by any
        caller yet still strongly referenced by the module-level cache,
        and still un-closed. Nothing in the codebase sweeps it -- only
        LRU pressure from *other* threads can evict it, and eviction
        does not close (#5778).
        """
        cache = LRUCache(maxsize=10)

        @thread_specific_cache(cache=cache)
        def open_session(username):
            return FakeSession(username)

        box = {}

        def worker():
            box["session"] = open_session("bob")

        thread = threading.Thread(target=worker)
        thread.start()
        thread.join()

        assert not thread.is_alive(), "worker thread should have exited"
        session = box.pop("session")
        assert session.closed is False
        assert len(cache) == 1

        ref = weakref.ref(session)
        del session
        gc.collect()

        assert ref() is not None, (
            "expected the dead thread's Session to still be pinned by the "
            "shared cache -- if this ever fails the leak has been fixed"
        )
        assert ref().closed is False, (
            "a Session belonging to a thread that no longer exists is still "
            "open and still reachable only from the cache; the uuid4 thread "
            "key guarantees no future caller can ever retrieve or close it"
        )

    def test_global_maxsize_evicts_a_live_threads_open_session(
        self, thread_id_sandbox
    ):
        """maxsize=10 is shared by ALL threads, not per thread.

        ``db_utils`` decorates ``_get_cached_user_session`` with
        ``LRUCache(maxsize=10)`` while the key space is
        (thread x username x namespace). anyio's default threadpool is
        40 workers, so an 11th distinct thread evicts the 1st thread's
        entry -- while that thread is still running and still holding
        the Session it was handed. The next call on that thread mints a
        SECOND Session for the same user; the first is never closed and
        its pooled connection is never returned.
        """
        # Tie the numbers below to the shipped configuration rather
        # than to a bound this test invented.
        from local_deep_research.utilities import db_utils

        production_cache = db_utils._get_cached_user_session.cache
        assert isinstance(production_cache, LRUCache)
        maxsize = production_cache.maxsize
        assert maxsize == 10

        cache = LRUCache(maxsize=maxsize)
        created = []

        @thread_specific_cache(cache=cache)
        def open_session(username):
            session = FakeSession(username)
            created.append(session)
            return session

        set_thread_id = thread_id_sandbox

        # 11 distinct live threads, one cached session each, same user.
        for i in range(maxsize + 1):
            set_thread_id(f"worker-{i}")
            open_session("alice")

        assert len(created) == maxsize + 1
        assert len(cache) == maxsize, (
            "cache is bounded globally, not per thread"
        )

        # Positive control: the most recent thread still gets its own
        # cached Session back -- the cache is not simply broken.
        set_thread_id(f"worker-{maxsize}")
        assert open_session("alice") is created[maxsize]
        assert len(created) == maxsize + 1

        # worker-0 is still alive and still holds created[0], but its
        # entry was evicted by unrelated threads' traffic.
        set_thread_id("worker-0")
        second = open_session("alice")

        assert len(created) == maxsize + 2, (
            "worker-0's entry was evicted by other threads, so it re-opened"
        )
        assert second is not created[0]
        assert created[0].closed is False, (
            "the evicted Session was never closed: cachetools drops the "
            "reference silently, so worker-0 now holds one orphaned open "
            "Session plus one live one for the same user"
        )

    def test_positional_and_keyword_calls_key_differently(
        self, thread_id_sandbox
    ):
        """The key is not normalised against the callee's signature.

        ``keys.hashkey`` separates positional args from keyword args, so
        ``f("alice")`` and ``f(username="alice")`` are two entries and
        two Sessions on ONE thread for ONE user. Today ``db_utils`` has
        a single call site and it is positional, so this is latent --
        but the decorator advertises itself as a per-thread resource
        cache, and a future keyword call site silently doubles the
        Session count instead of hitting the cache.
        """
        cache = LRUCache(maxsize=10)

        @thread_specific_cache(cache=cache)
        def open_session(username, namespace=""):
            return FakeSession(username)

        thread_id_sandbox("kwarg-thread")

        positional = open_session("alice")
        keyword = open_session(username="alice")

        assert positional is not keyword
        assert len(cache) == 2, (
            "same thread, same user, two cache entries -- the key encodes "
            "the CALL SHAPE, not just the arguments"
        )


class TestExtractLinksTypeContract:
    """``search_utilities.extract_links_from_search_results``.

    The whole per-result body sits inside one ``except Exception:
    continue``, so any type surprise on ANY field discards the entire
    result -- title, url, doi, authors and all -- with no signal beyond
    a server-side log line.
    """

    def test_int_index_is_dropped_here_but_supported_downstream(self):
        """Two consumers of ``index`` disagree about its type.

        ``format_links_to_markdown`` documents in-source that "Indices
        arrive as int (from strategy enumeration) or str" and coerces
        with ``str(i)``. ``extract_links_from_search_results`` calls
        ``.strip()`` on it, raises ``AttributeError``, and throws the
        whole citation away. Any producer that follows the formatter's
        documented contract loses its results at the extractor.
        """
        result = {
            "title": "Paper",
            "link": "https://example.com/paper",
            "index": 3,
            "doi": "10.1000/xyz",
        }

        assert extract_links_from_search_results([result]) == [], (
            "an int index silently discards the entire result"
        )

        # The downstream renderer accepts exactly the value the
        # extractor refused.
        markdown = format_links_to_markdown(
            [{"url": "https://example.com/paper", "title": "Paper", "index": 3}]
        )
        assert "[3]" in markdown
        assert "Paper" in markdown

    def test_non_string_title_or_link_drops_the_result_too(self):
        """Siblings of the int-index defect on the other two fields.

        ``title`` and ``url`` get the same None-only guard, so a numeric
        title (a year, a bare id) or a non-str link discards the result
        the same way. A non-dict element in the list does too.
        """
        results = [
            {"title": "Kept A", "link": "https://example.com/1", "index": "1"},
            {"title": 2024, "link": "https://example.com/2", "index": "2"},
            {"title": "Bad link", "link": 42, "index": "3"},
            {"title": "Int index", "link": "https://example.com/4", "index": 4},
            "not-a-dict",
            {"title": "Kept B", "link": "https://example.com/6", "index": "6"},
        ]

        links = extract_links_from_search_results(results)

        assert [link["url"] for link in links] == [
            "https://example.com/1",
            "https://example.com/6",
        ], "four of six results were silently discarded on type alone"

    def test_string_typed_results_survive_positive_control(self):
        """Positive control: the extractor works on well-typed input."""
        results = [
            {
                "title": "  Paper  ",
                "link": "  https://example.com/p  ",
                "index": " 7 ",
                "doi": "10.1000/xyz",
            }
        ]

        links = extract_links_from_search_results(results)

        assert links == [
            {
                "title": "Paper",
                "url": "https://example.com/p",
                "index": "7",
                "journal_quality": None,
                "doi": "10.1000/xyz",
            }
        ]


def _drain_log_queue():
    """Empty the shared log queue and return whatever was in it."""
    drained = []
    while True:
        try:
            drained.append(log_utils._log_queue.get_nowait())
        except queue.Empty:
            return drained


def _make_record(*, extra, message="settings saved"):
    """Build a loguru-record-shaped mapping for ``database_sink``."""
    return {
        "time": datetime.now(timezone.utc),
        "message": message,
        "name": "local_deep_research.web.routers.settings",
        "function": "save_setting",
        "line": 42,
        "level": SimpleNamespace(name="INFO"),
        "extra": extra,
        "exception": None,
    }


def _run_sink_off_main_thread(record):
    """Drive ``database_sink`` from a non-MainThread worker.

    ``database_sink`` writes synchronously only on MainThread with no
    running loop; every other caller enqueues. Running it on a worker
    exercises the queue branch that the deployed server always takes,
    and gives the sink a fresh (empty) contextvar copy so no ambient
    research context leaks in from the test session.
    """
    errors = []

    def worker():
        try:
            log_utils.database_sink(SimpleNamespace(record=record))
        except BaseException as exc:  # pragma: no cover - diagnostic
            errors.append(exc)

    thread = threading.Thread(target=worker, name="sink-under-test")
    thread.start()
    thread.join()
    assert not errors, f"database_sink raised: {errors}"


class TestDatabaseSinkResearchScopedGate:
    """``log_utils.database_sink``'s research-scoped persistence gate.

    The sink's own comment states the requirement: rows with
    ``research_id = NULL`` are unreachable by the ONLY deletion
    mechanism that ever touches ``app_logs`` (the ``ON DELETE CASCADE``
    from ``research_history``), so "delete this research" and "clear
    all history" cannot remove them and no retention job exists.
    """

    def test_bound_username_without_research_id_persists_an_orphan_row(self):
        """The research_id gate guards only ONE of the three sources.

        ``research_id is not None`` gates the ``_get_request_username()``
        fallback, but a ``logger.bind(username=...)`` extra (and the
        per-thread search context) reaches ``username`` with no such
        gate. The record then clears the ``research_id is None and
        username is None`` skip and is queued for permanent,
        undeletable persistence -- exactly the outcome the comment
        above that gate says it exists to prevent.
        """
        _drain_log_queue()
        record = _make_record(extra={"username": "alice"})

        _run_sink_off_main_thread(record)

        queued = _drain_log_queue()
        assert len(queued) == 1, (
            "expected the bound-username record to be queued for the DB"
        )
        entry = queued[0]
        assert entry["username"] == "alice"
        assert entry["research_id"] is None, (
            "queued for alice's app_logs with research_id=NULL: no cascade "
            "and no retention job can ever delete this row"
        )

    def test_context_free_record_is_dropped_positive_control(self):
        """Positive control: with no owner at all the record is dropped."""
        _drain_log_queue()
        record = _make_record(extra={})

        _run_sink_off_main_thread(record)

        assert _drain_log_queue() == []


class TestLogQueueProcessorRestart:
    """``log_utils.start_log_queue_processor`` / ``stop_...``."""

    def test_restart_after_timed_out_stop_returns_a_dying_daemon(self):
        """``start`` short-circuits without clearing the stop flag.

        ``stop_log_queue_processor`` deliberately keeps
        ``_queue_processor_thread`` populated when ``join()`` times out
        (so a second daemon can't be spawned onto the same queue) but
        leaves ``_stop_queue`` SET. ``start_log_queue_processor`` then
        sees a live thread and returns it *before* reaching its
        ``_stop_queue.clear()``. The caller is handed a thread that is
        already under orders to exit, believes drainage is live, and
        every subsequent log entry accumulates in the bounded queue
        until it silently fills and drops.
        """
        gate = threading.Event()
        stand_in = threading.Thread(target=gate.wait, daemon=True)
        stand_in.start()

        previous_thread = log_utils._queue_processor_thread
        log_utils._queue_processor_thread = stand_in
        try:
            # join(0) returns at once; the stand-in ignores the signal,
            # which is the timed-out-stop case the module handles.
            log_utils.stop_log_queue_processor(timeout=0)

            assert log_utils._queue_processor_thread is stand_in
            assert log_utils._stop_queue.is_set()

            restarted = log_utils.start_log_queue_processor()

            assert restarted is stand_in
            assert log_utils._stop_queue.is_set(), (
                "start_log_queue_processor returned a 'running' daemon "
                "while the stop flag is still set -- the real daemon would "
                "exit on its next loop check and nothing would restart it"
            )
        finally:
            gate.set()
            stand_in.join()
            log_utils._queue_processor_thread = previous_thread
            log_utils._stop_queue.clear()


class TestUrlUtilsUnvalidatedInput:
    """``url_utils`` / ``normalize_provider`` on untrusted request data.

    SECURITY-SENSITIVE: ``is_safe_custom_llm_endpoint`` is the
    request-boundary SSRF gate for a user-supplied LLM base_url.
    """

    @pytest.mark.parametrize(
        "endpoint",
        [
            ["http://169.254.169.254/latest/meta-data/"],
            123,
            {"url": "http://169.254.169.254/"},
        ],
    )
    def test_non_string_endpoint_fails_closed_instead_of_raising(
        self, endpoint
    ):
        """The guard now rejects a non-string endpoint instead of crashing.

        This used to crash with ``AttributeError`` inside ``.strip()``
        before reaching ``validate_url``'s own ``isinstance`` check --
        a DEFECT, since ``followup.py`` and ``news_flask_api.py`` call
        this unguarded and would surface an unhandled 500 for a JSON
        body like ``{"custom_endpoint": [...]}`` (an untyped dict is
        reachable from an authenticated request via
        ``research._extract_research_params``). ``is_safe_custom_llm_endpoint``
        now has its own ``isinstance(custom_endpoint, str)`` check up
        front and fails closed (returns ``False``, logs a warning)
        rather than reaching ``.strip()`` at all.

        Note ``research.py`` still pre-guards with
        ``isinstance(custom_endpoint, str)`` before calling this at
        all, which means a non-str endpoint SKIPS this function
        entirely on that path -- a separate, narrower gap than the one
        this test covers (this test is about the function's own
        behavior, not every caller's).
        """
        assert is_safe_custom_llm_endpoint(endpoint) is False

    def test_string_endpoints_are_classified_positive_control(self):
        """Positive control: well-typed endpoints are judged correctly."""
        assert is_safe_custom_llm_endpoint("localhost:11434") is True
        assert is_safe_custom_llm_endpoint("http://192.168.1.10:8000") is True
        assert is_safe_custom_llm_endpoint("") is True
        assert is_safe_custom_llm_endpoint(None) is True
        assert is_safe_custom_llm_endpoint("http://169.254.169.254") is False
        assert (
            is_safe_custom_llm_endpoint("http://[::ffff:169.254.169.254]")
            is False
        )

    @pytest.mark.parametrize(
        "metadata_endpoint",
        [
            # Oracle Compute Classic metadata is neither RFC1918 nor
            # link-local, so it needs an explicit always-blocked entry.
            "http://192.0.0.192/2007-08-29/meta-data/",
        ],
    )
    def test_oracle_classic_metadata_endpoint_is_blocked(
        self, metadata_endpoint
    ):
        """The custom LLM endpoint cannot reach Oracle Classic IMDS."""
        assert is_safe_custom_llm_endpoint(metadata_endpoint) is False

    @pytest.mark.parametrize(
        "link_local_endpoint",
        [
            # Generic link-local, not one of the always-blocked
            # metadata literals.
            "http://169.254.10.5/conf",
            # AWS VPC resolver / EC2 legacy helper range -- same /16, not
            # one of the always-blocked metadata literals.
            "http://169.254.169.253/",
        ],
    )
    def test_link_local_range_is_blocked_even_off_the_literal_list(
        self, link_local_endpoint
    ):
        """The literal-list gap over 169.254.0.0/16 has been closed.

        ``is_safe_custom_llm_endpoint`` now passes ``block_link_local=True``
        to ``validate_url`` (see the ``harden(ssrf): block link-local by
        range on the custom LLM endpoint path`` commit), so the entire
        link-local range is blocked, not just the enumerated
        cloud-metadata literals. This used to be pinned here as an
        allowed gap; it is a blocked range now, so this asserts the
        tightened behaviour instead.
        """
        assert is_safe_custom_llm_endpoint(link_local_endpoint) is False

    def test_normalize_provider_raises_on_non_string_input(self):
        """``normalize_provider`` is ``provider.strip().lower()`` (or None).

        It is applied directly to ``data.get("model_provider")`` from an
        untyped JSON body in ``research.py`` and ``followup.py``, and
        inside the egress policy's ``_is_user_registered_llm``. A
        non-str raises rather than normalising to a miss.
        """
        with pytest.raises(AttributeError):
            normalize_provider(["ollama"])
        with pytest.raises(AttributeError):
            normalize_provider(1)

        # And every falsy value collapses to the same None, so ""/0/False
        # are indistinguishable from "no provider given" downstream.
        assert normalize_provider("") is None
        assert normalize_provider(0) is None
        assert normalize_provider(False) is None
        # Positive control.
        assert normalize_provider("OpenAI_Endpoint") == "openai_endpoint"


class TestThreadContextIdentity:
    """``utilities/thread_context`` -- ownership of a context value."""

    def test_context_does_not_cross_into_a_plain_worker_thread(self):
        """A stdlib worker thread starts with an empty context copy.

        This is the property ``preserve_research_context`` exists to
        patch over, and it is why ``log_utils``'s per-thread fallback
        resolves nothing on a bare ``ThreadPoolExecutor`` worker: the
        sink there sees neither research_id nor username and drops the
        record. Pinned so a future switch of the storage primitive
        (e.g. back to ``threading.local``) has to confront it.
        """
        from local_deep_research.utilities.thread_context import (
            get_search_context,
            search_context,
        )

        seen = {}

        def worker():
            seen["context"] = get_search_context()

        with search_context({"research_id": "r-1", "username": "alice"}):
            assert get_search_context() == {
                "research_id": "r-1",
                "username": "alice",
            }
            thread = threading.Thread(target=worker)
            thread.start()
            thread.join()

        assert seen["context"] is None
        assert get_search_context() is None