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(retention): "delete last N min" actually unlinks mp4/wav + drops cached frames
Louis Beaumont328a3b48f96c
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rsindex e4eaa6210..3c7b6d85f 100644--- a/crates/screenpipe-db/src/db.rs+++ b/crates/screenpipe-db/src/db.rs@@ -4627,10 +4627,16 @@ impl DatabaseManager {// 1. Collect video file paths for chunks that become fully orphaned.// Only include files that have been uploaded to cloud (cloud_blob_id IS NOT NULL)// or files not managed by archive (no cloud tracking needed for non-archive deletes).+ // NOTE: filter out NULL video_chunk_id in the NOT IN subquery — SQL `x NOT IN+ // (NULL, ...)` evaluates to UNKNOWN for every row, silently zeroing out the+ // result set. frames.video_chunk_id is nullable (snapshot-only frames have no+ // mp4 chunk), so without this filter the entire deletion returned 0 files.let video_files: Vec<String> = sqlx::query_scalar(r#"SELECT file_path FROM video_chunks- WHERE id IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)- AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp NOT BETWEEN ?1 AND ?2)+ WHERE id IN (SELECT DISTINCT video_chunk_id FROM frames+ WHERE timestamp BETWEEN ?1 AND ?2 AND video_chunk_id IS NOT NULL)+ AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames+ WHERE timestamp NOT BETWEEN ?1 AND ?2 AND video_chunk_id IS NOT NULL)AND (cloud_blob_id IS NOT NULL OR file_path LIKE 'cloud://%')"#,).bind(&start_str)@@ -4650,11 +4656,14 @@ impl DatabaseManager {.fetch_all(&mut **tx.conn()).await?;- // 2. Collect audio file paths for chunks that become fully orphaned+ // 2. Collect audio file paths for chunks that become fully orphaned.+ // Same NULL-in-NOT-IN pitfall as above — filter NULL audio_chunk_id explicitly.let audio_files: Vec<String> = sqlx::query_scalar(r#"SELECT file_path FROM audio_chunks- WHERE id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp BETWEEN ?1 AND ?2)- AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp NOT BETWEEN ?1 AND ?2)+ WHERE id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions+ WHERE timestamp BETWEEN ?1 AND ?2 AND audio_chunk_id IS NOT NULL)+ AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions+ WHERE timestamp NOT BETWEEN ?1 AND ?2 AND audio_chunk_id IS NOT NULL)AND file_path NOT LIKE 'cloud://%'"#,).bind(&start_str)@@ -4819,11 +4828,20 @@ impl DatabaseManager {let start_str = start.to_rfc3339();let end_str = end.to_rfc3339();- // 1. Collect ALL video file paths for chunks that become fully orphaned+ // 1. Collect ALL video file paths for chunks that become fully orphaned.+ // SQL `x NOT IN (..., NULL)` evaluates to UNKNOWN for every row, which+ // makes the whole WHERE clause silently filter out *everything*.+ // frames.video_chunk_id is nullable (snapshot-only frames carry no+ // mp4 chunk reference), so the inner subquery must exclude NULLs+ // explicitly — otherwise the user clicks "delete last 15 minutes"+ // and the API responds with 0 files deleted while the mp4s stay on+ // disk.let video_files: Vec<String> = sqlx::query_scalar(r#"SELECT file_path FROM video_chunks- WHERE id IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)- AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp NOT BETWEEN ?1 AND ?2)+ WHERE id IN (SELECT DISTINCT video_chunk_id FROM frames+ WHERE timestamp BETWEEN ?1 AND ?2 AND video_chunk_id IS NOT NULL)+ AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames+ WHERE timestamp NOT BETWEEN ?1 AND ?2 AND video_chunk_id IS NOT NULL)AND file_path NOT LIKE 'cloud://%'"#,).bind(&start_str)@@ -4842,11 +4860,15 @@ impl DatabaseManager {.fetch_all(&mut **tx.conn()).await?;- // 3. Collect ALL audio file paths for chunks that become fully orphaned+ // 3. Collect ALL audio file paths for chunks that become fully orphaned.+ // Same NULL-in-NOT-IN guard as above (audio_transcriptions.audio_chunk_id+ // can be NULL for orphaned realtime transcript fragments).let audio_files: Vec<String> = sqlx::query_scalar(r#"SELECT file_path FROM audio_chunks- WHERE id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp BETWEEN ?1 AND ?2)- AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp NOT BETWEEN ?1 AND ?2)+ WHERE id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions+ WHERE timestamp BETWEEN ?1 AND ?2 AND audio_chunk_id IS NOT NULL)+ AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions+ WHERE timestamp NOT BETWEEN ?1 AND ?2 AND audio_chunk_id IS NOT NULL)AND file_path NOT LIKE 'cloud://%'"#,).bind(&start_str)@@ -5016,13 +5038,19 @@ impl DatabaseManager {// Collect video chunks fully covered by the range and not already// evicted. We only consider chunks whose ALL frames fall inside the// window — straddling chunks are skipped so old playback still works.+ // NOT IN (subquery) silently filters out everything if the subquery+ // contains NULL — frames.video_chunk_id is nullable. Same trap applies+ // to audio_transcriptions.audio_chunk_id. Filter NULLs in the inner+ // SELECT.let video_files: Vec<String> = sqlx::query_scalar(r#"SELECT file_path FROM video_chunksWHERE evicted_at IS NULLAND file_path != ''AND file_path NOT LIKE 'cloud://%'- AND id IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)- AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames WHERE timestamp NOT BETWEEN ?1 AND ?2)"#,+ AND id IN (SELECT DISTINCT video_chunk_id FROM frames+ WHERE timestamp BETWEEN ?1 AND ?2 AND video_chunk_id IS NOT NULL)+ AND id NOT IN (SELECT DISTINCT video_chunk_id FROM frames+ WHERE timestamp NOT BETWEEN ?1 AND ?2 AND video_chunk_id IS NOT NULL)"#,).bind(&start_str).bind(&end_str)@@ -5034,8 +5062,10 @@ impl DatabaseManager {WHERE evicted_at IS NULLAND file_path != ''AND file_path NOT LIKE 'cloud://%'- AND id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp BETWEEN ?1 AND ?2)- AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions WHERE timestamp NOT BETWEEN ?1 AND ?2)"#,+ AND id IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions+ WHERE timestamp BETWEEN ?1 AND ?2 AND audio_chunk_id IS NOT NULL)+ AND id NOT IN (SELECT DISTINCT audio_chunk_id FROM audio_transcriptions+ WHERE timestamp NOT BETWEEN ?1 AND ?2 AND audio_chunk_id IS NOT NULL)"#,).bind(&start_str).bind(&end_str)diff --git a/crates/screenpipe-engine/src/hot_frame_cache.rs b/crates/screenpipe-engine/src/hot_frame_cache.rsindex 2e8e185fd..8cb467e41 100644--- a/crates/screenpipe-engine/src/hot_frame_cache.rs+++ b/crates/screenpipe-engine/src/hot_frame_cache.rs@@ -288,6 +288,30 @@ impl HotFrameCache {}impl HotFrameCache {+ /// Drop every cached frame and audio entry whose timestamp falls in+ /// `[start, end]`. Called after a destructive delete in the storage UI+ /// so the timeline streamer doesn't re-emit ghost frames pointing at+ /// files we just removed (which is what made the timeline "jump+ /// backward" right after the user clicked delete-last-15-min).+ pub async fn evict_range(&self, start: DateTime<Utc>, end: DateTime<Utc>) {+ {+ let mut frames = self.frames.write().await;+ frames.retain(|(ts, _), _| *ts < start || *ts > end);+ }+ {+ let mut audio = self.audio.write().await;+ audio.retain(|ts, _| *ts < start || *ts > end);+ }+ // Push warm_start forward if it now points inside the evicted range.+ let mut ws = self.cache_warm_start.write().await;+ if let Some(existing) = *ws {+ if existing >= start && existing <= end {+ let frames = self.frames.read().await;+ *ws = frames.keys().next().map(|(t, _)| *t);+ }+ }+ }+/// Public wrapper: find audio entries near a given timestamp./// Used by the streaming handler to attach audio to live frames.pub async fn find_audio_near(&self, frame_ts: DateTime<Utc>) -> Vec<AudioEntry> {diff --git a/crates/screenpipe-engine/src/routes/data.rs b/crates/screenpipe-engine/src/routes/data.rsindex c1cb680c4..c606df8ac 100644--- a/crates/screenpipe-engine/src/routes/data.rs+++ b/crates/screenpipe-engine/src/routes/data.rs@@ -93,6 +93,15 @@ pub(crate) async fn delete_time_range_handler(}}+ // Evict the range from the in-memory hot frame cache. Without this the+ // /stream/frames WS keeps re-emitting cached entries that point at+ // mp4/jpeg files we just unlinked — which made the timeline "jump+ // backward" right after the user clicked delete-last-15-min.+ state+ .hot_frame_cache+ .evict_range(payload.start, payload.end)+ .await;+Ok(JsonResponse(DeleteTimeRangeResponse {frames_deleted: result.frames_deleted,ocr_deleted: result.ocr_deleted,@@ -206,6 +215,13 @@ pub(crate) async fn evict_media_handler(}}+ // Hot cache holds frame metadata that points at the evicted media files;+ // drop those entries so streaming consumers don't get 404s on /frames/:id.+ state+ .hot_frame_cache+ .evict_range(payload.start, payload.end)+ .await;+Ok(JsonResponse(EvictMediaResponse {video_chunks_evicted: result.video_chunks_evicted,audio_chunks_evicted: result.audio_chunks_evicted,