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.

83.5 KB
Raw Blame
"""The notification service URL must be redacted on the settings read paths.

`notifications.service_url` is an apprise-style URL that embeds credentials
(`mailto://user:pass@host`, `discord://webhook_id/token`, Slack/ntfy tokens).
The app already masks it in logs via `mask_sensitive_url()`, but it was returned
in PLAINTEXT on every `/settings/api` read path -- unlike API keys, which come
back `[REDACTED]`. That ships a secret to the browser / JSON API where it can be
cached by proxies or pasted into bug reports. The fix adds `service_url` to
`DataSanitizer.DEFAULT_SENSITIVE_KEYS`.

These tests pin: the configured URL is redacted on single/bulk/full GET and in
the save-all echo; an unconfigured (empty) URL stays readable (so the
configured/unconfigured distinction survives); and the write-back no-op guard
refuses to overwrite the real value with the redaction sentinel (no round-trip
clobber). A non-secret unit check confirms `is_sensitive_setting` now catches
the key.

Follow-up coverage pins the write/test paths introduced alongside the
redaction: clearing a configured URL stores the empty string (no silent
retention), a sentinel write is a no-op that leaves the stored secret intact,
Test Notification falls back to the stored URL when the field shows the
sentinel, and the settings debug logger redacts the key. Whitespace-only
values count as unconfigured (they stay readable, matching the notification
manager's `.strip()` check).

Ported onto the FastAPI surface (``web/routers/settings.py`` /
``web/fastapi_app.py``) for the ``refactor/fastapi-migration-phase1``
branch. Several assertions below are LEFT FAILING on purpose: a completed
merge audit found this file is the only end-to-end pin for PR #5602's
security contract, and the FastAPI rewrite silently dropped part of it.
Each failing test names the tracking issue in its body/docstring:

  * #5947 / fix in #5956 -- the ``_embeds_redaction_sentinel`` containment
    guard (a value that merely CONTAINS "[REDACTED]" must be rejected)
    does not exist anywhere in ``web/routers/settings.py``.
  * #5960 -- the write-path no-op guard was not narrowed to
    password-typed settings; it still treats "" (not just the exact
    sentinel) as a no-op for EVERY sensitive setting, so clearing
    ``notifications.service_url`` is a silent no-op instead of storing
    the empty string.
  * #5958 -- ``POST /settings/api/notifications/test-url`` lost its
    stored-URL fallback for a sentinel/empty ``service_url`` field.

Do not "fix" these tests by weakening, skipping, or xfailing the
assertion -- that is the exact failure mode this file exists to catch.
"""

import os

# Rate limiting is read once at import time in
# ``web/dependencies/rate_limit.py``; make sure it is disabled before the
# app (and that module) is imported. CI exports this container-wide, but
# set it defensively so a stray direct run of this file can't flake on the
# settings-mutation rate-limit bucket this file's ~20 tests share one user
# with.
os.environ.setdefault("LDR_DISABLE_RATE_LIMITING", "true")

import pytest  # noqa: E402
from contextlib import contextmanager  # noqa: E402

from local_deep_research.security.data_sanitizer import DataSanitizer  # noqa: E402
from local_deep_research.settings.logger import redact_sensitive_keys  # noqa: E402

WEBHOOK_KEY = "notifications.service_url"
SECRET_URL = "discord://HOOKID_XYZ/TOKEN_SECRET_abcdefghijklmnop"
ROTATED_SECRET_URL = "discord://HOOKID_ABC/ROTATED_TOKEN_zyxwvutsrqponmlk"
REDACTED = DataSanitizer.REDACTION_TEXT


# --------------------------------------------------------------------------- #
# Unit: the predicate now treats the notification service URL as a secret.
# --------------------------------------------------------------------------- #
def test_service_url_redacts_only_when_configured():
    assert DataSanitizer.redact_value(WEBHOOK_KEY, value=SECRET_URL) == REDACTED
    assert DataSanitizer.redact_value(WEBHOOK_KEY, value="") == ""
    assert DataSanitizer.redact_value(WEBHOOK_KEY, value="  ") == "  "


# --------------------------------------------------------------------------- #
# End-to-end through the real settings API.
# --------------------------------------------------------------------------- #
@pytest.fixture
def logged_in_client(authenticated_client):
    """A fresh registered+logged-in FastAPI TestClient for this test.

    ``authenticated_client`` (tests/conftest.py) is the established
    FastAPI-migration fixture: it registers a brand-new user (unique
    username via uuid), logs in, and arms ``X-CSRFToken`` as a default
    header on a Flask-compat-shimmed ``TestClient`` -- the same shim that
    gives ``.get_json()`` / ``.get_data(as_text=True)`` on responses this
    file's helpers below already rely on. It also stamps a unique
    ``X-Forwarded-For`` per client so this file's many tests don't share
    the per-IP registration bucket.

    Deliberately function-scoped (a NEW user per test, not one shared
    module-scoped user): this file mutates ``notifications.service_url``
    on the SAME key across ~20 tests, and the settings-mutation rate
    limit (30/min) is keyed per-user -- sharing one user across the whole
    module would burn that budget well before the file finishes.
    """
    return authenticated_client


def _put(client, key, value):
    r = client.put(f"/settings/api/{key}", json={"value": value})
    assert r.status_code == 200, (key, r.status_code, r.get_data(as_text=True))
    return r


def _single(client, key):
    r = client.get(f"/settings/api/{key}")
    assert r.status_code == 200
    return r.get_json().get("value")


def _bulk(client, key):
    r = client.get(f"/settings/api/bulk?keys[]={key}")
    assert r.status_code == 200
    return r.get_json()["settings"][key]["value"]


def _full(client, key):
    r = client.get("/settings/api")
    assert r.status_code == 200
    return r.get_json()["settings"][key]["value"]


def test_configured_service_url_is_redacted_on_every_read_path(
    logged_in_client,
):
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    assert _single(c, WEBHOOK_KEY) == REDACTED, "single GET leaks the webhook"
    assert _bulk(c, WEBHOOK_KEY) == REDACTED, "bulk GET leaks the webhook"
    assert _full(c, WEBHOOK_KEY) == REDACTED, "full GET leaks the webhook"

    # The plaintext secret must not appear anywhere in a full settings dump.
    full = c.get("/settings/api").get_data(as_text=True)
    assert "TOKEN_SECRET_abcdefghijklmnop" not in full

    # The save-all echo must not ship the plaintext back either.
    resp = c.post(
        "/settings/save_all_settings",
        json={WEBHOOK_KEY: "discord://HOOKID_XYZ/ROTATED_TOKEN_zzzzzzzz"},
    )
    assert resp.status_code == 200
    assert "ROTATED_TOKEN" not in resp.get_data(as_text=True)


def test_empty_service_url_stays_readable(logged_in_client):
    """An unconfigured URL is not a secret -- the empty-value rule keeps the
    configured/unconfigured distinction visible to the UI/API."""
    c = logged_in_client
    _put(c, WEBHOOK_KEY, "")
    assert _single(c, WEBHOOK_KEY) == ""
    assert _bulk(c, WEBHOOK_KEY) == ""
    assert _full(c, WEBHOOK_KEY) == ""


def test_sensitive_predicate_covers_service_url():
    assert DataSanitizer.is_sensitive_setting(WEBHOOK_KEY) is True


def test_clear_configured_service_url(logged_in_client):
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)
    assert _single(c, WEBHOOK_KEY) == REDACTED

    _put(c, WEBHOOK_KEY, "")
    assert _single(c, WEBHOOK_KEY) == ""

    _put(c, WEBHOOK_KEY, ROTATED_SECRET_URL)
    assert _single(c, WEBHOOK_KEY) == REDACTED


def test_sentinel_write_is_noop_for_configured_secret(logged_in_client):
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    response = _put(c, WEBHOOK_KEY, REDACTED)

    assert "unchanged" in response.get_json()["message"].lower()
    assert _single(c, WEBHOOK_KEY) == REDACTED


def test_sentinel_noop_leaves_stored_secret_usable(
    logged_in_client, monkeypatch
):
    """The sentinel no-op must leave the STORED secret intact, not store the
    sentinel: after a refused [REDACTED] write, the test-url fallback still
    resolves and tests the original secret (GET alone cannot distinguish a
    retained secret from a stored sentinel because redaction masks both)."""
    from local_deep_research.notifications.service import NotificationService

    captured_urls = []

    def capture_url(_service, url):
        captured_urls.append(url)
        return {"success": True, "message": "Notification sent"}

    monkeypatch.setattr(NotificationService, "test_service", capture_url)
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)
    _put(c, WEBHOOK_KEY, REDACTED)  # refused no-op

    response = c.post(
        "/settings/api/notifications/test-url",
        json={"service_url": REDACTED},
    )
    assert response.status_code == 200
    assert captured_urls == [SECRET_URL]


def test_test_url_uses_stored_when_sentinel(logged_in_client, monkeypatch):
    from local_deep_research.notifications.service import NotificationService

    captured_urls = []

    def capture_url(_service, url):
        captured_urls.append(url)
        return {"success": True, "message": "Notification sent"}

    monkeypatch.setattr(NotificationService, "test_service", capture_url)
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    response = c.post(
        "/settings/api/notifications/test-url",
        json={"service_url": REDACTED},
    )

    assert response.status_code == 200
    assert response.get_json()["success"] is True
    assert captured_urls == [SECRET_URL]

    _put(c, WEBHOOK_KEY, "")
    unconfigured_response = c.post(
        "/settings/api/notifications/test-url",
        json={"service_url": REDACTED},
    )
    assert unconfigured_response.status_code == 400
    assert unconfigured_response.get_json() == {
        "success": False,
        "error": "No notification URL configured",
    }
    assert captured_urls == [SECRET_URL]


