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.

10.4 KB
Raw Blame
"""The two *setup* failure paths of ``run_research_process``, and their scrub.

Ported from ``tests/web/services/test_research_service_coverage_gaps.py``
(``TestLLMConfigErrorRaisesValueError``, ``TestSearchEngineConfigError``,
``TestUnexpectedFailureDoesNotLeakRawException``), deleted in the
Flask->FastAPI migration.

``run_research_process`` can fail in three places, and the branch's successor
only drives one of them.
``tests/web/services/test_research_service_error_typing.py`` says so in its
own docstring -- "the real (undecorated) worker is driven to them with
``analyze_topic`` raising" -- and every case in it goes through
``run_quick_mode_with_search_error``. That covers the *third* site. The two
earlier ones are:

1. ``get_llm(...)`` raising during LLM setup. A message containing a config
   keyword (``llamacpp``, ``model path``, ``.gguf``, ``server``, ...) is
   re-raised as ``ValueError("LLM Configuration Error: {raw}")``, which the
   central handler matches on and REPLACES with "There was a problem with the
   LLM configuration." plus a hint.
2. ``get_search(...)`` raising during search-engine setup, likewise re-raised
   as ``ValueError("Search Engine Configuration Error (...): {raw}")``.

Both re-raises embed ``str(e)`` verbatim. ``settings_snapshot`` carries
``LDR_*`` environment overrides, so that raw text routinely contains server
filesystem paths, internal endpoint URLs and provider credentials in error
text. The *only* thing standing between it and the persisted, client-readable
``error_message`` is the arm in the central handler that overwrites
``user_friendly_error`` -- two ``elif`` branches, each three lines, neither
reachable from ``analyze_topic``. Delete either one and the raw text falls
through to the ``else`` branch... no: it falls through to being persisted
*as-is*, because ``user_friendly_error`` was initialised from ``str(e)``.
The run still fails, the status endpoint still answers, and nothing in the
suite notices.

The last class pins the same scrub at the other sink: the enhanced error
report, which is persisted and served by the report routes.
"""

from contextlib import ExitStack
from unittest.mock import MagicMock, patch

from tests.web.services.helpers import (
    MODULE,
    QUEUE_PROC_MOD,
    _base_run_patches,
    _egress_and_search_patches,
    _get_raw_run_research_process,
)

#: Minimal snapshot that gets a run past the egress fail-closed precheck so
#: the setup failure under test is actually reached.
_SNAPSHOT = {
    "llm.provider": "ollama",
    "llm.model": "m",
    "search.tool": "searxng",
}


