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.

63.8 KB
Raw Blame
"""Contracts the LLM provider adapters must hold at construction and call time.

Scope: ``config/llm_config.py`` and ``llm/providers/`` — provider selection,
base-URL handling, credential plumbing, and error paths.

The provider layer is byte-identical to ``main`` on this branch (``git diff
origin/main..HEAD -- src/local_deep_research/llm/`` is empty), so everything
asserted here is a standing property of the adapters, not a port regression.
The *request boundaries* that feed them (``web/routers/``) are new, and one
of them is where the gate census below finds a hole.

Five properties, each with a positive control that proves the path under
test actually executed:

1. **Credentials never reach a log line.** A sentinel key is planted in
   settings, the construction path is driven for real, and the *positive
   control* asserts the sentinel really did reach the SDK constructor
   (``Authorization: Bearer <sentinel>`` / ``api_key=<sentinel>``) before
   the absence assertion is made against the captured log text. Without
   that half, ``assert sentinel not in log`` passes trivially whenever the
   path never ran.

2. **Every operator-configurable base URL is SSRF-gated.** Two censuses:
   an executed one that drives *every auto-discovered provider* that
   declares a ``url_setting`` against a cloud-metadata IP (with a
   localhost positive control proving the constructor is reachable), and
   a static one over the provider modules so a *new* provider that reads
   a URL setting without a gate fails this file. A third census covers
   the request boundary (``is_safe_custom_llm_endpoint``).

3. **``normalize_provider`` is applied wherever a provider string
   arrives** — and is total over what actually arrives there.

4. **Distinct failures produce distinct, actionable errors.**

5. **The token-counting callback belongs to exactly one research/user.**

Tests marked ``xfail(strict=True)`` assert the *contract*, not today's
behavior: each is a defect that flips this file green when fixed. They are
listed in the report accompanying this file.

No network and no real LLM: every SDK constructor is patched at the module
that binds it, and the only mocked thing on the metrics path is the
database writer.
"""

import ast
import asyncio
import functools
import inspect
import re
import sys
import threading
from contextlib import ExitStack, contextmanager
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest.mock import MagicMock, patch

import pytest
from langchain_core.callbacks import BaseCallbackHandler, CallbackManager
from langchain_core.language_models.fake_chat_models import FakeListChatModel
from langchain_core.runnables import RunnableLambda, ensure_config
from loguru import logger as loguru_logger
from pydantic import Field

from local_deep_research.llm.providers import discover_providers
from local_deep_research.llm.providers.base import normalize_provider

# Obviously-fake sentinel. Not a token shape any scrubber recognises by
# pattern (no ``sk-``/``AIza``/JWT prefix), which is the point: it stands in
# for the arbitrary strings operators give self-hosted backends (vLLM,
# LM Studio behind an auth proxy, Ollama behind a gateway).
FAKE_KEY = "ldrfake-testonly-A1b2C3d4E5f6G7h8"  # gitleaks:allow

METADATA_URL = "http://169.254.169.254/v1"
LOCAL_URL = "http://localhost:11434/v1"

# SDK constructors are bound at import time in the module that uses them,
# so these are the only places a client can be built.
OLLAMA_CHAT = (
    "local_deep_research.llm.providers.implementations.ollama.ChatOllama"
)
ANTHROPIC_CHAT = (
    "local_deep_research.llm.providers.implementations.anthropic.ChatAnthropic"
)
OPENAI_CHAT = (
    "local_deep_research.llm.providers.implementations.openai.ChatOpenAI"
)
OPENAI_BASE_CHAT = "local_deep_research.llm.providers.openai_base.ChatOpenAI"
ALL_CLIENT_TARGETS = (
    OLLAMA_CHAT,
    ANTHROPIC_CHAT,
    OPENAI_CHAT,
    OPENAI_BASE_CHAT,
)

SRC = Path(__file__).resolve().parents[2] / "src" / "local_deep_research"


# --------------------------------------------------------------------------
# fixtures / helpers
# --------------------------------------------------------------------------


class _LogCapture:
    """Collect formatted loguru records so absence can be asserted."""

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

    def __call__(self, message):
        record = message.record
        self.lines.append(f"{record['level'].name} {record['message']}")

    @property
    def text(self):
        return "\n".join(self.lines)


@pytest.fixture
def captured_logs():
    """Capture at DEBUG — the widest sink any deployment can install."""
    capture = _LogCapture()
    loguru_logger.enable("local_deep_research")
    sink_id = loguru_logger.add(capture, level="DEBUG")
    try:
        yield capture
    finally:
        loguru_logger.remove(sink_id)


@pytest.fixture(autouse=True, scope="module")
def _providers_registered():
    """Another module's ``clear_llm_registry()`` must not empty discovery."""
    discover_providers(force_refresh=True)


def _snapshot(**overrides):
    """Permissive snapshot: settings resolution runs for real from here.

    ``search.tool`` is required — ``get_llm``'s egress PEP calls
    ``resolve_run_primary_engine`` and fails closed without a primary.
    """
    snap = {
        "llm.supports_max_tokens": True,
        "llm.max_tokens": 4096,
        "llm.context_window_unrestricted": True,
        "llm.local_context_window_size": 8192,
        "rate_limiting.llm_enabled": False,
        "search.tool": "searxng",
    }
    snap.update(overrides)
    return snap


def _url_configurable_providers():
    """Auto-discovered providers whose base URL an operator can edit."""
    out = []
    for key, info in sorted(discover_providers().items()):
        if getattr(info.provider_class, "url_setting", None):
            out.append(pytest.param(info.provider_class, id=key))
    return out


def _provider_snapshot(cls, url):
    snap = _snapshot(**{cls.url_setting: url, "llm.model": "test-model"})
    if cls.api_key_setting:
        snap[cls.api_key_setting] = FAKE_KEY
    return snap


def _patch_all_clients(stack, recorder):
    for target in ALL_CLIENT_TARGETS:
        stack.enter_context(patch(target, recorder))


def _mro_source(cls):
    """Source of every first-party module in ``cls``'s MRO.

    A subclass may inherit its SSRF gate (LM Studio and llama.cpp build
    their client in ``OpenAICompatibleProvider._create_llm_instance``), so
    the gate must be looked for across the whole chain, not per file.
    """
    seen, chunks = set(), []
    for klass in cls.__mro__:
        module_name = getattr(klass, "__module__", "")
        if not module_name.startswith("local_deep_research"):
            continue
        if module_name in seen:
            continue
        seen.add(module_name)
        module = sys.modules.get(module_name)
        if module is None:
            continue
        try:
            chunks.append(inspect.getsource(module))
        except OSError:  # pragma: no cover - source always available here
            continue
    return "\n".join(chunks)


class _Boom(RuntimeError):
    """Provider-shaped failure carrying a planted credential."""

    def __init__(self, message, response=None):
        super().__init__(message)
        if response is not None:
            self.response = response


# --------------------------------------------------------------------------
# 1. credentials on the construction path
# --------------------------------------------------------------------------