def test_test_url_treats_whitespace_stored_url_as_unconfigured(
    logged_in_client, monkeypatch
):
    """A stored "   " is truthy, so a falsiness check would hand Apprise
    literal whitespace. ``DataSanitizer._is_empty_value`` and the
    notification manager both ``.strip()``; the test endpoint must agree."""
    from local_deep_research.notifications.service import NotificationService

    captured_urls = []

    def capture_url(_service, url):
        captured_urls.append(url)
        return {"success": True, "message": "Notification sent"}

    monkeypatch.setattr(NotificationService, "test_service", capture_url)
    c = logged_in_client
    _put(c, WEBHOOK_KEY, "   ")

    for body in ({}, {"service_url": "   "}, {"service_url": REDACTED}):
        response = c.post("/settings/api/notifications/test-url", json=body)
        assert response.status_code == 400, body
        assert response.get_json() == {
            "success": False,
            "error": "No notification URL configured",
        }

    assert captured_urls == []


def test_logger_redacts_service_url():
    result = redact_sensitive_keys(
        {
            "notifications.service_url": "discord://x/y",
            "llm.openai.api_key": "k",
        }
    )

    assert result == {
        "notifications.service_url": "***REDACTED***",
        "llm.openai.api_key": "***REDACTED***",
    }


# --------------------------------------------------------------------------- #
# Containment guard: a value that EMBEDS the sentinel is a corrupted edit.
#
# `notifications.service_url` is the first non-password sensitive setting, so
# it is the first one whose control renders its (redacted) value into an
# editable field. A stale client that rendered "[REDACTED]" produces partial
# edits, and neither failure mode is caught by the exact-match no-op:
#
#   "[REDACTED],discord://webhook/tok"
#       parse_notification_url_list reports invalid_fragment="[REDACTED]", so
#       NotificationManager._filter_urls_by_egress_policy refuses EVERY url in
#       the list. Notifications stop entirely, with only a policy_audit
#       warning to show for it.
#
#   "discord://webhook/tok,[REDACTED]"
#       validate_multiple_urls returns True (the comma is not followed by a
#       scheme, so it stays one URL) and "[REDACTED]" is appended to the
#       Discord token: a permanently broken webhook that validation blesses.
#
# No legitimate secret contains the sentinel, so every write route rejects it.
# --------------------------------------------------------------------------- #
CORRUPT_PREFIX = f"{REDACTED},{SECRET_URL}"
CORRUPT_SUFFIX = f"{SECRET_URL},{REDACTED}"
CORRUPT_VALUES = (CORRUPT_PREFIX, CORRUPT_SUFFIX, f"  {REDACTED}  ")


def _stored_url(client, monkeypatch):
    """Return the URL the server actually has stored.

    A GET cannot tell a retained secret from a corrupted one -- redaction
    masks every non-empty value to the same sentinel. The test-url endpoint
    falls back to the stored value, so capturing what it hands Apprise is the
    only way to observe the plaintext the server kept.
    """
    from local_deep_research.notifications.service import NotificationService

    captured = []

    def capture_url(_service, url):
        captured.append(url)
        return {"success": True, "message": "Notification sent"}

    monkeypatch.setattr(NotificationService, "test_service", capture_url)
    response = client.post(
        "/settings/api/notifications/test-url", json={"service_url": REDACTED}
    )
    assert response.status_code == 200, response.get_data(as_text=True)
    assert len(captured) == 1
    return captured[0]


def test_embedded_sentinel_predicate():
    """Unit contract: embedding is rejected, an exact match is not (that stays
    the untouched-field no-op), and non-sensitive settings are untouched.

    GENUINE LOSS (#5947, fix in #5956): ``_embeds_redaction_sentinel`` does
    not exist anywhere in the ported
    ``web/routers/settings.py`` -- this import fails with an ImportError.
    Left failing on purpose; do not skip/xfail."""
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
    )

    for corrupt in CORRUPT_VALUES:
        assert _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", corrupt), (
            corrupt
        )
    # Exact match belongs to _is_secret_empty_noop, not here.
    assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", REDACTED)
    assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", SECRET_URL)
    assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", "")
    # Password inputs render blank so they cannot produce this, but the
    # predicate covers them anyway -- it keys off sensitivity, not ui_element.
    assert _embeds_redaction_sentinel(
        "llm.openai.api_key", "password", f"sk-{REDACTED}"
    )
    # A non-sensitive setting may legitimately contain the string.
    assert not _embeds_redaction_sentinel(
        "report.title", "text", f"How we {REDACTED} things"
    )
    # Non-strings are never rejected.
    assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", None)
    assert not _embeds_redaction_sentinel(WEBHOOK_KEY, "textarea", 17)


# --------------------------------------------------------------------------- #
# Container guard: round 3 taught ``redact_value`` to mask a dict/list value
# leaf-by-leaf (``_force_redact_strings``) for a key that matches ONLY the
# broadened suffix arm, e.g. ``local_search_milvus_token`` -> {"uri": ...,
# "token": ...}. Round 3 left the three write-back guards (a)
# ``isinstance(value, str)``-gated, so a masked container sailed past every
# one of them: GET, save untouched, and the sentinel is persisted over the
# real credential and its non-secret siblings. These pin the fix -- the
# container arm added to ``_is_secret_empty_noop`` /
# ``_embeds_redaction_sentinel`` / ``_embeds_sentinel_on_create``.
# --------------------------------------------------------------------------- #
BROADENED_ARM_KEY = "local_search_milvus_token"
# An outer key that is NOT itself sensitive. ``redact_value`` recurses a
# container by SUB-KEY, so a leaf named for an exact
# ``DEFAULT_SENSITIVE_KEYS`` entry is masked here regardless of the
# outer key -- and, unlike the broadened-arm key above, regardless of
# the leaf VALUE's type (the exact-match arm returns the bare sentinel
# before the container arm is reached). This is the only key class that
# can produce a bare sentinel over a stored list/dict/int/bool leaf.
NON_SENSITIVE_OUTER_KEY = "llm.model_kwargs"


def test_container_sentinel_roundtrip_is_noop():
    """(a) A container GET'd and saved back UNTOUCHED -- every string leaf
    is exactly "[REDACTED]", matching what ``_force_redact_strings`` would
    have produced, AND every non-string leaf still matches the stored row
    -- must be an idempotent no-op, not a write. The stored credential
    must not be overwritten with the sentinel.

    Round 5 (#6089-follow-up): the no-op arm now takes the setting's
    current stored value as a fourth argument and requires non-string
    leaves to match it too -- see ``test_container_non_string_leaf_edit_
    is_not_a_silent_noop`` below for the bug this closes.
    """
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
    )

    stored = {"uri": "mysql://real", "token": "s3cr3t-real", "port": 19530}
    roundtrip = {"uri": REDACTED, "token": REDACTED, "port": 19530}
    assert _is_secret_empty_noop(BROADENED_ARM_KEY, None, roundtrip, stored)
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, roundtrip, stored
    )

    # List containers, and empty/whitespace leaves (never masked by
    # _force_redact_strings), are covered the same way.
    stored_list = [{"value": "s3cr3t-real"}, "s3cr3t-real", 42, True]
    assert _is_secret_empty_noop(
        BROADENED_ARM_KEY,
        None,
        [{"value": REDACTED}, REDACTED, 42, True],
        stored_list,
    )
    stored_with_blank = {
        "uri": "mysql://real",
        "token": "s3cr3t-real",
        "note": "  ",
    }
    assert _is_secret_empty_noop(
        BROADENED_ARM_KEY,
        None,
        {"uri": REDACTED, "token": REDACTED, "note": "  "},
        stored_with_blank,
    )

    # A non-sensitive key's identical-looking container IS still a no-op.
    #
    # PREMISE CHANGE (post-#6201 review, item 4): earlier rounds gated the
    # dict/list arm on ``is_sensitive_setting(key)`` the same way the plain
    # string arm still is, so this assertion used to be ``assert not
    # _is_secret_empty_noop(...)`` -- a non-sensitive outer key's container
    # was deliberately NOT treated as a no-op round-trip. That premise was
    # reversed a few rounds later (see ``_is_secret_empty_noop``'s own
    # docstring, "The dict/list arm runs BEFORE the ``is_sensitive_setting``
    # gate"): ``redact_value`` masks a container by SUB-KEY, so an outer key
    # that is not itself sensitive (``llm.model_kwargs``) can still carry a
    # sensitive-named leaf (``stop_token``) that gets masked on GET. Gating
    # the container check on the OUTER key's sensitivity meant that leaf's
    # sentinel round-tripped straight back into the DB on the very next
    # save, because no guard ever looked at it (#6201's disclosure bug was
    # a direct consequence of this same "gate on outer key" mistake
    # resurfacing in the coercion step). The literal string "[REDACTED]" is
    # not a value any real setting leaf would hold, sensitive-key or not,
    # so the container arm now scans every dict/list write for it
    # unconditionally -- which makes this exact round-trip a no-op
    # regardless of ``llm.some_plain_setting`` not itself being a
    # sensitive-named key. This test was left asserting the OLD premise
    # (a red test at HEAD); the assertion below pins the current, intended
    # behaviour instead.
    assert _is_secret_empty_noop(
        "llm.some_plain_setting", None, roundtrip, stored
    )

    # No stored value to verify against (e.g. no prior row): a
    # sentinel-bearing container can never be proven an untouched
    # round-trip, so it must NOT be treated as a no-op.
    assert not _is_secret_empty_noop(BROADENED_ARM_KEY, None, roundtrip, None)
    assert _embeds_redaction_sentinel(BROADENED_ARM_KEY, None, roundtrip, None)


