fix(playevents): a late offline replay no longer closes the play that is live now (#8074)

RecordOfflinePlay auto-closed whatever play was open at the replayed play's
original, often hours-old, time. When Android drained its offline queue
while the next song was already playing, each replay ended the live row
before it began, as a zero-length skip. That stuck when the live play's own
close was lost, and for a Subsonic app's now-playing row it became a skip
plus a second play.

autoClosePriorOpen now leaves open any row that started after the time it
was asked to close at, for every caller; #8019's guard for the Subsonic
path moves into it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 00:04:06 -05:00
co-authored by Claude Opus 5.5
parent b6749fcf16
commit 2898e9d746
2 changed files with 34 additions and 4 deletions
+11 -4
View File
@@ -364,10 +364,8 @@ func (w *Writer) RecordSyntheticCompletedPlay(
playEventID = open.ID playEventID = open.ID
startedAt = open.StartedAt.Time startedAt = open.StartedAt.Time
default: default:
if hasOpen && !open.StartedAt.Time.After(at) { if err := w.autoClosePriorOpen(ctx, q, userID, at); err != nil {
if err := w.autoClosePriorOpen(ctx, q, userID, at); err != nil { return err
return err
}
} }
started, err := w.openPlay(ctx, q, userID, trackID, clientID, source, "", at) started, err := w.openPlay(ctx, q, userID, trackID, clientID, source, "", at)
if err != nil { if err != nil {
@@ -575,6 +573,12 @@ func (w *Writer) captureSessionVector(
// of being force-hidden. Deliberately writes no skip_events row — an // of being force-hidden. Deliberately writes no skip_events row — an
// auto-close is an ambiguous signal, not a deliberate skip, and must not // auto-close is an ambiguous signal, not a deliberate skip, and must not
// feed the skip-ratio / recommendation signal. // feed the skip-ratio / recommendation signal.
//
// A row that started after at is left open: it is a later play, still live,
// and closing it at at would end it before it began, as a zero-length skip.
// That happens when an older play arrives late, such as an offline play
// replayed while the next song is already playing, or a Subsonic app's
// report of a finished listen.
func (w *Writer) autoClosePriorOpen( func (w *Writer) autoClosePriorOpen(
ctx context.Context, ctx context.Context,
q *dbq.Queries, q *dbq.Queries,
@@ -588,6 +592,9 @@ func (w *Writer) autoClosePriorOpen(
} }
return err return err
} }
if prior.StartedAt.Time.After(at) {
return nil
}
track, err := q.GetTrackByID(ctx, prior.TrackID) track, err := q.GetTrackByID(ctx, prior.TrackID)
if err != nil { if err != nil {
return err return err
+23
View File
@@ -585,3 +585,26 @@ func TestRecordOfflinePlay_StampsForYouPickKind(t *testing.T) {
t.Errorf("pick_kind = %v, want taste", pickKind) t.Errorf("pick_kind = %v, want taste", pickKind)
} }
} }
// An offline play replayed while another song is playing live leaves the
// live play open: closing it at the replay's older time would end it before
// it began, as a zero-length skip.
func TestRecordOfflinePlay_LeavesALaterLivePlayOpen(t *testing.T) {
f := newFixture(t, 200_000)
ctx := context.Background()
now := time.Now().UTC()
live, err := f.w.RecordPlayStarted(ctx, f.user, f.track, "c", now)
if err != nil {
t.Fatalf("live start: %v", err)
}
if err := f.w.RecordOfflinePlay(ctx, f.user, f.track, "c", "", "", now.Add(-2*time.Hour), 120_000); err != nil {
t.Fatalf("offline replay: %v", err)
}
var ended pgtype.Timestamptz
if err := f.pool.QueryRow(ctx, "SELECT ended_at FROM play_events WHERE id = $1", live.PlayEventID).Scan(&ended); err != nil {
t.Fatalf("read live play: %v", err)
}
if ended.Valid {
t.Fatalf("the live play was closed at %v by an older replay", ended.Time)
}
}