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.4 KB
Raw Blame
"""Bound expensive query composition without losing useful plain diagnostics."""

import asyncio
from types import SimpleNamespace

import pytest

from local_deep_research.security import log_sanitizer as ls


def _track_guard_sizes(monkeypatch):
    original = ls._pipeline_credential_spans
    sizes = []

    def collect(text):
        sizes.append(len(text))
        assert len(text) <= ls._LOG_REDACTION_MAX_CHARS
        return original(text)

    monkeypatch.setattr(ls, "_pipeline_credential_spans", collect)
    return sizes


def _sparse_message(size, assignment="password=value"):
    prefix = (
        f"Request failed https://host/path?query=value {assignment} details: "
    )
    prose = (
        "The provider supplied additional explanation of the upstream problem. "
    )
    return (prefix + prose * (size // len(prose) + 1))[:size]


@pytest.mark.parametrize("dense", [False, True])
@pytest.mark.parametrize("size", [32_767, 32_768, 32_769, 131_072])
def test_query_guard_analysis_is_bounded(monkeypatch, dense, size):
    sizes = _track_guard_sizes(monkeypatch)
    unit = "https://host/path?query=value password=value details "
    message = (
        (unit * (size // len(unit) + 1))[:size]
        if dense
        else _sparse_message(size)
    )
    result = ls.scrub_error(message)
    assert sizes
    assert "https://host/path?<redacted>" in result
    if size > ls._LOG_REDACTION_MAX_CHARS:
        assert result.count("characters omitted from log output]") == 1
        assert result.endswith("characters omitted from log output]")
    else:
        assert "characters omitted" not in result


@pytest.mark.parametrize("control", ["\u200b", "\r", "\n"])
def test_control_split_assignment_uses_the_same_work_bound(
    monkeypatch, control
):
    sizes = _track_guard_sizes(monkeypatch)
    message = _sparse_message(131_072, f"pass{control}word=value")
    result = ls.scrub_error(message)
    assert sizes
    assert "https://host/path?<redacted>" in result
    assert result.endswith("characters omitted from log output]")


@pytest.mark.parametrize(
    "prefix",
    [
        "Plain failure: ",
        "Request failed https://host/path?query=value details: ",
        "password=value without a URL query: ",
    ],
)
def test_long_diagnostics_without_query_guards_keep_the_tail(prefix):
    message = prefix + "ordinary detail " * 8_000 + "diagnostic-end"
    result = ls.scrub_error(message)
    assert len(result) > ls._LOG_REDACTION_MAX_CHARS
    assert result.endswith("diagnostic-end")
    assert "characters omitted" not in result


def _padded_prefix(prefix, size):
    remaining = size - len(prefix)
    return prefix + "x " * (remaining // 2) + " " * (remaining % 2)


@pytest.mark.parametrize("separator", [" ", "\n", "\u200b "])
def test_known_literal_crossing_cut_is_scrubbed_before_truncation(
    monkeypatch, separator
):
    sizes = _track_guard_sizes(monkeypatch)
    first = "KnownAlphaCanary731xy"
    second = "KnownOmegaCanary982xy"
    secret = first + separator + second
    prefix = _padded_prefix(
        "Request https://host/path?query=value password=value ",
        ls._LOG_REDACTION_MAX_CHARS - 30,
    )
    result = ls.scrub_error(prefix + secret + " extra detail" * 4_000, secret)
    assert sizes
    assert first not in result
    assert second not in result
    assert "***REDACTED***" in result
    assert result.endswith("characters omitted from log output]")


@pytest.mark.parametrize("quote", ["'", '"'])
@pytest.mark.parametrize("separator", [" ", "\r\n", "\u200b "])
def test_quoted_credential_cut_keeps_no_value_in_log_output(
    monkeypatch, quote, separator
):
    _track_guard_sizes(monkeypatch)
    first = "FirstCanary731xy"
    second = "SecondCanary982xy"
    prefix = _padded_prefix(
        "Request https://host/path?query=value details: ",
        ls._LOG_REDACTION_MAX_CHARS - 38,
    )
    message = (
        prefix
        + f" password={quote}{first}{separator}{second} trailing value{quote}"
        + " additional details" * 4_000
    )
    scrubbed = ls.scrub_error(message)
    record = {"message": scrubbed, "extra": {}, "exception": None}
    ls.redact_log_record(record)
    assert first not in record["message"]
    assert second not in record["message"]
    assert "characters omitted" in scrubbed


def test_async_provider_error_bounds_query_work_before_logging(monkeypatch):
    import httpx
    import openai

    from local_deep_research.config import llm_config

    sizes = _track_guard_sizes(monkeypatch)
    logged = []
    monkeypatch.setattr(llm_config, "scrub_error", ls.scrub_error)
    monkeypatch.setattr(
        llm_config, "logger", SimpleNamespace(warning=logged.append)
    )
    response = httpx.Response(
        500,
        text=_sparse_message(131_072),
        request=httpx.Request(
            "POST", "https://provider.invalid/v1/chat/completions"
        ),
    )
    with openai.OpenAI(api_key="synthetic-test-value", max_retries=0) as client:
        upstream_error = client._make_status_error_from_response(response)

    class Model:
        async def ainvoke(self, *args, **kwargs):
            raise upstream_error

    wrapper = llm_config.ProcessingLLMWrapper(Model(), owns_llm=False)
    with pytest.raises(openai.InternalServerError) as captured:
        asyncio.run(wrapper.ainvoke("Return selected indices"))
    assert captured.value is upstream_error
    assert sizes
    assert len(logged) == 1
    assert "https://host/path?<redacted>" in logged[0]
    assert logged[0].endswith("characters omitted from log output]")