def test_container_spliced_sentinel_is_rejected_but_mixed_edit_merges():
    """(b) A leaf with the sentinel SPLICED into it (not an exact match) is
    always a corrupted edit and must be rejected with the same 400
    semantics as the plain string case, not silently persisted with
    "[REDACTED]" spliced into the real secret.

    A container where a DIFFERENT leaf was legitimately edited while a
    secret leaf stays verbatim "[REDACTED]" is NOT the same thing, and
    must not get the same reject-outright treatment (round 5's fix, since
    corrected -- see the follow-up round this test now pins):
    ``redact_value`` masks a container by SUB-KEY, so a mixed container
    such as ``{"uri": "...", "token": "...", "port": 19530}`` under a key
    that matches only the broadened arm has just its ``token`` masked on
    GET. Requiring EVERY maskable leaf to be exactly the sentinel before
    treating the container as anything but a corrupted edit made that
    GET's own output un-resubmittable: a client can never see the real
    secret to resend it, and any masked-leaf resubmission looked
    "corrupted". The write must instead merge -- restore ``token`` from
    the stored row, keep the edited ``host``/``port`` as submitted -- and
    save, not 400.
    """
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _reconciled_container_value_for_write,
    )

    stored = {"uri": "mysql://real", "token": "s3cr3t-real", "port": 19530}

    # A leaf with the sentinel spliced into it rather than exactly equal:
    # always a corrupted edit, regardless of what else in the container
    # changed.
    spliced = {
        "uri": REDACTED,
        "token": f"pre{REDACTED}post",
        "port": 19530,
    }
    assert _embeds_redaction_sentinel(BROADENED_ARM_KEY, None, spliced, stored)
    assert not _is_secret_empty_noop(BROADENED_ARM_KEY, None, spliced, stored)

    # A non-secret sibling legitimately edited (a new "host" key, an
    # updated "port") while the secret leaves are still verbatim
    # "[REDACTED]" must MERGE and save: the secret leaves restore from
    # `stored`, the edits are kept, and this is neither a no-op nor a
    # rejection.
    mixed = {
        "uri": REDACTED,
        "token": REDACTED,
        "host": "new-host.example.com",
        "port": 19531,
    }
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, mixed, stored
    )
    assert not _is_secret_empty_noop(BROADENED_ARM_KEY, None, mixed, stored)
    assert _reconciled_container_value_for_write(mixed, mixed, stored) == (
        {
            "uri": "mysql://real",
            "token": "s3cr3t-real",
            "host": "new-host.example.com",
            "port": 19531,
        },
        True,
    )

    # A genuinely fresh value with no sentinel anywhere trips neither guard.
    fresh = {"uri": "mysql://real", "token": "s3cr3t-real", "port": 19530}
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, fresh, stored
    )
    assert not _is_secret_empty_noop(BROADENED_ARM_KEY, None, fresh, stored)


def test_container_non_string_leaf_edit_merges_and_saves():
    """R1d / round 5 (#6089-follow-up), corrected by the next round: editing
    ONLY a non-string leaf inside a sensitive container -- e.g. a port
    number -- while every string leaf stays exactly "[REDACTED]" must NOT
    be silently discarded as a no-op (round 4's bug -- see the docstring
    below), and must NOT have the sentinel spliced into a string leaf
    either. Round 5 fixed both by rejecting the whole container outright;
    that overshot -- editing a non-secret sibling of a real secret is a
    completely ordinary edit, and rejecting it makes the container
    un-savable without retyping a secret the client cannot see. The
    correct outcome is a MERGE: the secret leaf restores from the stored
    row, the non-string edit is kept, and the write is neither a no-op
    nor a rejection.

    Round 4's ``_container_all_leaves_are_sentinel`` only ever inspected
    STRING leaves, so a stored ``{"token": "s", "port": 19530}`` submitted
    back as ``{"token": "[REDACTED]", "port": 19531}`` looked identical to
    a pure round-trip and the port edit vanished silently.
    """
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _reconciled_container_value_for_write,
    )

    stored = {"token": "s", "port": 19530}
    port_edit = {"token": REDACTED, "port": 19531}

    assert not _is_secret_empty_noop(BROADENED_ARM_KEY, None, port_edit, stored)
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, port_edit, stored
    )
    assert _reconciled_container_value_for_write(
        port_edit, port_edit, stored
    ) == ({"token": "s", "port": 19531}, True)

    # An untouched round-trip against the same stored row stays a no-op.
    untouched = {"token": REDACTED, "port": 19530}
    assert _is_secret_empty_noop(BROADENED_ARM_KEY, None, untouched, stored)
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, untouched, stored
    )

    # A bool-toggle sibling edit is merged and saved the same way.
    stored_with_flag = {"token": "s", "enabled": False}
    flag_edit = {"token": REDACTED, "enabled": True}
    assert not _is_secret_empty_noop(
        BROADENED_ARM_KEY, None, flag_edit, stored_with_flag
    )
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, flag_edit, stored_with_flag
    )
    assert _reconciled_container_value_for_write(
        flag_edit, flag_edit, stored_with_flag
    ) == ({"token": "s", "enabled": True}, True)


def test_container_orphan_sentinel_is_rejected():
    """A sentinel-exact leaf with NO corresponding secret in the stored row
    -- a brand-new key the client happened to set to the literal
    placeholder text, or a structural mismatch -- cannot be resolved by
    the merge and must still be rejected, unlike the resolvable mixed-edit
    case above.
    """
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
    )

    stored = {"token": "s", "port": 19530}

    # "new_secret" has no counterpart in `stored` to restore from.
    new_key_sentinel = {
        "token": REDACTED,
        "new_secret": REDACTED,
        "port": 19530,
    }
    assert _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, new_key_sentinel, stored
    )
    assert not _is_secret_empty_noop(
        BROADENED_ARM_KEY, None, new_key_sentinel, stored
    )

    # A leaf whose stored counterpart is EMPTY -- by ``_is_empty_value``'s
    # own rule, the same rule ``redact_value`` gates masking on -- is an
    # orphan too: nothing was masked, so the sentinel cannot have come
    # from a round-trip and there is nothing to restore.
    for empty in ("", "   ", None, [], {}):
        stored_empty = {"token": empty, "port": 19530}
        assert (
            DataSanitizer.redact_value(
                NON_SENSITIVE_OUTER_KEY, None, stored_empty
            )
            == stored_empty
        ), (
            "premise broken: an empty stored leaf was masked after all, so "
            f"the sentinel below WOULD be a legitimate round-trip ({empty!r})"
        )
        assert _embeds_redaction_sentinel(
            NON_SENSITIVE_OUTER_KEY,
            None,
            {"token": REDACTED, "port": 19530},
            stored_empty,
        ), (
            f"an orphan sentinel over an empty stored leaf was accepted ({empty!r})"
        )


def test_container_sentinel_over_a_non_string_stored_leaf_restores_it():
    """A masked leaf whose stored value is a list/dict/int/bool must be
    RESTORED from it, not rejected (post-#6201 review BL1, #6213 item c).

    ``redact_value`` masks a nested leaf to the bare sentinel for ANY
    non-empty value when the sub-key is an exact ``DEFAULT_SENSITIVE_KEYS``
    match -- the exact-match arm runs before the container arm, so the
    value's type never gets a say. An earlier version of this test asserted
    the OPPOSITE, with two defects worth naming so they are not
    reintroduced:

      * its rationale ("the stored counterpart is not a (non-empty)
        string, so there is nothing maskable to have restored the sentinel
        from") was false -- ``redact_value`` masks ``{"token": 42}`` to
        ``{"token": "[REDACTED]"}`` under a non-sensitive outer key, and
        ``42`` is exactly what should be restored; and
      * it used ``BROADENED_ARM_KEY``, for which the shape is UNREACHABLE
        (asserted below): that key matches only the broadened snake_case
        arm, whose container path is ``_force_redact_strings``, and that
        leaves a non-string leaf alone. So the assertion pinned a
        rejection using the one key class that can never produce it, while
        for the key class that CAN -- any non-sensitive outer key -- the
        pinned behaviour was a permanent, unsatisfiable 400.
    """
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _reconciled_container_value_for_write,
    )

    for stored_leaf in (42, True, False, 0, ["a", "b"], {"h": "v"}):
        stored = {"token": stored_leaf, "port": 19530}
        masked = DataSanitizer.redact_value(
            NON_SENSITIVE_OUTER_KEY, None, stored
        )
        # PREMISE: the GET really does produce the bare sentinel here.
        assert masked == {"token": REDACTED, "port": 19530}, (
            f"a stored {type(stored_leaf).__name__} under the exact leaf "
            f"'token' did not mask to the bare sentinel: {masked!r}"
        )
        # ...so submitting it straight back must be a recognized no-op.
        assert not _embeds_redaction_sentinel(
            NON_SENSITIVE_OUTER_KEY, None, masked, stored
        ), (
            f"an untouched round-trip of a stored "
            f"{type(stored_leaf).__name__} leaf was rejected -- the client "
            "cannot satisfy this request, it never saw the real value"
        )
        assert _is_secret_empty_noop(
            NON_SENSITIVE_OUTER_KEY, None, masked, stored
        )
        # ...and a sibling edit beside it restores the stored value with
        # its TYPE intact (``==`` alone would let True pass as 1).
        edit = dict(masked)
        edit["port"] = 19531
        merged, is_reconciled = _reconciled_container_value_for_write(
            edit, edit, stored
        )
        assert is_reconciled is True
        assert merged == {"token": stored_leaf, "port": 19531}
        assert type(merged["token"]) is type(stored_leaf)

    # NEGATIVE CONTROL, in the same arm the widening changed: a sentinel
    # SPLICED into a longer string is still a corrupted edit, whatever the
    # stored leaf's type. The widening moved one condition two lines above
    # this branch, so a future edit that collapses the two would silently
    # start restoring over a spliced value; nothing else pins the container
    # form of the splice (the existing CORRUPT_VALUES tests are all scalar
    # and multiselect).
    for stored_leaf in (42, True, ["a"], {"h": "v"}, "s3cr3t-real"):
        for spliced in (f"{REDACTED},new", f"new,{REDACTED}", f"x{REDACTED}"):
            assert _embeds_redaction_sentinel(
                NON_SENSITIVE_OUTER_KEY,
                None,
                {"token": spliced, "port": 19530},
                {"token": stored_leaf, "port": 19530},
            ), (
                "a spliced sentinel was accepted over a stored "
                f"{type(stored_leaf).__name__} leaf ({spliced!r})"
            )

    # The BROADENED-ARM key cannot reach this shape at all: its container
    # path is ``_force_redact_strings``, which leaves a non-string leaf
    # alone, so the GET returns the row unmasked and there is no sentinel
    # to round-trip. Asserted so the key choice above stays deliberate.
    assert DataSanitizer.redact_value(
        BROADENED_ARM_KEY, None, {"token": 42}
    ) == {"token": 42}


