From 5939248befd4184aff050935fe7b34d8d66fd061 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 00:08:37 -0500 Subject: [PATCH] fix(metrics): web and Android tag plays with one source vocabulary, so a surface reads the same on either (#8075) The recommendation metrics judge each surface against a baseline of manual plays. The web tagged only system mixes, so its radio, album, artist and history plays all sat in the baseline; Android tagged nearly everything, and its search, Liked-list, library-shuffle and artist-top plays fell to Other. The surface deltas depended on which client someone listened on. - The web sends the vocabulary Android uses: radio:, album:, artist:, artist-top:, home:most_played and history, kept in web/src/lib/player/playSource.ts. - The baseline is now named in one place (isManualSource): no source, a search pick, or a Liked-list pick. - library_shuffle gets its own row, the random baseline the mixes exist to beat; artist-top counts as an artist play. Co-Authored-By: Claude Opus 5.5 --- internal/api/me_recommendation_metrics.go | 26 +++++++++++++++--- .../api/me_recommendation_metrics_test.go | 27 +++++++++++++++++++ web/src/lib/components/AlbumCard.svelte | 3 ++- web/src/lib/components/AlbumCard.test.ts | 2 +- web/src/lib/components/ArtistCard.svelte | 3 ++- .../lib/components/CompactTrackCard.svelte | 7 +++-- .../lib/components/CompactTrackCard.test.ts | 2 +- web/src/lib/components/HistoryRow.svelte | 3 ++- web/src/lib/components/HistoryRow.test.ts | 2 +- web/src/lib/components/TrackRow.svelte | 5 +++- web/src/lib/components/TrackRow.test.ts | 6 ++--- web/src/lib/player/playSource.ts | 19 +++++++++++++ web/src/lib/player/store.svelte.ts | 15 +++++++---- web/src/routes/+page.svelte | 2 ++ web/src/routes/albums/[id]/+page.svelte | 7 ++--- web/src/routes/artists/[id]/+page.svelte | 5 ++-- web/src/routes/artists/[id]/artist.test.ts | 2 +- web/src/routes/library/liked/+page.svelte | 3 ++- 18 files changed, 111 insertions(+), 28 deletions(-) create mode 100644 web/src/lib/player/playSource.ts diff --git a/internal/api/me_recommendation_metrics.go b/internal/api/me_recommendation_metrics.go index f77d21c4..1c8e728f 100644 --- a/internal/api/me_recommendation_metrics.go +++ b/internal/api/me_recommendation_metrics.go @@ -66,8 +66,9 @@ type surfaceGroup struct { type recommendationMetricsResp struct { WindowDays int `json:"window_days"` // Baseline is the control group: plays the user picked manually - // (source IS NULL). Surfaces are judged as deltas against it; nil - // when the window holds no manual plays. + // (isManualSource: no source, search or the Liked list). Surfaces are + // judged as deltas against it; nil when the window holds no manual + // plays. Baseline *surfaceMetric `json:"baseline"` Groups []surfaceGroup `json:"groups"` } @@ -79,6 +80,19 @@ type recFamily struct { intent string } +// isManualSource reports whether a play's source marks a manual pick: the +// listener chose the song, with no recommendation between them and it. These +// make the baseline every surface is judged against. +// +// The play-source vocabulary both clients send is: a system mix's variant +// ("for_you", "discover", …), "radio:", "album:", +// "artist:", "artist-top:", "home:
", "offline:", +// "history", "library_shuffle", "search", "liked", or none. The web keeps +// it in web/src/lib/player/playSource.ts. +func isManualSource(src string) bool { + return src == "" || src == "search" || src == "liked" +} + // bucketRecSource maps a raw client-stamped source string to its stable // family. One-off sources (album:, radio:) collapse into // their family so the table stays readable at any library size. @@ -106,8 +120,12 @@ func bucketRecSource(src string) recFamily { return recFamily{"first_listens", "First listens", intentDiscovery} case strings.HasPrefix(src, "album:"): return recFamily{"direct_album", "Album plays", intentDirect} - case strings.HasPrefix(src, "artist:"): + case strings.HasPrefix(src, "artist:") || strings.HasPrefix(src, "artist-top:"): return recFamily{"direct_artist", "Artist plays", intentDirect} + case src == "library_shuffle": + // Shuffling the whole library: the random baseline the + // recommendations exist to beat, shown beside them. + return recFamily{"library_shuffle", "Library shuffle", intentDirect} case strings.HasPrefix(src, "offline:"): return recFamily{"offline", "Offline pools", intentDirect} case strings.HasPrefix(src, "home:"): @@ -287,7 +305,7 @@ func bucketMetricsResponse( families := map[string]*familyAccum{} picks := map[string]map[string]*familyAccum{} for _, row := range rows { - if row.Source == nil || *row.Source == "" { + if row.Source == nil || isManualSource(*row.Source) { baseline.add(row) continue } diff --git a/internal/api/me_recommendation_metrics_test.go b/internal/api/me_recommendation_metrics_test.go index 4199fb5b..01e8f1d7 100644 --- a/internal/api/me_recommendation_metrics_test.go +++ b/internal/api/me_recommendation_metrics_test.go @@ -279,3 +279,30 @@ func TestRecommendationMetrics_BucketsWithBaseline(t *testing.T) { t.Errorf("radio family = %+v, want plays=1 (collapsed from radio:)", radio) } } + +// Both clients' sources land on the same surfaces: search and Liked-list +// plays are manual picks in the baseline, library shuffle is its own row, +// and an artist's top songs count as artist plays, not Other. +func TestBucketMetricsResponse_SharedSourceVocabulary(t *testing.T) { + src := func(s string) *string { return &s } + rows := []dbq.RecommendationSourceMetricsForUserRow{ + {Source: nil, Plays: 10, Skips: 1, CompletionN: 10, AvgCompletion: 0.9}, + {Source: src("search"), Plays: 5, Skips: 1, CompletionN: 5, AvgCompletion: 0.8}, + {Source: src("liked"), Plays: 5, Skips: 0, CompletionN: 5, AvgCompletion: 1}, + {Source: src("library_shuffle"), Plays: 4, Skips: 2, CompletionN: 4, AvgCompletion: 0.5}, + {Source: src("artist-top:abcd"), Plays: 3, Skips: 0, CompletionN: 3, AvgCompletion: 1}, + } + resp := bucketMetricsResponse(recMetricsDefaultDays, rows) + if resp.Baseline == nil || resp.Baseline.Plays != 20 { + t.Fatalf("baseline = %+v, want the 20 manual, search and liked plays", resp.Baseline) + } + if s := findSurface(resp, "library_shuffle"); s == nil || s.Plays != 4 { + t.Errorf("library_shuffle = %+v, want its own row of 4", s) + } + if s := findSurface(resp, "direct_artist"); s == nil || s.Plays != 3 { + t.Errorf("direct_artist = %+v, want the 3 top-song plays", s) + } + if s := findSurface(resp, "other"); s != nil { + t.Errorf("other = %+v, want none: every source here has a surface", s) + } +} diff --git a/web/src/lib/components/AlbumCard.svelte b/web/src/lib/components/AlbumCard.svelte index 0273ccda..baf82efd 100644 --- a/web/src/lib/components/AlbumCard.svelte +++ b/web/src/lib/components/AlbumCard.svelte @@ -2,6 +2,7 @@ import type { AlbumRef, AlbumDetail } from '#lib/api/types.js'; import { api } from '#lib/api/client.js'; import { enqueueTracks, playQueue } from '#lib/player/store.svelte.js'; + import { playSource } from '#lib/player/playSource.js'; import { Play } from 'lucide-svelte'; import AlbumMenu from './AlbumMenu.svelte'; import CardActionCluster from './CardActionCluster.svelte'; @@ -20,7 +21,7 @@ e.preventDefault(); e.stopPropagation(); const detail = await api.get(`/api/albums/${album.id}`); - playQueue(detail.tracks, 0); + playQueue(detail.tracks, 0, { source: playSource.album(album.id) }); } diff --git a/web/src/lib/components/AlbumCard.test.ts b/web/src/lib/components/AlbumCard.test.ts index 7888a958..45607dba 100644 --- a/web/src/lib/components/AlbumCard.test.ts +++ b/web/src/lib/components/AlbumCard.test.ts @@ -120,7 +120,7 @@ describe('AlbumCard', () => { expect(api.get).toHaveBeenCalledWith('/api/albums/xyz'); await Promise.resolve(); await Promise.resolve(); - expect(playQueue).toHaveBeenCalledWith(tracks, 0); + expect(playQueue).toHaveBeenCalledWith(tracks, 0, { source: 'album:xyz' }); }); test('renders the bottom-right kebab menu trigger', () => { diff --git a/web/src/lib/components/ArtistCard.svelte b/web/src/lib/components/ArtistCard.svelte index 03ec5d9b..2c3c6566 100644 --- a/web/src/lib/components/ArtistCard.svelte +++ b/web/src/lib/components/ArtistCard.svelte @@ -3,6 +3,7 @@ import type { ArtistRef, TrackRef } from '#lib/api/types.js'; import { api } from '#lib/api/client.js'; import { playQueue, enqueueTracks } from '#lib/player/store.svelte.js'; + import { playSource } from '#lib/player/playSource.js'; import { Play } from 'lucide-svelte'; import ArtistMenu from './ArtistMenu.svelte'; import CardActionCluster from './CardActionCluster.svelte'; @@ -18,7 +19,7 @@ e.stopPropagation(); const tracks = await api.get(`/api/artists/${artist.id}/tracks`); if (tracks.length === 0) return; - playQueue(shuffled(tracks), 0); + playQueue(shuffled(tracks), 0, { source: playSource.artist(artist.id) }); } async function onAddToQueue(e: MouseEvent) { diff --git a/web/src/lib/components/CompactTrackCard.svelte b/web/src/lib/components/CompactTrackCard.svelte index 5c15d77c..68d77472 100644 --- a/web/src/lib/components/CompactTrackCard.svelte +++ b/web/src/lib/components/CompactTrackCard.svelte @@ -15,17 +15,20 @@ let { track, sectionTracks, - index + index, + source }: { track: TrackRef; sectionTracks: TrackRef[]; index: number; + /** Where a play started here began, in the playSource vocabulary. */ + source?: string; } = $props(); const cover = $derived(coverUrl(track.album_id)); function onClick() { - playQueue(sectionTracks, index); + playQueue(sectionTracks, index, { source }); } function onAddToQueue(e: MouseEvent) { diff --git a/web/src/lib/components/CompactTrackCard.test.ts b/web/src/lib/components/CompactTrackCard.test.ts index 8366c305..be953faa 100644 --- a/web/src/lib/components/CompactTrackCard.test.ts +++ b/web/src/lib/components/CompactTrackCard.test.ts @@ -41,7 +41,7 @@ describe('CompactTrackCard', () => { props: { track: tracks[1], sectionTracks: tracks, index: 1 } }); await fireEvent.click(screen.getByRole('button', { name: /play second/i })); - expect(playQueue).toHaveBeenCalledWith(tracks, 1); + expect(playQueue).toHaveBeenCalledWith(tracks, 1, { source: undefined }); }); test('renders title and artist name', () => { diff --git a/web/src/lib/components/HistoryRow.svelte b/web/src/lib/components/HistoryRow.svelte index 70c365d2..e7f51558 100644 --- a/web/src/lib/components/HistoryRow.svelte +++ b/web/src/lib/components/HistoryRow.svelte @@ -1,6 +1,7 @@