caliban-plugins 0.2.0

Plugin packaging orchestrator (ADR 0030) — skill / hook / agent / MCP / output-style bundles — internal crate for the caliban binary; no API stability, pin exact versions
Documentation
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
//! `plugin.json` manifest schema + parser.
//!
//! See `docs/superpowers/specs/2026-05-24-plugin-system-design.md` for the
//! authoritative spec.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::error::PluginError;

/// Top-level `plugin.json` shape. Unknown keys are preserved in `extra`
/// to leave room for forward-compatible additions.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PluginManifest {
    /// Plugin name. Must match the parent directory. `^[a-z0-9_-]{1,32}$`.
    pub name: String,
    /// Semver string. Validated by [`PluginManifest::validate`].
    pub version: String,
    /// One-line description; surfaced in `/plugins` and trust prompts.
    #[serde(default)]
    pub description: String,
    /// Free-form author tag.
    #[serde(default)]
    pub author: String,
    /// SPDX license id.
    #[serde(default)]
    pub license: String,
    /// Optional homepage URL.
    #[serde(default)]
    pub homepage: Option<String>,
    /// Component-paths map (skills, hooks, agents, `output_styles`, `mcp_servers`, commands).
    #[serde(default)]
    pub components: ComponentSpec,
    /// Inline MCP server configurations. Mutually exclusive with
    /// `components.mcp_servers`; inline wins when both are present.
    #[serde(default, rename = "mcpServers")]
    pub mcp_servers_inline: BTreeMap<String, InlineMcpServer>,
    /// Optional caliban-specific gating.
    #[serde(default)]
    pub caliban: CalibanRequirements,
    /// Unknown manifest keys (forward-compat).
    #[serde(flatten)]
    pub extra: BTreeMap<String, serde_json::Value>,
}

/// Component paths relative to the plugin root. Each value is *either* a
/// string (one path) or an array (multiple). Missing values default to
/// "discover everything in the conventional subdirectory" — see the spec
/// for which fields auto-discover.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct ComponentSpec {
    /// Skill subdirectories. Each entry should be a directory containing
    /// `SKILL.md`. When unset, the loader scans `skills/*/SKILL.md`.
    pub skills: Option<PathList>,
    /// Hook config file (defaults to `hooks/hooks.json`).
    pub hooks: Option<PathList>,
    /// Sub-agent `.md` files (defaults to `agents/*.md`).
    pub agents: Option<PathList>,
    /// Output style `.md` files (defaults to `output-styles/*.md`).
    pub output_styles: Option<PathList>,
    /// MCP server config file (defaults to `mcp/.mcp.json`).
    pub mcp_servers: Option<PathList>,
    /// Optional slash-command markdown files (deferred to ADR 0040).
    pub commands: Option<PathList>,
}

/// A "string-or-list-of-strings" wrapper used by every `components.*` field.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum PathList {
    /// Single path.
    Single(String),
    /// Multiple paths.
    Many(Vec<String>),
}

impl PathList {
    /// Return paths as a Vec.
    #[must_use]
    pub fn as_vec(&self) -> Vec<String> {
        match self {
            Self::Single(s) => vec![s.clone()],
            Self::Many(v) => v.clone(),
        }
    }
}

/// Inline MCP server config block under the top-level `mcpServers` key.
/// Mirrors the JSON shape Claude Code uses; not a full toml-mcp parse.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct InlineMcpServer {
    /// Executable path (after `${CALIBAN_PLUGIN_ROOT}` expansion).
    pub command: String,
    /// CLI args.
    #[serde(default)]
    pub args: Vec<String>,
    /// Env-var overrides.
    #[serde(default)]
    pub env: BTreeMap<String, String>,
    /// Working directory.
    #[serde(default)]
    pub cwd: Option<String>,
    /// Optional transport hint (`stdio` is default).
    #[serde(default)]
    pub transport: Option<String>,
}

