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.

52.2 KB
Raw Blame
"""Cross-engine failure contracts for the search-engine adapter layer.

Scope (PR #3299, Flask -> FastAPI port): the adapters under
``web_search_engines/`` and their shared rate-limiting layer. Every test
here drives a real adapter with its HTTP client (or vendored SDK) stubbed
out; **no test in this file makes an outbound network request**, and the
two tests that reason about wall-clock behaviour inject a fake clock
rather than sleeping.

What this file pins that the per-engine unit tests do not:

1. **Blast radius.** ``BaseSearchEngine.run`` is the single chokepoint
   that decides whether one dead backend kills a research run. It is
   pinned against the whole realistic failure surface (timeout, refused
   connection, HTTP error, malformed JSON, missing key, OOM) *paired
   with* a success case through the same stub, so "returns []" can never
   pass vacuously.
2. **Normalisation.** The preview dicts four different engines emit are
   fed to the real downstream consumer
   (``extract_links_from_search_results``) rather than to an assertion
   about a hand-copied shape.
3. **Rate limiting.** The adaptive tracker's learn/back-off arithmetic,
   and the SearXNG per-instance-URL throttle whose eviction path is
   issue #5748. The #5748 test proves the *consequence* (the throttle is
   silently lost) rather than restating that eviction happens -- the
   existing suite already covers the latter.
4. **Query privacy (#5734).** Whether an adapter puts the user's
   research query into a log record at a level the database sink
   persists.
"""

import json
import socket
from types import SimpleNamespace
from unittest.mock import MagicMock, Mock, patch

import pytest
import requests
from loguru import logger as loguru_logger

from local_deep_research.security import safe_requests
from local_deep_research.security.egress import validators as egress_validators
from local_deep_research.security.egress.policy import PolicyDeniedError
from local_deep_research.utilities import log_utils
from local_deep_research.utilities.search_utilities import (
    extract_links_from_search_results,
)
from local_deep_research.web_search_engines.engines import (
    _searxng_rate_limiter as searxng_throttle,
)
from local_deep_research.web_search_engines.engines import (
    search_engine_arxiv as arxiv_engine,
)
from local_deep_research.web_search_engines.engines import (
    search_engine_pubmed as pubmed_engine,
)
from local_deep_research.web_search_engines.engines import (
    search_engine_searxng as searxng_engine,
)
from local_deep_research.web_search_engines.engines import (
    search_engine_wikipedia as wikipedia_engine,
)
from local_deep_research.web_search_engines.rate_limiting import RateLimitError
from local_deep_research.web_search_engines.rate_limiting import (
    tracker as tracker_mod,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)

# A query string no production log line could plausibly contain by
# accident, so a substring match against captured log records is a sound
# leak detector.
PROBE_QUERY = "zzsentinelqq idiopathic pulmonary fibrosis prognosis"

# The keys every engine's preview dict must carry for the downstream
# citation/link pipeline to consume it.
REQUIRED_PREVIEW_KEYS = frozenset({"id", "title", "link", "snippet"})


# --------------------------------------------------------------------------
# Stubs
# --------------------------------------------------------------------------


class StubResponse:
    """Stand-in for ``requests.Response`` used by every stubbed adapter."""

    def __init__(self, status_code=200, text="", payload=None, json_exc=None):
        self.status_code = status_code
        self.text = text
        self.cookies = {}
        self.headers = {}
        self._payload = payload
        self._json_exc = json_exc

    def json(self):
        if self._json_exc is not None:
            raise self._json_exc
        return self._payload

    def raise_for_status(self):
        if self.status_code >= 400:
            raise requests.HTTPError(
                f"{self.status_code} Server Error", response=self
            )


class FakeClock:
    """Injected in place of a module's ``time`` import.

    ``sleep`` records the requested duration and advances the clock
    instead of blocking, so back-off assertions are exact and the suite
    never burns wall-clock time (see ``check-unmarked-sleep``).
    """

    def __init__(self, start=1000.0):
        self.now = start
        self.slept = []

    def monotonic(self):
        return self.now

    def time(self):
        return self.now

    def sleep(self, seconds):
        self.slept.append(seconds)
        self.now += seconds

    def advance(self, seconds):
        self.now += seconds


class _LogCapture:
    """Collects loguru records at/above a level, as a sink would."""

    def __init__(self):
        self.records = []

    def __call__(self, message):
        self.records.append(message.record)

    def messages_containing(self, needle):
        return [
            (r["level"].name, r["name"], r["message"])
            for r in self.records
            if needle in r["message"]
        ]


@pytest.fixture
def captured_logs():
    """Capture records at INFO and above -- the database sink's level.

    ``config_logger`` installs ``database_sink`` at ``sink_level``, which
    is ``"INFO"`` whenever debug logging is off (the production default);
    ``test_database_sink_is_installed_at_info_level`` pins that linkage.
    """
    capture = _LogCapture()
    loguru_logger.enable("local_deep_research")
    sink_id = loguru_logger.add(capture, level="INFO")
    try:
        yield capture
    finally:
        loguru_logger.remove(sink_id)


@pytest.fixture
def inert_tracker():
    """A rate tracker that neither sleeps nor persists.

    Rate-limiting behaviour is exercised directly against the real
    tracker in ``TestAdaptiveRateLimiter``; the per-adapter tests need it
    out of the way.
    """
    tracker = Mock()
    tracker.enabled = False
    tracker.apply_rate_limit.return_value = 0.0
    tracker.get_wait_time.return_value = 0.0
    tracker.record_outcome.return_value = None
    target = (
        "local_deep_research.web_search_engines.search_engine_base.get_tracker"
    )
    with patch(target, return_value=tracker):
        yield tracker


# --------------------------------------------------------------------------
# Adapter builders (all network-free)
# --------------------------------------------------------------------------


