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: preserve distinct audio segments in search results

Ezra Elletteacf844b85625
diff --git a/crates/screenpipe-db/src/db/search.rs b/crates/screenpipe-db/src/db/search.rs
index bad4290c1..aad0c5c18 100644
--- a/crates/screenpipe-db/src/db/search.rs
+++ b/crates/screenpipe-db/src/db/search.rs
@@ -1202,9 +1202,10 @@ impl DatabaseManager {
format!("WHERE {}", conditions.join(" AND "))
};
- // complete sql with group, order, limit and offset
+ // Group only tag-join duplicates. Distinct speech segments can share a
+ // chunk and offset_index, so grouping by those loses recorded speech.
let sql = format!(
- "{} {} GROUP BY audio_transcriptions.audio_chunk_id, audio_transcriptions.offset_index ORDER BY audio_transcriptions.timestamp {order_dir}, audio_transcriptions.audio_chunk_id {order_dir}, audio_transcriptions.offset_index {order_dir} LIMIT ? OFFSET ?",
+ "{} {} GROUP BY audio_transcriptions.id ORDER BY audio_transcriptions.timestamp {order_dir}, audio_transcriptions.audio_chunk_id {order_dir}, audio_transcriptions.offset_index {order_dir}, audio_transcriptions.id {order_dir} LIMIT ? OFFSET ?",
base_sql,
where_clause,
order_dir = match order {
diff --git a/crates/screenpipe-db/tests/audio_segment_search_test.rs b/crates/screenpipe-db/tests/audio_segment_search_test.rs
new file mode 100644
index 000000000..e0e157064
--- /dev/null
+++ b/crates/screenpipe-db/tests/audio_segment_search_test.rs
@@ -0,0 +1,98 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+use screenpipe_db::{AudioDevice, DatabaseManager, DeviceType, Order, TagContentType};
+
+#[tokio::test]
+async fn preserves_segments_with_shared_chunk_offset_and_paginates_without_tag_duplicates() {
+ let temp = tempfile::tempdir().unwrap();
+ let db = DatabaseManager::new(
+ temp.path().join("audio.sqlite").to_str().unwrap(),
+ Default::default(),
+ )
+ .await
+ .unwrap();
+ let chunk = db
+ .insert_audio_chunk("realistic-segmented-speech.mp4", None)
+ .await
+ .unwrap();
+ let device = AudioDevice {
+ name: "System Audio".into(),
+ device_type: DeviceType::Output,
+ };
+ let captured = chrono::Utc::now();
+ for (index, text) in [
+ "Juniper artwork",
+ "requires review",
+ "before customer sharing",
+ ]
+ .iter()
+ .enumerate()
+ {
+ db.insert_audio_transcription(
+ chunk,
+ text,
+ 0,
+ "test",
+ &device,
+ None,
+ Some(index as f64 * 2.0),
+ Some(index as f64 * 2.0 + 2.0),
+ Some(captured),
+ )
+ .await
+ .unwrap();
+ }
+ db.add_tags(
+ chunk,
+ TagContentType::Audio,
+ vec!["juniper".into(), "review".into()],
+ )
+ .await
+ .unwrap();
+ for (order, expected) in [
+ (
+ Order::Ascending,
+ vec![
+ "Juniper artwork",
+ "requires review",
+ "before customer sharing",
+ ],
+ ),
+ (
+ Order::Descending,
+ vec![
+ "before customer sharing",
+ "requires review",
+ "Juniper artwork",
+ ],
+ ),
+ ] {
+ let mut observed = Vec::new();
+ for offset in 0..4 {
+ let page = db
+ .search_audio_ordered(
+ "",
+ 1,
+ offset,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ None,
+ &[],
+ order,
+ )
+ .await
+ .unwrap();
+ for segment in page {
+ assert_eq!(segment.tags.len(), 2);
+ observed.push(segment.transcription);
+ }
+ }
+ assert_eq!(observed, expected);
+ }
+}