Files
bvandeusen 6e0d0e5723
test-go / test (push) Failing after 25s
test-go / integration (push) Has been cancelled
feat(taste): phase 1 — persistent per-user taste profile from graded engagement (#796)
Build a persistent, decaying model of each user's taste, recomputed daily,
that later phases consume across every recommendation surface. Phase 1 only
BUILDS the object — no behaviour change to what's surfaced yet.

Core mechanic — graded engagement (replaces binary was_skipped for learning;
was_skipped stays for History): a play's completion ratio maps to a signal in
[-1,+1] via two linear ramps (instant-skip → -1, ~0.30 neutral, ≥0.90 → +1).
Time-decayed (half-life ~75d) so recent behaviour dominates and the profile
tracks drift.

Per operator constraints:
- No explicit dislike button — negatives come only from passive behaviour
  (early skips). Nothing recorded to regret or opt out of.
- Negatives are track-scoped; artist/tag weight is the decayed SUM of their
  tracks' engagement, so one skip nets out against many good plays (a
  DB test asserts a liked artist stays positive despite an early-skipped
  track). A floor clamp bounds how negative any single entity can get.

- migration 0035: taste_profile_artists / taste_profile_tags (signed weight,
  indexed by (user, weight DESC)).
- internal/taste: engagement.go (pure curve + decay) + profile.go
  (accumulate plays + like bonuses, floor damping, size caps, atomic-replace).
- scheduler: rebuildUserDaily recomputes the profile before the playlist
  build (so phase 2 can read it), best-effort — a taste failure never blocks
  playlist building. Wired into the daily job + startup catch-up only (not
  manual/lazy rebuilds).
- tests: pure (engagement curve, decay, ranking, floor, genre split) +
  DB-backed (positive/negative weights, aggregation-protects-artist, like
  bonus, atomic replace). All green vs real Postgres.

Config knobs live in taste.DefaultConfig() for now; wiring them into the
server RecommendationConfig is a later follow-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 20:55:14 -04:00

82 lines
2.4 KiB
Go

// Package taste builds a persistent, decaying per-user taste profile (#796).
// engagement.go is the pure core: it turns a single play's completion ratio
// into a graded taste signal in [-1, +1], replacing the binary was_skipped
// for learning purposes (was_skipped stays as-is for History display).
package taste
import "math"
// EngagementParams tune the completion→engagement curve. All in [0,1] and
// ordered HardSkip < NeutralCompletion < FullCompletion.
type EngagementParams struct {
// HardSkip: completion at/below which a play is a strong reject (-1).
HardSkip float64
// NeutralCompletion: completion at which a play is neutral (0). Set low
// (~0.30) because people enjoy plenty of tracks they don't finish; only
// genuinely-early abandons read as negative.
NeutralCompletion float64
// FullCompletion: completion at/above which a play is a full positive
// (+1).
FullCompletion float64
}
// DefaultEngagementParams returns the tuned starting curve.
func DefaultEngagementParams() EngagementParams {
return EngagementParams{HardSkip: 0.05, NeutralCompletion: 0.30, FullCompletion: 0.90}
}
// Engagement maps a play's completion ratio to a taste signal in [-1, +1]
// via two linear ramps meeting at the neutral point:
//
// completion <= HardSkip → -1
// HardSkip < c < Neutral → ramp -1 → 0
// Neutral <= c < Full → ramp 0 → +1
// completion >= Full → +1
//
// Completion is clamped to [0,1]. Degenerate params (non-increasing
// thresholds) collapse the affected ramp to its endpoint rather than
// dividing by zero.
func Engagement(completion float64, p EngagementParams) float64 {
c := clamp01(completion)
if c <= p.HardSkip {
return -1
}
if c < p.NeutralCompletion {
span := p.NeutralCompletion - p.HardSkip
if span <= 0 {
return 0
}
return -1 + (c-p.HardSkip)/span
}
if c < p.FullCompletion {
span := p.FullCompletion - p.NeutralCompletion
if span <= 0 {
return 1
}
return (c - p.NeutralCompletion) / span
}
return 1
}
// decay is the exponential time-decay applied to a play's engagement:
// 1.0 at age 0, 0.5 at age == halfLifeDays. Negative ages clamp to 0.
func decay(ageDays, halfLifeDays float64) float64 {
if ageDays < 0 {
ageDays = 0
}
if halfLifeDays <= 0 {
return 1
}
return math.Exp(-math.Ln2 * ageDays / halfLifeDays)
}
func clamp01(x float64) float64 {
if x < 0 {
return 0
}
if x > 1 {
return 1
}
return x
}