Extract the db/CloudSync runtime out of tauri-plugin-db into a shell-neutral crate

Closed
DE
2mo ago
+3088-2827

Summary

Problem: ANLG-320 Phase 6 (CloudSync). The whole desktop database lifecycle — startup/schema readiness, live-query runtime, CloudSync configuration/activity/auth-generation handling, E2EE sync hook, witness watch, poison-recovery and full-resync scheduling (~1,900 lines in plugins/db/src/runtime.rs + runtime/) — was only reachable through tauri-plugin-db, so the GPUI shell could not host CloudSync without duplicating it. The code itself only touched Tauri in two places: tauri::ipc::Channel<QueryEvent> as the live-query sink and tauri::async_runtime::spawn to discover a Tokio handle.

Fix: Move the runtime verbatim (git mv, same file layout, tests included) into crates/desktop-db-runtime (anlg-desktop-db-runtime) and abstract the two touch points:

pub struct DesktopDbRuntime<S: QueryEventSink> { .. }     // was PluginDbRuntime, sink hard-wired to Channel
impl<S: QueryEventSink> DesktopDbRuntime<S> {
    pub fn new(db: Arc<Db>, handle: tokio::runtime::Handle) -> Self   // registers handle with anlg_db_sync, then enters it
}

The plugin keeps the Tauri-specific pieces and re-exports everything else, so commands.rs, js/bindings.gen.ts and permissions/ are unchanged:

// plugins/db/src/runtime.rs
pub struct QueryEventChannel(tauri::ipc::Channel<QueryEvent>);   // impl QueryEventSink
pub type PluginDbRuntime = DesktopDbRuntime<QueryEventChannel>;
// lib.rs: ensure_db_sync_runtime() -> Handle (the tauri::async_runtime discovery) passed to new()

The shared types the runtime needs (StartupStatus/StartupPhase, QueryEvent, TransactionStatement, CloudsyncTokenConfigurationResult, CloudsyncWorkspaceProjection*, CloudsyncE2eeWitness, legacy import/cleanup reports, Error) moved with it; specta::Type derives are behind a specta feature the plugin enables. Pure legacy-import readiness/cleanup SQL moved to desktop_db_runtime::legacy; the AppHandle-dependent rerun_legacy_import stays in the plugin. The transaction-pause test hooks that used to be #[cfg(test)] are now behind a test-hooks feature enabled only from the plugin's dev-dependencies.

No GPUI consumer yet — the next PR wires DesktopDbRuntime into apps/desktop-gpui with a GPUI sink and ports the credential/projection orchestration.

Verification

  • cargo test --locked -p desktop-db-runtime (82 passed — the moved runtime tests) and clippy --all-targets --no-deps -- -D warnings
  • cargo test --locked -p tauri-plugin-db (69 passed), cargo check --locked -p tauri-plugin-db (non-test build has no test hooks)
  • git diff --exit-code plugins/db/js plugins/db/permissions clean
  • rustfmt / dprint on changed files, git diff --check
  • cargo check -p desktop in the ubuntu-24.04 container stops at the pre-existing tauri-plugin-git gix 0.77 break on the base branch (fixed separately in #7540), before reaching this crate.

Link to Devin session: https://app.devin.ai/sessions/f83ed73d85c648fcbdbd041cd4f15720
Open in Devin Desktop: https://app.devin.ai/desktop/session/f83ed73d85c648fcbdbd041cd4f15720?variant=devin
Requested by: @ComputelessComputer