fix(578): the LIKE prefilter under-matched every accented artist; make the superset provable

Review of 1b78dc9e found the pre-filter's correctness claim was false, and the claim was in the
decision record as well as the code.

F1 (high). The pattern JSON-encoded the whole query prefix on the reasoning that the stored text
escapes non-ASCII, so encoding the prefix the same way would line up. It does not: SQL LOWER()
lowercases the *escape text* (`É` -> `é`); it cannot case-fold the codepoint that escape
denotes. So `q=é` built `%"é%`, the stored `Édith Piaf` never matched, and the row was
discarded before the in-memory filter could accept it. Every accented artist — Beyoncé, Björk,
Sigur Rós, Édith Piaf — was silently unsuggestable, which in a music library is the common case.

The invariant that was missing, now stated in the code: the SQL pre-filter is an OPTIMIZATION. It
may over-match; it must never under-match. Correctness lives in the in-memory filter. So the pattern
now narrows only on the leading run of characters the JSON writer stores verbatim and stops at the
first character it cannot prove — `q=Beyoncé` still narrows on `beyonc`, `q=é` narrows on nothing
and leans on the row cap. Soundness rests on two facts now asserted by exhaustive computation rather
than argued: no non-ASCII codepoint in U+0080..U+10FFFF OrdinalIgnoreCase-equals a printable ASCII
character (false for InvariantCultureIgnoreCase, which folds ~190 — the choice of Ordinal is
load-bearing), and the exact set of ASCII the encoder escapes.

F1b. `UseRequestLocalization` honours Accept-Language, so the culture was caller-controlled and
`ToLower()` plus the default linguistic `StartsWith(string)` let a header change the answer.
Comparison is now OrdinalIgnoreCase and ordering StringComparer.Ordinal throughout — including the
shared FilterSortTake that state/video_dynamic_range/content_rating also use. Sets unchanged,
order now ordinal rather than culture-dependent.

F2. The merge comment asserted an exactness the code does not have: sources truncate by their own
ordering (DB collation / primary key), not the merge's, so a dropped value can outrank a survivor.
Comment and record now say best-effort, exact only below the truncation points.

F3/F4. The cap now rides `ORDER BY Id` rather than the JSON column: MySQL sorts TEXT by only
max_sort_length bytes, so the old ordering was not deterministic there, and sorting the whole
matching set was avoidable work. What the cap still does NOT bound is the scan — a leading-wildcard
LIKE cannot seek an index — so that cost is now documented as accepted, with a normalized
`SongArtist` table named as the follow-up candidate rather than left implicit.

Every clause above is covered by a test verified to FAIL when that clause is mutated (old pattern
builder: 5 red; culture chain: 3 red; cap=3 / cap=limit / ORDER BY json / no cap: red each).

F5. Converted to a proper supersession. The old record did not merely hold a stale fact — it
recorded song/music-video credits as an "intentionally-uncovered gap" and album_artist as
unsupported, and this reverses that call, which `docs.decision-lifecycle` says is never a
line-edit. `api.search-field-values` is archived with its original prose restored, and
`api.search-field-values-sources` replaces it carrying the whole endpoint contract.
This commit is contained in:
2026-07-27 03:10:35 +02:00
parent cd6f36185c
commit 1641ca8305
13 changed files with 569 additions and 173 deletions
+12 -5
View File
@@ -456,11 +456,18 @@ No server-side caching. Powers the visual rule builder's value-input combobox fo
or partly) by `SongMetadata.Artists`/`AlbumArtists`, which EF maps as **primitive collections** — one
JSON array per row in a single column, with no server-side projection on either provider. `album_artist`
therefore no longer 404s, and `artist` now also covers free-text music-video (`MusicVideoArtist`) and
song credits, not only entity artists. For these fields the handler pre-filters on the raw JSON in SQL
and caps the rows it reads, so on a very large library the response is a **deterministic subset** of the
matches rather than every match — say so in the `[EndpointDescription]` of any endpoint that adopts this
shape, and never let an unbounded scan stand in for it. Full rationale, the rejected normalized-side-table
alternative and the dual-provider `LIKE`/`LOWER` portability rules: `api.search-field-values-list-columns`.
song credits, not only entity artists. For these fields the handler narrows on the raw JSON in SQL and
caps the rows it reads, so on a very large library the response is a bounded subset of the matches rather
than every match — say so in the `[EndpointDescription]` of any endpoint that adopts this shape.
Two rules generalize beyond this endpoint. **A SQL pre-filter layered under an in-memory exact filter may
over-match but must NEVER under-match** — an under-match is a silent false negative that no assertion over
the returned values can see, so prove the superset (ours is proved by an exhaustive Unicode sweep, not by
argument) rather than eyeballing it. And **prefix matching, dedup and ordering on any `/api/*` read must be
ordinal, never current-culture**: `UseRequestLocalization` honours `Accept-Language`, so `ToLower()` and the
default linguistic `StartsWith(string)` let a caller change the result by changing a header. Full rationale,
the rejected normalized-side-table alternative, the accepted scan cost and the dual-provider `LIKE`/`LOWER`
rules: `api.search-field-values-sources` (which supersedes `api.search-field-values`).
**Param + DTO expansion (#293, cap `search/all-items`)**: no new endpoint — `GET /api/v1/search/all-items`
gained two **optional** query params (`pageSize` default 500, clamped 1–1000 via the §1 Logs `Math.Clamp`