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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(storage): wait for restarts before retrying migration (#7041)
Ezra Ellette4c406e9d9e98
diff --git a/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx b/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsxindex 9daa4a887..b783112ab 100644--- a/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx+++ b/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx@@ -1,7 +1,7 @@// screenpipe — AI that knows everything you've seen, said, or heard// https://screenpipe.com-import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { StorageMigrationActivity, StorageMigrationStatus } from "@/lib/utils/tauri";@@ -156,6 +156,27 @@ describe("automatic storage migration prompt", () => {await waitFor(() => expect(commands.startStorageMigration).toHaveBeenCalledTimes(2));});+ it("keeps one explicit retry pending until the native restart releases storage", async () => {+ Object.assign(status, { pending: true, in_place: true, error: "The previous storage migration did not finish." });+ let finishRestart!: () => void;+ commands.startStorageMigration.mockImplementationOnce(() => new Promise((resolve) => {+ finishRestart = () => resolve({ status: "ok", data: null });+ }));+ const app = render(<StorageMigrationPrompt activity={idle} />);+ fireEvent.click(await screen.findByRole("button", { name: "try again" }));+ const starting = screen.getByRole("button", { name: "starting…" });+ expect(starting).toBeDisabled();+ expect(screen.getByRole("button", { name: "do later" })).toBeDisabled();+ fireEvent.click(starting);+ expect(commands.startStorageMigration).toHaveBeenCalledOnce();+ expect(commands.startStorageMigration).toHaveBeenCalledWith("/fixture");++ await act(async () => finishRestart());+ app.rerender(<StorageMigrationPrompt activity={{ ...idle, busy: true, message: "pausing recording" }} />);+ expect(screen.queryByRole("alertdialog")).toBeNull();+ expect(commands.startStorageMigration).toHaveBeenCalledOnce();+ });+it("offers recovery for an interrupted migration", async () => {Object.assign(status, { pending: true, can_cancel: true, error: "Verification failed; original kept." });render(<StorageMigrationPrompt activity={{ ...idle, error: status.error }} />);diff --git a/apps/screenpipe-app-tauri/src-tauri/src/storage_migration.rs b/apps/screenpipe-app-tauri/src-tauri/src/storage_migration.rsindex 959b47381..c21dcefa8 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/storage_migration.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/storage_migration.rs@@ -6,7 +6,7 @@ use screenpipe_db::storage::{migration_report, MigrationProgress, StorageDescripuse serde::{Deserialize, Serialize};use std::{path::{Path, PathBuf},- sync::{LazyLock, Mutex},+ sync::{Arc, LazyLock, Mutex},time::Instant,};use tauri::{Emitter, Manager, State};@@ -685,20 +685,37 @@ pub async fn start_storage_migration(start_storage_migration_inner(app, recording, root, false).await}+async fn acquire_migration_lifecycle(+ lifecycle: Arc<tokio::sync::Mutex<()>>,+ background: bool,+) -> Option<tokio::sync::OwnedMutexGuard<()>> {+ if background {+ // Policy checks must not queue automatic work behind a restart or an+ // explicit migration. A later check will inspect the resulting state.+ lifecycle.try_lock_owned().ok()+ } else {+ // The user's retry remains requested while startup/recovery owns the+ // database. Take exclusive ownership when it finishes, without polling+ // or interrupting the current writer.+ Some(lifecycle.lock_owned().await)+ }+}+async fn start_storage_migration_inner(app: tauri::AppHandle,recording: State<'_, RecordingState>,root: String,background: bool,) -> Result<(), String> {+ require_selected_root(&app, &root)?;+ let Some(lifecycle) =+ acquire_migration_lifecycle(recording.server_lifecycle.clone(), background).await+ else {+ return Ok(());+ };+ // Settings and storage may have changed while the request waited. Never+ // migrate a different directory using the earlier confirmation.let root = require_selected_root(&app, &root)?;- let lifecycle = recording- .server_lifecycle- .clone()- .try_lock_owned()- .map_err(|_| {- "Screenpipe is already restarting or changing storage. Try again when it finishes."- })?;let status = storage_migration_status(&app, &recording, false).await?;if background {if let Some(error) = &status.error {@@ -725,6 +742,15 @@ async fn start_storage_migration_inner(return Ok(());}} else if !status.can_migrate {+ if status.completed+ && status.using_new_storage+ && !status.pending+ && status.error.is_none()+ && status.blocked_reason.is_none()+ {+ // Another migration may have completed while this click waited.+ return Ok(());+ }return Err(status.blocked_reason.unwrap_or_else(|| "Migration is unavailable in the current storage state.".into()));@@ -991,6 +1017,56 @@ pub async fn delete_original_storage_database(mod tests {use super::*;+ #[tokio::test]+ async fn explicit_retry_waits_for_restart_then_owns_storage_before_later_restarts() {+ let lifecycle = Arc::new(tokio::sync::Mutex::new(()));+ let restart = lifecycle.lock().await;+ let retry = acquire_migration_lifecycle(lifecycle.clone(), false);+ tokio::pin!(retry);+ // Poll once to enqueue the click while startup/recovery still owns DB.+ tokio::select! {+ biased;+ _ = &mut retry => panic!("retry must wait for the current restart"),+ _ = std::future::ready(()) => {}+ }+ let later_restart = lifecycle.lock();+ tokio::pin!(later_restart);+ tokio::select! {+ biased;+ _ = &mut later_restart => panic!("restart must not overlap storage work"),+ _ = std::future::ready(()) => {}+ }+ drop(restart);+ let migration = tokio::time::timeout(std::time::Duration::from_secs(1), retry)+ .await+ .expect("one click must proceed when startup finishes")+ .unwrap();+ assert!(lifecycle.try_lock().is_err());+ drop(migration);+ let _restart = tokio::time::timeout(std::time::Duration::from_secs(1), later_restart)+ .await+ .expect("later restarts must proceed after migration releases storage");+ }++ #[tokio::test]+ async fn background_migration_skips_busy_storage_without_queueing_a_retry() {+ let lifecycle = Arc::new(tokio::sync::Mutex::new(()));+ let restart = lifecycle.lock().await;+ assert!(tokio::time::timeout(+ std::time::Duration::from_secs(1),+ acquire_migration_lifecycle(lifecycle.clone(), true),+ )+ .await+ .expect("background checks must not wait for startup")+ .is_none());+ drop(restart);+ assert!(+ lifecycle.try_lock().is_ok(),+ "no automatic retry was queued"+ );+ assert!(acquire_migration_lifecycle(lifecycle, true).await.is_some());+ }+#[test]fn completed_migration_does_not_prompt_during_server_startup_or_restart() {// The receipt already matches, but startup has not installed the