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.

41.5 KB
Raw Blame
"""The ``openai_endpoint`` key follows a URL override only on the same origin.

Covers the shared binder (``config/openai_endpoint_binding.py``) through its
real entry points: ``overlay_runtime_settings`` (the ``custom_endpoint``
research overlay), ``create_settings_snapshot`` (the Python API's
``settings_override=`` raw snapshot write), ``_init_search_system`` (the
Python API), and the re-check in ``get_llm``. The langchain client class is
patched and every URL is a loopback address, so nothing leaves the machine.
"""

from unittest.mock import MagicMock, patch

import pytest
from langchain_core.language_models.fake_chat_models import FakeListChatModel

from local_deep_research.api.settings_utils import create_settings_snapshot
from local_deep_research.config.llm_config import get_llm
from local_deep_research.config.openai_endpoint_binding import (
    OPENAI_ENDPOINT_KEY_OWNER_MARKER,
    OPENAI_ENDPOINT_SAVED_URL_MARKER,
    bind_openai_endpoint_override,
    resolve_openai_endpoint_override_key,
)
from local_deep_research.llm.providers.base import OPTIONAL_API_KEY_PLACEHOLDER
from local_deep_research.utilities.type_utils import overlay_runtime_settings

OPENAI_BASE_CHAT = "local_deep_research.llm.providers.openai_base.ChatOpenAI"
RESEARCH_KEY = "sk-research-endpoint-key"
STORED_URL = "http://localhost:9000/v1"
OTHER_URL = "http://localhost:8000/v1"
SAME_ORIGIN_URL = "http://localhost:9000/other/"
KEY = "llm.openai_endpoint.api_key"
URL = "llm.openai_endpoint.url"


@pytest.fixture(autouse=True, scope="module")
def _ensure_providers_registered():
    from local_deep_research.llm.providers import discover_providers

    discover_providers(force_refresh=True)


def _envelope_snapshot(url=STORED_URL, key=RESEARCH_KEY):
    """A snapshot shaped like SettingsManager.get_all_settings()."""
    return {
        "llm.supports_max_tokens": {"value": True, "ui_element": "checkbox"},
        "llm.max_tokens": {"value": 4096, "ui_element": "number"},
        "rate_limiting.llm_enabled": {"value": False, "ui_element": "checkbox"},
        "search.tool": {"value": "searxng", "ui_element": "select"},
        URL: {"value": url, "ui_element": "text"},
        KEY: {"value": key, "ui_element": "password"},
    }


def _client_kwargs(snapshot, **get_llm_kwargs):
    with patch(
        OPENAI_BASE_CHAT, return_value=FakeListChatModel(responses=["ok"])
    ) as mock_chat:
        get_llm(
            provider="openai_endpoint",
            model_name="m",
            settings_snapshot=snapshot,
            **get_llm_kwargs,
        )
    return mock_chat.call_args.kwargs


class TestOverlayRuntimeSettings:
    def test_custom_endpoint_on_another_origin_clears_the_key(self):
        snapshot = _envelope_snapshot()
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)

        assert snapshot[URL]["value"] == OTHER_URL
        # Raw "", not an envelope: an envelope read would re-apply an
        # LDR_LLM_OPENAI_ENDPOINT_API_KEY override.
        assert snapshot[KEY] == ""
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:9000"
        )

    def test_custom_endpoint_on_the_same_origin_keeps_the_key(self):
        snapshot = _envelope_snapshot()
        overlay_runtime_settings(snapshot, custom_endpoint=SAME_ORIGIN_URL)

        assert snapshot[URL]["value"] == SAME_ORIGIN_URL
        assert snapshot[KEY]["value"] == RESEARCH_KEY

    def test_overlay_then_get_llm_with_the_same_override_sends_no_key(self):
        """The caller pattern: overlay first, then get_llm(openai_endpoint_url=)."""
        snapshot = _envelope_snapshot()
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=OTHER_URL)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_key_restored_after_overlay_does_not_follow_the_override(self):
        """apply_environment_overrides_to_snapshot can write the stored key
        back after the overlay; get_llm re-checks the recorded owner."""
        snapshot = _envelope_snapshot()
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)
        snapshot[KEY] = RESEARCH_KEY  # what an env re-application does

        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_rebinding_back_to_the_owner_origin_keeps_the_key(self):
        snapshot = _envelope_snapshot()
        overlay_runtime_settings(snapshot, custom_endpoint=SAME_ORIGIN_URL)
        overlay_runtime_settings(snapshot, custom_endpoint=STORED_URL)

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=STORED_URL)
        assert kwargs["api_key"] == RESEARCH_KEY


