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.

172 KB
Raw Blame
"""Regression tests for the log-sink credential redaction backstop.

The loguru patcher installed by ``utilities/log_utils.config_logger``
(:func:`local_deep_research.utilities.log_utils._sanitize_record`) is the
single chokepoint every sink shares — stderr, the encrypted per-user DB, the
browser progress stream, and the persistent, unencrypted
``<LDR_DATA_DIR>/logs/*.log`` file. These tests pin the invariant that a
credential which reaches a record without going through ``scrub_error`` first
is still scrubbed before any sink sees it:

* a secret carried in the message string is absent from the sink output,
* a secret carried as a bound/structured ``extra`` value is absent too, and
* an ordinary, non-sensitive message passes through unchanged.

They also cover the pure helpers in ``security/log_sanitizer`` directly, so a
future refactor of the patcher wiring can't silently drop the redaction.
"""

import pytest
from loguru import logger

from local_deep_research.security.log_sanitizer import (
    redact_log_extra,
    redact_log_message,
    redact_sensitive_assignments,
    scrub_error,
)
from tests.test_utils import restored_loguru_state


class TestRedactSensitiveAssignments:
    """Bare ``field=value`` credential assignments in a plain string."""

    @pytest.mark.parametrize(
        "field",
        ["api_key", "password", "secret", "token", "user_password"],
    )
    def test_value_redacted_field_name_preserved(self, field):
        secret = "plaintext-secret-value-12345678"
        redacted = redact_sensitive_assignments(f"{field}={secret}")
        assert f"{field}=" in redacted
        assert secret not in redacted

    def test_compound_name_kept_whole(self):
        assert (
            redact_sensitive_assignments("client_secret=abcdef123456")
            == "client_secret=[REDACTED]"
        )

    def test_does_not_match_mid_identifier(self):
        # ``token`` inside ``mytoken`` must not trigger redaction.
        assert (
            redact_sensitive_assignments("mytoken=keepme") == "mytoken=keepme"
        )

    @pytest.mark.parametrize(
        "message",
        [
            "Starting research run 42",
            "the secret to success is hard work",
            "monkey=banana count=5 ratio=0.9",
            "Fetched 10 results from engine",
        ],
    )
    def test_ordinary_text_unchanged(self, message):
        assert redact_sensitive_assignments(message) == message


class TestRedactLogMessage:
    """Full message scrub: credential shapes + bare assignments."""

    def test_all_shapes_and_fields_scrubbed(self):
        message = (
            "provider failed "
            "https://user:[email protected]/v1"
            "?api_key=example-api-key-val00&token=tok-abcdefghij1234 "
            "sk-live-abcdefghijklmnopqrst and password=RawFieldSecret9999"
        )
        out = redact_log_message(message)
        for leak in (
            "hunter2secret",
            "example-api-key-val00",
            "tok-abcdefghij1234",
            "sk-live-abcdefghijklmnopqrst",
            "RawFieldSecret9999",
        ):
            assert leak not in out, leak
        assert "provider failed" in out

    def test_normal_message_unchanged(self):
        assert redact_log_message("Starting research run 42") == (
            "Starting research run 42"
        )


class TestRedactLogExtra:
    """Structured ``extra`` / bound values."""

    def test_sensitive_key_masked_structural_preserved(self):
        extra = {
            "research_id": "abc-123",
            "username": "alice",
            "user_password": "SuperSecretPw",
            "api_key": "AIzaSyABCDEFGHIJKLMNOPQRSTUV1234567890",
            "note": "harmless text",
        }
        out = redact_log_extra(extra)
        # Routing keys the sinks depend on survive unchanged.
        assert out["research_id"] == "abc-123"
        assert out["username"] == "alice"
        # Secrets are masked.
        assert out["user_password"] == "[REDACTED]"
        assert out["api_key"] == "[REDACTED]"
        # Innocuous values pass through.
        assert out["note"] == "harmless text"

    def test_credential_shape_under_innocuous_key(self):
        # The family bug: a secret bound under a non-sensitive key name that a
        # name-only check would miss is still scrubbed by value shape.
        extra = {
            "url": "https://u:p@host/path?token=leak-tok-1234567890abcdef",
        }
        out = redact_log_extra(extra)
        assert "leak-tok-1234567890abcdef" not in out["url"]
        assert "u:p@host" not in out["url"]

    def test_nested_and_empty_values(self):
        extra = {
            "nested": {"password": "nestedpw123", "ok": "fine"},
            "password": "",  # empty stays readable ("configured" vs "not")
            "count": 5,
        }
        out = redact_log_extra(extra)
        assert out["nested"]["password"] == "[REDACTED]"
        assert out["nested"]["ok"] == "fine"
        assert out["password"] == ""
        assert out["count"] == 5


@pytest.fixture
def captured_records():
    """Route loguru through the real patcher into an in-memory sink.

    Uses ``restored_loguru_state`` (tests/test_utils.py) to snapshot and
    restore loguru's process-wide patcher/handlers afterwards, so the global
    loguru state is left usable for other tests. ``logger.configure(patcher=
    None)`` does NOT do this -- loguru's ``configure()`` treats ``None`` as
    "leave unchanged", not "clear" -- so a fixture that relied on it would
    leak ``_sanitize_record`` into every later test in the same pytest
    worker.
    """
    from local_deep_research.utilities.log_utils import _sanitize_record

    records = []
    with restored_loguru_state():
        logger.remove()
        logger.configure(patcher=_sanitize_record)
        logger.add(
            lambda m: records.append(m.record), level="DEBUG", diagnose=False
        )
        yield records


class TestPatcherThroughSink:
    """End-to-end: the patcher scrubs before any sink observes the record."""

    def test_secret_in_message_absent_from_sink(self, captured_records):
        logger.info("provider auth failed password=SinkMessageSecret123")
        assert captured_records, "sink captured nothing"
        message = captured_records[-1]["message"]
        assert "password=" in message
        assert "SinkMessageSecret123" not in message

    def test_bound_secret_absent_from_sink(self, captured_records):
        logger.bind(user_password="BoundSecretPw999").info("connecting")
        extra = captured_records[-1]["extra"]
        assert extra.get("user_password") == "[REDACTED]"

    def test_bound_credential_shape_absent_from_sink(self, captured_records):
        logger.bind(detail="token=leak-bound-abcdef1234567890").info(
            "calling service"
        )
        extra = captured_records[-1]["extra"]
        assert "leak-bound-abcdef1234567890" not in extra["detail"]

    def test_normal_message_unchanged_through_sink(self, captured_records):
        logger.info("Starting research run 42")
        assert captured_records[-1]["message"] == "Starting research run 42"

    def test_control_characters_still_stripped(self, captured_records):
        logger.info("before\x00\x1b[31m\u202evisible")
        message = captured_records[-1]["message"]
        assert "visible" in message
        assert "\x00" not in message
        assert "\x1b" not in message
        assert "\u202e" not in message


@pytest.fixture
def rendered_sink():
    """Route loguru through the real patcher into a default-format stream sink.

    Unlike ``captured_records`` (which inspects the raw record dict), this
    sink RENDERS each record — including ``record["exception"]`` via loguru's
    own exception formatter — into an in-memory text stream, so the test can
    assert on what actually reaches a default-format stderr/file sink.

    Uses ``restored_loguru_state`` (tests/test_utils.py) to snapshot and
    restore loguru's process-wide patcher/handlers afterwards -- see
    ``captured_records`` above for why ``logger.configure(patcher=None)``
    can't be used for that instead.
    """
    import io

    from local_deep_research.utilities.log_utils import _sanitize_record

    stream = io.StringIO()
    with restored_loguru_state():
        logger.remove()
        logger.configure(patcher=_sanitize_record)
        logger.add(
            stream,
            level="DEBUG",
            diagnose=False,
            backtrace=False,
            colorize=False,
        )
        yield stream


class TestExceptionValueRedactedInRenderedSink:
    """A secret inside an exception value must not render to a text sink.

    Loguru renders ``record["exception"]`` through its own formatter, which
    the message scrub never touches; ``redact_log_exception`` closes that
    gap by building a redacted COPY of the exception (and its
    ``__cause__``/``__context__`` chain) for the sinks to render, leaving
    traceback structure intact -- and leaving the original, live exception
    instance completely unmutated (see ``TestExceptionInstanceNotMutated``
    below).
    """

    def test_credential_in_exception_value_absent_but_traceback_kept(
        self, rendered_sink
    ):
        secret = "SuperSecretPw12345"
        dsn = f"postgresql://admin:{secret}@db.internal:5432/app"
        try:
            raise ConnectionError(f"could not connect to {dsn}")
        except ConnectionError:
            logger.opt(exception=True).error("db connection failed")

        rendered = rendered_sink.getvalue()
        # Traceback STRUCTURE is preserved: type name and the rendered
        # exception line both survive.
        assert "ConnectionError" in rendered
        assert "Traceback" in rendered
        # ...but the credential embedded in the exception value is gone.
        assert secret not in rendered

    def test_credential_in_chained_cause_absent(self, rendered_sink):
        secret = "ChainedCauseSecret987"
        try:
            try:
                raise ValueError(f"token={secret}")
            except ValueError as inner:
                raise RuntimeError("wrapping failure") from inner
        except RuntimeError:
            logger.opt(exception=True).error("wrapped failure")

        rendered = rendered_sink.getvalue()
        assert "RuntimeError" in rendered
        assert secret not in rendered


class TestExceptionInstanceNotMutated:
    """`redact_log_exception` must copy, never mutate, the live exception.

    The exception instance logged here is the SAME object the surrounding
    ``except`` block holds, and may still inspect it, re-raise it, or hand
    it to other error handling after the logging call returns. Redacting a
    credential from the rendered output must never rewrite that instance's
    ``.args`` (or any other attribute) in place -- it must instead build a
    separate, redacted copy for the sink to render.
    """

    def test_original_args_unchanged_after_logging(self, captured_records):
        secret = "example-api-key-val00"  # noqa: S105 - fake, not a real credential shape
        exc = ValueError(f"token={secret}")
        original_args = exc.args
        try:
            raise exc
        except ValueError as caught:
            assert caught is exc
            logger.opt(exception=True).error("boom")

        # The live exception instance was NOT mutated by logging it.
        assert exc.args == original_args
        assert secret in exc.args[0]

        # ...while the copy handed to the sink IS redacted.
        redacted_value = captured_records[-1]["exception"].value
        assert redacted_value is not exc
        assert secret not in redacted_value.args[0]

    def test_original_cause_chain_unchanged_after_logging(
        self, captured_records
    ):
        secret = "example-chained-secret02"  # noqa: S105 - fake credential
        inner = ValueError(f"token={secret}")
        outer = RuntimeError("wrapping failure")
        try:
            try:
                raise inner
            except ValueError:
                raise outer from inner
        except RuntimeError:
            logger.opt(exception=True).error("wrapped failure")

        # Neither exception in the chain was mutated.
        assert inner.args == (f"token={secret}",)
        assert outer.__cause__ is inner

        redacted_outer = captured_records[-1]["exception"].value
        assert redacted_outer is not outer
        redacted_cause = redacted_outer.__cause__
        assert redacted_cause is not inner
        assert secret not in redacted_cause.args[0]


@pytest.mark.parametrize(
    "value",
    [
        '"QuotedMarker987 and another word"',
        "'QuotedMarker987 and another word'",
        r'"prefix\"QuotedMarker987"',
        "prefix,QuotedMarker987",
        "prefix#QuotedMarker987",
        "prefix&QuotedMarker987",
    ],
)
def test_complete_assignment_value_is_redacted(value):
    """Punctuation and quoting must not leave a credential suffix behind."""
    assert redact_sensitive_assignments(f"password={value} count=3") == (
        "password=[REDACTED] count=3"
    )


@pytest.mark.parametrize("quote", ['"', "'"])
def test_unfinished_quoted_assignment_redacts_remaining_text(quote):
    assert (
        redact_sensitive_assignments(
            f"token={quote}UnfinishedMarker987 with spaces\\"
        )
        == "token=[REDACTED]"
    )


@pytest.mark.parametrize("name", ["api_token", "service_url"])
def test_known_credential_names_in_messages_and_nested_extras(name):
    """These application credential names are not generic value shapes."""
    value = "discord://12345/InventoryMarker987"
    assert redact_log_message(f"{name}={value}") == f"{name}=[REDACTED]"
    assert redact_log_extra({"nested": {f"notifications.{name}": value}}) == {
        "nested": {f"notifications.{name}": "[REDACTED]"}
    }


def test_exception_notes_are_scrubbed_without_mutating_original(rendered_sink):
    exc = ValueError("ordinary failure")
    exc.add_note("token=NoteMarker987")
    exc.add_note("useful diagnostic note")
    original_notes = list(exc.__notes__)
    logger.opt(exception=exc).error("operation failed")
    rendered = rendered_sink.getvalue()
    assert "ValueError: ordinary failure" in rendered
    assert "useful diagnostic note" in rendered
    assert "NoteMarker987" not in rendered
    assert exc.__notes__ == original_notes


def test_group_message_and_notes_are_scrubbed(rendered_sink):
    records = []
    sink = logger.add(
        lambda message: records.append(message.record), diagnose=False
    )
    exc = ExceptionGroup("token=GroupMarker987", [ValueError("normal child")])
    exc.add_note("useful group note")
    try:
        logger.opt(exception=exc).error("group failed")
    finally:
        logger.remove(sink)
    rendered = rendered_sink.getvalue()
    assert "ExceptionGroup" in rendered and "normal child" in rendered
    assert "useful group note" in rendered
    assert "GroupMarker987" not in rendered
    assert exc.message == "token=GroupMarker987"
    from local_deep_research.utilities.log_utils import _exception_context

    assert records, "expected a group exception record"
    prefix = _exception_context(records[-1])
    assert "ExceptionGroup" in prefix
    assert "GroupMarker987" not in prefix


def test_group_keeps_benign_notes_when_only_child_changes(rendered_sink):
    exc = ExceptionGroup("ordinary group", [ValueError("token=ChildMarker987")])
    exc.add_note("preserve this diagnostic")
    logger.opt(exception=exc).error("group failed")
    rendered = rendered_sink.getvalue()
    assert "preserve this diagnostic" in rendered
    assert "ChildMarker987" not in rendered
    assert exc.exceptions[0].args == ("token=ChildMarker987",)


@pytest.mark.parametrize("count", [200, 201])
def test_exception_traversal_boundary_does_not_restore_raw_nodes(
    rendered_sink, count
):
    exc = ValueError("token=BoundaryMarker987")
    for _ in range(count - 1):
        parent = RuntimeError("ordinary wrapper")
        parent.__cause__ = exc
        exc = parent
    logger.opt(exception=exc).error("chain failed")
    rendered = rendered_sink.getvalue()
    assert "ordinary wrapper" in rendered
    assert "BoundaryMarker987" not in rendered
    if count == 201:
        assert "exception traversal limit reached" in rendered


def test_exception_cycle_uses_safe_placeholder(rendered_sink):
    outer = RuntimeError("token=CycleMarker987")
    inner = ValueError("ordinary child")
    outer.__cause__ = inner
    inner.__cause__ = outer
    logger.opt(exception=outer).error("cycle failed")
    rendered = rendered_sink.getvalue()
    assert "exception cycle omitted" in rendered
    assert "CycleMarker987" not in rendered
    assert inner.__cause__ is outer
    assert outer.args == ("token=CycleMarker987",)


@pytest.mark.parametrize("behavior", ["self", "alias", "raise"])
def test_custom_copy_hooks_are_never_called(rendered_sink, behavior):
    calls = []

    class CustomError(ValueError):
        def __copy__(self):
            calls.append(behavior)
            if behavior == "raise":
                raise TypeError("copy unavailable")
            return self if behavior == "self" else self.__context__

    exc = CustomError("token=CopyMarker987")
    exc.__context__ = ValueError("ordinary context")
    logger.opt(exception=exc).error("custom failure")
    rendered = rendered_sink.getvalue()
    assert "CustomError" in rendered
    assert "CopyMarker987" not in rendered
    assert calls == []
    assert exc.args == ("token=CopyMarker987",)
    assert exc.__context__.args == ("ordinary context",)


def test_nonstandard_constructor_preserves_safe_cause(rendered_sink):
    from types import SimpleNamespace

    from local_deep_research.security.egress.policy import PolicyDeniedError

    exc = PolicyDeniedError(SimpleNamespace(reason="synthetic denial"))
    cause = ValueError("token=PolicyCauseMarker987")
    exc.__cause__ = cause
    logger.opt(exception=exc).error("policy failure")
    rendered = rendered_sink.getvalue()
    assert "PolicyDeniedError" in rendered and "synthetic denial" in rendered
    assert "PolicyCauseMarker987" not in rendered
    assert exc.__cause__ is cause
    assert cause.args == ("token=PolicyCauseMarker987",)


def test_nested_logger_context_restores_existing_sink(captured_records):
    logger.info("before isolated context")
    with restored_loguru_state():
        logger.add(lambda message: None, diagnose=False)
        logger.info("inside isolated context")
    logger.info("after isolated context")
    assert [record["message"] for record in captured_records] == [
        "before isolated context",
        "after isolated context",
    ]


@pytest.mark.parametrize("field", ["text", "filename", "msg"])
def test_syntax_error_details_are_scrubbed(rendered_sink, field):
    exc = SyntaxError("invalid syntax", ("synthetic.py", 1, 1, "normal text"))
    setattr(exc, field, "token=SyntaxDetailMarker987")
    logger.opt(exception=exc).error("syntax failure")
    rendered = rendered_sink.getvalue()
    assert "SyntaxError" in rendered
    assert "SyntaxDetailMarker987" not in rendered
    assert getattr(exc, field) == "token=SyntaxDetailMarker987"


@pytest.mark.parametrize("separator", ["\n", "\r\n", "\t", "\u2028"])
def test_redaction_preserves_control_character_boundaries(
    captured_records, separator
):
    """Removing a separator must not hide an otherwise recognized name."""
    logger.info(f"failure{separator}token=BoundaryCredential739 count=3")
    assert captured_records, "sink captured nothing"
    assert captured_records[-1]["message"] == (
        "failuretoken=[REDACTED] count=3"
    )


@pytest.mark.parametrize("separator", ["\n", "\r\n", "\t", "\u2028", "\x1c"])
@pytest.mark.parametrize("name", ["password", "client_secret", "token"])
def test_scrub_error_output_keeps_control_character_boundaries(
    captured_records, separator, name
):
    """``logger.x(f"...{scrub_error(e)}")`` is the sanctioned pattern, so the
    patcher sees scrub_error's output, not the raw text. scrub_error must
    not glue a line-initial credential name onto the previous word."""
    safe = scrub_error(f"Request failed{separator}{name}=BoundaryCredential739")
    assert safe == f"Request failed{separator}{name}=BoundaryCredential739"
    logger.warning(f"Search failed: {safe}")
    assert captured_records, "sink captured nothing"
    message = captured_records[-1]["message"]
    assert "BoundaryCredential739" not in message
    assert message == f"Search failed: Request failed{name}=[REDACTED]"


@pytest.mark.parametrize("name", ["password", "pass\u200bword"])
def test_scrub_error_never_takes_a_name_from_its_quoted_value(
    captured_records, name
):
    """Joining lines lets a URL query run on into the next line. It must
    stop before a credential name whose quoted value it cannot reach, or
    the patcher is left the value without its name."""
    safe = scrub_error(f"GET https://h/p?q=1\n{name}='correct horse'")
    assert safe == f"GET https://h/p?<redacted>\n{name}='correct horse'"
    logger.warning(f"Search failed: {safe}")
    assert captured_records, "sink captured nothing"
    message = captured_records[-1]["message"]
    assert "correct" not in message
    assert "horse" not in message


def test_assignment_glued_onto_a_marker_is_redacted(captured_records):
    """Stripping the invisible character joins ``api_key`` onto the marker
    left by the assignment before it; that assignment's unquoted value must
    stop at the marker, not run on and take the name."""
    assert (
        redact_sensitive_assignments("token = [REDACTED]api_key = Glued739")
        == "token = [REDACTED]api_key = [REDACTED]"
    )
    assert (
        redact_sensitive_assignments('token=x;password="p q" r')
        == "token=[REDACTED]password=[REDACTED] r"
    )
    logger.info("token = abc\nap\u200bi_key = MarkerGlued739")
    assert captured_records, "sink captured nothing"
    assert "MarkerGlued739" not in captured_records[-1]["message"]


@pytest.mark.parametrize(
    "name", ["OPENAI_API_KEY", "db_password", "llm.openai.api_key"]
)
def test_prefixed_assignment_glued_onto_a_marker_is_redacted(
    captured_records, name
):
    """The next name must survive the first assignment's value match."""
    from local_deep_research.security.log_sanitizer import _redact_log_text

    cases = (
        (f"token = [REDACTED]{name} = MarkerGlued739", "MarkerGlued739"),
        (f'token=x;{name}="correct horse" r', "correct horse"),
    )
    for message, secret in cases:
        for redact in (
            redact_sensitive_assignments,
            redact_log_message,
            _redact_log_text,
        ):
            redacted = redact(message)
            assert all(word not in redacted for word in secret.split()), (
                redact.__name__,
                redacted,
            )
        logger.info(message)
        logged = captured_records[-1]["message"]
        assert all(word not in logged for word in secret.split()), logged


