| 1 | //! Normalized marketplace catalog model. |
| 2 | //! |
| 3 | //! Every parser (`kimi`, `claude`, `codex`, `codewhale`) funnels foreign |
| 4 | //! catalog entries into [`MarketplaceCandidate`]. Parsers are pure: no |
| 5 | //! network, no filesystem, no process execution. Sources are normalized |
| 6 | //! and mapped onto an install plan, but nothing is fetched here. |
| 7 | //! |
| 8 | //! # Security invariants |
| 9 | //! |
| 10 | //! - Catalog labels (`official`, `curated`, `verified`, `partner`) are |
| 11 | //! display provenance only. They never grant trust, enablement, |
| 12 | //! installation, or any runtime permission. Every installed plugin still |
| 13 | //! enters Codewhale disabled and untrusted and goes through the existing |
| 14 | //! content/capability hash review. |
| 15 | //! - Foreign `policy` blocks (Codex `INSTALLED_BY_DEFAULT`) never trigger |
| 16 | //! auto-install; Codewhale only installs on an explicit operator action. |
| 17 | //! - A malformed entry degrades that entry alone; it never hides the rest |
| 18 | //! of the catalog. |
| 19 | |
| 20 | use std::fmt; |
| 21 | use std::path::PathBuf; |
| 22 | |
| 23 | use serde::{Deserialize, Serialize}; |
| 24 | |
| 25 | use super::super::manifest::{PluginCompatibility, PluginInventory, PluginWhen}; |
| 26 | use super::super::types::PluginDiagnosticLevel; |
| 27 | use codewhale_config::PluginId; |
| 28 | |
| 29 | /// Which real catalog format a document follows. |
| 30 | /// |
| 31 | /// Every variant corresponds to a published schema; there is no |
| 32 | /// synthetic or guessed format. `Auto` detection only uses documented |
| 33 | /// structural markers and reports ambiguity instead of guessing. |
| 34 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] |
| 35 | #[serde(rename_all = "snake_case")] |
| 36 | pub enum MarketplaceFormat { |
| 37 | /// Detect from documented structural markers; ambiguous documents fail. |
| 38 | #[default] |
| 39 | Auto, |
| 40 | /// Codewhale native catalog: `plugins[]` with `name` + install-spec |
| 41 | /// `source` strings. |
| 42 | Codewhale, |
| 43 | /// Kimi / Moonshot `marketplace.json`: `version` + `plugins[]` with |
| 44 | /// `id`, `displayName`, `tier`, `source` (path / GitHub URL / zip URL). |
| 45 | Kimi, |
| 46 | /// Claude `.claude-plugin/marketplace.json`: `name` + `owner` + |
| 47 | /// `plugins[]`; `source` is a `./`-relative string or a |
| 48 | /// `{source: github|url|git-subdir|npm|archive|command}` object. |
| 49 | Claude, |
| 50 | /// Codex `.agents/plugins/marketplace.json`: `name` + `plugins[]` with |
| 51 | /// `source` objects (`local`/`url`/`git-subdir`/`npm`) and `policy`. |
| 52 | Codex, |
| 53 | /// DeepSeek Harness plugin market index, format 1: `format: 1` + `title` |
| 54 | /// + `plugins[]` with npm-valid `name`, localized display text, and |
| 55 | /// `versions[]` carrying exact `source` (`npm`/`git`) and `engines.dsh`. |
| 56 | Dsh, |
| 57 | } |
| 58 | |
| 59 | impl MarketplaceFormat { |
| 60 | #[must_use] |
| 61 | pub fn as_str(self) -> &'static str { |
| 62 | match self { |
| 63 | Self::Auto => "auto", |
| 64 | Self::Codewhale => "codewhale", |
| 65 | Self::Kimi => "kimi", |
| 66 | Self::Claude => "claude", |
| 67 | Self::Codex => "codex", |
| 68 | Self::Dsh => "dsh", |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl fmt::Display for MarketplaceFormat { |
| 74 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 75 | f.write_str(self.as_str()) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Stable identifier for a marketplace catalog (typically its configured |
| 80 | /// name or path alias). |
| 81 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 82 | #[serde(transparent)] |
| 83 | pub struct MarketplaceCatalogId(pub String); |
| 84 | |
| 85 | impl MarketplaceCatalogId { |
| 86 | #[must_use] |
| 87 | pub fn new(id: impl Into<String>) -> Self { |
| 88 | Self(id.into()) |
| 89 | } |
| 90 | |
| 91 | #[must_use] |
| 92 | pub fn as_str(&self) -> &str { |
| 93 | &self.0 |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl fmt::Display for MarketplaceCatalogId { |
| 98 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 99 | f.write_str(&self.0) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Stable, deterministic identifier for one catalog entry: |
| 104 | /// `<catalog_id>:<canonical plugin name>`. |
| 105 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 106 | #[serde(transparent)] |
| 107 | pub struct MarketplaceCandidateId(pub String); |
| 108 | |
| 109 | impl MarketplaceCandidateId { |
| 110 | #[must_use] |
| 111 | pub fn new(catalog_id: &MarketplaceCatalogId, candidate_name: &str) -> Self { |
| 112 | Self(format!("{}:{candidate_name}", catalog_id.as_str())) |
| 113 | } |
| 114 | |
| 115 | #[must_use] |
| 116 | pub fn from_raw(raw: impl Into<String>) -> Self { |
| 117 | Self(raw.into()) |
| 118 | } |
| 119 | |
| 120 | #[must_use] |
| 121 | pub fn as_str(&self) -> &str { |
| 122 | &self.0 |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl fmt::Display for MarketplaceCandidateId { |
| 127 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 128 | f.write_str(&self.0) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// Advisory curation tier carried from the catalog. Display only; never |
| 133 | /// grants trust (see module invariants). |
| 134 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] |
| 135 | #[serde(rename_all = "snake_case")] |
| 136 | pub enum CatalogTier { |
| 137 | #[default] |
| 138 | Community, |
| 139 | Official, |
| 140 | Curated, |
| 141 | Partner, |
| 142 | } |
| 143 | |
| 144 | impl CatalogTier { |
| 145 | #[must_use] |
| 146 | pub fn as_str(&self) -> &'static str { |
| 147 | match self { |
| 148 | Self::Community => "community", |
| 149 | Self::Official => "official", |
| 150 | Self::Curated => "curated", |
| 151 | Self::Partner => "partner", |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /// Parse a catalog-declared tier string. Unknown strings stay |
| 156 | /// community-tier with the raw value preserved by the caller's |
| 157 | /// diagnostic; tiers are display-only either way. |
| 158 | #[must_use] |
| 159 | pub fn parse(raw: &str) -> Self { |
| 160 | match raw { |
| 161 | "official" => Self::Official, |
| 162 | "curated" => Self::Curated, |
| 163 | "partner" => Self::Partner, |
| 164 | _ => Self::Community, |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | impl fmt::Display for CatalogTier { |
| 170 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 171 | f.write_str(self.as_str()) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// Display-only provenance for a catalog or candidate. |
| 176 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] |
| 177 | pub struct CatalogProvenance { |
| 178 | #[serde(default)] |
| 179 | pub tier: CatalogTier, |
| 180 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 181 | pub publisher: Option<String>, |
| 182 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 183 | pub source_url: Option<String>, |
| 184 | } |
| 185 | |
| 186 | impl CatalogProvenance { |
| 187 | /// Provenance never grants trust. Constant by construction; exists so |
| 188 | /// tests and reviewers can assert the invariant at call sites. |
| 189 | #[must_use] |
| 190 | pub fn grants_trust(&self) -> bool { |
| 191 | false |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /// Normalized description of where a plugin bundle lives. Pure data: the |
| 196 | /// parser never fetches any of these. |
| 197 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 198 | #[serde(tag = "type", rename_all = "snake_case")] |
| 199 | pub enum MarketplaceSourceSpec { |
| 200 | /// Directory path. May be relative to the catalog's own location; |
| 201 | /// resolution happens at install time, not parse time. |
| 202 | LocalPath { path: PathBuf }, |
| 203 | /// GitHub `owner/repo`, optionally with a pinned ref or commit. |
| 204 | GitHub { |
| 205 | owner: String, |
| 206 | repo: String, |
| 207 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 208 | git_ref: Option<String>, |
| 209 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 210 | sha: Option<String>, |
| 211 | }, |
| 212 | /// Tarball archive URL with an optional sha256 pin, verified at install. |
| 213 | ArchiveUrl { |
| 214 | url: String, |
| 215 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 216 | sha256: Option<String>, |
| 217 | }, |
| 218 | /// Non-GitHub git URL. The installer cannot fetch these yet; the plan |
| 219 | /// stays [`MarketplaceInstallPlan::Unsupported`]. |
| 220 | GitUrl { url: String }, |
| 221 | /// npm package declaration. Codewhale never executes npm: a DSH market |
| 222 | /// entry plans a fetch of its exact version's registry tarball, and the |
| 223 | /// unversioned Claude and Codex declarations stay unsupported. |
| 224 | Npm { package: String }, |
| 225 | /// A source form the catalog format defines but Codewhale refuses to |
| 226 | /// execute (e.g. Claude `command` sources), with the reason. |
| 227 | Refused { reason: String }, |
| 228 | /// Required source field present but not a documented shape for the |
| 229 | /// format; entry degrades with an error diagnostic. |
| 230 | Invalid { reason: String }, |
| 231 | } |
| 232 | |
| 233 | /// Whether and how Codewhale's existing installer could fetch a source. |
| 234 | /// Unsupported plans are visible, with an honest reason — a listed plugin |
| 235 | /// is never implied to be installable. |
| 236 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 237 | #[serde(tag = "status", rename_all = "snake_case")] |
| 238 | pub enum MarketplaceInstallPlan { |
| 239 | /// Maps onto a real [`PluginInstallSource`](crate::install::PluginInstallSource) spec. |
| 240 | Supported { spec: String, source_kind: String }, |
| 241 | /// Cannot be installed by Codewhale today, and why. |
| 242 | Unsupported { reason: String, raw: String }, |
| 243 | } |
| 244 | |
| 245 | impl MarketplaceInstallPlan { |
| 246 | #[must_use] |
| 247 | pub fn is_supported(&self) -> bool { |
| 248 | matches!(self, Self::Supported { .. }) |
| 249 | } |
| 250 | |
| 251 | #[must_use] |
| 252 | pub fn reason(&self) -> Option<&str> { |
| 253 | match self { |
| 254 | Self::Supported { .. } => None, |
| 255 | Self::Unsupported { reason, .. } => Some(reason.as_str()), |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /// What a catalog entry is. |
| 261 | /// |
| 262 | /// A marketplace carries more than plugins: the Codewhale marketplace lists |
| 263 | /// standalone skills as their own entries, and imported third-party catalogs |
| 264 | /// carry whatever their format allows. The kind is how a surface knows which |
| 265 | /// pool an entry belongs to — in particular, only `Plugin` entries are plugin |
| 266 | /// suggestions. (grokbuild's `MarketplaceEntry` carries the same distinction |
| 267 | /// as inventory flags — `skill_count`, `has_mcp`, … — on an always-a-plugin |
| 268 | /// entry; we keep the kind explicit because our catalogs list skills |
| 269 | /// directly.) |
| 270 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] |
| 271 | #[serde(rename_all = "snake_case")] |
| 272 | pub enum MarketplaceEntryKind { |
| 273 | #[default] |
| 274 | Plugin, |
| 275 | /// A standalone skill (instruction pack). Installable, but never |
| 276 | /// suggested as a plugin. |
| 277 | Skill, |
| 278 | } |
| 279 | |
| 280 | /// A normalized catalog entry. Catalog-declared component lists are kept |
| 281 | /// for display; the reviewed staged-tree manifest at install time remains |
| 282 | /// the only authority on what a bundle actually contains. |
| 283 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 284 | pub struct MarketplaceCandidate { |
| 285 | pub id: MarketplaceCandidateId, |
| 286 | pub catalog_id: MarketplaceCatalogId, |
| 287 | /// What this entry is. Defaults to `Plugin` so stored snapshots and |
| 288 | /// formats that do not declare a kind keep their previous meaning. |
| 289 | #[serde(default)] |
| 290 | pub kind: MarketplaceEntryKind, |
| 291 | /// Canonical (Agent Plugins standard) plugin name. |
| 292 | pub name: String, |
| 293 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 294 | pub display_name: Option<String>, |
| 295 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 296 | pub icon: Option<String>, |
| 297 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 298 | pub description: Option<String>, |
| 299 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 300 | pub version: Option<String>, |
| 301 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 302 | pub author: Option<String>, |
| 303 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 304 | pub homepage: Option<String>, |
| 305 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 306 | pub repository: Option<String>, |
| 307 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 308 | pub license: Option<String>, |
| 309 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 310 | pub keywords: Vec<String>, |
| 311 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 312 | pub categories: Vec<String>, |
| 313 | pub source: MarketplaceSourceSpec, |
| 314 | pub install_plan: MarketplaceInstallPlan, |
| 315 | /// Components the catalog entry itself declares. Many formats declare |
| 316 | /// none; `None` compatibility then means "decided at install review". |
| 317 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 318 | pub declared_components: Option<PluginInventory>, |
| 319 | /// Compatibility of declared components with this build's activation |
| 320 | /// policy. `None` when the format does not declare components. |
| 321 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 322 | pub compatibility: Option<PluginCompatibility>, |
| 323 | pub provenance: CatalogProvenance, |
| 324 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 325 | pub when: Option<PluginWhen>, |
| 326 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 327 | pub diagnostics: Vec<MarketplaceDiagnostic>, |
| 328 | } |
| 329 | |
| 330 | impl MarketplaceCandidate { |
| 331 | #[must_use] |
| 332 | pub fn plugin_id(&self) -> PluginId { |
| 333 | PluginId(self.name.clone()) |
| 334 | } |
| 335 | |
| 336 | #[must_use] |
| 337 | pub fn has_errors(&self) -> bool { |
| 338 | self.diagnostics |
| 339 | .iter() |
| 340 | .any(|d| d.level == PluginDiagnosticLevel::Error) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// A parsed catalog: survivors plus diagnostics. Fault isolation is |
| 345 | /// per-entry: one malformed entry never removes the others. |
| 346 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 347 | pub struct MarketplaceCatalog { |
| 348 | pub id: MarketplaceCatalogId, |
| 349 | pub format: MarketplaceFormat, |
| 350 | pub name: String, |
| 351 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 352 | pub display_name: Option<String>, |
| 353 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 354 | pub description: Option<String>, |
| 355 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 356 | pub version: Option<String>, |
| 357 | /// Resolution context: where the catalog document itself was read |
| 358 | /// from, so relative sources can be resolved at install time. |
| 359 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 360 | pub base: Option<String>, |
| 361 | pub provenance: CatalogProvenance, |
| 362 | pub candidates: Vec<MarketplaceCandidate>, |
| 363 | pub diagnostics: Vec<MarketplaceDiagnostic>, |
| 364 | } |
| 365 | |
| 366 | impl MarketplaceCatalog { |
| 367 | #[must_use] |
| 368 | pub fn candidate_by_name(&self, name: &str) -> Option<&MarketplaceCandidate> { |
| 369 | self.candidates.iter().find(|c| c.name == name) |
| 370 | } |
| 371 | |
| 372 | #[must_use] |
| 373 | pub fn total_candidates(&self) -> usize { |
| 374 | self.candidates.len() |
| 375 | } |
| 376 | |
| 377 | #[must_use] |
| 378 | pub fn error_count(&self) -> usize { |
| 379 | self.diagnostics |
| 380 | .iter() |
| 381 | .filter(|d| d.level == PluginDiagnosticLevel::Error) |
| 382 | .count() |
| 383 | } |
| 384 | |
| 385 | #[must_use] |
| 386 | pub fn warning_count(&self) -> usize { |
| 387 | self.diagnostics |
| 388 | .iter() |
| 389 | .filter(|d| d.level == PluginDiagnosticLevel::Warning) |
| 390 | .count() |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /// A diagnostic emitted while parsing a catalog or entry. |
| 395 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 396 | pub struct MarketplaceDiagnostic { |
| 397 | pub level: PluginDiagnosticLevel, |
| 398 | pub code: String, |
| 399 | pub message: String, |
| 400 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 401 | pub candidate_name: Option<String>, |
| 402 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 403 | pub entry_index: Option<usize>, |
| 404 | } |
| 405 | |
| 406 | impl MarketplaceDiagnostic { |
| 407 | #[must_use] |
| 408 | pub fn warning( |
| 409 | code: impl Into<String>, |
| 410 | message: impl Into<String>, |
| 411 | candidate_name: Option<String>, |
| 412 | entry_index: Option<usize>, |
| 413 | ) -> Self { |
| 414 | Self { |
| 415 | level: PluginDiagnosticLevel::Warning, |
| 416 | code: code.into(), |
| 417 | message: message.into(), |
| 418 | candidate_name, |
| 419 | entry_index, |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | #[must_use] |
| 424 | pub fn error( |
| 425 | code: impl Into<String>, |
| 426 | message: impl Into<String>, |
| 427 | candidate_name: Option<String>, |
| 428 | entry_index: Option<usize>, |
| 429 | ) -> Self { |
| 430 | Self { |
| 431 | level: PluginDiagnosticLevel::Error, |
| 432 | code: code.into(), |
| 433 | message: message.into(), |
| 434 | candidate_name, |
| 435 | entry_index, |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 |