use crate::slot::ConfigSlot;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TargetSection {
#[serde(rename = "type")]
pub target_type: String,
pub version: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MetadataSection {
pub server_type: String,
pub snapshot_baked: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AwsSection {
pub region: String,
}
#[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>,
}
#[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>,
}
#[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>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthScopesSection {
#[serde(default)]
pub custom: BTreeMap<String, String>,
}
#[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>,
#[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>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObservabilityAlarmsSection {
pub error_threshold: i64,
pub latency_threshold_ms: i64,
}
#[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>,
}
#[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>,
}
#[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,
}
#[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>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IamSection {
#[serde(default)]
pub statements: Vec<IamStatement>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GcpSection {
pub project_id: String,
pub region: String,
}
#[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>,
}
#[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,
#[serde(default)]
pub environment: BTreeMap<String, String>,
#[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>,
}
#[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,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CedarPolicySet(pub Vec<CedarPolicy>);
#[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>,
}
#[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,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ServerPackage {
pub name: String,
pub version: semver::Version,
#[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::*;
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(),
}
}
#[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() {
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);
}
#[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();
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()
);
}
}
#[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"));
}
#[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() {
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() {
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"));
}
}