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.

20.8 KB
Raw Blame
"""``/api/research/{id}/status`` must turn a failure into actionable advice.

When a research run dies, ``run_research_process``'s error handler writes the
raw failure text into ``research_meta["error"]``
(``web/services/research_service.py``, ``metadata.update({"phase": "error",
"error": user_friendly_error})``). The status endpoint is what the polling UI
reads, and it classifies that raw text into an ``error_info`` block --
``type`` / ``message`` / ``suggestion`` -- so the user sees "The Ollama service
is not responding properly. Make sure Ollama is running with 'ollama serve'
and the model is downloaded." instead of a stack-trace fragment.

Ported from ``tests/web/routes/test_research_routes_coverage.py``, deleted in
the Flask->FastAPI migration. Nothing on the branch replaced it: all ten
guidance strings live in ``web/routers/research.py`` and appeared in no test.
The deleted file is deliberately NOT ported as written -- its
``TestErrorClassification`` asserted against a private ``_classify_error``
copy of the route's if/elif chain pasted into the test module, so it would
have passed unchanged with the real chain deleted. Everything here drives the
real ``get_research_status`` and reads the real response.

What breaks if this regresses: the branch order is load-bearing and quietly
so. "Ollama connection refused at localhost:11434" contains "connection", and
"connection timeout occurred" contains "connection" too -- they must classify
as ``ollama_error`` and ``timeout`` respectively, because ``connection`` is
the last matcher in the chain. Reorder the arms and a user whose Ollama is
down is told to check their internet connection. That is exactly the moment
wrong advice costs the most.

The route body is not extractable -- the chain is inlined in the handler --
so the handler is called directly with a patched ``get_user_db_session``, the
pattern already used by ``test_history_report_unit.py``. HTTP adds nothing
here: the classification never touches the request.
"""

import ast
import json
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import Mock, patch

import pytest

from local_deep_research.web.routers import research as research_module
from local_deep_research.web.routers.research import (
    get_research_status,
    router,
)

RESEARCH_PATH = Path(research_module.__file__).resolve()

STATUS_ROUTE = "/api/research/{research_id}/status"

#: Every user-facing string the chain can emit, so a test can assert that a
#: *different* case emits none of them. Keep in sync with the handler.
ALL_MESSAGES = [
    "LLM service timed out during synthesis. This may be due to high server "
    "load or connectivity issues.",
    "The research query exceeded the AI model's token limit during synthesis.",
    "The AI model encountered an error during final answer synthesis.",
    "The Ollama service is not responding properly.",
    "Connection error with the AI service.",
]
ALL_SUGGESTIONS = [
    "Try again later or use a smaller query scope.",
    "Try using a more specific query or reduce the research scope.",
    "Check that your LLM service is running correctly or try a different model.",
    "Make sure Ollama is running with 'ollama serve' and the model is downloaded.",
    "Check your internet connection and AI service status.",
    "Try again with a different query or check the application logs.",
]


def _call_status(research_meta, *, status="failed"):
    """Run the real handler against a research row carrying ``research_meta``.

    Returns the handler's response dict. A row that is missing, or a handler
    that raised, comes back as a ``JSONResponse`` instead -- the caller's
    subscripting then fails loudly rather than silently skipping assertions.
    """
    row = Mock()
    row.status = status
    row.progress = 100
    row.completed_at = "2026-06-01T12:05:00+00:00"
    row.report_path = None
    row.research_meta = research_meta

    session = Mock()
    chain = session.query.return_value.filter_by.return_value
    # ResearchHistory lookup: .filter_by(id=...).first()
    chain.first.return_value = row
    # Latest-milestone lookup: .filter_by(...).order_by(...).first()
    chain.order_by.return_value.first.return_value = None

    @contextmanager
    def fake_db_session(*args, **kwargs):
        yield session

    with patch.object(
        research_module, "get_user_db_session", side_effect=fake_db_session
    ):
        return get_research_status(None, "research-1", username="alice")


def _error_info(response):
    return response["metadata"]["error_info"]


@pytest.mark.parametrize(
    "legacy_error",
    [
        "[Errno 13] Permission denied: '/srv/ldr/users/alice/reports/5.md'",
        r"[Errno 2] No such file or directory: 'C:\Users\Alice\LDR\report.md'",
    ],
)
def test_legacy_error_path_is_not_exposed_by_status(legacy_error):
    response = _call_status({"phase": "error", "error": legacy_error})

    rendered = json.dumps(response, default=str)
    assert legacy_error not in rendered
    assert response["metadata"]["error"] == (
        "Research failed. Check the server logs for details."
    )
    assert _error_info(response)["message"] == (
        "Research failed. Check the server logs for details."
    )


