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

fix: fast startup + immediate live polling + reduce FTS yield delay

Louis Beaumont13e9536249c9
diff --git a/crates/screenpipe-db/src/fts_indexer.rs b/crates/screenpipe-db/src/fts_indexer.rs
index e28ac8201..4d527fcee 100644
--- a/crates/screenpipe-db/src/fts_indexer.rs
+++ b/crates/screenpipe-db/src/fts_indexer.rs
@@ -31,7 +31,7 @@ const BACKFILL_MICRO_BATCH_DELAY: Duration = Duration::from_millis(500);
/// How long to sleep when the DB write lock is busy (real-time capture
/// is writing). This ensures FTS never blocks captures.
-const YIELD_ON_BUSY_DELAY: Duration = Duration::from_secs(2);
+const YIELD_ON_BUSY_DELAY: Duration = Duration::from_millis(200);
/// Start the background FTS indexer. Uses `try_begin_immediate` so it
/// NEVER blocks real-time captures — if the write lock is held, FTS
diff --git a/crates/screenpipe-server/src/event_driven_capture.rs b/crates/screenpipe-server/src/event_driven_capture.rs
index d65c42b20..a1e9f4c86 100644
--- a/crates/screenpipe-server/src/event_driven_capture.rs
+++ b/crates/screenpipe-server/src/event_driven_capture.rs
@@ -237,6 +237,44 @@ pub async fn event_driven_capture_loop(
};
let mut last_visual_check = Instant::now();
+ // Capture immediately on startup so the timeline has a frame right away.
+ // Also seeds the frame comparer so subsequent visual-change checks work.
+ {
+ // Small delay to let the monitor settle after startup
+ tokio::time::sleep(Duration::from_millis(500)).await;
+ state.last_capture = Instant::now() - Duration::from_millis(500); // allow capture
+ match do_capture(
+ &db,
+ &monitor,
+ monitor_id,
+ &device_name,
+ &snapshot_writer,
+ &tree_walker_config,
+ &CaptureTrigger::Manual,
+ )
+ .await
+ {
+ Ok(result) => {
+ state.mark_captured();
+ if let Some(ref mut comparer) = frame_comparer {
+ if let Ok((img, _)) = capture_monitor_image(&monitor).await {
+ let _ = comparer.compare(&img);
+ }
+ }
+ vision_metrics.record_capture();
+ vision_metrics
+ .record_db_write(Duration::from_millis(result.duration_ms as u64));
+ info!(
+ "startup capture for monitor {}: frame_id={}, dur={}ms",
+ monitor_id, result.frame_id, result.duration_ms
+ );
+ }
+ Err(e) => {
+ warn!("startup capture failed for monitor {}: {}", monitor_id, e);
+ }
+ }
+ }
+
loop {
if stop_signal.load(Ordering::Relaxed) {
info!("event-driven capture stopping for monitor {}", monitor_id);
diff --git a/crates/screenpipe-server/src/routes/streaming.rs b/crates/screenpipe-server/src/routes/streaming.rs
index 7aa3cc878..f226bf4e0 100644
--- a/crates/screenpipe-server/src/routes/streaming.rs
+++ b/crates/screenpipe-server/src/routes/streaming.rs
@@ -282,38 +282,57 @@ async fn handle_stream_frames_socket(
sent.clear();
}
- // DON'T set active_request yet - wait for initial fetch to complete
- // This prevents poll_timer from running with last_polled = start_time
- // which would cause ALL old frames to be re-sent
+ // Enable live polling IMMEDIATELY from `now()` so new
+ // frames stream to the client right away, even while the
+ // historical fetch is still running. The initial fetch
+ // handles backfill; live polling handles real-time.
+ {
+ let now = Utc::now();
+ let mut req = active_request_clone.lock().await;
+ *req = Some((start_time, end_time, is_descending, now));
+ info!(
+ "Live polling enabled immediately from {}",
+ now
+ );
+ }
let sent_frame_ids_inner = sent_frame_ids_clone.clone();
let active_request_inner = active_request_clone.clone();
tokio::spawn(async move {
- match fetch_and_process_frames_with_tracking(
- db,
- start_time,
- end_time,
- frame_tx,
- is_descending,
- sent_frame_ids_inner,
+ // Timeout the initial fetch — if DB is slow, don't
+ // block forever. Live polling already covers new frames.
+ let fetch_result = tokio::time::timeout(
+ std::time::Duration::from_secs(10),
+ fetch_and_process_frames_with_tracking(
+ db,
+ start_time,
+ end_time,
+ frame_tx,
+ is_descending,
+ sent_frame_ids_inner,
+ ),
)
- .await
- {
- Ok(latest_timestamp) => {
- // NOW set active_request - initial fetch is done
- // Use latest_timestamp (or end_time) as last_polled to avoid re-fetching
- let poll_start = latest_timestamp.unwrap_or(end_time);
- let mut req = active_request_inner.lock().await;
- *req = Some((start_time, end_time, is_descending, poll_start));
- info!(
- "Initial fetch complete, enabling live polling from {}",
- poll_start
- );
+ .await;
+
+ match fetch_result {
+ Ok(Ok(latest_timestamp)) => {
+ // Update poll cursor to latest fetched timestamp
+ // so polling doesn't re-fetch historical frames.
+ if let Some(ts) = latest_timestamp {
+ let mut req = active_request_inner.lock().await;
+ if let Some((s, e, d, _)) = *req {
+ *req = Some((s, e, d, ts));
+ }
+ }
+ info!("Initial fetch complete");
}
- Err(e) => {
+ Ok(Err(e)) => {
error!("frame fetching failed: {}", e);
}
+ Err(_) => {
+ warn!("Initial fetch timed out after 10s, live polling continues");
+ }
}
});
}