/// Caliban-specific requirements / filters.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct CalibanRequirements {
    /// Skip the plugin when the running caliban is older than this semver.
    pub min_version: Option<String>,
    /// Limit plugin to these platforms (`macos`, `linux`, `windows`).
    pub platforms: Option<Vec<String>>,
}

impl PluginManifest {
    /// Parse a manifest from raw JSON bytes.
    ///
    /// # Errors
    ///
    /// Returns [`PluginError::Parse`] on JSON syntax errors and
    /// [`PluginError::Invalid`] on validation failures.
    pub fn from_json(raw: &str, path: &Path) -> Result<Self, PluginError> {
        let mf: Self = serde_json::from_str(raw).map_err(|source| PluginError::Parse {
            path: path.to_path_buf(),
            source,
        })?;
        mf.validate(path)?;
        Ok(mf)
    }

    /// Read and parse a manifest from disk.
    ///
    /// # Errors
    ///
    /// Returns [`PluginError::Io`] on read failures, plus the errors
    /// surfaced by [`PluginManifest::from_json`].
    pub fn from_path(path: &Path) -> Result<Self, PluginError> {
        let raw = std::fs::read_to_string(path).map_err(|source| PluginError::Io {
            path: path.to_path_buf(),
            source,
        })?;
        Self::from_json(&raw, path)
    }

    /// Validate the manifest in isolation (no on-disk component checks).
    ///
    /// Performs name-regex, semver, and `caliban.min_version`/`platforms`
    /// shape checks. Does *not* check that `name` matches the parent dir
    /// — that's [`PluginManifest::check_name_matches_dir`].
    ///
    /// # Errors
    ///
    /// Returns [`PluginError::Invalid`] when any check fails.
    pub fn validate(&self, path: &Path) -> Result<(), PluginError> {
        if !is_valid_name(&self.name) {
            return Err(PluginError::Invalid {
                path: path.to_path_buf(),
                message: format!(
                    "invalid name '{}': must match [a-z0-9_-]{{1,32}} and be lowercase",
                    self.name
                ),
            });
        }
        // Version itself must parse as semver.
        semver::Version::parse(&self.version).map_err(|e| PluginError::Invalid {
            path: path.to_path_buf(),
            message: format!("invalid version '{}': {e}", self.version),
        })?;
        if let Some(min) = self.caliban.min_version.as_deref() {
            // Accept partial versions like "0.5" by widening to semver-req.
            semver::VersionReq::parse(&format!(">={min}")).map_err(|e| PluginError::Invalid {
                path: path.to_path_buf(),
                message: format!("invalid caliban.min_version '{min}': {e}"),
            })?;
        }
        if let Some(ps) = self.caliban.platforms.as_ref() {
            for p in ps {
                if !matches!(p.as_str(), "macos" | "linux" | "windows") {
                    return Err(PluginError::Invalid {
                        path: path.to_path_buf(),
                        message: format!(
                            "invalid caliban.platforms entry '{p}': must be macos|linux|windows"
                        ),
                    });
                }
            }
        }
        Ok(())
    }

    /// Confirm the on-disk parent directory's name matches `self.name`.
    ///
    /// # Errors
    ///
    /// Returns [`PluginError::NameMismatch`] when the parent dir name and
    /// manifest name disagree.
    pub fn check_name_matches_dir(&self, manifest_path: &Path) -> Result<(), PluginError> {
        let dir_name = manifest_path
            .parent()
            .and_then(Path::file_name)
            .and_then(|s| s.to_str())
            .unwrap_or_default()
            .to_string();
        if dir_name == self.name {
            Ok(())
        } else {
            Err(PluginError::NameMismatch {
                manifest_name: self.name.clone(),
                dir_name,
                path: manifest_path.to_path_buf(),
            })
        }
    }

    /// Return true if the manifest applies to the running platform.
    #[must_use]
    pub fn platform_matches(&self) -> bool {
        let Some(allowed) = self.caliban.platforms.as_ref() else {
            return true;
        };
        allowed.iter().any(|p| p == current_platform())
    }

