feat(discover): filter, cross-rail dedup, and overflow backfill for reco rails
The two Recommended-Artist rails showed artists the user already follows or blacklisted, and the same artist could appear in both rails at once. Builders now retain the full validated pool (rotated, not truncated at DISPLAY_CAP); the pools ride the results blob as before, so old blobs simply have no overflow until they expire. A new pure compose step in qbz-external-reco (unit-tested: exclusion, within/cross-rail dedup with common-wins ordering, visible/overflow split order) runs at the single paint choke point in external_reco.rs — covering BOTH the fresh build and the cached-blob paint. Exclusions fold followed (fav_cache) + blacklisted + reco-dismissed ids, each source independently fail-open. The past-cap survivors are retained as overflow for the Not-interested live backfill (no extra network). km
This commit is contained in:
4 files changed
+207
-18
No files matched your search
@@ -22,6 +22,9 @@ use crate::validate::{
|
||||
use crate::{RecoCatalog, RecoInputs};
|
||||
|
||||
const DISPLAY_CAP: usize = 20;
|
||||
/// Public alias for the paint layer (it composes the visible rows after
|
||||
/// filtering/dedup, so it needs the canonical per-rail visible cap).
|
||||
pub const ARTIST_DISPLAY_CAP: usize = DISPLAY_CAP;
|
||||
const PLAYLIST_CAP: usize = 30;
|
||||
const VALIDATE_CONCURRENCY: usize = 6;
|
||||
const ARTIST_SEEDS: usize = 6;
|
||||
@@ -239,7 +242,13 @@ async fn similar_artist_row(
|
||||
}
|
||||
let candidates: Vec<ArtistCandidate> = round_robin(groups).into_iter().take(45).collect();
|
||||
let pool = validate_artist_pool(inputs.catalog, inputs.cache, candidates).await;
|
||||
rotate_take(pool, inputs.rotation_seed, DISPLAY_CAP)
|
||||
// Rotate for daily variety but do NOT truncate at DISPLAY_CAP: the paint
|
||||
// layer composes the visible rows AFTER followed/blacklisted/dismissed
|
||||
// filtering + cross-rail dedup (compose_artist_rails) and keeps the
|
||||
// remaining validated candidates as backfill overflow — they ride the
|
||||
// results blob, so replacements need no extra network.
|
||||
let take = pool.len();
|
||||
rotate_take(pool, inputs.rotation_seed, take)
|
||||
}
|
||||
|
||||
/// "More like the artists you love" — your COMMON taste (overall top).
|
||||
@@ -258,6 +267,59 @@ pub async fn build_rec_artists_recent(
|
||||
similar_artist_row(inputs, history, "1month").await
|
||||
}
|
||||
|
||||
// ── Display composition (filter + cross-rail dedup + backfill split) ────────
|
||||
|
||||
/// One artist rail after display composition: the visible rows (the first
|
||||
/// `cap` survivors) plus the retained overflow (the remaining survivors, in
|
||||
/// pool order), which the paint layer keeps for live backfill after a
|
||||
/// "not interested" dismissal.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ArtistRailComposition {
|
||||
pub visible: Vec<ArtistReco>,
|
||||
pub overflow: Vec<ArtistReco>,
|
||||
}
|
||||
|
||||
/// Compose the two Recommended-Artist rails for display:
|
||||
///
|
||||
/// - drop every candidate whose Qobuz id is in `excluded` (the frontend folds
|
||||
/// followed / blacklisted / dismissed ids into that set; fail-soft = pass
|
||||
/// an empty set),
|
||||
/// - dedup WITHIN and ACROSS rails: an id shows in at most one rail — the
|
||||
/// COMMON rail is composed first and wins — and overflow ids are claimed
|
||||
/// too, so a common-overflow id cannot resurface in recent's visible,
|
||||
/// - the first `cap` survivors per rail are visible; the rest is overflow.
|
||||
pub fn compose_artist_rails(
|
||||
common_pool: Vec<ArtistReco>,
|
||||
recent_pool: Vec<ArtistReco>,
|
||||
excluded: &HashSet<u64>,
|
||||
cap: usize,
|
||||
) -> (ArtistRailComposition, ArtistRailComposition) {
|
||||
let mut seen: HashSet<u64> = HashSet::new();
|
||||
let common = compose_one_rail(common_pool, excluded, &mut seen, cap);
|
||||
let recent = compose_one_rail(recent_pool, excluded, &mut seen, cap);
|
||||
(common, recent)
|
||||
}
|
||||
|
||||
fn compose_one_rail(
|
||||
pool: Vec<ArtistReco>,
|
||||
excluded: &HashSet<u64>,
|
||||
seen: &mut HashSet<u64>,
|
||||
cap: usize,
|
||||
) -> ArtistRailComposition {
|
||||
let mut out = ArtistRailComposition::default();
|
||||
for reco in pool {
|
||||
if excluded.contains(&reco.qobuz_artist_id) || !seen.insert(reco.qobuz_artist_id) {
|
||||
continue;
|
||||
}
|
||||
if out.visible.len() < cap {
|
||||
out.visible.push(reco);
|
||||
} else {
|
||||
out.overflow.push(reco);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
// ── Recommended Albums (Last.fm: your artists' top albums, not scrobbled) ───
|
||||
|
||||
pub async fn build_rec_albums(inputs: &RecoInputs<'_>, history: &ExtHistory) -> Vec<AlbumReco> {
|
||||
@@ -637,3 +699,77 @@ pub async fn build_editorial(inputs: &RecoInputs<'_>) -> (Vec<AlbumReco>, Vec<Ar
|
||||
|
||||
(top_albums, top_artists)
|
||||
}
|
||||
|
||||
|
||||
// ── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn reco(id: u64) -> ArtistReco {
|
||||
ArtistReco {
|
||||
qobuz_artist_id: id,
|
||||
name: format!("Artist {id}"),
|
||||
image_url: String::new(),
|
||||
subtitle: String::new(),
|
||||
source: RecoSource::LastFm,
|
||||
}
|
||||
}
|
||||
|
||||
fn ids(recos: &[ArtistReco]) -> Vec<u64> {
|
||||
recos.iter().map(|r| r.qobuz_artist_id).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_excludes_ids_from_visible_and_overflow() {
|
||||
let common = vec![reco(1), reco(2), reco(3), reco(4)];
|
||||
let recent = vec![reco(5), reco(6)];
|
||||
let excluded = HashSet::from([2, 4, 5]);
|
||||
let (c, r) = compose_artist_rails(common, recent, &excluded, 2);
|
||||
assert_eq!(ids(&c.visible), vec![1, 3]);
|
||||
assert!(c.overflow.is_empty(), "excluded ids leave no overflow");
|
||||
assert_eq!(ids(&r.visible), vec![6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_dedups_cross_rail_common_wins() {
|
||||
let common = vec![reco(1), reco(2)];
|
||||
let recent = vec![reco(2), reco(3)];
|
||||
let (c, r) = compose_artist_rails(common, recent, &HashSet::new(), 20);
|
||||
assert_eq!(ids(&c.visible), vec![1, 2]);
|
||||
assert_eq!(ids(&r.visible), vec![3], "id 2 appears only in common");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_dedups_within_rail() {
|
||||
let common = vec![reco(1), reco(1), reco(2)];
|
||||
let (c, _r) = compose_artist_rails(common, Vec::new(), &HashSet::new(), 20);
|
||||
assert_eq!(ids(&c.visible), vec![1, 2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_splits_visible_and_overflow_in_order() {
|
||||
let common: Vec<ArtistReco> = (1..=5).map(reco).collect();
|
||||
let excluded = HashSet::from([1]);
|
||||
let (c, _r) = compose_artist_rails(common, Vec::new(), &excluded, 3);
|
||||
// The exclusion punches a hole in the first window; the next
|
||||
// candidates move up into the visible rows, the rest is overflow —
|
||||
// both in pool order (the backfill contract).
|
||||
assert_eq!(ids(&c.visible), vec![2, 3, 4]);
|
||||
assert_eq!(ids(&c.overflow), vec![5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_cross_rail_dedup_also_covers_overflow() {
|
||||
// An id sitting in common's OVERFLOW must not resurface in recent's
|
||||
// visible rows (it is still claimed for potential backfill).
|
||||
let common: Vec<ArtistReco> = (1..=3).map(reco).collect();
|
||||
let recent = vec![reco(3), reco(4)];
|
||||
let (c, r) = compose_artist_rails(common, recent, &HashSet::new(), 2);
|
||||
assert_eq!(ids(&c.visible), vec![1, 2]);
|
||||
assert_eq!(ids(&c.overflow), vec![3]);
|
||||
assert_eq!(ids(&r.visible), vec![4], "id 3 is claimed by common");
|
||||
assert!(r.overflow.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ use qbz_integrations::{LastFmClient, ListenBrainzClient, MusicBrainzClient};
|
||||
use qbz_models::{Album, Artist, Track};
|
||||
|
||||
pub use cache::RecoCache;
|
||||
pub use carousels::{compose_artist_rails, ArtistRailComposition, ARTIST_DISPLAY_CAP};
|
||||
pub use types::{
|
||||
AlbumReco, ArtistReco, ExternalCarousels, ExtHistory, LocalHistory, RecoSource, TrackReco,
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
//! Weekly Exploration/Jams (ListenBrainz), Deep-cut albums, and a Qobuz editorial
|
||||
//! cold-start fallback.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
@@ -18,9 +19,9 @@ use qbz_app::shell::AppRuntime;
|
||||
use qbz_external_reco::{
|
||||
build_deep_cut_albums, build_editorial, build_fresh_releases, build_rec_albums,
|
||||
build_rec_artists_common, build_rec_artists_recent, build_similar_albums_seeded,
|
||||
build_weekly_exploration, build_weekly_jams, gather_history, is_cold_start, AlbumReco,
|
||||
ArtistReco, ExternalCarousels, LastFmHandle, ListenBrainzHandle, LocalHistory, RecoCache,
|
||||
RecoCatalog, RecoInputs, TrackReco,
|
||||
build_weekly_exploration, build_weekly_jams, compose_artist_rails, gather_history,
|
||||
is_cold_start, AlbumReco, ArtistReco, ExternalCarousels, LastFmHandle, ListenBrainzHandle,
|
||||
LocalHistory, RecoCache, RecoCatalog, RecoInputs, TrackReco, ARTIST_DISPLAY_CAP,
|
||||
};
|
||||
use qbz_integrations::{LastFmClient, ListenBrainzClient, MusicBrainzClient};
|
||||
use qbz_models::{Album, Artist, Track};
|
||||
@@ -32,6 +33,15 @@ use crate::{AlbumCardItem, AppWindow, DiscoverSection, ExternalRecoState, SlimIt
|
||||
|
||||
static CACHE_DIR: Mutex<Option<PathBuf>> = Mutex::new(None);
|
||||
|
||||
/// Retained per-rail overflow (validated candidates past the visible cap) for
|
||||
/// the two Recommended-Artist rails — (common, recent). Kept for live
|
||||
/// backfill after a "not interested" dismissal, so a replacement card needs
|
||||
/// no extra network. Rewritten on every rail paint (fresh build AND cached
|
||||
/// blob paint; old blobs carry no overflow, so they simply don't backfill
|
||||
/// until they expire).
|
||||
static ARTIST_OVERFLOW: Mutex<(Vec<ArtistReco>, Vec<ArtistReco>)> =
|
||||
Mutex::new((Vec::new(), Vec::new()));
|
||||
|
||||
pub fn init_for_user(base_dir: &Path) {
|
||||
if let Ok(mut g) = CACHE_DIR.lock() {
|
||||
*g = Some(base_dir.to_path_buf());
|
||||
@@ -390,19 +400,21 @@ fn spawn(
|
||||
let history = gather_history(&inputs).await;
|
||||
let col = &collector;
|
||||
// Progressive: each branch paints its row AND collects it for the cache.
|
||||
let b_common = async {
|
||||
let r = build_rec_artists_common(&inputs, &history).await;
|
||||
// The two artist rails build in parallel but paint TOGETHER through
|
||||
// apply_artist_rails (the shared filter/dedup/backfill choke point —
|
||||
// cross-rail dedup needs both pools). The collector stores the FULL
|
||||
// validated pools (visible + overflow), so the results blob carries
|
||||
// the backfill candidates too.
|
||||
let b_artists = async {
|
||||
let (common_pool, recent_pool) = tokio::join!(
|
||||
build_rec_artists_common(&inputs, &history),
|
||||
build_rec_artists_recent(&inputs, &history),
|
||||
);
|
||||
if let Ok(mut g) = col.lock() {
|
||||
g.rec_artists_common = r.clone();
|
||||
g.rec_artists_common = common_pool.clone();
|
||||
g.rec_artists_recent = recent_pool.clone();
|
||||
}
|
||||
apply_artists(&weak, &image_cache, r, ArtistRow::RecArtistsCommon);
|
||||
};
|
||||
let b_recent = async {
|
||||
let r = build_rec_artists_recent(&inputs, &history).await;
|
||||
if let Ok(mut g) = col.lock() {
|
||||
g.rec_artists_recent = r.clone();
|
||||
}
|
||||
apply_artists(&weak, &image_cache, r, ArtistRow::RecArtistsRecent);
|
||||
apply_artist_rails(&weak, &image_cache, common_pool, recent_pool);
|
||||
};
|
||||
let b_albums = async {
|
||||
let r = build_rec_albums(&inputs, &history).await;
|
||||
@@ -439,7 +451,7 @@ fn spawn(
|
||||
}
|
||||
apply_albums(&weak, &image_cache, r, AlbumRow::DeepCuts);
|
||||
};
|
||||
tokio::join!(b_common, b_recent, b_albums, b_fresh, b_explore, b_jams, b_deep);
|
||||
tokio::join!(b_artists, b_albums, b_fresh, b_explore, b_jams, b_deep);
|
||||
}
|
||||
|
||||
// 3. Store the built result for instant future opens (48h TTL). GUARD
|
||||
@@ -522,8 +534,7 @@ fn clear_all_pending(w: &AppWindow) {
|
||||
/// their own per-week cache by `build_and_apply_weeklies`, so the blob can never
|
||||
/// pin a stale/empty weekly for the 48h window.
|
||||
fn apply_all(weak: &slint::Weak<AppWindow>, cache: &ImageCache, r: ExternalCarousels) {
|
||||
apply_artists(weak, cache, r.rec_artists_common, ArtistRow::RecArtistsCommon);
|
||||
apply_artists(weak, cache, r.rec_artists_recent, ArtistRow::RecArtistsRecent);
|
||||
apply_artist_rails(weak, cache, r.rec_artists_common, r.rec_artists_recent);
|
||||
apply_albums(weak, cache, r.rec_albums, AlbumRow::RecAlbums);
|
||||
apply_albums(weak, cache, r.fresh_releases, AlbumRow::FreshReleases);
|
||||
apply_albums(weak, cache, r.deep_cut_albums, AlbumRow::DeepCuts);
|
||||
@@ -531,6 +542,41 @@ fn apply_all(weak: &slint::Weak<AppWindow>, cache: &ImageCache, r: ExternalCarou
|
||||
apply_artists(weak, cache, r.top_artists, ArtistRow::TopArtists);
|
||||
}
|
||||
|
||||
/// Fold the three per-user exclusion sources — followed artists, the app-wide
|
||||
/// blacklist, and the reco-scoped "not interested" dismissals — into one id
|
||||
/// set for the rail composition. Every source is independently fail-open (an
|
||||
/// unbound/unreadable store simply contributes nothing).
|
||||
fn artist_exclusions() -> HashSet<u64> {
|
||||
let mut ids = crate::fav_cache::all_artists();
|
||||
ids.extend(crate::artist_blacklist::ids_snapshot());
|
||||
ids.extend(crate::reco_dismiss::ids_snapshot());
|
||||
ids
|
||||
}
|
||||
|
||||
/// THE paint choke point for the two Recommended-Artist rails — the fresh
|
||||
/// build AND the cached-blob paint both funnel through here. Composes the
|
||||
/// visible rows from the validated pools AFTER exclusion filtering +
|
||||
/// cross-rail dedup (common wins), takes the first ARTIST_DISPLAY_CAP
|
||||
/// survivors per rail, and retains the rest as backfill overflow for the
|
||||
/// "not interested" live replacement (compose_artist_rails does the split;
|
||||
/// the full pools ride the results blob, so a cached paint backfills too).
|
||||
fn apply_artist_rails(
|
||||
weak: &slint::Weak<AppWindow>,
|
||||
cache: &ImageCache,
|
||||
common_pool: Vec<ArtistReco>,
|
||||
recent_pool: Vec<ArtistReco>,
|
||||
) {
|
||||
let excluded = artist_exclusions();
|
||||
let (common, recent) =
|
||||
compose_artist_rails(common_pool, recent_pool, &excluded, ARTIST_DISPLAY_CAP);
|
||||
if let Ok(mut g) = ARTIST_OVERFLOW.lock() {
|
||||
g.0 = common.overflow;
|
||||
g.1 = recent.overflow;
|
||||
}
|
||||
apply_artists(weak, cache, common.visible, ArtistRow::RecArtistsCommon);
|
||||
apply_artists(weak, cache, recent.visible, ArtistRow::RecArtistsRecent);
|
||||
}
|
||||
|
||||
/// Build + paint the two Weekly rows from their own per-week cache (cheap on a
|
||||
/// hit; one ListenBrainz `createdfor` call + a SQLite read). Used on the
|
||||
/// instant-paint path so the weeklies follow ListenBrainz's weekly cadence
|
||||
|
||||
@@ -272,6 +272,12 @@ pub fn is_artist_favorite(artist_id: u64) -> bool {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Snapshot of the full followed-artist id set — the reco paint filter's
|
||||
/// "already following" exclusion input (mirrors `all()` for tracks).
|
||||
pub fn all_artists() -> HashSet<u64> {
|
||||
FAV_ARTISTS.read().map(|g| g.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Replace the followed-artist set with a freshly-fetched id list and mirror
|
||||
/// it to the per-user store (full replace — same lifecycle as the album set).
|
||||
/// Blocking disk write; call off the UI thread.
|
||||
|
||||
Reference in new issue
Block a user