class TestCredentialsAtConstruction:
    def test_ollama_key_becomes_a_bearer_header_and_is_never_logged(
        self, captured_logs
    ):
        """Ollama behind an auth proxy: key -> header, not -> log.

        Positive control: the sentinel really is handed to ``ChatOllama``
        as ``Authorization: Bearer <sentinel>``, and the log really did
        capture this call (the base_url line is present). Only then is the
        absence of the sentinel meaningful.

        ChatOllama forwards client_kwargs to both HTTP clients. Its model
        ignores unsupported top-level headers, so the positive control must
        inspect the shared transport options.
        """
        from local_deep_research.llm.providers.implementations.ollama import (
            OllamaProvider,
        )

        snap = _snapshot(
            **{
                "llm.ollama.url": "http://localhost:11434",
                "llm.ollama.api_key": FAKE_KEY,
            }
        )
        with patch(OLLAMA_CHAT) as chat:
            OllamaProvider.create_llm(
                model_name="llama3.1:8b",
                temperature=0.2,
                settings_snapshot=snap,
            )

        kwargs = chat.call_args.kwargs
        # positive control (a): the credential reached the client.
        assert kwargs["client_kwargs"]["headers"] == {
            "Authorization": f"Bearer {FAKE_KEY}"
        }
        assert "headers" not in kwargs
        # positive control (b): this call's logging reached the sink.
        assert "http://localhost:11434" in captured_logs.text
        # the property under test.
        assert FAKE_KEY not in captured_logs.text

    def test_openai_endpoint_logs_the_endpoint_but_not_the_key(
        self, captured_logs
    ):
        """``openai_base.create_llm`` logs model + endpoint on every build."""
        from local_deep_research.llm.providers.implementations.custom_openai_endpoint import (  # noqa: E501
            CustomOpenAIEndpointProvider,
        )

        snap = _provider_snapshot(
            CustomOpenAIEndpointProvider, "http://localhost:1234/v1"
        )
        with patch(OPENAI_BASE_CHAT) as chat:
            CustomOpenAIEndpointProvider.create_llm(
                model_name="local-model",
                temperature=0.1,
                settings_snapshot=snap,
            )

        # positive control (a): the credential reached the client.
        assert chat.call_args.kwargs["api_key"] == FAKE_KEY
        # positive control (b): the info line for this build was captured.
        assert "http://localhost:1234/v1" in captured_logs.text
        assert "local-model" in captured_logs.text
        # the property under test.
        assert FAKE_KEY not in captured_logs.text

    @pytest.mark.parametrize(
        "shape",
        [
            "Authorization: Bearer {key}",
            "x-api-key: {key}",
            "GET https://api.example/v1/models?api_key={key} -> 401",
            "invalid token {key}",
        ],
    )
    def test_model_listing_error_redacts_the_key_in_any_shape(
        self, monkeypatch, captured_logs, shape
    ):
        """``list_models`` knows the literal key, so shape does not matter.

        This is the dual-scrub the codebase documents: shape-based
        ``sanitize_error_message`` *plus* ``redact_secrets`` with the known
        literal — which is why even the last, shapeless variant is safe
        here. Contrast ``TestCredentialsAtCallTime`` below, where the
        literal is not available and only the shape pass runs.
        """
        from local_deep_research.llm.providers.implementations.openai import (
            OpenAIProvider,
        )

        def _boom(cls, api_key=None, base_url=None):
            raise _Boom(shape.format(key=FAKE_KEY))

        monkeypatch.setattr(
            OpenAIProvider, "list_models_for_api", classmethod(_boom)
        )
        snap = _snapshot(**{"llm.openai.api_key": FAKE_KEY})

        models = OpenAIProvider.list_models(settings_snapshot=snap)

        assert models == []
        # positive control: the failing branch is the one that logged.
        assert "Error listing models from" in captured_logs.text
        assert FAKE_KEY not in captured_logs.text

    def test_model_listing_refuses_a_non_string_credential(self, captured_logs):
        """A dict api_key would be str()'d into the Authorization header.

        The OpenAI SDK coerces whatever it is given, so a mis-typed
        credential would ship the dict's contents to the endpoint being
        listed. The guard must refuse *and* not log the value.
        """
        from local_deep_research.llm.providers.implementations.openai import (
            OpenAIProvider,
        )

        models = OpenAIProvider.list_models_for_api(
            api_key={"Authorization": f"Bearer {FAKE_KEY}"},
            base_url="https://api.openai.com/v1",
        )

        assert models == []
        # positive control: the refusal branch ran and named the bad type.
        assert "non-string api_key of type dict" in captured_logs.text
        assert FAKE_KEY not in captured_logs.text


# --------------------------------------------------------------------------
# 2. credentials on the inference path (per-call error handling)
# --------------------------------------------------------------------------


def _wrapper_over(failing_call):
    """A ``ProcessingLLMWrapper`` whose base LLM raises on every entry."""
    from local_deep_research.config.llm_config import ProcessingLLMWrapper

    base = MagicMock()
    base.invoke.side_effect = failing_call
    base.stream.side_effect = failing_call
    return ProcessingLLMWrapper(base)


class TestCredentialsAtCallTime:
    def test_invoke_failure_redacts_a_bearer_header(self, captured_logs):
        """Positive control for the whole ``_log_llm_error`` path.

        Proves the wrapper catches, scrubs and logs — so a *miss* in the
        next test is a gap in the scrubber's reach, not a dead path.
        """
        planted = _Boom(
            "502 from gateway: "
            f"POST /v1/chat/completions Authorization: Bearer {FAKE_KEY}"
        )
        wrapper = _wrapper_over(planted)

        with pytest.raises(_Boom):
            wrapper.invoke("hello")

        assert "LLM Request - Failed with error" in captured_logs.text
        assert "Bearer [REDACTED]" in captured_logs.text
        assert FAKE_KEY not in captured_logs.text

    @pytest.mark.xfail(
        strict=True,
        reason=(
            "DEFECT: llm_config._log_llm_error calls scrub_error(error) with "
            "no known-literal secret, so only the shape pass runs. A "
            "self-hosted backend's key has no recognisable shape and its "
            "echo survives into the log. Every other credential-handling "
            "catch site in the provider layer passes the key "
            "(redact_secrets(str(e), api_key)); this one — on the hottest "
            "path, every LLM call — does not."
        ),
    )
    def test_invoke_failure_redacts_a_self_hosted_token(self, captured_logs):
        """Same path, credential in a shape the pattern table lacks.

        Modeled on a gateway that echoes the rejected request. ``api-key:``
        (the header Azure OpenAI uses) is not in ``_CREDENTIAL_PATTERNS``,
        and self-hosted tokens carry no ``sk-``-style prefix to anchor on.
        """
        planted = _Boom(
            "401 from gateway: rejected request "
            f"{{'headers': {{'api-key': '{FAKE_KEY}'}}}}"
        )
        wrapper = _wrapper_over(planted)

        with pytest.raises(_Boom):
            wrapper.invoke("hello")

        # positive control: the path ran (same line as the test above).
        assert "LLM Request - Failed with error" in captured_logs.text
        assert FAKE_KEY not in captured_logs.text

    def test_invoke_reraises_the_original_exception_object(self):
        """Identity must survive: retry/rate-limit classification reads
        ``type(error)`` and ``error.response.status_code`` off the object
        the wrapper re-raises."""
        planted = _Boom("boom", response=SimpleNamespace(status_code=429))
        wrapper = _wrapper_over(planted)

        with pytest.raises(_Boom) as excinfo:
            wrapper.invoke("hello")

        assert excinfo.value is planted
        assert excinfo.value.response.status_code == 429

    def test_stream_failure_is_scrub_logged_like_invoke(self, captured_logs):
        planted = _Boom(f"502 from gateway: Authorization: Bearer {FAKE_KEY}")
        wrapper = _wrapper_over(planted)

        with pytest.raises(_Boom):
            list(wrapper.stream("hello"))

        assert "LLM Request - Failed with error" in captured_logs.text
        assert FAKE_KEY not in captured_logs.text


