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.

81.8 KB
Raw Blame
"""Regression tests for #6305: request-body values in the research router's
log lines must go through the bounded preview helper from #6191, not an
eager f-string.

``_extract_research_params`` and ``_start_research_sync``
(``web/routers/research.py``) interpolated the body-supplied ``model``
into eager f-strings at three points of one request (``Using model from
request``, ``Extracted model value`` and the ``Starting research with
provider ...`` info line), and the other raw body values logged on that
path (``model_provider``, ``search_engine``, ``iterations``,
``questions_per_iteration``, ``strategy``, the news-search
``triggered_by``, the body's key list on the ``Request data keys`` line)
had the same shape. So did ``save_research_strategy``
(``web/services/research_service.py``), which ``_start_research_sync``
calls synchronously with the same raw ``strategy`` and which f-stringed
it at DEBUG and INFO. An f-string is evaluated before loguru sees it, so
the FULL ``str()`` of the value was built on every request regardless of
log level, and the info lines are emitted at the default level into
every sink. For a wide dict/list posted as ``{"model": {...}}`` that is
full serialisation work per request, the same shape #6191 bounded for
the notes router's log-value previews.

Same policy as ``tests/notes/test_log_value_preview_dict_bound.py``:
``isinstance(preview, str)`` / ``len(preview) <= 100`` style assertions
are NOT regression guards (``reprlib`` bounds the output even when it
does the full work), so the tests below are written to fail under these
weakenings instead:

  (W1) un-wiring the fix at any call site: back to an eager f-string in
       ``_extract_research_params``, ``_start_research_sync`` or
       ``save_research_strategy``, or any other eager stringification
       of a body value added next to the bounded call (e.g.
       ``_audit = repr(search_engine)``)
  (W2) re-pointing the imported name at a naive helper (e.g.
       ``repr(value)[:100]``), which touches every element again
  (W3) settings router only: reintroducing the eager
       ``f"Converting checkbox ... {value}"`` message that #6201
       replaced with the value-free key/type/length line

No HTTP client / DB needed: the source guards run against the imported
modules directly, and the call-path probes mock the per-user session and
the settings manager at their import seams (the idiom of
``test_start_research_ssrf.py``), so everything stays fast and
deterministic. Together the call-path probes feed a wide value to every
value behind a bounded call in ``_BOUNDED_BODY_VALUE_CALLS`` on each line
they reach, except ``iterations`` and ``questions_per_iteration`` on the
``Additional parameters`` line: ``_extract_research_params`` range-checks
both before that line, so the below-return probe sends valid ints. So
(W1) for any other value at a site a probe reaches, not just the named
ones, fails that probe and not only a source guard. The ``No model
specified or configured`` line runs only when no model resolves, which no
probe reaches, so only its source guard pins it.
"""

import asyncio
import inspect
import json
from contextlib import contextmanager
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from local_deep_research.followup_research import (
    service as followup_service_module,
)
from local_deep_research.web.routers import api as api_module
from local_deep_research.web.routers import followup as followup_module
from local_deep_research.web.routers import notes as notes_module
from local_deep_research.web.routers import research as research_module
from local_deep_research.web.routers import settings as settings_module
from local_deep_research.web.services import (
    research_service as research_service_module,
)

# Local (inside-function) import in ``_start_research_sync``: patch it at
# its source module, as ``test_start_research_ssrf.py`` does.
_SETTINGS_MANAGER = "local_deep_research.settings.SettingsManager"

# The eager f-string logs of request-body values the two functions used to
# carry (by their opening text), and the bounded calls that replaced them.
_EAGER_BODY_VALUE_LOGS = {
    "_extract_research_params": (
        'f"Using model_provider from request',
        'f"Using model from request',
    ),
    "_start_research_sync": (
        'f"Request data keys',
        'f"News search request received',
        'f"Extracted model value',
        'f"Starting research with provider',
        'f"Additional parameters',
        'f"No model specified or configured',
    ),
    "save_research_strategy": (
        'f"save_research_strategy called',
        'f"Saved strategy',
    ),
}
_BOUNDED_BODY_VALUE_CALLS = {
    "_extract_research_params": (
        "_log_value_preview(model_provider)",
        "_log_value_preview(model)",
    ),
    "_start_research_sync": (
        "_log_value_preview(list(data.keys()))",
        '_log_value_preview(metadata.get("triggered_by", "unknown"))',
        "_log_value_preview(model)",
        "_log_value_preview(model_provider)",
        "_log_value_preview(search_engine)",
        "_log_value_preview(iterations)",
        "_log_value_preview(questions_per_iteration)",
        "_log_value_preview(strategy)",
    ),
    "save_research_strategy": ("_log_value_preview(strategy_name)",),
}


class CountingValue:
    """Dict/list element that counts how many times it is repr-ed.

    A direct *work* bound with no timing flakiness: an eager f-string (or
    a naive ``repr(value)[:100]``) stringifies every element, while the
    bounded preview walk hands ``reprlib`` at most 11 of them.
    """

    reprs = 0

    def __init__(self, n: int) -> None:
        self.n = n

    def __repr__(self) -> str:
        type(self).reprs += 1
        return f"V{self.n}"


def _wide(n: int = 50_000) -> dict:
    """A wide dict body value: 50,000 entries, each counting its reprs."""
    return {i: CountingValue(i) for i in range(n)}


class WideNameValue(dict):
    """A wide dict body value for the two fields the code treats as names.

    The request-type guard (``research._non_string_request_field``) rejects
    any non-string ``model_provider`` or ``search_engine`` before either is
    logged, so the below-return probe patches it out. Past the guard,
    ``normalize_provider`` calls ``.lower()`` on ``model_provider`` (after
    its ``Using model_provider from request`` line) and the collection
    precheck calls ``.startswith`` on ``search_engine``, both before the
    ``Starting research with provider`` line, so a plain wide dict would
    raise there and never reach that log site. This double answers both
    calls (``.lower()`` returns the value itself, ``.startswith`` returns
    False) so the real code carries it to the log lines unchanged. It is
    still a ``dict`` to the preview's bounding walk, which copies it into a
    plain 11-entry dict before ``reprlib`` sees it, so only an eager
    ``repr``/f-string of the value stringifies every entry.
    """

    def lower(self):
        return self

    def startswith(self, prefix):
        return False


def _wide_name(n: int = 50_000) -> WideNameValue:
    return WideNameValue((i, CountingValue(i)) for i in range(n))


def _settings_manager():
    """SettingsManager mock backed by a lookup table.

    Same idiom as ``test_start_research_ssrf.py``. The snapshot is NOT a
    dict, so ``_precheck_engine_policy`` skips (its documented behaviour
    for test doubles); the egress-policy precheck has its own suite.
    """
    lookup = {
        "llm.provider": "ollama",
        "llm.model": "gpt-4",
        "llm.ollama.url": "http://localhost:11434",
        "search.tool": "searxng",
        "search.iterations": 5,
        "search.questions_per_iteration": 5,
        "search.search_strategy": "source-based",
    }
    sm = MagicMock()
    sm.get_setting.side_effect = lambda key, default=None: lookup.get(
        key, default
    )
    sm.get_settings_snapshot.return_value = MagicMock()
    return sm