class TestReBindingAfterACrossOriginBind:
    def test_rebinding_back_to_the_owner_does_not_restore_the_key(self):
        """Documented fail-closed: the cleared key is not kept anywhere in
        the snapshot, so it cannot come back. A run that needs it again
        starts from a fresh snapshot."""
        snapshot = _envelope_snapshot()
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)
        overlay_runtime_settings(snapshot, custom_endpoint=STORED_URL)

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=STORED_URL)
        assert kwargs["base_url"] == STORED_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER
        assert RESEARCH_KEY not in repr(snapshot)


class TestEnvironmentKey:
    """LDR_LLM_OPENAI_ENDPOINT_API_KEY is applied on envelope reads."""

    ENV_KEY = "sk-from-the-environment"

    @pytest.fixture(autouse=True)
    def _env_key(self, monkeypatch):
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", self.ENV_KEY)
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL", raising=False)

    def test_env_key_reaches_the_stored_endpoint(self):
        kwargs = _client_kwargs(_envelope_snapshot(key="db-key"))
        assert kwargs["api_key"] == self.ENV_KEY

    def test_env_key_does_not_follow_a_cross_origin_overlay(self):
        snapshot = _envelope_snapshot(key="db-key")
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=OTHER_URL)
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_env_key_written_back_at_dispatch_does_not_follow_the_overlay(
        self,
    ):
        """research_service re-applies env overrides after the overlay."""
        from local_deep_research.settings.manager import (
            apply_environment_overrides_to_snapshot,
        )

        snapshot = _envelope_snapshot(key="db-key")
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)
        dispatched = apply_environment_overrides_to_snapshot(snapshot)
        # The binder's cleared "" sits under an owner marker; the env pass
        # leaves it in place instead of writing the env key back.
        assert dispatched[KEY] == ""

        kwargs = _client_kwargs(dispatched, openai_endpoint_url=OTHER_URL)
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER


class TestInitSearchSystem:
    """The Python API overlays the URL before calling get_llm."""

    def _client_kwargs(self, override):
        from local_deep_research.api import research_functions

        snapshot = _envelope_snapshot()
        with (
            patch(
                OPENAI_BASE_CHAT,
                return_value=FakeListChatModel(responses=["ok"]),
            ) as mock_chat,
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(
                research_functions, "AdvancedSearchSystem", MagicMock()
            ),
        ):
            research_functions._init_search_system(
                model_name="m",
                provider="openai_endpoint",
                openai_endpoint_url=override,
                settings_snapshot=snapshot,
            )
        # The caller's snapshot is copied, never modified.
        assert snapshot[KEY]["value"] == RESEARCH_KEY
        return mock_chat.call_args.kwargs

    def test_different_origin_override_does_not_receive_the_key(self):
        kwargs = self._client_kwargs(OTHER_URL)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_same_origin_override_reuses_the_key(self):
        kwargs = self._client_kwargs(SAME_ORIGIN_URL)
        assert kwargs["api_key"] == RESEARCH_KEY


class TestStoredUrlFallback:
    def test_no_stored_url_means_the_provider_default_owns_the_key(self):
        snapshot = {KEY: RESEARCH_KEY}
        assert resolve_openai_endpoint_override_key(
            "https://api.openai.com/v1", snapshot
        ) == (RESEARCH_KEY, False)
        assert resolve_openai_endpoint_override_key(OTHER_URL, snapshot) == (
            "",
            True,
        )

    def test_whitespace_stored_url_is_not_the_provider_default(self):
        """The provider normalises "  " into a host-less URL, not the default."""
        snapshot = {URL: "   ", KEY: RESEARCH_KEY}
        assert resolve_openai_endpoint_override_key(
            "https://api.openai.com/v1", snapshot
        ) == ("", True)

    @pytest.mark.parametrize(
        ("owner", "expected"),
        [
            (
                "https://sample:[email protected]/custom/v1?key=sample",
                "https://gateway.example:443",
            ),
            ("http://[::1]:8080/custom/v1", "http://[::1]:8080"),
        ],
    )
    def test_owner_metadata_keeps_only_the_origin(self, owner, expected):
        snapshot = _envelope_snapshot(url=owner)
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == expected

    def test_invalid_owner_stays_unmatched_after_another_overlay(self):
        snapshot = _envelope_snapshot(url="https://gateway.example:invalid/v1")
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == ""
        snapshot[KEY] = RESEARCH_KEY
        overlay_runtime_settings(snapshot, custom_endpoint=OTHER_URL)
        assert snapshot[KEY] == ""
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == ""


