diff --git a/crates/engine/src/tests/plugins.rs b/crates/engine/src/tests/plugins.rs index dad65195f3..87a11ba47f 100644 --- a/crates/engine/src/tests/plugins.rs +++ b/crates/engine/src/tests/plugins.rs @@ -1086,10 +1086,11 @@ fn replace_storing(s: &mut Session, id: &str, first: &str) -> Value { /// A plug-in's settings and cache are kept in the plug-in store under its id: the next session /// has them, replacing the plug-in keeps them, another plug-in can't read them, `plugin.clearCache` -/// empties the cache alone and `plugin.remove` deletes both. The caps and the bytes each call is -/// charged follow ([`plugin_storage_is_capped_and_survives_a_damaged_settings_file`], -/// [`plugin_storage_calls_are_charged_the_files_they_read_and_write`]) in the same test, since -/// clearing every plug-in's cache would empty theirs. +/// with its id empties its cache alone and `plugin.remove` deletes both. The caps and the bytes +/// each call is charged follow ([`plugin_storage_is_capped_and_survives_a_damaged_settings_file`], +/// [`plugin_storage_calls_are_charged_the_files_they_read_and_write`]) in the same test. +/// `plugin.clearCache` without an id empties the cache of every plug-in installed in the process, +/// so it is tested in a binary of its own (`tests/plugin_clear_cache.rs`). #[test] fn plugins_keep_settings_and_a_cache_of_their_own_in_the_store() { let folder = crate::tests_links::Folder::new("plugin-storage"); @@ -1116,16 +1117,13 @@ fn plugins_keep_settings_and_a_cache_of_their_own_in_the_store() { install_storing(&mut next, other, READS); assert!(next.execute("plugin.run", &json!({"id": other, "ids": []})).is_err()); - // Clearing the cache leaves the settings. - assert_eq!(run(&mut next, "plugin.clearCache", json!({"id": id})), json!({"cleared": [id], "bytesFreed": 3})); - assert_eq!(run(&mut next, "plugin.info", json!({"id": id}))["cacheBytes"], json!(0)); - run(&mut next, "plugin.run", json!({"id": id, "ids": []})); + // Clearing the cache leaves the settings, and the other plug-in's cache. replace_storing(&mut next, other, WRITES); run(&mut next, "plugin.run", json!({"id": other, "ids": []})); - let all = run(&mut next, "plugin.clearCache", json!({})); - let cleared = all["cleared"].as_array().unwrap(); - assert!(cleared.contains(&json!(id)) && cleared.contains(&json!(other)) && all["bytesFreed"] == json!(3), "{all}"); - assert_eq!(run(&mut next, "plugin.info", json!({"id": other}))["cacheBytes"], json!(0)); + assert_eq!(run(&mut next, "plugin.clearCache", json!({"id": id})), json!({"cleared": [id], "bytesFreed": 3})); + assert_eq!(run(&mut next, "plugin.info", json!({"id": id}))["cacheBytes"], json!(0)); + assert_eq!(run(&mut next, "plugin.info", json!({"id": other}))["cacheBytes"], json!(3)); + run(&mut next, "plugin.run", json!({"id": id, "ids": []})); assert!(next.execute("plugin.clearCache", &json!({"id": "test.engine.storing.missing"})).is_err()); run(&mut next, "plugin.remove", json!({"id": id})); diff --git a/crates/engine/tests/plugin_clear_cache.rs b/crates/engine/tests/plugin_clear_cache.rs new file mode 100644 index 0000000000..74ba69d644 --- /dev/null +++ b/crates/engine/tests/plugin_clear_cache.rs @@ -0,0 +1,65 @@ +//! `plugin.clearCache` without an id empties the cache of every installed plug-in and leaves their +//! settings. +//! +//! A test binary of its own: installed plug-ins are process-wide, so clearing every plug-in's cache +//! in the engine's other tests would empty the caches those tests check. +// Integration tests: unwrapping and panicking on failure is fine here, unlike in shipped code (AGENTS.md › Robustness). +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use serde_json::{Value, json}; +use vectorcraft_engine::Session; +use vectorcraft_plugins::wat as tpl; + +/// Sets "zoom" to "hello" and writes "hel" to the cache entry "tile". +const WRITES: &str = "(if (call $vc_settings_set (i32.const 0x2000) (i32.const 4) (i32.const 0x2100) (i32.const 5)) (then unreachable)) + (if (call $vc_cache_write (i32.const 0x2004) (i32.const 4) (i32.const 0x2100) (i32.const 3)) (then unreachable))"; + +/// Traps unless "zoom" is "hello". +const READS: &str = + "(if (i32.ne (call $vc_settings_get (i32.const 0x2000) (i32.const 4) (i32.const 0x3000) (i32.const 16)) (i32.const 5)) (then unreachable)) + (if (i32.ne (i32.load (i32.const 0x3000)) (i32.load (i32.const 0x2100))) (then unreachable))"; + +fn run(s: &mut Session, cmd: &str, p: Value) -> Value { + s.execute(cmd, &p).unwrap_or_else(|e| panic!("{cmd}: {e}")) +} + +/// Installs a filter `id` of API level 2 that lists `settings` and `cache` and whose `vc_run` runs +/// `first`, in place of any module installed with its id (`replace: true`). +fn install_storing(s: &mut Session, id: &str, first: &str) { + let manifest = format!(r#"{{"id":"{id}","name":"Test {id}","kind":"filter","api":2,"permissions":["settings","cache"]}}"#); + let data = format!(r#"{} (data (i32.const 0x2000) "zoomtile") (data (i32.const 0x2100) "hello")"#, tpl::STORAGE_IMPORTS); + let wasm = wat::parse_str(tpl::with_imports(&manifest, &data, first, r#"{"objects":[]}"#)).unwrap(); + let data = vectorcraft_format::base64_encode(&wasm); + run(s, "plugin.install", json!({"dataBase64": data, "grant": ["settings", "cache"], "replace": true})); +} + +#[test] +fn clearing_every_cache_empties_each_plugins_cache_and_keeps_its_settings() { + let folder = std::env::temp_dir().join(format!("vc-plugin-clear-cache-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&folder); + std::fs::create_dir_all(&folder).unwrap(); + let ids = ["test.clearcache.a", "test.clearcache.b"]; + let mut s = Session::new(); + s.execute("file.new", &json!({"width": 400, "height": 200})).unwrap(); + s.attach_plugin_store(folder.clone()); + for id in ids { + install_storing(&mut s, id, WRITES); + run(&mut s, "plugin.run", json!({"id": id, "ids": []})); + assert_eq!(run(&mut s, "plugin.info", json!({"id": id}))["cacheBytes"], json!(3)); + } + + assert_eq!(run(&mut s, "plugin.clearCache", json!({})), json!({"cleared": ids, "bytesFreed": 6})); + for id in ids { + assert_eq!(run(&mut s, "plugin.info", json!({"id": id}))["cacheBytes"], json!(0)); + // The settings stay: a module that reads them runs. + install_storing(&mut s, id, READS); + run(&mut s, "plugin.run", json!({"id": id, "ids": []})); + } + // Nothing is left to clear. + assert_eq!(run(&mut s, "plugin.clearCache", json!({})), json!({"cleared": ids, "bytesFreed": 0})); + + for id in ids { + run(&mut s, "plugin.remove", json!({"id": id})); + } + let _ = std::fs::remove_dir_all(&folder); +} diff --git a/crates/engine/tests/plugin_fuzz.rs b/crates/engine/tests/plugin_fuzz.rs index a7e45f6904..5087ad2e53 100644 --- a/crates/engine/tests/plugin_fuzz.rs +++ b/crates/engine/tests/plugin_fuzz.rs @@ -681,7 +681,17 @@ proptest! { use vectorcraft_plugins::host::STORAGE_BUDGET; use vectorcraft_plugins::storage::Meter; const FUNCS: [&str; 6] = ["vc_settings_get", "vc_settings_set", "vc_cache_read", "vc_cache_write", "vc_settings_delete", "vc_cache_clear"]; - let folder = std::env::temp_dir().join(format!("vectorcraft-plugin-fuzz-storage-{}", std::process::id())); + // Each case starts with an empty store, and the store is deleted when the case ends, also + // when it fails. + struct Store(std::path::PathBuf); + impl Drop for Store { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let store = Store(std::env::temp_dir().join(format!("vectorcraft-plugin-fuzz-storage-{}", std::process::id()))); + let _ = std::fs::remove_dir_all(&store.0); + let folder = store.0.clone(); let mut s = Session::new(); s.attach_plugin_store(folder.clone()); let storing = |id: &str| format!(r#"{{"id":"{id}","name":"F","kind":"filter","api":2,"permissions":["settings","cache"]}}"#); @@ -704,8 +714,11 @@ proptest! { let text: String = key.iter().map(|b| format!("\\{b:02x}")).collect(); let imports = format!(r#"{} (data (i32.const 0x2000) "{text}")"#, tpl::STORAGE_IMPORTS); let wasm = wat::parse_str(tpl::with_imports(&storing("fuzz.storage"), &imports, &format!("(local $r i32) {body}"), r#"{"objects":[]}"#)).unwrap(); + // A failed case leaves its module installed, and the cases that shrink it install others + // under the same id. let r = catch_quiet(|| { - s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "grant": grant})).map_err(|e| e.to_string())?; + s.execute("plugin.install", &json!({"dataBase64": vectorcraft_format::base64_encode(&wasm), "grant": grant, "replace": true})) + .map_err(|e| e.to_string())?; let p = vectorcraft_plugins::registry::get("fuzz.storage").ok_or("not installed")?; p.run(b"{\"objects\":[]}", b"{}").map_err(|e| e.to_string()) }); diff --git a/docs/mcp.md b/docs/mcp.md index c39581af70..94913592d7 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -6497,7 +6497,8 @@ under its id. One that lists `settings` or `cache` keeps settings or a cache of (VectorCraft's own caps: 256 settings and 1 MiB, 512 MiB of cache): `plugin.list` and `plugin.info` give the cache's size as `cacheBytes`, `plugin.clearCache {id?}` empties the cache of one plug-in or of all of them and returns `{cleared: [id], bytesFreed}`, replacing a plug-in keeps both, and `plugin.remove` deletes both (also for an id that -isn't installed but still has them). A module with other bytes than the plug-in installed with its id, or, with none +isn't installed but still has them). Files it can't delete stay in the plug-in's folder renamed with `.removed`, where +no plug-in reaches them, until a plug-in with that id is removed again ([plugins.md](plugins.md#settings-and-cache)). A module with other bytes than the plug-in installed with its id, or, with none installed, one whose id has settings or a cache in the `Plug-ins` folder (`needsReplace: true` in its `plugin.inspect` entry), installs only with `replace: true`: ask the user first, since it takes that plug-in's place and gets its settings and cache. Without it, `plugin.install` returns diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index b21af694ae..5fcaab0726 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1544,7 +1544,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - **Plug-in instruction budget at a bulk memory instruction:** a run that pauses where one slice of its instruction budget ends keeps the instructions the slice still held. A `memory.fill` or `memory.copy` that the slice can't pay for in full runs on the next slice, so a run whose budget covers its work finishes; a run ends with `Limit("instruction budget")` only when the instructions it has left can't pay for the next instruction. Regressions: `crates/plugins/tests/abi.rs`. Scores and hours unchanged. - **Plug-in permissions (G17):** a manifest may list `permissions` (`doc.read`, `doc.write`, `state`, `effect.render`, `export.write`; the first three follow storytold/photocraft#662) and an `api` level; without the list a plug-in gets what its kind implies and installs as before. Install Plug-in… and File › Open show the name, id, version, file SHA-256 and each permission in plain words, with control and bidirectional characters escaped, and install nothing on Cancel; `plugin.install` needs `grant`, and folders install only granted plug-ins. The grant is kept with the module's hash, a module asking for more asks again, and a filter run refuses output its permissions don't allow. Regressions: `crates/plugins/src/permissions.rs`, `crates/plugins/src/manifest.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`, `crates/ui-egui/src/tests/plugins.rs`. Scores and hours unchanged. - **Plug-in progress, Cancel and log lines (G17):** at plug-in API level 2 a plug-in with the `progress` permission imports `env.vc_progress`, `env.vc_cancelled` and `env.vc_log`. A heavy filter's and an export's progress windows show its progress as a percentage bar, their Cancel reaches it through `vc_cancelled`, and its log lines go to the app's log with its id, capped per line and per run. Installing checks every import and names the one it refuses. Regressions: `crates/plugins/src/host.rs`, `crates/plugins/tests/abi.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`, `crates/ui-egui/src/tests/plugin_runs.rs`. Scores and hours unchanged. -- **Plug-in settings and cache (G17, #2191):** at plug-in API level 2 a plug-in with the `settings` permission keeps settings of its own (`env.vc_settings_get`, `_set`, `_delete`) and one with `cache` a cache the user can clear (`env.vc_cache_read`, `_write`, `_clear`), under its id in the `Plug-ins` folder. Keys are plain names, never paths; VectorCraft's own caps refuse a write past 256 settings, 1 MiB of settings or 512 MiB of cache; a damaged settings file is moved aside and the settings start empty. `plugin.clearCache` empties a cache, `plugin.list` and `plugin.info` give `cacheBytes`, replacing a plug-in keeps its storage and `plugin.remove` deletes it. Another module under an id that is installed or has storage installs only with `replace: true`, which the app asks the user for; each storage call counts the whole settings file or cache entry it reads and writes toward the run's 1 GiB. Regressions: `crates/plugins/src/storage.rs`, `crates/plugins/tests/abi.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_fuzz.rs`. Scores and hours unchanged. +- **Plug-in settings and cache (G17, #2191):** at plug-in API level 2 a plug-in with the `settings` permission keeps settings of its own (`env.vc_settings_get`, `_set`, `_delete`) and one with `cache` a cache the user can clear (`env.vc_cache_read`, `_write`, `_clear`), under its id in the `Plug-ins` folder. Keys are plain names, never paths; VectorCraft's own caps refuse a write past 256 settings, 1 MiB of settings or 512 MiB of cache; a damaged settings file is moved aside and the settings start empty. `plugin.clearCache` empties a cache, `plugin.list` and `plugin.info` give `cacheBytes`, replacing a plug-in keeps its storage and `plugin.remove` deletes it. Another module under an id that is installed or has storage installs only with `replace: true`, which the app asks the user for; each storage call counts the whole settings file or cache entry it reads and writes toward the run's 1 GiB. Regressions: `crates/plugins/src/storage.rs`, `crates/plugins/tests/abi.rs`, `crates/engine/src/tests/plugins.rs`, `crates/engine/tests/plugin_clear_cache.rs`, `crates/engine/tests/plugin_fuzz.rs`. Scores and hours unchanged. - **Plug-in effects after a plug-in changes:** when a plug-in is installed for the first time, installed again or removed (`plugin.install`, `plugin.reload`, `plugin.remove`, File › Open of a `.wasm` file, the Additional Plug-ins Folder preference), the next frame the canvas draws uses the plug-ins then installed for objects with a plug-in effect and their raster effects, for the blends, envelopes and repeats that show such objects, and for pattern tiles and symbol instances whose art holds one.