class TestBodyValueLogSites:
    @pytest.mark.parametrize(
        "function_name",
        [
            "_extract_research_params",
            "_start_research_sync",
            "save_research_strategy",
        ],
    )
    def test_body_values_are_logged_through_the_bounded_preview(
        self, function_name
    ):
        """Catches (W1) at each call site: the wiring itself.

        Same style as ``test_async_handlers_offload.py``: the function's
        source must route every request-body value it logs through the
        helper, and none of the eager f-strings may come back.
        """
        module = (
            research_service_module
            if function_name == "save_research_strategy"
            else research_module
        )
        source = inspect.getsource(getattr(module, function_name))
        for eager in _EAGER_BODY_VALUE_LOGS[function_name]:
            assert eager not in source, (
                f"{function_name} interpolates a request-body value into an "
                f"eager f-string again ({eager}...) (#6305)"
            )
        for bounded in _BOUNDED_BODY_VALUE_CALLS[function_name]:
            assert bounded in source, (
                f"{function_name} no longer routes a request-body value "
                f"through the bounded log preview ({bounded}) (#6305)"
            )

    def test_research_binds_the_audited_notes_helper(self):
        """Catches (W2) via identity.

        The name research.py calls must BE the #6191 helper (whose bounds
        ``tests/notes/test_log_value_preview_dict_bound.py`` proves), not
        a lookalike local shim.
        """
        assert (
            research_module._log_value_preview
            is notes_module._log_value_preview
        )

    def test_wide_dict_model_value_is_not_fully_reprd(self):
        """Catches (W2): a work bound through the research-bound name.

        Measured: 10 element reprs with the bounded preview (reprlib
        renders one bounded 11-entry copy), 50,000 under an eager
        f-string or a naive ``repr(value)[:100]``.
        """
        wide = _wide()
        CountingValue.reprs = 0
        preview = research_module._log_value_preview(wide)
        assert CountingValue.reprs < 100
        # The preview must still record that entries were omitted.
        assert preview.endswith(", ...}")
        assert len(preview) <= 100

    def test_large_list_model_value_is_not_fully_reprd(self):
        """Catches (W2) for the list shape of the same body value."""
        big = [CountingValue(i) for i in range(50_000)]
        CountingValue.reprs = 0
        preview = research_module._log_value_preview(big)
        assert CountingValue.reprs < 100
        assert preview.endswith(", ...]")
        assert len(preview) <= 100

    def test_extract_research_params_does_not_repr_every_model_entry(self):
        """Catches (W1) and (W2) on the extraction path, through the real
        call.

        Measured: 10 element reprs with the bounded preview, 50,000 with
        the eager ``Using model from request`` f-string.
        """
        wide = _wide()
        CountingValue.reprs = 0
        params = research_module._extract_research_params(
            {"model": wide}, _settings_manager()
        )
        assert params["model"] is wide
        assert CountingValue.reprs < 100

    def test_start_research_sync_does_not_repr_every_body_entry(self):
        """Catches (W1) and (W2) at the above-early-return value sites,
        through the real call path.

        The body carries a 50,000-entry dict for EVERY raw value the
        function logs (``model``, logged three times along the path,
        ``model_provider`` twice, ``search_engine``, the three
        ``Additional parameters`` values and the news-search
        ``triggered_by``; the two name fields as ``WideNameValue``, as in
        the below-return probe) and no ``query``. Since #6517 the
        missing-query 400 lives at the top of
        ``_start_research_sync`` (before ``_extract_research_params``), so
        this probe only exercises the two log sites above that return
        (``Request data keys``, news ``triggered_by``) and then returns
        the 400 for the missing query. The sites below the return need a
        query to run and are pinned by
        ``test_start_research_sync_does_not_repr_below_return_entries``.
        Measured: ~10 element reprs with the bounded preview (10 per log
        of a wide value that actually runs); one eager stringification at
        a site above the return (e.g. ``_audit = repr(triggered_by)``)
        adds 50,000.
        """
        body = {
            "model_provider": _wide_name(),
            "model": _wide(),
            "search_engine": _wide_name(),
            "iterations": _wide(),
            "questions_per_iteration": _wide(),
            "strategy": _wide(),
            "metadata": {"is_news_search": True, "triggered_by": _wide()},
        }

        @contextmanager
        def _session_ctx(*args, **kwargs):
            yield MagicMock()

        CountingValue.reprs = 0
        with (
            patch.object(research_module, "get_user_db_session", _session_ctx),
            patch(_SETTINGS_MANAGER, return_value=_settings_manager()),
        ):
            response = research_module._start_research_sync(
                body, "testuser", "http://testserver/", None
            )

        assert response.status_code == 400
        assert json.loads(response.body) == {
            "status": "error",
            "message": "Query is required",
        }
        assert CountingValue.reprs < 200

    def test_start_research_sync_does_not_repr_below_return_entries(self):
        """Catches (W1) and (W2) at the below-early-return value sites,
        through the real call path (#6517 follow-up).

        The no-query probe above returns before ``_extract_research_params``,
        so the ``Extracted model value``, ``Starting research with
        provider``, and ``Additional parameters`` lines never run there (a
        planted eager ``repr()`` below the return escapes it). This probe
        posts a query plus wide ``model_provider``, ``model``,
        ``search_engine`` and ``strategy`` values (``iterations`` /
        ``questions_per_iteration`` stay valid ints because
        ``_extract_research_params`` range-checks them) and forces the
        query-length gate to refuse right after the log lines, so every
        below-return preview runs and nothing heavier does. The request-type
        guard ``_non_string_request_field`` would reject the wide provider,
        engine and strategy before any of those lines, so it is patched out
        here; ``test_provider_request_types.py`` tests the guard itself.
        Measured: 40 element reprs with the bounded preview (10 per log of
        a wide value), 250,000+ with the eager f-strings; one eager
        stringification of any single wide below-return value (e.g.
        ``_audit = repr(search_engine)``) adds 50,000.
        """
        body = {
            "query": "probe query",
            "model_provider": _wide_name(),
            "model": _wide(),
            "search_engine": _wide_name(),
            "iterations": 5,
            "questions_per_iteration": 5,
            "strategy": _wide(),
        }

        @contextmanager
        def _session_ctx(*args, **kwargs):
            yield MagicMock()

        CountingValue.reprs = 0
        with (
            patch.object(research_module, "get_user_db_session", _session_ctx),
            patch(_SETTINGS_MANAGER, return_value=_settings_manager()),
            patch.object(
                research_module, "_non_string_request_field", return_value=None
            ),
            patch.object(
                research_module,
                "validate_research_query_length",
                return_value="Query too long",
            ),
        ):
            response = research_module._start_research_sync(
                body, "testuser", "http://testserver/", None
            )

        assert response.status_code == 400
        assert json.loads(response.body) == {
            "status": "error",
            "message": "Query too long",
        }
        # Lower bound: the below-return preview sites must actually run.
        # Measured 40 element reprs (10 per wide-value log site); if the
        # query-length gate ever moves above these log lines, the request
        # returns its 400 before any of them and the count drops to 0 —
        # without this floor the ceiling alone would keep passing while
        # exercising nothing (#7109). The floor tolerates losing a single
        # site (>= 3 of 4) but not the gate drifting above the lines.
        assert CountingValue.reprs >= 30
        assert CountingValue.reprs < 200

    def test_start_research_sync_does_not_repr_every_body_key(self):
        """Catches (W1) and (W2) at the ``Request data keys`` line, the
        first line of ``_start_research_sync``, through the real call.

        The body has 50,000 keys (each counting its reprs) and no
        ``query``; the key list is the only wide thing in it, so the count
        isolates that one line. Measured: 10 element reprs with the
        bounded preview, 50,000 with the eager
        ``f"Request data keys: {list(data.keys())}"``.
        """
        body = {CountingValue(i): None for i in range(50_000)}

        @contextmanager
        def _session_ctx(*args, **kwargs):
            yield MagicMock()

        CountingValue.reprs = 0
        with (
            patch.object(research_module, "get_user_db_session", _session_ctx),
            patch(_SETTINGS_MANAGER, return_value=_settings_manager()),
        ):
            response = research_module._start_research_sync(
                body, "testuser", "http://testserver/", None
            )

        assert response.status_code == 400
        assert json.loads(response.body) == {
            "status": "error",
            "message": "Query is required",
        }
        assert CountingValue.reprs < 100

    def test_save_research_strategy_does_not_repr_every_strategy_entry(
        self, loguru_caplog
    ):
        """Catches (W1) and (W2) at both ``save_research_strategy`` sites,
        through the real call.

        ``_start_research_sync`` hands this function the same raw body
        ``strategy`` it just logged, so a wide dict reaches its DEBUG and
        INFO lines. The per-user session and the ORM row are patched at
        the module seams (the idiom of the research_service suites). The
        function swallows every exception, so the captured ``Saved
        strategy`` message is what proves both log lines actually ran.
        Measured: 20 element reprs with the bounded preview (10 per log),
        100,000 with the eager f-strings.
        """
        wide = _wide()
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            None
        )

        @contextmanager
        def _session_ctx(*args, **kwargs):
            yield session

        CountingValue.reprs = 0
        with (
            loguru_caplog.at_level("DEBUG"),
            patch.object(
                research_service_module, "get_user_db_session", _session_ctx
            ),
            patch.object(research_service_module, "ResearchStrategy"),
        ):
            research_service_module.save_research_strategy(
                "research-1", wide, username="testuser"
            )

        session.commit.assert_called_once()
        assert "Saved strategy" in loguru_caplog.text
        assert "Error saving research strategy" not in loguru_caplog.text
        assert CountingValue.reprs < 100


class TestSettingsCheckboxLogSite:
    def test_checkbox_conversion_log_stays_value_free(self):
        """Catches (W3): locks in the already-landed settings fix.

        #6305's other named site. ``_save_all_settings_sync``'s checkbox
        conversion message must keep logging only key, type name and
        length (via loguru brace args), never the submitted value: a
        checkbox row can carry a credential-bearing container, so even a
        bounded preview of the VALUE would disclose too much here (#6201).
        """
        source = inspect.getsource(settings_module._save_all_settings_sync)
        assert 'f"Converting checkbox' not in source, (
            "_save_all_settings_sync interpolates the submitted checkbox "
            "value into an eager f-string again (#6305)"
        )
        assert "Converting checkbox {} from {} (len={}) to bool" in source, (
            "the value-free checkbox conversion message changed; make sure "
            "any replacement still logs only key/type/length, via loguru "
            "brace args (#6201, #6305)"
        )