def build_searxng(inert_tracker, result_format="json"):
    """Construct a SearXNG engine whose availability probe is stubbed.

    ``SearXNGSearchEngine.__init__`` performs a live ``safe_get`` against
    the instance URL, so it must be patched even to construct one.
    """
    with patch.object(
        searxng_engine, "safe_get", return_value=StubResponse(200, "<html/>")
    ):
        engine = searxng_engine.SearXNGSearchEngine(
            instance_url="http://searx.invalid:8080/",
            result_format=result_format,
            llm=None,
            max_results=5,
        )
    assert engine._is_available, "stubbed probe should mark engine available"
    engine.rate_tracker = inert_tracker
    engine.programmatic_mode = True
    return engine


def build_wikipedia(inert_tracker):
    with patch.object(wikipedia_engine.wikipedia, "set_lang"):
        engine = wikipedia_engine.WikipediaSearchEngine(max_results=3, llm=None)
    engine.rate_tracker = inert_tracker
    engine.programmatic_mode = True
    return engine


def build_arxiv(inert_tracker):
    engine = arxiv_engine.ArXivSearchEngine(max_results=3, llm=None)
    engine.rate_tracker = inert_tracker
    engine.programmatic_mode = True
    return engine


def build_pubmed(inert_tracker):
    engine = pubmed_engine.PubMedSearchEngine(
        max_results=3,
        llm=None,
        optimize_queries=False,
        get_abstracts=False,
    )
    engine.rate_tracker = inert_tracker
    engine.programmatic_mode = True
    return engine


def fake_arxiv_client(papers=None, error=None):
    """Return a drop-in for ``fetch_arxiv_results`` that never touches the
    net.

    ``ArXivSearchEngine`` is routed through the typed
    ``utilities.arxiv_api.fetch_arxiv_results`` boundary rather than the
    vendored ``arxiv`` SDK directly, so the seam to stub is the module-level
    ``fetch_arxiv_results`` name imported into ``search_engine_arxiv``.
    """

    def _fetch(request):
        if error is not None:
            raise error
        return list(papers or [])

    return _fetch


def fake_arxiv_paper(entry_id="http://arxiv.org/abs/2401.00001"):
    return SimpleNamespace(
        entry_id=entry_id,
        title="A Stubbed Paper",
        summary="Stub abstract body.",
        authors=[SimpleNamespace(name="A. Author")],
        published=None,
        journal_ref=None,
        pdf_url="http://arxiv.org/pdf/2401.00001",
        categories=["cs.AI"],
        comment=None,
        doi=None,
        updated=None,
        primary_category="cs.AI",
    )


# --------------------------------------------------------------------------
# 1. One dead engine must not abort a research run
# --------------------------------------------------------------------------


class _ScriptedEngine(BaseSearchEngine):
    """Minimal concrete engine whose preview phase is scripted."""

    def __init__(self, behaviour, **kwargs):
        super().__init__(programmatic_mode=True, **kwargs)
        self.behaviour = behaviour
        self.preview_calls = 0

    def _get_previews(self, query):
        self.preview_calls += 1
        if isinstance(self.behaviour, BaseException):
            raise self.behaviour
        return self.behaviour


HEALTHY_PREVIEWS = [
    {
        "id": "p1",
        "title": "Healthy result",
        "link": "https://example.org/p1",
        "snippet": "A snippet.",
    }
]


class TestOneEngineFailureDoesNotAbortTheRun:
    """``BaseSearchEngine.run`` is the blast-radius chokepoint."""

    @pytest.mark.parametrize(
        "failure",
        [
            pytest.param(requests.Timeout("read timed out"), id="timeout"),
            pytest.param(
                requests.ConnectionError("connection refused"),
                id="backend-down",
            ),
            pytest.param(
                requests.HTTPError("500 Server Error"), id="http-error"
            ),
            pytest.param(
                json.JSONDecodeError("Expecting value", "<html>", 0),
                id="malformed-json",
            ),
            pytest.param(KeyError("results"), id="missing-field"),
            pytest.param(ValueError("unparseable payload"), id="bad-value"),
            pytest.param(MemoryError("oom while parsing"), id="oom"),
        ],
    )
    def test_backend_failure_degrades_to_empty_not_an_exception(self, failure):
        """Each failure yields ``[]``; the paired success proves the
        assertion is not vacuous (an engine that returned ``[]`` for
        everything would fail the first half)."""
        healthy = _ScriptedEngine(HEALTHY_PREVIEWS)
        assert healthy.run(PROBE_QUERY) == HEALTHY_PREVIEWS

        broken = _ScriptedEngine(failure)
        assert broken.run(PROBE_QUERY) == []
        assert broken.preview_calls == 1, (
            "a non-rate-limit failure must not be retried"
        )

    def test_rate_limit_error_is_retried_then_degrades_to_empty(self):
        """A ``RateLimitError`` is the one retryable failure: tenacity
        retries it up to 3 attempts, then ``run`` still returns ``[]``."""
        tracker = Mock()
        tracker.enabled = True
        # Zero wait keeps tenacity from sleeping on the wall clock.
        tracker.get_wait_time.return_value = 0.0
        tracker.record_outcome.return_value = None

        healthy = _ScriptedEngine(HEALTHY_PREVIEWS)
        healthy.rate_tracker = tracker
        assert healthy.run(PROBE_QUERY) == HEALTHY_PREVIEWS
        assert healthy.preview_calls == 1

        throttled = _ScriptedEngine(RateLimitError("429 slow down"))
        throttled.rate_tracker = tracker
        assert throttled.run(PROBE_QUERY) == []
        assert throttled.preview_calls == 3, (
            "rate limiting must exhaust stop_after_attempt(3)"
        )

    def test_policy_denial_is_the_one_error_that_escapes_run(self):
        """Documented boundary: the egress backstop runs *before*
        ``run``'s try block, so a denial propagates by design (constant
        denial latency). Every other backstop failure is swallowed."""
        denied = _ScriptedEngine(
            HEALTHY_PREVIEWS,
            settings_snapshot={"policy.egress_scope": "public_only"},
        )
        denied._engine_name = "searxng"
        decision = SimpleNamespace(reason="engine outside egress scope")
        with patch.object(
            _ScriptedEngine,
            "_check_egress_policy",
            side_effect=PolicyDeniedError(decision, target="searxng"),
        ):
            with pytest.raises(PolicyDeniedError):
                denied.run(PROBE_QUERY)

        # A *broken* policy decision point must not take the search down.
        broken_pdp = _ScriptedEngine(
            HEALTHY_PREVIEWS,
            settings_snapshot={"policy.egress_scope": "public_only"},
        )
        broken_pdp._engine_name = "searxng"
        with patch.object(
            _ScriptedEngine,
            "_check_egress_policy",
            side_effect=RuntimeError("policy store unreachable"),
        ):
            assert broken_pdp.run(PROBE_QUERY) == HEALTHY_PREVIEWS


