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 infinite FK constraint retry loop in audio reconciliation

Louis Beaumonte9e2dc252295
diff --git a/crates/screenpipe-audio/src/audio_manager/reconciliation.rs b/crates/screenpipe-audio/src/audio_manager/reconciliation.rs
index 2aa7df1dd..b1d6e7351 100644
--- a/crates/screenpipe-audio/src/audio_manager/reconciliation.rs
+++ b/crates/screenpipe-audio/src/audio_manager/reconciliation.rs
@@ -497,6 +497,23 @@ async fn retry_pending_transcriptions(
}
};
+ // Check if the parent audio_chunks row still exists. If it was deleted
+ // (e.g. by archive cleanup), the INSERT will always fail with a FK
+ // constraint violation — remove the orphaned pending file instead of
+ // retrying forever.
+ let chunk_exists = db.audio_chunk_exists(pending.audio_chunk_id)
+ .await
+ .unwrap_or(false);
+
+ if !chunk_exists {
+ info!(
+ "reconciliation: dropping orphaned pending file for deleted chunk {}",
+ pending.audio_chunk_id
+ );
+ let _ = std::fs::remove_file(&path);
+ continue;
+ }
+
// We don't have secondary file paths from the pending file, but they
// may already have been cleaned up. Pass empty slice — the DB deletion
// of secondary chunk IDs still happens.
diff --git a/crates/screenpipe-db/src/db.rs b/crates/screenpipe-db/src/db.rs
index 3f6352e07..98d4efc06 100644
--- a/crates/screenpipe-db/src/db.rs
+++ b/crates/screenpipe-db/src/db.rs
@@ -490,6 +490,17 @@ impl DatabaseManager {
Ok(())
}
+ /// Check whether an audio chunk row exists.
+ pub async fn audio_chunk_exists(&self, chunk_id: i64) -> Result<bool, sqlx::Error> {
+ let exists: bool = sqlx::query_scalar(
+ "SELECT EXISTS(SELECT 1 FROM audio_chunks WHERE id = ?1)",
+ )
+ .bind(chunk_id)
+ .fetch_one(&self.pool)
+ .await?;
+ Ok(exists)
+ }
+
/// Delete multiple audio chunks and their transcriptions in a single transaction.
/// Much cheaper than N individual delete_audio_chunk calls under write contention.
pub async fn delete_audio_chunks_batch(&self, chunk_ids: &[i64]) -> Result<(), sqlx::Error> {