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
This commit is contained in:
vicrodh committed 2026-06-26 19:28:07 -06:00
1 parent 4828e54cc3
commit 14b8a4006a
2 files changed
+26 -2

No files matched your search

+6 -1
View File
@@ -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.
+20 -1
View File
@@ -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
// ---------------------------------------------------------------------------