6e0d0e5723
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>
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package taste
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
func almostEqual(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
|
|
|
|
func TestEngagement_Curve(t *testing.T) {
|
|
p := DefaultEngagementParams() // HardSkip 0.05, Neutral 0.30, Full 0.90
|
|
cases := []struct {
|
|
name string
|
|
completion float64
|
|
want float64
|
|
}{
|
|
{"instant skip", 0.00, -1},
|
|
{"at hard-skip boundary", 0.05, -1},
|
|
{"neg ramp midpoint", 0.175, -0.5}, // (0.175-0.05)/(0.30-0.05)=0.5 → -0.5
|
|
{"neutral point", 0.30, 0},
|
|
{"pos ramp midpoint", 0.60, 0.5}, // (0.60-0.30)/(0.90-0.30)=0.5
|
|
{"at full boundary", 0.90, 1},
|
|
{"complete", 1.00, 1},
|
|
{"clamp above 1", 1.50, 1},
|
|
{"clamp below 0", -0.5, -1},
|
|
}
|
|
for _, c := range cases {
|
|
if got := Engagement(c.completion, p); !almostEqual(got, c.want) {
|
|
t.Errorf("%s: Engagement(%.3f) = %.4f, want %.4f", c.name, c.completion, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEngagement_MonotonicNonDecreasing(t *testing.T) {
|
|
p := DefaultEngagementParams()
|
|
prev := math.Inf(-1)
|
|
for i := 0; i <= 100; i++ {
|
|
c := float64(i) / 100
|
|
got := Engagement(c, p)
|
|
if got < prev-1e-9 {
|
|
t.Fatalf("engagement decreased at completion %.2f: %.4f < %.4f", c, got, prev)
|
|
}
|
|
prev = got
|
|
}
|
|
}
|
|
|
|
func TestEngagement_DegenerateParamsNoPanic(t *testing.T) {
|
|
// Non-increasing thresholds must collapse ramps, not divide by zero.
|
|
p := EngagementParams{HardSkip: 0.5, NeutralCompletion: 0.5, FullCompletion: 0.5}
|
|
for _, c := range []float64{0, 0.25, 0.5, 0.75, 1} {
|
|
_ = Engagement(c, p)
|
|
}
|
|
}
|
|
|
|
func TestDecay_HalfLife(t *testing.T) {
|
|
if got := decay(0, 75); !almostEqual(got, 1) {
|
|
t.Errorf("decay(0) = %.4f, want 1", got)
|
|
}
|
|
if got := decay(75, 75); !almostEqual(got, 0.5) {
|
|
t.Errorf("decay(halflife) = %.4f, want 0.5", got)
|
|
}
|
|
if got := decay(150, 75); !almostEqual(got, 0.25) {
|
|
t.Errorf("decay(2*halflife) = %.4f, want 0.25", got)
|
|
}
|
|
if got := decay(-5, 75); !almostEqual(got, 1) {
|
|
t.Errorf("decay(negative age) = %.4f, want 1 (clamped)", got)
|
|
}
|
|
if got := decay(10, 0); !almostEqual(got, 1) {
|
|
t.Errorf("decay(halflife=0) = %.4f, want 1 (guarded)", got)
|
|
}
|
|
}
|