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(app): heal wipe-primed empty settings store in place on L5 refusal (#4794)
Ezra Ellette253da27325d6
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.tomlindex c79813625..f5a5344e6 100644--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml@@ -297,6 +297,9 @@ jemalloc = ["tikv-jemallocator"]tempfile = "3"wiremock = "0.6"serial_test = "3"+# MockRuntime for store.rs recovery tests that must exercise the real+# tauri-plugin-store registry (state + resources table).+tauri = { version = "=2.11.2", features = ["test"] }[package.metadata.cargo-machete]diff --git a/apps/screenpipe-app-tauri/src-tauri/src/store.rs b/apps/screenpipe-app-tauri/src-tauri/src/store.rsindex ae447ce0b..626561803 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/store.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/store.rs@@ -82,7 +82,11 @@ const APP_ENTITLEMENT_CLOCK_SKEW_MINUTES: i64 = 5;// leave it in place.// L5: after the plugin builds, if the disk file has a `settings` key but// the loaded store doesn't, the load silently failed — refuse to hand-// out the wipe-primed handle.+// out the wipe-primed handle AND heal the registered instance in+// place via reload() (the plugin registers stores before we can+// inspect them and serves registry hits without re-reading disk, so+// an unhealed instance would be re-served to retries, the webview,+// and the exit-time save-all).// ---------------------------------------------------------------------------/// Suffix for the most-recent known-healthy snapshot.@@ -538,8 +542,18 @@ static STORE_CACHE: Mutex<Option<Arc<tauri_plugin_store::Store<tauri::Wry>>>> =/// Build (or rebuild) the store, retrying on TOCTOU races and stale resource IDs.fn build_store(app: &AppHandle) -> anyhow::Result<Arc<tauri_plugin_store::Store<tauri::Wry>>> {let base_dir = get_base_dir(app, None)?;- let store_path = base_dir.join("store.bin");-+ build_store_at(app, base_dir.join("store.bin"))+}++/// Runtime-generic core of [`build_store`]: decrypt, snapshot recovery and+/// the guarded build-retry loop over an already-resolved store path. Split+/// out so the recovery layers can be tested against `tauri::test::MockRuntime`+/// — the registry the L5 guard must clean up lives in tauri-managed state,+/// unreachable from pure path-based tests.+fn build_store_at<R: tauri::Runtime>(+ app: &tauri::AppHandle<R>,+ store_path: std::path::PathBuf,+) -> anyhow::Result<Arc<tauri_plugin_store::Store<R>>> {// Decrypt store.bin before the plugin reads it (no-op if plain JSON or keychain unavailable)if store_path.exists() && decrypt_store_file(&store_path) == DecryptOutcome::Locked {// L2b — the encrypted blob is unreadable (key denied/missing or@@ -613,6 +627,37 @@ fn build_store(app: &AppHandle) -> anyhow::Result<Arc<tauri_plugin_store::Store<store_path.display(),attempt + 1);+ // The plugin registered this instance before we could+ // inspect it and serves it from the registry on every+ // later build — ours AND the webview's — without ever+ // re-reading disk, so a bare `continue` would retry into+ // the same empty store and the first save would flush+ // defaults over the user's data. Heal it in place:+ // reload() re-reads the file with errors VISIBLE (build+ // swallows them), and fixing the shared instance fixes+ // every handle already pointing at it. Deliberately no+ // registry ejection — close_resource() takes the+ // resources-table and stores-map locks in the opposite+ // order to build_inner (ABBA deadlock risk) and would+ // invalidate resource ids held by live webview handles.+ match s.reload() {+ Ok(()) if s.get("settings").is_some() => {+ tracing::warn!(+ "store healed in place from {} after a \+ silently-swallowed load failure",+ store_path.display()+ );+ encrypt_store_file(&store_path);+ return Ok(s);+ }+ Ok(()) => tracing::error!(+ "store reload succeeded but settings key still \+ missing — retrying"+ ),+ Err(e) => {+ tracing::error!("store reload from disk failed: {} — retrying", e)+ }+ }last_err = None;std::thread::sleep(std::time::Duration::from_millis(100 * (attempt as u64 + 1),@@ -2481,6 +2526,70 @@ mod tests {);}+ #[test]+ fn l5_refusal_heals_wipe_primed_store_in_place() {+ // The plugin's build_inner registers a freshly-built store in its+ // registry BEFORE build_store can refuse it, and `.build()` serves+ // registry hits without re-reading disk. If the L5 refusal only+ // dropped its local Arc, every retry — and the webview's Store.load,+ // and the plugin's exit-time save-all — would keep getting the same+ // wipe-primed EMPTY instance, so the first save would flush defaults+ // over the user's real store.bin. The refusal must reload the SHARED+ // instance from disk in place: healing it heals every handle already+ // pointing at it (a webview's included), keeps its resource id valid,+ // and needs no registry surgery (whose lock order inverts the+ // plugin's and risks deadlock).+ use tauri_plugin_store::StoreExt;++ let tmp = tempfile::tempdir().unwrap();+ let store_path = tmp.path().join("store.bin");++ let app = tauri::test::mock_builder()+ .plugin(tauri_plugin_store::Builder::default().build())+ .build(tauri::test::mock_context(tauri::test::noop_assets()))+ .expect("failed to build mock app");++ // Poison the registry the way a transient read failure does: the+ // plugin swallows the load error and registers an EMPTY store. This+ // handle also stands in for one a webview grabbed before recovery.+ std::fs::write(&store_path, b"transient garbage").unwrap();+ let poisoned = StoreBuilder::new(app.handle(), store_path.clone())+ .build()+ .expect("plugin builds an empty store from an unreadable file");+ assert!(+ poisoned.get("settings").is_none(),+ "precondition: the registered store is empty"+ );++ // The disk heals underneath it — the user's data was there all along.+ write_store(+ tmp.path(),+ &json!({"settings": {"aiPresets": presets_n(2)}}),+ );++ let store = build_store_at(app.handle(), store_path.clone())+ .expect("must heal the poisoned instance from the disk state");+ assert!(+ store.get("settings").is_some(),+ "must hand out the on-disk settings, not the wipe-primed empty store"+ );+ assert!(+ Arc::ptr_eq(&poisoned, &store),+ "must heal the shared registered instance, not swap in a new one"+ );+ assert!(+ poisoned.get("settings").is_some(),+ "handles obtained before recovery must see the healed state"+ );++ // The registry — what the webview's Store.load and the plugin's+ // exit-time save-all read — must also serve the healthy instance.+ let registered = app+ .get_store(&store_path)+ .expect("healthy store must be registered");+ assert!(Arc::ptr_eq(®istered, &store));+ }+// ---- Existing tests ----#[test]