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(core+db): ffprobe pair validation + benign UNIQUE in transcription update

Louis Beaumont1d4f7566916e
diff --git a/crates/screenpipe-core/src/ffmpeg.rs b/crates/screenpipe-core/src/ffmpeg.rs
index 5b435446c..b3aaa3570 100644
--- a/crates/screenpipe-core/src/ffmpeg.rs
+++ b/crates/screenpipe-core/src/ffmpeg.rs
@@ -42,6 +42,30 @@ pub fn ffmpeg_cmd_async(path: impl AsRef<std::ffi::OsStr>) -> tokio::process::Co
cmd
}
+/// True when a usable ffprobe exists next to the given ffmpeg binary, OR
+/// somewhere on PATH. Frame extraction requires both — if we return an
+/// ffmpeg path without a matching ffprobe we get runtime 500s from
+/// `get_ffprobe_path`. Callers should fall through to the next discovery
+/// source when this returns false.
+fn has_matching_ffprobe(ffmpeg_path: &std::path::Path) -> bool {
+ #[cfg(windows)]
+ let sibling_names = ["ffprobe.exe", "ffprobe"];
+ #[cfg(not(windows))]
+ let sibling_names = ["ffprobe"];
+
+ for name in sibling_names {
+ if ffmpeg_path.with_file_name(name).exists() {
+ return true;
+ }
+ }
+
+ #[cfg(not(windows))]
+ let probe_name = "ffprobe";
+ #[cfg(windows)]
+ let probe_name = "ffprobe.exe";
+ which(probe_name).is_ok()
+}
+
fn find_ffmpeg_path_internal() -> Option<PathBuf> {
debug!("Starting search for ffmpeg executable");
@@ -73,14 +97,27 @@ fn find_ffmpeg_path_internal() -> Option<PathBuf> {
}
}
- // Check if `ffmpeg` is in the PATH environment variable
+ // Check if `ffmpeg` is in the PATH environment variable.
+ //
+ // We MUST only accept a PATH ffmpeg if a matching ffprobe is available —
+ // frame extraction requires both. A user can easily end up with just
+ // ffmpeg in ~/.local/bin (e.g. an old auto-install that only extracted
+ // ffmpeg, or a user-installed ffmpeg without the full suite); without
+ // this guard we pick the broken half-install over the app-bundled pair
+ // and every compacted-frame fetch returns a 500. See #2999.
if let Ok(path) = which(EXECUTABLE_NAME) {
- debug!("Found ffmpeg in PATH: {:?}", path);
- return Some(path);
+ if has_matching_ffprobe(&path) {
+ debug!("Found ffmpeg+ffprobe pair via PATH: {:?}", path);
+ return Some(path);
+ }
+ debug!(
+ "ffmpeg in PATH at {:?} has no matching ffprobe — falling through",
+ path
+ );
}
debug!("ffmpeg not found in PATH");
- // Check in $HOME/.local/bin on macOS
+ // Check in $HOME/.local/bin on macOS. Same pair requirement as above.
#[cfg(target_os = "macos")]
{
if let Ok(home) = std::env::var("HOME") {
@@ -88,11 +125,17 @@ fn find_ffmpeg_path_internal() -> Option<PathBuf> {
debug!("Checking $HOME/.local/bin: {:?}", local_bin);
let ffmpeg_in_local_bin = local_bin.join(EXECUTABLE_NAME);
if ffmpeg_in_local_bin.exists() {
+ if has_matching_ffprobe(&ffmpeg_in_local_bin) {
+ debug!(
+ "Found ffmpeg+ffprobe pair in $HOME/.local/bin: {:?}",
+ ffmpeg_in_local_bin
+ );
+ return Some(ffmpeg_in_local_bin);
+ }
debug!(
- "Found ffmpeg in $HOME/.local/bin: {:?}",
+ "ffmpeg in ~/.local/bin at {:?} has no matching ffprobe — falling through",
ffmpeg_in_local_bin
);
- return Some(ffmpeg_in_local_bin);
}
debug!("ffmpeg not found in $HOME/.local/bin");
}
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index 6b4a6dd8a..be093cd59 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -139,6 +139,16 @@ impl Drop for ImmediateTx {
}
}
+/// True when `e` is a UNIQUE-constraint violation from SQLite. Used by
+/// callers that want to treat benign duplicates as a no-op instead of
+/// letting the ImmediateTx drop uncommitted (which logs a warning).
+fn is_unique_violation(e: &sqlx::Error) -> bool {
+ if let sqlx::Error::Database(db_err) = e {
+ return db_err.is_unique_violation();
+ }
+ false
+}
+
pub struct DatabaseManager {
/// Read-only pool. Used for all SELECT queries.
/// Separated from writes so read bursts (search, timeline, API) can never
@@ -1163,19 +1173,32 @@ impl DatabaseManager {
let mut tx = self.begin_immediate_with_retry().await?;
// Insert the full transcription
- let affected = sqlx::query(
+ let result = sqlx::query(
"UPDATE audio_transcriptions SET transcription = ?1, text_length = ?2 WHERE audio_chunk_id = ?3",
)
.bind(trimmed)
.bind(text_length)
.bind(audio_chunk_id)
.execute(&mut **tx.conn())
- .await?
- .rows_affected();
+ .await;
- // Commit the transaction for the full transcription
- tx.commit().await?;
- Ok(affected as i64)
+ match result {
+ Ok(r) => {
+ tx.commit().await?;
+ Ok(r.rows_affected() as i64)
+ }
+ // UNIQUE(audio_chunk_id, transcription) is enforced by
+ // idx_audio_transcription_chunk_text. Re-transcribing an already-
+ // stored chunk with identical text collides benignly; callers
+ // already treat this as a no-op (see transcription_result.rs).
+ // Commit an empty tx so Drop doesn't fire the noisy
+ // "ImmediateTx dropped without commit" warning.
+ Err(e) if is_unique_violation(&e) => {
+ tx.commit().await?;
+ Ok(0)
+ }
+ Err(e) => Err(e),
+ }
}
/// Replace all transcription rows for an audio chunk with a single new transcription.