def test_normalized_credential_name_is_also_redacted(captured_records):
    """Redacting only before normalization misses an invisible name split."""
    logger.info("api_\u200bkey=NormalizedCredential739 count=3")
    assert captured_records, "sink captured nothing"
    assert captured_records[-1]["message"] == "api_key=[REDACTED] count=3"


@pytest.mark.parametrize("name", ["api_token", "service_url", "user_password"])
@pytest.mark.parametrize("prefix", ["?", "?page=3&"])
def test_known_query_names_preserve_other_parameters(
    captured_records, name, prefix
):
    """Bare and query assignments share names, but not value delimiters."""
    url = f"https://example.invalid/{prefix}{name}=QueryCredential739&sort=new#top"
    expected = f"https://example.invalid/{prefix}{name}=[REDACTED]&sort=new#top"
    assert redact_log_message(url) == expected
    logger.bind(detail=url).info(url)
    assert captured_records, "sink captured nothing"
    assert captured_records[-1]["message"] == expected
    assert captured_records[-1]["extra"]["detail"] == expected


def test_repeated_exception_children_respect_traversal_limit(captured_records):
    """Memoized children must not bypass the rendered group width limit."""
    child = ValueError("token=RepeatedCredential739")
    original = ExceptionGroup("ordinary group", [child] * 3000)
    logger.opt(exception=original).error("group failed")
    assert captured_records, "sink captured nothing"
    rendered = captured_records[-1]["exception"].value
    assert isinstance(rendered, ExceptionGroup)
    assert rendered is not original
    assert len(rendered.exceptions) == 200
    assert rendered.exceptions[0] is rendered.exceptions[1]
    assert str(rendered.exceptions[-1]) == "<remaining exceptions omitted>"
    assert all(
        "RepeatedCredential739" not in str(e) for e in rendered.exceptions
    )
    assert len(original.exceptions) == 3000
    assert all(e is child for e in original.exceptions)
    assert child.args == ("token=RepeatedCredential739",)


# The adversarial shapes below made the Slack/JWT patterns quadratic: every
# ``xoxb-``/``eyJ`` inside one character run started an attempt that rescanned
# the rest of the run. At these sizes the quadratic patterns took ~9-10 s per
# pass (measured); the linear ones take tens of milliseconds, so a 1 s budget
# has a wide margin on both sides.
_QUADRATIC_BUDGET_SECONDS = 1.0
# The sink test also runs Loguru's patcher for both the message and bound
# extra. Under coverage and xdist, this can exceed 1 s on a busy runner even
# though scanning remains bounded to the first 32 KiB of each value.
_LOG_PATH_BUDGET_SECONDS = 2.0