class TestForgedOwnerMarker:
    def test_a_marker_that_did_not_come_from_the_binder_is_refused(self):
        """A marker that did not come from the binder is refused."""
        snapshot = _envelope_snapshot()
        snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] = (
            "https://attacker.example/v1"
        )
        assert resolve_openai_endpoint_override_key(
            "https://attacker.example/v1", snapshot
        ) == ("", True)

        # The binder's own canonical record is honoured by the same check.
        snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] = "http://localhost:9000"
        assert resolve_openai_endpoint_override_key(STORED_URL, snapshot) == (
            RESEARCH_KEY,
            False,
        )


class TestSnapshotlessExplicitKey:
    def test_explicit_key_applies_without_a_snapshot(self):
        kwargs = _client_kwargs(
            None,
            openai_endpoint_url=OTHER_URL,
            openai_endpoint_api_key="sk-explicit",
        )
        assert kwargs["api_key"] == "sk-explicit"

    def test_key_only_call_without_a_snapshot_is_still_refused(self):
        from local_deep_research.security.egress.policy import (
            PolicyDeniedError,
        )

        with pytest.raises(PolicyDeniedError):
            get_llm(
                provider="openai_endpoint",
                model_name="m",
                openai_endpoint_api_key="sk-explicit",
                settings_snapshot=None,
            )


