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.

19.1 KB
Raw Blame
"""Regression tests for the dict/set-shaped-value blind spot in
``_log_value_preview`` (``web/routers/notes.py``).

Follow-up to #5497 (merged b7e0ac4a049a), which replaced the naive
``repr(v)[:100]`` audit-log preview with a ``reprlib.Repr``-based helper to
avoid materializing a full repr of an up-to-~100MB attacker-controlled value
before slicing it. That fix covers strings and lists/tuples, whose
``reprlib`` methods slice BEFORE recursing. It does NOT cover dicts, sets,
or frozensets: CPython's ``reprlib.Repr.repr_dict`` / ``repr_set`` /
``repr_frozenset`` call ``reprlib._possibly_sorted()`` (a plain
``sorted(x)``) over the ENTIRE container before slicing to
``maxdict``/``maxset``/``maxfrozenset`` — so a large dict/set reaching the
helper (e.g. ``{"is_collapsed": {<N keys>}}`` on
``PATCH /api/notes/<id>/research/<id>``) still costs O(N log N) time and
touches every element, reintroducing the exact class of cost #5497 exists
to eliminate.

These tests exercise the fix directly against the pure helper functions
(no HTTP client / DB needed) so they stay fast and deterministic.

IMPORTANT for future edits: ``reprlib`` bounds the *output* even when it
does the O(N log N) *work*, so ``isinstance(preview, str)`` and
``len(preview) <= 100`` hold identically whether or not the fix is wired
up — they are NOT regression guards. Every test below is instead written
to fail under at least one of these weakenings:

  (W1) un-wiring the fix: ``_log_value_repr.repr(value)[:100]``
  (W2) replacing ``itertools.islice`` with the ``sorted()`` this PR removes
  (W3) inflating ``_PREVIEW_MAX_ITEMS``
  (W4) dropping (or ignoring) the ``items_omitted`` flag, which
       re-collapses a nested 11-key payload onto its 10-key twin
  (W5) raising ``_PREVIEW_MAX_DEPTH`` past ``reprlib``'s ``maxlevel``,
       which inflates the container-copy bound the walk exists to cap
       while adding nothing reprlib would still render
  (W6) dropping the ``_PREVIEW_CONTAINER_TAILS`` branch, which mislabels
       an over-cap list/tuple with the dict tail

Each test names the weakening(s) it catches.
"""

from local_deep_research.web.routers.notes import (
    _PREVIEW_CONTAINER_TAILS,
    _PREVIEW_ELLIPSIS,
    _PREVIEW_ITEMS_OMITTED_TAIL,
    _PREVIEW_MAX_DEPTH,
    _PREVIEW_MAX_ITEMS,
    _bound_dict_shaped_walk,
    _log_value_preview,
    _log_value_repr,
)

# Deliberate literals, not the imported constants: asserting against
# ``_PREVIEW_MAX_ITEMS`` / ``_PREVIEW_MAX_DEPTH`` would scale with any
# change to them and so could never catch (W3) or (W5).
EXPECTED_MAX_ITEMS = 11
EXPECTED_MAX_DEPTH = 4


def _bounded_copy(value):
    """The bounded copy alone, for tests that ignore the omission flag.

    Production calls ``_bound_dict_shaped_walk`` directly, because
    ``_log_value_preview`` needs both halves of its return value.
    """
    return _bound_dict_shaped_walk(value, 0)[0]


def _reverse_ordered_dict(n: int = 1000) -> dict[int, int]:
    """A dict whose INSERTION order is the reverse of its SORTED order.

    ``{i: i for i in range(n)}`` cannot distinguish ``islice`` from
    ``sorted`` — the two orders are identical — so every order-sensitive
    assertion here uses this instead.
    """
    return {i: i for i in range(n, 0, -1)}


