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.

7.8 KB
Raw Blame
"""Exception details must stay out of reports saved for research users."""

from unittest.mock import Mock

import pytest

from local_deep_research.error_handling.report_generator import (
    ErrorReportGenerator,
)


@pytest.mark.parametrize(
    ("error_message", "expected_hint"),
    [
        pytest.param(
            "Unexpected failure at /srv/private/provider-internals.txt",
            "unexpected error",
            id="unknown",
        ),
        pytest.param(
            "Connection refused at /srv/private/provider-internals.txt",
            "Cannot connect to the LLM service",
            id="recognized",
        ),
        pytest.param(
            "Cannot reach provider at /srv/private/provider-internals.txt "
            "(Error type: openai_connection_refused) | Details: private endpoint",
            "configured LLM endpoint",
            id="typed",
        ),
    ],
)
def test_report_excludes_raw_exception_details(error_message, expected_hint):
    report = ErrorReportGenerator().generate_error_report(
        error_message, "research query"
    )

    assert expected_hint.lower() in report.lower()
    assert "/srv/private/provider-internals.txt" not in report
    assert "private endpoint" not in report
    assert "Technical error:" not in report


def test_report_fallback_excludes_raw_exception_details():
    generator = ErrorReportGenerator()
    generator.error_reporter.analyze_error = Mock(
        side_effect=RuntimeError("report analysis failed")
    )

    report = generator.generate_error_report(
        "Failure at /srv/private/provider-internals.txt", "research query"
    )

    assert "Research Failed" in report
    assert "/srv/private/provider-internals.txt" not in report


def test_report_skips_error_shaped_partial_knowledge():
    marker = "/srv/private/provider-internals.txt"
    report = ErrorReportGenerator().generate_error_report(
        f"Error: provider failed at {marker}",
        "research query",
        partial_results={
            "current_knowledge": f"Error: provider failed at {marker}",
            "findings": [
                {"phase": "Final synthesis", "content": f"Error: {marker}"}
            ],
        },
    )

    assert marker not in report


@pytest.mark.parametrize(
    "uppercase", [False, True], ids=["original", "uppercase"]
)
@pytest.mark.parametrize(
    ("error_message", "explanation", "recovery"),
    [
        pytest.param(
            "Error: final answer synthesis failed: context length exceeded",
            "model's context limit",
            "Reduce the research scope",
            id="context-length",
        ),
        pytest.param(
            "Error: token limit exceeded",
            "model's context limit",
            "Reduce the research scope",
            id="token-limit",
        ),
        pytest.param(
            "Error: Request timed out after 120s",
            "request took too long",
            "increase the request timeout",
            id="timed-out",
        ),
        pytest.param(
            "Error: timeout during synthesis",
            "request took too long",
            "increase the request timeout",
            id="timeout",
        ),
        pytest.param(
            "Error: rate limit exceeded (429)",
            "provider's request limit",
            "Wait a few minutes",
            id="rate-limit",
        ),
        pytest.param(
            "Error: rate limit exceeded (Error type: unknown)",
            "provider's request limit",
            "Wait a few minutes",
            id="rate-limit-typed-unknown-fallthrough",
        ),
        pytest.param(
            "Error: Final answer synthesis failed due to LLM timeout",
            "request took too long",
            "increase the request timeout",
            id="synthesis-failed-mixed-timeout-cue",
        ),
        pytest.param(
            "Error: connection lost during synthesis",
            "connection to the LLM service failed",
            "Check your network connection",
            id="connection",
        ),
        pytest.param(
            "Error: network unavailable",
            "connection to the LLM service failed",
            "Check your network connection",
            id="network",
        ),
        pytest.param(
            "Error: LLM error during synthesis",
            "language model could not complete",
            "Try a different model",
            id="llm-error",
        ),
        pytest.param(
            "Error: final answer synthesis failed",
            "language model could not complete",
            "Try a different model",
            id="synthesis-failed",
        ),
    ],
)
def test_report_gives_safe_recovery_for_quick_mode_failures(
    error_message: str, explanation: str, recovery: str, uppercase: bool
) -> None:
    if uppercase:
        error_message = error_message.upper()
    raw_error = (
        f"{error_message} /srv/private/provider-internals.txt"
        " | Details: private-details-7235 https://provider.private.test/v1"
    )

    report = ErrorReportGenerator().generate_error_report(
        raw_error, "research query"
    )

    what_happened = report.split("**What happened:** ", 1)[1].split(
        "\n*For detailed error information", 1
    )[0]
    assert explanation.lower() in what_happened.lower()
    assert recovery.lower() in what_happened.lower()
    assert "**Try this:**" in what_happened
    assert error_message not in report
    assert raw_error not in report
    assert "/srv/private/provider-internals.txt" not in report
    assert "private-details-7235" not in report
    assert "https://provider.private.test/v1" not in report
    assert "| Details:" not in report


@pytest.mark.parametrize(
    ("error_message", "expected_message"),
    [
        pytest.param(
            "Error: connection refused during final answer synthesis failed",
            "Cannot connect to the LLM service.",
            id="specific-connection",
        ),
        pytest.param(
            "Error: All search engines blocked and rate limited",
            "No search results were found for your query.",
            id="specific-search",
        ),
        pytest.param(
            "Error: TypeError Context Size; context length exceeded",
            "Model configuration issue.",
            id="specific-context",
        ),
        pytest.param(
            "Error: timeout (Error type: openai_auth)",
            "Authentication with the LLM provider failed.",
            id="known-typed",
        ),
        pytest.param(
            "Error: network unavailable (ERROR TYPE: OPENAI_TIMEOUT)",
            "The configured LLM endpoint timed out.",
            id="known-typed-uppercase",
        ),
        pytest.param(
            "Error: timeout (Error type: custom_provider_error)",
            "Research failed due to an unexpected error.",
            id="nonstandard-typed",
        ),
        pytest.param(
            "Error: unexplained failure (Error type: unknown)",
            "Research failed due to an unexpected error.",
            id="unknown-typed",
        ),
        pytest.param(
            "Error: unexplained failure",
            "Research failed due to an unexpected error.",
            id="unknown-untyped",
        ),
    ],
)
def test_report_preserves_message_precedence_with_private_details(
    error_message: str, expected_message: str
) -> None:
    raw_error = (
        f"{error_message} /srv/private/provider-internals.txt"
        " | Details: private-details-7235 https://provider.private.test/v1"
    )

    report = ErrorReportGenerator().generate_error_report(
        raw_error, "research query"
    )

    assert f"**What happened:** {expected_message}" in report
    assert error_message not in report
    assert "/srv/private/provider-internals.txt" not in report
    assert "private-details-7235" not in report
    assert "https://provider.private.test/v1" not in report
    assert "| Details:" not in report