# The #6938 remainder: the sites #6756's review named as "a natural
# follow-up PR" (the thread-side ``run_research_process`` strategy and
# parameter lines) plus the ones it marked optional (the api router's
# SSRF refusal, the followup router's params/query lines, and the
# config-write ``blocked_keys`` warning), and — once those landed — the
# same-function siblings that would have kept the #6938 changelog claim
# false: ``run_research_process``'s query-entry, override, LLM and
# search-engine lines (including the ``Search Engine Configuration
# Error`` raise) and the followup service's ``Prepared follow-up
# research`` question line.
_EAGER_REMAINDER_LOGS = {
    "run_research_process": (
        'f"Starting research process',
        'f"Research strategy',
        'f"Research parameters',
        'f"Overriding system settings',
        'f"Successfully set LLM',
        'f"Error setting LLM',
        'f"Successfully created search engine',
        'f"Error creating search engine',
        "Search Engine Configuration Error ({search_engine})",
    ),
    "api_add_resource": ('f"SSRF protection: Rejected URL',),
    "_start_followup_sync": (
        'f"Research params keys',
        'f"Query value',
    ),
    "save_raw_config": ('f"Security: Blocked attempt to write config',),
    "perform_followup": (
        'f"Prepared follow-up research',
        'f"Parent research not found',
    ),
    # The not-found paths, where the parent id is still the raw body
    # value (both followup routes reach them before any other check).
    "load_parent_research": ('f"Parent research not found',),
    "prepare_followup": ('f"Parent research {parent_id} not found',),
}
_BOUNDED_REMAINDER_CALLS = {
    "run_research_process": (
        "_log_value_preview(query)",
        "_log_value_preview(strategy)",
        "_log_value_preview(model_provider)",
        "_log_value_preview(model)",
        "_log_value_preview(search_engine)",
    ),
    "api_add_resource": (
        "redact_url_for_log(url)",
        "_log_value_preview(redact_url_for_log(url))",
    ),
    "_start_followup_sync": (
        "_log_value_preview(params_keys)",
        "_log_value_preview(query_value)",
    ),
    "save_raw_config": ("_log_value_preview(blocked_keys)",),
    "perform_followup": (
        "_log_value_preview(request.question)",
        "_log_value_preview(request.parent_research_id)",
    ),
    "load_parent_research": ("_log_value_preview(parent_research_id)",),
    "prepare_followup": ("_log_value_preview(parent_id)",),
}
# The callables the deferred-site source guards inspect (the followup
# site is a service method, so entries are callables, not modules).
_REMAINDER_SOURCES = {
    "run_research_process": research_service_module.run_research_process,
    "api_add_resource": api_module.api_add_resource,
    "_start_followup_sync": followup_module._start_followup_sync,
    "save_raw_config": research_module.save_raw_config,
    "perform_followup": (
        followup_service_module.FollowUpResearchService.perform_followup
    ),
    "load_parent_research": (
        followup_service_module.FollowUpResearchService.load_parent_research
    ),
    "prepare_followup": followup_module.prepare_followup,
}


class _StopAfterParameterLogs(BaseException):
    """Escapes ``run_research_process``'s ``except Exception`` handler.

    Raised by the patched ``apply_environment_overrides_to_snapshot``
    (the first call after the two parameter log lines) so the probe
    proves the lines ran without driving the real research pipeline.
    ``BaseException`` so the function's broad ``except Exception`` /
    ``finally`` cleanup paths do not swallow it.
    """


def _json_request(payload):
    """A ``Request`` double labelled ``application/json`` whose ``json()``
    resolves to ``payload``. The real header matters: routes that check the
    media type before parsing must see a JSON request, not a MagicMock."""
    request = MagicMock()
    request.headers = {"content-type": "application/json"}
    request.json = AsyncMock(return_value=payload)
    return request


def _bounded_line(caplog_text, prefix):
    """The single captured log line starting with ``prefix``."""
    return next(line for line in caplog_text.splitlines() if prefix in line)