class TestNoFailureMeansNoGuidance:
    """Positive control, asserted before any negative case.

    Without this, a handler that unconditionally stapled the generic advice
    onto every response would satisfy several assertions below.
    """

    @pytest.mark.parametrize("empty_error", [None, "", False, 0, [], {}])
    def test_cleared_error_does_not_create_failure_guidance(self, empty_error):
        response = _call_status(
            {"phase": "complete", "error": empty_error}, status="completed"
        )

        assert response["metadata"] == {
            "phase": "complete",
            "error": empty_error,
        }

    def test_a_completed_research_gets_no_error_info_block(self):
        response = _call_status(
            {"phase": "complete", "duration": 42}, status="completed"
        )

        assert response["status"] == "completed"
        assert "error_info" not in response["metadata"], (
            "a research that did not fail must carry no error_info; the "
            "handler only builds one when research_meta has an 'error' key"
        )

    def test_a_completed_research_response_contains_no_guidance_string(self):
        response = _call_status(
            {"phase": "complete", "duration": 42}, status="completed"
        )

        rendered = json.dumps(response, default=str)
        for text in ALL_MESSAGES + ALL_SUGGESTIONS:
            assert text not in rendered, (
                f"the success response leaked failure guidance: {text!r}"
            )

    def test_metadata_without_an_error_key_is_still_passed_through(self):
        """Guards the premise of the two tests above: they would also pass if
        the handler dropped ``metadata`` from the response entirely."""
        response = _call_status(
            {"phase": "complete", "duration": 42}, status="completed"
        )

        assert response["metadata"]["phase"] == "complete"
        assert response["metadata"]["duration"] == 42


#: (case id, raw error text written by run_research_process, expected type,
#: expected message, expected suggestion)
CLASSIFICATIONS = [
    pytest.param(
        "LLM request timeout after 120s",
        "timeout",
        "LLM service timed out during synthesis. This may be due to high "
        "server load or connectivity issues.",
        "Try again later or use a smaller query scope.",
        id="timeout",
    ),
    pytest.param(
        "Exceeded token limit for model gpt-4",
        "token_limit",
        "The research query exceeded the AI model's token limit during "
        "synthesis.",
        "Try using a more specific query or reduce the research scope.",
        id="token-limit",
    ),
    pytest.param(
        "context length exceeded: 128000 tokens",
        "token_limit",
        "The research query exceeded the AI model's token limit during "
        "synthesis.",
        "Try using a more specific query or reduce the research scope.",
        id="context-length",
    ),
    pytest.param(
        "Final answer synthesis failed: parser error",
        "llm_error",
        "The AI model encountered an error during final answer synthesis.",
        "Check that your LLM service is running correctly or try a different "
        "model.",
        id="final-answer-synthesis-fail",
    ),
    pytest.param(
        "LLM error: model returned empty response",
        "llm_error",
        "The AI model encountered an error during final answer synthesis.",
        "Check that your LLM service is running correctly or try a different "
        "model.",
        id="llm-error",
    ),
    pytest.param(
        "Ollama server returned 500 Internal Server Error",
        "ollama_error",
        "The Ollama service is not responding properly.",
        "Make sure Ollama is running with 'ollama serve' and the model is "
        "downloaded.",
        id="ollama",
    ),
    pytest.param(
        "Connection refused by remote host",
        "connection",
        "Connection error with the AI service.",
        "Check your internet connection and AI service status.",
        id="connection",
    ),
    pytest.param(
        "Something went wrong",
        "unknown",
        "Research failed. Check the server logs for details.",
        "Try again with a different query or check the application logs.",
        id="unclassified",
    ),
]