# --------------------------------------------------------------------------
# 3. base-URL safety
# --------------------------------------------------------------------------


class TestBaseUrlGateExecuted:
    """Drive the real construction path of every URL-configurable provider."""

    @pytest.mark.parametrize("provider_cls", _url_configurable_providers())
    def test_metadata_base_url_never_reaches_the_client(self, provider_cls):
        """169.254.169.254 is cloud-credential territory: refuse to build.

        The SDKs use their own httpx transport, so ``safe_requests`` cannot
        help — the only defence is this gate at construction.
        """
        snap = _provider_snapshot(provider_cls, METADATA_URL)
        recorder = MagicMock(
            side_effect=AssertionError(
                f"{provider_cls.__name__} built a client for a metadata URL"
            )
        )

        with ExitStack() as stack:
            _patch_all_clients(stack, recorder)
            with pytest.raises(ValueError, match="failed SSRF validation"):
                provider_cls.create_llm(
                    model_name="test-model",
                    temperature=0.1,
                    settings_snapshot=snap,
                )

        assert recorder.call_count == 0

    @pytest.mark.parametrize("provider_cls", _url_configurable_providers())
    def test_localhost_base_url_does_reach_the_client(self, provider_cls):
        """POSITIVE CONTROL for the test above.

        Without this, a provider whose ``create_llm`` failed early for an
        unrelated reason (missing key, missing model) would look "gated".
        Localhost is also the real deployment for Ollama / LM Studio /
        llama.cpp, so this doubles as a no-false-positive check.
        """
        snap = _provider_snapshot(provider_cls, LOCAL_URL)
        recorder = MagicMock(return_value=FakeListChatModel(responses=["ok"]))

        with ExitStack() as stack:
            _patch_all_clients(stack, recorder)
            provider_cls.create_llm(
                model_name="test-model",
                temperature=0.1,
                settings_snapshot=snap,
            )

        assert recorder.call_count == 1
        built_url = recorder.call_args.kwargs["base_url"]
        assert "localhost" in str(built_url)

    def test_the_census_covers_more_than_one_provider(self):
        """A census that silently shrank to zero proves nothing."""
        covered = {p.id for p in _url_configurable_providers()}
        assert {
            "OLLAMA",
            "LMSTUDIO",
            "LLAMACPP",
            "OPENAI_ENDPOINT",
            "ANTHROPIC_ENDPOINT",
        } <= covered


_URLISH_KEY = re.compile(r"^llm\.[a-z0-9_]+\.(url|api_base|base_url)$")


def _urlish_setting_keys(source):
    """Settings keys in *source* that name an operator-editable URL."""
    return {
        node.value
        for node in ast.walk(ast.parse(source))
        if isinstance(node, ast.Constant)
        and isinstance(node.value, str)
        and _URLISH_KEY.match(node.value)
    }


def _reads_url_without_gate(module_source, mro_source):
    """True when a module reads a URL setting no MRO source gates."""
    if not _urlish_setting_keys(module_source):
        return False
    return "assert_base_url_safe" not in mro_source


class TestBaseUrlGateStatic:
    """A *new* provider that reads a URL setting must not slip the gate."""

    def test_detector_flags_an_ungated_module(self):
        """POSITIVE CONTROL for ``_reads_url_without_gate``."""
        ungated = 'URL = get_setting("llm.newthing.url")\nChatOpenAI(URL)\n'
        assert _reads_url_without_gate(ungated, ungated) is True

    def test_detector_accepts_an_inherited_gate(self):
        """NEGATIVE CONTROL: the gate may live in a base class's module."""
        child = 'URL = get_setting("llm.newthing.url")\n'
        parent = "def build(u):\n    return assert_base_url_safe(u)\n"
        assert _reads_url_without_gate(child, child + parent) is False

    def test_detector_ignores_a_module_with_no_url_setting(self):
        """NEGATIVE CONTROL: no URL read, nothing to gate."""
        source = 'KEY = get_setting("llm.newthing.api_key")\n'
        assert _reads_url_without_gate(source, source) is False

    def test_every_provider_reading_a_url_setting_has_a_gate(self):
        offenders = []
        for key, info in sorted(discover_providers().items()):
            cls = info.provider_class
            module = sys.modules[cls.__module__]
            source = inspect.getsource(module)
            if _reads_url_without_gate(source, _mro_source(cls)):
                keys = sorted(_urlish_setting_keys(source))
                offenders.append((key, keys))

        assert offenders == [], (
            "provider(s) read an operator-editable URL setting with no "
            f"assert_base_url_safe anywhere in their MRO: {offenders}"
        )


def _boundary_census(source):
    """(passes_endpoint, gates_endpoint) for one router module.

    Module granularity on purpose: ``research.py`` gates inside
    ``_extract_research_params`` and starts the run from a different
    function, so a per-function check would report a false positive.
    """
    tree = ast.parse(source)
    passes = gates = False
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call):
            continue
        func = node.func
        name = (
            func.attr
            if isinstance(func, ast.Attribute)
            else getattr(func, "id", None)
        )
        if name == "start_research_process" and any(
            kw.arg == "custom_endpoint" for kw in node.keywords
        ):
            passes = True
        if name == "is_safe_custom_llm_endpoint":
            gates = True
    return passes, gates


class TestCustomEndpointBoundaryGate:
    """``is_safe_custom_llm_endpoint`` at the request boundary.

    Defence in depth: the provider's ``assert_base_url_safe`` still fires
    before any client is built. What the boundary buys is a clean 400
    *before* a research row is committed and a worker thread is spawned —
    which is exactly why ``followup.py`` and ``research.py`` do it.
    """

    def test_detector_positive_control(self):
        source = (
            "def go(data):\n"
            "    start_research_process(1, custom_endpoint=data['e'])\n"
        )
        assert _boundary_census(source) == (True, False)

    def test_detector_negative_control(self):
        source = (
            "def go(data):\n"
            "    if not is_safe_custom_llm_endpoint(data['e']):\n"
            "        return 400\n"
            "    start_research_process(1, custom_endpoint=data['e'])\n"
        )
        assert _boundary_census(source) == (True, True)

    def test_detector_finds_the_known_gated_routers(self):
        """POSITIVE CONTROL against real source: the detector does see
        the gates that exist, so an empty offender list would be real."""
        gated = set()
        for path in sorted((SRC / "web" / "routers").glob("*.py")):
            passes, gates = _boundary_census(path.read_text(encoding="utf-8"))
            if passes and gates:
                gated.add(path.name)
        assert {"research.py", "followup.py"} <= gated

    def test_every_research_start_boundary_gates_the_endpoint(self):
        offenders = []
        for path in sorted((SRC / "web" / "routers").glob("*.py")):
            passes, gates = _boundary_census(path.read_text(encoding="utf-8"))
            if passes and not gates:
                offenders.append(path.name)

        assert offenders == [], (
            "router(s) forward a custom LLM endpoint into a research run "
            f"without the boundary SSRF pre-flight: {offenders}"
        )