class TestDeferredRequestValueLogSites:
    """#6938: the remaining request-derived log sites the #6756 review
    deferred. Same policy as above — the probes feed a wide value to
    each site and fail if the emitted line (or the repr work behind it)
    is unbounded again."""

    @pytest.mark.parametrize("function_name", sorted(_EAGER_REMAINDER_LOGS))
    def test_body_values_are_logged_through_the_bounded_preview(
        self, function_name
    ):
        """Catches (W1) at each deferred call site: the wiring itself."""
        source = inspect.getsource(_REMAINDER_SOURCES[function_name])
        for eager in _EAGER_REMAINDER_LOGS[function_name]:
            assert eager not in source, (
                f"{function_name} interpolates a request-derived value "
                f"into an eager f-string again ({eager}...) (#6938)"
            )
        for bounded in _BOUNDED_REMAINDER_CALLS[function_name]:
            assert bounded in source, (
                f"{function_name} no longer routes a request-derived "
                f"value through the bounded log preview "
                f"({bounded}) (#6938)"
            )

    def test_routers_bind_the_audited_notes_helper(self):
        """Catches (W2) via identity for the two newly-bound routers."""
        assert api_module._log_value_preview is notes_module._log_value_preview
        assert (
            followup_module._log_value_preview
            is notes_module._log_value_preview
        )

    def test_add_resource_rejected_url_log_is_bounded(self, loguru_caplog):
        """A rejected non-string URL never reaches a log formatter or repr."""
        wide = _wide()
        request = _json_request({"title": "t", "url": wide})

        CountingValue.reprs = 0
        with (
            loguru_caplog.at_level("WARNING"),
            patch(
                "local_deep_research.security.ssrf_validator.validate_url",
                return_value=False,
            ),
        ):
            response = asyncio.run(
                api_module.api_add_resource(request, "research-1", "testuser")
            )

        assert response.status_code == 400
        line = _bounded_line(
            loguru_caplog.text, "SSRF protection: Rejected URL"
        )
        assert len(line) < 500
        assert line.endswith("(non-string)")
        assert CountingValue.reprs == 0

    def test_add_resource_rejected_url_log_hides_credentials_and_bounds_host(
        self, loguru_caplog
    ):
        secret = "credential-do-not-log"
        url = (
            f"https://alice:{secret}@{'a' * 50_000}.example/private"
            f"?token={secret}"
        )
        request = _json_request({"title": "t", "url": url})

        with (
            loguru_caplog.at_level("WARNING"),
            patch(
                "local_deep_research.security.ssrf_validator.validate_url",
                return_value=False,
            ),
        ):
            response = asyncio.run(
                api_module.api_add_resource(request, "research-1", "testuser")
            )

        assert response.status_code == 400
        line = _bounded_line(
            loguru_caplog.text, "SSRF protection: Rejected URL"
        )
        assert len(line) < 500
        assert secret not in line
        assert "alice" not in line
        assert "/private" not in line

    def test_model_pricing_failure_does_not_log_exception_text(
        self, loguru_caplog_full
    ):
        from local_deep_research.web.routers.metrics import api_model_pricing

        secret = "pricing-exception-secret"
        model = f"https://alice:{secret}@models.example/private?token={secret}"
        calculator = (
            "local_deep_research.metrics.pricing.cost_calculator.CostCalculator"
        )
        with (
            loguru_caplog_full.at_level("ERROR"),
            patch(calculator, side_effect=RuntimeError(secret)),
        ):
            response = api_model_pricing(MagicMock(), model, username="alice")

        assert response.status_code == 500
        assert "Error getting pricing for model" in loguru_caplog_full.text
        assert "RuntimeError" in loguru_caplog_full.text
        assert secret not in loguru_caplog_full.text

    def test_start_followup_sync_params_logs_are_bounded(self, loguru_caplog):
        """Catches (W1)/(W2) at the followup params/query lines, through
        the real call.

        ``perform_followup`` is patched to return a 50,001-entry dict
        (wide keys plus a wide ``query`` value), so both the keys line
        and the query line interpolate wide data. The settings snapshot
        reports an empty ``llm.model`` so the function returns its 400
        right after the four log lines, before spawning anything.
        Measured: 22 element reprs with the bounded preview, 100,000
        with the eager f-strings.
        """
        research_params = {CountingValue(i): None for i in range(50_000)}
        research_params["query"] = _wide()
        service = MagicMock()
        service.load_parent_research.return_value = object()
        service.perform_followup.return_value = research_params
        settings_manager = MagicMock()
        settings_manager.get_all_settings.return_value = {
            "llm.model": {"value": ""}
        }

        @contextmanager
        def _session_ctx(*args, **kwargs):
            yield MagicMock()

        CountingValue.reprs = 0
        with (
            loguru_caplog.at_level("INFO"),
            patch.object(
                followup_module, "FollowUpResearchService", return_value=service
            ),
            patch.object(
                followup_module,
                "resolve_user_password",
                return_value=(None, False),
            ),
            patch(
                "local_deep_research.settings.manager.SettingsManager",
                return_value=settings_manager,
            ),
            patch(
                "local_deep_research.database.session_context"
                ".get_user_db_session",
                _session_ctx,
            ),
        ):
            response = followup_module._start_followup_sync(
                {"parent_research_id": "r-1", "question": "q"}, "testuser"
            )

        assert response.status_code == 400
        assert (
            len(_bounded_line(loguru_caplog.text, "Research params keys")) < 500
        )
        assert len(_bounded_line(loguru_caplog.text, "Query value")) < 500
        assert CountingValue.reprs < 100

    def test_save_raw_config_blocked_keys_log_is_bounded(self, loguru_caplog):
        """Catches (W1)/(W2) at the blocked-keys warning, through the
        real call.

        The raw TOML carries as many blocked keys as the 403 lists at
        most (every key matches the ``module`` pattern), so
        ``_find_blocked_keys`` returns its longest list and the warning
        line interpolates all of it. No DB or file access happens before
        the 403 return.

        #7059 changed this test's premise (correct new behaviour, stale
        test). It sent 50,000 keys, about 1 MB of TOML, which
        save_raw_config now refuses with a 400 before parsing, since a
        configuration may be at most 512 KiB; and the 403 now lists at
        most ``_MAX_REPORTED_BLOCKED_KEYS`` keys. That many keys (100
        today) take about 2 KB of TOML, and the repr of their list is
        still far longer than the line bound asserted below.
        """
        count = research_module._MAX_REPORTED_BLOCKED_KEYS
        raw_config = "\n".join(f"module_path_{i} = 1" for i in range(count))
        request = _json_request({"raw_config": raw_config})

        with loguru_caplog.at_level("WARNING"):
            response = asyncio.run(
                research_module.save_raw_config(request, "testuser")
            )

        assert response.status_code == 403
        body = json.loads(response.body)
        assert len(body["blocked_keys"]) == count
        assert body["blocked_keys_truncated"] is False
        assert len(repr(body["blocked_keys"])) > 1000, (
            "the list is too short for the line bound below to mean anything"
        )
        line = _bounded_line(
            loguru_caplog.text,
            "Security: Blocked attempt to write config",
        )
        assert len(line) < 500

    def test_run_research_process_parameter_logs_are_bounded(
        self, loguru_caplog
    ):
        """Catches (W1)/(W2) at the thread-side strategy and parameter
        lines, through the real call.

        Every body-derived parameter the two INFO lines interpolate is
        a 50,000-entry dict. ``apply_environment_overrides_to_snapshot``
        (the first call after the lines) raises ``_StopAfterParameterLogs``,
        a ``BaseException`` the function's ``except Exception`` handler
        cannot swallow, so the probe stops before the research pipeline.
        The query is a short string so the count isolates the two
        parameter lines (the query's own entry line, bounded since the
        #6938 sibling fix, has its own probe below). Measured: 20
        element reprs with the bounded preview (10 per line), 500,000
        with the eager f-strings.
        """
        wide = _wide()
        CountingValue.reprs = 0
        with (
            loguru_caplog.at_level("INFO"),
            patch.object(research_service_module, "set_search_context"),
            patch.object(
                research_service_module,
                "apply_environment_overrides_to_snapshot",
                side_effect=_StopAfterParameterLogs,
            ),
            patch.object(research_service_module, "_sio_emit"),
            patch.object(research_service_module, "cleanup_research_resources"),
            patch(
                "local_deep_research.web.research_state"
                ".is_termination_requested",
                return_value=False,
            ),
            patch("local_deep_research.settings.logger.log_settings"),
        ):
            with pytest.raises(_StopAfterParameterLogs):
                research_service_module.run_research_process(
                    "research-1",
                    "q",
                    "quick",
                    username="testuser",
                    model_provider=wide,
                    model=wide,
                    search_engine=wide,
                    max_results=wide,
                    time_period=wide,
                    iterations=wide,
                    questions_per_iteration=wide,
                    custom_endpoint=wide,
                    strategy=wide,
                )

        assert (
            len(_bounded_line(loguru_caplog.text, "Research strategy:")) < 500
        )
        assert (
            len(_bounded_line(loguru_caplog.text, "Research parameters:"))
            < 2000
        )
        assert CountingValue.reprs < 200

    def test_run_research_process_entry_and_override_logs_are_bounded(
        self, loguru_caplog
    ):
        """Catches (W1)/(W2) at the query-entry line and the
        same-function ``Overriding system settings`` sibling that kept
        the #6938 changelog claim false, through the real call.

        The query is a 100,000-char string and every override value
        (``model_provider``, ``model``, ``search_engine``) a
        50,000-entry dict; ``get_llm`` — the first call after the
        ``Overriding`` line — raises ``_StopAfterParameterLogs`` so the
        probe stops before the LLM/search pipeline. Measured: 60
        element reprs with the bounded preview (10 per wide value per
        line), 300,000+ with the eager f-strings; both emitted lines
        stay under a few hundred chars instead of growing with the
        body.
        """
        wide = _wide()
        CountingValue.reprs = 0
        with (
            loguru_caplog.at_level("INFO"),
            patch.object(research_service_module, "set_search_context"),
            patch.object(
                research_service_module,
                "apply_environment_overrides_to_snapshot",
                lambda snapshot: snapshot,
            ),
            patch.object(
                research_service_module,
                "get_llm",
                side_effect=_StopAfterParameterLogs,
            ),
            patch.object(research_service_module, "_sio_emit"),
            patch.object(research_service_module, "cleanup_research_resources"),
            patch(
                "local_deep_research.web.research_state"
                ".is_termination_requested",
                return_value=False,
            ),
            patch("local_deep_research.settings.logger.log_settings"),
            patch(
                "local_deep_research.config.thread_settings"
                ".set_settings_context",
            ),
            patch("local_deep_research.security.set_active_context"),
            patch(
                "local_deep_research.security.egress.policy"
                ".build_run_egress_context",
            ),
            patch(
                "local_deep_research.security.egress.run_classification"
                ".audit_run_from_snapshot",
            ),
        ):
            with pytest.raises(_StopAfterParameterLogs):
                research_service_module.run_research_process(
                    "research-1",
                    "q" * 100_000,
                    "quick",
                    username="testuser",
                    model_provider=wide,
                    model=wide,
                    search_engine=wide,
                    # Keep the malformed-provider probe on the logging path;
                    # provider normalization for a missing endpoint rejects
                    # dict inputs before the later override diagnostic.
                    custom_endpoint=None,
                )

        assert (
            len(_bounded_line(loguru_caplog.text, "Starting research process"))
            < 500
        )
        assert (
            len(_bounded_line(loguru_caplog.text, "Overriding system settings"))
            < 500
        )
        assert CountingValue.reprs < 200

    def test_followup_service_question_log_is_bounded(self, loguru_caplog):
        """Catches (W1)/(W2) at the followup service's ``Prepared
        follow-up research`` question line, through the real call.

        The router path hands this service the raw request ``question``
        right after its own bounded params/query lines, and the service
        f-stringed the question (and parent id) in full.
        ``prepare_research_context`` is patched to a truthy context so
        the call reaches the log line with no DB access; the returned
        params must still carry the raw values unchanged. Both are
        100,000-char strings, so the eager f-string emitted them in
        full; with the preview the line stays under a few hundred
        chars.
        """
        service = followup_service_module.FollowUpResearchService(
            username="testuser"
        )
        request = followup_service_module.FollowUpRequest(
            parent_research_id="p" * 100_000,
            question="q" * 100_000,
        )

        with (
            loguru_caplog.at_level("INFO"),
            patch.object(
                service,
                "prepare_research_context",
                return_value={"parent_research_id": "parent-1"},
            ),
        ):
            params = service.perform_followup(request)

        assert params["query"] == request.question
        assert params["parent_research_id"] == request.parent_research_id
        line = _bounded_line(loguru_caplog.text, "Prepared follow-up research")
        assert len(line) < 500
        assert "q" * 150 not in line
        assert "p" * 150 not in line