class TestCreateSettingsSnapshotOverride:
    """The ``settings_override`` raw snapshot write goes through the binder.

    ``quick_summary(..., settings=..., settings_override={
    "llm.openai_endpoint.url": ...})`` and friends build their snapshot with
    ``create_settings_snapshot``, which used to write the URL verbatim: the
    stored key then rode along to whatever URL the override named, with no
    owner marker and without ``get_llm``'s binding guard ever firing.
    """

    def _quick_summary_client_kwargs(self, settings, settings_override):
        from local_deep_research.api import research_functions

        with (
            patch(
                OPENAI_BASE_CHAT,
                return_value=FakeListChatModel(responses=["ok"]),
            ) as mock_chat,
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(
                research_functions, "AdvancedSearchSystem", MagicMock()
            ),
        ):
            research_functions.quick_summary(
                "q",
                model_name="m",
                provider="openai_endpoint",
                settings=settings,
                settings_override=settings_override,
            )
        return mock_chat.call_args.kwargs

    def test_a_stored_key_does_not_follow_a_snapshot_url_override(self):
        """A stored key does not follow a snapshot url override."""
        kwargs = self._quick_summary_client_kwargs(
            settings=_envelope_snapshot(),
            settings_override={URL: OTHER_URL},
        )

        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER
        assert RESEARCH_KEY not in repr(kwargs)

    def test_a_same_origin_snapshot_override_keeps_the_key_and_binds_it(self):
        """A same origin snapshot override keeps the key and binds it."""
        snapshot = _envelope_snapshot()
        snapshot = create_settings_snapshot(
            overrides={URL: SAME_ORIGIN_URL}, base_settings=snapshot
        )

        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:9000"
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == SAME_ORIGIN_URL
        assert kwargs["api_key"] == RESEARCH_KEY

    def test_the_env_override_keeps_the_key_only_while_it_is_policy(
        self, monkeypatch
    ):
        """The env override keeps the key only while it is policy."""
        env_url = "http://localhost:9100/v1"
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_URL", env_url)
        snapshot = create_settings_snapshot(
            overrides={URL: OTHER_URL}, base_settings=_envelope_snapshot()
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == env_url
        assert kwargs["api_key"] == RESEARCH_KEY

        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL")
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_custom_endpoint_after_a_snapshot_override_stays_withheld(self):
        """Custom endpoint after a snapshot override stays withheld."""
        snapshot = create_settings_snapshot(
            overrides={URL: OTHER_URL}, base_settings=_envelope_snapshot()
        )
        overlay_runtime_settings(
            snapshot, custom_endpoint="http://localhost:8000/other/"
        )

        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == "http://localhost:8000/other/"
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_a_key_named_in_the_same_call_binds_to_the_override(self):
        """A key named in the same call binds to the override."""
        snapshot = create_settings_snapshot(
            overrides={URL: OTHER_URL, KEY: "sk-with-the-url"},
            base_settings=_envelope_snapshot(),
        )

        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:8000"
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == "sk-with-the-url"
        assert RESEARCH_KEY not in repr(snapshot)

    def test_a_forged_owner_marker_in_overrides_is_refused(self):
        """A forged owner marker in overrides is refused."""
        with pytest.raises(ValueError, match="internal snapshot key"):
            create_settings_snapshot(
                overrides={
                    OPENAI_ENDPOINT_KEY_OWNER_MARKER: (
                        "https://attacker.example:443"
                    ),
                    URL: "https://attacker.example/v1",
                },
                base_settings=_envelope_snapshot(),
            )
        # The refusal covers every internal key, not just the known marker.
        with pytest.raises(ValueError, match="internal snapshot key"):
            create_settings_snapshot(
                overrides={"_username": "attacker"},
                base_settings=_envelope_snapshot(),
            )

    def test_a_transient_env_url_never_re_owns_the_key(self, monkeypatch):
        """A transient env url never re owns the key."""
        monkeypatch.setenv(
            "LDR_LLM_OPENAI_ENDPOINT_URL", "http://localhost:9100/v1"
        )
        snapshot = create_settings_snapshot(
            overrides={URL: "http://localhost:9100/anything"},
            base_settings=_envelope_snapshot(),
        )
        # Operator policy while the env is set: the key is kept for the
        # effective (env) URL.
        kwargs = _client_kwargs(snapshot)
        assert kwargs["api_key"] == RESEARCH_KEY

        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL")
        # The env's former origin is foreign now: the same snapshot must
        # not hand the key over.
        kwargs = _client_kwargs(snapshot)
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

        # ... and a later settings_override naming that origin must not
        # ride the key either.
        snapshot = create_settings_snapshot(
            overrides={URL: "http://localhost:9100/anything"},
            base_settings=snapshot,
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER
        assert RESEARCH_KEY not in repr(snapshot)

    def test_after_the_env_is_gone_the_key_follows_its_configured_origin(
        self, monkeypatch
    ):
        """After the env is gone the key follows its configured origin."""
        monkeypatch.setenv(
            "LDR_LLM_OPENAI_ENDPOINT_URL", "http://localhost:9100/v1"
        )
        snapshot = create_settings_snapshot(
            overrides={URL: "http://localhost:9100/anything"},
            base_settings=_envelope_snapshot(),
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["api_key"] == RESEARCH_KEY

        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL")
        snapshot = create_settings_snapshot(
            overrides={URL: "http://localhost:9000/again/"},
            base_settings=snapshot,
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == "http://localhost:9000/again/"
        assert kwargs["api_key"] == RESEARCH_KEY

    def test_binder_written_markers_still_bind_across_calls(self):
        """Binder written markers still bind across calls."""
        snapshot = create_settings_snapshot(
            overrides={URL: SAME_ORIGIN_URL}, base_settings=_envelope_snapshot()
        )
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:9000"
        )
        snapshot = create_settings_snapshot(
            overrides={URL: "http://localhost:9000/third/"},
            base_settings=snapshot,
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == "http://localhost:9000/third/"
        assert kwargs["api_key"] == RESEARCH_KEY

        # The restored key must not follow a foreign override just because
        # the base's URL entry already points at that origin.
        snapshot = create_settings_snapshot(
            overrides={URL: OTHER_URL}, base_settings=_envelope_snapshot()
        )
        snapshot[KEY] = RESEARCH_KEY  # what an env write-back does
        snapshot = create_settings_snapshot(
            overrides={URL: "http://localhost:8000/second/"},
            base_settings=snapshot,
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER


def _web_dispatch_client_kwargs(custom_endpoint):
    """The web research flow for ``custom_endpoint``, up to the LLM client.

    ``get_all_settings(include_environment_overrides=False)`` envelopes,
    the route's ``custom_endpoint`` overlay, the dispatch-time
    ``apply_environment_overrides_to_snapshot`` and overlay, then
    ``get_llm(openai_endpoint_url=custom_endpoint)``.
    """
    from local_deep_research.settings.manager import (
        apply_environment_overrides_to_snapshot,
    )

    snapshot = _envelope_snapshot(key="db-key")
    overlay_runtime_settings(snapshot, custom_endpoint=custom_endpoint)
    snapshot = apply_environment_overrides_to_snapshot(snapshot)
    overlay_runtime_settings(snapshot, custom_endpoint=custom_endpoint)
    return _client_kwargs(snapshot, openai_endpoint_url=custom_endpoint)


class TestEnvironmentSuppliedKeyAtDispatch:
    """An ``LDR_LLM_OPENAI_ENDPOINT_API_KEY`` is the operator's, not the DB's.

    The web research flow captures its snapshot with
    ``include_environment_overrides=False`` (raw DB envelopes), overlays
    ``custom_endpoint``, re-applies the environment at dispatch
    (``apply_environment_overrides_to_snapshot``), overlays again and then
    calls ``get_llm(openai_endpoint_url=custom_endpoint)``. The owner the
    first overlay records comes from the stored URL; it must never authorise
    the env key, which follows only the env URL's origin.
    """

    ENV_KEY = "sk-operator-env-key"
    ENV_URL = "http://localhost:9100/v1"

    @pytest.fixture(autouse=True)
    def _env(self, monkeypatch):
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", self.ENV_KEY)
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_URL", self.ENV_URL)

    def test_env_key_does_not_follow_a_custom_endpoint_on_the_stored_origin(
        self,
    ):
        """Env key does not follow a custom endpoint on the stored origin."""
        kwargs = _web_dispatch_client_kwargs(SAME_ORIGIN_URL)
        assert kwargs["base_url"] == SAME_ORIGIN_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER
        assert self.ENV_KEY not in repr(kwargs)

    def test_env_key_follows_a_custom_endpoint_on_the_env_origin(self):
        kwargs = _web_dispatch_client_kwargs("http://localhost:9100/x/")
        assert kwargs["base_url"] == "http://localhost:9100/x/"
        assert kwargs["api_key"] == self.ENV_KEY

    def test_env_key_with_an_env_url_does_not_go_to_a_default_url(self):
        """Env key with an env url does not go to a default url."""
        snapshot = _envelope_snapshot(key="db-key")
        for url in (
            "https://openrouter.ai/api/v1",
            "https://api.openai.com/v1",
        ):
            assert resolve_openai_endpoint_override_key(url, snapshot) == (
                "",
                True,
            )

    def test_a_call_key_survives_env_re_application(self):
        """A call key survives env re application."""
        from local_deep_research.config.openai_endpoint_binding import (
            bind_openai_endpoint_override,
        )
        from local_deep_research.settings.manager import (
            apply_environment_overrides_to_snapshot,
        )

        snapshot = _envelope_snapshot(key="db-key")
        bind_openai_endpoint_override(
            snapshot, OTHER_URL, explicit_key="sk-call-key"
        )
        snapshot = apply_environment_overrides_to_snapshot(snapshot)
        assert snapshot[KEY] == "sk-call-key"

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=OTHER_URL)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == "sk-call-key"

    def test_the_env_key_under_a_call_marker_is_withheld(self):
        """The env key under a call marker is withheld."""
        from local_deep_research.config.openai_endpoint_binding import (
            bind_openai_endpoint_override,
        )

        snapshot = _envelope_snapshot(key="db-key")
        bind_openai_endpoint_override(
            snapshot, OTHER_URL, explicit_key="sk-call-key"
        )
        snapshot[KEY] = self.ENV_KEY

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=OTHER_URL)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER


class TestSameCallKeyOwner:
    """Which origin a key named with a ``settings_override`` URL belongs to."""

    @pytest.mark.parametrize("url_override", ["", None])
    def test_an_emptied_url_override_keeps_the_same_call_key(
        self, url_override
    ):
        """An emptied url override keeps the same call key."""
        snapshot = create_settings_snapshot(
            overrides={URL: url_override, KEY: "CALLER"}
        )
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "https://api.openai.com:443"
        )
        assert resolve_openai_endpoint_override_key(
            "https://api.openai.com/v1", snapshot
        ) == ("CALLER", False)

        snapshot = create_settings_snapshot(
            overrides={URL: url_override, KEY: "CALLER"},
            base_settings=_envelope_snapshot(),
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == "https://api.openai.com/v1"
        assert kwargs["api_key"] == "CALLER"
        assert RESEARCH_KEY not in repr(snapshot)

    def test_a_same_call_key_is_owned_by_its_url_not_the_env_url(
        self, monkeypatch
    ):
        """A same call key is owned by its url not the env url."""
        monkeypatch.setenv(
            "LDR_LLM_OPENAI_ENDPOINT_URL", "http://localhost:9100/v1"
        )
        snapshot = create_settings_snapshot(
            overrides={URL: OTHER_URL, KEY: "sk-with-the-url"},
            base_settings=_envelope_snapshot(),
        )
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:8000"
        )

        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL")
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == "sk-with-the-url"


class TestEnvironmentKeyWithoutAnEnvUrl:
    """``LDR_LLM_OPENAI_ENDPOINT_API_KEY`` alone follows the saved URL only.

    A plain ``get_llm()`` sends the env key to the saved
    ``llm.openai_endpoint.url`` (``test_env_key_reaches_the_stored_endpoint``);
    every binding path must give the same answer for that destination and
    withhold the key from any other.
    """

    ENV_KEY = "sk-operator-env-key"

    @pytest.fixture(autouse=True)
    def _env(self, monkeypatch):
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", self.ENV_KEY)
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL", raising=False)

    def test_web_research_on_the_saved_url_keeps_the_env_key(self):
        """The web route always passes the saved URL as ``custom_endpoint``."""
        kwargs = _web_dispatch_client_kwargs(STORED_URL)
        assert kwargs["base_url"] == STORED_URL
        assert kwargs["api_key"] == self.ENV_KEY

    def test_custom_endpoint_elsewhere_withholds_the_env_key(self):
        kwargs = _web_dispatch_client_kwargs(OTHER_URL)
        assert kwargs["base_url"] == OTHER_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER
        assert self.ENV_KEY not in repr(kwargs)

    def test_only_the_saved_origin_is_allowed(self):
        snapshot = _envelope_snapshot(key="db-key")
        assert resolve_openai_endpoint_override_key(
            SAME_ORIGIN_URL, snapshot
        ) == (self.ENV_KEY, False)
        for url in (
            "https://openrouter.ai/api/v1",
            "https://api.openai.com/v1",
        ):
            assert resolve_openai_endpoint_override_key(url, snapshot) == (
                "",
                True,
            )

    def test_a_call_marker_does_not_stand_in_for_the_saved_url(self):
        """A call marker does not stand in for the saved url."""
        from local_deep_research.config.openai_endpoint_binding import (
            bind_openai_endpoint_override,
        )

        snapshot = _envelope_snapshot(key="db-key")
        bind_openai_endpoint_override(
            snapshot, OTHER_URL, explicit_key="sk-call-key"
        )
        snapshot[KEY] = self.ENV_KEY

        kwargs = _client_kwargs(snapshot, openai_endpoint_url=OTHER_URL)
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_an_owner_marker_without_a_saved_record_fails_closed(self):
        """An owner marker without a saved record fails closed."""
        snapshot = {
            URL: "https://call.example/v1",
            KEY: {"value": "", "ui_element": "password"},
            OPENAI_ENDPOINT_KEY_OWNER_MARKER: "https://call.example:443",
        }
        assert resolve_openai_endpoint_override_key(
            "https://call.example/v1", snapshot
        ) == ("", True)

    def test_a_non_canonical_saved_record_is_refused(self):
        """Only the binder's ``scheme://host:port`` saved record is trusted."""
        from local_deep_research.config.openai_endpoint_binding import (
            OPENAI_ENDPOINT_SAVED_URL_MARKER,
        )

        snapshot = _envelope_snapshot(key="")
        snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] = "http://localhost:9000"
        snapshot[OPENAI_ENDPOINT_SAVED_URL_MARKER] = "https://call.example/v1"
        assert resolve_openai_endpoint_override_key(
            "https://call.example/v1", snapshot
        ) == ("", True)

    def test_an_empty_saved_url_means_the_provider_default(self):
        """An empty saved url means the provider default."""
        snapshot = _envelope_snapshot(url="", key="")
        assert resolve_openai_endpoint_override_key(
            "https://api.openai.com/v1", snapshot
        ) == (self.ENV_KEY, False)
        assert resolve_openai_endpoint_override_key(
            "https://openrouter.ai/api/v1", snapshot
        ) == ("", True)

    def test_a_missing_key_entry_counts_as_the_env_key(self):
        """A missing key entry counts as the env key."""
        from local_deep_research.config.openai_endpoint_binding import (
            _key_is_env_supplied,
        )

        assert _key_is_env_supplied({URL: STORED_URL}) is True
        assert _key_is_env_supplied({URL: STORED_URL, KEY: "sk-raw"}) is False


class TestSameCallKeyOwnerAndTheEnvironment:
    """The same-call key's owner for an emptied or blank URL override."""

    def test_an_env_key_does_not_replace_the_same_call_key(self, monkeypatch):
        """An env key does not replace the same call key."""
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", "sk-env-key")
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL", raising=False)
        snapshot = create_settings_snapshot(
            overrides={URL: "", KEY: "CALLER"},
            base_settings=_envelope_snapshot(),
        )
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "https://api.openai.com:443"
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == "https://api.openai.com/v1"
        assert kwargs["api_key"] == "CALLER"

    def test_an_emptied_url_under_an_env_url_is_owned_by_the_env_url(
        self, monkeypatch
    ):
        """An emptied url under an env url is owned by the env url."""
        monkeypatch.setenv(
            "LDR_LLM_OPENAI_ENDPOINT_URL", "http://localhost:9100/v1"
        )
        snapshot = create_settings_snapshot(
            overrides={URL: "", KEY: "CALLER"},
            base_settings=_envelope_snapshot(),
        )
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:9100"
        )
        kwargs = _client_kwargs(snapshot)
        assert kwargs["base_url"] == "http://localhost:9100/v1"
        assert kwargs["api_key"] == "CALLER"

    def test_a_whitespace_url_override_records_no_owner(self, monkeypatch):
        """A blank URL the provider cannot reach owns nothing."""
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL", raising=False)
        snapshot = create_settings_snapshot(
            overrides={URL: "   ", KEY: "CALLER"},
            base_settings=_envelope_snapshot(),
        )
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == ""
        assert resolve_openai_endpoint_override_key(
            "https://api.openai.com/v1", snapshot
        ) == ("", True)