def test_coerce_setting_for_write_would_flatten_a_reconciled_container():
    """The HAZARD behind the coercion skip, stated as an executable fact --
    NOT as a stand-in for the call sites.

    Running a merged, secret-bearing container through
    ``coerce_setting_for_write`` under a str-typed ui_element
    ("text"/"textarea"/"password"/"select") flattens it to its Python REPR
    via ``str()``, turning a just-restored secret into a plain,
    non-redacted string persisted under a key nothing downstream treats as
    sensitive -- which ``redact_value`` then ships back verbatim on the
    next GET. That is how #6201 round 1 turned a data-LOSS bug into a
    secret-DISCLOSURE bug.

    WHAT THIS TEST DELIBERATELY DOES NOT DO (round-5 review, defect 1):
    an earlier version of it re-implemented the call sites' own
    ``merged if is_reconciled_container else coerce_setting_for_write(...)``
    decision inside the test body and asserted on the result. That reads
    as coverage while pinning nothing: reverting the skip in
    ``_save_all_settings_sync`` or ``_save_settings_sync`` left it green,
    because the test never called a route. A test that reproduces the
    logic it is testing is worse than no test. The call sites are pinned
    end-to-end instead, one route-level test each, in
    ``tests/security/test_settings_egress_and_secrets_fastapi.py``:

      * ``test_save_all_settings_reconciled_container_is_not_coerced_to_str``
      * ``test_save_settings_form_reconciled_container_is_not_coerced_to_str``
      * ``test_nested_secret_edit_under_default_text_ui_element_does_not_leak``
        (``_api_update_setting_sync``)
      * ``test_reconciled_container_on_a_number_row_is_never_logged``
        (both the coercion and the ``validate_setting`` skip, all three
        routes, via the WARNING those converters emit on a numeric row)

    What is left here is the part that genuinely belongs in isolation: the
    merge contract (``_reconciled_container_value_for_write`` restores the
    secret, keeps the edit, and flags the result as reconciled) and proof
    that the coercion those call sites skip really would leak.
    """
    from local_deep_research.web.routers.settings import (
        _reconciled_container_value_for_write,
        coerce_setting_for_write,
    )

    key = "llm.probe.model_kwargs"  # not itself a sensitive-named key
    stored = {"stop_token": "s3cr3t-real-9911", "page_token": "old-value"}
    # A genuine edit: page_token changes, stop_token stays masked. A pure
    # no-op round-trip short-circuits before the coercion call, so it is
    # not the shape that matters here.
    edit = {"stop_token": REDACTED, "page_token": "new-value"}

    merged, is_reconciled_container = _reconciled_container_value_for_write(
        edit, edit, stored
    )
    assert is_reconciled_container is True, (
        "the caller's signal to skip coercion was not raised for a merged "
        "secret-bearing container"
    )
    assert merged == {
        "stop_token": "s3cr3t-real-9911",
        "page_token": "new-value",
    }

    # The hazard itself: this is what the call sites must NOT do with
    # `merged`. Asserting it here documents why the skip exists and fails
    # if `coerce_setting_for_write` ever stops flattening containers (at
    # which point the skip's rationale, and its docstrings, need revisiting
    # rather than silently rotting).
    would_have_leaked = coerce_setting_for_write(
        key=key, value=merged, ui_element="text"
    )
    assert type(would_have_leaked) is str
    assert "s3cr3t-real-9911" in would_have_leaked


def test_values_equal_treats_int_and_float_as_the_same_number():
    """Round-5 defect (2): ``_values_equal`` required ``type(a) is type(b)``
    for numerics, which made an ordinary browser round-trip of a masked
    >=2-item list UNSATISFIABLE.

    ``_non_sentinel_fields_match`` -- the anti-permutation gate -- calls
    this on every visible leaf, and a stored ``30.0`` can never be echoed
    back as a float by a browser: JavaScript's only number type is a
    double that serializes as ``30``, and ``settings.js`` additionally
    ``parseInt``s a decimal-free numeric before submitting. So the gate
    rejected EVERY submission, untouched round-trips included, with a 400
    the client had no way to satisfy.

    The end-to-end consequence is pinned by
    ``test_masked_list_edit_survives_javascript_number_normalization``
    (``tests/security/test_settings_egress_and_secrets_fastapi.py``),
    including the falsifier that a real permutation is still refused. This
    test pins the predicate itself, at every place it recurses.
    """
    from local_deep_research.web.routers.settings import (
        _non_sentinel_fields_match,
        _values_equal,
    )

    assert _values_equal(30, 30.0)
    assert _values_equal(30.0, 30)
    assert _values_equal({"timeout": 30}, {"timeout": 30.0})
    assert _values_equal([{"timeout": 30}], [{"timeout": 30.0}])
    # Same number, still not the same VALUE when it differs.
    assert not _values_equal(30, 31)
    assert not _values_equal(1, 1.5)
    # A number is still not a string spelling of that number.
    assert not _values_equal(30, "30")

    # The gate this predicate exists for: the visible fields of a masked
    # list item, echoed back with JavaScript's integer normalization,
    # must count as unchanged.
    stored_item = {"url": "https://a.example", "api_key": "K1", "timeout": 30.0}
    echoed_item = {
        "url": "https://a.example",
        "api_key": REDACTED,
        "timeout": 30,
    }
    assert _non_sentinel_fields_match(echoed_item, stored_item, REDACTED)
    # ... while a changed visible field still is not.
    moved_item = {
        "url": "https://b.example",
        "api_key": REDACTED,
        "timeout": 30,
    }
    assert not _non_sentinel_fields_match(moved_item, stored_item, REDACTED)


def test_values_equal_still_treats_bool_and_int_as_different():
    """The round-3 fix that must NOT regress: ``bool`` is an ``int``
    subclass, so a plain ``==`` calls ``1`` unchanged from a stored
    ``True`` and ``_resolve_container_secret_write``'s no-op check would
    silently DISCARD a genuine type change instead of persisting it.

    Loosening the numeric comparison for round-5 defect (2) had to keep
    this: ``bool`` is checked FIRST, before the numeric arm it would
    otherwise fall into.
    """
    from local_deep_research.web.routers.settings import (
        _resolve_container_secret_write,
        _values_equal,
    )

    assert not _values_equal(1, True)
    assert not _values_equal(True, 1)
    assert not _values_equal(0, False)
    assert not _values_equal(False, 0)
    assert not _values_equal(True, 1.0)
    assert not _values_equal(1.0, True)
    assert not _values_equal({"enabled": 1}, {"enabled": True})
    assert not _values_equal([{"enabled": 1}], [{"enabled": True}])
    # ... and a bool IS equal to the same bool.
    assert _values_equal(True, True)
    assert _values_equal({"enabled": False}, {"enabled": False})

    # End of the chain that actually matters: `{"enabled": 1}` over a
    # stored `{"enabled": True}` is a genuine EDIT, not a no-op, so the
    # merged container must be written rather than dropped.
    merged, is_noop, is_rejected = _resolve_container_secret_write(
        {"token": REDACTED, "enabled": 1},
        {"token": "s3cr3t-bool-arm", "enabled": True},
        REDACTED,
    )
    assert is_rejected is False
    assert is_noop is False, (
        "a bool -> int type change next to a restored secret was "
        "misreported as an untouched round-trip and would be discarded"
    )
    assert merged == {"token": "s3cr3t-bool-arm", "enabled": 1}


def test_text_ui_element_json_string_sentinel_bypass_is_closed():
    """Defect (2) in the post-#6201 review: the JSON-STRING encoding of a
    masked container must be caught by the guards even under a
    "text"/"textarea" ui_element, not only "json".

    ``_coerced_for_guard`` only promoted a JSON-string value to a dict/list
    when ``coerce_setting_for_write`` did -- but for "text"/"textarea" (and
    "select"/"password"), that function's target type IS ``str``, so it
    NEVER produces a dict/list. A masked container submitted in its
    JSON-string form under one of those ui_elements therefore stayed a
    plain string, fell to the outer-key-gated string arm (not sensitive
    here), and was accepted untouched -- silently overwriting the stored
    dict with the literal string, e.g. ``'{"stop_token":"[REDACTED]"}'``
    (silent DATA LOSS, the sibling defect to (1)'s disclosure).
    """
    import json

    from local_deep_research.web.routers.settings import (
        _coerced_for_guard,
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _reconciled_container_value_for_write,
    )

    key = "llm.probe.model_kwargs"
    stored = {"stop_token": "s3cr3t-real-4420", "page_token": "old-value"}

    for ui_element in ("text", "textarea", "select"):
        # A pure round-trip, submitted in JSON-STRING form.
        roundtrip_str = json.dumps(
            {"stop_token": REDACTED, "page_token": "old-value"}
        )
        guard_rt = _coerced_for_guard(key, ui_element, roundtrip_str)
        assert isinstance(guard_rt, dict), (
            f"{ui_element}: JSON-string container not decoded for the guards"
        )
        assert _is_secret_empty_noop(key, ui_element, guard_rt, stored), (
            f"{ui_element}: JSON-string round-trip not recognized as a no-op"
        )
        assert not _embeds_redaction_sentinel(key, ui_element, guard_rt, stored)

        # A genuine edit, also submitted in JSON-STRING form: must merge,
        # not overwrite the stored dict with the literal masked string.
        edit_str = json.dumps(
            {"stop_token": REDACTED, "page_token": "new-value"}
        )
        guard_edit = _coerced_for_guard(key, ui_element, edit_str)
        assert isinstance(guard_edit, dict), (
            f"{ui_element}: JSON-string edit not decoded for the guards"
        )
        assert not _is_secret_empty_noop(key, ui_element, guard_edit, stored)
        assert not _embeds_redaction_sentinel(
            key, ui_element, guard_edit, stored
        )
        merged, is_reconciled = _reconciled_container_value_for_write(
            edit_str, guard_edit, stored
        )
        assert is_reconciled is True
        assert merged == {
            "stop_token": "s3cr3t-real-4420",
            "page_token": "new-value",
        }, f"{ui_element}: secret was clobbered instead of restored"

    # Control: an ordinary, non-JSON string is unaffected (no accidental
    # decode, still a plain scalar write).
    assert (
        _coerced_for_guard("app.some_text", "text", "just a plain value")
        == "just a plain value"
    )


