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(storage): keep migration start waiting for readiness

Ezra Ellette8b6b00b6b3a4
diff --git a/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx b/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx
index e615f3025..9a1af8a71 100644
--- a/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx
+++ b/apps/screenpipe-app-tauri/components/storage-migration-prompt.test.tsx
@@ -30,7 +30,10 @@ beforeEach(() => {
commands.startStorageMigration.mockResolvedValue({ status: "ok", data: null });
commands.cancelStorageMigration.mockResolvedValue({ status: "ok", data: null });
});
-afterEach(cleanup);
+afterEach(() => {
+ cleanup();
+ vi.useRealTimers();
+});
describe("automatic storage migration prompt", () => {
it.each([
@@ -194,6 +197,50 @@ describe("automatic storage migration prompt", () => {
expect(commands.startStorageMigration).toHaveBeenCalledOnce();
});
+ it.each(["started", "failed"])("keeps Start now loading through startup status polls until migration has %s", async (outcome) => {
+ vi.useFakeTimers();
+ let finishStartup!: () => void;
+ commands.startStorageMigration.mockImplementationOnce(() => new Promise((resolve) => {
+ finishStartup = () => resolve(outcome === "started"
+ ? { status: "ok", data: null }
+ : { status: "error", error: "Could not prevent sleep. Migration has not started." });
+ }));
+ const app = render(<StorageMigrationPrompt activity={idle} />);
+ await act(async () => {});
+ // Begin a refresh before the click, so its now-stale startup response can
+ // also arrive after the explicit request has started waiting.
+ let finishStatus!: () => void;
+ commands.getStorageMigrationStatus.mockImplementationOnce(() => new Promise((resolve) => {
+ finishStatus = () => resolve({ status: "ok", data: { ...status } });
+ }));
+ await act(async () => { await vi.advanceTimersByTimeAsync(5000); });
+ fireEvent.click(screen.getByRole("button", { name: "Start now" }));
+ Object.assign(status, { can_migrate: false,
+ blocked_reason: "Screenpipe is restarting or restoring recording. Wait for startup to finish.",
+ });
+ await act(async () => {
+ finishStatus();
+ await vi.advanceTimersByTimeAsync(15_000);
+ });
+ expect(screen.getByRole("button", { name: "Starting…" })).toBeDisabled();
+ expect(screen.getByRole("button", { name: "Do later" })).toBeDisabled();
+ expect(commands.startStorageMigration).toHaveBeenCalledOnce();
+ expect(screen.queryByRole("alert")).toBeNull();
+
+ Object.assign(status, { can_migrate: true, blocked_reason: null });
+ await act(async () => finishStartup());
+ if (outcome === "failed") {
+ expect(screen.getByRole("alert")).toHaveTextContent("Could not prevent sleep");
+ expect(screen.getByRole("button", { name: "Try again" })).toBeEnabled();
+ } else {
+ app.rerender(<StorageMigrationPrompt activity={{ ...idle, busy: true, message: "pausing recording" }} />);
+ expect(screen.queryByRole("alertdialog")).toBeNull();
+ }
+ expect(commands.startStorageMigration).toHaveBeenCalledOnce();
+ expect(commands.cancelStorageMigration).not.toHaveBeenCalled();
+ expect(commands.deleteOriginalStorageDatabase).not.toHaveBeenCalled();
+ });
+
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/components/storage-migration-prompt.tsx b/apps/screenpipe-app-tauri/components/storage-migration-prompt.tsx
index ca0a9d8a3..ad22c2edf 100644
--- a/apps/screenpipe-app-tauri/components/storage-migration-prompt.tsx
+++ b/apps/screenpipe-app-tauri/components/storage-migration-prompt.tsx
@@ -47,6 +47,9 @@ export function StorageMigrationPrompt({ activity }: { activity: StorageMigratio
const [error, setError] = useState<string | null>(null);
useEffect(() => {
+ // The native command waits for startup under the lifecycle lock. Keep the
+ // submitted button visible until it settles, even if a status poll was in flight.
+ if (submitting) return;
if (activity.busy) {
setError(null);
setDismissed(null);
@@ -70,7 +73,7 @@ export function StorageMigrationPrompt({ activity }: { activity: StorageMigratio
};
void poll();
return () => { disposed = true; clearTimeout(timer); };
- }, [activity.busy, activity.completed, activity.error]);
+ }, [activity.busy, activity.completed, activity.error, submitting]);
const success = activity.root === status?.root && activity.completed && status?.completed && status.using_new_storage;
const failure = 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.rs
index 5c64deaa2..7661b9cb9 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/storage_migration.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/storage_migration.rs
@@ -1,7 +1,7 @@
// screenpipe — AI that knows everything you've seen, said, or heard
// https://screenpipe.com
-use crate::{recording::RecordingState, store::SettingsStore};
+use crate::{recording::RecordingState, server_core::ServerCore, store::SettingsStore};
use screenpipe_db::storage::{migration_report, MigrationProgress, StorageDescriptor};
use serde::{Deserialize, Serialize};
use std::{
@@ -312,8 +312,7 @@ pub(crate) async fn resume_before_startup(
// Own recovery before ServerCore opens the database. It can rebuild a
// large index: expose that work and keep retry unavailable until the
// server and saved capture preference have actually been restored.
- let awake =
- screenpipe_engine::power::KeepAwakeGuard::acquire_async().await?;
+ let awake = screenpipe_engine::power::KeepAwakeGuard::acquire_async().await?;
update_operation(app, |operation| {
*operation = Operation {
root: Some(root.clone()),
@@ -574,6 +573,20 @@ pub async fn get_storage_migration_status(
storage_migration_status(&app, &recording, lifecycle.is_err()).await
}
+async fn lock_migration_server(
+ server: &tokio::sync::Mutex<Option<ServerCore>>,
+ lifecycle_busy: bool,
+) -> Option<tokio::sync::MutexGuard<'_, Option<ServerCore>>> {
+ if lifecycle_busy {
+ // Status polling must remain responsive while startup owns storage.
+ server.try_lock().ok()
+ } else {
+ // The caller owns the lifecycle lock, so a server reader cannot be a
+ // restart. Wait for it instead of rejecting an explicit migration.
+ Some(server.lock().await)
+ }
+}
+
async fn storage_migration_status(
app: &tauri::AppHandle,
recording: &RecordingState,
@@ -609,11 +622,8 @@ async fn storage_migration_status(
let mut using_new_storage = None;
let mut can_delete_source = false;
{
- let server = recording.server.try_lock();
- if server.is_err() && !operation.busy {
- blocked_reason = Some("Screenpipe is restarting. Wait for startup to finish.".into());
- }
- if let Ok(server) = server {
+ if let Some(server) = lock_migration_server(&recording.server, lifecycle_busy).await {
+ require_selected_root(app, &root.display().to_string())?;
if let Some(server) = server.as_ref() {
if server.data_dir.canonicalize().map_err(|e| e.to_string())? != root {
blocked_reason = Some(
@@ -1119,6 +1129,43 @@ mod tests {
assert!(properties["error"].is_null());
}
+ #[tokio::test]
+ async fn migration_status_waits_for_server_readers_after_startup() {
+ let server = tokio::sync::Mutex::new(None);
+ let reader = server.lock().await;
+ let status = lock_migration_server(&server, false);
+ tokio::pin!(status);
+ tokio::select! {
+ biased;
+ _ = &mut status => panic!("a server reader must not be treated as a restart"),
+ _ = std::future::ready(()) => {}
+ }
+ drop(reader);
+ let guard = tokio::time::timeout(std::time::Duration::from_secs(1), status)
+ .await
+ .expect("migration must proceed when the reader releases the server")
+ .expect("the lifecycle owner must obtain the server state");
+ assert!(
+ guard.is_none(),
+ "an idle or paused server is still inspectable"
+ );
+ }
+
+ #[tokio::test]
+ async fn migration_status_does_not_wait_when_startup_owns_storage() {
+ let server = tokio::sync::Mutex::new(None);
+ let startup = server.lock().await;
+ assert!(tokio::time::timeout(
+ std::time::Duration::from_secs(1),
+ lock_migration_server(&server, true),
+ )
+ .await
+ .expect("status must remain available during startup")
+ .is_none());
+ drop(startup);
+ assert!(lock_migration_server(&server, true).await.is_some());
+ }
+
#[tokio::test]
async fn explicit_retry_waits_for_restart_then_owns_storage_before_later_restarts() {
let lifecycle = Arc::new(tokio::sync::Mutex::new(()));