def _run_with(failing_target, error_message, **run_kwargs):
    """Drive the real worker with ``failing_target`` raising, and return the
    ``queue_processor`` mock the central error handler wrote through."""
    qp = MagicMock()
    patches = _base_run_patches()
    patches[f"{MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
    patches[f"{MODULE}.get_search"] = MagicMock(return_value=MagicMock())
    patches[failing_target] = MagicMock(side_effect=Exception(error_message))
    patches[f"{QUEUE_PROC_MOD}.queue_processor"] = qp

    with ExitStack() as stack:
        for cm in _egress_and_search_patches():
            stack.enter_context(cm)
        for target, mock_obj in patches.items():
            stack.enter_context(patch(target, mock_obj))
        _get_raw_run_research_process()(
            1,
            "test query",
            "quick",
            username="user1",
            settings_snapshot=_SNAPSHOT,
            **run_kwargs,
        )

    assert qp.queue_error_update.called, (
        "queue_error_update was never called -- the central error handler "
        "was not reached, so this test would pass vacuously"
    )
    return qp.queue_error_update.call_args


class TestLLMSetupFailures:
    """``get_llm`` raising: classified, then scrubbed."""

    def _run(self, message):
        return _run_with(
            f"{MODULE}.get_llm", message, model="gpt-4", model_provider="openai"
        )

    def test_a_llamacpp_failure_is_named_as_an_llm_config_problem(self):
        call = self._run("llamacpp model failed to load")

        assert (
            call.kwargs["error_message"]
            == "There was a problem with the LLM configuration."
        )

    def test_the_llamacpp_failures_raw_text_never_reaches_the_client(self):
        call = self._run("llamacpp model failed to load")

        assert "llamacpp model failed to load" not in str(call)

    def test_a_model_path_failure_does_not_leak_the_path(self):
        """``model path`` messages carry a server filesystem path."""
        call = self._run("model path /srv/models/private does not exist")

        assert (
            call.kwargs["error_message"]
            == "There was a problem with the LLM configuration."
        )
        assert "/srv/models/private" not in str(call)

    def test_a_gguf_failure_does_not_leak_its_detail(self):
        call = self._run("please provide a valid .gguf file at /opt/weights")

        assert (
            call.kwargs["error_message"]
            == "There was a problem with the LLM configuration."
        )
        assert "/opt/weights" not in str(call)

    def test_a_classified_llm_failure_carries_an_actionable_hint(self):
        """The category string alone is a dead end; the hint names what to
        change."""
        call = self._run("llamacpp model failed to load")

        assert "solution" in call.kwargs["metadata"]
        assert "LLM model settings" in call.kwargs["metadata"]["solution"]

    def test_an_unclassified_llm_failure_is_genericized_not_forwarded(self):
        """No config keyword => the ``else`` arm. The raw text is server-side
        internal detail with no curated form, so it must not be surfaced
        (CWE-209)."""
        call = self._run("null pointer in inference at 0xdeadbeef")

        assert "LLM Configuration Error" not in str(call)
        assert "null pointer in inference" not in str(call)
        assert "unexpected error" in call.kwargs["error_message"].lower()


class TestSearchEngineSetupFailures:
    """``get_search`` raising: classified, then scrubbed."""

    def _run(self, message):
        return _run_with(
            f"{MODULE}.get_search", message, search_engine="searxng"
        )

    def test_a_searxng_failure_is_named_as_a_search_config_problem(self):
        call = self._run("SearXNG instance unreachable at http://10.0.0.5:8888")

        assert (
            call.kwargs["error_message"]
            == "There was a problem with the search engine configuration."
        )

    def test_the_searxng_failure_does_not_leak_the_internal_endpoint(self):
        call = self._run("SearXNG instance unreachable at http://10.0.0.5:8888")

        assert "10.0.0.5" not in str(call)

    def test_an_api_key_failure_does_not_leak_the_message(self):
        call = self._run("Missing api_key 'sk-live-abcdef' for search provider")

        assert (
            call.kwargs["error_message"]
            == "There was a problem with the search engine configuration."
        )
        assert "sk-live-abcdef" not in str(call)

    def test_a_connection_failure_is_classified_as_search_config(self):
        call = self._run("Connection refused by search backend")

        assert (
            call.kwargs["error_message"]
            == "There was a problem with the search engine configuration."
        )
        assert "Connection refused by search backend" not in str(call)

    def test_a_classified_search_failure_carries_an_actionable_hint(self):
        call = self._run("SearXNG instance unreachable")

        assert "solution" in call.kwargs["metadata"]
        assert "search engine settings" in call.kwargs["metadata"]["solution"]

    def test_an_unclassified_search_failure_is_genericized(self):
        call = self._run("random internal crash at /srv/internal")

        assert "Search Engine Configuration Error" not in str(call)
        assert "/srv/internal" not in str(call)
        assert "unexpected error" in call.kwargs["error_message"].lower()


class TestTheErrorReportSinkIsScrubbedToo:
    """The enhanced report is persisted and retrievable via the report routes,
    so its ``error_message`` must be the sanitized text, not raw ``str(e)``.

    This is a second, independent sink: the queue-update assertions above
    would all still pass if ``generate_error_report`` were handed ``{e!s}``.
    """

    def test_the_raw_exception_text_is_not_embedded_in_the_report(self):
        secret = "ZZSECRETZZ /opt/internal raw traceback frame 0xdeadbeef"
        captured = {}

        def _capture(*args, **kwargs):
            captured["error_message"] = kwargs.get("error_message", "")
            return "error report"

        generator = MagicMock()
        generator.generate_error_report.side_effect = _capture

        patches = _base_run_patches()
        patches[f"{MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
        patches[f"{MODULE}.get_search"] = MagicMock(
            side_effect=Exception(secret)
        )
        patches[f"{MODULE}.ErrorReportGenerator"] = MagicMock(
            return_value=generator
        )

        with ExitStack() as stack:
            for cm in _egress_and_search_patches():
                stack.enter_context(cm)
            for target, mock_obj in patches.items():
                stack.enter_context(patch(target, mock_obj))
            _get_raw_run_research_process()(
                1,
                "test query",
                "quick",
                username="testuser",
                search_engine="searxng",
                settings_snapshot=_SNAPSHOT,
            )

        assert "error_message" in captured, (
            "the error report was never generated; the except handler was "
            "not reached"
        )
        assert secret not in captured["error_message"]
        assert "unexpected error" in captured["error_message"].lower()


def test_the_helper_resolves_the_service_function_this_file_targets():
    """Name the SUT directly rather than only through patch-target strings.

    Everything above reaches ``run_research_process`` via string patch targets
    and a helper, so nothing in this module names the function itself. If it
    moved, the patches would fail loudly — but a reader (and the shadow-test
    hook) cannot see what is under test.
    """
    from local_deep_research.web.services.research_service import (
        run_research_process,
    )

    assert callable(run_research_process)
    assert run_research_process.__module__.endswith("research_service")