feat(recommendation): For You exploration attribution — taste vs fresh picks
test-go / test (push) Successful in 31s
test-web / test (push) Successful in 37s
test-go / integration (push) Successful in 4m37s

Milestone #127 step 2 (#1249). For You deliberately blends two
populations — a head of top-scored taste picks and a tail sampled from
deeper ranking (the freshness injection) — but the metrics judged it as
one blob, so its skip rate couldn't distinguish "the taste engine is
missing" from "the freshness tax is too high". That number decides the
exploration share before we tune it.

- Migration 0038: nullable pick_kind ('taste'|'fresh') on both
  playlist_tracks (stamped at snapshot build) and play_events (frozen at
  play-ingestion — the snapshot rebuilds daily, so attribution cannot be
  reconstructed at read time).
- Builder: pickHeadAndTail marks head=taste / tail=fresh; the small-pool
  fallback is all taste (top-N-by-score IS the taste mechanism). Other
  variants persist NULL.
- Ingestion: for_you plays (live + offline replay) look the track up in
  the user's current snapshot; not found → unattributed, never guessed.
- Metrics: For You's row gains a breakdown (taste / fresh / earlier
  unattributed plays), parent row stays the sum; web card renders the
  sub-rows indented with the same baseline deltas + low-data dimming.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TsF3cNoKrqCYsU78cXC8U6
This commit is contained in:
2026-07-02 20:39:41 -04:00
parent 60533073ad
commit fb4431207d
18 changed files with 593 additions and 39 deletions
+57 -1
View File
@@ -39,6 +39,11 @@ type surfaceMetric struct {
SkipRate float64 `json:"skip_rate"` // skips / plays, [0,1]
AvgCompletion float64 `json:"avg_completion"` // mean completion ratio, [0,1]
LowConfidence bool `json:"low_confidence"` // plays < recMetricsLowVolume
// Breakdown splits the family into sub-populations. Only For You
// carries one (#1249): taste picks vs fresh picks (the deliberate
// freshness injection), plus earlier plays that predate attribution.
// The parent row remains the sum of its breakdown.
Breakdown []surfaceMetric `json:"breakdown,omitempty"`
}
// surfaceGroup is one intent band of surface families.
@@ -163,6 +168,40 @@ func (h *handlers) handleGetRecommendationMetrics(w http.ResponseWriter, r *http
writeJSON(w, http.StatusOK, bucketMetricsResponse(days, rows))
}
// forYouPickFamilies defines the For You breakdown rows (#1249), keyed
// by play_events.pick_kind. "" (NULL pick_kind) = plays recorded before
// attribution shipped, or whose track had already rotated out of the
// snapshot at ingestion — kept visible so the parent row's sums stay
// transparent instead of silently shrinking.
var forYouPickFamilies = map[string]recFamily{
"taste": {"for_you_taste", "Taste picks", intentGoTo},
"fresh": {"for_you_fresh", "Fresh picks", intentGoTo},
"": {"for_you_unattributed", "Earlier plays", intentGoTo},
}
// forYouBreakdown folds pick-kind accums into the parent metric's
// Breakdown. Attached only when at least one attributed (taste/fresh)
// play exists — an all-unattributed breakdown would just repeat the
// parent row.
func forYouBreakdown(picks map[string]*familyAccum) []surfaceMetric {
attributed := int64(0)
for kind, acc := range picks {
if kind != "" {
attributed += acc.plays
}
}
if attributed == 0 {
return nil
}
out := make([]surfaceMetric, 0, len(picks))
for _, kind := range []string{"taste", "fresh", ""} {
if acc, ok := picks[kind]; ok && acc.plays > 0 {
out = append(out, acc.metric())
}
}
return out
}
// bucketMetricsResponse folds the raw per-source rows into the grouped,
// baseline-anchored response shape. Split from the handler for pure-unit
// testability.
@@ -171,6 +210,7 @@ func bucketMetricsResponse(
) recommendationMetricsResp {
baseline := &familyAccum{fam: recFamily{"manual", "Manual library plays", ""}}
families := map[string]*familyAccum{}
forYouPicks := map[string]*familyAccum{}
for _, row := range rows {
if row.Source == nil || *row.Source == "" {
baseline.add(row)
@@ -183,6 +223,18 @@ func bucketMetricsResponse(
families[fam.key] = acc
}
acc.add(row)
if fam.key == "for_you" {
kind := ""
if row.PickKind != nil {
kind = *row.PickKind
}
pick, ok := forYouPicks[kind]
if !ok {
pick = &familyAccum{fam: forYouPickFamilies[kind]}
forYouPicks[kind] = pick
}
pick.add(row)
}
}
resp := recommendationMetricsResp{WindowDays: days, Groups: []surfaceGroup{}}
@@ -198,7 +250,11 @@ func bucketMetricsResponse(
group := surfaceGroup{Intent: g.intent, Label: g.label}
for _, acc := range families {
if acc.fam.intent == g.intent {
group.Surfaces = append(group.Surfaces, acc.metric())
m := acc.metric()
if acc.fam.key == "for_you" {
m.Breakdown = forYouBreakdown(forYouPicks)
}
group.Surfaces = append(group.Surfaces, m)
}
}
if len(group.Surfaces) == 0 {