def test_list_of_dicts_permutation_does_not_rebind_secrets():
    """Defect (3) in the post-#6201 review: a same-length PERMUTATION of a
    list of dicts must not positionally rebind a restored secret to the
    WRONG entry.

    ``_merge_redacted_container`` walks a same-length list by ``zip()``
    position. Given stored ``[{url:a,api_key:K1},{url:b,api_key:K2}]``, a
    client that reorders the two entries and resubmits both with their
    ``api_key`` still masked would, before this fix, get K1 silently
    restored under ``b`` and K2 under ``a`` -- the WRONG secret bound to
    each url, not a rejection. The fix requires every visible (non-
    sentinel) field at an index to match the stored item at that same
    index before trusting the position; a permutation fails that check and
    is rejected instead.
    """
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _merge_redacted_container,
    )

    key = "search.engine.web.probe.instances"
    stored = [
        {"url": "host-a.example.com", "api_key": "K1-real-secret"},
        {"url": "host-b.example.com", "api_key": "K2-real-secret"},
    ]

    # Same length, entries SWAPPED, both api_key leaves masked.
    permuted = [
        {"url": "host-b.example.com", "api_key": REDACTED},
        {"url": "host-a.example.com", "api_key": REDACTED},
    ]
    merged, rejected = _merge_redacted_container(permuted, stored, REDACTED)
    assert rejected is True, (
        "a same-length permutation must be rejected, not silently "
        "resolved by position"
    )
    assert _embeds_redaction_sentinel(key, None, permuted, stored)

    # Had this NOT been rejected, position-based restoration would have
    # bound K1 (host-a's real secret) under host-b and vice versa -- the
    # wrong secret under the wrong url. Not asserted directly (the write
    # is rejected, so `merged` is never persisted); this sanity check just
    # confirms the rejection isn't accidentally a no-op in disguise.
    assert merged != stored

    # CONTROL: an in-place round-trip (same positions, same urls) still
    # resolves correctly -- the fix must not break the ordinary case.
    in_place = [
        {"url": "host-a.example.com", "api_key": REDACTED},
        {"url": "host-b.example.com", "api_key": REDACTED},
    ]
    merged_ok, rejected_ok = _merge_redacted_container(
        in_place, stored, REDACTED
    )
    assert rejected_ok is False
    assert merged_ok == stored


def test_list_of_dicts_rename_or_drop_field_does_not_rebind_secrets():
    """Follow-up to defect (3): the straight-swap permutation above is
    caught, but ``_non_sentinel_fields_match``'s dict branch only walked
    the SUBMITTED item's keys and skipped any key absent from the stored
    item -- it never required the stored item's visible keys to all be
    present in the submission. That means a client can bypass the
    permutation guard without ever "matching" on a renamed field: RENAME
    the visible field (old key vanishes, new key is skipped because it
    isn't in ``stored_value``), DROP it entirely (same effect), or do it
    in two separate writes that individually only ever touch known field
    names. Each shape below still rebinds K1 <-> K2 to the wrong url if
    the key-equality check in ``_non_sentinel_fields_match`` is removed
    (i.e. reverting to ``if sub_key not in stored_value: continue``).
    """
    from local_deep_research.web.routers.settings import (
        _merge_redacted_container,
    )

    stored = [
        {"url": "host-a.example.com", "api_key": "K1-real-secret"},
        {"url": "host-b.example.com", "api_key": "K2-real-secret"},
    ]

    # --- Shape 1: RENAME the visible field (url -> endpoint) -------------
    # The old key "url" disappears from the submission and the new key
    # "endpoint" isn't in `stored_value`, so a subset-only key walk finds
    # nothing to disprove the position and treats this as an in-place
    # edit -- letting the swap through under a different field name.
    renamed = [
        {"endpoint": "host-b.example.com", "api_key": REDACTED},
        {"endpoint": "host-a.example.com", "api_key": REDACTED},
    ]
    merged, rejected = _merge_redacted_container(renamed, stored, REDACTED)
    assert rejected is True, (
        "renaming the visible field must not bypass the permutation guard"
    )
    assert merged != [
        {"endpoint": "host-b.example.com", "api_key": "K1-real-secret"},
        {"endpoint": "host-a.example.com", "api_key": "K2-real-secret"},
    ], "the wrong secret must not be rebound to the wrong endpoint"

    # --- Shape 2: DROP the visible field entirely -------------------------
    # No renamed field at all -- just the masked secret, swapped in
    # position. Same bypass: the visible key set no longer matches, so
    # there is nothing left for a subset-only walk to compare.
    dropped = [{"api_key": REDACTED}, {"api_key": REDACTED}]
    merged_dropped, rejected_dropped = _merge_redacted_container(
        dropped, stored, REDACTED
    )
    assert rejected_dropped is True, (
        "dropping the visible field must not bypass the permutation guard "
        "(this is also exactly the setup for the two-step bypass below)"
    )

    # --- Shape 3: the same bypass split into two separate writes, using
    # only unmodified field names at every step (no renaming needed) -----
    # Step 1: submit both items with ONLY api_key visible (masked). If
    # this is wrongly accepted as a no-op-shaped edit, the stored rows
    # lose their "url" field entirely and become indistinguishable by
    # position.
    step1 = [{"api_key": REDACTED}, {"api_key": REDACTED}]
    merged1, rejected1 = _merge_redacted_container(step1, stored, REDACTED)
    assert rejected1 is True, (
        "step 1 of the two-step bypass (submitting only the secret field) "
        "must be rejected -- it would otherwise silently drop 'url' and "
        "set up step 2 to rebind the wrong secret"
    )
    # Even if step 1 were (wrongly) accepted, step 2 -- reintroducing
    # "url" with the entries swapped -- must independently be rejected
    # against whatever step 1 actually persisted. Simulate the worst case
    # where step 1's merged value (dropping "url") became the new stored
    # state, and confirm step 2 is still caught.
    worst_case_stored_after_step1 = [
        {"api_key": "K1-real-secret"},
        {"api_key": "K2-real-secret"},
    ]
    step2 = [
        {"api_key": REDACTED, "url": "host-b.example.com"},
        {"api_key": REDACTED, "url": "host-a.example.com"},
    ]
    merged2, rejected2 = _merge_redacted_container(
        step2, worst_case_stored_after_step1, REDACTED
    )
    assert rejected2 is True, (
        "step 2 of the two-step bypass (reintroducing 'url' on the swapped "
        "entries) must be rejected -- otherwise the wrong secret is bound "
        "to the wrong url across two otherwise-unremarkable writes"
    )
    assert merged2 != [
        {"api_key": "K1-real-secret", "url": "host-b.example.com"},
        {"api_key": "K2-real-secret", "url": "host-a.example.com"},
    ]

    # CONTROL: a genuine in-place edit that keeps the exact same key set
    # at each index must still be accepted -- the key-equality check must
    # not over-reject ordinary edits.
    in_place_edit = [
        {"url": "host-a.example.com", "api_key": REDACTED},
        {"url": "host-b.example.com", "api_key": REDACTED},
    ]
    merged_ok, rejected_ok = _merge_redacted_container(
        in_place_edit, stored, REDACTED
    )
    assert rejected_ok is False
    assert merged_ok == stored


def test_single_element_list_sibling_edit_is_not_rejected():
    """Recordable (5) in the post-#6201 review: the list branch's sibling-
    match gate exists only to catch a same-length REORDER (see
    ``test_list_of_dicts_permutation_does_not_rebind_secrets``), but it
    was applied unconditionally, including to a single-element list.
    A list of one item has no other position for that item to have been
    reordered from or to, so an ordinary sibling edit next to a masked
    secret in a length-1 list must merge like the single-object dict case
    does, not be rejected as if it were indistinguishable from a reorder.
    """
    from local_deep_research.web.routers.settings import (
        _merge_redacted_container,
    )

    stored = [{"url": "host-a.example.com", "api_key": "K1-real-secret"}]

    # Sibling field edited, the only secret leaf still masked.
    edited = [{"url": "host-a-renamed.example.com", "api_key": REDACTED}]
    merged, rejected = _merge_redacted_container(edited, stored, REDACTED)
    assert rejected is False, (
        "a sibling edit in a single-element list has no reorder to be "
        "confused with and must not be rejected"
    )
    assert merged == [
        {"url": "host-a-renamed.example.com", "api_key": "K1-real-secret"}
    ]

    # CONTROL: a genuine pure round-trip (no edit) still resolves the
    # secret and is a no-op.
    roundtrip = [{"url": "host-a.example.com", "api_key": REDACTED}]
    merged_rt, rejected_rt = _merge_redacted_container(
        roundtrip, stored, REDACTED
    )
    assert rejected_rt is False
    assert merged_rt == stored


def test_container_no_op_check_is_type_aware():
    """Defect (5) in the post-#6201 review: the merged-vs-stored no-op
    comparison must be type-aware. Plain ``==`` treats ``1 == True`` as
    equal, so ``{"enabled": 1}`` submitted over a stored
    ``{"enabled": True, ...}`` would look byte-for-byte identical and the
    genuine type-changing edit would be silently discarded as a no-op.
    """
    from local_deep_research.web.routers.settings import (
        _is_secret_empty_noop,
        _reconciled_container_value_for_write,
    )

    key = "llm.probe.model_kwargs"
    stored = {"enabled": True, "api_key": "s3cr3t-real-type-aware"}
    type_changed = {"enabled": 1, "api_key": REDACTED}

    assert not _is_secret_empty_noop(key, None, type_changed, stored), (
        "1 vs True must not be treated as an unchanged value"
    )
    merged, is_reconciled = _reconciled_container_value_for_write(
        type_changed, type_changed, stored
    )
    assert is_reconciled is True
    assert merged == {"enabled": 1, "api_key": "s3cr3t-real-type-aware"}
    assert type(merged["enabled"]) is int

    # CONTROL: a true round-trip (identical types) is still a no-op.
    true_roundtrip = {"enabled": True, "api_key": REDACTED}
    assert _is_secret_empty_noop(key, None, true_roundtrip, stored)


