pmcp-package 0.4.0

AI-Package format for portable MCP agent/server/team/workflow packages: typed manifest schemas, config-slot aggregation, local OCI pack/unpack, and canonical-digest computation.
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! `ServerPackage` — the closed, deployable `mcp-server` package type.
//!
//! A `ServerPackage` carries everything needed to deploy one built-in MCP
//! server: a reference to its compiled bootstrap binary, its
//! [`DeployDescriptor`] (the typed equivalent of a `.pmcp/deploy.toml`), its
//! cedar-policy set, tool metadata, and its declared [`ConfigSlot`]s.
//!
//! # `DeployDescriptor` — closed-set enforcement
//!
//! `DeployDescriptor` losslessly represents the UNION of TOML tables observed
//! across all 19 tracked `.pmcp/deploy.toml` files in this repo (see the
//! `fixture_coverage_all_tracked_deploy_descriptors_parse` test below, which
//! discovers and parses every one of them via `git ls-files`). Every
//! CFN-shaping sub-struct — the ones whose fields directly gate what
//! CloudFormation resources a deploy produces — carries
//! `#[serde(deny_unknown_fields)]` (a descriptor with an unrecognized
//! field must fail to parse rather than silently imply undeclared
//! infrastructure). Tables that are open-ended by nature (`[environment]`,
//! `[secrets]`, `[auth.groups]`, `[auth.scopes.custom]`) stay permissive
//! maps — that openness is the correct shape for arbitrary env-var
//! *names*/secret *names* declarations, not a gap in the closed-set
//! guarantee (RESEARCH Pitfall 4: apply `deny_unknown_fields` narrowly).
//!
//! Optional tables not present on every server (`[metadata]`, `[auth.dcr]`,
//! `[auth.groups]`, `[observability.alarms]`, `[assets]`, `[composition]`,
//! `[[iam.statements]]`, `[gcp]`, `[layout]`) are modeled as `Option<T>` so a
//! descriptor lacking them still parses.
//!
//! # Cedar policies are AVP tuples, not files (RESEARCH Pitfall 2)
//!
//! [`CedarPolicySet`] is a plain `Vec<CedarPolicy>` with no
//! `policy_file_path`/`path` field. Cedar policies are runtime state in
//! Amazon Verified Permissions, mutated live via the admin UI's Code Mode
//! policy editor — there is no on-disk `.cedar` file this crate could
//! reference. The capture step maps
//! `PolicyManager::list_policies(Some(server_id))`'s `PolicyMetadata` results
//! into this shape; this phase only needs the shape to exist and round-trip.
//!
//! # 4 MB ECR manifest ceiling (RESEARCH Pitfall 3)
//!
//! ECR's `Image.imageManifest` field has a documented ~4 MB max length. A
//! realistic `ServerPackage`/`WorkflowManifest` (dozens of components, a few
//! dozen slots) is nowhere near this — no action needed in this phase — but
//! the composition model already avoids the problem by construction:
//! the bootstrap binary is referenced by digest (an OCI layer), never
//! inlined in the manifest (see [`BinaryRef`]).

use crate::slot::ConfigSlot;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

// ---------------------------------------------------------------------
// DeployDescriptor and its sub-tables (closed-set enforcement)
// ---------------------------------------------------------------------

/// `[target]` — the deploy target selection. `target_type` maps to TOML's
/// `type` key (a reserved word in Rust, hence the rename).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TargetSection {
    #[serde(rename = "type")]
    pub target_type: String,
    pub version: String,
}

/// `[metadata]` — optional, graph-rag-family servers only (config-driven CFN
/// template metadata, cargo-pmcp >= 0.16).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MetadataSection {
    pub server_type: String,
    pub snapshot_baked: bool,
}

/// `[aws]` — always present across every tracked descriptor (even the
/// google-cloud-run target requires it — cargo-pmcp's unified `DeployConfig`
/// parser mandates the field even where the target ignores it).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AwsSection {
    pub region: String,
}

