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.

5.9 KB
Raw Blame
"""Outcome bookkeeping in ``BaseSearchEngine.run()`` (issue #8126).

Two behaviours of the shared search boundary:

* A control error (egress policy denial, unreadable setting) still
  propagates unchanged, but the ``SearchTracker`` row written in the
  ``finally`` block records the search as failed with the exception class
  as ``error_type``. Research termination is not a search failure and is
  not recorded as one.
* The per-search temporary attributes are removed without a
  check-then-delete, so a search whose attribute was already removed by
  another search on the same instance does not raise from ``run()``.
"""

from unittest.mock import Mock, patch

import pytest

from local_deep_research.exceptions import ResearchTerminatedException
from local_deep_research.security.egress.policy import (
    Decision,
    PolicyDeniedError,
)
from local_deep_research.settings.exceptions import SettingsValueError
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)

RECORD_SEARCH = (
    "local_deep_research.metrics.search_tracker.SearchTracker.record_search"
)

PREVIEW = {"title": "T", "snippet": "S", "url": "https://example.com/a"}


class _Engine(BaseSearchEngine):
    """Raises ``preview_error`` or ``content_error`` when set."""

    preview_error: BaseException | None = None
    content_error: BaseException | None = None

    def _get_previews(self, query):
        self._search_results = [PREVIEW]
        if self.preview_error is not None:
            raise self.preview_error
        return [dict(PREVIEW)]

    def _get_full_content(self, relevant_items):
        if self.content_error is not None:
            raise self.content_error
        return relevant_items


def _tracked_engine(rate_limited=False, **errors):
    engine = _Engine(programmatic_mode=True, search_snippets_only=False)
    # Construct in programmatic mode (no global tracker), then enable the
    # SearchTracker path, which run() takes only outside programmatic mode.
    engine.programmatic_mode = False
    engine.rate_tracker = Mock(enabled=rate_limited)
    engine.rate_tracker.get_wait_time.return_value = 0
    engine._verify_egress_scope = Mock()
    for name, error in errors.items():
        setattr(engine, name, error)
    return engine


def _policy_denied():
    return PolicyDeniedError(
        Decision(allowed=False, reason="scope_mismatch"), target="test"
    )


def _settings_error():
    return SettingsValueError(
        "search.engine.test.api_key", source="snapshot", reason="invalid"
    )


@pytest.mark.parametrize("rate_limited", [False, True])
@pytest.mark.parametrize("stage", ["preview_error", "content_error"])
@pytest.mark.parametrize(
    "make_error, error_type",
    [
        (_policy_denied, "PolicyDeniedError"),
        (_settings_error, "SettingsValueError"),
    ],
)
def test_control_error_propagates_and_is_recorded_as_failure(
    make_error, error_type, stage, rate_limited
):
    error = make_error()
    engine = _tracked_engine(rate_limited=rate_limited, **{stage: error})

    with patch(RECORD_SEARCH) as record_search:
        with pytest.raises(type(error)) as raised:
            engine.run("query")

    # The exception itself is unchanged.
    assert raised.value is error
    record_search.assert_called_once()
    recorded = record_search.call_args.kwargs
    assert recorded["success"] is False
    assert recorded["error_type"] == error_type
    assert recorded["error_message"]
    assert recorded["results_count"] == 0
    # The cleanup in the same finally block still ran.
    assert "_search_results" not in vars(engine)


def test_successful_search_is_still_recorded_as_success():
    """Positive control for the parametrised failure test above."""
    engine = _tracked_engine()

    with patch(RECORD_SEARCH) as record_search:
        results = engine.run("query")

    assert len(results) == 1
    recorded = record_search.call_args.kwargs
    assert recorded["success"] is True
    assert recorded["error_type"] is None
    assert recorded["results_count"] == 1


def test_research_termination_is_not_recorded_as_a_search_failure():
    """Termination is a user stop, not an engine failure.

    ``ResearchTerminatedException`` derives from ``BaseException`` and
    bypasses the failure handling, so engine error counts are unaffected.
    """
    error = ResearchTerminatedException("stopped")
    engine = _tracked_engine(preview_error=error)

    with patch(RECORD_SEARCH) as record_search:
        with pytest.raises(ResearchTerminatedException) as raised:
            engine.run("query")

    assert raised.value is error
    recorded = record_search.call_args.kwargs
    assert recorded["success"] is True
    assert recorded["error_type"] is None


class _ConcurrentlyCleanedEngine(_Engine):
    """Another search on this instance removes ``_search_results`` the
    moment this search has seen it, i.e. between a presence check and the
    delete that follows it."""

    def __getattribute__(self, name):
        value = super().__getattribute__(name)
        if name == "_search_results":
            vars(self).pop(name, None)
        return value


def test_cleanup_tolerates_temp_attribute_removed_concurrently():
    engine = _ConcurrentlyCleanedEngine(programmatic_mode=True)
    engine.rate_tracker = Mock(enabled=False)
    engine._verify_egress_scope = Mock()

    results = engine.run("query")

    assert len(results) == 1
    assert "_search_results" not in vars(engine)


def test_cleanup_removes_every_temp_attribute():
    class _TwoAttrs(_Engine):
        def _get_previews(self, query):
            self._extra = "x"
            return super()._get_previews(query)

        def _temp_attributes(self):
            return super()._temp_attributes() + ["_extra", "_never_set"]

    engine = _TwoAttrs(programmatic_mode=True)
    engine.rate_tracker = Mock(enabled=False)
    engine._verify_egress_scope = Mock()

    engine.run("query")

    assert "_search_results" not in vars(engine)
    assert "_extra" not in vars(engine)