story 2-12-preview-partial-mode: implemented and reviewed via bmad-loop

This commit is contained in:
bsodfather committed 2026-10-03 15:04:15 +03:00
1 parent d1ba0bd0e4
commit ab35d67177
56 files changed
+5801 -376

No files matched your search

+5 -1
View File
@@ -29,7 +29,7 @@ use crate::{
capabilities::get_capabilities,
imports::{
apply_openapi_import_plan, create_openapi_import, create_openapi_import_plan,
preview_openapi_import,
preview_openapi_import, revalidate_openapi_import_plan,
},
observability::{
approve_approval, deny_approval, export_logs_csv, export_usage_csv, get_agent_usage,
@@ -65,6 +65,10 @@ pub fn build_app(state: AppState) -> Router {
"/imports/openapi/{job_id}/plans",
post(create_openapi_import_plan),
)
.route(
"/imports/openapi/plans/{plan_id}",
get(revalidate_openapi_import_plan),
)
.route(
"/imports/openapi/{plan_id}/apply",
post(apply_openapi_import_plan).layer(DefaultBodyLimit::max(16 * 1024)),
+13
View File
@@ -656,12 +656,25 @@ pub struct OpenApiImportPlanCandidateView {
pub struct OpenApiImportPlanCreateResponse {
pub plan_reference: String,
pub plan_digest: String,
pub catalog_revision: String,
pub support_matrix_version: String,
pub expires_at: String,
pub selection_mode: String,
pub counts: OpenApiImportPlanCounts,
pub candidates: Vec<OpenApiImportPlanCandidateView>,
}
#[derive(Clone, Debug, Serialize)]
pub struct OpenApiImportPlanRevalidationResponse {
pub plan_reference: String,
pub plan_digest: String,
pub revalidation_status: String,
pub revalidation_required: bool,
pub reason: Option<String>,
pub counts: OpenApiImportPlanCounts,
pub candidates: Vec<OpenApiImportPlanCandidateView>,
}
#[derive(Clone, Debug, Serialize)]
pub struct CreatedOperationResponse {
pub operation_id: String,
+14
View File
@@ -223,6 +223,20 @@ pub async fn create_openapi_import_plan(
Ok(Json(json!(plan)))
}
pub async fn revalidate_openapi_import_plan(
Path(path): Path<WorkspacePlanPath>,
State(state): State<AppState>,
) -> Result<Json<Value>, ApiError> {
let plan = state
.service
.revalidate_openapi_import_plan(
&path.workspace_id.as_str().into(),
&path.plan_id.as_str().into(),
)
.await?;
Ok(Json(json!(plan)))
}
pub async fn apply_openapi_import_plan(
Path(path): Path<WorkspacePlanPath>,
State(state): State<AppState>,
+174 -5
View File
@@ -6,8 +6,9 @@ use crank_core::{
SecretStatus, ToolQualityFinding, ToolQualitySeverity, WorkspaceId,
};
use crank_import::rest::{
ImportFinding, ImportFindingSeverity, ImportOperationCandidate, NORMALIZER_VERSION,
PROJECTION_VERSION, operation_draft_from_candidate, validate_document_source_safe,
ImportFinding, ImportFindingSeverity, ImportNameConflict, ImportOperationCandidate,
NORMALIZER_VERSION, PROJECTION_VERSION, operation_draft_from_candidate,
validate_document_source_safe,
};
use crank_registry::{
ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity,
@@ -27,7 +28,8 @@ use crate::{
AdminService, OpenApiImportAuthProfileChoice, OpenApiImportCreatePayload,
OpenApiImportCreateResponse, OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload,
OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload,
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision, OpenApiImportPreviewResponse,
OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision,
OpenApiImportPlanRevalidationResponse, OpenApiImportPreviewResponse,
OpenApiImportSecurityProfileChoice, OpenApiImportSkippedOperation, OpenApiUpload,
OpenApiUploadLocale, OperationPayload, new_prefixed_id,
},
@@ -52,7 +54,8 @@ use verified_preview::{
const IMPORT_JOB_TTL_HOURS: i64 = 24;
const OPENAPI_PARSE_DEADLINE: std::time::Duration = std::time::Duration::from_secs(30);
const OPENAPI_PARSE_CONCURRENCY: usize = 4;
const MAX_IMPORT_PLAN_KEYS: usize = 512;
const MAX_IMPORT_PLAN_KEYS: usize = 2_000;
const OPENAPI_SUPPORT_MATRIX_VERSION: &str = "1.0.2";
const MAX_IMPORT_PLAN_KEY_BYTES: usize = 4_096;
const MAX_IMPORT_PLAN_SERVER_URL_BYTES: usize = 8_192;
const MAX_IMPORT_PLAN_NAME_BYTES: usize = 256;
@@ -60,6 +63,132 @@ const MAX_IMPORT_PLAN_ID_REF_BYTES: usize = 128;
static OPENAPI_PARSE_SLOTS: tokio::sync::Semaphore =
tokio::sync::Semaphore::const_new(OPENAPI_PARSE_CONCURRENCY);
fn preview_name_conflicts(
preview: &crank_import::rest::ImportPreview,
catalog_names: &[String],
) -> Vec<ImportNameConflict> {
let operations = preview
.groups
.iter()
.flat_map(|group| group.operations.iter())
.collect::<Vec<_>>();
let mut operation_ids = BTreeMap::<&str, usize>::new();
for candidate in &operations {
if let Some(id) = candidate.operation_id.as_deref() {
*operation_ids.entry(id).or_default() += 1;
}
}
let occupied = catalog_names
.iter()
.map(String::as_str)
.collect::<BTreeSet<_>>();
let mut proposals = catalog_names.iter().cloned().collect::<BTreeSet<_>>();
proposals.extend(
operations
.iter()
.map(|candidate| candidate.suggested_name.clone()),
);
let mut conflicts = Vec::new();
for candidate in operations {
let duplicate = candidate
.operation_id
.as_deref()
.is_some_and(|id| operation_ids.get(id).is_some_and(|count| *count > 1));
let invalid = candidate.operation_id.as_deref().is_some_and(|id| {
id.is_empty()
|| id.len() > MAX_IMPORT_PLAN_NAME_BYTES
|| !id.bytes().enumerate().all(|(index, ch)| {
ch.is_ascii_alphabetic() || ch == b'_' || (index > 0 && ch.is_ascii_digit())
})
});
let taken = occupied.contains(candidate.suggested_name.as_str());
if !duplicate && !invalid && !taken {
continue;
}
let base = candidate
.suggested_name
.chars()
.take(240)
.collect::<String>();
let proposal = next_available_name(&base, &mut proposals);
for conflict_type in [
duplicate.then_some("duplicate_name"),
invalid.then_some("invalid_name"),
taken.then_some("taken_name"),
]
.into_iter()
.flatten()
{
conflicts.push(ImportNameConflict {
operation_key: candidate.key.clone(),
conflict_type: conflict_type.to_owned(),
proposed_name: proposal.clone(),
source_location: candidate.source_location.as_ref().map(|location| {
crank_import::rest::SourceLocation {
pointer: if candidate.operation_id.is_some() {
format!("{}/operationId", location.pointer)
} else {
location.pointer.clone()
},
}
}),
});
}
}
conflicts
}
fn next_available_name(base: &str, occupied: &mut BTreeSet<String>) -> String {
// A catalog may contain more names than the preview candidate cap. With
// occupied.len() + 1 suffixes, at least one candidate must be free.
(2..=occupied.len() + 2)
.map(|index| format!("{base}_{index}"))
.find(|name| occupied.insert(name.clone()))
.expect("finite occupied names always leave a free suffix")
}
fn attach_preview_readiness(
preview: &mut crank_import::rest::ImportPreview,
choices: &[OpenApiImportSecurityProfileChoice],
) {
let document_blocked = preview.findings.iter().any(|finding| {
finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none()
});
for candidate in preview
.groups
.iter_mut()
.flat_map(|group| group.operations.iter_mut())
{
let blocked = document_blocked
|| candidate
.findings
.iter()
.any(|finding| finding.severity == ImportFindingSeverity::Error);
let alternatives = &candidate.security_requirements.alternatives;
let public = alternatives.is_empty()
|| alternatives
.iter()
.any(|alternative| alternative.supported && alternative.schemes.is_empty());
candidate.status = if blocked {
"blocked"
} else if public {
"actionable"
} else if alternatives.iter().enumerate().any(|(index, alternative)| {
alternative.supported
&& choices.iter().any(|choice| {
choice.operation_key == candidate.key
&& choice.alternative_index == index
&& !choice.profiles.is_empty()
})
}) {
"unresolved"
} else {
"blocked"
}
.to_owned();
}
}
impl AdminService {
#[instrument(skip(self, upload), fields(workspace_id = %workspace_id.as_str()))]
pub async fn preview_openapi_import(
@@ -186,6 +315,20 @@ impl AdminService {
detach_guard.detach_now().await;
return Err(ApiError::openapi_upload(locale, "no_methods"));
}
let candidate_count: usize = parsed
.preview
.groups
.iter()
.map(|group| group.operations.len())
.sum();
if candidate_count > MAX_IMPORT_PLAN_KEYS {
dependencies.detach_all().await;
detach_guard.detach_now().await;
return Err(ApiError::validation_with_context(
"OpenAPI preview has too many candidates",
json!({ "error_code": "openapi_preview_too_many_candidates", "limit": MAX_IMPORT_PLAN_KEYS }),
));
}
let source_envelope = ImportJobSourceEnvelope {
source_id,
digest: artifact.artifact_ref().clone(),
@@ -236,12 +379,19 @@ impl AdminService {
let security_profile_choices = self
.security_profile_choices(workspace_id, &parsed.preview)
.await?;
let catalog_names = self
.registry
.list_import_catalog_names(workspace_id)
.await?;
let mut response_preview = parsed.preview;
response_preview.conflicts = preview_name_conflicts(&response_preview, &catalog_names);
attach_preview_readiness(&mut response_preview, &security_profile_choices);
Ok(OpenApiImportPreviewResponse {
job_id: job_id.as_str().to_owned(),
expires_at: expires_at
.format(&Rfc3339)
.map_err(|error| ApiError::internal(error.to_string()))?,
preview: parsed.preview,
preview: response_preview,
security_profile_choices,
})
}
@@ -542,6 +692,7 @@ fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreate
operation.name
),
operation_key: Some(operation.operation_key.clone()),
source_location: None,
})
})
.collect::<Vec<_>>();
@@ -560,6 +711,7 @@ fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreate
format!("Операция {name} уже существует и была пропущена.")
},
operation_key: Some(operation.operation_key.clone()),
source_location: None,
});
OpenApiImportSkippedOperation {
operation_key: operation.operation_key.clone(),
@@ -658,3 +810,20 @@ fn tool_quality_finding_from_import(finding: &ImportFinding) -> ToolQualityFindi
field_path: finding.operation_key.clone(),
}
}
#[cfg(test)]
mod preview_name_tests {
use super::next_available_name;
use std::collections::BTreeSet;
#[test]
fn crowded_catalog_still_gets_a_free_deterministic_proposal() {
let mut occupied = (2..=2_002)
.map(|index| format!("shared_{index}"))
.collect::<BTreeSet<_>>();
occupied.insert("shared".to_owned());
assert_eq!(next_available_name("shared", &mut occupied), "shared_2003");
assert_eq!(next_available_name("shared", &mut occupied), "shared_2004");
}
}
+60 -130
View File
@@ -1,3 +1,5 @@
mod validation;
use super::*;
use crank_core::AuthConfig;
use crank_import::rest::SecurityRequirementsSource as SecuritySource;
@@ -61,6 +63,7 @@ impl AdminService {
ImportPreviewResolution::NonFatal(error) => error,
})?;
let preview = &resolved.preview;
let catalog_revision = self.registry.import_catalog_revision(workspace_id).await?;
// Stored external dependency snapshots are re-verified for every
// replay variant (including historically persisted ones) without a
@@ -81,6 +84,12 @@ impl AdminService {
.map(|operation| (operation.key.clone(), operation))
.collect();
let all_set: BTreeSet<String> = candidates.keys().cloned().collect();
if all_set.len() > MAX_IMPORT_PLAN_KEYS {
return Err(ApiError::validation_with_context(
"too many candidates for an import plan",
json!({ "error_code": "import_plan_invalid_input" }),
));
}
let selected = resolve_plan_selection(
&payload.selection_mode,
&payload.selected_operation_keys,
@@ -100,7 +109,31 @@ impl AdminService {
.filter(|key| !selected.contains(*key))
.cloned()
.collect();
let decisions = validate_plan_decisions(&payload.decisions, &selected)?;
let excluded_reasons = excluded
.iter()
.map(|key| {
let reason = preview
.findings
.iter()
.find(|finding| {
finding.severity == ImportFindingSeverity::Error
&& finding.operation_key.is_none()
})
.map(|finding| finding.code.clone())
.or_else(|| {
candidates.get(key).and_then(|candidate| {
candidate
.findings
.iter()
.find(|finding| finding.severity == ImportFindingSeverity::Error)
.map(|finding| finding.code.clone())
})
})
.unwrap_or_else(|| "not_selected".to_owned());
(key.clone(), reason)
})
.collect::<BTreeMap<_, _>>();
let decisions = validation::validate_plan_decisions(&payload.decisions, &selected)?;
let global_blocker = preview
.findings
@@ -144,6 +177,15 @@ impl AdminService {
.count(),
blocked: planned.iter().filter(|candidate| candidate.blocked).count(),
};
if payload.selection_mode == "partial" && counts.blocked != 0 {
return Err(ApiError::validation_with_context(
"partial selection includes blocked candidates",
json!({ "error_code": "import_plan_partial_blocked" }),
));
}
if self.registry.import_catalog_revision(workspace_id).await? != catalog_revision {
return Err(import_plan_repreview_error("import_plan_catalog_stale"));
}
// Server-authored immutable plan. Client-supplied candidate material,
// digests or payload fields are not read: every value below derives
@@ -157,7 +199,7 @@ impl AdminService {
});
let plan_reference = new_prefixed_id("plan");
let plan_payload = json!({
"schema_version": "import-plan-v1",
"schema_version": "import-plan-v2",
"plan_reference": plan_reference,
"job_id": job_id.as_str(),
"server_url": payload.server_url,
@@ -167,12 +209,25 @@ impl AdminService {
},
"dependencies": job.preview_payload.get("dependencies").cloned().unwrap_or(json!([])),
"fingerprint": resolved.fingerprint,
"catalog_revision": catalog_revision,
"support_matrix_version": OPENAPI_SUPPORT_MATRIX_VERSION,
"findings": {
"document": preview.findings,
"candidates": candidates.iter().map(|(key, candidate)| json!({
"key": key,
"findings": candidate.findings,
})).collect::<Vec<_>>(),
},
"selection": {
"mode": payload.selection_mode,
"all": all_keys,
"selected": selected_keys,
"excluded": excluded,
},
"excluded_reasons": excluded_reasons.iter().map(|(key, reason)| json!({
"key": key,
"reason": reason,
})).collect::<Vec<_>>(),
"counts": plan_counts_value,
"candidates": planned.iter().map(plan_candidate_payload).collect::<Result<Vec<_>, _>>()?,
});
@@ -224,13 +279,15 @@ impl AdminService {
confirmed_name: None,
auth_profile_id: None,
security_alternative_index: None,
reason: Some("not_selected".to_owned()),
reason: excluded_reasons.get(key).cloned(),
}
}));
Ok(OpenApiImportPlanCreateResponse {
plan_reference: created.id.as_str().to_owned(),
plan_digest: created.digest,
catalog_revision,
support_matrix_version: OPENAPI_SUPPORT_MATRIX_VERSION.to_owned(),
expires_at: created
.expires_at
.format(&Rfc3339)
@@ -703,133 +760,6 @@ fn resolve_plan_selection(
}
}
/// Binds decisions to selected candidates with action-specific field rules:
/// unknown/duplicate keys, foreign fields and oversized references are
/// rejected before the plan is materialized.
fn validate_plan_decisions<'input>(
decisions: &'input [OpenApiImportPlanDecision],
selected: &BTreeSet<String>,
) -> Result<BTreeMap<String, &'input OpenApiImportPlanDecision>, ApiError> {
let invalid = |context: Value| {
Err(ApiError::validation_with_context(
"import plan decision is invalid",
context,
))
};
let mut mapping = BTreeMap::new();
for decision in decisions {
if decision.operation_key.is_empty()
|| decision.operation_key.len() > MAX_IMPORT_PLAN_KEY_BYTES
{
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
if !matches!(
decision.action.as_str(),
"create" | "skip" | "rename" | "new_version"
) {
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
validate_decision_fields(decision)?;
if !selected.contains(&decision.operation_key) {
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_decision_unselected"
}));
}
if mapping
.insert(decision.operation_key.clone(), decision)
.is_some()
{
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
}
Ok(mapping)
}
fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(), ApiError> {
let context = json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
});
let invalid = |message: &str| -> Result<(), ApiError> {
Err(ApiError::validation_with_context(message, context.clone()))
};
if decision
.target_operation_id
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
{
return invalid("decision target_operation_id is empty or too long");
}
if decision
.confirmed_name
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_NAME_BYTES)
{
return invalid("decision confirmed_name is empty or too long");
}
if decision
.auth_profile_id
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
{
return invalid("decision auth_profile_id is empty or too long");
}
if decision
.security_alternative_index
.is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS)
{
return invalid("decision security_alternative_index is too large");
}
match decision.action.as_str() {
"create" => {
if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() {
return invalid(
"target_operation_id and confirmed_name are not allowed for action create",
);
}
}
"skip" => {
if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() {
return invalid("security binding is not allowed for action skip");
}
if decision.target_operation_id.is_none() {
return invalid("action skip requires the exact target_operation_id");
}
if decision.confirmed_name.is_some() {
return invalid("confirmed_name is not allowed for action skip");
}
}
"rename" => {
if decision.target_operation_id.is_some() {
return invalid("target_operation_id is not allowed for action rename");
}
if decision.confirmed_name.is_none() {
return invalid("action rename requires the exact confirmed_name");
}
}
"new_version" => {
if decision.target_operation_id.is_none() {
return invalid("action new_version requires the exact target_operation_id");
}
if decision.confirmed_name.is_some() {
return invalid("confirmed_name is not allowed for action new_version");
}
}
_ => unreachable!("decision action is validated before the field rules"),
}
Ok(())
}
async fn resolve_security_binding(
service: &AdminService,
workspace_id: &WorkspaceId,
@@ -0,0 +1,128 @@
use super::*;
/// Binds decisions to selected candidates with action-specific field rules:
/// unknown/duplicate keys, foreign fields and oversized references are
/// rejected before the plan is materialized.
pub(super) fn validate_plan_decisions<'input>(
decisions: &'input [OpenApiImportPlanDecision],
selected: &BTreeSet<String>,
) -> Result<BTreeMap<String, &'input OpenApiImportPlanDecision>, ApiError> {
let invalid = |context: Value| {
Err(ApiError::validation_with_context(
"import plan decision is invalid",
context,
))
};
let mut mapping = BTreeMap::new();
for decision in decisions {
if decision.operation_key.is_empty()
|| decision.operation_key.len() > MAX_IMPORT_PLAN_KEY_BYTES
{
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
if !matches!(
decision.action.as_str(),
"create" | "skip" | "rename" | "new_version"
) {
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
validate_decision_fields(decision)?;
if !selected.contains(&decision.operation_key) {
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_decision_unselected"
}));
}
if mapping
.insert(decision.operation_key.clone(), decision)
.is_some()
{
return invalid(json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
}));
}
}
Ok(mapping)
}
fn validate_decision_fields(decision: &OpenApiImportPlanDecision) -> Result<(), ApiError> {
let context = json!({
"operation_key": decision.operation_key,
"error_code": "import_plan_invalid_input"
});
let invalid = |message: &str| -> Result<(), ApiError> {
Err(ApiError::validation_with_context(message, context.clone()))
};
if decision
.target_operation_id
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
{
return invalid("decision target_operation_id is empty or too long");
}
if decision
.confirmed_name
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_NAME_BYTES)
{
return invalid("decision confirmed_name is empty or too long");
}
if decision
.auth_profile_id
.as_deref()
.is_some_and(|value| value.is_empty() || value.len() > MAX_IMPORT_PLAN_ID_REF_BYTES)
{
return invalid("decision auth_profile_id is empty or too long");
}
if decision
.security_alternative_index
.is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS)
{
return invalid("decision security_alternative_index is too large");
}
match decision.action.as_str() {
"create" => {
if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() {
return invalid(
"target_operation_id and confirmed_name are not allowed for action create",
);
}
}
"skip" => {
if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() {
return invalid("security binding is not allowed for action skip");
}
if decision.target_operation_id.is_none() {
return invalid("action skip requires the exact target_operation_id");
}
if decision.confirmed_name.is_some() {
return invalid("confirmed_name is not allowed for action skip");
}
}
"rename" => {
if decision.target_operation_id.is_some() {
return invalid("target_operation_id is not allowed for action rename");
}
if decision.confirmed_name.is_none() {
return invalid("action rename requires the exact confirmed_name");
}
}
"new_version" => {
if decision.target_operation_id.is_none() {
return invalid("action new_version requires the exact target_operation_id");
}
if decision.confirmed_name.is_some() {
return invalid("confirmed_name is not allowed for action new_version");
}
}
_ => unreachable!("decision action is validated before the field rules"),
}
Ok(())
}
@@ -4,6 +4,160 @@ use crank_registry::{ApplyImportPlanRequest, ImportPlanApplyReport};
const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128;
impl AdminService {
/// Reads an immutable plan and checks its current authority without Apply mutations.
pub async fn revalidate_openapi_import_plan(
&self,
workspace_id: &WorkspaceId,
plan_id: &ImportPlanId,
) -> Result<OpenApiImportPlanRevalidationResponse, ApiError> {
self.ensure_workspace_exists(workspace_id).await?;
let plan = self
.registry
.get_import_plan(workspace_id, plan_id)
.await?
.ok_or_else(|| {
ApiError::not_found_with_context(
"import plan was not found in this workspace",
json!({ "error_code": "import_plan_not_found" }),
)
})?;
let mut reason = None;
let now = OffsetDateTime::now_utc();
if plan.expires_at <= now {
reason = Some("import_plan_expired");
} else if let Some(job) = self
.registry
.get_import_job(workspace_id, &plan.job_id)
.await?
{
if job.kind != ImportJobKind::OpenApi
|| job.status != ImportJobStatus::Pending
|| job.expires_at <= now
{
reason = Some("import_plan_job_terminal");
} else {
match self
.resolve_verified_openapi_preview(workspace_id, &job, OpenApiUploadLocale::En)
.await
{
Ok(resolved) => {
let v2 = plan.payload.get("schema_version").and_then(Value::as_str)
== Some("import-plan-v2");
if self
.read_import_job_dependencies(
workspace_id,
&job.preview_payload,
OpenApiUploadLocale::En,
)
.await
.is_err()
{
reason = Some("import_dependency_verification_failed");
} else if !v2 {
// Historical plans stay readable for audit, but lack the
// catalog evidence required for current Apply authority.
reason = Some("importer_version_changed");
} else if resolved
.fingerprint
.get("normalizer_version")
.and_then(Value::as_str)
!= Some(NORMALIZER_VERSION)
|| resolved
.fingerprint
.get("projection_version")
.and_then(Value::as_str)
!= Some(PROJECTION_VERSION)
|| plan
.payload
.get("support_matrix_version")
.and_then(Value::as_str)
!= Some(OPENAPI_SUPPORT_MATRIX_VERSION)
{
reason = Some("importer_version_changed");
} else if self
.registry
.revalidate_import_plan(workspace_id, plan_id, &resolved.fingerprint)
.await
.is_err()
{
reason = Some("import_plan_revalidation_failed");
}
}
Err(_) => reason = Some("import_preview_revalidation_failed"),
}
}
} else {
reason = Some("import_plan_job_unavailable");
}
let counts = &plan.payload["counts"];
let count = |key: &str| {
counts
.get(key)
.and_then(Value::as_u64)
.and_then(|value| usize::try_from(value).ok())
.unwrap_or(0)
};
let selected = plan.payload["candidates"]
.as_array()
.into_iter()
.flatten()
.map(|candidate| {
let decision = &candidate["decision"];
let string = |value: &Value| value.as_str().map(str::to_owned);
OpenApiImportPlanCandidateView {
key: string(&candidate["key"]).unwrap_or_default(),
name: string(&candidate["name"]).unwrap_or_default(),
state: string(&candidate["state"]).unwrap_or_default(),
action: string(&decision["action"]),
target_operation_id: string(&decision["target_operation_id"]),
confirmed_name: string(&decision["confirmed_name"]),
auth_profile_id: string(&decision["auth_profile_id"]),
security_alternative_index: decision["security_alternative_index"]
.as_u64()
.and_then(|value| usize::try_from(value).ok()),
reason: string(&candidate["reason"]),
}
});
let excluded = plan
.payload
.pointer("/selection/excluded")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(Value::as_str)
.map(|key| OpenApiImportPlanCandidateView {
key: key.to_owned(),
name: key.to_owned(),
state: "excluded".to_owned(),
action: None,
target_operation_id: None,
confirmed_name: None,
auth_profile_id: None,
security_alternative_index: None,
reason: plan.payload["excluded_reasons"]
.as_array()
.and_then(|items| items.iter().find(|item| item["key"].as_str() == Some(key)))
.and_then(|item| item["reason"].as_str())
.map(str::to_owned)
.or_else(|| Some("not_selected".to_owned())),
});
Ok(OpenApiImportPlanRevalidationResponse {
plan_reference: plan.id.as_str().to_owned(),
plan_digest: plan.digest,
revalidation_status: if reason.is_some() { "stale" } else { "current" }.to_owned(),
revalidation_required: reason.is_some(),
reason: reason.map(str::to_owned),
counts: OpenApiImportPlanCounts {
selected: count("selected"),
excluded: count("excluded"),
actionable: count("actionable"),
blocked: count("blocked"),
},
candidates: selected.chain(excluded).collect(),
})
}
#[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), plan_id = %plan_id.as_str()))]
pub async fn apply_openapi_import_plan(
&self,
@@ -85,6 +239,13 @@ impl AdminService {
.get("projection_version")
.and_then(Value::as_str)
!= Some(PROJECTION_VERSION)
|| (plan.payload.get("schema_version").and_then(Value::as_str)
== Some("import-plan-v2")
&& plan
.payload
.get("support_matrix_version")
.and_then(Value::as_str)
!= Some(OPENAPI_SUPPORT_MATRIX_VERSION))
{
return Err(import_plan_repreview_error("importer_version_changed"));
}
@@ -209,6 +209,7 @@ fn import_replay_contract(
("normalized-ir-v2", "preview-v2")
| ("normalized-ir-v3", "preview-v3")
| ("normalized-ir-v4", "preview-v4")
| ("normalized-ir-v5", "preview-v5")
) {
return Err(ApiError::openapi_upload(locale, "source_integrity"));
}
@@ -13,6 +13,9 @@ mod external_references;
mod http_contract;
mod import_plan_apply;
mod import_plan_create;
mod large_preview;
mod upload_outcomes;
mod v5_replay;
#[tokio::test]
#[serial]
@@ -26,10 +26,17 @@ async fn http_contract_preview_apply_and_replay_preserve_json_multipart_schemas_
.unwrap()
.preview_payload["preview"]
.clone();
assert_eq!(
persisted_preview_before_apply,
serde_json::to_value(&preview.preview).unwrap()
);
// Readiness is workspace-aware response metadata, not part of the immutable
// projection persisted with the source job.
let mut response_projection = serde_json::to_value(&preview.preview).unwrap();
for group in response_projection["groups"].as_array_mut().unwrap() {
for operation in group["operations"].as_array_mut().unwrap() {
assert_eq!(operation["status"], "actionable");
operation.as_object_mut().unwrap().remove("status");
}
}
assert!(preview.preview.conflicts.is_empty());
assert_eq!(persisted_preview_before_apply, response_projection);
let operations = preview
.preview
@@ -987,3 +987,4 @@ async fn ac_4_corpus_operation_completes_the_real_http_to_mcp_workflow() {
mod corpus_hostile;
mod security_source;
mod support_matrix;
@@ -0,0 +1,106 @@
use super::*;
use admin_api::error::ApiError;
use admin_api::service::{
OpenApiImportPlanApplyPayload, OpenApiImportPlanCreatePayload, OpenApiUpload,
OpenApiUploadLocale,
};
use crank_core::WorkspaceId;
use crank_registry::{CreateImportPlanRequest, ImportPlanId};
#[tokio::test]
#[serial]
async fn older_v2_support_matrix_rejects_new_apply_but_keeps_plan_auditable() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_old_support_matrix_apply"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(
&workspace_id,
OpenApiUpload {
bytes: OPENAPI_SINGLE.as_bytes().to_vec(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
},
)
.await
.unwrap();
let job_id = preview.job_id.as_str().into();
let created = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
OpenApiImportPlanCreatePayload {
selection_mode: "full".to_owned(),
selected_operation_keys: Vec::new(),
server_url: None,
decisions: Vec::new(),
},
)
.await
.unwrap();
let original_id: ImportPlanId = created.plan_reference.as_str().into();
let original = registry
.get_import_plan(&workspace_id, &original_id)
.await
.unwrap()
.unwrap();
// Model an immutable plan saved under an earlier support matrix. The
// registry derives its valid semantic digest from the changed payload.
let old_id: ImportPlanId = "plan_older_support_matrix".into();
let mut old_payload = original.payload.clone();
old_payload["plan_reference"] = json!(old_id.as_str());
old_payload["support_matrix_version"] = json!("1.0.1");
let older = registry
.create_import_plan(CreateImportPlanRequest {
id: &old_id,
workspace_id: &workspace_id,
job_id: &original.job_id,
payload: &old_payload,
created_at: &original.created_at,
expires_at: &original.expires_at,
})
.await
.unwrap();
assert_ne!(older.digest, original.digest);
let audit = service
.revalidate_openapi_import_plan(&workspace_id, &old_id)
.await
.unwrap();
assert_eq!(audit.revalidation_status, "stale");
assert!(audit.revalidation_required);
assert_eq!(audit.reason.as_deref(), Some("importer_version_changed"));
let error = service
.apply_openapi_import_plan(
&workspace_id,
&old_id,
OpenApiImportPlanApplyPayload {
plan_digest: older.digest,
idempotency_key: "old-matrix-new-attempt".to_owned(),
},
)
.await
.expect_err("old support matrix must not authorize a new Apply");
match error {
ApiError::Conflict {
context: Some(context),
..
} => assert_eq!(context["error_code"], "importer_version_changed"),
other => panic!("expected typed re-preview conflict, got {other:?}"),
}
assert!(
service
.list_operations(&workspace_id)
.await
.unwrap()
.is_empty()
);
}
@@ -16,6 +16,134 @@ paths:
responses: { '200': { description: ok } }
"#;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn preview_name_conflicts_are_typed_and_deterministic() {
let registry = test_registry().await;
let upstream = spawn_upstream_server().await;
let service = test_service(
registry,
test_storage_root("openapi_preview_name_conflicts"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace = WorkspaceId::new("ws_default");
service
.create_operation(
&workspace,
test_operation_payload(&upstream, "duplicate_id"),
)
.await
.unwrap();
let document = r#"
openapi: 3.0.3
info: { title: Naming }
servers: [{ url: https://api.example.test }]
paths:
/one:
get:
operationId: duplicate_id
responses: { '200': { description: ok } }
/two:
get:
operationId: duplicate_id
responses: { '200': { description: ok } }
/bad:
get:
operationId: bad-id
responses: { '200': { description: ok } }
"#;
let first = service
.preview_openapi_import(&workspace, upload(document))
.await
.unwrap();
let repeated = service
.preview_openapi_import(&workspace, upload(document))
.await
.unwrap();
assert_eq!(first.preview.conflicts, repeated.preview.conflicts);
assert!(
first
.preview
.conflicts
.iter()
.any(|item| item.conflict_type == "duplicate_name")
);
assert!(
first
.preview
.conflicts
.iter()
.any(|item| item.conflict_type == "invalid_name")
);
assert!(
first
.preview
.conflicts
.iter()
.any(|item| item.conflict_type == "taken_name")
);
assert!(first.preview.conflicts.iter().all(|item| {
item.source_location
.as_ref()
.is_some_and(|location| location.pointer.starts_with("/paths/"))
&& !item.proposed_name.is_empty()
}));
assert_eq!(service.list_operations(&workspace).await.unwrap().len(), 1);
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn reopening_plan_is_read_only_and_reports_catalog_drift() {
let registry = test_registry().await;
let upstream = spawn_upstream_server().await;
let service = test_service(
registry,
test_storage_root("openapi_plan_reopen_catalog"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(&workspace, upload(OPENAPI_SINGLE))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let plan = service
.create_openapi_import_plan(&workspace, &job_id, plan_payload("full", vec![], vec![]))
.await
.unwrap();
let plan_id = plan.plan_reference.as_str().into();
let current = service
.revalidate_openapi_import_plan(&workspace, &plan_id)
.await
.unwrap();
assert_eq!(current.revalidation_status, "current");
assert!(!current.revalidation_required);
assert!(
service
.list_operations(&workspace)
.await
.unwrap()
.is_empty()
);
service
.create_operation(
&workspace,
test_operation_payload(&upstream, "unrelated_operation"),
)
.await
.unwrap();
let stale = service
.revalidate_openapi_import_plan(&workspace, &plan_id)
.await
.unwrap();
assert_eq!(stale.revalidation_status, "stale");
assert!(stale.revalidation_required);
assert_eq!(service.list_operations(&workspace).await.unwrap().len(), 1);
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation() {
@@ -31,6 +159,7 @@ async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation(
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE))
.await
.unwrap();
assert_eq!(preview.preview.groups[0].operations[0].status, "actionable");
let job_id: ImportJobId = preview.job_id.as_str().into();
let job = registry
.get_import_job(&workspace_id, &job_id)
@@ -77,6 +206,11 @@ async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation(
.unwrap()
.expect("the created plan must be readable in its workspace");
assert_eq!(stored.digest, plan.plan_digest);
assert_eq!(stored.payload["schema_version"], "import-plan-v2");
assert_eq!(stored.payload["catalog_revision"], plan.catalog_revision);
assert_eq!(stored.payload["support_matrix_version"], "1.0.2");
assert!(stored.payload["findings"]["document"].is_array());
assert!(stored.payload["findings"]["candidates"].is_array());
assert_eq!(stored.expires_at, expires_at);
assert_eq!(
stored.payload["selection"]["selected"],
@@ -98,6 +232,21 @@ async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation(
job.preview_payload["source"]["digest"]
);
assert!(stored.payload["fingerprint"]["ir_fingerprint"].is_string());
let repeated = service
.create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![]))
.await
.unwrap();
assert_ne!(repeated.plan_reference, plan.plan_reference);
assert_eq!(repeated.plan_digest, plan.plan_digest);
let repeated_stored = registry
.get_import_plan(&workspace_id, &repeated.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert_ne!(
repeated_stored.payload["candidates"][0]["operation"]["id"],
stored.payload["candidates"][0]["operation"]["id"]
);
// Planning is non-mutating: no operation exists and the job stays pending.
assert!(
service
@@ -154,6 +303,18 @@ async fn partial_plan_counts_selection_and_rejects_invalid_request_material() {
.expect("the excluded candidate is listed for confirmation");
assert_eq!(excluded.state, "excluded");
assert_eq!(excluded.reason.as_deref(), Some("not_selected"));
let stored = registry
.get_import_plan(&workspace_id, &plan.plan_reference.as_str().into())
.await
.unwrap()
.unwrap();
assert_eq!(
stored.payload["excluded_reasons"],
json!([{
"key": second,
"reason": "not_selected",
}])
);
let stored = registry
.get_import_plan(&workspace_id, &plan.plan_reference.as_str().into())
@@ -351,7 +512,7 @@ async fn client_supplied_candidate_material_has_no_authority() {
stored.payload["selection"]["all"],
json!(["GET /v2/latest"])
);
assert_eq!(stored.payload["schema_version"], "import-plan-v1");
assert_eq!(stored.payload["schema_version"], "import-plan-v2");
assert!(stored.digest.len() == 64 && stored.digest.bytes().all(|b| b.is_ascii_hexdigit()));
}
@@ -403,6 +564,11 @@ async fn plan_creation_is_workspace_scoped() {
.await
.unwrap();
assert!(stored.is_none(), "a foreign plan id must be invisible");
let reopen_error = service
.revalidate_openapi_import_plan(&secondary, &plan.plan_reference.as_str().into())
.await
.expect_err("a foreign plan cannot be reopened");
assert_http_status(reopen_error, axum::http::StatusCode::NOT_FOUND);
}
#[tokio::test(flavor = "multi_thread")]
@@ -161,6 +161,7 @@ fn assert_http_status(error: ApiError, expected: axum::http::StatusCode) {
}
mod creation;
mod route_get;
pub(super) use creation::OPENAPI_SINGLE_SECURITY;
mod security_availability;
mod security_binding;
@@ -0,0 +1,87 @@
use super::*;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn persisted_plan_get_requires_owner_and_reports_catalog_drift() {
let registry = test_registry().await;
let storage_root = test_storage_root("openapi_import_plan_get_route");
let service = test_service(
registry.clone(),
storage_root.clone(),
test_auth_settings(),
test_secret_crypto(),
);
let workspace = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(&workspace, upload(OPENAPI_SINGLE))
.await
.unwrap();
let job_id: ImportJobId = preview.job_id.as_str().into();
let plan = service
.create_openapi_import_plan(&workspace, &job_id, plan_payload("full", vec![], vec![]))
.await
.unwrap();
let plan_id = plan.plan_reference.clone();
let plan_digest = plan.plan_digest.clone();
let foreign_workspace = WorkspaceId::new("ws_plan_get_foreign");
let now = OffsetDateTime::now_utc();
registry
.create_workspace(CreateWorkspaceRequest {
workspace: &Workspace {
id: foreign_workspace.clone(),
slug: "ws-plan-get-foreign".to_owned(),
display_name: "Foreign plan workspace".to_owned(),
status: WorkspaceStatus::Active,
settings: json!({}),
created_at: now,
updated_at: now,
},
})
.await
.unwrap();
let server = spawn_admin_api(build_test_app(registry, storage_root)).await;
let client = authorized_client(&server).await;
let url = format!("{server}/imports/openapi/plans/{plan_id}");
let anonymous = reqwest::Client::new().get(&url).send().await.unwrap();
assert_eq!(anonymous.status(), reqwest::StatusCode::UNAUTHORIZED);
let foreign = client
.get(url.replace("ws_default", foreign_workspace.as_str()))
.send()
.await
.unwrap();
assert_eq!(foreign.status(), reqwest::StatusCode::FORBIDDEN);
let current = client.get(&url).send().await.unwrap();
assert_eq!(current.status(), reqwest::StatusCode::OK);
let current: Value = current.json().await.unwrap();
assert_eq!(current["plan_reference"], plan_id);
assert_eq!(current["plan_digest"], plan_digest);
assert_eq!(current["revalidation_status"], "current");
assert_eq!(current["revalidation_required"], false);
assert!(
service
.list_operations(&workspace)
.await
.unwrap()
.is_empty()
);
let upstream = spawn_upstream_server().await;
service
.create_operation(
&workspace,
test_operation_payload(&upstream, "plan_get_catalog_drift"),
)
.await
.unwrap();
let stale = client.get(&url).send().await.unwrap();
assert_eq!(stale.status(), reqwest::StatusCode::OK);
let stale: Value = stale.json().await.unwrap();
assert_eq!(stale["plan_reference"], plan_id);
assert_eq!(stale["plan_digest"], plan_digest);
assert_eq!(stale["revalidation_status"], "stale");
assert_eq!(stale["revalidation_required"], true);
assert_eq!(service.list_operations(&workspace).await.unwrap().len(), 1);
}
@@ -119,6 +119,7 @@ async fn auth_profile_references_are_validated_stored_and_inherited() {
.preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_SECURITY))
.await
.unwrap();
assert_eq!(preview.preview.groups[0].operations[0].status, "unresolved");
let selectable = preview
.security_profile_choices
.iter()
@@ -648,7 +649,7 @@ async fn basic_and_query_profiles_bind_to_matching_security_alternatives_and_app
plan_payload(
"partial",
vec!["GET /v2/query".to_owned()],
vec![query_decision],
vec![query_decision.clone()],
),
)
.await
@@ -664,7 +665,7 @@ async fn basic_and_query_profiles_bind_to_matching_security_alternatives_and_app
Some(query_profile.id.as_str())
);
for (plan, expected_profile, idempotency_key) in [
for (index, (plan, expected_profile, idempotency_key)) in [
(
&basic_plan,
basic_profile.id.as_str(),
@@ -675,7 +676,44 @@ async fn basic_and_query_profiles_bind_to_matching_security_alternatives_and_app
query_profile.id.as_str(),
"apply-query-profile",
),
] {
]
.into_iter()
.enumerate()
{
let fresh_plan;
let plan = if index == 1 {
let stale = service
.apply_openapi_import_plan(
&workspace_id,
&crank_registry::ImportPlanId::new(plan.plan_reference.clone()),
admin_api::service::OpenApiImportPlanApplyPayload {
plan_digest: plan.plan_digest.clone(),
idempotency_key: "stale-query-profile".to_owned(),
},
)
.await
.expect_err("the earlier query plan is stale after catalog mutation");
assert_error_code(&stale, "import_plan_fingerprint_stale");
let refreshed = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_BASIC_AND_QUERY_SECURITY))
.await
.unwrap();
fresh_plan = service
.create_openapi_import_plan(
&workspace_id,
&refreshed.job_id.as_str().into(),
plan_payload(
"partial",
vec!["GET /v2/query".to_owned()],
vec![query_decision.clone()],
),
)
.await
.unwrap();
&fresh_plan
} else {
plan
};
let report = service
.apply_openapi_import_plan(
&workspace_id,
@@ -1,5 +1,80 @@
use super::*;
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn preview_rejects_candidate_2001_without_truncation_or_operations() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_preview_candidate_limit"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace = WorkspaceId::new("ws_default");
let mut document = String::from(
"openapi: 3.0.3\ninfo: { title: Limit }\nservers: [{ url: https://api.example.test }]\npaths:\n",
);
for index in 0..2001 {
document.push_str(&format!(" /p{index}:\n get:\n operationId: op{index}\n responses: {{ '200': {{ description: ok }} }}\n"));
}
let error = service
.preview_openapi_import(&workspace, upload(&document))
.await
.expect_err("candidate 2001 must be rejected as a whole");
assert_error_code(&error, "openapi_preview_too_many_candidates");
assert!(
service
.list_operations(&workspace)
.await
.unwrap()
.is_empty()
);
let active_sources: i64 = sqlx::query_scalar(
"select count(*) from artifact_sources where workspace_id = $1 and lifecycle = 'active'",
)
.bind(workspace.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(active_sources, 0);
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn partial_plan_rejects_selected_blocker_before_persistence() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_plan_partial_blocker"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(&workspace_id, upload(OPENAPI_GLOBAL_BLOCKER))
.await
.unwrap();
assert_eq!(preview.preview.groups[0].operations[0].status, "blocked");
let job_id: ImportJobId = preview.job_id.as_str().into();
let key = preview.preview.groups[0].operations[0].key.clone();
let error = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
plan_payload("partial", vec![key], vec![]),
)
.await
.expect_err("blocked partial selection must be rejected");
assert_error_code(&error, "import_plan_partial_blocked");
assert!(
service
.list_operations(&workspace_id)
.await
.unwrap()
.is_empty()
);
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn explicit_targets_are_checked_even_when_the_candidate_is_blocked() {
@@ -0,0 +1,142 @@
use super::*;
use admin_api::service::OpenApiImportPlanCreatePayload;
use std::fmt::Write as _;
const OPERATION_COUNT: usize = 2_000;
const MAX_UPLOAD_BYTES: usize = 256 * 1024;
const MAX_STORED_PLAN_BYTES: usize = 16 * 1024 * 1024;
fn large_openapi_document() -> String {
let mut document = String::from(
"openapi: 3.0.3\n\
info: { title: Large preview, version: '1' }\n\
servers: [{ url: 'https://api.example.test' }]\n\
paths:\n",
);
for index in 0..OPERATION_COUNT {
writeln!(
document,
" /p{index:04}:\n get:\n operationId: o{index:04}\n responses: {{ '200': {{ description: ok }} }}"
)
.unwrap();
}
document
}
#[tokio::test]
#[serial]
async fn previews_and_plans_two_thousand_operations_without_catalog_mutation() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_large_preview"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let document = large_openapi_document();
assert!(
document.len() <= MAX_UPLOAD_BYTES,
"generated document is {} bytes, over the 256 KiB upload limit",
document.len()
);
let preview = service
.preview_openapi_import(
&workspace_id,
OpenApiUpload {
bytes: document.into_bytes(),
mime_type: "application/yaml".to_owned(),
locale: OpenApiUploadLocale::En,
},
)
.await
.expect("valid 2,000-operation OpenAPI document should preview");
let preview_candidates = preview
.preview
.groups
.iter()
.flat_map(|group| &group.operations)
.collect::<Vec<_>>();
assert_eq!(preview_candidates.len(), OPERATION_COUNT);
assert_eq!(preview_candidates[0].key, "GET /p0000");
assert_eq!(preview_candidates[OPERATION_COUNT - 1].key, "GET /p1999");
assert!(preview_candidates.iter().all(|candidate| {
candidate.status == "actionable"
&& candidate.method == crank_core::HttpMethod::Get
&& candidate.path.starts_with("/p")
}));
let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into();
let plan = service
.create_openapi_import_plan(
&workspace_id,
&job_id,
OpenApiImportPlanCreatePayload {
selection_mode: "full".to_owned(),
selected_operation_keys: Vec::new(),
server_url: None,
decisions: Vec::new(),
},
)
.await
.expect("full plan should include all 2,000 operations");
assert_eq!(plan.counts.selected, OPERATION_COUNT);
assert_eq!(plan.counts.actionable, OPERATION_COUNT);
assert_eq!(plan.counts.excluded, 0);
assert_eq!(plan.counts.blocked, 0);
assert_eq!(plan.candidates.len(), OPERATION_COUNT);
assert!(plan.candidates.iter().all(|candidate| {
candidate.state == "actionable" && candidate.action.as_deref() == Some("create")
}));
let plan_id = plan.plan_reference.as_str().into();
let stored = registry
.get_import_plan(&workspace_id, &plan_id)
.await
.unwrap()
.expect("full plan should be persisted and readable");
let stored_payload_bytes = serde_json::to_vec(&stored.payload).unwrap().len();
assert!(
stored_payload_bytes <= MAX_STORED_PLAN_BYTES,
"stored plan payload is {stored_payload_bytes} bytes, over 16 MiB"
);
let stored_candidates = stored.payload["candidates"]
.as_array()
.expect("stored plan contains its full candidate list");
assert_eq!(stored_candidates.len(), OPERATION_COUNT);
assert!(stored_candidates.iter().all(|candidate| {
candidate["state"] == "actionable" && candidate["decision"]["action"] == "create"
}));
assert_eq!(stored.payload["counts"]["selected"], OPERATION_COUNT);
assert_eq!(stored.payload["counts"]["actionable"], OPERATION_COUNT);
let revalidated = service
.revalidate_openapi_import_plan(&workspace_id, &plan_id)
.await
.expect("revalidating the unchanged plan should be read-only and current");
assert_eq!(revalidated.revalidation_status, "current");
assert!(!revalidated.revalidation_required);
assert_eq!(revalidated.counts.selected, OPERATION_COUNT);
assert_eq!(revalidated.counts.actionable, OPERATION_COUNT);
assert_eq!(revalidated.counts.excluded, 0);
assert_eq!(revalidated.counts.blocked, 0);
assert_eq!(revalidated.candidates.len(), OPERATION_COUNT);
let (operations, operation_versions) = sqlx::query_as::<_, (i64, i64)>(
"select
(select count(*) from operations where workspace_id = $1),
(select count(*) from operation_versions ov
join operations o on o.id = ov.operation_id where o.workspace_id = $1)",
)
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
assert_eq!(operations, 0, "preview and plan must not create operations");
assert_eq!(
operation_versions, 0,
"preview and plan must not create operation versions"
);
}
@@ -0,0 +1,140 @@
use super::*;
use admin_api::error::ApiError;
use crank_artifacts::MAX_ARTIFACT_BYTES;
use crank_core::HttpMethod;
#[derive(Debug, PartialEq, Eq)]
struct CatalogCounts {
operations: i64,
operation_versions: i64,
auth_profiles: i64,
agent_operation_bindings: i64,
}
async fn catalog_counts(
registry: &crank_registry::PostgresRegistry,
workspace_id: &WorkspaceId,
) -> CatalogCounts {
let (operations, operation_versions, auth_profiles, agent_operation_bindings) =
sqlx::query_as::<_, (i64, i64, i64, i64)>(
"select
(select count(*) from operations where workspace_id = $1),
(select count(*) from operation_versions ov
join operations o on o.id = ov.operation_id where o.workspace_id = $1),
(select count(*) from auth_profiles where workspace_id = $1),
(select count(*) from agent_operation_bindings b
join operations o on o.id = b.operation_id where o.workspace_id = $1)",
)
.bind(workspace_id.as_str())
.fetch_one(registry.pool())
.await
.unwrap();
CatalogCounts {
operations,
operation_versions,
auth_profiles,
agent_operation_bindings,
}
}
fn upload(bytes: Vec<u8>) -> OpenApiUpload {
OpenApiUpload {
bytes,
mime_type: "application/json".to_owned(),
locale: OpenApiUploadLocale::En,
}
}
fn assert_upload_error(error: ApiError, variant: &str, code: &str) {
let context = match (variant, error) {
("validation", ApiError::Validation { context, .. }) => context,
("payload_too_large", ApiError::PayloadTooLarge { context, .. }) => context,
(expected, actual) => panic!("expected {expected} error, got {actual:?}"),
}
.expect("OpenAPI upload errors carry a machine-readable code");
assert_eq!(context["error_code"], format!("openapi_upload.{code}"));
}
#[tokio::test]
#[serial]
async fn preview_outcomes_do_not_mutate_operation_catalog() {
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_upload_outcomes"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let before = catalog_counts(&registry, &workspace_id).await;
let preview = service
.preview_openapi_import(
&workspace_id,
upload(
br#"{
"openapi":"3.0.3",
"info":{"title":"Upload outcomes","version":"1"},
"paths":{"/ready":{"get":{"responses":{"200":{"description":"ok"}}}}}
}"#
.to_vec(),
),
)
.await
.expect("valid OpenAPI document should return a preview");
let candidates = preview
.preview
.groups
.iter()
.flat_map(|group| &group.operations)
.collect::<Vec<_>>();
assert_eq!(candidates.len(), 1);
assert_eq!(candidates[0].method, HttpMethod::Get);
assert_eq!(candidates[0].path, "/ready");
assert_eq!(candidates[0].key, "GET /ready");
assert_eq!(catalog_counts(&registry, &workspace_id).await, before);
let before = catalog_counts(&registry, &workspace_id).await;
let error = service
.preview_openapi_import(&workspace_id, upload(Vec::new()))
.await
.expect_err("empty upload should be rejected");
assert_upload_error(error, "validation", "empty_file");
assert_eq!(catalog_counts(&registry, &workspace_id).await, before);
let before = catalog_counts(&registry, &workspace_id).await;
let error = service
.preview_openapi_import(&workspace_id, upload(b"{".to_vec()))
.await
.expect_err("malformed OpenAPI document should be rejected");
assert_upload_error(error, "validation", "invalid_document");
assert_eq!(catalog_counts(&registry, &workspace_id).await, before);
let before = catalog_counts(&registry, &workspace_id).await;
let error = service
.preview_openapi_import(&workspace_id, upload(vec![b' '; MAX_ARTIFACT_BYTES + 1]))
.await
.expect_err("oversized upload should be rejected");
assert_upload_error(error, "payload_too_large", "file_too_large");
assert_eq!(catalog_counts(&registry, &workspace_id).await, before);
let before = catalog_counts(&registry, &workspace_id).await;
let error = service
.preview_openapi_import(
&workspace_id,
upload(
br#"{
"openapi":"3.0.3",
"info":{"title":"No methods","version":"1"},
"paths":{}
}"#
.to_vec(),
),
)
.await
.expect_err("document without supported methods should be rejected");
assert_upload_error(error, "validation", "no_methods");
assert_eq!(catalog_counts(&registry, &workspace_id).await, before);
}
@@ -0,0 +1,93 @@
use super::*;
#[tokio::test]
#[serial]
async fn exact_v5_contract_replays_saved_preview_without_source_locations() {
use sha2::{Digest, Sha256};
fn remove_source_locations(value: &mut serde_json::Value) {
match value {
serde_json::Value::Object(object) => {
object.remove("source_location");
for value in object.values_mut() {
remove_source_locations(value);
}
}
serde_json::Value::Array(values) => {
for value in values {
remove_source_locations(value);
}
}
_ => {}
}
}
let registry = test_registry().await;
let service = test_service(
registry.clone(),
test_storage_root("openapi_import_v5_compatibility"),
test_auth_settings(),
test_secret_crypto(),
);
let workspace_id = WorkspaceId::new("ws_default");
let preview = service
.preview_openapi_import(&workspace_id, openapi_upload())
.await
.unwrap();
let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into();
let mut payload = registry
.get_import_job(&workspace_id, &job_id)
.await
.unwrap()
.unwrap()
.preview_payload;
// A preview persisted under v5 has the then-current projection and digest,
// without the source_location fields added by v6.
let persisted_preview = payload
.get_mut("preview")
.expect("newly saved job includes its preview");
assert!(persisted_preview.to_string().contains("source_location"));
remove_source_locations(persisted_preview);
let digest = format!(
"{:x}",
Sha256::digest(serde_json::to_vec(persisted_preview).unwrap())
);
payload["normalization"]["normalizer_version"] = "normalized-ir-v5".into();
payload["normalization"]["projection_version"] = "preview-v5".into();
payload["normalization"]["config"]["normalizer_version"] = "normalized-ir-v5".into();
payload["normalization"]["config"]["projection_version"] = "preview-v5".into();
payload["preview_digest"] = digest.into();
// If v5 is reparsed, this deliberately absent dependency makes the replay fail.
payload["dependency_snapshots"] = serde_json::json!([{
"source_id": "src_openapi_dep_v5_must_not_be_read",
"digest": format!("sha256:{}", "0".repeat(64)),
"canonical_uri": "https://schemas.example.test/v5.yaml"
}]);
sqlx::query("update import_jobs set preview_payload = $1::jsonb where id = $2")
.bind(serde_json::to_string(&payload).unwrap())
.bind(job_id.as_str())
.execute(registry.pool())
.await
.unwrap();
let result = service
.create_openapi_import(
&workspace_id,
&job_id,
OpenApiImportCreatePayload {
selected_operation_keys: vec!["GET /v2/latest".to_owned()],
server_url: Some("https://api.frankfurter.dev".to_owned()),
conflict_mode: "skip".to_owned(),
},
)
.await
.unwrap();
assert_eq!(result.created.len(), 1);
let completed = registry
.get_import_job(&workspace_id, &job_id)
.await
.unwrap()
.unwrap();
assert_eq!(completed.status, ImportJobStatus::Completed);
}