Skip to content

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

chore: wire up FTS indexer in embedded server and CLI

Louis Beaumont43ccc10b95d1
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
index 61b0f8867..77768e0ce 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
@@ -534,6 +534,9 @@ pub async fn start_embedded_server(
});
}
+ // Start background FTS indexer (replaces synchronous INSERT triggers)
+ let _fts_handle = screenpipe_db::fts_indexer::start_fts_indexer(db.clone());
+
// Start resource monitor
let resource_monitor = ResourceMonitor::new(config.analytics_enabled);
resource_monitor.start_monitoring(Duration::from_secs(30), Some(Duration::from_secs(60)));
diff --git a/crates/screenpipe-server/src/bin/screenpipe-server.rs b/crates/screenpipe-server/src/bin/screenpipe-server.rs
index dd83a6e38..1c142b253 100644
--- a/crates/screenpipe-server/src/bin/screenpipe-server.rs
+++ b/crates/screenpipe-server/src/bin/screenpipe-server.rs
@@ -1354,6 +1354,9 @@ async fn main() -> anyhow::Result<()> {
}
};
+ // Start background FTS indexer (replaces synchronous INSERT triggers)
+ let _fts_handle = screenpipe_db::fts_indexer::start_fts_indexer(db.clone());
+
let server_future = server.start(cli.enable_frame_cache);
pin_mut!(server_future);
diff --git a/crates/screenpipe-server/src/ui_recorder.rs b/crates/screenpipe-server/src/ui_recorder.rs
index 4cbf16c67..11c248170 100644
--- a/crates/screenpipe-server/src/ui_recorder.rs
+++ b/crates/screenpipe-server/src/ui_recorder.rs
@@ -192,6 +192,8 @@ pub async fn start_ui_recording(
let mut batch: Vec<InsertUiEvent> = Vec::with_capacity(batch_size);
let mut last_flush = std::time::Instant::now();
+ let mut consecutive_failures: u32 = 0;
+ let max_batch_age = Duration::from_secs(30); // Drop events older than 30s during storms
loop {
if stop_flag_clone.load(Ordering::Relaxed) {
@@ -206,23 +208,57 @@ pub async fn start_ui_recording(
// Flush if batch is full
if batch.len() >= batch_size {
- flush_batch(&db, &mut batch).await;
+ flush_batch(&db, &mut batch, &mut consecutive_failures).await;
last_flush = std::time::Instant::now();
}
}
None => {
// Timeout - check if we should flush
if !batch.is_empty() && last_flush.elapsed() >= batch_timeout {
- flush_batch(&db, &mut batch).await;
+ // During contention storms, drop old events to prevent unbounded growth
+ if consecutive_failures > 3 && batch.len() > batch_size * 2 {
+ let old_len = batch.len();
+ // Keep only the most recent batch_size events
+ let drain_count = old_len.saturating_sub(batch_size);
+ batch.drain(..drain_count);
+ warn!(
+ "UI recorder: dropped {} old events during DB contention (kept {})",
+ drain_count, batch.len()
+ );
+ }
+
+ flush_batch(&db, &mut batch, &mut consecutive_failures).await;
last_flush = std::time::Instant::now();
+
+ // Exponential backoff on consecutive failures
+ if consecutive_failures > 0 {
+ let backoff = Duration::from_millis(
+ (500 * (1u64 << consecutive_failures.min(5))).min(30_000)
+ );
+ debug!(
+ "UI recorder: backing off {}ms after {} failures",
+ backoff.as_millis(), consecutive_failures
+ );
+ tokio::time::sleep(backoff).await;
+ }
}
}
}
+
+ // Safety: drop entire batch if it's too old (>30s without successful flush)
+ if !batch.is_empty() && last_flush.elapsed() > max_batch_age && consecutive_failures > 5 {
+ warn!(
+ "UI recorder: dropping {} stale events (last flush {}s ago, {} consecutive failures)",
+ batch.len(), last_flush.elapsed().as_secs(), consecutive_failures
+ );
+ batch.clear();
+ last_flush = std::time::Instant::now();
+ }
}
// Final flush
if !batch.is_empty() {
- flush_batch(&db, &mut batch).await;
+ flush_batch(&db, &mut batch, &mut consecutive_failures).await;
}
handle.stop();
@@ -236,7 +272,11 @@ pub async fn start_ui_recording(
}
#[cfg(feature = "ui-events")]
-async fn flush_batch(db: &Arc<DatabaseManager>, batch: &mut Vec<InsertUiEvent>) {
+async fn flush_batch(
+ db: &Arc<DatabaseManager>,
+ batch: &mut Vec<InsertUiEvent>,
+ consecutive_failures: &mut u32,
+) {
if batch.is_empty() {
return;
}
@@ -244,9 +284,16 @@ async fn flush_batch(db: &Arc<DatabaseManager>, batch: &mut Vec<InsertUiEvent>)
match db.insert_ui_events_batch(batch).await {
Ok(inserted) => {
debug!("Flushed {} UI events to database", inserted);
+ *consecutive_failures = 0;
}
Err(e) => {
- error!("Failed to insert UI events batch: {}", e);
+ *consecutive_failures += 1;
+ if *consecutive_failures <= 3 {
+ error!("Failed to insert UI events batch: {}", e);
+ } else {
+ // Reduce log spam during contention storms
+ debug!("Failed to insert UI events batch (failure #{}): {}", consecutive_failures, e);
+ }
}
}
batch.clear();