def test_sensitive_multiselect_bare_sentinel_roundtrip_stays_a_noop():
    """Defect (6) in the post-#6201 review: a bare "[REDACTED]" string
    round-tripped for a sensitive ``multiselect`` setting must stay a
    200 no-op regardless of the stored list's length.

    ``redact_value`` masks an EXACT-match sensitive setting (e.g. a key
    literally named ``access_token``) to the bare sentinel STRING even
    when its real value is a list -- the exact/password arm returns
    ``redaction_text`` outright for any non-empty value, container or not.
    A client that GETs and saves that field back untouched resubmits the
    bare string. ``_coerced_for_guard`` used to run it through
    ``coerce_setting_for_write("multiselect", ...)``, whose
    ``_parse_multiselect`` comma-separated fallback wraps an un-quoted,
    comma-less string into a ONE-ELEMENT LIST (``["[REDACTED]"]``) -- which
    then looks like a malformed/rejected container write (a length
    mismatch) against any stored list whose length isn't 1, turning what
    used to be an idempotent 200 into a 400.
    """
    from local_deep_research.web.routers.settings import (
        _coerced_for_guard,
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        coerce_setting_for_write,
    )

    key = "llm.probe.access_token"  # exact-match sensitive leaf
    stored = ["tok-real-1", "tok-real-2", "tok-real-3"]  # length != 1

    guard_value = _coerced_for_guard(key, "multiselect", REDACTED)
    assert guard_value == REDACTED, (
        "the bare sentinel must not be mangled into a list before the "
        "guards see it"
    )
    assert _is_secret_empty_noop(key, "multiselect", guard_value, stored)
    assert not _embeds_redaction_sentinel(
        key, "multiselect", guard_value, stored
    )

    # Strict parsing rejects the JSON-looking bare sentinel rather than
    # interpreting it as CSV. Write coercion preserves rejected input for
    # validation, so both paths must keep this no-op scalar unchanged.
    raw_coerced = coerce_setting_for_write(
        key=key, value=REDACTED, ui_element="multiselect"
    )
    assert raw_coerced == REDACTED
    assert _is_secret_empty_noop(key, "multiselect", raw_coerced, stored)

    # The historical list-shaped input is still a different operation: it
    # must not become a no-op over a three-element stored list.
    assert not _is_secret_empty_noop(key, "multiselect", [REDACTED], stored)


def test_policy_audit_line_cannot_carry_a_credential():
    """The ``policy.*`` audit line records old/new for FREE-FORM JSON keys,
    so it must not interpolate their values in plaintext.

    Round 6 left ``SettingsManager.set_setting``'s audit line logging
    ``old={}``/``new={}`` raw, on the stated grounds that its scope is
    "``policy.*`` plus three keys holding booleans and a hostname list".
    That is not what the code does. ``_is_policy_setting`` PREFIX-matches
    ``policy.`` rather than consulting an allowlist, and three keys already
    in scope ship as free-form editable JSON with no item schema:
    ``policy.trusted_search_engines``,
    ``policy.trusted_inference_providers`` and
    ``llm.allowed_local_hostnames`` (all ``ui_element="json"``,
    ``editable=True``, default ``[]``). Their egress validator
    ``continue``s past non-``str`` entries, so a list of dicts carrying an
    API key is accepted, reaches ``set_setting``, and used to be written to
    the log in plaintext at WARNING -- a value-logging path that never went
    through ``get_typed_setting_value``, which is what round 6's
    root-cause framing assumed covered every such path.

    The audit line still records old/new; it records the value's SHAPE
    (type, length/entry count), never its content, and never a
    deterministic fingerprint of its content (#6201 rounds 7-8).
    """
    from local_deep_research.settings.manager import (
        _is_policy_setting,
        _policy_audit_allowed_values,
        _policy_audit_value,
    )

    # The scope really is a prefix, not an allowlist.
    assert _is_policy_setting("policy.trusted_search_engines")
    assert _is_policy_setting("policy.some_future_key")
    assert _is_policy_setting("llm.allowed_local_hostnames")
    assert not _is_policy_setting("llm.temperature")

    secret = "sk-live-must-not-reach-the-log"
    leaky_list = [{"name": "x", "api_key": secret}]
    rendered = _policy_audit_value(leaky_list)
    assert secret not in rendered
    assert "api_key" not in rendered
    assert rendered == "<list[1]>"

    # A free-form STRING value is de-valued the same way -- a
    # ``policy.*`` key created through the API has no fixed vocabulary.
    assert secret not in _policy_audit_value(secret)
    assert _policy_audit_value(secret) == f"<str[{len(secret)}]>"

    # NO DICTIONARY ORACLE (round 8): the rendering carries structural
    # metadata only -- type, length for a string, entry count for a
    # container -- and NO token derived from the value's content. An
    # earlier version logged a truncated SHA-256 of the canonical JSON,
    # and an offline candidate list recovered a common password from
    # that prefix (probe: pr-6201-policy-digest-probe.txt). Two distinct
    # same-shape values must therefore render IDENTICALLY: that is
    # exactly the property that makes the line useless for confirming a
    # guessed credential.
    assert "password123" not in _policy_audit_value("password123")
    assert _policy_audit_value("password123") == _policy_audit_value(
        "swordfish42"
    )
    assert _policy_audit_value("password123") == "<str[11]>"
    assert _policy_audit_value(leaky_list) == _policy_audit_value(
        [{"api_key": secret, "name": "x"}]
    )
    # Different CONTENT, same shape: indistinguishable in the log. The
    # coarse "did it change" signal survives at the shape level only
    # (see the count assertion below) -- the tradeoff that kills the
    # oracle.
    assert _policy_audit_value(leaky_list) == _policy_audit_value(
        [{"name": "x", "api_key": "sk-live-something-else"}]
    )
    assert _policy_audit_value(leaky_list) != _policy_audit_value(
        [
            {"name": "x", "api_key": "sk-live-something-else"},
            {"name": "y", "api_key": "sk-live-another-one"},
        ]
    )

    # Plaintext is KEPT where it is proven safe by a check, not a comment:
    # values with no string content at all, and a string drawn from the
    # key's own shipped ``select`` vocabulary (``policy.egress_scope``).
    assert _policy_audit_value(True) == "True"
    assert _policy_audit_value(False) == "False"
    assert _policy_audit_value(None) == "None"
    assert _policy_audit_value(7) == "7"

    scope_options = _policy_audit_allowed_values(
        {
            "ui_element": "select",
            "options": [{"value": "adaptive"}, {"value": "strict"}],
        }
    )
    assert scope_options == ["adaptive", "strict"]
    assert _policy_audit_value("adaptive", scope_options) == "'adaptive'"
    # ...but a string that is NOT in the vocabulary is still de-valued,
    # so a rogue value cannot ride in on a select-typed key.
    assert secret not in _policy_audit_value(secret, scope_options)

    # A ``json`` row has no vocabulary to vouch for anything.
    assert (
        _policy_audit_allowed_values({"ui_element": "json", "options": None})
        is None
    )
    assert _policy_audit_allowed_values(None) is None


def test_shipped_policy_audit_keys_include_free_form_json_containers():
    """Pins the premise of the test above against the shipped defaults: the
    ``policy.*`` audit scope is not "booleans and a hostname list"."""
    import json as _json
    from pathlib import Path

    import local_deep_research.defaults as _defaults
    from local_deep_research.settings.manager import _is_policy_setting

    defaults_path = Path(_defaults.__file__).parent / "default_settings.json"
    shipped = _json.loads(defaults_path.read_text(encoding="utf-8"))

    free_form = {
        key: meta
        for key, meta in shipped.items()
        if _is_policy_setting(key)
        and meta.get("ui_element") == "json"
        and meta.get("editable") is True
    }
    assert set(free_form) == {
        "policy.trusted_search_engines",
        "policy.trusted_inference_providers",
        "llm.allowed_local_hostnames",
    }, (
        "the policy audit scope's free-form JSON keys changed; re-check "
        "_policy_audit_value's rationale"
    )


