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
feat: implement cache cleanup
Louis Beaumont6bc76a3fc28e
diff --git a/screenpipe-server/src/video_cache.rs b/screenpipe-server/src/video_cache.rsindex 6bc84cf95..697b79629 100644--- a/screenpipe-server/src/video_cache.rs+++ b/screenpipe-server/src/video_cache.rs@@ -110,25 +110,20 @@ struct CacheEntry {}#[derive(Debug, Clone)]-#[allow(dead_code)]struct CacheConfig {cache_dir: PathBuf,- #[allow(dead_code)]max_cache_size_gb: f64,- #[allow(dead_code)]frame_retention_days: u64,compression_quality: u8,- fps: f64,}impl Default for CacheConfig {fn default() -> Self {Self {cache_dir: PathBuf::from("frame_cache"),- max_cache_size_gb: 10.0,- frame_retention_days: 7,+ max_cache_size_gb: 3.0,+ frame_retention_days: 1,compression_quality: 50,- fps: 0.1,}}}@@ -345,30 +340,87 @@ impl FrameDiskCache {device_id.replace(['/', '\\', ':'], "_")))}++ async fn cleanup(&mut self) -> Result<()> {+ debug!("starting cache cleanup");++ // Calculate size limit in bytes+ let max_size_bytes = (self.config.max_cache_size_gb * 1024.0 * 1024.0 * 1024.0) as u64;++ // Calculate retention cutoff+ let retention_cutoff = Utc::now() - Duration::days(self.config.frame_retention_days as i64);++ let mut frames_to_remove = Vec::new();++ // Identify frames to remove based on age and total size+ for (&(timestamp, ref device_id), _entry) in &self.entries {+ if timestamp < retention_cutoff {+ frames_to_remove.push((timestamp, device_id.clone()));+ continue;+ }++ // If we're still over size limit, remove oldest frames+ if self.total_size > max_size_bytes {+ frames_to_remove.push((timestamp, device_id.clone()));+ }+ }++ // Remove identified frames+ for (timestamp, device_id) in frames_to_remove {+ if let Some(entry) = self.entries.remove(&(timestamp, device_id)) {+ self.total_size = self.total_size.saturating_sub(entry.frame.frame_size);+ if let Err(e) = fs::remove_file(&entry.path).await {+ debug!("failed to remove cached frame: {}", e);+ }+ }+ }++ // Save updated index+ self.save_index().await?;++ debug!(+ "cleanup complete - current cache size: {:.2} GB",+ self.total_size as f64 / (1024.0 * 1024.0 * 1024.0)+ );++ Ok(())+ }}async fn run_cache_manager(mut cache: FrameDiskCache, mut rx: mpsc::Receiver<CacheMessage>) {- while let Some(msg) = rx.recv().await {- match msg {- CacheMessage::Store {- cache_key,- frame_data,- device_data,- audio_entries,- response,- } => {- let result = cache- .store_frame(&cache_key, &frame_data, device_data, &audio_entries)- .await;- let _ = response.send(result);+ let mut cleanup_interval = tokio::time::interval(tokio::time::Duration::from_secs(3600)); // Hourly cleanup++ loop {+ tokio::select! {+ Some(msg) = rx.recv() => {+ match msg {+ CacheMessage::Store {+ cache_key,+ frame_data,+ device_data,+ audio_entries,+ response,+ } => {+ let result = cache+ .store_frame(&cache_key, &frame_data, device_data, &audio_entries)+ .await;+ let _ = response.send(result);+ }+ CacheMessage::Get {+ cache_key,+ response,+ } => {+ let result = cache.get_frame_data(&cache_key).await;+ let _ = response.send(result);+ }+ }}- CacheMessage::Get {- cache_key,- response,- } => {- let result = cache.get_frame_data(&cache_key).await;- let _ = response.send(result);+ _ = cleanup_interval.tick() => {+ if let Err(e) = cache.cleanup().await {+ debug!("cache cleanup failed: {}", e);+ }}+ else => break,}}}diff --git a/screenpipe-server/tests/video_cache_test.rs b/screenpipe-server/tests/video_cache_test.rsindex 358957167..de3fe5be2 100644--- a/screenpipe-server/tests/video_cache_test.rs+++ b/screenpipe-server/tests/video_cache_test.rs@@ -547,3 +547,76 @@ async fn test_cache_effectiveness() -> Result<()> {Ok(())}++#[tokio::test]+async fn test_cache_cleanup() -> Result<()> {+ let (cache, _db) = setup_test_env().await?;++ println!("\ncache cleanup test:");+ println!("-----------------");++ // First, populate cache with some frames+ let target_time = Utc::now() - Duration::minutes(5);+ let (tx1, mut rx1) = tokio::sync::mpsc::channel(100);++ println!("populating cache with initial frames...");+ cache.get_frames(target_time, 10, tx1, true).await?;++ let mut initial_frames = Vec::new();+ while let Some(frame) = rx1.recv().await {+ initial_frames.push(frame);+ }++ println!("initial cache population: {} frames", initial_frames.len());++ // Wait for cleanup interval (we'll use a shorter interval for testing)+ println!("waiting for cleanup cycle...");+ tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;++ // Request frames again to verify cache state+ let (tx2, mut rx2) = tokio::sync::mpsc::channel(100);+ cache.get_frames(target_time, 10, tx2, true).await?;++ let mut post_cleanup_frames = Vec::new();+ while let Some(frame) = rx2.recv().await {+ post_cleanup_frames.push(frame);+ }++ println!("post-cleanup frames: {}", post_cleanup_frames.len());++ // Verify that frames within retention period are still present+ let retained_frames = post_cleanup_frames+ .iter()+ .filter(|frame| {+ let age = Utc::now() - frame.timestamp;+ age.num_days() < 7 // default retention period+ })+ .count();++ println!("\ncleanup metrics:");+ println!("- initial frames: {}", initial_frames.len());+ println!("- post-cleanup frames: {}", post_cleanup_frames.len());+ println!("- retained frames: {}", retained_frames);++ // Assert that frames within retention period are kept+ assert!(+ retained_frames > 0,+ "should retain frames within retention period"+ );++ // Verify that very old frames are removed+ let old_frames = post_cleanup_frames+ .iter()+ .filter(|frame| {+ let age = Utc::now() - frame.timestamp;+ age.num_days() > 7+ })+ .count();++ assert_eq!(+ old_frames, 0,+ "should not have frames older than retention period"+ );++ Ok(())+}