# --------------------------------------------------------------------------
# 2. Per-adapter degradation, each paired with a success control
# --------------------------------------------------------------------------


SEARXNG_OK_PAYLOAD = {
    "results": [
        {
            "url": "https://example.org/article",
            "title": "Fibrosis review",
            "content": "Snippet text.",
            "engine": "duckduckgo",
            "category": "general",
        }
    ]
}


class TestSearxngDegradation:
    def test_success_control(self, inert_tracker):
        engine = build_searxng(inert_tracker)
        with patch.object(
            searxng_engine,
            "safe_get",
            return_value=StubResponse(200, payload=SEARXNG_OK_PAYLOAD),
        ):
            previews = engine._get_previews(PROBE_QUERY)
        assert len(previews) == 1
        assert previews[0]["link"] == "https://example.org/article"
        assert previews[0]["snippet"] == "Snippet text."

    @pytest.mark.parametrize(
        "stub_kwargs",
        [
            pytest.param({"return_value": StubResponse(500)}, id="http-500"),
            pytest.param({"return_value": StubResponse(429)}, id="http-429"),
            pytest.param(
                {
                    "return_value": StubResponse(
                        200,
                        json_exc=json.JSONDecodeError("bad", "<html>", 0),
                    )
                },
                id="malformed-json",
            ),
            pytest.param(
                {"side_effect": requests.Timeout("timed out")}, id="timeout"
            ),
            pytest.param(
                {"side_effect": requests.ConnectionError("refused")},
                id="backend-down",
            ),
            pytest.param(
                {"side_effect": ValueError("URL failed SSRF validation")},
                id="ssrf-refusal",
            ),
        ],
    )
    def test_failure_yields_no_previews_without_raising(
        self, inert_tracker, stub_kwargs
    ):
        engine = build_searxng(inert_tracker)
        with patch.object(searxng_engine, "safe_get", **stub_kwargs):
            assert engine.run(PROBE_QUERY) == []
        assert engine.last_search_failure is not None

    def test_searxng_internal_pages_are_not_returned_as_results(
        self, inert_tracker
    ):
        """When a SearXNG backend engine dies the instance emits its own
        ``/stats`` pages inside ``results``; those must be dropped while
        the genuine hit alongside them survives."""
        engine = build_searxng(inert_tracker)
        payload = {
            "results": [
                {"url": "http://searx.invalid:8080/stats?engine=google"},
                {"url": "/preferences"},
                {"url": "https://example.org/real", "title": "Real"},
            ]
        }
        with patch.object(
            searxng_engine,
            "safe_get",
            return_value=StubResponse(200, payload=payload),
        ):
            previews = engine._get_previews(PROBE_QUERY)
        assert [p["link"] for p in previews] == ["https://example.org/real"]


class TestWikipediaDegradation:
    def test_success_control(self, inert_tracker):
        engine = build_wikipedia(inert_tracker)
        with (
            patch.object(
                wikipedia_engine.wikipedia,
                "search",
                return_value=["Pulmonary fibrosis"],
            ),
            patch.object(
                wikipedia_engine.wikipedia,
                "summary",
                return_value="A lung disease.",
            ),
        ):
            previews = engine._get_previews(PROBE_QUERY)
        assert len(previews) == 1
        assert previews[0]["title"] == "Pulmonary fibrosis"
        assert previews[0]["link"].endswith("/Pulmonary_fibrosis")

    @pytest.mark.parametrize(
        "failure",
        [
            # MediaWiki answers HTTP 429 with an HTML body, so the
            # `wikipedia` package surfaces a JSON decode error.
            pytest.param(
                json.JSONDecodeError("Expecting value", "<html>", 0),
                id="rate-limited",
            ),
            pytest.param(requests.Timeout("timed out"), id="timeout"),
            pytest.param(
                requests.ConnectionError("refused"), id="backend-down"
            ),
            pytest.param(requests.HTTPError("503"), id="http-error"),
        ],
    )
    def test_failure_yields_no_previews_without_raising(
        self, inert_tracker, failure
    ):
        engine = build_wikipedia(inert_tracker)
        with patch.object(
            wikipedia_engine.wikipedia, "search", side_effect=failure
        ):
            assert engine.run(PROBE_QUERY) == []
        assert engine.last_search_failure is not None

    def test_mid_batch_throttle_keeps_the_previews_already_collected(
        self, inert_tracker
    ):
        """Wikipedia fetches one summary per title. A 429 partway through
        must return the partial batch, not discard it."""
        engine = build_wikipedia(inert_tracker)
        with (
            patch.object(
                wikipedia_engine.wikipedia,
                "search",
                return_value=["First", "Second", "Third"],
            ),
            patch.object(
                wikipedia_engine.wikipedia,
                "summary",
                side_effect=[
                    "First summary",
                    json.JSONDecodeError("Expecting value", "<html>", 0),
                    "Third summary",
                ],
            ),
        ):
            previews = engine._get_previews(PROBE_QUERY)
        assert [p["title"] for p in previews] == ["First"]

    def test_single_bad_page_does_not_discard_its_siblings(self, inert_tracker):
        """A ``PageError`` on one title is skipped; the others survive."""
        engine = build_wikipedia(inert_tracker)
        page_error = wikipedia_engine.wikipedia.exceptions.PageError("Gone")
        with (
            patch.object(
                wikipedia_engine.wikipedia,
                "search",
                return_value=["Good", "Missing"],
            ),
            patch.object(
                wikipedia_engine.wikipedia,
                "summary",
                side_effect=["Good summary", page_error],
            ),
        ):
            previews = engine._get_previews(PROBE_QUERY)
        assert [p["title"] for p in previews] == ["Good"]

    @pytest.mark.parametrize(
        "page_failure",
        [
            pytest.param(AssertionError(), id="assertion-error"),
            pytest.param(KeyError("extract"), id="key-error"),
            pytest.param(
                wikipedia_engine.wikipedia.exceptions.PageError("Gone"),
                id="page-error",
            ),
            pytest.param(
                wikipedia_engine.wikipedia.exceptions.DisambiguationError(
                    "First", []
                ),
                id="disambiguation-without-options",
            ),
            pytest.param(
                wikipedia_engine.wikipedia.exceptions.WikipediaException(
                    "odd payload"
                ),
                id="wikipedia-exception",
            ),
        ],
    )
    def test_first_page_failure_skips_only_that_title(
        self, inert_tracker, page_failure
    ):
        """A page-specific failure on the first title must not stop the
        loop or cool the engine down: the later titles still come back."""
        engine = build_wikipedia(inert_tracker)
        with (
            patch.object(
                wikipedia_engine.wikipedia,
                "search",
                return_value=["First", "Second", "Third"],
            ),
            patch.object(
                wikipedia_engine.wikipedia,
                "summary",
                side_effect=[page_failure, "Second summary", "Third summary"],
            ) as summary,
        ):
            previews = engine._search_previews(PROBE_QUERY)
        assert [p["title"] for p in previews] == ["Second", "Third"]
        assert summary.call_count == 3
        assert engine.last_search_failure is None

    def test_every_page_failing_is_an_empty_search_not_an_outage(
        self, inert_tracker
    ):
        engine = build_wikipedia(inert_tracker)
        with (
            patch.object(
                wikipedia_engine.wikipedia,
                "search",
                return_value=["First", "Second"],
            ),
            patch.object(
                wikipedia_engine.wikipedia,
                "summary",
                side_effect=[AssertionError(), KeyError("extract")],
            ),
        ):
            assert engine._search_previews(PROBE_QUERY) == []
        assert engine.last_search_failure is None

    def test_provider_timeout_stops_the_batch_and_cools_down(
        self, inert_tracker
    ):
        """HTTPTimeoutError subclasses WikipediaException but is a provider
        failure: later titles are not requested and the failure is kept."""
        engine = build_wikipedia(inert_tracker)
        timeout = wikipedia_engine.wikipedia.exceptions.HTTPTimeoutError(
            "First"
        )
        with (
            patch.object(
                wikipedia_engine.wikipedia,
                "search",
                return_value=["First", "Second", "Third"],
            ),
            patch.object(
                wikipedia_engine.wikipedia,
                "summary",
                side_effect=[timeout, "Second summary", "Third summary"],
            ) as summary,
        ):
            assert engine._search_previews(PROBE_QUERY) == []
        assert summary.call_count == 1
        assert engine.last_search_failure is not None
        assert engine.last_search_failure.cooldown_seconds is not None


