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: sync UPDATEs go through write coalescing queue instead of bypassing write pool
Louis Beaumontf4962dd3259a
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rsindex c722591f6..b5232ed25 100644--- a/crates/screenpipe-db/src/db.rs+++ b/crates/screenpipe-db/src/db.rs@@ -470,6 +470,28 @@ impl DatabaseManager {}}+ /// Mark records as synced via the write coalescing queue.+ /// This ensures sync UPDATEs go through the write semaphore and don't+ /// bypass the write pool (which was causing WAL lock contention).+ pub async fn mark_synced(+ &self,+ table: crate::write_queue::SyncTable,+ synced_at: &str,+ time_start: &str,+ time_end: &str,+ ) -> Result<(), sqlx::Error> {+ use crate::write_queue::WriteOp;+ self.write_queue+ .submit(WriteOp::MarkSynced {+ table,+ synced_at: synced_at.to_string(),+ time_start: time_start.to_string(),+ time_end: time_end.to_string(),+ })+ .await?;+ Ok(())+ }+pub async fn insert_audio_chunk(&self,file_path: &str,diff --git a/crates/screenpipe-db/src/lib.rs b/crates/screenpipe-db/src/lib.rsindex 128d6f9b7..852e0d080 100644--- a/crates/screenpipe-db/src/lib.rs+++ b/crates/screenpipe-db/src/lib.rs@@ -10,6 +10,7 @@ mod video_db;pub(crate) mod write_queue;pub use db::{find_matching_a11y_positions, parse_all_text_positions, DatabaseManager, DeleteTimeRangeResult};+pub use write_queue::SyncTable;pub use migration_worker::{create_migration_worker, MigrationCommand, MigrationConfig, MigrationResponse, MigrationStatus,MigrationWorker,diff --git a/crates/screenpipe-db/src/write_queue.rs b/crates/screenpipe-db/src/write_queue.rsindex 436298394..9448844e5 100644--- a/crates/screenpipe-db/src/write_queue.rs+++ b/crates/screenpipe-db/src/write_queue.rs@@ -100,6 +100,23 @@ pub(crate) enum WriteOp {device_name: String,fps: f64,},+ /// Mark records as synced by timestamp range. Used by cloud sync to+ /// go through the write queue instead of bypassing it on the read pool.+ MarkSynced {+ table: SyncTable,+ synced_at: String,+ time_start: String,+ time_end: String,+ },+}++/// Which table to mark as synced.+#[derive(Debug, Clone)]+pub enum SyncTable {+ Frames,+ AudioTranscriptions,+ FramesAccessibility,+ UiEvents,}/// Result returned to callers. Each variant matches the return type@@ -550,6 +567,35 @@ async fn execute_single_write(.last_insert_rowid();Ok(WriteResult::Id(id))}++ WriteOp::MarkSynced {+ table,+ synced_at,+ time_start,+ time_end,+ } => {+ let sql = match table {+ SyncTable::Frames => {+ "UPDATE frames SET synced_at = ?1 WHERE timestamp >= ?2 AND timestamp <= ?3 AND synced_at IS NULL"+ }+ SyncTable::AudioTranscriptions => {+ "UPDATE audio_transcriptions SET synced_at = ?1 WHERE timestamp >= ?2 AND timestamp <= ?3 AND synced_at IS NULL"+ }+ SyncTable::FramesAccessibility => {+ "UPDATE frames SET synced_at = ?1 WHERE timestamp >= ?2 AND timestamp <= ?3 AND text_source = 'accessibility' AND synced_at IS NULL"+ }+ SyncTable::UiEvents => {+ "UPDATE ui_events SET synced_at = ?1 WHERE timestamp >= ?2 AND timestamp <= ?3 AND synced_at IS NULL"+ }+ };+ sqlx::query(sql)+ .bind(synced_at.as_str())+ .bind(time_start.as_str())+ .bind(time_end.as_str())+ .execute(&mut **conn)+ .await?;+ Ok(WriteResult::Unit)+ }}}diff --git a/crates/screenpipe-engine/src/sync_provider.rs b/crates/screenpipe-engine/src/sync_provider.rsindex f607ad090..b797ab078 100644--- a/crates/screenpipe-engine/src/sync_provider.rs+++ b/crates/screenpipe-engine/src/sync_provider.rs@@ -781,83 +781,39 @@ impl ScreenpipeSyncProvider {}/// Mark records as synced after successful upload.+ /// Uses the write coalescing queue so sync UPDATEs go through the write+ /// semaphore and don't bypass the write pool (which was causing WAL lock+ /// contention and starving audio/vision inserts).async fn mark_records_synced(&self,blob_type: BlobType,time_start: &str,time_end: &str,) -> SyncResult<()> {- let pool = &self.db.pool;+ use screenpipe_db::SyncTable;let now = Utc::now().to_rfc3339();- match blob_type {- BlobType::Ocr => {- // Mark frames and their OCR as synced- sqlx::query(- r#"- UPDATE frames SET synced_at = ?- WHERE timestamp >= ? AND timestamp <= ? AND synced_at IS NULL- "#,- )- .bind(&now)- .bind(time_start)- .bind(time_end)- .execute(pool)- .await- .map_err(|e| SyncError::Database(format!("failed to mark frames synced: {}", e)))?;- }- BlobType::Transcripts => {- sqlx::query(- r#"- UPDATE audio_transcriptions SET synced_at = ?- WHERE timestamp >= ? AND timestamp <= ? AND synced_at IS NULL- "#,- )- .bind(&now)- .bind(time_start)- .bind(time_end)- .execute(pool)- .await- .map_err(|e| {- SyncError::Database(format!("failed to mark transcriptions synced: {}", e))- })?;- }- BlobType::Accessibility => {- // Mark accessibility frames as synced- sqlx::query(- r#"- UPDATE frames SET synced_at = ?- WHERE timestamp >= ? AND timestamp <= ?- AND text_source = 'accessibility' AND synced_at IS NULL- "#,- )- .bind(&now)- .bind(time_start)- .bind(time_end)- .execute(pool)- .await- .map_err(|e| {- SyncError::Database(format!("failed to mark accessibility synced: {}", e))- })?;- }- BlobType::Input => {- sqlx::query(- r#"- UPDATE ui_events SET synced_at = ?- WHERE timestamp >= ? AND timestamp <= ? AND synced_at IS NULL- "#,- )- .bind(&now)- .bind(time_start)- .bind(time_end)- .execute(pool)- .await- .map_err(|e| {- SyncError::Database(format!("failed to mark ui_events synced: {}", e))- })?;- }- _ => {}- }+ let table = match blob_type {+ BlobType::Ocr => SyncTable::Frames,+ BlobType::Transcripts => SyncTable::AudioTranscriptions,+ BlobType::Accessibility => SyncTable::FramesAccessibility,+ BlobType::Input => SyncTable::UiEvents,+ _ => return Ok(()),+ };++ self.db+ .mark_synced(table, &now, time_start, time_end)+ .await+ .map_err(|e| {+ SyncError::Database(format!("failed to mark {} synced: {}",+ match blob_type {+ BlobType::Ocr => "frames",+ BlobType::Transcripts => "transcriptions",+ BlobType::Accessibility => "accessibility",+ BlobType::Input => "ui_events",+ _ => "unknown",+ }, e))+ })?;Ok(())}