bmux_plugin 0.0.1-alpha.1

Plugin system for bmux terminal multiplexer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use bmux_plugin_sdk::{
    CommandExecutionKind, HostScope, PluginCommand, PluginContext, PluginError,
    PluginEventPublication, PluginEventSubscription, PluginFeature, PluginService, Result,
    VersionRange,
};
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PluginOwnedPath(pub Vec<String>);

impl PluginOwnedPath {
    #[must_use]
    pub fn matches(&self, path: &[String]) -> bool {
        self.0 == path
    }

    #[must_use]
    pub fn is_valid(&self) -> bool {
        !self.0.is_empty() && self.0.iter().all(|segment| !segment.trim().is_empty())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct PluginId(String);

impl PluginId {
    /// Create a new plugin identifier.
    ///
    /// # Errors
    ///
    /// Returns an error if the value is not a valid plugin identifier (must
    /// start with a lowercase ASCII letter and contain only lowercase ASCII
    /// letters, digits, hyphens, underscores, and dots).
    pub fn new(value: impl Into<String>) -> Result<Self> {
        let value = value.into();
        if is_valid_plugin_id(&value) {
            Ok(Self(value))
        } else {
            Err(PluginError::InvalidPluginId { id: value })
        }
    }

    #[must_use]
    pub const fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PluginEntrypoint {
    Native {
        symbol: String,
    },
    Process {
        command: String,
        args: Vec<String>,
        persistent_worker: bool,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum PluginExecutionClass {
    NativeFast,
    #[default]
    NativeStandard,
    Interpreter,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginLifecycle {
    #[serde(default = "default_true")]
    pub activate_on_startup: bool,
    #[serde(default)]
    pub receive_events: bool,
    #[serde(default = "default_true")]
    pub allow_hot_reload: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginDependency {
    pub plugin_id: PluginId,
    #[serde(default = "default_dependency_version_req")]
    pub version_req: String,
    #[serde(default = "default_true")]
    pub required: bool,
}

impl PluginDependency {
    /// # Errors
    ///
    /// Returns an error when the version requirement cannot be parsed.
    pub fn parsed_version_req(&self) -> Result<VersionReq> {
        VersionReq::parse(&self.version_req).map_err(|error| {
            PluginError::InvalidDependencyVersion {
                plugin_id: self.plugin_id.as_str().to_string(),
                dependency_id: self.plugin_id.as_str().to_string(),
                version_req: self.version_req.clone(),
                details: error.to_string(),
            }
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginDeclaration {
    pub id: PluginId,
    pub display_name: String,
    pub plugin_version: String,
    pub plugin_api: VersionRange,
    pub native_abi: VersionRange,
    pub entrypoint: PluginEntrypoint,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub homepage: Option<String>,
    #[serde(default)]
    pub provider_priority: i32,
    #[serde(default)]
    pub execution_class: PluginExecutionClass,
    #[serde(default)]
    pub owns_namespaces: BTreeSet<String>,
    #[serde(default)]
    pub owns_paths: BTreeSet<PluginOwnedPath>,
    #[serde(default)]
    #[serde(alias = "required_host_scopes")]
    pub required_capabilities: BTreeSet<HostScope>,
    #[serde(default)]
    pub provided_capabilities: BTreeSet<HostScope>,
    #[serde(default)]
    pub provided_features: BTreeSet<PluginFeature>,
    #[serde(default)]
    pub services: Vec<PluginService>,
    #[serde(default)]
    pub commands: Vec<PluginCommand>,
    #[serde(default)]
    pub event_subscriptions: Vec<PluginEventSubscription>,
    #[serde(default)]
    pub event_publications: Vec<PluginEventPublication>,
    #[serde(default)]
    pub dependencies: Vec<PluginDependency>,
    #[serde(default)]
    pub lifecycle: PluginLifecycle,
    #[serde(default)]
    pub ready_signals: Vec<bmux_plugin_sdk::ReadySignalDecl>,
}

impl PluginDeclaration {
    /// Merge additional service declarations into this declaration.
    ///
    /// Used by native bundled plugins to add BPDL-generated service
    /// descriptors exported by the plugin binary. Existing manifest services
    /// are preserved so non-BPDL compatibility/override services remain
    /// supported.
    ///
    /// # Errors
    ///
    /// Returns if an added service is invalid or uses a capability the plugin
    /// does not provide.
    pub fn merge_services(
        &mut self,
        services: impl IntoIterator<Item = PluginService>,
    ) -> Result<()> {
        for service in services {
            service.validate(self.id.as_str())?;
            if !self.provided_capabilities.contains(&service.capability) {
                return Err(PluginError::UnownedServiceCapability {
                    plugin_id: self.id.as_str().to_string(),
                    capability: service.capability.as_str().to_string(),
                    interface_id: service.interface_id,
                });
            }
            if !self.services.contains(&service) {
                self.services.push(service);
            }
        }
        self.validate()
    }

    /// # Errors
    ///
    /// Returns an error when the declaration contains duplicate command names or
    /// runtime hook commands without a hot-path capability.
    pub fn validate(&self) -> Result<()> {
        let mut command_names = BTreeSet::new();
        let mut dependency_ids = BTreeSet::new();
        for command in &self.commands {
            if !command_names.insert(command.name.clone()) {
                return Err(PluginError::DuplicateCommand {
                    plugin_id: self.id.as_str().to_string(),
                    command: command.name.clone(),
                });
            }

            if matches!(command.execution, CommandExecutionKind::RuntimeHook)
                && !self
                    .required_capabilities
                    .iter()
                    .any(HostScope::is_hot_path)
            {
                return Err(PluginError::MissingRequiredCapability {
                    plugin_id: self.id.as_str().to_string(),
                    capability: "bmux.terminal.input_intercept".to_string(),
                });
            }

            let canonical_path = command.canonical_path();
            if canonical_path.is_empty() || canonical_path.iter().any(std::string::String::is_empty)
            {
                return Err(PluginError::InvalidPluginCommandPath {
                    plugin_id: self.id.as_str().to_string(),
                    command: command.name.clone(),
                });
            }

            let mut seen_paths = BTreeSet::from([canonical_path.clone()]);
            for alias in &command.aliases {
                if alias.is_empty() || alias.iter().any(std::string::String::is_empty) {
                    return Err(PluginError::InvalidPluginCommandPath {
                        plugin_id: self.id.as_str().to_string(),
                        command: command.name.clone(),
                    });
                }
                if !seen_paths.insert(alias.clone()) {
                    return Err(PluginError::DuplicatePluginCommandAlias {
                        plugin_id: self.id.as_str().to_string(),
                        command: command.name.clone(),
                    });
                }
            }
        }

        for namespace in &self.owns_namespaces {
            if namespace.trim().is_empty() || namespace.split_whitespace().count() != 1 {
                return Err(PluginError::InvalidPluginCommandPath {
                    plugin_id: self.id.as_str().to_string(),
                    command: format!("namespace:{namespace}"),
                });
            }
        }
        for path in &self.owns_paths {
            if !path.is_valid() {
                return Err(PluginError::InvalidPluginCommandPath {
                    plugin_id: self.id.as_str().to_string(),
                    command: format!("owned_path:{}", path.0.join(" ")),
                });
            }
        }

        for capability in &self.provided_capabilities {
            if self.required_capabilities.contains(capability) {
                return Err(PluginError::CapabilitySelfRequirement {
                    plugin_id: self.id.as_str().to_string(),
                    capability: capability.as_str().to_string(),
                });
            }
        }

        for service in &self.services {
            service.validate(self.id.as_str())?;
            if !self.provided_capabilities.contains(&service.capability) {
                return Err(PluginError::UnownedServiceCapability {
                    plugin_id: self.id.as_str().to_string(),
                    capability: service.capability.as_str().to_string(),
                    interface_id: service.interface_id.clone(),
                });
            }
        }

        for dependency in &self.dependencies {
            if dependency.plugin_id == self.id {
                return Err(PluginError::PluginDependencyOnSelf {
                    plugin_id: self.id.as_str().to_string(),
                });
            }
            if !dependency_ids.insert(dependency.plugin_id.as_str().to_string()) {
                return Err(PluginError::DuplicatePluginDependency {
                    plugin_id: self.id.as_str().to_string(),
                    dependency_id: dependency.plugin_id.as_str().to_string(),
                });
            }
            VersionReq::parse(&dependency.version_req).map_err(|error| {
                PluginError::InvalidDependencyVersion {
                    plugin_id: self.id.as_str().to_string(),
                    dependency_id: dependency.plugin_id.as_str().to_string(),
                    version_req: dependency.version_req.clone(),
                    details: error.to_string(),
                }
            })?;
        }

        Ok(())
    }
}

impl Default for PluginLifecycle {
    fn default() -> Self {
        Self {
            activate_on_startup: true,
            receive_events: false,
            allow_hot_reload: true,
        }
    }
}

pub trait NativePlugin: Send + Sync {
    fn declaration(&self) -> &PluginDeclaration;

    /// # Errors
    ///
    /// Returns an error if the context setup fails.
    fn activate(&mut self, _context: &mut PluginContext<'_>) -> Result<()> {
        Ok(())
    }

    /// # Errors
    ///
    /// Returns an error if the cleanup fails.
    fn deactivate(&mut self, _context: &mut PluginContext<'_>) -> Result<()> {
        Ok(())
    }
}

const fn default_true() -> bool {
    true
}

fn is_valid_plugin_id(value: &str) -> bool {
    let mut chars = value.chars();
    let Some(first) = chars.next() else {
        return false;
    };

    if !first.is_ascii_lowercase() {
        return false;
    }

    chars.all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || matches!(ch, '-' | '_' | '.'))
}

fn default_dependency_version_req() -> String {
    "*".to_string()
}

#[cfg(test)]
mod tests {
    use super::{PluginDeclaration, PluginDependency, PluginEntrypoint, PluginId};
    use bmux_plugin_sdk::{
        ApiVersion, CommandExecutionKind, HostScope, PluginCommand, VersionRange,
    };
    use std::collections::BTreeSet;

    #[test]
    fn plugin_id_requires_stable_ascii_format() {
        assert!(PluginId::new("git.status").is_ok());
        assert!(PluginId::new("GitStatus").is_err());
    }

    #[test]
    fn validate_rejects_duplicate_commands() {
        let declaration = PluginDeclaration {
            id: PluginId::new("example.plugin").expect("id should parse"),
            display_name: "Example".to_string(),
            plugin_version: "0.1.0".to_string(),
            plugin_api: VersionRange::at_least(ApiVersion::new(1, 0)),
            native_abi: VersionRange::at_least(ApiVersion::new(1, 0)),
            entrypoint: PluginEntrypoint::Native {
                symbol: "bmux_plugin_entry_v1".to_string(),
            },
            description: None,
            homepage: None,
            provider_priority: 0,
            execution_class: super::PluginExecutionClass::NativeStandard,
            owns_namespaces: BTreeSet::new(),
            owns_paths: BTreeSet::new(),
            required_capabilities: BTreeSet::new(),
            provided_capabilities: BTreeSet::new(),
            provided_features: BTreeSet::new(),
            services: Vec::new(),
            commands: vec![
                PluginCommand {
                    name: "run".to_string(),
                    path: Vec::new(),
                    aliases: Vec::new(),
                    summary: "run".to_string(),
                    description: None,
                    arguments: Vec::new(),
                    execution: CommandExecutionKind::ProviderExec,
                    expose_in_cli: true,
                    accepts_repeat: false,
                },
                PluginCommand {
                    name: "run".to_string(),
                    path: Vec::new(),
                    aliases: Vec::new(),
                    summary: "again".to_string(),
                    description: None,
                    arguments: Vec::new(),
                    execution: CommandExecutionKind::ProviderExec,
                    expose_in_cli: true,
                    accepts_repeat: false,
                },
            ],
            event_subscriptions: Vec::new(),
            event_publications: Vec::new(),
            dependencies: Vec::new(),
            lifecycle: super::PluginLifecycle::default(),
            ready_signals: Vec::new(),
        };

        assert!(declaration.validate().is_err());
    }

    #[test]
    fn validate_allows_runtime_hook_when_hot_path_scope_exists() {
        let mut required_capabilities = BTreeSet::new();
        required_capabilities
            .insert(HostScope::new("bmux.terminal.input_intercept").expect("scope should parse"));

        let declaration = PluginDeclaration {
            id: PluginId::new("example.runtime").expect("id should parse"),
            display_name: "Runtime".to_string(),
            plugin_version: "0.1.0".to_string(),
            plugin_api: VersionRange::at_least(ApiVersion::new(1, 0)),
            native_abi: VersionRange::at_least(ApiVersion::new(1, 0)),
            entrypoint: PluginEntrypoint::Native {
                symbol: "bmux_plugin_entry_v1".to_string(),
            },
            description: None,
            homepage: None,
            provider_priority: 0,
            execution_class: super::PluginExecutionClass::NativeStandard,
            owns_namespaces: BTreeSet::new(),
            owns_paths: BTreeSet::new(),
            required_capabilities,
            provided_capabilities: BTreeSet::new(),
            provided_features: BTreeSet::new(),
            services: Vec::new(),
            commands: vec![PluginCommand {
                name: "runtime".to_string(),
                path: Vec::new(),
                aliases: Vec::new(),
                summary: "runtime".to_string(),
                description: None,
                arguments: Vec::new(),
                execution: CommandExecutionKind::RuntimeHook,
                expose_in_cli: true,
                accepts_repeat: false,
            }],
            event_subscriptions: Vec::new(),
            event_publications: Vec::new(),
            dependencies: Vec::new(),
            lifecycle: super::PluginLifecycle::default(),
            ready_signals: Vec::new(),
        };

        assert!(declaration.validate().is_ok());
    }

    #[test]
    fn validate_rejects_duplicate_dependencies() {
        let declaration = PluginDeclaration {
            id: PluginId::new("example.plugin").expect("id should parse"),
            display_name: "Example".to_string(),
            plugin_version: "0.1.0".to_string(),
            plugin_api: VersionRange::at_least(ApiVersion::new(1, 0)),
            native_abi: VersionRange::at_least(ApiVersion::new(1, 0)),
            entrypoint: PluginEntrypoint::Native {
                symbol: "bmux_plugin_entry_v1".to_string(),
            },
            description: None,
            homepage: None,
            provider_priority: 0,
            execution_class: super::PluginExecutionClass::NativeStandard,
            owns_namespaces: BTreeSet::new(),
            owns_paths: BTreeSet::new(),
            required_capabilities: BTreeSet::new(),
            provided_capabilities: BTreeSet::new(),
            provided_features: BTreeSet::new(),
            services: Vec::new(),
            commands: Vec::new(),
            event_subscriptions: Vec::new(),
            event_publications: Vec::new(),
            dependencies: vec![
                PluginDependency {
                    plugin_id: PluginId::new("bmux.sessions").expect("dep id should parse"),
                    version_req: "^0.1".to_string(),
                    required: true,
                },
                PluginDependency {
                    plugin_id: PluginId::new("bmux.sessions").expect("dep id should parse"),
                    version_req: "^0.1".to_string(),
                    required: true,
                },
            ],
            lifecycle: super::PluginLifecycle::default(),
            ready_signals: Vec::new(),
        };

        assert!(declaration.validate().is_err());
    }
}