fix(recommendation): radio and shuffle keep one artist's songs apart (#8216)
release / web (push) Successful in 1m17s
release / go (push) Failing after 1m42s
release / govulncheck (push) Successful in 17s
release / integration (push) Failing after 3m57s
release / android (push) Successful in 4m23s
release / Build signed APK (push) Successful in 4m50s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped

Radio returned its picks in score order, and a seed's own artist matches
it best, so a DROELOE radio opened with four DROELOE songs in a row. The
artist cap bounded how many songs an artist got, not where they played.

SpreadArtists reorders picks so an artist doesn't play again within
three songs where another choice exists. Each slot takes the best
remaining pick outside that gap; when none is, the artist heard longest
ago goes next, so two artists alternate. Which songs are picked doesn't
change. Radio counts the seed's artist as just played; the library
shuffle spreads from a blank start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 06:48:04 -05:00
co-authored by Claude Opus 5.5
parent 352720f13f
commit 27e3725e54
3 changed files with 178 additions and 4 deletions
+13 -4
View File
@@ -51,7 +51,8 @@ func (p *Picker) logger() *slog.Logger {
// Radio returns up to limit tracks to play after seedID, the seed itself not
// among them. Candidates come from tracks similar to the seed, and fall back
// to the whole library if that pool cannot be loaded. exclude keeps tracks
// already queued out.
// already queued out. The picks are spread (SpreadArtists) so the seed's own
// artist, whose songs match it best, doesn't open the radio back to back.
func (p *Picker) Radio(ctx context.Context, q *dbq.Queries, userID, seedID pgtype.UUID, limit int, exclude []pgtype.UUID) ([]Candidate, error) {
limit = p.bound(limit)
if limit <= 0 {
@@ -80,7 +81,14 @@ func (p *Picker) Radio(ctx context.Context, q *dbq.Queries, userID, seedID pgtyp
}
// Diversity caps scaled to the length asked for, so a 20-track radio
// and a 200-track one are capped alike.
return Shuffle(candidates, p.Weights(), time.Now().UTC(), p.Rand, limit, RadioDiversityCaps(limit)), nil
picks := Shuffle(candidates, p.Weights(), time.Now().UTC(), p.Rand, limit, RadioDiversityCaps(limit))
// The seed plays first, so its artist counts as just played. An unknown
// seed only loses that: the picks are still spread among themselves.
var seedArtist pgtype.UUID
if seed, err := q.GetTrackByID(ctx, seedID); err == nil {
seedArtist = seed.ArtistID
}
return SpreadArtists(picks, seedArtist), nil
}
// noSeed is a track id no track has. The whole-library candidate query
@@ -90,7 +98,8 @@ var noSeed = pgtype.UUID{Valid: true}
// Shuffle returns up to limit tracks from the whole library, chosen by the
// same scoring as radio (what the user likes, skips and has heard lately)
// rather than at random. keep, when not nil, narrows the library first.
// rather than at random, and spread so one artist doesn't play back to back.
// keep, when not nil, narrows the library first.
func (p *Picker) Shuffle(ctx context.Context, q *dbq.Queries, userID pgtype.UUID, limit int, keep func(dbq.Track) bool) ([]Candidate, error) {
limit = p.bound(limit)
if limit <= 0 {
@@ -109,7 +118,7 @@ func (p *Picker) Shuffle(ctx context.Context, q *dbq.Queries, userID pgtype.UUID
}
candidates = kept
}
return Shuffle(candidates, p.Weights(), time.Now().UTC(), p.Rand, limit, RadioDiversityCaps(limit)), nil
return SpreadArtists(Shuffle(candidates, p.Weights(), time.Now().UTC(), p.Rand, limit, RadioDiversityCaps(limit))), nil
}
// currentContext is what the user is listening in right now: the session