# --------------------------------------------------------------------------
# 4. normalize_provider
# --------------------------------------------------------------------------


class TestNormalizeProvider:
    @pytest.mark.parametrize(
        ("raw", "expected"),
        [
            (None, None),
            ("", None),
            ("OpenAI", "openai"),
            ("OPENAI_ENDPOINT", "openai_endpoint"),
            ("ollama", "ollama"),
            (" QWEN ", "qwen"),
            ("\topenai_endpoint\n", "openai_endpoint"),
            ("   ", None),
            # Settings round-trips ship literal quotes; ``get_llm`` has always
            # resolved these, so the route checks must too.
            ('"openai_endpoint"', "openai_endpoint"),
            ("'openai_endpoint'", "openai_endpoint"),
            ('  "OpenAI_Endpoint" ', "openai_endpoint"),
            ("  'ollama'  ", "ollama"),
            ('""', None),
            ("''", None),
            ('" "', None),
        ],
    )
    def test_none_safe_and_case_folding(self, raw, expected):
        assert normalize_provider(raw) == expected

    def test_get_llm_normalizes_a_quoted_mixed_case_provider(self):
        """Settings round-trips have shipped quoted values; the UI ships
        uppercase provider keys. Both must reach the same provider."""
        from local_deep_research.config.llm_config import get_llm

        snap = _snapshot(**{"llm.ollama.url": "http://localhost:11434"})
        fake = FakeListChatModel(responses=["ok"])

        with patch(OLLAMA_CHAT, return_value=fake) as chat:
            wrapped = get_llm(
                provider='  "OLLAMA" ',
                model_name="llama3.1:8b",
                settings_snapshot=snap,
            )

        assert chat.call_count == 1
        assert wrapped.base_llm is fake

    @pytest.mark.parametrize(
        "quoted", ['"openai_endpoint"', "'openai_endpoint'"]
    )
    def test_quoted_openai_endpoint_keeps_request_custom_endpoint(self, quoted):
        """A quoted ``model_provider`` must take the same openai_endpoint
        branch ``get_llm`` does: the request's ``custom_endpoint`` is used
        (not silently dropped for the stored URL) and the provider comes
        back in canonical form."""
        from local_deep_research.web.routers.research import (
            _extract_research_params,
        )

        settings_manager = MagicMock()
        settings_manager.get_setting.side_effect = lambda key, default=None: (
            "http://192.168.1.10:1234/v1"
            if key == "llm.openai_endpoint.url"
            else default
        )

        params = _extract_research_params(
            {
                "query": "q",
                "model_provider": quoted,
                "custom_endpoint": "http://192.168.1.20:5678/v1",
            },
            settings_manager,
        )

        assert params["model_provider"] == "openai_endpoint"
        assert params["custom_endpoint"] == "http://192.168.1.20:5678/v1"

    def test_quoted_openai_endpoint_falls_back_to_stored_url(self):
        """With no ``custom_endpoint`` in the request, a quoted provider
        still resolves the stored endpoint URL, like the unquoted one."""
        from local_deep_research.web.routers.research import (
            _extract_research_params,
        )

        settings_manager = MagicMock()
        settings_manager.get_setting.side_effect = lambda key, default=None: (
            "http://192.168.1.10:1234/v1"
            if key == "llm.openai_endpoint.url"
            else default
        )

        params = _extract_research_params(
            {"query": "q", "model_provider": '"openai_endpoint"'},
            settings_manager,
        )

        assert params["model_provider"] == "openai_endpoint"
        assert params["custom_endpoint"] == "http://192.168.1.10:1234/v1"

    def test_non_string_model_provider_is_a_client_error(self):
        from local_deep_research.web.routers.research import (
            _extract_research_params,
        )

        settings_manager = MagicMock()
        settings_manager.get_setting.side_effect = lambda key, default=None: (
            default
        )

        with pytest.raises(ValueError):
            _extract_research_params(
                {"query": "q", "model_provider": 123}, settings_manager
            )


# --------------------------------------------------------------------------
# 5. error taxonomy
# --------------------------------------------------------------------------


def _missing_key_error():
    from local_deep_research.llm.providers.implementations.openai import (
        OpenAIProvider,
    )

    with pytest.raises(ValueError) as excinfo:
        OpenAIProvider.create_llm(
            model_name="gpt-4o-mini", settings_snapshot=_snapshot()
        )
    return excinfo.value


def _missing_model_error():
    from local_deep_research.llm.providers.implementations.openai import (
        OpenAIProvider,
    )

    snap = _snapshot(**{"llm.openai.api_key": FAKE_KEY})
    with pytest.raises(ValueError) as excinfo:
        OpenAIProvider.create_llm(model_name="   ", settings_snapshot=snap)
    return excinfo.value


def _unsafe_url_error():
    from local_deep_research.llm.providers.implementations.ollama import (
        OllamaProvider,
    )

    snap = _snapshot(**{"llm.ollama.url": METADATA_URL})
    with patch(OLLAMA_CHAT):
        with pytest.raises(ValueError) as excinfo:
            OllamaProvider.create_llm(
                model_name="llama3.1:8b", settings_snapshot=snap
            )
    return excinfo.value


def _unknown_provider_error():
    from local_deep_research.config.llm_config import get_llm

    with pytest.raises(ValueError) as excinfo:
        get_llm(
            provider="not-a-provider",
            model_name="whatever",
            settings_snapshot=_snapshot(),
        )
    return excinfo.value


class TestErrorTaxonomy:
    """Four different misconfigurations, four different remedies.

    All four are ``ValueError`` — callers cannot branch on type — so the
    message is the whole contract, and each must name the setting the
    operator has to change.
    """

    @pytest.mark.parametrize(
        ("factory", "must_name"),
        [
            (_missing_key_error, "llm.openai.api_key"),
            (_missing_model_error, "llm.model"),
            (_unsafe_url_error, "llm.ollama.url"),
            (_unknown_provider_error, "not-a-provider"),
        ],
    )
    def test_each_failure_names_what_to_fix(self, factory, must_name):
        assert must_name in str(factory())

    def test_the_four_failures_are_not_one_generic_message(self):
        messages = [
            str(_missing_key_error()),
            str(_missing_model_error()),
            str(_unsafe_url_error()),
            str(_unknown_provider_error()),
        ]
        assert len(set(messages)) == 4
        # ... and none of them leaks the configured credential.
        assert all(FAKE_KEY not in message for message in messages)

    @pytest.mark.parametrize(
        ("error", "is_rate_limit"),
        [
            # 429 by status code, with a message that says nothing.
            (_Boom("upstream error", SimpleNamespace(status_code=429)), True),
            # 401: a key problem, never a pacing problem.
            (
                _Boom(
                    "Invalid Authentication",
                    SimpleNamespace(status_code=401),
                ),
                False,
            ),
            # a connection timeout.
            (TimeoutError("Read timed out"), False),
            # a malformed / non-JSON response body.
            (ValueError("Expecting value: line 1 column 1 (char 0)"), False),
        ],
    )
    def test_rate_limit_detection_separates_429_from_the_rest(
        self, error, is_rate_limit
    ):
        from local_deep_research.web_search_engines.rate_limiting.llm.detection import (  # noqa: E501
            is_llm_rate_limit_error,
        )

        assert is_llm_rate_limit_error(error) is is_rate_limit

    def test_a_timeout_that_says_try_again_is_still_a_timeout(self):
        from local_deep_research.web_search_engines.rate_limiting.llm.detection import (  # noqa: E501
            is_llm_rate_limit_error,
        )

        error = TimeoutError("Read timed out, please try again later")
        assert is_llm_rate_limit_error(error) is False