class TestArxivDegradation:
    def test_success_control(self, inert_tracker):
        engine = build_arxiv(inert_tracker)
        client = fake_arxiv_client(papers=[fake_arxiv_paper()])
        with patch.object(arxiv_engine, "fetch_arxiv_results", client):
            previews = engine._get_previews(PROBE_QUERY)
        assert len(previews) == 1
        assert previews[0]["title"] == "A Stubbed Paper"

    @pytest.mark.parametrize(
        "failure",
        [
            pytest.param(requests.Timeout("timed out"), id="timeout"),
            pytest.param(
                requests.ConnectionError("refused"), id="backend-down"
            ),
            pytest.param(ValueError("unparseable feed"), id="malformed-feed"),
        ],
    )
    def test_failure_yields_no_previews_without_raising(
        self, inert_tracker, failure
    ):
        engine = build_arxiv(inert_tracker)
        client = fake_arxiv_client(error=failure)
        with patch.object(arxiv_engine, "fetch_arxiv_results", client):
            assert engine.run(PROBE_QUERY) == []
        assert engine.last_search_failure is not None

    @pytest.mark.parametrize(
        "error,rate_limited,delay",
        [
            (requests.HTTPError(response=StubResponse(429)), True, 300),
            (requests.HTTPError(response=StubResponse(503)), False, 60),
            (Exception("rate limit hit"), True, 300),
        ],
        ids=["http-429", "http-503", "sdk-rate-limit"],
    )
    def test_provider_failures_keep_their_retry_policy(
        self, inert_tracker, error, rate_limited, delay
    ):
        """The common boundary preserves transport and SDK retry semantics."""
        engine = build_arxiv(inert_tracker)
        client = fake_arxiv_client(error=error)
        with patch.object(arxiv_engine, "fetch_arxiv_results", client):
            if rate_limited:
                with pytest.raises(RateLimitError):
                    engine._search_previews(PROBE_QUERY)
            else:
                assert engine._search_previews(PROBE_QUERY) == []
        assert engine.last_search_failure.cooldown_seconds == delay


PUBMED_OK_SEARCH = {"esearchresult": {"idlist": ["111", "222"], "count": "2"}}