class CountingKey:
    """Dict key that counts how many ``<`` comparisons it takes part in.

    Gives the tests a real *work* bound with no timing/flakiness: reprlib
    sorts whatever container it is handed, so the comparison count is a
    direct measure of how much of the input was touched.
    """

    comparisons = 0

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

    def __lt__(self, other: "CountingKey") -> bool:
        type(self).comparisons += 1
        return self.n < other.n

    def __hash__(self) -> int:
        return hash(self.n)

    def __eq__(self, other: object) -> bool:
        return isinstance(other, CountingKey) and self.n == other.n

    def __repr__(self) -> str:
        return f"K{self.n}"


class TestPreviewBounds:
    def test_exceeds_reprlib_limits_by_exactly_one(self):
        """Catches (W3), and any change back to 10.

        ``_PREVIEW_MAX_ITEMS`` must stay one MORE than reprlib's own
        per-container limits: reprlib appends its ``...`` marker only when
        ``n > maxdict`` (etc.), so pre-truncating to exactly the limit
        deletes the marker from every oversized preview.
        """
        assert _PREVIEW_MAX_ITEMS == EXPECTED_MAX_ITEMS
        for limit in (
            _log_value_repr.maxdict,
            _log_value_repr.maxlist,
            _log_value_repr.maxset,
            _log_value_repr.maxfrozenset,
            _log_value_repr.maxtuple,
        ):
            assert _PREVIEW_MAX_ITEMS == limit + 1

    def test_max_depth_matches_reprlib_maxlevel(self):
        """Catches (W5).

        The walk must stop exactly where ``reprlib`` stops enumerating.
        Below ``maxlevel`` the pre-bounding is what keeps
        ``repr_dict``/``repr_set`` off the full container; at or past it
        ``reprlib`` collapses the container to ``{...}`` whatever the walk
        did, so every extra level only multiplies the number of containers
        copied (the ``1 + M + M**2 + ...`` bound documented on the walk) for
        output that is never rendered.
        """
        assert _PREVIEW_MAX_DEPTH == EXPECTED_MAX_DEPTH
        assert _PREVIEW_MAX_DEPTH == _log_value_repr.maxlevel


