Host the CloudSync runtime in the GPUI shell and activate it from the GPUI session

Closed
DE
2mo ago
+4327-637

Summary

Problem: ANLG-320 Phase 6. After #7541 (GPUI sign-in) and #7542 (shell-neutral DesktopDbRuntime), the GPUI shell still opened the SQLite file with a plain Db::connect_local_read_write and had no CloudSync at all: no runtime, no credential exchange, no recovery-key setup, and the E2EE glue (load_e2ee_recovery_key, open_shared_workspace_keyrings, recovery-key create/inspect/import, the bodies of the configure_cloudsync_token / configure_e2ee_replica commands) lived only inside tauri-plugin-db command handlers.

Fix: Stacked on #7542 and merging #7541's branch (both are needed; the diff shown here therefore includes #7541's auth commits until it lands).

  • desktop-db-runtime::cloudsync_config: the command bodies move here behind E2eeSecretReader/E2eeSecretWriter traits (secret access is the only shell-specific piece):
    impl<S: QueryEventSink> DesktopDbRuntime<S> {
        pub async fn configure_cloudsync_token_with_keys(&self, secrets: &dyn E2eeSecretReader, ...) -> Result<CloudsyncTokenConfigurationResult, String>;
        pub async fn configure_e2ee_replica_with_keys(&self, secrets: &dyn E2eeSecretReader, ...) -> Result<..., String>;
    }
    pub fn create_e2ee_recovery_code() -> Result<String, String>;
    pub fn inspect_e2ee_recovery_key(code: &str) -> Result<String /* key_id */, String>;
    pub async fn import_e2ee_recovery_key(secrets: &dyn E2eeSecretWriter, account_user_id: &str, code: &str) -> Result<(), String>;
    
    Tauri commands become a TauriE2eeSecrets(AppHandle) adapter over tauri_plugin_store2 plus calls into these; command signatures and plugins/db/js/permissions are unchanged.
  • GPUI Store::open now opens through open_app_db(Some(path)) (CloudSync-capable open + prepare_schema, creating the database like Tauri does instead of bailing when it's missing) and owns Arc<DesktopDbRuntime<GpuiQueryEventSink>>, running ensure_app_schema / finish_startup like the plugin's bootstrap_app_database — minus the legacy JSON import, which still needs an AppHandle-free port.
  • New apps/desktop-gpui/src/cloudsync.rs, a Rust port of the core of apps/desktop/src/auth/cloudsync*.ts: POST /sync/token (404 → /sync/replica/credentials) with the same headers (X-Anarlog-E2EE-Key-Id, member public key, x-anarlog-cloudsync-transports: replica, x-device-fingerprint, sanitized device name), untagged Legacy/E2ee/Replica response, the same status → CredentialBlock mapping (404/501 unavailable, 401 reauth_required, 403 device_limit/not_entitled, other → retry), post-exchange key-id and account mismatch checks, generation-guarded activation, refresh at expiresAt with the web constants (2 min lead / 1 min retry / 1 s floor), suspend on sign-out. Key id / member public key come from the stored recovery key exactly as get_e2ee_identity_status derives them. Activation runs on startup, after DeepLink::AuthCallback, and after sign-out.
  • Device enrollment (port of enrollCurrentDevice): when no recovery key is stored, GPUI loads/creates the device enrollment key (get_or_create_e2ee_device_identity / import_e2ee_device_enrollment moved into cloudsync_config, Tauri commands become thin wrappers), POSTs /sync/e2ee/device-enrollments, maps e2ee_enrollment_requires_existing_key → setup_required, 403 → device_limit/not_entitled, 401 → reauth_required, polls every 5 s while approval_pending, and on a sealed package imports the recovery key and best-effort acks /consume before the credential exchange.
  • Local-library account switching (parity with #7637): the credential exchange checks local_library_connections for the current account and goes straight to /sync/replica/credentials when the library has been connected (no /sync/token attempt); when activation blocks with identity_mismatch, the signed-in settings section offers "Connect library", which reads the cloudsync_workspace_binding workspace id, calls DesktopDbRuntime::connect_local_library (added on #7542 from main's runtime) and re-activates.
  • Recovery-key setup (port of settings/general/e2ee-setup.tsx): when the block is setup_required the signed-in settings section offers "Create a recovery key" (shows the code read-only with Copy and "I saved it") or "Use an existing key"; both paths run inspect → PUT /sync/e2ee/identity (same 409 / mismatch messages as the web) → import → re-activate. Settings also show a one-line sync status.

Deliberately not in this PR: recovery-key download-to-file and clipboard auto-clear, teardown-wait and pre-emptive suspend logic, device-limit toast, activity deferral, a GPUI cloud_sync_enabled toggle (none exists yet; the setting is read with the web default), legacy import.

Verification

  • cargo test --locked -p desktop-db-runtime (85) + clippy -D warnings; cargo test --locked -p tauri-plugin-db (69)
  • git diff --exit-code plugins/db/js plugins/db/permissions clean
  • /home/ubuntu/gpui-cargo.sh test --locked -p desktop-gpui (467, includes new tests for credential-variant parsing, 403 block mapping, device-name sanitising, refresh delay, recovery-key inspect/import) and clippy --all-targets --no-deps -- -D warnings
  • cargo test --locked -p desktop-gpui cloudsync:: (12, incl. mock-server tests that the replica endpoint is used directly for a connected local library and token-first with 404 fallback otherwise)
  • rustfmt/dprint on changed files, git diff --check
  • Not exercised against a live API/sync server from this VM.

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