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.

3.7 KB
Raw Blame
"""Error classification must bound regex work on provider-controlled text."""

import re

import pytest

from local_deep_research.error_handling.error_messages import (
    MAX_ERROR_CLASSIFICATION_CHARS,
    TYPED_RESEARCH_ERROR_MESSAGES,
)
from local_deep_research.error_handling.error_reporter import (
    ErrorCategory,
    ErrorReporter,
)
from local_deep_research.error_handling.report_generator import (
    ErrorReportGenerator,
)


@pytest.mark.parametrize(
    "prefix", ["Model ", "localhost localhost ", "LM Studio Docker ", "İ"]
)
@pytest.mark.parametrize("stage", ["category", "guidance", "report"])
def test_large_provider_errors_bound_every_regex_input(
    monkeypatch, prefix, stage
):
    """Exercise the real patterns, without timing-dependent assertions.

    The observer fails before running an unbounded search on the old code;
    the corrected code still executes each real regex and builds the report.
    This covers the category scan that precedes the friendly-message scan.
    """
    raw_error = prefix * 50_000 + "/srv/private/provider-response.txt"
    searched_lengths = []
    native_search = re.search

    def bounded_search(pattern, string, *args, **kwargs):
        searched_lengths.append(len(string))
        assert len(string) <= MAX_ERROR_CLASSIFICATION_CHARS
        return native_search(pattern, string, *args, **kwargs)

    monkeypatch.setattr(re, "search", bounded_search)
    if stage == "category":
        result = ErrorReporter().categorize_error(raw_error)
        assert result == ErrorCategory.UNKNOWN_ERROR
    elif stage == "guidance":
        result = ErrorReportGenerator()._make_error_user_friendly(raw_error)
        assert "unexpected error" in result
    else:
        result = ErrorReportGenerator().generate_error_report(
            raw_error, query="Error classification budget"
        )
        assert "**Error Type:** Unexpected Error" in result
        assert "showing basic error information" not in result

    assert searched_lengths
    assert max(searched_lengths) <= MAX_ERROR_CLASSIFICATION_CHARS
    assert "/srv/private/provider-response.txt" not in str(result)


@pytest.mark.parametrize("over_boundary", [False, True])
def test_guidance_and_category_agree_at_the_input_boundary(over_boundary):
    cue = "database is locked"
    padding = MAX_ERROR_CLASSIFICATION_CHARS - len(cue) + over_boundary
    raw_error = "x" * padding + cue

    category = ErrorReporter().categorize_error(raw_error)
    guidance = ErrorReportGenerator()._make_error_user_friendly(raw_error)

    if over_boundary:
        assert category == ErrorCategory.UNKNOWN_ERROR
        assert "unexpected error" in guidance
    else:
        assert category == ErrorCategory.FILE_ERROR
        assert "local database is temporarily locked" in guidance


def test_early_guidance_survives_a_large_private_suffix():
    raw_error = (
        "Connection refused: " + "/srv/private/provider-response.txt" * 5000
    )
    generator = ErrorReportGenerator()

    assert generator._make_error_user_friendly(raw_error) == (
        generator._make_error_user_friendly("Connection refused")
    )
    assert (
        ErrorReporter().categorize_error(raw_error)
        == ErrorCategory.CONNECTION_ERROR
    )


@pytest.mark.parametrize("after_limit", [False, True])
def test_typed_guidance_obeys_the_same_prefix_limit(after_limit):
    token = "(Error type: openai_auth)"
    raw_error = (
        "x" * MAX_ERROR_CLASSIFICATION_CHARS + token
        if after_limit
        else token + "x" * (MAX_ERROR_CLASSIFICATION_CHARS * 2)
    )

    result = ErrorReportGenerator()._make_error_user_friendly(raw_error)

    if after_limit:
        assert "unexpected error" in result
    else:
        assert result == TYPED_RESEARCH_ERROR_MESSAGES["openai_auth"]