diff --git a/internal/db/dbq/taste_profile.sql.go b/internal/db/dbq/taste_profile.sql.go index c7aa5c7f..59369642 100644 --- a/internal/db/dbq/taste_profile.sql.go +++ b/internal/db/dbq/taste_profile.sql.go @@ -196,6 +196,7 @@ JOIN tracks t ON t.id = pe.track_id JOIN albums a ON a.id = t.album_id WHERE pe.user_id = $1 AND pe.started_at > now() - ($2::float8 * INTERVAL '1 day') + AND (pe.completion_ratio IS NOT NULL OR pe.duration_played_ms IS NOT NULL) AND NOT EXISTS ( SELECT 1 FROM quarantine q WHERE q.user_id = $1 AND q.track_id = t.id @@ -223,7 +224,9 @@ type ListPlayEngagementInputsForUserRow struct { // completion ratio (precomputed column when present, else duration_played / // track duration, clamped to [0,1]); age_days drives the time-decay. Genre // is split into tags in Go; release_date derives the decade for the era -// facet. Quarantined tracks are excluded. +// facet. Quarantined tracks are excluded, and so is a play whose completion +// isn't known: one still open (playing now), or one the sweeper closed +// without hearing how it ended. Graded as 0 they would read as skips. func (q *Queries) ListPlayEngagementInputsForUser(ctx context.Context, arg ListPlayEngagementInputsForUserParams) ([]ListPlayEngagementInputsForUserRow, error) { rows, err := q.db.Query(ctx, listPlayEngagementInputsForUser, arg.UserID, arg.Column2) if err != nil { diff --git a/internal/db/queries/taste_profile.sql b/internal/db/queries/taste_profile.sql index d04da13f..16d55014 100644 --- a/internal/db/queries/taste_profile.sql +++ b/internal/db/queries/taste_profile.sql @@ -7,7 +7,9 @@ -- completion ratio (precomputed column when present, else duration_played / -- track duration, clamped to [0,1]); age_days drives the time-decay. Genre -- is split into tags in Go; release_date derives the decade for the era --- facet. Quarantined tracks are excluded. +-- facet. Quarantined tracks are excluded, and so is a play whose completion +-- isn't known: one still open (playing now), or one the sweeper closed +-- without hearing how it ended. Graded as 0 they would read as skips. SELECT t.id AS track_id, t.artist_id, @@ -23,6 +25,7 @@ JOIN tracks t ON t.id = pe.track_id JOIN albums a ON a.id = t.album_id WHERE pe.user_id = $1 AND pe.started_at > now() - ($2::float8 * INTERVAL '1 day') + AND (pe.completion_ratio IS NOT NULL OR pe.duration_played_ms IS NOT NULL) AND NOT EXISTS ( SELECT 1 FROM quarantine q WHERE q.user_id = $1 AND q.track_id = t.id diff --git a/internal/taste/profile_db_test.go b/internal/taste/profile_db_test.go index 14f62da8..663d1ca6 100644 --- a/internal/taste/profile_db_test.go +++ b/internal/taste/profile_db_test.go @@ -149,6 +149,38 @@ func TestBuildTasteProfile_PositiveVsNegative(t *testing.T) { } } +// A song playing now, and a play the sweeper closed without hearing how it +// ended, have no completion. Neither reads as a skip: the artist gets no +// negative weight from them. +func TestBuildTasteProfile_APlayWithNoKnownCompletionIsNotASkip(t *testing.T) { + pool := dbtest.Pool(t) + now := time.Now().UTC() + u := seedUser(t, pool, "tp-open") + playing := seedArtist(t, pool, "Playing Now") + track := seedTrack(t, pool, playing.ID, "Folk") + for i, ended := range []bool{false, true} { + started := now.Add(-time.Duration(i+1) * time.Hour) + _, err := pool.Exec(context.Background(), ` + WITH s AS ( + INSERT INTO play_sessions (user_id, started_at, last_event_at) + VALUES ($1, $3, $3) RETURNING id + ) + INSERT INTO play_events (user_id, track_id, session_id, started_at, ended_at) + SELECT $1, $2, s.id, $3, CASE WHEN $4 THEN now() END FROM s + `, u.ID, track.ID, started, ended) + if err != nil { + t.Fatalf("seed play (ended=%v): %v", ended, err) + } + } + + if err := BuildTasteProfile(context.Background(), pool, discardLogger(), u.ID, DefaultConfig()); err != nil { + t.Fatalf("build: %v", err) + } + if w, ok := artistWeight(t, pool, u.ID, playing.ID); ok && w < 0 { + t.Errorf("artist weight = %.3f, want no negative weight from plays of unknown completion", w) + } +} + // TestBuildTasteProfile_AggregationProtectsArtist: one early-skip among many // completed plays must NOT make a loved artist net-negative (the user's // concern that a track-dislike shouldn't tank the artist).