a3s-use-core 0.2.8

Shared typed contracts for A3S Use domains
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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use std::collections::{BTreeMap, BTreeSet};

use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};

use crate::{OkfBundleContract, UseResult};

use super::validation::{
    strictly_sorted_unique, valid_catalog_text, valid_package_id, valid_repository_url,
    valid_sha256, valid_spdx_expression, valid_tag, valid_target, valid_target_name,
};
use super::{
    canonical_digest, canonical_json, contract_error, parse_contract, PluginPackageDependency,
    PluginPermissionCeiling, PluginSurfaceKind, PluginSurfaceRef, ToolWorkloadClass,
    PLUGIN_CATALOG_SCHEMA_V3,
};

pub(super) const CATALOG_ERROR: &str = "use.plugin.catalog_invalid";
pub(super) const MAX_CATALOG_SURFACES: usize = 256;
const MAX_REMOTE_ARCHIVE_BYTES: u64 = 512 * 1024 * 1024;
const MAX_PLANNING_TARGET_BYTES: u64 = 512 * 1024;
const MAX_EXPANDED_PACKAGE_BYTES: u64 = 1024 * 1024 * 1024;
const MAX_PACKAGE_FILES: u64 = 10_000;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PluginCatalogRecord {
    pub schema: String,
    pub package_id: String,
    pub display_name: String,
    pub description: String,
    pub publisher: String,
    pub keywords: Vec<String>,
    pub categories: Vec<String>,
    pub version: String,
    pub channel: PluginReleaseChannel,
    pub requires_use: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dependencies: Vec<PluginPackageDependency>,
    pub target: String,
    pub surfaces: Vec<CatalogSurface>,
    pub permission_ceiling: PluginPermissionCeiling,
    pub permission_ceiling_digest: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub planning: Option<CatalogPlanningTarget>,
    pub archive: CatalogArchive,
    pub package: CatalogPackage,
    pub license: String,
    pub repository: String,
    pub availability: CatalogAvailability,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PluginReleaseChannel {
    Beta,
    Nightly,
    Stable,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CatalogSurface {
    pub kind: PluginSurfaceKind,
    pub id: String,
    pub optional: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workload: Option<ToolWorkloadClass>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mcp_transport: Option<CatalogMcpTransport>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mcp_tool_count: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub okf_bundle: Option<OkfBundleContract>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub requires: Vec<PluginSurfaceRef>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum CatalogMcpTransport {
    Stdio,
    StreamableHttp,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CatalogArchive {
    pub target_name: String,
    pub length: u64,
    pub sha256: String,
}

/// Exact small TUF target containing executable-surface planning evidence.
///
/// Keeping this separate from the package archive lets clients review Tool and
/// MCP Runtime plans without downloading or expanding the full plugin.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CatalogPlanningTarget {
    pub target_name: String,
    pub length: u64,
    pub sha256: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CatalogPackage {
    pub expanded_bytes: u64,
    pub file_count: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub manifest_sha256: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
    tag = "state",
    rename_all = "kebab-case",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum CatalogAvailability {
    Available,
    Deprecated {
        message: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        replacement: Option<String>,
    },
    Withdrawn {
        reason: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        advisory_url: Option<String>,
    },
}

impl PluginCatalogRecord {
    pub fn from_json(input: &[u8]) -> UseResult<Self> {
        parse_contract(
            input,
            "plugin catalog record",
            CATALOG_ERROR,
            Self::validate,
        )
    }

    pub fn validate(&self) -> UseResult<()> {
        if self.schema != PLUGIN_CATALOG_SCHEMA_V3
            || !valid_package_id(&self.package_id)
            || !valid_catalog_text(&self.display_name, 128)
            || !valid_catalog_text(&self.description, 2048)
            || !super::validation::valid_segment(&self.publisher)
            || self.package_id.split('/').next() != Some(self.publisher.as_str())
            || self.keywords.len() > 64
            || self.categories.len() > 64
            || !strictly_sorted_unique(&self.keywords)
            || !strictly_sorted_unique(&self.categories)
            || self.keywords.iter().any(|value| !valid_tag(value))
            || self.categories.iter().any(|value| !valid_tag(value))
            || Version::parse(&self.version)
                .map(|version| version.to_string() != self.version)
                .unwrap_or(true)
            || VersionReq::parse(&self.requires_use).is_err()
            || !valid_target(&self.target)
            || self.surfaces.is_empty()
            || self.surfaces.len() > MAX_CATALOG_SURFACES
        {
            return Err(catalog_error(
                "The plugin catalog identity, search metadata, compatibility, or target is invalid.",
            ));
        }
        if PluginPackageDependency::validate_set(&self.package_id, &self.dependencies).is_err() {
            return Err(catalog_error(
                "Package dependencies must be a canonical sorted dependency set.",
            ));
        }

        let mut surface_refs = BTreeSet::new();
        let mut tool_classes = BTreeMap::new();
        let mut mcp_transports = BTreeMap::new();
        let mut previous = None;
        for surface in &self.surfaces {
            surface.validate()?;
            let reference = PluginSurfaceRef {
                kind: surface.kind,
                id: surface.id.clone(),
            };
            if previous.as_ref().is_some_and(|value| value >= &reference)
                || !surface_refs.insert(reference.clone())
            {
                return Err(catalog_error(
                    "Catalog surfaces must be sorted and unique by kind and ID.",
                ));
            }
            previous = Some(reference.clone());
            if let Some(workload) = surface.workload {
                tool_classes.insert(surface.id.as_str(), workload);
            }
            if let Some(transport) = surface.mcp_transport {
                mcp_transports.insert(surface.id.as_str(), transport);
            }
        }
        self.validate_surface_dependencies(&surface_refs)?;

        self.permission_ceiling
            .validate()
            .map_err(|_| catalog_error("The catalog permission ceiling is invalid."))?;
        if self.permission_ceiling.descriptor_digest()? != self.permission_ceiling_digest {
            return Err(catalog_error(
                "The catalog permission ceiling digest does not match its content.",
            ));
        }
        for permission in &self.permission_ceiling.surfaces {
            if !surface_refs.contains(&permission.surface) {
                return Err(catalog_error(
                    "The permission ceiling references a surface absent from the catalog.",
                ));
            }
            for binding in &permission.ui_http {
                if tool_classes.get(binding.tool_id.as_str()) != Some(&ToolWorkloadClass::Service) {
                    return Err(catalog_error(
                        "A UI HTTP permission must bind a cataloged Tool Service.",
                    ));
                }
                let tool = PluginSurfaceRef {
                    kind: PluginSurfaceKind::Tool,
                    id: binding.tool_id.clone(),
                };
                if !self
                    .surfaces
                    .iter()
                    .find(|surface| surface.reference() == permission.surface)
                    .is_some_and(|surface| surface.requires.contains(&tool))
                {
                    return Err(catalog_error(
                        "A complete plugin catalog UI HTTP binding must declare its Tool dependency.",
                    ));
                }
            }
            let resources = permission.resources.as_ref();
            let long_running_resources = resources.is_some_and(|value| {
                value.task_timeout_ms.is_none()
                    && value.max_stdout_bytes.is_none()
                    && value.max_stderr_bytes.is_none()
            });
            match permission.surface.kind {
                PluginSurfaceKind::Tool => match tool_classes.get(permission.surface.id.as_str()) {
                    Some(ToolWorkloadClass::Task)
                        if !permission.private_service
                            && resources.is_some_and(|value| {
                                value.task_timeout_ms.is_some()
                                    && value.max_stdout_bytes.is_some()
                                    && value.max_stderr_bytes.is_some()
                            }) => {}
                    Some(ToolWorkloadClass::Service)
                        if !permission.native_execution
                            && permission.private_service
                            && long_running_resources => {}
                    _ => {
                        return Err(catalog_error(
                                "A Tool permission ceiling does not match its Task or Service workload.",
                            ));
                    }
                },
                PluginSurfaceKind::Mcp => {
                    match mcp_transports.get(permission.surface.id.as_str()) {
                        Some(CatalogMcpTransport::Stdio)
                            if permission.native_execution
                                && !permission.private_service
                                && long_running_resources => {}
                        Some(CatalogMcpTransport::StreamableHttp)
                            if !permission.native_execution
                                && permission.private_service
                                && long_running_resources => {}
                        _ => {
                            return Err(catalog_error(
                                "An MCP permission ceiling does not match its declared transport.",
                            ));
                        }
                    }
                }
                PluginSurfaceKind::Ui => {}
                PluginSurfaceKind::Flow | PluginSurfaceKind::Okf | PluginSurfaceKind::Skill => {
                    return Err(catalog_error(
                        "Flow, OKF, and Skill surfaces cannot carry runtime permission ceilings.",
                    ));
                }
            }
        }
        for surface in &self.surfaces {
            if matches!(
                surface.kind,
                PluginSurfaceKind::Tool | PluginSurfaceKind::Mcp
            ) && !self.permission_ceiling.surfaces.iter().any(|permission| {
                permission.surface.kind == surface.kind && permission.surface.id == surface.id
            }) {
                return Err(catalog_error(
                    "Every executable Tool and MCP surface requires a permission ceiling.",
                ));
            }
        }

        self.archive
            .validate(&self.package_id, &self.version, self.channel, &self.target)?;
        let has_executable = self.surfaces.iter().any(|surface| {
            matches!(
                surface.kind,
                PluginSurfaceKind::Tool | PluginSurfaceKind::Mcp
            )
        });
        match (&self.planning, has_executable) {
            (Some(planning), true) => {
                planning.validate(&self.package_id, &self.version, self.channel, &self.target)?
            }
            (None, false) => {}
            _ => {
                return Err(catalog_error(
                    "Catalog records must carry one planning target exactly when they contain executable surfaces.",
                ))
            }
        }
        self.package.validate()?;
        if !valid_spdx_expression(&self.license) || !valid_repository_url(&self.repository) {
            return Err(catalog_error(
                "The plugin catalog license or repository identity is invalid.",
            ));
        }
        self.availability.validate(&self.package_id)
    }

    pub fn canonical_bytes(&self) -> UseResult<Vec<u8>> {
        self.validate()?;
        canonical_json(self, "plugin catalog record", CATALOG_ERROR)
    }

    pub fn descriptor_digest(&self) -> UseResult<String> {
        Ok(canonical_digest(&self.canonical_bytes()?))
    }

    pub fn is_package_plan_ready(&self) -> bool {
        self.schema == PLUGIN_CATALOG_SCHEMA_V3
    }

    fn validate_surface_dependencies(
        &self,
        surface_refs: &BTreeSet<PluginSurfaceRef>,
    ) -> UseResult<()> {
        super::catalog_selection::validate_surface_dependencies(&self.surfaces, surface_refs)
    }
}

impl CatalogSurface {
    pub(super) fn validate(&self) -> UseResult<()> {
        if !super::validation::valid_segment(&self.id) {
            return Err(catalog_error("A catalog surface ID is invalid."));
        }
        match self.kind {
            PluginSurfaceKind::Tool
                if self.workload.is_some()
                    && self.mcp_transport.is_none()
                    && self.mcp_tool_count.is_none()
                    && self.okf_bundle.is_none() =>
            {
                Ok(())
            }
            PluginSurfaceKind::Mcp
                if self.workload.is_none()
                    && self.mcp_transport.is_some()
                    && self.mcp_tool_count.is_none_or(|count| count <= 10_000)
                    && self.okf_bundle.is_none() =>
            {
                Ok(())
            }
            PluginSurfaceKind::Flow | PluginSurfaceKind::Skill | PluginSurfaceKind::Ui
                if self.workload.is_none()
                    && self.mcp_transport.is_none()
                    && self.mcp_tool_count.is_none()
                    && self.okf_bundle.is_none() =>
            {
                Ok(())
            }
            PluginSurfaceKind::Okf
                if self.workload.is_none()
                    && self.mcp_transport.is_none()
                    && self.mcp_tool_count.is_none()
                    && self
                        .okf_bundle
                        .as_ref()
                        .is_some_and(|bundle| bundle.validate().is_ok()) =>
            {
                Ok(())
            }
            _ => Err(catalog_error(
                "Catalog surface workload metadata does not match its surface kind.",
            )),
        }
    }

    pub fn reference(&self) -> PluginSurfaceRef {
        PluginSurfaceRef {
            kind: self.kind,
            id: self.id.clone(),
        }
    }

    pub fn canonical_bytes(&self) -> UseResult<Vec<u8>> {
        self.validate()?;
        canonical_json(self, "plugin catalog surface", CATALOG_ERROR)
    }

    pub fn descriptor_digest(&self) -> UseResult<String> {
        Ok(canonical_digest(&self.canonical_bytes()?))
    }
}

impl CatalogArchive {
    pub(super) fn validate(
        &self,
        package_id: &str,
        version: &str,
        channel: PluginReleaseChannel,
        target: &str,
    ) -> UseResult<()> {
        let channel = channel.as_str();
        let prefix = format!("extensions/{package_id}/{version}/{channel}/{target}/");
        if self.length == 0
            || self.length > MAX_REMOTE_ARCHIVE_BYTES
            || !valid_sha256(&self.sha256)
            || !valid_target_name(&self.target_name)
            || !self.target_name.starts_with(&prefix)
        {
            return Err(catalog_error(
                "The plugin catalog archive identity or size is invalid.",
            ));
        }
        Ok(())
    }
}

impl CatalogPlanningTarget {
    pub(super) fn validate(
        &self,
        package_id: &str,
        version: &str,
        channel: PluginReleaseChannel,
        target: &str,
    ) -> UseResult<()> {
        let expected_target_name = format!(
            "extensions/{package_id}/{version}/{}/{target}/planning-v1.json",
            channel.as_str()
        );
        if self.length == 0
            || self.length > MAX_PLANNING_TARGET_BYTES
            || !valid_sha256(&self.sha256)
            || self.target_name != expected_target_name
        {
            return Err(catalog_error(
                "The plugin catalog planning target identity or size is invalid.",
            ));
        }
        Ok(())
    }
}

impl CatalogPackage {
    fn validate(&self) -> UseResult<()> {
        if self.expanded_bytes == 0
            || self.expanded_bytes > MAX_EXPANDED_PACKAGE_BYTES
            || self.file_count == 0
            || self.file_count > MAX_PACKAGE_FILES
            || self
                .sha256
                .as_deref()
                .is_some_and(|value| !valid_sha256(value))
            || self
                .manifest_sha256
                .as_deref()
                .is_some_and(|value| !valid_sha256(value))
            || self.sha256.is_none()
            || self.manifest_sha256.is_none()
        {
            return Err(catalog_error(
                "The plugin catalog package estimate or digest is invalid.",
            ));
        }
        Ok(())
    }
}

impl CatalogAvailability {
    fn validate(&self, package_id: &str) -> UseResult<()> {
        match self {
            Self::Available => Ok(()),
            Self::Deprecated {
                message,
                replacement,
            } => {
                if !valid_catalog_text(message, 1024)
                    || replacement
                        .as_deref()
                        .is_some_and(|value| value == package_id || !valid_package_id(value))
                {
                    return Err(catalog_error("The plugin deprecation metadata is invalid."));
                }
                Ok(())
            }
            Self::Withdrawn {
                reason,
                advisory_url,
            } => {
                if !valid_catalog_text(reason, 1024)
                    || advisory_url
                        .as_deref()
                        .is_some_and(|value| !valid_repository_url(value))
                {
                    return Err(catalog_error("The plugin withdrawal metadata is invalid."));
                }
                Ok(())
            }
        }
    }
}

impl PluginReleaseChannel {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Beta => "beta",
            Self::Nightly => "nightly",
            Self::Stable => "stable",
        }
    }
}

pub(super) fn catalog_error(message: impl Into<String>) -> crate::UseError {
    contract_error(CATALOG_ERROR, message)
}