diff --git a/internal/playlists/system.go b/internal/playlists/system.go index 8a2915f4..87bf5595 100644 --- a/internal/playlists/system.go +++ b/internal/playlists/system.go @@ -5,6 +5,7 @@ package playlists import ( + "bytes" "context" "crypto/sha256" "encoding/binary" @@ -180,8 +181,21 @@ func scoreAndSortCandidates(cands []recommendation.Candidate, userID pgtype.UUID c recommendation.Candidate score float64 } - pairs := make([]scored, len(cands)) - for i, c := range cands { + // Pin candidate order by track id before drawing jitter. The candidate + // query (LoadRadioCandidatesV2) has ORDER BY random() arms and no + // stable outer ordering, so DB row order varies call-to-call. Since + // the i-th candidate gets the i-th seeded jitter draw, an unstable + // input order would assign different jitter to the same track across + // same-day rebuilds and reorder near-ties — breaking the daily + // determinism this function promises. Sorting by id first makes the + // jitter assignment a function of (track, day) alone. + ordered := make([]recommendation.Candidate, len(cands)) + copy(ordered, cands) + sort.SliceStable(ordered, func(i, j int) bool { + return uuidLessPL(ordered[i].Track.ID, ordered[j].Track.ID) + }) + pairs := make([]scored, len(ordered)) + for i, c := range ordered { pairs[i] = scored{c: c, score: recommendation.Score(c.Inputs, systemMixWeights, now, rng.Float64)} } sort.SliceStable(pairs, func(i, j int) bool { @@ -709,6 +723,12 @@ func insertSystemPlaylist(ctx context.Context, qtx *dbq.Queries, userID pgtype.U // (pgtype.UUID's String method exists on some pgx versions but not others; // also the playlists package needs this independent of test helpers.) // Suffix `PL` distinguishes it from any test helper named uuidString. +// uuidLessPL reports whether a sorts before b by raw 16-byte value. Used +// to pin candidate order deterministically before jitter assignment. +func uuidLessPL(a, b pgtype.UUID) bool { + return bytes.Compare(a.Bytes[:], b.Bytes[:]) < 0 +} + func uuidStringPL(u pgtype.UUID) string { if !u.Valid { return ""