class TestBoundDictShapedWalk:
    def test_large_dict_is_truncated(self):
        """Catches (W3) — literal bound, not the imported constant."""
        bounded = _bounded_copy({i: i for i in range(50_000)})
        assert isinstance(bounded, dict)
        assert len(bounded) == EXPECTED_MAX_ITEMS

    def test_large_set_is_truncated(self):
        """Catches (W3)."""
        bounded = _bounded_copy(set(range(50_000)))
        assert isinstance(bounded, set)
        assert len(bounded) == EXPECTED_MAX_ITEMS

    def test_large_frozenset_is_truncated(self):
        """Catches (W3)."""
        bounded = _bounded_copy(frozenset(range(50_000)))
        assert isinstance(bounded, frozenset)
        assert len(bounded) == EXPECTED_MAX_ITEMS

    def test_large_list_is_truncated(self):
        """Catches (W3)."""
        bounded = _bounded_copy(list(range(50_000)))
        assert isinstance(bounded, list)
        assert len(bounded) == EXPECTED_MAX_ITEMS

    def test_bounding_keeps_iteration_order_and_never_sorts(self):
        """Catches (W2) and (W3).

        The kept keys must be the FIRST ``_PREVIEW_MAX_ITEMS`` in insertion
        order (1000, 999, ...), not the smallest ones a ``sorted()`` would
        select (1, 2, ...) — and not all 1000, as an inflated limit would
        keep.
        """
        bounded = _bounded_copy(_reverse_ordered_dict())
        assert list(bounded) == list(range(1000, 1000 - EXPECTED_MAX_ITEMS, -1))

    def test_small_dict_passes_through_unbounded(self):
        small = {"a": 1, "b": 2}
        assert _bounded_copy(small) == small

    def test_scalar_values_pass_through(self):
        for value in ("hello", 42, 3.14, True, None):
            assert _bounded_copy(value) == value

    def test_set_subclass_is_rebuilt_as_a_plain_set(self):
        """Guards the ``type(value)(...)`` footgun.

        A set subclass used to be reconstructed via ``type(value)(...)``,
        which runs the subclass's own ``__init__`` — so the "bounded" copy
        could come back with every original element still in it.
        """

        class SelfPopulatingSet(set):
            def __init__(self, label):
                super().__init__(range(50_000))
                self.label = label

        bounded = _bounded_copy(SelfPopulatingSet("x"))
        assert type(bounded) is set
        assert len(bounded) == EXPECTED_MAX_ITEMS

    def test_set_subclass_with_incompatible_init_does_not_raise(self):
        """``type(value)(...)`` used to raise ``TypeError`` here.

        Raised from inside a ``logger.warning`` call, that turns an
        intended 400 into a 500 via ``handle_api_error``.
        """

        class TwoArgSet(set):
            def __init__(self, elements, label):
                super().__init__(elements)
                self.label = label

        bounded = _bounded_copy(TwoArgSet(range(50_000), "x"))
        assert type(bounded) is set
        assert len(bounded) == EXPECTED_MAX_ITEMS

    def test_large_dict_nested_in_list_is_truncated(self):
        """Catches (W3)."""
        # The exact reachable shape at PATCH /api/notes/<id>/research/<id>:
        # ``{"is_collapsed": <value>}`` where <value> could itself be a
        # large dict nested inside a list a caller constructs.
        nested = [{"is_collapsed": {i: i for i in range(50_000)}}]
        bounded = _bounded_copy(nested)
        assert isinstance(bounded, list)
        assert len(bounded) == 1
        inner = bounded[0]["is_collapsed"]
        assert isinstance(inner, dict)
        assert len(inner) == EXPECTED_MAX_ITEMS

    def test_large_dict_value_nested_in_dict_is_truncated(self):
        """Catches (W2) and (W3)."""
        nested = {"outer": _reverse_ordered_dict()}
        bounded = _bounded_copy(nested)
        assert len(bounded["outer"]) == EXPECTED_MAX_ITEMS
        assert list(bounded["outer"])[0] == 1000  # islice, not sorted

    def test_depth_beyond_cap_is_returned_unbounded(self):
        # Past _PREVIEW_MAX_DEPTH the walk stops recursing and the sub-value
        # is handed to reprlib as-is. For a DICT that is free: repr_dict
        # checks ``level <= 0`` before calling _possibly_sorted, so it
        # collapses to ``{...}`` without sorting. (That is NOT true of
        # repr_set/repr_frozenset, which sort first and check level after —
        # see the helper's docstring.)
        deeply_nested = [[[[[{i: i for i in range(50_000)}]]]]]
        bounded = _bounded_copy(deeply_nested)
        # Walk down the bounded list-of-lists to the point where recursion
        # stopped and assert the dict at the bottom was NOT truncated.
        node = bounded
        while isinstance(node, list):
            node = node[0]
        assert isinstance(node, dict)
        assert len(node) == 50_000


