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: update hot frame cache after snapshot compaction to prevent black frames
Louis Beaumonta76a11fb2794
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rsindex 9821caf8b..bd8167e28 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs@@ -377,6 +377,7 @@ pub async fn start_embedded_server(config.video_quality.clone(),shutdown_tx_clone.subscribe(),power_manager.clone(),+ Some(hot_frame_cache.clone()),);// Create and start HTTP serverdiff --git a/crates/screenpipe-server/src/bin/screenpipe-server.rs b/crates/screenpipe-server/src/bin/screenpipe-server.rsindex f8cf4a6a9..45f5477fc 100644--- a/crates/screenpipe-server/src/bin/screenpipe-server.rs+++ b/crates/screenpipe-server/src/bin/screenpipe-server.rs@@ -712,6 +712,7 @@ async fn main() -> anyhow::Result<()> {config.video_quality.clone(),shutdown_tx.subscribe(),power_manager.clone(),+ Some(hot_frame_cache.clone()),);// Create VisionManager for event-driven capture on all monitorsdiff --git a/crates/screenpipe-server/src/hot_frame_cache.rs b/crates/screenpipe-server/src/hot_frame_cache.rsindex 8cf71f45f..2e8e185fd 100644--- a/crates/screenpipe-server/src/hot_frame_cache.rs+++ b/crates/screenpipe-server/src/hot_frame_cache.rs@@ -294,6 +294,29 @@ impl HotFrameCache {let audio_map = self.audio.read().await;find_audio_for_frame(&audio_map, frame_ts)}++ /// Update cached frames after snapshot compaction.+ /// Replaces snapshot_path with the MP4 chunk path and sets offset_index/fps+ /// so the frontend uses video seek instead of direct file loading.+ pub async fn update_compacted_frames(+ &self,+ compacted: &[(i64, String, i64, f64)], // (frame_id, mp4_path, offset_index, fps)+ ) {+ let mut frames = self.frames.write().await;+ // Build a lookup by frame_id for O(n) scan+ let mut updates: std::collections::HashMap<i64, (&str, i64, f64)> =+ std::collections::HashMap::with_capacity(compacted.len());+ for (fid, path, offset, fps) in compacted {+ updates.insert(*fid, (path.as_str(), *offset, *fps));+ }+ for (_, hot) in frames.iter_mut() {+ if let Some((mp4_path, offset, fps)) = updates.get(&hot.frame_id) {+ hot.snapshot_path = mp4_path.to_string();+ hot.offset_index = *offset;+ hot.fps = *fps;+ }+ }+ }}/// Find audio entries within ±60s of a frame timestamp.diff --git a/crates/screenpipe-server/src/snapshot_compaction.rs b/crates/screenpipe-server/src/snapshot_compaction.rsindex f7afc9359..4edd3f885 100644--- a/crates/screenpipe-server/src/snapshot_compaction.rs+++ b/crates/screenpipe-server/src/snapshot_compaction.rs@@ -20,6 +20,7 @@ use std::sync::Arc;use tokio::sync::broadcast;use tracing::{debug, error, info, warn};+use crate::hot_frame_cache::HotFrameCache;use crate::power::{PowerManagerHandle, ThermalState};use crate::video::{finish_ffmpeg_process, video_quality_to_crf, write_frame_to_ffmpeg};@@ -42,6 +43,7 @@ pub fn start_snapshot_compaction(video_quality: String,mut shutdown_rx: broadcast::Receiver<()>,power_manager: Arc<PowerManagerHandle>,+ hot_frame_cache: Option<Arc<HotFrameCache>>,) {tokio::spawn(async move {info!(@@ -82,7 +84,7 @@ pub fn start_snapshot_compaction(};let compacted = tokio::select! {- result = run_compaction_cycle(&db, &video_quality, chunk_size) => {+ result = run_compaction_cycle(&db, &video_quality, chunk_size, &hot_frame_cache) => {match result {Ok(n) => n,Err(e) => {@@ -126,6 +128,7 @@ async fn run_compaction_cycle(db: &DatabaseManager,video_quality: &str,chunk_size: usize,+ hot_frame_cache: &Option<Arc<HotFrameCache>>,) -> Result<usize> {let cutoff = Utc::now() - Duration::seconds(MIN_AGE_SECS);@@ -159,13 +162,20 @@ async fn run_compaction_cycle(for (device_name, frames) in &by_device {for chunk in frames.chunks(chunk_size) {- if let Err(e) = compact_chunk(db, device_name, chunk, video_quality).await {- warn!(- "snapshot compaction failed for device {} ({} frames): {}",- device_name,- chunk.len(),- e- );+ match compact_chunk(db, device_name, chunk, video_quality).await {+ Ok(updates) => {+ if let (Some(cache), Some(updates)) = (hot_frame_cache, updates) {+ cache.update_compacted_frames(&updates).await;+ }+ }+ Err(e) => {+ warn!(+ "snapshot compaction failed for device {} ({} frames): {}",+ device_name,+ chunk.len(),+ e+ );+ }}}}@@ -174,14 +184,15 @@ async fn run_compaction_cycle(}/// Encode a batch of JPEG snapshots into a single MP4 chunk.+/// Returns cache update info: Vec<(frame_id, mp4_path, offset_index, fps)>.async fn compact_chunk(db: &DatabaseManager,device_name: &str,frames: &[(i64, String, String)], // (frame_id, snapshot_path, timestamp_str)video_quality: &str,-) -> Result<()> {+) -> Result<Option<Vec<(i64, String, i64, f64)>>> {if frames.is_empty() {- return Ok(());+ return Ok(None);}let first_path = Path::new(&frames[0].1);@@ -264,7 +275,7 @@ async fn compact_chunk(if encoded_count == 0 {let _ = tokio::fs::remove_file(&mp4_path).await;- return Ok(());+ return Ok(None);}// Verify MP4 output@@ -325,7 +336,14 @@ async fn compact_chunk(deleted);- Ok(())+ // Return cache update info so hot_frame_cache can be patched+ let cache_updates: Vec<(i64, String, i64, f64)> = frames+ .iter()+ .enumerate()+ .map(|(offset, (frame_id, _, _))| (*frame_id, mp4_path_str.clone(), offset as i64, fps))+ .collect();++ Ok(Some(cache_updates))}/// Spawn ffmpeg with low CPU priority for background compaction.