class TestRecordingOnTheThreadContextPath:
    """A transient env URL is never recorded, even when the fallback is active.

    With ``settings_snapshot=None`` (get_llm binds the binder on an empty
    copy) or a snapshot without its own URL entry, the owner resolves
    through ``get_setting_from_snapshot``'s thread-settings-context
    fallback — the same source the key read uses. A context that applies
    live ``LDR_*`` overrides on read (as the DB-backed settings managers
    do) then surfaces the operator's ``LDR_LLM_OPENAI_ENDPOINT_URL``.
    That value is fine for a live decision — ``_key_may_follow``'s env rule
    grants exactly its origin while the variable is set and nothing after —
    but the owner and saved-URL MARKERS are records that outlive the
    variable, so the recording path must fail closed to "no owner" instead
    of writing the env origin down.
    """

    ENV_URL = "http://localhost:9100/v1"

    @pytest.fixture(autouse=True)
    def _env(self, monkeypatch):
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_URL", self.ENV_URL)
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", raising=False)

    def test_transient_env_url_is_never_recorded_as_owner_or_saved_url(self):
        from local_deep_research.api.settings_utils import (
            InMemorySettingsManager,
        )
        from local_deep_research.config.openai_endpoint_binding import (
            OPENAI_ENDPOINT_SAVED_URL_MARKER,
            _configured_openai_endpoint_url,
            bind_openai_endpoint_override,
        )
        from local_deep_research.config.thread_settings import settings_context

        with settings_context(InMemorySettingsManager()):
            # The fallback is active and the context surfaces the live
            # env URL (that is what a DB-backed context returns under an
            # LDR_* override) ...
            assert _configured_openai_endpoint_url({}) == self.ENV_URL

            snapshot = {}
            bind_openai_endpoint_override(snapshot, OTHER_URL)

        # ... but no record names its origin.
        env_origin = "http://localhost:9100"
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == ""
        assert snapshot[OPENAI_ENDPOINT_SAVED_URL_MARKER] == ""
        assert env_origin not in repr(snapshot)

    def test_a_same_origin_call_key_still_binds_with_the_fallback_active(self):
        """The fallback keeps working for values that are not the env URL.

        The suppression is about the transient env value only: the
        explicit-key branch records the URL the key was named for, and the
        env policy still applies at decision time.
        """
        from local_deep_research.api.settings_utils import (
            InMemorySettingsManager,
        )
        from local_deep_research.config.openai_endpoint_binding import (
            bind_openai_endpoint_override,
        )
        from local_deep_research.config.thread_settings import settings_context

        with settings_context(InMemorySettingsManager()):
            snapshot = {}
            bind_openai_endpoint_override(
                snapshot, OTHER_URL, explicit_key="sk-with-the-url"
            )

        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
            "http://localhost:8000"
        )
        assert snapshot[KEY] == "sk-with-the-url"


