feat(tuning): scoring weights → DB-backed admin tuning lab
The recommendation scoring knobs move out of YAML (radio profile) and out of the systemMixWeights hard-code (daily_mix profile) into DB-backed settings with live effect (#1250) — the defaults-discovery lab per decision #1247: the operator turns knobs to find good values, which then get baked back into shipped defaults; end users and other operators should never need the card. - Migration 0040: recommendation_weight_profiles (radio / daily_mix, 8 weight columns), taste_tuning singleton (engagement half-life + completion-curve points), recommendation_tuning_audit (one row per change with a {field, old, new} diff — the trend view's markers, #1251). - internal/recsettings: boot reconcile seeds shipped defaults without clobbering tuned rows (coverart SettingsService pattern), validates patches (bounds, curve ordering), writes audit rows, and pushes daily_mix weights + taste config into package playlists. No-op patches write no audit row. - playlists gains SetSystemMixWeights / SetTasteConfig swap points under a RWMutex — no signature threading through the producers; the scheduler's taste rebuild reads the pushed config. - Radio reads its weight profile from the service per request; the 8 weight fields leave config.RecommendationConfig (YAML keeps only RecentlyPlayedHours / RadioSize / RadioSizeMax). - Admin API: GET/PATCH/reset under /api/admin/recommendation-tuning, echoing current + shipped values. - Web: new admin Tuning tab — two weight profiles side by side, taste card, per-scope save (changed fields only) + reset, deviation dots against shipped defaults. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TsF3cNoKrqCYsU78cXC8U6
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
package recsettings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
func newPool(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test in -short mode")
|
||||
}
|
||||
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
if err := db.Migrate(dsn, logger); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
pool, err := pgxpool.New(context.Background(), dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("pool: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
dbtest.ResetDB(t, pool)
|
||||
return pool
|
||||
}
|
||||
|
||||
func newService(t *testing.T, pool *pgxpool.Pool) *Service {
|
||||
t.Helper()
|
||||
s, err := New(context.Background(), pool, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
if err != nil {
|
||||
t.Fatalf("recsettings.New: %v", err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func auditRows(t *testing.T, pool *pgxpool.Pool) []struct {
|
||||
Scope, Action string
|
||||
Changes []fieldChange
|
||||
} {
|
||||
t.Helper()
|
||||
rows, err := pool.Query(context.Background(),
|
||||
`SELECT scope, action, changes FROM recommendation_tuning_audit ORDER BY id`)
|
||||
if err != nil {
|
||||
t.Fatalf("query audit: %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []struct {
|
||||
Scope, Action string
|
||||
Changes []fieldChange
|
||||
}
|
||||
for rows.Next() {
|
||||
var scope, action string
|
||||
var raw []byte
|
||||
if err := rows.Scan(&scope, &action, &raw); err != nil {
|
||||
t.Fatalf("scan audit: %v", err)
|
||||
}
|
||||
var changes []fieldChange
|
||||
if err := json.Unmarshal(raw, &changes); err != nil {
|
||||
t.Fatalf("unmarshal audit changes: %v", err)
|
||||
}
|
||||
out = append(out, struct {
|
||||
Scope, Action string
|
||||
Changes []fieldChange
|
||||
}{scope, action, changes})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestNew_SeedsShippedDefaults(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
s := newService(t, pool)
|
||||
if got := s.Weights(ScopeRadio); got != ShippedRadioWeights() {
|
||||
t.Errorf("radio weights = %+v, want shipped defaults", got)
|
||||
}
|
||||
if got := s.Weights(ScopeDailyMix); got != ShippedDailyMixWeights() {
|
||||
t.Errorf("daily_mix weights = %+v, want shipped defaults", got)
|
||||
}
|
||||
if got := s.Taste(); got != ShippedTasteTuning() {
|
||||
t.Errorf("taste tuning = %+v, want shipped defaults", got)
|
||||
}
|
||||
// Seeding must not write audit rows — nothing changed.
|
||||
if rows := auditRows(t, pool); len(rows) != 0 {
|
||||
t.Errorf("boot reconcile wrote %d audit rows, want 0", len(rows))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_PersistsAndAudits(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
s := newService(t, pool)
|
||||
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
||||
map[string]float64{"taste_weight": 2.5, "skip_penalty": 3.0}); err != nil {
|
||||
t.Fatalf("UpdateProfile: %v", err)
|
||||
}
|
||||
got := s.Weights(ScopeRadio)
|
||||
if got.TasteWeight != 2.5 || got.SkipPenalty != 3.0 {
|
||||
t.Errorf("weights = %+v, want taste 2.5 / skip 3.0", got)
|
||||
}
|
||||
// The other profile is untouched.
|
||||
if s.Weights(ScopeDailyMix) != ShippedDailyMixWeights() {
|
||||
t.Error("daily_mix must be unaffected by a radio update")
|
||||
}
|
||||
// Values survive a fresh boot (persisted, not just cached), and the
|
||||
// reconcile's ON CONFLICT DO NOTHING doesn't clobber tuned rows.
|
||||
s2 := newService(t, pool)
|
||||
if got := s2.Weights(ScopeRadio); got.TasteWeight != 2.5 {
|
||||
t.Errorf("rebooted taste_weight = %v, want 2.5", got.TasteWeight)
|
||||
}
|
||||
rows := auditRows(t, pool)
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("audit rows = %d, want 1", len(rows))
|
||||
}
|
||||
if rows[0].Scope != ScopeRadio || rows[0].Action != "update" || len(rows[0].Changes) != 2 {
|
||||
t.Errorf("audit row = %+v, want radio/update with 2 changes", rows[0])
|
||||
}
|
||||
// Changes are field-sorted: skip_penalty before taste_weight.
|
||||
if rows[0].Changes[0].Field != "skip_penalty" || rows[0].Changes[0].New != 3.0 {
|
||||
t.Errorf("changes[0] = %+v, want skip_penalty → 3.0", rows[0].Changes[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_Validation(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
s := newService(t, pool)
|
||||
cases := []struct {
|
||||
name string
|
||||
scope string
|
||||
patch map[string]float64
|
||||
want error
|
||||
}{
|
||||
{"unknown scope", "banana", map[string]float64{"taste_weight": 1}, ErrUnknownScope},
|
||||
{"unknown field", ScopeRadio, map[string]float64{"vibes": 1}, ErrUnknownField},
|
||||
{"over bound", ScopeRadio, map[string]float64{"taste_weight": 11}, ErrOutOfRange},
|
||||
{"negative jitter", ScopeRadio, map[string]float64{"jitter_magnitude": -0.1}, ErrOutOfRange},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if err := s.UpdateProfile(context.Background(), c.scope, c.patch); !errors.Is(err, c.want) {
|
||||
t.Errorf("%s: err = %v, want %v", c.name, err, c.want)
|
||||
}
|
||||
}
|
||||
if s.Weights(ScopeRadio) != ShippedRadioWeights() {
|
||||
t.Error("rejected patches must not partially apply")
|
||||
}
|
||||
if rows := auditRows(t, pool); len(rows) != 0 {
|
||||
t.Errorf("rejected patches wrote %d audit rows, want 0", len(rows))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateTaste_CurveOrderingEnforced(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
s := newService(t, pool)
|
||||
// Moving neutral above full must reject on the PATCHED result.
|
||||
err := s.UpdateTaste(context.Background(), map[string]float64{"engagement_neutral": 0.95})
|
||||
if !errors.Is(err, ErrOutOfRange) {
|
||||
t.Fatalf("err = %v, want ErrOutOfRange (curve ordering)", err)
|
||||
}
|
||||
// A coherent multi-point move is fine.
|
||||
if err := s.UpdateTaste(context.Background(), map[string]float64{
|
||||
"engagement_neutral": 0.40, "engagement_full": 0.95, "half_life_days": 30,
|
||||
}); err != nil {
|
||||
t.Fatalf("UpdateTaste: %v", err)
|
||||
}
|
||||
cfg := s.TasteConfig()
|
||||
if cfg.HalfLifeDays != 30 || cfg.Engagement.NeutralCompletion != 0.40 {
|
||||
t.Errorf("taste config = %+v, want tuned values", cfg)
|
||||
}
|
||||
// WindowDays scales with the half-life at the shipped ratio.
|
||||
if cfg.WindowDays != 30*3.6 {
|
||||
t.Errorf("WindowDays = %v, want %v", cfg.WindowDays, 30*3.6)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReset_RestoresShippedAndAudits(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
s := newService(t, pool)
|
||||
// Reset with nothing changed is a no-op (no audit row).
|
||||
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
|
||||
t.Fatalf("no-op reset: %v", err)
|
||||
}
|
||||
if rows := auditRows(t, pool); len(rows) != 0 {
|
||||
t.Fatalf("no-op reset wrote audit rows")
|
||||
}
|
||||
if err := s.UpdateProfile(context.Background(), ScopeDailyMix,
|
||||
map[string]float64{"similarity_weight": 4}); err != nil {
|
||||
t.Fatalf("UpdateProfile: %v", err)
|
||||
}
|
||||
if err := s.Reset(context.Background(), ScopeDailyMix); err != nil {
|
||||
t.Fatalf("Reset: %v", err)
|
||||
}
|
||||
if s.Weights(ScopeDailyMix) != ShippedDailyMixWeights() {
|
||||
t.Error("reset must restore shipped defaults")
|
||||
}
|
||||
rows := auditRows(t, pool)
|
||||
if len(rows) != 2 || rows[1].Action != "reset" {
|
||||
t.Fatalf("audit rows = %+v, want update then reset", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate_NoOpWritesNoAudit(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
s := newService(t, pool)
|
||||
shipped := ShippedRadioWeights()
|
||||
if err := s.UpdateProfile(context.Background(), ScopeRadio,
|
||||
map[string]float64{"taste_weight": shipped.TasteWeight}); err != nil {
|
||||
t.Fatalf("UpdateProfile: %v", err)
|
||||
}
|
||||
if rows := auditRows(t, pool); len(rows) != 0 {
|
||||
t.Errorf("no-op update wrote %d audit rows, want 0", len(rows))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user