From 47e749225e33cd0f6d7b69ba62b8d35408586586 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Thu, 26 Nov 2026 12:46:44 -0500 Subject: [PATCH] refactor(recommendation): both candidate pools score through one listener context (#8002) LoadCandidates and LoadCandidatesFromSimilarity each loaded contextual likes, the taste profile and context affinity, and each built ScoringInputs field by field, so a new scoring input added to one pool would be missing from the other. loadScoringContext loads them once and scoringContext.inputs builds every candidate's inputs; the similarity pool adds its similarity score on top. Co-Authored-By: Claude Opus 5.5 --- internal/recommendation/candidates.go | 122 +++++++++++++------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/internal/recommendation/candidates.go b/internal/recommendation/candidates.go index ba388f5d..ba2d2d19 100644 --- a/internal/recommendation/candidates.go +++ b/internal/recommendation/candidates.go @@ -32,43 +32,19 @@ func LoadCandidates( return nil, err } - likes, err := loadContextualLikesByTrack(ctx, q, userID) - if err != nil { - return nil, err - } - - profile, err := LoadTasteProfile(ctx, q, userID) - if err != nil { - return nil, err - } - - affinity, err := LoadContextAffinity(ctx, q, userID, currentVector.DeviceClass) + sc, err := loadScoringContext(ctx, q, userID, currentVector) if err != nil { return nil, err } out := make([]Candidate, 0, len(rows)) for _, r := range rows { - var lpt *time.Time - if r.LastPlayedAt.Valid { - t := r.LastPlayedAt.Time - lpt = &t - } - ctxScore := ContextualMatchScore(currentVector, likes[r.Track.ID], DefaultSimilarityWeights) + // Fallback path: mood is scored only in the primary (similarity) + // loader — loading per-candidate tags over this near-whole-library + // pool isn't worth it (nil moods → 0). out = append(out, Candidate{ - Track: r.Track, - Inputs: ScoringInputs{ - IsGeneralLiked: r.IsLiked, - LastPlayedAt: lpt, - PlayCount: int(r.PlayCount), - SkipCount: int(r.SkipCount), - ContextualMatchScore: ctxScore, - // Fallback path: mood is scored only in the primary - // (similarity) loader — loading per-candidate tags over this - // near-whole-library pool isn't worth it (nil moods → 0). - TasteMatchScore: profile.Match(r.Track.ArtistID, r.Track.Genre, r.ReleaseDate, nil), - ContextAffinityScore: affinity.Affinity(r.Track.ArtistID), - }, + Track: r.Track, + Inputs: sc.inputs(r.Track, r.IsLiked, r.LastPlayedAt, r.PlayCount, r.SkipCount, r.ReleaseDate, nil), }) } return out, nil @@ -200,17 +176,7 @@ func LoadCandidatesFromSimilarity( return nil, err } - likes, err := loadContextualLikesByTrack(ctx, q, userID) - if err != nil { - return nil, err - } - - profile, err := LoadTasteProfile(ctx, q, userID) - if err != nil { - return nil, err - } - - affinity, err := LoadContextAffinity(ctx, q, userID, currentVector.DeviceClass) + sc, err := loadScoringContext(ctx, q, userID, currentVector) if err != nil { return nil, err } @@ -226,11 +192,6 @@ func LoadCandidatesFromSimilarity( out := make([]Candidate, 0, len(rows)) for _, r := range rows { - var lpt *time.Time - if r.LastPlayedAt.Valid { - t := r.LastPlayedAt.Time - lpt = &t - } // sqlc returns SimilarityScore as interface{} (couldn't infer the // type through max(...) over a UNION). Type-assert; default to 0 // on the (impossible-but-defensive) case where it's nil/wrong type. @@ -238,25 +199,64 @@ func LoadCandidatesFromSimilarity( if v, ok := r.SimilarityScore.(float64); ok { simScore = v } - ctxScore := ContextualMatchScore(currentVector, likes[r.Track.ID], DefaultSimilarityWeights) - out = append(out, Candidate{ - Track: r.Track, - Inputs: ScoringInputs{ - IsGeneralLiked: r.IsLiked, - LastPlayedAt: lpt, - PlayCount: int(r.PlayCount), - SkipCount: int(r.SkipCount), - ContextualMatchScore: ctxScore, - SimilarityScore: simScore, - TasteMatchScore: profile.Match( - r.Track.ArtistID, r.Track.Genre, r.ReleaseDate, moods[r.Track.ID]), - ContextAffinityScore: affinity.Affinity(r.Track.ArtistID), - }, - }) + inputs := sc.inputs(r.Track, r.IsLiked, r.LastPlayedAt, r.PlayCount, r.SkipCount, r.ReleaseDate, + moods[r.Track.ID]) + inputs.SimilarityScore = simScore + out = append(out, Candidate{Track: r.Track, Inputs: inputs}) } return out, nil } +// scoringContext is what scoring needs to know about the listener, loaded +// once per candidate pool. Both pool loaders build their inputs through it, +// so a scoring input added here reaches radio, Songs-like and the mixes +// alike. +type scoringContext struct { + vector SessionVector + likes map[pgtype.UUID][]SessionVector + profile TasteProfile + affinity ContextAffinity +} + +func loadScoringContext( + ctx context.Context, q *dbq.Queries, userID pgtype.UUID, currentVector SessionVector, +) (scoringContext, error) { + likes, err := loadContextualLikesByTrack(ctx, q, userID) + if err != nil { + return scoringContext{}, err + } + profile, err := LoadTasteProfile(ctx, q, userID) + if err != nil { + return scoringContext{}, err + } + affinity, err := LoadContextAffinity(ctx, q, userID, currentVector.DeviceClass) + if err != nil { + return scoringContext{}, err + } + return scoringContext{vector: currentVector, likes: likes, profile: profile, affinity: affinity}, nil +} + +// inputs scores one candidate row against the listener. moods may be nil. +func (s scoringContext) inputs( + track dbq.Track, liked bool, lastPlayed pgtype.Timestamptz, plays, skips int64, + released pgtype.Date, moods []string, +) ScoringInputs { + var lpt *time.Time + if lastPlayed.Valid { + t := lastPlayed.Time + lpt = &t + } + return ScoringInputs{ + IsGeneralLiked: liked, + LastPlayedAt: lpt, + PlayCount: int(plays), + SkipCount: int(skips), + ContextualMatchScore: ContextualMatchScore(s.vector, s.likes[track.ID], DefaultSimilarityWeights), + TasteMatchScore: s.profile.Match(track.ArtistID, track.Genre, released, moods), + ContextAffinityScore: s.affinity.Affinity(track.ArtistID), + } +} + // loadCandidateMoods fetches the enriched tags for the given candidate tracks // and reduces each to its canonical mood buckets (internal/mood), so the // scorer can apply the mood facet per candidate. Tracks with no mood-word tags