def test_set_setting_policy_audit_sink_never_logs_the_values():
    """The ``set_setting`` call site -- not just the helper -- must render
    old/new through ``_policy_audit_value`` (round 8 / B2: the helper-only
    tests above passed even with the call site reverted to raw old/new).

    Drives the REAL sink: a ``SettingsManager`` on an in-memory SQLite
    session, two ``set_setting`` calls on a ``policy.*`` key -- the
    second one changes a stored secret-bearing list, so the audit line
    has both an ``old=`` and a ``new=`` that carry distinct secrets --
    and the second call uses ``commit=False`` to pin the PR's own claim
    that the audit line fires on the bulk (``commit=False``) routes too.
    The key is deliberately NOT in the shipped defaults, so the
    ``default_settings.get(key)`` lookup at the call site finds no
    metadata and no ``select`` vocabulary can vouch for anything.

    EXACT REVERT THIS CATCHES: in ``SettingsManager.set_setting``,
    replacing the two ``_policy_audit_value(old_value/_audit_options)`` /
    ``_policy_audit_value(value/_audit_options)`` arguments of the
    ``logger.bind(policy_audit=True).warning(...)`` call with the raw
    ``old_value`` / ``value`` -- the repr of both secret-bearing lists
    (sub-key names included) then reaches the captured log and both the
    non-presence and the rendering assertions below fail.

    The POSITIVE CONTROL is not optional: the package disables its own
    loguru namespace at import, so "no secret in the captured text"
    passes with nothing captured at all. The control line is the audit
    WARNING itself, which must name the key.
    """
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    from local_deep_research.database.models import Base
    from local_deep_research.settings.manager import SettingsManager

    key = "policy.probe_audit_sink_round8"
    old_secret = "sk-live-old-must-not-reach-the-log-8841"
    new_secret = "sk-live-new-must-not-reach-the-log-0417"

    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    try:
        manager = SettingsManager(db_session=session)
        assert manager.set_setting(key, [{"name": "x", "api_key": old_secret}])

        with _captured_audit_warnings() as lines:
            # commit=False: the bulk-save shape. The audit line sits
            # outside ``if commit:`` and must still fire.
            assert manager.set_setting(
                key, [{"name": "x", "api_key": new_secret}], commit=False
            )

        # POSITIVE CONTROL: the audit line was emitted, reached the
        # sink, and names the key.
        audit_lines = [ln for ln in lines if "policy setting changed" in ln]
        assert any(key in ln for ln in audit_lines), (
            "log capture is not wired up, or the policy audit line no "
            f"longer fires on the commit=False route (captured {lines!r})"
        )

        # Neither the old nor the new secret -- nor the container's
        # sub-key names -- may appear anywhere in the captured text.
        for forbidden in (old_secret, new_secret, "api_key"):
            leaked = [ln for ln in lines if forbidden in ln]
            assert not leaked, (
                f"{forbidden!r} reached the log: {leaked[0][:300]}"
            )

        # Machine-observable rendering pin: old and new are structural
        # only. A same-length string with different content, or a
        # same-shape list, is indistinguishable here -- that is the
        # anti-oracle property itself.
        the_line = next(ln for ln in audit_lines if key in ln)
        assert "old=<list[1]> new=<list[1]>" in the_line, the_line[:300]
    finally:
        session.close()
        engine.dispose()


@contextmanager
def _captured_audit_warnings():
    """Collect every rendered WARNING-or-worse loguru line emitted inside
    the block.

    The local twin of ``_captured_warnings`` in
    ``test_settings_egress_and_secrets_fastapi.py``: the package calls
    ``logger.disable("local_deep_research")`` at import, so without the
    explicit enable the audit line never reaches any sink and a
    "nothing leaked" assertion would pass vacuously. Same
    enable/add/remove/disable idiom; ``disable`` in the ``finally``
    restores the package's own default state.
    """
    from loguru import logger

    rendered: list[str] = []

    def _sink(message):
        rendered.append(str(message))

    logger.enable("local_deep_research")
    sink_id = logger.add(_sink, level="WARNING", diagnose=False)
    try:
        yield rendered
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")


def test_sensitive_multiselect_spliced_sentinel_is_rejected_not_merged():
    """A SPLICED sentinel on a sensitive ``multiselect`` must be a hard
    400, not a 200 that silently destroys the stored items.

    ``_coerced_for_guard`` special-cased only the BARE sentinel. A spliced
    one ("[REDACTED],new" -- what a stale client that rendered the mask and
    had a value appended submits) still went through
    ``coerce_setting_for_write(..., "multiselect")`` ->
    ``_parse_multiselect``, whose comma-split fallback fires because the
    string isn't valid JSON. That turns the corrupted SCALAR into
    ``["[REDACTED]", "new"]``, a list whose first item is now an EXACT
    sentinel match -- so the container arm merges it positionally against
    the stored list and persists ``["A", "new"]`` with a 200, DROPPING the
    stored ``"B"`` the client never resent. The "contains the sentinel but
    is not equal to it => hard 400" rule that
    ``_merge_redacted_container``'s own docstring states (and that the
    scalar arm applies to every other ui_element) never fired, because the
    split manufactured the exact match out of a corrupted edit. Silent
    data loss on a sensitive setting, and length-dependent: three items
    submitted against two stored happened to 400 on the length check,
    which is why it hid (#6201 round 7).
    """
    from local_deep_research.web.routers.settings import (
        _coerced_for_guard,
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _resolve_container_secret_write,
        coerce_setting_for_write,
    )

    key = "llm.probe.access_token"  # exact-match sensitive leaf
    stored = ["tok-real-1", "tok-real-2"]  # same length as the split

    for spliced in (f"{REDACTED},new", f"new,{REDACTED}"):
        guard_value = _coerced_for_guard(key, "multiselect", spliced)
        assert guard_value == spliced, (
            "a spliced sentinel must reach the guards AS A STRING; "
            "splitting it on commas manufactures an exact-match list item"
        )
        assert not _is_secret_empty_noop(
            key, "multiselect", guard_value, stored
        )
        assert _embeds_redaction_sentinel(
            key, "multiselect", guard_value, stored
        )

    # Strict parsing rejects JSON-looking input, so a leading sentinel
    # is no longer split even by raw coercion. A trailing sentinel still
    # looks like ordinary supported CSV: bypassing the guard really does
    # split it and silently replace the first stored item. Keep that real
    # negative control rather than relying on the old parser's fallback.
    assert (
        coerce_setting_for_write(
            key=key, value=f"{REDACTED},new", ui_element="multiselect"
        )
        == f"{REDACTED},new"
    )
    raw_coerced = coerce_setting_for_write(
        key=key, value=f"new,{REDACTED}", ui_element="multiselect"
    )
    assert raw_coerced == ["new", REDACTED]
    merged, is_noop, is_rejected = _resolve_container_secret_write(
        raw_coerced, stored, REDACTED
    )
    assert (is_noop, is_rejected) == (False, False)
    assert merged == ["new", "tok-real-2"]  # "tok-real-1" destroyed

    # CONTROL: the BARE sentinel round-trip stays the 200 no-op the
    # sibling test above pins -- this narrowing must not turn it into a
    # 400, and an ordinary multiselect edit must still coerce to a list.
    bare = _coerced_for_guard(key, "multiselect", REDACTED)
    assert bare == REDACTED
    assert _is_secret_empty_noop(key, "multiselect", bare, stored)
    assert not _embeds_redaction_sentinel(key, "multiselect", bare, stored)
    assert _coerced_for_guard(key, "multiselect", "alpha,beta") == [
        "alpha",
        "beta",
    ]


def test_depth_cap_rejection_has_its_own_message_not_the_sentinel_one():
    """A payload nested past ``_MAX_CONTAINER_DEPTH`` is rejected -- it
    can't be proven free of the sentinel that deep -- but when it
    genuinely contains none, the 400 must say so is nested too deeply, not
    claim it "contains the redaction placeholder" (which
    ``_redaction_sentinel_error`` -- reused unconditionally for every
    dict/list rejection before this fix -- would say regardless of
    whether a sentinel is anywhere in it).
    """
    from local_deep_research.web.routers.settings import (
        _MAX_CONTAINER_DEPTH,
        _container_rejection_error,
        _embeds_redaction_sentinel,
        _redaction_sentinel_error,
    )

    def nest(n, leaf="ordinary-value"):
        value = leaf
        for _ in range(n):
            value = {"k": value}
        return value

    too_deep_no_sentinel = nest(_MAX_CONTAINER_DEPTH + 2)
    stored = nest(_MAX_CONTAINER_DEPTH + 2)

    # Still rejected -- can't be proven safe that deep -- but for a
    # different reason than the sentinel case.
    assert _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, too_deep_no_sentinel, stored
    )
    message = _container_rejection_error(None, too_deep_no_sentinel)
    assert "nested too deeply" in message
    assert DataSanitizer.REDACTION_TEXT not in message
    assert message != _redaction_sentinel_error(None)

    # A shallow, ordinary sentinel-bearing container keeps the usual
    # message.
    ordinary_reject_message = _container_rejection_error(
        None, {"token": REDACTED, "port": 19531}
    )
    assert ordinary_reject_message == _redaction_sentinel_error(None)


def test_depth_cap_covers_the_list_positional_gate_not_just_the_dict_branch():
    """The depth cap must hold on the LIST branch too, where an uncapped
    walker used to reach a real ``RecursionError`` -> HTTP 500.

    ``_merge_redacted_container``'s dict branch caps itself, so a plain
    over-deep dict has always been rejected cleanly (its sibling test
    above pins that). Its LIST branch is different: before asking
    ``_merge_redacted_container`` to recurse, it consults
    ``_non_sentinel_fields_match`` -- the positional-rebinding gate -- and
    that helper (plus ``_values_equal``, which it bottoms out in) were the
    last two in the merge path with NO depth parameter and no cap, and the
    list branch called the gate with no depth argument at all. Nothing
    stops the gate being reached for a pathological item either:
    ``_container_embeds_sentinel`` answers True unconditionally past its
    own cap, so the gate is consulted for EVERY too-deep list item, and no
    sentinel needs to be present anywhere for that to happen.

    A ~7 KB list whose first item nested past the interpreter's recursion
    limit against a stored list of the same shape therefore raised an
    uncaught ``RecursionError``, which the write routes' broad
    ``except Exception`` turned into an opaque 500 -- falsifying both
    ``_MAX_CONTAINER_DEPTH``'s own comment ("instead of surfacing as an
    opaque 500") and this feature's changelog claim of a clean 400. It is
    also a regression against the pre-#6201 behaviour, where the container
    arm sat BEHIND ``is_sensitive_setting`` and a non-sensitive key like
    ``search.favorites`` never reached these helpers at all; moving the
    container arm in front of that gate exposed the uncapped walk to every
    dict/list write (#6201 round 7).

    Revert either cap (drop the ``_depth`` parameter/guard from
    ``_non_sentinel_fields_match`` or ``_values_equal``, or stop threading
    ``_depth + 1`` from the list branch) and the first assertion below
    raises ``RecursionError`` instead of returning a bool.
    """
    import sys

    from local_deep_research.web.routers.settings import (
        _container_rejection_error,
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _non_sentinel_fields_match,
        _values_equal,
    )

    def nest(n, leaf):
        value = leaf
        for _ in range(n):
            value = {"k": value}
        return value

    # Deeper than the interpreter can recurse, so an uncapped walker
    # cannot answer at all -- while ``json.loads`` happily parses this
    # from a request body (it does not consume a Python frame per level)
    # and the request-size middleware only caps BYTES, of which this is a
    # few kilobytes.
    depth = sys.getrecursionlimit() + 500
    submitted = [nest(depth, REDACTED), {"ordinary": 1}]
    stored = [nest(depth, "real-secret"), {"ordinary": 1}]

    # Both write guards must ANSWER -- a clean 400 -- rather than raise.
    assert (
        _is_secret_empty_noop(BROADENED_ARM_KEY, "json", submitted, stored)
        is False
    )
    assert (
        _embeds_redaction_sentinel(BROADENED_ARM_KEY, "json", submitted, stored)
        is True
    )
    assert "nested too deeply" in _container_rejection_error("json", submitted)

    # The two helpers the list branch reaches must terminate on their own,
    # fail-closed: "cannot prove this position is unchanged" / "cannot
    # prove these are equal", never a stack overflow.
    assert (
        _non_sentinel_fields_match(
            nest(depth, REDACTED), nest(depth, "real-secret"), REDACTED
        )
        is False
    )
    assert _values_equal(nest(depth, "same"), nest(depth, "same")) is False

    # CONTROL: the cap must not have swallowed ordinary-depth lists --
    # a normal positional round-trip is still recognised as a no-op.
    shallow = [
        {"url": "a", "api_key": REDACTED},
        {"url": "b", "api_key": REDACTED},
    ]
    shallow_stored = [
        {"url": "a", "api_key": "K1-real"},
        {"url": "b", "api_key": "K2-real"},
    ]
    assert _is_secret_empty_noop(
        BROADENED_ARM_KEY, "json", shallow, shallow_stored
    )
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, "json", shallow, shallow_stored
    )