class TestPubmedDegradation:
    def test_success_control(self, inert_tracker):
        engine = build_pubmed(inert_tracker)
        with patch.object(
            pubmed_engine,
            "safe_get",
            return_value=StubResponse(200, payload=PUBMED_OK_SEARCH),
        ):
            assert engine._search_pubmed(PROBE_QUERY) == ["111", "222"]

    @pytest.mark.parametrize(
        "stub_kwargs",
        [
            pytest.param({"return_value": StubResponse(503)}, id="http-503"),
            pytest.param({"return_value": StubResponse(429)}, id="http-429"),
            pytest.param(
                {
                    "return_value": StubResponse(
                        200,
                        json_exc=json.JSONDecodeError("bad", "<html>", 0),
                    )
                },
                id="malformed-json",
            ),
            pytest.param(
                {"return_value": StubResponse(200, payload={"oops": 1})},
                id="missing-esearchresult",
            ),
            pytest.param(
                {"side_effect": requests.Timeout("timed out")}, id="timeout"
            ),
            pytest.param(
                {"side_effect": requests.ConnectionError("refused")},
                id="backend-down",
            ),
        ],
    )
    def test_failure_yields_no_ids_without_raising(
        self, inert_tracker, stub_kwargs
    ):
        engine = build_pubmed(inert_tracker)
        with patch.object(pubmed_engine, "safe_get", **stub_kwargs):
            with patch.object(
                engine, "_get_previews", side_effect=engine._search_pubmed
            ):
                assert engine.run(PROBE_QUERY) == []
        assert engine.last_search_failure is not None

    def test_one_malformed_article_discards_the_whole_esummary_batch(
        self, inert_tracker
    ):
        """DEFECT (see report): ``_get_article_summaries`` builds every
        article inside a single try block and indexes ``author["name"]``
        directly, so one author entry missing ``name`` -- which NCBI does
        emit for collective authorships -- throws away every *other*
        article in the same response instead of skipping the one.

        The good-only control fixes the assertion to the real cause: the
        method does return summaries when the payload is clean.
        """
        engine = build_pubmed(inert_tracker)
        well_formed = {
            "title": "Well formed",
            "authors": [{"name": "A. Author"}],
            "fulljournalname": "Chest",
        }
        malformed = {
            "title": "Collective authorship",
            "authors": [{"collectivename": "The Study Group"}],
            "fulljournalname": "Chest",
        }

        with patch.object(
            pubmed_engine,
            "safe_get",
            return_value=StubResponse(
                200, payload={"result": {"111": well_formed}}
            ),
        ):
            control = engine._get_article_summaries(["111"])
        assert [s["id"] for s in control] == ["111"]

        with patch.object(
            pubmed_engine,
            "safe_get",
            return_value=StubResponse(
                200,
                payload={"result": {"111": well_formed, "222": malformed}},
            ),
        ):
            with patch.object(
                engine,
                "_get_previews",
                side_effect=lambda query: engine._get_article_summaries(
                    ["111", "222"]
                ),
            ):
                mixed = engine.run(PROBE_QUERY)
        assert engine.last_search_failure is not None
        assert mixed == [], (
            "current behaviour: the sibling article is lost too. When the "
            "per-article extraction is hardened, this should become "
            "['111'] (or ['111', '222'])."
        )


# --------------------------------------------------------------------------
# 3. Result normalisation across engines
# --------------------------------------------------------------------------


def _searxng_previews(inert_tracker):
    engine = build_searxng(inert_tracker)
    with patch.object(
        searxng_engine,
        "safe_get",
        return_value=StubResponse(200, payload=SEARXNG_OK_PAYLOAD),
    ):
        return engine._get_previews(PROBE_QUERY)


def _wikipedia_previews(inert_tracker):
    engine = build_wikipedia(inert_tracker)
    with (
        patch.object(
            wikipedia_engine.wikipedia, "search", return_value=["Fibrosis"]
        ),
        patch.object(
            wikipedia_engine.wikipedia, "summary", return_value="A disease."
        ),
    ):
        return engine._get_previews(PROBE_QUERY)


def _arxiv_previews(inert_tracker):
    engine = build_arxiv(inert_tracker)
    client = fake_arxiv_client(papers=[fake_arxiv_paper()])
    with patch.object(arxiv_engine, "fetch_arxiv_results", client):
        return engine._get_previews(PROBE_QUERY)


PREVIEW_PRODUCERS = {
    "searxng": _searxng_previews,
    "wikipedia": _wikipedia_previews,
    "arxiv": _arxiv_previews,
}


class TestResultNormalisation:
    @pytest.mark.parametrize("engine_name", sorted(PREVIEW_PRODUCERS))
    def test_every_engine_emits_the_same_preview_contract(
        self, inert_tracker, engine_name
    ):
        previews = PREVIEW_PRODUCERS[engine_name](inert_tracker)
        assert previews, f"{engine_name} produced no previews to check"
        for preview in previews:
            missing = REQUIRED_PREVIEW_KEYS - set(preview)
            assert not missing, (
                f"{engine_name} preview is missing {sorted(missing)}"
            )
            assert isinstance(preview["link"], str) and preview["link"]
            assert isinstance(preview["title"], str)

    @pytest.mark.parametrize("engine_name", sorted(PREVIEW_PRODUCERS))
    def test_previews_survive_the_real_downstream_link_extractor(
        self, inert_tracker, engine_name
    ):
        """The engines' own output is fed to the production consumer
        rather than to a hand-written copy of its expectations."""
        previews = PREVIEW_PRODUCERS[engine_name](inert_tracker)
        links = extract_links_from_search_results(previews)
        assert len(links) == len(previews)
        assert all(link["url"] and link["title"] for link in links)

    @pytest.mark.parametrize(
        "broken_preview",
        [
            pytest.param({"title": "No link key"}, id="missing-link"),
            pytest.param({"link": "https://example.org/x"}, id="missing-title"),
            pytest.param(
                {"title": None, "link": None}, id="null-title-and-link"
            ),
            pytest.param({}, id="empty-dict"),
        ],
    )
    def test_a_missing_engine_field_never_raises_keyerror_downstream(
        self, inert_tracker, broken_preview
    ):
        """A degraded preview must be dropped, not explode. Paired with a
        healthy preview in the same batch to prove the extractor is
        actually running rather than short-circuiting on an empty list.
        """
        healthy = {
            "id": "ok",
            "title": "Healthy",
            "link": "https://example.org/ok",
            "snippet": "s",
        }
        links = extract_links_from_search_results([healthy, broken_preview])
        assert [link["url"] for link in links] == ["https://example.org/ok"], (
            "the degraded preview should be dropped silently, not raise"
        )


# --------------------------------------------------------------------------
# 4. User-supplied engine base URLs are validated
# --------------------------------------------------------------------------


SEARXNG_URL_KEY = searxng_engine.SearXNGSearchEngine.url_setting


@pytest.fixture
def no_dns():
    """Poison name resolution so any accepted URL is proven DNS-free."""
    original = socket.getaddrinfo

    def _blocked(*args, **kwargs):
        raise AssertionError("test performed a DNS lookup")

    socket.getaddrinfo = _blocked
    try:
        yield
    finally:
        socket.getaddrinfo = original


