From 14b8a4006a8a8d135cee527f3ecf7c392ab7759f Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 26 Jun 2026 19:28:07 -0600 Subject: [PATCH] feat(reco): train recommendation scores after session activation Fire RecoStore::train() fire-and-forget (spawn_blocking) right after the store is opened on login and session-restore, mirroring Tauri's non-awaited trainScores. Off-thread, default decay/weight params, no-op when reco is disabled. Offline entry stays init-only. cc --- crates/qbz/src/auth.rs | 7 ++++++- crates/qbz/src/reco.rs | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/qbz/src/auth.rs b/crates/qbz/src/auth.rs index 23abea338..94c7381a8 100644 --- a/crates/qbz/src/auth.rs +++ b/crates/qbz/src/auth.rs @@ -110,8 +110,10 @@ where if let Some(dir) = crate::offline_mode::user_data_dir(user_id) { crate::offline_mode::init_for_user(&dir); crate::fav_cache::init_for_user(&dir); - // Recommendation event store (shared events.db with Tauri). + // Recommendation event store (shared events.db with Tauri). Train + // after init (off-thread) so the seeds reflect this session's events. crate::reco::init_for_user(&dir); + crate::reco::train_async(); crate::discover_prefs::init_for_user(&dir); crate::artist_blacklist::init_for_user(&dir); // Intelligent Search (cache + ranking), seeded from the persisted pref. @@ -229,7 +231,10 @@ where crate::offline_mode::init_for_user(&dir); crate::fav_cache::init_for_user(&dir); // Recommendation event store (shared events.db with Tauri). + // Train after init (off-thread) so the seeds reflect this + // session's events. crate::reco::init_for_user(&dir); + crate::reco::train_async(); crate::discover_prefs::init_for_user(&dir); crate::artist_blacklist::init_for_user(&dir); // Intelligent Search (cache + ranking), seeded from the pref. diff --git a/crates/qbz/src/reco.rs b/crates/qbz/src/reco.rs index b4e4da446..5afbb5aa5 100644 --- a/crates/qbz/src/reco.rs +++ b/crates/qbz/src/reco.rs @@ -15,7 +15,7 @@ use std::path::Path; use std::sync::Mutex; -use qbz_app::settings::reco_store::{HomeSeedLimits, HomeSeeds, RecoStore}; +use qbz_app::settings::reco_store::{HomeSeedLimits, HomeSeeds, RecoStore, TrainParams}; /// Per-user reco event store. `None` until a session (online or offline) is /// activated; every helper is a no-op in that window. @@ -92,6 +92,25 @@ pub fn log_play_gated( false } +// --------------------------------------------------------------------------- +// Training (W5) +// --------------------------------------------------------------------------- + +/// Recompute reco scores off-thread, fire-and-forget (mirrors Tauri's +/// non-awaited `trainScores` after login). Never blocks the caller; uses the +/// engine's default decay/weight params. No-op when reco is disabled. +pub fn train_async() { + tokio::task::spawn_blocking(|| { + if let Ok(mut guard) = RECO.lock() { + if let Some(store) = guard.as_mut() { + if let Err(e) = store.train(TrainParams::default()) { + log::warn!("[reco] train failed: {e}"); + } + } + } + }); +} + // --------------------------------------------------------------------------- // Tests // ---------------------------------------------------------------------------