@pytest.mark.parametrize(
    ("fragment", "size"),
    [("xoxb-", 80_000), ("xapp-", 80_000), ("eyJ-", 200_000)],
)
def test_credential_shape_patterns_are_linear_on_repeated_prefixes(
    fragment, size
):
    """sanitize_error_message (not length-capped) stays fast on a long run."""
    import time

    # Measure redaction's CPU work without counting xdist/runner preemption.
    # The same one-second budget still rejects pathological backtracking.

    from local_deep_research.security.log_sanitizer import (
        sanitize_error_message,
    )

    message = fragment * (size // len(fragment))
    started = time.thread_time()
    sanitize_error_message(message)
    elapsed = time.thread_time() - started
    assert elapsed < _QUADRATIC_BUDGET_SECONDS, elapsed


# Explicit ids: without them pytest uses each string value verbatim as the
# test id, so the node ids here were 0.2-1 MB each. Every such id is written
# into the ``-v`` progress lines, the xdist reports and the JUnit XML, and in
# CI those four lines stalled the job's log for over an hour.
@pytest.mark.parametrize(
    "message",
    ["a" * 1_000_000, "xoxb-" * 40_000, "eyJ-" * 80_000, "token=x " * 100_000],
    ids=["a-1M", "xoxb-200K", "eyJ-320K", "token-assignment-800K"],
)
def test_log_path_redaction_cost_is_bounded(captured_records, message):
    """The patcher scans a bounded prefix and marks the omitted remainder."""
    import time

    started = time.perf_counter()
    logger.bind(detail=message).info(message)
    elapsed = time.perf_counter() - started
    assert elapsed < _LOG_PATH_BUDGET_SECONDS, elapsed
    record = captured_records[-1]
    for value in (record["message"], record["extra"]["detail"]):
        assert value.endswith("characters omitted from log output]")
        assert len(value) < 100_000


def test_truncation_does_not_split_a_credential():
    """A secret straddling the length bound is dropped, not half-emitted."""
    from local_deep_research.security.log_sanitizer import (
        _LOG_REDACTION_MAX_CHARS,
    )

    # A hard cut at the bound would keep "sk-Stradd", too short for the
    # ``sk-`` shape pattern to recognise, so it would reach the sink.
    prefix = "w" * (_LOG_REDACTION_MAX_CHARS - 11) + " "
    message = prefix + "sk-StraddlingKey0123456789abcdef tail " * 50
    out = redact_log_message(message)
    assert "sk-Stradd" not in out
    assert out.startswith(prefix)
    assert out[len(prefix) :].startswith(" [... ")
    assert out.endswith("characters omitted from log output]")
    assert redact_log_message("short message") == "short message"


def test_truncation_at_userinfo_at_sign_drops_the_whole_url():
    """A cut landing on the ``@`` must not keep ``user:password``.

    Without its ``@`` the URL-userinfo pattern cannot match, so a cut just
    before it would ship the password; the whole URL run is dropped instead.
    """
    from local_deep_research.security.log_sanitizer import (
        _LOG_REDACTION_MAX_CHARS,
        _redact_log_text,
    )

    url = "postgresql://admin:S3cretPassw0rd"
    pad_len = _LOG_REDACTION_MAX_CHARS - len(url)
    pad = "x " * (pad_len // 2) + "x" * (pad_len % 2)
    message = pad + url + "@db.internal:5432/app tail"
    assert message[_LOG_REDACTION_MAX_CHARS] == "@"
    for redact in (redact_log_message, _redact_log_text):
        out = redact(message)
        assert "S3cr" not in out
        assert "admin:" not in out
        assert out.endswith("characters omitted from log output]")


@pytest.mark.parametrize(
    "secret",
    [
        "sk-" + "A" * 22,
        "ghp_Ab3dEf6hIj9kLm2oPq5sTu8wXy1zAb4cD5eF6",
        "AKIAIOSFODNN7EXAMPLE",
        "admin:Tr0ub4dor&3xyzQ@db",
    ],
)
def test_truncation_without_whitespace_drops_the_whole_run(secret):
    """No whitespace before the bound: nothing of the run is kept.

    A hard cut (or a back-off to punctuation inside the run) would keep a
    key prefix too short for its shape pattern, or a password up to a ``&``.
    """
    from local_deep_research.security.log_sanitizer import (
        _LOG_REDACTION_MAX_CHARS,
        _redact_log_text,
    )

    for offset in (-12, -6, -3):
        head = "a/" * ((_LOG_REDACTION_MAX_CHARS + offset) // 2)
        message = head + secret + "9" * 20_000
        marker = f" [... {len(message)} characters omitted from log output]"
        for redact in (redact_log_message, _redact_log_text):
            out = redact(message)
            assert secret[:4] not in out
            assert out == marker


@pytest.mark.parametrize("separator", ["\n", "\t", "\x1c", "\u2028", "\x85"])
def test_truncation_does_not_cut_at_a_stripped_control_character(
    captured_records, separator
):
    """A control character inside a credential is not a cut boundary.

    ``_redact_log_text`` strips control characters between its passes,
    which rejoins ``S3cret`` and ``Passw0rd@db...`` into one URL the
    userinfo pattern redacts whole. Cutting at the control character and
    keeping ``postgresql://admin:S3cret`` would ship the password's first
    half: without its ``@`` the pattern cannot match.
    """
    from local_deep_research.security.log_sanitizer import (
        _LOG_REDACTION_MAX_CHARS,
        _redact_log_text,
    )

    url = f"postgresql://admin:S3cret{separator}[email protected]/app"
    assert "S3cr" not in _redact_log_text("pad " + url + " end")
    # The bound on the separator itself, and with "Pa" between the two.
    for gap in (0, 3):
        pad_len = _LOG_REDACTION_MAX_CHARS - (url.index(separator) + gap)
        pad = "x " * (pad_len // 2) + "x" * (pad_len % 2)
        message = pad[:-1] + " " + url + " " + "y" * 40_000
        assert message[_LOG_REDACTION_MAX_CHARS - gap] == separator
        logger.info(message)
        outputs = [
            _redact_log_text(message),
            redact_log_message(message),
            captured_records[-1]["message"],
        ]
        for out in outputs:
            assert "S3cr" not in out
            assert "admin" not in out
            assert "Pass" not in out
            assert out.endswith("characters omitted from log output]")


def test_log_length_before_truncation_reads_a_genuine_marker():
    """The patcher's marker yields the pre-cut length; others ``len()``."""
    from local_deep_research.security.log_sanitizer import (
        _LOG_REDACTION_MAX_CHARS,
        log_length_before_truncation,
    )

    message = "word " * (_LOG_REDACTION_MAX_CHARS // 2)
    out = redact_log_message(message)
    assert out.endswith("characters omitted from log output]")
    assert len(out) < len(message)
    assert log_length_before_truncation(out) == len(message)
    assert log_length_before_truncation("no marker") == len("no marker")


@pytest.mark.parametrize(
    "digits",
    ["9" * 4_300, "9" * 4_301, "9" * 20, "\u0663" * 5, "\uff19" * 5],
    ids=[
        "4300-digits",
        "4301-digits",
        "20-digits",
        "arabic-indic",
        "fullwidth",
    ],
)
def test_log_length_before_truncation_never_raises_on_a_forged_marker(
    digits,
):
    """Logged text can end in a forged marker; its count is not trusted.

    An unbounded digit group let ``int()`` raise past Python's digit limit,
    and the DB and frontend sinks, which report this length, then dropped
    the record.
    """
    from local_deep_research.security.log_sanitizer import (
        log_length_before_truncation,
    )

    message = (
        "x" * 6_000 + f" [... {digits} characters omitted from log output]"
    )
    assert log_length_before_truncation(message) == len(message)


def test_self_referential_extra_keeps_record_and_routing(captured_records):
    """A cycle is rendered as a placeholder instead of dropping all extras."""
    blob = {"note": "token=CycleCredential739"}
    blob["self"] = blob
    blob["items"] = [blob]
    logger.bind(username="alice", research_id="r-1", blob=blob).info("cyc")
    extra = captured_records[-1]["extra"]
    assert extra["username"] == "alice"
    assert extra["research_id"] == "r-1"
    assert extra["blob"]["note"] == "token=[REDACTED]"
    assert extra["blob"]["self"] == "<cycle>"
    assert extra["blob"]["items"] == ["<cycle>"]
    assert blob["note"] == "token=CycleCredential739"


def test_deeply_nested_extra_is_bounded(captured_records):
    deep = "token=DeepCredential739"
    for _ in range(5000):
        deep = [deep]
    logger.bind(username="alice", deep=deep).info("deep")
    extra = captured_records[-1]["extra"]
    assert extra["username"] == "alice"
    assert "DeepCredential739" not in repr(extra)
    assert "<nested too deep>" in repr(extra)


class _RaisingEq:
    def __eq__(self, other):
        raise RuntimeError("comparison refused")

    __hash__ = object.__hash__


def test_raising_eq_value_under_sensitive_key_is_masked():
    out = redact_log_extra(
        {"username": "alice", "api_key": _RaisingEq(), "note": "fine"}
    )
    assert out == {"username": "alice", "api_key": "[REDACTED]", "note": "fine"}


class _RaisingItems(dict):
    def items(self):
        raise RuntimeError("items refused")


def test_fail_closed_extra_keeps_routing_keys():
    """When extra cannot be redacted, only the routing keys survive."""
    from local_deep_research.security.log_sanitizer import redact_log_record

    record = {
        "message": "m",
        "extra": {
            "username": "alice",
            "research_id": "r-1",
            "policy_audit": True,
            "api_key": "FailClosedCredential739",
            "blob": _RaisingItems(secret="FailClosedCredential739"),
        },
    }
    redact_log_record(record)
    assert record["extra"] == {
        "username": "alice",
        "research_id": "r-1",
        "policy_audit": True,
    }


def test_extra_strings_are_normalized_like_messages(captured_records):
    """An invisible character cannot hide a credential name in an extra."""
    logger.bind(
        detail="api_\u200bkey=ExtraZeroWidth739 count=3",
        lines="failure\ntoken=ExtraNewline739 count=3",
    ).info("x")
    extra = captured_records[-1]["extra"]
    assert extra["detail"] == "api_key=[REDACTED] count=3"
    assert extra["lines"] == "failuretoken=[REDACTED] count=3"


def test_prefixed_sensitive_extra_keys_are_masked():
    """Extra keys use settings redaction's underscore-suffix rule."""
    out = redact_log_extra(
        {
            "openai_api_key": "PrefixedCredential739",
            "db_password": "PrefixedCredential739",
            "api\u200b_key": "PrefixedCredential739",
            "nested": {"llm.openai.api_key": "PrefixedCredential739"},
            "requires_api_key": True,
            "max_tokens": 512,
        }
    )
    assert "PrefixedCredential739" not in repr(out)
    assert out["openai_api_key"] == "[REDACTED]"
    assert out["db_password"] == "[REDACTED]"
    assert out["nested"]["llm.openai.api_key"] == "[REDACTED]"
    assert out["requires_api_key"] is True
    assert out["max_tokens"] == 512


class TestAddedCredentialShapesThroughThePatcher:
    """``_ADDITIONAL_CREDENTIAL_PATTERNS`` reach every sink of a record.

    The patcher runs them last over the message and the ``extra`` strings,
    and the database sink runs them over its exception prefix. The rendered
    exception value (``_redact_exception_value``) is redacted by
    ``redact_log_message`` alone and does not get them.
    """

    # Assembled so no token-shaped literal appears in the file (gitleaks).
    SECRET = "Kq7ZrXw2Lp9Vt4Ns" + "AddedShape"

    def test_message_extra_and_database_exception_prefix(
        self, captured_records
    ):
        from local_deep_research.security.log_sanitizer import scrub_error
        from local_deep_research.utilities.log_utils import _exception_context

        # The shape of llm_config._log_llm_error: a gateway echoing the
        # rejected request, scrubbed with no known literal, then logged.
        error = RuntimeError(
            "401 from gateway: rejected request "
            f"{{'headers': {{'api-key': '{self.SECRET}'}}}}"
        )
        logger.opt(exception=error).bind(
            detail=f"sent Api-Key: {self.SECRET}"
        ).error(f"LLM Request - Failed with error: {scrub_error(error)}")

        record = captured_records[-1]
        assert "LLM Request - Failed with error" in record["message"]
        assert self.SECRET not in record["message"]
        assert self.SECRET not in record["extra"]["detail"]
        prefix = _exception_context(record)
        assert prefix.startswith("[RuntimeError: 401 from gateway")
        assert self.SECRET not in prefix

    @pytest.mark.parametrize(
        "pad",
        [205, 215, 225, 230],
        ids=["22-left", "12-left", "2-left", "label-cut"],
    )
    def test_database_exception_prefix_is_redacted_before_its_cap(self, pad):
        """The added shapes run on the 4096-bounded text before the
        240-character cap: a key the cap would cut below the header
        pattern's 16-character floor (12 or 2 of its characters left, or
        its label itself cut) must not print its head."""
        from types import SimpleNamespace

        from local_deep_research.utilities.log_utils import _exception_context

        error = ValueError(
            "x" * pad + f" Api-Key: {self.SECRET}, then a long tail" * 3
        )
        prefix = _exception_context(
            {"exception": SimpleNamespace(type=ValueError, value=error)}
        )
        assert prefix.startswith("[ValueError: xxx")
        assert prefix.endswith("…] ")
        assert len(prefix) <= len("[ValueError: ] ") + 238
        assert f"Api-Key: {self.SECRET[:2]}" not in prefix, prefix

    def test_database_exception_prefix_slash_dsn_cut_before_its_at(self):
        """A slash-bearing DSN password whose ``@`` lies past the cap is
        redacted before the cap removes the ``@`` it is recognised by."""
        from types import SimpleNamespace

        from local_deep_research.utilities.log_utils import _exception_context

        password = "ab/cd" + "Zq7" * 12
        error = ValueError(
            "x" * 180 + f" postgresql://ldr:{password}@postgres:5432/ldr"
        )
        prefix = _exception_context(
            {"exception": SimpleNamespace(type=ValueError, value=error)}
        )
        assert "ab/cd" not in prefix, prefix
        assert "[REDACTED]:[REDACTED]@postgres" in prefix

    @pytest.mark.parametrize(
        "tail",
        [
            ' {"password": "hunter2' + "Zq" * 60 + " it's here\"}",
            " ;password=hunter2" + "Zq" * 60 + "&q=1",
        ],
        ids=["quote-of-the-other-kind", "ampersand"],
    )
    @pytest.mark.parametrize("pad", [100, 200])
    def test_database_exception_prefix_is_redacted_again_after_its_cap(
        self, pad, tail
    ):
        """A value the first pass leaves alone because of what follows it
        (a quote of the other kind, a ``&``) loses that to the cap; the
        pass on the capped text then redacts its head."""
        from types import SimpleNamespace

        from local_deep_research.utilities.log_utils import _exception_context

        error = ValueError("x" * pad + tail)
        prefix = _exception_context(
            {"exception": SimpleNamespace(type=ValueError, value=error)}
        )
        assert prefix.endswith("…] ")
        assert "hunter2" not in prefix, prefix
        assert "password" in prefix

    def test_one_match_in_a_long_message_costs_what_the_bound_allows(self):
        """One added-shape match in a long message is redacted at the cost
        of a few regex passes over the 32 KiB the patcher keeps: no
        per-character bookkeeping, no growth with the message."""
        import tracemalloc

        from local_deep_research.security.log_sanitizer import (
            redact_log_record,
        )

        message = '{"password": "q w"} ' + "lorem ipsum " * 90_000
        record = {"message": message, "extra": {"detail": message}}
        tracemalloc.start()
        try:
            redact_log_record(record)
            _, peak = tracemalloc.get_traced_memory()
        finally:
            tracemalloc.stop()
        assert record["message"].startswith('{"password": "[REDACTED]"}')
        assert record["extra"]["detail"].startswith(
            '{"password": "[REDACTED]"}'
        )
        # Two 32 KiB strings and their intermediate copies, with headroom.
        assert peak < 4 * 1024 * 1024, peak


# Longest node id the parametrized tests in this module may produce. The
# adversarial inputs above are hundreds of KB; they belong in the test body,
# never in the id pytest prints for every test.
_MAX_NODE_ID_LENGTH = 300


def test_parametrized_node_ids_stay_short(request):
    """No test in this module gets a multi-KB id from a parametrize value."""
    module_path = request.node.path
    long_ids = [
        (item.nodeid[:120], len(item.nodeid))
        for item in request.session.items
        if item.path == module_path and len(item.nodeid) > _MAX_NODE_ID_LENGTH
    ]
    assert not long_ids, long_ids


class _RaisingStrError(Exception):
    def __str__(self):
        raise RuntimeError("str refused")


class _NonStrStrError(Exception):
    def __str__(self):
        return 42


class _RaisingNote:
    def __str__(self):
        raise RuntimeError("note refused")


def _raise_and_log(exc):
    """Raise *exc* from this frame and log it.

    The rendered traceback then shows this frame's source line, ``raise exc``,
    which only a rendered traceback contains (the log line's header names the
    function but not the source line).
    """
    try:
        raise exc
    except BaseException:
        logger.opt(exception=True).error("unprintable failure")


@pytest.mark.parametrize(
    "exc_type", [_RaisingStrError, _NonStrStrError], ids=["raises", "non_str"]
)
def test_unprintable_exception_keeps_traceback_and_redacts_cause(
    rendered_sink, exc_type
):
    """A failing ``__str__`` replaces that node's text, not the traceback."""
    exc = exc_type()
    exc.__cause__ = ValueError("token=UnprintableCause739")
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "raise exc" in rendered
    assert f"<unprintable {exc_type.__name__}>" in rendered
    assert "UnprintableCause739" not in rendered


def test_unprintable_note_keeps_traceback_and_other_notes(rendered_sink):
    exc = ValueError("token=UnprintableNote739")
    exc.__notes__ = [_RaisingNote(), "kept diagnostic note"]
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "raise exc" in rendered
    assert "<unprintable _RaisingNote>" in rendered
    assert "kept diagnostic note" in rendered
    assert "UnprintableNote739" not in rendered


@pytest.mark.parametrize("link", ["__cause__", "__context__"])
def test_unprintable_cause_or_context_keeps_traceback(rendered_sink, link):
    exc = ValueError("token=UnprintableLink739")
    setattr(exc, link, _RaisingStrError())
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "raise exc" in rendered
    assert "_RaisingStrError" in rendered
    assert "UnprintableLink739" not in rendered


def test_unprintable_group_child_keeps_traceback(rendered_sink):
    exc = ExceptionGroup(
        "token=UnprintableGroup739",
        [_RaisingStrError(), ValueError("ordinary child")],
    )
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "raise exc" in rendered
    assert "_RaisingStrError" in rendered
    assert "ordinary child" in rendered
    assert "UnprintableGroup739" not in rendered


def test_exception_redaction_failure_keeps_traceback(
    rendered_sink, monkeypatch
):
    """If the rebuild itself raises, the value is replaced, not the record."""
    from local_deep_research.security import log_sanitizer

    def _fail(*args, **kwargs):
        raise RuntimeError("rebuild failed")

    monkeypatch.setattr(log_sanitizer, "_build_redacted_exception", _fail)
    _raise_and_log(ValueError("token=RebuildFailure739"))
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "raise exc" in rendered
    assert "redaction failed" in rendered
    assert "RebuildFailure739" not in rendered


def test_normalized_credential_name_in_exception_is_redacted(rendered_sink):
    """An invisible character cannot hide a credential name in an exception."""
    _raise_and_log(ValueError("api_\u200bkey=ExceptionZeroWidth739"))
    rendered = rendered_sink.getvalue()
    assert "ValueError" in rendered
    assert "ExceptionZeroWidth739" not in rendered


# Characters ``str.isspace()`` calls whitespace that a sink can still draw
# as nothing; each must be removed like a zero-width space.
_HIDING_WHITESPACE = {
    "fs": "\x1c",
    "us": "\x1f",
    "nel": "\x85",
    "vt": "\x0b",
    "ff": "\x0c",
    "ls": "\u2028",
}


@pytest.mark.parametrize(
    "char", list(_HIDING_WHITESPACE.values()), ids=list(_HIDING_WHITESPACE)
)
def test_whitespace_control_char_cannot_hide_a_credential_in_an_exception(
    rendered_sink, char
):
    """``str.isspace()`` is true for these, yet they can hide a split."""
    _raise_and_log(
        ValueError(
            f"db https://admin:hunt{char}[email protected]/ ; "
            f"tok{char}en=abcd1234NameSplit739"
        )
    )
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "UserInfo739" not in rendered
    assert "NameSplit739" not in rendered


@pytest.mark.parametrize(
    "char", list(_HIDING_WHITESPACE.values()), ids=list(_HIDING_WHITESPACE)
)
def test_whitespace_control_char_cannot_hide_a_credential_in_the_db_prefix(
    captured_records, char
):
    from local_deep_research.utilities.log_utils import _exception_context

    _raise_and_log(ValueError(f"lookup failed: tok{char}en=abcd1234DbRow739"))
    prefix = _exception_context(captured_records[-1])
    assert prefix.startswith("[ValueError: ")
    assert "DbRow739" not in prefix


def test_benign_multiline_exception_keeps_its_line_breaks(rendered_sink):
    exc = ValueError("first line\nsecond line")
    exc.__cause__ = ValueError("token=MultilineCause739")
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "first line\nsecond line" in rendered
    assert "MultilineCause739" not in rendered


class ApplicationError(Exception):
    """Module level, so an ``enqueue=True`` sink can pickle it by name."""


def test_rebuilt_application_exception_names_its_type_once(captured_records):
    from local_deep_research.utilities.log_utils import _exception_context

    _raise_and_log(ApplicationError("token=ContextPrefix739"))
    prefix = _exception_context(captured_records[-1])
    assert prefix == "[ApplicationError: token=[REDACTED]] "


def test_unpicklable_application_exception_type_is_replaced(captured_records):
    """A class defined in a function cannot stay the record's ``type``."""
    from local_deep_research.utilities.log_utils import _exception_context

    class LocalApplicationError(Exception):
        pass

    _raise_and_log(LocalApplicationError("token=ContextPrefix739"))
    record = captured_records[-1]
    assert record["exception"].type is RuntimeError
    assert _exception_context(record) == (
        "[RuntimeError: LocalApplicationError: token=[REDACTED]] "
    )


def test_extra_keys_sets_and_bytes_are_scrubbed():
    out = redact_log_extra(
        {
            "hits": {"https://alice:[email protected]/x": 3},
            "https://bob:[email protected]/": 1,
            "raw": b"api_key=ExtraBytesSecret739",
            "tags": {"token=ExtraSetSecret739"},
            "frozen": frozenset({"token=ExtraFrozenSecret739"}),
            "plain": b"ordinary\nbytes",
        }
    )
    assert "Secret739" not in repr(out)
    assert out["raw"] == "api_key=[REDACTED]"
    assert out["tags"] == ["token=[REDACTED]"]
    assert out["plain"] == b"ordinary\nbytes"


@pytest.mark.parametrize(
    "raw",
    [b"tok\x1fen=BytesHidden739", b"tok\nen=BytesHidden739"],
    ids=["unit_separator", "newline"],
)
def test_bytes_redacted_like_the_same_str(raw):
    """A bytes leaf is redacted whenever the same text as a str would be."""
    out = redact_log_extra({"raw": raw, "text": raw.decode()})
    assert out["text"] == "token=[REDACTED]"
    assert out["raw"] == "token=[REDACTED]"


class _RaisingNotesError(Exception):
    @property
    def __notes__(self):
        raise RuntimeError("token=" + "NotesProperty739")


def test_raising_notes_property_is_not_rendered(rendered_sink):
    """Python reports a raising ``__notes__`` with the error's repr."""
    _raise_and_log(_RaisingNotesError("ordinary failure"))
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "_RaisingNotesError: ordinary failure" in rendered
    assert "notes omitted" in rendered
    assert "NotesProperty739" not in rendered


@pytest.mark.parametrize(
    ("text", "expected"),
    [
        (
            "request failed: api_key=abc123\nstatus: 401",
            "request failed: api_key=[REDACTED]\nstatus: 401",
        ),
        ("token=abc\nxyz", "token=[REDACTED]\nxyz"),
    ],
    ids=["next_line_label", "next_line_word"],
)
def test_redacted_line_end_keeps_the_next_line(rendered_sink, text, expected):
    """The second pass must not glue a redacted value onto the next line."""
    _raise_and_log(ValueError(text))
    assert f"ValueError: {expected}" in rendered_sink.getvalue()


@pytest.mark.parametrize("routing_first", [True, False], ids=["first", "last"])
def test_redacted_key_cannot_replace_a_routing_key(routing_first):
    items = [("username", "alice"), ("user\u200bname", "attacker")]
    if not routing_first:
        items.reverse()
    out = redact_log_extra(dict(items))
    assert out["username"] == "alice"
    assert "attacker" in out.values()


def test_colliding_redacted_keys_keep_every_entry():
    out = redact_log_extra(
        {
            "nested": {
                "https://alice:[email protected]/": 1,
                "https://alice:[email protected]/": 2,
                ("token=TupleKey739",): 3,
            },
            "a\nb": 4,
            "ab": 5,
        }
    )
    assert "739" not in repr(out)
    assert sorted(out["nested"].values()) == [1, 2, 3]
    assert sorted(v for k, v in out.items() if k != "nested") == [4, 5]


def test_long_bytes_are_bounded_and_redacted():
    from local_deep_research.security.log_sanitizer import (
        _LOG_REDACTION_MAX_CHARS,
    )

    raw = b"api_key=LongBytes739 " + b"ab " * 100_000
    out = redact_log_extra({"raw": raw})["raw"]
    marker = (
        f" [... {len(raw)} bytes in total, the rest omitted from log output]"
    )
    assert isinstance(out, str)
    assert "LongBytes739" not in out
    assert out.startswith("api_key=[REDACTED] ab ab ")
    assert out.endswith(marker)
    # The kept text is the redacted head, cut at the character bound at a
    # space: no longer than the bound, and not much shorter.
    kept = out[: -len(marker)]
    assert (
        _LOG_REDACTION_MAX_CHARS - 16 <= len(kept) <= _LOG_REDACTION_MAX_CHARS
    )
    assert "omitted from log output" not in kept


def test_undecodable_byte_does_not_hide_a_credential_name():
    out = redact_log_extra({"raw": b"\x80token=BytesGlue739"})
    assert "BytesGlue739" not in repr(out)


def test_failed_redaction_keeps_the_type_in_the_db_prefix(
    captured_records, monkeypatch
):
    from local_deep_research.security import log_sanitizer
    from local_deep_research.utilities.log_utils import _exception_context

    def _fail(*args, **kwargs):
        raise RuntimeError("rebuild failed")

    monkeypatch.setattr(log_sanitizer, "_build_redacted_exception", _fail)
    _raise_and_log(ValueError("token=PrefixFailure739"))
    prefix = _exception_context(captured_records[-1])
    assert prefix == (
        "[ValueError: <exception text omitted: redaction failed>] "
    )


class ApplicationGroup(ExceptionGroup):
    """Module level, so an ``enqueue=True`` sink can pickle it by name."""


def test_group_subclass_keeps_its_name(rendered_sink):
    from local_deep_research.utilities.log_utils import _exception_context

    records = []
    sink = logger.add(
        lambda message: records.append(message.record), diagnose=False
    )
    try:
        _raise_and_log(
            ApplicationGroup("token=GroupSubclass739", [ValueError("child")])
        )
    finally:
        logger.remove(sink)
    rendered = rendered_sink.getvalue()
    # The rendered traceback names the subclass, not only a builtin group.
    assert "ApplicationGroup: token=[REDACTED]" in rendered
    assert "GroupSubclass739" not in rendered
    # The DB row took the name from the record's type before this change
    # too; what this pins is that the name the rebuilt group now carries in
    # its text is not repeated (``[ApplicationGroup: ApplicationGroup: ...``).
    assert _exception_context(records[-1]) == (
        "[ApplicationGroup: token=[REDACTED] (1 sub-exception)] "
    )


class _NoteWithHiddenStr:
    """A note whose ``str()`` (what traceback renders) differs from repr."""

    def __str__(self):
        return "token=" + "NoteItemStr739"

    def __repr__(self):
        return "<note>"


class _NotesObject:
    """A non-sequence ``__notes__``: traceback renders its ``repr()``."""

    def __str__(self):
        return "benign notes"

    def __repr__(self):
        return "token=" + "NotesRepr739"


def test_user_list_notes_are_checked_item_by_item(rendered_sink):
    from collections import UserList

    exc = ValueError("ordinary failure")
    exc.__notes__ = UserList([_NoteWithHiddenStr(), "kept note"])
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "token=[REDACTED]" in rendered
    assert "kept note" in rendered
    assert "NoteItemStr739" not in rendered


def test_non_sequence_notes_are_checked_by_repr(rendered_sink):
    exc = ValueError("ordinary failure")
    exc.__notes__ = _NotesObject()
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "token=[REDACTED]" in rendered
    assert "NotesRepr739" not in rendered


class _ChangingNote:
    """A note whose text changes after the patcher has checked it."""

    def __init__(self):
        self.calls = 0

    def __str__(self):
        self.calls += 1
        if self.calls == 1:
            return "benign note"
        return "token=" + "ChangingNote739"


def test_note_object_is_rendered_as_checked(rendered_sink):
    """Sinks render the note text that was checked, not a fresh ``str()``."""
    exc = ValueError("ordinary failure")
    exc.__notes__ = [_ChangingNote()]
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "benign note" in rendered
    assert "ChangingNote739" not in rendered


# The sinks render an exception again after the patcher checked it
# (loguru's ``format_exception_only``, the DB row's ``str()``). Each class
# below renders benign text, or raises, on the first call and a credential
# on every later one, so a patcher that checks the text and then passes the
# original object on lets the credential reach the sink.
_LATER_SECRET = "password=" + "LaterCallSecret739"


class _FirstCall:
    """Benign on the first call (or raise, when *first* is an exception)."""

    def __init__(self, first):
        self.first = first
        self.calls = 0

    def __call__(self):
        self.calls += 1
        if self.calls > 1:
            return _LATER_SECRET
        if isinstance(self.first, BaseException):
            raise self.first
        return self.first


def _changing_str_error(first):
    first_call = _FirstCall(first)

    class ChangingStrError(Exception):
        def __str__(self):
            return first_call()

    return ChangingStrError()


def _changing_notes_error():
    first_call = _FirstCall("benign note")

    class ChangingNotesError(Exception):
        @property
        def __notes__(self):
            return [first_call()]

    return ChangingNotesError("ordinary failure")


def _changing_cause_error():
    first_call = _FirstCall(None)

    class ChangingCauseError(Exception):
        @property
        def __cause__(self):
            text = first_call()
            return None if text is None else ValueError(text)

        @__cause__.setter
        def __cause__(self, value):
            pass

    return ChangingCauseError("ordinary failure")


def _changing_group():
    first_call = _FirstCall("benign group")

    class ChangingGroup(ExceptionGroup):
        def __str__(self):
            return first_call()

    return ChangingGroup("group", [ValueError("ordinary child")])


class _ChangingArg:
    """An argument object: ``ValueError(obj)`` renders ``str(obj)``."""

    def __init__(self):
        self.first_call = _FirstCall("benign argument")

    def __str__(self):
        return self.first_call()


class _SecretStr(str):
    """A ``str`` subclass that renders a credential however it is shown."""

    def __str__(self):
        return _LATER_SECRET

    __repr__ = __str__

    def __format__(self, spec):
        return _LATER_SECRET


def _str_subclass_error():
    class SubclassTextError(Exception):
        def __str__(self):
            return _SecretStr("benign text")

    return SubclassTextError()


# The same overrides on a mixin listed after a builtin base. The builtin
# classes in between (``ValueError``, ``Exception``, ``BaseException``) do
# not define ``__str__``, ``__notes__``, ``__getattr__`` or
# ``__getattribute__`` themselves, so Python still finds the mixin's.
def _mixin_str_error():
    first_call = _FirstCall("benign text")

    class StrMixin(Exception):
        def __str__(self):
            return first_call()

    # MRO: MixinStrError, ValueError, StrMixin, Exception, BaseException.
    class MixinStrError(ValueError, StrMixin):
        pass

    return MixinStrError()


def _mixin_notes_error():
    first_call = _FirstCall("benign note")

    class NotesMixin:
        @property
        def __notes__(self):
            return [first_call()]

    # MRO: MixinNotesError, ValueError, Exception, BaseException,
    # NotesMixin, object.
    class MixinNotesError(ValueError, NotesMixin):
        pass

    return MixinNotesError("ordinary failure")


def _mixin_getattr_error():
    first_call = _FirstCall("benign note")

    class GetattrMixin:
        def __getattr__(self, name):
            if name == "__notes__":
                return [first_call()]
            raise AttributeError(name)

    class MixinGetattrError(KeyError, GetattrMixin):
        pass

    return MixinGetattrError("ordinary failure")


def _mixin_getattribute_notes_error():
    first_call = _FirstCall("benign note")

    class GetattributeMixin:
        def __getattribute__(self, name):
            if name == "__notes__":
                return [first_call()]
            return super().__getattribute__(name)

    class MixinGetattributeError(RuntimeError, GetattributeMixin):
        pass

    return MixinGetattributeError("ordinary failure")


def _mixin_getattribute_cause_error():
    first_call = _FirstCall(None)

    class CauseMixin:
        def __getattribute__(self, name):
            if name == "__cause__":
                text = first_call()
                return None if text is None else ValueError(text)
            return super().__getattribute__(name)

    class MixinCauseError(Exception, CauseMixin):
        pass

    return MixinCauseError("ordinary failure")


@pytest.mark.parametrize(
    "make",
    [
        lambda: _changing_str_error(RuntimeError("first str() refused")),
        lambda: _changing_str_error("benign text"),
        _changing_notes_error,
        _changing_cause_error,
        _changing_group,
        lambda: ValueError(_ChangingArg()),
        _str_subclass_error,
        _mixin_str_error,
        _mixin_notes_error,
        _mixin_getattr_error,
        _mixin_getattribute_notes_error,
        _mixin_getattribute_cause_error,
    ],
    ids=[
        "str_raises_first",
        "str_benign_first",
        "notes_property",
        "cause_property",
        "group_str",
        "argument_object",
        "str_subclass",
        "mixin_after_builtin_str",
        "mixin_after_builtin_notes_property",
        "mixin_after_builtin_getattr",
        "mixin_after_builtin_getattribute_notes",
        "mixin_after_builtin_getattribute_cause",
    ],
)
def test_sinks_render_the_exception_text_that_was_checked(rendered_sink, make):
    """Text read again by a sink must be the text the patcher checked.

    A second sink on the same patcher captures the record, so the DB row
    prefix (``_exception_context``) is checked on the same record.
    """
    from local_deep_research.utilities.log_utils import _exception_context

    records = []
    sink = logger.add(
        lambda message: records.append(message.record), diagnose=False
    )
    try:
        _raise_and_log(make())
    finally:
        logger.remove(sink)
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "raise exc" in rendered
    assert "LaterCallSecret739" not in rendered
    assert "LaterCallSecret739" not in _exception_context(records[-1])


def test_unprintable_exception_alone_is_rebuilt_with_placeholder(
    rendered_sink,
):
    """Nothing else changes, yet the placeholder text is what is rendered."""
    _raise_and_log(_RaisingStrError())
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "<unprintable _RaisingStrError>" in rendered


def test_stable_exception_keeps_its_identity(captured_records):
    """A builtin exception with plain text is not rebuilt."""
    exc = OSError(2, "No such file", "plain-name.txt")
    _raise_and_log(exc)
    assert captured_records[-1]["exception"].value is exc


class _ChangingKey:
    """A dict key whose ``str()`` raises once and then renders a secret."""

    def __init__(self):
        self.first_call = _FirstCall(RuntimeError("first str() refused"))

    def __str__(self):
        return self.first_call()

    __repr__ = __str__

    def __hash__(self):
        return 1


def test_extra_object_key_is_replaced_by_its_checked_text():
    out = redact_log_extra({"hits": {_ChangingKey(): 1}})
    assert list(out["hits"]) == ["<unprintable _ChangingKey>"]
    assert "LaterCallSecret739" not in repr(out)


def test_extra_empty_str_subclass_becomes_plain_str():
    out = redact_log_extra({"note": _SecretStr("")})
    assert type(out["note"]) is str
    assert "LaterCallSecret739" not in repr(out)


# Hooks a sink runs on an exception while rendering it, after the patcher
# checked its text: loguru's ``if exc_value:`` (``__bool__``/``__len__``),
# the ``isinstance`` group check (``__class__``), a chained exception's
# ``__traceback__``, pickling by an ``enqueue=True`` sink (``__reduce_ex__``,
# and the objects in the instance dict), the type's ``__module__`` (a
# metaclass descriptor) and the "Did you mean" suggestion (``obj.__dir__()``
# and the named module's ``__dir__``). Each hook rewrites the exception's
# text once the record has been patched, so a kept original renders (or
# makes a later sink render) text that was never checked.
_ARMED = []


def _rewrite_after_patching(exc):
    if _ARMED:
        exc.args = (_LATER_SECRET,)
        if issubclass(type(exc), ImportError):
            exc.msg = _LATER_SECRET


def _bool_mixin_error():
    class BoolMixin:
        def __bool__(self):
            _rewrite_after_patching(self)
            return True

    class BoolMixinError(ValueError, BoolMixin):
        pass

    return BoolMixinError("benign text")


def _len_error():
    class LenError(Exception):
        def __len__(self):
            _rewrite_after_patching(self)
            return 1

    return LenError("benign text")


def _class_property_error():
    class ClassPropertyError(Exception):
        @property
        def __class__(self):
            _rewrite_after_patching(self)
            return type(self)

    return ClassPropertyError("benign text")


def _traceback_property_cause():
    class TracebackPropertyError(Exception):
        @property
        def __traceback__(self):
            _rewrite_after_patching(self)
            return None

        @__traceback__.setter
        def __traceback__(self, value):
            pass

    exc = ValueError("outer failure")
    exc.__cause__ = TracebackPropertyError("benign cause")
    return exc


def _reduce_ex_error():
    class ReduceExError(Exception):
        def __reduce_ex__(self, protocol):
            _rewrite_after_patching(self)
            return super().__reduce_ex__(protocol)

    return ReduceExError("benign text")


def _pickled_attribute_error():
    exc = ValueError("benign text")

    class Payload:
        def __reduce__(self):
            _rewrite_after_patching(exc)
            return (str, ("payload",))

    exc.payload = Payload()
    return exc


def _metaclass_module_error():
    holder = []

    class ModuleMeta(type):
        @property
        def __module__(cls):
            if holder:
                _rewrite_after_patching(holder[0])
            return "app.errors"

    class MetaclassError(Exception, metaclass=ModuleMeta):
        pass

    holder.append(MetaclassError("benign text"))
    return holder[0]


def _len_override_class(meta):
    """An exception class with a ``__len__`` hook, under metaclass *meta*."""

    class HiddenHookError(Exception, metaclass=meta):
        def __len__(self):
            _rewrite_after_patching(self)
            return 1

    return HiddenHookError


def _metaclass_mro_error():
    class MroMeta(type):
        @property
        def __mro__(cls):
            return (Exception, BaseException, object)

    return _len_override_class(MroMeta)("benign text")


def _metaclass_dict_error():
    class DictMeta(type):
        @property
        def __dict__(cls):
            return {}

    return _len_override_class(DictMeta)("benign text")


def _metaclass_callable_getattribute_error():
    holder = []

    class Hook:
        # Not a descriptor (no ``__get__``), but CPython still calls it for
        # every attribute read on the class.
        def __call__(self, *args):
            if holder:
                _rewrite_after_patching(holder[0])
            return type.__getattribute__(args[0], args[-1])

    class HookMeta(type):
        __getattribute__ = Hook()

    class HookedError(Exception, metaclass=HookMeta):
        pass

    holder.append(HookedError("benign text"))
    return holder[0]


def _metaclass_name_error():
    class NameMeta(type):
        @property
        def __name__(cls):
            return _LATER_SECRET if _ARMED else "AppError"

    class NamedError(Exception, metaclass=NameMeta):
        pass

    return NamedError("password=hunter2 benign text")


def _secret_class_name_error():
    return type(_LATER_SECRET, (Exception,), {})("benign text")


class _AlwaysEqualMeta(type):
    """Answers ``True`` to ``==``, so ``kind in (str, int)`` holds for it."""

    def __eq__(cls, other):
        return True

    __hash__ = type.__hash__


def _metaclass_eq_argument_error():
    class Argument(metaclass=_AlwaysEqualMeta):
        def __str__(self):
            return _LATER_SECRET if _ARMED else "benign text"

        __repr__ = __str__

    return ValueError(Argument())


def _attribute_suggestion_error():
    holder = []

    class Target:
        def __dir__(self):
            _rewrite_after_patching(holder[0])
            return ["present"]

    holder.append(AttributeError("benign text", name="presnt", obj=Target()))
    return holder[0]


def _import_suggestion_error(monkeypatch):
    import sys
    import types

    holder = []
    module = types.ModuleType("ldr_log_sink_probe_module")

    def __dir__():
        _rewrite_after_patching(holder[0])
        return ["present"]

    module.__dir__ = __dir__
    monkeypatch.setitem(sys.modules, module.__name__, module)
    holder.append(
        ImportError("benign text", name=module.__name__, name_from="presnt")
    )
    return holder[0]


@pytest.mark.parametrize(
    "make",
    [
        lambda mp: _bool_mixin_error(),
        lambda mp: _len_error(),
        lambda mp: _class_property_error(),
        lambda mp: _traceback_property_cause(),
        lambda mp: _reduce_ex_error(),
        lambda mp: _pickled_attribute_error(),
        lambda mp: _metaclass_module_error(),
        lambda mp: _metaclass_mro_error(),
        lambda mp: _metaclass_dict_error(),
        lambda mp: _metaclass_callable_getattribute_error(),
        lambda mp: _metaclass_name_error(),
        lambda mp: _secret_class_name_error(),
        lambda mp: _metaclass_eq_argument_error(),
        lambda mp: _attribute_suggestion_error(),
        _import_suggestion_error,
    ],
    ids=[
        "bool_mixin",
        "len",
        "class_property",
        "cause_traceback_property",
        "reduce_ex",
        "pickled_attribute",
        "metaclass_module",
        "metaclass_mro",
        "metaclass_dict",
        "metaclass_callable_getattribute",
        "metaclass_name",
        "secret_class_name",
        "metaclass_eq_argument",
        "attribute_suggestion",
        "import_suggestion",
    ],
)
def test_render_time_hooks_cannot_change_a_kept_exception(make, monkeypatch):
    """Sinks: arm the hooks, render, pickle (as ``enqueue=True``), DB row."""
    import io
    import pickle

    from local_deep_research.utilities.log_utils import (
        _exception_context,
        _sanitize_record,
    )

    def pickle_like_enqueue(message):
        # What an ``enqueue=True`` sink's queue put does; the patcher makes
        # the record picklable, so this must not raise (loguru's error
        # handler would dump the unredacted original to stderr).
        pickle.dumps(message)

    exc = make(monkeypatch)
    stream = io.StringIO()
    records = []
    prefixes = []
    _ARMED.clear()
    try:
        with restored_loguru_state():
            logger.remove()
            logger.configure(patcher=_sanitize_record)
            logger.add(lambda message: _ARMED.append(True), diagnose=False)
            logger.add(stream, diagnose=False, backtrace=False, colorize=False)
            logger.add(pickle_like_enqueue, diagnose=False)
            logger.add(
                lambda message: records.append(message.record),
                diagnose=False,
            )
            # The DB row prefix, built at sink time like the DB sink does.
            logger.add(
                lambda message: prefixes.append(
                    _exception_context(message.record)
                ),
                diagnose=False,
            )
            _raise_and_log(exc)
    finally:
        _ARMED.clear()
    rendered = stream.getvalue()
    assert "raise exc" in rendered
    assert "LaterCallSecret739" not in rendered
    assert "LaterCallSecret739" not in prefixes[-1]
    assert "LaterCallSecret739" not in _exception_context(records[-1])


def test_suggestion_exception_is_rebuilt_as_its_builtin_type(rendered_sink):
    """A builtin exception rebuilt to skip the suggestion keeps its type."""
    import types

    target = types.SimpleNamespace(field=1)
    _raise_and_log(AttributeError("no such field", name="feild", obj=target))
    rendered = rendered_sink.getvalue()
    assert "AttributeError: no such field" in rendered
    assert "Did you mean" not in rendered


def _syntax_error_with_typo_source():
    exc = SyntaxError("invalid syntax", ("<probe>", 1, 1, "improt os\n", 1, 7))
    # The keyword-typo suggestion (Python 3.13+) shows the source line it
    # fixed, from ``_metadata``, in place of ``text``.
    exc._metadata = (0, 0, "improt os  # password=TypoSecret771\n")
    return exc


@pytest.mark.parametrize(
    "make",
    [
        lambda: SyntaxError(
            "invalid syntax",
            (b"https://user:TypoSecret771@host/x.py", 1, 1, "x", 1, 2),
        ),
        lambda: SyntaxError(
            "invalid syntax",
            (
                b"https://user:TypoSecret771@host/x.py",
                None,
                None,
                None,
                None,
                None,
            ),
        ),
        lambda: SyntaxError(
            "invalid syntax", ("f.py", "password=TypoSecret771", 1, "x", 1, 2)
        ),
        _syntax_error_with_typo_source,
    ],
    ids=["bytes_filename", "bytes_filename_no_line", "str_lineno", "typo"],
)
def test_syntax_error_details_are_checked_as_rendered(rendered_sink, make):
    _raise_and_log(make())
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "TypoSecret771" not in rendered


class _FalsyText(str):
    """Text that claims to be false, so ``not value`` holds."""

    def __bool__(self):
        return False


class _ZeroLengthText(str):
    """Text that claims to have no characters."""

    def __len__(self):
        return 0


@pytest.mark.parametrize(
    "kind", [_FalsyText, _ZeroLengthText], ids=["bool", "len"]
)
def test_text_that_claims_to_be_empty_is_still_redacted(kind):
    value = kind("password=" + "FalsySecret4242")
    out = redact_log_extra(
        {
            "detail": value,
            kind("token=" + "FalsySecret4242"): 1,
            "nested": [value],
            "password": kind("hunter2" + "FalsySecret4242"),
        }
    )
    assert "FalsySecret4242" not in repr(out)
    assert out["password"] == "[REDACTED]"
    assert "FalsySecret4242" not in redact_log_message(value)
    assert "FalsySecret4242" not in redact_sensitive_assignments(value)


@pytest.mark.parametrize(
    "kind", [_FalsyText, _ZeroLengthText], ids=["bool", "len"]
)
def test_falsy_text_is_redacted_in_records(captured_records, kind):
    logger.bind(detail=kind("password=" + "FalsySecret4242")).info(
        kind("token=" + "FalsySecret4242")
    )
    record = captured_records[-1]
    assert "FalsySecret4242" not in repr(record["extra"])
    assert "FalsySecret4242" not in str.__str__(record["message"])


@pytest.mark.parametrize(
    "kind", [_FalsyText, _ZeroLengthText], ids=["bool", "len"]
)
def test_falsy_group_message_is_redacted(rendered_sink, kind):
    _raise_and_log(
        ExceptionGroup(kind("password=" + "FalsySecret4242"), [ValueError()])
    )
    assert "FalsySecret4242" not in rendered_sink.getvalue()


class _EmptyLookingDict(dict):
    def __len__(self):
        return 0


def test_container_subclass_claiming_to_be_empty_is_masked():
    out = redact_log_extra(
        {"password": _EmptyLookingDict(note="hunter2" + "FalsySecret4242")}
    )
    assert out == {"password": "[REDACTED]"}


class _ForgedRoutingKey(str):
    """Hashes and compares equal to ``"username"`` whatever its text."""

    def __hash__(self):
        return hash("username")

    def __eq__(self, other):
        return other == "username" or str.__eq__(self, other)


class _HarmlessLookingKey(str):
    """Reports a harmless last segment to ``rsplit``-based name checks."""

    def rsplit(self, *args, **kwargs):
        return ["benign"]

    def lower(self):
        return "benign"


_FORGED_KEY_TEXT = "token=ghp_" + "a" * 36


@pytest.mark.parametrize(
    ("key", "value", "secrets"),
    [
        (
            _ForgedRoutingKey(_FORGED_KEY_TEXT),
            "password=hunter2plain",
            ("hunter2plain", "a" * 36),
        ),
        (_HarmlessLookingKey("db_password"), "hunter2plain", ("hunter2plain",)),
    ],
    ids=["forged_routing_key", "rsplit_override"],
)
def test_str_subclass_extra_key_is_checked_by_its_characters(
    captured_records, key, value, secrets
):
    """A ``str`` subclass key is no routing key and no harmless name."""
    logger.bind(**{key: value}).info("bound")
    extra = captured_records[-1]["extra"]
    assert all(type(k) is str for k in extra)
    assert "username" not in extra
    for secret in secrets:
        assert secret not in repr(extra)


def test_fallback_extra_keeps_only_exact_routing_keys(
    captured_records, monkeypatch
):
    """The fail-closed path does not keep a key that only equals a route."""
    from local_deep_research.security import log_sanitizer

    def _fail(extra):
        raise RuntimeError("extra redaction failed")

    monkeypatch.setattr(log_sanitizer, "redact_log_extra", _fail)
    logger.bind(
        **{_ForgedRoutingKey(_FORGED_KEY_TEXT): "password=hunter2plain"},
        research_id=7,
    ).info("bound")
    assert captured_records[-1]["extra"] == {"research_id": 7}


class _EmptyLookingSecret(metaclass=_AlwaysEqualMeta):
    """Claims (through its metaclass) to be a ``str``, and to be empty."""

    def __len__(self):
        return 0

    def __repr__(self):
        return "password=" + "MetaEqSecret741"

    __str__ = __repr__


def test_metaclass_eq_cannot_pass_a_value_or_key_as_a_builtin(
    captured_records,
):
    """``type(x) in (str, ...)`` would be ``True`` for these; ``is`` is not."""
    logger.bind(
        password=_EmptyLookingSecret(), outer={_EmptyLookingSecret(): 1}
    ).info("bound")
    extra = captured_records[-1]["extra"]
    assert extra["password"] == "[REDACTED]"
    assert "MetaEqSecret741" not in repr(extra)


def test_failed_redaction_redacts_the_type_name(captured_records, monkeypatch):
    """The placeholder and the DB prefix name the type, redacted."""
    from local_deep_research.security import log_sanitizer
    from local_deep_research.utilities.log_utils import _exception_context

    def _fail(*args, **kwargs):
        raise RuntimeError("rebuild failed")

    monkeypatch.setattr(log_sanitizer, "_build_redacted_exception", _fail)
    _raise_and_log(type(_LATER_SECRET, (ValueError,), {})("benign"))
    record = captured_records[-1]
    assert "LaterCallSecret739" not in str(record["exception"].value)
    assert "LaterCallSecret739" not in _exception_context(record)


# ``enqueue=True`` stderr sinks (both processes) pickle every record before
# queueing it. When that fails, loguru's error handler writes ``str(record)``
# and the pickling traceback to ``sys.stderr``, and that traceback's
# ``__context__`` is the exception being handled -- the unredacted original.
_ENQUEUE_SECRET = "EnqueueDump" + "Secret739"


def _local_class_with_secret_text():
    class ProviderError(Exception):
        pass

    return ProviderError(f"request failed: api_key=sk-live-{_ENQUEUE_SECRET}")


def _clean_local_class_with_secret_cause():
    class LocalError(Exception):
        pass

    exc = LocalError("clean text")
    exc.__cause__ = ValueError(f"token=ghp_{_ENQUEUE_SECRET}")
    return exc


def _secret_named_class():
    return type(f"password={_ENQUEUE_SECRET}", (ValueError,), {})("benign")


def _builtin_with_secret_text():
    return ValueError(f"token=ghp_{_ENQUEUE_SECRET}")


@pytest.fixture
def enqueue_stderr(monkeypatch):
    """A real ``enqueue=True`` stderr sink, as ``config_logger`` adds it.

    ``sys.stderr`` is replaced first, so it captures both the sink's output
    and loguru's error handler, which writes to ``sys.stderr`` directly.
    The MCP process's sink (``configure_mcp_logging``) is tested the same
    way in tests/mcp/test_stderr_control_chars.py.
    """
    import io
    import sys

    from local_deep_research.utilities.log_utils import _sanitize_record

    stderr = io.StringIO()
    monkeypatch.setattr(sys, "stderr", stderr)
    with restored_loguru_state():
        logger.remove()
        logger.configure(patcher=_sanitize_record)
        # config_logger's stderr sink (utilities/log_utils.py).
        logger.add(sys.stderr, level="INFO", diagnose=False, enqueue=True)

        def read():
            logger.complete()
            return stderr.getvalue()

        yield read


@pytest.mark.parametrize(
    "make, bind_lock, with_exception",
    [
        (_local_class_with_secret_text, False, True),
        (_clean_local_class_with_secret_cause, False, True),
        (_secret_named_class, False, True),
        (_builtin_with_secret_text, True, True),
        (_builtin_with_secret_text, True, False),
    ],
    ids=[
        "local_class",
        "local_class_cause",
        "secret_class_name",
        "unpicklable_extra",
        "unpicklable_extra_no_exception",
    ],
)
def test_enqueue_sink_never_dumps_the_unredacted_exception(
    enqueue_stderr, make, bind_lock, with_exception
):
    """Logged with or without the exception, while handling it."""
    import threading

    bound = {"lock": threading.Lock()} if bind_lock else {}
    exc = make()
    try:
        raise exc
    except BaseException:
        logger.bind(**bound).opt(exception=with_exception).error(
            "handled failure"
        )
    out = enqueue_stderr()
    assert "handled failure" in out
    assert "Logging error in Loguru Handler" not in out
    assert "EnqueueDumpSecret739" not in out


def test_unpicklable_extra_value_is_replaced_by_its_type_name(
    captured_records,
):
    import threading

    logger.bind(lock=threading.Lock(), research_id=7).info("bound")
    extra = captured_records[-1]["extra"]
    assert extra == {"lock": "<unpicklable lock>", "research_id": 7}


# Reads of ``_FlippingModuleError.__module__`` through its metaclass since
# the counter was last reset.
_MODULE_READS = [0]


class _FlippingModuleMeta(type):
    """Names the real module on the first ``__module__`` read, then another.

    Pickling looks a class up by the ``__module__`` it reads with
    ``getattr``, which finds this property before the class's own entry: a
    trial pickle succeeds and the ``enqueue=True`` sink's pickle fails.
    """

    @property
    def __module__(cls):
        _MODULE_READS[0] += 1
        return __name__ if _MODULE_READS[0] == 1 else "builtins"


class _FlippingModuleError(Exception, metaclass=_FlippingModuleMeta):
    """Module level, so pickle can find it while its metaclass allows."""


def test_class_whose_pickling_can_change_is_not_kept_as_record_type(
    enqueue_stderr,
):
    """A rebuilt exception's class is replaced, not trusted after one trial."""
    _MODULE_READS[0] = 0
    try:
        raise _FlippingModuleError(
            f"request failed: api_key=sk-live-{_ENQUEUE_SECRET}"
        )
    except _FlippingModuleError:
        logger.exception("handled failure")
    out = enqueue_stderr()
    assert "handled failure" in out
    assert "_FlippingModuleError: request failed" in out
    assert "Logging error in Loguru Handler" not in out
    assert _ENQUEUE_SECRET not in out


def test_flipping_metaclass_type_is_replaced_in_the_record(captured_records):
    _MODULE_READS[0] = 0
    _raise_and_log(_FlippingModuleError("benign"))
    record = captured_records[-1]
    assert record["exception"].type is RuntimeError
    assert str(record["exception"].value) == "_FlippingModuleError: benign"


class _HookedOuterMeta(type):
    def __getattribute__(cls, name):
        return type.__getattribute__(cls, name)


class _HookedOuter(metaclass=_HookedOuterMeta):
    class Error(Exception):
        """Found by pickle only through its enclosing class's metaclass."""


class _PlainOuter:
    class Error(Exception):
        """Found by pickle through plain class dicts."""


class _ModuleLevelError(Exception):
    """Found by pickle in this module's namespace."""


@pytest.mark.parametrize(
    "kind, kept",
    [
        (ValueError, True),
        (_ModuleLevelError, True),
        (_PlainOuter.Error, True),
        (_FlippingModuleError, False),
        (_HookedOuter.Error, False),
    ],
    ids=["builtin", "module_level", "nested", "metaclass_hook", "outer_hook"],
)
def test_record_type_is_kept_only_when_pickling_it_runs_no_class_code(
    kind, kept
):
    from local_deep_research.security.log_sanitizer import (
        _record_type_is_safe,
    )

    _MODULE_READS[0] = 0
    assert _record_type_is_safe(kind) is kept
    # Deciding never ran the metaclass's code.
    assert _MODULE_READS[0] == 0


def test_type_without_value_is_checked_too(enqueue_stderr):
    """``opt(exception=(Type, None, tb))``: the sink still pickles ``type``."""
    local = type("LocalError", (Exception,), {})
    try:
        raise _builtin_with_secret_text()
    except ValueError:
        logger.opt(exception=(local, None, None)).error("handled failure")
    out = enqueue_stderr()
    assert "handled failure" in out
    assert "RuntimeError: LocalError" in out
    assert "Logging error in Loguru Handler" not in out
    assert _ENQUEUE_SECRET not in out


def test_type_without_value_keeps_a_safe_type(captured_records):
    try:
        raise _builtin_with_secret_text()
    except ValueError:
        logger.opt(exception=(KeyError, None, None)).error("handled failure")
    exception = captured_records[-1]["exception"]
    assert exception.type is KeyError
    assert exception.value is None


class _ChangingWhenPickled:
    """Pickles on the first try and raises on every later one."""

    def __init__(self):
        self.calls = 0

    def __reduce_ex__(self, protocol):
        self.calls += 1
        if self.calls > 1:
            raise TypeError("not this time")
        return (str, ("benign",))


def test_extra_value_whose_pickling_can_change_is_replaced(enqueue_stderr):
    """A trial pickle of an ``extra`` value says nothing about the next one."""
    value = _ChangingWhenPickled()
    try:
        raise _builtin_with_secret_text()
    except ValueError:
        logger.bind(value=value).error("handled failure")
    out = enqueue_stderr()
    assert "handled failure" in out
    assert "Logging error in Loguru Handler" not in out
    assert _ENQUEUE_SECRET not in out
    # Its pickling code never ran.
    assert value.calls == 0


@pytest.mark.parametrize(
    "value, plain",
    [
        ({"a": [1, 2.0, None, b"x", (True, 3j)], "b": {1}}, True),
        (bytearray(b"x"), True),
        (frozenset({"x"}), True),
        (_ChangingWhenPickled(), False),
        (["fine", _ChangingWhenPickled()], False),
        ({_EmptyLookingSecret(): 1}, False),
        (_FalsyText("x"), False),
    ],
    ids=[
        "nested",
        "bytearray",
        "frozenset",
        "object",
        "in_list",
        "key",
        "str_subclass",
    ],
)
def test_plain_data_is_exact_builtin_values_only(value, plain):
    from local_deep_research.security.log_sanitizer import _is_plain_data

    assert _is_plain_data(value) is plain


def test_plain_data_check_is_linear_on_shared_and_cyclic_containers():
    from local_deep_research.security.log_sanitizer import _is_plain_data

    shared = ["leaf"]
    for _ in range(20):
        shared = [shared, shared]
    assert _is_plain_data(shared) is True
    cycle = []
    cycle.append(cycle)
    assert _is_plain_data(cycle) is False


class _RewritingMembers(list):
    """Rewrites its member's text when an ``enqueue=True`` sink pickles it."""

    def __reduce_ex__(self, protocol):
        for member in self:
            member.args = (f"rewritten: api_key=sk-live-{_ENQUEUE_SECRET}",)
        return (list, (list(self),))


def _group_with_extra_member_in_args():
    members = [ValueError("a")]
    group = ExceptionGroup("plain", members)
    # The list the group was built from is the caller's; pickling the group
    # pickles it, not ``exceptions``.
    members.append(_ChangingWhenPickled())
    return group


def test_group_member_container_cannot_rewrite_members_for_later_sinks(
    enqueue_stderr,
):
    """The group is pickled through ``args``, which hold the caller's list."""
    import io

    later = io.StringIO()
    sink = logger.add(later, format="{message}", diagnose=False)
    try:
        try:
            raise ExceptionGroup(
                "benign", _RewritingMembers([ValueError("benign")])
            )
        except ExceptionGroup:
            logger.exception("handled failure")
        out = enqueue_stderr()
    finally:
        logger.remove(sink)
    rendered = later.getvalue()
    assert "handled failure" in out
    assert "ValueError: benign" in rendered
    assert _ENQUEUE_SECRET not in out + rendered


@pytest.mark.parametrize(
    "make, kept",
    [
        (lambda: ExceptionGroup("plain", [ValueError("a")]), True),
        (lambda: ExceptionGroup("plain", (ValueError("a"),)), True),
        (
            lambda: ExceptionGroup(
                "plain", _RewritingMembers([ValueError("a")])
            ),
            False,
        ),
        (_group_with_extra_member_in_args, False),
    ],
    ids=["list", "tuple", "list_subclass", "args_not_members"],
)
def test_group_is_kept_only_when_its_args_are_its_members(
    captured_records, make, kept
):
    group = make()
    _raise_and_log(group)
    assert (captured_records[-1]["exception"].value is group) is kept


# The exception a pickling hook below rewrites, set by the test that logs it.
_PICKLE_VICTIM = []
# Names of the hooks that ran.
_PICKLE_HOOKS_RUN = []


def _rewrite_victim(hook):
    """Give the victim a secret, as a pickling hook could between sinks."""
    _PICKLE_HOOKS_RUN.append(hook)
    if _PICKLE_VICTIM:
        _PICKLE_VICTIM[-1].args = (
            f"rewritten: api_key=sk-live-{_ENQUEUE_SECRET}",
        )


class _LookupHookMeta(type):
    """Runs when pickle looks up ``_LookupHookOuter.Error`` by name."""

    def __getattribute__(cls, name):
        if name == "Error":
            _rewrite_victim("enclosing_getattribute")
        return type.__getattribute__(cls, name)


class _LookupHookOuter(metaclass=_LookupHookMeta):
    class Error(Exception):
        """Its own metaclass is ``type``; its enclosing class's is not."""


class _GetHookMeta(type):
    """Runs when ``getattr(_GetHookOuter, "Error")`` finds the class."""

    def __get__(cls, instance, owner=None):
        _rewrite_victim("own_metaclass_get")
        return cls


class _GetHookOuter:
    class Error(Exception, metaclass=_GetHookMeta):
        """Nested in a plain class, with a ``__get__`` of its own."""


class _HashHookMeta(type):
    """Runs when the pickler looks the class up in its dispatch tables."""

    def __hash__(cls):
        _rewrite_victim("metaclass_hash")
        return type.__hash__(cls)


class _HashHookError(Exception, metaclass=_HashHookMeta):
    """Module level, so pickle finds it by name."""


class _ReduceHookMeta(type):
    """Runs when ``object.__reduce_ex__`` reads ``type(obj).__reduce__``."""

    @property
    def __reduce__(cls):
        _rewrite_victim("metaclass_reduce")
        return BaseException.__reduce__


class _ReduceHookError(Exception, metaclass=_ReduceHookMeta):
    """Module level, so pickle finds it by name."""


class _ReducerRegisteredError(Exception):
    """A plain class, with a ``copyreg`` reducer registered by the test."""


def _rewriting_reducer(error):
    _rewrite_victim("registered_reducer")
    return (_ReducerRegisteredError, error.args)


class _RewritingSpec:
    """A module ``__spec__`` whose ``_initializing`` the import reads."""

    @property
    def _initializing(self):
        _rewrite_victim("module_spec")
        return False


_SPEC_HOOK_MODULE = "_log_sink_spec_hook_module"


class _SpecHookError(Exception):
    """Pickled by reference to a module whose ``__spec__`` runs code."""


_SpecHookError.__module__ = _SPEC_HOOK_MODULE
_SpecHookError.__qualname__ = "_SpecHookError"


def _install_pickling_hooks(monkeypatch):
    """Register the hooks that live outside the classes themselves."""
    import copyreg
    import sys
    import types

    monkeypatch.setitem(
        copyreg.dispatch_table, _ReducerRegisteredError, _rewriting_reducer
    )
    module = types.ModuleType(_SPEC_HOOK_MODULE)
    module._SpecHookError = _SpecHookError
    module.__spec__ = _RewritingSpec()
    monkeypatch.setitem(sys.modules, _SPEC_HOOK_MODULE, module)
    stock = copyreg.dispatch_table[complex]

    def complex_reducer(value):
        _rewrite_victim("complex_reducer")
        return stock(value)

    monkeypatch.setitem(copyreg.dispatch_table, complex, complex_reducer)


# Read from the class dicts: ``_LookupHookOuter.Error`` and
# ``_GetHookOuter.Error`` would run the hooks.
_PICKLE_HOOKED_CLASSES = [
    vars(_LookupHookOuter)["Error"],
    vars(_GetHookOuter)["Error"],
    _HashHookError,
    _ReduceHookError,
    _ReducerRegisteredError,
    _SpecHookError,
]
_PICKLE_HOOKED_IDS = [
    "enclosing_getattribute",
    "own_metaclass_get",
    "metaclass_hash",
    "metaclass_reduce",
    "registered_reducer",
    "module_spec",
]


def _hooked_member_group(kind):
    def make():
        victim = ValueError("benign sibling")
        _PICKLE_VICTIM.append(victim)
        return ExceptionGroup("benign", [victim, kind("benign member")])

    return make


def _complex_argument_group():
    victim = ValueError("benign sibling")
    _PICKLE_VICTIM.append(victim)
    return ExceptionGroup("benign", [victim, ValueError("benign", 1j)])


def _hooked_value_under_another_type():
    """``opt(exception=(Type, value, tb))``: the value's class is not ``type``."""
    victim = ValueError("benign sibling")
    _PICKLE_VICTIM.append(victim)
    value = vars(_GetHookOuter)["Error"]("benign")
    value.__context__ = victim
    return (ValueError, value, None)


@pytest.fixture
def pickling_hooks(monkeypatch):
    _install_pickling_hooks(monkeypatch)
    _PICKLE_VICTIM.clear()
    _PICKLE_HOOKS_RUN.clear()
    yield _PICKLE_HOOKS_RUN
    _PICKLE_VICTIM.clear()
    _PICKLE_HOOKS_RUN.clear()


@pytest.mark.parametrize(
    "make",
    [_hooked_member_group(kind) for kind in _PICKLE_HOOKED_CLASSES]
    + [_complex_argument_group, _hooked_value_under_another_type],
    ids=[*_PICKLE_HOOKED_IDS, "complex_reducer", "value_not_record_type"],
)
def test_pickling_a_kept_exception_runs_no_code_before_later_sinks(
    enqueue_stderr, pickling_hooks, make
):
    """The ``enqueue=True`` sink pickles every kept node, not only ``type``.

    Pickling looks each kept node's class up by name, through the picklers'
    dispatch tables, its metaclass, the enclosing classes and its module's
    ``__spec__``; a hook there could rewrite a kept sibling's text after
    the first sink rendered it and before a later sink renders it.
    """
    import io

    later = io.StringIO()
    sink = logger.add(later, format="{message}", diagnose=False)
    try:
        exception = make()
        logger.opt(exception=exception).error("handled failure")
        out = enqueue_stderr()
    finally:
        logger.remove(sink)
    rendered = later.getvalue()
    assert "handled failure" in out
    assert "benign sibling" in rendered
    assert _ENQUEUE_SECRET not in out + rendered
    assert pickling_hooks == []


@pytest.mark.parametrize("kind", _PICKLE_HOOKED_CLASSES, ids=_PICKLE_HOOKED_IDS)
def test_kept_nodes_and_record_type_share_one_pickling_check(
    pickling_hooks, kind
):
    """A class is either kept everywhere in the record or nowhere."""
    from local_deep_research.security.log_sanitizer import (
        _exception_text_is_stable,
        _pickled_by_reference_runs_no_code,
        _record_type_is_safe,
    )

    assert _pickled_by_reference_runs_no_code(kind) is False
    assert _record_type_is_safe(kind) is False
    assert _exception_text_is_stable(kind("benign"), False) is False
    # Deciding never ran the hooks.
    assert pickling_hooks == []


@pytest.mark.parametrize(
    "kind",
    [ValueError, _ModuleLevelError, _PlainOuter.Error],
    ids=["builtin", "module_level", "nested"],
)
def test_plain_classes_pass_the_pickling_check(kind):
    from local_deep_research.security.log_sanitizer import (
        _exception_text_is_stable,
        _pickled_by_reference_runs_no_code,
        _record_type_is_safe,
    )

    assert _pickled_by_reference_runs_no_code(kind) is True
    assert _record_type_is_safe(kind) is True
    assert _exception_text_is_stable(kind("benign"), False) is True


def test_local_class_member_is_still_kept(captured_records):
    """Pickle refuses a class defined in a function before any lookup."""

    class LocalError(Exception):
        pass

    group = ExceptionGroup("benign", [LocalError("benign")])
    _raise_and_log(group)
    assert captured_records[-1]["exception"].value is group


@pytest.mark.parametrize(
    "make",
    [
        lambda: ValueError("benign", 1j),
        lambda: ExceptionGroup(
            "benign", [ValueError("a"), _ModuleLevelError("b")]
        ),
        lambda: ExceptionGroup("benign", [_PlainOuter.Error("c")]),
        *[_hooked_member_group(kind) for kind in _PICKLE_HOOKED_CLASSES],
        _complex_argument_group,
    ],
    ids=[
        "complex_argument",
        "plain_group",
        "nested_member",
        *_PICKLE_HOOKED_IDS,
        "hooked_complex",
    ],
)
def test_pickling_the_redacted_exception_calls_no_python_code(
    captured_records, pickling_hooks, make, request
):
    """The invariant itself: what the sinks get pickles in builtin code.

    ``copyreg``'s own reducer for ``complex`` is the one Python function
    the pickler may call, and only while it is the registered one.
    """
    import copyreg
    import pickle
    import sys

    if request.node.callspec.id == "complex_argument":
        # This case checks the stock reducer, not the test's hook.
        copyreg.dispatch_table[complex] = copyreg.pickle_complex
    _raise_and_log(make())
    value = captured_records[-1]["exception"].value
    calls = []

    def profile(frame, event, arg):
        if event == "call":
            calls.append(frame.f_code)

    sys.setprofile(profile)
    try:
        pickle.dumps(value)
    finally:
        sys.setprofile(None)
    assert [
        code for code in calls if code is not copyreg.pickle_complex.__code__
    ] == []
    assert pickling_hooks == []


class InvalidAuthorization(Exception):
    """Its name ends in a credential header name."""


_SPANNING_TOKEN = "SpanTok739" + "abcdef12"


def test_credential_spanning_type_name_and_text_is_redacted(rendered_sink):
    """Name and text are each clean; ``Name: text`` is a credential."""
    from local_deep_research.utilities.log_utils import _exception_context

    records = []
    logger.add(lambda message: records.append(message.record), diagnose=False)
    _raise_and_log(InvalidAuthorization(_SPANNING_TOKEN))
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "InvalidAuthorization" in rendered
    assert "SpanTok739" not in rendered
    assert "SpanTok739" not in _exception_context(records[-1])


def test_db_prefix_checks_the_joined_type_name_and_text():
    """The DB row joins name and text itself, whatever the patcher did."""
    from local_deep_research.utilities.log_utils import _exception_context

    exc = InvalidAuthorization(_SPANNING_TOKEN)
    prefix = _exception_context({"exception": (type(exc), exc, None)})
    assert prefix == "[InvalidAuthorization: [REDACTED]] "


def test_builtin_exception_names_never_join_into_a_credential():
    """A rebuilt node renders ``BuiltinName: checked text``.

    The rebuild redacts its text without the builtin name in front, which is
    exact only while no builtin exception name completes a credential
    pattern with the text after it.
    """
    import builtins

    from local_deep_research.security.log_sanitizer import (
        _redact_exception_text,
    )

    names = [
        name
        for name, value in vars(builtins).items()
        if isinstance(value, type) and issubclass(value, BaseException)
    ]
    assert "RuntimeError" in names
    for name in names:
        for text in (_SPANNING_TOKEN, "dXNlcjpwYXNzd29yZA==", "x"):
            line = f"{name}: {text}"
            assert _redact_exception_text(line) == line


def test_unsuppressed_context_beside_a_shared_cause_is_redacted(
    rendered_sink,
):
    """A cause rendered once already leaves the context to be rendered."""
    shared = KeyError("shared cause")
    first = ValueError("first member")
    second = ValueError("second member")
    first.__cause__ = shared
    second.__cause__ = shared
    second.__suppress_context__ = False
    second.__context__ = RuntimeError("password=" + "SharedCtxSecret739")
    _raise_and_log(ExceptionGroup("group", [first, second]))
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "second member" in rendered
    assert "password=[REDACTED]" in rendered
    assert "SharedCtxSecret739" not in rendered


# The sinks render the ``TypeName: text`` line and then each note on a line
# of its own, so a credential label at the end of one and its value at the
# start of the next are adjacent text even when each piece is clean.
_NOTE_SPAN_SECRET = "NoteSpan" + "Secret739abcdef"


def _label_in_message_value_in_note():
    exc = ValueError("config: password=")
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


def _header_label_in_message_value_in_note():
    exc = ConnectionError("upstream rejected header Authorization:")
    exc.add_note("abc123def456" + _NOTE_SPAN_SECRET)
    return exc


def _label_and_value_in_two_notes():
    exc = ValueError("benign")
    exc.add_note("db password =")
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


def _split_note_in_a_group_member():
    return ExceptionGroup(
        "group", [_label_in_message_value_in_note(), KeyError("other")]
    )


def _split_note_on_a_rebuilt_node():
    # The token makes the node rebuilt; the label after it is left as is.
    exc = ValueError("token=ghp_" + "a" * 36 + " password=")
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


def _split_note_on_a_local_class():
    class LocalError(Exception):
        pass

    exc = LocalError("config: password=")
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


# A ``SyntaxError``'s header line shows ``msg``, not ``str()``: the
# `` (file, line N)`` suffix ``str()`` adds is not printed between the
# message and the notes (``TracebackException._format_syntax_error``).
def _split_note_on_a_located_syntax_error():
    exc = SyntaxError(
        "upstream rejected header Authorization:", ("cfg.py", 3, None, None)
    )
    exc.add_note("abc123def456" + _NOTE_SPAN_SECRET)
    return exc


def _split_note_on_a_syntax_error_with_source():
    exc = SyntaxError("config: password=", ("x.py", 1, 1, "a b\n", 1, 2))
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


def _split_note_on_a_syntax_error_with_filename_only():
    # Printed as ``msg (f.py)``: the suffix ends up between the label and
    # the value, which is still the label's value.
    exc = SyntaxError("config: password=", ("f.py", None, None, None))
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


class _MissingBearer(Exception):
    """Raised with no arguments, it renders as ``_MissingBearer`` alone."""


def _split_note_after_an_empty_message():
    exc = _MissingBearer()
    exc.add_note(_NOTE_SPAN_SECRET)
    return exc


@pytest.mark.parametrize(
    "make",
    [
        _label_in_message_value_in_note,
        _header_label_in_message_value_in_note,
        _label_and_value_in_two_notes,
        _split_note_in_a_group_member,
        _split_note_on_a_rebuilt_node,
        _split_note_on_a_local_class,
        _split_note_on_a_located_syntax_error,
        _split_note_on_a_syntax_error_with_source,
        _split_note_on_a_syntax_error_with_filename_only,
    ],
    ids=[
        "message_note",
        "header_note",
        "note_note",
        "group_member",
        "rebuilt_node",
        "local_class",
        "syntax_error_located",
        "syntax_error_source",
        "syntax_error_filename",
    ],
)
def test_credential_spanning_message_and_notes_is_redacted(rendered_sink, make):
    """Each piece is clean; the rendered block is a credential."""
    _raise_and_log(make())
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "[REDACTED]" in rendered
    assert "NoteSpanSecret739" not in rendered


def test_credential_spanning_bare_type_name_and_notes_is_redacted(
    rendered_sink,
):
    """An empty message renders ``TypeName`` alone, then the notes."""
    _raise_and_log(_split_note_after_an_empty_message())
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "_MissingBearer\n<exception notes omitted" in rendered
    assert "NoteSpanSecret739" not in rendered


def test_clean_notes_are_kept_beside_their_exception(captured_records):
    exc = ValueError("benign failure")
    exc.add_note("useful diagnostic note")
    _raise_and_log(exc)
    assert captured_records[-1]["exception"].value is exc


def test_clean_syntax_error_with_notes_is_kept(captured_records):
    exc = SyntaxError("invalid syntax", ("cfg.py", 3, 1, "a b\n", 3, 2))
    exc.add_note("useful diagnostic note")
    _raise_and_log(exc)
    assert captured_records[-1]["exception"].value is exc


def _header_cases():
    for msg in ("bad", "", None, 0, b"x"):
        for details in (
            ("f.py", 3, None, None),
            ("f.py", None, None, None),
            (b"f.py", None, None, None),
            (None, 2, None, None),
            (None, None, None, None),
            ("f.py", 3, 1, "a b\n", 3, 2),
        ):
            yield SyntaxError(msg, details)
            yield IndentationError(msg, details)
        yield SyntaxError(msg)
    yield ValueError()
    yield ValueError("")
    yield KeyError("key")
    yield OSError(2, "missing", "f.py")
    yield ExceptionGroup("group", [ValueError()])


def test_checked_header_line_is_the_one_the_traceback_prints():
    """The checked header is ``format_exception_only``'s header line."""
    import traceback

    from local_deep_research.security.log_sanitizer import (
        _rendered_header_tails,
    )

    for exc in _header_cases():
        lines = traceback.format_exception_only(exc)
        printed = next(line for line in lines if not line.startswith(" "))
        tail = _rendered_header_tails(exc, str(exc))[0]
        assert printed == f"{type(exc).__qualname__}{tail}\n", exc


def test_str_lineno_joined_with_source_line_is_redacted(rendered_sink):
    """``line <lineno>`` and the source line below it are adjacent text.

    A ``str`` line number ending in a label and source text holding its
    value are each clean alone; the traceback prints them as one block.
    """
    exc = SyntaxError(
        "m", ("f.py", "password=", None, "hunter2" + "LinenoJoin739")
    )
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "LinenoJoin739" not in rendered


@pytest.mark.parametrize(
    "details",
    [
        ("f.py", 1, 1, b"src\n"),
        ("f.py", 1, "1", "src\n"),
        ("f.py", "1", None, "src\n"),
        ("f.py", True, None, "src\n"),
        ("f.py", 1, 1, "src\n", 1, "2"),
        ("f.py", 1, 1, "src\n", 1.0, 2),
        (b"f.py", 1, None, None),
    ],
    ids=[
        "bytes_text",
        "str_offset",
        "str_lineno",
        "bool_lineno",
        "str_end_offset",
        "float_end_lineno",
        "bytes_filename",
    ],
)
def test_syntax_error_with_non_canonical_fields_is_rebuilt(
    captured_records, details
):
    """Only the field types the interpreter fills a ``SyntaxError`` with keep it.

    On Python 3.12 a ``bytes`` text or a ``str`` offset makes the traceback
    module raise inside loguru's formatter; loguru's error handler then
    prints that error's context -- the exception being handled -- to stderr
    unredacted. A rebuilt node carries no such field.
    """
    exc = SyntaxError("bad", details)
    try:
        raise ValueError("Authorization: Bearer " + "abc123def456ghi789XYZ0")
    except ValueError:
        logger.opt(exception=exc).error("syntax failure")
    value = captured_records[-1]["exception"].value
    assert value is not exc
    assert type(value) is SyntaxError
    for name in ("filename", "lineno", "offset", "text"):
        assert getattr(value, name) is None, name


def test_syntax_error_with_canonical_fields_is_kept(captured_records):
    exc = SyntaxError("bad", ("f.py", 1, 1, "src\n", 1, 2))
    _raise_and_log(exc)
    assert captured_records[-1]["exception"].value is exc


_OFFSET_SECRET = "abc123def456ghi789XYZ0" + "OffsetSecret739"

# ``int`` fields of the interpreter's type but outside its ranges. The
# traceback module draws the caret line with ``'^' * (end - start)``: on
# Python 3.12 (any source line) and 3.14 (an empty one) a huge offset makes
# it raise ``OverflowError`` or ``MemoryError`` inside loguru's formatter.
_OUT_OF_RANGE_SYNTAX_DETAILS = [
    ("f.py", 1, 1, "", 1, 10**20),
    ("f.py", 1, 1, "", 1, 2**62),
    ("f.py", 1, 1, "src\n", 1, 10**19),
    ("f.py", 1, 1, "src\n", 1, -(10**19)),
    ("f.py", 1, 10**19, "src\n", 1, None),
    ("f.py", 1, 1, "x", 2, 2**64),
    ("f.py", 1, 1, "", 1, 10**9),
]
_OUT_OF_RANGE_SYNTAX_IDS = [
    "empty_text_end_offset_1e20",
    "empty_text_end_offset_2e62",
    "end_offset_1e19",
    "end_offset_minus_1e19",
    "offset_1e19",
    "other_line_end_offset_2e64",
    "empty_text_end_offset_1e9",
]


@pytest.mark.parametrize(
    "details", _OUT_OF_RANGE_SYNTAX_DETAILS, ids=_OUT_OF_RANGE_SYNTAX_IDS
)
def test_syntax_error_with_out_of_range_fields_never_dumps_the_handled_exception(
    enqueue_stderr, details
):
    """A crash rendering the caret line prints the exception being handled.

    Loguru's error handler prints the formatting error's traceback, whose
    context is the ``ValueError`` being handled here, unredacted to stderr.
    """
    exc = SyntaxError("bad", details)
    try:
        raise ValueError("Authorization: Bearer " + _OFFSET_SECRET)
    except ValueError:
        logger.opt(exception=exc).error("syntax failure")
    out = enqueue_stderr()
    assert "syntax failure" in out
    assert "Logging error in Loguru Handler" not in out
    assert "OffsetSecret739" not in out
    assert len(out) < 100_000


@pytest.mark.parametrize(
    "details", _OUT_OF_RANGE_SYNTAX_DETAILS, ids=_OUT_OF_RANGE_SYNTAX_IDS
)
def test_syntax_error_with_out_of_range_fields_is_rebuilt(
    captured_records, details
):
    """Rebuilt from the range check, before any caret line is built."""
    import time

    exc = SyntaxError("bad", details)
    started = time.monotonic()
    _raise_and_log(exc)
    assert time.monotonic() - started < 5
    value = captured_records[-1]["exception"].value
    kept = value is exc
    assert not kept
    assert type(value) is SyntaxError
    for name in ("filename", "lineno", "offset", "text", "end_offset"):
        assert getattr(value, name) is None, name


# The limit for ``offset`` and ``end_offset`` when ``text`` is a ``str``:
# its UTF-8 length plus one plus the later-line slack (2**16).
_SPAN = 2**16


@pytest.mark.parametrize(
    "details, kept",
    [
        (("f.py", 1, 5, "src\n", 1, 5), True),
        (("f.py", 1, 5 + _SPAN, "src\n", 1, None), True),
        (("f.py", 1, 6 + _SPAN, "src\n", 1, None), False),
        (("f.py", 1, 1, "src\n", 2, 5 + _SPAN), True),
        (("f.py", 1, 1, "src\n", 2, 6 + _SPAN), False),
        # UTF-8 bytes, not characters: "\u00e9\u00e9\u00e9\n" is 7 bytes.
        (("f.py", 1, 1, "\u00e9\u00e9\u00e9\n", 1, 8 + _SPAN), True),
        (("f.py", 1, 1, "\u00e9\u00e9\u00e9\n", 1, 9 + _SPAN), False),
        (("f.py", 1, -1, "src\n", 1, -1), True),
        (("f.py", 1, -2, "src\n", 1, None), False),
        (("f.py", 1, 1, "src\n", 1, -2), False),
        (("f.py", 1, 1, "", 1, 1), True),
        (("f.py", 1, 1, "", 1, 1 + _SPAN), True),
        (("f.py", 1, 1, "", 1, 2 + _SPAN), False),
        (("f.py", 0, None, "src\n"), True),
        (("f.py", -1, None, "src\n"), False),
        (("f.py", 2**31 - 1, None, "src\n", 2**31 - 1, None), True),
        (("f.py", 2**31, None, "src\n"), False),
        (("f.py", 1, None, "src\n", 2**31, None), False),
        (("f.py", 1, 2**31 - 1, None, 1, 2**31 - 1), True),
        (("f.py", 1, 2**31, None, 1, None), False),
    ],
)
def test_syntax_error_int_field_range_bounds(captured_records, details, kept):
    """Offsets within -1..UTF-8 len(text)+1+2**16, lines within a C ``int``.

    A kept node at the upper limit is still rendered within bounds: Python
    3.12 draws its ``end_offset - offset`` carets (none for an ``offset``
    below 1), at most the UTF-8 length of ``text`` plus ``2**16``
    (``end_offset`` at the limit, ``offset`` 1).
    """
    from local_deep_research.security.log_sanitizer import (
        _SYNTAX_ERROR_SPAN_END_SLACK,
    )

    assert _SYNTAX_ERROR_SPAN_END_SLACK == _SPAN
    exc = SyntaxError("bad", details)
    _raise_and_log(exc)
    assert (captured_records[-1]["exception"].value is exc) is kept


_REAL_SYNTAX_ERROR_SOURCES = {
    "genexp_multiline": "f(x for x in\n y" + "y" * 80 + ", 1)",
    "assign_multiline": "x = (a\n+b" + "c" * 80 + ") = 3",
    "del_multiline": "del f(a,\n" + "b" * 80 + ")",
    "indent": "  x = 1\n",
    "duplicate_argument": "def f(a, a):\n pass",
    "late_future_import": "from os import path\nfrom __future__ import annotations",
    "unterminated_nonascii": "x = '" + "\u00e9" * 10 + " $",
    "unterminated_string": "'abc",
    "invalid_syntax": "a b c",
    # Tokenizer errors report UTF-8 byte offsets: 21/22 and 34/35 against
    # 19 and 17 characters of text.
    "tokenizer_byte_offsets": "\u00e9" * 5 + " = 1; y = 0777\n",
    "tokenizer_byte_offsets_cjk": "\u5909\u6570" * 5 + " = 0777\n",
    # Compiler and symtable errors: byte offsets, and on import an
    # end_offset measured on the span's last line, not on ``text``.
    "return_span_on_later_line": "return f(\n    1) + some_long_function_name("
    + "a, " * 15
    + "b)\n",
    "yield_span_on_later_line": "yield (1,\n  " + "x" * 70 + ")\n",
    "await_span_on_later_line": "await f(1,\n  " + "y" * 70 + ")\n",
    "await_nonascii": "x = [\n  " + "\u00e9" * 30 + ", await y]\n",
    "return_nonascii": 'x = "' + "\u00e9" * 30 + '"; return 1\n',
    "nonlocal_nonascii": "def g():\n  \u00e9 = 1\n  def h():\n    print(\u00e9); nonlocal \u00e9\n",
    "return_emoji": "x = '\U0001f600\U0001f600\U0001f600'; return\n",
    "return_span_on_20000_char_line": "return f(\n" + "a" * 20_000 + ")\n",
}


@pytest.mark.parametrize("how", ["compile", "import"])
@pytest.mark.parametrize(
    "case",
    list(_REAL_SYNTAX_ERROR_SOURCES),
    ids=list(_REAL_SYNTAX_ERROR_SOURCES),
)
def test_interpreter_syntax_errors_are_kept(
    captured_records, tmp_path, monkeypatch, case, how
):
    """Real interpreter errors (multi-line spans, ``-1`` offsets, no source
    line, UTF-8 byte offsets, and spans ending on a later line).

    The range check and the trial rendering must not drop the location
    details of an error the interpreter raised. Compiler and symtable errors
    only carry their source line (and so their offsets are only checked)
    when the file exists, so each source is also imported from a file.
    """
    import importlib
    import sys

    source = _REAL_SYNTAX_ERROR_SOURCES[case]
    error = None
    try:
        if how == "compile":
            compile(source, "f.py", "exec")
        else:
            name = "ldr_syntax_probe_" + case
            (tmp_path / (name + ".py")).write_text(
                source, encoding="utf-8", newline=""
            )
            monkeypatch.syspath_prepend(str(tmp_path))
            importlib.invalidate_caches()
            try:
                importlib.import_module(name)
            finally:
                sys.modules.pop(name, None)
    except SyntaxError as exc:
        error = exc
    if error is None:
        pytest.fail("source compiled")
    if how == "import":
        assert error.text is not None
    _raise_and_log(error)
    assert captured_records[-1]["exception"].value is error


def test_syntax_error_whose_trial_rendering_fails_is_rebuilt(
    captured_records, monkeypatch
):
    """The keep decision renders the node as the sinks will, last."""
    import traceback

    real = traceback.format_exception_only

    def failing(exc, *args, **kwargs):
        # Only for a node with location details: the rebuilt node the sink
        # formats has none.
        if isinstance(exc, SyntaxError) and exc.lineno is not None:
            raise OverflowError("cannot fit 'int' into an index-sized integer")
        return real(exc, *args, **kwargs)

    exc = SyntaxError("bad", ("f.py", 1, 1, "src\n", 1, 2))
    monkeypatch.setattr(traceback, "format_exception_only", failing)
    _raise_and_log(exc)
    monkeypatch.setattr(traceback, "format_exception_only", real)
    value = captured_records[-1]["exception"].value
    assert value is not exc
    assert value.lineno is None and value.text is None


def test_syntax_error_whose_trial_rendering_runs_long_is_rebuilt(
    captured_records, monkeypatch
):
    import traceback

    from local_deep_research.security.log_sanitizer import (
        _SYNTAX_ERROR_RENDER_SLACK,
    )

    real = traceback.format_exception_only

    def long(exc, *args, **kwargs):
        lines = real(exc, *args, **kwargs)
        if isinstance(exc, SyntaxError) and exc.lineno is not None:
            lines.append("^" * (_SYNTAX_ERROR_RENDER_SLACK + 100))
        return lines

    exc = SyntaxError("bad", ("f.py", 1, 1, "src\n", 1, 2))
    monkeypatch.setattr(traceback, "format_exception_only", long)
    _raise_and_log(exc)
    monkeypatch.setattr(traceback, "format_exception_only", real)
    assert captured_records[-1]["exception"].value is not exc


def test_checked_location_block_is_the_one_the_traceback_prints():
    """The checked ``File``/source block is ``format_exception_only``'s."""
    import traceback

    from local_deep_research.security.log_sanitizer import (
        _syntax_error_location_block,
    )

    for details in (
        ("f.py", 3, None, None),
        ("", 3, None, "  a b\n\n"),
        (None, 2, None, "\f x"),
        ("f.py", None, None, "a b\n"),
        (None, None, None, None),
        ("f.py", 3, None, "a b\n", 3, None),
    ):
        exc = SyntaxError("bad", details)
        lines = traceback.format_exception_only(exc)
        printed = "".join(
            line for line in lines if not line.startswith("SyntaxError")
        )
        assert _syntax_error_location_block(exc) == printed.rstrip("\n"), (
            details
        )


class _ArglessApplicationError(Exception):
    """Module level and raised with no arguments: its ``str()`` is empty."""


def test_empty_text_application_error_name_is_not_repeated_in_db_row(
    captured_records,
):
    from local_deep_research.utilities.log_utils import _exception_context

    exc = _ArglessApplicationError()
    exc.__cause__ = ValueError("token=" + "abcdef123456EmptyRow739")
    _raise_and_log(exc)
    record = captured_records[-1]
    assert record["exception"].value is not exc
    assert _exception_context(record) == "[_ArglessApplicationError] "


def test_enqueue_sink_pickles_extra_with_the_queue_pickler(enqueue_stderr):
    """``SimpleQueue.put`` uses ``ForkingPickler``, not ``pickle.dumps``.

    Once ``multiprocessing.sharedctypes`` has created a value of a ``ctypes``
    type, ``ForkingPickler`` has a reducer for that type which raises
    outside process spawning, while ``pickle.dumps`` still succeeds.
    """
    import ctypes
    import pickle
    from multiprocessing import sharedctypes

    sharedctypes.RawValue(ctypes.c_ushort, 1)
    value = ctypes.c_ushort(3)
    pickle.dumps(value)
    try:
        raise _builtin_with_secret_text()
    except ValueError:
        logger.bind(value=value).info("bound ctypes value")
    out = enqueue_stderr()
    assert "bound ctypes value" in out
    assert "Logging error in Loguru Handler" not in out
    assert "EnqueueDumpSecret739" not in out


# The redactor runs application code itself while it reads the graph (a
# parent's, a group subclass's or a note object's ``__str__``). That code
# can change a node read before it -- a cause, context or member whose text
# was clean -- or the record's exception class after its pickling was
# checked. Every decision must therefore come after all of that code.
_REWRITE_SECRET = "api_key=sk-live-" + "RewriteAfterCheck739"


class _PlainValueError(ValueError):
    """Module level and plain, so it is kept when its text is clean."""


class _SecretStrValueError(ValueError):
    def __str__(self):
        return _REWRITE_SECRET


def _parent_rewrites(rewrite):
    victim = _PlainValueError("benign cause")

    class RewritingParent(Exception):
        def __str__(self):
            rewrite(victim)
            return "benign parent"

    exc = RewritingParent()
    exc.__cause__ = victim
    return exc


def _rewrite_args(victim):
    victim.args = (_REWRITE_SECRET,)


def _swap_class(victim):
    victim.__class__ = _SecretStrValueError


def _relink_cause(victim):
    victim.__cause__ = ValueError(_REWRITE_SECRET)


def _add_note(victim):
    victim.add_note(_REWRITE_SECRET)


def _group_rewrites_member():
    victim = _PlainValueError("benign member")

    class RewritingGroup(ExceptionGroup):
        def __str__(self):
            _rewrite_args(victim)
            return "benign group"

    return RewritingGroup("benign", [victim])


class _RewritingNote:
    def __init__(self, victim):
        self.victim = victim

    def __str__(self):
        _rewrite_args(self.victim)
        return "benign note"


def _note_rewrites_context():
    victim = _PlainValueError("benign context")
    exc = RuntimeError("benign top")
    exc.__context__ = victim
    exc.__notes__ = [_RewritingNote(victim)]
    return exc


_REWRITE_CASES = {
    "parent_str_rewrites_cause_args": lambda: _parent_rewrites(_rewrite_args),
    "parent_str_swaps_cause_class": lambda: _parent_rewrites(_swap_class),
    "parent_str_relinks_cause": lambda: _parent_rewrites(_relink_cause),
    "parent_str_adds_cause_note": lambda: _parent_rewrites(_add_note),
    "group_str_rewrites_member": _group_rewrites_member,
    "note_str_rewrites_context": _note_rewrites_context,
}


@pytest.mark.parametrize(
    "make", list(_REWRITE_CASES.values()), ids=list(_REWRITE_CASES)
)
def test_node_changed_after_its_check_is_rendered_as_checked(
    rendered_sink, make
):
    """A node changed by code the redactor ran later is not kept as is.

    It is rebuilt from the text read and checked for it, so the sinks
    render that text, not what the application code made of the node.
    """
    import traceback

    records = []
    logger.add(lambda m: records.append(m.record), diagnose=False)
    _raise_and_log(make())
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "RewriteAfterCheck739" not in rendered
    assert "benign" in rendered
    value = records[-1]["exception"].value
    assert "RewriteAfterCheck739" not in "".join(
        traceback.format_exception(value)
    )


class _RenamedLaterError(Exception):
    """Module level: pickled by name until its ``__qualname__`` changes."""


def test_record_type_renamed_after_its_check_is_replaced(enqueue_stderr):
    """The record's type is checked after the code that could rename it.

    A cause's ``__str__`` renames the record's exception class, so pickle
    can no longer find it by name: had the type been checked before that
    code ran, the ``enqueue=True`` sink's pickle would fail and loguru's
    error handler would print the live chain, unredacted, to stderr.
    """
    calls = []

    class RenamingCause(Exception):
        def __str__(self):
            calls.append(None)
            if len(calls) == 1:
                _RenamedLaterError.__qualname__ = "_NoSuchName"
                return "benign cause"
            return _REWRITE_SECRET

    exc = _RenamedLaterError("benign top")
    exc.__cause__ = RenamingCause()
    try:
        _raise_and_log(exc)
        out = enqueue_stderr()
    finally:
        _RenamedLaterError.__qualname__ = "_RenamedLaterError"
    assert "unprintable failure" in out
    assert "Logging error in Loguru Handler" not in out
    assert "RewriteAfterCheck739" not in out


class _StrRunningKey:
    """An ``extra`` key whose ``str()`` is application code."""

    def __str__(self):
        return "benign key"


def test_application_code_runs_only_before_any_decision():
    """The invariant: no application code runs once a decision is made.

    Application code here is anything defined in this file. It runs while
    the patcher reads the record (an ``extra`` key's ``str()``, the
    exception graph's ``__str__`` and note objects) and never afterwards:
    not while the patcher decides what to keep, nor while the result is
    rendered and pickled the way the sinks do.
    """
    import pickle
    import sys
    import traceback
    from collections import namedtuple

    from local_deep_research.security import log_sanitizer

    decisions = {
        function.__code__
        for function in (
            log_sanitizer._build_redacted_exception,
            log_sanitizer._record_type_is_safe,
            log_sanitizer._exception_text_is_stable,
            log_sanitizer._pickled_by_reference_runs_no_code,
            log_sanitizer._type_header_changed,
            log_sanitizer._picklable_extra,
        )
    }
    exc = _note_rewrites_context()
    exc.__cause__ = _parent_rewrites(_rewrite_args)
    group = _group_rewrites_member()
    group.__context__ = exc
    record_exception = namedtuple("RecordException", "type value traceback")
    record = {
        "message": "benign message",
        "extra": {_StrRunningKey(): "benign value"},
        "exception": record_exception(type(group), group, None),
    }
    events = []

    def profile(frame, event, arg):
        if event != "call":
            return
        if frame.f_code.co_filename == __file__:
            events.append("application")
        elif frame.f_code in decisions:
            events.append("decision")

    sys.setprofile(profile)
    try:
        log_sanitizer.redact_log_record(record)
        value = record["exception"].value
        rendered = "".join(traceback.format_exception(value))
        pickle.dumps(value)
    finally:
        sys.setprofile(None)
    assert "RewriteAfterCheck739" not in rendered
    first_decision = events.index("decision")
    assert "application" in events[:first_decision]
    assert "application" not in events[first_decision:]


class SwappedInAuthorization(ValueError):
    """Plain and stable, and its name ends in a credential header name."""


_SWAP_TOKEN = "abc123def456ghi789jkl012mno345"


def test_cause_swapped_to_a_stable_credential_named_class_is_rebuilt(
    rendered_sink,
):
    """A node whose class changed after it was read is not kept.

    A parent's ``__str__`` swaps a clean cause to a plain, stable class whose
    name joined with the cause's text forms a credential
    (``SwappedInAuthorization: <token>``). The header check is made with the
    class the text was read from, so only the class identity check stops
    the swapped node reaching the sinks as is.
    """
    import traceback

    victim = _PlainValueError(_SWAP_TOKEN)

    class SwappingParent(Exception):
        def __str__(self):
            victim.__class__ = SwappedInAuthorization
            return "benign parent"

    exc = SwappingParent()
    exc.__cause__ = victim
    records = []
    logger.add(lambda m: records.append(m.record), diagnose=False)
    _raise_and_log(exc)
    rendered = rendered_sink.getvalue()
    assert "raise exc" in rendered
    assert "benign parent" in rendered
    assert f"SwappedInAuthorization: {_SWAP_TOKEN}" not in rendered
    value = records[-1]["exception"].value
    assert f"SwappedInAuthorization: {_SWAP_TOKEN}" not in "".join(
        traceback.format_exception(value)
    )


class _ClassPropertyName:
    """A ``name`` whose ``__class__`` property rewrites a kept node.

    ``isinstance(obj, str)`` falls back to ``obj.__class__`` and so runs it.
    """

    def __init__(self, victim):
        self.victim = victim

    @property
    def __class__(self):
        _rewrite_args(self.victim)
        return _ClassPropertyName


class _RewritingDict(dict):
    """An instance ``__dict__`` whose own methods rewrite a kept node."""

    def _rewrite(self):
        _rewrite_args(vars(self)["victim"])

    def __len__(self):
        self._rewrite()
        return dict.__len__(self)

    def __iter__(self):
        self._rewrite()
        return dict.__iter__(self)

    def items(self):
        self._rewrite()
        return dict.items(self)


def _rewriting_state(victim):
    state = _RewritingDict(note="benign")
    vars(state)["victim"] = victim
    return state


def _name_rewrites_cause(kind, field):
    victim = _PlainValueError("benign cause")
    exc = kind("benign")
    setattr(exc, field, _ClassPropertyName(victim))
    exc.__cause__ = victim
    return exc


def _name_rewrites_group_sibling():
    victim = _PlainValueError("benign member")
    member = NameError("benign")
    member.name = _ClassPropertyName(victim)
    return ExceptionGroup("benign", [victim, member])


def _state_rewrites_cause():
    victim = _PlainValueError("benign cause")
    middle = _PlainValueError("benign middle")
    middle.__dict__ = _rewriting_state(victim)
    middle.__cause__ = victim
    exc = RuntimeError("benign top")
    exc.__cause__ = middle
    return exc


def _state_rewrites_group_sibling():
    victim = _PlainValueError("benign member")
    member = _PlainValueError("benign member two")
    member.__dict__ = _rewriting_state(victim)
    return ExceptionGroup("benign", [victim, member])


_DECISION_HOOK_CASES = {
    "attribute_error_name_class_property": lambda: _name_rewrites_cause(
        AttributeError, "name"
    ),
    "import_error_name_from_class_property": lambda: _name_rewrites_cause(
        ImportError, "name_from"
    ),
    "name_error_name_rewrites_group_sibling": _name_rewrites_group_sibling,
    "dict_subclass_state_rewrites_cause": _state_rewrites_cause,
    "dict_subclass_state_rewrites_group_sibling": _state_rewrites_group_sibling,
}


@pytest.mark.parametrize(
    "make", list(_DECISION_HOOK_CASES.values()), ids=list(_DECISION_HOOK_CASES)
)
def test_decisions_run_no_hook_of_a_field_value_or_instance_dict(make):
    """Deciding what to keep runs no code of a field value or ``__dict__``.

    A non-``str`` ``name``/``name_from`` (whose ``__class__`` an
    ``isinstance`` check would run) or an instance ``__dict__`` that is a
    ``dict`` subclass (whose ``__len__``/``items`` iterating it would run)
    could otherwise rewrite a cause or group sibling already decided to be
    kept, and every sink would render the rewritten text. Such a node is
    rebuilt instead, and no application code runs once a decision is made.
    """
    import pickle
    import sys
    import traceback
    from collections import namedtuple

    from local_deep_research.security import log_sanitizer

    decisions = {
        function.__code__
        for function in (
            log_sanitizer._build_redacted_exception,
            log_sanitizer._record_type_is_safe,
            log_sanitizer._exception_text_is_stable,
            log_sanitizer._pickled_by_reference_runs_no_code,
            log_sanitizer._type_header_changed,
            log_sanitizer._picklable_extra,
        )
    }
    exc = make()
    record_exception = namedtuple("RecordException", "type value traceback")
    record = {
        "message": "benign message",
        "extra": {},
        "exception": record_exception(type(exc), exc, None),
    }
    events = []

    def profile(frame, event, arg):
        if event != "call":
            return
        if frame.f_code.co_filename == __file__:
            events.append("application")
        elif frame.f_code in decisions:
            events.append("decision")

    sys.setprofile(profile)
    try:
        log_sanitizer.redact_log_record(record)
        value = record["exception"].value
        rendered = "".join(traceback.format_exception(value))
        pickled = pickle.dumps(value)
        rendered_again = "".join(traceback.format_exception(value))
    finally:
        sys.setprofile(None)
    assert "benign" in rendered
    assert "RewriteAfterCheck739" not in rendered
    assert "RewriteAfterCheck739" not in rendered_again
    assert b"RewriteAfterCheck739" not in pickled
    first_decision = events.index("decision")
    assert "application" not in events[first_decision:]


def test_module_spec_dict_subclass_is_not_iterated(monkeypatch):
    """A module ``__spec__`` whose ``__dict__`` is a subclass is rejected.

    Pickle's lookup of a class goes through its module's ``__spec__``; its
    instance dict is checked as an exact ``dict``, so a subclass's
    ``__iter__``/``__contains__`` never runs during the check.
    """
    import sys
    import types
    from importlib.machinery import ModuleSpec

    from local_deep_research.security.log_sanitizer import (
        _pickled_by_reference_runs_no_code,
    )

    calls = []

    class SpyDict(dict):
        def __iter__(self):
            calls.append("__iter__")
            return dict.__iter__(self)

        def __contains__(self, key):
            calls.append("__contains__")
            return dict.__contains__(self, key)

    name = "_log_sink_spec_dict_subclass_module"
    module = types.ModuleType(name)
    spec = ModuleSpec(name, None)
    spec.__dict__ = SpyDict(vars(spec))
    module.__spec__ = spec
    kind = type("SpecDictError", (Exception,), {"__module__": name})
    module.SpecDictError = kind
    monkeypatch.setitem(sys.modules, name, module)
    assert _pickled_by_reference_runs_no_code(kind) is False
    assert calls == []


def test_pickling_a_class_never_calls_its_module_getattr(monkeypatch):
    """Pickle imports a class's module without reading ``__path__``.

    ``_module_import_runs_no_code`` does not look at a module-level
    (PEP 562) ``__getattr__``: CPython 3.12 to 3.14 import the module with
    an empty ``fromlist`` (``PyImport_Import``), so the import never reads
    ``__path__`` (or any other missing name) on it. This pins that.
    """
    import pickle
    import sys
    import types
    from multiprocessing.reduction import ForkingPickler

    from local_deep_research.security.log_sanitizer import (
        _pickled_by_reference_runs_no_code,
    )

    name = "_log_sink_module_getattr_module"
    module = types.ModuleType(name)
    module.__spec__ = None
    calls = []

    def module_getattr(attribute):
        calls.append(attribute)
        raise AttributeError(attribute)

    module.__getattr__ = module_getattr
    kind = type("ModuleGetattrError", (Exception,), {"__module__": name})
    module.ModuleGetattrError = kind
    monkeypatch.setitem(sys.modules, name, module)
    assert _pickled_by_reference_runs_no_code(kind) is True
    pickle.dumps(kind("benign"))
    ForkingPickler.dumps(kind("benign"))
    pickle.dumps(kind)
    assert calls == []


def _plain_module_error(monkeypatch, name):
    """An exception class defined at the top level of a plain module."""
    import sys
    import types

    module = types.ModuleType(name)
    module.__spec__ = None
    kind = type("PlainModuleError", (Exception,), {"__module__": name})
    module.PlainModuleError = kind
    monkeypatch.setitem(sys.modules, name, module)
    return kind


def _group_record(members):
    try:
        raise ExceptionGroup("group", members)
    except ExceptionGroup as group:
        return {
            "message": "m",
            "extra": {},
            "exception": (type(group), group, group.__traceback__),
        }


def test_sys_modules_is_scanned_once_per_record(monkeypatch):
    """A large exception group scans ``sys.modules`` once, not per member.

    Every kept member's pickling check reads ``sys.modules``, which holds
    thousands of entries in the web process; scanning its keys once per
    member made one record with a 150-member group cost tens of
    milliseconds on the logging thread.
    """
    import sys

    from local_deep_research.security import log_sanitizer
    from local_deep_research.security.log_sanitizer import redact_log_record

    kind = _plain_module_error(monkeypatch, "_log_sink_scan_once_module")
    scans = []
    plain_names = log_sanitizer._plain_names

    def counting(namespace):
        if namespace is sys.modules:
            scans.append(1)
        return plain_names(namespace)

    monkeypatch.setattr(log_sanitizer, "_plain_names", counting)
    record = _group_record([kind(f"member {i}") for i in range(150)])
    group = record["exception"][1]
    redact_log_record(record)
    # Every member is still checked and kept as the original object.
    assert record["exception"][1] is group
    assert len(scans) == 1
    # The once-per-record answer does not outlive the record.
    assert log_sanitizer._DECISION_MEMO.get() is None
    redact_log_record(_group_record([kind("again")]))
    assert len(scans) == 2


def test_non_str_sys_modules_key_added_while_reading_is_seen(monkeypatch):
    """The once-per-record scan is made after the graph is read.

    A ``__str__`` the read phase runs can add a key to ``sys.modules``; the
    pickling checks made afterwards must see it, so a member whose class
    would be kept is rebuilt instead, as when the key was there from the
    start.
    """
    import sys

    from local_deep_research.security.log_sanitizer import redact_log_record

    kind = _plain_module_error(monkeypatch, "_log_sink_scan_late_module")
    module = sys.modules["_log_sink_scan_late_module"]

    class AddsModuleKeyError(Exception):
        def __str__(self):
            sys.modules[_NON_STR_MODULE_KEY] = module
            return "benign"

    monkeypatch.delitem(sys.modules, _NON_STR_MODULE_KEY, raising=False)
    try:
        member = kind("benign member")
        record = _group_record([AddsModuleKeyError(), member])
        redact_log_record(record)
        assert _NON_STR_MODULE_KEY in sys.modules
        rendered = record["exception"][1]
        assert str(rendered.exceptions[1]) == "PlainModuleError: benign member"
        assert rendered.exceptions[1] is not member
    finally:
        sys.modules.pop(_NON_STR_MODULE_KEY, None)

    # Without the key, the same member is kept as the original object.
    member = kind("benign member")
    record = _group_record([kind("other"), member])
    redact_log_record(record)
    assert record["exception"][1].exceptions[1] is member


# A ``sys.modules`` key that is not a ``str``: one whose comparison could
# run code, so the pickling checks keep no application class while it is
# there.
_NON_STR_MODULE_KEY = ("_log_sink_non_str_module_key",)


def test_class_level_checks_run_once_per_class_per_record(monkeypatch):
    """A group of one class pays for that class's pickling check once.

    Whether pickling a class runs code depends on the class and global
    tables only, so a record answers it once per class, not once per
    member: a 150-member group cost about ten times what the patcher cost
    before these checks while it repeated them for every member.
    """
    from local_deep_research.security import log_sanitizer
    from local_deep_research.security.log_sanitizer import redact_log_record

    kind = _plain_module_error(monkeypatch, "_log_sink_class_once_module")
    lookups = []
    lookup = log_sanitizer._pickle_lookup_runs_no_code

    def counting(checked, module, qualname):
        lookups.append(checked)
        return lookup(checked, module, qualname)

    monkeypatch.setattr(log_sanitizer, "_pickle_lookup_runs_no_code", counting)
    record = _group_record([kind(f"member {i}") for i in range(150)])
    group = record["exception"][1]
    redact_log_record(record)
    # Every member is still checked and kept as the original object.
    assert record["exception"][1] is group
    # Once for the members' class and once for the group's.
    assert sorted(map(id, lookups)) == sorted({id(kind), id(ExceptionGroup)})
    assert log_sanitizer._DECISION_MEMO.get() is None
    # The answers do not outlive the record: the next one checks again.
    redact_log_record(_group_record([kind("again")]))
    assert len(lookups) == 4


def test_clean_kept_exception_text_is_scanned_twice(monkeypatch):
    """A kept exception's text costs two redaction passes, as on main.

    Main scanned a node's text twice (its message and its ``str()``). The
    header check (``TypeName: text``) and the read-phase check are the two
    passes here: the second pass of :func:`_redact_exception_text` is
    skipped when it would scan the same text again, and the type is
    joined with the text only in the forms a sink renders (``ValueError``,
    not ``builtins.ValueError``). Before, a kept ``ValueError`` holding a
    long message cost three times what main did.
    """
    from local_deep_research.security import log_sanitizer
    from local_deep_research.security.log_sanitizer import redact_log_record

    text = "the upstream service returned an unexpected payload " * 40
    scans = []
    redact = log_sanitizer._redact_bounded_log_text

    def counting(value):
        if text.strip() in value:
            scans.append(value)
        return redact(value)

    monkeypatch.setattr(log_sanitizer, "_redact_bounded_log_text", counting)
    try:
        raise ValueError(text)
    except ValueError as exc:
        value = exc
        record = {
            "message": "m",
            "extra": {},
            "exception": (ValueError, exc, exc.__traceback__),
        }
    redact_log_record(record)
    assert record["exception"][1] is value
    assert scans == [text, f"ValueError: {text}"]


@pytest.mark.parametrize(
    "text",
    [
        "plain text with no credential",
        "token=abc123def456ghi789",
        "api_\u200bkey=abc123def456ghi789",
        "tok\x1fen=abc123def456ghi789",
        "line one\nline two\tpassword=hunter2hunter2",
        "https://user:pa\u200b[email protected]/path",
        "\x85\u2028\u200b",
        "Bearer \u200bsk-abcdefghijklmnopqrstuvwxyz012345",
        "word " * 7000 + "password=hunter2hunter2",
        "x" * 40_000,
    ],
    ids=[
        "plain",
        "token",
        "invisible-api-key",
        "control-character-token",
        "multiline-password",
        "url-password",
        "invisible-only",
        "bearer-token",
        "long-password",
        "long-plain",
    ],
)
def test_skipped_second_pass_returns_what_both_passes_return(text):
    """Skipping the second pass on unchanged text never changes the result.

    Reference: both passes run unconditionally, as before the skip.
    """
    from local_deep_research.security import log_sanitizer

    def two_passes(value, strip):
        head, marker = log_sanitizer._truncate_for_log_redaction(value)
        first = log_sanitizer._redact_bounded_log_text(head)
        visible = strip(first)
        second = log_sanitizer._redact_bounded_log_text(visible)
        return first, visible, second, marker

    first, visible, second, marker = two_passes(
        text, lambda value: log_sanitizer._EXCEPTION_INVISIBLE_RE.sub("", value)
    )
    expected = (first if second == visible else second) + marker
    assert log_sanitizer._redact_exception_text(text) == expected

    _, _, second, marker = two_passes(text, log_sanitizer.strip_control_chars)
    assert log_sanitizer._redact_log_text(text) == second + marker


@pytest.mark.parametrize(
    "shape",
    [
        "Api-Key: {secret}",
        "postgresql://ldr:ab/cd{secret}@db:5432/ldr",
        '{{"password": "{secret}"}}',
    ],
    ids=["api-key-header", "slash-dsn-password", "quoted-key"],
)
def test_additional_credential_shapes_are_scrubbed_from_exception_values(shape):
    """The shapes added to record text also stay out of rendered tracebacks."""
    import traceback

    from local_deep_research.security.log_sanitizer import redact_log_record

    secret = "ZZZTESTCREDENTIAL123456"
    try:
        raise ValueError(shape.format(secret=secret))
    except ValueError as error:
        record = {
            "message": "logging failed",
            "extra": {},
            "exception": (ValueError, error, error.__traceback__),
        }

    redact_log_record(record)
    assert secret not in str(record["exception"][1])
    assert secret not in "".join(
        traceback.format_exception(*record["exception"])
    )


@pytest.mark.parametrize(
    "scheme", ["Basic", "Bearer", "Digest", "Negotiate", "ApiKey", "Token"]
)
@pytest.mark.parametrize(
    "placement", ["message", "extra", "exception", "note", "group-member"]
)
def test_unquoted_authorization_is_redacted_before_each_sink(
    enqueue_stderr, scheme, placement
):
    """The credential, not only its scheme word, is removed at real sinks."""
    from local_deep_research.utilities.log_utils import _exception_context

    secret = "BoundarySinkCanary0123456789"
    payload = "{'Authorization': " + scheme + " " + secret + "==}"
    records = []
    logger.add(lambda message: records.append(message.record), diagnose=False)
    error = None
    if placement == "exception":
        error = ValueError(payload)
    elif placement == "note":
        error = ValueError("upstream rejected request")
        error.add_note(payload)
    elif placement == "group-member":
        error = ExceptionGroup("upstream failures", [ValueError(payload)])
    bound = logger.bind(
        username="review-owner",
        research_id="review-research",
        detail=payload if placement == "extra" else "ordinary detail",
    )
    bound.opt(exception=error).error(
        payload if placement == "message" else "provider failed"
    )
    output = enqueue_stderr()
    assert "Logging error in Loguru Handler" not in output
    assert len(records) == 1
    record = records[0]
    assert record["extra"]["username"] == "review-owner"
    assert record["extra"]["research_id"] == "review-research"
    for shown in (
        output,
        record["message"],
        repr(record["extra"]),
        _exception_context(record),
    ):
        assert secret not in shown, shown


# --- #5768 follow-ups -------------------------------------------------------


@pytest.mark.parametrize(
    "name",
    [
        "OPENAI_API_KEY",
        "LDR_LLM_OPENAI_API_KEY",
        "llm.openai.api_key",
        "db_password",
        "search.engine.web.serper.api_key",
        "MILVUS_TOKEN",
        "app.client_secret",
    ],
)
def test_prefixed_env_and_dotted_names_redacted_in_message_text(name):
    """Message text uses the same exact-or-suffix name rule as extra keys."""
    message = f"loaded {name}=NameMarker987plain and more"
    assert redact_log_message(message) == (f"loaded {name}=[REDACTED] and more")


@pytest.mark.parametrize(
    "message",
    [
        "max tokens = 4096",
        "token count: 5",
        "max_tokens=4096 context_tokens=8192",
        "requires_api_key=True",
        "search.engine.web.brave.requires_api_key=False",
        "mytoken=keepme",
        "x-token=keepme",
        "api_keys=3",
        "OLDPWD=/home/user",
        "token_count=5",
    ],
)
def test_prefixed_name_rule_leaves_non_credentials_readable(message):
    assert redact_log_message(message) == message


def test_trailing_list_delimiter_is_not_consumed():
    """``max token = 4096,`` keeps its comma; the value is still redacted."""
    assert redact_log_message(
        "LLM config: max token = 4096, temperature = 0.7"
    ) == ("LLM config: max token = [REDACTED], temperature = 0.7")
    assert redact_log_message("token=abc; next") == "token=[REDACTED]; next"
    # Commas INSIDE a value are still part of it.
    assert redact_log_message("password=a,b,Marker987 x") == (
        "password=[REDACTED] x"
    )


@pytest.mark.parametrize(
    "message, secret",
    [
        ("Engine requires_api_key=True,api_key=Hide987a", "Hide987a"),
        ("requires_api_key=true;password=Hide987b", "Hide987b"),
        ("requires_api_key=False|token=Hide987c", "Hide987c"),
        # An unterminated quote after the flag runs to the end of the text.
        ('requires_api_key=" api_key=Hide987d', "Hide987d"),
        ("cfg requires_api_key='yes token=Hide987e", "Hide987e"),
        ('requires_api_key="true password=Hide987f"', "Hide987f"),
        ("requires_api_key=password = Hide987g", "Hide987g"),
        ("requires_api_key = password=Hide987h", "Hide987h"),
        ("requires_api_key=True\u200bapi_key=Hide987i", "Hide987i"),
        ("query=requires_api_key=1,api_key=Hide987j", "Hide987j"),
        ("requires_api_key =\napi_key = Hide987k", "Hide987k"),
        ("REQUIRES_API_KEY=\nDB_PASSWORD=Hide987l", "Hide987l"),
        (
            "search.engine.web.brave.requires_api_key=False,api_key=Hide987m",
            "Hide987m",
        ),
        (
            "search.engine.web.brave.requires_api_key=\n"
            "search.engine.web.brave.api_key=Hide987n",
            "Hide987n",
        ),
        ("requires_api_key=True,OPENAI_API_KEY=Hide987o", "Hide987o"),
    ],
)
def test_exempt_flag_does_not_hide_the_assignment_after_it(message, secret):
    """The readable ``requires_api_key`` flag must not consume its value.

    Had the match consumed the flag's value and handed it back unchanged,
    ``re.sub`` would resume after it and never scan the credential inside.
    """
    from local_deep_research.security.log_sanitizer import _redact_log_text

    for redact in (redact_log_message, _redact_log_text):
        redacted = redact(message)
        assert secret not in redacted, (redact.__name__, redacted)
        assert "requires_api" in redacted.lower()
    assert redact_log_message("Engine requires_api_key=True,api_key=x") == (
        "Engine requires_api_key=True,api_key=[REDACTED]"
    )


@pytest.mark.parametrize(
    "message, secret",
    [
        # A prefixed name's value runs on into an exact-name assignment.
        ("OPENAI_API_KEY=abc,password = Hide987p", "Hide987p"),
        ("OPENAI_API_KEY=api_key = Hide987q", "Hide987q"),
        ("OPENAI_password\x1c=x;secret= Hide987r", "Hide987r"),
        ('db_password="a token="b Hide987s"', "Hide987s"),
        # ... or a first-pass replacement changes what the second pass,
        # after stripping control characters, reads around it.
        ('s_apikey=s\npasswd=""apikey =""Hide987t', "Hide987t"),
        (
            "tok\u200ben='a OPENAI_API_KEY='bc' pass\u200bword='d Hide987u' x'",
            "Hide987u",
        ),
        # A prefixed value after a redacted line, glued to it once the
        # newline is stripped.
        ('password=abc\nOPENAI_API_KEY="def Hide987v"', "Hide987v"),
        ("client_secret='x'\nllm.api_key='y Hide987w'", "Hide987w"),
    ],
)
def test_prefixed_names_never_hide_an_exact_name_assignment(message, secret):
    from local_deep_research.security.log_sanitizer import _redact_log_text

    redacted = _redact_log_text(message)
    assert secret not in redacted, redacted
    # ``redact_log_message`` (exception text) strips nothing, so a name an
    # invisible character splits stays unrecognised there.
    if "\u200b" not in message:
        assert secret not in redact_log_message(message)


@pytest.mark.parametrize(
    "message, secret",
    [
        # A name on its own line, after a line the strip glues it to: a
        # name after ``-``, ``?``, ``&``, ``.`` or a non-ASCII letter is
        # not recognised in the stripped text.
        ("-----\nDB_PASSWORD=Line987a\n-----", "Line987a"),
        (
            "Loaded .env:\n# ---\nANTHROPIC_API_KEY=Line987b\nLDR_DEBUG=1",
            "Line987b",
        ),
        ("Are you sure?\nDB_PASSWORD=Line987c\nok", "Line987c"),
        (
            "Konfiguration f\u00fcr Gr\u00f6\u00dfe\nOPENAI_API_KEY=Line987d\n",
            "Line987d",
        ),
        ("-----\r\nDB_PASSWORD=Line987e\r\n", "Line987e"),
        ("x-\tSMTP_PASSWORD=Line987f\tdone", "Line987f"),
        ("a&\nllm.openai.api_key=Line987g\nend", "Line987g"),
        ("Loading...\tOPENAI_API_KEY=Line987h\tok", "Line987h"),
        (
            "name=caf\u00e9\nLDR_SEARCH_ENGINE_WEB_BRAVE_API_KEY=Line987i\nx=1",
            "Line987i",
        ),
        ("HOST=web-1\x1cDB_PASSWORD=Line987j\u2028PORT=5432", "Line987j"),
        ("requires_api_key- OPENAI_API_KEY=Line987k", "Line987k"),
        # ... or the previous line's value runs on into it.
        (
            "SERPER_API_KEY = abc123\nOPENAI_API_KEY = Line987l\nTIMEOUT = 30",
            "Line987l",
        ),
        ("db_password = abc\nredis_password = Line987m\nport = 1", "Line987m"),
        (
            "search.engine.web.brave.api_key = xyz\n"
            "llm.openai.api_key = Line987n\nllm.model = gpt",
            "Line987n",
        ),
        # An empty assignment does not take the next line's name as value.
        ("DB_PASSWORD=\nOPENAI_API_KEY = Line987o\n", "Line987o"),
        ("password=\napi_key = Line987p", "Line987p"),
    ],
)
def test_prefixed_name_on_its_own_line_is_redacted(message, secret):
    from local_deep_research.security.log_sanitizer import _redact_log_text

    redacted = _redact_log_text(message)
    assert secret not in redacted, redacted
    assert "[REDACTED]" in redacted
    # Exception text keeps its newlines; it is read line by line the same.
    assert secret not in redact_log_message(message)


# Whitespace (and invisible characters) between an empty assignment and
# the next line's name: the indentation of a pretty-printed dump, on the
# next line or after a blank one, with CRLF, a tab, NBSP, U+3000 or a
# zero-width character before the name.
_NEXT_LINE_GAPS = [
    "\n ",
    "\n  ",
    "\n    ",
    "\n\t  ",
    "\r\n  ",
    " \n  ",
    "\n\n  ",
    "\n\u00a0",
    "\n\u3000",
    "\n\u200b",
    "\n  \u200b",
    "\x1c  ",
    "\u2028 ",
]


@pytest.mark.parametrize("gap", _NEXT_LINE_GAPS)
@pytest.mark.parametrize(
    "empty", ["DB_PASSWORD=", "DB_PASSWORD =", "password=", "llm.api_key ="]
)
@pytest.mark.parametrize("next_name", ["OPENAI_API_KEY", "api_key"])
def test_empty_assignment_does_not_take_an_indented_next_name(
    gap, empty, next_name
):
    """The next line's name is not the empty assignment's value, however
    the next line is indented, and its value -- followed by a newline, as
    every line of a dump is -- is redacted."""
    from local_deep_research.security.log_sanitizer import _redact_log_text

    message = (
        "Settings:\n  "
        + empty
        + gap
        + next_name
        + " = Indent987\n  TIMEOUT = 30\n"
    )
    for redact in (redact_log_message, _redact_log_text):
        out = redact(message)
        assert "Indent987" not in out, (redact.__name__, out)
        assert "TIMEOUT = 30" in out, (redact.__name__, out)


@pytest.mark.parametrize(
    "message",
    [
        # An indented .env-style block.
        "Loaded config:\n  HOST = web-1\n  DB_PASSWORD =\n"
        "  OPENAI_API_KEY = Indent987a\n  TIMEOUT = 30\n",
        # A YAML-style block (two-space indent, an empty value first).
        "settings:\n  llm:\n    password=\n    api_key = Indent987b\n"
        "    model = gpt\n",
        # Tab-and-space indentation with CRLF line endings.
        "[db]\r\n\tDB_PASSWORD=\r\n\t  REDIS_PASSWORD = Indent987c\r\n",
    ],
)
def test_indented_config_block_is_redacted(message):
    from local_deep_research.security.log_sanitizer import _redact_log_text

    for redact in (redact_log_message, _redact_log_text):
        out = redact(message)
        assert "Indent987" not in out, (redact.__name__, out)


def _exact_names_only_log_text(value):
    """The log redaction with only the exact names (no prefix rule).

    One reference for the "never less" property: the assignment pattern
    that recognised only the exact names, as it was before the prefix rule.
    """
    import re

    from local_deep_research.security import log_sanitizer as ls

    exact_only = re.compile(
        r"(?i)(?<![\w.?&-])(" + ls._LOG_SENSITIVE_NAME_PATTERN + r")"
        r"(\s*=\s*)"
        r"(?:\"(?:\\.|[^\"\\])*(?:\"|\\?$)|'(?:\\.|[^'\\])*(?:'|\\?$)"
        r"|[^\s]+)"
    )

    def one_pass(text):
        shaped = ls.sanitize_error_message(text)
        queried = ls._SENSITIVE_QUERY_RE.sub(r"\1[REDACTED]", shaped)
        return exact_only.sub(r"\1\2[REDACTED]", queried)

    return one_pass(ls.strip_control_chars(one_pass(value)))


def _prefixed_names_log_text(value):
    """The log redaction with prefixed names, in one pattern.

    The other reference: exact and prefixed names (``OPENAI_API_KEY``,
    ``db_password``, ``llm.openai.api_key``) matched in start order on the
    original separators and again on the stripped text, the
    ``requires_api_key`` flag's assignment kept as it is.
    """
    import re

    from local_deep_research.security import log_sanitizer as ls

    names = re.compile(
        r"(?i)(?<![\w.?&-])((?:[a-z0-9]+[._])*(?:"
        + ls._LOG_SENSITIVE_NAME_PATTERN
        + r"))(\s*=\s*)"
        r"(?:\"(?:\\.|[^\"\\])*(?:\"|\\?$)|'(?:\\.|[^'\\])*(?:'|\\?$)"
        r"|\S+?(?=[,;]?(?:\s|$)))"
    )

    def replace(match):
        leaf = match.group(1).rsplit(".", 1)[-1].lower().replace("-", "_")
        if leaf == "requires_api_key":
            return match.group(0)
        return match.group(1) + match.group(2) + "[REDACTED]"

    def one_pass(text):
        shaped = ls.sanitize_error_message(text)
        queried = ls._SENSITIVE_QUERY_RE.sub(r"\1[REDACTED]", shaped)
        return names.sub(replace, queried)

    return one_pass(ls.strip_control_chars(one_pass(value)))


def _seeded_log_messages(rng, case):
    """One seeded message and the secrets in it (values of credentials)."""
    exempt = [
        "requires_api_key",
        "REQUIRES_API_KEY",
        "x.requires_api_key",
        "requires_api-key",
    ]
    names = [
        "token",
        "password",
        "api_key",
        "api-key",
        "pwd",
        "client_secret",
        "OPENAI_API_KEY",
        "DB_PASSWORD",
        "db_password",
        "llm.openai.api_key",
        "LDR_SEARCH_ENGINE_WEB_BRAVE_API_KEY",
        "s_apikey",
    ]
    secrets = []
    if case % 2:
        # Lines of ``NAME=value`` / ``NAME = value``, between lines that
        # end in characters the stripped text glues a name onto.
        prose = [
            "-----",
            "# ---",
            "Are you sure?",
            "a&",
            "ok.",
            "caf\u00e9",
            "",
        ]
        lines = []
        for _ in range(rng.randint(1, 6)):
            kind = rng.random()
            if kind < 0.6:
                secret = f"Sk{case}x{len(secrets)}Q"
                secrets.append(secret)
                quote = rng.choice(['"', "'", "", ""])
                lines.append(
                    rng.choice(names)
                    + rng.choice(["=", " = "])
                    + quote
                    + secret
                    + quote
                )
            elif kind < 0.75:
                lines.append(
                    rng.choice(exempt)
                    + rng.choice(["=", " = "])
                    + rng.choice(["True", "", "1"])
                )
            else:
                lines.append(rng.choice(prose))
        message = lines[0]
        for line in lines[1:]:
            message += (
                rng.choice(["\n", "\r\n", "\t", "\x1c", "\u2028", "\n\n"])
                + line
            )
        return message, secrets
    seps = ["=", " = ", "=\n", " =", "\n=\n"]
    glue = [",", ";", "|", " ", "\n", "\u200b", "\x1c", "", " and ", "'", '"']
    parts = []
    for _ in range(rng.randint(1, 4)):
        quote = rng.choice(['"', "'", ""])
        parts.append(
            rng.choice(exempt)
            + rng.choice(seps)
            + quote
            + rng.choice(["True", "1", "yes", "", "a b"])
            + rng.choice([quote, ""])
            + rng.choice(glue)
        )
        for _ in range(rng.randint(1, 2)):
            secret = f"Sk{case}x{len(secrets)}Q"
            secrets.append(secret)
            quote = rng.choice(['"', "'", ""])
            parts.append(
                rng.choice(names)
                + rng.choice(seps)
                + quote
                + rng.choice(["", "a b ", "x,"])
                + secret
                + rng.choice([quote, ""])
                + rng.choice(glue)
            )
    return "".join(parts), secrets


def test_log_text_never_redacts_less_than_either_reference():
    """Seeded differential: no secret either reference redacts survives.

    The references are the exact names alone and the prefixed names in one
    pattern (see :func:`_prefixed_names_log_text`), so a prefixed value
    the log redaction leaves while the one-pattern reading redacts it --
    a name on its own line after ``-`` or ``?``, a value the previous
    line's runs on into -- is a failure here too.
    """
    import random

    from local_deep_research.security.log_sanitizer import _redact_log_text

    rng = random.Random(5768)
    failures = []
    for case in range(2000):
        message, secrets = _seeded_log_messages(rng, case)
        redacted = _redact_log_text(message)
        present = [s for s in secrets if s in redacted]
        if not present:
            continue
        for reference in (_exact_names_only_log_text, _prefixed_names_log_text):
            expected = reference(message)
            leaked = [s for s in present if s not in expected]
            if leaked:
                failures.append((reference.__name__, message, redacted))
    assert not failures, failures[:3]


def test_log_text_pipelines_track_every_character():
    """Each pipeline's tracked run gives its untracked text, and each kept
    character's origin is the character it was in the original."""
    import random

    from local_deep_research.security import log_sanitizer as ls

    rng = random.Random(6879)
    for case in range(300):
        message, _ = _seeded_log_messages(rng, case)
        plain = ls._PipelineRun(message, tracked=False)
        tracked = ls._PipelineRun(
            message, tracked=True, unmatched=plain.unmatched
        )
        for index in ls._LOG_TEXT_PIPELINE_INDEXES:
            text, origins = tracked.result(index)
            assert text == plain.result(index)[0], (index, message)
            assert len(origins) == len(text)
            assert all(
                origin < 0 or message[origin] == character
                for character, origin in zip(text, origins)
            ), (index, message)


@pytest.mark.parametrize(
    "message",
    [
        "Engine requires_api_key=True,api_key=hunter2 next",
        "OPENAI_API_KEY=\"a b\"tail and db_password='x'y",
        'password=abc\nOPENAI_API_KEY="def ghi"',
        'b_passwd=""e',
        "requires_api_key=True",
        "-----\nDB_PASSWORD=hunter2abc\n-----",
        "SERPER_API_KEY = abc\nOPENAI_API_KEY = def\nTIMEOUT = 30",
        "DB_PASSWORD=\nOPENAI_API_KEY = x\n",
        'password="a"Secret9',
        'pwd\u200b=""Secret1',
    ],
)
def test_log_text_redaction_is_idempotent(message):
    from local_deep_research.security.log_sanitizer import _redact_log_text

    once = _redact_log_text(message)
    assert _redact_log_text(once) == once


@pytest.mark.parametrize(
    "message",
    [
        "A_" * 16_000 + "API_KEY=x",
        "a." * 16_000,
        "a_api_key_password_" * 1_700,
        "token=" + "a," * 16_000,
        "db_password = a, " * 1_900,
        "requires_api_key=" * 1_900,
        'requires_api_key="' * 1_800,
        "=a_a" * 8_000,
        "a_password=" * 2_900,
        'a_token="' * 3_600,
        "requires_api_key=password=" * 1_200,
        "OPENAI_API_KEY=a," * 1_900,
        # The pipelines disagree on every line, so the origins are tracked.
        "-\nDB_PASSWORD=x\n" * 2_000,
        "DB_PASSWORD=\nOPENAI_API_KEY = x\n" * 900,
        "requires_api_key=a,b,db_password\x1c=x;" * 700,
        'password="a"x ' * 2_300,
        'DB_PASSWORD=\nOPENAI_API_KEY = "x"t\n' * 800,
        'OPENAI_API_KEY=abc,DB_PASSWORD="a b c" ' * 800,
    ],
    ids=[
        "upper-snake",
        "dotted",
        "name-runs",
        "comma-value",
        "list",
        "exempt-runs",
        "exempt-quote-runs",
        "word-starts",
        "prefixed-runs",
        "prefixed-quote-runs",
        "exempt-then-exact",
        "prefixed-comma-runs",
        "env-lines-after-dashes",
        "empty-then-name-lines",
        "flag-value-runs",
        "glued-quote-runs",
        "next-line-quoted-runs",
        "read-on-quoted-runs",
    ],
)
def test_prefixed_name_rule_is_linear(message):
    import time

    # Measure redaction's CPU work without counting xdist/runner preemption.
    # The same one-second budget still rejects pathological backtracking.

    from local_deep_research.security.log_sanitizer import _redact_log_text

    for redact in (redact_log_message, _redact_log_text):
        started = time.thread_time()
        redact(message)
        elapsed = time.thread_time() - started
        assert elapsed < _QUADRATIC_BUDGET_SECONDS, (redact.__name__, elapsed)


@pytest.mark.parametrize(
    ("message", "secret"),
    [
        ('password="a"Glued987', "Glued987"),
        ("pwd='x'Glued987 next", "Glued987"),
        ('pwd\u200b=""Glued987', "Glued987"),
        ('password\x7f="a"Glued987', "Glued987"),
        ("pass\x00word='x'Glued987", "Glued987"),
        ('DB_PASSWORD=\nOPENAI_API_KEY = "x"Glued987', "Glued987"),
    ],
)
def test_exact_name_glued_quote_tail_is_redacted_once(message, secret):
    """A quoted value glued to the next text leaves no readable tail on
    either log path, and scrubbing the result again changes nothing."""
    from local_deep_research.security.log_sanitizer import _redact_log_text

    redacts = [_redact_log_text]
    # ``redact_log_message`` (exception text) strips nothing, so a name an
    # invisible character splits stays unrecognised there.
    if not any(character in message for character in "\u200b\x7f\x00"):
        redacts.append(redact_log_message)
    for redact in redacts:
        once = redact(message)
        assert secret not in once, (redact.__name__, once)
        assert redact(once) == once, (redact.__name__, once)


def test_full_pass_name_steps_are_a_fixpoint():
    """Seeded: the "exact names first" name steps, run again on their own
    result, change nothing (the claim at ``_FULL_PASS_NAME_STEPS``)."""
    import random

    from local_deep_research.security import log_sanitizer as ls

    rng = random.Random(6879)
    failures = []
    for case in range(1500):
        message, _ = _seeded_log_messages(rng, case)
        for text in (message, ls.strip_control_chars(message)):
            once = ls._run_steps(text, ls._FULL_PASS_NAME_STEPS)
            if ls._run_steps(once, ls._FULL_PASS_NAME_STEPS) != once:
                failures.append(text)
    assert not failures, failures[:3]


@pytest.mark.parametrize(
    "message",
    [
        'OPENAI_API_KEY=abc,DB_PASSWORD="a b Restricted987"',
        'OPENAI_API_KEY=abc,DB_PASSWORD="hunter two Restricted987" next',
        'OPENAI_API_KEY=abc,DB_PASSWORD="a b Restricted987", next',
        'OPENAI_API_KEY=abc,DB_PASSWORD="a b Restricted987";',
        # A quoted EXACT name is closed whatever follows the quote.
        'OPENAI_API_KEY=abc,password="a b Restricted987"\n',
        'OPENAI_API_KEY=abc,password="a b Restricted987"\tnext',
        '-----\nOPENAI_API_KEY = abc,DB_PASSWORD = "a b Restricted987"',
    ],
)
def test_value_read_on_into_a_quoted_assignment_is_redacted(message):
    """``abc,DB_PASSWORD="a`` read as one unquoted value would leave the
    rest of the quoted password readable; for a prefixed quoted name, only
    the "restricted first pass" reading, which redacts the quoted value
    first, hides it (an exact one the bare-name step reads first)."""
    from local_deep_research.security.log_sanitizer import _redact_log_text

    for redact in (redact_log_message, _redact_log_text):
        out = redact(message)
        assert "Restricted987" not in out, (redact.__name__, out)
        assert " b " not in out and "two" not in out, (redact.__name__, out)


def test_second_pass_disagreement_is_not_skipped():
    """First passes that agree do not make the second passes agree: only
    a pipeline other than the kept one hides the value here."""
    from local_deep_research.security.log_sanitizer import _redact_log_text

    message = (
        "requires_api_key = x,a1,;\tapi_key = x,,\r\nb2,"
        'p\x7fwd\n=\n"a b xSecond987='
    )
    assert "Second987" not in _redact_log_text(message)


@pytest.mark.parametrize(
    ("text", "secret"),
    [
        ("Config:\npassword=\napi_key = NextLine987", "NextLine987"),
        (
            "DB_PASSWORD=\nOPENAI_API_KEY = NextLine987\nTIMEOUT=3",
            "NextLine987",
        ),
        ('DB_PASSWORD=\nOPENAI_API_KEY = "x"NextLine987', "NextLine987"),
        ('OPENAI_API_KEY=abc,DB_PASSWORD="a b NextLine987"', "NextLine987"),
        ('password="a"NextLine987', "NextLine987"),
        (
            "Config:\n  DB_PASSWORD =\n  OPENAI_API_KEY = NextLine987\n",
            "NextLine987",
        ),
        ("password=\n\t  api_key = NextLine987\r\n", "NextLine987"),
    ],
)
def test_exception_text_gets_the_log_message_readings(
    rendered_sink, text, secret
):
    """Exception ``str()`` and notes go through ``redact_log_message``,
    which keeps their newlines; it must read them as a log message is
    read, so the next line's name is not taken as an empty value."""
    exc = ValueError(text)
    exc.add_note("note: " + text)
    logger.opt(exception=exc).error("config load failed")
    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered or "ValueError" in rendered
    assert secret not in rendered


def test_log_message_pipelines_track_every_character():
    """``redact_log_message``'s pipelines (no second pass) track origins
    as the log-text ones do."""
    import random

    from local_deep_research.security import log_sanitizer as ls

    rng = random.Random(7068)
    pipelines = ls._LOG_MESSAGE_PIPELINES
    for case in range(300):
        message, _ = _seeded_log_messages(rng, case)
        plain = ls._PipelineRun(message, tracked=False, pipelines=pipelines)
        tracked = ls._PipelineRun(
            message,
            tracked=True,
            unmatched=plain.unmatched,
            pipelines=pipelines,
        )
        for index in range(len(pipelines)):
            text, origins = tracked.result(index)
            assert text == plain.result(index)[0], (index, message)
            assert len(origins) == len(text)
            assert all(
                origin < 0 or message[origin] == character
                for character, origin in zip(text, origins)
            ), (index, message)


def _redact_without_fast_path(monkeypatch, redact, text):
    from local_deep_research.security import log_sanitizer as ls

    with monkeypatch.context() as patched:
        patched.setattr(
            ls, "_result_without_name_assignments", lambda *_args: None
        )
        return redact(text)


@pytest.mark.parametrize(
    "text",
    [
        "Starting research",
        "LLM call completed in 1.24s (tokens=512, max_tokens=4096)",
        "Fetching https://user:FastPathPw1@h/x?q=1&limit=10",
        "Authorization: Bearer abcdefghijklmnopqrstuvwxyz0123",
        "max_tokens=5 tokens\x1c=7 x-tokens = 2",
        "password is not set\x1c",
    ],
)
def test_text_without_a_name_assignment_skips_the_name_steps(monkeypatch, text):
    """A record with no credential name followed by ``=`` gets only the
    shape redaction, without running every pipeline's name steps (most
    records), and the same text the pipelines give."""
    from local_deep_research.security import log_sanitizer as ls

    def _no_name_steps(*_args):
        raise AssertionError("name steps ran")

    for redact in (redact_log_message, ls._redact_log_text):
        expected = _redact_without_fast_path(monkeypatch, redact, text)
        with monkeypatch.context() as patched:
            patched.setattr(ls._PipelineRun, "first_pass", _no_name_steps)
            assert redact(text) == expected, (redact.__name__, text)


@pytest.mark.parametrize(
    ("text", "secret"),
    [
        ("api\u200bkey=HintAfterStrip987", "HintAfterStrip987"),
        ("pass\x1cword = HintAfterStrip987", "HintAfterStrip987"),
        ("OPENAI_API_KEY\n=\nHintAfterStrip987", "HintAfterStrip987"),
        ("paſſword=HintCaseFold987", "HintCaseFold987"),
    ],
)
def test_name_assignment_hint_sees_what_the_name_steps_see(text, secret):
    """The fast path never skips a name the steps would read: one that
    only appears once control characters are stripped, or one matched by
    ``(?i)`` case folding (U+017F folds to ``s``)."""
    from local_deep_research.security.log_sanitizer import _redact_log_text

    assert secret not in _redact_log_text(text)


def test_fast_path_gives_the_pipelines_result(monkeypatch):
    """Seeded differential: with and without the fast path, both
    redactions give the same text, also for messages with the ``=`` or
    the name broken up so that the fast path applies."""
    import random

    from local_deep_research.security import log_sanitizer as ls

    rng = random.Random(6879)
    mismatches = []
    for case in range(300):
        message, _ = _seeded_log_messages(rng, case)
        for text in (
            message,
            message.replace("=", " "),
            message.replace("=", ":"),
            message.replace("_", "\u200b"),
        ):
            for redact in (redact_log_message, ls._redact_log_text):
                slow = _redact_without_fast_path(monkeypatch, redact, text)
                if redact(text) != slow:
                    mismatches.append((redact.__name__, text))
    assert not mismatches, mismatches[:3]


def test_prefixed_name_and_header_are_scrubbed_from_exception_sink(
    rendered_sink,
):
    """The three logging fixes compose on the rendered traceback path."""
    name_secret = "NamedCredential987654"
    header_secret = "HeaderCredential987654"
    try:
        raise ValueError(
            f"OPENAI_API_KEY={name_secret} Api-Key: {header_secret}"
        )
    except ValueError:
        logger.exception("provider failed")

    rendered = rendered_sink.getvalue()
    assert "Traceback" in rendered
    assert "ValueError" in rendered
    assert name_secret not in rendered
    assert header_secret not in rendered