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: UTF-8 panic in truncate_string + debounce monitor capture errors

Louis Beaumont965d6011d38a
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 6dbdc62a0..8a4eef5a0 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -2388,7 +2388,11 @@ fn truncate_string(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
- format!("{}…[truncated]", &s[..max_len])
+ let mut end = max_len;
+ while !s.is_char_boundary(end) {
+ end -= 1;
+ }
+ format!("{}…[truncated]", &s[..end])
}
}
diff --git a/crates/screenpipe-server/src/event_driven_capture.rs b/crates/screenpipe-server/src/event_driven_capture.rs
index 6fc53ea19..52aab62b0 100644
--- a/crates/screenpipe-server/src/event_driven_capture.rs
+++ b/crates/screenpipe-server/src/event_driven_capture.rs
@@ -242,6 +242,10 @@ pub async fn event_driven_capture_loop(
// Track last successful DB write time — dedup is bypassed after 30s
// to guarantee the timeline always has periodic entries
let mut last_db_write = Instant::now();
+ // Debounce consecutive capture errors — log error! once on first failure,
+ // then suppress until success. Prevents monitor disconnect from flooding
+ // Sentry with 100k+ identical events.
+ let mut consecutive_capture_errors: u32 = 0;
// Capture immediately on startup so the timeline has a frame right away.
// Also seeds the frame comparer so subsequent visual-change checks work.
@@ -414,6 +418,14 @@ pub async fn event_driven_capture_loop(
Ok(Ok(output)) => {
state.mark_captured();
+ if consecutive_capture_errors > 0 {
+ info!(
+ "monitor {} capture recovered after {} consecutive errors",
+ monitor_id, consecutive_capture_errors
+ );
+ consecutive_capture_errors = 0;
+ }
+
// Feed the captured frame to comparer so we don't
// re-trigger on the same visual state (reuses capture
// image — no extra screenshot needed)
@@ -450,12 +462,27 @@ pub async fn event_driven_capture_loop(
}
}
Ok(Err(e)) => {
- error!(
- "event capture failed (trigger={}, monitor={}): {}",
- trigger.as_str(),
- monitor_id,
- e
- );
+ consecutive_capture_errors += 1;
+ if consecutive_capture_errors == 1 {
+ // First failure — log at error level (shows in Sentry)
+ error!(
+ "event capture failed (trigger={}, monitor={}): {}",
+ trigger.as_str(),
+ monitor_id,
+ e
+ );
+ } else if consecutive_capture_errors % 100 == 0 {
+ // Periodic reminder at warn level (no Sentry flood)
+ warn!(
+ "monitor {} capture still failing ({} consecutive errors): {}",
+ monitor_id, consecutive_capture_errors, e
+ );
+ } else {
+ debug!(
+ "monitor {} capture error #{}: {}",
+ monitor_id, consecutive_capture_errors, e
+ );
+ }
}
Err(_timeout) => {
warn!(
diff --git a/crates/screenpipe-vision/src/utils.rs b/crates/screenpipe-vision/src/utils.rs
index 8ce193590..4627dcf47 100644
--- a/crates/screenpipe-vision/src/utils.rs
+++ b/crates/screenpipe-vision/src/utils.rs
@@ -176,7 +176,7 @@ pub async fn capture_monitor_image(
let capture_start = Instant::now();
let image = monitor.capture_image().await.map_err(|e| {
debug!("failed to capture monitor image: {}", e);
- anyhow::anyhow!("monitor capture failed")
+ anyhow::anyhow!("monitor capture failed: {}", e)
})?;
let capture_duration = capture_start.elapsed();
Ok((image, capture_duration))