/// `[server]` — the deployed function's identity + sizing.
///
/// `memory_mb` is `Option` because at least one tracked descriptor
/// (`approval-mcp`) omits it: cargo-pmcp v0.x ignored the field entirely, so
/// it was commented out rather than set. **That is no longer true, and the
/// `Option` now carries meaning rather than just tolerance.** cargo-pmcp's
/// pmcp-run deploy path rewrites `Properties.MemorySize` post-synth when the
/// field is present, on both of its synth engines; `None` means "leave the
/// engine's own default alone". The parallel field on cargo-pmcp's own
/// `ServerConfig` was changed from a defaulted `u32` to an `Option<u32>` for
/// exactly this reason — with a serde default, "omitted" and "explicitly 512"
/// are indistinguishable, and materializing the 512 would silently resize
/// every Lambda that never asked. See debug session
/// `deploy-server-memory-timeout`.
///
/// Not every AWS path honors it: `pmcp-cfn-renderer` still pins memory to a
/// module const, and cargo-pmcp's `aws-lambda` target has no post-render seam,
/// so both print a divergence warning instead. `timeout_seconds` is required
/// here (every tracked descriptor sets it) and IS threaded by the renderer.
///
/// The `memory`/`cpu`/`ingress`/`allow_unauthenticated`/`binary` fields are
/// google-cloud-run-target-only extras (observed in
/// `built-in/test-harness/oauth-external-google/.pmcp/deploy.toml`) — ignored
/// by the pmcp-run (AWS Lambda) target but must round-trip losslessly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ServerSection {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub memory_mb: Option<i64>,
    pub timeout_seconds: i64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub memory: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cpu: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ingress: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allow_unauthenticated: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub binary: Option<String>,
}

/// `[auth.cognito]` — Cognito-specific OAuth configuration, written by
/// `cargo pmcp deploy init --oauth cognito` (`cargo-pmcp`'s
/// `CognitoConfig`, `deployment/config.rs`). Not among the original 19
/// tracked fixtures (none of them enable Cognito OAuth), but real —
/// discovered by the `pmcp-cfn-renderer` Task 2 golden-generation script
/// synthesizing a fresh `--oauth cognito` scaffold, whose emitted
/// `.pmcp/deploy.toml` failed to parse before this section existed. Mirrors
/// `CognitoConfig` field-for-field (including its defaults) so descriptors
/// captured from either surface round-trip identically.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CognitoSection {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user_pool_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user_pool_name: Option<String>,
    pub resource_server_id: String,
    #[serde(default)]
    pub social_providers: Vec<String>,
    pub mfa: String,
    pub access_token_ttl: String,
    pub refresh_token_ttl: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub domain: Option<String>,
}

/// `[auth.dcr]` — dynamic client registration config. All fields but
/// `enabled` default to empty (one tracked descriptor —
/// `oauth-external-google/aws-lambda` — declares only `enabled = false`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthDcrSection {
    pub enabled: bool,
    #[serde(default)]
    pub public_client_patterns: Vec<String>,
    #[serde(default)]
    pub default_scopes: Vec<String>,
    #[serde(default)]
    pub allowed_scopes: Vec<String>,
}

/// `[auth.scopes.custom]` — an open, always-currently-empty extension point
/// for future custom scope name→description declarations. Not
/// `deny_unknown_fields` at the map level (maps accept any key by
/// construction); the wrapping struct is closed so nothing else can hide
/// under `[auth.scopes]`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthScopesSection {
    #[serde(default)]
    pub custom: BTreeMap<String, String>,
}

/// `[auth]` — always present. `groups` (role name → description) is
/// currently observed only on the parked `epstein-oversight` descriptor;
/// `dcr`/`scopes` are the common optional sub-tables.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthSection {
    pub enabled: bool,
    pub provider: String,
    #[serde(default)]
    pub callback_urls: Vec<String>,
    /// Cognito-specific config, present only when `provider = "cognito"`
    /// AND the deploy is a local (non-`pmcp-run`) `aws-lambda` OAuth stack
    /// — see [`CognitoSection`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cognito: Option<CognitoSection>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dcr: Option<AuthDcrSection>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub groups: Option<BTreeMap<String, String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scopes: Option<AuthScopesSection>,
}

/// `[observability.alarms]` — optional CloudWatch alarm thresholds.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObservabilityAlarmsSection {
    pub error_threshold: i64,
    pub latency_threshold_ms: i64,
}

