The first public release of Fabled Minstrel #158

Merged
bvandeusen merged 256 commits from dev into main 2026-12-03 21:41:58 -05:00
5 changed files with 101 additions and 19 deletions
Showing only changes of commit 528ea9df0c - Show all commits
+6
View File
@@ -273,6 +273,12 @@ func TestHandleRadio_ContextualMatch_BoostsRankingOverControl(t *testing.T) {
// Insert a contextual_like on the target track whose stored vector matches // Insert a contextual_like on the target track whose stored vector matches
// the rock vibe. Direct DB insert — we want full control over the vector // the rock vibe. Direct DB insert — we want full control over the vector
// for this test. // for this test.
// Both are liked (only a liked song's captured context counts), so the
// like itself favours neither and the contextual match is what differs.
if _, err := pool.Exec(context.Background(),
`INSERT INTO general_likes (user_id, track_id) VALUES ($1, $2), ($1, $3)`, user.ID, target.ID, control.ID); err != nil {
t.Fatalf("like target and control: %v", err)
}
if _, err := pool.Exec(context.Background(), if _, err := pool.Exec(context.Background(),
`INSERT INTO contextual_likes (user_id, track_id, session_vector) VALUES ($1, $2, $3)`, `INSERT INTO contextual_likes (user_id, track_id, session_vector) VALUES ($1, $2, $3)`,
user.ID, target.ID, rockVecJSON); err != nil { user.ID, target.ID, rockVecJSON); err != nil {
+19 -8
View File
@@ -34,11 +34,12 @@ func (q *Queries) InsertContextualLike(ctx context.Context, arg InsertContextual
} }
const listActiveContextualLikesForUser = `-- name: ListActiveContextualLikesForUser :many const listActiveContextualLikesForUser = `-- name: ListActiveContextualLikesForUser :many
SELECT track_id, session_vector SELECT cl.track_id, cl.session_vector
FROM contextual_likes FROM contextual_likes cl
WHERE user_id = $1 WHERE cl.user_id = $1
AND deleted_at IS NULL AND cl.deleted_at IS NULL
AND session_vector IS NOT NULL AND cl.session_vector IS NOT NULL
AND EXISTS (SELECT 1 FROM general_likes g WHERE g.user_id = cl.user_id AND g.track_id = cl.track_id)
` `
type ListActiveContextualLikesForUserRow struct { type ListActiveContextualLikesForUserRow struct {
@@ -50,6 +51,9 @@ type ListActiveContextualLikesForUserRow struct {
// non-null vectors. Cardinality is bounded by the user's actual like-while- // non-null vectors. Cardinality is bounded by the user's actual like-while-
// playing history — typically tens to low hundreds. Used by the engine to // playing history — typically tens to low hundreds. Used by the engine to
// compute contextual_match_score for the candidate pool. // compute contextual_match_score for the candidate pool.
//
// Only rows whose song is still liked count, so a context left active by
// an unlike that missed it can't keep boosting a song the user let go of.
func (q *Queries) ListActiveContextualLikesForUser(ctx context.Context, userID pgtype.UUID) ([]ListActiveContextualLikesForUserRow, error) { func (q *Queries) ListActiveContextualLikesForUser(ctx context.Context, userID pgtype.UUID) ([]ListActiveContextualLikesForUserRow, error) {
rows, err := q.db.Query(ctx, listActiveContextualLikesForUser, userID) rows, err := q.db.Query(ctx, listActiveContextualLikesForUser, userID)
if err != nil { if err != nil {
@@ -73,7 +77,11 @@ func (q *Queries) ListActiveContextualLikesForUser(ctx context.Context, userID p
const softDeleteContextualLikesForUserTrack = `-- name: SoftDeleteContextualLikesForUserTrack :exec const softDeleteContextualLikesForUserTrack = `-- name: SoftDeleteContextualLikesForUserTrack :exec
UPDATE contextual_likes UPDATE contextual_likes
SET deleted_at = now() SET deleted_at = now()
WHERE user_id = $1 AND track_id = $2 AND deleted_at IS NULL WHERE user_id = $1::uuid
AND deleted_at IS NULL
AND (track_id = $2::uuid
OR track_id IN (SELECT t.id FROM tracks t
WHERE t.song_key = (SELECT s.song_key FROM tracks s WHERE s.id = $2::uuid)))
` `
type SoftDeleteContextualLikesForUserTrackParams struct { type SoftDeleteContextualLikesForUserTrackParams struct {
@@ -81,8 +89,11 @@ type SoftDeleteContextualLikesForUserTrackParams struct {
TrackID pgtype.UUID TrackID pgtype.UUID
} }
// Marks all currently-active rows for (user, track) as deleted. Idempotent — // Marks the user's active rows for the song as deleted: every copy sharing
// already-deleted rows aren't re-touched. // the track's song_key, as UnlikeTrack removes the like from every copy.
// The context was captured on whichever copy was playing, which needn't be
// the one the unlike names. Idempotent — already-deleted rows aren't
// re-touched.
func (q *Queries) SoftDeleteContextualLikesForUserTrack(ctx context.Context, arg SoftDeleteContextualLikesForUserTrackParams) error { func (q *Queries) SoftDeleteContextualLikesForUserTrack(ctx context.Context, arg SoftDeleteContextualLikesForUserTrackParams) error {
_, err := q.db.Exec(ctx, softDeleteContextualLikesForUserTrack, arg.UserID, arg.TrackID) _, err := q.db.Exec(ctx, softDeleteContextualLikesForUserTrack, arg.UserID, arg.TrackID)
return err return err
+19 -8
View File
@@ -3,19 +3,30 @@ INSERT INTO contextual_likes (user_id, track_id, session_vector, session_id)
VALUES ($1, $2, $3, $4); VALUES ($1, $2, $3, $4);
-- name: SoftDeleteContextualLikesForUserTrack :exec -- name: SoftDeleteContextualLikesForUserTrack :exec
-- Marks all currently-active rows for (user, track) as deleted. Idempotent — -- Marks the user's active rows for the song as deleted: every copy sharing
-- already-deleted rows aren't re-touched. -- the track's song_key, as UnlikeTrack removes the like from every copy.
-- The context was captured on whichever copy was playing, which needn't be
-- the one the unlike names. Idempotent — already-deleted rows aren't
-- re-touched.
UPDATE contextual_likes UPDATE contextual_likes
SET deleted_at = now() SET deleted_at = now()
WHERE user_id = $1 AND track_id = $2 AND deleted_at IS NULL; WHERE user_id = sqlc.arg(user_id)::uuid
AND deleted_at IS NULL
AND (track_id = sqlc.arg(track_id)::uuid
OR track_id IN (SELECT t.id FROM tracks t
WHERE t.song_key = (SELECT s.song_key FROM tracks s WHERE s.id = sqlc.arg(track_id)::uuid)));
-- name: ListActiveContextualLikesForUser :many -- name: ListActiveContextualLikesForUser :many
-- Returns all the user's active (non-soft-deleted) contextual_likes with -- Returns all the user's active (non-soft-deleted) contextual_likes with
-- non-null vectors. Cardinality is bounded by the user's actual like-while- -- non-null vectors. Cardinality is bounded by the user's actual like-while-
-- playing history — typically tens to low hundreds. Used by the engine to -- playing history — typically tens to low hundreds. Used by the engine to
-- compute contextual_match_score for the candidate pool. -- compute contextual_match_score for the candidate pool.
SELECT track_id, session_vector --
FROM contextual_likes -- Only rows whose song is still liked count, so a context left active by
WHERE user_id = $1 -- an unlike that missed it can't keep boosting a song the user let go of.
AND deleted_at IS NULL SELECT cl.track_id, cl.session_vector
AND session_vector IS NOT NULL; FROM contextual_likes cl
WHERE cl.user_id = $1
AND cl.deleted_at IS NULL
AND cl.session_vector IS NOT NULL
AND EXISTS (SELECT 1 FROM general_likes g WHERE g.user_id = cl.user_id AND g.track_id = cl.track_id);
@@ -75,3 +75,50 @@ func TestSoftDeleteContextualLikes_MarksActiveRowsDeleted(t *testing.T) {
t.Errorf("deleted_at not set after SoftDelete") t.Errorf("deleted_at not set after SoftDelete")
} }
} }
// An unlike reaches the context captured on any copy of the song: the like
// is on the song, and the heart can be tapped on a different copy than the
// one that was playing.
func TestSoftDeleteContextualLikes_ReachesEveryCopyOfTheSong(t *testing.T) {
f := newFixture(t, 200_000)
ctx := context.Background()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
played := f.track
var album, artist pgtype.UUID
if err := f.pool.QueryRow(ctx, "SELECT album_id, artist_id FROM tracks WHERE id = $1", played).Scan(&album, &artist); err != nil {
t.Fatalf("read track: %v", err)
}
other, err := f.q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "Track", AlbumID: album, ArtistID: artist, FilePath: "/tmp/track-fixture-copy.flac", DurationMs: 200_000,
})
if err != nil {
t.Fatalf("copy: %v", err)
}
if _, err := f.pool.Exec(ctx, "UPDATE tracks SET song_id = $1 WHERE id = $2", played, other.ID); err != nil {
t.Fatalf("link copies: %v", err)
}
_, _ = f.w.RecordPlayStarted(ctx, f.user, played, "c", time.Now().UTC())
if _, err := f.q.LikeTrack(ctx, dbq.LikeTrackParams{UserID: f.user, TrackID: played}); err != nil {
t.Fatalf("like: %v", err)
}
if err := CaptureContextualLikeIfPlaying(ctx, f.q, f.user, played, logger); err != nil {
t.Fatalf("capture: %v", err)
}
// Unliked through the other copy, as from another album holding the song.
if _, err := f.q.UnlikeTrack(ctx, dbq.UnlikeTrackParams{UserID: f.user, TrackID: other.ID}); err != nil {
t.Fatalf("unlike: %v", err)
}
if err := SoftDeleteContextualLikes(ctx, f.q, f.user, other.ID); err != nil {
t.Fatalf("SoftDelete: %v", err)
}
var active int
if err := f.pool.QueryRow(ctx,
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND deleted_at IS NULL", f.user).Scan(&active); err != nil {
t.Fatalf("count: %v", err)
}
if active != 0 {
t.Fatalf("%d contextual likes still active after unliking the song", active)
}
}
+10 -3
View File
@@ -196,15 +196,22 @@ func trackTitles(cs []Candidate) []string {
return out return out
} }
// helperInsertContextualLike inserts a contextual_like row with the given // helperInsertContextualLike likes the track and inserts a contextual_like
// session_vector marshaled to JSON. Bypasses playevents.CaptureContextualLikeIfPlaying // row with the given session_vector marshaled to JSON, as a like while
// because we want full control over the stored vector for these unit tests. // playing does. Bypasses playevents.CaptureContextualLikeIfPlaying because we
// want full control over the stored vector for these unit tests. (Only a
// still-liked song's context counts, so the like has to be there too.)
func helperInsertContextualLike(t *testing.T, f fixture, trackID pgtype.UUID, vec SessionVector) { func helperInsertContextualLike(t *testing.T, f fixture, trackID pgtype.UUID, vec SessionVector) {
t.Helper() t.Helper()
raw, err := json.Marshal(vec) raw, err := json.Marshal(vec)
if err != nil { if err != nil {
t.Fatalf("marshal: %v", err) t.Fatalf("marshal: %v", err)
} }
if _, err := f.pool.Exec(context.Background(),
`INSERT INTO general_likes (user_id, track_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
f.user, trackID); err != nil {
t.Fatalf("like: %v", err)
}
if _, err := f.pool.Exec(context.Background(), if _, err := f.pool.Exec(context.Background(),
`INSERT INTO contextual_likes (user_id, track_id, session_vector) VALUES ($1, $2, $3)`, `INSERT INTO contextual_likes (user_id, track_id, session_vector) VALUES ($1, $2, $3)`,
f.user, trackID, raw); err != nil { f.user, trackID, raw); err != nil {