class TestLogValuePreviewDictShaped:
    def test_preview_of_large_dict_keeps_insertion_order(self):
        """Catches (W1), (W2) and (W3) — the core regression guard.

        Under the fix, reprlib only ever sees the first 11 keys in
        insertion order (1000..990) and sorts those, so the preview starts
        at 990. Un-wire the bounding step, restore the ``sorted()``, or
        inflate the limit, and reprlib sorts the whole dict instead and the
        preview starts at ``{1: 1``.
        """
        preview = _log_value_preview(_reverse_ordered_dict())
        assert preview.startswith("{990: 990, 991: 991")

    def test_preview_of_large_dict_does_not_touch_every_key(self):
        """Catches (W1), (W2) and (W3) — a *work* bound, not a timing one.

        Measured: 10 comparisons with the fix (an 11-element descending
        run), 999+ under any of the three weakenings.
        """
        big = {CountingKey(i): i for i in range(1000, 0, -1)}
        CountingKey.comparisons = 0
        _log_value_preview(big)
        assert CountingKey.comparisons < 100

    def test_preview_of_oversized_container_keeps_the_ellipsis_marker(self):
        """Catches a return to ``_PREVIEW_MAX_ITEMS = 10``.

        Without the marker a 400,000-key attacker payload logs a line
        byte-identical to a legitimate 10-key body, erasing the one fact
        (#5497) this log site exists to record.
        """
        assert _log_value_preview({i: i for i in range(400_000)}).endswith(
            ", ...}"
        )
        assert _log_value_preview(list(range(50_000))).endswith(", ...]")
        assert _log_value_preview(set(range(50_000))).endswith(", ...}")
        assert _log_value_preview(tuple(range(50_000))).endswith(", ...)")

    def test_preview_of_long_valued_dict_stays_distinguishable(self):
        """The gate probe: values long enough that the FINAL char bound bites.

        Ten ~100-char string values overflow the 100-char preview cap on
        their own, so the final slice did the truncating — and a plain
        ``[:100]`` sliced reprlib's ``...`` marker off with it, making an
        11-key (or 50,010-key) body preview byte-identical to a legitimate
        10-key one. The oversized previews must keep a truncation marker
        and differ from the exactly-10-key one (which is only char-bounded,
        and marked as such).
        """
        ten = {f"k{i}": "v" * 100 for i in range(10)}
        preview_ten = _log_value_preview(ten)
        assert preview_ten.endswith("...")
        assert len(preview_ten) <= 100
        for n in (11, 50_010):
            oversized = {f"k{i}": "v" * 100 for i in range(n)}
            preview = _log_value_preview(oversized)
            assert preview != preview_ten
            assert preview.endswith(", ...}")
            assert len(preview) <= 100

    def test_over_cap_list_and_tuple_keep_their_own_container_tail(self):
        """Catches (W6) — the only test that reaches the tail branch.

        The oversized containers used above preview in ~35 chars and so
        return via the ``len(text) <= 100`` fast path, never touching
        ``_PREVIEW_CONTAINER_TAILS``. Reaching it needs a repr that BOTH
        overflows the char cap and ends in ``reprlib``'s own fill: a
        top-level list/tuple of 11 ~100-char strings. Without the branch
        these fall through to the ``items_omitted`` fallback and get the
        dict tail ``', ...}'``, mislabelling a list as a mapping in the
        audit line.
        """
        wide_values = ["v" * 100] * EXPECTED_MAX_ITEMS
        for value, tail in (
            (wide_values, ", ...]"),
            (tuple(wide_values), ", ...)"),
        ):
            preview = _log_value_preview(value)
            assert len(preview) == 100
            assert preview.endswith(tail)
        # The 10-item twin loses no entries, so it is char-bounded only and
        # ends in a bare "..." — the pair stays distinguishable.
        ten = _log_value_preview(["v" * 100] * 10)
        assert ten.endswith(_PREVIEW_ELLIPSIS)
        assert not ten.endswith(_PREVIEW_CONTAINER_TAILS)

    def test_preview_of_exactly_max_items_container_has_no_marker(self):
        """The marker must mean "there was more", not appear unconditionally."""
        preview = _log_value_preview({i: i for i in range(10)})
        assert (
            preview
            == "{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}"
        )

    def test_preview_of_large_dict_is_short(self):
        # Output bound only — deliberately NOT a regression guard (reprlib
        # bounds the output even when it does the full O(N log N) work).
        preview = _log_value_preview({i: i for i in range(400_000)})
        assert isinstance(preview, str)
        assert len(preview) <= 100

    def test_preview_of_large_set_is_short(self):
        preview = _log_value_preview(set(range(400_000)))
        assert isinstance(preview, str)
        assert len(preview) <= 100

    def test_preview_of_bool_is_unaffected(self):
        # The common real-world reject value at these call sites (e.g.
        # is_collapsed="yes", force_reindex="no") — make sure the dict/set
        # bounding doesn't disturb plain-scalar previews.
        assert _log_value_preview(True) == "True"
        assert _log_value_preview("yes") == "'yes'"


