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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: content_type=all search returns wrong count and broken pagination
Louis Beaumont039d5fea02f6
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rsindex ac983f060..b75b4ca12 100644--- a/crates/screenpipe-db/src/db.rs+++ b/crates/screenpipe-db/src/db.rs@@ -1871,14 +1871,19 @@ impl DatabaseManager {match content_type {ContentType::All => {+ // For All: each sub-function must fetch enough rows to cover the+ // global pagination window. We pass limit+offset with offset=0 to+ // each, then apply skip(offset).take(limit) once on the merged set.+ let fetch_limit = limit.saturating_add(offset);+let (ocr_results, audio_results, ui_results) =if app_name.is_none() && window_name.is_none() && frame_name.is_none() {// Run all three queries in parallellet (ocr, audio, ui) = tokio::try_join!(self.search_ocr(query,- limit,- offset,+ fetch_limit,+ 0,start_time,end_time,app_name,@@ -1891,8 +1896,8 @@ impl DatabaseManager {),self.search_audio(query,- limit,- offset,+ fetch_limit,+ 0,start_time,end_time,min_length,@@ -1906,8 +1911,8 @@ impl DatabaseManager {window_name,start_time,end_time,- limit,- offset,+ fetch_limit,+ 0,))?;(ocr, Some(audio), ui)@@ -1916,8 +1921,8 @@ impl DatabaseManager {let (ocr, ui) = tokio::try_join!(self.search_ocr(query,- limit,- offset,+ fetch_limit,+ 0,start_time,end_time,app_name,@@ -1934,8 +1939,8 @@ impl DatabaseManager {window_name,start_time,end_time,- limit,- offset,+ fetch_limit,+ 0,))?;(ocr, None, ui)@@ -2032,6 +2037,16 @@ impl DatabaseManager {timestamp_b.cmp(×tamp_a)});+ // For ContentType::All, sub-functions each fetched limit+offset rows+ // with offset=0. Now apply pagination once on the globally-sorted set.+ if matches!(content_type, ContentType::All) {+ results = results+ .into_iter()+ .skip(offset as usize)+ .take(limit as usize)+ .collect();+ }+Ok(results)}diff --git a/crates/screenpipe-db/tests/search_ocr_snapshot_test.rs b/crates/screenpipe-db/tests/search_ocr_snapshot_test.rsindex 75252b671..52700eca1 100644--- a/crates/screenpipe-db/tests/search_ocr_snapshot_test.rs+++ b/crates/screenpipe-db/tests/search_ocr_snapshot_test.rs@@ -5,11 +5,14 @@//! Regression tests for search bugs://! - #2336: search_ocr() INNER JOIN video_chunks dropped snapshot frames//! - #2339: search() double-applied offset (SQL + Rust skip) — pages beyond first empty+//! - content_type=all pagination: limit not enforced on merged results#[cfg(test)]mod tests {use chrono::Utc;- use screenpipe_db::{ContentType, DatabaseManager, OcrEngine, SearchResult};+ use screenpipe_db::{+ AudioDevice, ContentType, DatabaseManager, DeviceType, OcrEngine, SearchResult,+ };use std::sync::Arc;async fn setup_test_db() -> DatabaseManager {@@ -172,4 +175,205 @@ mod tests {.unwrap();assert_eq!(page3.len(), 1, "page 3 should have 1 result");}++ /// Bug: ContentType::All with limit=2 can return more than 2 results.+ ///+ /// Each sub-function (search_ocr, search_audio) gets the user's limit+ /// independently in SQL. The merged results are sorted but never capped,+ /// so the caller can receive up to limit * N_content_types items.+ #[tokio::test]+ async fn test_search_all_limit_exceeded() {+ let db = setup_test_db().await;++ let device = AudioDevice {+ name: "test_mic".to_string(),+ device_type: DeviceType::Input,+ };++ // Insert 3 OCR items matching "allbug"+ for i in 0..3 {+ let frame_id = db+ .insert_snapshot_frame(+ "test_device",+ Utc::now(),+ &format!("/tmp/allbug_ocr_{}.jpg", i),+ Some("test_app"),+ Some("test_window"),+ None,+ true,+ None,+ None,+ None,+ None,+ None,+ None,+ )+ .await+ .unwrap();++ db.insert_ocr_text(+ frame_id,+ &format!("allbug ocr content number {}", i),+ "",+ Arc::new(OcrEngine::Tesseract),+ )+ .await+ .unwrap();+ }++ // Insert 3 audio items matching "allbug"+ for i in 0..3 {+ let chunk_id = db+ .insert_audio_chunk(&format!("/tmp/allbug_audio_{}.wav", i), None)+ .await+ .unwrap();++ db.insert_audio_transcription(+ chunk_id,+ &format!("allbug audio transcript unique item {}", i),+ i as i64,+ "test_engine",+ &device,+ None,+ None,+ None,+ None,+ )+ .await+ .unwrap();+ }++ // Search with ContentType::All and limit=2+ // Expected: at most 2 results total+ // Actual bug: returns up to 4 (2 from OCR + 2 from audio)+ let results = db+ .search(+ "allbug",+ ContentType::All,+ 2,+ 0,+ None, None, None, None, None, None, None, None, None, None, None,+ )+ .await+ .unwrap();++ // This assertion demonstrates the bug — it will FAIL because+ // the code returns more than the requested limit of 2+ assert!(+ results.len() <= 2,+ "ContentType::All with limit=2 should return at most 2 results, got {}",+ results.len()+ );+ }++ /// Bug: ContentType::All pagination is broken — page 2 returns 0 items.+ ///+ /// Each sub-function applies offset independently in SQL, so with+ /// offset=3, both OCR (3 items) and audio (3 items) skip past all+ /// their own items, returning nothing. Meanwhile page 1 returns+ /// ALL 6 items (exceeding the limit of 3).+ #[tokio::test]+ async fn test_search_all_pagination_wrong_page_two() {+ let db = setup_test_db().await;++ let device = AudioDevice {+ name: "test_mic".to_string(),+ device_type: DeviceType::Input,+ };++ // Insert 3 OCR items+ for i in 0..3 {+ let frame_id = db+ .insert_snapshot_frame(+ "test_device",+ Utc::now(),+ &format!("/tmp/pagebug_ocr_{}.jpg", i),+ Some("test_app"),+ Some("test_window"),+ None,+ true,+ None,+ None,+ None,+ None,+ None,+ None,+ )+ .await+ .unwrap();++ db.insert_ocr_text(+ frame_id,+ &format!("pagebug screen text item {}", i),+ "",+ Arc::new(OcrEngine::Tesseract),+ )+ .await+ .unwrap();+ }++ // Insert 3 audio items+ for i in 0..3 {+ let chunk_id = db+ .insert_audio_chunk(&format!("/tmp/pagebug_audio_{}.wav", i), None)+ .await+ .unwrap();++ db.insert_audio_transcription(+ chunk_id,+ &format!("pagebug audio recording unique {}", i),+ i as i64,+ "test_engine",+ &device,+ None,+ None,+ None,+ None,+ )+ .await+ .unwrap();+ }++ // Total: 6 items matching "pagebug".+ // Page 1 (limit=3, offset=0): should return exactly 3+ let page1 = db+ .search(+ "pagebug",+ ContentType::All,+ 3,+ 0,+ None, None, None, None, None, None, None, None, None, None, None,+ )+ .await+ .unwrap();++ // Bug: page 1 returns 6 items (3 OCR + 3 audio) instead of 3+ assert_eq!(+ page1.len(),+ 3,+ "page 1 with limit=3 should return exactly 3 results, got {}",+ page1.len()+ );++ // Page 2 (limit=3, offset=3): should return the remaining 3+ let page2 = db+ .search(+ "pagebug",+ ContentType::All,+ 3,+ 3,+ None, None, None, None, None, None, None, None, None, None, None,+ )+ .await+ .unwrap();++ // Bug: page 2 returns 0 items (each sub-function independently+ // skips 3, but each type only has 3 items → nothing left)+ assert_eq!(+ page2.len(),+ 3,+ "page 2 with limit=3 should return 3 results, got {}",+ page2.len()+ );+ }}