    /// Resolve `components.*` entries to absolute paths under `root`.
    /// Missing files are *not* an error here — the downstream loader
    /// decides whether to warn or skip.
    #[must_use]
    pub fn resolved_components(&self, root: &Path) -> ResolvedComponents {
        let resolve_list = |pl: &Option<PathList>| -> Vec<PathBuf> {
            pl.as_ref()
                .map(PathList::as_vec)
                .unwrap_or_default()
                .into_iter()
                .map(|s| root.join(s))
                .collect()
        };
        ResolvedComponents {
            skills: resolve_list(&self.components.skills),
            hooks: resolve_list(&self.components.hooks),
            agents: resolve_list(&self.components.agents),
            output_styles: resolve_list(&self.components.output_styles),
            mcp_servers: resolve_list(&self.components.mcp_servers),
            commands: resolve_list(&self.components.commands),
        }
    }
}

/// Resolved component paths (all absolute under the plugin root).
#[derive(Debug, Clone, Default)]
pub struct ResolvedComponents {
    /// Skill directories (each contains `SKILL.md`).
    pub skills: Vec<PathBuf>,
    /// Hook config files.
    pub hooks: Vec<PathBuf>,
    /// Sub-agent `.md` files.
    pub agents: Vec<PathBuf>,
    /// Output-style `.md` files.
    pub output_styles: Vec<PathBuf>,
    /// MCP server config files.
    pub mcp_servers: Vec<PathBuf>,
    /// Slash command files.
    pub commands: Vec<PathBuf>,
}

/// Plugin name regex check (`^[a-z0-9_-]{1,32}$`).
#[must_use]
pub fn is_valid_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 32
        && name
            .chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '-')
}