class TestNestedTruncationDistinguishability:
    """The nested 10-vs-11 collision (gate round 2). Catches (W4).

    ``reprlib``'s fill (``', ...}'``) for a NESTED truncated container is
    followed by the outer container's closing delimiters, so it is never
    the suffix of the whole repr and the tail-preservation branch cannot
    keep it. Without the ``items_omitted`` flag, the nested 11-key and
    10-key payloads below produced byte-identical 100-char previews (both
    sliced by the generic ``...`` fallback), erasing "entries were dropped"
    from the audit line — the exact erasure this log site exists to record.
    """

    @staticmethod
    def _pair(wrap):
        """10-key and 11-key inner dicts of ~100-char values, wrapped."""
        ten = wrap({f"k{i}": "v" * 100 for i in range(10)})
        eleven = wrap({f"k{i}": "v" * 100 for i in range(11)})
        return _log_value_preview(ten), _log_value_preview(eleven)

    def test_nested_dict_11_keys_differs_from_10_keys(self):
        preview_ten, preview_eleven = self._pair(lambda inner: {"outer": inner})
        assert preview_ten != preview_eleven
        # No container anywhere exceeded reprlib's limit: the only
        # truncation is the char cap, marked with a bare "...".
        assert preview_ten.endswith(_PREVIEW_ELLIPSIS)
        assert not preview_ten.endswith(_PREVIEW_CONTAINER_TAILS)
        # The inner dict lost its 11th entry: items-omitted tail.
        assert preview_eleven.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
        assert len(preview_ten) <= 100
        assert len(preview_eleven) <= 100

    def test_nested_dict_50_010_keys_also_carries_the_items_tail(self):
        big = {"outer": {f"k{i}": "v" * 100 for i in range(50_010)}}
        preview = _log_value_preview(big)
        assert preview.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
        assert len(preview) <= 100

    def test_list_wrapped_nested_dict_11_keys_differs_from_10_keys(self):
        preview_ten, preview_eleven = self._pair(lambda inner: [inner])
        assert preview_ten != preview_eleven
        assert preview_ten.endswith(_PREVIEW_ELLIPSIS)
        assert not preview_ten.endswith(_PREVIEW_CONTAINER_TAILS)
        assert preview_eleven.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
        assert len(preview_ten) <= 100
        assert len(preview_eleven) <= 100

    def test_dict_in_list_in_dict_nested_11_keys_differs_from_10_keys(self):
        preview_ten, preview_eleven = self._pair(
            lambda inner: {"a": [{"b": inner}]}
        )
        assert preview_ten != preview_eleven
        assert preview_ten.endswith(_PREVIEW_ELLIPSIS)
        assert not preview_ten.endswith(_PREVIEW_CONTAINER_TAILS)
        assert preview_eleven.endswith(_PREVIEW_ITEMS_OMITTED_TAIL)
        assert len(preview_ten) <= 100
        assert len(preview_eleven) <= 100

    def test_walk_reports_whether_any_visited_container_lost_entries(self):
        # Exactly at reprlib's limit: no fill, no flag.
        bounded, omitted = _bound_dict_shaped_walk(
            {"outer": {f"k{i}": "v" for i in range(10)}}, 0
        )
        assert omitted is False
        assert len(bounded["outer"]) == 10
        # One past reprlib's limit — even though nothing is pre-bounded
        # away (11 <= _PREVIEW_MAX_ITEMS), reprlib drops the 11th entry,
        # so the walk must already report it.
        _, omitted = _bound_dict_shaped_walk(
            {"outer": {f"k{i}": "v" for i in range(11)}}, 0
        )
        assert omitted is True
        # Deeper nesting and a list container report it too.
        _, omitted = _bound_dict_shaped_walk({"a": [{"b": list(range(11))}]}, 0)
        assert omitted is True

    def test_nested_container_that_fits_uses_length_only_marker(self):
        small = {"outer": {f"k{i}": "v" for i in range(10)}}
        preview = _log_value_preview(small)
        assert preview.endswith(_PREVIEW_ELLIPSIS)
        assert not preview.endswith(_PREVIEW_CONTAINER_TAILS)