/// `[observability]` — always present (even the google-cloud-run target
/// descriptor carries it, with `alarms` absent).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObservabilitySection {
    pub log_retention_days: i64,
    pub enable_xray: bool,
    pub create_dashboard: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub alarms: Option<ObservabilityAlarmsSection>,
}

/// `[assets]` — optional include/exclude glob lists for the deploy bundle.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AssetsSection {
    #[serde(default)]
    pub include: Vec<String>,
    #[serde(default)]
    pub exclude: Vec<String>,
}

/// `[composition]` — optional (absent on the google-cloud-run target
/// descriptor). `tier` is a closed set at the platform level
/// (`foundation`/`domain`) enforced by [`crate::validation::allowlist`], not
/// by this struct (kept as `String` here so an unrecognized tier still
/// PARSES — the allowlist is the deliberate second, explicit gate — see the
/// `registry-tier-enum-mismatch` incident referenced in
/// `oauth-external-google/aws-lambda/.pmcp/deploy.toml`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CompositionSection {
    pub tier: String,
    pub allow_composition: bool,
    pub internal_only: bool,
}

/// One `[[iam.statements]]` entry — a custom IAM grant spliced into the
/// generated CDK stack.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IamStatement {
    pub effect: String,
    pub actions: Vec<String>,
    pub resources: Vec<String>,
}

/// `[iam]` — wraps the `[[iam.statements]]` array-of-tables.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IamSection {
    #[serde(default)]
    pub statements: Vec<IamStatement>,
}

/// `[gcp]` — google-cloud-run-target-only.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GcpSection {
    pub project_id: String,
    pub region: String,
}

/// `[layout]` — google-cloud-run-target-only (multi-crate workspace layout hint).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LayoutSection {
    pub kind: String,
    pub primary: String,
    #[serde(default)]
    pub path_deps: Vec<String>,
}

/// The closed, typed equivalent of a `.pmcp/deploy.toml` file.
///
/// `#[serde(deny_unknown_fields)]` on this outer struct is the actual
/// enforcement point: an unrecognized TOP-LEVEL table/field fails to parse.
/// Every table observed across all 19 tracked descriptors (see the
/// fixture-coverage test) is declared here — most as `Option<T>` since no
/// single server carries every optional table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DeployDescriptor {
    pub target: TargetSection,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<MetadataSection>,
    pub aws: AwsSection,
    pub server: ServerSection,
    /// Env-var NAMES → literal (never-secret) values only — never a
    /// resolved secret value. `BTreeMap` for stable ordering (no
    /// `HashMap` — canonical-digest stability).
    #[serde(default)]
    pub environment: BTreeMap<String, String>,
    /// Declared secret NAMES → (currently always empty) placeholder value.
    /// Secret VALUES are injected out-of-band at deploy time via
    /// `cargo pmcp secret set`/the platform secret-management surface —
    /// never written here (the payload declares required secret
    /// *names*, never values).
    #[serde(default)]
    pub secrets: BTreeMap<String, String>,
    pub auth: AuthSection,
    pub observability: ObservabilitySection,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub composition: Option<CompositionSection>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assets: Option<AssetsSection>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub iam: Option<IamSection>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub gcp: Option<GcpSection>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub layout: Option<LayoutSection>,
}

// ---------------------------------------------------------------------
// CedarPolicySet (RESEARCH Pitfall 2 — AVP tuples, not files)
// ---------------------------------------------------------------------

/// One cedar policy, in the shape `PolicyManager::list_policies` /
/// `PolicyMetadata` produces (`built-in/shared/mcp-server-common/src/
/// code_mode/policy_management.rs`) — `category`/`risk` are kept as `String`
/// here (not that crate's enums) so this format-only crate has no dependency
/// on `mcp-server-common`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CedarPolicy {
    pub id: String,
    pub cedar_text: String,
    pub title: String,
    pub description: String,
    pub category: String,
    pub risk: String,
}

/// A server's cedar-policy set. Deliberately a plain `Vec<CedarPolicy>`
/// (`#[serde(transparent)]` — serializes as a flat JSON array) with NO
/// `policy_file_path`/`path` field: policies come from AVP at capture time
///, never from a directory of `.cedar` files.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CedarPolicySet(pub Vec<CedarPolicy>);

