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
perf: defer element inserts, fix element cleanup, enable OCR dedup, bump v2.2.185
Louis Beaumontcdefe6cd5c27
diff --git a/Cargo.toml b/Cargo.tomlindex 8c59d1478..1bd410da7 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -6,7 +6,7 @@ exclude = ["apps/screenpipe-app-tauri/src-tauri", "crates/screenpipe-integrationresolver = "2"[workspace.package]-version = "0.3.187"+version = "0.3.188"authors = ["louis030195 <[email protected]>"]description = ""repository = "https://github.com/screenpipe/screenpipe"diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.tomlindex 9734d39e9..7e94ff247 100644--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml@@ -1,6 +1,6 @@[package]name = "screenpipe-app"-version = "2.2.184"+version = "2.2.185"description = "24/7 memory for your desktop. 100% local."authors = ["you"]license = ""diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rsindex 77198d7c7..6b16eff7a 100644--- a/crates/screenpipe-db/src/db.rs+++ b/crates/screenpipe-db/src/db.rs@@ -1937,6 +1937,18 @@ impl DatabaseManager {.or_else(|| ocr_text_str.filter(|t| !t.is_empty()).map(String::from)),};+ // Capture element data before moving into the frame write op+ let ocr_json_for_elements = if elements_ref_frame_id.is_none() {+ ocr_data.map(|(_, j, _)| j.to_string())+ } else {+ None+ };+ let a11y_json_for_elements = if elements_ref_frame_id.is_none() {+ accessibility_tree_json.map(String::from)+ } else {+ None+ };+let result = self.write_queue.submit(WriteOp::InsertSnapshotFrameWithOcr {@@ -1961,10 +1973,32 @@ impl DatabaseManager {}).await?;- match result {- WriteResult::Id(id) => Ok(id),+ let frame_id = match result {+ WriteResult::Id(id) => id,_ => unreachable!(),+ };++ // Submit element inserts as a separate write op so they don't hold+ // the write lock during the frame transaction. Elements are supplementary+ // data — the frame is usable for search immediately after the first commit.+ let has_elements = ocr_json_for_elements.as_ref().is_some_and(|j| !j.is_empty())+ || a11y_json_for_elements.as_ref().is_some_and(|j| !j.is_empty());+ if has_elements {+ // Fire-and-forget: spawn so we don't block the capture loop waiting+ // for element insertion. Errors are logged inside insert_*_elements.+ let queue = self.write_queue.clone();+ tokio::spawn(async move {+ let _ = queue+ .submit(WriteOp::InsertDeferredElements {+ frame_id,+ ocr_text_json: ocr_json_for_elements,+ accessibility_tree_json: a11y_json_for_elements,+ })+ .await;+ });}++ Ok(frame_id)}/// Get the next frame offset for a device.@@ -4247,6 +4281,15 @@ impl DatabaseManager {}}+ // 4c. Delete elements belonging to frames in the delete range (no CASCADE on FK)+ sqlx::query(+ "DELETE FROM elements WHERE frame_id IN (SELECT id FROM frames WHERE timestamp BETWEEN ?1 AND ?2)",+ )+ .bind(&start_str)+ .bind(&end_str)+ .execute(&mut **tx.conn())+ .await?;+// 5. Delete frames — triggers frames_fts delete; vision_tags CASCADE'd automaticallylet frames_result = sqlx::query("DELETE FROM frames WHERE timestamp BETWEEN ?1 AND ?2").bind(&start_str)@@ -4335,7 +4378,15 @@ impl DatabaseManager {.await?;let ocr_deleted = ocr_result.rows_affected();- // 2. Delete frames from this machine (vision_tags CASCADE automatically)+ // 2. Delete elements for frames from this machine (no CASCADE on FK)+ sqlx::query(+ "DELETE FROM elements WHERE frame_id IN (SELECT id FROM frames WHERE machine_id = ?1)",+ )+ .bind(machine_id)+ .execute(&mut **tx.conn())+ .await?;++ // 3. Delete frames from this machine (vision_tags CASCADE automatically)let frames_result = sqlx::query("DELETE FROM frames WHERE machine_id = ?1").bind(machine_id).execute(&mut **tx.conn())diff --git a/crates/screenpipe-db/src/write_queue.rs b/crates/screenpipe-db/src/write_queue.rsindex 5b5af8c68..8ded5df12 100644--- a/crates/screenpipe-db/src/write_queue.rs+++ b/crates/screenpipe-db/src/write_queue.rs@@ -235,6 +235,15 @@ pub(crate) enum WriteOp {element_bounds: Option<String>,frame_id: Option<i64>,},+ /// Deferred element insertion: inserts OCR and/or accessibility elements+ /// for a frame in a separate transaction from the frame insert itself.+ /// This avoids holding the write lock during the 30-80+ individual+ /// INSERT...RETURNING id queries that element insertion requires.+ InsertDeferredElements {+ frame_id: i64,+ ocr_text_json: Option<String>,+ accessibility_tree_json: Option<String>,+ },/// Batch insert frames with OCR text. Replaces the direct/// `begin_immediate_with_retry` call in `insert_frames_with_ocr_batch`/// so that frame inserts go through the coalescing queue.@@ -680,6 +689,7 @@ async fn execute_single_write(.last_insert_rowid();// Insert OCR text in same transaction (always — needed for search)+ // Element inserts are deferred to a separate transaction (see caller).if let (Some(text), Some(text_json), Some(engine)) = (ocr_text.as_deref(),ocr_text_json.as_deref(),@@ -696,21 +706,6 @@ async fn execute_single_write(.bind(text_length).execute(&mut **conn).await?;-- // Skip OCR element insertion when referencing another frame's elements- if elements_ref_frame_id.is_none() {- crate::db::DatabaseManager::insert_ocr_elements(conn, id, text_json).await;- }- }-- // Skip accessibility element insertion when referencing another frame's elements- if elements_ref_frame_id.is_none() {- if let Some(tree_json) = accessibility_tree_json.as_deref() {- if !tree_json.is_empty() {- crate::db::DatabaseManager::insert_accessibility_elements(conn, id, tree_json)- .await;- }- }}if let Some(ref_id) = elements_ref_frame_id {@@ -727,6 +722,25 @@ async fn execute_single_write(Ok(WriteResult::Id(id))}+ WriteOp::InsertDeferredElements {+ frame_id,+ ocr_text_json,+ accessibility_tree_json,+ } => {+ if let Some(ref text_json) = ocr_text_json {+ if !text_json.is_empty() {+ crate::db::DatabaseManager::insert_ocr_elements(conn, *frame_id, text_json).await;+ }+ }+ if let Some(ref tree_json) = accessibility_tree_json {+ if !tree_json.is_empty() {+ crate::db::DatabaseManager::insert_accessibility_elements(conn, *frame_id, tree_json)+ .await;+ }+ }+ Ok(WriteResult::Unit)+ }+WriteOp::InsertVideoChunkWithFps {file_path,device_name,diff --git a/crates/screenpipe-engine/src/paired_capture.rs b/crates/screenpipe-engine/src/paired_capture.rsindex 77542231f..0f4a292b5 100644--- a/crates/screenpipe-engine/src/paired_capture.rs+++ b/crates/screenpipe-engine/src/paired_capture.rs@@ -184,7 +184,12 @@ pub async fn paired_capture(// only returns window chrome ("System, Minimize, Restore, Close") which// is noise. If OCR fails, store nothing rather than chrome.if !ocr_text.is_empty() {- (Some(ocr_text.clone()), None, None, None)+ (+ Some(ocr_text.clone()),+ None,+ Some(TreeSnapshot::compute_hash(&ocr_text) as i64),+ None,+ )} else {(None, None, None, None)}@@ -204,7 +209,12 @@ pub async fn paired_capture(if ocr_text.is_empty() {(None, None, None, None)} else {- (Some(ocr_text.clone()), None, None, None)+ (+ Some(ocr_text.clone()),+ None,+ Some(TreeSnapshot::compute_hash(&ocr_text) as i64),+ None,+ )}}}