Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: seed all speakers to prevent fragmentation (#2969) (#3077)

Louis Beaumont755733f15b05
diff --git a/crates/screenpipe-audio/src/audio_manager/manager.rs b/crates/screenpipe-audio/src/audio_manager/manager.rs
index aceea356f..42e5e021f 100644
--- a/crates/screenpipe-audio/src/audio_manager/manager.rs
+++ b/crates/screenpipe-audio/src/audio_manager/manager.rs
@@ -1295,23 +1295,27 @@ impl AudioManager {
/// This allows returning voices to be recognized immediately instead of
/// starting anonymous for the first 30+ seconds.
async fn seed_speakers_from_db(db: &Arc<DatabaseManager>, seg_mgr: &Arc<SegmentationManager>) {
- match db.get_named_speakers_with_centroids().await {
+ // Seed all speakers (named and unnamed) to prevent re-creation of existing voices.
+ // Limit to 500 most recent speakers to avoid memory bloat on long-running systems.
+ const MAX_SPEAKERS_TO_SEED: usize = 500;
+
+ match db.get_all_speakers_with_centroids(MAX_SPEAKERS_TO_SEED).await {
Ok(speakers) if !speakers.is_empty() => {
for (_db_id, name, centroid) in &speakers {
let emb = ndarray::Array1::from_vec(centroid.clone());
seg_mgr.seed_speaker(emb);
- debug!("seeded known speaker '{}' into embedding manager", name);
+ debug!("seeded speaker '{}' into embedding manager", name);
}
info!(
- "seeded {} known speakers from DB into embedding manager",
+ "seeded {} speakers (named + unnamed) from DB into embedding manager",
speakers.len()
);
}
Ok(_) => {
- debug!("no named speakers with centroids found in DB to seed");
+ debug!("no speakers with centroids found in DB to seed");
}
Err(e) => {
- warn!("failed to query named speakers for seeding: {}", e);
+ warn!("failed to query speakers for seeding: {}", e);
}
}
}
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index 4e85c70d9..142117b19 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -1431,6 +1431,29 @@ impl DatabaseManager {
.fetch_optional(&self.pool)
.await?;
+ if speaker.is_none() {
+ // Log the closest distance for debugging speaker fragmentation issues
+ let closest: Option<(f32,)> = sqlx::query_as(
+ "SELECT vec_distance_cosine(centroid, vec_f32(?1))
+ FROM speakers
+ WHERE centroid IS NOT NULL
+ ORDER BY vec_distance_cosine(centroid, vec_f32(?1))
+ LIMIT 1",
+ )
+ .bind(bytes)
+ .fetch_optional(&self.pool)
+ .await
+ .ok()
+ .flatten();
+
+ if let Some((distance,)) = closest {
+ debug!(
+ "speaker embedding match failed: threshold={}, closest_distance={}",
+ speaker_threshold, distance
+ );
+ }
+ }
+
Ok(speaker)
}
@@ -1589,6 +1612,40 @@ impl DatabaseManager {
.collect())
}
+ /// Get ALL speakers with non-null centroids (including unnamed ones) for seeding.
+ /// Limit to the N most recent speakers to avoid memory bloat on long-running systems.
+ /// Returns (speaker_id, name, centroid as Vec<f32>).
+ pub async fn get_all_speakers_with_centroids(
+ &self,
+ limit: usize,
+ ) -> Result<Vec<(i64, String, Vec<f32>)>, SqlxError> {
+ let rows: Vec<(i64, Option<String>, Vec<u8>)> = sqlx::query_as(
+ "SELECT id, name, centroid FROM speakers \
+ WHERE centroid IS NOT NULL \
+ AND (hallucination IS NULL OR hallucination = 0) \
+ ORDER BY id DESC LIMIT ?1",
+ )
+ .bind(limit as i64)
+ .fetch_all(&self.pool)
+ .await?;
+
+ Ok(rows
+ .into_iter()
+ .filter_map(|(id, name, blob)| {
+ if blob.len() == 512 * 4 {
+ let floats: Vec<f32> = blob
+ .chunks_exact(4)
+ .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
+ .collect();
+ let name_str = name.unwrap_or_else(|| format!("speaker_{}", id));
+ Some((id, name_str, floats))
+ } else {
+ None
+ }
+ })
+ .collect())
+ }
+
/// Get speakers active during a time range, grouped by device type.
/// Returns speaker_id, current name, device type, and transcription count.
/// Excludes hallucination speakers.