class TestEngineBaseUrlValidation:
    def test_a_public_instance_url_is_accepted(self, no_dns):
        """Control: the rejections below are not a blanket refusal."""
        assert (
            egress_validators.validate_engine_instance_urls(
                {SEARXNG_URL_KEY: "https://93.184.216.34:8080"}, {}
            )
            is None
        )

    @pytest.mark.parametrize(
        ("url", "expected_fragment"),
        [
            pytest.param(
                "file:///etc/passwd", "http:// or https://", id="file-scheme"
            ),
            pytest.param(
                "ftp://mirror.example.com",
                "http:// or https://",
                id="ftp-scheme",
            ),
            pytest.param(
                "http://169.254.169.254/latest/meta-data/",
                "cloud-metadata",
                id="cloud-metadata",
            ),
            pytest.param(
                "http://127.0.0.1:8080",
                "private, loopback, or link-local",
                id="loopback",
            ),
            pytest.param(
                "http://10.1.2.3:8080",
                "private, loopback, or link-local",
                id="rfc1918",
            ),
            pytest.param("not a url", "illegal URL characters", id="not-a-url"),
        ],
    )
    def test_hostile_instance_urls_are_rejected_at_save_time(
        self, no_dns, url, expected_fragment
    ):
        error = egress_validators.validate_engine_instance_urls(
            {SEARXNG_URL_KEY: url}, {}
        )
        assert error is not None, f"{url!r} was accepted"
        assert error["key"] == SEARXNG_URL_KEY
        assert expected_fragment in error["error"]

    @pytest.mark.parametrize(
        "url",
        [
            "file:///etc/passwd",
            "http://169.254.169.254/latest/meta-data/",
            "http://127.0.0.1:8080/search",
        ],
    )
    def test_safe_get_refuses_before_any_socket_is_opened(self, url):
        """Second line of defence: even if a hostile URL reaches the HTTP
        helper, validation happens *before* the request is issued."""
        with patch.object(
            safe_requests.requests,
            "get",
            side_effect=AssertionError("a request was issued"),
        ):
            with pytest.raises(ValueError, match="security validation"):
                safe_requests.safe_get(url, timeout=1)

    def test_result_format_and_safe_search_fall_back_on_bad_config(
        self, inert_tracker
    ):
        """User-supplied enum-ish settings are normalised rather than
        crashing the engine at construction."""
        with patch.object(
            searxng_engine,
            "safe_get",
            return_value=StubResponse(200, "<html/>"),
        ):
            engine = searxng_engine.SearXNGSearchEngine(
                instance_url="http://searx.invalid:8080/",
                result_format="xml",
                safe_search="NOT_A_LEVEL",
                llm=None,
            )
        assert engine.result_format == "html"
        assert engine.safe_search is searxng_engine.SafeSearchSetting.OFF
        assert engine.instance_url == "http://searx.invalid:8080", (
            "trailing slash must be stripped so self-URL filtering matches"
        )


# --------------------------------------------------------------------------
# 5. Adaptive per-engine rate limiter
# --------------------------------------------------------------------------


@pytest.fixture
def tracker_clock(monkeypatch):
    clock = FakeClock()
    monkeypatch.setattr(tracker_mod, "time", clock)
    # No ambient settings context in a unit test: pin the tracker to its
    # own learning_rate / memory_window so the arithmetic is exact.
    monkeypatch.setattr(tracker_mod, "get_settings_context", lambda: None)
    return clock


def make_tracker():
    return tracker_mod.AdaptiveRateLimitTracker(
        settings_snapshot={"rate_limiting.enabled": {"value": True}},
        programmatic_mode=True,
    )


class TestAdaptiveRateLimiter:
    def test_disabled_tracker_neither_waits_nor_learns(self, tracker_clock):
        # Programmatic mode defaults rate limiting off; contrast with
        # ``make_tracker()``, which opts it back on via the snapshot.
        tracker = tracker_mod.AdaptiveRateLimitTracker(
            settings_snapshot={},
            programmatic_mode=True,
        )
        assert tracker.enabled is False
        assert tracker.apply_rate_limit("Engine") == 0.0
        for _ in range(3):
            tracker.record_outcome("Engine", 5.0, success=False, retry_count=1)
        assert "Engine" not in tracker.current_estimates
        assert tracker_clock.slept == []

    def test_unknown_engine_starts_optimistic(self, tracker_clock):
        tracker = make_tracker()
        assert tracker.get_wait_time("NeverSeenEngine") == pytest.approx(0.1)

    def test_successes_teach_the_median_wait_and_derive_bounds(
        self, tracker_clock
    ):
        tracker = make_tracker()
        for wait in (1.0, 2.0, 3.0):
            tracker.record_outcome("Engine", wait, success=True, retry_count=1)
        estimate = tracker.current_estimates["Engine"]
        # Median of the successful waits, not the fastest one.
        assert estimate["base"] == pytest.approx(2.0)
        assert estimate["min"] == pytest.approx(1.0)
        assert estimate["max"] == pytest.approx(6.0)

    def test_repeated_failure_backs_off_above_the_failing_wait(
        self, tracker_clock
    ):
        tracker = make_tracker()
        for _ in range(3):
            tracker.record_outcome(
                "Engine",
                2.0,
                success=False,
                retry_count=1,
                error_type="RateLimitError",
            )
        # No success to learn from: back off to 1.5x the failing wait.
        assert tracker.current_estimates["Engine"]["base"] == pytest.approx(3.0)

    def test_backoff_is_capped_so_it_cannot_run_away(self, tracker_clock):
        tracker = make_tracker()
        for _ in range(3):
            tracker.record_outcome("Engine", 60.0, success=False, retry_count=1)
        estimate = tracker.current_estimates["Engine"]
        assert estimate["base"] == pytest.approx(10.0)
        assert estimate["max"] == pytest.approx(10.0)

    def test_estimate_moves_toward_a_slower_reality_one_step_at_a_time(
        self, tracker_clock
    ):
        """The estimate is an exponential moving average re-evaluated on
        every recorded outcome, so a step change in the backend's real
        pacing is approached asymptotically rather than adopted at once.
        That damping is the property that stops a single slow response
        from spiking every subsequent wait."""
        tracker = make_tracker()
        for wait in (1.0, 2.0, 3.0):
            tracker.record_outcome("Engine", wait, success=True, retry_count=1)
        assert tracker.current_estimates["Engine"]["base"] == pytest.approx(2.0)

        tracker.learning_rate = 0.5
        # Window medians as 6.0s samples arrive: [1,2,3,6] -> 2.0,
        # [1,2,3,6,6] -> 3.0, [1,2,3,6,6,6] -> 3.0. Each is blended
        # 50/50 with the running estimate.
        expected = [2.0, 2.5, 2.75]
        observed = []
        for _ in range(3):
            tracker.record_outcome("Engine", 6.0, success=True, retry_count=1)
            observed.append(tracker.current_estimates["Engine"]["base"])
        assert observed == pytest.approx(expected)
        # Never overshoots the slowest observation it is converging on.
        assert observed[-1] < 6.0

    def test_applied_wait_respects_learned_bounds_and_is_slept_exactly_once(
        self, tracker_clock, monkeypatch
    ):
        tracker = make_tracker()
        for wait in (1.0, 2.0, 3.0):
            tracker.record_outcome("Engine", wait, success=True, retry_count=1)

        # Force the exploit branch with no jitter for a fixed expectation.
        fake_random = SimpleNamespace(
            random=lambda: 1.0, uniform=lambda lo, hi: 1.0
        )
        monkeypatch.setattr(tracker_mod, "random", fake_random)
        applied = tracker.apply_rate_limit("Engine")
        assert applied == pytest.approx(2.0)
        assert tracker_clock.slept == [pytest.approx(2.0)]

        # Exploration branch probes a *faster* rate, still inside bounds.
        tracker_clock.slept.clear()
        fake_random.random = lambda: 0.0
        fake_random.uniform = lambda lo, hi: lo
        explored = tracker.apply_rate_limit("Engine")
        estimate = tracker.current_estimates["Engine"]
        assert explored < applied
        assert estimate["min"] <= explored <= estimate["max"]
        assert tracker_clock.slept == [pytest.approx(explored)]