# --------------------------------------------------------------------------
# 6. wrap_llm / token-counting callback ownership
# --------------------------------------------------------------------------


def _run_in_worker_thread(fn):
    """Run *fn* off MainThread — the branch research threads take.

    ``TokenCountingCallback._save_to_db`` writes through
    ``thread_metrics.metrics_writer`` (and takes its username from the
    research context) only when it is not on MainThread.
    """
    box = {}

    def _target():
        try:
            box["value"] = fn()
        except BaseException as exc:  # pragma: no cover - surfaced below
            box["error"] = exc

    thread = threading.Thread(target=_target, name="ldr-research-worker")
    thread.start()
    thread.join(timeout=30)
    if "error" in box:
        raise box["error"]
    return box.get("value")


def _wrap_for(llm, research_id, username, *, owns_llm=False):
    from local_deep_research.config.llm_config import (
        wrap_llm_without_think_tags,
    )

    return wrap_llm_without_think_tags(
        llm,
        research_id=research_id,
        provider="ollama",
        owns_llm=owns_llm,
        research_context={
            "username": username,
            "user_password": f"pw-{username}",
        },
        settings_snapshot=_snapshot(),
    )


_METRICS_WRITER = "local_deep_research.database.thread_metrics.metrics_writer"


def _written(writer):
    return [call.args[:2] for call in writer.write_token_metrics.mock_calls]


@contextmanager
def _registered(provider, llm):
    from local_deep_research.llm import register_llm, unregister_llm

    register_llm(provider, llm)
    try:
        yield
    finally:
        unregister_llm(provider)


def _get_llm_for(provider, username, **snapshot_overrides):
    """The production dispatch: registry lookup, then the shared wrap."""
    from local_deep_research.config.llm_config import get_llm

    return get_llm(
        provider=provider,
        model_name="test-model",
        username=username,
        research_id=f"res-{username}",
        research_context={
            "username": username,
            "user_password": f"pw-{username}",
        },
        settings_snapshot=_snapshot(**snapshot_overrides),
    )


class _Recorder(BaseCallbackHandler):
    """A caller's own handler: counts the model runs it sees."""

    def __init__(self):
        self.started = 0
        self.completed = 0

    def on_chat_model_start(self, serialized, messages, **kwargs):
        self.started += 1

    def on_llm_end(self, response, **kwargs):
        self.completed += 1


class _ToolCallingFake(FakeListChatModel):
    """Accepts ``bind_tools``, which ``with_structured_output`` also uses."""

    def bind_tools(self, tools, **kwargs):
        return self.bind(tools=tools, **kwargs)


class _DelegatingFake(FakeListChatModel):
    """Keeps another model under ``base_llm``, like the rate-limiting
    wrapper example in docs/CUSTOM_LLM_INTEGRATION.md."""

    base_llm: Any = None


class _ChainToolFake(FakeListChatModel):
    """``bind_tools`` returns a chain, as a custom model's ``prompt | self``
    would, so the model call is a child run of that chain."""

    def bind_tools(self, tools, **kwargs):
        return RunnableLambda(lambda value: value) | self


class _NestingFake(FakeListChatModel):
    """Answers with an inner model that it runs as a child of its own run,
    the way a routing model that traces its inner call would. The child
    gets the run's inheritable handlers, as ``ParentRunManager.get_child``
    gives a chain's child runs."""

    inner: Any = None

    def _call(self, messages, stop=None, run_manager=None, **kwargs):
        child = CallbackManager(handlers=[], parent_run_id=run_manager.run_id)
        child.set_handlers(run_manager.inheritable_handlers)
        return self.inner.invoke(messages, {"callbacks": child}).content


class _ConfigRecorder(FakeListChatModel):
    """Records the config each call hands the model."""

    received: list = Field(default_factory=list)

    def invoke(self, input, config=None, **kwargs):
        self.received.append(ensure_config(config))
        return super().invoke(input, config, **kwargs)

    def stream(self, input, config=None, **kwargs):
        self.received.append(ensure_config(config))
        return super().stream(input, config, **kwargs)


class _ClosableModel(FakeListChatModel):
    """Holds clients of its own, like ``ChatOllama``: ``_close_base_llm``
    calls its ``close()``, and a closed model cannot answer."""

    closed: bool = False

    def close(self):
        self.closed = True

    def _call(self, *args, **kwargs):
        assert not self.closed, "a closed model was used"
        return super()._call(*args, **kwargs)

    def _stream(self, *args, **kwargs):
        assert not self.closed, "a closed model was used"
        yield from super()._stream(*args, **kwargs)


_LOOKUP_TOOL = {
    "type": "function",
    "function": {
        "name": "lookup",
        "parameters": {"type": "object", "properties": {}},
    },
}