class TestEmptyConfiguredUrlWithNoEnvUrl:
    """Empty Configured Url With No Env Url."""

    CONTEXT_KEY = "sk-context-key"
    DEFAULT_URL = "https://api.openai.com/v1"
    DEFAULT_ORIGIN = "https://api.openai.com:443"

    @pytest.mark.parametrize("endpoint_url", [DEFAULT_URL, ""])
    def test_the_provider_default_is_recorded_and_the_key_survives(
        self, endpoint_url, monkeypatch
    ):
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL", raising=False)
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", raising=False)
        from local_deep_research.api.settings_utils import (
            InMemorySettingsManager,
        )
        from local_deep_research.config.thread_settings import settings_context

        manager = InMemorySettingsManager()
        manager.set_setting("llm.openai_endpoint.url", "")
        manager.set_setting("llm.openai_endpoint.api_key", self.CONTEXT_KEY)
        with settings_context(manager):
            snapshot = {}
            bind_openai_endpoint_override(snapshot, endpoint_url)

            # The empty configured URL IS the provider default, so that is
            # what the markers must record — not "" ("no owner").
            assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == (
                self.DEFAULT_ORIGIN
            )
            assert snapshot[OPENAI_ENDPOINT_SAVED_URL_MARKER] == (
                self.DEFAULT_ORIGIN
            )
            # The key is owned by the endpoint it was configured for ...
            assert resolve_openai_endpoint_override_key(
                self.DEFAULT_URL, snapshot
            ) == (self.CONTEXT_KEY, False)

            # ... the re-check performed by every later bind keeps it ...
            bind_openai_endpoint_override(snapshot, None)
            assert snapshot.get(KEY) in (None, self.CONTEXT_KEY)

            # ... and resolve keeps working at the default origin.
            assert resolve_openai_endpoint_override_key(
                self.DEFAULT_URL, snapshot
            ) == (self.CONTEXT_KEY, False)