// ---------------------------------------------------------------------
// ToolMetadata
// ---------------------------------------------------------------------

/// Tool/connector surface metadata. Kept permissive — `annotations` carries
/// whatever additional per-tool fields (read_only_hint, cost_hint, schema
/// hints,...) the capture step produces, without this crate needing to
/// model every one as a typed field.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolMetadata {
    pub name: String,
    pub description: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<serde_json::Value>,
}

// ---------------------------------------------------------------------
// BinaryRef — a reference to the bootstrap blob, never the bytes themselves
// ---------------------------------------------------------------------

/// A reference to the server's bootstrap binary — the WIRE payload of the
/// `application/vnd.pmcp.mcp-server.binary-ref.v1+json` layer.
///
/// The binary bytes are NEVER inlined here: `pack_server` takes a
/// [`BinaryMode`] as a SEPARATE argument and turns it into either an embedded
/// bootstrap layer or a binary-ref layer carrying this struct. This type only
/// carries a digest plus a descriptive media-type hint.
///
/// `digest` is `Option` for WIRE TOLERANCE only — a decoded layer can be
/// missing it, and `unpack_server` rejects that case. The API-level
/// [`BinaryMode::Referenced`] digest is non-optional, so callers of the
/// packing API cannot express an unpinned binary at all.
///
/// [`BinaryMode`]: crate::oci::pack::BinaryMode
/// [`BinaryMode::Referenced`]: crate::oci::pack::BinaryMode::Referenced
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BinaryRef {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub digest: Option<crate::digest::ManifestDigest>,
    pub media_type: String,
}

// ---------------------------------------------------------------------
// ServerPackage
// ---------------------------------------------------------------------