class TestFailureGuidance:
    """Each raw failure text must map to its own message and suggestion."""

    @pytest.mark.parametrize(
        "error_text,expected_type,expected_message,expected_suggestion",
        CLASSIFICATIONS,
    )
    def test_exact_message_and_suggestion(
        self, error_text, expected_type, expected_message, expected_suggestion
    ):
        info = _error_info(
            _call_status({"phase": "error", "error": error_text})
        )

        assert info["type"] == expected_type
        assert info["message"] == expected_message
        assert info["suggestion"] == expected_suggestion

    @pytest.mark.parametrize(
        "error_text,expected_type,expected_message,expected_suggestion",
        CLASSIFICATIONS,
    )
    def test_no_other_branch_message_bleeds_in(
        self, error_text, expected_type, expected_message, expected_suggestion
    ):
        """Cross-branch control: collapsing the chain onto a single arm would
        keep every ``test_exact_message_and_suggestion`` case green for that
        one arm, and fail here for all the others."""
        info = _error_info(
            _call_status({"phase": "error", "error": error_text})
        )

        for other in ALL_MESSAGES:
            if other == expected_message:
                continue
            assert info["message"] != other
        for other in ALL_SUGGESTIONS:
            if other == expected_suggestion:
                continue
            assert info["suggestion"] != other

    def test_the_unclassified_case_withholds_the_raw_error_text(self):
        """The generic arm must not expose a legacy exception message."""
        raw = "ValueError: unhashable type in strategy adapter"
        info = _error_info(_call_status({"phase": "error", "error": raw}))

        assert info["message"] == (
            "Research failed. Check the server logs for details."
        )
        assert (
            info["suggestion"]
            == "Try again with a different query or check the application logs."
        )

    def test_a_metadata_solution_wins_over_the_generic_advice(self):
        """``run_research_process``'s error handler attaches an
        ``error_context`` with a ``solution`` for the failures it recognises
        itself. When it does, the endpoint must forward it rather than fall
        through to "check the application logs"."""
        info = _error_info(
            _call_status(
                {
                    "phase": "error",
                    "error": "Model 'mistral' not found on the server",
                    "solution": "Run 'ollama pull mistral' to download the "
                    "required model.",
                }
            )
        )

        assert info["type"] == "unknown"
        assert info["message"] == (
            "Research failed. Check the server logs for details."
        )
        assert info["suggestion"] == (
            "Run 'ollama pull mistral' to download the required model."
        )
        assert (
            info["suggestion"]
            != "Try again with a different query or check the application logs."
        )

    def test_a_solution_does_not_override_a_classified_failure(self):
        """An unrecognised stored message keeps the Ollama branch's advice."""
        info = _error_info(
            _call_status(
                {
                    "phase": "error",
                    "error": "Ollama server returned 500",
                    "solution": "Some unrelated hint.",
                }
            )
        )

        assert info["type"] == "ollama_error"
        assert info["suggestion"] == (
            "Make sure Ollama is running with 'ollama serve' and the model "
            "is downloaded."
        )


@pytest.mark.parametrize(
    "error,solution,json_metadata",
    [
        pytest.param(
            "Ollama AI service is unavailable. Please check that Ollama is "
            "running properly on your system.",
            "Start Ollama with 'ollama serve' or check if it's installed correctly.",
            False,
            id="ollama-unavailable",
        ),
        pytest.param(
            "Required Ollama model not found. Please pull the model first.",
            "Run 'ollama pull mistral' to download the required model.",
            False,
            id="model-not-found",
        ),
        pytest.param(
            "Required Ollama model not found. Please pull the model first.",
            "Run 'ollama pull mistral' to download the required model.",
            True,
            id="json-metadata",
        ),
    ],
)
def test_known_worker_solution_survives_status(
    error: str, solution: str, json_metadata: bool
) -> None:
    metadata = {"phase": "error", "error": error, "solution": solution}

    response = _call_status(json.dumps(metadata) if json_metadata else metadata)

    assert response["metadata"]["error"] == error
    assert _error_info(response) == {
        "type": "ollama_error",
        "message": error,
        "suggestion": solution,
    }


@pytest.mark.parametrize(
    "solution_metadata", [{}, {"solution": None}, {"solution": ""}]
)
def test_known_worker_error_without_solution_keeps_default_advice(
    solution_metadata: dict[str, str | None],
) -> None:
    error = "Required Ollama model not found. Please pull the model first."
    metadata = {"phase": "error", "error": error, **solution_metadata}

    info = _error_info(_call_status(metadata))

    assert info == {
        "type": "ollama_error",
        "message": error,
        "suggestion": "Make sure Ollama is running with 'ollama serve' and the "
        "model is downloaded.",
    }


def test_worker_message_with_private_suffix_keeps_masking_and_precedence() -> (
    None
):
    private_detail = "/srv/private/provider-internals.txt"
    metadata = {
        "phase": "error",
        "error": "Required Ollama model not found. Please pull the model first. "
        + private_detail,
        "solution": "Run 'ollama pull mistral' to download the required model.",
        "settings_snapshot": {"private_path": private_detail},
    }

    response = _call_status(metadata)

    assert private_detail not in json.dumps(response, default=str)
    assert "settings_snapshot" not in response["metadata"]
    assert response["metadata"]["error"] == (
        "Research failed. Check the server logs for details."
    )
    assert _error_info(response) == {
        "type": "ollama_error",
        "message": "The Ollama service is not responding properly.",
        "suggestion": "Make sure Ollama is running with 'ollama serve' and the "
        "model is downloaded.",
    }