def test_container_boolean_edit_is_saved_and_browser_numbers_round_trip():
    """Keep the boolean/integer distinction while accepting JSON number normalization."""
    from local_deep_research.web.routers.settings import (
        _embeds_redaction_sentinel,
        _is_secret_empty_noop,
        _reconciled_container_value_for_write,
    )

    stored = {"token": "s", "mode": 1}
    bool_for_int = {"token": REDACTED, "mode": True}
    assert not _is_secret_empty_noop(
        BROADENED_ARM_KEY, None, bool_for_int, stored
    )
    assert not _embeds_redaction_sentinel(
        BROADENED_ARM_KEY, None, bool_for_int, stored
    )
    merged, reconciled = _reconciled_container_value_for_write(
        bool_for_int, bool_for_int, stored
    )
    assert reconciled
    assert merged["mode"] is True
    assert merged["token"] == "s"

    # JSON.stringify cannot preserve the Python int/float distinction.
    # These equivalent numeric values are an unchanged browser round-trip.
    for mode in (1, 1.0):
        untouched = {"token": REDACTED, "mode": mode}
        assert _is_secret_empty_noop(BROADENED_ARM_KEY, None, untouched, stored)
        assert not _embeds_redaction_sentinel(
            BROADENED_ARM_KEY, None, untouched, stored
        )


def test_container_sentinel_on_create_has_no_noop_exemption():
    """Creation has no prior stored value, so unlike the update path, even
    the pure round-trip shape (every leaf exactly the sentinel) cannot mean
    "keep what's there" -- it is rejected too."""
    from local_deep_research.web.routers.settings import (
        _embeds_sentinel_on_create,
    )

    roundtrip = {"uri": REDACTED, "token": REDACTED, "port": 19530}
    spliced = {"uri": REDACTED, "token": f"pre{REDACTED}post"}
    fresh = {"uri": "mysql://real", "token": "s3cr3t-real"}

    assert _embeds_sentinel_on_create(BROADENED_ARM_KEY, None, roundtrip)
    assert _embeds_sentinel_on_create(BROADENED_ARM_KEY, None, spliced)
    assert not _embeds_sentinel_on_create(BROADENED_ARM_KEY, None, fresh)


def test_sentinel_error_matches_the_clear_semantics_per_ui_element():
    """The 400 message must not advertise an escape that cannot work.

    ``_is_secret_empty_noop`` swallows an empty write to a ``password``
    input, so "submit an empty value to clear it" is only true for the
    non-password sensitive settings (the ``notifications.service_url``
    textarea). ``_embeds_redaction_sentinel`` keys off sensitivity alone,
    so a password setting reaches this error via a direct API call and must
    be told how it actually clears.

    GENUINE LOSS (#5947, fix in #5956): neither ``_is_secret_empty_noop``
    nor ``_redaction_sentinel_error`` exist in the ported
    ``web/routers/settings.py`` -- this import fails with an ImportError.
    Left failing on purpose; do not skip/xfail.
    """
    from local_deep_research.web.routers.settings import (
        _is_secret_empty_noop,
        _redaction_sentinel_error,
    )

    clearable = _redaction_sentinel_error("textarea")
    assert not _is_secret_empty_noop(WEBHOOK_KEY, "textarea", "")
    assert "submit an empty value to clear it" in clearable
    assert REDACTED in clearable

    password = _redaction_sentinel_error("password")
    assert _is_secret_empty_noop("llm.openai.api_key", "password", "")
    assert "empty value" not in password
    assert "environment variable" in password
    assert REDACTED in password

    # An unknown ui_element keeps the general advice both cases share.
    for message in (clearable, password, _redaction_sentinel_error(None)):
        assert "retype the whole value" in message


@pytest.mark.parametrize("corrupt", CORRUPT_VALUES)
def test_put_rejects_embedded_sentinel(logged_in_client, monkeypatch, corrupt):
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    response = c.put(f"/settings/api/{WEBHOOK_KEY}", json={"value": corrupt})

    assert response.status_code == 400, response.get_data(as_text=True)
    assert REDACTED in response.get_json()["error"]
    assert _stored_url(c, monkeypatch) == SECRET_URL


@pytest.mark.parametrize("corrupt", CORRUPT_VALUES)
def test_save_all_settings_rejects_embedded_sentinel(
    logged_in_client, monkeypatch, corrupt
):
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    response = c.post(
        "/settings/save_all_settings", json={WEBHOOK_KEY: corrupt}
    )

    assert response.status_code == 400, response.get_data(as_text=True)
    payload = response.get_json()
    assert payload["status"] == "error"
    assert [e["key"] for e in payload["errors"]] == [WEBHOOK_KEY]
    assert REDACTED in payload["errors"][0]["error"]
    assert _stored_url(c, monkeypatch) == SECRET_URL


@pytest.mark.parametrize("corrupt", CORRUPT_VALUES)
def test_save_settings_form_post_rejects_embedded_sentinel(
    logged_in_client, monkeypatch, corrupt
):
    """The JS-disabled form fallback must agree with the JSON routes, or it
    becomes the way around the guard."""
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    # follow_redirects=False: httpx's TestClient (unlike Flask's) follows
    # redirects by default, which would silently swap this 302 for the
    # 200 of the page it redirects to.
    response = c.post(
        "/settings/save_settings",
        data={WEBHOOK_KEY: corrupt},
        follow_redirects=False,
    )

    assert response.status_code == 302
    assert _stored_url(c, monkeypatch) == SECRET_URL


def test_recreated_setting_rejects_sentinel_entirely(logged_in_client):
    """On the DELETE-then-PUT recreate path there is no stored value, so the
    sentinel cannot mean "keep what is there" -- the exact match is a
    corrupted value too, and storing it would make "[REDACTED]" the webhook."""
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)
    delete = c.delete(f"/settings/api/{WEBHOOK_KEY}")
    assert delete.status_code == 200, delete.get_data(as_text=True)

    for corrupt in (*CORRUPT_VALUES, REDACTED):
        response = c.put(
            f"/settings/api/{WEBHOOK_KEY}", json={"value": corrupt}
        )
        assert response.status_code == 400, (
            corrupt,
            response.get_data(as_text=True),
        )
        assert REDACTED in response.get_json()["error"]

    # A real value still recreates the setting (201, not the 200 an update
    # returns), so the guard rejects only the corrupted values.
    recreate = c.put(
        f"/settings/api/{WEBHOOK_KEY}", json={"value": ROTATED_SECRET_URL}
    )
    assert recreate.status_code == 201, recreate.get_data(as_text=True)
    assert _single(c, WEBHOOK_KEY) == REDACTED


def test_exact_sentinel_stays_a_noop_not_an_error(logged_in_client):
    """Regression fence for the containment guard: the untouched-field
    round-trip must keep its idempotent 200, not become a 400."""
    c = logged_in_client
    _put(c, WEBHOOK_KEY, SECRET_URL)

    put = c.put(f"/settings/api/{WEBHOOK_KEY}", json={"value": REDACTED})
    assert put.status_code == 200
    assert "unchanged" in put.get_json()["message"].lower()

    save_all = c.post(
        "/settings/save_all_settings", json={WEBHOOK_KEY: REDACTED}
    )
    assert save_all.status_code == 200


def test_save_all_create_rejects_sentinel_for_plain_sensitive_leaf(
    logged_in_client,
):
    """save-all CREATE of a sensitive leaf with no DB row yet (a plain
    value, not a container) must reject the sentinel too: the update
    path's exact-match no-op has nothing to keep, so an accepted write
    would store "[REDACTED]" as the credential (#5947). The single-PUT
    recreate path is pinned above (test_recreated_setting_rejects_
    sentinel_entirely); this pins the save-all batch create path, whose
    ui_element is not yet known so sensitivity is decided by the leaf
    name alone."""
    c = logged_in_client
    key = "notifications.round3_create_guard.api_key"

    response = c.post("/settings/save_all_settings", json={key: REDACTED})

    assert response.status_code == 400, response.get_data(as_text=True)
    payload = response.get_json()
    assert payload["status"] == "error"
    assert [e["key"] for e in payload["errors"]] == [key]
    assert REDACTED in payload["errors"][0]["error"]

    # Not persisted: no row was created, so the follow-up GET 404s.
    assert c.get(f"/settings/api/{key}").status_code == 404