class TestTokenCallbackOwnership:
    """A registered LLM can be shared; its callbacks must not be.

    ``register_llm(name, llm_instance, username=...)`` (the programmatic
    API) stores the instance itself — and a registration made without a
    username lands in the shared namespace, resolving for every user. A
    registered factory can share too: it can return the same model on
    every call (a singleton, or a cached builder), or give every model it
    builds one callback list or manager. Each research's token callback
    travels with that research's calls, so the model is invoked as it is,
    keeps its own state, and never collects another research's callback.
    """

    def test_the_callback_fires_and_writes_for_its_own_research(self):
        """POSITIVE CONTROL for the whole metrics chain.

        Real ``wrap_llm_without_think_tags``, real ``TokenCountingCallback``,
        real langchain callback dispatch; only the DB writer is a mock.
        """
        writer = MagicMock()
        shared = FakeListChatModel(responses=["a", "b", "c", "d"])
        wrapped = _wrap_for(shared, "res-alice", "alice")

        with patch(
            "local_deep_research.database.thread_metrics.metrics_writer",
            writer,
        ):
            _run_in_worker_thread(lambda: wrapped.invoke("hi"))

        written = [
            call.args[:2] for call in writer.write_token_metrics.mock_calls
        ]
        assert written == [("alice", "res-alice")]

    def test_shared_registry_instance_isolated_through_get_llm(self):
        from local_deep_research.config.llm_config import get_llm
        from local_deep_research.llm import register_llm, unregister_llm

        provider = "shared-callback-isolation-test"
        shared = FakeListChatModel(responses=["a", "b"])
        register_llm(provider, shared)
        try:
            wrappers = {}
            for username in ("alice", "bob"):
                wrappers[username] = get_llm(
                    provider=provider,
                    model_name="test-model",
                    username=username,
                    research_id=f"res-{username}",
                    research_context={
                        "username": username,
                        "user_password": f"pw-{username}",
                    },
                    settings_snapshot=_snapshot(),
                )

            writer = MagicMock()
            with patch(
                "local_deep_research.database.thread_metrics.metrics_writer",
                writer,
            ):
                _run_in_worker_thread(lambda: wrappers["bob"].invoke("hi"))

            assert [
                call.args[:2] for call in writer.write_token_metrics.mock_calls
            ] == [("bob", "res-bob")]
            # The caller's own instance is invoked, never a copy.
            assert wrappers["alice"].base_llm is shared
            assert wrappers["bob"].base_llm is shared
            assert not shared.callbacks
            assert getattr(shared, "_ldr_research_id", None) is None
        finally:
            unregister_llm(provider)

    def test_a_registered_instance_keeps_its_own_state_across_researches(
        self,
    ):
        """``quick_summary(llms={...})`` registers the caller's model and
        gives every run a research id. The caller's model must be the one
        invoked: FakeListChatModel's response cursor ``i`` is rebound on
        each call, like a monitor's ``call_count += 1``, so it only
        advances on the registered instance if no copy is made."""
        shared = FakeListChatModel(responses=["first", "second", "third"])
        provider = "shared-state-test"
        with _registered(provider, shared):
            alice = _get_llm_for(provider, "alice")
            bob = _get_llm_for(provider, "bob")
            writer = MagicMock()
            with patch(_METRICS_WRITER, writer):
                replies = [
                    _run_in_worker_thread(lambda: alice.invoke("x")).content,
                    _run_in_worker_thread(lambda: bob.invoke("y")).content,
                ]

        assert replies == ["first", "second"]
        assert shared.i == 2
        assert _written(writer) == [("alice", "res-alice"), ("bob", "res-bob")]

    @pytest.mark.parametrize("container", ["list", "manager"])
    def test_a_factory_sharing_a_callback_container_keeps_it_unchanged(
        self, container
    ):
        """A factory can derive each model from one configured template.
        ``model_copy()`` shares the template's callback list or manager, so
        adding Alice's token callback to it in place would make Bob's later
        call write under Alice's name too."""
        user_handler = _Recorder()
        callbacks = (
            CallbackManager([user_handler])
            if container == "manager"
            else [user_handler]
        )
        template = FakeListChatModel(responses=["a"], callbacks=callbacks)
        shared_callbacks = template.callbacks

        def factory(model_name, temperature, settings_snapshot):
            return template.model_copy()

        provider = "factory-callback-isolation-test"
        with _registered(provider, factory):
            _get_llm_for(provider, "alice")
            bob = _get_llm_for(provider, "bob")
            writer = MagicMock()
            with patch(_METRICS_WRITER, writer):
                _run_in_worker_thread(lambda: bob.invoke("hi"))

        assert _written(writer) == [("bob", "res-bob")]
        # Positive control: the shared container still served the call.
        assert user_handler.completed == 1
        handlers = (
            shared_callbacks.handlers
            if container == "manager"
            else shared_callbacks
        )
        assert handlers == [user_handler]

    @pytest.mark.parametrize(
        "rate_limited", [False, True], ids=["direct", "rate-limited"]
    )
    @pytest.mark.parametrize("builder", ["singleton", "lru_cache"])
    def test_a_factory_returning_one_model_keeps_it_unchanged(
        self, builder, rate_limited
    ):
        """A factory can return the same model on every call, for instance
        a local model it loads once. If a research's token callback were
        stored on that model, Bob's call would also fire Alice's callback
        and write Bob's usage under Alice's name and research id."""
        user_handler = _Recorder()
        built = []

        def build():
            model = FakeListChatModel(
                responses=["a", "b"], callbacks=[user_handler]
            )
            built.append((model, model.callbacks))
            return model

        if builder == "singleton":
            singleton = build()

            def factory(**kwargs):
                return singleton

        else:

            @functools.lru_cache(maxsize=None)
            def cached_build(model_name, temperature):
                return build()

            def factory(model_name, temperature, settings_snapshot):
                return cached_build(model_name, temperature)

        provider = f"one-model-factory-{builder}-test"
        settings = {"rate_limiting.llm_enabled": rate_limited}
        with _registered(provider, factory):
            alice = _get_llm_for(provider, "alice", **settings)
            bob = _get_llm_for(provider, "bob", **settings)
            writer = MagicMock()
            with patch(_METRICS_WRITER, writer):
                _run_in_worker_thread(lambda: alice.invoke("x"))
                _run_in_worker_thread(lambda: bob.invoke("y"))

        # Positive controls: one model served both researches, behind the
        # rate-limiting layer when it is on, and its own handler still ran.
        assert len(built) == 1
        model, own_callbacks = built[0]
        expected_layer = (
            "RateLimitedLLMWrapper" if rate_limited else "FakeListChatModel"
        )
        assert type(bob.base_llm).__name__ == expected_layer
        assert user_handler.completed == 2
        assert _written(writer) == [("alice", "res-alice"), ("bob", "res-bob")]
        assert model.callbacks is own_callbacks
        assert own_callbacks == [user_handler]
        assert getattr(model, "_ldr_research_id", None) is None

    def test_a_factory_building_a_model_per_call_records_each_call_once(
        self,
    ):
        """The usual factory, like a built-in provider's ``create_llm``,
        builds a new model per call. Nothing is stored on that model
        either, and the research's usage is still recorded once per call."""
        built = []

        def factory(model_name, temperature, settings_snapshot):
            model = FakeListChatModel(responses=["a", "b"])
            built.append(model)
            return model

        provider = "model-per-call-factory-test"
        with _registered(provider, factory):
            alice = _get_llm_for(provider, "alice")
            writer = MagicMock()
            with patch(_METRICS_WRITER, writer):
                _run_in_worker_thread(lambda: alice.invoke("x"))
                _run_in_worker_thread(lambda: alice.invoke("y"))

        assert len(built) == 1
        assert alice.base_llm is built[0]
        assert _written(writer) == [("alice", "res-alice")] * 2
        assert not built[0].callbacks

    @pytest.mark.parametrize("registration", ["instance", "factory"])
    def test_rows_are_written_with_llm_rate_limiting_enabled(
        self, registration
    ):
        """The rate-limiting wrapper calls the model without its own
        attributes, so a callback stored on that wrapper never fires."""

        def factory(model_name, temperature, settings_snapshot):
            return FakeListChatModel(responses=["a"])

        registered = (
            FakeListChatModel(responses=["a"])
            if registration == "instance"
            else factory
        )
        provider = "rate-limited-callback-test"
        with _registered(provider, registered):
            alice = _get_llm_for(
                provider, "alice", **{"rate_limiting.llm_enabled": True}
            )
            writer = MagicMock()
            with patch(_METRICS_WRITER, writer):
                _run_in_worker_thread(lambda: alice.invoke("hi"))

        # Positive control: the rate-limiting layer is in the chain and
        # still carries the research's cancellation scope.
        assert type(alice.base_llm).__name__ == "RateLimitedLLMWrapper"
        assert alice.base_llm._ldr_research_id == "res-alice"
        assert _written(writer) == [("alice", "res-alice")]
        # The call's config carried the callback through that layer; the
        # model itself was left as it was.
        assert not alice.base_llm.base_llm.callbacks

    @pytest.mark.parametrize(
        "path",
        [
            "ainvoke",
            "stream",
            "astream",
            "batch",
            "bind_tools",
            "with_structured_output",
            "bind",
            "with_config",
        ],
    )
    def test_every_call_path_writes_one_row_for_its_own_research(self, path):
        model = _ToolCallingFake(responses=["ok"])
        bob = _wrap_for(model, "res-bob", "bob")
        tool = {
            "type": "function",
            "function": {
                "name": "lookup",
                "parameters": {"type": "object", "properties": {}},
            },
        }
        schema = {
            "title": "Answer",
            "type": "object",
            "properties": {"text": {"type": "string"}},
        }

        async def _astream():
            return [chunk async for chunk in bob.astream("hi")]

        calls = {
            "ainvoke": lambda: asyncio.run(bob.ainvoke("hi")),
            "stream": lambda: list(bob.stream("hi")),
            "astream": lambda: asyncio.run(_astream()),
            "batch": lambda: bob.batch(["hi"]),
            "bind_tools": lambda: bob.bind_tools([tool]).invoke("hi"),
            "with_structured_output": lambda: bob.with_structured_output(
                schema
            ).invoke("hi"),
            "bind": lambda: bob.bind(stop=["never"]).invoke("hi"),
            "with_config": lambda: bob.with_config(run_name="bob").invoke("hi"),
        }
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            _run_in_worker_thread(calls[path])

        assert _written(writer) == [("bob", "res-bob")]
        assert not model.callbacks
        assert getattr(model, "_ldr_research_id", None) is None

    def test_callbacks_of_an_enclosing_run_still_see_the_model_call(self):
        """The research callback joins the config the model would inherit
        (from a LangGraph node, say). It must not replace that config."""
        outer = _Recorder()
        alice = _wrap_for(
            FakeListChatModel(responses=["a"]), "res-alice", "alice"
        )
        chain = RunnableLambda(lambda text: alice.invoke(text))
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            _run_in_worker_thread(
                lambda: chain.invoke("hi", {"callbacks": [outer]})
            )

        assert (outer.started, outer.completed) == (1, 1)
        assert _written(writer) == [("alice", "res-alice")]

    def test_with_config_keeps_the_callbacks_it_binds(self):
        """``with_config(callbacks=...)`` re-wraps; the bound callbacks and
        the research callback both see the model call."""
        bound = _Recorder()
        alice = _wrap_for(
            FakeListChatModel(responses=["a"]), "res-alice", "alice"
        )
        configured = alice.with_config(callbacks=[bound])
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            _run_in_worker_thread(lambda: configured.invoke("hi"))

        assert (bound.started, bound.completed) == (1, 1)
        assert _written(writer) == [("alice", "res-alice")]

    def test_an_agent_without_tools_runs_the_model_through_bind(self):
        """``create_agent()`` calls ``model.bind(...)`` when the agent has no
        tools, and runs what it returns. That model call must still strip
        ``<think>`` tags and record the research's token usage, as the
        ``bind_tools`` path does for an agent with tools."""
        from langchain.agents import create_agent

        model = FakeListChatModel(responses=["<think>plan</think>answer"])
        alice = _wrap_for(model, "res-alice", "alice")
        agent = create_agent(model=alice, tools=[])
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            result = _run_in_worker_thread(
                lambda: agent.invoke(
                    {"messages": [{"role": "user", "content": "hi"}]}
                )
            )

        assert result["messages"][-1].content == "answer"
        assert _written(writer) == [("alice", "res-alice")]
        assert not model.callbacks

    @pytest.mark.parametrize(
        "owns_llm", [True, False], ids=["owned", "registered"]
    )
    def test_the_cancellation_scope_stays_on_the_wrappers(self, owns_llm):
        inner = FakeListChatModel(responses=["inner"])
        model = _DelegatingFake(responses=["outer"], base_llm=inner)
        wrapped = _wrap_for(model, "res-alice", "alice", owns_llm=owns_llm)

        assert wrapped._ldr_research_id == "res-alice"
        assert getattr(model, "_ldr_research_id", None) is None
        assert getattr(inner, "_ldr_research_id", None) is None

    def test_a_second_users_call_does_not_write_under_the_first_user(self):
        writer = MagicMock()
        shared = FakeListChatModel(responses=["a", "b", "c", "d"])

        _wrap_for(shared, "res-alice", "alice")
        bob = _wrap_for(shared, "res-bob", "bob")

        with patch(
            "local_deep_research.database.thread_metrics.metrics_writer",
            writer,
        ):
            _run_in_worker_thread(lambda: bob.invoke("hi"))

        written = [
            call.args[:2] for call in writer.write_token_metrics.mock_calls
        ]
        assert written == [("bob", "res-bob")]
        assert not shared.callbacks
        assert getattr(shared, "_ldr_research_id", None) is None

    def test_unscoped_call_does_not_reuse_an_earlier_research_callback(self):
        writer = MagicMock()
        shared = FakeListChatModel(responses=["a", "b"])

        _wrap_for(shared, "res-alice", "alice")
        unscoped = _wrap_for(shared, None, "bob")

        with patch(
            "local_deep_research.database.thread_metrics.metrics_writer",
            writer,
        ):
            _run_in_worker_thread(lambda: unscoped.invoke("hi"))

        writer.write_token_metrics.assert_not_called()
        assert not shared.callbacks

    def test_registered_callback_manager_keeps_its_handlers(self):
        class Recorder(BaseCallbackHandler):
            def __init__(self):
                self.completed = 0

            def on_llm_end(self, response, **kwargs):
                self.completed += 1

        user_handler = Recorder()
        manager = CallbackManager([user_handler])
        shared = FakeListChatModel(responses=["a", "b"], callbacks=manager)
        alice = _wrap_for(shared, "res-alice", "alice")
        bob = _wrap_for(shared, "res-bob", "bob")
        writer = MagicMock()

        with patch(
            "local_deep_research.database.thread_metrics.metrics_writer",
            writer,
        ):
            _run_in_worker_thread(lambda: alice.invoke("first"))
            _run_in_worker_thread(lambda: bob.invoke("second"))

        assert user_handler.completed == 2
        assert manager.handlers == [user_handler]
        assert [
            call.args[:2] for call in writer.write_token_metrics.mock_calls
        ] == [("alice", "res-alice"), ("bob", "res-bob")]

    def test_no_callback_is_attached_without_a_research_id(self):
        """Metrics are opt-in per research: no research_id, no listener on
        the (possibly shared) instance."""
        shared = FakeListChatModel(responses=["a"])
        _wrap_for(shared, None, "alice")

        assert not shared.callbacks

    @pytest.mark.parametrize("path", ["invoke", "stream"])
    @pytest.mark.parametrize(
        "call_config",
        [None, {"metadata": {"k": "call"}}],
        ids=["no-config", "explicit-config"],
    )
    def test_with_config_resolves_as_in_langchain_inside_an_enclosing_run(
        self, path, call_config
    ):
        """Called without a config inside an enclosing run, a binding's
        bound config wins over the one the call inherits; called with one,
        the call's config is merged over it. Adding the research callback
        must leave every key as ``model.with_config(...)`` resolves it."""
        bound = {
            "metadata": {"k": "bound"},
            "configurable": {"c": "bound"},
            "recursion_limit": 55,
        }
        enclosing = {
            "metadata": {"k": "outer"},
            "configurable": {"c": "outer"},
            "recursion_limit": 77,
        }
        args = ("hi",) if call_config is None else ("hi", call_config)

        def call(runnable):
            if path == "stream":
                return lambda _: list(runnable.stream(*args))
            return lambda _: runnable.invoke(*args)

        def resolved(model):
            config = model.received[-1]
            return (
                config["metadata"]["k"],
                config["configurable"]["c"],
                config["recursion_limit"],
            )

        plain = _ConfigRecorder(responses=["a"])
        RunnableLambda(call(plain.with_config(**bound))).invoke("x", enclosing)
        model = _ConfigRecorder(responses=["a"])
        alice = _wrap_for(model, "res-alice", "alice").with_config(**bound)
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            _run_in_worker_thread(
                lambda: RunnableLambda(call(alice)).invoke("x", enclosing)
            )

        if call_config is None:
            # Positive control: LangChain's own precedence for a binding.
            assert resolved(plain) == ("bound", "bound", 55)
        assert resolved(model) == resolved(plain)
        assert _written(writer) == [("alice", "res-alice")]

    @pytest.mark.parametrize("returned", ["binding", "chain"])
    def test_a_tool_bound_model_records_each_call_once(self, returned):
        """``bind_tools`` usually returns a ``RunnableBinding``, which hands
        the call's config to the model's own run. A custom model can return
        a chain instead, which runs the model as a child run. Either way
        each call writes one row."""
        fake = _ToolCallingFake if returned == "binding" else _ChainToolFake
        alice = _wrap_for(fake(responses=["ok"]), "res-alice", "alice")
        bound = alice.bind_tools([_LOOKUP_TOOL])
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            _run_in_worker_thread(lambda: bound.invoke("hi"))
            _run_in_worker_thread(lambda: list(bound.stream("hi")))

        assert _written(writer) == [("alice", "res-alice")] * 2

    @pytest.mark.parametrize("returned", ["binding", "chain"])
    def test_an_agent_with_tools_records_its_model_call_once(self, returned):
        """``create_agent()`` runs what the model's ``bind_tools`` returns,
        whether that is a ``RunnableBinding`` or a chain."""
        from langchain.agents import create_agent
        from langchain_core.tools import tool

        @tool
        def lookup(query: str) -> str:
            """Look a query up."""
            return "found"

        fake = _ToolCallingFake if returned == "binding" else _ChainToolFake
        model = fake(responses=["<think>plan</think>answer"])
        agent = create_agent(
            model=_wrap_for(model, "res-alice", "alice"), tools=[lookup]
        )
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            result = _run_in_worker_thread(
                lambda: agent.invoke(
                    {"messages": [{"role": "user", "content": "hi"}]}
                )
            )

        assert result["messages"][-1].content == "answer"
        assert _written(writer) == [("alice", "res-alice")]
        assert not model.callbacks

    def test_a_model_running_an_inner_model_records_one_row(self):
        """A model can answer by running another model as a child of its own
        run, as a router does. Like the model's own ``callbacks``, the
        research callback sees only the model's own run, so one call writes
        one row, not one per model."""
        inner = FakeListChatModel(responses=["inner answer"])
        model = _NestingFake(
            responses=["unused"], inner=inner, disable_streaming=True
        )
        alice = _wrap_for(model, "res-alice", "alice")
        writer = MagicMock()
        with patch(_METRICS_WRITER, writer):
            reply = _run_in_worker_thread(lambda: alice.invoke("hi"))

        # Positive control: the inner model produced the answer.
        assert reply.content == "inner answer"
        assert _written(writer) == [("alice", "res-alice")]