class TestMatchOrderIsLoadBearing:
    """Overlapping error texts must resolve to the earlier arm.

    Real failure text routinely matches two matchers at once. These are the
    cases where reordering the chain silently hands the user wrong advice.
    """

    def test_ollama_beats_connection(self):
        """Ollama's own failure text says "connection refused"; telling that
        user to check their internet connection is actively misleading."""
        info = _error_info(
            _call_status(
                {
                    "phase": "error",
                    "error": "Ollama connection refused at localhost:11434",
                }
            )
        )

        assert info["type"] == "ollama_error"
        assert (
            info["message"] == "The Ollama service is not responding properly."
        )
        assert info["message"] != "Connection error with the AI service."

    def test_timeout_beats_connection(self):
        info = _error_info(
            _call_status(
                {"phase": "error", "error": "connection timeout occurred"}
            )
        )

        assert info["type"] == "timeout"
        assert info["suggestion"] == (
            "Try again later or use a smaller query scope."
        )
        assert info["suggestion"] != (
            "Check your internet connection and AI service status."
        )

    def test_token_limit_beats_llm_error(self):
        """A context-window overflow reported as an LLM error must still get
        the "narrow your query" advice, not "restart your LLM server"."""
        info = _error_info(
            _call_status(
                {"phase": "error", "error": "LLM error: token limit exceeded"}
            )
        )

        assert info["type"] == "token_limit"
        assert info["suggestion"] == (
            "Try using a more specific query or reduce the research scope."
        )
        assert info["suggestion"] != (
            "Check that your LLM service is running correctly or try a "
            "different model."
        )


def _classification_arms():
    """Yield the string literals tested by each arm of the real if/elif chain.

    Read out of the source rather than the runtime so a newly added arm is
    visible even if no test input reaches it.
    """
    tree = ast.parse(RESEARCH_PATH.read_text(encoding="utf-8"))
    fn = next(
        n
        for n in ast.walk(tree)
        if isinstance(n, ast.FunctionDef) and n.name == "get_research_status"
    )
    top = next(
        (
            n
            for n in ast.walk(fn)
            if isinstance(n, ast.If)
            and ast.unparse(n.test) == "'timeout' in error_msg.lower()"
        ),
        None,
    )
    if top is None:
        return None
    arms = []
    node = top
    while True:
        arms.append(
            tuple(
                c.value
                for c in ast.walk(node.test)
                if isinstance(c, ast.Constant) and isinstance(c.value, str)
            )
        )
        if len(node.orelse) == 1 and isinstance(node.orelse[0], ast.If):
            node = node.orelse[0]
        else:
            break
    return arms


class TestChainShapeIsPinned:
    """Premise guard for everything above.

    The parametrised table covers the arms that exist *today*. If someone adds
    a seventh classification, or renames a matcher so an existing test input
    stops reaching its arm, the table would keep passing while the new advice
    went untested. This fails instead, and says what to add.
    """

    def test_the_chain_was_found_at_all(self):
        arms = _classification_arms()
        assert arms is not None, (
            "could not locate the error classification chain in "
            f"{RESEARCH_PATH}; the guard below is scanning nothing and the "
            "tests above may be exercising a handler that no longer exists"
        )

    def test_the_matchers_are_exactly_the_ones_covered_here(self):
        assert _classification_arms() == [
            ("timeout",),
            ("token limit", "context length"),
            ("final answer synthesis fail", "llm error"),
            ("ollama",),
            ("connection",),
            ("solution",),
        ], (
            "the error classification chain in get_research_status changed. "
            "Add a CLASSIFICATIONS row (and a match-order test if the new "
            "matcher can overlap an existing one) before updating this list."
        )


def test_the_status_route_is_still_wired_to_this_handler():
    """The tests above call the handler directly; this is what ties that
    function to the URL the polling UI actually hits."""
    matches = [
        route
        for route in router.routes
        if getattr(route, "path", None) == STATUS_ROUTE
    ]

    assert len(matches) == 1, (
        f"expected exactly one {STATUS_ROUTE} route on the research router, "
        f"found {len(matches)}"
    )
    assert matches[0].endpoint is get_research_status
    assert "GET" in matches[0].methods