/// Return the static platform string used by `caliban.platforms`.
#[must_use]
pub fn current_platform() -> &'static str {
    #[cfg(target_os = "macos")]
    {
        "macos"
    }
    #[cfg(target_os = "linux")]
    {
        "linux"
    }
    #[cfg(target_os = "windows")]
    {
        "windows"
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
    {
        "unknown"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn parses_minimal_manifest() {
        let raw = r#"{ "name": "demo", "version": "0.1.0", "description": "demo plugin" }"#;
        let mf = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap();
        assert_eq!(mf.name, "demo");
        assert_eq!(mf.version, "0.1.0");
        assert_eq!(mf.description, "demo plugin");
        assert!(mf.components.skills.is_none());
    }

    #[test]
    fn parses_full_manifest() {
        let raw = r#"{
            "name": "superpowers",
            "version": "1.4.2",
            "description": "Curated skills",
            "author": "alice <alice@example.com>",
            "license": "MIT",
            "homepage": "https://example.com",
            "components": {
                "skills": ["skills/foo", "skills/bar"],
                "hooks": "hooks/hooks.json",
                "agents": ["agents/reviewer.md"],
                "output_styles": "output-styles/learning.md",
                "mcp_servers": "mcp/.mcp.json",
                "commands": ["commands/recap.md"]
            },
            "mcpServers": {
                "fixtures": {
                    "command": "${CALIBAN_PLUGIN_ROOT}/bin/server",
                    "args": ["--verbose"]
                }
            },
            "caliban": { "min_version": "0.5.0", "platforms": ["macos", "linux"] }
        }"#;
        let mf = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap();
        assert_eq!(mf.author, "alice <alice@example.com>");
        let skills = mf.components.skills.as_ref().unwrap().as_vec();
        assert_eq!(skills, vec!["skills/foo".to_string(), "skills/bar".into()]);
        let agents = mf.components.agents.as_ref().unwrap().as_vec();
        assert_eq!(agents, vec!["agents/reviewer.md".to_string()]);
        let hooks = mf.components.hooks.as_ref().unwrap().as_vec();
        assert_eq!(hooks, vec!["hooks/hooks.json".to_string()]);
        assert_eq!(mf.mcp_servers_inline.len(), 1);
        assert_eq!(mf.caliban.platforms.as_ref().unwrap().len(), 2);
    }

    #[test]
    fn invalid_json_is_parse_error() {
        let raw = r"not json at all";
        let err = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap_err();
        assert!(matches!(err, PluginError::Parse { .. }));
    }

    #[test]
    fn name_regex_enforced() {
        assert!(is_valid_name("demo"));
        assert!(is_valid_name("demo-1_x"));
        assert!(!is_valid_name(""));
        assert!(!is_valid_name("UPPER"));
        assert!(!is_valid_name("with space"));
        assert!(!is_valid_name(&"x".repeat(33)));
        assert!(!is_valid_name("dot.name"));
    }

    #[test]
    fn invalid_name_rejected_in_manifest() {
        let raw = r#"{ "name": "BAD", "version": "0.1.0" }"#;
        let err = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap_err();
        assert!(matches!(err, PluginError::Invalid { .. }));
    }

    #[test]
    fn invalid_semver_rejected() {
        let raw = r#"{ "name": "demo", "version": "not.a.version" }"#;
        let err = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap_err();
        assert!(matches!(err, PluginError::Invalid { .. }));
    }

    #[test]
    fn unknown_platform_rejected() {
        let raw = r#"{
            "name": "demo", "version": "0.1.0",
            "caliban": { "platforms": ["beos"] }
        }"#;
        let err = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap_err();
        assert!(matches!(err, PluginError::Invalid { .. }));
    }

    #[test]
    fn check_name_matches_dir_ok() {
        let raw = r#"{ "name": "demo", "version": "0.1.0" }"#;
        let mf = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap();
        let tmp = tempfile::TempDir::new().unwrap();
        let plug_dir = tmp.path().join("demo");
        std::fs::create_dir_all(&plug_dir).unwrap();
        let manifest_path = plug_dir.join("plugin.json");
        std::fs::write(&manifest_path, raw).unwrap();
        mf.check_name_matches_dir(&manifest_path).unwrap();
    }

    #[test]
    fn check_name_mismatch_errors() {
        let raw = r#"{ "name": "demo", "version": "0.1.0" }"#;
        let mf = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap();
        let tmp = tempfile::TempDir::new().unwrap();
        let plug_dir = tmp.path().join("wrong");
        std::fs::create_dir_all(&plug_dir).unwrap();
        let manifest_path = plug_dir.join("plugin.json");
        std::fs::write(&manifest_path, raw).unwrap();
        let err = mf.check_name_matches_dir(&manifest_path).unwrap_err();
        assert!(matches!(err, PluginError::NameMismatch { .. }));
    }

    #[test]
    fn unknown_fields_preserved() {
        let raw = r#"{
            "name": "demo", "version": "0.1.0",
            "future_field": { "anything": [1, 2, 3] }
        }"#;
        let mf = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap();
        assert!(mf.extra.contains_key("future_field"));
    }

    #[test]
    fn resolves_components_to_absolute_paths() {
        let raw = r#"{
            "name": "demo", "version": "0.1.0",
            "components": { "skills": ["skills/a", "skills/b"] }
        }"#;
        let mf = PluginManifest::from_json(raw, Path::new("plugin.json")).unwrap();
        let root = Path::new("/plugins/demo");
        let rc = mf.resolved_components(root);
        assert_eq!(rc.skills.len(), 2);
        assert_eq!(rc.skills[0], root.join("skills/a"));
        assert_eq!(rc.skills[1], root.join("skills/b"));
    }

    #[test]
    fn platform_matches_filters() {
        let raw_other = r#"{
            "name": "demo", "version": "0.1.0",
            "caliban": { "platforms": ["windows"] }
        }"#;
        let mf = PluginManifest::from_json(raw_other, Path::new("plugin.json")).unwrap();
        #[cfg(not(target_os = "windows"))]
        assert!(!mf.platform_matches());
        let raw_unset = r#"{ "name": "demo", "version": "0.1.0" }"#;
        let mf2 = PluginManifest::from_json(raw_unset, Path::new("plugin.json")).unwrap();
        assert!(mf2.platform_matches());
    }
}