class TestModelOwnership:
    """LDR closes only the models that its built-in providers build.

    When a research ends, ``safe_close(system.model)`` in
    ``api/research_functions.py`` closes its LLM. A built-in provider
    builds a new model on every ``get_llm()`` call, so that releases only
    this research's clients. A registered instance, or a model that a
    registered factory returns, can serve other researches at the same
    time, because a factory can return one cached or singleton model.
    """

    def test_a_built_in_provider_model_is_closed_when_its_research_ends(
        self,
    ):
        from local_deep_research.utilities.resource_utils import safe_close

        url = {"llm.ollama.url": "http://127.0.0.1:11434"}
        alice = _get_llm_for("ollama", "alice", **url)
        bob = _get_llm_for("ollama", "bob", **url)
        model = alice.base_llm
        try:
            # Positive controls: a real client, and one per research.
            assert type(model).__name__ == "ChatOllama"
            assert bob.base_llm is not model

            safe_close(alice, "system LLM")

            assert model._client._client.is_closed
            assert model._async_client._client.is_closed
            assert not bob.base_llm._client._client.is_closed
        finally:
            safe_close(bob, "system LLM")

    @pytest.mark.parametrize(
        "registration",
        ["instance", "singleton factory", "lru_cache factory", "new per call"],
    )
    def test_ldr_never_closes_a_registered_model(self, registration):
        """Ending Alice's research must leave Bob's research able to use
        the model. LDR cannot tell a factory that returns one model from
        one that builds a new model per call, so it closes neither."""
        from local_deep_research.utilities.resource_utils import safe_close

        built = []

        def build():
            model = _ClosableModel(responses=["a", "b"])
            built.append(model)
            return model

        if registration == "instance":
            registered = build()
        elif registration == "singleton factory":
            singleton = build()

            def registered(model_name, temperature, settings_snapshot):
                return singleton

        elif registration == "lru_cache factory":

            @functools.lru_cache(maxsize=None)
            def cached_build(model_name, temperature):
                return build()

            def registered(model_name, temperature, settings_snapshot):
                return cached_build(model_name, temperature)

        else:

            def registered(model_name, temperature, settings_snapshot):
                return build()

        provider = "registered-model-ownership-test"
        with _registered(provider, registered):
            alice = _get_llm_for(provider, "alice")
            bob = _get_llm_for(provider, "bob")
            writer = MagicMock()
            with patch(_METRICS_WRITER, writer):
                safe_close(alice, "system LLM")
                reply = _run_in_worker_thread(lambda: bob.invoke("hi"))

        assert len(built) == (2 if registration == "new per call" else 1)
        assert reply.content == "a"
        assert not any(model.closed for model in built)

    def test_only_built_in_provider_factories_count_as_built_in(self):
        from local_deep_research.llm import get_llm_from_registry
        from local_deep_research.llm.providers.auto_discovery import (
            is_builtin_provider_factory,
        )
        from local_deep_research.llm.providers.implementations.ollama import (
            OllamaProvider,
        )

        discovered = discover_providers()
        assert discovered
        for key in discovered:
            registered = get_llm_from_registry(normalize_provider(key))
            assert is_builtin_provider_factory(registered), key

        class CachingOllamaProvider(OllamaProvider):
            """A user's subclass, defined outside LDR's providers."""

        def factory(model_name, temperature, settings_snapshot):
            return FakeListChatModel(responses=["a"])

        assert not is_builtin_provider_factory(CachingOllamaProvider.create_llm)
        assert not is_builtin_provider_factory(OllamaProvider.is_available)
        assert not is_builtin_provider_factory(factory)
        assert not is_builtin_provider_factory(MagicMock())
        assert not is_builtin_provider_factory(
            FakeListChatModel(responses=["a"])
        )