# --------------------------------------------------------------------------
# 6. SearXNG per-instance-URL throttle -- issue #5748
# --------------------------------------------------------------------------


@pytest.fixture
def throttle_clock(monkeypatch):
    """Fake clock + small capacity for the SearXNG URL throttle."""
    clock = FakeClock()
    monkeypatch.setattr(searxng_throttle, "time", clock)
    monkeypatch.setattr(searxng_throttle, "MAX_TRACKED_URLS", 4)
    searxng_throttle.reset_for_tests()
    try:
        yield clock
    finally:
        searxng_throttle.reset_for_tests()


DELAY = 5.0


class TestSearxngUrlThrottleEviction:
    """Issue #5748: the throttle is keyed by instance URL in a bounded
    map. A brand-new entry carries ``last_request == 0.0``, meaning "no
    request recorded yet" -- see ``_eviction_sort_key`` -- which sorts
    *last*, not first, in the eviction order: an in-flight request whose
    timestamp has not been recorded yet is not the preferred
    eviction victim just for lacking a timestamp.

    What is pinned here is the harm eviction still causes despite that
    ordering rule -- the configured delay can silently stop applying to a
    genuinely idle-but-recently-used instance -- and the exact ordering
    rule that decides which entries go.
    """

    def test_repeat_request_to_one_instance_is_throttled(self, throttle_clock):
        """Baseline control for every assertion below: with the entry
        intact the second call really does wait."""
        searxng_throttle.respect_rate_limit("http://searx.invalid", DELAY)
        assert throttle_clock.slept == [], "first call must not wait"

        throttle_clock.advance(1.0)
        searxng_throttle.respect_rate_limit("http://searx.invalid", DELAY)
        assert throttle_clock.slept == [pytest.approx(DELAY - 1.0)]

    def test_a_zero_delay_never_touches_the_tracker(self, throttle_clock):
        searxng_throttle.respect_rate_limit("http://searx.invalid", 0.0)
        searxng_throttle.respect_rate_limit("http://searx.invalid", -1.0)
        assert searxng_throttle._url_state == {}
        assert throttle_clock.slept == []

    def test_a_new_entry_sorts_last_and_is_not_evicted_ahead_of_live_ones(
        self, throttle_clock
    ):
        """The ordering key is ``_eviction_sort_key``, and an in-flight
        entry (state created, request not yet recorded) reads
        ``last_request == 0.0`` -- which the key maps to ``+inf``, so it
        sorts after every real monotonic timestamp, not before, and is
        never the preferred eviction victim."""
        for index in range(3):
            searxng_throttle.respect_rate_limit(
                f"http://live{index}.invalid", DELAY
            )
            throttle_clock.advance(DELAY)

        # A caller that has obtained its state but not yet completed a
        # request: present in the map, unlocked, timestamp still 0.0.
        in_flight = "http://in-flight.invalid"
        state = searxng_throttle._get_url_state(in_flight)
        assert state.last_request == 0.0
        assert not state.lock.locked()
        assert len(searxng_throttle._url_state) == 4

        live_timestamps = [
            searxng_throttle._url_state[f"http://live{i}.invalid"].last_request
            for i in range(3)
        ]
        assert all(ts > 0.0 for ts in live_timestamps)

        # Reaching capacity evicts the oldest half by timestamp -> the
        # in-flight entry (no timestamp, sorts last) survives, and the
        # two genuinely oldest *live* entries go instead.
        searxng_throttle.respect_rate_limit("http://newcomer.invalid", DELAY)
        assert in_flight in searxng_throttle._url_state
        assert "http://live0.invalid" not in searxng_throttle._url_state
        assert "http://live1.invalid" not in searxng_throttle._url_state
        assert "http://live2.invalid" in searxng_throttle._url_state

    def test_an_evicted_instance_silently_loses_its_configured_throttle(
        self, throttle_clock
    ):
        """The harm behind #5748. Eviction is purely capacity-driven, with
        no floor on how recently an entry was used, so an instance that
        was queried moments ago can be dropped. Its timestamp goes with
        it, and the next call rebuilds the entry at ``last_request ==
        0.0`` -- the "first request for this URL" state -- so the
        configured delay silently does not apply.

        Contrast with ``test_repeat_request_to_one_instance_is_throttled``
        above: identical call pattern, identical delay, and the only
        difference is the eviction in between. Driven entirely through
        the public ``respect_rate_limit`` API so the assertion tracks the
        real code path.
        """
        victim = "http://searx.invalid"
        searxng_throttle.respect_rate_limit(victim, DELAY)
        stamped = searxng_throttle._url_state[victim]
        assert stamped.last_request > 0.0

        # Traffic to other instances pushes the tracker to capacity. The
        # victim was used 0.5s ago -- well inside its own delay window.
        for index in range(5):
            throttle_clock.advance(0.1)
            searxng_throttle.respect_rate_limit(
                f"http://other{index}.invalid", DELAY
            )
        assert victim not in searxng_throttle._url_state

        throttle_clock.slept.clear()
        searxng_throttle.respect_rate_limit(victim, DELAY)
        assert throttle_clock.slept == [], (
            "the delay for this instance was lost by eviction"
        )
        rebuilt = searxng_throttle._url_state[victim]
        assert rebuilt is not stamped
        assert rebuilt.last_request > 0.0

    def test_a_tracked_url_never_triggers_eviction(self, throttle_clock):
        """Eviction runs only when a *new* key is inserted, so a tracker
        already at capacity full of live instances does not shed entries
        just because they keep being used."""
        urls = [f"http://live{index}.invalid" for index in range(4)]
        for url in urls:
            searxng_throttle.respect_rate_limit(url, DELAY)
            throttle_clock.advance(DELAY)
        assert len(searxng_throttle._url_state) == 4

        for url in urls:
            throttle_clock.advance(DELAY)
            searxng_throttle.respect_rate_limit(url, DELAY)
        assert set(searxng_throttle._url_state) == set(urls)

    def test_an_entry_whose_lock_is_held_is_not_evictable(self, throttle_clock):
        """The held-lock guard is what keeps a *sleeping* caller's delay
        from being dropped -- the guard the in-flight window misses."""
        held = "http://busy.invalid"
        state = searxng_throttle._get_url_state(held)
        # A genuine old timestamp, not 0.0: 0.0 now means "in flight" and
        # sorts *last* for eviction (see _eviction_sort_key), so it would
        # no longer be the entry eviction most wants to reclaim. An old
        # real timestamp is, which is what this test needs to show the
        # held-lock guard -- not the ordering -- is what saves it.
        state.last_request = 1.0
        state.lock.acquire()
        try:
            for index in range(5):
                searxng_throttle.respect_rate_limit(
                    f"http://other{index}.invalid", DELAY
                )
            assert searxng_throttle._url_state.get(held) is state
        finally:
            state.lock.release()