class TestEnvValuesAreNeverRecordedAsStoredOwnership:
    """Env Values Are Never Recorded As Stored Ownership."""

    ENV_URL = "http://localhost:9100/v1"
    ENV_ORIGIN = "http://localhost:9100"

    @pytest.fixture(autouse=True)
    def _env(self, monkeypatch):
        monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_URL", self.ENV_URL)
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", raising=False)

    def _assert_no_env_record(self, snapshot, monkeypatch):
        # The stored key is still there — only its ownership record is in
        # question.
        assert snapshot[KEY]["value"] == RESEARCH_KEY
        # Operator policy while the variable is set is untouched ...
        assert resolve_openai_endpoint_override_key(self.ENV_URL, snapshot) == (
            RESEARCH_KEY,
            False,
        )
        # ... but no marker may name the env origin for a stored key.
        assert snapshot[OPENAI_ENDPOINT_KEY_OWNER_MARKER] == ""
        assert snapshot[OPENAI_ENDPOINT_SAVED_URL_MARKER] == ""
        assert self.ENV_ORIGIN not in repr(snapshot)
        # Once the variable is gone, nothing recorded authorises its
        # former origin: the stored key is withheld from it.
        monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL")
        assert resolve_openai_endpoint_override_key(self.ENV_URL, snapshot) == (
            "",
            True,
        )

    def test_a_value_an_env_pass_wrote_into_the_entry_is_not_recorded(
        self, monkeypatch
    ):
        """The documented env pass writes env values into raw entries.

        ``apply_environment_overrides_to_snapshot`` materialising an
        ``LDR_*`` value into a snapshot entry is relied-on behaviour
        elsewhere; the binder must not read that value "as stored" into
        its ownership records.
        """
        from local_deep_research.settings.manager import (
            apply_environment_overrides_to_snapshot,
        )

        base = apply_environment_overrides_to_snapshot(_envelope_snapshot())
        assert base[URL]["value"] == self.ENV_URL

        snapshot = create_settings_snapshot(
            overrides={URL: OTHER_URL}, base_settings=base
        )
        self._assert_no_env_record(snapshot, monkeypatch)

    def test_a_materialized_env_value_is_not_recorded(self, monkeypatch):
        """``create_settings_snapshot`` materialises the env-applied URL
        into a missing ``llm.openai_endpoint.url`` entry one line before
        the binder reads it "as stored" (api/settings_utils.py).
        """
        from local_deep_research.api.settings_utils import (
            InMemorySettingsManager,
        )
        from local_deep_research.config.thread_settings import settings_context

        with settings_context(InMemorySettingsManager()):
            snapshot = create_settings_snapshot(
                overrides={URL: OTHER_URL},
                base_settings={
                    KEY: {"value": RESEARCH_KEY, "ui_element": "password"}
                },
            )
        self._assert_no_env_record(snapshot, monkeypatch)


def test_bound_snapshot_keeps_its_configured_key_across_environment_changes(
    monkeypatch,
):
    from local_deep_research.config.openai_endpoint_binding import (
        bind_openai_endpoint_override,
    )
    from local_deep_research.settings.manager import (
        apply_environment_overrides_to_snapshot,
    )

    monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", raising=False)
    monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL", raising=False)
    snapshot = _envelope_snapshot()
    bind_openai_endpoint_override(snapshot, SAME_ORIGIN_URL, keep_envelope=True)
    monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY", "synthetic-env-key")
    monkeypatch.setenv("LDR_LLM_OPENAI_ENDPOINT_URL", OTHER_URL)
    dispatched = apply_environment_overrides_to_snapshot(snapshot)
    assert resolve_openai_endpoint_override_key(OTHER_URL, dispatched) == (
        "synthetic-env-key",
        False,
    )

    monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_API_KEY")
    monkeypatch.delenv("LDR_LLM_OPENAI_ENDPOINT_URL")
    bind_openai_endpoint_override(dispatched, STORED_URL)
    assert resolve_openai_endpoint_override_key(STORED_URL, dispatched) == (
        RESEARCH_KEY,
        False,
    )