class TestRawParentIdAndUrlLogSites:
    """#6938 follow-through: the lines that log the RAW request value on
    the paths an attacker actually reaches, which run before the bounded
    lines above.

    * the follow-up parent id on its not-found paths — every
      ``/api/followup/prepare`` and ``/start`` call reaches
      ``load_parent_research`` with the body's unchecked
      ``parent_research_id``, and ``/prepare`` logs it a second time;
    * the scheme / host of a request-supplied URL inside
      ``validate_url`` (the add-resource route calls it with the body
      URL before its own bounded refusal line) and in
      ``redact_url_for_log``, whose ``scheme://host`` form was
      credential-free but not length-bounded.

    Each probe feeds a 100,000-char value and asserts the emitted line
    stays short. On the pre-fix code every one of these lines carried
    the full value.
    """

    HUGE = 100_000

    @staticmethod
    def _not_found_session():
        """``get_user_db_session`` double whose lookup finds no row."""

        @contextmanager
        def _session_ctx(*args, **kwargs):
            session = MagicMock()
            session.query.return_value.filter_by.return_value.first.return_value = None
            yield session

        return _session_ctx

    def test_load_parent_research_not_found_log_is_bounded(self, loguru_caplog):
        parent_id = "p" * self.HUGE
        service = followup_service_module.FollowUpResearchService(
            username="testuser"
        )
        with (
            loguru_caplog.at_level("WARNING"),
            patch.object(
                followup_service_module,
                "get_user_db_session",
                self._not_found_session(),
            ),
        ):
            assert service.load_parent_research(parent_id) == {}

        line = _bounded_line(loguru_caplog.text, "Parent research not found")
        assert len(line) < 500
        assert "p" * 150 not in line

    def test_perform_followup_empty_context_log_is_bounded(self, loguru_caplog):
        service = followup_service_module.FollowUpResearchService(
            username="testuser"
        )
        request = followup_service_module.FollowUpRequest(
            parent_research_id="p" * self.HUGE,
            question="q",
        )
        with (
            loguru_caplog.at_level("WARNING"),
            patch.object(service, "prepare_research_context", return_value={}),
        ):
            params = service.perform_followup(request)

        assert params["parent_research_id"] == request.parent_research_id
        line = _bounded_line(loguru_caplog.text, "using empty context")
        assert len(line) < 500
        assert "p" * 150 not in line

    def test_prepare_followup_unknown_parent_logs_are_bounded(
        self, loguru_caplog
    ):
        """Through the real route: both not-found warnings (service and
        router) stay bounded, and the route still answers 404."""
        parent_id = "p" * self.HUGE
        request = _json_request(
            {"parent_research_id": parent_id, "question": "q"}
        )
        settings_manager = MagicMock()
        settings_manager.get_all_settings.return_value = {}

        async def _inline_run_db_sync(fn, *args, **kwargs):
            return fn(*args, **kwargs)

        with (
            loguru_caplog.at_level("WARNING"),
            patch.object(followup_module, "run_db_sync", _inline_run_db_sync),
            patch(
                "local_deep_research.settings.manager.SettingsManager",
                return_value=settings_manager,
            ),
            patch(
                "local_deep_research.database.session_context"
                ".get_user_db_session",
                self._not_found_session(),
            ),
            patch.object(
                followup_service_module,
                "get_user_db_session",
                self._not_found_session(),
            ),
        ):
            response = asyncio.run(
                followup_module.prepare_followup(request, "testuser")
            )

        assert response.status_code == 404
        lines = [
            line
            for line in loguru_caplog.text.splitlines()
            if "Parent research" in line and "not found" in line
        ]
        assert len(lines) == 2, lines
        for line in lines:
            assert len(line) < 500
            assert "p" * 150 not in line

    def test_validate_url_invalid_scheme_log_is_bounded(self, loguru_caplog):
        from local_deep_research.security import ssrf_validator

        url = "a" * self.HUGE + "://example.com/"
        with loguru_caplog.at_level("WARNING"):
            assert ssrf_validator.validate_url(url) is False

        line = _bounded_line(
            loguru_caplog.text, "Blocked URL with invalid scheme"
        )
        assert len(line) < 1000
        assert "a" * 300 not in line

    def test_validate_url_unresolvable_host_log_is_bounded(self, loguru_caplog):
        import socket

        from local_deep_research.security import ssrf_validator

        url = "http://" + "a." * (self.HUGE // 2) + "com/"
        with (
            loguru_caplog.at_level("WARNING"),
            patch.object(
                ssrf_validator.socket,
                "getaddrinfo",
                side_effect=socket.gaierror("no such host"),
            ),
        ):
            assert ssrf_validator.validate_url(url) is False

        line = _bounded_line(loguru_caplog.text, "Failed to resolve hostname")
        assert len(line) < 1000
        assert "a." * 150 not in line

    def test_redact_url_for_log_is_length_bounded(self):
        """A long host is cut with a ``...`` marker; a host past the
        1,024 characters ``redact_and_bound_for_log`` scans is replaced by
        the log patcher's omitted-length marker instead.

        The second half changed when ``redact_url_for_log`` started to
        redact each component before cutting it
        (``redact_and_bound_for_log``): a 100,000-char host is one
        whitespace-free run longer than the patcher scans, so the patcher's
        own rule drops it whole rather than keep an unscanned prefix. That
        is what the patcher already did to the same host in a raw log line.
        """
        from local_deep_research.security.ssrf_validator import (
            redact_url_for_log,
        )

        redacted = redact_url_for_log(
            "https://" + "h" * 1_000 + ".example:8443/path"
        )
        assert len(redacted) < 1000
        assert redacted.startswith("https://hhh")
        assert redacted.endswith("...:8443")

        redacted = redact_url_for_log(
            "https://" + "h" * self.HUGE + ".example:8443/path"
        )
        assert len(redacted) < 1000
        assert "h" * 300 not in redacted
        assert redacted.endswith(":8443")
        # Ordinary URLs are untouched.
        assert (
            redact_url_for_log("http://u:[email protected]:8080/x")
            == "http://example.com:8080"
        )


def _secret(n: int, offset: int = 0) -> str:
    """A credential stand-in built at runtime (no token-shaped literal for
    the secret scanners), with no 8-char window repeated within 26 chars."""
    return "".join(chr(ord("A") + (i * 7 + offset) % 26) for i in range(n))


def _leaked_fragments(secret: str, text: str, width: int = 8) -> list[str]:
    return [
        secret[i : i + width]
        for i in range(len(secret) - width + 1)
        if secret[i : i + width] in text
    ]


def _credential_urls():
    """The reviewers' shapes: a long URL whose credential label (``?key=``,
    ``?api_key=``, ``?access_token=``, or the ``user:`` before ``@``) sits
    in the middle that a head/tail cut drops, while the cut keeps the end
    of the secret."""
    cases = []
    for n in (48, 64):
        secret = _secret(n)
        for label in (
            "openai?key=",
            "openai/v1/chat?api_key=",
            "openai/v1/chat?access_token=",
        ):
            cases.append(
                (
                    "https://generativelanguage.googleapis.com/v1beta/"
                    f"{label}{secret}",
                    secret,
                )
            )
    for n in (40, 60):
        secret = _secret(n, 3)
        cases.append(
            (
                f"https://svc-account-{'x' * 49}:{secret}@llm.corp.example/v1",
                secret,
            )
        )
    return cases


class TestRedactBeforeBound:
    """#6938 r3: bounding a log value must never let the log patcher's
    credential redaction see less than it did when the raw value was
    logged.

    ``reprlib`` keeps a long string's first 47 and last 48 characters and
    ``sanitize_for_log`` keeps a prefix, so either cut can drop a
    credential's label while keeping part of the secret. The patcher then
    has no pattern to match. Each probe checks that no 8-char window of the
    secret survives, in the bounded value and after the patcher's own
    redaction (``_redact_log_text``) runs over the emitted line. On
    d7ac5b7630 every probe here leaked.
    """

    @pytest.mark.parametrize(
        ("url", "secret"),
        _credential_urls(),
        ids=lambda v: v[:24] if isinstance(v, str) else None,
    )
    def test_preview_redacts_before_cutting(self, url, secret):
        from local_deep_research.security.log_sanitizer import (
            _redact_log_text,
        )

        preview = notes_module._log_value_preview(url)
        assert _leaked_fragments(secret, preview) == []
        emitted = _redact_log_text("custom_endpoint=" + preview)
        assert _leaked_fragments(secret, emitted) == []
        # Nested strings go through the same redaction.
        nested = notes_module._log_value_preview({"endpoint": [url]})
        assert _leaked_fragments(secret, nested) == []

    @pytest.mark.parametrize(
        ("url", "secret"),
        _credential_urls(),
        ids=lambda v: v[:24] if isinstance(v, str) else None,
    )
    def test_redact_and_bound_redacts_before_cutting(self, url, secret):
        from local_deep_research.security.log_sanitizer import (
            redact_and_bound_for_log,
        )

        for cap in (50, 100, 200):
            bounded = redact_and_bound_for_log(url, cap)
            assert len(bounded) <= cap
            assert _leaked_fragments(secret, bounded) == []

    def test_preview_masks_a_value_under_a_sensitive_key(self):
        secret = _secret(64)
        preview = notes_module._log_value_preview(
            {"api_key": secret, "password": "", "model": "m"}
        )
        assert _leaked_fragments(secret, preview) == []
        # Empty values stay readable, as in the patcher's extra redaction.
        assert "'password': ''" in preview
        assert "'model': 'm'" in preview

    def test_preview_redaction_work_is_budgeted(self):
        """A container of many long strings is not redacted string by
        string without limit: each string is charged at most 512
        characters (only its first 512 are redacted) against a
        1,024-character budget per preview, and past the budget a string
        is shown as a length, never raw. reprlib renders ten of the
        eleven: two fit the budget, eight do not. Checked on the repr
        before the 100-char backstop, which would cut the later elements
        off anyway."""
        long = "x " * 20_000
        text = notes_module._RedactingRepr().repr([long] * 11)
        assert text.count("<40000-char string not shown>") == 8
        assert len(notes_module._log_value_preview([long] * 11)) <= 100

    def test_research_parameters_line_redacts_custom_endpoint(
        self, loguru_caplog
    ):
        url, secret = _credential_urls()[0]
        with (
            loguru_caplog.at_level("INFO"),
            patch.object(research_service_module, "set_search_context"),
            patch.object(
                research_service_module,
                "apply_environment_overrides_to_snapshot",
                side_effect=_StopAfterParameterLogs,
            ),
            patch.object(research_service_module, "_sio_emit"),
            patch.object(research_service_module, "cleanup_research_resources"),
            patch(
                "local_deep_research.web.research_state"
                ".is_termination_requested",
                return_value=False,
            ),
            patch("local_deep_research.settings.logger.log_settings"),
        ):
            with pytest.raises(_StopAfterParameterLogs):
                research_service_module.run_research_process(
                    "research-1",
                    "q",
                    "quick",
                    username="testuser",
                    custom_endpoint=url,
                )

        line = _bounded_line(loguru_caplog.text, "Research parameters:")
        assert _leaked_fragments(secret, line) == []
        assert "custom_endpoint=https://generativelanguage.googleapis.com," in (
            line
        )

    def test_notification_rejection_message_bounds_the_host(self):
        """An oversized IPv6 zone is rejected by destination parsing before
        IP policy, without reflecting the host into the error callers log."""
        from local_deep_research.security.notification_validator import (
            NotificationURLValidator,
        )

        url = "http://[fe80::1%25" + "z" * 100_000 + "]/x"
        is_valid, error = NotificationURLValidator.validate_service_url(url)
        assert is_valid is False
        assert error == "Notification destination is unsupported or ambiguous"
        assert len(error) < 1000
        assert "z" * 300 not in error

    def test_notification_unsupported_scheme_message_is_bounded(self):
        from local_deep_research.security.notification_validator import (
            NotificationURLValidator,
        )

        is_valid, error = NotificationURLValidator.validate_service_url(
            "s" * 100_000 + "://example.com/"
        )
        assert is_valid is False
        assert len(error) < 1000
        assert "s" * 300 not in error

    def test_engineio_logger_gets_the_bounded_origin_message(self):
        from local_deep_research.web.services.socketio_asgi import (
            _install_origin_rejection_logging,
        )

        calls = []

        class _Eio:
            def _log_error_once(self, message, message_key):
                calls.append(message)

        class _Sio:
            eio = _Eio()

        server = _Sio()
        assert _install_origin_rejection_logging(server) is True
        server.eio._log_error_once(
            "http://" + "o" * 100_000 + " is not an accepted origin.",
            "bad-origin",
        )
        assert len(calls) == 1
        assert len(calls[0]) <= 300

    def test_auth_log_lines_never_interpolate_the_raw_username(self):
        """Every logger call in ``web/routers/auth.py`` that formats the
        username goes through the bounded preview: registration has no
        maximum length, so a self-registered 100,000-char name would
        otherwise be logged on every login, post-login step, logout and
        password change."""
        import ast

        from local_deep_research.web.routers import auth as auth_module

        tree = ast.parse(inspect.getsource(auth_module))
        raw = []
        for node in ast.walk(tree):
            if not (
                isinstance(node, ast.Call)
                and isinstance(node.func, ast.Attribute)
                and isinstance(node.func.value, ast.Name)
                and node.func.value.id == "logger"
            ):
                continue
            for arg in node.args:
                for sub in ast.walk(arg):
                    if (
                        isinstance(sub, ast.FormattedValue)
                        and isinstance(sub.value, ast.Name)
                        and sub.value.id == "username"
                    ):
                        raw.append(node.lineno)
            for arg in node.args[1:]:
                if isinstance(arg, ast.Name) and arg.id == "username":
                    raw.append(node.lineno)
        assert raw == [], f"raw username logged at auth.py lines {raw}"


class TestLogValueWorkIsBounded:
    """#6938 r4: the redaction added in r3 must not make a log line's cost
    grow with the request.

    Each guard counts calls (or characters) at the seam that does the
    work, so it fails on 8ab4a0bc0 regardless of machine speed: there a
    preview of ``{a..j: {a..j: {a..j: {a..j: "a"}}}}`` ran ~21,000
    redactions, a dict key of any length went through the sensitive-key
    check in full, and ``validate_safe_path`` redacted every path it
    accepted.
    """

    @staticmethod
    def _tiny_strings(width: int = 10):
        keys = [chr(ord("a") + i) for i in range(width)]
        return {
            a: {b: {c: dict.fromkeys(keys, "a") for c in keys} for b in keys}
            for a in keys
        }

    def test_preview_redaction_calls_are_capped(self):
        from local_deep_research.security.log_sanitizer import redact_for_log

        calls = []

        def counting(text):
            calls.append(len(text))
            return redact_for_log(text)

        with patch.object(notes_module, "redact_for_log", counting):
            preview = notes_module._log_value_preview(self._tiny_strings())
        # Deliberate literal (1,024-char budget / 32-char minimum charge).
        assert 0 < len(calls) <= 32
        # The strings the 100-char preview shows are the first rendered,
        # so they are the ones redacted.
        assert preview.startswith("{'a': {'a': {'a': {'a': 'a', 'b': 'a',")

    def test_preview_key_checks_are_capped(self):
        checked = []
        real = notes_module._is_sensitive_log_key

        def counting(key):
            checked.append(len(key) if isinstance(key, str) else 0)
            return real(key)

        width = 11
        keys = [chr(ord("a") + i) * 40 for i in range(width)]
        value = {
            a: {b: {c: dict.fromkeys(keys, "v") for c in keys} for b in keys}
            for a in keys
        }
        with patch.object(notes_module, "_is_sensitive_log_key", counting):
            notes_module._log_value_preview(value)
        assert len(checked) <= 512
        assert sum(checked) <= 32_768

    def test_huge_key_is_not_checked_and_its_value_is_masked(self):
        checked = []
        real = notes_module._is_sensitive_log_key

        def counting(key):
            checked.append(len(key))
            return real(key)

        secret = _secret(48)
        huge = "k" * 1_000_000
        with patch.object(notes_module, "_is_sensitive_log_key", counting):
            preview = notes_module._log_value_preview({huge: secret})
            disguised = notes_module._log_value_preview(
                {"x" * 1_000_000 + "_api_key": secret}
            )
        assert all(n <= 32_768 for n in checked), checked
        assert _leaked_fragments(secret, preview) == []
        assert _leaked_fragments(secret, disguised) == []

    def test_unchecked_key_still_reports_dropped_entries(self):
        """A value masked because the key budget ran out is still walked
        for the items-omitted flag (with no budget, so nothing under it is
        checked)."""
        eleven = {f"k{i}": "v" for i in range(11)}
        bounded, omitted = notes_module._bound_dict_shaped_walk(
            {"k" * 40_000: eleven}, 0
        )
        assert list(bounded.values()) == ["[REDACTED]"]
        assert omitted is True
        _, omitted = notes_module._bound_dict_shaped_walk(
            {"k" * 40_000: {f"k{i}": "v" for i in range(10)}}, 0
        )
        assert omitted is False

    def test_patcher_key_check_fails_closed_past_the_bound(self):
        from local_deep_research.security.log_sanitizer import (
            _is_sensitive_log_key,
            redact_log_extra,
        )

        assert _is_sensitive_log_key("model") is False
        assert _is_sensitive_log_key("k" * 32_768) is False
        assert _is_sensitive_log_key("k" * 32_769) is True
        redacted = redact_log_extra({"k" * 40_000: "v"})
        assert list(redacted.values()) == ["[REDACTED]"]
        # The integrated sink also bounds structured keys before logging.
        key = next(iter(redacted))
        assert len(key) < 100
        assert "40000 characters omitted" in key

    def test_validate_safe_path_redacts_only_to_log_a_rejection(self, tmp_path):
        from local_deep_research.security import path_validator

        (tmp_path / "app.js").write_text("x")
        seen = []

        def counting(value, max_length):
            seen.append(len(value))
            return value[:max_length]

        with patch.object(path_validator, "redact_and_bound_for_log", counting):
            path_validator.PathValidator.validate_safe_path(
                "app.js", str(tmp_path)
            )
            assert seen == []
            with pytest.raises(ValueError):
                path_validator.PathValidator.validate_safe_path(
                    "../" * 20_000 + "etc/passwd", str(tmp_path)
                )
        assert len(seen) == 1

    def test_validate_safe_path_rejection_log_is_redacted(
        self, tmp_path, loguru_caplog
    ):
        from local_deep_research.security import path_validator

        secret = _secret(40)
        # One long key-shaped run (redacted to a marker) puts the URL
        # userinfo 30 characters before the 1,024-character scan bound
        # (4 * 200 is below the 1,024 minimum), so the cut falls inside the
        # secret and the redacted head is short enough to be shown whole.
        # A plain slice keeps 10 secret characters without their ``@``.
        start = 1_024 - 30
        prefix = "../sk-" + "A" * (start - len("../sk-") - 1) + " "
        for path in (
            f"../https://svc-{'x' * 200}:{secret}@h/x",
            f"{prefix}https://svc-account:{secret}@h/x" + " tail" * 50,
        ):
            loguru_caplog.clear()
            with loguru_caplog.at_level("WARNING"), pytest.raises(ValueError):
                path_validator.PathValidator.validate_safe_path(
                    path, str(tmp_path)
                )
            assert "Path traversal attempt blocked" in loguru_caplog.text
            assert _leaked_fragments(secret, loguru_caplog.text) == []


class TestRedactionRunsOnlyWhenLogged:
    """#6938 r5: a pre-auth or per-row log site redacts only when its text
    is actually logged.

    On 80d651e9b the Socket.IO origin hook redacted the (unbounded,
    attacker-chosen) Origin on EVERY rejected handshake, including after
    its 100-origin warning cap and on every repeat that engine.io's
    ERROR-level logger drops, and ``get_news_feed`` redacted every row's
    full stored query for INFO lines on every feed load. Each guard counts
    calls at the redaction seam, so it fails there regardless of machine
    speed.
    """

    # Standard-library level numbers (engine.io logs through ``logging``).
    ERROR, INFO = 40, 20

    @staticmethod
    def _engineio_stub(seen_keys, level):
        calls = []

        class _StdlibLogger:
            def isEnabledFor(self, wanted):
                return wanted >= level

        eio_logger = _StdlibLogger()

        class _Eio:
            log_message_keys = set(seen_keys)
            logger = eio_logger

            def _log_error_once(self, message, message_key):
                calls.append(message)

        class _Sio:
            eio = _Eio()

        return _Sio(), calls

    @staticmethod
    def _counting_redaction(module):
        seen = []
        real = module.redact_and_bound_for_log

        def counting(value, max_length):
            seen.append(len(value))
            return real(value, max_length)

        return seen, patch.object(module, "redact_and_bound_for_log", counting)

    def test_origin_hook_redacts_nothing_past_the_cap(self):
        from local_deep_research.web.services import socketio_asgi

        server, calls = self._engineio_stub({"bad-origin"}, self.ERROR)
        assert socketio_asgi._install_origin_rejection_logging(server)
        seen, patched = self._counting_redaction(socketio_asgi)
        hostile = "https://a:b@ " * 3_000 + " is not an accepted origin."
        with patched:
            for i in range(100):
                server.eio._log_error_once(
                    f"http://o{i}.test is not an accepted origin.",
                    "bad-origin",
                )
            assert len(seen) == 100  # one per warning emitted
            for _ in range(50):
                server.eio._log_error_once(hostile, "bad-origin")
        assert len(seen) == 100, (
            "the hook redacted an origin it neither warned about nor "
            f"handed to an emitting engine.io logger ({len(seen)} calls)"
        )
        assert len(calls) == 150
        assert all(len(c) <= 300 for c in calls)
        assert hostile not in calls

    def test_origin_hook_redacts_a_repeated_origin_once(self):
        from local_deep_research.web.services import socketio_asgi

        server, calls = self._engineio_stub({"bad-origin"}, self.ERROR)
        socketio_asgi._install_origin_rejection_logging(server)
        seen, patched = self._counting_redaction(socketio_asgi)
        with patched:
            for _ in range(20):
                server.eio._log_error_once(
                    "http://same.test is not an accepted origin.",
                    "bad-origin",
                )
        assert len(seen) == 1
        assert len(calls) == 20

    @pytest.mark.parametrize(
        "seen_keys,level",
        [
            (set(), "ERROR"),  # first occurrence: engine.io logs at ERROR
            ({"bad-origin"}, "INFO"),  # repeats emitted: logger lowered
        ],
    )
    def test_origin_hook_hands_an_emitting_engineio_the_redacted_text(
        self, seen_keys, level
    ):
        from local_deep_research.web.services import socketio_asgi

        server, calls = self._engineio_stub(seen_keys, getattr(self, level))
        socketio_asgi._install_origin_rejection_logging(server)
        secret = _secret(40)
        origin = f"https://svc-{'x' * 200}:{secret}@h"
        for _ in range(2):  # past the warning dedup, too
            server.eio._log_error_once(
                origin + " is not an accepted origin.", "bad-origin"
            )
        assert len(calls) == 2
        for text in calls:
            assert len(text) <= 300
            assert text.endswith(" is not an accepted origin.")
            assert _leaked_fragments(secret, text) == []

    def test_news_feed_rows_are_not_redacted_unless_logged(self):
        """With no sink at the per-row lines' level nothing is redacted;
        their lazy arguments, when a sink does take them, are redacted
        before they are cut."""
        from contextlib import contextmanager as _cm
        from datetime import UTC, datetime

        from sqlalchemy import create_engine
        from sqlalchemy.orm import sessionmaker

        from local_deep_research.database.models import Base, ResearchHistory
        from local_deep_research.news import api as news_api

        secret = _secret(40)
        # The password starts inside every line's cut, so a cut made
        # before redaction would keep part of it.
        query = f"latest news https://u:{secret}@h/x " + "q" * 20_000
        engine = create_engine("sqlite:///:memory:")
        Base.metadata.create_all(engine)
        session_local = sessionmaker(bind=engine)
        now = datetime.now(UTC).isoformat()
        seed = session_local()
        for i in range(6):
            seed.add(
                ResearchHistory(
                    id=f"r{i}",
                    query=query,
                    title=None,
                    mode="quick",
                    status="completed",
                    created_at=now,
                    completed_at=now,
                    report_content="Answer.",
                    research_meta={"is_news_search": True},
                )
            )
        seed.commit()
        seed.close()

        @_cm
        def _fake_user_db(username=None, password=None):
            db = session_local()
            try:
                yield db
            finally:
                db.close()

        seen, patched = self._counting_redaction(news_api)
        mock_logger = MagicMock()
        try:
            with (
                patch(
                    "local_deep_research.database.session_context."
                    "get_user_db_session",
                    _fake_user_db,
                ),
                patch.object(news_api, "logger", mock_logger),
                patched,
            ):
                result = news_api.get_news_feed(
                    user_id="testuser", limit=20, use_cache=False
                )
                assert len(result["news_items"]) == 6
                assert seen == [], (
                    f"get_news_feed redacted {len(seen)} values for log "
                    "lines no sink was taking"
                )
                rendered = []
                for call in mock_logger.opt.return_value.debug.call_args_list:
                    rendered.extend(
                        arg() if callable(arg) else arg for arg in call.args[1:]
                    )
        finally:
            engine.dispose()
        texts = [r for r in rendered if isinstance(r, str)]
        assert any(t.startswith("latest news https://") for t in texts)
        assert any(t.startswith("News: latest news") for t in texts)
        for text in texts:
            assert len(text) <= 50
            assert _leaked_fragments(secret, text) == []
        assert seen, "the lazy arguments never reached the redaction"


# Files this PR's log redaction covers. A ``value[:N]`` slice of a logged
# value cuts it before the patcher's redaction sees it; the only slices
# left are of ids (uuid prefixes) and the authenticated user's own name.
_REDACT_BEFORE_CUT_MODULES = (
    "local_deep_research.chat.service",
    "local_deep_research.news.api",
    "local_deep_research.security.notification_validator",
    "local_deep_research.security.path_validator",
    "local_deep_research.security.url_validator",
    "local_deep_research.web.routers.auth",
    "local_deep_research.web.routers.library",
    "local_deep_research.web.routers.news_flask_api",
    "local_deep_research.web.routers.research",
    "local_deep_research.web.services.research_service",
)


def _sliced_name(node):
    """``x`` for ``x[:N]``, ``x.y`` for ``x.y[:N]``, ``x['k']`` for
    ``x['k'][:N]``; None for anything else."""
    import ast

    if not (
        isinstance(node, ast.Subscript)
        and isinstance(node.slice, ast.Slice)
        and node.slice.lower is None
        and node.slice.upper is not None
    ):
        return None
    return ast.unparse(node.value)


def _is_logger_chain(node):
    """``logger``, or ``logger.opt(...)`` / ``logger.bind(...)`` chains."""
    import ast

    while isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
        node = node.func.value
    return isinstance(node, ast.Name) and node.id == "logger"


@pytest.mark.parametrize("module_name", _REDACT_BEFORE_CUT_MODULES)
def test_logged_values_are_redacted_before_they_are_cut(module_name):
    """#6938 r4: every ``logger`` call in these modules cuts a value only
    after redacting it (``redact_and_bound_for_log`` or the preview), never
    with a bare slice or ``sanitize_for_log``. On
    8ab4a0bc0 ``run_research_process`` still logged ``query[:100]``: a URL
    whose ``@`` sits past offset 100 then kept 22 characters of its
    password through the patcher."""
    import ast
    import importlib

    module = importlib.import_module(module_name)
    tree = ast.parse(inspect.getsource(module))
    cut = []
    for node in ast.walk(tree):
        if not (
            isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and _is_logger_chain(node.func.value)
        ):
            continue
        for arg in node.args:
            for sub in ast.walk(arg):
                if (
                    isinstance(sub, ast.Call)
                    and isinstance(sub.func, ast.Name)
                    and sub.func.id == "sanitize_for_log"
                ):
                    # Strips and cuts, but does not redact.
                    cut.append((node.lineno, ast.unparse(sub)))
                    continue
                name = _sliced_name(sub)
                if name is None:
                    continue
                if name.endswith("id") or name == "self.username":
                    continue
                cut.append((node.lineno, name))
    assert cut == [], f"{module_name} cuts before redacting: {cut}"


class TestRedactionCostIsBoundedPerValue:
    """#6938 r6: redacting a value for a short log text scans only a
    bounded head of it, and DEBUG lines redact only when a sink takes
    them.

    On 6f6b7dd223 the preview and ``redact_and_bound_for_log`` redacted
    up to 32 KiB of each string to show at most a few hundred characters
    (~100-170 ms for one adversarial 33 KB string, against ~0.02 ms for
    the unredacted preview), and sixteen DEBUG lines that the default
    INFO sinks never emit ran it eagerly on every call. Each guard counts
    characters or calls at the redaction seam, so it fails there
    regardless of machine speed.
    """

    HOSTILE = "https://a:b@ " * 2_600

    @staticmethod
    def _counting(module):
        seen = []
        real = module.redact_for_log

        def counting(text):
            seen.append(len(text))
            return real(text)

        return seen, patch.object(module, "redact_for_log", counting)

    @pytest.mark.parametrize(
        "shape", ["str", "list", "dict"], ids=["str", "list", "dict"]
    )
    def test_preview_redacts_a_bounded_head_of_a_long_string(self, shape):
        value = {
            "str": self.HOSTILE,
            "list": [self.HOSTILE],
            # Under 32 KiB, so the old per-preview budget redacted it whole.
            "dict": {"model": self.HOSTILE[:30_000]},
        }[shape]
        seen, patched = self._counting(notes_module)
        with patched:
            preview = notes_module._log_value_preview(value)
        assert seen, "the long string was never redacted"
        # Deliberate literals: 512 chars per string, 1,024 per preview.
        assert max(seen) <= 512, seen
        assert sum(seen) <= 1_024, seen
        assert "a:b@" not in preview
        if shape == "str":
            assert "characters omitted from log output" in preview

    @pytest.mark.parametrize("max_length", [60, 100, 256])
    def test_redact_and_bound_redacts_a_bounded_head(self, max_length):
        from local_deep_research.security import log_sanitizer

        seen, patched = self._counting(log_sanitizer)
        with patched:
            bounded = log_sanitizer.redact_and_bound_for_log(
                self.HOSTILE, max_length
            )
        assert seen
        # Deliberate literal: the larger of 1,024 and 4 * max_length.
        assert sum(seen) <= max(1_024, 4 * max_length), seen
        assert len(bounded) <= max_length
        assert "a:b@" not in bounded

    _BOUNDARY_SHAPES = [
        "https://svc-account:{s}@llm.corp.example/v1",
        "https://h.example/v1?api_key={s}&a=1",
        "api_key={s}",
        'password="{s} two words {s}"',
        "Authorization: Bearer {s}",
    ]

    @staticmethod
    def _boundary_value(bound, offset, shape):
        """A value whose credential starts ``bound + offset`` characters in.

        The prefix is ONE long key-shaped run (an ``sk-`` key, redacted to a
        short marker), so the redacted head is short and the region around
        the cut is inside the first characters every output shows. Padding
        with ordinary words instead leaves that region past the visible
        window of each output, so a cut that slices mid-credential would
        pass unseen.
        """
        secret = _secret(40)
        start = bound + offset
        prefix = "sk-" + "A" * (start - 4) + " "
        value = prefix + shape.format(s=secret) + " tail" * 50
        return secret, value

    @pytest.mark.parametrize("offset", [-60, -45, -35, -30, -22, -12, -1, 0, 1])
    @pytest.mark.parametrize("shape", _BOUNDARY_SHAPES)
    def test_a_credential_across_the_preview_bound_never_leaks(
        self, offset, shape
    ):
        """The head is cut only at a whitespace boundary, dropping the
        whole run the bound falls in, so a credential that starts just
        before the bound is redacted whole or dropped whole, never kept
        in part without its label."""
        secret, value = self._boundary_value(
            notes_module._PREVIEW_STRING_REDACTION_MAX_CHARS, offset, shape
        )
        assert (
            _leaked_fragments(secret, notes_module.redact_for_log(value)) == []
        )
        for text in (
            notes_module._RedactingRepr().repr(value),
            notes_module._log_value_preview(value),
            notes_module._log_value_preview({"endpoint": [value]}),
        ):
            assert _leaked_fragments(secret, text) == [], text

    @pytest.mark.parametrize("offset", [-60, -45, -35, -30, -22, -12, -1, 0, 1])
    @pytest.mark.parametrize("shape", _BOUNDARY_SHAPES)
    def test_a_credential_across_the_head_bound_never_leaks(
        self, offset, shape
    ):
        from local_deep_research.security.log_sanitizer import (
            bounded_redaction_chars,
            redact_and_bound_for_log,
            redact_head_for_log,
        )

        for bound in (512, 1_024, 1_200):
            secret, value = self._boundary_value(bound, offset, shape)
            head = redact_head_for_log(value, bound)
            assert _leaked_fragments(secret, head) == [], (bound, head)
        for max_length in (60, 100, 256, 300):
            secret, value = self._boundary_value(
                bounded_redaction_chars(max_length), offset, shape
            )
            text = redact_and_bound_for_log(value, max_length)
            assert _leaked_fragments(secret, text) == [], (max_length, text)

    def test_research_not_found_previews_only_when_logged(self):
        calls = []

        def counting(value):
            calls.append(value)
            return notes_module._log_value_preview(value)

        mock_logger = MagicMock()
        with (
            patch.object(research_module, "logger", mock_logger),
            patch.object(research_module, "_log_value_preview", counting),
        ):
            response = research_module._research_not_found("r" * 50_000)
            assert response.status_code == 404
            assert calls == [], "previewed for a line no sink was taking"
            rendered = [
                arg() if callable(arg) else arg
                for call in mock_logger.opt.return_value.debug.call_args_list
                for arg in call.args[1:]
            ]
        assert len(calls) == 1
        # One 50,000-char run is longer than the scanned head, so the
        # patcher's rule drops it whole and only its length is shown.
        assert any(
            isinstance(r, str)
            and len(r) <= 100
            and "50000 characters omitted" in r
            for r in rendered
        ), rendered
        assert "Research not found" in rendered


_REDACTING_CALLS = frozenset(
    {"_log_value_preview", "redact_and_bound_for_log", "redact_for_log"}
)


def _debug_lines_redacting_eagerly(tree):
    """``logger.debug``/``.trace`` calls that redact an argument before
    loguru knows whether any sink takes the line (no ``opt(lazy=True)``,
    or a redacting call outside the lambdas)."""
    import ast

    found = []
    for node in ast.walk(tree):
        if not (
            isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr in ("debug", "trace")
            and _is_logger_chain(node.func.value)
        ):
            continue
        chain = node.func.value
        lazy = (
            isinstance(chain, ast.Call)
            and isinstance(chain.func, ast.Attribute)
            and chain.func.attr == "opt"
            and any(
                kw.arg == "lazy"
                and isinstance(kw.value, ast.Constant)
                and kw.value.value is True
                for kw in chain.keywords
            )
        )
        lambdas = set()
        for arg in [*node.args, *(kw.value for kw in node.keywords)]:
            if lazy and isinstance(arg, ast.Lambda):
                lambdas.update(id(sub) for sub in ast.walk(arg))
        for arg in [*node.args, *(kw.value for kw in node.keywords)]:
            for sub in ast.walk(arg):
                if id(sub) in lambdas or not isinstance(sub, ast.Call):
                    continue
                func = sub.func
                name = getattr(func, "id", None) or getattr(func, "attr", None)
                if name in _REDACTING_CALLS:
                    found.append(node.lineno)
    return found


def test_no_debug_line_redacts_eagerly():
    """#6938 r6: a DEBUG line runs the preview or the redaction only
    inside ``logger.opt(lazy=True)`` lambdas, so the default INFO sinks
    cost no redaction. On 6f6b7dd223 sixteen DEBUG lines (research
    router and service, history, rag, auth, library, topic generator,
    dns pinning, notification validator) ran it eagerly.

    Scope: only the three names in ``_REDACTING_CALLS``. About 45 DEBUG
    lines (downloaders, search engines, url builder) still call
    ``redact_url_for_log`` eagerly; each call scans at most 1,024
    characters per URL component, so it is bounded and not guarded."""
    import ast
    from pathlib import Path

    import local_deep_research

    root = Path(local_deep_research.__file__).parent
    eager = {}
    for path in sorted(root.rglob("*.py")):
        source = path.read_text(encoding="utf-8")
        if not any(name in source for name in _REDACTING_CALLS):
            continue
        lines = _debug_lines_redacting_eagerly(ast.parse(source))
        if lines:
            eager[str(path.relative_to(root))] = lines
    assert eager == {}, f"DEBUG lines redacting eagerly: {eager}"


def test_eager_debug_guard_positive_control():
    import ast

    code = (
        "logger.debug('a {}', _log_value_preview(x))\n"
        "logger.opt(lazy=True).debug('b {}', redact_and_bound_for_log(x, 9))\n"
        "logger.opt(lazy=True).debug('c {}', lambda: _log_value_preview(x))\n"
        "logger.info('d {}', _log_value_preview(x))\n"
    )
    assert _debug_lines_redacting_eagerly(ast.parse(code)) == [1, 2]