# --------------------------------------------------------------------------
# 7. Research queries in logs -- issue #5734
# --------------------------------------------------------------------------


def _run_searxng_for_logs(inert_tracker):
    engine = build_searxng(inert_tracker)
    with patch.object(
        searxng_engine,
        "safe_get",
        return_value=StubResponse(200, payload={"results": []}),
    ):
        engine._get_previews(PROBE_QUERY)


def _run_wikipedia_for_logs(inert_tracker):
    engine = build_wikipedia(inert_tracker)
    with patch.object(wikipedia_engine.wikipedia, "search", return_value=[]):
        engine._get_previews(PROBE_QUERY)


def _run_pubmed_for_logs(inert_tracker):
    engine = build_pubmed(inert_tracker)
    with patch.object(
        pubmed_engine,
        "safe_get",
        return_value=StubResponse(
            200, payload={"esearchresult": {"idlist": [], "count": "0"}}
        ),
    ):
        engine._search_pubmed(PROBE_QUERY)


def _run_arxiv_for_logs(inert_tracker):
    engine = build_arxiv(inert_tracker)
    with patch.object(
        arxiv_engine, "fetch_arxiv_results", fake_arxiv_client(papers=[])
    ):
        engine._get_previews(PROBE_QUERY)


class TestResearchQueryIsNotPersistedToLogs:
    """#5734: research queries are sensitive. ``config_logger`` attaches
    ``database_sink`` at INFO, so anything an adapter logs at INFO or
    above is written verbatim into the user's encrypted ``app_logs``
    table and survives the run.
    """

    def test_database_sink_is_installed_at_info_level(self):
        """Pins the premise the tests below rely on."""
        recorder = MagicMock()
        with patch.object(log_utils, "logger", recorder):
            log_utils.config_logger("test-app", debug=False)

        db_levels = [
            call.kwargs.get("level")
            for call in recorder.add.call_args_list
            if call.args and call.args[0] is log_utils.database_sink
        ]
        assert db_levels == ["INFO"], (
            "database_sink must be registered exactly once, at INFO"
        )

    def test_the_leak_detector_reports_clean_for_an_engine_that_is_clean(
        self, inert_tracker, captured_logs
    ):
        """Negative control for the whole section: arXiv logs a constant
        string instead of the query, and the detector says so. Without
        this the assertions below could be passing on a broken detector."""
        _run_arxiv_for_logs(inert_tracker)
        assert captured_logs.records, "no records captured -- sink is dead"
        assert captured_logs.messages_containing(PROBE_QUERY) == []

    @pytest.mark.parametrize(
        "runner",
        [
            pytest.param(_run_searxng_for_logs, id="searxng"),
            pytest.param(_run_wikipedia_for_logs, id="wikipedia"),
            pytest.param(_run_pubmed_for_logs, id="pubmed"),
        ],
    )
    def test_adapters_must_not_log_the_query_at_database_sink_level(
        self, inert_tracker, captured_logs, runner
    ):
        """#5646 removes raw-query logging; these regressions now pass normally."""
        runner(inert_tracker)
        assert captured_logs.records, "no records captured -- sink is dead"
        leaks = captured_logs.messages_containing(PROBE_QUERY)
        assert leaks == [], (
            "the research query reached the database sink at: "
            + ", ".join(sorted({f"{lvl} {name}" for lvl, name, _ in leaks}))
        )