diff --git a/Cargo.lock b/Cargo.lock index 9457f57..a51c073 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -221,9 +221,9 @@ checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" [[package]] name = "aws-lc-rs" -version = "1.17.3" +version = "1.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00bdb5da18dac48ca2cc7cd4a98e533e8635a58e2361d13a1a4ee3888e0d72f1" +checksum = "b281d307588d634de920874890732659e2e7672f72b5e10e81badc1a8a83621e" dependencies = [ "aws-lc-sys", "zeroize", @@ -231,9 +231,9 @@ dependencies = [ [[package]] name = "aws-lc-sys" -version = "0.43.0" +version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43103168cc76fe62678a375e722fc9cb3a0146159ac5828bc4f0dfd755c2224c" +checksum = "9bff6c3b54fad79a2e60b8102caf565819711497c1f5f092f49508e2f5c31b27" dependencies = [ "cc", "cmake", @@ -1142,7 +1142,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -2671,7 +2671,7 @@ dependencies = [ "once_cell", "socket2 0.5.10", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -3010,14 +3010,14 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] name = "rustls" -version = "0.23.41" +version = "0.23.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b92b125634d9b795e7beca796cc790df15a7fb38323bf3196fda83292d06b1f" +checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" dependencies = [ "aws-lc-rs", "log", @@ -3069,7 +3069,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -3080,9 +3080,9 @@ checksum = "f87165f0995f63a9fbeea62b64d10b4d9d8e78ec6d7d51fb2125fda7bb36788f" [[package]] name = "rustls-webpki" -version = "0.103.13" +version = "0.103.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" dependencies = [ "aws-lc-rs", "ring", @@ -4473,7 +4473,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/apps/admin-api/src/app.rs b/apps/admin-api/src/app.rs index d54bc84..95b1324 100644 --- a/apps/admin-api/src/app.rs +++ b/apps/admin-api/src/app.rs @@ -27,7 +27,10 @@ use crate::{ }, auth_profiles::{create_auth_profile, get_auth_profile, list_auth_profiles}, capabilities::get_capabilities, - imports::{create_openapi_import, preview_openapi_import}, + imports::{ + apply_openapi_import_plan, create_openapi_import, create_openapi_import_plan, + preview_openapi_import, + }, observability::{ approve_approval, deny_approval, export_logs_csv, export_usage_csv, get_agent_usage, get_approval, get_log, get_operation_usage, get_usage, list_approvals, list_logs, @@ -58,6 +61,14 @@ pub fn build_app(state: AppState) -> Router { "/imports/openapi/{job_id}/create", post(create_openapi_import), ) + .route( + "/imports/openapi/{job_id}/plans", + post(create_openapi_import_plan), + ) + .route( + "/imports/openapi/{plan_id}/apply", + post(apply_openapi_import_plan).layer(DefaultBodyLimit::max(16 * 1024)), + ) .route( "/operations/analyze-quality", post(analyze_operation_quality), diff --git a/apps/admin-api/src/dto.rs b/apps/admin-api/src/dto.rs index 4f22489..a71cacc 100644 --- a/apps/admin-api/src/dto.rs +++ b/apps/admin-api/src/dto.rs @@ -575,6 +575,73 @@ pub struct OpenApiImportSkippedOperation { pub reason: String, } +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct OpenApiImportPlanCreatePayload { + #[serde(default = "default_openapi_plan_selection_mode")] + pub selection_mode: String, + #[serde(default)] + pub selected_operation_keys: Vec, + #[serde(default)] + pub server_url: Option, + #[serde(default)] + pub decisions: Vec, +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct OpenApiImportPlanApplyPayload { + pub plan_digest: String, + pub idempotency_key: String, +} + +fn default_openapi_plan_selection_mode() -> String { + "full".to_owned() +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct OpenApiImportPlanDecision { + pub operation_key: String, + pub action: String, + #[serde(default)] + pub target_operation_id: Option, + #[serde(default)] + pub confirmed_name: Option, + #[serde(default)] + pub auth_profile_id: Option, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize)] +pub struct OpenApiImportPlanCounts { + pub selected: usize, + pub excluded: usize, + pub actionable: usize, + pub blocked: usize, +} + +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct OpenApiImportPlanCandidateView { + pub key: String, + pub name: String, + pub state: String, + pub action: Option, + pub target_operation_id: Option, + pub confirmed_name: Option, + pub auth_profile_id: Option, + pub reason: Option, +} + +#[derive(Clone, Debug, Serialize)] +pub struct OpenApiImportPlanCreateResponse { + pub plan_reference: String, + pub plan_digest: String, + pub expires_at: String, + pub selection_mode: String, + pub counts: OpenApiImportPlanCounts, + pub candidates: Vec, +} + #[derive(Clone, Debug, Serialize)] pub struct CreatedOperationResponse { pub operation_id: String, diff --git a/apps/admin-api/src/error.rs b/apps/admin-api/src/error.rs index cbc4bc1..eb7ed85 100644 --- a/apps/admin-api/src/error.rs +++ b/apps/admin-api/src/error.rs @@ -687,6 +687,94 @@ impl From for ApiError { format!("import job {job_id} was already applied with different parameters"), json!({ "job_id": job_id }), ), + RegistryError::InvalidImportPlan { field } => Self::validation_with_context( + format!("invalid import plan input for field {field}"), + json!({ + "field": field, + "error_code": "import_plan_invalid_input" + }), + ), + RegistryError::ImportPlanConflict { plan_id } => Self::conflict_with_context( + format!("import plan {plan_id} already exists with different material"), + json!({ + "plan_id": plan_id, + "error_code": "import_plan_conflict", + "recovery": "repreview_required" + }), + ), + RegistryError::ImportPlanJobUnavailable { job_id } => Self::not_found_with_context( + format!("import job {job_id} is unavailable for planning"), + json!({ "job_id": job_id, "error_code": "import_plan_job_unavailable" }), + ), + RegistryError::ImportPlanJobTerminal { job_id } + | RegistryError::ImportPlanJobExpired { job_id } => Self::conflict_with_context( + format!("import job {job_id} is not in planning state"), + json!({ + "job_id": job_id, + "error_code": "import_plan_job_terminal", + "recovery": "repreview_required", + "repreview_required": true + }), + ), + RegistryError::ImportPlanNotFound { .. } => Self::not_found_with_context( + "import plan was not found in this workspace", + json!({ "error_code": "import_plan_not_found" }), + ), + RegistryError::ImportPlanExpired { .. } => Self::conflict_with_context( + "import plan has expired; create a fresh preview", + json!({ + "error_code": "import_plan_expired", + "recovery": "repreview_required", + "repreview_required": true + }), + ), + RegistryError::ImportPlanPayloadCorrupt { .. } => Self::conflict_with_context( + "import plan is invalid; create a fresh preview", + json!({ + "error_code": "import_plan_payload_invalid", + "recovery": "repreview_required", + "repreview_required": true + }), + ), + RegistryError::ImportPlanFingerprintStale { .. } => Self::conflict_with_context( + "import plan no longer matches the verified preview; create a fresh preview", + json!({ + "error_code": "import_plan_fingerprint_stale", + "recovery": "repreview_required", + "repreview_required": true + }), + ), + RegistryError::ImportPlanKeyDigestConflict { .. } => Self::conflict_with_context( + "idempotency key was already committed for different plan material", + json!({ + "error_code": "import_plan_key_digest_conflict", + "recovery": "repreview_required", + "repreview_required": true + }), + ), + RegistryError::ImportPlanNameConflict { .. } => Self::conflict_with_context( + "an operation name changed after the plan was created", + json!({ + "error_code": "import_plan_name_conflict", + "recovery": "confirm_skip_rename_or_new_version" + }), + ), + RegistryError::ImportPlanTargetStale { .. } => Self::conflict_with_context( + "an operation changed after the plan was created; create a fresh preview", + json!({ + "error_code": "import_plan_target_stale", + "recovery": "repreview_required", + "repreview_required": true + }), + ), + RegistryError::ImportPlanAuthUnavailable { .. } => Self::conflict_with_context( + "an authentication profile is unavailable; create a fresh preview", + json!({ + "error_code": "import_plan_auth_unavailable", + "recovery": "repreview_required", + "repreview_required": true + }), + ), RegistryError::SourceNotFound { .. } => Self::not_found_with_context( "artifact source was not found", json!({ diff --git a/apps/admin-api/src/routes/imports.rs b/apps/admin-api/src/routes/imports.rs index ac1f50d..5b39c45 100644 --- a/apps/admin-api/src/routes/imports.rs +++ b/apps/admin-api/src/routes/imports.rs @@ -10,7 +10,10 @@ use serde_json::{Value, json}; use crate::{ error::ApiError, - service::{OpenApiImportCreatePayload, OpenApiUpload, OpenApiUploadLocale}, + service::{ + OpenApiImportCreatePayload, OpenApiImportPlanApplyPayload, OpenApiImportPlanCreatePayload, + OpenApiUpload, OpenApiUploadLocale, + }, state::AppState, }; @@ -25,6 +28,12 @@ pub struct WorkspaceImportPath { pub job_id: String, } +#[derive(Deserialize)] +pub struct WorkspacePlanPath { + pub workspace_id: String, + pub plan_id: String, +} + pub async fn preview_openapi_import( Path(path): Path, State(state): State, @@ -198,6 +207,49 @@ pub async fn create_openapi_import( Ok(Json(json!(imported))) } +pub async fn create_openapi_import_plan( + Path(path): Path, + State(state): State, + Json(payload): Json, +) -> Result, ApiError> { + let plan = state + .service + .create_openapi_import_plan( + &path.workspace_id.as_str().into(), + &path.job_id.as_str().into(), + payload, + ) + .await?; + Ok(Json(json!(plan))) +} + +pub async fn apply_openapi_import_plan( + Path(path): Path, + State(state): State, + Json(payload): Json, +) -> Result { + let report = state + .service + .apply_openapi_import_plan( + &path.workspace_id.as_str().into(), + &path.plan_id.as_str().into(), + payload, + ) + .await?; + let status = if report.status == crank_registry::ImportPlanApplyStatus::Rejected { + axum::http::StatusCode::CONFLICT + } else { + axum::http::StatusCode::OK + }; + Ok(( + status, + Json( + serde_json::to_value(report) + .map_err(|_| ApiError::internal("could not serialize import plan report"))?, + ), + )) +} + #[cfg(test)] mod tests { use axum::http::{HeaderMap, HeaderValue}; diff --git a/apps/admin-api/src/service/imports.rs b/apps/admin-api/src/service/imports.rs index 835f5c2..cd0e95b 100644 --- a/apps/admin-api/src/service/imports.rs +++ b/apps/admin-api/src/service/imports.rs @@ -2,8 +2,8 @@ use std::collections::{BTreeMap, BTreeSet}; use crank_artifacts::{ArtifactError, MAX_ARTIFACT_BYTES}; use crank_core::{ - ExecutionConfig, OperationSecurityLevel, Protocol, ToolQualityFinding, ToolQualitySeverity, - WorkspaceId, + AuthProfileId, ExecutionConfig, OperationId, OperationSecurityLevel, OperationStatus, Protocol, + ToolQualityFinding, ToolQualitySeverity, WorkspaceId, }; use crank_import::rest::{ ImportFinding, ImportFindingSeverity, ImportOperationCandidate, NORMALIZER_VERSION, @@ -11,11 +11,12 @@ use crank_import::rest::{ }; use crank_registry::{ ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity, - CreateArtifactSourceRequest, CreateImportJobRequest, FinishImportJobRequest, - ImportConflictMode, ImportJobApplyResult, ImportJobId, ImportJobKind, ImportJobSourceEnvelope, - ImportJobStatus, ImportOperationDraft, RegistryError, + CreateArtifactSourceRequest, CreateImportJobRequest, CreateImportPlanRequest, + FinishImportJobRequest, ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobId, + ImportJobKind, ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, ImportPlanId, + RegistryError, RegistryOperation, }; -use serde_json::json; +use serde_json::{Value, json}; use sha2::{Digest, Sha256}; use time::{Duration, OffsetDateTime, format_description::well_known::Rfc3339}; use tracing::{info, instrument}; @@ -24,21 +25,38 @@ use crate::{ error::ApiError, service::{ AdminService, OpenApiImportCreatePayload, OpenApiImportCreateResponse, - OpenApiImportCreatedOperation, OpenApiImportPreviewResponse, OpenApiImportSkippedOperation, - OpenApiUpload, OpenApiUploadLocale, OperationPayload, new_prefixed_id, + OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload, + OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload, + OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision, OpenApiImportPreviewResponse, + OpenApiImportSkippedOperation, OpenApiUpload, OpenApiUploadLocale, OperationPayload, + new_prefixed_id, }, }; mod external_references; +mod import_plan; mod job_contract; +mod plan_apply; +mod verified_preview; + use external_references::ImportReplayContext; +use import_plan::import_plan_repreview_error; use job_contract::{ SourceDetachGuard, import_job_dependencies, import_job_normalization_config, import_job_source, }; +use verified_preview::{ + ImportPreviewResolution, artifact_error, openapi_source_error, parse_verified_preview, + preview_digest, preview_has_blocker, +}; 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_KEY_BYTES: usize = 4_096; +const MAX_IMPORT_PLAN_SERVER_URL_BYTES: usize = 8_192; +const MAX_IMPORT_PLAN_NAME_BYTES: usize = 256; +const MAX_IMPORT_PLAN_ID_REF_BYTES: usize = 128; static OPENAPI_PARSE_SLOTS: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(OPENAPI_PARSE_CONCURRENCY); @@ -276,141 +294,20 @@ impl AdminService { return Ok(openapi_import_response(applied)); } - let source = match import_job_source(&job.preview_payload, locale) { - Ok(source) => source, - Err(error) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_source_verification_failed", - error, - ) - .await; - } - }; - let verified = match self - .registry - .read_artifact_source( - std::sync::Arc::clone(&self.artifact_store), - workspace_id, - &source.source_id, - ) + let resolved_preview = match self + .resolve_verified_openapi_preview(workspace_id, &job, locale) .await { - Ok(verified) => verified, - Err( - error @ (RegistryError::SourceNotFound { .. } | RegistryError::SourceUnavailable), - ) => { + Ok(resolved) => resolved, + Err(ImportPreviewResolution::Fatal(error_code, error)) => { return self - .fail_openapi_import_or_replay( - &replay_context, - "import_source_verification_failed", - openapi_source_error(locale, error), - ) + .fail_openapi_import_or_replay(&replay_context, error_code, error) .await; } - Err(error @ RegistryError::SourceIntegrity) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_source_verification_failed", - openapi_source_error(locale, error), - ) - .await; - } - Err(error) => return Err(openapi_source_error(locale, error)), + Err(ImportPreviewResolution::NonFatal(error)) => return Err(error), }; - if verified.source.blob.artifact_ref != source.digest { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_source_verification_failed", - ApiError::openapi_upload(locale, "source_integrity"), - ) - .await; - } - let replay_contract = match import_replay_contract(&job.preview_payload, locale) { - Ok(contract) => contract, - Err(error) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_replay_verification_failed", - error, - ) - .await; - } - }; - let parsed = match replay_contract { - ImportReplayContract::PersistedV2(parsed) => parsed, - ImportReplayContract::LegacyV1 | ImportReplayContract::Current => { - let legacy_v1 = matches!(replay_contract, ImportReplayContract::LegacyV1); - let dependency_snapshots = match self - .read_import_job_dependencies(workspace_id, &job.preview_payload, locale) - .await - { - Ok(snapshots) => snapshots, - Err(error) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_dependency_verification_failed", - error, - ) - .await; - } - }; - let normalization_config = match import_job_normalization_config( - &job.preview_payload, - &self.external_reference_normalization, - legacy_v1, - locale, - ) { - Ok(config) => config, - Err(error) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_replay_verification_failed", - error, - ) - .await; - } - }; - let parsed = match parse_verified_preview( - verified.bytes, - source.digest.digest_hex().to_owned(), - dependency_snapshots, - normalization_config, - locale, - legacy_v1, - ) - .await - { - Ok(parsed) => parsed, - Err(error) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_replay_verification_failed", - error, - ) - .await; - } - }; - if let Err(error) = verify_preview_contract(&job.preview_payload, &parsed, locale) { - return self - .fail_openapi_import_or_replay( - &replay_context, - "import_replay_verification_failed", - error, - ) - .await; - } - parsed - } - }; - if preview_has_blocker(&parsed.preview, &selected) { + let preview = resolved_preview.preview; + if preview_has_blocker(&preview, &selected) { return self .fail_openapi_import_or_replay( &replay_context, @@ -420,7 +317,7 @@ impl AdminService { .await; } let mut candidates = BTreeMap::new(); - for group in &parsed.preview.groups { + for group in &preview.groups { for operation in &group.operations { candidates.insert(operation.key.clone(), operation); } @@ -599,227 +496,6 @@ fn validate_openapi_upload(upload: &OpenApiUpload) -> Result<(), ApiError> { Ok(()) } -struct ParsedOpenApiPreview { - preview: crank_import::rest::ImportPreview, - ir_fingerprint: String, -} - -enum ImportReplayContract { - LegacyV1, - PersistedV2(ParsedOpenApiPreview), - Current, -} - -fn import_replay_contract( - payload: &serde_json::Value, - locale: OpenApiUploadLocale, -) -> Result { - let Some(normalization) = payload.get("normalization") else { - return Ok(ImportReplayContract::LegacyV1); - }; - let normalizer = normalization - .get("normalizer_version") - .and_then(serde_json::Value::as_str) - .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; - let projection = normalization - .get("projection_version") - .and_then(serde_json::Value::as_str) - .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; - let ir_fingerprint = normalization - .get("ir_fingerprint") - .and_then(serde_json::Value::as_str) - .filter(|value| is_lower_sha256(value)) - .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; - let expected_preview_digest = payload - .get("preview_digest") - .and_then(serde_json::Value::as_str) - .filter(|value| is_lower_sha256(value)) - .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; - - if normalizer == NORMALIZER_VERSION && projection == PROJECTION_VERSION { - return Ok(ImportReplayContract::Current); - } - // Historical preview payloads are replayed as their persisted projection. - // They must never be parsed again with the current v4 semantics. - if !matches!( - (normalizer, projection), - ("normalized-ir-v2", "preview-v2") | ("normalized-ir-v3", "preview-v3") - ) { - return Err(ApiError::openapi_upload(locale, "source_integrity")); - } - - let preview_value = payload - .get("preview") - .cloned() - .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; - if preview_digest(&preview_value)? != expected_preview_digest { - return Err(ApiError::openapi_upload(locale, "source_integrity")); - } - let preview = serde_json::from_value(preview_value) - .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; - Ok(ImportReplayContract::PersistedV2(ParsedOpenApiPreview { - preview, - ir_fingerprint: ir_fingerprint.to_owned(), - })) -} - -fn is_lower_sha256(value: &str) -> bool { - value.len() == 64 - && value - .bytes() - .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) -} - -async fn parse_verified_preview( - bytes: Vec, - digest: String, - snapshots: Vec, - normalization_config: crank_import::rest::NormalizationConfig, - locale: OpenApiUploadLocale, - legacy_v1: bool, -) -> Result { - let started = tokio::time::Instant::now(); - let permit = tokio::time::timeout(OPENAPI_PARSE_DEADLINE, OPENAPI_PARSE_SLOTS.acquire()) - .await - .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))? - .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))?; - let parsing = tokio::task::spawn_blocking(move || { - // Keep the permit inside the blocking task. Timing out the caller - // cannot cancel CPU work already running, but abandoned parsers remain - // globally bounded and release capacity when they actually finish. - let _permit = permit; - let document = std::str::from_utf8(&bytes) - .map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?; - if legacy_v1 { - return crank_import::rest::preview_document_legacy_v1(document) - .map(|preview| ParsedOpenApiPreview { - preview, - ir_fingerprint: String::new(), - }) - .map_err(|_| ApiError::openapi_upload(locale, "invalid_document")); - } - let digest = crank_import::rest::SourceDigest::parse(digest) - .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; - let ir = crank_import::rest::normalize_verified_bundle( - document, - digest, - &snapshots, - &normalization_config, - ) - .map_err(|error| match error { - crank_import::rest::ImportParseError::NoMethods => { - ApiError::openapi_upload(locale, "no_methods") - } - _ => ApiError::openapi_upload(locale, "invalid_document"), - })?; - let ir_fingerprint = preview_digest( - &serde_json::to_value(&ir) - .map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?, - )?; - Ok::<_, ApiError>(ParsedOpenApiPreview { - preview: crank_import::rest::preview_from_ir(&ir), - ir_fingerprint, - }) - }); - let remaining = OPENAPI_PARSE_DEADLINE - .checked_sub(started.elapsed()) - .unwrap_or(std::time::Duration::ZERO); - tokio::time::timeout(remaining, parsing) - .await - .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))? - .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))? -} - -fn artifact_error(locale: OpenApiUploadLocale, error: ArtifactError) -> ApiError { - match error { - ArtifactError::EmptySource => ApiError::openapi_upload(locale, "empty_file"), - ArtifactError::SourceTooLarge => ApiError::openapi_upload(locale, "file_too_large"), - ArtifactError::Integrity => ApiError::openapi_upload(locale, "source_integrity"), - ArtifactError::Storage - | ArtifactError::NotFound - | ArtifactError::InvalidReference - | ArtifactError::UnsafeRoot => ApiError::openapi_upload(locale, "storage_unavailable"), - } -} - -fn openapi_source_error(locale: OpenApiUploadLocale, error: RegistryError) -> ApiError { - match error { - RegistryError::SourceNotFound { .. } | RegistryError::SourceUnavailable => { - ApiError::openapi_upload(locale, "source_unavailable") - } - RegistryError::SourceIntegrity => ApiError::openapi_upload(locale, "source_integrity"), - other => ApiError::from(other), - } -} - -fn preview_digest(preview: &serde_json::Value) -> Result { - let canonical = - serde_json::to_vec(preview).map_err(|error| ApiError::internal(error.to_string()))?; - Ok(format!("{:x}", Sha256::digest(canonical))) -} - -fn verify_preview_contract( - payload: &serde_json::Value, - parsed: &ParsedOpenApiPreview, - locale: OpenApiUploadLocale, -) -> Result<(), ApiError> { - // Pre-fingerprint jobs are legacy rolling-upgrade records. They retain the - // old reparse behavior; new jobs fail closed if parser output drifts or a - // persisted preview has been changed. - let expected = payload - .get("preview_digest") - .and_then(serde_json::Value::as_str); - if payload.get("normalization").is_some() && expected.is_none() { - return Err(ApiError::openapi_upload(locale, "source_integrity")); - } - let Some(expected) = expected else { - return Ok(()); - }; - let actual = preview_digest( - &serde_json::to_value(&parsed.preview) - .map_err(|error| ApiError::internal(error.to_string()))?, - )?; - if actual == expected { - if let Some(normalization) = payload.get("normalization") { - let normalizer = normalization - .get("normalizer_version") - .and_then(serde_json::Value::as_str); - let projection = normalization - .get("projection_version") - .and_then(serde_json::Value::as_str); - let fingerprint = normalization - .get("ir_fingerprint") - .and_then(serde_json::Value::as_str); - if normalizer != Some(NORMALIZER_VERSION) - || projection != Some(PROJECTION_VERSION) - || fingerprint != Some(parsed.ir_fingerprint.as_str()) - { - return Err(ApiError::openapi_upload(locale, "source_integrity")); - } - } - Ok(()) - } else { - Err(ApiError::openapi_upload(locale, "source_integrity")) - } -} - -fn preview_has_blocker( - preview: &crank_import::rest::ImportPreview, - selected_operation_keys: &BTreeSet, -) -> bool { - preview - .findings - .iter() - .any(|finding| finding.severity == ImportFindingSeverity::Error) - || preview - .groups - .iter() - .flat_map(|group| group.operations.iter()) - .filter(|operation| selected_operation_keys.contains(&operation.key)) - .flat_map(|operation| operation.findings.iter()) - .any(|finding| finding.severity == ImportFindingSeverity::Error) -} - fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result { let selected_operation_keys = payload .selected_operation_keys diff --git a/apps/admin-api/src/service/imports/import_plan.rs b/apps/admin-api/src/service/imports/import_plan.rs new file mode 100644 index 0000000..11dc890 --- /dev/null +++ b/apps/admin-api/src/service/imports/import_plan.rs @@ -0,0 +1,777 @@ +use super::*; + +impl AdminService { + #[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))] + pub async fn create_openapi_import_plan( + &self, + workspace_id: &WorkspaceId, + job_id: &ImportJobId, + payload: OpenApiImportPlanCreatePayload, + ) -> Result { + self.ensure_workspace_exists(workspace_id).await?; + + let now = OffsetDateTime::now_utc(); + let job = self + .registry + .get_import_job(workspace_id, job_id) + .await? + .ok_or_else(|| { + ApiError::not_found_with_context( + "import job was not found", + json!({ "job_id": job_id.as_str() }), + ) + })?; + if job.kind != ImportJobKind::OpenApi { + return Err(ApiError::validation("import job kind is not openapi")); + } + if job.expires_at <= now { + return Err(ApiError::validation("import preview has expired")); + } + if job.status != ImportJobStatus::Pending { + return Err(import_plan_repreview_error("import_plan_job_terminal")); + } + + if payload.decisions.len() > MAX_IMPORT_PLAN_KEYS { + return Err(ApiError::validation( + "too many decision blocks for this import plan", + )); + } + if payload.server_url.as_deref().is_some_and(|server_url| { + server_url.is_empty() || server_url.len() > MAX_IMPORT_PLAN_SERVER_URL_BYTES + }) { + return Err(ApiError::validation_with_context( + "server_url is empty or too long", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + + // The plan is anchored to the verified job snapshots. A stale or + // corrupt preview is reported as bounded re-preview guidance and + // never mutates the job: other stored plans may still reference the + // same job and its replay must remain readable. + let resolved = self + .resolve_verified_openapi_preview(workspace_id, &job, OpenApiUploadLocale::En) + .await + .map_err(|resolution| match resolution { + ImportPreviewResolution::Fatal(error_code, _) => { + import_plan_repreview_error(error_code) + } + ImportPreviewResolution::NonFatal(error) => error, + })?; + let preview = &resolved.preview; + + // Stored external dependency snapshots are re-verified for every + // replay variant (including historically persisted ones) without a + // network fetch: detached or altered snapshots are re-preview + // material and no plan is created. + self.read_import_job_dependencies( + workspace_id, + &job.preview_payload, + OpenApiUploadLocale::En, + ) + .await + .map_err(|_| import_plan_repreview_error("import_dependency_verification_failed"))?; + + let candidates: BTreeMap = preview + .groups + .iter() + .flat_map(|group| group.operations.iter()) + .map(|operation| (operation.key.clone(), operation)) + .collect(); + let all_set: BTreeSet = candidates.keys().cloned().collect(); + let selected = resolve_plan_selection( + &payload.selection_mode, + &payload.selected_operation_keys, + &all_set, + )?; + if selected.len() > MAX_IMPORT_PLAN_KEYS + || all_set.len() - selected.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_keys: Vec = selected.iter().cloned().collect(); + let excluded: Vec = all_set + .iter() + .filter(|key| !selected.contains(*key)) + .cloned() + .collect(); + let decisions = validate_plan_decisions(&payload.decisions, &selected)?; + + let global_blocker = preview + .findings + .iter() + .find(|finding| finding.severity == ImportFindingSeverity::Error) + .map(|finding| finding.code.clone()); + + let mut planned = Vec::with_capacity(selected_keys.len()); + for key in &selected_keys { + let Some(candidate) = candidates.get(key) else { + unreachable!("selection is validated against the candidate set"); + }; + let blocker = global_blocker.clone().or_else(|| { + candidate + .findings + .iter() + .find(|finding| finding.severity == ImportFindingSeverity::Error) + .map(|finding| finding.code.clone()) + }); + let candidate_plan = self + .plan_import_candidate( + workspace_id, + payload.server_url.as_deref(), + key, + candidate, + decisions.get(key).copied(), + blocker, + ) + .await?; + planned.push(candidate_plan); + } + + let counts = OpenApiImportPlanCounts { + selected: planned.len(), + excluded: excluded.len(), + actionable: planned + .iter() + .filter(|candidate| !candidate.blocked) + .count(), + blocked: planned.iter().filter(|candidate| candidate.blocked).count(), + }; + + // Server-authored immutable plan. Client-supplied candidate material, + // digests or payload fields are not read: every value below derives + // from the verified job preview and the revalidated decisions. + let all_keys: Vec = all_set.iter().cloned().collect(); + let plan_counts_value = json!({ + "selected": counts.selected, + "excluded": counts.excluded, + "actionable": counts.actionable, + "blocked": counts.blocked, + }); + let plan_reference = new_prefixed_id("plan"); + let plan_payload = json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_reference, + "job_id": job_id.as_str(), + "server_url": payload.server_url, + "source": { + "source_id": resolved.source.source_id.as_str(), + "digest": resolved.source.digest.as_str(), + }, + "dependencies": job.preview_payload.get("dependencies").cloned().unwrap_or(json!([])), + "fingerprint": resolved.fingerprint, + "selection": { + "mode": payload.selection_mode, + "all": all_keys, + "selected": selected_keys, + "excluded": excluded, + }, + "counts": plan_counts_value, + "candidates": planned.iter().map(plan_candidate_payload).collect::, _>>()?, + }); + + let plan_reference_id = ImportPlanId::new(&plan_reference); + let created = self + .registry + .create_import_plan(CreateImportPlanRequest { + id: &plan_reference_id, + workspace_id, + job_id, + payload: &plan_payload, + created_at: &now, + expires_at: &job.expires_at, + }) + .await + .map_err(ApiError::from)?; + + let mut candidate_views: Vec = planned + .iter() + .map(|candidate| OpenApiImportPlanCandidateView { + key: candidate.key.clone(), + name: candidate.name.clone(), + state: if candidate.blocked { + "blocked" + } else { + "actionable" + } + .to_owned(), + action: Some(candidate.action.clone()), + target_operation_id: candidate.target_operation_id.clone(), + confirmed_name: candidate.confirmed_name.clone(), + auth_profile_id: candidate.auth_profile_id.clone(), + reason: candidate.reason.clone(), + }) + .collect(); + candidate_views.extend(excluded.iter().map(|key| { + let name = candidates + .get(key) + .map(|candidate| candidate.draft.name.clone()) + .unwrap_or_default(); + OpenApiImportPlanCandidateView { + key: key.clone(), + name, + state: "excluded".to_owned(), + action: None, + target_operation_id: None, + confirmed_name: None, + auth_profile_id: None, + reason: Some("not_selected".to_owned()), + } + })); + + Ok(OpenApiImportPlanCreateResponse { + plan_reference: created.id.as_str().to_owned(), + plan_digest: created.digest, + expires_at: created + .expires_at + .format(&Rfc3339) + .map_err(|error| ApiError::internal(error.to_string()))?, + selection_mode: payload.selection_mode, + counts, + candidates: candidate_views, + }) + } + + /// Materializes one selected candidate of the plan: the confirmed + /// decision, the exact authorized target with its expected state and the + /// full Draft snapshot (create/rename/new_version) or the bounded + /// redacted rejection reason (blocked). + async fn plan_import_candidate( + &self, + workspace_id: &WorkspaceId, + server_url: Option<&str>, + key: &str, + candidate: &ImportOperationCandidate, + decision: Option<&OpenApiImportPlanDecision>, + blocker_reason: Option, + ) -> Result { + let action = decision + .map(|decision| decision.action.as_str()) + .unwrap_or("create"); + let target_operation_id = + decision.and_then(|decision| decision.target_operation_id.clone()); + let confirmed_name = decision.and_then(|decision| decision.confirmed_name.clone()); + let auth_profile_id = decision.and_then(|decision| decision.auth_profile_id.clone()); + let candidate_name = candidate.draft.name.clone(); + + let blocked = blocker_reason.is_some(); + + // Explicit skip/new_version targets are always resolved in the + // workspace: an unknown target, a name mismatch, or an incompatible + // protocol is a request error even when the candidate is already + // blocked. + let mut target = None; + let mut target_summary = None; + let mut next_version: Option = None; + if matches!(action, "skip" | "new_version") { + let target_id = target_operation_id + .as_ref() + .expect("target_operation_id is validated for skip/new_version") + .clone(); + let target_operation = OperationId::new(target_id.clone()); + let summary = self + .registry + .get_operation_summary(workspace_id, &target_operation) + .await? + .ok_or_else(|| { + ApiError::validation_with_context( + "decision target operation was not found in this workspace", + json!({ + "target_operation_id": target_id, + "error_code": "import_plan_target_invalid" + }), + ) + })?; + if summary.name != candidate_name { + return Err(ApiError::validation_with_context( + "decision target does not match the candidate name in this workspace", + json!({ + "target_operation_id": target_id, + "error_code": "import_plan_target_invalid" + }), + )); + } + if action == "new_version" + && (summary.protocol != Protocol::Rest + || summary.status == OperationStatus::Archived) + { + return Err(ApiError::validation_with_context( + "new_version target must be an active REST operation", + json!({ + "target_operation_id": target_id, + "error_code": "import_plan_target_invalid" + }), + )); + } + target = Some(json!({ + "operation_id": target_id, + "name": summary.name, + "expected_state": { + "status": serde_json::to_value(summary.status) + .map_err(|error| ApiError::internal(error.to_string()))?, + "current_draft_version": summary.current_draft_version, + "latest_published_version": summary.latest_published_version, + }, + })); + if action == "new_version" { + let version = summary + .current_draft_version + .checked_add(1) + .filter(|value| i32::try_from(*value).is_ok()) + .ok_or_else(|| { + ApiError::conflict_with_context( + "operation revision limit reached", + json!({ "error_code": "operation_revision_exhausted" }), + ) + })?; + next_version = Some(version); + } + target_summary = Some(summary); + } + + // create/rename must land on a name that is free now; the server + // never auto-selects a replacement when the confirmed name is taken. + if !blocked && matches!(action, "create" | "rename") { + let final_name = confirmed_name.as_deref().unwrap_or(candidate_name.as_str()); + if self + .find_operation_by_name(workspace_id, final_name) + .await? + .is_some() + { + if action == "create" && decision.is_none() { + return Ok(PlannedCandidate { + key: key.to_owned(), + name: candidate_name, + blocked: true, + reason: Some("operation_name_conflict".to_owned()), + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + auth_snapshot: None, + target: None, + operation: None, + }); + } + return Err(ApiError::conflict_with_context( + format!("operation with name {final_name} already exists"), + json!({ + "name": final_name, + "error_code": "import_plan_name_conflict", + "recovery": "confirm_skip_rename_or_new_version" + }), + )); + } + } + + // The final server-authored auth profile reference: the explicit + // decision, or the target's reference inherited for an actionable + // new_version. Only the reference (never a secret value) is stored. + let mut auth_profile_ref = auth_profile_id + .as_deref() + .map(|id| AuthProfileId::new(id.to_owned())); + if let Some(summary) = target_summary.as_ref() + && !blocked + && action == "new_version" + && auth_profile_ref.is_none() + { + let record = self + .registry + .get_operation_version(workspace_id, &summary.id, summary.current_draft_version) + .await? + .ok_or_else(|| ApiError::internal("current operation version is unavailable"))?; + auth_profile_ref = record.snapshot.execution_config.auth_profile_ref; + } + let auth_profile_id = auth_profile_ref.as_ref().map(|id| id.as_str().to_owned()); + + if blocked { + return Ok(PlannedCandidate { + key: key.to_owned(), + name: candidate_name, + blocked: true, + reason: blocker_reason, + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + auth_snapshot: None, + target, + operation: None, + }); + } + + if action == "skip" { + return Ok(PlannedCandidate { + key: key.to_owned(), + name: candidate_name, + blocked: false, + reason: None, + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + auth_snapshot: None, + target, + operation: None, + }); + } + + let mut draft = operation_draft_from_candidate(candidate, server_url); + if action == "rename" { + draft.name = confirmed_name + .clone() + .expect("rename decisions carry the exact confirmed_name"); + } + attach_import_findings(&mut draft, candidate); + let operation = self.new_operation_snapshot(OperationPayload { + name: draft.name.clone(), + display_name: draft.display_name.clone(), + category: draft.category, + protocol: Protocol::Rest, + security_level: OperationSecurityLevel::Standard, + target: crank_core::Target::Rest(draft.target), + input_schema: draft.input_schema, + output_schema: draft.output_schema, + input_mapping: draft.input_mapping, + output_mapping: draft.output_mapping, + execution_config: ExecutionConfig { + timeout_ms: 10_000, + retry_policy: None, + response_cache: None, + idempotency: None, + safety: None, + approval_policy: None, + auth_profile_ref, + headers: BTreeMap::new(), + }, + tool_description: draft.tool_description, + wizard_state: draft.wizard_state, + })?; + // The same workspace/auth secret validator the operation creation + // path uses: the referenced profile and its active secret refs must + // resolve in this workspace before the plan is accepted. + self.validate_registry_operation_in_workspace(workspace_id, &operation) + .await?; + let mut operation = operation; + if action == "new_version" { + let target_id = target_operation_id + .clone() + .expect("new_version decisions carry target_operation_id"); + operation.id = OperationId::new(target_id); + operation.version = next_version.expect("new_version computes the next Draft version"); + } + let auth_snapshot = match operation.execution_config.auth_profile_ref.as_ref() { + Some(profile_id) => Some(self.import_auth_snapshot(workspace_id, profile_id).await?), + None => None, + }; + + Ok(PlannedCandidate { + key: key.to_owned(), + name: draft.name, + blocked: false, + reason: None, + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + auth_snapshot, + target, + operation: Some(operation), + }) + } + + /// Captures only profile configuration and secret metadata. Credential + /// values never enter the plan; Apply compares this snapshot under locks. + async fn import_auth_snapshot( + &self, + workspace_id: &WorkspaceId, + profile_id: &AuthProfileId, + ) -> Result { + let unavailable = || import_plan_repreview_error("import_plan_auth_unavailable"); + let profile = self + .registry + .get_auth_profile(workspace_id, profile_id) + .await? + .ok_or_else(unavailable)?; + let mut secret_ids: Vec = profile + .config + .secret_ids() + .into_iter() + .map(|id| id.as_str().to_owned()) + .collect(); + secret_ids.sort(); + secret_ids.dedup(); + let mut secrets = Vec::with_capacity(secret_ids.len()); + for id in secret_ids { + let secret = self + .registry + .get_secret(workspace_id, &crank_core::SecretId::new(id.clone())) + .await? + .ok_or_else(unavailable)? + .secret; + secrets.push(json!({ + "id": id, + "status": secret.status, + "current_version": secret.current_version, + "updated_at": secret.updated_at.format(&Rfc3339) + .map_err(|error| ApiError::internal(error.to_string()))?, + })); + } + Ok(json!({ + "profile_id": profile_id.as_str(), + "name": profile.name, + "kind": profile.kind, + "config": profile.config, + "updated_at": profile.updated_at.format(&Rfc3339) + .map_err(|error| ApiError::internal(error.to_string()))?, + "secrets": secrets, + })) + } +} + +struct PlannedCandidate { + key: String, + name: String, + blocked: bool, + reason: Option, + action: String, + target_operation_id: Option, + confirmed_name: Option, + auth_profile_id: Option, + auth_snapshot: Option, + target: Option, + operation: Option, +} + +pub(super) fn import_plan_repreview_error(error_code: &'static str) -> ApiError { + ApiError::conflict_with_context( + "import plan revalidation failed; create a fresh preview", + json!({ + "error_code": error_code, + "recovery": "repreview_required", + "repreview_required": true + }), + ) +} + +/// Closes the selection against the current preview: `full` keeps every +/// candidate (an explicit list must equal the whole candidate set), `partial` +/// requires a non-empty exact subset. Unknown or duplicate keys are rejected +/// before any plan material exists. +fn resolve_plan_selection( + selection_mode: &str, + requested: &[String], + all: &BTreeSet, +) -> Result, ApiError> { + if !matches!(selection_mode, "full" | "partial") { + return Err(ApiError::validation_with_context( + "selection_mode must be full or partial", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + if requested.len() > MAX_IMPORT_PLAN_KEYS { + return Err(ApiError::validation_with_context( + "too many selected_operation_keys for this import plan", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + for key in requested { + if key.is_empty() || key.len() > MAX_IMPORT_PLAN_KEY_BYTES { + return Err(ApiError::validation_with_context( + "selected_operation_keys entries are empty or too long", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + } + let requested_set: BTreeSet<&String> = requested.iter().collect(); + if requested_set.len() != requested.len() { + return Err(ApiError::validation_with_context( + "selected_operation_keys contains duplicates", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + let all_set: BTreeSet<&String> = all.iter().collect(); + match selection_mode { + "full" => { + if !requested_set.is_empty() && requested_set != all_set { + return Err(ApiError::validation_with_context( + "full selection covers the entire candidate set; selected_operation_keys does not match the preview", + json!({ "error_code": "import_plan_selection_mismatch" }), + )); + } + Ok(all.clone()) + } + _ => { + if requested_set.is_empty() { + return Err(ApiError::validation_with_context( + "partial selection requires at least one selected_operation_key", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + if !requested_set.is_subset(&all_set) { + return Err(ApiError::validation_with_context( + "selected_operation_keys reference operations absent from the preview", + json!({ "error_code": "import_plan_unknown_operation_key" }), + )); + } + Ok(requested.iter().cloned().collect()) + } + } +} + +/// 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, +) -> Result, 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"); + } + 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() { + return invalid("auth_profile_id 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(()) +} + +/// Serializes one planned candidate into the versioned plan payload: the +/// redacted decision, the bounded rejection reason (if any), the exact +/// authorized target with expected state and, for mutating actions, the full +/// server-authored Draft snapshot. +fn plan_candidate_payload(candidate: &PlannedCandidate) -> Result { + let mut object = json!({ + "key": candidate.key, + "name": candidate.name, + "state": if candidate.blocked { "blocked" } else { "actionable" }, + "decision": { + "action": candidate.action, + "target_operation_id": candidate.target_operation_id, + "confirmed_name": candidate.confirmed_name, + "auth_profile_id": candidate.auth_profile_id, + }, + }); + if let Some(reason) = &candidate.reason { + object["reason"] = json!(reason); + } + if let Some(target) = &candidate.target { + object["target"] = target.clone(); + } + if let Some(auth_snapshot) = &candidate.auth_snapshot { + object["auth_snapshot"] = auth_snapshot.clone(); + } + if let Some(operation) = &candidate.operation { + let value = serde_json::to_value(operation) + .map_err(|error| ApiError::internal(error.to_string()))?; + object["operation"] = value; + } + Ok(object) +} diff --git a/apps/admin-api/src/service/imports/plan_apply.rs b/apps/admin-api/src/service/imports/plan_apply.rs new file mode 100644 index 0000000..fd486a2 --- /dev/null +++ b/apps/admin-api/src/service/imports/plan_apply.rs @@ -0,0 +1,185 @@ +use super::*; +use crank_registry::{ApplyImportPlanRequest, ImportPlanApplyReport}; + +const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128; + +impl AdminService { + #[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, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + payload: OpenApiImportPlanApplyPayload, + ) -> Result { + self.ensure_workspace_exists(workspace_id).await?; + validate_apply_payload(&payload)?; + + // The replay row is deliberately checked before the plan or job. + // Its report remains authoritative after normal TTL cleanup removes + // those source rows. + if let Some(application) = self + .registry + .get_import_plan_application(workspace_id, &payload.idempotency_key) + .await? + { + if application.plan_digest != payload.plan_digest { + return Err(key_digest_conflict()); + } + return Ok(application.result.report); + } + + 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" }), + ) + })?; + + if plan.digest != payload.plan_digest { + return Err(import_plan_repreview_error("import_plan_digest_mismatch")); + } + let now = OffsetDateTime::now_utc(); + if plan.expires_at <= now { + return Err(import_plan_repreview_error("import_plan_expired")); + } + + let job = self + .registry + .get_import_job(workspace_id, &plan.job_id) + .await? + .ok_or_else(|| import_plan_repreview_error("import_plan_job_unavailable"))?; + if job.kind != ImportJobKind::OpenApi { + return Err(import_plan_repreview_error("import_plan_job_kind_mismatch")); + } + if job.status != ImportJobStatus::Pending || job.expires_at <= now { + return Err(import_plan_repreview_error("import_plan_job_terminal")); + } + + let resolved = self + .resolve_verified_openapi_preview(workspace_id, &job, OpenApiUploadLocale::En) + .await + .map_err(|resolution| match resolution { + ImportPreviewResolution::Fatal(error_code, _) => { + import_plan_repreview_error(error_code) + } + ImportPreviewResolution::NonFatal(_) => { + import_plan_repreview_error("import_preview_revalidation_failed") + } + })?; + + // Applying a new plan requires the current normalizer/projection + // contract. Historic jobs remain available to the legacy replay path, + // but a plan created from a historic projection must be previewed + // again before it can authorize mutations. + 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) + { + return Err(import_plan_repreview_error("importer_version_changed")); + } + + // Verify every persisted external snapshot again without making a + // network request. The registry repeats source/dependency checks + // under its transaction before it mutates operations. + self.read_import_job_dependencies( + workspace_id, + &job.preview_payload, + OpenApiUploadLocale::En, + ) + .await + .map_err(|_| import_plan_repreview_error("import_dependency_verification_failed"))?; + + let result = self + .registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id, + plan_id, + plan_digest: &payload.plan_digest, + idempotency_key: &payload.idempotency_key, + verified_fingerprint: &resolved.fingerprint, + applied_at: &now, + }) + .await + .map_err(map_apply_registry_error)?; + Ok(result.report) + } +} + +fn validate_apply_payload(payload: &OpenApiImportPlanApplyPayload) -> Result<(), ApiError> { + let digest_valid = payload.plan_digest.len() == 64 + && payload + .plan_digest + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)); + if !digest_valid { + return Err(ApiError::validation_with_context( + "plan_digest must be 64 lowercase hexadecimal characters", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + if !(1..=MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES).contains(&payload.idempotency_key.len()) + || payload.idempotency_key.trim() != payload.idempotency_key + || payload.idempotency_key.chars().any(char::is_control) + { + return Err(ApiError::validation_with_context( + "idempotency_key must be a non-empty bounded printable value", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + Ok(()) +} + +fn map_apply_registry_error(error: RegistryError) -> ApiError { + match error { + RegistryError::ImportPlanNotFound { .. } => ApiError::not_found_with_context( + "import plan was not found in this workspace", + json!({ "error_code": "import_plan_not_found" }), + ), + RegistryError::ImportPlanExpired { .. } => { + import_plan_repreview_error("import_plan_expired") + } + RegistryError::ImportPlanPayloadCorrupt { .. } => { + import_plan_repreview_error("import_plan_payload_invalid") + } + RegistryError::ImportPlanFingerprintStale { .. } => { + import_plan_repreview_error("import_plan_fingerprint_stale") + } + RegistryError::ImportPlanKeyDigestConflict { .. } => key_digest_conflict(), + RegistryError::ImportPlanNameConflict { .. } => ApiError::conflict_with_context( + "an operation name changed after the plan was created", + json!({ + "error_code": "import_plan_name_conflict", + "recovery": "confirm_skip_rename_or_new_version" + }), + ), + RegistryError::ImportPlanTargetStale { .. } => { + import_plan_repreview_error("import_plan_target_stale") + } + RegistryError::ImportPlanAuthUnavailable { .. } => { + import_plan_repreview_error("import_plan_auth_unavailable") + } + other => ApiError::from(other), + } +} + +fn key_digest_conflict() -> ApiError { + ApiError::conflict_with_context( + "idempotency key was already committed for different plan material", + json!({ + "error_code": "import_plan_key_digest_conflict", + "recovery": "repreview_required", + "repreview_required": true + }), + ) +} diff --git a/apps/admin-api/src/service/imports/verified_preview.rs b/apps/admin-api/src/service/imports/verified_preview.rs new file mode 100644 index 0000000..16ac620 --- /dev/null +++ b/apps/admin-api/src/service/imports/verified_preview.rs @@ -0,0 +1,390 @@ +use super::*; + +/// Revalidates the verified source/dependency snapshots and the persisted +/// replay contract of a stored job without mutating the job. Both the +/// legacy Apply and the ImportPlan creation derive from this result, so +/// the legacy failure-text code travels with the outcome. +impl AdminService { + pub(super) async fn resolve_verified_openapi_preview( + &self, + workspace_id: &WorkspaceId, + job: &ImportJob, + locale: OpenApiUploadLocale, + ) -> Result { + let source = match import_job_source(&job.preview_payload, locale) { + Ok(source) => source, + Err(error) => { + return Err(ImportPreviewResolution::Fatal( + "import_source_verification_failed", + error, + )); + } + }; + let verified = match self + .registry + .read_artifact_source( + std::sync::Arc::clone(&self.artifact_store), + workspace_id, + &source.source_id, + ) + .await + { + Ok(verified) => verified, + Err( + error @ (RegistryError::SourceNotFound { .. } | RegistryError::SourceUnavailable), + ) => { + return Err(ImportPreviewResolution::Fatal( + "import_source_verification_failed", + openapi_source_error(locale, error), + )); + } + Err(error @ RegistryError::SourceIntegrity) => { + return Err(ImportPreviewResolution::Fatal( + "import_source_verification_failed", + openapi_source_error(locale, error), + )); + } + Err(error) => { + return Err(ImportPreviewResolution::NonFatal(openapi_source_error( + locale, error, + ))); + } + }; + if verified.source.blob.artifact_ref != source.digest { + return Err(ImportPreviewResolution::Fatal( + "import_source_verification_failed", + ApiError::openapi_upload(locale, "source_integrity"), + )); + } + let replay_contract = match import_replay_contract(&job.preview_payload, locale) { + Ok(contract) => contract, + Err(error) => { + return Err(ImportPreviewResolution::Fatal( + "import_replay_verification_failed", + error, + )); + } + }; + let (preview, normalizer_version, projection_version, ir_fingerprint) = + match replay_contract { + ImportReplayContract::PersistedV2(replay) => ( + replay.preview.preview, + replay.normalizer_version, + replay.projection_version, + replay.preview.ir_fingerprint, + ), + ImportReplayContract::LegacyV1 | ImportReplayContract::Current => { + let legacy_v1 = matches!(replay_contract, ImportReplayContract::LegacyV1); + let dependency_snapshots = self + .read_import_job_dependencies(workspace_id, &job.preview_payload, locale) + .await + .map_err(|error| { + ImportPreviewResolution::Fatal( + "import_dependency_verification_failed", + error, + ) + })?; + let normalization_config = import_job_normalization_config( + &job.preview_payload, + &self.external_reference_normalization, + legacy_v1, + locale, + ) + .map_err(|error| { + ImportPreviewResolution::Fatal("import_replay_verification_failed", error) + })?; + let parsed = parse_verified_preview( + verified.bytes, + source.digest.digest_hex().to_owned(), + dependency_snapshots, + normalization_config, + locale, + legacy_v1, + ) + .await + .map_err(|error| { + ImportPreviewResolution::Fatal("import_replay_verification_failed", error) + })?; + if let Err(error) = + verify_preview_contract(&job.preview_payload, &parsed, locale) + { + return Err(ImportPreviewResolution::Fatal( + "import_replay_verification_failed", + error, + )); + } + if legacy_v1 { + (parsed.preview, String::new(), String::new(), String::new()) + } else { + ( + parsed.preview, + NORMALIZER_VERSION.to_owned(), + PROJECTION_VERSION.to_owned(), + parsed.ir_fingerprint, + ) + } + } + }; + Ok(ResolvedOpenApiPreview { + source, + preview, + fingerprint: json!({ + "normalizer_version": if normalizer_version.is_empty() { Value::Null } else { Value::String(normalizer_version) }, + "projection_version": if projection_version.is_empty() { Value::Null } else { Value::String(projection_version) }, + "ir_fingerprint": if ir_fingerprint.is_empty() { Value::Null } else { Value::String(ir_fingerprint) }, + "preview_digest": job.preview_payload.get("preview_digest").and_then(Value::as_str).map(|digest| Value::String(digest.to_owned())).unwrap_or(Value::Null), + }), + }) + } +} + +pub(super) struct ParsedOpenApiPreview { + pub(super) preview: crank_import::rest::ImportPreview, + pub(super) ir_fingerprint: String, +} + +/// Verified job preview material shared by the legacy Apply path and the +/// ImportPlan creation: the replay contract has held, no job mutation +/// happened. +pub(super) struct ResolvedOpenApiPreview { + pub(super) source: ImportJobSourceEnvelope, + pub(super) preview: crank_import::rest::ImportPreview, + pub(super) fingerprint: Value, +} + +/// Outcome of a failed preview revalidation. `Fatal` codes are the same +/// stable failure texts the legacy path records on a failed job; the plan +/// path must surface the same material as a bounded re-preview conflict +/// without touching the job, because other stored plans may anchor to it. +pub(super) enum ImportPreviewResolution { + Fatal(&'static str, ApiError), + NonFatal(ApiError), +} + +struct PersistedOpenApiReplay { + preview: ParsedOpenApiPreview, + normalizer_version: String, + projection_version: String, +} + +enum ImportReplayContract { + LegacyV1, + PersistedV2(Box), + Current, +} + +fn import_replay_contract( + payload: &serde_json::Value, + locale: OpenApiUploadLocale, +) -> Result { + let Some(normalization) = payload.get("normalization") else { + return Ok(ImportReplayContract::LegacyV1); + }; + let normalizer = normalization + .get("normalizer_version") + .and_then(serde_json::Value::as_str) + .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; + let projection = normalization + .get("projection_version") + .and_then(serde_json::Value::as_str) + .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; + let ir_fingerprint = normalization + .get("ir_fingerprint") + .and_then(serde_json::Value::as_str) + .filter(|value| is_lower_sha256(value)) + .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; + let expected_preview_digest = payload + .get("preview_digest") + .and_then(serde_json::Value::as_str) + .filter(|value| is_lower_sha256(value)) + .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; + + if normalizer == NORMALIZER_VERSION && projection == PROJECTION_VERSION { + return Ok(ImportReplayContract::Current); + } + // Historical preview payloads are replayed as their persisted projection. + // They must never be parsed again with the current v4 semantics. + if !matches!( + (normalizer, projection), + ("normalized-ir-v2", "preview-v2") | ("normalized-ir-v3", "preview-v3") + ) { + return Err(ApiError::openapi_upload(locale, "source_integrity")); + } + + let preview_value = payload + .get("preview") + .cloned() + .ok_or_else(|| ApiError::openapi_upload(locale, "source_integrity"))?; + if preview_digest(&preview_value)? != expected_preview_digest { + return Err(ApiError::openapi_upload(locale, "source_integrity")); + } + let preview = serde_json::from_value(preview_value) + .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; + Ok(ImportReplayContract::PersistedV2(Box::new( + PersistedOpenApiReplay { + preview: ParsedOpenApiPreview { + preview, + ir_fingerprint: ir_fingerprint.to_owned(), + }, + normalizer_version: normalizer.to_owned(), + projection_version: projection.to_owned(), + }, + ))) +} + +fn is_lower_sha256(value: &str) -> bool { + value.len() == 64 + && value + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) +} + +pub(super) async fn parse_verified_preview( + bytes: Vec, + digest: String, + snapshots: Vec, + normalization_config: crank_import::rest::NormalizationConfig, + locale: OpenApiUploadLocale, + legacy_v1: bool, +) -> Result { + let started = tokio::time::Instant::now(); + let permit = tokio::time::timeout(OPENAPI_PARSE_DEADLINE, OPENAPI_PARSE_SLOTS.acquire()) + .await + .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))? + .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))?; + let parsing = tokio::task::spawn_blocking(move || { + // Keep the permit inside the blocking task. Timing out the caller + // cannot cancel CPU work already running, but abandoned parsers remain + // globally bounded and release capacity when they actually finish. + let _permit = permit; + let document = std::str::from_utf8(&bytes) + .map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?; + if legacy_v1 { + return crank_import::rest::preview_document_legacy_v1(document) + .map(|preview| ParsedOpenApiPreview { + preview, + ir_fingerprint: String::new(), + }) + .map_err(|_| ApiError::openapi_upload(locale, "invalid_document")); + } + let digest = crank_import::rest::SourceDigest::parse(digest) + .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; + let ir = crank_import::rest::normalize_verified_bundle( + document, + digest, + &snapshots, + &normalization_config, + ) + .map_err(|error| match error { + crank_import::rest::ImportParseError::NoMethods => { + ApiError::openapi_upload(locale, "no_methods") + } + _ => ApiError::openapi_upload(locale, "invalid_document"), + })?; + let ir_fingerprint = preview_digest( + &serde_json::to_value(&ir) + .map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?, + )?; + Ok::<_, ApiError>(ParsedOpenApiPreview { + preview: crank_import::rest::preview_from_ir(&ir), + ir_fingerprint, + }) + }); + let remaining = OPENAPI_PARSE_DEADLINE + .checked_sub(started.elapsed()) + .unwrap_or(std::time::Duration::ZERO); + tokio::time::timeout(remaining, parsing) + .await + .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))? + .map_err(|_| ApiError::openapi_upload(locale, "parser_unavailable"))? +} + +pub(super) fn artifact_error(locale: OpenApiUploadLocale, error: ArtifactError) -> ApiError { + match error { + ArtifactError::EmptySource => ApiError::openapi_upload(locale, "empty_file"), + ArtifactError::SourceTooLarge => ApiError::openapi_upload(locale, "file_too_large"), + ArtifactError::Integrity => ApiError::openapi_upload(locale, "source_integrity"), + ArtifactError::Storage + | ArtifactError::NotFound + | ArtifactError::InvalidReference + | ArtifactError::UnsafeRoot => ApiError::openapi_upload(locale, "storage_unavailable"), + } +} + +pub(super) fn openapi_source_error(locale: OpenApiUploadLocale, error: RegistryError) -> ApiError { + match error { + RegistryError::SourceNotFound { .. } | RegistryError::SourceUnavailable => { + ApiError::openapi_upload(locale, "source_unavailable") + } + RegistryError::SourceIntegrity => ApiError::openapi_upload(locale, "source_integrity"), + other => ApiError::from(other), + } +} + +pub(super) fn preview_digest(preview: &serde_json::Value) -> Result { + let canonical = + serde_json::to_vec(preview).map_err(|error| ApiError::internal(error.to_string()))?; + Ok(format!("{:x}", Sha256::digest(canonical))) +} + +fn verify_preview_contract( + payload: &serde_json::Value, + parsed: &ParsedOpenApiPreview, + locale: OpenApiUploadLocale, +) -> Result<(), ApiError> { + // Pre-fingerprint jobs are legacy rolling-upgrade records. They retain the + // old reparse behavior; new jobs fail closed if parser output drifts or a + // persisted preview has been changed. + let expected = payload + .get("preview_digest") + .and_then(serde_json::Value::as_str); + if payload.get("normalization").is_some() && expected.is_none() { + return Err(ApiError::openapi_upload(locale, "source_integrity")); + } + let Some(expected) = expected else { + return Ok(()); + }; + let actual = preview_digest( + &serde_json::to_value(&parsed.preview) + .map_err(|error| ApiError::internal(error.to_string()))?, + )?; + if actual == expected { + if let Some(normalization) = payload.get("normalization") { + let normalizer = normalization + .get("normalizer_version") + .and_then(serde_json::Value::as_str); + let projection = normalization + .get("projection_version") + .and_then(serde_json::Value::as_str); + let fingerprint = normalization + .get("ir_fingerprint") + .and_then(serde_json::Value::as_str); + if normalizer != Some(NORMALIZER_VERSION) + || projection != Some(PROJECTION_VERSION) + || fingerprint != Some(parsed.ir_fingerprint.as_str()) + { + return Err(ApiError::openapi_upload(locale, "source_integrity")); + } + } + Ok(()) + } else { + Err(ApiError::openapi_upload(locale, "source_integrity")) + } +} + +pub(super) fn preview_has_blocker( + preview: &crank_import::rest::ImportPreview, + selected_operation_keys: &BTreeSet, +) -> bool { + preview + .findings + .iter() + .any(|finding| finding.severity == ImportFindingSeverity::Error) + || preview + .groups + .iter() + .flat_map(|group| group.operations.iter()) + .filter(|operation| selected_operation_keys.contains(&operation.key)) + .flat_map(|operation| operation.findings.iter()) + .any(|finding| finding.severity == ImportFindingSeverity::Error) +} diff --git a/apps/admin-api/tests/config_startup.rs b/apps/admin-api/tests/config_startup.rs index bb3f6cc..182d610 100644 --- a/apps/admin-api/tests/config_startup.rs +++ b/apps/admin-api/tests/config_startup.rs @@ -111,7 +111,7 @@ async fn real_admin_startup_runs_immediate_import_maintenance() { .fetch_one(&pool) .await .unwrap(), - Some(13), + Some(14), "migration binary must install the current canonical ledger", ); sqlx::query( diff --git a/apps/admin-api/tests/integration/openapi_import.rs b/apps/admin-api/tests/integration/openapi_import.rs index acc294a..4917473 100644 --- a/apps/admin-api/tests/integration/openapi_import.rs +++ b/apps/admin-api/tests/integration/openapi_import.rs @@ -11,6 +11,8 @@ use super::common::{ mod external_references; mod http_contract; +mod import_plan_apply; +mod import_plan_create; const OPENAPI3: &str = r#" openapi: 3.0.3 diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs new file mode 100644 index 0000000..a6f7372 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs @@ -0,0 +1,664 @@ +use serde_json::{Value, json}; +use serial_test::serial; + +use super::super::common::*; + +const OPENAPI_SINGLE: &str = r#" +openapi: 3.0.3 +info: { title: Apply test } +servers: [{ url: https://api.example.test }] +paths: + /alpha: + get: + operationId: alpha + responses: { '200': { description: ok } } +"#; + +const OPENAPI_TWO_OPERATIONS: &str = r#" +openapi: 3.0.3 +info: { title: Partial Apply test } +servers: [{ url: https://api.example.test }] +paths: + /alpha: + get: + operationId: alpha + responses: { '200': { description: ok } } + /beta: + get: + operationId: beta + responses: { '200': { description: ok } } +"#; + +const OPENAPI_WITH_BLOCKER: &str = r#" +openapi: 3.0.3 +info: { title: Blocked full Apply test } +servers: [{ url: https://api.example.test }] +paths: + /actionable: + get: + operationId: actionable + responses: { '200': { description: ok } } + /blocked: + post: + operationId: blocked + requestBody: + required: true + content: + application/xml: + schema: { type: string } + responses: { '200': { description: ok } } +"#; + +struct CreatedPlan { + reference: String, + digest: String, + body: Value, +} + +async fn create_plan( + client: &reqwest::Client, + base_url: impl AsRef, + document: &str, + plan_payload: Value, +) -> CreatedPlan { + let base_url = base_url.as_ref(); + let upload = reqwest::multipart::Part::bytes(document.as_bytes().to_vec()) + .file_name("import.yaml") + .mime_str("application/yaml") + .unwrap(); + let preview_response = client + .post(format!("{base_url}/imports/openapi/preview")) + .multipart(reqwest::multipart::Form::new().part("file", upload)) + .send() + .await + .expect("OpenAPI preview request should reach the Admin API"); + assert_eq!( + preview_response.status(), + reqwest::StatusCode::OK, + "preview failed: {}", + preview_response.text().await.unwrap_or_default() + ); + let preview: Value = preview_response.json().await.unwrap(); + let job_id = preview["job_id"] + .as_str() + .expect("preview response must include the persisted job ID"); + + let plan_response = client + .post(format!("{base_url}/imports/openapi/{job_id}/plans")) + .json(&plan_payload) + .send() + .await + .expect("ImportPlan request should reach the Admin API"); + let status = plan_response.status(); + let body: Value = plan_response.json().await.unwrap_or(Value::Null); + assert_eq!( + status, + reqwest::StatusCode::OK, + "plan creation failed: {body}" + ); + let reference = body["plan_reference"] + .as_str() + .expect("CreatePlan must return its server-issued reference") + .to_owned(); + let digest = body["plan_digest"] + .as_str() + .expect("CreatePlan must return its server-derived digest") + .to_owned(); + assert!(reference.starts_with("plan_")); + assert_eq!(digest.len(), 64); + assert!(digest.bytes().all(|byte| byte.is_ascii_hexdigit())); + CreatedPlan { + reference, + digest, + body, + } +} + +async fn apply_plan( + client: &reqwest::Client, + base_url: impl AsRef, + plan: &CreatedPlan, + idempotency_key: &str, +) -> (reqwest::StatusCode, Value) { + let base_url = base_url.as_ref(); + let response = client + .post(format!( + "{base_url}/imports/openapi/{}/apply", + plan.reference + )) + .json(&json!({ + "plan_digest": plan.digest, + "idempotency_key": idempotency_key + })) + .send() + .await + .expect("Apply request should reach the Admin API"); + let status = response.status(); + let body = response.json::().await.unwrap_or(Value::Null); + (status, body) +} + +async fn operation_list(client: &reqwest::Client, base_url: impl AsRef) -> Value { + let base_url = base_url.as_ref(); + let response = client + .get(format!("{base_url}/operations")) + .send() + .await + .expect("operation list request should reach the Admin API"); + assert_eq!(response.status(), reqwest::StatusCode::OK); + response.json().await.unwrap() +} + +fn full_plan() -> Value { + json!({ "selection_mode": "full", "selected_operation_keys": [] }) +} + +fn report_has_outcome_status(report: &Value, expected: &str) -> bool { + report["outcomes"].as_array().is_some_and(|outcomes| { + outcomes + .iter() + .any(|outcome| outcome["status"] == expected || outcome["outcome"] == expected) + }) +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn apply_creates_one_draft_from_a_server_created_plan() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_new_draft"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + assert_eq!(plan.body["counts"]["actionable"], 1); + + let (status, report) = apply_plan(&client, &base_url, &plan, "apply-new-draft").await; + + assert_eq!(status, reqwest::StatusCode::OK, "Apply failed: {report}"); + assert_eq!(report["plan_digest"], plan.digest); + assert_eq!(report["outcomes"].as_array().unwrap().len(), 1); + assert!(report_has_outcome_status(&report, "created")); + assert!(report["outcomes"].to_string().contains("GET /alpha")); + let operations = operation_list(&client, &base_url).await; + assert_eq!(operations["total"], 1); + assert_eq!(operations["items"][0]["status"], "draft"); + assert_eq!(operations["items"][0]["current_draft_version"], 1); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn full_apply_with_a_blocked_candidate_is_atomic_and_reports_the_failure() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_full_blocked"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan(&client, &base_url, OPENAPI_WITH_BLOCKER, full_plan()).await; + assert_eq!(plan.body["counts"]["blocked"], 1); + let before = operation_list(&client, &base_url).await; + + let (status, report) = apply_plan(&client, &base_url, &plan, "apply-full-blocked").await; + + assert_eq!( + status, + reqwest::StatusCode::CONFLICT, + "unexpected report: {report}" + ); + assert_eq!(operation_list(&client, &base_url).await, before); + assert!( + report["outcomes"].is_array(), + "blocked Apply must return outcomes: {report}" + ); + assert!(report_has_outcome_status(&report, "rejected")); + assert!(report["outcomes"].to_string().contains("POST /blocked")); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn explicit_partial_plan_applies_only_selected_candidates_and_explains_exclusions() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_partial"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan( + &client, + &base_url, + OPENAPI_TWO_OPERATIONS, + json!({ + "selection_mode": "partial", + "selected_operation_keys": ["GET /alpha"] + }), + ) + .await; + assert_eq!(plan.body["counts"]["selected"], 1); + assert_eq!(plan.body["counts"]["excluded"], 1); + + let (status, report) = apply_plan(&client, &base_url, &plan, "apply-partial").await; + + assert_eq!(status, reqwest::StatusCode::OK, "Apply failed: {report}"); + assert!(report["outcomes"].is_array()); + assert!(report_has_outcome_status(&report, "created")); + let report_text = report.to_string(); + assert!(report_text.contains("GET /alpha")); + assert!(report_text.contains("GET /beta")); + assert!(report_text.contains("excluded")); + let operations = operation_list(&client, &base_url).await; + assert_eq!(operations["total"], 1); + assert_eq!(operations["items"][0]["name"], "alpha"); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn same_key_and_digest_replay_report_without_extra_draft_and_changed_digest_conflicts() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_idempotency"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + let (first_status, first_report) = + apply_plan(&client, &base_url, &plan, "stable-apply-key").await; + assert_eq!( + first_status, + reqwest::StatusCode::OK, + "Apply failed: {first_report}" + ); + let operations_after_first = operation_list(&client, &base_url).await; + + let (replay_status, replay_report) = + apply_plan(&client, &base_url, &plan, "stable-apply-key").await; + assert_eq!(replay_status, reqwest::StatusCode::OK); + assert_eq!(replay_report, first_report); + assert_eq!( + operation_list(&client, &base_url).await, + operations_after_first + ); + + let changed = CreatedPlan { + reference: plan.reference.clone(), + digest: "0".repeat(64), + body: plan.body.clone(), + }; + let (changed_status, _) = apply_plan(&client, &base_url, &changed, "stable-apply-key").await; + assert_eq!(changed_status, reqwest::StatusCode::CONFLICT); + assert_eq!( + operation_list(&client, &base_url).await, + operations_after_first + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn concurrent_same_key_apply_converges_on_one_persisted_report() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_concurrent_replay"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + + let (left, right) = tokio::join!( + apply_plan(&client, &base_url, &plan, "concurrent-apply-key"), + apply_plan(&client, &base_url, &plan, "concurrent-apply-key"), + ); + + assert_eq!(left.0, reqwest::StatusCode::OK, "left: {}", left.1); + assert_eq!(right.0, reqwest::StatusCode::OK, "right: {}", right.1); + assert_eq!(left.1, right.1); + assert_eq!(operation_list(&client, &base_url).await["total"], 1); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn apply_rejects_client_candidate_fields_and_malformed_digest() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_input_validation"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + let url = format!("{base_url}/imports/openapi/{}/apply", plan.reference); + + let extra_fields = client + .post(&url) + .json(&json!({ + "plan_digest": plan.digest, + "idempotency_key": "validation-key", + "operations": [{ "operation_id": "attacker-chosen" }] + })) + .send() + .await + .unwrap(); + assert_eq!( + extra_fields.status(), + reqwest::StatusCode::UNPROCESSABLE_ENTITY + ); + + let malformed_digest = client + .post(url) + .json(&json!({ + "plan_digest": "not-a-digest", + "idempotency_key": "validation-key" + })) + .send() + .await + .unwrap(); + assert_eq!(malformed_digest.status(), reqwest::StatusCode::BAD_REQUEST); + assert_eq!(operation_list(&client, &base_url).await["total"], 0); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn exact_skip_and_rename_decisions_affect_only_the_confirmed_operations() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_skip_rename"), + )) + .await; + let client = authorized_client(&base_url).await; + + let initial = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + let (initial_status, initial_report) = + apply_plan(&client, &base_url, &initial, "skip-rename-seed").await; + assert_eq!(initial_status, reqwest::StatusCode::OK, "{initial_report}"); + let before_skip = operation_list(&client, &base_url).await; + let operation_id = before_skip["items"][0]["id"] + .as_str() + .expect("operation list includes the workspace operation ID"); + + let skip = create_plan( + &client, + &base_url, + OPENAPI_SINGLE, + json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /alpha", + "action": "skip", + "target_operation_id": operation_id + }] + }), + ) + .await; + let (skip_status, skip_report) = + apply_plan(&client, &base_url, &skip, "skip-exact-target").await; + assert_eq!(skip_status, reqwest::StatusCode::OK, "{skip_report}"); + assert!(report_has_outcome_status(&skip_report, "skipped")); + assert_eq!(skip_report["outcomes"][0]["operation_id"], operation_id); + assert_eq!(operation_list(&client, &base_url).await, before_skip); + + let rename = create_plan( + &client, + &base_url, + OPENAPI_SINGLE, + json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /alpha", + "action": "rename", + "confirmed_name": "alpha_imported" + }] + }), + ) + .await; + let (rename_status, rename_report) = + apply_plan(&client, &base_url, &rename, "rename-confirmed-name").await; + assert_eq!(rename_status, reqwest::StatusCode::OK, "{rename_report}"); + assert!(report_has_outcome_status(&rename_report, "renamed")); + let after_rename = operation_list(&client, &base_url).await; + assert_eq!(after_rename["total"], 2); + assert!( + after_rename["items"] + .as_array() + .unwrap() + .iter() + .any(|item| { item["name"] == "alpha_imported" && item["status"] == "draft" }) + ); + assert!( + after_rename["items"] + .as_array() + .unwrap() + .iter() + .any(|item| { item["id"] == operation_id && item["name"] == "alpha" }) + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn new_version_preserves_published_version_and_prior_version() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_new_version_published"), + )) + .await; + let client = authorized_client(&base_url).await; + let seed = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + let (seed_status, seed_report) = + apply_plan(&client, &base_url, &seed, "new-version-seed").await; + assert_eq!(seed_status, reqwest::StatusCode::OK, "{seed_report}"); + let seeded = operation_list(&client, &base_url).await; + let operation_id = seeded["items"][0]["id"].as_str().unwrap().to_owned(); + + let publish = client + .post(format!("{base_url}/operations/{operation_id}/publish")) + .header( + reqwest::header::IF_MATCH, + operation_etag(&client, &base_url, &operation_id).await, + ) + .json(&json!({ "version": 1 })) + .send() + .await + .expect("publish request should reach the Admin API"); + assert_eq!(publish.status(), reqwest::StatusCode::OK); + let published: Value = publish.json().await.unwrap(); + assert_eq!(published["published_version"], 1); + let prior_version_before: Value = client + .get(format!("{base_url}/operations/{operation_id}/versions/1")) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + + let plan = create_plan( + &client, + &base_url, + OPENAPI_SINGLE, + json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /alpha", + "action": "new_version", + "target_operation_id": operation_id + }] + }), + ) + .await; + let (status, report) = apply_plan(&client, &base_url, &plan, "new-version-exact-target").await; + assert_eq!(status, reqwest::StatusCode::OK, "{report}"); + assert!(report_has_outcome_status(&report, "versioned")); + assert_eq!(report["outcomes"][0]["operation_id"], operation_id); + assert_eq!(report["outcomes"][0]["version"], 2); + + let after = operation_list(&client, &base_url).await; + assert_eq!(after["total"], 1); + assert_eq!(after["items"][0]["id"], operation_id); + assert_eq!(after["items"][0]["latest_published_version"], 1); + assert_eq!(after["items"][0]["current_draft_version"], 2); + let published_again: Value = client + .get(format!("{base_url}/operations/{operation_id}/versions/1")) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + assert_eq!(published_again, prior_version_before); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn disabled_auth_secret_rejects_apply_without_leaking_credentials() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry.clone(), + test_storage_root("import_plan_apply_revoked_auth"), + )) + .await; + let client = authorized_client(&base_url).await; + let secret_canary = "import-plan-auth-canary-must-not-leak"; + let secret_response = client + .post(format!("{base_url}/secrets")) + .json(&json!({ + "name": "import-plan-auth-token", + "kind": "token", + "value": { "token": secret_canary } + })) + .send() + .await + .unwrap(); + assert_eq!(secret_response.status(), reqwest::StatusCode::OK); + let secret: Value = secret_response.json().await.unwrap(); + let secret_id = secret["id"].as_str().unwrap(); + let profile_response = client + .post(format!("{base_url}/auth-profiles")) + .json(&json!({ + "name": "import-plan-auth-profile", + "kind": "api_key_header", + "config": { + "api_key_header": { + "header_name": "X-Api-Key", + "secret_id": secret_id + } + } + })) + .send() + .await + .unwrap(); + assert_eq!(profile_response.status(), reqwest::StatusCode::OK); + let profile: Value = profile_response.json().await.unwrap(); + let auth_profile_id = profile["id"].as_str().unwrap(); + + let redacted_plan = create_plan( + &client, + &base_url, + OPENAPI_TWO_OPERATIONS, + json!({ + "selection_mode": "partial", + "selected_operation_keys": ["GET /beta"], + "decisions": [{ + "operation_key": "GET /beta", + "action": "create", + "auth_profile_id": auth_profile_id + }] + }), + ) + .await; + let (redacted_status, redacted_report) = + apply_plan(&client, &base_url, &redacted_plan, "redacted-auth-report").await; + assert_eq!( + redacted_status, + reqwest::StatusCode::OK, + "{redacted_report}" + ); + assert!(redacted_report["source_digest"].is_string()); + assert!(!redacted_report.to_string().contains(secret_canary)); + assert!(!redacted_report.to_string().contains("ciphertext")); + + let plan = create_plan( + &client, + &base_url, + OPENAPI_SINGLE, + json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /alpha", + "action": "create", + "auth_profile_id": auth_profile_id + }] + }), + ) + .await; + + // A profile can remain active while its credential mapping changes. + // The confirmed plan must not silently adopt the new configuration. + sqlx::query( + "update auth_profiles + set config_json = jsonb_set(config_json, '{api_key_header,header_name}', '\"X-Changed-Key\"'), + updated_at = now() + where workspace_id = $1 and id = $2", + ) + .bind("ws_default") + .bind(auth_profile_id) + .execute(registry.pool()) + .await + .unwrap(); + let (drift_status, drift_error) = + apply_plan(&client, &base_url, &plan, "changed-auth-config-apply").await; + assert_eq!(drift_status, reqwest::StatusCode::CONFLICT, "{drift_error}"); + assert_eq!( + drift_error["error"]["context"]["error_code"], + "import_plan_auth_unavailable" + ); + assert_eq!(operation_list(&client, &base_url).await["total"], 1); + + let plan = create_plan( + &client, + &base_url, + OPENAPI_SINGLE, + json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /alpha", + "action": "create", + "auth_profile_id": auth_profile_id + }] + }), + ) + .await; + + // There is no profile revoke endpoint; disabling its backing credential + // models revocation between confirmation and apply. + sqlx::query( + "update secrets set status = 'disabled', updated_at = now() where workspace_id = $1 and id = $2", + ) + .bind("ws_default") + .bind(secret_id) + .execute(registry.pool()) + .await + .unwrap(); + + let (status, error) = apply_plan(&client, &base_url, &plan, "revoked-auth-apply").await; + assert_eq!(status, reqwest::StatusCode::CONFLICT, "{error}"); + assert_eq!( + error["error"]["context"]["error_code"], + "import_plan_auth_unavailable" + ); + let response_text = error.to_string(); + assert!(!response_text.contains(secret_canary)); + assert!(!response_text.contains("ciphertext")); + let after = operation_list(&client, &base_url).await; + assert_eq!(after["total"], 1); + assert_eq!(after["items"][0]["name"], "beta"); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs new file mode 100644 index 0000000..77598ea --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs @@ -0,0 +1,952 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_full"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let job = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap(); + + let plan = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .unwrap(); + + assert!(plan.plan_reference.starts_with("plan_")); + assert!( + plan.plan_digest.len() == 64 + && plan + .plan_digest + .bytes() + .all(|byte| byte.is_ascii_hexdigit()) + ); + assert_eq!(plan.selection_mode, "full"); + assert_eq!(plan.counts.selected, 1); + assert_eq!(plan.counts.excluded, 0); + assert_eq!(plan.counts.actionable, 1); + assert_eq!(plan.counts.blocked, 0); + let candidate = &plan.candidates[0]; + assert_eq!(candidate.key, "GET /v2/latest"); + assert_eq!(candidate.name, "latest_rates"); + assert_eq!(candidate.state, "actionable"); + assert_eq!(candidate.action.as_deref(), Some("create")); + assert_eq!(candidate.reason, None); + + let expires_at = OffsetDateTime::parse( + &plan.expires_at, + &time::format_description::well_known::Rfc3339, + ) + .unwrap(); + assert!(expires_at > OffsetDateTime::now_utc()); + assert!(expires_at <= job.expires_at); + + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .expect("the created plan must be readable in its workspace"); + assert_eq!(stored.digest, plan.plan_digest); + assert_eq!(stored.expires_at, expires_at); + assert_eq!( + stored.payload["selection"]["selected"], + json!(["GET /v2/latest"]) + ); + assert_eq!(stored.payload["selection"]["excluded"], json!([])); + assert_eq!(stored.payload["counts"]["actionable"], json!(1)); + let candidate = &stored.payload["candidates"][0]; + assert_eq!(candidate["decision"]["action"], "create"); + assert!( + candidate["operation"]["id"] + .as_str() + .unwrap() + .starts_with("op_") + ); + assert_eq!(candidate["operation"]["status"], "draft"); + assert_eq!( + stored.payload["source"]["digest"], + job.preview_payload["source"]["digest"] + ); + assert!(stored.payload["fingerprint"]["ir_fingerprint"].is_string()); + // Planning is non-mutating: no operation exists and the job stays pending. + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); + let job = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap(); + assert_eq!(job.status, ImportJobStatus::Pending); + assert_eq!(job.error_text, None); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn partial_plan_counts_selection_and_rejects_invalid_request_material() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_partial"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_TWO_OPS)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let keys = preview_operation_keys(&preview); + assert_eq!(keys.len(), 2); + let (first, second) = (keys[0].clone(), keys[1].clone()); + + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload("partial", vec![first.clone()], vec![]), + ) + .await + .unwrap(); + assert_eq!(plan.counts.selected, 1); + assert_eq!(plan.counts.excluded, 1); + assert_eq!(plan.counts.actionable, 1); + assert_eq!(plan.counts.blocked, 0); + let excluded = plan + .candidates + .iter() + .find(|candidate| candidate.key == second) + .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(); + let expected_all = json!([first, second]); + assert_eq!(stored.payload["selection"]["all"], expected_all); + // `excluded` candidates never receive a decision, target or draft material. + let listed = stored.payload["candidates"].as_array().unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0]["key"], first); + assert!(listed[0].get("target").is_none()); + + let cases: Vec<(OpenApiImportPlanCreatePayload, &'static str)> = vec![ + ( + plan_payload("full", vec![first.clone()], vec![]), + "import_plan_selection_mismatch", + ), + ( + plan_payload("partial", vec!["GET /unknown".to_owned()], vec![]), + "import_plan_unknown_operation_key", + ), + ( + plan_payload("partial", vec![], vec![]), + "import_plan_invalid_input", + ), + ( + plan_payload("partial", vec![first.clone(), first.clone()], vec![]), + "import_plan_invalid_input", + ), + ( + plan_payload("bogus", vec![first.clone()], vec![]), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone()], + vec![decision("GET /other", "create", None, None, None)], + ), + "import_plan_decision_unselected", + ), + ( + plan_payload( + "partial", + vec![first.clone(), second.clone()], + vec![ + decision(&first, "create", None, None, None), + decision(&first, "skip", Some("op_other"), None, None), + ], + ), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone()], + vec![decision(&first, "skip", None, None, None)], + ), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone()], + vec![decision( + &first, + "skip", + Some("op_other"), + Some("other_name"), + None, + )], + ), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone(), second.clone()], + vec![decision( + &first, + "skip", + Some("op_other"), + None, + Some("auth_profile_any"), + )], + ), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone(), second.clone()], + vec![ + decision(&first, "create", None, Some("renamed"), None), + decision(&second, "rename", Some("op_other"), None, None), + ], + ), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone()], + vec![decision(&first, "rename", None, None, None)], + ), + "import_plan_invalid_input", + ), + ( + plan_payload( + "partial", + vec![first.clone()], + vec![decision(&first, "new_version", None, None, None)], + ), + "import_plan_invalid_input", + ), + ]; + for (payload, expected_code) in cases { + let error = service + .create_openapi_import_plan(&workspace_id, &job_id, payload) + .await + .expect_err("invalid plan material must be rejected"); + assert_error_code(&error, expected_code); + assert_http_status(error, axum::http::StatusCode::BAD_REQUEST); + } + // The successful plan above still exists untouched. + let readable = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap(); + assert!(readable.is_some()); + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn client_supplied_candidate_material_has_no_authority() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_client_authority"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + + // Client-supplied digests, payloads or candidate arrays are not part of + // the request contract and must not influence the server-authored plan. + let request = json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "plan_digest": format!("sha256:{}", "ab".repeat(32)), + "plan_payload": { + "schema_version": "import-plan-client-v999", + "candidates": [{ "key": "GET /forged", "name": "forged" }], + }, + "candidates": [{ "key": "GET /forged" }], + }); + assert!( + serde_json::from_value::(request).is_err(), + "client-supplied candidate material must be rejected at the request boundary" + ); + let payload: OpenApiImportPlanCreatePayload = serde_json::from_value(json!({ + "selection_mode": "full", + "selected_operation_keys": [] + })) + .unwrap(); + let plan = service + .create_openapi_import_plan(&workspace_id, &job_id, payload) + .await + .unwrap(); + + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + let material = stored.payload.to_string(); + assert!(!material.contains("import-plan-client-v999")); + assert!(!material.contains("GET /forged")); + assert!(!material.contains("abababab")); + assert_eq!( + stored.payload["selection"]["all"], + json!(["GET /v2/latest"]) + ); + assert_eq!(stored.payload["schema_version"], "import-plan-v1"); + assert!(stored.digest.len() == 64 && stored.digest.bytes().all(|b| b.is_ascii_hexdigit())); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_creation_is_workspace_scoped() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_scope"), + test_auth_settings(), + test_secret_crypto(), + ); + let primary = WorkspaceId::new("ws_default"); + let secondary = WorkspaceId::new("ws_plan_scope_secondary"); + let created_at = OffsetDateTime::now_utc(); + registry + .create_workspace(CreateWorkspaceRequest { + workspace: &Workspace { + id: secondary.clone(), + slug: "ws-plan-scope-secondary".to_owned(), + display_name: "Plan scope secondary".to_owned(), + status: WorkspaceStatus::Active, + settings: json!({}), + created_at, + updated_at: created_at, + }, + }) + .await + .unwrap(); + + let preview = service + .preview_openapi_import(&primary, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan(&primary, &job_id, plan_payload("full", vec![], vec![])) + .await + .unwrap(); + + let cross_scope_error = service + .create_openapi_import_plan(&secondary, &job_id, plan_payload("full", vec![], vec![])) + .await + .expect_err("a foreign job must be invisible, not planned"); + assert_http_status(cross_scope_error, axum::http::StatusCode::NOT_FOUND); + let stored = registry + .get_import_plan(&secondary, &plan.plan_reference.as_str().into()) + .await + .unwrap(); + assert!(stored.is_none(), "a foreign plan id must be invisible"); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn exact_skip_rename_and_new_version_decisions_are_saved_verbatim() { + let registry = test_registry().await; + let upstream_base_url = spawn_upstream_server().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_decisions"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + + // A conflicting existing operation so `create` is no longer safe by default. + let existing = service + .create_operation( + &workspace_id, + test_operation_payload(&upstream_base_url, "latest_rates"), + ) + .await + .unwrap(); + let operation_id = existing.operation_id; + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "skip", + Some(operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .expect("skip must target the exact existing operation"); + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + let candidate = &stored.payload["candidates"][0]; + assert_eq!(plan.counts.actionable, 1); + assert_eq!(candidate["decision"]["action"], "skip"); + assert_eq!(candidate["target"]["operation_id"], json!(operation_id)); + assert_eq!( + candidate["target"]["expected_state"], + json!({ + "status": "draft", + "current_draft_version": 1, + "latest_published_version": null + }) + ); + assert!( + candidate.get("operation").is_none(), + "skip must not create draft material" + ); + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let renamed = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "rename", + None, + Some("latest_rates_confirmed"), + None, + )], + ), + ) + .await + .unwrap(); + let stored = registry + .get_import_plan(&workspace_id, &renamed.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!( + stored.payload["candidates"][0]["decision"]["confirmed_name"], + "latest_rates_confirmed" + ); + assert_eq!( + stored.payload["candidates"][0]["operation"]["name"], + "latest_rates_confirmed" + ); + assert!( + stored.payload["candidates"][0]["operation"]["id"] + .as_str() + .unwrap() + .starts_with("op_") + ); + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let versioned = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "new_version", + Some(operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .unwrap(); + let stored = registry + .get_import_plan(&workspace_id, &versioned.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + let candidate = &stored.payload["candidates"][0]; + assert_eq!( + candidate["operation"]["id"], operation_id, + "new_version snapshot must bind the exact target operation" + ); + assert_eq!(candidate["operation"]["version"], 2); + assert_eq!( + candidate["target"]["expected_state"], + json!({ + "status": "draft", + "current_draft_version": 1, + "latest_published_version": null + }) + ); + + // Planning never mutates: only the original Draft Version exists. + let operations = service.list_operations(&workspace_id).await.unwrap(); + assert_eq!(operations.len(), 1); + assert_eq!(operations[0].name, "latest_rates"); + assert_eq!(operations[0].current_draft_version, 1); + + // A mismatched target is rejected with the stable target code. + let other = service + .create_operation( + &workspace_id, + test_operation_payload(&upstream_base_url, "other_operation"), + ) + .await + .unwrap(); + for mismatched in [ + vec![decision( + "GET /v2/latest", + "skip", + Some(other.operation_id.as_str()), + None, + None, + )], + vec![decision( + "GET /v2/latest", + "new_version", + Some(other.operation_id.as_str()), + None, + None, + )], + ] { + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let error = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload("full", vec![], mismatched), + ) + .await + .expect_err("a mismatched target must be rejected"); + assert_error_code(&error, "import_plan_target_invalid"); + assert_http_status(error, axum::http::StatusCode::BAD_REQUEST); + } + + // An explicit `create` against a taken name conflicts; the server never + // auto-selects a replacement name. + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let error = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision("GET /v2/latest", "create", None, None, None)], + ), + ) + .await + .expect_err("create with a taken name must conflict"); + assert_error_code(&error, "import_plan_name_conflict"); + assert_http_status(error, axum::http::StatusCode::CONFLICT); + + // Without an explicit decision the same collision stays a bounded, + // redacted blocked candidate inside the immutable plan. + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .unwrap(); + assert_eq!(plan.counts.actionable, 0); + assert_eq!(plan.counts.blocked, 1); + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + let candidate = &stored.payload["candidates"][0]; + assert_eq!(candidate["state"], "blocked"); + assert_eq!(candidate["reason"], "operation_name_conflict"); + assert!(candidate.get("operation").is_none()); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn auth_profile_references_are_validated_stored_and_inherited() { + let registry = test_registry().await; + let upstream_base_url = spawn_upstream_server().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_auth"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + + let secret_token = "plan-secret-canary-2-13"; + let secret = service + .create_secret( + &workspace_id, + None, + SecretPayload { + name: "plan-auth-token".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": secret_token }), + }, + None, + ) + .await + .unwrap(); + let auth_profile = service + .create_auth_profile( + &workspace_id, + AuthProfilePayload { + name: "plan-auth-profile".to_owned(), + kind: AuthKind::ApiKeyHeader, + config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-Api-Key".to_owned(), + secret_id: SecretId::new(secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "create", + None, + None, + Some(auth_profile.id.as_str()), + )], + ), + ) + .await + .unwrap(); + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!( + stored.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"], + auth_profile.id.as_str() + ); + // Only the reference survives: no secret material anywhere in the plan. + assert!(!stored.payload.to_string().contains(secret_token)); + assert_eq!( + plan.candidates.first().unwrap().auth_profile_id.as_deref(), + Some(auth_profile.id.as_str()) + ); + + // An unknown profile reference fails closed in the workspace scope check. + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let error = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "create", + None, + None, + Some("auth_missing_profile"), + )], + ), + ) + .await + .expect_err("an unknown auth profile reference must be rejected"); + assert_error_code(&error, "operation_auth_profile_invalid"); + assert_http_status(error, axum::http::StatusCode::UNPROCESSABLE_ENTITY); + + // `new_version` without an explicit choice inherits the target's ref. + let existing = service + .create_operation(&workspace_id, { + let mut payload = test_operation_payload(&upstream_base_url, "latest_rates"); + payload.execution_config.auth_profile_ref = + Some(AuthProfileId::new(auth_profile.id.as_str())); + payload + }) + .await + .expect("an existing operation with the candidate name"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "new_version", + Some(existing.operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .unwrap(); + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + let candidate = &stored.payload["candidates"][0]; + assert_eq!( + candidate["decision"]["auth_profile_id"], + auth_profile.id.as_str(), + "the inherited target reference is reflected in the candidate summary" + ); + assert_eq!( + candidate["operation"]["execution_config"]["auth_profile_ref"], + auth_profile.id.as_str(), + "the inherited target reference must be retained in the snapshot" + ); + assert!(!stored.payload.to_string().contains(secret_token)); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn stale_or_terminal_jobs_preview_again_without_mutating_the_job() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_stale"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + sqlx::query( + "update import_jobs + set preview_payload = jsonb_set(preview_payload, '{preview_digest}', to_jsonb($1::text)) + where id = $2", + ) + .bind("0".repeat(64)) + .bind(job_id.as_str()) + .execute(registry.pool()) + .await + .unwrap(); + + let error = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .expect_err("a stale preview contract must be rejected"); + assert_error_code(&error, "import_replay_verification_failed"); + let context = public_error_context(&error).cloned().unwrap(); + assert_eq!(context["repreview_required"], json!(true)); + assert_eq!( + context.get("recovery").and_then(Value::as_str), + Some("repreview_required") + ); + assert_http_status(error, axum::http::StatusCode::CONFLICT); + + // Unlike the legacy path the job itself is untouched: other stored + // plans anchored to it must remain readable. + let job = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap(); + assert_eq!(job.status, ImportJobStatus::Pending); + assert_eq!(job.error_text, None); + assert!(job.finished_at.is_none()); + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); + + let completed_at = OffsetDateTime::now_utc(); + registry + .finish_import_job(FinishImportJobRequest { + id: &job_id, + status: ImportJobStatus::Failed, + created_operation_ids: &json!([]), + error_text: Some("test_finalized"), + finished_at: &completed_at, + }) + .await + .unwrap(); + let error = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .expect_err("a terminal job cannot be planned"); + assert_error_code(&error, "import_plan_job_terminal"); + assert_http_status(error, axum::http::StatusCode::CONFLICT); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn public_plan_route_returns_bounded_confirmation_and_requires_a_session() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_plan_http"); + let service = test_service( + registry.clone(), + storage_root.clone(), + test_auth_settings(), + test_secret_crypto(), + ); + let app = build_test_app(registry.clone(), storage_root); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id = preview.job_id.clone(); + + let server = spawn_admin_api(app).await; + let client = authorized_client(&server).await; + let url = format!("{}/imports/openapi/{}/plans", server.as_ref(), job_id); + + // Without a session (and thus without a workspace owner context) the + // public contract refuses and no plan is created. + let anonymous = reqwest::Client::new(); + let denied = anonymous + .post(&url) + .json(&json!({ "selection_mode": "full", "selected_operation_keys": [] })) + .send() + .await + .unwrap(); + assert_eq!(denied.status(), reqwest::StatusCode::UNAUTHORIZED); + + let accepted = client + .post(&url) + .json(&json!({ "selection_mode": "full", "selected_operation_keys": [] })) + .send() + .await + .unwrap(); + assert_eq!(accepted.status(), reqwest::StatusCode::OK); + let body: Value = accepted.json().await.unwrap(); + let plan_reference = body["plan_reference"].as_str().unwrap_or_default(); + assert!( + plan_reference.starts_with("plan_"), + "plan reference must be server-issued: {body:?}" + ); + let plan_digest = body["plan_digest"].as_str().unwrap_or_default(); + assert!( + plan_digest.len() == 64 && plan_digest.bytes().all(|b| b.is_ascii_hexdigit()), + "plan digest must be the registry-derived SHA-256: {body:?}" + ); + assert_eq!(body["selection_mode"], "full"); + assert_eq!( + body["counts"], + json!({ "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }) + ); + assert_eq!(body["candidates"].as_array().unwrap().len(), 1); + assert_eq!(body["candidates"][0]["key"], "GET /v2/latest"); + assert_eq!(body["candidates"][0]["state"], "actionable"); + assert_eq!(body["candidates"][0]["action"], "create"); + assert!(body["expires_at"].is_string()); + // The HTTP response is the bounded confirmation only: no draft snapshot, + // no raw preview material, no secret material. + assert!(body["candidates"][0].get("operation").is_none()); + assert!(!body.to_string().to_lowercase().contains("password")); + + // The plan is readable in the same workspace right after the 200. + let plan_id = crank_registry::ImportPlanId::new(plan_reference); + let readable = registry + .get_import_plan(&workspace_id, &plan_id) + .await + .unwrap() + .expect("the HTTP-created plan must be persisted"); + assert_eq!(readable.digest, plan_digest); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/mod.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/mod.rs new file mode 100644 index 0000000..e7d58bf --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/mod.rs @@ -0,0 +1,163 @@ +use admin_api::{ + error::ApiError, + service::{ + AuthProfilePayload, OpenApiImportPlanCreatePayload, OpenApiImportPlanDecision, + OpenApiUpload, OpenApiUploadLocale, SecretPayload, + }, +}; +use axum::response::IntoResponse; +use crank_core::{ + ApiKeyHeaderAuthConfig, AuthConfig, AuthKind, AuthProfileId, OperationId, SecretId, SecretKind, + Workspace, WorkspaceId, WorkspaceStatus, +}; +use crank_registry::{ + CreateWorkspaceRequest, FinishImportJobRequest, ImportJobId, ImportJobStatus, +}; +use serde_json::{Value, json}; +use serial_test::serial; +use time::OffsetDateTime; + +use super::super::common::*; +const OPENAPI_SINGLE: &str = r#" +openapi: 3.0.3 +info: + title: Frankfurter API +servers: + - url: https://api.frankfurter.dev +paths: + /v2/latest: + get: + operationId: latestRates + summary: Получить последние курсы валют + tags: [currency] + parameters: + - name: base + in: query + required: true + schema: { type: string } + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + base: { type: string } + rates: + type: object +"#; + +const OPENAPI_TWO_OPS: &str = r#" +openapi: 3.0.3 +info: { title: Two Operations } +servers: [{ url: https://api.example.test }] +paths: + /alpha: + get: + operationId: alpha + responses: { '200': { description: ok } } + /beta: + get: + operationId: betaOp + responses: { '200': { description: ok } } +"#; + +const OPENAPI_GLOBAL_BLOCKER: &str = r#" +openapi: 3.0.3 +info: { title: Global blocker } +servers: [{ url: https://api.example.test }] +paths: + /valid: + get: + responses: { '200': { description: ok } } + /broken: not-a-path-item +"#; + +fn upload(document: &str) -> OpenApiUpload { + OpenApiUpload { + bytes: document.as_bytes().to_vec(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + } +} + +fn preview_operation_keys( + preview: &admin_api::service::OpenApiImportPreviewResponse, +) -> Vec { + let mut keys = preview + .preview + .groups + .iter() + .flat_map(|group| &group.operations) + .map(|operation| operation.key.clone()) + .collect::>(); + keys.sort(); + keys +} + +fn decision( + key: &str, + action: &str, + target: Option<&str>, + name: Option<&str>, + auth: Option<&str>, +) -> OpenApiImportPlanDecision { + OpenApiImportPlanDecision { + operation_key: key.to_owned(), + action: action.to_owned(), + target_operation_id: target.map(ToOwned::to_owned), + confirmed_name: name.map(ToOwned::to_owned), + auth_profile_id: auth.map(ToOwned::to_owned), + } +} + +fn plan_payload( + selection_mode: &str, + selected: Vec, + decisions: Vec, +) -> OpenApiImportPlanCreatePayload { + OpenApiImportPlanCreatePayload { + selection_mode: selection_mode.to_owned(), + selected_operation_keys: selected, + server_url: None, + decisions, + } +} + +fn public_error_context(error: &ApiError) -> Option<&Value> { + match error { + ApiError::Unauthorized { context, .. } + | ApiError::Forbidden { context, .. } + | ApiError::Validation { context, .. } + | ApiError::Unprocessable { context, .. } + | ApiError::NotFound { context, .. } + | ApiError::Conflict { context, .. } + | ApiError::PayloadTooLarge { context, .. } + | ApiError::PreconditionRequired { context, .. } + | ApiError::RateLimited { context, .. } + | ApiError::Internal { context, .. } => context.as_ref(), + } +} + +fn assert_error_code(error: &ApiError, expected_code: &str) { + assert_eq!( + public_error_context(error) + .and_then(|context| context.get("error_code")) + .and_then(Value::as_str), + Some(expected_code), + "unexpected API error: {error:?}" + ); +} + +/// Asserts the rendered HTTP status of a rejection without private accessors. +fn assert_http_status(error: ApiError, expected: axum::http::StatusCode) { + assert_eq!( + error.into_response().status(), + expected, + "the rendered status must match the public contract" + ); +} + +mod creation; +mod validation; diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/validation.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/validation.rs new file mode 100644 index 0000000..555f9d7 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/validation.rs @@ -0,0 +1,275 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn explicit_targets_are_checked_even_when_the_candidate_is_blocked() { + let registry = test_registry().await; + let upstream_base_url = spawn_upstream_server().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_blocked_target"), + 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(); + let blocker_code = preview + .preview + .findings + .iter() + .find(|finding| finding.severity == crank_import::rest::ImportFindingSeverity::Error) + .map(|finding| finding.code.clone()) + .expect("the fixture produces a global error finding"); + let candidate = &preview.preview.groups[0].operations[0]; + let job_id: ImportJobId = preview.job_id.as_str().into(); + + // A mismatched/unknown explicit target is a request error even though + // the candidate is already blocked by the preview. + let error = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + &candidate.key, + "skip", + Some("op_does_not_exist"), + None, + None, + )], + ), + ) + .await + .expect_err("an unknown target is a request error even for a blocked candidate"); + assert_error_code(&error, "import_plan_target_invalid"); + assert_http_status(error, axum::http::StatusCode::BAD_REQUEST); + + // A resolvable exact target is recorded while the candidate stays + // blocked with its bounded redacted reason. + let existing = service + .create_operation( + &workspace_id, + test_operation_payload(&upstream_base_url, &candidate.draft.name), + ) + .await + .unwrap(); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_GLOBAL_BLOCKER)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + &candidate.key, + "skip", + Some(existing.operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .expect("a valid explicit target is accepted for a blocked candidate"); + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + let candidate = &stored.payload["candidates"][0]; + assert_eq!(candidate["state"], "blocked"); + assert_eq!(candidate["reason"], blocker_code); + assert_eq!( + candidate["target"]["operation_id"], + json!(existing.operation_id) + ); + assert!( + candidate.get("operation").is_none(), + "a blocked candidate never stores draft material" + ); + assert!(service.list_operations(&workspace_id).await.unwrap().len() <= 1); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn archived_rest_target_cannot_be_planned_for_new_version() { + let registry = test_registry().await; + let upstream_base_url = spawn_upstream_server().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_archived_target"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let existing = service + .create_operation( + &workspace_id, + test_operation_payload(&upstream_base_url, "latest_rates"), + ) + .await + .unwrap(); + registry + .archive_operation( + &workspace_id, + &OperationId::new(existing.operation_id.clone()), + &time::OffsetDateTime::now_utc(), + ) + .await + .unwrap(); + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let error = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "new_version", + Some(existing.operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .expect_err("archived target cannot accept a new Draft Version"); + assert_error_code(&error, "import_plan_target_invalid"); + assert_http_status(error, axum::http::StatusCode::BAD_REQUEST); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn detached_dependency_snapshot_repreview_requires_without_a_plan() { + let app = axum::Router::new().route( + "/schema.yaml", + axum::routing::get(|| async { + "Result: { type: object, properties: { id: { type: string } } }" + }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + let origin = format!("http://{address}"); + + let registry = test_registry().await; + let service = test_service_with_external_references( + registry.clone(), + test_storage_root("openapi_import_plan_dependency_stale"), + vec![format!("{origin}/")], + ); + let workspace_id = WorkspaceId::new("ws_default"); + let upload = OpenApiUpload { + bytes: format!( + r#" +openapi: 3.1.0 +info: {{ title: External dependency }} +servers: [{{ url: https://api.example.test }}] +paths: + /items: + get: + operationId: listItems + responses: + '200': + description: ok + content: + application/json: + schema: {{ $ref: '{origin}/schema.yaml#/Result' }} +"# + ) + .into_bytes(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + }; + let preview = service + .preview_openapi_import(&workspace_id, upload) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let job = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap(); + let dependency_sources = job.preview_payload["dependencies"] + .as_array() + .cloned() + .unwrap(); + assert!( + !dependency_sources.is_empty(), + "fixture must create a dependency" + ); + + // The plan is creatable while the stored snapshot is still active. + let fresh = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .expect("an active dependency snapshot must not block planning"); + assert_eq!( + registry + .get_import_plan(&workspace_id, &fresh.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap() + .payload["dependencies"] + .as_array() + .unwrap() + .len(), + dependency_sources.len() + ); + + // Detach one stored snapshot: the same job now requires re-preview and + // no plan is created; the legacy job itself stays untouched. + let victim = &dependency_sources[0]; + sqlx::query( + "update artifact_sources + set lifecycle = 'detached', updated_at = now(), detached_at = now() + where workspace_id = $1 and source_id = $2", + ) + .bind(workspace_id.as_str()) + .bind(victim["source_id"].as_str().unwrap()) + .execute(registry.pool()) + .await + .unwrap(); + let error = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .expect_err("a detached dependency snapshot is re-preview material"); + assert_error_code(&error, "import_dependency_verification_failed"); + let context = public_error_context(&error).cloned().unwrap(); + assert_eq!(context["repreview_required"], json!(true)); + assert_http_status(error, axum::http::StatusCode::CONFLICT); + let job = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap(); + assert_eq!(job.status, ImportJobStatus::Pending); + assert_eq!(job.error_text, None); + assert!(job.finished_at.is_none()); + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); +} diff --git a/apps/admin-api/tests/migration_command.rs b/apps/admin-api/tests/migration_command.rs index 91e2ca3..f516612 100644 --- a/apps/admin-api/tests/migration_command.rs +++ b/apps/admin-api/tests/migration_command.rs @@ -40,7 +40,7 @@ fn plan_is_deterministic_and_committed_contract_is_current() { ); assert_eq!(first.stdout, second.stdout); let plan: serde_json::Value = serde_json::from_slice(&first.stdout).unwrap(); - assert_eq!(plan["sequence"].as_array().unwrap().len(), 13); + assert_eq!(plan["sequence"].as_array().unwrap().len(), 14); let checked = command(&["plan", "--check"], None); assert!( @@ -82,7 +82,7 @@ async fn database_only_config_can_apply_and_preflight_a_fresh_schema() { ); let result: serde_json::Value = serde_json::from_slice(&preflight.stdout).unwrap(); assert_eq!(result["status"], "current"); - assert_eq!(result["version"], 13); + assert_eq!(result["version"], 14); } #[tokio::test] diff --git a/apps/ui/css/openapi-import.css b/apps/ui/css/openapi-import.css index bbaf990..9d56e52 100644 --- a/apps/ui/css/openapi-import.css +++ b/apps/ui/css/openapi-import.css @@ -160,6 +160,103 @@ margin-top: 18px; } +.openapi-import-plan-confirmation { + display: grid; + gap: 12px; + padding: 18px; + border: 1px solid var(--border); + border-radius: 16px; + background: var(--bg-surface); +} + +.openapi-import-plan-confirmation[hidden] { + display: none; +} + +.openapi-import-plan-confirmation h3, +.openapi-import-plan-intro, +.openapi-import-plan-versioning, +.openapi-import-plan-summary { + margin: 0; +} + +.openapi-import-plan-intro, +.openapi-import-plan-versioning { + color: var(--text-secondary); + font-size: 13px; +} + +.openapi-import-plan-versioning:not([hidden]) { + padding: 12px; + border-left: 3px solid var(--accent); + border-radius: 6px; + background: var(--bg-overlay); +} + +.openapi-import-plan-counts { + display: flex; + flex-wrap: wrap; + gap: 8px; +} + +.openapi-import-plan-count { + padding: 6px 10px; + border: 1px solid var(--border); + border-radius: 999px; + color: var(--text-secondary); + font-size: 12px; +} + +.openapi-import-plan-candidates, +.openapi-import-plan-outcomes { + display: grid; + gap: 8px; +} + +.openapi-import-plan-candidate, +.openapi-import-plan-outcome { + display: grid; + gap: 5px; + padding: 12px; + border: 1px solid var(--border-subtle); + border-radius: 12px; + background: var(--bg-overlay); +} + +.openapi-import-plan-candidate[data-state="blocked"], +.openapi-import-plan-outcome[data-outcome="rejected"] { + border-color: color-mix(in srgb, var(--red) 45%, var(--border)); +} + +.openapi-import-plan-candidate code, +.openapi-import-plan-outcome code { + color: var(--text-muted); + font-size: 12px; + overflow-wrap: anywhere; +} + +.openapi-import-plan-candidate p, +.openapi-import-plan-outcome p { + margin: 0; + color: var(--text-secondary); + font-size: 13px; +} + +.openapi-import-plan-reason { + color: var(--text-muted) !important; +} + +.openapi-import-plan-actions { + display: flex; + flex-wrap: wrap; + justify-content: flex-end; + gap: 10px; +} + +.openapi-import-plan-summary { + color: var(--text-secondary); +} + .openapi-import-source, .openapi-import-group { border: 1px solid var(--border); @@ -185,7 +282,6 @@ } #openapi-import-server, -#openapi-import-conflict-mode, #openapi-import-search, #openapi-import-method-filter, .openapi-import-server-custom { @@ -266,12 +362,50 @@ } .openapi-import-operation { + display: grid; + gap: 10px; + padding: 14px 16px; + border-bottom: 1px solid var(--border); +} + +.openapi-import-operation-selection { display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: start; - padding: 14px 16px; - border-bottom: 1px solid var(--border); + cursor: pointer; +} + +.openapi-import-decision-controls { + display: flex; + flex-wrap: wrap; + gap: 10px 14px; + padding-left: 28px; +} + +.openapi-import-decision-controls label { + display: grid; + gap: 5px; + color: var(--text-secondary); + font-size: 12px; + font-weight: 700; +} + +.openapi-import-decision-controls select, +.openapi-import-decision-controls input { + min-width: 210px; + max-width: min(100%, 360px); + padding: 8px 10px; + border: 1px solid var(--border); + border-radius: 10px; + background: var(--bg-surface); + color: var(--text-primary); +} + +.openapi-import-plan-blocked-help { + margin: 0; + color: var(--red); + font-weight: 700; } .openapi-import-operation:last-child { @@ -304,6 +438,10 @@ font-size: 12px; } +.openapi-import-operation .openapi-import-findings { + grid-column: auto; +} + .openapi-import-finding { display: inline-flex; align-items: baseline; @@ -484,7 +622,7 @@ justify-content: flex-start; } - .openapi-import-operation { + .openapi-import-operation-selection { grid-template-columns: auto 1fr; } diff --git a/apps/ui/index.html b/apps/ui/index.html index a0af1ba..7cb1bdd 100644 --- a/apps/ui/index.html +++ b/apps/ui/index.html @@ -417,13 +417,6 @@ -
- - -
@@ -443,9 +436,21 @@
- + +
diff --git a/apps/ui/js/api.js b/apps/ui/js/api.js index e93bb28..5399fdd 100644 --- a/apps/ui/js/api.js +++ b/apps/ui/js/api.js @@ -436,6 +436,28 @@ signal: options && options.signal, }); }, + createOpenApiImportPlan: function(workspaceId, jobId, payload, options) { + return request(API_BASE + '/workspaces/' + encodeURIComponent(workspaceId) + '/imports/openapi/' + encodeURIComponent(jobId) + '/plans', { + method: 'POST', + headers: headers({ + 'Content-Type': 'application/json', + 'Accept-Language': localStorage.getItem('crank_lang') === 'ru' ? 'ru' : 'en', + }), + body: JSON.stringify(payload), + signal: options && options.signal, + }); + }, + applyOpenApiImportPlan: function(workspaceId, planReference, payload, options) { + return request(API_BASE + '/workspaces/' + encodeURIComponent(workspaceId) + '/imports/openapi/' + encodeURIComponent(planReference) + '/apply', { + method: 'POST', + headers: headers({ + 'Content-Type': 'application/json', + 'Accept-Language': localStorage.getItem('crank_lang') === 'ru' ? 'ru' : 'en', + }), + body: JSON.stringify(payload), + signal: options && options.signal, + }); + }, listAgents: function(workspaceId) { return get('/workspaces/' + encodeURIComponent(workspaceId) + '/agents'); }, diff --git a/apps/ui/js/i18n.js b/apps/ui/js/i18n.js index 0aef40d..519952f 100644 --- a/apps/ui/js/i18n.js +++ b/apps/ui/js/i18n.js @@ -611,6 +611,7 @@ var TRANSLATIONS = { 'wizard.step2.auth_mode.none': 'No auth', 'wizard.step2.auth_mode.existing': 'Use existing authorization profile', 'wizard.step2.auth_mode.create': 'Create authorization profile now', + 'openapi.error.plan_too_large': 'Select at most 512 operations and leave at most 512 excluded operations.', 'wizard.step2.auth_profile': 'Authorization profile', 'wizard.step2.auth_profile_hint': 'The selected profile is applied automatically on every tool call.', 'wizard.step2.auth_profile_empty': 'No authorization profiles yet. Create one below.', @@ -1658,6 +1659,7 @@ var TRANSLATIONS = { 'wizard.step2.auth_mode.none': 'Без авторизации', 'wizard.step2.auth_mode.existing': 'Использовать существующий профиль авторизации', 'wizard.step2.auth_mode.create': 'Создать профиль авторизации сейчас', + 'openapi.error.plan_too_large': 'Выберите не более 512 операций и оставьте не более 512 исключённых операций.', 'wizard.step2.auth_profile': 'Профиль авторизации', 'wizard.step2.auth_profile_hint': 'Выбранный профиль применяется автоматически при каждом вызове инструмента.', 'wizard.step2.auth_profile_empty': 'Профилей авторизации пока нет. Создайте профиль ниже.', @@ -2093,6 +2095,57 @@ Object.assign(TRANSLATIONS.en, { 'openapi.select_visible': 'Select visible', 'openapi.clear_visible': 'Clear visible', 'openapi.create': 'Create drafts', + 'openapi.plan.create': 'Review import plan', + 'openapi.plan.confirm_title': 'Confirm import plan', + 'openapi.plan.confirm_intro': 'Review the exact server plan before applying it.', + 'openapi.plan.back': 'Back to selection', + 'openapi.plan.back_fix': 'Back to fix decisions', + 'openapi.plan.apply': 'Apply import', + 'openapi.plan.blocked_help': 'Fix the decisions for blocked operations, then create a new plan.', + 'openapi.plan.exact_target': 'Exact operation', + 'openapi.plan.confirmed_name': 'Confirmed name', + 'openapi.decision.action_label': 'Import decision', + 'openapi.decision.create': 'Create', + 'openapi.decision.skip': 'Skip existing operation', + 'openapi.decision.rename': 'Create with a confirmed name', + 'openapi.decision.new_version': 'Create a new Draft Version', + 'openapi.decision.target_label': 'Existing operation', + 'openapi.decision.target_placeholder': 'Choose the exact operation', + 'openapi.decision.confirmed_name_label': 'Confirmed name', + 'openapi.decision.confirmed_name_placeholder': 'Enter the exact new name', + 'openapi.plan.selected': 'Selected: {count}', + 'openapi.plan.excluded': 'Excluded: {count}', + 'openapi.plan.actionable': 'Actionable: {count}', + 'openapi.plan.blocked': 'Blocked: {count}', + 'openapi.plan.versioning_new': 'Creates a new Draft Version. The Published Version and history remain unchanged.', + 'openapi.plan.action.create': 'Create a new Draft', + 'openapi.plan.action.new_version': 'Create a new Draft Version', + 'openapi.plan.action.rename': 'Create a separate Draft with the confirmed name', + 'openapi.plan.action.skip': 'Keep the existing operation unchanged', + 'openapi.plan.action.excluded': 'Excluded from this partial import', + 'openapi.plan.action.blocked': 'Blocked and will not be applied', + 'openapi.plan.result_title': 'Import plan result', + 'openapi.plan.outcome.created': 'Created', + 'openapi.plan.outcome.versioned': 'New Draft Version', + 'openapi.plan.outcome.renamed': 'Created with confirmed name', + 'openapi.plan.outcome.skipped': 'Skipped', + 'openapi.plan.outcome.excluded': 'Excluded', + 'openapi.plan.outcome.rejected': 'Rejected', + 'openapi.plan.summary': 'Applied: {applied}; skipped: {skipped}; excluded: {excluded}; rejected: {rejected}.', + 'openapi.plan.repreview': 'This plan is stale or could not be verified. Create a fresh preview and review a new plan.', + 'openapi.plan.resolve_conflict': 'Review the conflict decision and create a new plan before applying again.', + 'openapi.plan.retry_unknown': 'The Apply result is unknown. Retry sends the same plan reference, digest and idempotency key.', + 'openapi.status.planning': 'Preparing the import plan…', + 'openapi.status.plan_ready': 'Review the server plan and confirm to apply it.', + 'openapi.status.applying': 'Applying the import plan…', + 'openapi.status.applied': 'Import plan applied.', + 'openapi.error.plan': 'The import plan could not be created.', + 'openapi.error.apply': 'The import plan could not be applied.', + 'openapi.error.operations_unavailable': 'The workspace operations could not be loaded. Retry the preview before creating a plan.', + 'openapi.error.decision_target': 'Choose an exact existing operation for this decision.', + 'openapi.error.confirmed_name': 'Enter the exact confirmed name for the rename decision.', + 'openapi.error.decision_action': 'Choose a valid import decision.', + 'openapi.status.loading_operations': 'Checking existing workspace operations…', 'openapi.selection.empty': 'Selected: 0', 'openapi.selection': 'Selected: {selected} of {total}, shown: {visible}', 'openapi.group.selection': 'selected {selected} of {total}, shown {visible}', @@ -2209,6 +2262,57 @@ Object.assign(TRANSLATIONS.ru, { 'openapi.select_visible': 'Выбрать видимые', 'openapi.clear_visible': 'Снять видимые', 'openapi.create': 'Создать черновики', + 'openapi.plan.create': 'Проверить план импорта', + 'openapi.plan.confirm_title': 'Подтвердите план импорта', + 'openapi.plan.confirm_intro': 'Перед применением проверьте точный план сервера.', + 'openapi.plan.back': 'Назад к выбору', + 'openapi.plan.back_fix': 'Назад к исправлению решений', + 'openapi.plan.apply': 'Применить импорт', + 'openapi.plan.blocked_help': 'Исправьте решения для заблокированных операций и создайте новый план.', + 'openapi.plan.exact_target': 'Точная операция', + 'openapi.plan.confirmed_name': 'Подтверждённое имя', + 'openapi.decision.action_label': 'Решение для импорта', + 'openapi.decision.create': 'Создать', + 'openapi.decision.skip': 'Пропустить существующую операцию', + 'openapi.decision.rename': 'Создать с подтверждённым именем', + 'openapi.decision.new_version': 'Создать новую версию черновика', + 'openapi.decision.target_label': 'Существующая операция', + 'openapi.decision.target_placeholder': 'Выберите точную операцию', + 'openapi.decision.confirmed_name_label': 'Подтверждённое имя', + 'openapi.decision.confirmed_name_placeholder': 'Введите точное новое имя', + 'openapi.plan.selected': 'Выбрано: {count}', + 'openapi.plan.excluded': 'Исключено: {count}', + 'openapi.plan.actionable': 'Можно применить: {count}', + 'openapi.plan.blocked': 'Заблокировано: {count}', + 'openapi.plan.versioning_new': 'Будет создана новая версия черновика. Опубликованная версия и история останутся без изменений.', + 'openapi.plan.action.create': 'Создать черновик', + 'openapi.plan.action.new_version': 'Создать новую версию черновика', + 'openapi.plan.action.rename': 'Создать отдельный черновик с подтверждённым именем', + 'openapi.plan.action.skip': 'Оставить существующую операцию без изменений', + 'openapi.plan.action.excluded': 'Исключено из частичного импорта', + 'openapi.plan.action.blocked': 'Заблокировано и не будет применено', + 'openapi.plan.result_title': 'Результат плана импорта', + 'openapi.plan.outcome.created': 'Создано', + 'openapi.plan.outcome.versioned': 'Новая версия черновика', + 'openapi.plan.outcome.renamed': 'Создано с подтверждённым именем', + 'openapi.plan.outcome.skipped': 'Пропущено', + 'openapi.plan.outcome.excluded': 'Исключено', + 'openapi.plan.outcome.rejected': 'Отклонено', + 'openapi.plan.summary': 'Применено: {applied}; пропущено: {skipped}; исключено: {excluded}; отклонено: {rejected}.', + 'openapi.plan.repreview': 'План устарел или не прошёл проверку. Создайте новый предпросмотр и проверьте новый план.', + 'openapi.plan.resolve_conflict': 'Проверьте решение конфликта и создайте новый план перед повторным применением.', + 'openapi.plan.retry_unknown': 'Результат применения неизвестен. Повтор отправит те же ссылку, digest и ключ идемпотентности.', + 'openapi.status.planning': 'Подготавливаю план импорта…', + 'openapi.status.plan_ready': 'Проверьте план сервера и подтвердите его применение.', + 'openapi.status.applying': 'Применяю план импорта…', + 'openapi.status.applied': 'План импорта применён.', + 'openapi.error.plan': 'Не удалось создать план импорта.', + 'openapi.error.apply': 'Не удалось применить план импорта.', + 'openapi.error.operations_unavailable': 'Не удалось загрузить операции workspace. Повторите предпросмотр перед созданием плана.', + 'openapi.error.decision_target': 'Выберите точную существующую операцию для этого решения.', + 'openapi.error.confirmed_name': 'Введите точное подтверждённое имя для переименования.', + 'openapi.error.decision_action': 'Выберите допустимое решение для импорта.', + 'openapi.status.loading_operations': 'Проверяю существующие операции workspace…', 'openapi.selection.empty': 'Выбрано: 0', 'openapi.selection': 'Выбрано: {selected} из {total}, показано: {visible}', 'openapi.group.selection': 'выбрано {selected} из {total}, показано {visible}', diff --git a/apps/ui/js/openapi-import.js b/apps/ui/js/openapi-import.js index a120d01..cafc5f4 100644 --- a/apps/ui/js/openapi-import.js +++ b/apps/ui/js/openapi-import.js @@ -1,6 +1,6 @@ (function() { var MAX_FILE_BYTES = 256 * 1024; - var APPLY_REPLAY_STORAGE_KEY = 'crank_openapi_apply_replay_v1'; + var APPLY_REPLAY_STORAGE_KEY = 'crank_openapi_plan_apply_replay_v2'; var restoredApplyRequest = loadApplyReplay(); var state = { workspaceId: null, @@ -8,10 +8,15 @@ file: null, jobId: null, preview: null, + existingOperations: [], + operationsReady: false, + decisions: Object.create(null), + plan: null, filterQuery: '', filterMethod: '', revision: 0, previewController: null, + planController: null, applyController: null, applyInFlight: false, applyRequest: restoredApplyRequest, @@ -25,23 +30,9 @@ function validApplyReplay(request) { return request && typeof request.workspaceId === 'string' && request.workspaceId.length > 0 && request.workspaceId.length <= 128 - && typeof request.jobId === 'string' && request.jobId.length > 0 && request.jobId.length <= 128 - && request.payload && Array.isArray(request.payload.selected_operation_keys) - && request.payload.selected_operation_keys.length <= 1024 - && request.payload.selected_operation_keys.every(function(key) { - return typeof key === 'string' && key.length > 0 && key.length <= 512; - }) - && (request.payload.server_url === null - || (typeof request.payload.server_url === 'string' && request.payload.server_url.length <= 2048)) - && (request.payload.conflict_mode === 'skip' || request.payload.conflict_mode === 'rename') - && Array.isArray(request.operationMetadata) - && request.operationMetadata.length <= 1024 - && request.operationMetadata.every(function(operation) { - return operation - && typeof operation.key === 'string' && operation.key.length > 0 && operation.key.length <= 512 - && typeof operation.method === 'string' && operation.method.length > 0 && operation.method.length <= 16 - && typeof operation.path === 'string' && operation.path.length > 0 && operation.path.length <= 2048; - }); + && typeof request.planReference === 'string' && request.planReference.length > 0 && request.planReference.length <= 128 + && typeof request.planDigest === 'string' && /^[a-f0-9]{64}$/.test(request.planDigest) + && typeof request.idempotencyKey === 'string' && request.idempotencyKey.length > 0 && request.idempotencyKey.length <= 128; } function loadApplyReplay() { @@ -131,6 +122,116 @@ }); } + function importOperations() { + return (state.preview && state.preview.groups || []).flatMap(function(group) { + return group.operations || []; + }); + } + + function decisionFor(operationKey) { + if (!state.decisions[operationKey]) { + state.decisions[operationKey] = { action: 'create', targetOperationId: '', confirmedName: '' }; + } + return state.decisions[operationKey]; + } + + function matchingOperations(operation) { + return state.existingOperations.filter(function(existing) { + return existing.name === operation.suggested_name; + }); + } + + function renderDecisionControls(operation, row) { + var previous = row.querySelector('.openapi-import-decision-controls'); + if (previous) previous.remove(); + + var controls = document.createElement('div'); + var actionLabel = document.createElement('label'); + var actionSelect = document.createElement('select'); + var candidateDecision = decisionFor(operation.key); + var matches = matchingOperations(operation); + var versionable = matches.filter(function(existing) { + return existing.protocol === 'rest' && existing.status !== 'archived'; + }); + var actions = [ + ['create', 'openapi.decision.create'], + ['skip', 'openapi.decision.skip'], + ['rename', 'openapi.decision.rename'], + ['new_version', 'openapi.decision.new_version'], + ]; + + controls.className = 'openapi-import-decision-controls'; + actionLabel.textContent = tKey('openapi.decision.action_label'); + actionLabel.htmlFor = 'openapi-import-decision-' + operation.key; + actionSelect.id = actionLabel.htmlFor; + actionSelect.dataset.openapiDecisionAction = operation.key; + actions.forEach(function(spec) { + var option = document.createElement('option'); + option.value = spec[0]; + option.textContent = tKey(spec[1]); + if ((spec[0] === 'skip' && !matches.length) + || (spec[0] === 'new_version' && !versionable.length)) { + option.disabled = true; + } + actionSelect.appendChild(option); + }); + actionSelect.value = candidateDecision.action; + actionSelect.addEventListener('change', function() { + candidateDecision.action = actionSelect.value; + if (candidateDecision.action === 'skip' || candidateDecision.action === 'new_version') { + var allowed = candidateDecision.action === 'new_version' ? versionable : matches; + if (!allowed.some(function(existing) { return existing.id === candidateDecision.targetOperationId; })) { + candidateDecision.targetOperationId = allowed.length === 1 ? allowed[0].id : ''; + } + } + renderDecisionControls(operation, row); + }); + actionLabel.appendChild(actionSelect); + controls.appendChild(actionLabel); + + if (candidateDecision.action === 'skip' || candidateDecision.action === 'new_version') { + var targetLabel = document.createElement('label'); + var targetSelect = document.createElement('select'); + var allowedTargets = candidateDecision.action === 'new_version' ? versionable : matches; + var placeholder = document.createElement('option'); + targetLabel.textContent = tKey('openapi.decision.target_label'); + targetSelect.dataset.openapiDecisionTarget = operation.key; + placeholder.value = ''; + placeholder.textContent = tKey('openapi.decision.target_placeholder'); + targetSelect.appendChild(placeholder); + allowedTargets.forEach(function(existing) { + var option = document.createElement('option'); + option.value = existing.id; + option.textContent = existing.name + ' · ' + existing.id; + targetSelect.appendChild(option); + }); + targetSelect.value = candidateDecision.targetOperationId; + targetSelect.addEventListener('change', function() { + candidateDecision.targetOperationId = targetSelect.value; + }); + targetLabel.appendChild(targetSelect); + controls.appendChild(targetLabel); + } + + if (candidateDecision.action === 'rename') { + var renameLabel = document.createElement('label'); + var renameInput = document.createElement('input'); + renameLabel.textContent = tKey('openapi.decision.confirmed_name_label'); + renameInput.type = 'text'; + renameInput.maxLength = 128; + renameInput.value = candidateDecision.confirmedName; + renameInput.placeholder = tKey('openapi.decision.confirmed_name_placeholder'); + renameInput.dataset.openapiDecisionName = operation.key; + renameInput.addEventListener('input', function() { + candidateDecision.confirmedName = renameInput.value; + }); + renameLabel.appendChild(renameInput); + controls.appendChild(renameLabel); + } + + row.appendChild(controls); + } + function operationRows() { return Array.from(document.querySelectorAll('.openapi-import-operation')); } @@ -371,6 +472,10 @@ state.jobId = response.job_id; state.preview = preview; + state.existingOperations = []; + state.operationsReady = false; + state.decisions = Object.create(null); + importOperations().forEach(function(operation) { decisionFor(operation.key); }); state.filterQuery = ''; state.filterMethod = ''; qs('openapi-import-preview-panel').hidden = false; @@ -407,9 +512,11 @@ groupNode.appendChild(header); group.operations.forEach(function(operation) { - var row = document.createElement('label'); + var row = document.createElement('div'); + var selectionLabel = document.createElement('label'); row.className = 'openapi-import-operation'; + selectionLabel.className = 'openapi-import-operation-selection'; row.dataset.openapiMethod = operation.method; row.dataset.openapiSearch = [ group.title, @@ -419,12 +526,13 @@ operation.path, operation.method, ].join(' '); - row.innerHTML = '' + selectionLabel.innerHTML = '' + '
' + escapeHtml(operation.suggested_display_name) + '
' + '
' + escapeHtml(operation.suggested_name) + ' · ' + escapeHtml(operation.path) + ' · ' + escapeHtml(tfKey('openapi.operation.fields', { input: operation.input_fields, output: operation.output_fields })) + '
' + escapeHtml(operation.method) + ''; + row.appendChild(selectionLabel); row.appendChild(renderOperationMappingPreview(operation)); if (operation.findings && operation.findings.length) { @@ -434,6 +542,7 @@ row.appendChild(findings); } + renderDecisionControls(operation, row); groupNode.appendChild(row); }); @@ -480,6 +589,10 @@ function clearPreview() { state.jobId = null; state.preview = null; + state.existingOperations = []; + state.operationsReady = false; + state.decisions = Object.create(null); + state.plan = null; state.applyRequest = null; state.applyMetadata = []; state.applyResult = null; @@ -488,6 +601,11 @@ state.filterQuery = ''; state.filterMethod = ''; qs('openapi-import-preview-panel').hidden = true; + qs('openapi-import-plan-confirmation').hidden = true; + qs('openapi-import-plan-counts').replaceChildren(); + qs('openapi-import-plan-candidates').replaceChildren(); + qs('openapi-import-versioning-consequences').hidden = true; + qs('openapi-import-plan-blocked-help').hidden = true; qs('openapi-import-groups').replaceChildren(); qs('openapi-import-result').hidden = true; qs('openapi-import-result').replaceChildren(); @@ -502,12 +620,16 @@ function invalidate(options) { state.revision += 1; abort(state.previewController); + abort(state.planController); abort(state.applyController); state.previewController = null; + state.planController = null; state.applyController = null; state.applyInFlight = false; qs('openapi-import-preview').disabled = false; qs('openapi-import-create').disabled = false; + qs('openapi-import-plan-apply').disabled = false; + qs('openapi-import-plan-back').disabled = false; qs('openapi-import-file-select').disabled = false; qs('openapi-import-reset').disabled = false; qs('openapi-import-cancel').hidden = true; @@ -529,6 +651,39 @@ qs('openapi-import-preview').disabled = locked; qs('openapi-import-file-select').disabled = locked; qs('openapi-import-reset').disabled = locked; + qs('openapi-import-plan-apply').disabled = locked; + qs('openapi-import-plan-back').disabled = locked; + qs('openapi-import-server').disabled = locked; + qs('openapi-import-server-custom').disabled = locked; + qs('openapi-import-search').disabled = locked; + qs('openapi-import-method-filter').disabled = locked; + qs('openapi-import-select-visible').disabled = locked; + qs('openapi-import-clear-visible').disabled = locked; + document.querySelectorAll('[data-openapi-operation], [data-openapi-group]').forEach(function(input) { + input.disabled = locked; + }); + document.querySelectorAll('.openapi-import-decision-controls select, .openapi-import-decision-controls input').forEach(function(input) { + input.disabled = locked; + }); + } + + function lockPlanEditing(locked) { + qs('openapi-import-create').disabled = locked; + qs('openapi-import-preview').disabled = locked; + qs('openapi-import-file-select').disabled = locked; + qs('openapi-import-reset').disabled = locked; + qs('openapi-import-server').disabled = locked; + qs('openapi-import-server-custom').disabled = locked; + qs('openapi-import-search').disabled = locked; + qs('openapi-import-method-filter').disabled = locked; + qs('openapi-import-select-visible').disabled = locked; + qs('openapi-import-clear-visible').disabled = locked; + document.querySelectorAll('[data-openapi-operation], [data-openapi-group]').forEach(function(input) { + input.disabled = locked; + }); + document.querySelectorAll('.openapi-import-decision-controls select, .openapi-import-decision-controls input').forEach(function(input) { + input.disabled = locked; + }); } function markApplyOutcomeUnknown(options) { @@ -539,14 +694,14 @@ state.applyInFlight = false; state.applyOutcomeUnknown = true; persistApplyReplay(state.applyRequest); - // Until the same job is replayed, starting another import could hide an + // Until the same plan is replayed, starting another import could hide an // already committed server outcome and lead to duplicate drafts. lockApplyInputs(true); qs('openapi-import-cancel').hidden = true; if (!options || options.render !== false) { showRetry(true); - setStatus(tKey('openapi.status.apply_outcome_unknown'), true, true); + setStatus(tKey('openapi.plan.retry_unknown'), true, true); } return true; @@ -557,6 +712,16 @@ markApplyOutcomeUnknown(); return; } + if (state.planController) { + abort(state.planController); + state.planController = null; + state.revision += 1; + lockPlanEditing(false); + qs('openapi-import-cancel').hidden = true; + showRetry(false); + setStatus(tKey('openapi.status.cancelled')); + return; + } if (!state.previewController) return; invalidate(); @@ -621,6 +786,23 @@ if (!isCurrent(revision, workspaceId, language)) return; renderPreview(response); + qs('openapi-import-create').disabled = true; + setStatus(tKey('openapi.status.loading_operations')); + var operationResponse = await window.CrankApi.listOperations(workspaceId); + if (!isCurrent(revision, workspaceId, language)) return; + if (!operationResponse || !Array.isArray(operationResponse.items)) { + throw new Error('Backend returned an invalid operations list response'); + } + state.existingOperations = operationResponse.items; + state.operationsReady = true; + importOperations().forEach(function(operation) { + var row = Array.from(document.querySelectorAll('.openapi-import-operation')).find(function(candidateRow) { + var input = candidateRow.querySelector('[data-openapi-operation]'); + return input && input.value === operation.key; + }); + if (row) renderDecisionControls(operation, row); + }); + qs('openapi-import-create').disabled = false; setStatus(tKey('openapi.status.preview_ready')); } catch (error) { if (!isCurrent(revision, workspaceId, language)) return; @@ -785,12 +967,176 @@ } function createdStatus(response) { + if (response && response.counts) { + return tfKey('openapi.plan.summary', { + applied: (Number(response.counts.created) || 0) + + (Number(response.counts.versioned) || 0) + + (Number(response.counts.renamed) || 0), + skipped: Number(response.counts.skipped) || 0, + excluded: Number(response.counts.excluded) || 0, + rejected: Number(response.counts.rejected) || 0, + }); + } return tfKey('openapi.status.created', { created: (response.created || []).length, skipped: (response.skipped || []).length, }); } + function planActionLabel(candidate) { + if (candidate.state === 'excluded') return tKey('openapi.plan.action.excluded'); + if (candidate.state === 'blocked') return tKey('openapi.plan.action.blocked'); + var action = String(candidate.action || 'create'); + var key = 'openapi.plan.action.' + action; + return tKey(key) === key ? tKey('openapi.plan.action.create') : tKey(key); + } + + function renderImportPlan(plan) { + var countsNode = qs('openapi-import-plan-counts'); + var candidatesNode = qs('openapi-import-plan-candidates'); + var versioningNode = qs('openapi-import-versioning-consequences'); + var counts = plan.counts || {}; + var hasBlocked = (Number(counts.blocked) || 0) > 0; + var countSpecs = [ + ['selected', 'openapi.plan.selected'], + ['excluded', 'openapi.plan.excluded'], + ['actionable', 'openapi.plan.actionable'], + ['blocked', 'openapi.plan.blocked'], + ]; + + countsNode.replaceChildren(); + candidatesNode.replaceChildren(); + countSpecs.forEach(function(spec) { + var item = document.createElement('span'); + item.className = 'openapi-import-plan-count'; + item.textContent = tfKey(spec[1], { count: Number(counts[spec[0]]) || 0 }); + countsNode.appendChild(item); + }); + + var hasNewVersion = false; + (Array.isArray(plan.candidates) ? plan.candidates : []).forEach(function(candidate) { + var item = document.createElement('article'); + var title = document.createElement('strong'); + var key = document.createElement('code'); + var action = document.createElement('p'); + item.className = 'openapi-import-plan-candidate'; + item.dataset.state = String(candidate.state || 'blocked'); + title.textContent = String(candidate.name || candidate.key || tKey('openapi.result.unnamed')); + key.textContent = String(candidate.key || ''); + action.textContent = planActionLabel(candidate); + item.append(title, key, action); + if (candidate.target_operation_id) { + var target = document.createElement('p'); + target.textContent = tKey('openapi.plan.exact_target') + ': ' + candidate.target_operation_id; + item.appendChild(target); + } + if (candidate.confirmed_name) { + var confirmedName = document.createElement('p'); + confirmedName.textContent = tKey('openapi.plan.confirmed_name') + ': ' + candidate.confirmed_name; + item.appendChild(confirmedName); + } + if (candidate.action === 'new_version' && candidate.state !== 'blocked' && candidate.state !== 'excluded') { + hasNewVersion = true; + } + if (candidate.reason) { + var reason = document.createElement('p'); + reason.className = 'openapi-import-plan-reason'; + reason.textContent = findingMessage({ code: candidate.reason }); + item.appendChild(reason); + } + candidatesNode.appendChild(item); + }); + + versioningNode.textContent = tKey('openapi.plan.versioning_new'); + versioningNode.hidden = !hasNewVersion; + qs('openapi-import-plan-confirmation').hidden = false; + qs('openapi-import-plan-blocked-help').hidden = !hasBlocked; + qs('openapi-import-result').hidden = true; + qs('openapi-import-plan-apply').disabled = hasBlocked; + qs('openapi-import-plan-back').textContent = tKey(hasBlocked ? 'openapi.plan.back_fix' : 'openapi.plan.back'); + lockPlanEditing(true); + } + + function reportOutcomeLabel(outcome) { + var value = String(outcome || 'rejected'); + var key = 'openapi.plan.outcome.' + value; + return tKey(key) === key ? tKey('openapi.plan.outcome.rejected') : tKey(key); + } + + function renderImportPlanReport(report) { + var node = qs('openapi-import-result'); + var title = document.createElement('strong'); + node.replaceChildren(); + title.textContent = tKey('openapi.plan.result_title'); + node.appendChild(title); + + var counts = report.counts || {}; + var applied = (Number(counts.created) || 0) + + (Number(counts.versioned) || 0) + + (Number(counts.renamed) || 0); + var summary = document.createElement('p'); + summary.className = 'openapi-import-plan-summary'; + summary.textContent = tfKey('openapi.plan.summary', { + applied: applied, + skipped: Number(counts.skipped) || 0, + excluded: Number(counts.excluded) || 0, + rejected: Number(counts.rejected) || 0, + }); + node.appendChild(summary); + + var outcomes = Array.isArray(report.outcomes) ? report.outcomes : []; + if (outcomes.length) { + var list = document.createElement('div'); + list.className = 'openapi-import-plan-outcomes'; + outcomes.forEach(function(outcome) { + var item = document.createElement('article'); + var heading = document.createElement('strong'); + var key = document.createElement('code'); + var detail = document.createElement('p'); + var outcomeStatus = outcome.status || outcome.outcome || 'rejected'; + item.className = 'openapi-import-plan-outcome'; + item.dataset.outcome = String(outcomeStatus); + heading.textContent = String(outcome.name || outcome.key || tKey('openapi.result.unnamed')); + key.textContent = String(outcome.key || ''); + detail.textContent = reportOutcomeLabel(outcomeStatus); + if (Number.isInteger(outcome.version)) { + detail.textContent += ' · v' + outcome.version; + } + item.append(heading, key, detail); + if (outcome.reason) { + var reason = document.createElement('p'); + reason.className = 'openapi-import-plan-reason'; + reason.textContent = findingMessage({ code: outcome.reason }); + item.appendChild(reason); + } + list.appendChild(item); + }); + node.appendChild(list); + } + + node.hidden = false; + qs('openapi-import-plan-confirmation').hidden = true; + } + + function recoveryCode(payload) { + var value = payload && payload.error && typeof payload.error === 'object' + ? payload.error + : payload; + var context = value && value.context && typeof value.context === 'object' + ? value.context + : value; + return String((context && context.recovery) || (payload && payload.recovery) || ''); + } + + function reportFromPayload(payload) { + if (!payload || typeof payload !== 'object') return null; + var errorContext = payload.error && payload.error.context; + var report = payload.report || payload.apply_report + || (errorContext && (errorContext.report || errorContext.apply_report || errorContext)) + || payload; + return report && Array.isArray(report.outcomes) ? report : null; + } + async function notifyImported(revision, workspaceId, language) { if (typeof state.onImported !== 'function') return; @@ -819,13 +1165,16 @@ lockApplyInputs(true); qs('openapi-import-cancel').hidden = false; showRetry(false); - setStatus(tKey('openapi.status.creating')); + setStatus(tKey('openapi.status.applying')); try { - var response = await window.CrankApi.createOpenApiImport( + var response = await window.CrankApi.applyOpenApiImportPlan( workspaceId, - request.jobId, - request.payload, + request.planReference, + { + plan_digest: request.planDigest, + idempotency_key: request.idempotencyKey, + }, { signal: controller.signal } ); // Abort is advisory: a superseded request can still deliver a response. @@ -847,9 +1196,19 @@ } state.applyResult = response; state.applyRequest = null; + state.plan = null; if (isCurrent(revision, workspaceId, language)) { - setStatus(createdStatus(response)); - renderResult(response); + renderImportPlanReport(response); + if (response.recovery === 'repreview_required') { + setStatus(tKey('openapi.plan.repreview'), true, true); + showRetry(true); + } else if (response.status === 'rejected' + || response.recovery === 'confirm_skip_rename_or_new_version') { + setStatus(tKey('openapi.plan.resolve_conflict'), true, true); + showRetry(true); + } else { + setStatus(tKey('openapi.status.applied')); + } } await notifyImported(revision, workspaceId, language); } catch (error) { @@ -859,9 +1218,10 @@ if (error && error.name === 'AbortError') { state.applyOutcomeUnknown = true; persistApplyReplay(state.applyRequest); + lockApplyInputs(true); showRetry(true); if (!qs('openapi-import-modal').hidden) { - setStatus(tKey('openapi.status.apply_outcome_unknown'), true, true); + setStatus(tKey('openapi.plan.retry_unknown'), true, true); } return; } @@ -872,19 +1232,35 @@ state.applyRequest = null; clearApplyReplay(); showRetry(true); + var report = reportFromPayload(error.payload); + if (report && isCurrent(revision, workspaceId, language)) { + state.applyResult = report; + renderImportPlanReport(report); + } + var recovery = recoveryCode(error.payload); + state.plan = null; + qs('openapi-import-plan-confirmation').hidden = true; + lockPlanEditing(false); if (!qs('openapi-import-modal').hidden) { - setStatus(errorStatus(error, 'openapi.error.create'), true, true); + if (recovery === 'repreview_required') { + setStatus(tKey('openapi.plan.repreview'), true, true); + } else if (recovery === 'confirm_skip_rename_or_new_version') { + setStatus(tKey('openapi.plan.resolve_conflict'), true, true); + } else { + setStatus(errorStatus(error, 'openapi.error.apply'), true, true); + } } return; } // A browser/network failure is not proof that the server rolled the - // transaction back. Replaying this exact job is idempotent server-side. + // transaction back. Replaying this exact plan is idempotent server-side. state.applyOutcomeUnknown = true; persistApplyReplay(state.applyRequest); + lockApplyInputs(true); showRetry(true); if (!qs('openapi-import-modal').hidden) { - setStatus(errorStatus(error, 'openapi.error.create') + ' ' + tKey('openapi.status.apply_outcome_unknown'), true, true); + setStatus(errorStatus(error, 'openapi.error.apply') + ' ' + tKey('openapi.plan.retry_unknown'), true, true); } } finally { if (state.applyController === controller) { @@ -896,14 +1272,18 @@ } } - async function createDrafts() { - if (state.applyInFlight) return; + async function createImportPlan() { + if (state.applyInFlight || state.planController) return; var keys = selectedKeys(); if (!keys.length) { setStatus(tKey('openapi.error.selection'), true, true); return; } + if (keys.length > 512 || importOperations().length - keys.length > 512) { + setStatus(tKey('openapi.error.plan_too_large'), true, true); + return; + } if (!state.jobId || !state.preview) { setStatus(tKey('openapi.error.preview_required'), true, true); return; @@ -915,25 +1295,141 @@ return; } - state.applyRequest = { - workspaceId: state.workspaceId, - jobId: state.jobId, - payload: { - selected_operation_keys: keys, - server_url: serverUrl, - conflict_mode: qs('openapi-import-conflict-mode').value || 'rename', - }, - operationMetadata: (state.preview.groups || []).flatMap(function(group) { - return (group.operations || []).filter(function(operation) { - return keys.indexOf(operation.key) >= 0; - }).map(function(operation) { - return { key: operation.key, method: operation.method, path: operation.path }; + if (!state.workspaceId) { + setStatus(tKey('openapi.error.workspace'), true, true); + return; + } + + if (!state.operationsReady) { + setStatus(tKey('openapi.error.operations_unavailable'), true, true); + return; + } + + var selectedSet = new Set(keys); + var decisions = []; + var invalidDecision = null; + importOperations().forEach(function(operation) { + if (!selectedSet.has(operation.key) || invalidDecision) return; + var decision = decisionFor(operation.key); + var output = { operation_key: operation.key, action: decision.action }; + if (decision.action === 'skip' || decision.action === 'new_version') { + var allowedTargets = matchingOperations(operation).filter(function(existing) { + return decision.action !== 'new_version' + || (existing.protocol === 'rest' && existing.status !== 'archived'); }); - }), + if (!allowedTargets.some(function(existing) { return existing.id === decision.targetOperationId; })) { + invalidDecision = 'openapi.error.decision_target'; + return; + } + output.target_operation_id = decision.targetOperationId; + } else if (decision.action === 'rename') { + if (!decision.confirmedName || !decision.confirmedName.trim()) { + invalidDecision = 'openapi.error.confirmed_name'; + return; + } + output.confirmed_name = decision.confirmedName; + } else if (decision.action !== 'create') { + invalidDecision = 'openapi.error.decision_action'; + return; + } + if (decision.action !== 'create') decisions.push(output); + }); + if (invalidDecision) { + setStatus(tKey(invalidDecision), true, true); + return; + } + + var total = (state.preview.groups || []).reduce(function(count, group) { + return count + (group.operations || []).length; + }, 0); + var selectionMode = keys.length === total ? 'full' : 'partial'; + var payload = { + selection_mode: selectionMode, + selected_operation_keys: selectionMode === 'full' ? [] : keys, + server_url: serverUrl, + decisions: decisions, }; - state.applyMetadata = state.applyRequest.operationMetadata; - persistApplyReplay(state.applyRequest); - await submitApply(state.applyRequest); + var revision = state.revision; + var workspaceId = state.workspaceId; + var language = locale(); + var controller = new AbortController(); + state.planController = controller; + state.plan = null; + lockPlanEditing(true); + qs('openapi-import-cancel').hidden = false; + qs('openapi-import-plan-confirmation').hidden = true; + showRetry(false); + setStatus(tKey('openapi.status.planning')); + + try { + var plan = await window.CrankApi.createOpenApiImportPlan( + workspaceId, + state.jobId, + payload, + { signal: controller.signal } + ); + if (state.planController !== controller) return; + if (!isCurrent(revision, workspaceId, language)) return; + if (!plan || typeof plan.plan_reference !== 'string' + || !/^[a-f0-9]{64}$/.test(String(plan.plan_digest || '')) + || !plan.counts || !Array.isArray(plan.candidates)) { + throw new Error('Backend returned an invalid ImportPlan response'); + } + state.plan = plan; + renderImportPlan(plan); + setStatus(tKey('openapi.status.plan_ready')); + } catch (error) { + if (state.planController !== controller) return; + if (error && error.name === 'AbortError') { + setStatus(tKey('openapi.status.cancelled')); + return; + } + setStatus(errorStatus(error, 'openapi.error.plan'), true, true); + showRetry(true); + } finally { + if (state.planController === controller) { + state.planController = null; + lockPlanEditing(!!state.plan); + qs('openapi-import-cancel').hidden = true; + } + } + } + + function newIdempotencyKey() { + if (window.crypto && typeof window.crypto.randomUUID === 'function') { + return 'openapi:' + window.crypto.randomUUID(); + } + return 'openapi:' + Date.now().toString(36) + ':' + Math.random().toString(36).slice(2); + } + + async function applyImportPlan() { + if (state.applyInFlight || !state.plan || state.applyOutcomeUnknown) return; + var request = { + workspaceId: state.workspaceId, + planReference: state.plan.plan_reference, + planDigest: state.plan.plan_digest, + idempotencyKey: newIdempotencyKey(), + }; + if (!validApplyReplay(request)) { + setStatus(tKey('openapi.error.apply'), true, true); + return; + } + state.applyRequest = request; + persistApplyReplay(request); + await submitApply(request); + } + + function backToImportSelection() { + if (state.applyInFlight || state.applyOutcomeUnknown) return; + state.plan = null; + qs('openapi-import-plan-confirmation').hidden = true; + qs('openapi-import-plan-counts').replaceChildren(); + qs('openapi-import-plan-candidates').replaceChildren(); + qs('openapi-import-versioning-consequences').hidden = true; + qs('openapi-import-plan-blocked-help').hidden = true; + qs('openapi-import-plan-back').textContent = tKey('openapi.plan.back'); + lockPlanEditing(false); + setStatus(tKey('openapi.status.preview_ready')); } function retryCurrent() { @@ -948,7 +1444,6 @@ showRetry(false); setStatus(''); qs('openapi-import-server-custom').value = ''; - qs('openapi-import-conflict-mode').value = 'rename'; } function open(options) { @@ -963,12 +1458,25 @@ qs('openapi-import-modal').hidden = false; renderFileName(); if (state.applyResult) { - renderResult(state.applyResult); - setStatus(createdStatus(state.applyResult) + (state.applyRefreshFailed ? ' ' + tKey('openapi.status.refresh_failed') : '')); + if (Array.isArray(state.applyResult.outcomes)) { + renderImportPlanReport(state.applyResult); + } else { + renderResult(state.applyResult); + } + if (state.applyResult.recovery === 'repreview_required') { + showRetry(true); + setStatus(tKey('openapi.plan.repreview'), true, true); + } else if (state.applyResult.status === 'rejected' + || state.applyResult.recovery === 'confirm_skip_rename_or_new_version') { + showRetry(true); + setStatus(tKey('openapi.plan.resolve_conflict'), true, true); + } else { + setStatus(createdStatus(state.applyResult) + (state.applyRefreshFailed ? ' ' + tKey('openapi.status.refresh_failed') : '')); + } } else if (state.applyOutcomeUnknown && state.applyRequest) { lockApplyInputs(true); showRetry(true); - setStatus(tKey('openapi.status.apply_outcome_unknown'), true, true); + setStatus(tKey('openapi.plan.retry_unknown'), true, true); qs('openapi-import-retry').focus(); return; } @@ -1031,7 +1539,9 @@ }); qs('openapi-import-preview').addEventListener('click', preview); qs('openapi-import-retry').addEventListener('click', retryCurrent); - qs('openapi-import-create').addEventListener('click', createDrafts); + qs('openapi-import-create').addEventListener('click', createImportPlan); + qs('openapi-import-plan-apply').addEventListener('click', applyImportPlan); + qs('openapi-import-plan-back').addEventListener('click', backToImportSelection); qs('openapi-import-cancel').addEventListener('click', cancelCurrent); qs('openapi-import-reset').addEventListener('click', reset); qs('openapi-import-search').addEventListener('input', function(event) { @@ -1082,7 +1592,7 @@ } if (state.applyOutcomeUnknown && state.applyRequest) { showRetry(true); - setStatus(tKey('openapi.status.apply_outcome_unknown'), true, true); + setStatus(tKey('openapi.plan.retry_unknown'), true, true); return; } invalidate(options); @@ -1091,9 +1601,15 @@ setStatus(tKey('openapi.status.context_changed')); } } - window.addEventListener('crank:langchange', function() { + function handleLanguageChange() { + if (!qs('openapi-import-modal').hidden && state.plan && !state.applyInFlight && !state.applyOutcomeUnknown) { + renderImportPlan(state.plan); + setStatus(tKey('openapi.status.plan_ready')); + return; + } invalidateForContext(); - }); + } + window.addEventListener('crank:langchange', handleLanguageChange); window.addEventListener('crank:workspacechange', function(event) { if (!qs('openapi-import-modal').hidden) { state.workspaceId = event.detail && event.detail.id || null; @@ -1101,7 +1617,7 @@ } }); window.addEventListener('storage', function(event) { - if (event.key === 'crank_lang') invalidateForContext(); + if (event.key === 'crank_lang') handleLanguageChange(); }); window.addEventListener('pagehide', function() { if (state.applyInFlight || state.applyController) { @@ -1124,7 +1640,7 @@ if (state.applyOutcomeUnknown && state.applyRequest) { lockApplyInputs(true); showRetry(true); - setStatus(tKey('openapi.status.apply_outcome_unknown'), true, true); + setStatus(tKey('openapi.plan.retry_unknown'), true, true); } else { invalidateForContext({ clearFile: true }); } diff --git a/apps/ui/tests/e2e/onboarding.spec.js b/apps/ui/tests/e2e/onboarding.spec.js index 37f7abd..87dfb2f 100644 --- a/apps/ui/tests/e2e/onboarding.spec.js +++ b/apps/ui/tests/e2e/onboarding.spec.js @@ -156,6 +156,7 @@ test('onboarding error exposes safe support correlation and retries without a cl }); await page.goto('/'); + await expect(page.getByTestId('onboarding-trigger')).toContainText(/3\s*\/\s*7/); await page.getByTestId('onboarding-trigger').click(); fail = true; await page.getByTestId('onboarding-refresh').click(); diff --git a/apps/ui/tests/e2e/openapi-import-plan.spec.js b/apps/ui/tests/e2e/openapi-import-plan.spec.js new file mode 100644 index 0000000..77bdfe2 --- /dev/null +++ b/apps/ui/tests/e2e/openapi-import-plan.spec.js @@ -0,0 +1,278 @@ +const { test, expect } = require('@playwright/test'); +const { login, localized, uniqueName } = require('./helpers'); + +async function dismissOnboardingIfOpen(page) { + await page.locator('#crank-onboarding-panel').waitFor({ state: 'attached' }); + await page.locator('[data-testid="onboarding-dismiss"]').waitFor({ state: 'attached' }); + await page.evaluate(() => { + const dismiss = document.querySelector('[data-testid="onboarding-dismiss"]'); + if (dismiss) dismiss.click(); + }); + await expect(page.locator('#crank-onboarding-panel')).toBeHidden(); +} + +function importDocument(prefix) { + return [ + 'openapi: 3.0.3', + 'info: { title: ImportPlan UI test, version: \'1\' }', + 'paths:', + ' /' + prefix + '-selected:', + ' get:', + ' operationId: ' + prefix + '_selected', + ' responses:', + ' \'200\': { description: OK }', + ' /' + prefix + '-excluded:', + ' get:', + ' operationId: ' + prefix + '_excluded', + ' responses:', + ' \'200\': { description: OK }', + ].join('\n'); +} + +function planResponse(prefix) { + return { + plan_reference: 'plan_e2e_importplan_213', + plan_digest: 'a'.repeat(64), + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + selection_mode: 'partial', + counts: { selected: 1, excluded: 1, actionable: 1, blocked: 0 }, + candidates: [ + { + key: 'GET /' + prefix + '-selected', + name: 'selected_operation', + state: 'actionable', + action: 'new_version', + target_operation_id: 'op_existing_target', + confirmed_name: null, + auth_profile_id: null, + reason: null, + }, + { + key: 'GET /' + prefix + '-excluded', + name: 'excluded_operation', + state: 'excluded', + action: null, + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: 'not_selected', + }, + ], + }; +} + +async function preparePlanFlow(page, locale) { + const planCalls = []; + const applyCalls = []; + const prefix = uniqueName('plan_apply'); + let releaseApplyResponse; + const applyResponseHeld = new Promise((resolve) => { + releaseApplyResponse = resolve; + }); + + // Keep this mock aligned with the public CreatePlan response shape. + await page.route('**/api/admin/workspaces/*/operations', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ items: [], page: 1, page_size: 0, total: 0 }), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + planCalls.push({ + url: route.request().url(), + payload: route.request().postDataJSON(), + }); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix)), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/apply$/, async (route) => { + applyCalls.push({ + url: route.request().url(), + payload: route.request().postDataJSON(), + }); + // Keep the request pending until the test has checked the duplicate guard. + await applyResponseHeld; + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + plan_digest: 'a'.repeat(64), + counts: { selected: 1, excluded: 1, created: 0, versioned: 1 }, + outcomes: [{ status: 'versioned' }], + }), + }); + }); + // Contain the legacy path so this red-phase test cannot mutate the E2E DB. + await page.route(/\/imports\/openapi\/[^/]+\/create$/, async (route) => { + await route.fulfill({ + status: 409, + contentType: 'application/json', + body: JSON.stringify({ message: 'ImportPlan flow required' }), + }); + }); + + await login(page); + await dismissOnboardingIfOpen(page); + await page.evaluate((language) => window.setLang(language), locale); + await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'import-plan.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(importDocument(prefix)), + }); + const previewResponse = page.waitForResponse((response) => + response.request().method() === 'POST' + && response.url().includes('/imports/openapi/preview'), + ); + await page.locator('#openapi-import-preview').click(); + await previewResponse; + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + await page.locator('#openapi-import-server-custom').fill('https://api.example.test'); + + // The existing preview action becomes Plan creation followed by confirmation. + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await expect.poll(() => planCalls.length).toBe(1); + return { planCalls, applyCalls, prefix, releaseApplyResponse }; +} + +async function assertConfirmation(page, prefix) { + const confirmation = page.locator('[data-testid="openapi-import-apply-confirmation"]'); + const counts = page.locator('[data-testid="openapi-import-apply-counts"]'); + const versioning = page.locator('[data-testid="openapi-import-versioning-consequences"]'); + + await expect(counts).toContainText(localized('Selected: 1', 'Выбрано: 1')); + await expect(counts).toContainText(localized('Excluded: 1', 'Исключено: 1')); + await expect(counts).toContainText(localized('Actionable: 1', 'Можно применить: 1')); + await expect(counts).toContainText(localized('Blocked: 0', 'Заблокировано: 0')); + await expect(versioning).toContainText(localized('new Draft Version', 'новая версия черновика')); + await expect(versioning).toContainText( + localized('Published Version and history remain unchanged', 'Опубликованная версия и история останутся без изменений'), + ); + await expect(confirmation).toContainText('GET /' + prefix + '-selected'); + await expect(confirmation).toContainText('GET /' + prefix + '-excluded'); + await expect(confirmation).toContainText('op_existing_target'); +} + +test('[P1] AC-13 renders server-confirmed partial and new_version consequences in RU and EN', async ({ page }) => { + // This test covers rendering of a stored server plan only. It does not + // specify how UI controls compose selection/decision request fields. + const { planCalls, prefix } = await preparePlanFlow(page, 'ru'); + for (const locale of ['ru', 'en']) { + await test.step('confirmation in ' + locale, async () => { + await page.evaluate((language) => window.setLang(language), locale); + await assertConfirmation(page, prefix); + }); + } + expect(planCalls).toHaveLength(1); + expect(planCalls[0].url).toMatch(/\/imports\/openapi\/[^/]+\/plans$/); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]').getByRole('button', { + name: localized('Apply import', 'Применить импорт'), + })).toBeVisible(); +}); + +test('[P1] AC-13 double-click sends one Apply with the stored reference, digest and idempotency key', async ({ page }) => { + const { applyCalls, releaseApplyResponse } = await preparePlanFlow(page, 'en'); + const applyButton = page.getByRole('button', { + name: localized('Apply import', 'Применить импорт'), + }); + + await applyButton.dblclick(); + + await expect.poll(() => applyCalls.length).toBe(1); + await expect(applyButton).toBeDisabled(); + releaseApplyResponse(); + await expect(page.locator('[data-testid="openapi-import-apply-result"]')).toBeVisible(); + expect(applyCalls).toHaveLength(1); + expect(applyCalls[0].url).toMatch(/\/imports\/openapi\/plan_e2e_importplan_213\/apply$/); + expect(applyCalls[0].payload).toMatchObject({ + plan_digest: 'a'.repeat(64), + }); + expect(typeof applyCalls[0].payload.idempotency_key).toBe('string'); + expect(applyCalls[0].payload.idempotency_key.length).toBeGreaterThan(0); +}); + +test('[P1] AC-13 submits the confirmed new_version decision for the exact existing operation', async ({ page }) => { + const prefix = uniqueName('plan_version'); + const targetOperationId = 'op_exact_importplan_target'; + const planCalls = []; + const expectedName = prefix + '_selected'; + await page.route('**/api/admin/workspaces/*/operations', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + items: [{ id: targetOperationId, name: expectedName, protocol: 'rest', status: 'draft' }], + page: 1, + page_size: 1, + total: 1, + }), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + const payload = route.request().postDataJSON(); + planCalls.push(payload); + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + plan_reference: 'plan_e2e_importplan_decision', + plan_digest: 'b'.repeat(64), + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + selection_mode: payload.selection_mode, + counts: { selected: 1, excluded: 0, actionable: 1, blocked: 0 }, + candidates: [{ + key: payload.decisions[0].operation_key, + name: expectedName, + state: 'actionable', + action: 'new_version', + target_operation_id: targetOperationId, + confirmed_name: null, + auth_profile_id: null, + reason: null, + }], + }), + }); + }); + + await login(page); + await dismissOnboardingIfOpen(page); + await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'import-plan-decision.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(importDocument(prefix)), + }); + const previewResponse = page.waitForResponse((response) => + response.request().method() === 'POST' && response.url().includes('/imports/openapi/preview'), + ); + await page.locator('#openapi-import-preview').click(); + await previewResponse; + const operationRow = page.locator('.openapi-import-operation').filter({ hasText: expectedName }); + const operationCheckbox = operationRow.locator('[data-openapi-operation]'); + await expect(operationCheckbox).toBeVisible(); + const operationKey = await operationCheckbox.inputValue(); + for (const checkbox of await page.locator('[data-openapi-operation]').all()) { + if (await checkbox.inputValue() !== operationKey) await checkbox.uncheck(); + } + await expect(operationRow.locator('.openapi-import-operation-meta')).toContainText(expectedName); + const action = operationRow.locator('[data-openapi-decision-action]'); + await expect(action.locator('option[value="new_version"]')).toBeEnabled(); + await action.selectOption('new_version'); + await expect(operationRow.locator('[data-openapi-decision-target]')).toHaveValue(targetOperationId); + await page.locator('#openapi-import-server-custom').fill('https://api.example.test'); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toContainText(targetOperationId); + + expect(planCalls).toHaveLength(1); + expect(planCalls[0].selection_mode).toBe('partial'); + expect(planCalls[0].selected_operation_keys).toEqual([operationKey]); + expect(planCalls[0].decisions).toEqual([{ + operation_key: operationKey, + action: 'new_version', + target_operation_id: targetOperationId, + }]); +}); diff --git a/apps/ui/tests/e2e/operations.spec.js b/apps/ui/tests/e2e/operations.spec.js index 22c27e2..8d2cb95 100644 --- a/apps/ui/tests/e2e/operations.spec.js +++ b/apps/ui/tests/e2e/operations.spec.js @@ -11,6 +11,48 @@ async function dismissOnboardingIfOpen(page) { await expect(page.locator('#crank-onboarding-panel')).toBeHidden(); } +function planResponse(payload, planReference) { + const decisions = payload.decisions || []; + return { + plan_reference: planReference, + plan_digest: 'c'.repeat(64), + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + selection_mode: payload.selection_mode, + counts: { selected: decisions.length, excluded: 0, actionable: decisions.length, blocked: 0 }, + candidates: decisions.map((decision) => ({ + key: decision.operation_key, + name: decision.confirmed_name || decision.operation_key, + state: 'actionable', + action: decision.action, + target_operation_id: decision.target_operation_id || null, + confirmed_name: decision.confirmed_name || null, + auth_profile_id: null, + reason: null, + })), + }; +} + +function applyResponse(payload, names) { + const outcomes = (payload.outcomes || []).map((outcome) => ({ + key: outcome.key, + name: names && names[outcome.key] || outcome.name || outcome.key, + status: outcome.status || 'created', + })); + return { + counts: { created: outcomes.filter((outcome) => outcome.status === 'created').length }, + outcomes, + }; +} + +async function mockEmptyOperationsList(page) { + await page.route('**/api/admin/workspaces/*/operations', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ items: [], page: 1, page_size: 0, total: 0 }), + }); + }); +} + test('operations page shows demo catalog and filter works', async ({ page }) => { await login(page); await expect(page.locator('.page-heading')).toHaveText(localized('Operations', 'Операции')); @@ -112,7 +154,6 @@ paths: await expect(importedOperation.locator('.openapi-import-mapping-preview')).toContainText('Header'); await expect(importedOperation.locator('.openapi-import-mapping-preview')).toContainText('X-API-Version'); await expect(importedOperation.locator('.openapi-import-mapping-preview')).toContainText('Body'); - await expect(importedOperation.locator('.openapi-import-mapping-preview')).toContainText('symbols'); await expect(importedOperation.locator('.openapi-import-mapping-preview')).toContainText(localized('Response', 'Ответ')); await expect(page.locator('#openapi-import-selection')).toContainText(localized('Selected: 2', 'Выбрано: 2')); await page.locator('#openapi-import-search').fill('status'); @@ -127,18 +168,19 @@ paths: await page.locator('#openapi-import-method-filter').selectOption(''); await page.locator('#openapi-import-server-custom').fill('https://api.example.test'); await page.locator('#openapi-import-create').click(); - await expect(page.locator('#openapi-import-result')).toContainText(localized('Import result', 'Результат импорта')); - await expect(page.locator('.openapi-import-result-table')).toBeVisible(); - await expect(page.locator('.openapi-import-result-row').filter({ hasText: operationId })).toContainText('POST /v1/rates/{date}'); - await expect(page.locator('.openapi-import-result-row').filter({ hasText: operationId })).toContainText(localized('tool description is too short', 'Описание инструмента слишком короткое')); - await expect(page.locator('.openapi-import-result-row').filter({ hasText: operationId })).toContainText(localized('Fix in wizard', 'Исправить в мастере')); - await expect(page.locator('.openapi-import-primary-result a')).toHaveAttribute( - 'href', - /\/wizard\/\?mode=edit&operationId=op_/ - ); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await page.locator('#openapi-import-plan-apply').click(); + await expect(page.locator('#openapi-import-result')).toBeVisible(); + await expect(page.locator('#openapi-import-result')).toContainText(localized('Import plan result', 'Результат плана импорта')); + await expect(page.locator('.openapi-import-plan-outcomes')).toContainText(operationId); + await expect(page.locator('.openapi-import-plan-outcomes')).toContainText('POST /v1/rates/{date}'); + await expect(importedOperation.locator('.openapi-import-findings')).toContainText(localized('tool description is too short', 'Описание инструмента слишком короткое')); await expect(page.locator('tbody')).toContainText(new RegExp(operationId, 'i')); await expect(page.locator('tbody')).not.toContainText(new RegExp(statusOperationId, 'i')); await expect(page.locator('body')).not.toContainText(sourceCanary); + await page.locator('[data-openapi-close]').last().click(); + await page.locator('tbody tr').filter({ hasText: operationId }).locator('.row-btn-edit').click(); + await expect(page).toHaveURL(/\/wizard\/\?mode=edit&operationId=op_/); }); test('OpenAPI upload rejects invalid files locally and restores focus after Escape', async ({ page }) => { @@ -246,6 +288,7 @@ test('OpenAPI apply cancellation preserves the import job for an idempotent retr await login(page); await dismissOnboardingIfOpen(page); await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await mockEmptyOperationsList(page); await page.route('**/imports/openapi/preview', async (route) => { await route.fulfill({ @@ -269,11 +312,18 @@ test('OpenAPI apply cancellation preserves the import job for an idempotent retr }); const requests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(planResponse(payload, 'plan_retry_same_authority')), + }); + }); let releaseFirst; const firstRequest = new Promise((resolve) => { releaseFirst = resolve; }); - await page.route('**/imports/openapi/*/create', async (route) => { + await page.route('**/imports/openapi/*/apply', async (route) => { requests.push({ url: route.request().url(), body: route.request().postData() }); if (requests.length === 1) await firstRequest; if (requests.length === 3) { @@ -287,12 +337,9 @@ test('OpenAPI apply cancellation preserves the import job for an idempotent retr try { await route.fulfill({ contentType: 'application/json', - body: JSON.stringify({ - created: [{ - operation_key: 'get:/retry', name: 'renamed_after_conflict', operation_id: 'op_retry', version: 1, - }], - skipped: [], findings: [], - }), + body: JSON.stringify(applyResponse({ outcomes: [{ + key: 'get:/retry', name: 'renamed_after_conflict', status: 'created', + }] })), }); } catch (_error) { // The first request is deliberately aborted by the browser. @@ -305,23 +352,25 @@ test('OpenAPI apply cancellation preserves the import job for an idempotent retr await page.locator('#openapi-import-preview').click(); await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await page.locator('#openapi-import-plan-apply').click(); await expect.poll(() => requests.length).toBe(1); await page.locator('#openapi-import-cancel').click(); - await expect(page.locator('#openapi-import-status')).toContainText(localized('may have completed', 'мог завершиться')); + await expect(page.locator('#openapi-import-status')).toContainText(localized('Apply result is unknown', 'Результат применения неизвестен')); await expect(page.locator('#openapi-import-retry')).toBeVisible(); await page.locator('[data-openapi-close]').last().click(); await page.reload(); await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); await expect(page.locator('#openapi-import-retry')).toBeVisible(); await page.locator('#openapi-import-retry').click(); - await expect(page.locator('#openapi-import-result')).toContainText('GET /retry'); + await expect(page.locator('#openapi-import-result')).toContainText('get:/retry'); await expect.poll(() => requests.length).toBe(2); // Let the cancelled first attempt answer after the retry. Its stale response // must not overwrite or unlock the newer attempt's state. releaseFirst(); await page.waitForTimeout(50); - await expect(page.locator('#openapi-import-result')).toContainText('GET /retry'); - expect(requests[0].url).toContain('/imports/openapi/job_retry_same_authority/create'); + await expect(page.locator('#openapi-import-result')).toContainText('get:/retry'); + expect(requests[0].url).toContain('/imports/openapi/plan_retry_same_authority/apply'); expect(requests[1].url).toBe(requests[0].url); expect(requests[1].body).toBe(requests[0].body); @@ -334,17 +383,20 @@ test('OpenAPI apply cancellation preserves the import job for an idempotent retr await page.locator('#openapi-import-preview').click(); await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await page.locator('#openapi-import-plan-apply').click(); await expect.poll(() => requests.length).toBe(3); await expect(page.locator('#openapi-import-file-select')).toBeEnabled(); await expect(page.locator('#openapi-import-reset')).toBeEnabled(); await expect(page.locator('#openapi-import-retry')).toBeVisible(); - expect(await page.evaluate(() => sessionStorage.getItem('crank_openapi_apply_replay_v1'))).toBeNull(); + expect(await page.evaluate(() => sessionStorage.getItem('crank_openapi_plan_apply_replay_v2'))).toBeNull(); }); test('OpenAPI apply preserves job authority after language or workspace changes', async ({ page }) => { await login(page); await dismissOnboardingIfOpen(page); await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await mockEmptyOperationsList(page); await page.route('**/imports/openapi/preview', async (route) => { await route.fulfill({ @@ -368,16 +420,21 @@ test('OpenAPI apply preserves job authority after language or workspace changes' }); const releases = []; const applyRequests = []; - await page.route('**/imports/openapi/*/create', async (route) => { + await page.route('**/imports/openapi/*/plans', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(planResponse(route.request().postDataJSON(), 'plan_active_apply')), + }); + }); + await page.route('**/imports/openapi/*/apply', async (route) => { applyRequests.push({ url: route.request().url(), body: route.request().postData() }); await new Promise((resolve) => releases.push(resolve)); try { await route.fulfill({ contentType: 'application/json', - body: JSON.stringify({ - created: [{ operation_key: 'get:/active', name: 'active', operation_id: 'op_active', version: 1 }], - skipped: [], findings: [], - }), + body: JSON.stringify(applyResponse({ outcomes: [{ + key: 'get:/active', name: 'active', status: 'created', + }] })), }); } catch (_error) { // The invalidation aborts a request that must not update the modal afterwards. @@ -394,6 +451,8 @@ test('OpenAPI apply preserves job authority after language or workspace changes' await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); await page.locator('#openapi-import-server-custom').fill('https://api.example.test'); await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await page.locator('#openapi-import-plan-apply').click(); await expect(page.locator('#openapi-import-cancel')).toBeVisible(); await expect.poll(() => releases.length).toBeGreaterThan(0); } @@ -402,14 +461,14 @@ test('OpenAPI apply preserves job authority after language or workspace changes' // Exercise the actual UI language switcher path: it updates storage, // re-renders translations and dispatches crank:langchange in one action. await page.evaluate(() => window.setLang('en')); - await expect(page.locator('#openapi-import-status')).toContainText('may have completed'); + await expect(page.locator('#openapi-import-status')).toContainText('Apply result is unknown'); await expect(page.locator('#openapi-import-retry')).toBeVisible(); await expect(page.locator('#openapi-import-file-select')).toBeDisabled(); releases.shift()(); await page.locator('#openapi-import-retry').click(); await expect.poll(() => releases.length).toBeGreaterThan(0); releases.shift()(); - await expect(page.locator('#openapi-import-result')).toContainText('GET /active'); + await expect(page.locator('#openapi-import-result')).toContainText('get:/active'); expect(applyRequests[1].url).toBe(applyRequests[0].url); expect(applyRequests[1].body).toBe(applyRequests[0].body); @@ -419,7 +478,7 @@ test('OpenAPI apply preserves job authority after language or workspace changes' await page.evaluate(() => { window.dispatchEvent(new CustomEvent('crank:workspacechange', { detail: { id: 'workspace-after-apply' } })); }); - await expect(page.locator('#openapi-import-status')).toContainText('may have completed'); + await expect(page.locator('#openapi-import-status')).toContainText('Apply result is unknown'); await expect(page.locator('#openapi-import-retry')).toBeVisible(); await expect(page.locator('#openapi-import-file-select')).toBeDisabled(); releases.shift()(); @@ -516,6 +575,7 @@ test('OpenAPI upload ignores a stale failure and renders only correlation identi await login(page); await dismissOnboardingIfOpen(page); await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await mockEmptyOperationsList(page); const sourceCanary = 'do-not-render-openapi-source-canary'; await page.locator('#openapi-import-file').setInputFiles({ name: 'safe.yaml', @@ -590,14 +650,30 @@ test('OpenAPI upload ignores a stale failure and renders only correlation identi }); await page.locator('#openapi-import-retry').click(); await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); - let createRequests = 0; - await page.route('**/imports/openapi/*/create', async (route) => { - createRequests += 1; - await new Promise((resolve) => setTimeout(resolve, 100)); - await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ created: [], skipped: [], findings: [] }) }); + await page.route('**/imports/openapi/*/plans', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(planResponse(route.request().postDataJSON(), 'plan_ui_evidence')), + }); + }); + let applyRequests = 0; + let releaseApply; + const applyResponseHeld = new Promise((resolve) => { releaseApply = resolve; }); + await page.route('**/imports/openapi/*/apply', async (route) => { + applyRequests += 1; + await applyResponseHeld; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(applyResponse({ outcomes: [{ key: 'get:/health', name: 'health', status: 'created' }] })), + }); }); await page.locator('#openapi-import-create').click(); - await page.locator('#openapi-import-create').evaluate((button) => button.click()); - await expect(page.locator('#openapi-import-status')).toContainText('Created: 0; skipped: 0.'); - expect(createRequests).toBe(1); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + const applyButton = page.locator('#openapi-import-plan-apply'); + await applyButton.dblclick(); + await expect.poll(() => applyRequests).toBe(1); + await expect(applyButton).toBeDisabled(); + releaseApply(); + await expect(page.locator('#openapi-import-status')).toContainText('Import plan applied.'); + expect(applyRequests).toBe(1); }); diff --git a/apps/ui/tests/e2e/wizard.spec.js b/apps/ui/tests/e2e/wizard.spec.js index d20ce58..3b64d2d 100644 --- a/apps/ui/tests/e2e/wizard.spec.js +++ b/apps/ui/tests/e2e/wizard.spec.js @@ -193,7 +193,7 @@ test('wizard builds visual request mappings from JSON sample and path params', a await page.goto('/wizard/'); await page.locator('[data-testid="wizard-protocol-rest"]').click(); - await page.evaluate(() => window.CrankWizardShell.goToStep(2)); + await page.locator('#btn-continue').click(); await expect(page.locator('#step-panel-2')).toBeVisible(); await page.locator('#endpoint-path').fill('/rates/{date}'); await page.evaluate(() => window.CrankWizardShell.goToStep(3)); diff --git a/crates/crank-metrics/src/schema.rs b/crates/crank-metrics/src/schema.rs index 5102fad..592510b 100644 --- a/crates/crank-metrics/src/schema.rs +++ b/crates/crank-metrics/src/schema.rs @@ -9,8 +9,8 @@ use crate::labels::{ pub const METRIC_SCHEMA_VERSION: u32 = 1; pub const MAX_METRIC_FAMILIES: usize = 128; pub const MAX_PRODUCT_LABELS_PER_FAMILY: usize = 4; -pub const MAX_LOGICAL_SERIES_PER_PROCESS: usize = 5_250; -pub const MAX_RENDERED_SERIES_PER_PROCESS: usize = 16_750; +pub const MAX_LOGICAL_SERIES_PER_PROCESS: usize = 5_400; +pub const MAX_RENDERED_SERIES_PER_PROCESS: usize = 17_000; pub const MAX_EXPOSITION_BYTES: usize = 8 * 1024 * 1024; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -183,6 +183,8 @@ pub const HTTP_ROUTE_DOMAIN: &[&str] = &[ "/api/admin/workspaces/{workspace_id}/operations", "/api/admin/workspaces/{workspace_id}/imports/openapi/preview", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/create", + "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/plans", + "/api/admin/workspaces/{workspace_id}/imports/openapi/{plan_id}/apply", "/api/admin/workspaces/{workspace_id}/operations/analyze-quality", "/api/admin/workspaces/{workspace_id}/operations/import", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}", @@ -304,7 +306,7 @@ const METRIC_SCHEMA: &[MetricDefinition] = &[ ], &[], BOTH, - 67 * 10 * 6, + 69 * 10 * 6, "Total HTTP requests.", ), metric( @@ -318,7 +320,7 @@ const METRIC_SCHEMA: &[MetricDefinition] = &[ ], DURATION_BUCKETS_SECONDS, BOTH, - 67 * 10, + 69 * 10, "HTTP request duration in seconds.", ), metric( diff --git a/crates/crank-metrics/tests/schema_v1.rs b/crates/crank-metrics/tests/schema_v1.rs index 2b493a9..c4fbfff 100644 --- a/crates/crank-metrics/tests/schema_v1.rs +++ b/crates/crank-metrics/tests/schema_v1.rs @@ -46,16 +46,16 @@ fn schema_v1_is_complete_deterministic_and_inside_the_frozen_budget() { #[test] fn frozen_budget_edges_are_exact() { - assert!(validate_budget(5_249, 16_749).is_ok()); - assert!(validate_budget(5_250, 16_750).is_ok()); - assert!(validate_budget(5_251, 16_000).is_err()); - assert!(validate_budget(5_200, 16_751).is_err()); - assert_eq!(crank_metrics::max_exemplar_slots(), 10_570); + assert!(validate_budget(5_399, 16_999).is_ok()); + assert!(validate_budget(5_400, 17_000).is_ok()); + assert!(validate_budget(5_401, 16_000).is_err()); + assert!(validate_budget(5_200, 17_001).is_err()); + assert_eq!(crank_metrics::max_exemplar_slots(), 10_850); } #[test] fn every_registered_route_round_trips_without_dynamic_segments() { - assert_eq!(HTTP_ROUTE_DOMAIN.len(), 67); + assert_eq!(HTTP_ROUTE_DOMAIN.len(), 69); for route in HTTP_ROUTE_DOMAIN { assert_eq!(HttpRoute::from_matched_path(route).as_str(), *route); assert!(!route.contains("trace_id")); @@ -68,8 +68,8 @@ fn canonical_schema_validation_reports_its_category() { assert_eq!( schema_budget(), Ok(crank_metrics::SchemaBudget { - logical_series: 5_213, - rendered_series: 16_538 + logical_series: 5_353, + rendered_series: 16_978 }) ); } diff --git a/crates/crank-registry/src/error.rs b/crates/crank-registry/src/error.rs index 40dcdd4..7399b11 100644 --- a/crates/crank-registry/src/error.rs +++ b/crates/crank-registry/src/error.rs @@ -136,6 +136,40 @@ pub enum RegistryError { ImportJobNotFound { job_id: String }, #[error("import job {job_id} was already applied with different parameters")] ImportJobAlreadyApplied { job_id: String }, + #[error("invalid import plan input for field {field}")] + InvalidImportPlan { field: &'static str }, + #[error("import plan {plan_id} already exists with different material")] + ImportPlanConflict { plan_id: String }, + #[error("import job {job_id} is unavailable for planning in this workspace")] + ImportPlanJobUnavailable { job_id: String }, + #[error("import job {job_id} is in a terminal state and cannot be planned")] + ImportPlanJobTerminal { job_id: String }, + #[error("import job {job_id} has expired and cannot be planned")] + ImportPlanJobExpired { job_id: String }, + #[error("import plan {plan_id} was not found in this workspace")] + ImportPlanNotFound { plan_id: String }, + #[error("import plan {plan_id} has expired or its anchor job changed; create a fresh preview")] + ImportPlanExpired { plan_id: String }, + #[error( + "import plan {plan_id} material is corrupt or no longer consistent; create a fresh preview" + )] + ImportPlanPayloadCorrupt { plan_id: String }, + #[error( + "import plan {plan_id} no longer matches the revalidated preview; create a fresh preview" + )] + ImportPlanFingerprintStale { plan_id: String }, + #[error( + "import plan idempotency key {idempotency_key} was already used with different plan material; create a fresh preview" + )] + ImportPlanKeyDigestConflict { idempotency_key: String }, + #[error("operation with name {name} already exists; confirm skip, rename, or new version")] + ImportPlanNameConflict { name: String }, + #[error("operation {operation_id} changed after the plan was created; create a fresh preview")] + ImportPlanTargetStale { operation_id: String }, + #[error( + "import plan auth profile {auth_profile_id} is unavailable, revoked or mismatched; create a fresh preview" + )] + ImportPlanAuthUnavailable { auth_profile_id: String }, #[error("unsupported enum representation for field {field}")] InvalidEnumRepresentation { field: &'static str }, #[error("invalid numeric value for field {field}: {value}")] diff --git a/crates/crank-registry/src/lib.rs b/crates/crank-registry/src/lib.rs index 2803855..0250e75 100644 --- a/crates/crank-registry/src/lib.rs +++ b/crates/crank-registry/src/lib.rs @@ -21,40 +21,44 @@ pub mod records { ArtifactSourceLifecycle, ArtifactSourcePage, ArtifactSourceRecord, ArtifactSourceSensitivity, AuthUserRecord, DescriptorKind, DescriptorMetadata, ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId, ImportJobKind, - ImportJobSourceEnvelope, ImportJobStatus, InvitationRecord, InvocationHistoryLoss, - InvocationHistoryLossCategory, InvocationHistoryWriteOutcome, InvocationLogRecord, - InvocationRetentionOutcome, InvocationRetentionPolicy, InvocationRetentionStatus, - MasterKeyIdentityRecord, MasterKeyRotationRecord, MasterKeyRotationStatus, - MembershipRecord, OnboardingMilestoneResult, OnboardingPresentationMilestone, - OperationAgentRef, OperationSampleMetadata, OperationSummary, OperationUsageSummary, - OperationVersionRecord, Page, PlatformApiKeyRecord, ProductEventRecord, - PublishedAgentCatalog, PublishedAgentTool, RegistryOperation, SampleKind, SecretRecord, - SecretVersionRecord, SessionRecord, SkippedImportOperation, UsageAgentBreakdown, - UsageBucket, UsageOperationBreakdown, UsageOutcomeBreakdown, UsageOutcomeGroup, - UsageRollupRecord, UsageSummary, UsageTimelinePoint, VerifiedArtifactSource, - WorkspaceMembershipRecord, WorkspaceRecord, WorkspaceUpstream, WorkspaceUpstreamId, - YamlImportJob, YamlImportJobCompletion, YamlImportJobId, YamlImportJobStatus, + ImportJobSourceEnvelope, ImportJobStatus, ImportPlan, ImportPlanApplication, + ImportPlanApplyCounts, ImportPlanApplyReport, ImportPlanApplyStatus, + ImportPlanCandidateOutcome, ImportPlanCandidateReport, ImportPlanDependencyRef, + ImportPlanId, InvitationRecord, InvocationHistoryLoss, InvocationHistoryLossCategory, + InvocationHistoryWriteOutcome, InvocationLogRecord, InvocationRetentionOutcome, + InvocationRetentionPolicy, InvocationRetentionStatus, MasterKeyIdentityRecord, + MasterKeyRotationRecord, MasterKeyRotationStatus, MembershipRecord, + OnboardingMilestoneResult, OnboardingPresentationMilestone, OperationAgentRef, + OperationSampleMetadata, OperationSummary, OperationUsageSummary, OperationVersionRecord, + Page, PlatformApiKeyRecord, ProductEventRecord, PublishedAgentCatalog, PublishedAgentTool, + RegistryOperation, SampleKind, SecretRecord, SecretVersionRecord, SessionRecord, + SkippedImportOperation, UsageAgentBreakdown, UsageBucket, UsageOperationBreakdown, + UsageOutcomeBreakdown, UsageOutcomeGroup, UsageRollupRecord, UsageSummary, + UsageTimelinePoint, VerifiedArtifactSource, WorkspaceMembershipRecord, WorkspaceRecord, + WorkspaceUpstream, WorkspaceUpstreamId, YamlImportJob, YamlImportJobCompletion, + YamlImportJobId, YamlImportJobStatus, }; } pub mod requests { pub use crate::model::{ AdminSecurityAuditRequest, AppendProductEventRequest, ApplyImportJobRequest, - ClaimArtifactReconciliationRequest, ClaimExpiredArtifactReconciliationRequest, - ConsumeAdminBootstrapContractRequest, CreateAdminBootstrapContractRequest, - CreateAgentDraftVersionRequest, CreateAgentRequest, CreateApprovalRequest, - CreateArtifactSourceRequest, CreateImportJobRequest, CreateInvitationRequest, - CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest, - CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest, - DecideApprovalRequest, DetachArtifactSourceRequest, ExpireApprovalRequest, - FinishApprovalRequest, FinishImportJobRequest, ImportConflictMode, ImportJobSourceEnvelope, - ImportOperationDraft, ListApprovalRequestsQuery, ListArtifactSourcesQuery, - ListInvocationLogsQuery, ListProductEventsQuery, MasterKeyIdentityCandidate, - PublishAgentRequest, PublishRequest, RecordOnboardingCompletionRequest, - RecordOnboardingMilestoneRequest, RecoverAdminPasswordRequest, RotateSecretRequest, - SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, - SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, - UpdateAgentSummaryRequest, UpdateWorkspaceRequest, UsageQuery, + ApplyImportPlanRequest, ClaimArtifactReconciliationRequest, + ClaimExpiredArtifactReconciliationRequest, ConsumeAdminBootstrapContractRequest, + CreateAdminBootstrapContractRequest, CreateAgentDraftVersionRequest, CreateAgentRequest, + CreateApprovalRequest, CreateArtifactSourceRequest, CreateImportJobRequest, + CreateImportPlanRequest, CreateInvitationRequest, CreateInvocationLogRequest, + CreatePlatformApiKeyRequest, CreateSecretRequest, CreateVersionRequest, + CreateWorkspaceRequest, CreateYamlImportJobRequest, DecideApprovalRequest, + DetachArtifactSourceRequest, ExpireApprovalRequest, FinishApprovalRequest, + FinishImportJobRequest, ImportConflictMode, ImportJobSourceEnvelope, ImportOperationDraft, + ListApprovalRequestsQuery, ListArtifactSourcesQuery, ListInvocationLogsQuery, + ListProductEventsQuery, MasterKeyIdentityCandidate, PublishAgentRequest, PublishRequest, + RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest, + RecoverAdminPasswordRequest, RotateSecretRequest, SaveAgentBindingsRequest, + SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, SaveDescriptorMetadataRequest, + SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, UpdateAgentSummaryRequest, + UpdateWorkspaceRequest, UsageQuery, }; } @@ -70,31 +74,35 @@ pub mod infrastructure { pub use model::{ AdminBootstrapContractRecord, AdminSecurityAuditRequest, AgentStateExpectation, AgentSummary, AgentVersionRecord, AppendProductEventOutcome, AppendProductEventRequest, - AppliedImportOperation, ApplyImportJobRequest, ApprovalRequestRecord, ArtifactBlobLifecycle, - ArtifactBlobRecord, ArtifactClaimFinalization, ArtifactClaimFinalizeOutcome, - ArtifactClaimOutcome, ArtifactClaimRecheckOutcome, ArtifactClaimToken, ArtifactDigest, - ArtifactExpiredClaimProbe, ArtifactReconciliationClaim, ArtifactSourceCursor, ArtifactSourceId, - ArtifactSourceLifecycle, ArtifactSourcePage, ArtifactSourceRecord, ArtifactSourceSensitivity, - AuthUserRecord, ClaimArtifactReconciliationRequest, ClaimExpiredArtifactReconciliationRequest, + AppliedImportOperation, ApplyImportJobRequest, ApplyImportPlanRequest, ApplyImportPlanResult, + ApprovalRequestRecord, ArtifactBlobLifecycle, ArtifactBlobRecord, ArtifactClaimFinalization, + ArtifactClaimFinalizeOutcome, ArtifactClaimOutcome, ArtifactClaimRecheckOutcome, + ArtifactClaimToken, ArtifactDigest, ArtifactExpiredClaimProbe, ArtifactReconciliationClaim, + ArtifactSourceCursor, ArtifactSourceId, ArtifactSourceLifecycle, ArtifactSourcePage, + ArtifactSourceRecord, ArtifactSourceSensitivity, AuthUserRecord, + ClaimArtifactReconciliationRequest, ClaimExpiredArtifactReconciliationRequest, ConsumeAdminBootstrapContractRequest, CreateAdminBootstrapContractRequest, CreateAgentDraftVersionRequest, CreateAgentRequest, CreateApprovalRequest, - CreateArtifactSourceRequest, CreateImportJobRequest, CreateInvitationRequest, - CreateInvocationLogRequest, CreatePlatformApiKeyRequest, CreateSecretRequest, - CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest, + CreateArtifactSourceRequest, CreateImportJobRequest, CreateImportPlanRequest, + CreateInvitationRequest, CreateInvocationLogRequest, CreatePlatformApiKeyRequest, + CreateSecretRequest, CreateVersionRequest, CreateWorkspaceRequest, CreateYamlImportJobRequest, DecideApprovalRequest, DescriptorKind, DescriptorMetadata, DetachArtifactSourceRequest, ExpireApprovalRequest, FinishApprovalRequest, FinishImportJobRequest, ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId, ImportJobKind, - ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, InvitationRecord, - InvocationHistoryLoss, InvocationHistoryLossCategory, InvocationHistoryWriteOutcome, - InvocationLogRecord, InvocationRetentionOutcome, InvocationRetentionPolicy, - InvocationRetentionStatus, ListApprovalRequestsQuery, ListArtifactSourcesQuery, - ListInvocationLogsQuery, ListProductEventsQuery, MASTER_KEY_CIPHER_CONTRACT, - MAX_ARTIFACT_CLAIM_RECOVERY_BATCH, MAX_ARTIFACT_SOURCE_PAGE_SIZE, MasterKeyIdentityCandidate, - MasterKeyIdentityRecord, MasterKeyRotationRecord, MasterKeyRotationStatus, MembershipRecord, - OnboardingMilestoneResult, OnboardingPresentationMilestone, OperationAgentRef, - OperationSampleMetadata, OperationStateExpectation, OperationSummary, OperationUsageSummary, - OperationVersionRecord, Page, PlatformApiKeyRecord, ProductEventRecord, PublishAgentRequest, - PublishRequest, PublishedAgentCatalog, PublishedAgentTool, RecordOnboardingCompletionRequest, + ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, ImportPlan, + ImportPlanApplication, ImportPlanApplyCounts, ImportPlanApplyReport, ImportPlanApplyStatus, + ImportPlanCandidateOutcome, ImportPlanCandidateReport, ImportPlanDependencyRef, ImportPlanId, + InvitationRecord, InvocationHistoryLoss, InvocationHistoryLossCategory, + InvocationHistoryWriteOutcome, InvocationLogRecord, InvocationRetentionOutcome, + InvocationRetentionPolicy, InvocationRetentionStatus, ListApprovalRequestsQuery, + ListArtifactSourcesQuery, ListInvocationLogsQuery, ListProductEventsQuery, + MASTER_KEY_CIPHER_CONTRACT, MAX_ARTIFACT_CLAIM_RECOVERY_BATCH, MAX_ARTIFACT_SOURCE_PAGE_SIZE, + MasterKeyIdentityCandidate, MasterKeyIdentityRecord, MasterKeyRotationRecord, + MasterKeyRotationStatus, MembershipRecord, OnboardingMilestoneResult, + OnboardingPresentationMilestone, OperationAgentRef, OperationSampleMetadata, + OperationStateExpectation, OperationSummary, OperationUsageSummary, OperationVersionRecord, + Page, PlatformApiKeyRecord, ProductEventRecord, PublishAgentRequest, PublishRequest, + PublishedAgentCatalog, PublishedAgentTool, RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest, RecoverAdminPasswordRequest, RegistryOperation, RotateSecretRequest, SampleKind, SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, diff --git a/crates/crank-registry/src/migrations.rs b/crates/crank-registry/src/migrations.rs index 9973ba2..cced6d4 100644 --- a/crates/crank-registry/src/migrations.rs +++ b/crates/crank-registry/src/migrations.rs @@ -6,6 +6,7 @@ mod artifact_metadata_v12; mod authority; mod baseline_v1; mod execution_outcome_v5; +mod import_plan_v14; mod master_key_identity_v7; mod onboarding_product_events_v11; mod owned_relations; @@ -16,6 +17,7 @@ mod schema_guard_v10; mod schema_guard_v11; mod schema_guard_v12; mod schema_guard_v13; +mod schema_guard_v14; mod schema_guard_v7; mod schema_guard_v8; mod schema_guard_v9; diff --git a/crates/crank-registry/src/migrations/authority.rs b/crates/crank-registry/src/migrations/authority.rs index a9fdf7f..ed061e5 100644 --- a/crates/crank-registry/src/migrations/authority.rs +++ b/crates/crank-registry/src/migrations/authority.rs @@ -4,6 +4,7 @@ use super::approval_side_effects_v10; use super::artifact_cleanup_indexes_v13; use super::artifact_metadata_v12; use super::execution_outcome_v5; +use super::import_plan_v14; use super::master_key_identity_v7; use super::onboarding_product_events_v11; use super::owned_relations; @@ -25,8 +26,8 @@ mod contract; use contract::baseline_source_digest; use contract::validate_descriptors; const MIGRATION_LOCK_ID: i64 = 0x4352_414E_4B4D_4947; -const CURRENT_VERSION: i64 = 13; -const IMPLEMENTED_VERSIONS: &[i64] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; +const CURRENT_VERSION: i64 = 14; +const IMPLEMENTED_VERSIONS: &[i64] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; const BASELINE_SOURCE_SHA256: &str = "eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675"; const CONSOLIDATION_SOURCE: &str = include_str!("consolidation_v2.sql"); @@ -264,6 +265,7 @@ impl MigrationAuthority { artifact_cleanup_indexes_v13::SOURCE_SHA256, 12, ), + expand_descriptor(14, "import-plan-v14", import_plan_v14::SOURCE_SHA256, 13), ] } pub fn validate_sequence() -> Result<(), MigrationError> { @@ -392,6 +394,9 @@ impl MigrationAuthority { if from < 13 { artifact_cleanup_indexes_v13::apply(&mut transaction, &Self::sequence()[12]).await?; } + if from < 14 { + import_plan_v14::apply(&mut transaction, &Self::sequence()[13]).await?; + } match inspect(&mut transaction).await? { MigrationPreflight::Current { version: CURRENT_VERSION, @@ -558,6 +563,8 @@ async fn inspect(connection: &mut PgConnection) -> Result>(), - vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] + vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] ); assert_eq!(first[0].checksum, "crank-community-baseline-v1"); assert_eq!(first[0].source_digest, BASELINE_SOURCE_SHA256); diff --git a/crates/crank-registry/src/migrations/import_plan_v14.rs b/crates/crank-registry/src/migrations/import_plan_v14.rs new file mode 100644 index 0000000..fb940bd --- /dev/null +++ b/crates/crank-registry/src/migrations/import_plan_v14.rs @@ -0,0 +1,44 @@ +use sqlx::{Postgres, Transaction, query}; + +use super::authority::{MigrationDescriptor, MigrationError}; + +pub(super) const SOURCE: &str = include_str!("import_plan_v14.sql"); +pub(super) const SOURCE_SHA256: &str = + "176c5b3cd12ddaf78dd0d312626a0c387f9c240a765194b34e88f7e0d3b4ca2a"; + +pub(super) async fn apply( + transaction: &mut Transaction<'_, Postgres>, + descriptor: &MigrationDescriptor, +) -> Result<(), MigrationError> { + sqlx::raw_sql(SOURCE) + .execute(&mut **transaction) + .await + .map_err(|_| { + MigrationError::new( + "apply_failed", + "apply.import_plan", + Some(14), + "restore_known_good_backup", + ) + })?; + query( + "insert into __crank_migrations (version, name, checksum, phase, compatibility) + values ($1, $2, $3, $4, $5)", + ) + .bind(descriptor.version) + .bind(descriptor.name) + .bind(&descriptor.checksum) + .bind(descriptor.phase) + .bind(descriptor.compatibility) + .execute(&mut **transaction) + .await + .map_err(|_| { + MigrationError::new( + "apply_failed", + "apply.canonical_ledger", + Some(14), + "restore_known_good_backup", + ) + })?; + Ok(()) +} diff --git a/crates/crank-registry/src/migrations/import_plan_v14.sql b/crates/crank-registry/src/migrations/import_plan_v14.sql new file mode 100644 index 0000000..f595f9e --- /dev/null +++ b/crates/crank-registry/src/migrations/import_plan_v14.sql @@ -0,0 +1,68 @@ +-- Import plan re-import: an immutable plan snapshot bound to the source import +-- job, plus immutable application evidence. Both are workspace-scoped and +-- append-only; UPDATE is rejected. Retention semantics: a plan never outlives +-- its job, so deleting the job cascades to the plan. Application rows +-- deliberately carry no FK to the plan: they keep +-- (workspace_id, idempotency_key, plan_digest) as replay evidence after the +-- job and plan have been cleaned up, and their `plan_id` is a history string. +-- No payload/report byte bounds here: the registry/service contract enforces +-- them. + +alter table import_jobs + add constraint import_jobs_workspace_id_unique + unique (workspace_id, id); + +create table import_plans ( + workspace_id text not null references workspaces(id) on delete cascade, + id text not null, + job_id text not null, + digest text not null, + payload jsonb not null, + created_at timestamptz not null, + expires_at timestamptz not null, + constraint import_plans_digest_check check (digest ~ '^[0-9a-f]{64}$'), + constraint import_plans_payload_check check (jsonb_typeof(payload) = 'object'), + constraint import_plans_expires_check check (expires_at > created_at), + constraint import_plans_job_fk + foreign key (workspace_id, job_id) + references import_jobs(workspace_id, id) + on delete cascade, + primary key (workspace_id, id) +); + +create table import_plan_applications ( + workspace_id text not null references workspaces(id) on delete cascade, + idempotency_key text not null, + plan_id text not null, + plan_digest text not null, + report jsonb not null, + created_at timestamptz not null, + constraint import_plan_applications_plan_digest_check + check (plan_digest ~ '^[0-9a-f]{64}$'), + constraint import_plan_applications_report_check + check (jsonb_typeof(report) = 'object'), + primary key (workspace_id, idempotency_key) +); + +create index import_plans_workspace_job_idx + on import_plans(workspace_id, job_id); + +create index import_plan_applications_workspace_plan_idx + on import_plan_applications(workspace_id, plan_id); + +create function crank_reject_import_plan_mutation() +returns trigger +language plpgsql +as $$ +begin + raise exception 'import plan rows are immutable' using errcode = '23514'; +end; +$$; + +create trigger import_plans_immutable_guard +before update on import_plans +for each row execute function crank_reject_import_plan_mutation(); + +create trigger import_plan_applications_immutable_guard +before update on import_plan_applications +for each row execute function crank_reject_import_plan_mutation(); diff --git a/crates/crank-registry/src/migrations/owned_relations.rs b/crates/crank-registry/src/migrations/owned_relations.rs index b851b4e..eb027f0 100644 --- a/crates/crank-registry/src/migrations/owned_relations.rs +++ b/crates/crank-registry/src/migrations/owned_relations.rs @@ -33,6 +33,8 @@ pub(super) const ONBOARDING_PRODUCT_EVENTS: &[&str] = &[ pub(super) const ARTIFACT_METADATA: &[&str] = &["artifact_blobs", "artifact_sources"]; +pub(super) const IMPORT_PLAN: &[&str] = &["import_plans", "import_plan_applications"]; + pub(super) const CONSOLIDATION: &[&str] = &[ "__crank_migrations", "__crank_migration_legacy_audit", diff --git a/crates/crank-registry/src/migrations/schema_guard.rs b/crates/crank-registry/src/migrations/schema_guard.rs index 2dd3164..1b2de8e 100644 --- a/crates/crank-registry/src/migrations/schema_guard.rs +++ b/crates/crank-registry/src/migrations/schema_guard.rs @@ -37,6 +37,8 @@ pub(super) const OWNED_RELATIONS: &[&str] = &[ "usage_rollups", "artifact_blobs", "artifact_sources", + "import_plans", + "import_plan_applications", ]; const REQUIRED_COLUMNS: &[(&str, &[&str])] = &[ @@ -645,6 +647,13 @@ pub(super) async fn validate_schema_fingerprint( } else { super::schema_guard_v13::validate_v13_artifact_cleanup_indexes(connection).await?; } + if current_version < 14 { + if !super::schema_guard_v14::validate_v14_absent(connection).await? { + return Err(schema_error(current_version)); + } + } else { + super::schema_guard_v14::validate_v14_import_plan(connection).await?; + } Ok(()) } diff --git a/crates/crank-registry/src/migrations/schema_guard_v14.rs b/crates/crank-registry/src/migrations/schema_guard_v14.rs new file mode 100644 index 0000000..3ce6af7 --- /dev/null +++ b/crates/crank-registry/src/migrations/schema_guard_v14.rs @@ -0,0 +1,515 @@ +use sqlx::{PgConnection, Row, query}; + +use super::authority::MigrationError; +use super::schema_guard::schema_error; + +const PLAN_COLUMNS: &[(&str, &str, bool, Option<&str>)] = &[ + ("workspace_id", "text", false, None), + ("id", "text", false, None), + ("job_id", "text", false, None), + ("digest", "text", false, None), + ("payload", "jsonb", false, None), + ("created_at", "timestamp with time zone", false, None), + ("expires_at", "timestamp with time zone", false, None), +]; + +const APPLICATION_COLUMNS: &[(&str, &str, bool, Option<&str>)] = &[ + ("workspace_id", "text", false, None), + ("idempotency_key", "text", false, None), + ("plan_id", "text", false, None), + ("plan_digest", "text", false, None), + ("report", "jsonb", false, None), + ("created_at", "timestamp with time zone", false, None), +]; + +const PLAN_CONSTRAINTS: &[(&str, &str, &str)] = &[ + ("import_plans_pkey", "p", "PRIMARY KEY (workspace_id, id)"), + ( + "import_plans_job_fk", + "f", + "FOREIGN KEY (workspace_id, job_id) REFERENCES import_jobs (workspace_id, id) ON DELETE CASCADE", + ), + ( + "import_plans_workspace_id_fkey", + "f", + "FOREIGN KEY (workspace_id) REFERENCES workspaces (id) ON DELETE CASCADE", + ), + ( + "import_plans_digest_check", + "c", + "CHECK (digest ~ '^[0-9a-f]{64}$'::text)", + ), + ( + "import_plans_payload_check", + "c", + "CHECK (jsonb_typeof(payload) = 'object'::text)", + ), + ( + "import_plans_expires_check", + "c", + "CHECK (expires_at > created_at)", + ), +]; + +const APPLICATION_CONSTRAINTS: &[(&str, &str, &str)] = &[ + ( + "import_plan_applications_pkey", + "p", + "PRIMARY KEY (workspace_id, idempotency_key)", + ), + ( + "import_plan_applications_workspace_id_fkey", + "f", + "FOREIGN KEY (workspace_id) REFERENCES workspaces (id) ON DELETE CASCADE", + ), + ( + "import_plan_applications_plan_digest_check", + "c", + "CHECK (plan_digest ~ '^[0-9a-f]{64}$'::text)", + ), + ( + "import_plan_applications_report_check", + "c", + "CHECK (jsonb_typeof(report) = 'object'::text)", + ), +]; + +pub(super) async fn validate_v14_absent( + connection: &mut PgConnection, +) -> Result { + let mut absent = true; + for table in ["import_plans", "import_plan_applications"] { + if relation_exists(connection, table).await? { + absent = false; + } + } + if absent + && query( + "select exists ( + select 1 from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and t.relname = 'import_jobs' + and c.conname = 'import_jobs_workspace_id_unique' + ) as present", + ) + .fetch_one(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))? + .try_get::("present") + .map_err(|_| MigrationError::storage("preflight.schema"))? + { + absent = false; + } + if absent && function_exists(connection, "crank_reject_import_plan_mutation").await? { + absent = false; + } + Ok(absent) +} + +pub(super) async fn validate_v14_import_plan( + connection: &mut PgConnection, +) -> Result<(), MigrationError> { + validate_table_properties(connection).await?; + validate_columns(connection, "import_plans", PLAN_COLUMNS).await?; + validate_columns(connection, "import_plan_applications", APPLICATION_COLUMNS).await?; + validate_constraints(connection, "import_plans", PLAN_CONSTRAINTS).await?; + validate_constraints( + connection, + "import_plan_applications", + APPLICATION_CONSTRAINTS, + ) + .await?; + validate_jobs_unique(connection).await?; + validate_deferred_flags(connection).await?; + validate_indexes(connection).await?; + validate_immutable_guards(connection).await +} + +async fn validate_table_properties(connection: &mut PgConnection) -> Result<(), MigrationError> { + let rows = query( + "select c.relname, c.relpersistence::text as persistence, + c.relrowsecurity, c.relforcerowsecurity, c.reloptions + from pg_catalog.pg_class c + join pg_catalog.pg_namespace n on n.oid = c.relnamespace + where n.nspname = current_schema() + and c.relname in ('import_plans', 'import_plan_applications') + order by c.relname", + ) + .fetch_all(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let valid = rows.len() == 2 + && rows.iter().all(|row| { + row.try_get::("persistence").ok().as_deref() == Some("p") + && row.try_get::("relrowsecurity").ok() == Some(false) + && row.try_get::("relforcerowsecurity").ok() == Some(false) + && row + .try_get::>, _>("reloptions") + .ok() + .flatten() + .is_none() + }); + if valid { Ok(()) } else { Err(schema_error(14)) } +} + +async fn validate_columns( + connection: &mut PgConnection, + table: &str, + expected: &[(&str, &str, bool, Option<&str>)], +) -> Result<(), MigrationError> { + let rows = query( + "select column_name, data_type, is_nullable, column_default, + datetime_precision, collation_name + from information_schema.columns + where table_schema = current_schema() and table_name = $1 + order by ordinal_position", + ) + .bind(table) + .fetch_all(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + if rows.len() != expected.len() { + return Err(schema_error(14)); + } + for (row, (name, data_type, nullable, default)) in rows.iter().zip(expected) { + let actual_default = row + .try_get::, _>("column_default") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let datetime_precision = row + .try_get::, _>("datetime_precision") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let collation = row + .try_get::, _>("collation_name") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + if row.try_get::("column_name").ok().as_deref() != Some(*name) + || row.try_get::("data_type").ok().as_deref() != Some(*data_type) + || row.try_get::("is_nullable").ok().as_deref() + != Some(if *nullable { "YES" } else { "NO" }) + || actual_default.as_deref() != *default + || (data_type == &"timestamp with time zone" && datetime_precision != Some(6)) + || collation.is_some() + { + return Err(schema_error(14)); + } + } + Ok(()) +} + +async fn validate_constraints( + connection: &mut PgConnection, + table: &str, + expected: &[(&str, &str, &str)], +) -> Result<(), MigrationError> { + let rows = query( + "select c.conname, c.contype::text as contype, pg_get_constraintdef(c.oid, true) as definition + from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() and t.relname = $1 + order by c.conname", + ) + .bind(table) + .fetch_all(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + if rows.len() != expected.len() { + return Err(schema_error(14)); + } + for row in rows { + let name = row + .try_get::("conname") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let kind = row + .try_get::("contype") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let definition = row + .try_get::("definition") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let Some((_, _, expected_definition)) = + expected.iter().find(|(expected_name, expected_kind, _)| { + name == *expected_name && kind == *expected_kind + }) + else { + return Err(schema_error(14)); + }; + if canonical_definition(&definition) != canonical_definition(expected_definition) { + return Err(schema_error(14)); + } + } + Ok(()) +} + +async fn validate_deferred_flags(connection: &mut PgConnection) -> Result<(), MigrationError> { + for (deferrable, deferred, constraint) in [ + (false, false, "import_plans_job_fk"), + (false, false, "import_plans_workspace_id_fkey"), + (false, false, "import_plan_applications_workspace_id_fkey"), + ] { + let row = query( + "select c.conname, c.condeferrable, c.condeferred + from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() and c.conname = $1 and c.contype = 'f'", + ) + .bind(constraint) + .fetch_optional(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let valid = row.is_some_and(|row| { + row.try_get::("condeferrable").ok() == Some(deferrable) + && row.try_get::("condeferred").ok() == Some(deferred) + }); + if !valid { + return Err(schema_error(14)); + } + } + Ok(()) +} + +async fn validate_jobs_unique(connection: &mut PgConnection) -> Result<(), MigrationError> { + let row = query( + "select c.conname, pg_get_constraintdef(c.oid, true) as definition + from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and t.relname = 'import_jobs' + and c.conname = 'import_jobs_workspace_id_unique' + and c.contype = 'u'", + ) + .fetch_optional(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let valid = row.is_some_and(|row| { + row.try_get::("definition") + .ok() + .as_deref() + .is_some_and(|definition| { + canonical_definition(definition) + == canonical_definition("UNIQUE (workspace_id, id)") + }) + }); + if valid { Ok(()) } else { Err(schema_error(14)) } +} + +async fn validate_indexes(connection: &mut PgConnection) -> Result<(), MigrationError> { + struct IndexContract { + name: &'static str, + table: &'static str, + unique: bool, + columns: &'static [&'static str], + } + const CONTRACTS: &[IndexContract] = &[ + IndexContract { + name: "import_plans_workspace_job_idx", + table: "import_plans", + unique: false, + columns: &["workspace_id", "job_id"], + }, + IndexContract { + name: "import_plan_applications_workspace_plan_idx", + table: "import_plan_applications", + unique: false, + columns: &["workspace_id", "plan_id"], + }, + IndexContract { + name: "import_jobs_workspace_id_unique", + table: "import_jobs", + unique: true, + columns: &["workspace_id", "id"], + }, + IndexContract { + name: "import_plans_pkey", + table: "import_plans", + unique: true, + columns: &["workspace_id", "id"], + }, + IndexContract { + name: "import_plan_applications_pkey", + table: "import_plan_applications", + unique: true, + columns: &["workspace_id", "idempotency_key"], + }, + ]; + for contract in CONTRACTS { + let row = query( + "select t.relname as table_name, am.amname as access_method, + i.indisvalid, i.indisready, i.indisunique, + i.indnkeyatts, i.indnatts, + pg_get_indexdef(i.indexrelid, 1, true) as first_column, + pg_get_indexdef(i.indexrelid, 2, true) as second_column, + pg_get_expr(i.indpred, i.indrelid) as predicate + from pg_catalog.pg_index i + join pg_catalog.pg_class idx on idx.oid = i.indexrelid + join pg_catalog.pg_class t on t.oid = i.indrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + join pg_catalog.pg_am am on am.oid = idx.relam + where n.nspname = current_schema() and idx.relname = $1", + ) + .bind(contract.name) + .fetch_optional(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let valid = row.is_some_and(|row| { + row.try_get::("table_name").ok().as_deref() == Some(contract.table) + && row.try_get::("access_method").ok().as_deref() == Some("btree") + && row.try_get::("indisvalid").ok() == Some(true) + && row.try_get::("indisready").ok() == Some(true) + && row.try_get::("indisunique").ok() == Some(contract.unique) + && row.try_get::("indnkeyatts").ok() == Some(2) + && row.try_get::("indnatts").ok() == Some(2) + && row.try_get::("first_column").ok().as_deref() + == Some(contract.columns[0]) + && row.try_get::("second_column").ok().as_deref() + == Some(contract.columns[1]) + && row + .try_get::, _>("predicate") + .ok() + .flatten() + .is_none() + }); + if !valid { + return Err(schema_error(14)); + } + } + Ok(()) +} + +async fn validate_immutable_guards(connection: &mut PgConnection) -> Result<(), MigrationError> { + let rows = query( + "select tg.tgname, t.relname as table_name, tg.tgtype::int as tgtype, + tg.tgenabled::text as tgenabled, tg.tgisinternal, + pg_get_triggerdef(tg.oid, true) as definition + from pg_catalog.pg_trigger tg + join pg_catalog.pg_class t on t.oid = tg.tgrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and t.relname in ('import_plans', 'import_plan_applications') + and not tg.tgisinternal + order by tg.tgname", + ) + .fetch_all(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let contracts = [ + ("import_plans_immutable_guard", "import_plans"), + ( + "import_plan_applications_immutable_guard", + "import_plan_applications", + ), + ]; + let mut seen = [false; 2]; + for row in &rows { + let name = row + .try_get::("tgname") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let table = row + .try_get::("table_name") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let tgtype = row + .try_get::("tgtype") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let enabled = row + .try_get::("tgenabled") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let internal = row + .try_get::("tgisinternal") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let definition = row + .try_get::("definition") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let index = contracts + .iter() + .position(|(expected_name, expected_table)| { + name == *expected_name && table == *expected_table + }) + .ok_or_else(|| schema_error(14))?; + let before_update_row = (tgtype & 1) != 0 && (tgtype & 2) != 0 && (tgtype & 16) != 0; + let calls_guard = definition + .to_lowercase() + .contains("execute function crank_reject_import_plan_mutation()"); + if !(before_update_row && enabled == "O" && !internal && calls_guard) { + return Err(schema_error(14)); + } + seen[index] = true; + } + if !seen.iter().all(|present| *present) { + return Err(schema_error(14)); + } + if !function_exists(connection, "crank_reject_import_plan_mutation").await? { + return Err(schema_error(14)); + } + let policy_count = sqlx::query_scalar::<_, i64>( + "select + (select count(*) from pg_catalog.pg_policy p + join pg_catalog.pg_class t on t.oid = p.polrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and t.relname in ('import_plans', 'import_plan_applications'))", + ) + .fetch_one(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + if policy_count != 0 { + return Err(schema_error(14)); + } + Ok(()) +} + +fn canonical_definition(value: &str) -> String { + let mut quoted = false; + value + .chars() + .filter_map(|character| { + if character == '\'' { + quoted = !quoted; + return Some(character); + } + if character.is_ascii_whitespace() { + None + } else if quoted { + Some(character) + } else { + Some(character.to_ascii_lowercase()) + } + }) + .collect() +} + +async fn relation_exists( + connection: &mut PgConnection, + relation: &str, +) -> Result { + query("select to_regclass(format('%I.%I', current_schema(), $1))::text is not null as present") + .bind(relation) + .fetch_one(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.inventory"))? + .try_get::("present") + .map_err(|_| MigrationError::storage("preflight.inventory")) +} + +async fn function_exists( + connection: &mut PgConnection, + function: &str, +) -> Result { + query( + "select exists ( + select 1 from pg_catalog.pg_proc p + join pg_catalog.pg_namespace n on n.oid = p.pronamespace + where n.nspname = current_schema() + and p.proname = $1 + and p.pronargs = 0 + and p.prokind = 'f' + ) as present", + ) + .bind(function) + .fetch_one(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))? + .try_get::("present") + .map_err(|_| MigrationError::storage("preflight.schema")) +} diff --git a/crates/crank-registry/src/model.rs b/crates/crank-registry/src/model.rs index 01f4a89..cc30d9a 100644 --- a/crates/crank-registry/src/model.rs +++ b/crates/crank-registry/src/model.rs @@ -48,8 +48,13 @@ mod artifact_source; pub use artifact_source::*; +mod import_plan; + +pub use import_plan::*; + define_registry_id!(YamlImportJobId); define_registry_id!(ImportJobId); +define_registry_id!(ImportPlanId); define_registry_id!(WorkspaceUpstreamId); pub type RegistryOperation = Operation; diff --git a/crates/crank-registry/src/model/import_plan.rs b/crates/crank-registry/src/model/import_plan.rs new file mode 100644 index 0000000..aacbc5b --- /dev/null +++ b/crates/crank-registry/src/model/import_plan.rs @@ -0,0 +1,166 @@ +use super::*; + +/// A server-authored, immutable OpenAPI import plan. +/// +/// The `digest` is not supplied by callers: the registry derives it from the +/// deterministic byte encoding of `payload` at creation time, so a replay can +/// be recognized as byte-identical material versus a changed conflict. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ImportPlan { + pub id: ImportPlanId, + pub workspace_id: WorkspaceId, + pub job_id: ImportJobId, + pub digest: String, + pub payload: Value, + #[serde(with = "time::serde::rfc3339")] + pub created_at: OffsetDateTime, + #[serde(with = "time::serde::rfc3339")] + pub expires_at: OffsetDateTime, +} + +/// Request to persist a new immutable import plan. +/// +/// There is deliberately no `digest` field: the registry computes it from the +/// server-authored `payload`, so client-supplied digests can never bind. +#[derive(Clone, Debug, PartialEq)] +pub struct CreateImportPlanRequest<'a> { + pub id: &'a ImportPlanId, + pub workspace_id: &'a WorkspaceId, + pub job_id: &'a ImportJobId, + pub payload: &'a Value, + pub created_at: &'a OffsetDateTime, + pub expires_at: &'a OffsetDateTime, +} + +/// Applies a stored, immutable import plan atomically. +/// +/// The registry loads and revalidates the plan itself: client candidate +/// material is never accepted. The request carries only the plan reference, +/// the caller-confirmed plan digest, a bounded idempotency key and the +/// service-recomputed preview fingerprint. A replay of a committed +/// `(workspace, idempotency_key, plan_digest)` triple returns the stored +/// immutable report even after the plan and its job have been cleaned up. +#[derive(Clone, Debug, PartialEq)] +pub struct ApplyImportPlanRequest<'a> { + pub workspace_id: &'a WorkspaceId, + pub plan_id: &'a ImportPlanId, + /// Caller-confirmed digest of the immutable plan payload (64 lowercase + /// hex). Replays must match the digest stored with the key; a different + /// digest under an already-used key is a typed conflict. + pub plan_digest: &'a str, + /// Bounded idempotency key identifying this logical apply. + pub idempotency_key: &'a str, + /// Preview fingerprint recomputed by the service from the revalidated + /// source/dependencies (importer/projection/preview digests). For new + /// attempts the registry compares it against the immutable plan + /// fingerprint inside the apply transaction: a mismatch is a typed + /// re-preview conflict. Committed replays skip this comparison. + pub verified_fingerprint: &'a Value, + pub applied_at: &'a OffsetDateTime, +} + +/// Stored application evidence for a scoped idempotency key: the plan +/// digest the apply was committed under plus the immutable report. The row +/// outlives the plan and its job, so Admin can return committed replays +/// (or detect a changed-digest conflict) before the plan/job can be looked +/// up at all. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ImportPlanApplication { + pub plan_digest: String, + pub result: ApplyImportPlanResult, +} + +/// Result of one `apply_import_plan` call. +/// +/// `replayed` is a separate transport marker: the stored immutable report +/// is returned byte-for-byte on replay and never mutated. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ApplyImportPlanResult { + /// True when the report came from committed replay evidence instead of + /// being produced by this call. + pub replayed: bool, + pub report: ImportPlanApplyReport, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ImportPlanApplyStatus { + Applied, + Rejected, +} + +/// Per-candidate outcome vocabulary of an import plan apply report. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ImportPlanCandidateOutcome { + Created, + Versioned, + Renamed, + Skipped, + Excluded, + Rejected, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ImportPlanCandidateReport { + pub key: String, + pub name: String, + pub outcome: ImportPlanCandidateOutcome, + /// Authoritative target identifier for `renamed` (new operation), + /// `versioned` (targeted operation) and `skipped`/`rejected` (exact + /// authorized target, when the plan carried one). + pub operation_id: Option, + /// Draft version produced by `created`/`versioned`/`renamed`. + pub version: Option, + /// Bounded redacted machine reason (for example `not_selected`, the + /// blocked-candidate finding code, `operation_name_conflict`). + pub reason: Option, +} + +/// The immutable, redacted machine view of one import plan apply. +/// +/// Contains only digests, identifiers, names and reasons — never credentials +/// (secret material lives in the secrets table and is only referenced here +/// through bounded id strings). Successful applies persist this exact +/// object as the replay evidence; rejected applies return it without +/// persisting, so it is always readable in the Admin/UI response. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ImportPlanApplyReport { + pub schema_version: String, + pub status: ImportPlanApplyStatus, + pub plan_id: String, + pub plan_digest: String, + pub job_id: String, + pub idempotency_key: String, + pub source_id: String, + pub source_digest: String, + pub dependencies: Vec, + pub counts: ImportPlanApplyCounts, + pub outcomes: Vec, + /// Bounded human summary (RU) derived from the machine fields. + pub summary: String, + /// Stable recovery guidance: `none`, `repreview_required` or + /// `confirm_skip_rename_or_new_version`. + pub recovery: String, + #[serde(with = "time::serde::rfc3339")] + pub applied_at: OffsetDateTime, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct ImportPlanApplyCounts { + pub created: u32, + pub versioned: u32, + pub renamed: u32, + pub skipped: u32, + pub excluded: u32, + pub rejected: u32, +} + +/// Bounded verified-source digest reference carried by an apply report. +/// Plain strings keep the report serializable without reusing artifact +/// internals; the registry populates it from the immutable plan material. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ImportPlanDependencyRef { + pub source_id: String, + pub digest: String, +} diff --git a/crates/crank-registry/src/postgres/import_job.rs b/crates/crank-registry/src/postgres/import_job.rs index be56587..a5a8e8a 100644 --- a/crates/crank-registry/src/postgres/import_job.rs +++ b/crates/crank-registry/src/postgres/import_job.rs @@ -1,10 +1,19 @@ +mod plan_apply; + use super::*; -use crate::{ArtifactSourceId, ImportJobSourceEnvelope}; +use crate::{ + ArtifactSourceId, CreateImportPlanRequest, ImportJobKind, ImportJobSourceEnvelope, ImportPlan, + ImportPlanId, +}; use crank_artifacts::ArtifactRef; +use sha2::{Digest, Sha256}; +use sqlx::Executor; const APPLICATION_RESULT_KEY: &str = "_crank_application_result"; const IMPORT_JOB_CLEANUP_BATCH: u32 = 128; const MAX_IMPORT_JOB_DOCUMENTS: usize = 32; +const MAX_IMPORT_PLAN_ID_BYTES: usize = 128; +const MAX_IMPORT_PLAN_PAYLOAD_BYTES: usize = 4 * 1024 * 1024; const DANGLING_OPENAPI_SOURCE_GRACE: time::Duration = time::Duration::minutes(5); impl PostgresRegistry { @@ -79,6 +88,186 @@ impl PostgresRegistry { row.as_ref().map(map_import_job).transpose() } + /// Persists a new server-authored, immutable OpenAPI import plan. + /// + /// The `digest` is derived by the registry from a deterministic byte + /// encoding of `payload`; callers cannot bind one. Creation is validated + /// against a workspace-owned, non-expired, non-terminal OpenAPI job that + /// still carries a verifiable source envelope, and the plan's lifetime + /// must not outlive the job's. Repeating the request with the same id and + /// byte-identical material returns the existing plan; the same id with + /// different material/job/expiry is a typed conflict (no UPDATE). + /// + /// Concurrency is serialized under the job row lock, then the workspace + /// row lock — the same lock order as the legacy Apply path (job -> + /// workspace). The authoritative existence re-check under the workspace + /// lock makes concurrent inserts of the same id converge to a replay or a + /// typed conflict instead of a raw unique violation. Timestamps are + /// normalized to Postgres `timestamptz` precision (microseconds) so the + /// first returned plan is exactly what every later read returns. + pub async fn create_import_plan( + &self, + request: CreateImportPlanRequest<'_>, + ) -> Result { + validate_plan_id(request.id)?; + let created_at = db_precision_utc(*request.created_at); + let expires_at = db_precision_utc(*request.expires_at); + if expires_at <= created_at { + return Err(RegistryError::InvalidImportPlan { + field: "expires_at", + }); + } + let digest = plan_digest_from_payload(request.payload)?; + + let mut transaction = self.pool.begin().await?; + + if let Some(existing) = + find_plan_row(&mut *transaction, request.workspace_id, request.id).await? + { + match plan_replay_outcome(existing, &digest, &request, created_at, expires_at) { + Ok(stored) => { + transaction.commit().await?; + return Ok(stored); + } + Err(error) => { + transaction.rollback().await?; + return Err(error); + } + } + } + + let job_row = sqlx::query( + "select kind, status, preview_payload, expires_at + from import_jobs + where workspace_id = $1 and id = $2 + for update", + ) + .bind(request.workspace_id.as_str()) + .bind(request.job_id.as_str()) + .fetch_optional(&mut *transaction) + .await?; + + let job_row = match job_row { + Some(row) => row, + None => { + transaction.rollback().await?; + return Err(RegistryError::ImportPlanJobUnavailable { + job_id: request.job_id.as_str().to_owned(), + }); + } + }; + + let kind = deserialize_enum_text::(job_row.try_get("kind")?, "kind")?; + if kind != ImportJobKind::OpenApi { + transaction.rollback().await?; + return Err(RegistryError::ImportPlanJobUnavailable { + job_id: request.job_id.as_str().to_owned(), + }); + } + let status = + deserialize_enum_text::(job_row.try_get("status")?, "status")?; + if status != ImportJobStatus::Pending { + transaction.rollback().await?; + return Err(RegistryError::ImportPlanJobTerminal { + job_id: request.job_id.as_str().to_owned(), + }); + } + let job_expires: OffsetDateTime = job_row.try_get("expires_at")?; + if job_expires <= OffsetDateTime::now_utc() { + transaction.rollback().await?; + return Err(RegistryError::ImportPlanJobExpired { + job_id: request.job_id.as_str().to_owned(), + }); + } + // The plan must not outlive the job that anchors it, otherwise the + // protected source snapshots can expire while the plan is still valid. + if expires_at > job_expires { + transaction.rollback().await?; + return Err(RegistryError::InvalidImportPlan { + field: "expires_at", + }); + } + let preview_payload: Value = job_row.try_get("preview_payload")?; + if source_from_payload(&preview_payload)?.is_none() { + transaction.rollback().await?; + return Err(RegistryError::ImportPlanJobUnavailable { + job_id: request.job_id.as_str().to_owned(), + }); + } + + sqlx::query("select id from workspaces where id = $1 for update") + .bind(request.workspace_id.as_str()) + .fetch_one(&mut *transaction) + .await?; + + // Authoritative re-check: any concurrent insert of this (workspace, id) + // must also hold the workspace lock, so a winner cannot appear between + // the fast-path check and the insert. + if let Some(existing) = + find_plan_row(&mut *transaction, request.workspace_id, request.id).await? + { + match plan_replay_outcome(existing, &digest, &request, created_at, expires_at) { + Ok(stored) => { + transaction.commit().await?; + return Ok(stored); + } + Err(error) => { + transaction.rollback().await?; + return Err(error); + } + } + } + + sqlx::query( + "insert into import_plans ( + workspace_id, id, job_id, digest, payload, created_at, expires_at + ) values ( + $1, $2, $3, $4, $5, $6::timestamptz, $7::timestamptz + )", + ) + .bind(request.workspace_id.as_str()) + .bind(request.id.as_str()) + .bind(request.job_id.as_str()) + .bind(&digest) + .bind(request.payload) + .bind(created_at) + .bind(expires_at) + .execute(&mut *transaction) + .await?; + transaction.commit().await?; + + Ok(ImportPlan { + id: request.id.as_str().to_owned().into(), + workspace_id: (*request.workspace_id).clone(), + job_id: (*request.job_id).clone(), + digest, + payload: (*request.payload).clone(), + created_at, + expires_at, + }) + } + + /// Reads an import plan. Returns `None` for an absent id or any id owned + /// by another workspace so a foreign scope is never revealed by an error. + pub async fn get_import_plan( + &self, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + ) -> Result, RegistryError> { + validate_plan_id(plan_id)?; + let row = sqlx::query( + "select id, workspace_id, job_id, digest, payload, created_at, expires_at + from import_plans + where workspace_id = $1 and id = $2", + ) + .bind(workspace_id.as_str()) + .bind(plan_id.as_str()) + .fetch_optional(&self.pool) + .await?; + + row.as_ref().map(map_import_plan).transpose() + } + pub async fn finish_import_job( &self, request: FinishImportJobRequest<'_>, @@ -673,3 +862,96 @@ async fn next_available_operation_name( unreachable!() } + +fn map_import_plan(row: &PgRow) -> Result { + Ok(ImportPlan { + id: ImportPlanId::new(row.try_get::("id")?), + workspace_id: WorkspaceId::new(row.try_get::("workspace_id")?), + job_id: ImportJobId::new(row.try_get::("job_id")?), + digest: row.try_get::("digest")?, + payload: row.try_get::("payload")?, + created_at: row.try_get("created_at")?, + expires_at: row.try_get("expires_at")?, + }) +} + +/// Derives the canonical plan digest: the lowercase SHA-256 of the payload +/// serialized to its deterministic byte form. `serde_json::Value` object keys +/// are ordered (BTreeMap-backed map), so `to_vec` is stable across calls and +/// platforms — the digest is therefore reproducible for byte-identical +/// material without requiring a client-supplied digest. +fn plan_digest_from_payload(payload: &Value) -> Result { + if !payload.is_object() { + return Err(RegistryError::InvalidImportPlan { field: "payload" }); + } + let canonical = serde_json::to_vec(payload)?; + if canonical.len() > MAX_IMPORT_PLAN_PAYLOAD_BYTES { + return Err(RegistryError::InvalidImportPlan { field: "payload" }); + } + Ok(format!("{:x}", Sha256::digest(canonical))) +} + +fn validate_plan_id(id: &ImportPlanId) -> Result<(), RegistryError> { + if !(1..=MAX_IMPORT_PLAN_ID_BYTES).contains(&id.as_str().len()) { + return Err(RegistryError::InvalidImportPlan { field: "id" }); + } + Ok(()) +} + +/// Reads a workspace-scoped plan row, locking it if it exists, inside the +/// given executor (the creation transaction). +async fn find_plan_row<'c, E>( + executor: E, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, +) -> Result, RegistryError> +where + E: Executor<'c, Database = Postgres>, +{ + Ok(sqlx::query( + "select id, workspace_id, job_id, digest, payload, created_at, expires_at + from import_plans + where workspace_id = $1 and id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(plan_id.as_str()) + .fetch_optional(executor) + .await?) +} + +/// Decides the outcome for an already-present plan: byte-identical material, +/// same job and same (DB-precision) lifetime is a replay returning the stored +/// row; anything else is a typed conflict. Pure check — no transaction side +/// effects, so the caller keeps commit/rollback authority. +fn plan_replay_outcome( + existing: PgRow, + digest: &str, + request: &CreateImportPlanRequest<'_>, + created_at: OffsetDateTime, + expires_at: OffsetDateTime, +) -> Result { + let stored = map_import_plan(&existing)?; + let replay = stored.digest == digest + && stored.job_id == *request.job_id + && stored.created_at == created_at + && stored.expires_at == expires_at; + if replay { + return Ok(stored); + } + Err(RegistryError::ImportPlanConflict { + plan_id: request.id.as_str().to_owned(), + }) +} + +/// Reduces an instant to Postgres `timestamptz` precision (microseconds). +/// +/// `OffsetDateTime` may carry nanosecond precision that the column silently +/// truncates; binding the unnormalized value would make the first returned +/// plan differ from every later read and break replay equality. +fn db_precision_utc(instant: OffsetDateTime) -> OffsetDateTime { + let nanos = instant.unix_timestamp_nanos(); + let micros = (nanos / 1_000) * 1_000; + OffsetDateTime::from_unix_timestamp_nanos(micros) + .expect("timestamp within the valid range after truncating nanoseconds") +} diff --git a/crates/crank-registry/src/postgres/import_job/plan_apply.rs b/crates/crank-registry/src/postgres/import_job/plan_apply.rs new file mode 100644 index 0000000..d9c5851 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply.rs @@ -0,0 +1,387 @@ +use super::*; +use crate::{ + ApplyImportPlanRequest, ApplyImportPlanResult, ImportJobKind, ImportJobSourceEnvelope, + ImportPlanApplication, ImportPlanApplyCounts, ImportPlanApplyReport, ImportPlanApplyStatus, + ImportPlanCandidateOutcome, ImportPlanCandidateReport, ImportPlanDependencyRef, +}; +use crank_core::{AuthProfile, OperationId, OperationStatus, Protocol}; +use serde::Deserialize; +use std::collections::{BTreeMap, BTreeSet}; + +mod material; +mod mutations; +mod verify; + +use material::*; +use mutations::*; +use verify::*; + +const PLAN_PAYLOAD_SCHEMA_VERSION: &str = "import-plan-v1"; +const APPLY_REPORT_SCHEMA_VERSION: &str = "import-plan-apply-report-v1"; +/// Same upper bound as plan creation keeps the report compatible with every +/// plan that was accepted: at most this many candidates / names / targets. +const MAX_IMPORT_PLAN_KEYS: usize = 512; +const MAX_IMPORT_PLAN_KEY_BYTES: usize = 4_096; +const MAX_IMPORT_PLAN_NAME_BYTES: usize = 256; +const MAX_IMPORT_PLAN_ID_REF_BYTES: usize = 128; +const MAX_IMPORT_PLAN_SERVER_URL_BYTES: usize = 8_192; +const MAX_IMPORT_PLAN_REASON_BYTES: usize = 1_024; +const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128; +/// Bound for the serialized apply report. Must accommodate a full accepted +/// plan: report entries carry only key/name/ids (already bounded) while the +/// plan payload — which carries far more per candidate — is capped at +/// `MAX_IMPORT_PLAN_PAYLOAD_BYTES` (4 MiB) at creation. This keeps every +/// accepted plan's exact outcomes inside the report without truncation. +const MAX_IMPORT_PLAN_REPORT_BYTES: usize = MAX_IMPORT_PLAN_PAYLOAD_BYTES; + +/// Failure of one apply transaction: a typed registry conflict, or the +/// application-row uniqueness collision that must be reconciled after the +/// transaction is rolled back. +#[derive(Debug)] +pub(super) enum ApplyTxFailure { + Registry(RegistryError), + KeyConflict, +} + +// --------------------------------------------------------------------------- +// The transaction itself. +// --------------------------------------------------------------------------- + +/// Runs the whole apply: locks in the fixed order, revalidates the +/// immutable material, applies every selected candidate and inserts the +/// application record. All domain mutations and the report insert are one +/// transaction. +async fn apply_import_plan_transaction( + tx: &mut Transaction<'_, Postgres>, + request: &ApplyImportPlanRequest<'_>, +) -> Result<(bool, ImportPlanApplyReport), ApplyTxFailure> { + let fail = |error: RegistryError| ApplyTxFailure::Registry(error); + let plan_id = request.plan_id.as_str().to_owned(); + + // Lock order is strictly `import_jobs` -> `import_plans` -> + // `workspaces` -> artifact rows -> auth rows -> operation rows. The + // expired-job cleanup locks the job first and cascades to the plan, so + // a plan->job ordering could deadlock; this ordering cannot wait on the + // cleanup. + let plan_hint = + sqlx::query("select job_id from import_plans where workspace_id = $1 and id = $2") + .bind(request.workspace_id.as_str()) + .bind(request.plan_id.as_str()) + .fetch_optional(&mut **tx) + .await + .map_err(|e| fail(RegistryError::Storage(e)))?; + let Some(plan_hint) = plan_hint else { + return Err(fail(RegistryError::ImportPlanNotFound { plan_id })); + }; + let job_id: String = plan_hint + .try_get::("job_id") + .map_err(|e| fail(RegistryError::Storage(e)))?; + + let now = OffsetDateTime::now_utc(); + let job_row = sqlx::query( + "select kind, status, preview_payload, expires_at + from import_jobs + where workspace_id = $1 and id = $2 + for update", + ) + .bind(request.workspace_id.as_str()) + .bind(&job_id) + .fetch_optional(&mut **tx) + .await + .map_err(|e| fail(RegistryError::Storage(e)))?; + let Some(job_row) = job_row else { + // The job was cleaned up (or the plan cascaded with it): stale. + return Err(fail(RegistryError::ImportPlanExpired { + plan_id: plan_id.clone(), + })); + }; + let job_kind_str: String = job_row + .try_get::("kind") + .map_err(|e| fail(RegistryError::Storage(e)))?; + let job_kind: ImportJobKind = deserialize_enum_text(&job_kind_str, "kind").map_err(fail)?; + if job_kind != ImportJobKind::OpenApi { + return Err(fail(RegistryError::ImportPlanPayloadCorrupt { + plan_id: plan_id.clone(), + })); + } + let job_status_str: String = job_row + .try_get::("status") + .map_err(|e| fail(RegistryError::Storage(e)))?; + let job_status: ImportJobStatus = + deserialize_enum_text(&job_status_str, "status").map_err(fail)?; + if job_status != ImportJobStatus::Pending { + return Err(fail(RegistryError::ImportPlanExpired { + plan_id: plan_id.clone(), + })); + } + let job_expires = job_row + .try_get::("expires_at") + .map_err(|e| fail(RegistryError::Storage(e)))?; + if job_expires <= now { + return Err(fail(RegistryError::ImportPlanExpired { + plan_id: plan_id.clone(), + })); + } + let job_preview_payload = job_row + .try_get::("preview_payload") + .map_err(|e| fail(RegistryError::Storage(e)))?; + + // Now lock the plan itself and verify it survived with identical + // material. + let plan_row = sqlx::query( + "select job_id, digest, payload, expires_at + from import_plans + where workspace_id = $1 and id = $2 + for update", + ) + .bind(request.workspace_id.as_str()) + .bind(request.plan_id.as_str()) + .fetch_optional(&mut **tx) + .await + .map_err(|e| fail(RegistryError::Storage(e)))?; + let Some(plan_row) = plan_row else { + return Err(fail(RegistryError::ImportPlanExpired { + plan_id: plan_id.clone(), + })); + }; + let stored_job_id: String = plan_row + .try_get::("job_id") + .map_err(|e| fail(RegistryError::Storage(e)))?; + if stored_job_id != job_id { + return Err(fail(RegistryError::ImportPlanPayloadCorrupt { + plan_id: plan_id.clone(), + })); + } + let stored_digest: String = plan_row + .try_get::("digest") + .map_err(|e| fail(RegistryError::Storage(e)))?; + let plan_payload = plan_row + .try_get::("payload") + .map_err(|e| fail(RegistryError::Storage(e)))?; + let plan_expires = plan_row + .try_get::("expires_at") + .map_err(|e| fail(RegistryError::Storage(e)))?; + if plan_expires <= now { + return Err(fail(RegistryError::ImportPlanExpired { + plan_id: plan_id.clone(), + })); + } + + // Serialize the workspace and re-check the idempotency key: a racing + // identical apply committed first, or a racing different digest. + let workspace_present = sqlx::query_scalar::<_, bool>( + "select exists(select 1 from workspaces where id = $1 for update)", + ) + .bind(request.workspace_id.as_str()) + .fetch_one(&mut **tx) + .await + .map_err(|e| fail(RegistryError::Storage(e)))?; + if !workspace_present { + return Err(fail(RegistryError::WorkspaceNotFound { + workspace_id: request.workspace_id.as_str().to_owned(), + })); + } + let stored_application = sqlx::query( + "select plan_digest, report + from import_plan_applications + where workspace_id = $1 and idempotency_key = $2", + ) + .bind(request.workspace_id.as_str()) + .bind(request.idempotency_key) + .fetch_optional(&mut **tx) + .await + .map_err(|e| fail(RegistryError::Storage(e)))?; + if let Some(stored_application) = stored_application { + let stored_digest: String = stored_application + .try_get::("plan_digest") + .map_err(|e| fail(RegistryError::Storage(e)))?; + if stored_digest == request.plan_digest { + let report: ImportPlanApplyReport = serde_json::from_value( + stored_application + .try_get::("report") + .map_err(|e| fail(RegistryError::Storage(e)))?, + ) + .map_err(|error| ApplyTxFailure::Registry(RegistryError::Serialization(error)))?; + return Ok((true, report)); + } + return Err(fail(RegistryError::ImportPlanKeyDigestConflict { + idempotency_key: request.idempotency_key.to_owned(), + })); + } + + // Revalidate the immutable material and the service fingerprint. + let plan = + read_and_validate_plan_material(&plan_payload, request, &stored_digest, &stored_job_id) + .map_err(fail)?; + verify_job_source_consistency(&job_preview_payload, &plan, &plan_id).map_err(fail)?; + + // Blocked selected candidates reject the whole apply: zero mutation. + if plan + .candidates + .iter() + .any(|candidate| candidate.state == "blocked") + { + let report = build_rejected_report(request, &plan, db_precision_utc(*request.applied_at)); + return Ok((false, report)); + } + + // Verify the source and dependency snapshots are still active with the + // recorded digests (no network fetch). The job stays pending and the + // sources stay attached: other stored plans remain viable until TTL. + let mut envelopes: Vec = plan + .dependencies + .iter() + .cloned() + .map(|dependency| ImportJobSourceEnvelope { + source_id: ArtifactSourceId::new(dependency.source_id), + digest: ArtifactRef::parse(&dependency.digest).expect("validated above"), + }) + .collect(); + envelopes.push(ImportJobSourceEnvelope { + source_id: ArtifactSourceId::new(plan.source.source_id.clone()), + digest: ArtifactRef::parse(&plan.source.digest).expect("validated above"), + }); + envelopes.sort_by(|a, b| a.source_id.as_str().cmp(b.source_id.as_str())); + envelopes.dedup_by(|a, b| a.source_id == b.source_id && a.digest == b.digest); + for envelope in &envelopes { + lock_and_verify_envelope(tx, request.workspace_id, envelope) + .await + .map_err(fail)?; + } + + // Revalidate every referenced auth profile and secret under locks. + lock_and_verify_auth(tx, request.workspace_id, &plan.candidates) + .await + .map_err(fail)?; + + // Apply the selected candidates in deterministic order. + let mut candidates: Vec<&StoredPlanCandidate> = plan.candidates.iter().collect(); + candidates.sort_by(|a, b| a.key.cmp(&b.key)); + let mut outcomes = Vec::with_capacity(candidates.len()); + for candidate in candidates { + let outcome = apply_candidate_action(tx, request.workspace_id, candidate) + .await + .map_err(fail)?; + outcomes.push(outcome); + } + + let applied_at = db_precision_utc(*request.applied_at); + let report = build_applied_report(request, &plan, outcomes, applied_at); + persist_application_row(tx, request, &stored_digest, &report, applied_at).await?; + Ok((false, report)) +} + +impl PostgresRegistry { + /// Atomically applies a stored, immutable import plan. + /// + /// * Repays of a committed `(workspace, idempotency_key, plan_digest)` + /// triple return the stored immutable report — byte-for-byte, even + /// after the plan and its job have been cleaned up — with + /// `result.replayed == true`. A different digest under an already-used + /// key is a typed conflict. + /// * A fresh apply revalidates the plan digest/schema/selection + /// closure, the anchor job, the verified source/dependency snapshots + /// and every referenced auth profile + secret, then materializes the + /// create/rename/skip/new_version mutations — all inside one + /// transaction with the application report insert. The successful + /// apply leaves the job pending and the sources attached so other + /// stored plans remain viable until the TTL expires. + pub async fn apply_import_plan( + &self, + request: &ApplyImportPlanRequest<'_>, + ) -> Result { + validate_apply_request(request)?; + + // Replay fast path: committed report evidence is self-contained and + // outlives the plan and its job, so a matching key+digest returns it + // before the plan/job lookups would see the cleaned-up rows. + if let Some(application) = self + .get_import_plan_application(request.workspace_id, request.idempotency_key) + .await? + { + if application.plan_digest == request.plan_digest { + return Ok(application.result); + } + return Err(RegistryError::ImportPlanKeyDigestConflict { + idempotency_key: request.idempotency_key.to_owned(), + }); + } + + let mut transaction = self.pool.begin().await?; + let outcome = apply_import_plan_transaction(&mut transaction, request).await; + match outcome { + Ok((replayed, report)) => { + transaction.commit().await?; + Ok(ApplyImportPlanResult { replayed, report }) + } + Err(ApplyTxFailure::KeyConflict) => { + // The application row appeared while we were committing: a + // concurrent identical apply committed first. Roll back our + // mutations and converge on the stored report (or, if the + // digest differs, the typed conflict). + transaction.rollback().await?; + let Some(application) = self + .get_import_plan_application(request.workspace_id, request.idempotency_key) + .await? + else { + return Err(RegistryError::ImportPlanKeyDigestConflict { + idempotency_key: request.idempotency_key.to_owned(), + }); + }; + if application.plan_digest == request.plan_digest { + return Ok(application.result); + } + Err(RegistryError::ImportPlanKeyDigestConflict { + idempotency_key: request.idempotency_key.to_owned(), + }) + } + Err(ApplyTxFailure::Registry(error)) => { + transaction.rollback().await?; + Err(error) + } + } + } + + /// Reads the stored application evidence for a scoped idempotency key + /// (read-only, workspace-scoped): the committed plan digest plus the + /// immutable report wrapped as a replayed result. Returns `None` when + /// the key was never committed in this workspace. This is the + /// replay-first entry point for Admin: a matching digest returns the + /// stored report even after the plan and its job have been cleaned + /// up; a different digest must be surfaced as a changed plan conflict + /// (`ImportPlanKeyDigestConflict`). + pub async fn get_import_plan_application( + &self, + workspace_id: &WorkspaceId, + idempotency_key: &str, + ) -> Result, RegistryError> { + if !(1..=MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES).contains(&idempotency_key.len()) { + return Err(RegistryError::InvalidImportPlan { + field: "idempotency_key", + }); + } + let row = sqlx::query( + "select plan_digest, report + from import_plan_applications + where workspace_id = $1 and idempotency_key = $2", + ) + .bind(workspace_id.as_str()) + .bind(idempotency_key) + .fetch_optional(&self.pool) + .await?; + match row { + Some(row) => { + let report: ImportPlanApplyReport = + serde_json::from_value(row.try_get::("report")?)?; + Ok(Some(ImportPlanApplication { + plan_digest: row.try_get("plan_digest")?, + result: ApplyImportPlanResult { + replayed: true, + report, + }, + })) + } + None => Ok(None), + } + } +} diff --git a/crates/crank-registry/src/postgres/import_job/plan_apply/material.rs b/crates/crank-registry/src/postgres/import_job/plan_apply/material.rs new file mode 100644 index 0000000..6ce34d8 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/material.rs @@ -0,0 +1,524 @@ +use super::*; +// --------------------------------------------------------------------------- +// Typed parse of the immutable plan material (import-plan-v1). The registry +// never trusts the caller: the stored payload is re-derived into a typed +// shape and every relationship it claims for itself is re-checked. +// --------------------------------------------------------------------------- + +#[derive(Deserialize)] +pub(super) struct StoredPlan { + pub(crate) schema_version: String, + pub(crate) plan_reference: String, + pub(crate) job_id: String, + pub(crate) source: StoredPlanSource, + #[serde(default)] + pub(crate) dependencies: Vec, + pub(crate) selection: StoredPlanSelection, + pub(crate) counts: StoredPlanCounts, + pub(crate) candidates: Vec, +} + +#[derive(Clone, Deserialize)] +pub(super) struct StoredPlanSource { + pub(crate) source_id: String, + pub(crate) digest: String, +} + +#[derive(Deserialize)] +pub(super) struct StoredPlanSelection { + pub(crate) mode: String, + pub(crate) all: Vec, + pub(crate) selected: Vec, + pub(crate) excluded: Vec, +} + +#[derive(Deserialize, Default)] +pub(super) struct StoredPlanCounts { + pub(crate) selected: u32, + pub(crate) excluded: u32, + pub(crate) actionable: u32, + pub(crate) blocked: u32, +} + +#[derive(Deserialize)] +pub(super) struct StoredPlanCandidate { + pub(crate) key: String, + pub(crate) name: String, + pub(crate) state: String, + pub(crate) decision: StoredPlanDecision, + #[serde(default)] + pub(crate) reason: Option, + #[serde(default)] + pub(crate) target: Option, + #[serde(default)] + pub(crate) operation: Option, + /// Plan-time auth profile evidence. Required exactly when an actionable + /// candidate's operation carries an `auth_profile_ref`: it lets apply + /// reject a profile that drifted since confirmation (config change, + /// secret rotation/revocation) instead of silently using the new one. + /// Never carries credential bytes — only profile metadata and secret + /// ids/status/versions. + #[serde(default)] + pub(crate) auth_snapshot: Option, +} + +/// Plan-time snapshot of one auth profile's redacted metadata. +/// +/// Field names are fixed by the Admin/registry contract: `profile_id`, +/// `name`, `kind`, `config` (the stored `config_json` object), `updated_at` +/// (RFC3339) and the sorted-by-id `secrets` list. +#[derive(Debug, Deserialize, PartialEq)] +pub(super) struct StoredAuthSnapshot { + pub(crate) profile_id: String, + pub(crate) name: String, + pub(crate) kind: String, + pub(crate) config: Value, + #[serde(with = "time::serde::rfc3339")] + pub(crate) updated_at: OffsetDateTime, + pub(crate) secrets: Vec, +} + +#[derive(Debug, Deserialize, PartialEq)] +pub(super) struct StoredAuthSecretSnapshot { + pub(crate) id: String, + pub(crate) status: String, + pub(crate) current_version: i32, + #[serde(with = "time::serde::rfc3339")] + pub(crate) updated_at: OffsetDateTime, +} + +#[derive(Deserialize)] +pub(super) struct StoredPlanDecision { + pub(crate) action: String, + #[serde(default)] + pub(crate) target_operation_id: Option, + #[serde(default)] + pub(crate) confirmed_name: Option, + #[serde(default)] + pub(crate) auth_profile_id: Option, +} + +#[derive(Clone, Deserialize)] +pub(super) struct StoredPlanTarget { + pub(crate) operation_id: String, + pub(crate) name: String, + pub(crate) expected_state: StoredPlanExpectedState, +} + +#[derive(Clone, Deserialize)] +pub(super) struct StoredPlanExpectedState { + pub(crate) status: OperationStatus, + pub(crate) current_draft_version: u32, + pub(crate) latest_published_version: Option, +} + +fn digest_is_hex64(value: &str) -> bool { + value.len() == 64 + && value + .bytes() + .all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) +} + +pub(super) fn validate_apply_request( + request: &ApplyImportPlanRequest, +) -> Result<(), RegistryError> { + validate_plan_id(request.plan_id)?; + if !digest_is_hex64(request.plan_digest) { + return Err(RegistryError::InvalidImportPlan { + field: "plan_digest", + }); + } + if !(1..=MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES).contains(&request.idempotency_key.len()) { + return Err(RegistryError::InvalidImportPlan { + field: "idempotency_key", + }); + } + Ok(()) +} + +pub(super) fn read_and_validate_plan_material( + payload: &Value, + request: &ApplyImportPlanRequest<'_>, + stored_digest: &str, + stored_job_id: &str, +) -> Result { + let plan_id = request.plan_id.as_str().to_owned(); + let corrupt = || RegistryError::ImportPlanPayloadCorrupt { + plan_id: plan_id.clone(), + }; + let stale = || RegistryError::ImportPlanFingerprintStale { + plan_id: plan_id.clone(), + }; + + if request.plan_digest != stored_digest + || super::super::plan_digest_from_payload(payload).map_err(|_| corrupt())? != stored_digest + { + return Err(corrupt()); + } + if let Some(server_url) = payload.get("server_url").and_then(Value::as_str) + && (server_url.is_empty() || server_url.len() > MAX_IMPORT_PLAN_SERVER_URL_BYTES) + { + return Err(corrupt()); + } + if payload + .pointer("/selection/selected") + .and_then(Value::as_array) + .is_some_and(|items| items.len() > MAX_IMPORT_PLAN_KEYS) + || payload + .pointer("/selection/all") + .and_then(Value::as_array) + .is_some_and(|items| items.len() > MAX_IMPORT_PLAN_KEYS * 2) + || payload + .pointer("/selection/excluded") + .and_then(Value::as_array) + .is_some_and(|items| items.len() > MAX_IMPORT_PLAN_KEYS) + { + return Err(corrupt()); + } + let source_digest = payload + .pointer("/source/digest") + .and_then(Value::as_str) + .ok_or_else(corrupt)?; + if ArtifactRef::parse(source_digest).is_err() { + return Err(corrupt()); + } + for item in payload + .pointer("/dependencies") + .and_then(Value::as_array) + .into_iter() + .flatten() + { + let source_id = item + .get("source_id") + .and_then(Value::as_str) + .ok_or_else(corrupt)?; + if !(1..=132).contains(&source_id.len()) { + return Err(corrupt()); + } + let digest = item + .get("digest") + .and_then(Value::as_str) + .ok_or_else(corrupt)?; + if ArtifactRef::parse(digest).is_err() { + return Err(corrupt()); + } + } + + let plan: StoredPlan = serde_json::from_value(payload.clone()).map_err(|_| corrupt())?; + + // The typed material must agree with the request it is being applied for. + if plan.schema_version.as_str() != PLAN_PAYLOAD_SCHEMA_VERSION + || plan.plan_reference.as_str() != request.plan_id.as_str() + || plan.job_id != stored_job_id + { + return Err(corrupt()); + } + + // Selection closure. + if !matches!(plan.selection.mode.as_str(), "full" | "partial") { + return Err(corrupt()); + } + let no_duplicates = |items: &[String]| { + items.iter().all(|item| !item.is_empty()) && { + let mut unique = BTreeSet::new(); + items.iter().all(|item| unique.insert(item)) + } + }; + if !no_duplicates(&plan.selection.all) + || !no_duplicates(&plan.selection.selected) + || !no_duplicates(&plan.selection.excluded) + { + return Err(corrupt()); + } + if plan.selection.selected.is_empty() || plan.candidates.is_empty() { + return Err(corrupt()); + } + let selected: BTreeSet<&String> = plan.selection.selected.iter().collect(); + let excluded: BTreeSet<&String> = plan.selection.excluded.iter().collect(); + let all: BTreeSet<&String> = plan.selection.all.iter().collect(); + let mut union = selected.clone(); + union.extend(excluded.iter()); + if union != all || selected.iter().any(|key| excluded.contains(key)) { + return Err(corrupt()); + } + if plan.selection.mode == "full" && (!excluded.is_empty() || selected != all) { + return Err(corrupt()); + } + let candidate_keys: BTreeSet<&String> = plan + .candidates + .iter() + .map(|candidate| &candidate.key) + .collect(); + if candidate_keys != selected { + return Err(corrupt()); + } + let actionable = plan + .candidates + .iter() + .filter(|c| c.state == "actionable") + .count(); + let blocked = plan + .candidates + .iter() + .filter(|c| c.state == "blocked") + .count(); + if plan.counts.selected as usize != plan.selection.selected.len() + || plan.counts.excluded as usize != plan.selection.excluded.len() + || plan.counts.actionable != actionable as u32 + || plan.counts.blocked != blocked as u32 + || plan.counts.actionable + plan.counts.blocked != plan.counts.selected + { + return Err(corrupt()); + } + + // Source bounds (matching the legacy envelope bounds). The digests are + // canonical `sha256:` artifact refs exactly as the job envelopes store them. + if ArtifactRef::parse(&plan.source.digest).is_err() { + return Err(corrupt()); + } + let mut dependency_ids = BTreeSet::new(); + if plan.dependencies.len() > MAX_IMPORT_JOB_DOCUMENTS { + return Err(corrupt()); + } + for dependency in &plan.dependencies { + if !(1..=132).contains(&dependency.source_id.len()) + || ArtifactRef::parse(&dependency.digest).is_err() + || !dependency_ids.insert(dependency.source_id.clone()) + { + return Err(corrupt()); + } + } + + // Per-candidate decision shape. + for candidate in &plan.candidates { + if !(1..=MAX_IMPORT_PLAN_KEY_BYTES).contains(&candidate.key.len()) + || !(1..=MAX_IMPORT_PLAN_NAME_BYTES).contains(&candidate.name.len()) + || !matches!(candidate.state.as_str(), "actionable" | "blocked") + { + return Err(corrupt()); + } + if let Some(reason) = &candidate.reason + && (reason.is_empty() || reason.len() > MAX_IMPORT_PLAN_REASON_BYTES) + { + return Err(corrupt()); + } + let decision = &candidate.decision; + if !matches!( + decision.action.as_str(), + "create" | "skip" | "rename" | "new_version" + ) { + return Err(corrupt()); + } + if decision + .target_operation_id + .as_deref() + .is_some_and(|id| id.is_empty() || id.len() > MAX_IMPORT_PLAN_ID_REF_BYTES) + || decision + .confirmed_name + .as_deref() + .is_some_and(|name| name.is_empty() || name.len() > MAX_IMPORT_PLAN_NAME_BYTES) + || decision + .auth_profile_id + .as_deref() + .is_some_and(|id| id.is_empty() || id.len() > MAX_IMPORT_PLAN_ID_REF_BYTES) + { + return Err(corrupt()); + } + let target = candidate.target.as_ref(); + if let Some(target) = target + && (!(1..=MAX_IMPORT_PLAN_ID_REF_BYTES).contains(&target.operation_id.len()) + || !(1..=MAX_IMPORT_PLAN_NAME_BYTES).contains(&target.name.len())) + { + return Err(corrupt()); + } + let actionable = candidate.state == "actionable"; + + // Auth-snapshot contract: an actionable candidate whose operation + // references a profile must carry its plan-time snapshot; nothing + // else may (a no-ref candidate carrying one, or a blocked candidate, + // is a corrupt plan; a missing one for an auth-bound operation means + // the plan predates the snapshot and must be re-previewed). + let auth_ref = candidate + .operation + .as_ref() + .and_then(|operation| operation.execution_config.auth_profile_ref.clone()); + match (&candidate.auth_snapshot, &auth_ref) { + (Some(snapshot), Some(id)) if snapshot.profile_id == id.as_str() => {} + (Some(snapshot), Some(id)) => { + debug_assert!(snapshot.profile_id != id.as_str()); + return Err(corrupt()); + } + (Some(_), None) => return Err(corrupt()), + (None, Some(id)) => { + if actionable { + return Err(RegistryError::ImportPlanAuthUnavailable { + auth_profile_id: id.as_str().to_owned(), + }); + } + } + (None, None) => {} + } + if let Some(snapshot) = &candidate.auth_snapshot { + if snapshot.profile_id.is_empty() + || snapshot.profile_id.len() > MAX_IMPORT_PLAN_ID_REF_BYTES + || snapshot.name.is_empty() + || snapshot.name.len() > MAX_IMPORT_PLAN_NAME_BYTES + || snapshot.kind.is_empty() + || snapshot.kind.len() > 64 + || snapshot.secrets.len() > MAX_IMPORT_JOB_DOCUMENTS + { + return Err(corrupt()); + } + let mut sorted = snapshot + .secrets + .iter() + .map(|secret| secret.id.as_str()) + .collect::>(); + for secret in &snapshot.secrets { + if secret.id.is_empty() + || secret.id.len() > MAX_IMPORT_PLAN_ID_REF_BYTES + || secret.status.is_empty() + || secret.status.len() > 16 + || secret.current_version < 0 + || !sorted.remove(secret.id.as_str()) + { + return Err(corrupt()); + } + } + if let Some(decision_auth) = &decision.auth_profile_id + && decision_auth != &snapshot.profile_id + { + return Err(corrupt()); + } + } + match decision.action.as_str() { + "create" => { + if target.is_some() + || decision.confirmed_name.is_some() + || decision.target_operation_id.is_some() + { + return Err(corrupt()); + } + // Blocked candidates are stored without a snapshot; the + // snapshot is only required for an actionable apply. + if actionable { + let operation = candidate.operation.as_ref().ok_or_else(corrupt)?; + validate_snapshot_operation(operation)?; + if operation.version != 1 { + return Err(corrupt()); + } + } + } + "rename" => { + if target.is_some() || decision.target_operation_id.is_some() { + return Err(corrupt()); + } + let confirmed = decision.confirmed_name.as_deref().ok_or_else(corrupt)?; + if actionable { + let operation = candidate.operation.as_ref().ok_or_else(corrupt)?; + validate_snapshot_operation(operation)?; + if operation.version != 1 || operation.name != confirmed { + return Err(corrupt()); + } + } + } + "skip" => { + let target = target.ok_or_else(corrupt)?; + if decision.target_operation_id.as_deref() != Some(target.operation_id.as_str()) + || decision.confirmed_name.is_some() + || candidate.operation.is_some() + { + return Err(corrupt()); + } + } + "new_version" => { + let target = target.ok_or_else(corrupt)?; + if decision.target_operation_id.as_deref() != Some(target.operation_id.as_str()) + || decision.confirmed_name.is_some() + || candidate.name != target.name + { + return Err(corrupt()); + } + if actionable { + let operation = candidate.operation.as_ref().ok_or_else(corrupt)?; + validate_snapshot_operation(operation)?; + if operation.id.as_str() != target.operation_id { + return Err(corrupt()); + } + let expected_next = target + .expected_state + .current_draft_version + .checked_add(1) + .ok_or_else(corrupt)?; + if operation.version != expected_next { + return Err(corrupt()); + } + } + } + _ => unreachable!("action is validated above"), + } + } + + // The service recomputes the preview fingerprint from the revalidated + // source/dependencies before any mutation; the immutable plan stores the + // fingerprint it was created from. A mismatch means the revalidation + // drifted from the plan material: re-preview, zero mutation. + let Some(plan_fingerprint) = payload.get("fingerprint") else { + return Err(stale()); + }; + if plan_fingerprint != request.verified_fingerprint { + return Err(stale()); + } + + Ok(plan) +} + +/// Shared snapshot invariants for planned Draft operations. +fn validate_snapshot_operation(operation: &RegistryOperation) -> Result<(), RegistryError> { + if operation.status != OperationStatus::Draft || operation.protocol != Protocol::Rest { + return Err(RegistryError::InvalidImportPlan { field: "operation" }); + } + Ok(()) +} + +/// Verifies the plan source/dependencies are exactly the anchor job's +/// verified envelopes (the plan material must agree with the job). +pub(super) fn verify_job_source_consistency( + job_payload: &Value, + plan: &StoredPlan, + plan_id: &str, +) -> Result<(), RegistryError> { + let stale = || RegistryError::ImportPlanFingerprintStale { + plan_id: plan_id.to_owned(), + }; + let job_source = source_from_payload(job_payload).map_err(|_| stale())?; + let Some(job_source) = job_source else { + return Err(stale()); + }; + if job_source.source_id.as_str() != plan.source.source_id + || job_source.digest.as_str() != plan.source.digest + { + return Err(stale()); + } + let job_dependencies = dependencies_from_payload(job_payload).map_err(|_| stale())?; + let mut job_set: Vec<(String, String)> = job_dependencies + .iter() + .map(|dependency| { + ( + dependency.source_id.as_str().to_owned(), + dependency.digest.as_str().to_owned(), + ) + }) + .collect(); + let mut plan_set: Vec<(String, String)> = plan + .dependencies + .iter() + .map(|dependency| (dependency.source_id.clone(), dependency.digest.clone())) + .collect(); + job_set.sort(); + plan_set.sort(); + if job_set != plan_set { + return Err(stale()); + } + Ok(()) +} diff --git a/crates/crank-registry/src/postgres/import_job/plan_apply/mutations.rs b/crates/crank-registry/src/postgres/import_job/plan_apply/mutations.rs new file mode 100644 index 0000000..24c7d80 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/mutations.rs @@ -0,0 +1,366 @@ +use super::material::{StoredPlan, StoredPlanCandidate}; +use super::*; +use crate::postgres::operation::matches_operation_name_conflict; +// --------------------------------------------------------------------------- +// Candidate mutations — exact names only, no runtime auto-rename. +// --------------------------------------------------------------------------- + +async fn operation_id_exists( + tx: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + operation_id: &OperationId, +) -> Result { + Ok(sqlx::query_scalar::<_, bool>( + "select exists(select 1 from operations where workspace_id = $1 and id = $2)", + ) + .bind(workspace_id.as_str()) + .bind(operation_id.as_str()) + .fetch_one(&mut **tx) + .await?) +} + +pub(super) async fn apply_candidate_action( + tx: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + candidate: &StoredPlanCandidate, +) -> Result { + match candidate.decision.action.as_str() { + "create" | "rename" => { + let operation = candidate + .operation + .clone() + .ok_or_else(|| RegistryError::InvalidImportPlan { field: "operation" })?; + if operation_id_exists(tx, workspace_id, &operation.id).await? { + return Err(RegistryError::OperationAlreadyExists { + operation_id: operation.id.as_str().to_owned(), + }); + } + if operation_name_exists(tx, workspace_id, &operation.name).await? { + return Err(RegistryError::ImportPlanNameConflict { + name: operation.name.clone(), + }); + } + match insert_operation_rows(tx, workspace_id, &operation, None).await { + Err(error) if matches_operation_name_conflict(&error) => { + return Err(RegistryError::ImportPlanNameConflict { + name: operation.name, + }); + } + other => other?, + } + Ok(ImportPlanCandidateReport { + key: candidate.key.clone(), + name: candidate.name.clone(), + outcome: if candidate.decision.action == "create" { + ImportPlanCandidateOutcome::Created + } else { + ImportPlanCandidateOutcome::Renamed + }, + operation_id: Some(operation.id.as_str().to_owned()), + version: Some(operation.version), + reason: None, + }) + } + "skip" => { + let target = candidate + .target + .clone() + .ok_or_else(|| RegistryError::InvalidImportPlan { field: "target" })?; + let stale = || RegistryError::ImportPlanTargetStale { + operation_id: target.operation_id.clone(), + }; + let row = sqlx::query( + "select name, status, current_draft_version, latest_published_version + from operations + where workspace_id = $1 and id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(&target.operation_id) + .fetch_optional(&mut **tx) + .await? + .ok_or_else(stale)?; + let row_name = row.try_get::("name")?; + let status: OperationStatus = deserialize_enum_text(row.try_get("status")?, "status")?; + let current = from_db_version( + row.try_get::("current_draft_version")?, + "current_draft_version", + )?; + let latest = row + .try_get::, _>("latest_published_version")? + .map(|value| from_db_version(value, "latest_published_version")) + .transpose()?; + if row_name != target.name + || status != target.expected_state.status + || current != target.expected_state.current_draft_version + || latest != target.expected_state.latest_published_version + { + return Err(stale()); + } + Ok(ImportPlanCandidateReport { + key: candidate.key.clone(), + name: candidate.name.clone(), + outcome: ImportPlanCandidateOutcome::Skipped, + operation_id: Some(target.operation_id), + version: None, + reason: None, + }) + } + "new_version" => { + let target = candidate + .target + .clone() + .ok_or_else(|| RegistryError::InvalidImportPlan { field: "target" })?; + let operation = candidate + .operation + .clone() + .ok_or_else(|| RegistryError::InvalidImportPlan { field: "operation" })?; + let stale = || RegistryError::ImportPlanTargetStale { + operation_id: target.operation_id.clone(), + }; + let row = sqlx::query( + "select name, status, protocol, current_draft_version, latest_published_version + from operations + where workspace_id = $1 and id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(&target.operation_id) + .fetch_optional(&mut **tx) + .await? + .ok_or_else(stale)?; + let row_name = row.try_get::("name")?; + let status: OperationStatus = deserialize_enum_text(row.try_get("status")?, "status")?; + let protocol: Protocol = deserialize_enum_text(row.try_get("protocol")?, "protocol")?; + let current = from_db_version( + row.try_get::("current_draft_version")?, + "current_draft_version", + )?; + let latest = row + .try_get::, _>("latest_published_version")? + .map(|value| from_db_version(value, "latest_published_version")) + .transpose()?; + if row_name != target.name + || status != target.expected_state.status + || current != target.expected_state.current_draft_version + || latest != target.expected_state.latest_published_version + || status == OperationStatus::Archived + || protocol != Protocol::Rest + || operation.name != target.name + || operation.id.as_str() != target.operation_id + || operation.status != OperationStatus::Draft + || operation.version != current.checked_add(1).ok_or_else(stale)? + { + return Err(stale()); + } + match insert_version_row(tx, &operation, None, None).await { + Err(RegistryError::Storage(sqlx::Error::Database(db_error))) + if db_error.code().as_deref() == Some("23505") => + { + return Err(stale()); + } + other => other?, + } + let updated = sqlx::query( + "update operations + set status = $1, current_draft_version = $2, updated_at = $3::timestamptz + where workspace_id = $4 and id = $5", + ) + .bind(serialize_enum_text(&OperationStatus::Draft, "status")?) + .bind(to_db_version(operation.version)) + .bind(operation.updated_at) + .bind(workspace_id.as_str()) + .bind(&target.operation_id) + .execute(&mut **tx) + .await?; + if updated.rows_affected() != 1 { + return Err(stale()); + } + Ok(ImportPlanCandidateReport { + key: candidate.key.clone(), + name: candidate.name.clone(), + outcome: ImportPlanCandidateOutcome::Versioned, + operation_id: Some(target.operation_id), + version: Some(operation.version), + reason: None, + }) + } + _ => Err(RegistryError::InvalidImportPlan { field: "decision" }), + } +} + +// --------------------------------------------------------------------------- +// Report builders and application-record persistence. +// --------------------------------------------------------------------------- + +fn excluded_outcomes(plan: &StoredPlan) -> Vec { + plan.selection + .excluded + .iter() + .map(|key| ImportPlanCandidateReport { + key: key.clone(), + name: String::new(), + outcome: ImportPlanCandidateOutcome::Excluded, + operation_id: None, + version: None, + reason: Some("not_selected".to_owned()), + }) + .collect() +} + +pub(super) fn build_applied_report( + request: &ApplyImportPlanRequest<'_>, + plan: &StoredPlan, + outcomes: Vec, + applied_at: OffsetDateTime, +) -> ImportPlanApplyReport { + let mut outcomes = outcomes; + outcomes.extend(excluded_outcomes(plan)); + let count_of = |outcome: ImportPlanCandidateOutcome| { + outcomes + .iter() + .filter(|reported| reported.outcome == outcome) + .count() as u32 + }; + let counts = ImportPlanApplyCounts { + created: count_of(ImportPlanCandidateOutcome::Created), + versioned: count_of(ImportPlanCandidateOutcome::Versioned), + renamed: count_of(ImportPlanCandidateOutcome::Renamed), + skipped: count_of(ImportPlanCandidateOutcome::Skipped), + excluded: count_of(ImportPlanCandidateOutcome::Excluded), + rejected: 0, + }; + ImportPlanApplyReport { + schema_version: APPLY_REPORT_SCHEMA_VERSION.to_owned(), + status: ImportPlanApplyStatus::Applied, + plan_id: request.plan_id.as_str().to_owned(), + plan_digest: request.plan_digest.to_owned(), + job_id: plan.job_id.clone(), + idempotency_key: request.idempotency_key.to_owned(), + source_id: plan.source.source_id.clone(), + source_digest: plan.source.digest.clone(), + dependencies: plan + .dependencies + .iter() + .map(|dependency| ImportPlanDependencyRef { + source_id: dependency.source_id.clone(), + digest: dependency.digest.clone(), + }) + .collect(), + counts, + outcomes, + summary: format!( + "План применён: создано {}, переименовано {}, новых версий {}, пропущено {}, исключено {}.", + counts.created, counts.renamed, counts.versioned, counts.skipped, counts.excluded + ), + recovery: "none".to_owned(), + applied_at, + } +} + +pub(super) fn build_rejected_report( + request: &ApplyImportPlanRequest<'_>, + plan: &StoredPlan, + applied_at: OffsetDateTime, +) -> ImportPlanApplyReport { + let mut outcomes: Vec = plan + .candidates + .iter() + .map(|candidate| ImportPlanCandidateReport { + key: candidate.key.clone(), + name: candidate.name.clone(), + outcome: ImportPlanCandidateOutcome::Rejected, + operation_id: candidate.decision.target_operation_id.clone(), + version: None, + reason: if candidate.state == "blocked" { + Some( + candidate + .reason + .clone() + .unwrap_or_else(|| "import_candidate_blocked".to_owned()), + ) + } else { + Some("blocked_by_selection".to_owned()) + }, + }) + .collect(); + outcomes.extend(excluded_outcomes(plan)); + let counts = ImportPlanApplyCounts { + created: 0, + versioned: 0, + renamed: 0, + skipped: 0, + excluded: plan.selection.excluded.len() as u32, + rejected: plan.candidates.len() as u32, + }; + ImportPlanApplyReport { + schema_version: APPLY_REPORT_SCHEMA_VERSION.to_owned(), + status: ImportPlanApplyStatus::Rejected, + plan_id: request.plan_id.as_str().to_owned(), + plan_digest: request.plan_digest.to_owned(), + job_id: plan.job_id.clone(), + idempotency_key: request.idempotency_key.to_owned(), + source_id: plan.source.source_id.clone(), + source_digest: plan.source.digest.clone(), + dependencies: plan + .dependencies + .iter() + .map(|dependency| ImportPlanDependencyRef { + source_id: dependency.source_id.clone(), + digest: dependency.digest.clone(), + }) + .collect(), + counts, + outcomes, + summary: format!( + "Применение отклонено: {} кандидат(ов) требуют подтверждения, {} кандидат(ов) исключено. Создайте новый preview.", + counts.rejected, counts.excluded + ), + recovery: "repreview_required".to_owned(), + applied_at, + } +} + +pub(super) async fn persist_application_row( + tx: &mut Transaction<'_, Postgres>, + request: &ApplyImportPlanRequest<'_>, + stored_digest: &str, + report: &ImportPlanApplyReport, + applied_at: OffsetDateTime, +) -> Result<(), ApplyTxFailure> { + let fail = |error: RegistryError| ApplyTxFailure::Registry(error); + let report_value = serde_json::to_value(report) + .map_err(|error| ApplyTxFailure::Registry(RegistryError::Serialization(error)))?; + if serde_json::to_vec(&report_value) + .map_err(|error| ApplyTxFailure::Registry(RegistryError::Serialization(error)))? + .len() + > MAX_IMPORT_PLAN_REPORT_BYTES + { + return Err(fail(RegistryError::ImportPlanPayloadCorrupt { + plan_id: request.plan_id.as_str().to_owned(), + })); + } + let result = sqlx::query( + "insert into import_plan_applications ( + workspace_id, idempotency_key, plan_id, plan_digest, report, created_at + ) values ($1, $2, $3, $4, $5, $6::timestamptz)", + ) + .bind(request.workspace_id.as_str()) + .bind(request.idempotency_key) + .bind(request.plan_id.as_str()) + .bind(stored_digest) + .bind(report_value) + .bind(applied_at) + .execute(&mut **tx) + .await; + match result { + Ok(_) => Ok(()), + Err(sqlx::Error::Database(db_error)) + if db_error.code().as_deref() == Some("23505") + && db_error.constraint() == Some("import_plan_applications_pkey") => + { + Err(ApplyTxFailure::KeyConflict) + } + Err(error) => Err(fail(RegistryError::Storage(error))), + } +} diff --git a/crates/crank-registry/src/postgres/import_job/plan_apply/verify.rs b/crates/crank-registry/src/postgres/import_job/plan_apply/verify.rs new file mode 100644 index 0000000..fbdcb2e --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/verify.rs @@ -0,0 +1,265 @@ +use super::*; +/// Locks one verified source envelope and requires it to still be active +/// with the exact recorded digest. No network fetch is performed. +pub(super) async fn lock_and_verify_envelope( + tx: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + envelope: &ImportJobSourceEnvelope, +) -> Result<(), RegistryError> { + let row = sqlx::query( + "select s.lifecycle, b.artifact_ref + from artifact_sources s + join artifact_blobs b on b.digest = s.blob_digest + where s.workspace_id = $1 and s.source_id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(envelope.source_id.as_str()) + .fetch_optional(&mut **tx) + .await?; + let current = row + .as_ref() + .map(|row| { + let lifecycle = row.try_get::("lifecycle").ok(); + let artifact_ref = row.try_get::("artifact_ref").ok(); + lifecycle.as_deref() == Some("active") + && artifact_ref.as_deref() == Some(envelope.digest.as_str()) + }) + .unwrap_or(false); + if !current { + return Err(RegistryError::SourceUnavailable); + } + Ok(()) +} + +fn auth_profile_from_row( + row: &PgRow, + workspace_id: &WorkspaceId, +) -> Result { + build_auth_profile( + row.try_get::("id")?, + workspace_id.as_str().to_owned(), + row.try_get::("name")?, + row.try_get::("kind")?, + row.try_get::("config_json")?, + row.try_get::("created_at")?, + row.try_get::("updated_at")?, + ) +} + +/// Checks every referenced auth profile and secret the same way the +/// operation creation path does, but under the apply transaction's locks: +/// +/// 1. read each referenced profile (absent, or name/kind/config/updated_at +/// drift from its plan-time snapshot — a stale conflict); +/// 2. lock the union of referenced secret rows in sorted order (the same +/// order as `save_auth_profile`) and require active + a stored current +/// version, plus exact `{id, status, current_version, updated_at}` +/// agreement with the sorted plan-time secret list; +/// 3. lock the profile rows in sorted order and require the secret set from +/// step 1 still equals the live secret set and name/kind/config still +/// agree. +/// +/// Only actionable candidates with an operation `auth_profile_ref` take +/// part; the plan material was already checked to carry a snapshot for +/// exactly those, so a `None` here is unreachable in the checked flow. +pub(super) async fn lock_and_verify_auth( + tx: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + candidates: &[StoredPlanCandidate], +) -> Result<(), RegistryError> { + use material::StoredAuthSnapshot; + + let unavailable = |auth_profile_id: &str| RegistryError::ImportPlanAuthUnavailable { + auth_profile_id: auth_profile_id.to_owned(), + }; + + // Step 0: the referenced (profile_id, snapshot) pairs in sorted order. + // Every candidate binding one profile must carry the same plan-time + // snapshot; a disagreement means the profile drifted during CreatePlan + // and the whole apply must be re-previewed before any mutation. + let mut snapshots: BTreeMap = BTreeMap::new(); + { + use std::collections::btree_map::Entry; + for candidate in candidates { + if candidate.state == "blocked" { + continue; + } + let Some(operation) = candidate.operation.as_ref() else { + continue; + }; + let Some(ref_id) = operation.execution_config.auth_profile_ref.as_ref() else { + continue; + }; + let snapshot = candidate + .auth_snapshot + .as_ref() + .ok_or_else(|| unavailable(ref_id.as_str()))?; + match snapshots.entry(ref_id.as_str().to_owned()) { + Entry::Vacant(vacant) => { + vacant.insert(snapshot); + } + Entry::Occupied(occupied) => { + let id = occupied.key().clone(); + if *occupied.get() != snapshot { + return Err(unavailable(&id)); + } + } + } + } + } + let collected: Vec<(String, &StoredAuthSnapshot)> = snapshots.into_iter().collect(); + if collected.is_empty() { + return Ok(()); + } + + // Step 1: profile reads with plan-time snapshot comparison. + let mut secret_owner: BTreeMap = BTreeMap::new(); + let mut read_secret_sets: BTreeMap> = BTreeMap::new(); + for (profile_id, snapshot) in &collected { + let row = sqlx::query( + "select id, name, kind, config_json, created_at, updated_at + from auth_profiles + where workspace_id = $1 and id = $2", + ) + .bind(workspace_id.as_str()) + .bind(profile_id) + .fetch_optional(&mut **tx) + .await? + .ok_or_else(|| unavailable(profile_id))?; + let profile = + auth_profile_from_row(&row, workspace_id).map_err(|_| unavailable(profile_id))?; + if profile.kind != profile.config.kind() { + return Err(unavailable(profile_id)); + } + // Drift since the plan was confirmed (config, name, kind or + // updated_at) means the re-preview contract: typed conflict, no + // mutation. + if row.try_get::("name")? != snapshot.name + || row.try_get::("kind")? != snapshot.kind + || row.try_get::("updated_at")? != snapshot.updated_at + || row.try_get::("config_json")? != snapshot.config + { + return Err(unavailable(profile_id)); + } + let mut secret_ids = profile + .config + .secret_ids() + .into_iter() + .map(|secret_id| secret_id.as_str().to_owned()) + .collect::>(); + secret_ids.sort(); + secret_ids.dedup(); + for secret_id in &secret_ids { + secret_owner + .entry(secret_id.clone()) + .or_insert_with(|| profile_id.clone()); + } + read_secret_sets.insert(profile_id.clone(), secret_ids); + } + + // Step 2: sorted secret locks (same order as `save_auth_profile`), + // active-state checks and exact plan-time field agreement. + let secret_ids: Vec = secret_owner.keys().cloned().collect(); + let mut current_secrets: BTreeMap = BTreeMap::new(); + for secret_id in &secret_ids { + let owner = secret_owner.get(secret_id).cloned().unwrap_or_default(); + let row = sqlx::query( + "select status, current_version, updated_at + from secrets + where workspace_id = $1 and id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(secret_id) + .fetch_optional(&mut **tx) + .await? + .ok_or_else(|| unavailable(&owner))?; + let status: String = row.try_get("status")?; + if status != "active" { + return Err(unavailable(&owner)); + } + let current_version = row.try_get::("current_version")?; + let updated_at = row.try_get::("updated_at")?; + let version_present = sqlx::query_scalar::<_, bool>( + "select exists(select 1 from secret_versions where secret_id = $1 and version = $2)", + ) + .bind(secret_id) + .bind(current_version) + .fetch_one(&mut **tx) + .await?; + if !version_present { + return Err(unavailable(&owner)); + } + current_secrets.insert(secret_id.clone(), (status, current_version, updated_at)); + } + // Every profile's ordered live secret list must equal its plan-time + // snapshot list exactly (same ids, same order, same fields). + for (profile_id, snapshot) in &collected { + let live: Vec<(&str, String, i32, OffsetDateTime)> = read_secret_sets[profile_id] + .iter() + .map(|id| { + let (status, version, updated_at) = ¤t_secrets[id]; + (id.as_str(), status.clone(), *version, *updated_at) + }) + .collect(); + if live.len() != snapshot.secrets.len() { + return Err(unavailable(profile_id)); + } + for (live_id, live_status, live_version, live_updated_at) in live.iter() { + let Some(snapshot_secret) = + snapshot.secrets.iter().find(|secret| secret.id == *live_id) + else { + return Err(unavailable(profile_id)); + }; + if snapshot_secret.id != *live_id + || snapshot_secret.status != live_status.as_str() + || snapshot_secret.current_version != *live_version + || snapshot_secret.updated_at != *live_updated_at + { + return Err(unavailable(profile_id)); + } + } + // The sorted-by-id snapshot must contain the live set with no extras + // covered above; equal lengths plus every live id present is exact. + } + + // Step 3: sorted profile locks + stability re-check under the lock. + for (profile_id, snapshot) in &collected { + let row = sqlx::query( + "select id, name, kind, config_json, created_at, updated_at + from auth_profiles + where workspace_id = $1 and id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(profile_id) + .fetch_optional(&mut **tx) + .await? + .ok_or_else(|| unavailable(profile_id))?; + let profile = + auth_profile_from_row(&row, workspace_id).map_err(|_| unavailable(profile_id))?; + if profile.kind != profile.config.kind() { + return Err(unavailable(profile_id)); + } + if row.try_get::("name")? != snapshot.name + || row.try_get::("kind")? != snapshot.kind + || row.try_get::("updated_at")? != snapshot.updated_at + || row.try_get::("config_json")? != snapshot.config + { + return Err(unavailable(profile_id)); + } + let mut current_secret_ids = profile + .config + .secret_ids() + .into_iter() + .map(|secret_id| secret_id.as_str().to_owned()) + .collect::>(); + current_secret_ids.sort(); + current_secret_ids.dedup(); + if read_secret_sets.get(profile_id) != Some(¤t_secret_ids) { + return Err(unavailable(profile_id)); + } + } + Ok(()) +} diff --git a/crates/crank-registry/src/postgres/operation.rs b/crates/crank-registry/src/postgres/operation.rs index 8fe98be..b6d2561 100644 --- a/crates/crank-registry/src/postgres/operation.rs +++ b/crates/crank-registry/src/postgres/operation.rs @@ -989,7 +989,7 @@ fn verify_expected_state( Ok(()) } -fn matches_operation_name_conflict(error: &RegistryError) -> bool { +pub(super) fn matches_operation_name_conflict(error: &RegistryError) -> bool { let RegistryError::Storage(sqlx::Error::Database(error)) = error else { return false; }; diff --git a/crates/crank-registry/tests/integration.rs b/crates/crank-registry/tests/integration.rs index 29b0e0a..71abcf7 100644 --- a/crates/crank-registry/tests/integration.rs +++ b/crates/crank-registry/tests/integration.rs @@ -5,6 +5,7 @@ mod integration { mod artifact_sources; mod common; mod credential_touch; + mod import_plan; mod master_key_identity; mod migrations; mod observability; diff --git a/crates/crank-registry/tests/integration/import_plan.rs b/crates/crank-registry/tests/integration/import_plan.rs new file mode 100644 index 0000000..36da024 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan.rs @@ -0,0 +1,78 @@ +use crank_artifacts::ArtifactRef; +use crank_core::{Workspace, WorkspaceId, WorkspaceStatus}; +use crank_registry::{ + ArtifactSourceId, CreateImportJobRequest, CreateWorkspaceRequest, ImportJobId, ImportJobKind, + ImportJobSourceEnvelope, ImportJobStatus, +}; +use serde_json::json; +use time::OffsetDateTime; +use time::format_description::well_known::Rfc3339; + +mod apply; +mod create; +pub(super) fn parse_ts(value: &str) -> OffsetDateTime { + OffsetDateTime::parse(value, &Rfc3339).unwrap() +} + +pub(super) async fn create_workspace( + registry: &crank_registry::PostgresRegistry, + id: &str, +) -> WorkspaceId { + let workspace_id = WorkspaceId::new(id); + let created_at = parse_ts("2026-09-26T10:00:00Z"); + registry + .create_workspace(CreateWorkspaceRequest { + workspace: &Workspace { + id: workspace_id.clone(), + slug: id.replace('_', "-"), + display_name: id.to_owned(), + status: WorkspaceStatus::Active, + settings: json!({}), + created_at, + updated_at: created_at, + }, + }) + .await + .unwrap(); + workspace_id +} + +/// Creates a well-formed, pending, non-expired OpenAPI import job carrying a +/// verifiable source envelope, exactly as the legacy ingress path does. +pub(super) async fn create_pending_openapi_job( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, + job_id: &str, + now: OffsetDateTime, + job_expires_at: OffsetDateTime, +) -> ImportJobId { + let job = ImportJobId::new(job_id); + let source_id = ArtifactSourceId::new("src_openapi_test"); + let digest = ArtifactRef::from_digest_hex(&"ab".repeat(32)).unwrap(); + let preview_payload = json!({ + "source": { + "source_id": source_id.as_str(), + "digest": digest.as_str(), + }, + "preview": { "ops": ["a", "b"] } + }); + registry + .create_import_job(CreateImportJobRequest { + id: &job, + workspace_id, + kind: ImportJobKind::OpenApi, + source_format: "openapi", + source_version: Some("3.1.0"), + status: ImportJobStatus::Pending, + source: &ImportJobSourceEnvelope { + source_id, + digest: digest.clone(), + }, + preview_payload: &preview_payload, + created_at: &now, + expires_at: &job_expires_at, + }) + .await + .unwrap(); + job +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/auth.rs b/crates/crank-registry/tests/integration/import_plan/apply/auth.rs new file mode 100644 index 0000000..860d6bf --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/auth.rs @@ -0,0 +1,423 @@ +//! Auth profile/secret revalidation with plan-time snapshots: matching +//! snapshot applies; config drift, secret rotation/revocation and missing +//! snapshots conflict with zero mutation. + +use super::*; + +use crank_core::{ + ApiKeyHeaderAuthConfig, AuthConfig, AuthKind, AuthProfile, Secret, SecretId, SecretKind, + SecretStatus, +}; +use crank_registry::MASTER_KEY_CIPHER_CONTRACT; +use crank_registry::{ + CreateSecretRequest, MasterKeyIdentityCandidate, PostgresRegistry, SaveAuthProfileRequest, +}; +use time::format_description::well_known::Rfc3339; + +fn ts() -> OffsetDateTime { + OffsetDateTime::parse("2026-03-25T12:00:00Z", &Rfc3339).unwrap() +} + +/// Creates a master key epoch, one active secret and an ApiKeyHeader profile +/// for it; returns the secret id. +async fn create_auth_fixture(registry: &PostgresRegistry, workspace_id: &WorkspaceId) -> SecretId { + registry + .verify_or_register_master_key_identity(MasterKeyIdentityCandidate { + epoch: 1, + fingerprint: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + cipher_contract: MASTER_KEY_CIPHER_CONTRACT, + observed_at: &ts(), + }) + .await + .unwrap(); + let secret_id = SecretId::new("sec_apply"); + let secret = Secret { + id: secret_id.clone(), + workspace_id: workspace_id.clone(), + name: "Apply secret".to_owned(), + kind: SecretKind::Token, + status: SecretStatus::Active, + current_version: 1, + created_at: ts(), + updated_at: ts(), + last_used_at: None, + }; + registry + .create_secret(CreateSecretRequest { + secret: &secret, + ciphertext: "ciphertext", + key_version: "v1", + master_key_epoch: 1, + created_by: None, + }) + .await + .unwrap(); + let profile = AuthProfile { + id: "auth_apply".into(), + workspace_id: workspace_id.clone(), + name: "Apply key".to_owned(), + kind: AuthKind::ApiKeyHeader, + config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-Apply-Key".to_owned(), + secret_id: secret_id.clone(), + }), + created_at: ts(), + updated_at: ts(), + }; + registry + .save_auth_profile(SaveAuthProfileRequest { + workspace_id, + profile: &profile, + }) + .await + .unwrap(); + secret_id +} + +fn auth_operation(id: &str) -> RegistryOperation { + let mut operation = plan_operation(id); + operation.execution_config.auth_profile_ref = Some("auth_apply".into()); + operation +} + +/// The plan-time snapshot an Admin CreatePlan captures for this profile + +/// secret (metadata only, never credential bytes). +fn capture_snapshot(profile: &AuthProfile, secret: &crank_core::Secret) -> Value { + json!({ + "profile_id": profile.id.as_str(), + "name": profile.name, + "kind": profile.kind, + "config": profile.config, + "updated_at": profile.updated_at.format(&Rfc3339).unwrap(), + "secrets": [{ + "id": secret.id.as_str(), + "status": secret.status, + "current_version": secret.current_version, + "updated_at": secret.updated_at.format(&Rfc3339).unwrap() + }] + }) +} + +fn auth_plan_payload( + plan_id: &str, + job_id: &str, + fingerprint: &Value, + snapshot: Option, +) -> Value { + let operation = auth_operation("op_apply_auth"); + let mut candidate = json!({ + "key": "auth", + "name": operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": operation + }); + if let Some(snapshot) = snapshot { + candidate["auth_snapshot"] = snapshot; + } + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["auth"], + "selected": ["auth"], + "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [candidate] + }) +} + +async fn auth_base( + database: &TestDatabase, + workspace_tag: &str, +) -> ( + PostgresRegistry, + PgPool, + WorkspaceId, + ImportJobId, + Value, + Value, +) { + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, workspace_tag).await; + let job = create_pending_openapi_job( + ®istry, + &ws, + &format!("job_apply_auth_{workspace_tag}"), + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + let secret_id = create_auth_fixture(®istry, &ws).await; + let profile = registry + .get_auth_profile(&ws, &crank_core::AuthProfileId::new("auth_apply")) + .await + .unwrap() + .unwrap(); + let secret = registry + .get_secret(&ws, &secret_id) + .await + .unwrap() + .unwrap() + .secret; + let fp = fingerprint(); + ( + registry, + raw_pool, + ws, + job, + fp, + capture_snapshot(&profile, &secret), + ) +} + +#[tokio::test] +async fn apply_accepts_matching_auth_snapshot() { + let database = TestDatabase::new().await; + let (registry, _raw_pool, ws, job, fp, snapshot) = auth_base(&database, "a_ok").await; + + let payload = auth_plan_payload("plan_auth_ok", job.as_str(), &fp, Some(snapshot)); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_auth_ok", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_auth_ok", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + let result = registry.apply_import_plan(&request).await.unwrap(); + assert!(!result.replayed); + assert_eq!(result.report.status, ImportPlanApplyStatus::Applied); + assert_eq!(result.report.counts.created, 1); + let summary = registry + .get_operation_summary(&ws, &OperationId::new("op_apply_auth")) + .await + .unwrap() + .unwrap(); + assert_eq!(summary.current_draft_version, 1); + let stored = result.report; + drop(stored); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_rejects_auth_profile_config_drift() { + let database = TestDatabase::new().await; + let (registry, _raw_pool, ws, job, fp, snapshot) = auth_base(&database, "a_drift").await; + + let payload = auth_plan_payload("plan_auth_drift", job.as_str(), &fp, Some(snapshot)); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_auth_drift", &fp, payload).await; + + // After the plan was captured, the profile's config drifted. + let drifted = AuthProfile { + name: "Drifted key".to_owned(), + config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-Other-Key".to_owned(), + secret_id: SecretId::new("sec_apply"), + }), + updated_at: OffsetDateTime::now_utc(), + ..capture_profile_fixture(®istry, &ws).await + }; + registry + .save_auth_profile(SaveAuthProfileRequest { + workspace_id: &ws, + profile: &drifted, + }) + .await + .unwrap(); + + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_auth_drift", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + assert!( + matches!( + registry + .apply_import_plan(&request) + .await, + Err(RegistryError::ImportPlanAuthUnavailable { + auth_profile_id + }) if auth_profile_id == "auth_apply" + ), + "a config drift must be a typed re-preview conflict" + ); + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_apply_auth")) + .await + .unwrap() + .is_none(), + "zero mutation on auth drift" + ); + database.cleanup().await; +} + +async fn capture_profile_fixture(registry: &PostgresRegistry, ws: &WorkspaceId) -> AuthProfile { + registry + .get_auth_profile(ws, &crank_core::AuthProfileId::new("auth_apply")) + .await + .unwrap() + .unwrap() +} + +#[tokio::test] +async fn apply_rejects_secret_rotation_or_revocation() { + let database = TestDatabase::new().await; + let (registry, raw_pool, ws, job, fp, snapshot) = auth_base(&database, "a_rotate").await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + + // Rotated secret: same id, new current version. + let payload = auth_plan_payload("plan_auth_rot", job.as_str(), &fp, Some(snapshot.clone())); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_auth_rot", &fp, payload).await; + let now = OffsetDateTime::now_utc(); + registry + .rotate_secret(crank_registry::RotateSecretRequest { + workspace_id: &ws, + secret_id: &SecretId::new("sec_apply"), + ciphertext: "new-ciphertext", + key_version: "v2", + master_key_epoch: 1, + created_at: &now, + updated_at: &now, + created_by: None, + }) + .await + .unwrap(); + assert!( + matches!( + registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_auth_rot", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await, + Err(RegistryError::ImportPlanAuthUnavailable { .. }) + ), + "a rotated secret must be a typed re-preview conflict" + ); + + // Revoked/disabled secret: same id, changed status. + let payload = auth_plan_payload("plan_auth_dis", job.as_str(), &fp, Some(snapshot)); + let (dis_plan, dis_digest) = + store_plan(®istry, &ws, &job, "plan_auth_dis", &fp, payload).await; + sqlx::query("update secrets set status = 'disabled' where workspace_id = $1 and id = $2") + .bind(ws.as_str()) + .bind("sec_apply") + .execute(&raw_pool) + .await + .unwrap(); + assert!( + matches!( + registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &dis_plan, + plan_digest: &dis_digest, + idempotency_key: "idem_auth_dis", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await, + Err(RegistryError::ImportPlanAuthUnavailable { .. }) + ), + "a revoked secret must be a typed re-preview conflict" + ); + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_apply_auth")) + .await + .unwrap() + .is_none(), + "zero mutation on secret drift" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_rejects_auth_candidates_missing_or_mismatched_snapshot() { + let database = TestDatabase::new().await; + let (registry, _raw_pool, ws, job, fp, snapshot) = auth_base(&database, "a_missing").await; + + // Actionable + auth ref but NO snapshot -> unavailable (re-preview). + let payload = auth_plan_payload("plan_auth_mis", job.as_str(), &fp, None); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_auth_mis", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + assert!( + matches!( + registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_auth_mis", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await, + Err(RegistryError::ImportPlanAuthUnavailable { .. }) + ), + "an auth-bound candidate without a snapshot must re-preview" + ); + + // Snapshot on a candidate WITHOUT an auth ref -> corrupt plan. + let unauth_operation = plan_operation("op_apply_noauth"); + let payload = json!({ + "schema_version": "import-plan-v1", + "plan_reference": "plan_auth_corrupt", + "job_id": job.as_str(), + "fingerprint": fp, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", "all": ["na"], "selected": ["na"], "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "na", + "name": unauth_operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": unauth_operation, + "auth_snapshot": snapshot + }] + }); + let (c_plan, c_digest) = + store_plan(®istry, &ws, &job, "plan_auth_corrupt", &fp, payload).await; + assert!( + matches!( + registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &c_plan, + plan_digest: &c_digest, + idempotency_key: "idem_auth_corrupt", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await, + Err(RegistryError::ImportPlanPayloadCorrupt { .. }) + ), + "a no-ref candidate must not carry an auth snapshot" + ); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/blocked.rs b/crates/crank-registry/tests/integration/import_plan/apply/blocked.rs new file mode 100644 index 0000000..1f67c59 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/blocked.rs @@ -0,0 +1,122 @@ +//! Full blocked rejection (zero mutation) and the rejected outcome shape. + +use super::*; + +fn blocked_candidate_payload(plan_id: &str, job_id: &str, fingerprint: &Value) -> Value { + let operation = plan_operation("op_apply_blocked_ok"); + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["ready", "blocked"], + "selected": ["ready", "blocked"], + "excluded": [] + }, + "counts": { "selected": 2, "excluded": 0, "actionable": 1, "blocked": 1 }, + "candidates": [ + { + "key": "ready", + "name": operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": operation + }, + { + "key": "blocked", + "name": "import_candidate_blocked", + "state": "blocked", + "reason": "import_candidate_blocked", + "decision": { "action": "create" } + } + ] + }) +} + +#[tokio::test] +async fn apply_rejects_whole_plan_when_any_candidate_is_blocked() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_blocked").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_blocked", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let plan_id_str = "plan_apply_blocked"; + let payload = blocked_candidate_payload(plan_id_str, job.as_str(), &fp); + let (plan, digest) = store_plan(®istry, &ws, &job, plan_id_str, &fp, payload).await; + let applied_at = OffsetDateTime::now_utc(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_blocked", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + + // A single blocked candidate rejects the whole apply: zero mutation. + let result = registry.apply_import_plan(&request).await.unwrap(); + assert!( + !result.replayed, + "a rejected apply is a call outcome, not a replay" + ); + let report = &result.report; + assert_eq!(report.status, ImportPlanApplyStatus::Rejected); + assert_eq!( + report.counts.rejected, 2, + "both selected candidates are rejected" + ); + assert_eq!(report.counts.created, 0); + assert!( + report + .outcomes + .iter() + .all(|reported| reported.outcome == ImportPlanCandidateOutcome::Rejected) + ); + let blocked = report + .outcomes + .iter() + .find(|reported| reported.key == "blocked") + .expect("the blocked candidate must be explained in the report"); + assert_eq!(blocked.reason.as_deref(), Some("import_candidate_blocked")); + assert_eq!(report.recovery, "repreview_required"); + + // No operation materialized and no application row persisted. + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_apply_blocked_ok")) + .await + .unwrap() + .is_none(), + "a rejected apply must not create the actionable candidate" + ); + assert_eq!( + registry + .get_import_plan_application(&ws, "idem_blocked") + .await + .unwrap(), + None, + "a rejected apply must not persist application evidence" + ); + + // It can be retried under the same key and must reject again, not replay. + let retry = registry.apply_import_plan(&request).await.unwrap(); + assert!(!retry.replayed); + assert_eq!(retry.report.status, ImportPlanApplyStatus::Rejected); + + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/concurrency.rs b/crates/crank-registry/tests/integration/import_plan/apply/concurrency.rs new file mode 100644 index 0000000..2f0a882 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/concurrency.rs @@ -0,0 +1,143 @@ +//! Concurrent applies with the same scoped idempotency key: one coherent +//! commit, deterministic conflicts for changed digests. + +use super::*; + +#[tokio::test] +async fn concurrent_applies_with_same_key_and_digest_converge() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_race").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_race", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_race", &fp).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_race", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + + // Two identical applies race: both must converge on the same report and + // exactly one operation / application row may exist. + let (a, b) = tokio::join!( + registry.apply_import_plan(&request), + registry.apply_import_plan(&request) + ); + let a = a.unwrap(); + let b = b.unwrap(); + assert_eq!(a.report, b.report, "racers must see the same report"); + let fresh = (a.replayed, b.replayed); + assert!( + (fresh == (false, true)) || (fresh == (true, false)), + "exactly one racer produces the report, the other replays: {a:?} / {b:?}" + ); + assert_eq!( + sqlx::query_scalar::<_, i64>("select count(*) from operations") + .fetch_one(&raw_pool) + .await + .unwrap(), + 1, + "the race must materialize exactly one operation" + ); + assert_eq!( + sqlx::query_scalar::<_, i64>("select count(*) from import_plan_applications") + .fetch_one(&raw_pool) + .await + .unwrap(), + 1, + "the race must persist exactly one application row" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn concurrent_applies_same_key_changed_digest_conflicts_deterministically() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_race2").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_race2", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_race2", &fp).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let good = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_race2", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + let other_digest = "ef".repeat(32); + let bad = ApplyImportPlanRequest { + plan_digest: &other_digest, + ..good + }; + + // Same key, different plan digest: exactly one may succeed. + let (a, b) = tokio::join!( + registry.apply_import_plan(&good), + registry.apply_import_plan(&bad) + ); + assert!( + a.is_ok() ^ b.is_ok(), + "one winner, one typed conflict: a={a:?}, b={b:?}" + ); + for result in [&a, &b] { + if let Err(error) = result { + assert!( + matches!(error, RegistryError::ImportPlanKeyDigestConflict { .. }), + "the loser must get the typed digest conflict, got {error:?}" + ); + } + } + // The committed state is coherent: exactly one application row, and it + // binds the digest of the winner's successful apply. + let winner = match &a { + Ok(result) => result, + Err(_) => b.as_ref().unwrap(), + }; + assert_eq!( + sqlx::query_scalar::<_, i64>("select count(*) from import_plan_applications") + .fetch_one(&raw_pool) + .await + .unwrap(), + 1 + ); + assert!( + !winner.replayed, + "the winner of a changed-digest race must be a fresh apply" + ); + let stored = registry + .get_import_plan_application(&ws, "idem_race2") + .await + .unwrap() + .expect("the committed apply must be readable"); + assert_eq!(stored.plan_digest, winner.report.plan_digest); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/mod.rs b/crates/crank-registry/tests/integration/import_plan/apply/mod.rs new file mode 100644 index 0000000..48fcff0 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/mod.rs @@ -0,0 +1,249 @@ +//! Apply tests for the registry-only transactional `apply_import_plan`. +//! +//! Conventions follow the existing CreatePlan tests in `create.rs`: one +//! unique schema per test, registry API for domain objects and the raw pool +//! for storage-level fixtures (artifact sources). + +#![allow(dead_code)] +use super::super::common::{TestDatabase, test_operation}; +use super::{create_pending_openapi_job, create_workspace}; +pub use time::Duration; + +pub use crank_core::{OperationId, OperationStatus, WorkspaceId}; +pub use serde_json::{Value, json}; +pub use sqlx::PgPool; +pub use time::OffsetDateTime; + +pub use crank_registry::{ + ApplyImportPlanRequest, CreateImportPlanRequest, ImportJobId, ImportJobStatus, + ImportPlanApplyStatus, ImportPlanCandidateOutcome, ImportPlanId, RegistryError, + RegistryOperation, +}; + +mod auth; +mod blocked; +mod concurrency; +mod new_version; +mod partial; +mod replay_stale; +mod skip_rename; +/// Shared fixture source id — must match `create_pending_openapi_job`. +pub(crate) const SOURCE_ID: &str = "src_openapi_test"; +fn source_hex() -> String { + "ab".repeat(32) +} +fn source_ref() -> String { + format!("sha256:{}", source_hex()) +} + +/// Activates the source row the apply path revalidates under lock: the job +/// envelope must still be `active` with exactly the recorded artifact ref. +async fn activate_source(raw: &PgPool, workspace_id: &WorkspaceId, now: OffsetDateTime) { + let hex = source_hex(); + sqlx::query( + "insert into artifact_blobs (digest, artifact_ref, size_bytes, created_at, updated_at) + values ($1, 'sha256:' || $1, 16, $2, $2)", + ) + .bind(&hex) + .bind(now) + .execute(raw) + .await + .unwrap(); + sqlx::query( + "insert into artifact_sources ( + workspace_id, source_id, blob_digest, mime_type, sensitivity, lifecycle, + created_at, updated_at + ) values ($1, $2, $3, 'application/yaml', 'public', 'active', $4, $4)", + ) + .bind(workspace_id.as_str()) + .bind(SOURCE_ID) + .bind(&hex) + .bind(now) + .execute(raw) + .await + .unwrap(); +} + +/// A planned Draft operation without auth references: apply must not +/// require profile/secret rows for the basic create path. +fn plan_operation(id: &str) -> RegistryOperation { + let mut operation = test_operation(id, 1, OperationStatus::Draft); + operation.execution_config.auth_profile_ref = None; + operation +} + +/// Builds a valid `import-plan-v1` payload applying a single `create` +/// candidate over the shared verifiable source. +fn single_create_payload(plan_id: &str, job_id: &str, fingerprint: &Value) -> Value { + let operation = plan_operation("op_apply_basic"); + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["basic"], + "selected": ["basic"], + "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "basic", + "name": operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": operation + }] + }) +} + +async fn stored_plan( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, + job: &ImportJobId, + plan_id: &str, + fingerprint: &Value, +) -> (ImportPlanId, String) { + let payload = single_create_payload(plan_id, job.as_str(), fingerprint); + store_plan(registry, workspace_id, job, plan_id, fingerprint, payload).await +} + +/// Persists any well-formed `import-plan-v1` payload bound to the shared +/// verifiable source; returns the plan id and the registry-computed digest. +async fn store_plan( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, + job: &ImportJobId, + plan_id: &str, + _fingerprint: &Value, + payload: Value, +) -> (ImportPlanId, String) { + let plan = ImportPlanId::new(plan_id); + let now = OffsetDateTime::now_utc(); + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id, + job_id: job, + payload: &payload, + created_at: &now, + expires_at: &(now + Duration::minutes(30)), + }) + .await + .unwrap(); + (plan, created.digest) +} + +/// Basename of the workspace slug for slug-scoped registry reads. +#[allow(dead_code)] +fn workspace_slug(workspace_id: &WorkspaceId) -> String { + workspace_id.as_str().replace('_', "-") +} + +fn fingerprint() -> Value { + json!({"importer": "test", "digest": "fp-1"}) +} + +#[tokio::test] +async fn apply_creates_draft_operation_and_replays_committed_report() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_1").await; + + let job = + create_pending_openapi_job(®istry, &ws, "job_apply_1", now, now + Duration::hours(1)) + .await; + activate_source(&raw, &ws, now).await; + + let fp = fingerprint(); + let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_apply_1", &fp).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_apply_1", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + let result = registry.apply_import_plan(&request).await.unwrap(); + + // Fresh apply: the report is produced by this call, not a replay. + assert!( + !result.replayed, + "a first apply must never be marked replayed" + ); + let report = &result.report; + assert_eq!(report.status, ImportPlanApplyStatus::Applied); + assert_eq!(report.plan_id, plan.as_str()); + assert_eq!(report.job_id, job.as_str()); + assert_eq!(report.plan_digest, digest); + assert_eq!(report.idempotency_key, "idem_apply_1"); + assert_eq!(report.counts.created, 1); + assert_eq!(report.counts.versioned, 0); + assert_eq!(report.counts.renamed, 0); + assert_eq!(report.counts.skipped, 0); + assert_eq!(report.counts.excluded, 0); + assert_eq!(report.counts.rejected, 0); + assert_eq!(report.outcomes.len(), 1); + assert_eq!(report.outcomes[0].key, "basic"); + assert_eq!( + report.outcomes[0].outcome, + ImportPlanCandidateOutcome::Created + ); + assert_eq!( + report.outcomes[0].operation_id, + Some(OperationId::new("op_apply_basic").as_str().to_owned()) + ); + assert_eq!(report.outcomes[0].version, Some(1)); + assert_eq!(report.outcomes[0].reason, None); + assert_eq!(report.source_id, SOURCE_ID); + assert_eq!(report.source_digest, source_ref()); + assert_eq!( + report.recovery, "none", + "a clean apply must not ask for recovery" + ); + + // The planned contract landed as a new Draft operation at version 1. + let summary = registry + .get_operation_summary(&ws, &OperationId::new("op_apply_basic")) + .await + .unwrap() + .unwrap(); + assert_eq!(summary.status, OperationStatus::Draft); + assert_eq!(summary.current_draft_version, 1); + assert_eq!(summary.latest_published_version, None); + + // The anchor job is untouched: legacy replay and other plans stay viable. + let job_after = registry.get_import_job(&ws, &job).await.unwrap().unwrap(); + assert_eq!(job_after.status, ImportJobStatus::Pending); + + // Replay with the same scoped key and digest: identical immutable report. + let replay = registry.apply_import_plan(&request).await.unwrap(); + assert!(replay.replayed, "a second identical apply must be a replay"); + assert_eq!( + replay.report, *report, + "the stored report must be returned byte-for-byte" + ); + + // A changed digest under the same key is a typed conflict. + let changed_digest = "cd".repeat(32); + let changed = ApplyImportPlanRequest { + plan_digest: &changed_digest, + ..request + }; + assert!( + matches!( + registry.apply_import_plan(&changed).await, + Err(RegistryError::ImportPlanKeyDigestConflict { .. }) + ), + "a changed digest under a used key must be a typed conflict" + ); + + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/new_version.rs b/crates/crank-registry/tests/integration/import_plan/apply/new_version.rs new file mode 100644 index 0000000..24c1e85 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/new_version.rs @@ -0,0 +1,239 @@ +//! `new_version`: Published state, prior versions, bindings are preserved. + +use super::*; + +use super::super::super::common::{test_agent, test_agent_version}; +use crank_core::{AgentOperationBinding, AgentStatus}; +use crank_registry::{ + CreateAgentRequest, CreateVersionRequest, ImportPlanApplyStatus, PublishAgentRequest, + PublishRequest, +}; + +fn new_version_payload( + plan_id: &str, + job_id: &str, + fingerprint: &Value, + expected_current: u32, +) -> Value { + let mut operation = plan_operation("op_nv"); + operation.version = expected_current + 1; + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["nv"], + "selected": ["nv"], + "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "nv", + "name": "op_nv_tool", + "state": "actionable", + "decision": { "action": "new_version", "target_operation_id": "op_nv" }, + "target": { + "operation_id": "op_nv", + "name": "op_nv_tool", + "expected_state": { + "status": "draft", + "current_draft_version": expected_current, + "latest_published_version": 1 + } + }, + "operation": operation + }] + }) +} + +#[tokio::test] +async fn apply_new_version_preserves_published_prior_versions_and_bindings() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_nv").await; + let ws_slug = ws.as_str().replace('_', "-"); + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_nv", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + // Target operation: published v1 + a later draft v2. + let base = plan_operation("op_nv"); + registry.create_operation(&ws, &base, None).await.unwrap(); + let published_at = now - Duration::hours(2); + registry + .publish_operation(PublishRequest { + workspace_id: &ws, + operation_id: &OperationId::new("op_nv"), + version: 1, + published_at: &published_at, + published_by: Some("alice"), + }) + .await + .unwrap(); + let mut draft_v2 = clone_operation(&base, 2); + draft_v2.status = crank_core::OperationStatus::Draft; + registry + .create_version(CreateVersionRequest { + workspace_id: &ws, + snapshot: &draft_v2, + change_note: None, + created_by: None, + }) + .await + .unwrap(); + + // A published agent already bound to operation v1: the Agent/MCP surface + // is exactly one tool at version 1. + let mut agent = test_agent("agent_nv", AgentStatus::Draft); + agent.workspace_id = ws.clone(); + let version = test_agent_version(&agent.id, 1, AgentStatus::Draft); + let binding = AgentOperationBinding { + operation_id: OperationId::new("op_nv"), + operation_version: 1, + tool_name: "nv_tool".to_owned(), + tool_title: "NV tool".to_owned(), + tool_description_override: None, + enabled: true, + }; + registry + .create_agent(CreateAgentRequest { + agent: &agent, + version: &version, + bindings: std::slice::from_ref(&binding), + }) + .await + .unwrap(); + registry + .publish_agent(PublishAgentRequest { + workspace_id: &ws, + agent_id: &agent.id, + version: 1, + published_at: &now, + published_by: Some("alice"), + expected_state: None, + }) + .await + .unwrap(); + + let catalog_before = registry + .get_published_agent_tools_by_slug(&ws_slug, &agent.slug) + .await + .unwrap(); + assert_eq!(catalog_before.len(), 1); + assert_eq!(catalog_before[0].operation.version, 1); + assert!(catalog_before[0].operation.is_published()); + + // Apply the confirmed `new_version` (v3 on top of draft v2). + let fp = fingerprint(); + let payload = new_version_payload("plan_nv", job.as_str(), &fp, 2); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_nv", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_nv", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + + let result = registry.apply_import_plan(&request).await.unwrap(); + assert!(!result.replayed); + let report = &result.report; + assert_eq!(report.status, ImportPlanApplyStatus::Applied); + assert_eq!(report.counts.versioned, 1); + assert_eq!( + report.outcomes[0].outcome, + ImportPlanCandidateOutcome::Versioned + ); + assert_eq!(report.outcomes[0].operation_id.as_deref(), Some("op_nv")); + assert_eq!(report.outcomes[0].version, Some(3)); + + let summary = registry + .get_operation_summary(&ws, &OperationId::new("op_nv")) + .await + .unwrap() + .unwrap(); + assert_eq!( + summary.current_draft_version, 3, + "the new Draft version appears" + ); + assert_eq!( + summary.latest_published_version, + Some(1), + "the Published Version stays untouched" + ); + + // Prior version 1 keeps its published state; 3 is a fresh draft. + let v1 = registry + .get_operation_version(&ws, &OperationId::new("op_nv"), 1) + .await + .unwrap() + .unwrap(); + assert!(v1.snapshot.is_published(), "version 1 must stay published"); + let v3 = registry + .get_operation_version(&ws, &OperationId::new("op_nv"), 3) + .await + .unwrap() + .unwrap(); + assert!(!v3.snapshot.is_published(), "version 3 must be a draft"); + + // The Agent/MCP surface is unchanged: still exactly the published v1 + // tool; the new draft version 3 is invisible until publication. + let catalog_after = registry + .get_published_agent_tools_by_slug(&ws_slug, &agent.slug) + .await + .unwrap(); + assert_eq!( + catalog_after.len(), + 1, + "the catalog must gain no new draft tool" + ); + assert_eq!(catalog_after[0].operation.version, 1); + assert!(catalog_after[0].operation.is_published()); + + // A stale expected state is a typed conflict and changes nothing. + let stale_payload = new_version_payload("plan_nv_stale", job.as_str(), &fp, 5); + let (stale_plan, stale_digest) = + store_plan(®istry, &ws, &job, "plan_nv_stale", &fp, stale_payload).await; + let stale_request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &stale_plan, + plan_digest: &stale_digest, + idempotency_key: "idem_nv_stale", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + assert!( + matches!( + registry.apply_import_plan(&stale_request).await, + Err(RegistryError::ImportPlanTargetStale { .. }) + ), + "a stale new_version target must be a typed conflict" + ); + let unchanged = registry + .get_operation_summary(&ws, &OperationId::new("op_nv")) + .await + .unwrap() + .unwrap(); + assert_eq!(unchanged.current_draft_version, 3); + database.cleanup().await; +} + +fn clone_operation(base: &RegistryOperation, version: u32) -> RegistryOperation { + let mut clone = base.clone(); + clone.version = version; + clone +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/partial.rs b/crates/crank-registry/tests/integration/import_plan/apply/partial.rs new file mode 100644 index 0000000..88fb040 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/partial.rs @@ -0,0 +1,97 @@ +//! Partial selection: only the closed selected set mutates. + +use super::*; + +fn partial_payload(plan_id: &str, job_id: &str, fingerprint: &Value) -> Value { + let operation = plan_operation("op_apply_partial"); + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "partial", + "all": ["picked", "not_picked"], + "selected": ["picked"], + "excluded": ["not_picked"] + }, + "counts": { "selected": 1, "excluded": 1, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "picked", + "name": operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": operation + }] + }) +} + +#[tokio::test] +async fn apply_partial_selection_only_mutates_selected_and_explains_excluded() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_partial").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_partial", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let plan_id_str = "plan_apply_partial"; + let payload = partial_payload(plan_id_str, job.as_str(), &fp); + let (plan, digest) = store_plan(®istry, &ws, &job, plan_id_str, &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_partial", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + + let result = registry.apply_import_plan(&request).await.unwrap(); + assert!(!result.replayed); + let report = &result.report; + assert_eq!(report.status, ImportPlanApplyStatus::Applied); + assert_eq!(report.counts.created, 1); + assert_eq!( + report.counts.excluded, 1, + "excluded candidates remain explained" + ); + assert_eq!(report.counts.rejected, 0); + + let excluded = report + .outcomes + .iter() + .find(|reported| reported.key == "not_picked") + .expect("the excluded candidate must be explained in the report"); + assert_eq!( + excluded.outcome, + ImportPlanCandidateOutcome::Excluded, + "an excluded candidate is reported, not mutated" + ); + assert_eq!(excluded.reason.as_deref(), Some("not_selected")); + assert!( + excluded.operation_id.is_none(), + "an excluded candidate must not gain an operation" + ); + + // Exactly one operation exists in the workspace — the selected one. + let listed = registry + .get_operation_summary(&ws, &OperationId::new("op_apply_partial")) + .await + .unwrap() + .unwrap(); + assert_eq!(listed.current_draft_version, 1); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/replay_stale.rs b/crates/crank-registry/tests/integration/import_plan/apply/replay_stale.rs new file mode 100644 index 0000000..b07ba09 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/replay_stale.rs @@ -0,0 +1,410 @@ +//! Crash-replay evidence, stale fingerprint, rollback atomicity and +//! stale job/expiry/source conflicts. + +use super::*; + +#[tokio::test] +async fn apply_rejects_plan_material_inconsistent_with_its_anchor() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_material_mismatch").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_material_mismatch", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + for (index, change) in ["plan_wrong_job", "plan_partial_full"].iter().enumerate() { + let plan_id = format!("plan_material_mismatch_{index}"); + let mut payload = single_create_payload(&plan_id, job.as_str(), &fp); + if *change == "plan_wrong_job" { + payload["job_id"] = json!("different_job"); + } else { + payload["selection"]["excluded"] = json!(["other"]); + payload["selection"]["all"] = json!(["basic", "other"]); + payload["counts"]["excluded"] = json!(1); + } + let (plan, digest) = store_plan(®istry, &ws, &job, &plan_id, &fp, payload).await; + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: change, + verified_fingerprint: &fp, + applied_at: &now, + }; + assert!(matches!( + registry.apply_import_plan(&request).await, + Err(RegistryError::ImportPlanPayloadCorrupt { .. }) + )); + } + + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_apply_basic")) + .await + .unwrap() + .is_none() + ); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_returns_stored_report_after_job_cleanup() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_replay").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_replay", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_replay", &fp).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_replay", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + let first = registry.apply_import_plan(&request).await.unwrap(); + assert!(!first.replayed); + + // The job expires and the expired-job cleanup removes job + plan, but + // the committed application evidence outlives both. + sqlx::query( + "update import_jobs set expires_at = $1 + where id = $2 and workspace_id = $3", + ) + .bind(now - Duration::minutes(1)) + .bind(job.as_str()) + .bind(ws.as_str()) + .execute(&raw_pool) + .await + .unwrap(); + let cleanup = registry.cleanup_expired_import_jobs(8).await.unwrap(); + assert_eq!(cleanup.deleted_jobs, 1); + assert!( + registry.get_import_job(&ws, &job).await.unwrap().is_none(), + "the expired job must be gone" + ); + assert!( + registry + .get_import_plan(&ws, &plan) + .await + .unwrap() + .is_none(), + "the plan must cascade with its job" + ); + + // Replay after cleanup returns the stored immutable report. + let replay = registry.apply_import_plan(&request).await.unwrap(); + assert!( + replay.replayed, + "replay evidence must outlive the plan and its job" + ); + assert_eq!( + replay.report, first.report, + "the post-cleanup replay must be byte-for-byte the committed report" + ); + + let stored = registry + .get_import_plan_application(&ws, "idem_replay") + .await + .unwrap() + .expect("the application anchor must survive cleanup"); + assert_eq!(stored.plan_digest, digest); + assert_eq!(stored.result.report, first.report); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_rejects_stale_fingerprint_without_mutation() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_fp").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_fp", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_fp", &fp).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let drifted = json!({"importer": "test", "digest": "fp-2"}); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_fp", + verified_fingerprint: &drifted, + applied_at: &applied_at, + }; + assert!( + matches!( + registry.apply_import_plan(&request).await, + Err(RegistryError::ImportPlanFingerprintStale { .. }) + ), + "a fingerprint drift is a typed re-preview conflict" + ); + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_apply_basic")) + .await + .unwrap() + .is_none(), + "zero mutation on a stale fingerprint" + ); + assert_eq!( + registry + .get_import_plan_application(&ws, "idem_fp") + .await + .unwrap(), + None + ); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_rolls_back_entire_plan_when_one_candidate_conflicts() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_rollback").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_rb", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + // Occupies the name the second candidate wants. + let mut blocker = plan_operation("op_blocker"); + blocker.name = "clash_tool".to_owned(); + registry + .create_operation(&ws, &blocker, None) + .await + .unwrap(); + + let fp_value = json!({"importer": "test", "digest": "fp-rb"}); + let op_ok = plan_operation("op_rb_ok"); + let mut op_bad = plan_operation("op_rb_bad"); + op_bad.name = "clash_tool".to_owned(); + let payload = json!({ + "schema_version": "import-plan-v1", + "plan_reference": "plan_rb", + "job_id": job.as_str(), + "fingerprint": fp_value.clone(), + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["a_ok", "z_bad"], + "selected": ["a_ok", "z_bad"], + "excluded": [] + }, + "counts": { "selected": 2, "excluded": 0, "actionable": 2, "blocked": 0 }, + "candidates": [ + { + "key": "a_ok", + "name": op_ok.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": op_ok + }, + { + "key": "z_bad", + "name": op_bad.name, + "state": "actionable", + "decision": { "action": "create" }, + "operation": op_bad + } + ] + }); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_rb", &fp_value, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_rb", + verified_fingerprint: &fp_value, + applied_at: &applied_at, + }; + + // One conflict rolls the whole transaction back: no partial state. + assert!( + matches!( + registry.apply_import_plan(&request).await, + Err(RegistryError::ImportPlanNameConflict { name }) if name == "clash_tool" + ), + "a candidate conflict must produce a typed conflict" + ); + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_rb_ok")) + .await + .unwrap() + .is_none(), + "the earlier successful candidate must roll back with the transaction" + ); + assert_eq!( + sqlx::query_scalar::<_, i64>("select count(*) from operations") + .fetch_one(&raw_pool) + .await + .unwrap(), + 1, + "only the pre-existing blocker operation remains" + ); + assert_eq!( + registry + .get_import_plan_application(&ws, "idem_rb") + .await + .unwrap(), + None, + "a rolled-back apply must not persist application evidence" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_rejects_expired_plan_expired_job_and_detached_source() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_stale").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_stale", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + + // All three plans are created while the anchor job is still valid. + let expired_payload = single_create_payload("plan_stale_p", job.as_str(), &fp); + let p1 = ImportPlanId::new("plan_stale_p"); + let d1 = registry + .create_import_plan(CreateImportPlanRequest { + id: &p1, + workspace_id: &ws, + job_id: &job, + payload: &expired_payload, + created_at: &(now - Duration::hours(2)), + expires_at: &(now - Duration::hours(1)), + }) + .await + .unwrap() + .digest; + let (p2, d2) = stored_plan(®istry, &ws, &job, "plan_stale_j", &fp).await; + let (p3, d3) = stored_plan(®istry, &ws, &job, "plan_stale_s", &fp).await; + + // The plan was created with a past lifetime; immutable rows stay untouched. + assert!( + matches!( + registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &p1, + plan_digest: &d1, + idempotency_key: "idem_stale_p", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await, + Err(RegistryError::ImportPlanExpired { .. }) + ), + "an expired plan must be a typed expired conflict" + ); + + // Expired anchor job (plan still inside its lifetime). + sqlx::query("update import_jobs set expires_at = $1 where id = $2 and workspace_id = $3") + .bind(now - Duration::minutes(5)) + .bind(job.as_str()) + .bind(ws.as_str()) + .execute(&raw_pool) + .await + .unwrap(); + let r2 = registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &p2, + plan_digest: &d2, + idempotency_key: "idem_stale_j", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await; + assert!( + matches!(&r2, Err(RegistryError::ImportPlanExpired { .. })), + "an expired anchor job must be a typed expired conflict, got {r2:?}" + ); + + // Restored job lifetime; now the source itself is detached. + sqlx::query("update import_jobs set expires_at = $1 where id = $2 and workspace_id = $3") + .bind(now + Duration::hours(1)) + .bind(job.as_str()) + .bind(ws.as_str()) + .execute(&raw_pool) + .await + .unwrap(); + sqlx::query( + "update artifact_sources set lifecycle = 'detached', detached_at = $3 + where workspace_id = $1 and source_id = $2", + ) + .bind(ws.as_str()) + .bind(SOURCE_ID) + .bind(now) + .execute(&raw_pool) + .await + .unwrap(); + let r3 = registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &p3, + plan_digest: &d3, + idempotency_key: "idem_stale_s", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await; + assert!( + matches!(&r3, Err(RegistryError::SourceUnavailable)), + "a detached source must be a typed conflict, got {r3:?}" + ); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/apply/skip_rename.rs b/crates/crank-registry/tests/integration/import_plan/apply/skip_rename.rs new file mode 100644 index 0000000..b200a30 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/skip_rename.rs @@ -0,0 +1,292 @@ +//! `skip` keeps the exact target untouched; `rename` creates the exact +//! planned name and never auto-generates a fallback. + +use super::*; + +async fn operation_count(raw_pool: &PgPool) -> i64 { + sqlx::query_scalar::<_, i64>("select count(*) from operations") + .fetch_one(raw_pool) + .await + .unwrap() +} + +fn skip_payload(plan_id: &str, job_id: &str, fingerprint: &Value, target_version: u32) -> Value { + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["sk"], + "selected": ["sk"], + "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "sk", + "name": "op_skip_tool", + "state": "actionable", + "decision": { + "action": "skip", + "target_operation_id": "op_skip" + }, + "target": { + "operation_id": "op_skip", + "name": "op_skip_tool", + "expected_state": { + "status": "draft", + "current_draft_version": target_version, + "latest_published_version": null + } + } + }] + }) +} + +#[tokio::test] +async fn apply_skip_keeps_exact_target_untouched_and_stale_target_conflicts() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_skip").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_skip", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + // Existing operation that the plan will skip. + let existing = plan_operation("op_skip"); + registry + .create_operation(&ws, &existing, None) + .await + .unwrap(); + let before = registry + .get_operation_summary(&ws, &OperationId::new("op_skip")) + .await + .unwrap() + .unwrap(); + assert_eq!(before.current_draft_version, 1); + + let fp = fingerprint(); + let ok_payload = skip_payload("plan_skip_ok", job.as_str(), &fp, 1); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_skip_ok", &fp, ok_payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_skip", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + + let result = registry.apply_import_plan(&request).await.unwrap(); + assert!(!result.replayed); + let report = &result.report; + assert_eq!(report.status, ImportPlanApplyStatus::Applied); + assert_eq!(report.counts.skipped, 1); + assert_eq!(report.counts.created, 0); + let skipped = &report.outcomes[0]; + assert_eq!(skipped.outcome, ImportPlanCandidateOutcome::Skipped); + assert_eq!(skipped.operation_id.as_deref(), Some("op_skip")); + assert!( + skipped.version.is_none(), + "a skip must not produce a new version" + ); + + // The exact target is untouched. + let after = registry + .get_operation_summary(&ws, &OperationId::new("op_skip")) + .await + .unwrap() + .unwrap(); + assert_eq!( + before, after, + "skip must leave the authorized target unchanged" + ); + assert_eq!(operation_count(&raw_pool).await, 1); + + // A stale target state (wrong expected draft version) is a typed conflict. + let stale_payload = skip_payload("plan_skip_stale", job.as_str(), &fp, 2); + let (stale_plan, stale_digest) = + store_plan(®istry, &ws, &job, "plan_skip_stale", &fp, stale_payload).await; + let stale_request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &stale_plan, + plan_digest: &stale_digest, + idempotency_key: "idem_skip_stale", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + assert!( + matches!( + registry.apply_import_plan(&stale_request).await, + Err(RegistryError::ImportPlanTargetStale { + operation_id + }) if operation_id == "op_skip" + ), + "a stale skip target must be a typed conflict" + ); + database.cleanup().await; +} + +fn rename_payload(plan_id: &str, job_id: &str, fingerprint: &Value) -> Value { + let mut operation = plan_operation("op_renamed"); + operation.name = "renamed_tool".to_owned(); + json!({ + "schema_version": "import-plan-v1", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["rn"], + "selected": ["rn"], + "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "rn", + "name": "renamed_tool", + "state": "actionable", + "decision": { "action": "rename", "confirmed_name": "renamed_tool" }, + "operation": operation + }] + }) +} + +#[tokio::test] +async fn apply_rename_creates_exact_planned_name_and_stale_name_conflicts() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw_pool = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_apply_rename").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_apply_rename", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw_pool, &ws, now).await; + + let fp = fingerprint(); + let payload = rename_payload("plan_rename", job.as_str(), &fp); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_rename", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_rename", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + + // Confirmed name is free: the exact separate Operation is created. + let result = registry.apply_import_plan(&request).await.unwrap(); + assert!(!result.replayed); + let report = &result.report; + assert_eq!(report.status, ImportPlanApplyStatus::Applied); + assert_eq!(report.counts.renamed, 1); + assert_eq!(report.counts.created, 0); + assert_eq!( + report.outcomes[0].outcome, + ImportPlanCandidateOutcome::Renamed + ); + let renamed = registry + .get_operation_summary(&ws, &OperationId::new("op_renamed")) + .await + .unwrap() + .unwrap(); + assert_eq!( + renamed.name, "renamed_tool", + "the exact planned name is materialized" + ); + assert_eq!(renamed.current_draft_version, 1); + + // A different plan confirming a name that is already taken conflicts, + // and never falls back to an auto-generated alternative. + let mut blocker = plan_operation("op_blocker"); + blocker.name = "occupied_tool".to_owned(); + registry + .create_operation(&ws, &blocker, None) + .await + .unwrap(); + let mut conflicting_op = plan_operation("op_conflict"); + conflicting_op.name = "occupied_tool".to_owned(); + let conflicting_payload = json!({ + "schema_version": "import-plan-v1", + "plan_reference": "plan_rename_stale", + "job_id": job.as_str(), + "fingerprint": fp, + "source": { "source_id": SOURCE_ID, "digest": source_ref() }, + "dependencies": [], + "selection": { + "mode": "full", + "all": ["rn2"], + "selected": ["rn2"], + "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "rn2", + "name": "occupied_tool", + "state": "actionable", + "decision": { "action": "rename", "confirmed_name": "occupied_tool" }, + "operation": conflicting_op + }] + }); + let (stale_plan, stale_digest) = store_plan( + ®istry, + &ws, + &job, + "plan_rename_stale", + &fp, + conflicting_payload, + ) + .await; + let stale_request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &stale_plan, + plan_digest: &stale_digest, + idempotency_key: "idem_rename_stale", + verified_fingerprint: &fp, + applied_at: &applied_at, + }; + assert!( + matches!( + registry + .apply_import_plan(&stale_request) + .await + .err(), + Some(RegistryError::ImportPlanNameConflict { ref name }) + if *name == "occupied_tool" + ), + "a taken confirmed name must conflict, never auto-rename" + ); + // Only the pre-existing blocker carries that name — zero new operations. + assert_eq!( + sqlx::query_scalar::<_, i64>( + "select count(*) from operations where name = 'occupied_tool'" + ) + .fetch_one(&raw_pool) + .await + .unwrap(), + 1 + ); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/import_plan/create.rs b/crates/crank-registry/tests/integration/import_plan/create.rs new file mode 100644 index 0000000..b4e402d --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/create.rs @@ -0,0 +1,988 @@ +use super::super::common::TestDatabase; +use super::{create_pending_openapi_job, create_workspace}; +use time::Duration; + +use crank_artifacts::ArtifactRef; +use crank_core::WorkspaceId; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use sqlx::Row; +use time::OffsetDateTime; + +use crank_registry::{ + ArtifactSourceId, CreateImportJobRequest, CreateImportPlanRequest, ImportJobId, ImportJobKind, + ImportJobSourceEnvelope, ImportJobStatus, ImportPlan, ImportPlanId, RegistryError, +}; + +fn plan_payload() -> Value { + json!({ + "selection": ["a", "b"], + "mode": "full", + "decisions": { "a": "keep" } + }) +} + +fn expected_digest(payload: &Value) -> String { + let canonical = serde_json::to_vec(payload).unwrap(); + format!("{:x}", Sha256::digest(canonical)) +} + +fn current_second() -> OffsetDateTime { + OffsetDateTime::from_unix_timestamp(OffsetDateTime::now_utc().unix_timestamp()).unwrap() +} + +#[tokio::test] +async fn import_plan_roundtrips_with_registry_computed_digest_and_foorescope_is_invisible() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let ws1 = create_workspace(®istry, "ws_plan_a").await; + let ws2 = create_workspace(®istry, "ws_plan_b").await; + + let job1 = create_pending_openapi_job(®istry, &ws1, "job_a", created_at, expires_at).await; + let plan = ImportPlanId::new("plan_a"); + let payload = plan_payload(); + let digest = expected_digest(&payload); + + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws1, + job_id: &job1, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + + assert_eq!(created.id, plan); + assert_eq!(created.workspace_id, ws1); + assert_eq!(created.job_id, job1); + assert_eq!(created.payload, payload); + assert_eq!(created.created_at, created_at); + assert_eq!(created.expires_at, expires_at); + // Registry-derived digest must match an independent recomputation and be + // lowercase 64-hex. + assert_eq!(created.digest, digest); + assert_eq!(created.digest.len(), 64); + assert!( + created + .digest + .bytes() + .all(|b| b.is_ascii_digit() || matches!(b, b'a'..=b'f')) + ); + + // Read returns the exact stored fields. + let read = registry + .get_import_plan(&ws1, &plan) + .await + .unwrap() + .expect("plan must be visible in its own workspace"); + assert_eq!(read, created); + + // A plan id that only exists in ws1 is invisible from ws2 via `None`, + // never an error. + let absent = registry.get_import_plan(&ws2, &plan).await.unwrap(); + assert!( + absent.is_none(), + "foreign-scope plan must be invisible, not an error" + ); + + let missing = ImportPlanId::new("does_not_exist"); + assert!( + registry + .get_import_plan(&ws1, &missing) + .await + .unwrap() + .is_none() + ); + database.cleanup().await; +} + +#[tokio::test] +async fn duplicate_replay_returns_same_plan_but_changed_material_or_expiry_conflicts() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let ws = create_workspace(®istry, "ws_replay").await; + let job = create_pending_openapi_job(®istry, &ws, "job_r", created_at, expires_at).await; + let plan = ImportPlanId::new("plan_r"); + let payload = plan_payload(); + + let first = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + + // Byte-identical material, same job and same lifetime -> the same immutable + // plan is returned (a replay), not a conflict. + let replayed = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + assert_eq!( + replayed, first, + "an identical replay must return the same plan" + ); + + // Same id + different material -> typed conflict, no UPDATE. + let changed = json!({ "mode": "partial" }); + let conflict = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &changed, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .expect_err("the same id with changed material must conflict"); + assert!( + matches!(conflict, RegistryError::ImportPlanConflict { .. }), + "{conflict:?}" + ); + assert_eq!( + registry.get_import_plan(&ws, &plan).await.unwrap().unwrap(), + first, + "the conflicting request must not have overwritten the stored plan" + ); + + // Same id + same material + different expiry -> typed conflict. + let other_expiry = expires_at + Duration::hours(1); + let conflict = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &other_expiry, + }) + .await + .expect_err("the same id with a different lifetime must conflict"); + assert!( + matches!(conflict, RegistryError::ImportPlanConflict { .. }), + "{conflict:?}" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn cross_workspace_same_plan_id_is_separate_but_scoped_invisible() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let ws1 = create_workspace(®istry, "ws_cw_1").await; + let ws2 = create_workspace(®istry, "ws_cw_2").await; + let job1 = create_pending_openapi_job(®istry, &ws1, "job_cw1", created_at, expires_at).await; + let job2 = create_pending_openapi_job(®istry, &ws2, "job_cw2", created_at, expires_at).await; + let plan = ImportPlanId::new("shared_id"); + let payload = plan_payload(); + + // The same id is legal per workspace and binds to that workspace's job. + let in_ws2 = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws2, + job_id: &job2, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + assert_eq!(in_ws2.workspace_id, ws2); + assert_eq!(in_ws2.job_id, job2); + + // ws1 has not yet created the id, so it is invisible there ... + assert!( + registry + .get_import_plan(&ws1, &plan) + .await + .unwrap() + .is_none() + ); + + let in_ws1 = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws1, + job_id: &job1, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + assert_eq!(in_ws1.workspace_id, ws1); + assert_eq!(in_ws1.job_id, job1); + + // Each workspace now reads back exactly its own row. + assert_eq!( + registry + .get_import_plan(&ws1, &plan) + .await + .unwrap() + .unwrap(), + in_ws1 + ); + assert_eq!( + registry + .get_import_plan(&ws2, &plan) + .await + .unwrap() + .unwrap(), + in_ws2 + ); + database.cleanup().await; +} + +#[tokio::test] +async fn create_rejects_invalid_input_bounds() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let ws = create_workspace(®istry, "ws_bounds").await; + let job = create_pending_openapi_job(®istry, &ws, "job_b", created_at, expires_at).await; + let payload = plan_payload(); + + // Empty id (0 bytes) is rejected. + let empty_id = ImportPlanId::new(""); + assert!(matches!( + registry + .create_import_plan(CreateImportPlanRequest { + id: &empty_id, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { field: "id" } + )); + + // Over-long id (> 128 bytes) is rejected. + let long_id = ImportPlanId::new("id".repeat(70)); // 140 bytes + assert!(matches!( + registry + .create_import_plan(CreateImportPlanRequest { + id: &long_id, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { field: "id" } + )); + + // A non-object payload is rejected. + let arr = json!([1, 2, 3]); + assert!(matches!( + registry + .create_import_plan(CreateImportPlanRequest { + id: &ImportPlanId::new("plan_ok"), + workspace_id: &ws, + job_id: &job, + payload: &arr, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { field: "payload" } + )); + + // expires_at must be strictly greater than created_at. + for bad_expires in [created_at, created_at - Duration::minutes(1)] { + assert!(matches!( + registry + .create_import_plan(CreateImportPlanRequest { + id: &ImportPlanId::new("plan_ttl"), + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &bad_expires, + }) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { + field: "expires_at" + } + )); + } + + // A serialized payload over the 4 MiB ceiling is rejected. + let big = json!({ "blob": "x".repeat(4 * 1024 * 1024) }); + assert!(matches!( + registry + .create_import_plan(CreateImportPlanRequest { + id: &ImportPlanId::new("plan_big"), + workspace_id: &ws, + job_id: &job, + payload: &big, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { field: "payload" } + )); + + // A get also validates the id bound without leaking row data. + assert!(matches!( + registry + .get_import_plan(&ws, &ImportPlanId::new("")) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { field: "id" } + )); + database.cleanup().await; +} + +#[tokio::test] +async fn create_rejects_terminal_expired_foreign_and_unsourced_jobs() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_job_states").await; + let ws_other = create_workspace(®istry, "ws_job_other").await; + let payload = plan_payload(); + + // Terminal (completed) job -> typed terminal conflict. + let completed_job = ImportJobId::new("job_done"); + { + let source_id = ArtifactSourceId::new("src_openapi_test"); + let digest = ArtifactRef::from_digest_hex(&"cd".repeat(32)).unwrap(); + let preview_payload = json!({ + "source": { "source_id": source_id.as_str(), "digest": digest.as_str() }, + }); + registry + .create_import_job(CreateImportJobRequest { + id: &completed_job, + workspace_id: &ws, + kind: ImportJobKind::OpenApi, + source_format: "openapi", + source_version: Some("3.1.0"), + status: ImportJobStatus::Completed, + source: &ImportJobSourceEnvelope { + source_id, + digest: digest.clone(), + }, + preview_payload: &preview_payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + } + assert!(matches!( + attempt_create( + ®istry, + &ws, + &ImportPlanId::new("plan_jobs"), + &completed_job, + &payload, + &created_at, + &expires_at + ) + .await + .unwrap_err(), + RegistryError::ImportPlanJobTerminal { .. } + )); + + // Expired (non-terminal) job -> typed expired conflict. + let expired_job = ImportJobId::new("job_expired"); + { + let source_id = ArtifactSourceId::new("src_openapi_test"); + let digest = ArtifactRef::from_digest_hex(&"ef".repeat(32)).unwrap(); + let created_back = now - Duration::hours(3); + let expired_at = now - Duration::hours(1); + let preview_payload = json!({ + "source": { "source_id": source_id.as_str(), "digest": digest.as_str() }, + }); + registry + .create_import_job(CreateImportJobRequest { + id: &expired_job, + workspace_id: &ws, + kind: ImportJobKind::OpenApi, + source_format: "openapi", + source_version: Some("3.1.0"), + status: ImportJobStatus::Pending, + source: &ImportJobSourceEnvelope { + source_id, + digest: digest.clone(), + }, + preview_payload: &preview_payload, + created_at: &created_back, + expires_at: &expired_at, + }) + .await + .unwrap(); + } + assert!(matches!( + attempt_create( + ®istry, + &ws, + &ImportPlanId::new("plan_jobs"), + &expired_job, + &payload, + &created_at, + &expires_at + ) + .await + .unwrap_err(), + RegistryError::ImportPlanJobExpired { .. } + )); + + // A job that belongs to another workspace is unavailable in this scope. + let foreign_job = + create_pending_openapi_job(®istry, &ws_other, "job_foreign", created_at, expires_at) + .await; + assert!(matches!( + attempt_create( + ®istry, + &ws, + &ImportPlanId::new("plan_jobs"), + &foreign_job, + &payload, + &created_at, + &expires_at + ) + .await + .unwrap_err(), + RegistryError::ImportPlanJobUnavailable { .. } + )); + + // A pending job with no verifiable source envelope is unavailable. + let raw = database.raw_pool().await; + sqlx::query( + "insert into import_jobs ( + id, workspace_id, kind, source_format, status, preview_payload, created_at, expires_at + ) values ( + 'job_unsourced', $1, 'open_api', 'openapi', 'pending', '{}'::jsonb, $2, $3 + )", + ) + .bind(ws.as_str()) + .bind(created_at) + .bind(expires_at) + .execute(&raw) + .await + .unwrap(); + let unsourced_job = ImportJobId::new("job_unsourced"); + assert!(matches!( + attempt_create( + ®istry, + &ws, + &ImportPlanId::new("plan_jobs"), + &unsourced_job, + &payload, + &created_at, + &expires_at + ) + .await + .unwrap_err(), + RegistryError::ImportPlanJobUnavailable { .. } + )); + database.cleanup().await; +} + +#[tokio::test] +async fn concurrent_create_with_same_plan_id_serializes_to_replay_or_typed_conflict() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let ws = create_workspace(®istry, "ws_race").await; + let job = create_pending_openapi_job(®istry, &ws, "job_race", created_at, expires_at).await; + + // Identical material racing on the same id: both callers must converge + // on the same stored plan, never a raw unique violation. + let plan_same = ImportPlanId::new("plan_race_same"); + let racers_same = plan_same.clone(); + let payload_a = plan_payload(); + let (first, second) = tokio::join!( + attempt_create( + ®istry, + &ws, + &plan_same, + &job, + &payload_a, + &created_at, + &expires_at + ), + attempt_create( + ®istry, + &ws, + &racers_same, + &job, + &payload_a, + &created_at, + &expires_at + ) + ); + let first = first.expect("concurrent identical insert must succeed"); + let second = second.expect("the loser must converge onto a replay, not an error"); + assert_eq!( + first, second, + "both callers must see the same immutable plan" + ); + assert_eq!( + registry + .get_import_plan(&ws, &plan_same) + .await + .unwrap() + .unwrap(), + first, + "the stored plan must be the one both racers returned" + ); + + // Different material racing on the same id: exactly one winner, and the + // loser must get the typed conflict, never a raw unique violation. + let plan_other = ImportPlanId::new("plan_race_diff"); + let racers_other = plan_other.clone(); + let payload_b = json!({ "mode": "partial" }); + let (a, b) = tokio::join!( + attempt_create( + ®istry, + &ws, + &plan_other, + &job, + &payload_a, + &created_at, + &expires_at + ), + attempt_create( + ®istry, + &ws, + &racers_other, + &job, + &payload_b, + &created_at, + &expires_at + ) + ); + let stored = registry + .get_import_plan(&ws, &plan_other) + .await + .unwrap() + .unwrap(); + assert!( + a.is_ok() ^ b.is_ok(), + "exactly one racer may install the plan: a={a:?}, b={b:?}" + ); + for result in [a, b] { + match result { + Ok(plan) => assert_eq!( + plan, + stored.clone(), + "the winner must equal the stored plan" + ), + Err(error) => assert!( + matches!(error, RegistryError::ImportPlanConflict { .. }), + "the losing racer must see the typed conflict, got {error:?}" + ), + } + } + database.cleanup().await; +} + +#[tokio::test] +async fn sub_microsecond_timestamps_roundtrip_and_replay_identically() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + // Sub-microsecond instants: the Postgres timestamptz column stores + // microseconds, so the registry must normalize deterministically instead + // of letting read-back silently truncate only on the second read. + let created_at = OffsetDateTime::from_unix_timestamp_nanos(1_800_000_000_123_456_789).unwrap(); + let expires_at_nanos = 1_800_000_000_123_456_789 + 86_400_000_000_512; + let expires_at = OffsetDateTime::from_unix_timestamp_nanos(expires_at_nanos).unwrap(); + let expected_created = + OffsetDateTime::from_unix_timestamp_nanos((1_800_000_000_123_456_789 / 1_000) * 1_000) + .unwrap(); + let expected_expires = + OffsetDateTime::from_unix_timestamp_nanos((expires_at_nanos / 1_000) * 1_000).unwrap(); + let ws = create_workspace(®istry, "ws_micro").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_micro", + expected_created, + expected_expires + Duration::hours(1), + ) + .await; + let plan = ImportPlanId::new("plan_micro"); + let payload = plan_payload(); + + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + // The first returned object already carries the DB-precision values. + assert_eq!(created.created_at, expected_created); + assert_eq!(created.expires_at, expected_expires); + + // Every later read must be identical to the first response. + let read = registry.get_import_plan(&ws, &plan).await.unwrap().unwrap(); + assert_eq!( + read, created, + "the read-back plan must equal the created plan" + ); + + // A replay re-issued with the original nanosecond instants must still be + // recognized as the same material, not a spurious conflict. + let replayed = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap_or_else(|error| { + panic!("an identical replay with sub-microsecond instants must succeed, got {error:?}") + }); + assert_eq!(replayed, created, "the replay must return the stored plan"); + database.cleanup().await; +} + +#[tokio::test] +async fn plan_expiration_must_not_outlive_its_job() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let job_expires_at = created_at + Duration::hours(24); + let ws = create_workspace(®istry, "ws_expires").await; + let payload = plan_payload(); + + // A plan living exactly as long as the job is allowed (expires_at <= job). + let exact_job = + create_pending_openapi_job(®istry, &ws, "job_exact", created_at, job_expires_at).await; + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &ImportPlanId::new("plan_exact"), + workspace_id: &ws, + job_id: &exact_job, + payload: &payload, + created_at: &created_at, + expires_at: &job_expires_at, + }) + .await + .expect("a plan expiring exactly with its job must be accepted"); + assert_eq!(created.expires_at, job_expires_at); + + // A plan outliving the job is rejected with a typed input error. + let long_job = + create_pending_openapi_job(®istry, &ws, "job_long", created_at, job_expires_at).await; + assert!(matches!( + attempt_create( + ®istry, + &ws, + &ImportPlanId::new("plan_long"), + &long_job, + &payload, + &created_at, + &(job_expires_at + Duration::hours(1)) + ) + .await + .unwrap_err(), + RegistryError::InvalidImportPlan { + field: "expires_at" + } + )); + // The rejection left no plan behind and did not mutate the job. + assert!( + registry + .get_import_plan(&ws, &ImportPlanId::new("plan_long")) + .await + .unwrap() + .is_none() + ); + let job = registry + .get_import_job(&ws, &long_job) + .await + .unwrap() + .unwrap(); + assert_eq!(job.status, ImportJobStatus::Pending); + database.cleanup().await; +} + +async fn attempt_create( + registry: &crank_registry::PostgresRegistry, + ws: &WorkspaceId, + plan_id: &ImportPlanId, + job: &ImportJobId, + payload: &Value, + created_at: &OffsetDateTime, + expires_at: &OffsetDateTime, +) -> Result { + registry + .create_import_plan(CreateImportPlanRequest { + id: plan_id, + workspace_id: ws, + job_id: job, + payload, + created_at, + expires_at, + }) + .await +} + +#[tokio::test] +async fn plans_do_not_mutate_the_legacy_import_job_contract() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let created_at = current_second(); + let expires_at = created_at + Duration::hours(24); + let ws = create_workspace(®istry, "ws_legacy").await; + let job = + create_pending_openapi_job(®istry, &ws, "job_legacy", created_at, expires_at).await; + + let before = registry.get_import_job(&ws, &job).await.unwrap().unwrap(); + assert_eq!(before.status, ImportJobStatus::Pending); + assert_eq!(before.error_text, None); + + let plan = ImportPlanId::new("plan_legacy"); + let payload = plan_payload(); + let _plan = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &created_at, + expires_at: &expires_at, + }) + .await + .unwrap(); + + // The legacy read contract is unchanged by plan creation: same pending + // status, no error text, and the original preview payload byte-for-byte. + let after = registry + .get_import_job(&ws, &job) + .await + .unwrap() + .expect("legacy import job must still be readable"); + assert_eq!(after.status, ImportJobStatus::Pending); + assert_eq!(after.error_text, None); + assert_eq!(after.preview_payload, before.preview_payload); + + // Legacy get_import_job continues to honor workspace scoping. + assert!( + registry + .get_import_job(&create_workspace(®istry, "ws_none").await, &job) + .await + .unwrap() + .is_none() + ); + database.cleanup().await; +} + +#[tokio::test] +async fn expired_cleanup_deletes_job_and_plan_but_keeps_application_report() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw = database.raw_pool().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_cleanup").await; + + // Pending OpenAPI job with a verifiable source envelope that is still + // alive when the plan is created ... + let job_created = now - Duration::hours(2); + let job_expires = now + Duration::hours(1); + let job = + create_pending_openapi_job(®istry, &ws, "job_cleanup", job_created, job_expires).await; + + // ... anchored by an active source row so the cleanup detachment is + // observable ... + let blob_digest = "ab".repeat(32); + sqlx::query( + "insert into artifact_blobs (digest, artifact_ref, size_bytes, created_at, updated_at) + values ($1, 'sha256:' || $1, 16, $2, $2)", + ) + .bind(&blob_digest) + .bind(now) + .execute(&raw) + .await + .unwrap(); + sqlx::query( + "insert into artifact_sources ( + workspace_id, source_id, blob_digest, mime_type, sensitivity, lifecycle, + created_at, updated_at + ) values ($1, 'src_openapi_test', $2, 'application/yaml', 'public', 'active', $3, $3)", + ) + .bind(ws.as_str()) + .bind(&blob_digest) + .bind(now) + .execute(&raw) + .await + .unwrap(); + + // ... and a plan that does not outlive the job plus its idempotent + // application report ... + let plan = ImportPlanId::new("plan_cleanup"); + let payload = plan_payload(); + let plan_created = now - Duration::minutes(45); + let plan_expires = now - Duration::minutes(15); + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &plan_created, + expires_at: &plan_expires, + }) + .await + .unwrap(); + let report = json!({ + "application_key": "app_cleanup_1", + "created": [ + { "operation_key": "a", "name": "op_a" } + ] + }); + sqlx::query( + "insert into import_plan_applications ( + workspace_id, idempotency_key, plan_id, plan_digest, report, created_at + ) values ($1, 'idem_cleanup', 'plan_cleanup', $2, $3, $4)", + ) + .bind(ws.as_str()) + .bind(&created.digest) + .bind(&report) + .bind(now) + .execute(&raw) + .await + .unwrap(); + + // The job expires (its plan was created while it was still valid). + sqlx::query("update import_jobs set expires_at = $1 where id = $2 and workspace_id = $3") + .bind(now - Duration::minutes(1)) + .bind(job.as_str()) + .bind(ws.as_str()) + .execute(&raw) + .await + .unwrap(); + + let cleanup = registry + .cleanup_expired_import_jobs(8) + .await + .expect("expired-job cleanup must succeed when the job carries a plan"); + assert_eq!(cleanup.deleted_jobs, 1, "the expired job must be removed"); + assert_eq!( + cleanup.detached_sources, 1, + "the job's source must be detached exactly once" + ); + assert!( + !cleanup.more_work, + "a single expired job is the whole workload" + ); + + assert_eq!( + sqlx::query_scalar::<_, i64>("select count(*) from import_jobs") + .fetch_one(&raw) + .await + .unwrap(), + 0, + "the expired job must be gone" + ); + assert_eq!( + sqlx::query_scalar::<_, i64>("select count(*) from import_plans") + .fetch_one(&raw) + .await + .unwrap(), + 0, + "the plan must cascade with its job, not block the cleanup" + ); + assert_eq!( + registry.get_import_job(&ws, &job).await.unwrap(), + None, + "the registry read sees no job after cleanup" + ); + assert_eq!( + registry.get_import_plan(&ws, &plan).await.unwrap(), + None, + "the registry read sees no plan after cleanup" + ); + + // The idempotent application report survives: replay evidence keyed by + // (workspace_id, idempotency_key, plan_digest) with the stored plan_id + // preserved as history. + let anchor = sqlx::query( + "select idempotency_key, plan_id, plan_digest, report + from import_plan_applications where workspace_id = $1", + ) + .bind(ws.as_str()) + .fetch_one(&raw) + .await + .expect("the application report must survive job + plan cleanup"); + assert_eq!( + anchor.try_get::("idempotency_key").unwrap(), + "idem_cleanup" + ); + assert_eq!( + anchor.try_get::("plan_id").unwrap(), + "plan_cleanup" + ); + assert_eq!( + anchor.try_get::("plan_digest").unwrap(), + created.digest + ); + assert_eq!(anchor.try_get::("report").unwrap(), report); + + let source = sqlx::query( + "select lifecycle, detached_at from artifact_sources + where workspace_id = $1 and source_id = 'src_openapi_test'", + ) + .bind(ws.as_str()) + .fetch_one(&raw) + .await + .unwrap(); + assert_eq!( + source.try_get::("lifecycle").unwrap(), + "detached" + ); + assert!( + source + .try_get::, _>("detached_at") + .unwrap() + .is_some(), + "detached_at must be recorded" + ); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/migrations.rs b/crates/crank-registry/tests/integration/migrations.rs index 00a38eb..9be69fa 100644 --- a/crates/crank-registry/tests/integration/migrations.rs +++ b/crates/crank-registry/tests/integration/migrations.rs @@ -2,7 +2,9 @@ use crank_registry::{MigrationAuthority, MigrationPreflight, PostgresRegistry}; use sqlx::Row; mod artifact_metadata; +mod import_plan; mod legacy_adoption; +mod preflight; mod rollback; static EVENT_TRIGGER_TEST_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); @@ -53,6 +55,7 @@ async fn controlled_authority_is_versioned_and_safe_under_concurrent_apply() { (11, "onboarding-product-events-v11"), (12, "artifact-metadata-v12"), (13, "artifact-cleanup-indexes-v13"), + (14, "import-plan-v14"), ]; assert_eq!(rows.len(), expected.len()); for (row, (version, name)) in rows.iter().zip(expected) { @@ -106,9 +109,20 @@ async fn controlled_authority_is_versioned_and_safe_under_concurrent_apply() { .await .expect("V12 artifact metadata relations must be readable"); assert_eq!(artifact_relations.len(), 2); + let import_plan_relations = sqlx::query( + "select table_name + from information_schema.tables + where table_schema = current_schema() + and table_name in ('import_plans', 'import_plan_applications') + order by table_name", + ) + .fetch_all(first.pool()) + .await + .expect("V14 import plan relations must be readable"); + assert_eq!(import_plan_relations.len(), 2); assert_eq!( MigrationAuthority::preflight(first.pool()).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); } #[tokio::test] @@ -232,7 +246,7 @@ async fn ledgerless_legacy_baseline_is_consolidated_without_data_loss() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -345,13 +359,13 @@ async fn healthy_v2_is_reported_as_migration_required_and_upgrades_to_v3() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 2, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); let trace_column: bool = sqlx::query_scalar( "select exists ( @@ -394,7 +408,7 @@ async fn healthy_v3_upgrades_to_v4_with_honest_legacy_snapshot_provenance() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 3, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -454,7 +468,7 @@ async fn healthy_v4_upgrades_to_v5_without_fabricating_legacy_outcomes() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 4, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -708,7 +722,7 @@ async fn v3_upgrade_ignores_oversized_legacy_request_ids_in_partial_index() { MigrationAuthority::apply(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); } async fn remove_v3_schema(pool: &sqlx::PgPool) { @@ -862,7 +876,20 @@ async fn remove_v11_schema(pool: &sqlx::PgPool) { .await .unwrap(); } +async fn remove_v14_schema(pool: &sqlx::PgPool) { + sqlx::raw_sql( + "drop table if exists import_plan_applications; + drop table if exists import_plans; + drop function if exists crank_reject_import_plan_mutation(); + alter table import_jobs drop constraint if exists import_jobs_workspace_id_unique; + delete from __crank_migrations where version = 14;", + ) + .execute(pool) + .await + .unwrap(); +} async fn remove_v12_schema(pool: &sqlx::PgPool) { + remove_v14_schema(pool).await; sqlx::raw_sql( "drop index if exists import_jobs_openapi_source_idx; drop index if exists import_jobs_expires_at_idx; @@ -878,106 +905,3 @@ async fn remove_v12_schema(pool: &sqlx::PgPool) { .await .unwrap(); } -#[tokio::test] -async fn partial_sequence_fails_closed() { - let database_url = crank_test_support::postgres_schema_url("test_partial_sequence").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - MigrationAuthority::apply(&pool).await.unwrap(); - sqlx::query("delete from __crank_migrations where version = 1") - .execute(&pool) - .await - .unwrap(); - assert_eq!( - MigrationAuthority::preflight(&pool) - .await - .unwrap_err() - .code(), - "partial_sequence" - ); -} -#[tokio::test] -async fn missing_relation_with_current_ledger_fails_closed() { - let database_url = crank_test_support::postgres_schema_url("test_missing_relation").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - MigrationAuthority::apply(&pool).await.unwrap(); - sqlx::query("drop table usage_rollups") - .execute(&pool) - .await - .unwrap(); - let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); - assert_eq!(error.code(), "partial_sequence"); - assert_eq!(error.stage(), "preflight.schema"); -} -#[tokio::test] -async fn unregistered_legacy_extension_provenance_is_rejected() { - let database_url = crank_test_support::postgres_schema_url("test_legacy_extension").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - MigrationAuthority::apply(&pool).await.unwrap(); - sqlx::query("drop table __crank_migrations, __crank_migration_legacy_audit") - .execute(&pool) - .await - .unwrap(); - let checksum = "a".repeat(64); - sqlx::query( - "insert into __crank_ext_migrations (extension_name, version, checksum) - values ('known-extension', 1, $1)", - ) - .bind(&checksum) - .execute(&pool) - .await - .unwrap(); - let error = MigrationAuthority::apply(&pool).await.unwrap_err(); - assert_eq!(error.code(), "legacy_conflict"); -} -#[tokio::test] -async fn any_owned_relation_without_core_ledger_is_partial() { - let database_url = crank_test_support::postgres_schema_url("test_owned_partial").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - sqlx::query("create table users (id text primary key)") - .execute(&pool) - .await - .unwrap(); - let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); - assert_eq!(error.code(), "partial_sequence"); - assert_eq!(error.stage(), "preflight.core_missing"); -} -#[tokio::test] -async fn empty_core_ledger_is_reported_separately() { - let database_url = crank_test_support::postgres_schema_url("test_empty_core_ledger").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - MigrationAuthority::apply(&pool).await.unwrap(); - sqlx::query("delete from __crank_core_migrations") - .execute(&pool) - .await - .unwrap(); - let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); - assert_eq!(error.code(), "partial_sequence"); - assert_eq!(error.stage(), "preflight.core_cardinality"); -} -#[tokio::test] -async fn current_ledger_with_structural_drift_fails_closed() { - let database_url = crank_test_support::postgres_schema_url("test_structural_drift").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - MigrationAuthority::apply(&pool).await.unwrap(); - sqlx::query("alter table mcp_transport_sessions drop column supports_elicitation") - .execute(&pool) - .await - .unwrap(); - let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); - assert_eq!(error.code(), "partial_sequence"); - assert_eq!(error.stage(), "preflight.schema"); -} -#[tokio::test] -async fn tampered_legacy_audit_fails_closed() { - let database_url = crank_test_support::postgres_schema_url("test_tampered_audit").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); - MigrationAuthority::apply(&pool).await.unwrap(); - sqlx::query( - "update __crank_migration_legacy_audit set source_checksum = 'tampered' where source = 'core'", - ) - .execute(&pool) - .await - .unwrap(); - let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); - assert_eq!(error.code(), "legacy_conflict"); -} diff --git a/crates/crank-registry/tests/integration/migrations/artifact_metadata.rs b/crates/crank-registry/tests/integration/migrations/artifact_metadata.rs index 6fd67b8..9312aba 100644 --- a/crates/crank-registry/tests/integration/migrations/artifact_metadata.rs +++ b/crates/crank-registry/tests/integration/migrations/artifact_metadata.rs @@ -27,7 +27,7 @@ async fn healthy_v11_upgrades_to_v13_without_rewriting_prior_ledger() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 11, - target: 13, + target: 14, } ); @@ -158,7 +158,7 @@ async fn v12_guard_still_rejects_column_constraint_index_and_relation_drift() { sqlx::raw_sql(restore).execute(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 }, + MigrationPreflight::Current { version: 14 }, "restore: {restore}" ); } @@ -211,7 +211,7 @@ async fn v13_guard_rejects_wrong_cleanup_index_order_and_predicate() { sqlx::raw_sql(restore).execute(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 }, + MigrationPreflight::Current { version: 14 }, "restore: {restore}" ); } diff --git a/crates/crank-registry/tests/integration/migrations/import_plan.rs b/crates/crank-registry/tests/integration/migrations/import_plan.rs new file mode 100644 index 0000000..557276e --- /dev/null +++ b/crates/crank-registry/tests/integration/migrations/import_plan.rs @@ -0,0 +1,459 @@ +use sqlx::Row; + +use super::*; + +fn count_sql(table: &str) -> sqlx::AssertSqlSafe { + sqlx::AssertSqlSafe(format!("select count(*) from {table}")) +} + +async fn count_rows(pool: &sqlx::PgPool, sql: sqlx::AssertSqlSafe) -> i64 { + let value: i64 = sqlx::query_scalar::<_, i64>(sql) + .fetch_one(pool) + .await + .unwrap(); + value +} + +async fn seed_import_plan_fixture(pool: &sqlx::PgPool) { + let now = time::OffsetDateTime::now_utc(); + sqlx::query( + "insert into workspaces (id, slug, display_name, status, settings_json, created_at, updated_at) + values ('ws_import_plan', 'import-plan', 'Import Plan', 'active', '{}'::jsonb, $1, $1)", + ) + .bind(now) + .bind(now) + .execute(pool) + .await + .unwrap(); + sqlx::query( + "insert into import_jobs ( + id, workspace_id, kind, source_format, status, preview_payload, created_at, expires_at + ) values ( + 'job_v14', 'ws_import_plan', 'openapi', 'openapi', 'ready', '{}'::jsonb, $1, $1 + interval '1 hour' + )", + ) + .bind(now) + .bind(now) + .execute(pool) + .await + .unwrap(); + let digest = "ab".repeat(32); + sqlx::query( + "insert into import_plans ( + workspace_id, id, job_id, digest, payload, created_at, expires_at + ) values ( + 'ws_import_plan', 'plan_v14', 'job_v14', $1, '{}'::jsonb, $2, $2 + interval '1 hour' + )", + ) + .bind(&digest) + .bind(now) + .execute(pool) + .await + .unwrap(); + sqlx::query( + "insert into import_plan_applications ( + workspace_id, idempotency_key, plan_id, plan_digest, report, created_at + ) values ( + 'ws_import_plan', 'idem_v14', 'plan_v14', $1, '{}'::jsonb, $2 + )", + ) + .bind(&digest) + .bind(now) + .execute(pool) + .await + .unwrap(); +} + +#[tokio::test] +async fn fresh_install_exposes_import_plan_objects_with_cascading_job_fk() { + let database_url = crank_test_support::postgres_schema_url("test_v14_fresh").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + assert_eq!( + MigrationAuthority::preflight(&pool).await.unwrap(), + MigrationPreflight::Current { version: 14 } + ); + let tables = sqlx::query( + "select table_name + from information_schema.tables + where table_schema = current_schema() + and table_name in ('import_plans', 'import_plan_applications') + order by table_name", + ) + .fetch_all(&pool) + .await + .unwrap(); + let names = tables + .iter() + .map(|row| row.get::("table_name")) + .collect::>(); + assert_eq!(names, vec!["import_plan_applications", "import_plans"]); + // The plan FK to the job is immediate with ON DELETE CASCADE: a plan never + // outlives its job. The applications table keeps only its workspace FK — + // replay evidence must survive plan and job cleanup. + let foreign_keys = sqlx::query( + "select c.conname, c.condeferrable, c.condeferred, c.confdeltype::text as delete_rule + from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and c.contype = 'f' + and c.conname in ( + 'import_plans_job_fk', + 'import_plans_workspace_id_fkey', + 'import_plan_applications_workspace_id_fkey' + ) + order by c.conname", + ) + .fetch_all(&pool) + .await + .unwrap(); + assert_eq!( + foreign_keys.len(), + 3, + "workspace-scoped FKs must all exist and the applications plan FK must be absent" + ); + for row in &foreign_keys { + assert!( + !row.get::("condeferrable"), + "v14 plan FKs must be immediate, not deferrable" + ); + assert!( + !row.get::("condeferred"), + "v14 plan FKs must start immediate" + ); + assert_eq!( + row.get::("delete_rule"), + "c", + "only workspace-cascading FKs may remain" + ); + } + let plan_fk_absent: bool = sqlx::query_scalar( + "select not exists ( + select 1 from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and c.conname = 'import_plan_applications_plan_fk' + )", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert!( + plan_fk_absent, + "replay evidence must not be pinned to the plan row" + ); + let unique: bool = sqlx::query_scalar( + "select exists ( + select 1 from pg_catalog.pg_constraint c + join pg_catalog.pg_class t on t.oid = c.conrelid + join pg_catalog.pg_namespace n on n.oid = t.relnamespace + where n.nspname = current_schema() + and t.relname = 'import_jobs' + and c.conname = 'import_jobs_workspace_id_unique' + and c.contype = 'u' + )", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert!( + unique, + "workspace-scoped import job identity must be unique" + ); +} + +#[tokio::test] +async fn healthy_v13_upgrades_to_v14_without_changing_prior_ledger() { + let database_url = crank_test_support::postgres_schema_url("test_v13_to_v14_import_plan").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + let now = time::OffsetDateTime::now_utc(); + sqlx::query( + "insert into workspaces + (id, slug, display_name, status, settings_json, created_at, updated_at) + values ('ws_v13_data', 'v13-data', 'V13 Data', 'active', '{}'::jsonb, $1, $1)", + ) + .bind(now) + .bind(now) + .execute(&pool) + .await + .unwrap(); + sqlx::query( + "insert into import_jobs ( + id, workspace_id, kind, source_format, status, preview_payload, created_at, expires_at + ) values ( + 'job_v13_data', 'ws_v13_data', 'openapi', 'openapi', 'ready', + '{\"marker\":\"preserved\"}'::jsonb, $1, $1 + interval '1 hour' + )", + ) + .bind(now) + .bind(now) + .execute(&pool) + .await + .unwrap(); + remove_v14_schema(&pool).await; + let job_before: String = sqlx::query_scalar( + "select preview_payload::text || status from import_jobs where id = 'job_v13_data'", + ) + .fetch_one(&pool) + .await + .unwrap(); + let prior = sqlx::query( + "select version, name, checksum, phase, compatibility, applied_at + from __crank_migrations where version <= 13 order by version", + ) + .fetch_all(&pool) + .await + .unwrap() + .into_iter() + .map(|row| { + ( + row.get::("version"), + row.get::("name"), + row.get::("checksum"), + row.get::("phase"), + row.get::("compatibility"), + row.get::("applied_at"), + ) + }) + .collect::>(); + assert_eq!(prior.len(), 13, "the prior ledger must cover v1..v13"); + assert_eq!( + MigrationAuthority::preflight(&pool).await.unwrap(), + MigrationPreflight::MigrationRequired { + current: 13, + target: 14, + } + ); + MigrationAuthority::apply(&pool).await.unwrap(); + let after = sqlx::query( + "select version, name, checksum, phase, compatibility, applied_at + from __crank_migrations where version <= 13 order by version", + ) + .fetch_all(&pool) + .await + .unwrap() + .into_iter() + .map(|row| { + ( + row.get::("version"), + row.get::("name"), + row.get::("checksum"), + row.get::("phase"), + row.get::("compatibility"), + row.get::("applied_at"), + ) + }) + .collect::>(); + assert_eq!( + after, prior, + "prior ledger must survive the v13 -> v14 upgrade" + ); + let job_after: String = sqlx::query_scalar( + "select preview_payload::text || status from import_jobs where id = 'job_v13_data'", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!( + job_after, job_before, + "existing import jobs must be untouched by the upgrade" + ); + let objects: i64 = count_rows( + &pool, + sqlx::AssertSqlSafe(String::from( + "select count(*) from information_schema.tables + where table_schema = current_schema() + and table_name in ('import_plans', 'import_plan_applications')", + )), + ) + .await; + assert_eq!(objects, 2, "both v14 objects must exist after the upgrade"); +} + +#[tokio::test] +async fn wrong_workspace_foreign_key_is_rejected_immediately() { + let database_url = crank_test_support::postgres_schema_url("test_v14_wrong_workspace").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + let now = time::OffsetDateTime::now_utc(); + let mut transaction = pool.begin().await.unwrap(); + for (workspace, label) in [("ws_job_owner", "job-owner"), ("ws_intruder", "intruder")] { + sqlx::query( + "insert into workspaces + (id, slug, display_name, status, settings_json, created_at, updated_at) + values ($1, $2, $2, 'active', '{}'::jsonb, $3, $3)", + ) + .bind(workspace) + .bind(label) + .bind(now) + .execute(&mut *transaction) + .await + .unwrap(); + } + sqlx::query( + "insert into import_jobs ( + id, workspace_id, kind, source_format, status, preview_payload, created_at, expires_at + ) values ( + 'job_owned', 'ws_job_owner', 'openapi', 'openapi', 'ready', + '{}'::jsonb, $1, $1 + interval '1 hour' + )", + ) + .bind(now) + .bind(now) + .execute(&mut *transaction) + .await + .unwrap(); + let digest = "ab".repeat(32); + let error = sqlx::query( + "insert into import_plans ( + workspace_id, id, job_id, digest, payload, created_at, expires_at + ) values ( + 'ws_intruder', 'plan_cross_workspace', 'job_owned', $1, '{}'::jsonb, $2, + $2 + interval '1 hour' + )", + ) + .bind(&digest) + .bind(now) + .execute(&mut *transaction) + .await + .expect_err("a plan may not bind a job from another workspace"); + assert!( + error + .to_string() + .contains("violates foreign key constraint"), + "wrong-workspace reference must fail the job FK: {}", + error + ); + transaction.rollback().await.unwrap(); +} + +#[tokio::test] +async fn update_guards_reject_plan_and_application_mutations() { + let database_url = crank_test_support::postgres_schema_url("test_v14_immutable").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + seed_import_plan_fixture(&pool).await; + for statement in [ + "update import_plans set expires_at = expires_at + interval '1 hour' where id = 'plan_v14'", + "update import_plan_applications + set report = report || jsonb_build_object('tampered', true) + where idempotency_key = 'idem_v14'", + ] { + let error = sqlx::query(statement) + .execute(&pool) + .await + .expect_err("import plan rows must be immutable"); + assert!( + error.to_string().contains("import plan rows are immutable"), + "statement '{}' must hit the immutability guard: {}", + statement, + error + ); + } +} + +#[tokio::test] +async fn deletion_cascades_to_plans_but_preserves_application_anchor() { + let database_url = crank_test_support::postgres_schema_url("test_v14_deletion_retention").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + seed_import_plan_fixture(&pool).await; + + // Deleting the plan succeeds: application rows are replay evidence, not + // dependents. The anchor must keep its full replay data. + let mut transaction = pool.begin().await.unwrap(); + sqlx::query("delete from import_plans where id = 'plan_v14'") + .execute(&mut *transaction) + .await + .unwrap(); + transaction + .commit() + .await + .expect("plan deletion must not be blocked by the application row"); + let application_count = count_rows(&pool, count_sql("import_plan_applications")).await; + assert_eq!( + application_count, 1, + "application must remain as replay evidence after plan deletion" + ); + + // Re-create the plan anchored to its job, then delete the expired job: + // the cascade must take the plan with it while the anchor survives. + let now = time::OffsetDateTime::now_utc(); + let digest = "ab".repeat(32); + sqlx::query( + "insert into import_plans ( + workspace_id, id, job_id, digest, payload, created_at, expires_at + ) values ( + 'ws_import_plan', 'plan_v14', 'job_v14', $1, '{}'::jsonb, $2, $2 + interval '1 hour' + )", + ) + .bind(&digest) + .bind(now) + .execute(&pool) + .await + .unwrap(); + let mut transaction = pool.begin().await.unwrap(); + sqlx::query("delete from import_jobs where id = 'job_v14'") + .execute(&mut *transaction) + .await + .unwrap(); + transaction + .commit() + .await + .expect("job deletion must cascade to its plan, not be blocked by it"); + assert_eq!( + count_rows(&pool, count_sql("import_jobs")).await, + 0, + "the expired job must be gone" + ); + assert_eq!( + count_rows(&pool, count_sql("import_plans")).await, + 0, + "the plan must cascade with its job" + ); + // The anchor row survives both deletions and is still replayable data. + let anchor = sqlx::query( + "select idempotency_key, plan_id, plan_digest, report + from import_plan_applications where workspace_id = 'ws_import_plan'", + ) + .fetch_one(&pool) + .await + .expect("the application anchor must survive job and plan deletion"); + assert_eq!( + anchor.try_get::("idempotency_key").unwrap(), + "idem_v14", + "the idempotency identity must survive for replay" + ); + assert_eq!( + anchor.try_get::("plan_id").unwrap(), + "plan_v14", + "the stored plan_id stays a history string, not an FK" + ); + assert_eq!(anchor.try_get::("plan_digest").unwrap(), digest); + let report = anchor.try_get::("report").unwrap(); + assert!(report.is_object(), "the report must stay valid jsonb"); +} + +#[tokio::test] +async fn workspace_deletion_cascades_to_jobs_plans_and_applications() { + let database_url = crank_test_support::postgres_schema_url("test_v14_workspace_cascade").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + seed_import_plan_fixture(&pool).await; + let mut transaction = pool.begin().await.unwrap(); + sqlx::query("delete from workspaces where id = 'ws_import_plan'") + .execute(&mut *transaction) + .await + .unwrap(); + transaction + .commit() + .await + .expect("workspace deletion must succeed after the cascades"); + for table in ["import_jobs", "import_plans", "import_plan_applications"] { + let count = count_rows(&pool, count_sql(table)).await; + assert_eq!(count, 0, "{table} rows must cascade with the workspace"); + } +} diff --git a/crates/crank-registry/tests/integration/migrations/legacy_adoption.rs b/crates/crank-registry/tests/integration/migrations/legacy_adoption.rs index 390f81f..3d2f980 100644 --- a/crates/crank-registry/tests/integration/migrations/legacy_adoption.rs +++ b/crates/crank-registry/tests/integration/migrations/legacy_adoption.rs @@ -88,7 +88,7 @@ async fn published_ledgerless_baseline_upgrades_without_data_loss() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -101,7 +101,7 @@ async fn published_ledgerless_baseline_upgrades_without_data_loss() { assert_eq!(operation_count, 1); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); } @@ -194,7 +194,7 @@ async fn published_ledgerless_historical_column_layout_upgrades_without_data_los MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -230,7 +230,7 @@ async fn published_ledgerless_historical_column_layout_upgrades_without_data_los ); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); } @@ -262,7 +262,7 @@ async fn published_initial_mcp_layout_is_accepted() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -280,7 +280,7 @@ async fn published_initial_mcp_layout_is_accepted() { assert!(supports_elicitation); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); } @@ -314,13 +314,13 @@ async fn published_in_place_mcp_upgrade_layout_is_accepted() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 14, } ); MigrationAuthority::apply(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 14 } ); } diff --git a/crates/crank-registry/tests/integration/migrations/preflight.rs b/crates/crank-registry/tests/integration/migrations/preflight.rs new file mode 100644 index 0000000..40d8fd6 --- /dev/null +++ b/crates/crank-registry/tests/integration/migrations/preflight.rs @@ -0,0 +1,105 @@ +use super::*; + +#[tokio::test] +async fn partial_sequence_fails_closed() { + let database_url = crank_test_support::postgres_schema_url("test_partial_sequence").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + sqlx::query("delete from __crank_migrations where version = 1") + .execute(&pool) + .await + .unwrap(); + assert_eq!( + MigrationAuthority::preflight(&pool) + .await + .unwrap_err() + .code(), + "partial_sequence" + ); +} +#[tokio::test] +async fn missing_relation_with_current_ledger_fails_closed() { + let database_url = crank_test_support::postgres_schema_url("test_missing_relation").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + sqlx::query("drop table usage_rollups") + .execute(&pool) + .await + .unwrap(); + let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); + assert_eq!(error.code(), "partial_sequence"); + assert_eq!(error.stage(), "preflight.schema"); +} +#[tokio::test] +async fn unregistered_legacy_extension_provenance_is_rejected() { + let database_url = crank_test_support::postgres_schema_url("test_legacy_extension").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + sqlx::query("drop table __crank_migrations, __crank_migration_legacy_audit") + .execute(&pool) + .await + .unwrap(); + let checksum = "a".repeat(64); + sqlx::query( + "insert into __crank_ext_migrations (extension_name, version, checksum) + values ('known-extension', 1, $1)", + ) + .bind(&checksum) + .execute(&pool) + .await + .unwrap(); + let error = MigrationAuthority::apply(&pool).await.unwrap_err(); + assert_eq!(error.code(), "legacy_conflict"); +} +#[tokio::test] +async fn any_owned_relation_without_core_ledger_is_partial() { + let database_url = crank_test_support::postgres_schema_url("test_owned_partial").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + sqlx::query("create table users (id text primary key)") + .execute(&pool) + .await + .unwrap(); + let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); + assert_eq!(error.code(), "partial_sequence"); + assert_eq!(error.stage(), "preflight.core_missing"); +} +#[tokio::test] +async fn empty_core_ledger_is_reported_separately() { + let database_url = crank_test_support::postgres_schema_url("test_empty_core_ledger").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + sqlx::query("delete from __crank_core_migrations") + .execute(&pool) + .await + .unwrap(); + let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); + assert_eq!(error.code(), "partial_sequence"); + assert_eq!(error.stage(), "preflight.core_cardinality"); +} +#[tokio::test] +async fn current_ledger_with_structural_drift_fails_closed() { + let database_url = crank_test_support::postgres_schema_url("test_structural_drift").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + sqlx::query("alter table mcp_transport_sessions drop column supports_elicitation") + .execute(&pool) + .await + .unwrap(); + let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); + assert_eq!(error.code(), "partial_sequence"); + assert_eq!(error.stage(), "preflight.schema"); +} +#[tokio::test] +async fn tampered_legacy_audit_fails_closed() { + let database_url = crank_test_support::postgres_schema_url("test_tampered_audit").await; + let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + MigrationAuthority::apply(&pool).await.unwrap(); + sqlx::query( + "update __crank_migration_legacy_audit set source_checksum = 'tampered' where source = 'core'", + ) + .execute(&pool) + .await + .unwrap(); + let error = MigrationAuthority::preflight(&pool).await.unwrap_err(); + assert_eq!(error.code(), "legacy_conflict"); +} diff --git a/crates/crank-registry/tests/integration/migrations/rollback.rs b/crates/crank-registry/tests/integration/migrations/rollback.rs index c0454c5..d705c78 100644 --- a/crates/crank-registry/tests/integration/migrations/rollback.rs +++ b/crates/crank-registry/tests/integration/migrations/rollback.rs @@ -63,7 +63,7 @@ async fn failed_artifact_metadata_migration_rolls_back_schema_and_ledger() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 11, - target: 13, + target: 14, } ); } diff --git a/docs/openapi-import.md b/docs/openapi-import.md index b4c885e..1f8cd7d 100644 --- a/docs/openapi-import.md +++ b/docs/openapi-import.md @@ -11,20 +11,25 @@ Crank умеет импортировать REST API из OpenAPI 3.x и Swagger 5. Проверьте preview: сгенерированный contract сохраняет parameter locations, body formats и варианты успешного ответа. 6. Выберите нужные группы и методы. 7. Проверьте `Base URL`. Если в документе нет `servers`, укажите URL вручную. -8. Выберите поведение при конфликте имени: - - **Создать копию с новым именем** — Crank добавит суффикс `_2`, `_3` и так далее. - - **Пропустить** — существующие операции не будут изменены. -9. Нажмите **Создать черновики**. +8. Для уже существующей операции подтвердите решение отдельно: пропустить точную операцию, создать отдельную операцию с указанным вами свободным именем или добавить новую черновую версию к выбранной REST-операции. Автоматический подбор имени при применении плана не выполняется. +9. Создайте план и проверьте число выбранных, исключённых, доступных и заблокированных методов. По умолчанию полный план требует, чтобы каждый метод можно было применить; для частичного импорта явно оставьте только нужные методы. +10. Подтвердите план и примените его. Создание плана не меняет операции. Применение выполняет все выбранные изменения одной транзакцией и показывает результат для каждого метода. ## Загрузка и восстановление Браузер отправляет выбранный файл как `multipart/form-data`; границу multipart формирует сам браузер. Файл остаётся только в памяти текущего окна импорта. При замене файла, сбросе, закрытии окна, смене workspace/языка или уходе со страницы текущий запрос отменяется, а поздний ответ игнорируется. При `pagehide` (включая BFCache) выбранный файл и Base URL очищаются: после возврата нужно выбрать файл заново, повтор старой загрузки не предлагается. -Для ошибки можно выбрать файл заново или нажать **Повторить**. Диагностика показывает только ограниченные имя/размер файла и, если backend их вернул, `Request ID`/`Trace ID`; содержимое спецификации, digest, source ID и внутренние пути не выводятся. Повторное нажатие **Создать черновики** во время выполнения не создаёт вторую мутацию. +Для ошибки можно выбрать файл заново или нажать **Повторить**. Диагностика загрузки показывает только ограниченные имя/размер файла и, если backend их вернул, `Request ID`/`Trace ID`; содержимое спецификации и внутренние пути не выводятся. Подтверждение плана показывает счётчики и решения по кандидатам, но не исходный документ и не учётные данные. Digest плана передаётся в Apply автоматически. + +При неизвестном результате Apply, например после разрыва соединения, повторите запрос с теми же ссылкой на план, digest и ключом идемпотентности. Повтор после commit возвращает сохранённый отчёт и не создаёт ещё одну версию; тот же ключ с другим digest даёт конфликт. Пока Apply выполняется, повторное нажатие кнопки не отправляет вторую логическую операцию. + +Если с момента создания плана изменились источник или внешние документы, версия импортера либо состояние выбранной операции, а также конфигурация выбранного Auth Profile или версия его секрета, Apply отклоняет план без частичных изменений и предлагает заново разобрать документ. Отозванный или недоступный профиль также приводит к отказу. Срок жизни плана ограничен сроком исходного preview job. ## Что создается -Для каждого выбранного метода Crank создает отдельную операцию в статусе черновика: +Для каждого нового выбранного метода Crank создает отдельную операцию в статусе черновика. Для `new_version` новая Draft Version добавляется к точно выбранной операции; текущая опубликованная версия, прежние версии, привязки и история остаются прежними. `skip` не меняет выбранную существующую операцию. Исключённые методы не создаются, но перечисляются в отчёте. + +Контракт нового черновика: - имя инструмента берется из `operationId`, а если его нет — строится из HTTP-метода и пути; - отображаемое имя берется из `summary`; diff --git a/docs/schemas/metrics-registry-v1.json b/docs/schemas/metrics-registry-v1.json index 05c91da..a2d597b 100644 --- a/docs/schemas/metrics-registry-v1.json +++ b/docs/schemas/metrics-registry-v1.json @@ -1,14 +1,14 @@ { "schema_version": 1, - "budget": {"logical_series": 5213, "rendered_series": 16538, "max_logical_series": 5250, "max_rendered_series": 16750}, + "budget": {"logical_series": 5353, "rendered_series": 16978, "max_logical_series": 5400, "max_rendered_series": 17000}, "global_labels": [ {"name": "service", "class": "process_constant", "domain": ["admin-api", "mcp-server"]}, {"name": "version", "class": "process_constant", "domain": ["validated_release_identity"]}, {"name": "environment", "class": "process_constant", "domain": ["validated_startup_value"]} ], "metrics": [ - {"name": "crank_http_requests_total", "kind": "counter", "unit": "count", "help": "Total HTTP requests.", "processes": ["admin_api", "mcp_server"], "labels": [{"name": "route", "class": "product_closed", "domain": ["unmatched", "/health", "/ready", "/api/auth/login", "/api/auth/bootstrap/status", "/api/auth/bootstrap/complete", "/api/auth/logout", "/api/auth/session", "/api/auth/session/csrf", "/api/auth/profile", "/api/auth/password", "/api/admin/capabilities", "/api/admin/workspaces", "/api/admin/workspaces/{workspace_id}", "/api/admin/workspaces/{workspace_id}/onboarding", "/api/admin/workspaces/{workspace_id}/onboarding/events", "/api/admin/workspaces/{workspace_id}/onboarding/reset-selection", "/api/admin/workspaces/{workspace_id}/operations", "/api/admin/workspaces/{workspace_id}/imports/openapi/preview", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/create", "/api/admin/workspaces/{workspace_id}/operations/analyze-quality", "/api/admin/workspaces/{workspace_id}/operations/import", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/publish", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/archive", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/test-runs", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/input-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/output-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/drafts/generate", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/export", "/api/admin/workspaces/{workspace_id}/agents", "/api/admin/workspaces/{workspace_id}/agents/tool-search/preview", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/bindings", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/publish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/unpublish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/archive", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}/revoke", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}", "/api/admin/workspaces/{workspace_id}/auth-profiles", "/api/admin/workspaces/{workspace_id}/auth-profiles/{auth_profile_id}", "/api/admin/workspaces/{workspace_id}/upstreams", "/api/admin/workspaces/{workspace_id}/upstreams/{upstream_id}", "/api/admin/workspaces/{workspace_id}/secrets", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}/rotate", "/api/admin/workspaces/{workspace_id}/export", "/api/admin/workspaces/{workspace_id}/logs", "/api/admin/workspaces/{workspace_id}/logs/export.csv", "/api/admin/workspaces/{workspace_id}/logs/{log_id}", "/api/admin/workspaces/{workspace_id}/approvals", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/approve", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/deny", "/api/admin/workspaces/{workspace_id}/usage", "/api/admin/workspaces/{workspace_id}/usage/export.csv", "/api/admin/workspaces/{workspace_id}/usage/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/usage/agents/{agent_id}", "/v1/{workspace_slug}/{agent_slug}", "/v1/{workspace_slug}/{agent_slug}/approvals", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/approve", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/deny"]},{"name": "method", "class": "product_closed", "domain": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD", "CONNECT", "TRACE", "OTHER"]},{"name": "status_class", "class": "product_closed", "domain": ["1xx", "2xx", "3xx", "4xx", "5xx", "other"]}], "buckets_seconds": [], "max_logical_series": 4020, "max_rendered_series": 4020}, - {"name": "crank_http_request_duration_seconds", "kind": "histogram", "unit": "seconds", "help": "HTTP request duration in seconds.", "processes": ["admin_api", "mcp_server"], "labels": [{"name": "route", "class": "product_closed", "domain": ["unmatched", "/health", "/ready", "/api/auth/login", "/api/auth/bootstrap/status", "/api/auth/bootstrap/complete", "/api/auth/logout", "/api/auth/session", "/api/auth/session/csrf", "/api/auth/profile", "/api/auth/password", "/api/admin/capabilities", "/api/admin/workspaces", "/api/admin/workspaces/{workspace_id}", "/api/admin/workspaces/{workspace_id}/onboarding", "/api/admin/workspaces/{workspace_id}/onboarding/events", "/api/admin/workspaces/{workspace_id}/onboarding/reset-selection", "/api/admin/workspaces/{workspace_id}/operations", "/api/admin/workspaces/{workspace_id}/imports/openapi/preview", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/create", "/api/admin/workspaces/{workspace_id}/operations/analyze-quality", "/api/admin/workspaces/{workspace_id}/operations/import", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/publish", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/archive", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/test-runs", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/input-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/output-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/drafts/generate", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/export", "/api/admin/workspaces/{workspace_id}/agents", "/api/admin/workspaces/{workspace_id}/agents/tool-search/preview", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/bindings", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/publish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/unpublish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/archive", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}/revoke", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}", "/api/admin/workspaces/{workspace_id}/auth-profiles", "/api/admin/workspaces/{workspace_id}/auth-profiles/{auth_profile_id}", "/api/admin/workspaces/{workspace_id}/upstreams", "/api/admin/workspaces/{workspace_id}/upstreams/{upstream_id}", "/api/admin/workspaces/{workspace_id}/secrets", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}/rotate", "/api/admin/workspaces/{workspace_id}/export", "/api/admin/workspaces/{workspace_id}/logs", "/api/admin/workspaces/{workspace_id}/logs/export.csv", "/api/admin/workspaces/{workspace_id}/logs/{log_id}", "/api/admin/workspaces/{workspace_id}/approvals", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/approve", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/deny", "/api/admin/workspaces/{workspace_id}/usage", "/api/admin/workspaces/{workspace_id}/usage/export.csv", "/api/admin/workspaces/{workspace_id}/usage/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/usage/agents/{agent_id}", "/v1/{workspace_slug}/{agent_slug}", "/v1/{workspace_slug}/{agent_slug}/approvals", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/approve", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/deny"]},{"name": "method", "class": "product_closed", "domain": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD", "CONNECT", "TRACE", "OTHER"]}], "buckets_seconds": [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60], "max_logical_series": 670, "max_rendered_series": 10720}, + {"name": "crank_http_requests_total", "kind": "counter", "unit": "count", "help": "Total HTTP requests.", "processes": ["admin_api", "mcp_server"], "labels": [{"name": "route", "class": "product_closed", "domain": ["unmatched", "/health", "/ready", "/api/auth/login", "/api/auth/bootstrap/status", "/api/auth/bootstrap/complete", "/api/auth/logout", "/api/auth/session", "/api/auth/session/csrf", "/api/auth/profile", "/api/auth/password", "/api/admin/capabilities", "/api/admin/workspaces", "/api/admin/workspaces/{workspace_id}", "/api/admin/workspaces/{workspace_id}/onboarding", "/api/admin/workspaces/{workspace_id}/onboarding/events", "/api/admin/workspaces/{workspace_id}/onboarding/reset-selection", "/api/admin/workspaces/{workspace_id}/operations", "/api/admin/workspaces/{workspace_id}/imports/openapi/preview", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/create", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/plans", "/api/admin/workspaces/{workspace_id}/imports/openapi/{plan_id}/apply", "/api/admin/workspaces/{workspace_id}/operations/analyze-quality", "/api/admin/workspaces/{workspace_id}/operations/import", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/publish", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/archive", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/test-runs", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/input-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/output-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/drafts/generate", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/export", "/api/admin/workspaces/{workspace_id}/agents", "/api/admin/workspaces/{workspace_id}/agents/tool-search/preview", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/bindings", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/publish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/unpublish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/archive", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}/revoke", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}", "/api/admin/workspaces/{workspace_id}/auth-profiles", "/api/admin/workspaces/{workspace_id}/auth-profiles/{auth_profile_id}", "/api/admin/workspaces/{workspace_id}/upstreams", "/api/admin/workspaces/{workspace_id}/upstreams/{upstream_id}", "/api/admin/workspaces/{workspace_id}/secrets", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}/rotate", "/api/admin/workspaces/{workspace_id}/export", "/api/admin/workspaces/{workspace_id}/logs", "/api/admin/workspaces/{workspace_id}/logs/export.csv", "/api/admin/workspaces/{workspace_id}/logs/{log_id}", "/api/admin/workspaces/{workspace_id}/approvals", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/approve", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/deny", "/api/admin/workspaces/{workspace_id}/usage", "/api/admin/workspaces/{workspace_id}/usage/export.csv", "/api/admin/workspaces/{workspace_id}/usage/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/usage/agents/{agent_id}", "/v1/{workspace_slug}/{agent_slug}", "/v1/{workspace_slug}/{agent_slug}/approvals", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/approve", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/deny"]},{"name": "method", "class": "product_closed", "domain": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD", "CONNECT", "TRACE", "OTHER"]},{"name": "status_class", "class": "product_closed", "domain": ["1xx", "2xx", "3xx", "4xx", "5xx", "other"]}], "buckets_seconds": [], "max_logical_series": 4140, "max_rendered_series": 4140}, + {"name": "crank_http_request_duration_seconds", "kind": "histogram", "unit": "seconds", "help": "HTTP request duration in seconds.", "processes": ["admin_api", "mcp_server"], "labels": [{"name": "route", "class": "product_closed", "domain": ["unmatched", "/health", "/ready", "/api/auth/login", "/api/auth/bootstrap/status", "/api/auth/bootstrap/complete", "/api/auth/logout", "/api/auth/session", "/api/auth/session/csrf", "/api/auth/profile", "/api/auth/password", "/api/admin/capabilities", "/api/admin/workspaces", "/api/admin/workspaces/{workspace_id}", "/api/admin/workspaces/{workspace_id}/onboarding", "/api/admin/workspaces/{workspace_id}/onboarding/events", "/api/admin/workspaces/{workspace_id}/onboarding/reset-selection", "/api/admin/workspaces/{workspace_id}/operations", "/api/admin/workspaces/{workspace_id}/imports/openapi/preview", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/create", "/api/admin/workspaces/{workspace_id}/imports/openapi/{job_id}/plans", "/api/admin/workspaces/{workspace_id}/imports/openapi/{plan_id}/apply", "/api/admin/workspaces/{workspace_id}/operations/analyze-quality", "/api/admin/workspaces/{workspace_id}/operations/import", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/publish", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/archive", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/test-runs", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/input-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/samples/output-json", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/drafts/generate", "/api/admin/workspaces/{workspace_id}/operations/{operation_id}/export", "/api/admin/workspaces/{workspace_id}/agents", "/api/admin/workspaces/{workspace_id}/agents/tool-search/preview", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/versions/{version}", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/bindings", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/publish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/unpublish", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/archive", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}/revoke", "/api/admin/workspaces/{workspace_id}/agents/{agent_id}/platform-api-keys/{key_id}", "/api/admin/workspaces/{workspace_id}/auth-profiles", "/api/admin/workspaces/{workspace_id}/auth-profiles/{auth_profile_id}", "/api/admin/workspaces/{workspace_id}/upstreams", "/api/admin/workspaces/{workspace_id}/upstreams/{upstream_id}", "/api/admin/workspaces/{workspace_id}/secrets", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}", "/api/admin/workspaces/{workspace_id}/secrets/{secret_id}/rotate", "/api/admin/workspaces/{workspace_id}/export", "/api/admin/workspaces/{workspace_id}/logs", "/api/admin/workspaces/{workspace_id}/logs/export.csv", "/api/admin/workspaces/{workspace_id}/logs/{log_id}", "/api/admin/workspaces/{workspace_id}/approvals", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/approve", "/api/admin/workspaces/{workspace_id}/approvals/{approval_id}/deny", "/api/admin/workspaces/{workspace_id}/usage", "/api/admin/workspaces/{workspace_id}/usage/export.csv", "/api/admin/workspaces/{workspace_id}/usage/operations/{operation_id}", "/api/admin/workspaces/{workspace_id}/usage/agents/{agent_id}", "/v1/{workspace_slug}/{agent_slug}", "/v1/{workspace_slug}/{agent_slug}/approvals", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/approve", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}", "/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/deny"]},{"name": "method", "class": "product_closed", "domain": ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD", "CONNECT", "TRACE", "OTHER"]}], "buckets_seconds": [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60], "max_logical_series": 690, "max_rendered_series": 11040}, {"name": "crank_http_inflight", "kind": "gauge", "unit": "count", "help": "HTTP requests currently being processed.", "processes": ["admin_api", "mcp_server"], "labels": [], "buckets_seconds": [], "max_logical_series": 1, "max_rendered_series": 1}, {"name": "crank_mcp_requests_total", "kind": "counter", "unit": "count", "help": "Total MCP JSON-RPC requests.", "processes": ["mcp_server"], "labels": [{"name": "method", "class": "product_closed", "domain": ["initialize", "initialized", "ping", "tools_list", "tools_call", "notification", "unsupported", "response", "invalid"]},{"name": "response_mode", "class": "product_closed", "domain": ["json", "sse", "unknown"]},{"name": "outcome", "class": "product_closed", "domain": ["success", "client_error", "server_error", "jsonrpc_error", "tool_error", "aborted", "other"]}], "buckets_seconds": [], "max_logical_series": 189, "max_rendered_series": 189}, {"name": "crank_mcp_request_duration_seconds", "kind": "histogram", "unit": "seconds", "help": "MCP JSON-RPC request duration in seconds.", "processes": ["mcp_server"], "labels": [{"name": "method", "class": "product_closed", "domain": ["initialize", "initialized", "ping", "tools_list", "tools_call", "notification", "unsupported", "response", "invalid"]},{"name": "outcome", "class": "product_closed", "domain": ["success", "client_error", "server_error", "jsonrpc_error", "tool_error", "aborted", "other"]}], "buckets_seconds": [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60], "max_logical_series": 63, "max_rendered_series": 1008}, diff --git a/docs/schemas/migration-sequence.json b/docs/schemas/migration-sequence.json index b7aa7ae..562ebb5 100644 --- a/docs/schemas/migration-sequence.json +++ b/docs/schemas/migration-sequence.json @@ -208,6 +208,22 @@ "source_digest": "abe822c967e1b2cc3966ede988e05eb2d9b2a062b539c45aaa8cad677395ab05", "transactional": true, "version": 13 + }, + { + "backfill": { + "kind": "none" + }, + "checksum": "176c5b3cd12ddaf78dd0d312626a0c387f9c240a765194b34e88f7e0d3b4ca2a", + "compatibility": "n-minus-one-readable", + "contract_evidence": null, + "name": "import-plan-v14", + "owner": "crank-registry", + "phase": "expand", + "readable_schema_max": 14, + "readable_schema_min": 13, + "source_digest": "176c5b3cd12ddaf78dd0d312626a0c387f9c240a765194b34e88f7e0d3b4ca2a", + "transactional": true, + "version": 14 } ] }