v3 library: artist sort orders titles within an artist (tree-view feel) (#720)

* v3 library: artist sort orders titles within an artist (tree-view feel)

Tester report: "the list is set up by artist, but the cards are
alphabetical(-ish random)". Real: the tree orders artist -> album -> title,
while the grid's artist sort ordered within an artist by RAW FILENAME —
community-pack filename noise, so an artist's cards looked shuffled.

- artist / artist-desc gain a title secondary (direction baked per entry so
  the legacy `dir=desc` append can't land on the title term; titles stay
  A->Z under Z->A artists).
- The two-term (value, filename) keyset cursor can't seek a three-term
  order, so artist sorts leave _KEYSET_SORTS and page by OFFSET — measured
  trivial at real library sizes; title/recent keep their keyset. Restore
  via a composite sort-key column if 50k-song libraries ever hurt.
- The tree view says "List view groups by artist — the selected sort
  applies to the card grid" when a non-artist sort is active, instead of
  silently ignoring the picker.
- Keyset proof-tests repinned to the title sort (same property, a sort
  that still keysets); 2 new tests pin the title-within-artist order and
  the OFFSET pagination's no-skip/no-dupe across pages.

Full-suite failure set identical to the same-main baseline.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Nm7tHs1Yvjjtnnu4nzJgdN

* v3 library: honor legacy sort=artist&dir=desc (fold dir into effective sort)

Codex/review follow-up to the title-within-artist change: the new artist
ORDER BY bakes in `ASC` (for the title secondary), so the global `dir=desc`
append is suppressed and `sort=artist&dir=desc` silently returned A->Z
instead of Z->A — a regression on the legacy /api/library dir contract.

Fold `dir=desc` into the canonical sort key BEFORE the sort_map lookup via
the existing _effective_keyset_sort helper (same fold the cursor side already
does), so the ORDER BY is built from the effective sort. Only artist/title
fold (they have `-desc` twins); title/recent/tuning/year/mastery are
unaffected — verified by the keyset/filter suites.

New test pins that legacy `sort=artist&dir=desc` matches the explicit
`artist-desc` ordering (Z->A artists, A->Z titles within each).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: byrongamatos <xasiklas@gmail.com>
This commit is contained in:
ChrisBeWithYou
2026-07-02 20:51:33 +02:00
committed by GitHub
co-authored by Claude Opus 4.8 byrongamatos
parent 7564934d06
commit 55060c4f67
6 changed files with 130 additions and 16 deletions
+25 -6
View File
@@ -455,8 +455,12 @@ def _apply_pending_db_restore(config_dir: Path) -> None:
# TOTAL — which also fixes a latent OFFSET skip/dupe across equal-key rows.
# (column, collate-clause, primary-direction) — tiebreak is always `filename` ASC.
_KEYSET_SORTS = {
"artist": ("artist", "COLLATE NOCASE", "ASC"),
"artist-desc": ("artist", "COLLATE NOCASE", "DESC"),
# artist/artist-desc left OUT deliberately: their ORDER BY carries a
# title secondary (so cards within an artist read alphabetically, like
# the tree view) which a two-term (value, filename) cursor can't seek
# correctly — they page by OFFSET, which is measured-trivial at real
# library sizes. Restore them with a composite sort-key column if
# 50k-song libraries ever make OFFSET hurt.
"title": ("title", "COLLATE NOCASE", "ASC"),
"title-desc": ("title", "COLLATE NOCASE", "DESC"),
"recent": ("mtime", "", "DESC"),
@@ -3517,7 +3521,13 @@ class MetadataDB:
where += self._GROUP_REP_PREDICATE
sort_map = {
"artist": "artist COLLATE NOCASE", "artist-desc": "artist COLLATE NOCASE DESC",
# Artist sorts order WITHIN an artist by title (the tree view's
# artist -> album -> title feel) instead of raw filename — the
# "list is organised, cards look random" report. Direction is
# baked per entry (the legacy `dir=desc` append would otherwise
# land on the title term); title stays ascending under Z->A.
"artist": "artist COLLATE NOCASE ASC, title COLLATE NOCASE ASC",
"artist-desc": "artist COLLATE NOCASE DESC, title COLLATE NOCASE ASC",
"title": "title COLLATE NOCASE", "title-desc": "title COLLATE NOCASE DESC",
"recent": "mtime DESC",
# Tuning sort uses musical distance from E Standard
@@ -3589,14 +3599,23 @@ class MetadataDB:
"FROM work_display w1 WHERE w1.filename = songs.filename))")
sort_map["mastery"] = f"({_gm} IS NULL) ASC, {_gm} ASC"
sort_map["mastery-desc"] = f"({_gm} IS NULL) ASC, {_gm} DESC"
order = sort_map.get(sort, "artist COLLATE NOCASE")
# Fold the legacy `dir=desc` toggle into the canonical sort key BEFORE
# the lookup, so the ORDER BY is built from the effective sort — mirrors
# what `_effective_keyset_sort` does on the cursor side. Needed because
# the artist clause now bakes in `ASC` (for the title secondary), so the
# ` DESC` append below is suppressed and would otherwise silently ignore
# `sort=artist&dir=desc` (return A→Z). Only artist/title fold (they have
# `-desc` twins); tuning/year/mastery keep their own dir handling.
eff = _effective_keyset_sort(sort, direction)
order = sort_map.get(eff, "artist COLLATE NOCASE")
# Legacy `dir=desc` toggle: only safe to append on simple sort
# clauses that don't already encode a direction. Compound /
# multi-term entries above (tuning, year, year-desc) bake their
# multi-term entries above (artist, tuning, year, year-desc) bake their
# ASC/DESC into the clause, so a global ` DESC` append would
# produce invalid SQL like `CAST(year AS INTEGER) ASC DESC`.
# Skip the append in that case — clients flipping direction on
# those sorts use the explicit `-desc` sort key instead.
# those sorts use the explicit `-desc` sort key instead. (For
# artist/title the fold above already picked the `-desc` clause.)
if direction == "desc" and " ASC" not in order and " DESC" not in order:
order += " DESC"
# Unique, deterministic tiebreak → a TOTAL order. Without it, rows with