/// The closed, deployable `mcp-server` AI-Package payload.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ServerPackage {
    pub name: String,
    pub version: semver::Version,
    /// Set at pack time — `None` before packing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub digest: Option<crate::digest::ManifestDigest>,
    pub deploy: DeployDescriptor,
    pub policies: CedarPolicySet,
    pub tools: Vec<ToolMetadata>,
    pub config_slots: Vec<ConfigSlot>,
}

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

    /// A `DeployDescriptor` matching the team-fs baseline (RESEARCH's
    /// confirmed real-file example), used as the round-trip / allowlist
    /// fixture across this crate's tests.
    pub(crate) fn sample_deploy_descriptor() -> DeployDescriptor {
        DeployDescriptor {
            target: TargetSection {
                target_type: "pmcp-run".to_string(),
                version: "1.0.0".to_string(),
            },
            metadata: None,
            aws: AwsSection {
                region: "us-east-1".to_string(),
            },
            server: ServerSection {
                name: "team-fs".to_string(),
                memory_mb: Some(1024),
                timeout_seconds: 30,
                memory: None,
                cpu: None,
                ingress: None,
                allow_unauthenticated: None,
                binary: None,
            },
            environment: BTreeMap::from([
                ("RUST_LOG".to_string(), "info".to_string()),
                ("TRIGGER_BRIDGE".to_string(), "true".to_string()),
            ]),
            secrets: BTreeMap::new(),
            auth: AuthSection {
                enabled: false,
                provider: "none".to_string(),
                callback_urls: vec![],
                cognito: None,
                dcr: Some(AuthDcrSection {
                    enabled: true,
                    public_client_patterns: vec!["claude".to_string(), "desktop".to_string()],
                    default_scopes: vec!["openid".to_string(), "email".to_string()],
                    allowed_scopes: vec!["openid".to_string(), "mcp/read".to_string()],
                }),
                groups: None,
                scopes: Some(AuthScopesSection::default()),
            },
            observability: ObservabilitySection {
                log_retention_days: 30,
                enable_xray: true,
                create_dashboard: true,
                alarms: Some(ObservabilityAlarmsSection {
                    error_threshold: 10,
                    latency_threshold_ms: 5000,
                }),
            },
            composition: Some(CompositionSection {
                tier: "foundation".to_string(),
                allow_composition: true,
                internal_only: false,
            }),
            assets: Some(AssetsSection {
                include: vec![],
                exclude: vec!["**/*.tmp".to_string()],
            }),
            iam: None,
            gcp: None,
            layout: None,
        }
    }

    fn sample_cedar_policy() -> CedarPolicy {
        CedarPolicy {
            id: "p1".to_string(),
            cedar_text: "permit(principal, action, resource);".to_string(),
            title: "Allow all".to_string(),
            description: "test policy".to_string(),
            category: "read".to_string(),
            risk: "low".to_string(),
        }
    }

    // --- DeployDescriptor: deny_unknown_fields ---

    #[test]
    fn deploy_descriptor_rejects_unknown_top_level_field() {
        let mut value = serde_json::to_value(sample_deploy_descriptor()).unwrap();
        value
            .as_object_mut()
            .unwrap()
            .insert("unexpected_field".to_string(), serde_json::json!(true));
        let result: std::result::Result<DeployDescriptor, _> = serde_json::from_value(value);
        assert!(
            result.is_err(),
            "DeployDescriptor must reject an unrecognized top-level field"
        );
    }

    #[test]
    fn deploy_descriptor_round_trips_losslessly() {
        let descriptor = sample_deploy_descriptor();
        let json = serde_json::to_string(&descriptor).unwrap();
        let back: DeployDescriptor = serde_json::from_str(&json).unwrap();
        assert_eq!(back, descriptor);
    }

    #[test]
    fn deploy_descriptor_parses_a_descriptor_missing_all_optional_tables() {
        // Mirrors the google-cloud-run test-harness descriptor's shape: no
        // [secrets], no [auth.dcr], no [observability.alarms], no [assets],
        // no [composition] — but WITH [gcp]/[layout].
        let descriptor = DeployDescriptor {
            target: TargetSection {
                target_type: "google-cloud-run".to_string(),
                version: "1.0.0".to_string(),
            },
            metadata: None,
            aws: AwsSection {
                region: "us-east-1".to_string(),
            },
            server: ServerSection {
                name: "auth-echo-cloud-run".to_string(),
                memory_mb: Some(256),
                timeout_seconds: 30,
                memory: Some("256Mi".to_string()),
                cpu: Some("1".to_string()),
                ingress: Some("all".to_string()),
                allow_unauthenticated: Some(true),
                binary: Some("server".to_string()),
            },
            environment: BTreeMap::from([(
                "EXPECTED_AUDIENCE".to_string(),
                "placeholder".to_string(),
            )]),
            secrets: BTreeMap::new(),
            auth: AuthSection {
                enabled: false,
                provider: "none".to_string(),
                callback_urls: vec![],
                cognito: None,
                dcr: None,
                groups: None,
                scopes: None,
            },
            observability: ObservabilitySection {
                log_retention_days: 14,
                enable_xray: false,
                create_dashboard: false,
                alarms: None,
            },
            composition: None,
            assets: None,
            iam: None,
            gcp: Some(GcpSection {
                project_id: "ai-agents-446904".to_string(),
                region: "us-central1".to_string(),
            }),
            layout: Some(LayoutSection {
                kind: "multi-crate-isolated".to_string(),
                primary: "gcp-cloud-run".to_string(),
                path_deps: vec!["auth-echo-core".to_string()],
            }),
        };
        let json = serde_json::to_string(&descriptor).unwrap();
        let back: DeployDescriptor = serde_json::from_str(&json).unwrap();
        assert_eq!(back, descriptor);
    }

    /// fixture-coverage guard: discover EVERY tracked `.pmcp/deploy.toml`
    /// via `git ls-files` (not a hand-picked sample) and assert each one
    /// parses into `DeployDescriptor` without a `deny_unknown_fields`
    /// rejection. Dev-only (`toml` is a dev-dependency) — does not affect the
    /// crate's JSON runtime format.
    #[test]
    fn fixture_coverage_all_tracked_deploy_descriptors_parse() {
        use std::path::PathBuf;
        use std::process::Command;

        let repo_root: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("..")
            .join("..");

        let output = Command::new("git")
            .arg("ls-files")
            .arg("*/.pmcp/deploy.toml")
            .current_dir(&repo_root)
            .output()
            .expect("git ls-files must run (dev-only coverage test)");
        assert!(
            output.status.success(),
            "git ls-files failed: status={:?} stderr={}",
            output.status,
            String::from_utf8_lossy(&output.stderr)
        );

        let stdout = String::from_utf8(output.stdout).expect("git ls-files output must be UTF-8");
        let files: Vec<&str> = stdout.lines().filter(|l| !l.is_empty()).collect();

        // The discovery floor is host-repo dependent: it reflects however many
        // real `.pmcp/deploy.toml` fixtures the consuming repository happens to
        // track. The invariant this guard enforces is that EVERY tracked
        // descriptor parses into `DeployDescriptor` without a
        // `deny_unknown_fields` rejection — not a specific fixture count.
        for rel_path in &files {
            let full_path = repo_root.join(rel_path);
            let contents = std::fs::read_to_string(&full_path)
                .unwrap_or_else(|e| panic!("failed to read {full_path:?}: {e}"));
            let parsed: std::result::Result<DeployDescriptor, _> = toml::from_str(&contents);
            assert!(
                parsed.is_ok(),
                "DeployDescriptor failed to parse {rel_path}: {:?}",
                parsed.err()
            );
        }
    }

    // --- CedarPolicySet: no file-path field, flat-array round trip ---

    #[test]
    fn cedar_policy_set_round_trips_as_flat_json_array() {
        let set = CedarPolicySet(vec![sample_cedar_policy()]);
        let json = serde_json::to_value(&set).unwrap();
        assert!(
            json.is_array(),
            "CedarPolicySet must serialize as a flat JSON array (transparent newtype)"
        );
        let back: CedarPolicySet = serde_json::from_value(json).unwrap();
        assert_eq!(back, set);
    }

    #[test]
    fn cedar_policy_has_no_file_path_field() {
        let json = serde_json::to_value(sample_cedar_policy()).unwrap();
        let obj = json.as_object().unwrap();
        assert!(!obj.contains_key("policy_file_path"));
        assert!(!obj.contains_key("path"));
    }

    // --- ServerPackage round trip ---

    #[test]
    fn server_package_round_trips_losslessly() {
        let pkg = ServerPackage {
            name: "team-fs".to_string(),
            version: semver::Version::parse("1.0.0").unwrap(),
            digest: None,
            deploy: sample_deploy_descriptor(),
            policies: CedarPolicySet(vec![sample_cedar_policy()]),
            tools: vec![ToolMetadata {
                name: "fs__list".to_string(),
                description: "List files in a team workspace".to_string(),
                annotations: Some(serde_json::json!({ "read_only_hint": true })),
            }],
            config_slots: vec![],
        };
        let json = serde_json::to_string(&pkg).unwrap();
        let back: ServerPackage = serde_json::from_str(&json).unwrap();
        assert_eq!(back, pkg);
    }

    #[test]
    fn binary_ref_carries_no_inline_bytes() {
        // Structural proof: BinaryRef's only fields are `digest` (a
        // ManifestDigest, itself a validated String newtype) and
        // `media_type` (a String) — there is no way to construct one
        // holding a Vec<u8> of bootstrap bytes.
        let binary_ref = BinaryRef {
            digest: Some(crate::digest::ManifestDigest::from_bytes(b"fixture")),
            media_type: "application/x-lambda-bootstrap".to_string(),
        };
        assert!(binary_ref.digest.is_some());
    }

    #[test]
    fn server_package_has_no_binary_ref_field() {
        // D-08: "which binary" is a LAYER, not a struct field, so the
        // serialized ServerPackage must not carry a `binary_ref` key at all.
        // A stale field here would be a second source of truth able to
        // disagree with the package's actual binary layer.
        let pkg = ServerPackage {
            name: "team-fs".to_string(),
            version: semver::Version::parse("1.0.0").unwrap(),
            digest: None,
            deploy: sample_deploy_descriptor(),
            policies: CedarPolicySet(vec![]),
            tools: vec![],
            config_slots: vec![],
        };
        let json = serde_json::to_value(&pkg).unwrap();
        assert!(!json.as_object().unwrap().contains_key("binary_ref"));
    }
}