From 1fd00c61033b92d74d9b0b70e1abca39aa6b650c Mon Sep 17 00:00:00 2001 From: Tyler Green Date: Mon, 23 Nov 2026 08:14:16 -0600 Subject: [PATCH] Let a plugin manifest name the oldest Buzz it runs on A plugin that starts using a host feature has to keep working on every Buzz that lacks it, by feature-detecting each one, because nothing stops an older Buzz from installing or updating to it. A manifest can now set "minBuzzVersion": "1.2.0". The desktop tells the plugin manager its own version, and an install, update or reload to a plugin that needs a newer Buzz is refused with a message naming both versions; an installed copy stays where it is. A prerelease counts as its release. A Buzz from before this field already refuses such a manifest as an unknown key. The check is not part of Manifest::validate, which runs on every installed plugin each time the registry is read, so a plugin installed before a downgrade does not stop the others from loading. The buzzodz CLI passes no version and refuses nothing. Co-authored-by: Swiper <24077d5cda8e7e757be4b5e182ef5a8daf7c0447eb339cbf9c3d148fbcc8ecfa@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 Signed-off-by: Tyler Green --- crates/plugin-manager/src/lib.rs | 211 ++++++++++++++++++++++++++- crates/plugin-manager/src/main.rs | 1 + crates/plugin-manager/src/managed.rs | 1 + docs/managed-plugins.md | 5 + docs/plugin-authoring-guide.md | 1 + docs/plugins.md | 4 + src-tauri/src/lib.rs | 8 +- src/plugins/api.ts | 3 + 8 files changed, 230 insertions(+), 4 deletions(-) diff --git a/crates/plugin-manager/src/lib.rs b/crates/plugin-manager/src/lib.rs index c5de4d049c..70c37617c3 100644 --- a/crates/plugin-manager/src/lib.rs +++ b/crates/plugin-manager/src/lib.rs @@ -36,6 +36,11 @@ pub struct Manifest { pub id: String, pub name: String, pub api_version: u32, + /// The oldest Buzz release the plugin runs on, as `MAJOR.MINOR.PATCH`. A Buzz + /// older than this refuses to install the plugin or update to it. A Buzz from + /// before this field rejects the manifest as an unknown field, so it refuses too. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub min_buzz_version: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub description: Option, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -223,7 +228,51 @@ fn valid_program(program: &str, args: &[String]) -> bool { .iter() .all(|argument| argument.len() <= 1024 && !argument.contains('\0')) } +/// A Buzz version, compared by its `MAJOR.MINOR.PATCH` core. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct BuzzVersion(u64, u64, u64); +impl BuzzVersion { + /// A release version as a manifest names it: `1.2.0`, with no prerelease or build. + pub fn release(text: &str) -> Option { + let mut parts = text.split('.').map(|part| { + let digits = !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit()); + let canonical = part == "0" || !part.starts_with('0'); + (digits && canonical && part.len() <= 9) + .then(|| part.parse().ok()) + .flatten() + }); + let version = Self(parts.next()??, parts.next()??, parts.next()??); + parts.next().is_none().then_some(version) + } + /// The running app's version, as Tauri reports it. A prerelease counts as its + /// release (`1.2.0-preview.3` as `1.2.0`), so the previews and betas of the + /// release that adds a feature can install plugins that need it. + pub fn app(text: &str) -> Option { + Self::release(text.split(['-', '+']).next()?) + } +} +impl std::fmt::Display for BuzzVersion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}.{}.{}", self.0, self.1, self.2) + } +} impl Manifest { + /// Refuses a plugin that needs a newer Buzz than `app`. This is separate from + /// `validate`, which also checks every installed plugin on each read: a plugin + /// installed before a downgrade must not stop the others from loading. + pub fn check_buzz_version(&self, app: Option) -> Result<()> { + let required = self + .min_buzz_version + .as_deref() + .and_then(BuzzVersion::release); + match (app, required) { + (Some(app), Some(required)) if app < required => Err(format!( + "{} needs Buzz {required} or later, and this is Buzz {app}. Update Buzz to install it.", + self.name + )), + _ => Ok(()), + } + } pub fn validate(&self) -> Result<()> { valid_id(&self.id)?; if self.name.trim().is_empty() || self.name.len() > 80 { @@ -239,6 +288,13 @@ impl Manifest { if self.api_version != 1 { return Err("Only page plugin API version 1 is supported".into()); } + if self + .min_buzz_version + .as_deref() + .is_some_and(|version| BuzzVersion::release(version).is_none()) + { + return Err("minBuzzVersion must be a release version such as 1.2.0".into()); + } if let Some(discovery) = &self.discovery { if let Some(category) = &discovery.category { valid_id(category)?; @@ -1032,6 +1088,8 @@ pub struct Manager { packaged_fetch: std::sync::Arc>, automatic: automatic::Session, policy_signer: Option, + /// The running Buzz, for manifests' `minBuzzVersion`. `None` refuses nothing. + buzz_version: Option, } impl Manager { /// Host control-plane location that protected workers must not modify. @@ -1062,8 +1120,17 @@ impl Manager { automatic: Default::default(), policy_signer: (!managed::PACKAGED_SIGNER.is_empty()) .then(|| managed::PACKAGED_SIGNER.to_owned()), + buzz_version: None, }) } + /// Refuse to install a plugin, or update or reload one, when its manifest needs + /// a newer Buzz than `version`. The desktop passes its own version. Without + /// one, as in the `buzzodz` CLI, or with one that does not parse, nothing is + /// refused. + pub fn with_buzz_version(mut self, version: &str) -> Self { + self.buzz_version = BuzzVersion::app(version); + self + } /// Opens the environment's plugin home, else `home`: the desktop passes its own app data folder. pub fn from_env(home: Option) -> Result { Self::open( @@ -1350,6 +1417,7 @@ impl Manager { let bytes = &release.bytes; let artifact = release.artifact()?; let manifest = artifact.manifest.clone(); + manifest.check_buzz_version(self.buzz_version)?; let readme_revision = artifact.readme_revision(); let icon_png_revision = icon_revision(artifact.icon_png.as_deref()); let icon_jpg_revision = icon_revision(artifact.icon_jpg.as_deref()); @@ -1530,6 +1598,7 @@ impl Manager { let release = prepare_reload_artifact(&snapshot.1)?; let artifact = release.artifact()?; let manifest = artifact.manifest.clone(); + manifest.check_buzz_version(self.buzz_version)?; let readme_revision = artifact.readme_revision(); let icon_png_revision = icon_revision(artifact.icon_png.as_deref()); let icon_jpg_revision = icon_revision(artifact.icon_jpg.as_deref()); @@ -1917,7 +1986,7 @@ fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> { mod tests { use super::{ artifact_from_text, bundled_manifests, enabled_by_default, validate_candidate_path, - Manager, Manifest, Release, MAX_HOST_COMMAND_OUTPUT_BYTES, + BuzzVersion, Manager, Manifest, Release, MAX_HOST_COMMAND_OUTPUT_BYTES, }; use std::collections::BTreeMap; use std::fs; @@ -3163,6 +3232,146 @@ mod tests { ); } + #[test] + fn min_buzz_version_is_a_release_version_and_absent_by_default() { + let old: Manifest = + serde_json::from_str(r#"{"id":"example.page","name":"Example","apiVersion":1}"#) + .unwrap(); + assert_eq!(old.min_buzz_version, None); + assert!(old.check_buzz_version(BuzzVersion::app("0.0.1")).is_ok()); + assert_eq!( + serde_json::to_value(&old).unwrap(), + serde_json::json!({"id":"example.page","name":"Example","apiVersion":1}) + ); + let with = |version: &str| { + serde_json::from_value::(serde_json::json!({ + "id": "example.page", "name": "Example", "apiVersion": 1, + "minBuzzVersion": version + })) + .unwrap() + }; + for valid in ["0.0.0", "1.2.0", "10.20.30"] { + assert!(with(valid).validate().is_ok(), "{valid}"); + } + for invalid in [ + "", + "1", + "1.2", + "1.2.3.4", + "01.2.3", + "1.02.3", + "1.2.3-beta.1", + "1.2.3+b", + ">=1.2.3", + "^1.2.3", + "v1.2.3", + " 1.2.3", + "1.2.x", + "1..3", + "1234567890.0.0", + ] { + assert!(with(invalid).validate().is_err(), "{invalid:?}"); + } + } + + #[test] + fn app_versions_compare_by_release_and_prereleases_count_as_their_release() { + assert_eq!(BuzzVersion::app("1.2.3"), BuzzVersion::release("1.2.3")); + assert_eq!( + BuzzVersion::app("1.2.3-preview.4"), + BuzzVersion::release("1.2.3") + ); + assert_eq!( + BuzzVersion::app("1.2.3-beta.1+abc"), + BuzzVersion::release("1.2.3") + ); + assert_eq!(BuzzVersion::app("not a version"), None); + let manifest: Manifest = serde_json::from_str( + r#"{"id":"example.page","name":"Example","apiVersion":1,"minBuzzVersion":"1.10.0"}"#, + ) + .unwrap(); + for (app, ok) in [ + ("1.9.9", false), + ("1.10.0-preview.1", true), + ("1.10.0", true), + ("1.10.1", true), + ("2.0.0", true), + ("0.99.0", false), + ] { + assert_eq!( + manifest.check_buzz_version(BuzzVersion::app(app)).is_ok(), + ok, + "{app}" + ); + } + let error = manifest + .check_buzz_version(BuzzVersion::app("1.9.9")) + .unwrap_err(); + assert!(error.contains("needs Buzz 1.10.0 or later"), "{error}"); + assert!(error.contains("this is Buzz 1.9.9"), "{error}"); + // A manager that does not know its Buzz version refuses nothing. + assert!(manifest.check_buzz_version(None).is_ok()); + } + + #[test] + fn a_plugin_that_needs_a_newer_buzz_is_not_installed_updated_or_reloaded() { + let temp = tempfile::tempdir().unwrap(); + let manager = Manager::open(Some(temp.path().into()), "test", false) + .unwrap() + .with_buzz_version("1.4.0"); + let source = temp.path().join("build"); + fs::create_dir(&source).unwrap(); + fs::write(source.join("plugin.js"), "export function apply() {}").unwrap(); + let write = |min: &str| { + fs::write( + source.join("manifest.json"), + format!( + r#"{{"id":"example.page","name":"Example","apiVersion":1,"minBuzzVersion":"{min}"}}"# + ), + ) + .unwrap(); + sign_source(&source); + }; + let installed = |manager: &Manager| { + manager + .catalog() + .unwrap() + .plugins + .into_iter() + .find(|plugin| plugin.manifest.id == "example.page") + }; + + write("1.5.0"); + let error = manager.install(&source).err().unwrap(); + assert!(error.contains("needs Buzz 1.5.0 or later"), "{error}"); + assert!(installed(&manager).is_none()); + + write("1.4.0"); + manager.install(&source).unwrap(); + let first = installed(&manager).unwrap().revision; + + // An update or reload to a version that needs a newer Buzz keeps the current one. + write("1.5.0"); + for error in [ + manager.install(&source).err().unwrap(), + manager.reload("example.page").err().unwrap(), + ] { + assert!(error.contains("needs Buzz 1.5.0 or later"), "{error}"); + } + let current = installed(&manager).unwrap(); + assert_eq!(current.revision, first); + assert_eq!(current.manifest.min_buzz_version.as_deref(), Some("1.4.0")); + + // After an update to Buzz it installs, and a later downgrade still lists it: + // an installed manifest is not rechecked against the running Buzz. + let newer = Manager::open(Some(temp.path().into()), "test", false) + .unwrap() + .with_buzz_version("1.5.0-beta.2"); + newer.install(&source).unwrap(); + assert_ne!(installed(&newer).unwrap().revision, first); + assert!(installed(&manager).is_some()); + } + #[test] fn reload_rejects_changed_host_grants_before_enable() { let temp = tempfile::tempdir().unwrap(); diff --git a/crates/plugin-manager/src/main.rs b/crates/plugin-manager/src/main.rs index d25f52f0d4..ab0af87731 100644 --- a/crates/plugin-manager/src/main.rs +++ b/crates/plugin-manager/src/main.rs @@ -317,6 +317,7 @@ fn init(directory: &Path, id: &str, name: &str) -> Result<()> { id: id.into(), name: name.into(), api_version: 1, + min_buzz_version: None, description: None, discovery: None, updates: None, diff --git a/crates/plugin-manager/src/managed.rs b/crates/plugin-manager/src/managed.rs index be86f012eb..d75e858aaa 100644 --- a/crates/plugin-manager/src/managed.rs +++ b/crates/plugin-manager/src/managed.rs @@ -683,6 +683,7 @@ pub(crate) mod tests { id: id.into(), name: id.into(), api_version: 1, + min_buzz_version: None, description: None, discovery: None, updates: None, diff --git a/docs/managed-plugins.md b/docs/managed-plugins.md index bdab5e8598..8c64e1fb32 100644 --- a/docs/managed-plugins.md +++ b/docs/managed-plugins.md @@ -108,6 +108,11 @@ one. Otherwise the person gets the update to review. The pin wins over the perso choice of version. A plugin installed from a folder or bundled with Buzz is never replaced. +A pinned version whose manifest sets a `minBuzzVersion` newer than the running Buzz is +not installed. An installed copy stays at its current version, and an automatic entry is +tried again on the next run. Pin such a version only once the people it reaches can have +that Buzz. + ### Signed policies A build that should honor automatic entries names the key allowed to sign its policy: diff --git a/docs/plugin-authoring-guide.md b/docs/plugin-authoring-guide.md index 92770a3494..1fe0f37d4e 100644 --- a/docs/plugin-authoring-guide.md +++ b/docs/plugin-authoring-guide.md @@ -47,6 +47,7 @@ export function apply(ctx) { ## Contract rules - **Manifest:** `id`, `name`, `apiVersion` (`1`), and optional `description`, `discovery` and `host`. Unknown keys reject the whole folder. +- **Minimum Buzz:** optional `"minBuzzVersion": "1.2.0"` names the oldest Buzz release the plugin runs on, as `MAJOR.MINOR.PATCH`. An older Buzz refuses to install the plugin, or to update or reload it to that version, and keeps the installed copy. Previews and betas count as their release. A Buzz from before the field rejects it as an unknown key, so it refuses too, with a less helpful message. Set it when a release starts using host features that older Buzz lacks, instead of feature-detecting them. - **Plugin id:** lowercase letters, digits, `.` and `-`, starting with a letter, at most 80 bytes, no empty segments, no underscores. Bundled plugins' ids are reserved. Updates match on `id`. - **Contribution ids** (pages, panels, shortcuts and so on) match `/^[a-z0-9][a-z0-9._-]*$/` and are unique within the plugin. - **No work at module top level.** Do everything in `apply`. Enabled plugins activate even when their page is never opened. diff --git a/docs/plugins.md b/docs/plugins.md index e86aac0e38..65340f6d33 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -43,6 +43,10 @@ version, nothing is promised across versions, and the author types are versioned [`src/plugins/author.ts`](../src/plugins/author.ts) and the bundled plugins in `src/bundled/`. +A plugin that needs a newer host can say so: a manifest's `minBuzzVersion` stops +an older Buzz from installing it or updating to it (see the +[authoring guide](plugin-authoring-guide.md#contract-rules)). + Some parts change less often than others: - **Steadier:** the envelope (manifest, `inject`/`apply`, `ctx.effect`), owning a diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d6a60fc234..5569650183 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1040,6 +1040,7 @@ pub fn run() { .unwrap_or_else(|message| sign_out::exit_with(&message)); let identity = IdentityHost::default(); let identifier = context.config().identifier.clone(); + let buzz_version = context.package_info().version.to_string(); let builder = tauri::Builder::default(); let builder = if !tauri::is_dev() { // Single instance comes first, as its documentation requires. Its deep-link @@ -1333,9 +1334,10 @@ pub fn run() { .manage(DeepLinks::default()) // Plugin storage lives in this identifier's app data, so development // builds keep theirs apart from the installed app. - .manage(PluginManager(Manager::from_env( - dirs::data_dir().map(|dir| dir.join(&identifier)), - ))) + .manage(PluginManager( + Manager::from_env(dirs::data_dir().map(|dir| dir.join(&identifier))) + .map(|manager| manager.with_buzz_version(&buzz_version)), + )) .invoke_handler({ let application_commands = commands::(); let browser_commands: fn(tauri::ipc::Invoke) -> bool = tauri::generate_handler![ diff --git a/src/plugins/api.ts b/src/plugins/api.ts index 12ece12adf..ac7a83332c 100644 --- a/src/plugins/api.ts +++ b/src/plugins/api.ts @@ -7,6 +7,9 @@ export type PluginManifest = Readonly<{ id: string; name: string; apiVersion: 1; + /** Oldest Buzz release the plugin runs on, as `MAJOR.MINOR.PATCH`. An older + * Buzz refuses to install the plugin or update to it. */ + minBuzzVersion?: string; description?: string; /** Support Git update notices. The user's saved choice overrides this default. */ updates?: Readonly<{ defaultEnabled: boolean }>;