diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 843eb1f..79d814a 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -21,6 +21,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v5 + with: + fetch-depth: 0 - name: Install Rust toolchain run: | @@ -109,6 +111,31 @@ jobs: - name: Check Rust boundaries run: scripts/check-rust-boundaries.sh + - name: Run offline OpenAPI support corpus gate + run: cargo test -p crank-import --test corpus -- --test-threads=1 + + - name: Check OpenAPI matrix and corpus compatibility versions + env: + MATRIX_BASE_REF: ${{ gitea.base_ref }} + MATRIX_EVENT_BEFORE: ${{ gitea.event.before }} + run: | + if [ -n "$MATRIX_BASE_REF" ]; then + base_ref="origin/$MATRIX_BASE_REF" + elif [ -n "$MATRIX_EVENT_BEFORE" ] && ! printf '%s' "$MATRIX_EVENT_BEFORE" | grep -Eq '^0+$'; then + base_ref="$MATRIX_EVENT_BEFORE" + else + base_ref="$(git merge-base HEAD origin/main)" + fi + python3 scripts/check-openapi-matrix-compatibility.py --base "$base_ref" + + - name: Save OpenAPI corpus evidence + if: always() + uses: actions/upload-artifact@v4 + with: + name: openapi-corpus-evidence + path: crates/crank-import/target/openapi-corpus/evidence-*.json + if-no-files-found: warn + - name: Run clippy run: cargo clippy --workspace --all-targets --all-features --jobs "$CARGO_BUILD_JOBS" -- -D warnings diff --git a/Cargo.lock b/Cargo.lock index e15f9d5..644e866 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -766,6 +766,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", + "sha2 0.10.9", "thiserror 2.0.18", "url", ] diff --git a/apps/admin-api/src/app.rs b/apps/admin-api/src/app.rs index d54bc84..ac26abd 100644 --- a/apps/admin-api/src/app.rs +++ b/apps/admin-api/src/app.rs @@ -27,12 +27,18 @@ 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, revalidate_openapi_import_plan, + }, 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, }, - onboarding::{get_onboarding, record_onboarding_event, reset_onboarding_selection}, + onboarding::{ + get_onboarding, record_onboarding_event, register_onboarding_import_context, + reset_onboarding_selection, select_onboarding_import_draft, + }, operations::{ analyze_operation_quality, archive_operation, create_operation, create_version, delete_operation, export_operation, generate_draft, get_operation, @@ -58,6 +64,18 @@ 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).layer(DefaultBodyLimit::max(16 * 1024 * 1024)), + ) + .route( + "/imports/openapi/plans/{plan_id}", + get(revalidate_openapi_import_plan), + ) + .route( + "/imports/openapi/{plan_id}/apply", + post(apply_openapi_import_plan).layer(DefaultBodyLimit::max(16 * 1024)), + ) .route( "/operations/analyze-quality", post(analyze_operation_quality), @@ -158,6 +176,14 @@ pub fn build_app(state: AppState) -> Router { .route( "/onboarding/reset-selection", post(reset_onboarding_selection), + ) + .route( + "/onboarding/import-context", + post(register_onboarding_import_context).layer(DefaultBodyLimit::max(16 * 1024)), + ) + .route( + "/onboarding/select-draft", + post(select_onboarding_import_draft).layer(DefaultBodyLimit::max(16 * 1024)), ); let workspace_root_router = Router::new() diff --git a/apps/admin-api/src/bin/crank-migrate.rs b/apps/admin-api/src/bin/crank-migrate.rs index 49e1dab..8ace5e4 100644 --- a/apps/admin-api/src/bin/crank-migrate.rs +++ b/apps/admin-api/src/bin/crank-migrate.rs @@ -186,6 +186,19 @@ async fn run() -> Result { }) .collect::>(); let plan = json!({ "schema_version": 1, "sequence": sequence }); + if option == Some("--target-version") { + let target_version = plan["sequence"] + .as_array() + .and_then(|sequence| sequence.last()) + .and_then(|migration| migration["version"].as_i64()) + .ok_or(CliError::new( + "migration_sequence_invalid", + "plan.target_version", + "contact_operator", + ))?; + println!("{target_version}"); + return Ok(ExitCode::SUCCESS); + } if option == Some("--check") { let bytes = std::fs::read("docs/schemas/migration-sequence.json") .map_err(|_| CliError::new("contract_drift", "plan.read", "contact_operator"))?; diff --git a/apps/admin-api/src/dto.rs b/apps/admin-api/src/dto.rs index 7237796..c968784 100644 --- a/apps/admin-api/src/dto.rs +++ b/apps/admin-api/src/dto.rs @@ -353,6 +353,35 @@ pub struct OnboardingResponse { pub platform_api_key_id: Option, pub mcp_endpoint: Option, pub first_call: Option, + pub import_context: Option, +} + +#[derive(Clone, Debug, Serialize)] +pub struct OnboardingImportChoiceView { + pub key: String, + pub name: String, + pub outcome: String, + pub operation_id: Option, + pub version: Option, + pub reason: Option, + pub selectable: bool, +} + +#[derive(Clone, Debug, Serialize)] +pub struct OnboardingImportDriftView { + pub reason: String, + pub current_operation_version: Option, +} + +#[derive(Clone, Debug, Serialize)] +pub struct OnboardingImportContextView { + pub application_idempotency_key: String, + pub plan_digest: String, + pub report: Value, + pub choices: Vec, + pub selected_operation_id: Option, + pub selected_operation_version: Option, + pub drift: Option, } #[derive(Clone, Debug, Serialize)] @@ -375,6 +404,38 @@ pub struct ResetOnboardingSelectionResponse { pub onboarding: OnboardingResponse, } +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct RegisterOnboardingImportContextPayload { + pub application_idempotency_key: String, + pub plan_digest: String, + pub expected_revision: i64, +} + +#[derive(Clone, Debug, Serialize)] +pub struct RegisterOnboardingImportContextResponse { + pub registered: bool, + #[serde(flatten)] + pub onboarding: OnboardingResponse, +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SelectOnboardingImportDraftPayload { + pub application_idempotency_key: String, + pub plan_digest: String, + pub operation_id: String, + pub operation_version: u32, + pub expected_revision: i64, +} + +#[derive(Clone, Debug, Serialize)] +pub struct SelectOnboardingImportDraftResponse { + pub selected: bool, + #[serde(flatten)] + pub onboarding: OnboardingResponse, +} + #[derive(Clone, Debug, Serialize)] pub struct WorkspaceCatalogSnapshotResponse { pub kind: String, @@ -542,48 +603,34 @@ pub struct OpenApiImportPreviewResponse { pub job_id: String, pub expires_at: String, pub preview: crank_import::rest::ImportPreview, - /// Compatible Auth Profiles visible to the current workspace, grouped by - /// operation and security alternative. Profile credentials are never - /// included. - pub auth_profile_choices: Vec, + #[serde(default)] + pub security_profile_choices: Vec, } #[derive(Clone, Debug, Serialize)] -pub struct OpenApiAuthProfileChoice { +pub struct OpenApiImportSecurityProfileChoice { pub operation_key: String, pub alternative_index: usize, - pub scheme_names: Vec, - pub supported: bool, - pub profiles: Vec, + pub schemes: Vec, + pub profiles: Vec, } #[derive(Clone, Debug, Serialize)] -pub struct OpenApiAuthProfileOption { +pub struct OpenApiImportAuthProfileChoice { pub id: String, pub name: String, - pub kind: AuthKind, + pub kind: crank_core::AuthKind, } #[derive(Clone, Debug, Deserialize)] pub struct OpenApiImportCreatePayload { pub selected_operation_keys: Vec, #[serde(default)] - pub auth_mappings: Vec, - #[serde(default)] pub server_url: Option, #[serde(default = "default_openapi_conflict_mode")] pub conflict_mode: String, } -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct OpenApiAuthMapping { - pub operation_key: String, - pub alternative_index: usize, - /// `None` is an explicit unresolved mapping. It is never actionable for a - /// required authentication alternative. - pub auth_profile_id: Option, -} - #[derive(Clone, Debug, Serialize)] pub struct OpenApiImportCreateResponse { pub created: Vec, @@ -606,6 +653,89 @@ 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, + #[serde(default)] + pub security_alternative_index: 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 security_alternative_index: Option, + pub reason: Option, +} + +#[derive(Clone, Debug, Serialize)] +pub struct OpenApiImportPlanCreateResponse { + pub plan_reference: String, + pub plan_digest: String, + pub catalog_revision: String, + pub support_matrix_version: String, + pub expires_at: String, + pub selection_mode: String, + pub counts: OpenApiImportPlanCounts, + pub candidates: Vec, +} + +#[derive(Clone, Debug, Serialize)] +pub struct OpenApiImportPlanRevalidationResponse { + pub plan_reference: String, + pub plan_digest: String, + pub revalidation_status: String, + pub revalidation_required: bool, + pub reason: Option, + 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 12e3b32..bce7ae9 100644 --- a/apps/admin-api/src/error.rs +++ b/apps/admin-api/src/error.rs @@ -135,41 +135,6 @@ impl ApiError { "OpenAPI document is invalid or unsupported" } } - (_, "literal_credentials_rejected") => { - if russian { - "документ OpenAPI содержит literal credential и отклонён" - } else { - "OpenAPI document contains literal credential material and was rejected" - } - } - (_, "auth_mapping_required") => { - if russian { - "для требования аутентификации выберите доступный профиль" - } else { - "an available Auth Profile mapping is required for this authentication requirement" - } - } - (_, "auth_mapping_unsupported") => { - if russian { - "это требование аутентификации не поддерживается импортом" - } else { - "this authentication requirement is not supported by import" - } - } - (_, "auth_mapping_invalid") => { - if russian { - "привязка аутентификации в запросе импорта некорректна" - } else { - "OpenAPI import authentication mapping is invalid" - } - } - (_, "auth_binding_unavailable") => { - if russian { - "выбранный профиль аутентификации недоступен для этого импорта" - } else { - "selected Auth Profile is unavailable for this import" - } - } (_, "no_methods") => { if russian { "документ OpenAPI не содержит поддерживаемых методов" @@ -230,18 +195,6 @@ impl ApiError { } } - pub(crate) fn openapi_auth_binding_unavailable(locale: OpenApiUploadLocale) -> Self { - let message = if locale == OpenApiUploadLocale::Ru { - "выбранный профиль аутентификации недоступен для этого импорта" - } else { - "selected Auth Profile is unavailable for this import" - }; - Self::unprocessable_with_context( - message, - json!({ "error_code": "openapi_auth_binding_unavailable" }), - ) - } - pub(crate) fn rate_limited_with_context(message: impl Into, context: Value) -> Self { Self::RateLimited { message: message.into(), @@ -711,6 +664,29 @@ impl From for ApiError { "onboarding domain steps are not complete", json!({"error_code":"onboarding_incomplete"}), ), + RegistryError::OnboardingImportDraftUnavailable => Self::unprocessable_with_context( + "the requested operation is not an actionable imported Draft", + json!({ + "error_code":"onboarding_import_draft_unavailable", + "recovery":"reload_import_report" + }), + ), + RegistryError::OnboardingImportApplicationUnavailable => { + Self::unprocessable_with_context( + "no committed import apply report matches the requested application", + json!({ + "error_code":"onboarding_import_application_unavailable", + "recovery":"reload_import_report" + }), + ) + } + RegistryError::OnboardingImportContextUnavailable => Self::unprocessable_with_context( + "the workspace has no durable import context for the requested application", + json!({ + "error_code":"onboarding_import_context_unavailable", + "recovery":"register_import_context" + }), + ), RegistryError::InvalidEnumRepresentation { field } => Self::validation_with_context( format!("unsupported enum representation for field {field}"), json!({ "field": field }), @@ -722,10 +698,6 @@ impl From for ApiError { "value": value, }), ), - RegistryError::ImportAuthBindingUnavailable => Self::unprocessable_with_context( - "selected Auth Profile is unavailable for this import", - json!({ "error_code": "openapi_auth_binding_unavailable" }), - ), RegistryError::YamlImportJobNotFound { job_id } => Self::validation_with_context( format!("yaml import job {job_id} was not found"), json!({ "job_id": job_id }), @@ -738,6 +710,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..edca0fe 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,69 @@ pub async fn create_openapi_import( Ok(Json(json!(imported))) } +pub async fn create_openapi_import_plan( + Path(path): Path, + State(state): State, + headers: HeaderMap, + Json(payload): Json, +) -> Result, ApiError> { + let plan = state + .service + .create_openapi_import_plan_with_locale( + &path.workspace_id.as_str().into(), + &path.job_id.as_str().into(), + payload, + openapi_upload_locale(&headers), + ) + .await?; + Ok(Json(json!(plan))) +} + +pub async fn revalidate_openapi_import_plan( + Path(path): Path, + State(state): State, + headers: HeaderMap, +) -> Result, ApiError> { + let plan = state + .service + .revalidate_openapi_import_plan_with_locale( + &path.workspace_id.as_str().into(), + &path.plan_id.as_str().into(), + openapi_upload_locale(&headers), + ) + .await?; + Ok(Json(json!(plan))) +} + +pub async fn apply_openapi_import_plan( + Path(path): Path, + State(state): State, + headers: HeaderMap, + Json(payload): Json, +) -> Result { + let report = state + .service + .apply_openapi_import_plan_with_locale( + &path.workspace_id.as_str().into(), + &path.plan_id.as_str().into(), + payload, + openapi_upload_locale(&headers), + ) + .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/routes/onboarding.rs b/apps/admin-api/src/routes/onboarding.rs index e325722..ba76195 100644 --- a/apps/admin-api/src/routes/onboarding.rs +++ b/apps/admin-api/src/routes/onboarding.rs @@ -7,7 +7,9 @@ use crate::{ error::ApiError, service::{ OnboardingEventPayload, OnboardingEventResponse, OnboardingResponse, + RegisterOnboardingImportContextPayload, RegisterOnboardingImportContextResponse, ResetOnboardingSelectionPayload, ResetOnboardingSelectionResponse, + SelectOnboardingImportDraftPayload, SelectOnboardingImportDraftResponse, }, state::AppState, }; @@ -57,3 +59,29 @@ pub async fn record_onboarding_event( .await?, )) } + +pub async fn register_onboarding_import_context( + Path(path): Path, + State(state): State, + Json(payload): Json, +) -> Result, ApiError> { + Ok(Json( + state + .service + .register_onboarding_import_context(&path.workspace_id.as_str().into(), payload) + .await?, + )) +} + +pub async fn select_onboarding_import_draft( + Path(path): Path, + State(state): State, + Json(payload): Json, +) -> Result, ApiError> { + Ok(Json( + state + .service + .select_onboarding_import_draft(&path.workspace_id.as_str().into(), payload) + .await?, + )) +} diff --git a/apps/admin-api/src/service/imports.rs b/apps/admin-api/src/service/imports.rs index 95e33d2..a7d74ad 100644 --- a/apps/admin-api/src/service/imports.rs +++ b/apps/admin-api/src/service/imports.rs @@ -2,20 +2,22 @@ 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, + SecretStatus, ToolQualityFinding, ToolQualitySeverity, WorkspaceId, }; use crank_import::rest::{ - ImportFinding, ImportFindingSeverity, ImportOperationCandidate, NORMALIZER_VERSION, - PROJECTION_VERSION, operation_draft_from_candidate, + ImportFinding, ImportFindingSeverity, ImportNameConflict, ImportOperationCandidate, + NORMALIZER_VERSION, PROJECTION_VERSION, operation_draft_from_candidate, + validate_document_source_safe, }; use crank_registry::{ ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity, - CreateArtifactSourceRequest, CreateImportJobRequest, FinishImportJobRequest, - ImportAuthRequirement, 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}; @@ -23,26 +25,170 @@ use tracing::{info, instrument}; use crate::{ error::ApiError, service::{ - AdminService, OpenApiAuthMapping, OpenApiImportCreatePayload, OpenApiImportCreateResponse, - OpenApiImportCreatedOperation, OpenApiImportPreviewResponse, OpenApiImportSkippedOperation, - OpenApiUpload, OpenApiUploadLocale, OperationPayload, new_prefixed_id, + AdminService, OpenApiImportAuthProfileChoice, OpenApiImportCreatePayload, + OpenApiImportCreateResponse, OpenApiImportCreatedOperation, OpenApiImportPlanApplyPayload, + OpenApiImportPlanCandidateView, OpenApiImportPlanCounts, OpenApiImportPlanCreatePayload, + OpenApiImportPlanCreateResponse, OpenApiImportPlanDecision, + OpenApiImportPlanRevalidationResponse, OpenApiImportPreviewResponse, + OpenApiImportSecurityProfileChoice, OpenApiImportSkippedOperation, OpenApiUpload, + OpenApiUploadLocale, OperationPayload, new_prefixed_id, }, }; mod external_references; +mod import_plan; mod job_contract; -mod security; +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 = 2_000; +const OPENAPI_SUPPORT_MATRIX_VERSION: &str = "1.0.3"; +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); +fn preview_name_conflicts( + preview: &crank_import::rest::ImportPreview, + catalog_names: &[String], +) -> Vec { + let operations = preview + .groups + .iter() + .flat_map(|group| group.operations.iter()) + .collect::>(); + let mut operation_ids = BTreeMap::<&str, usize>::new(); + for candidate in &operations { + if let Some(id) = candidate.operation_id.as_deref() { + *operation_ids.entry(id).or_default() += 1; + } + } + let occupied = catalog_names + .iter() + .map(String::as_str) + .collect::>(); + let mut proposals = catalog_names.iter().cloned().collect::>(); + proposals.extend( + operations + .iter() + .map(|candidate| candidate.suggested_name.clone()), + ); + let mut conflicts = Vec::new(); + for candidate in operations { + let duplicate = candidate + .operation_id + .as_deref() + .is_some_and(|id| operation_ids.get(id).is_some_and(|count| *count > 1)); + let invalid = candidate.operation_id.as_deref().is_some_and(|id| { + id.is_empty() + || id.len() > MAX_IMPORT_PLAN_NAME_BYTES + || !id.bytes().enumerate().all(|(index, ch)| { + ch.is_ascii_alphabetic() || ch == b'_' || (index > 0 && ch.is_ascii_digit()) + }) + }); + let taken = occupied.contains(candidate.suggested_name.as_str()); + if !duplicate && !invalid && !taken { + continue; + } + let base = candidate + .suggested_name + .chars() + .take(240) + .collect::(); + let proposal = next_available_name(&base, &mut proposals); + for conflict_type in [ + duplicate.then_some("duplicate_name"), + invalid.then_some("invalid_name"), + taken.then_some("taken_name"), + ] + .into_iter() + .flatten() + { + conflicts.push(ImportNameConflict { + operation_key: candidate.key.clone(), + conflict_type: conflict_type.to_owned(), + proposed_name: proposal.clone(), + source_location: candidate.source_location.as_ref().map(|location| { + crank_import::rest::SourceLocation { + pointer: if candidate.operation_id.is_some() { + format!("{}/operationId", location.pointer) + } else { + location.pointer.clone() + }, + } + }), + }); + } + } + conflicts +} + +fn next_available_name(base: &str, occupied: &mut BTreeSet) -> String { + // A catalog may contain more names than the preview candidate cap. With + // occupied.len() + 1 suffixes, at least one candidate must be free. + (2..=occupied.len() + 2) + .map(|index| format!("{base}_{index}")) + .find(|name| occupied.insert(name.clone())) + .expect("finite occupied names always leave a free suffix") +} + +fn attach_preview_readiness( + preview: &mut crank_import::rest::ImportPreview, + choices: &[OpenApiImportSecurityProfileChoice], +) { + let document_blocked = preview.findings.iter().any(|finding| { + finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none() + }); + for candidate in preview + .groups + .iter_mut() + .flat_map(|group| group.operations.iter_mut()) + { + let blocked = document_blocked + || candidate + .findings + .iter() + .any(|finding| finding.severity == ImportFindingSeverity::Error); + let alternatives = &candidate.security_requirements.alternatives; + let public = alternatives.is_empty() + || alternatives + .iter() + .any(|alternative| alternative.supported && alternative.schemes.is_empty()); + candidate.status = if blocked { + "blocked" + } else if public { + "actionable" + } else if alternatives.iter().enumerate().any(|(index, alternative)| { + alternative.supported + && choices.iter().any(|choice| { + choice.operation_key == candidate.key + && choice.alternative_index == index + && !choice.profiles.is_empty() + }) + }) { + "unresolved" + } else { + "blocked" + } + .to_owned(); + } +} + impl AdminService { #[instrument(skip(self, upload), fields(workspace_id = %workspace_id.as_str()))] pub async fn preview_openapi_import( @@ -53,29 +199,53 @@ impl AdminService { self.ensure_workspace_exists(workspace_id).await?; validate_openapi_upload(&upload)?; - let literal_findings = crank_import::rest::scan_secret_literals(&upload.bytes) - .map_err(|_| ApiError::openapi_upload(upload.locale, "invalid_document"))?; - if !literal_findings.is_empty() { - return Err(ApiError::openapi_upload( - upload.locale, - "literal_credentials_rejected", - )); - } let OpenApiUpload { - bytes, + bytes: raw_bytes, mime_type, locale, } = upload; - let store = self.artifact_store.clone(); - let artifact = tokio::task::spawn_blocking(move || store.put_registered(&bytes)) - .await - .map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable"))? - .map_err(|error| artifact_error(locale, error))?; + let raw_document = std::str::from_utf8(&raw_bytes) + .map_err(|_| ApiError::openapi_upload(locale, "invalid_utf8"))?; + validate_document_source_safe(raw_document, &self.external_reference_normalization) + .map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?; let now = OffsetDateTime::now_utc(); + let mut dependencies = self + .materialize_external_reference_snapshots(workspace_id, &raw_bytes, now) + .await; + if dependencies.unsafe_source { + return Err(ApiError::openapi_upload(locale, "invalid_document")); + } + let source_digest = + crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(&raw_bytes))) + .map_err(|_| ApiError::openapi_upload(locale, "invalid_document"))?; + if crank_import::rest::validate_resolved_bundle_source_safe( + raw_document, + source_digest, + &dependencies.snapshots, + &self.external_reference_normalization, + ) + .is_err() + { + dependencies.detach_all().await; + return Err(ApiError::openapi_upload(locale, "invalid_document")); + } + let bytes = raw_bytes; + let store = self.artifact_store.clone(); + let artifact_result = tokio::task::spawn_blocking(move || store.put_registered(&bytes)) + .await + .map_err(|_| ApiError::openapi_upload(locale, "storage_unavailable")) + .and_then(|result| result.map_err(|error| artifact_error(locale, error))); + let artifact = match artifact_result { + Ok(artifact) => artifact, + Err(error) => { + dependencies.detach_all().await; + return Err(error); + } + }; let expires_at = now + Duration::hours(IMPORT_JOB_TTL_HOURS); let job_id = ImportJobId::new(new_prefixed_id("imp")); let source_id = ArtifactSourceId::new(new_prefixed_id("src_openapi")); - let source = self + let source_result = self .registry .create_artifact_source(CreateArtifactSourceRequest { workspace_id, @@ -85,7 +255,14 @@ impl AdminService { sensitivity: ArtifactSourceSensitivity::Internal, created_at: now, }) - .await?; + .await; + let source = match source_result { + Ok(source) => source, + Err(error) => { + dependencies.detach_all().await; + return Err(ApiError::from(error)); + } + }; let mut detach_guard = SourceDetachGuard::new( self.registry.clone(), workspace_id.clone(), @@ -111,17 +288,6 @@ impl AdminService { detach_guard.detach_now().await; return Err(ApiError::openapi_upload(locale, "source_integrity")); } - let mut dependencies = self - .materialize_external_reference_snapshots(workspace_id, &verified.bytes, now) - .await; - if dependencies.secret_literal_rejected { - dependencies.detach_all().await; - detach_guard.detach_now().await; - return Err(ApiError::openapi_upload( - locale, - "literal_credentials_rejected", - )); - } let parsed = match parse_verified_preview( verified.bytes, artifact.artifact_ref().digest_hex().to_owned(), @@ -149,8 +315,22 @@ impl AdminService { detach_guard.detach_now().await; return Err(ApiError::openapi_upload(locale, "no_methods")); } - let auth_profile_choices = match self - .openapi_auth_profile_choices(workspace_id, &parsed.preview) + let candidate_count: usize = parsed + .preview + .groups + .iter() + .map(|group| group.operations.len()) + .sum(); + if candidate_count > MAX_IMPORT_PLAN_KEYS { + dependencies.detach_all().await; + detach_guard.detach_now().await; + return Err(ApiError::validation_with_context( + "OpenAPI preview has too many candidates", + json!({ "error_code": "openapi_preview_too_many_candidates", "limit": MAX_IMPORT_PLAN_KEYS }), + )); + } + let security_profile_choices = match self + .security_profile_choices(workspace_id, &parsed.preview) .await { Ok(choices) => choices, @@ -160,6 +340,14 @@ impl AdminService { return Err(error); } }; + let catalog_names = match self.registry.list_import_catalog_names(workspace_id).await { + Ok(names) => names, + Err(error) => { + dependencies.detach_all().await; + detach_guard.detach_now().await; + return Err(ApiError::from(error)); + } + }; let source_envelope = ImportJobSourceEnvelope { source_id, digest: artifact.artifact_ref().clone(), @@ -207,16 +395,92 @@ impl AdminService { dependencies.disarm(); detach_guard.disarm(); + let mut response_preview = parsed.preview; + response_preview.conflicts = preview_name_conflicts(&response_preview, &catalog_names); + attach_preview_readiness(&mut response_preview, &security_profile_choices); Ok(OpenApiImportPreviewResponse { job_id: job_id.as_str().to_owned(), expires_at: expires_at .format(&Rfc3339) .map_err(|error| ApiError::internal(error.to_string()))?, - auth_profile_choices, - preview: parsed.preview, + preview: response_preview, + security_profile_choices, }) } + async fn security_profile_choices( + &self, + workspace_id: &WorkspaceId, + preview: &crank_import::rest::ImportPreview, + ) -> Result, ApiError> { + let profiles = self.registry.list_auth_profiles(workspace_id).await?; + let mut available = Vec::new(); + for profile in profiles { + if profile.kind != profile.config.kind() { + continue; + } + let mut usable = true; + for secret_id in profile.config.secret_ids() { + let Some(secret) = self.registry.get_secret(workspace_id, secret_id).await? else { + usable = false; + break; + }; + if secret.secret.status != SecretStatus::Active + || self + .registry + .get_current_secret_version(workspace_id, secret_id) + .await? + .is_none() + { + usable = false; + break; + } + } + if usable { + available.push(profile); + } + } + + let mut choices = Vec::new(); + for operation in preview + .groups + .iter() + .flat_map(|group| group.operations.iter()) + { + for (alternative_index, alternative) in operation + .security_requirements + .alternatives + .iter() + .enumerate() + { + if !alternative.supported || alternative.schemes.len() != 1 { + continue; + } + let Some(scheme) = operation.security_schemes.get(&alternative.schemes[0]) else { + continue; + }; + let profiles = available + .iter() + .filter(|profile| { + import_plan::auth_config_matches_scheme(&profile.config, scheme) + }) + .map(|profile| OpenApiImportAuthProfileChoice { + id: profile.id.as_str().to_owned(), + name: profile.name.clone(), + kind: profile.kind, + }) + .collect::>(); + choices.push(OpenApiImportSecurityProfileChoice { + operation_key: operation.key.clone(), + alternative_index, + schemes: alternative.schemes.clone(), + profiles, + }); + } + } + Ok(choices) + } + #[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))] pub async fn create_openapi_import( &self, @@ -259,7 +523,11 @@ impl AdminService { ) })?; if job.expires_at <= OffsetDateTime::now_utc() { - return Err(ApiError::validation("import preview has expired")); + let message = match locale { + OpenApiUploadLocale::Ru => "срок действия preview импорта истёк", + OpenApiUploadLocale::En => "import preview has expired", + }; + return Err(ApiError::validation(message)); } if job.kind != ImportJobKind::OpenApi { return Err(ApiError::validation("import job kind is not openapi")); @@ -276,15 +544,6 @@ impl AdminService { )); } let finished_at = OffsetDateTime::now_utc(); - let mut auth_mappings = BTreeMap::new(); - for mapping in &payload.auth_mappings { - if auth_mappings - .insert(mapping.operation_key.clone(), mapping) - .is_some() - { - return Err(ApiError::openapi_upload(locale, "auth_mapping_invalid")); - } - } let application_key = openapi_application_key(&payload)?; let conflict_mode = if payload.conflict_mode == "skip" { ImportConflictMode::Skip @@ -314,141 +573,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, @@ -458,25 +596,11 @@ 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); } } - if auth_mappings.iter().any(|(operation_key, mapping)| { - !selected.contains(operation_key) - || candidates.get(operation_key).is_none_or(|candidate| { - mapping.alternative_index >= candidate.security_requirements.alternatives.len() - }) - }) { - return self - .fail_openapi_import_or_replay( - &replay_context, - "auth_mapping_invalid", - ApiError::openapi_upload(locale, "auth_mapping_invalid"), - ) - .await; - } let mut skipped = Vec::new(); let mut operations = Vec::new(); @@ -490,26 +614,6 @@ impl AdminService { }); continue; }; - let (auth_profile_ref, auth_requirement) = match self - .resolve_openapi_auth_binding( - workspace_id, - candidate, - auth_mappings.get(&operation_key).copied(), - locale, - ) - .await - { - Ok(binding) => binding, - Err(error) => { - return self - .fail_openapi_import_or_replay( - &replay_context, - "auth_binding_unavailable", - error, - ) - .await; - } - }; let mut draft = operation_draft_from_candidate(candidate, payload.server_url.as_deref()); attach_import_findings(&mut draft, candidate); @@ -531,7 +635,7 @@ impl AdminService { idempotency: None, safety: None, approval_policy: None, - auth_profile_ref, + auth_profile_ref: None, headers: BTreeMap::new(), }, tool_description: draft.tool_description, @@ -540,7 +644,6 @@ impl AdminService { operations.push(ImportOperationDraft { operation_key: candidate.key.clone(), operation, - auth_requirement, }); } @@ -555,13 +658,7 @@ impl AdminService { pre_skipped: &skipped, finished_at: &finished_at, }) - .await - .map_err(|error| match error { - RegistryError::ImportAuthBindingUnavailable => { - ApiError::openapi_auth_binding_unavailable(locale) - } - error => error.into(), - })?; + .await?; Ok(openapi_import_response(applied)) } @@ -611,6 +708,7 @@ fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreate operation.name ), operation_key: Some(operation.operation_key.clone()), + source_location: None, }) }) .collect::>(); @@ -629,6 +727,7 @@ fn openapi_import_response(applied: ImportJobApplyResult) -> OpenApiImportCreate format!("Операция {name} уже существует и была пропущена.") }, operation_key: Some(operation.operation_key.clone()), + source_location: None, }); OpenApiImportSkippedOperation { operation_key: operation.operation_key.clone(), @@ -678,252 +777,14 @@ 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 v2-v4 preview payloads are replayed as their persisted - // projection. They must never be parsed again with current v5 semantics. - if !matches!( - (normalizer, projection), - ("normalized-ir-v2", "preview-v2") - | ("normalized-ir-v3", "preview-v3") - | ("normalized-ir-v4", "preview-v4") - ) { - 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 - && !matches!( - finding.code.as_str(), - "unsupported_security_and_requirement" - | "unsupported_security_scheme" - | "unresolved_security_scheme" - ) - }) -} - fn openapi_application_key(payload: &OpenApiImportCreatePayload) -> Result { let selected_operation_keys = payload .selected_operation_keys .iter() .cloned() .collect::>(); - let mut auth_mappings = payload.auth_mappings.clone(); - auth_mappings.sort_by(|left, right| { - left.operation_key - .cmp(&right.operation_key) - .then(left.alternative_index.cmp(&right.alternative_index)) - }); let canonical = serde_json::to_vec(&json!({ "selected_operation_keys": selected_operation_keys, - "auth_mappings": auth_mappings, "server_url": payload.server_url.as_deref(), "conflict_mode": payload.conflict_mode.as_str(), })) @@ -965,3 +826,20 @@ fn tool_quality_finding_from_import(finding: &ImportFinding) -> ToolQualityFindi field_path: finding.operation_key.clone(), } } + +#[cfg(test)] +mod preview_name_tests { + use super::next_available_name; + use std::collections::BTreeSet; + + #[test] + fn crowded_catalog_still_gets_a_free_deterministic_proposal() { + let mut occupied = (2..=2_002) + .map(|index| format!("shared_{index}")) + .collect::>(); + occupied.insert("shared".to_owned()); + + assert_eq!(next_available_name("shared", &mut occupied), "shared_2003"); + assert_eq!(next_available_name("shared", &mut occupied), "shared_2004"); + } +} diff --git a/apps/admin-api/src/service/imports/external_references.rs b/apps/admin-api/src/service/imports/external_references.rs index c2b6260..b116152 100644 --- a/apps/admin-api/src/service/imports/external_references.rs +++ b/apps/admin-api/src/service/imports/external_references.rs @@ -4,10 +4,16 @@ use tracing::warn; pub(super) struct MaterializedDependencies { pub(super) snapshots: Vec, - pub(super) secret_literal_rejected: bool, envelopes: Vec, canonical_uris: Vec, guards: Vec, + pub(super) unsafe_source: bool, +} + +struct StagedDependency { + canonical_uri: String, + bytes: Vec, + document: String, } pub(super) struct ImportReplayContext<'a> { @@ -22,10 +28,10 @@ impl MaterializedDependencies { fn empty() -> Self { Self { snapshots: Vec::new(), - secret_literal_rejected: false, envelopes: Vec::new(), canonical_uris: Vec::new(), guards: Vec::new(), + unsafe_source: false, } } @@ -178,8 +184,8 @@ impl AdminService { } } let mut seen = BTreeSet::new(); - let mut result = MaterializedDependencies::empty(); - let mut dependency_by_digest = BTreeMap::::new(); + let mut staged = Vec::::new(); + let mut staged_documents = vec![primary.to_owned()]; while let Some((canonical_uri, depth)) = queue.pop_front() { if !seen.insert(canonical_uri.clone()) || depth > self.external_reference_normalization.max_reference_depth @@ -196,52 +202,77 @@ impl AdminService { materialization_failure( "fetch", external_fetch_error_code(&error), - result.snapshots.len(), + staged.len(), ); continue; } }; let Ok(document) = std::str::from_utf8(&bytes).map(str::to_owned) else { - materialization_failure( - "dependency_decode", - "invalid_utf8", - result.snapshots.len(), - ); + materialization_failure("dependency_decode", "invalid_utf8", staged.len()); continue; }; - match crank_import::rest::scan_secret_literals(&bytes) { - Ok(findings) if !findings.is_empty() => { - materialization_failure( - "security_scan", - "literal_credentials_rejected", - result.snapshots.len(), - ); - result.secret_literal_rejected = true; - break; - } - Ok(_) => {} - Err(_) => { - materialization_failure( - "security_scan", - "invalid_document", - result.snapshots.len(), - ); - continue; - } - } let Ok(references) = crank_import::rest::external_reference_uris( &document, &self.external_reference_normalization, ) else { - materialization_failure( - "dependency_scan", - "invalid_document", - result.snapshots.len(), - ); + materialization_failure("dependency_scan", "invalid_document", staged.len()); continue; }; + staged_documents.push(document.clone()); + staged.push(StagedDependency { + canonical_uri: canonical_uri.clone(), + bytes, + document, + }); + for reference in references { + if let Some(uri) = canonical_external_document_uri(Some(&canonical_uri), &reference) + { + queue.push_back((uri, depth.saturating_add(1))); + } + } + } + let staged_snapshots = staged + .iter() + .filter_map(|dependency| { + let digest = format!("{:x}", Sha256::digest(&dependency.bytes)); + let digest = crank_import::rest::SourceDigest::parse(digest).ok()?; + Some(crank_import::rest::ExternalDocumentSnapshot { + canonical_uri: dependency.canonical_uri.clone(), + digest, + document: dependency.document.clone(), + }) + }) + .collect::>(); + let primary_digest = + crank_import::rest::SourceDigest::parse(format!("{:x}", Sha256::digest(primary_bytes))) + .expect("SHA-256 digest is valid"); + let raw_bundle_is_unsafe = crank_import::rest::validate_document_bundle_source_safe( + &staged_documents + .iter() + .map(String::as_str) + .collect::>(), + &self.external_reference_normalization, + ) + .is_err(); + let resolved_bundle_is_unsafe = crank_import::rest::validate_resolved_bundle_source_safe( + primary, + primary_digest, + &staged_snapshots, + &self.external_reference_normalization, + ) + .is_err(); + if raw_bundle_is_unsafe || resolved_bundle_is_unsafe { + materialization_failure("dependency_redaction", "invalid_document", staged.len()); + let mut rejected = MaterializedDependencies::empty(); + rejected.unsafe_source = true; + return rejected; + } + + let mut result = MaterializedDependencies::empty(); + let mut dependency_by_digest = BTreeMap::::new(); + for dependency in staged { let store = self.artifact_store.clone(); - let artifact_bytes = bytes.clone(); + let artifact_bytes = dependency.bytes.clone(); let Ok(Ok(artifact)) = tokio::task::spawn_blocking(move || store.put_registered(&artifact_bytes)).await else { @@ -317,31 +348,22 @@ impl AdminService { dependency_by_digest.insert(digest_key, index); index }; - let dependency = &result.envelopes[envelope_index]; - let snapshot_digest = match crank_import::rest::SourceDigest::parse( - dependency.digest.digest_hex().to_owned(), - ) { - Ok(digest) => digest, - Err(_) => { - materialization_failure("snapshot", "source_integrity", result.snapshots.len()); - continue; - } + let dependency_envelope = &result.envelopes[envelope_index]; + let Ok(snapshot_digest) = crank_import::rest::SourceDigest::parse( + dependency_envelope.digest.digest_hex().to_owned(), + ) else { + materialization_failure("snapshot", "source_integrity", result.snapshots.len()); + continue; }; result .snapshots .push(crank_import::rest::ExternalDocumentSnapshot { - canonical_uri: canonical_uri.clone(), + canonical_uri: dependency.canonical_uri.clone(), digest: snapshot_digest, - document, + document: dependency.document, }); materialized_count.store(result.snapshots.len(), std::sync::atomic::Ordering::Relaxed); - result.canonical_uris.push(canonical_uri.clone()); - for reference in references { - if let Some(uri) = canonical_external_document_uri(Some(&canonical_uri), &reference) - { - queue.push_back((uri, depth.saturating_add(1))); - } - } + result.canonical_uris.push(dependency.canonical_uri); } // Registry ownership is one envelope per immutable digest, while the // resolver may map several canonical URIs to that same snapshot. Keep @@ -390,14 +412,6 @@ impl AdminService { } let document = String::from_utf8(verified.bytes) .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; - let findings = crank_import::rest::scan_secret_literals(document.as_bytes()) - .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; - if !findings.is_empty() { - return Err(ApiError::openapi_upload( - locale, - "literal_credentials_rejected", - )); - } let digest = crank_import::rest::SourceDigest::parse(dependency.digest.digest_hex().to_owned()) .map_err(|_| ApiError::openapi_upload(locale, "source_integrity"))?; 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..5b77212 --- /dev/null +++ b/apps/admin-api/src/service/imports/import_plan.rs @@ -0,0 +1,976 @@ +mod validation; + +use super::*; +use crank_core::AuthConfig; +use crank_import::rest::SecurityRequirementsSource as SecuritySource; + +impl AdminService { + pub async fn create_openapi_import_plan( + &self, + workspace_id: &WorkspaceId, + job_id: &ImportJobId, + payload: OpenApiImportPlanCreatePayload, + ) -> Result { + self.create_openapi_import_plan_with_locale( + workspace_id, + job_id, + payload, + OpenApiUploadLocale::En, + ) + .await + } + + #[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), job_id = %job_id.as_str()))] + pub async fn create_openapi_import_plan_with_locale( + &self, + workspace_id: &WorkspaceId, + job_id: &ImportJobId, + payload: OpenApiImportPlanCreatePayload, + locale: OpenApiUploadLocale, + ) -> 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 { + let message = match locale { + OpenApiUploadLocale::Ru => "срок действия preview импорта истёк", + OpenApiUploadLocale::En => "import preview has expired", + }; + return Err(ApiError::validation(message)); + } + if job.status != ImportJobStatus::Pending { + return Err(import_plan_repreview_error( + locale, + "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, locale) + .await + .map_err(|resolution| match resolution { + ImportPreviewResolution::Fatal(error_code, _) => { + import_plan_repreview_error(locale, error_code) + } + ImportPreviewResolution::NonFatal(error) => error, + })?; + let preview = &resolved.preview; + let catalog_revision = self.registry.import_catalog_revision(workspace_id).await?; + + // Stored external dependency snapshots are re-verified for every + // replay variant (including historically persisted ones) without a + // 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, locale) + .await + .map_err(|_| { + import_plan_repreview_error(locale, "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(); + if all_set.len() > MAX_IMPORT_PLAN_KEYS { + return Err(ApiError::validation_with_context( + "too many candidates for an import plan", + json!({ "error_code": "import_plan_invalid_input" }), + )); + } + let selected = resolve_plan_selection( + &payload.selection_mode, + &payload.selected_operation_keys, + &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 excluded_reasons = excluded + .iter() + .map(|key| { + let name = candidates + .get(key) + .map(|candidate| candidate.draft.name.clone()) + .unwrap_or_else(|| key.clone()); + let reason = preview + .findings + .iter() + .find(|finding| { + finding.severity == ImportFindingSeverity::Error + && finding.operation_key.is_none() + }) + .map(|finding| finding.code.clone()) + .or_else(|| { + candidates.get(key).and_then(|candidate| { + candidate + .findings + .iter() + .find(|finding| finding.severity == ImportFindingSeverity::Error) + .map(|finding| finding.code.clone()) + }) + }) + .unwrap_or_else(|| "not_selected".to_owned()); + (key.clone(), (name, reason)) + }) + .collect::>(); + let decisions = validation::validate_plan_decisions(&payload.decisions, &selected)?; + + let global_blocker = preview + .findings + .iter() + .find(|finding| { + finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none() + }) + .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(), + candidate, + decisions.get(key).copied(), + blocker, + locale, + ) + .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(), + }; + if payload.selection_mode == "partial" && counts.blocked != 0 { + return Err(ApiError::validation_with_context( + "partial selection includes blocked candidates", + json!({ "error_code": "import_plan_partial_blocked" }), + )); + } + if self.registry.import_catalog_revision(workspace_id).await? != catalog_revision { + return Err(import_plan_repreview_error( + locale, + "import_plan_catalog_stale", + )); + } + + // 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-v2", + "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, + "catalog_revision": catalog_revision, + "support_matrix_version": OPENAPI_SUPPORT_MATRIX_VERSION, + "findings": { + "document": preview.findings, + "candidates": candidates.iter().map(|(key, candidate)| json!({ + "key": key, + "findings": candidate.findings, + })).collect::>(), + }, + "selection": { + "mode": payload.selection_mode, + "all": all_keys, + "selected": selected_keys, + "excluded": excluded, + }, + "excluded_reasons": excluded_reasons.iter().map(|(key, (name, reason))| json!({ + "key": key, + "name": name, + "reason": reason, + })).collect::>(), + "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(), + security_alternative_index: candidate.security_alternative_index, + reason: candidate.reason.clone(), + }) + .collect(); + candidate_views.extend(excluded.iter().map(|key| { + let name = excluded_reasons + .get(key) + .map(|(name, _)| name.clone()) + .unwrap_or_else(|| key.clone()); + OpenApiImportPlanCandidateView { + key: key.clone(), + name, + state: "excluded".to_owned(), + action: None, + target_operation_id: None, + confirmed_name: None, + auth_profile_id: None, + security_alternative_index: None, + reason: excluded_reasons.get(key).map(|(_, reason)| reason.clone()), + } + })); + + Ok(OpenApiImportPlanCreateResponse { + plan_reference: created.id.as_str().to_owned(), + plan_digest: created.digest, + catalog_revision, + support_matrix_version: OPENAPI_SUPPORT_MATRIX_VERSION.to_owned(), + expires_at: created + .expires_at + .format(&Rfc3339) + .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>, + candidate: &ImportOperationCandidate, + decision: Option<&OpenApiImportPlanDecision>, + blocker_reason: Option, + locale: OpenApiUploadLocale, + ) -> 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 requested_security_alternative = + decision.and_then(|decision| decision.security_alternative_index); + let mut blocker_reason = blocker_reason; + let candidate_name = candidate.draft.name.clone(); + + let mut 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: candidate.key.clone(), + name: candidate_name, + blocked: true, + reason: Some("operation_name_conflict".to_owned()), + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + security_alternative_index: requested_security_alternative, + auth_requirement: None, + auth_snapshot: None, + target: None, + operation: None, + }); + } + let message = match locale { + OpenApiUploadLocale::Ru => { + format!("операция с именем {final_name} уже существует") + } + OpenApiUploadLocale::En => { + format!("operation with name {final_name} already exists") + } + }; + return Err(ApiError::conflict_with_context( + message, + json!({ + "name": final_name, + "error_code": "import_plan_name_conflict", + "recovery": "confirm_skip_rename_or_new_version" + }), + )); + } + } + // Use an explicit decision or inherit the target reference for new_version. + let mut auth_profile_ref = auth_profile_id + .as_deref() + .map(|id| AuthProfileId::new(id.to_owned())); + let mut inherited_auth_profile = false; + if let Some(summary) = target_summary.as_ref() + && !blocked + && action == "new_version" + && auth_profile_ref.is_none() + && (candidate.security_requirements.source == SecuritySource::None + || !candidate.security_requirements.alternatives.is_empty()) + { + 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; + inherited_auth_profile = auth_profile_ref.is_some(); + } + let auth_profile_id = auth_profile_ref.as_ref().map(|id| id.as_str().to_owned()); + let inherited_unconstrained_auth = inherited_auth_profile + && action == "new_version" + && candidate.security_requirements.source + == crank_import::rest::SecurityRequirementsSource::None + && candidate.security_requirements.alternatives.is_empty() + && requested_security_alternative.is_none(); + let security_alternative_index = if blocked { + requested_security_alternative + } else if inherited_unconstrained_auth { + None + } else { + resolve_security_binding( + self, + workspace_id, + candidate, + auth_profile_ref.as_ref(), + requested_security_alternative, + &mut blocker_reason, + ) + .await? + }; + blocked = blocker_reason.is_some(); + let auth_requirement = if !blocked && inherited_unconstrained_auth { + Some(json!({ "kind": "unconstrained" })) + } else if !blocked && auth_profile_ref.is_some() { + security_alternative_index + .and_then(|index| candidate.security_requirements.alternatives.get(index)) + .and_then(|alternative| { + (alternative.schemes.len() == 1).then(|| &alternative.schemes[0]) + }) + .and_then(|scheme_name| candidate.security_schemes.get(scheme_name)) + .and_then(|scheme| match &scheme.kind { + crank_import::rest::NormalizedSecuritySchemeKind::Http { scheme, .. } => { + match scheme.to_ascii_lowercase().as_str() { + "bearer" => Some(json!({ "kind": "bearer" })), + "basic" => Some(json!({ "kind": "basic" })), + _ => None, + } + } + crank_import::rest::NormalizedSecuritySchemeKind::ApiKey { + location: crank_import::rest::SecurityParameterLocation::Header, + parameter_name, + .. + } => { + Some(json!({ "kind": "api_key_header", "parameter_name": parameter_name })) + } + crank_import::rest::NormalizedSecuritySchemeKind::ApiKey { + location: crank_import::rest::SecurityParameterLocation::Query, + parameter_name, + .. + } => Some(json!({ "kind": "api_key_query", "parameter_name": parameter_name })), + _ => None, + }) + } else { + None + }; + + if blocked { + return Ok(PlannedCandidate { + key: candidate.key.clone(), + name: candidate_name, + blocked: true, + reason: blocker_reason, + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + security_alternative_index, + auth_requirement, + auth_snapshot: None, + target, + operation: None, + }); + } + + if action == "skip" { + return Ok(PlannedCandidate { + key: candidate.key.clone(), + name: candidate_name, + blocked: false, + reason: None, + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + security_alternative_index, + auth_requirement: None, + 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: candidate.key.clone(), + name: draft.name, + blocked: false, + reason: None, + action: action.to_owned(), + target_operation_id, + confirmed_name, + auth_profile_id, + security_alternative_index, + auth_requirement, + 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 = || { + ApiError::unprocessable_with_context( + "The selected authentication profile is unavailable for this import.", + json!({ "error_code": "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, + security_alternative_index: Option, + auth_requirement: Option, + auth_snapshot: Option, + target: Option, + operation: Option, +} + +pub(super) fn import_plan_repreview_error( + locale: OpenApiUploadLocale, + error_code: &'static str, +) -> ApiError { + let message = match locale { + OpenApiUploadLocale::Ru => { + "не удалось повторно проверить план импорта; создайте новый preview" + } + OpenApiUploadLocale::En => "import plan revalidation failed; create a fresh preview", + }; + ApiError::conflict_with_context( + message, + 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()) + } + } +} + +async fn resolve_security_binding( + service: &AdminService, + workspace_id: &WorkspaceId, + candidate: &ImportOperationCandidate, + profile_ref: Option<&AuthProfileId>, + requested_index: Option, + blocker_reason: &mut Option, +) -> Result, ApiError> { + let alternatives = &candidate.security_requirements.alternatives; + if alternatives.is_empty() { + if profile_ref.is_some() || requested_index.is_some() { + return Err(ApiError::validation_with_context( + "security binding was supplied for an operation with no security requirement", + json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }), + )); + } + return Ok(None); + } + + let public_index = alternatives + .iter() + .position(|alternative| alternative.supported && alternative.schemes.is_empty()); + let compatible = if let Some(profile_ref) = profile_ref { + let profile = service + .registry + .get_auth_profile(workspace_id, profile_ref) + .await? + .ok_or_else(|| { + ApiError::unprocessable_with_context( + "selected Auth Profile is unavailable in this workspace", + json!({ "error_code": "import_plan_auth_unavailable" }), + ) + })?; + let compatible = alternatives + .iter() + .enumerate() + .filter(|(_, alternative)| { + alternative.supported + && alternative.schemes.len() == 1 + && candidate + .security_schemes + .get(&alternative.schemes[0]) + .is_some_and(|scheme| auth_config_matches_scheme(&profile.config, scheme)) + }) + .map(|(index, _)| index) + .collect::>(); + Some((profile, compatible)) + } else { + None + }; + + if let Some((_, compatible_indices)) = compatible.as_ref() { + let selected = match requested_index { + Some(index) if compatible_indices.contains(&index) => index, + Some(_) => { + return Err(ApiError::validation_with_context( + "selected Auth Profile is incompatible with the selected security alternative", + json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }), + )); + } + None if compatible_indices.len() == 1 => compatible_indices[0], + None if compatible_indices.is_empty() => { + return Err(ApiError::validation_with_context( + "selected Auth Profile is incompatible with the source security schemes", + json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }), + )); + } + None => { + return Err(ApiError::validation_with_context( + "security_alternative_index is required when a profile matches multiple alternatives", + json!({ "operation_key": candidate.key, "error_code": "import_plan_security_alternative_required" }), + )); + } + }; + return Ok(Some(selected)); + } + + if let Some(index) = requested_index { + if alternatives + .get(index) + .is_none_or(|alternative| !alternative.supported) + { + return Err(ApiError::validation_with_context( + "selected security alternative is unsupported", + json!({ "operation_key": candidate.key, "error_code": "import_plan_auth_incompatible" }), + )); + } + if alternatives[index].schemes.is_empty() { + return Ok(Some(index)); + } + } + if let Some(public_index) = public_index + && requested_index.is_none() + { + return Ok(Some(public_index)); + } + *blocker_reason = Some("auth_profile_required".to_owned()); + Ok(requested_index) +} + +pub(super) fn auth_config_matches_scheme( + config: &AuthConfig, + scheme: &crank_import::rest::NormalizedSecurityScheme, +) -> bool { + use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation}; + match (&scheme.kind, config) { + (NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Bearer(_)) => { + scheme.eq_ignore_ascii_case("bearer") + } + (NormalizedSecuritySchemeKind::Http { scheme, .. }, AuthConfig::Basic(_)) => { + scheme.eq_ignore_ascii_case("basic") + } + ( + NormalizedSecuritySchemeKind::ApiKey { + location: SecurityParameterLocation::Header, + parameter_name, + .. + }, + AuthConfig::ApiKeyHeader(config), + ) => parameter_name.eq_ignore_ascii_case(&config.header_name), + ( + NormalizedSecuritySchemeKind::ApiKey { + location: SecurityParameterLocation::Query, + parameter_name, + .. + }, + AuthConfig::ApiKeyQuery(config), + ) => parameter_name == &config.param_name, + _ => false, + } +} + +/// 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, + "security_alternative_index": candidate.security_alternative_index, + }, + }); + 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(auth_requirement) = &candidate.auth_requirement { + object["auth_requirement"] = auth_requirement.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/import_plan/validation.rs b/apps/admin-api/src/service/imports/import_plan/validation.rs new file mode 100644 index 0000000..0c6ae53 --- /dev/null +++ b/apps/admin-api/src/service/imports/import_plan/validation.rs @@ -0,0 +1,128 @@ +use super::*; + +/// Binds decisions to selected candidates with action-specific field rules: +/// unknown/duplicate keys, foreign fields and oversized references are +/// rejected before the plan is materialized. +pub(super) fn validate_plan_decisions<'input>( + decisions: &'input [OpenApiImportPlanDecision], + selected: &BTreeSet, +) -> 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"); + } + if decision + .security_alternative_index + .is_some_and(|index| index > MAX_IMPORT_PLAN_KEYS) + { + return invalid("decision security_alternative_index is too large"); + } + match decision.action.as_str() { + "create" => { + if decision.target_operation_id.is_some() || decision.confirmed_name.is_some() { + return invalid( + "target_operation_id and confirmed_name are not allowed for action create", + ); + } + } + "skip" => { + if decision.auth_profile_id.is_some() || decision.security_alternative_index.is_some() { + return invalid("security binding is not allowed for action skip"); + } + if decision.target_operation_id.is_none() { + return invalid("action skip requires the exact target_operation_id"); + } + if decision.confirmed_name.is_some() { + return invalid("confirmed_name is not allowed for action skip"); + } + } + "rename" => { + if decision.target_operation_id.is_some() { + return invalid("target_operation_id is not allowed for action rename"); + } + if decision.confirmed_name.is_none() { + return invalid("action rename requires the exact confirmed_name"); + } + } + "new_version" => { + if decision.target_operation_id.is_none() { + return invalid("action new_version requires the exact target_operation_id"); + } + if decision.confirmed_name.is_some() { + return invalid("confirmed_name is not allowed for action new_version"); + } + } + _ => unreachable!("decision action is validated before the field rules"), + } + Ok(()) +} 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..5fb1fb1 --- /dev/null +++ b/apps/admin-api/src/service/imports/plan_apply.rs @@ -0,0 +1,440 @@ +use super::*; +use crank_registry::{ApplyImportPlanRequest, ImportPlanApplyReport}; + +const MAX_IMPORT_PLAN_IDEMPOTENCY_KEY_BYTES: usize = 128; + +impl AdminService { + /// Reads an immutable plan and checks its current authority without Apply mutations. + pub async fn revalidate_openapi_import_plan( + &self, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + ) -> Result { + self.revalidate_openapi_import_plan_with_locale( + workspace_id, + plan_id, + OpenApiUploadLocale::En, + ) + .await + } + + /// Locale-aware variant used by the HTTP boundary. + pub async fn revalidate_openapi_import_plan_with_locale( + &self, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + locale: OpenApiUploadLocale, + ) -> Result { + self.ensure_workspace_exists(workspace_id).await?; + let plan = self + .registry + .get_import_plan(workspace_id, plan_id) + .await? + .ok_or_else(|| { + ApiError::not_found_with_context( + "import plan was not found in this workspace", + json!({ "error_code": "import_plan_not_found" }), + ) + })?; + let mut reason = None; + let now = OffsetDateTime::now_utc(); + if plan.expires_at <= now { + reason = Some("import_plan_expired"); + } else if let Some(job) = self + .registry + .get_import_job(workspace_id, &plan.job_id) + .await? + { + if job.kind != ImportJobKind::OpenApi + || job.status != ImportJobStatus::Pending + || job.expires_at <= now + { + reason = Some("import_plan_job_terminal"); + } else { + match self + .resolve_verified_openapi_preview(workspace_id, &job, locale) + .await + { + Ok(resolved) => { + let v2 = plan.payload.get("schema_version").and_then(Value::as_str) + == Some("import-plan-v2"); + if self + .read_import_job_dependencies( + workspace_id, + &job.preview_payload, + locale, + ) + .await + .is_err() + { + reason = Some("import_dependency_verification_failed"); + } else if !v2 { + // Historical plans stay readable for audit, but lack the + // catalog evidence required for current Apply authority. + reason = Some("importer_version_changed"); + } else if resolved + .fingerprint + .get("normalizer_version") + .and_then(Value::as_str) + != Some(NORMALIZER_VERSION) + || resolved + .fingerprint + .get("projection_version") + .and_then(Value::as_str) + != Some(PROJECTION_VERSION) + || plan + .payload + .get("support_matrix_version") + .and_then(Value::as_str) + != Some(OPENAPI_SUPPORT_MATRIX_VERSION) + { + reason = Some("importer_version_changed"); + } else if self + .registry + .revalidate_import_plan(workspace_id, plan_id, &resolved.fingerprint) + .await + .is_err() + { + reason = Some("import_plan_revalidation_failed"); + } + } + Err(_) => reason = Some("import_preview_revalidation_failed"), + } + } + } else { + reason = Some("import_plan_job_unavailable"); + } + + let counts = &plan.payload["counts"]; + let count = |key: &str| { + counts + .get(key) + .and_then(Value::as_u64) + .and_then(|value| usize::try_from(value).ok()) + .unwrap_or(0) + }; + let selected = plan.payload["candidates"] + .as_array() + .into_iter() + .flatten() + .map(|candidate| { + let decision = &candidate["decision"]; + let string = |value: &Value| value.as_str().map(str::to_owned); + OpenApiImportPlanCandidateView { + key: string(&candidate["key"]).unwrap_or_default(), + name: string(&candidate["name"]).unwrap_or_default(), + state: string(&candidate["state"]).unwrap_or_default(), + action: string(&decision["action"]), + target_operation_id: string(&decision["target_operation_id"]), + confirmed_name: string(&decision["confirmed_name"]), + auth_profile_id: string(&decision["auth_profile_id"]), + security_alternative_index: decision["security_alternative_index"] + .as_u64() + .and_then(|value| usize::try_from(value).ok()), + reason: string(&candidate["reason"]), + } + }); + let excluded = plan + .payload + .pointer("/selection/excluded") + .and_then(Value::as_array) + .into_iter() + .flatten() + .filter_map(Value::as_str) + .map(|key| OpenApiImportPlanCandidateView { + key: key.to_owned(), + name: excluded_candidate_display_name(&plan.payload, key), + state: "excluded".to_owned(), + action: None, + target_operation_id: None, + confirmed_name: None, + auth_profile_id: None, + security_alternative_index: None, + reason: plan.payload["excluded_reasons"] + .as_array() + .and_then(|items| items.iter().find(|item| item["key"].as_str() == Some(key))) + .and_then(|item| item["reason"].as_str()) + .map(str::to_owned) + .or_else(|| Some("not_selected".to_owned())), + }); + Ok(OpenApiImportPlanRevalidationResponse { + plan_reference: plan.id.as_str().to_owned(), + plan_digest: plan.digest, + revalidation_status: if reason.is_some() { "stale" } else { "current" }.to_owned(), + revalidation_required: reason.is_some(), + reason: reason.map(str::to_owned), + counts: OpenApiImportPlanCounts { + selected: count("selected"), + excluded: count("excluded"), + actionable: count("actionable"), + blocked: count("blocked"), + }, + candidates: selected.chain(excluded).collect(), + }) + } + + pub async fn apply_openapi_import_plan( + &self, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + payload: OpenApiImportPlanApplyPayload, + ) -> Result { + self.apply_openapi_import_plan_with_locale( + workspace_id, + plan_id, + payload, + OpenApiUploadLocale::En, + ) + .await + } + + #[instrument(skip(self, payload), fields(workspace_id = %workspace_id.as_str(), plan_id = %plan_id.as_str()))] + pub async fn apply_openapi_import_plan_with_locale( + &self, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + payload: OpenApiImportPlanApplyPayload, + locale: OpenApiUploadLocale, + ) -> 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(locale)); + } + 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( + locale, + "import_plan_digest_mismatch", + )); + } + let now = OffsetDateTime::now_utc(); + if plan.expires_at <= now { + return Err(import_plan_repreview_error(locale, "import_plan_expired")); + } + + let job = self + .registry + .get_import_job(workspace_id, &plan.job_id) + .await? + .ok_or_else(|| import_plan_repreview_error(locale, "import_plan_job_unavailable"))?; + if job.kind != ImportJobKind::OpenApi { + return Err(import_plan_repreview_error( + locale, + "import_plan_job_kind_mismatch", + )); + } + if job.status != ImportJobStatus::Pending || job.expires_at <= now { + return Err(import_plan_repreview_error( + locale, + "import_plan_job_terminal", + )); + } + + let resolved = self + .resolve_verified_openapi_preview(workspace_id, &job, locale) + .await + .map_err(|resolution| match resolution { + ImportPreviewResolution::Fatal(error_code, _) => { + import_plan_repreview_error(locale, error_code) + } + ImportPreviewResolution::NonFatal(_) => { + import_plan_repreview_error(locale, "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) + || (plan.payload.get("schema_version").and_then(Value::as_str) + == Some("import-plan-v2") + && plan + .payload + .get("support_matrix_version") + .and_then(Value::as_str) + != Some(OPENAPI_SUPPORT_MATRIX_VERSION)) + { + return Err(import_plan_repreview_error( + locale, + "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, locale) + .await + .map_err(|_| { + import_plan_repreview_error(locale, "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(|error| map_apply_registry_error(locale, error))?; + Ok(result.report) + } +} + +fn excluded_candidate_display_name(plan: &Value, key: &str) -> String { + plan["excluded_reasons"] + .as_array() + .and_then(|items| items.iter().find(|item| item["key"].as_str() == Some(key))) + .and_then(|item| item["name"].as_str()) + .filter(|name| !name.is_empty()) + .unwrap_or(key) + .to_owned() +} + +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(locale: OpenApiUploadLocale, 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(locale, "import_plan_expired") + } + RegistryError::ImportPlanPayloadCorrupt { .. } => { + import_plan_repreview_error(locale, "import_plan_payload_invalid") + } + RegistryError::ImportPlanFingerprintStale { .. } => { + import_plan_repreview_error(locale, "import_plan_fingerprint_stale") + } + RegistryError::ImportPlanKeyDigestConflict { .. } => key_digest_conflict(locale), + RegistryError::ImportPlanNameConflict { .. } => { + let message = match locale { + OpenApiUploadLocale::Ru => "имя операции изменилось после создания плана", + OpenApiUploadLocale::En => "an operation name changed after the plan was created", + }; + ApiError::conflict_with_context( + message, + json!({ + "error_code": "import_plan_name_conflict", + "recovery": "confirm_skip_rename_or_new_version" + }), + ) + } + RegistryError::ImportPlanTargetStale { .. } => { + import_plan_repreview_error(locale, "import_plan_target_stale") + } + RegistryError::ImportPlanAuthUnavailable { .. } => { + import_plan_repreview_error(locale, "import_plan_auth_unavailable") + } + other => ApiError::from(other), + } +} + +fn key_digest_conflict(locale: OpenApiUploadLocale) -> ApiError { + let message = match locale { + OpenApiUploadLocale::Ru => { + "ключ идемпотентности уже зафиксирован для другого материала плана; создайте новый preview" + } + OpenApiUploadLocale::En => { + "idempotency key was already committed for different plan material" + } + }; + ApiError::conflict_with_context( + message, + json!({ + "error_code": "import_plan_key_digest_conflict", + "recovery": "repreview_required", + "repreview_required": true + }), + ) +} + +#[cfg(test)] +mod tests { + use super::excluded_candidate_display_name; + use serde_json::json; + + #[test] + fn excluded_candidate_name_uses_persisted_display_name_and_legacy_key_fallback() { + let current = json!({ + "excluded_reasons": [{ + "key": "GET /alpha", + "name": "Alpha display label", + "reason": "not_selected" + }] + }); + assert_eq!( + excluded_candidate_display_name(¤t, "GET /alpha"), + "Alpha display label" + ); + + let historic = json!({ + "excluded_reasons": [{ "key": "GET /alpha", "reason": "not_selected" }] + }); + assert_eq!( + excluded_candidate_display_name(&historic, "GET /alpha"), + "GET /alpha" + ); + } +} diff --git a/apps/admin-api/src/service/imports/security.rs b/apps/admin-api/src/service/imports/security.rs deleted file mode 100644 index 5220b3f..0000000 --- a/apps/admin-api/src/service/imports/security.rs +++ /dev/null @@ -1,195 +0,0 @@ -use super::*; -use crate::service::{OpenApiAuthProfileChoice, OpenApiAuthProfileOption}; -use crank_core::{AuthConfig, AuthProfile, AuthProfileId}; - -impl AdminService { - pub(super) async fn openapi_auth_profile_choices( - &self, - workspace_id: &WorkspaceId, - preview: &crank_import::rest::ImportPreview, - ) -> Result, ApiError> { - let mut profiles = self - .registry - .list_auth_profiles(workspace_id) - .await? - .into_iter() - .filter(|profile| profile.kind == profile.config.kind()) - .collect::>(); - let mut available_profiles = Vec::with_capacity(profiles.len()); - for profile in profiles.drain(..) { - if auth_profile_is_available(&self.registry, workspace_id, &profile).await? { - available_profiles.push(profile); - } - } - let mut choices = Vec::new(); - for operation in preview - .groups - .iter() - .flat_map(|group| group.operations.iter()) - { - for (alternative_index, alternative) in operation - .security_requirements - .alternatives - .iter() - .enumerate() - { - let scheme = (alternative.schemes.len() == 1) - .then(|| operation.security_schemes.get(&alternative.schemes[0])) - .flatten(); - let requirement = scheme.and_then(import_auth_requirement); - let supported = alternative.supported - && (alternative.schemes.is_empty() || requirement.is_some()); - let options = requirement - .as_ref() - .map(|requirement| { - available_profiles - .iter() - .filter(|profile| auth_profile_matches(profile, requirement)) - .map(|profile| OpenApiAuthProfileOption { - id: profile.id.as_str().to_owned(), - name: profile.name.clone(), - kind: profile.kind, - }) - .collect() - }) - .unwrap_or_default(); - choices.push(OpenApiAuthProfileChoice { - operation_key: operation.key.clone(), - alternative_index, - scheme_names: alternative.schemes.clone(), - supported, - profiles: options, - }); - } - } - Ok(choices) - } - - pub(super) async fn resolve_openapi_auth_binding( - &self, - workspace_id: &WorkspaceId, - candidate: &ImportOperationCandidate, - mapping: Option<&OpenApiAuthMapping>, - locale: OpenApiUploadLocale, - ) -> Result<(Option, Option), ApiError> { - let alternatives = &candidate.security_requirements.alternatives; - if alternatives.is_empty() { - return if mapping.is_none() { - Ok((None, None)) - } else { - Err(ApiError::openapi_upload(locale, "auth_mapping_invalid")) - }; - } - let mapping = - mapping.ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_required"))?; - let alternative = alternatives - .get(mapping.alternative_index) - .ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_invalid"))?; - if !alternative.supported { - return Err(ApiError::openapi_upload(locale, "auth_mapping_unsupported")); - } - if alternative.schemes.is_empty() { - return if mapping.auth_profile_id.is_none() { - Ok((None, None)) - } else { - Err(ApiError::openapi_upload(locale, "auth_mapping_invalid")) - }; - } - if alternative.schemes.len() != 1 { - return Err(ApiError::openapi_upload(locale, "auth_mapping_unsupported")); - } - let scheme = candidate - .security_schemes - .get(&alternative.schemes[0]) - .ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_unsupported"))?; - let requirement = import_auth_requirement(scheme) - .ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_unsupported"))?; - let profile_id = mapping - .auth_profile_id - .as_deref() - .filter(|value| !value.is_empty()) - .ok_or_else(|| ApiError::openapi_upload(locale, "auth_mapping_required"))?; - let profile_id = AuthProfileId::new(profile_id.to_owned()); - let profile = self - .registry - .get_auth_profile(workspace_id, &profile_id) - .await? - .ok_or_else(|| ApiError::openapi_auth_binding_unavailable(locale))?; - if !auth_profile_matches(&profile, &requirement) - || !auth_profile_is_available(&self.registry, workspace_id, &profile).await? - { - return Err(ApiError::openapi_auth_binding_unavailable(locale)); - } - Ok((Some(profile_id), Some(requirement))) - } -} - -fn import_auth_requirement( - scheme: &crank_import::rest::NormalizedSecurityScheme, -) -> Option { - use crank_import::rest::{NormalizedSecuritySchemeKind, SecurityParameterLocation}; - - match &scheme.kind { - NormalizedSecuritySchemeKind::Http { scheme, .. } - if scheme.eq_ignore_ascii_case("bearer") => - { - Some(ImportAuthRequirement::Bearer) - } - NormalizedSecuritySchemeKind::Http { scheme, .. } - if scheme.eq_ignore_ascii_case("basic") => - { - Some(ImportAuthRequirement::Basic) - } - NormalizedSecuritySchemeKind::ApiKey { - location: SecurityParameterLocation::Header, - parameter_name, - .. - } => Some(ImportAuthRequirement::ApiKeyHeader { - parameter_name: parameter_name.clone(), - }), - NormalizedSecuritySchemeKind::ApiKey { - location: SecurityParameterLocation::Query, - parameter_name, - .. - } => Some(ImportAuthRequirement::ApiKeyQuery { - parameter_name: parameter_name.clone(), - }), - _ => None, - } -} - -fn auth_profile_matches(profile: &AuthProfile, requirement: &ImportAuthRequirement) -> bool { - if profile.kind != profile.config.kind() { - return false; - } - match (requirement, &profile.config) { - (ImportAuthRequirement::Bearer, AuthConfig::Bearer(_)) - | (ImportAuthRequirement::Basic, AuthConfig::Basic(_)) => true, - ( - ImportAuthRequirement::ApiKeyHeader { parameter_name }, - AuthConfig::ApiKeyHeader(config), - ) => config.header_name.eq_ignore_ascii_case(parameter_name), - ( - ImportAuthRequirement::ApiKeyQuery { parameter_name }, - AuthConfig::ApiKeyQuery(config), - ) => config.param_name == *parameter_name, - _ => false, - } -} - -async fn auth_profile_is_available( - registry: &crank_registry::PostgresRegistry, - workspace_id: &WorkspaceId, - profile: &AuthProfile, -) -> Result { - for secret_id in profile.config.secret_ids() { - if registry - .get_current_secret_version(workspace_id, secret_id) - .await? - .is_none() - { - return Ok(false); - } - } - Ok(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..d06cbb1 --- /dev/null +++ b/apps/admin-api/src/service/imports/verified_preview.rs @@ -0,0 +1,401 @@ +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 current normalization semantics. + if !matches!( + (normalizer, projection), + ("normalized-ir-v2", "preview-v2") + | ("normalized-ir-v3", "preview-v3") + | ("normalized-ir-v4", "preview-v4") + | ("normalized-ir-v5", "preview-v5") + ) { + return Err(ApiError::openapi_upload(locale, "source_integrity")); + } + + 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)) + .any(|operation| { + operation + .findings + .iter() + .any(|finding| finding.severity == ImportFindingSeverity::Error) + || (!operation.security_requirements.alternatives.is_empty() + && !operation.security_requirements.alternatives.iter().any( + |alternative| alternative.supported && alternative.schemes.is_empty(), + )) + }) +} diff --git a/apps/admin-api/src/service/onboarding.rs b/apps/admin-api/src/service/onboarding.rs index 9fd1da2..81ad9a0 100644 --- a/apps/admin-api/src/service/onboarding.rs +++ b/apps/admin-api/src/service/onboarding.rs @@ -1,8 +1,10 @@ use crank_core::{ - OnboardingProjection, OnboardingStepId, ProductEventId, ProductEventKind, WorkspaceId, + OnboardingProjection, OnboardingStepId, OperationId, ProductEventId, ProductEventKind, + WorkspaceId, }; use crank_registry::{ - OnboardingPresentationMilestone, RecordOnboardingMilestoneRequest, RegistryError, + OnboardingImportContext, OnboardingPresentationMilestone, RecordOnboardingMilestoneRequest, + RegisterOnboardingImportContextRequest, RegistryError, SelectOnboardingImportDraftRequest, }; use serde_json::json; use time::OffsetDateTime; @@ -11,7 +13,10 @@ use crate::{ error::ApiError, service::{ AdminService, OnboardingEventPayload, OnboardingEventResponse, OnboardingFirstCallEvidence, - OnboardingResponse, OnboardingStepView, ResetOnboardingSelectionResponse, format_timestamp, + OnboardingImportChoiceView, OnboardingImportContextView, OnboardingImportDriftView, + OnboardingResponse, OnboardingStepView, RegisterOnboardingImportContextPayload, + RegisterOnboardingImportContextResponse, ResetOnboardingSelectionResponse, + SelectOnboardingImportDraftPayload, SelectOnboardingImportDraftResponse, format_timestamp, new_prefixed_id, }, }; @@ -22,11 +27,12 @@ impl AdminService { workspace_id: &WorkspaceId, ) -> Result { self.ensure_workspace_exists(workspace_id).await?; - let projection = self + let snapshot = self .registry - .ensure_onboarding_eligibility(workspace_id, OffsetDateTime::now_utc()) + .ensure_onboarding_eligibility_snapshot(workspace_id, OffsetDateTime::now_utc()) .await?; - self.map_onboarding_response(projection).await + self.map_onboarding_response(snapshot.projection, snapshot.import_context) + .await } pub async fn record_onboarding_event( @@ -67,13 +73,12 @@ impl AdminService { }; // Presentation events must never exist outside the server-owned cohort, // including a direct API call before the first UI snapshot GET. - let current = self - .registry - .ensure_onboarding_eligibility(workspace_id, OffsetDateTime::now_utc()) + self.registry + .ensure_onboarding_eligibility_snapshot(workspace_id, OffsetDateTime::now_utc()) .await?; let outcome = self .registry - .record_onboarding_milestone(RecordOnboardingMilestoneRequest { + .record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest { workspace_id, event_id: &ProductEventId::new(new_prefixed_id("pe")), milestone, @@ -94,9 +99,18 @@ impl AdminService { .as_ref() .is_some_and(|record| record.event.kind == event_kind) { + let latest = self + .registry + .ensure_onboarding_eligibility_snapshot( + workspace_id, + OffsetDateTime::now_utc(), + ) + .await?; return Ok(OnboardingEventResponse { accepted: false, - onboarding: self.map_onboarding_response(current).await?, + onboarding: self + .map_onboarding_response(latest.projection, latest.import_context) + .await?, }); } if replay.is_some() { @@ -122,7 +136,12 @@ impl AdminService { }; Ok(OnboardingEventResponse { accepted: outcome.accepted, - onboarding: self.map_onboarding_response(outcome.projection).await?, + onboarding: self + .map_onboarding_response( + outcome.snapshot.projection, + outcome.snapshot.import_context, + ) + .await?, }) } @@ -132,9 +151,13 @@ impl AdminService { expected_revision: i64, ) -> Result { self.ensure_workspace_exists(workspace_id).await?; - let projection = self + let snapshot = self .registry - .reset_onboarding_selection(workspace_id, expected_revision, OffsetDateTime::now_utc()) + .reset_onboarding_selection_snapshot( + workspace_id, + expected_revision, + OffsetDateTime::now_utc(), + ) .await .map_err(|error| match error { RegistryError::OnboardingStaleRevision => ApiError::conflict_with_context( @@ -145,13 +168,76 @@ impl AdminService { })?; Ok(ResetOnboardingSelectionResponse { selection_reset: true, - onboarding: self.map_onboarding_response(projection).await?, + onboarding: self + .map_onboarding_response(snapshot.projection, snapshot.import_context) + .await?, + }) + } + + pub async fn register_onboarding_import_context( + &self, + workspace_id: &WorkspaceId, + payload: RegisterOnboardingImportContextPayload, + ) -> Result { + self.ensure_workspace_exists(workspace_id).await?; + validate_application_identity(&payload.application_idempotency_key, &payload.plan_digest)?; + let snapshot = self + .registry + .register_onboarding_import_context_snapshot(RegisterOnboardingImportContextRequest { + workspace_id, + application_idempotency_key: &payload.application_idempotency_key, + plan_digest: &payload.plan_digest, + expected_revision: payload.expected_revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await?; + Ok(RegisterOnboardingImportContextResponse { + registered: true, + onboarding: self + .map_onboarding_response(snapshot.projection, snapshot.import_context) + .await?, + }) + } + + pub async fn select_onboarding_import_draft( + &self, + workspace_id: &WorkspaceId, + payload: SelectOnboardingImportDraftPayload, + ) -> Result { + self.ensure_workspace_exists(workspace_id).await?; + validate_application_identity(&payload.application_idempotency_key, &payload.plan_digest)?; + if payload.operation_id.is_empty() || payload.operation_id.len() > 128 { + return Err(ApiError::validation( + "operation_id must contain 1..128 bytes", + )); + } + if payload.operation_version == 0 { + return Err(ApiError::validation("operation_version must be positive")); + } + let snapshot = self + .registry + .select_onboarding_import_draft_snapshot(SelectOnboardingImportDraftRequest { + workspace_id, + application_idempotency_key: &payload.application_idempotency_key, + plan_digest: &payload.plan_digest, + operation_id: &OperationId::new(payload.operation_id), + operation_version: payload.operation_version, + expected_revision: payload.expected_revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await?; + Ok(SelectOnboardingImportDraftResponse { + selected: true, + onboarding: self + .map_onboarding_response(snapshot.projection, snapshot.import_context) + .await?, }) } async fn map_onboarding_response( &self, projection: OnboardingProjection, + import_context: Option, ) -> Result { let endpoint = if let Some(agent_id) = projection.agent_id.as_ref() { let workspace = self.get_workspace(&projection.workspace_id).await?; @@ -195,6 +281,7 @@ impl AdminService { _ => None, }; let first_incomplete = projection.steps.iter().position(|step| !step.completed); + let import_context = import_context.map(map_import_context); Ok(OnboardingResponse { schema_version: 1, workspace_id: projection.workspace_id.as_str().to_owned(), @@ -244,10 +331,61 @@ impl AdminService { .map(|id| id.as_str().to_owned()), mcp_endpoint: endpoint, first_call, + import_context, }) } } +fn map_import_context(context: OnboardingImportContext) -> OnboardingImportContextView { + OnboardingImportContextView { + application_idempotency_key: context.application_idempotency_key, + plan_digest: context.plan_digest, + report: context.report, + choices: context + .choices + .into_iter() + .map(|choice| OnboardingImportChoiceView { + key: choice.key, + name: choice.name, + outcome: choice.outcome, + operation_id: choice.operation_id, + version: choice.version, + reason: choice.reason, + selectable: choice.selectable, + }) + .collect(), + selected_operation_id: context + .selected_operation_id + .map(|id| id.as_str().to_owned()), + selected_operation_version: context.selected_operation_version, + drift: context.drift.map(|drift| OnboardingImportDriftView { + reason: drift.reason, + current_operation_version: drift.current_operation_version, + }), + } +} + +fn validate_application_identity( + application_idempotency_key: &str, + plan_digest: &str, +) -> Result<(), ApiError> { + if application_idempotency_key.is_empty() || application_idempotency_key.len() > 128 { + return Err(ApiError::validation( + "application_idempotency_key must contain 1..128 bytes", + )); + } + let valid_digest = plan_digest.len() == 64 + && plan_digest + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)); + if !valid_digest { + return Err(ApiError::validation( + "plan_digest must be 64 lowercase hex characters", + )); + } + Ok(()) +} + fn action_code(step: OnboardingStepId) -> &'static str { match step { OnboardingStepId::Operation => "create_operation", diff --git a/apps/admin-api/tests/config_startup.rs b/apps/admin-api/tests/config_startup.rs index bb3f6cc..1ef92b6 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(15), "migration binary must install the current canonical ledger", ); sqlx::query( diff --git a/apps/admin-api/tests/integration/common.rs b/apps/admin-api/tests/integration/common.rs index 06f450f..5fb655c 100644 --- a/apps/admin-api/tests/integration/common.rs +++ b/apps/admin-api/tests/integration/common.rs @@ -3,6 +3,7 @@ use std::{ collections::BTreeMap, env, fmt, + str::FromStr, sync::Arc, time::{SystemTime, UNIX_EPOCH}, }; @@ -15,7 +16,9 @@ use crank_core::{ }; use crank_core::{IdentityError, IdentityProvider, IdentityProviderKind, LoginOutcome}; use crank_mapping::{MappingRule, MappingSet}; -use crank_registry::{MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate, PostgresRegistry}; +use crank_registry::{ + MASTER_KEY_CIPHER_CONTRACT, MasterKeyIdentityCandidate, PostgresPoolConfig, PostgresRegistry, +}; use crank_runtime::SecretCrypto; use crank_schema::{Schema, SchemaKind}; use serde_json::{Value, json}; @@ -358,11 +361,33 @@ pub(super) async fn create_lead(Json(payload): Json) -> Json { pub(super) async fn test_registry() -> PostgresRegistry { let database_url = crank_test_support::postgres_schema_url("test_admin_api").await; - let pool = sqlx::PgPool::connect(&database_url).await.unwrap(); + test_registry_at_url(&database_url, None).await +} + +pub(super) async fn test_registry_with_single_connection() -> (PostgresRegistry, String) { + let database_url = crank_test_support::postgres_schema_url("test_admin_api_replay").await; + let pool_config = PostgresPoolConfig::try_new(1, 0, 5_000, 60_000, 1_800_000).unwrap(); + let registry = test_registry_at_url(&database_url, Some(pool_config)).await; + (registry, database_url) +} + +async fn test_registry_at_url( + database_url: &str, + pool_config: Option, +) -> PostgresRegistry { + let pool = sqlx::PgPool::connect(database_url).await.unwrap(); crank_registry::MigrationAuthority::apply(&pool) .await .unwrap(); - let registry = PostgresRegistry::connect(&database_url).await.unwrap(); + let registry = match pool_config { + Some(config) => { + let options = sqlx::postgres::PgConnectOptions::from_str(database_url).unwrap(); + PostgresRegistry::connect_with_options_and_pool_config(options, config) + .await + .unwrap() + } + None => PostgresRegistry::connect(database_url).await.unwrap(), + }; let secret_crypto = test_secret_crypto(); registry .verify_or_register_master_key_identity(MasterKeyIdentityCandidate { diff --git a/apps/admin-api/tests/integration/onboarding.rs b/apps/admin-api/tests/integration/onboarding.rs index 36b3d05..abc9fca 100644 --- a/apps/admin-api/tests/integration/onboarding.rs +++ b/apps/admin-api/tests/integration/onboarding.rs @@ -5,6 +5,8 @@ use super::common::*; use serde_json::{Value, json}; use serial_test::serial; +mod replay; + #[tokio::test(flavor = "multi_thread")] #[serial] async fn exposes_server_authoritative_onboarding_projection() { @@ -264,6 +266,573 @@ async fn direct_presentation_mutation_cannot_precede_server_eligibility() { assert_eq!(snapshot["status"], "in_progress"); } +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn import_context_registration_and_selection_are_revision_guarded() { + let registry = test_registry().await; + let storage_root = test_storage_root("onboarding_select_draft"); + let pool = registry.pool(); + insert_admin_import_fixture(pool).await; + + let base_url = spawn_admin_api(build_test_app(registry, storage_root)).await; + let client = authorized_client(&base_url).await; + + let initial = client + .get(format!("{base_url}/onboarding")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + let revision = initial["revision"].as_i64().unwrap(); + assert!(initial["import_context"].is_null()); + + // Forged identity: no committed immutable apply report. + let forged = client + .post(format!("{base_url}/onboarding/import-context")) + .json(&json!({ + "application_idempotency_key": "idem_missing", + "plan_digest": "b".repeat(64), + "expected_revision": revision, + })) + .send() + .await + .unwrap(); + let forged_status = forged.status(); + let forged = forged.json::().await.unwrap(); + assert_eq!( + forged_status, + reqwest::StatusCode::UNPROCESSABLE_ENTITY, + "body={forged}" + ); + assert_eq!( + forged["error"]["code"], + "onboarding_import_application_unavailable" + ); + + // Stale revision cannot register a context. + let stale = client + .post(format!("{base_url}/onboarding/import-context")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "expected_revision": revision + 1, + })) + .send() + .await + .unwrap(); + let stale_status = stale.status(); + let stale = stale.json::().await.unwrap(); + assert_eq!(stale_status, reqwest::StatusCode::CONFLICT, "body={stale}"); + assert_eq!(stale["error"]["code"], "onboarding_stale_revision"); + + // A valid registration stores a pending, recoverable import context and + // suppresses generic automatic Operation selection. + let registered = client + .post(format!("{base_url}/onboarding/import-context")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "expected_revision": revision, + })) + .send() + .await + .unwrap(); + let registered_status = registered.status(); + let registered = registered.json::().await.unwrap(); + assert_eq!( + registered_status, + reqwest::StatusCode::OK, + "body={registered}" + ); + assert_eq!(registered["registered"], true); + assert_eq!(registered["operation_id"], Value::Null); + assert_eq!( + registered["import_context"]["selected_operation_id"], + Value::Null + ); + assert_eq!(registered["import_context"]["plan_digest"], "b".repeat(64)); + assert_eq!( + registered["import_context"]["report"]["schema_version"], + "import-plan-apply-report-v1" + ); + assert_eq!( + registered["import_context"]["choices"][0]["selectable"], + true + ); + let registered_revision = registered["revision"].as_i64().unwrap(); + + // Selecting under a different application identity is rejected. + let mismatched = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_other", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": registered_revision, + })) + .send() + .await + .unwrap(); + let mismatched_status = mismatched.status(); + let mismatched = mismatched.json::().await.unwrap(); + assert_eq!( + mismatched_status, + reqwest::StatusCode::UNPROCESSABLE_ENTITY, + "body={mismatched}" + ); + assert_eq!( + mismatched["error"]["code"], + "onboarding_import_context_unavailable" + ); + + // A forged operation is not a successful outcome of the report. + let forged = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_forged", + "operation_version": 1, + "expected_revision": registered_revision, + })) + .send() + .await + .unwrap(); + let forged_status = forged.status(); + let forged = forged.json::().await.unwrap(); + assert_eq!( + forged_status, + reqwest::StatusCode::UNPROCESSABLE_ENTITY, + "body={forged}" + ); + assert_eq!( + forged["error"]["code"], + "onboarding_import_draft_unavailable" + ); + + // Unknown/raw client fields are rejected outright. + let unknown = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": registered_revision, + "raw_credential": "crk_forbidden_canary", + })) + .send() + .await + .unwrap(); + let unknown_status = unknown.status(); + let unknown = unknown.text().await.unwrap(); + assert_eq!( + unknown_status, + reqwest::StatusCode::UNPROCESSABLE_ENTITY, + "body={unknown}" + ); + + // A verified choice anchors the exact imported Draft. + let selected = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": registered_revision, + })) + .send() + .await + .unwrap(); + let selected_status = selected.status(); + let selected = selected.json::().await.unwrap(); + assert_eq!(selected_status, reqwest::StatusCode::OK, "body={selected}"); + assert_eq!(selected["selected"], true); + assert_eq!(selected["operation_id"], "op_admin_import"); + assert_eq!(selected["status"], "in_progress"); + assert_eq!(selected["import_context"]["selected_operation_version"], 1); + + // Recovery after a fresh GET preserves the explicit selection. + let recovered = client + .get(format!("{base_url}/onboarding")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + assert_eq!( + recovered["import_context"]["selected_operation_id"], + "op_admin_import" + ); + assert_eq!(recovered["import_context"]["selected_operation_version"], 1); + assert!(recovered["import_context"]["drift"].is_null()); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn onboarding_revision_round_trips_through_js_safe_integer() { + let registry = test_registry().await; + let storage_root = test_storage_root("onboarding_revision_round_trip"); + let pool = registry.pool(); + insert_admin_import_fixture(pool).await; + + let base_url = spawn_admin_api(build_test_app(registry, storage_root)).await; + let client = authorized_client(&base_url).await; + + // Real GET JSON -> browser JS Number -> POST expected_revision CAS. The + // deterministic SQL mask keeps the revision a safe integer so the value + // survives the f64 round trip that a browser performs. + let value = client + .get(format!("{base_url}/onboarding")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + let revision = value["revision"].as_i64().unwrap(); + assert!(revision >= 0); + assert!( + revision <= 9_007_199_254_740_991, + "revision must stay within Number.MAX_SAFE_INTEGER" + ); + let js_round_trip = (revision as f64) as i64; + assert_eq!( + js_round_trip, revision, + "revision must round trip through a browser JS Number" + ); + + let registered = client + .post(format!("{base_url}/onboarding/import-context")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "expected_revision": js_round_trip, + })) + .send() + .await + .unwrap(); + let registered_status = registered.status(); + let registered = registered.json::().await.unwrap(); + assert_eq!( + registered_status, + reqwest::StatusCode::OK, + "body={registered}" + ); + let registered_revision = registered["revision"].as_i64().unwrap(); + assert!((registered_revision as f64) as i64 == registered_revision); + + // A stale JS-safe revision is rejected without mutation. + let stale = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": registered_revision - 1, + })) + .send() + .await + .unwrap(); + let stale_status = stale.status(); + let stale = stale.json::().await.unwrap(); + assert_eq!(stale_status, reqwest::StatusCode::CONFLICT, "body={stale}"); + assert_eq!(stale["error"]["code"], "onboarding_stale_revision"); + + let selected = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": (registered_revision as f64) as i64, + })) + .send() + .await + .unwrap(); + let selected_status = selected.status(); + let selected = selected.json::().await.unwrap(); + assert_eq!(selected_status, reqwest::StatusCode::OK, "body={selected}"); + assert_eq!(selected["selected"], true); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn import_context_snapshot_stays_consistent_when_candidate_changes() { + let registry = test_registry().await; + let storage_root = test_storage_root("onboarding_import_snapshot"); + let pool = registry.pool().clone(); + insert_admin_import_fixture(&pool).await; + + let base_url = spawn_admin_api(build_test_app(registry, storage_root)).await; + let client = authorized_client(&base_url).await; + + let initial = client + .get(format!("{base_url}/onboarding")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + let revision = initial["revision"].as_i64().unwrap(); + + let registered = client + .post(format!("{base_url}/onboarding/import-context")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "expected_revision": revision, + })) + .send() + .await + .unwrap(); + let registered_status = registered.status(); + let registered = registered.json::().await.unwrap(); + assert_eq!( + registered_status, + reqwest::StatusCode::OK, + "body={registered}" + ); + let registered_revision = registered["revision"].as_i64().unwrap(); + // Projection revision and import_context come from one authoritative + // snapshot: the choice is actionable and nothing is selected yet. + assert_eq!( + registered["import_context"]["selected_operation_id"], + Value::Null + ); + assert_eq!( + registered["import_context"]["choices"][0]["outcome"], + "created" + ); + assert_eq!( + registered["import_context"]["choices"][0]["selectable"], + true + ); + + // The pending candidate becomes non-actionable between requests. + sqlx::query("update operations set status = 'archived' where id = 'op_admin_import'") + .execute(&pool) + .await + .unwrap(); + + let snapshot = client + .get(format!("{base_url}/onboarding")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + let snapshot_revision = snapshot["revision"].as_i64().unwrap(); + assert_ne!(snapshot_revision, registered_revision); + // The same response carries the updated revision and matching choices. + assert_eq!( + snapshot["import_context"]["choices"][0]["selectable"], + false + ); + + let stale = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": registered_revision, + })) + .send() + .await + .unwrap(); + let stale_status = stale.status(); + let stale = stale.json::().await.unwrap(); + assert_eq!(stale_status, reqwest::StatusCode::CONFLICT, "body={stale}"); + assert_eq!(stale["error"]["code"], "onboarding_stale_revision"); + + let unavailable = client + .post(format!("{base_url}/onboarding/select-draft")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": snapshot_revision, + })) + .send() + .await + .unwrap(); + let unavailable_status = unavailable.status(); + let unavailable = unavailable.json::().await.unwrap(); + assert_eq!( + unavailable_status, + reqwest::StatusCode::UNPROCESSABLE_ENTITY, + "body={unavailable}" + ); + assert_eq!( + unavailable["error"]["code"], + "onboarding_import_draft_unavailable" + ); + + let selected: Option = sqlx::query_scalar( + "select selected_operation_id from onboarding_import_selections + where workspace_id = 'ws_default'", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert!( + selected.is_none(), + "rejected selections must not mutate state" + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +#[serial] +async fn concurrent_archive_vs_select_draft_rejects_without_mutation() { + let registry = test_registry().await; + let storage_root = test_storage_root("onboarding_concurrent_select"); + let pool = registry.pool().clone(); + insert_admin_import_fixture(&pool).await; + + let base_url = spawn_admin_api(build_test_app(registry, storage_root)).await; + let client = authorized_client(&base_url).await; + + let initial = client + .get(format!("{base_url}/onboarding")) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + let revision = initial["revision"].as_i64().unwrap(); + let registered = client + .post(format!("{base_url}/onboarding/import-context")) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "expected_revision": revision, + })) + .send() + .await + .unwrap() + .json::() + .await + .unwrap(); + let registered_revision = registered["revision"].as_i64().unwrap(); + + // Hold the exact candidate row lock while the HTTP selection contends. + let mut blocker = pool.begin().await.unwrap(); + sqlx::query("select id from operations where workspace_id = $1 and id = $2 for update") + .bind("ws_default") + .bind("op_admin_import") + .fetch_one(&mut *blocker) + .await + .unwrap(); + + let select_client = client.clone(); + let select_url = format!("{base_url}/onboarding/select-draft"); + let select_task = tokio::spawn(async move { + select_client + .post(select_url) + .json(&json!({ + "application_idempotency_key": "idem_admin_import", + "plan_digest": "b".repeat(64), + "operation_id": "op_admin_import", + "operation_version": 1, + "expected_revision": registered_revision, + })) + .send() + .await + .unwrap() + }); + + tokio::time::sleep(std::time::Duration::from_millis(200)).await; + sqlx::query( + "update operations set status = 'archived', updated_at = now() + where workspace_id = 'ws_default' and id = 'op_admin_import'", + ) + .execute(&mut *blocker) + .await + .unwrap(); + blocker.commit().await.unwrap(); + + let response = select_task.await.unwrap(); + let status = response.status(); + let body = response.json::().await.unwrap(); + assert!( + status == reqwest::StatusCode::CONFLICT + || status == reqwest::StatusCode::UNPROCESSABLE_ENTITY, + "concurrent archive must reject the selection: status={status} body={body}" + ); + let selected: Option = sqlx::query_scalar( + "select selected_operation_id from onboarding_import_selections + where workspace_id = 'ws_default'", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert!( + selected.is_none(), + "rejected selection must not mutate state" + ); +} + +async fn insert_admin_import_fixture(pool: &sqlx::PgPool) { + sqlx::query( + "insert into operations ( + id, workspace_id, name, display_name, category, protocol, security_level, status, + current_draft_version, latest_published_version, created_at, updated_at + ) values ('op_admin_import', 'ws_default', 'admin_import', 'Admin Import', 'general', + 'rest', 'standard', 'draft', 1, null, now(), now())", + ) + .execute(pool) + .await + .unwrap(); + sqlx::query( + "insert into operation_versions ( + operation_id, version, status, target_json, input_schema_json, output_schema_json, + input_mapping_json, output_mapping_json, execution_config_json, + tool_description_json, name, display_name, category, protocol, security_level, + snapshot_provenance, snapshot_observed_at, created_at + ) values ('op_admin_import', 1, 'draft', '{}'::jsonb, '{}'::jsonb, '{}'::jsonb, + '{}'::jsonb, '{}'::jsonb, '{}'::jsonb, '{}'::jsonb, + 'admin_import', 'Admin Import', 'general', 'rest', 'standard', + 'native_v4', now(), 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_default', 'idem_admin_import', 'plan_admin', $1, $2, now())", + ) + .bind("b".repeat(64)) + .bind(json!({ + "schema_version": "import-plan-apply-report-v1", + "status": "applied", + "source_digest": "c".repeat(64), + "outcomes": [{ + "key": "candidate_admin", + "name": "adminImport", + "outcome": "created", + "operation_id": "op_admin_import", + "version": 1, + }], + })) + .execute(pool) + .await + .unwrap(); +} + #[tokio::test(flavor = "multi_thread")] #[serial] async fn reset_selection_is_explicit_and_revision_guarded() { diff --git a/apps/admin-api/tests/integration/onboarding/replay.rs b/apps/admin-api/tests/integration/onboarding/replay.rs new file mode 100644 index 0000000..b104c3f --- /dev/null +++ b/apps/admin-api/tests/integration/onboarding/replay.rs @@ -0,0 +1,160 @@ +use super::*; +use std::{future::Future, pin::Pin, task::Poll}; + +use crank_core::{OperationId, ProductEventId, WorkspaceId}; +use crank_registry::{ + OnboardingPresentationMilestone, PostgresRegistry, RecordOnboardingMilestoneRequest, + RegisterOnboardingImportContextRequest, SelectOnboardingImportDraftRequest, +}; +use time::OffsetDateTime; + +async fn poll_once_is_pending(mut future: Pin<&mut F>) -> bool { + std::future::poll_fn(|cx| Poll::Ready(future.as_mut().poll(cx).is_pending())).await +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn stale_same_kind_replay_returns_projection_after_concurrent_change() { + let (registry, database_url) = test_registry_with_single_connection().await; + let pool = registry.pool().clone(); + let external_registry = PostgresRegistry::connect(&database_url).await.unwrap(); + let raw = sqlx::PgPool::connect(&database_url).await.unwrap(); + let workspace_id = WorkspaceId::new("ws_default"); + insert_admin_import_fixture(&raw).await; + let service = test_service( + registry.clone(), + test_storage_root("onboarding_stale_replay"), + test_auth_settings(), + test_secret_crypto(), + ); + let now = OffsetDateTime::now_utc(); + let initial = registry + .ensure_onboarding_eligibility_snapshot(&workspace_id, now) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context_snapshot(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_admin_import", + plan_digest: &"b".repeat(64), + expected_revision: initial.projection.revision, + occurred_at: now, + }) + .await + .unwrap(); + let selected = registry + .select_onboarding_import_draft_snapshot(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_admin_import", + plan_digest: &"b".repeat(64), + operation_id: &OperationId::new("op_admin_import"), + operation_version: 1, + expected_revision: registered.projection.revision, + occurred_at: now, + }) + .await + .unwrap(); + let legacy_key = "legacy-started-replay"; + registry + .record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest { + workspace_id: &workspace_id, + event_id: &ProductEventId::new("pe_legacy_started_replay"), + milestone: OnboardingPresentationMilestone::Started, + idempotency_key: legacy_key, + expected_revision: selected.projection.revision, + occurred_at: now, + eligible_since: initial.projection.eligible_since, + }) + .await + .unwrap(); + let before = registry + .ensure_onboarding_eligibility_snapshot(&workspace_id, now) + .await + .unwrap(); + assert!(before.import_context.as_ref().unwrap().choices[0].selectable); + + let mut service_call = Box::pin(service.record_onboarding_event( + &workspace_id, + admin_api::dto::OnboardingEventPayload { + event: "started".to_owned(), + idempotency_key: legacy_key.to_owned(), + expected_revision: before.projection.revision, + completed_steps: None, + }, + )); + + // Queue the service's workspace lookup ahead of the first connection + // barrier, then prove that the first onboarding snapshot is queued ahead + // of the second barrier. sqlx's pool acquisition queue is FIFO. + let held = pool.acquire().await.unwrap(); + assert!(poll_once_is_pending(service_call.as_mut()).await); + let mut first_barrier = Box::pin(pool.acquire()); + assert!(poll_once_is_pending(first_barrier.as_mut()).await); + drop(held); + let first_connection = tokio::select! { + connection = first_barrier.as_mut() => connection.unwrap(), + _ = service_call.as_mut() => panic!("service call completed before first snapshot barrier"), + }; + assert!(poll_once_is_pending(service_call.as_mut()).await); + + let mut second_barrier = Box::pin(pool.acquire()); + assert!(poll_once_is_pending(second_barrier.as_mut()).await); + drop(first_connection); + let second_connection = tokio::select! { + connection = second_barrier.as_mut() => connection.unwrap(), + _ = service_call.as_mut() => panic!("service call completed before second snapshot barrier"), + }; + assert!(poll_once_is_pending(service_call.as_mut()).await); + + // The first snapshot has completed and the event write is now queued + // behind the second barrier. Change the projection and import context from + // an independent registry connection before allowing that write to observe state. + sqlx::query( + "update operations set status = 'archived', updated_at = now() + where workspace_id = 'ws_default' and id = 'op_admin_import'", + ) + .execute(&raw) + .await + .unwrap(); + let pre_mutation = external_registry + .ensure_onboarding_eligibility_snapshot(&workspace_id, now) + .await + .unwrap(); + external_registry + .record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest { + workspace_id: &workspace_id, + event_id: &ProductEventId::new("pe_replay_race_mutation"), + milestone: OnboardingPresentationMilestone::Dismissed, + idempotency_key: "mutation-between-snapshots", + expected_revision: pre_mutation.projection.revision, + occurred_at: now, + eligible_since: None, + }) + .await + .unwrap(); + let fresh = external_registry + .ensure_onboarding_eligibility_snapshot(&workspace_id, now) + .await + .unwrap(); + assert_ne!(fresh.projection.revision, pre_mutation.projection.revision); + assert!(!fresh.import_context.as_ref().unwrap().choices[0].selectable); + assert!(fresh.import_context.as_ref().unwrap().drift.is_some()); + drop(second_connection); + + let response = service_call.await.unwrap(); + assert!(!response.accepted); + assert_eq!( + response.onboarding.revision, fresh.projection.revision, + "same-kind idempotency replay must return the latest projection after the concurrent mutation" + ); + assert!(!response.onboarding.import_context.as_ref().unwrap().choices[0].selectable); + assert!( + response + .onboarding + .import_context + .as_ref() + .unwrap() + .drift + .is_some() + ); +} diff --git a/apps/admin-api/tests/integration/openapi_import.rs b/apps/admin-api/tests/integration/openapi_import.rs index d0a4f96..e0c6213 100644 --- a/apps/admin-api/tests/integration/openapi_import.rs +++ b/apps/admin-api/tests/integration/openapi_import.rs @@ -1,15 +1,9 @@ use admin_api::service::{ - AdminServiceBuilder, AuthProfilePayload, OpenApiAuthMapping, OpenApiImportCreatePayload, - OpenApiImportCreateResponse, OpenApiUpload, OpenApiUploadLocale, SecretPayload, + AdminServiceBuilder, OpenApiImportCreatePayload, OpenApiUpload, OpenApiUploadLocale, }; -use crank_core::{ - AuthConfig, AuthKind, BearerAuthConfig, SecretId, SecretKind, Workspace, WorkspaceId, - WorkspaceStatus, -}; -use crank_registry::{CreateWorkspaceRequest, ImportJobStatus}; -use serde_json::json; +use crank_core::WorkspaceId; +use crank_registry::ImportJobStatus; use serial_test::serial; -use sha2::Digest; use super::common::{ test_auth_settings, test_registry, test_secret_crypto, test_service, test_storage_root, @@ -17,7 +11,120 @@ use super::common::{ mod external_references; mod http_contract; -mod security; +mod import_plan_apply; +mod import_plan_create; +mod large_preview; +mod upload_outcomes; +mod v5_replay; + +#[tokio::test] +#[serial] +async fn upload_rejects_credential_literals_before_persisting_source_artifacts() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_storage_redaction"); + let service = test_service( + registry.clone(), + storage_root, + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let upload = OpenApiUpload { + bytes: br#"{ + "openapi":"3.0.3", + "info":{"title":"Redaction persistence","version":"1"}, + "servers":[{"url":"https://bare-canary@api.example.test/v1?api_key=query-canary"}], + "components":{"securitySchemes":{"key":{"type":"apiKey","in":"header","name":"X-Api-Key"}}}, + "paths":{"/private":{"get":{ + "operationId":"privateOperation", + "parameters":[{"name":"X-Api-Key","in":"header","example":"opaque-canary-42"}], + "responses":{"200":{"description":"ok"}}, + "x-client-secret":true + }}} + }"# + .to_vec(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; + let error = service + .preview_openapi_import(&workspace_id, upload) + .await + .expect_err("credential-bearing uploads must be rejected before persistence"); + let rendered_error = format!("{error:?}"); + assert!( + rendered_error.contains("invalid_document"), + "unexpected typed error: {rendered_error}" + ); + for canary in ["bare-canary", "query-canary", "opaque-canary-42"] { + assert!( + !rendered_error.contains(canary), + "API error leaked {canary}" + ); + } + let source_count: i64 = + sqlx::query_scalar("select count(*) from artifact_sources where workspace_id = $1") + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!(source_count, 0, "unsafe upload created an artifact source"); +} + +#[tokio::test] +#[serial] +async fn legacy_apply_rejects_required_auth_without_binding_before_creating_draft() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_unresolved_auth"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let upload = OpenApiUpload { + bytes: br#"{ + "openapi":"3.0.3", + "info":{"title":"Required auth","version":"1"}, + "servers":[{"url":"https://api.example.test"}], + "components":{"securitySchemes":{"bearer":{"type":"http","scheme":"bearer"}}}, + "security":[{"bearer":[]}], + "paths":{"/secure":{"get":{"operationId":"secureOperation","responses":{"200":{"description":"ok"}}}}} + }"# + .to_vec(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; + let preview = service + .preview_openapi_import(&workspace_id, upload) + .await + .unwrap(); + let result = service + .create_openapi_import( + &workspace_id, + &preview.job_id.as_str().into(), + OpenApiImportCreatePayload { + selected_operation_keys: vec!["GET /secure".to_owned()], + server_url: None, + conflict_mode: "rename".to_owned(), + }, + ) + .await; + let error = result.expect_err("legacy apply cannot bypass unresolved required auth"); + assert!(format!("{error:?}").contains("invalid_document")); + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); + let job = registry + .get_import_job(&workspace_id, &preview.job_id.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!(job.status, ImportJobStatus::Failed); +} const OPENAPI3: &str = r#" openapi: 3.0.3 @@ -85,11 +192,11 @@ async fn previews_openapi_and_creates_draft_operations() { assert_eq!(preview_job.status, ImportJobStatus::Pending); assert_eq!( preview_job.preview_payload["normalization"]["normalizer_version"], - "normalized-ir-v5" + crank_import::rest::NORMALIZER_VERSION ); assert_eq!( preview_job.preview_payload["normalization"]["projection_version"], - "preview-v5" + crank_import::rest::PROJECTION_VERSION ); assert!(preview_job.preview_payload["normalization"]["ir_fingerprint"].is_string()); @@ -99,7 +206,6 @@ async fn previews_openapi_and_creates_draft_operations() { &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -118,7 +224,6 @@ async fn previews_openapi_and_creates_draft_operations() { &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -171,7 +276,6 @@ async fn previews_openapi_and_creates_draft_operations() { &skip_preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -194,7 +298,6 @@ async fn previews_openapi_and_creates_draft_operations() { &rename_preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "rename".to_owned(), }, @@ -224,7 +327,6 @@ async fn unknown_selected_operations_are_persisted_in_the_canonical_replay() { .unwrap(); let payload = OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned(), "GET /missing".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }; @@ -266,7 +368,6 @@ async fn concurrent_openapi_import_replays_the_same_atomic_result() { let job_id = preview.job_id.as_str().into(); let payload = OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "rename".to_owned(), }; @@ -321,7 +422,6 @@ async fn concurrent_openapi_import_replays_the_same_atomic_result() { &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -363,7 +463,6 @@ async fn apply_fails_closed_when_the_preview_parser_contract_drifts() { &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "rename".to_owned(), }, @@ -440,7 +539,6 @@ async fn apply_fails_closed_for_each_normalization_contract_field_and_digest_sha &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "rename".to_owned(), } @@ -509,7 +607,6 @@ async fn legacy_job_without_normalization_contract_replays_until_expiry() { &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -564,7 +661,6 @@ paths: &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /valid".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, @@ -652,7 +748,6 @@ paths: &valid_preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /valid".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, @@ -671,7 +766,6 @@ paths: &full_preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /valid".to_owned(), "GET /broken".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "rename".to_owned(), }, @@ -753,7 +847,6 @@ components: &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /referenced".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, diff --git a/apps/admin-api/tests/integration/openapi_import/external_references.rs b/apps/admin-api/tests/integration/openapi_import/external_references.rs index e9a6e88..4375871 100644 --- a/apps/admin-api/tests/integration/openapi_import/external_references.rs +++ b/apps/admin-api/tests/integration/openapi_import/external_references.rs @@ -1,11 +1,15 @@ use super::super::common::test_service_with_external_references; use super::*; use axum::{Router, routing::get}; +use sha2::{Digest, Sha256}; use std::sync::{ Arc, atomic::{AtomicUsize, Ordering}, }; +// Serialized by the integration harness at baseline f5f40cb167a82b1e3c1ea2475db5cbaed9d9abfa; this v4 payload preserves the then-current security blocker and digest. +const BASELINE_V4_SECURITY_PREVIEW_PAYLOAD: &str = r###"{"dependencies":[],"dependency_snapshots":[],"normalization":{"config":{"external_references_enabled":false,"max_aliases":128,"max_bytes":262144,"max_collection_items":10000,"max_depth":64,"max_expanded_nodes":100000,"max_external_document_bytes":262144,"max_nodes":50000,"max_reference_depth":32,"max_reference_documents":32,"max_references":4096,"max_scalar_bytes":262144,"normalizer_version":"normalized-ir-v4","projection_version":"preview-v4"},"ir_fingerprint":"ae2319de76c13d631b26843623092d966e5deea7cda9e3e40e4acc6fa36a25a8","normalizer_version":"normalized-ir-v4","projection_version":"preview-v4"},"preview":{"findings":[{"code":"unsupported_security_requirement","message":"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.","operation_key":"GET /secure","severity":"error"}],"groups":[{"key":"imported_operation","operations":[{"category":"imported","description":"Выполняет GET /public","draft":{"category":"imported","display_name":"Public","input_mapping":{},"input_schema":{"description":"Входные параметры MCP-инструмента","nullable":false,"required":true,"type":"object"},"name":"public","output_mapping":{"rules":[{"required":true,"source":"$.response.body","target":"$.output"}]},"output_schema":{"description":"Успешный ответ без тела","nullable":false,"required":true,"type":"null"},"target":{"base_url":"https://api.example.test","method":"GET","path_template":"/public","response_contract":[{"schema_variant":0,"status_code":200}]},"tool_description":{"description":"Выполняет GET /public","examples":[{"input":{}}],"title":"Public"},"wizard_state":{}},"findings":[{"code":"missing_summary","message":"У метода нет summary, отображаемое имя будет сгенерировано автоматически.","operation_key":"GET /public","severity":"warning"},{"code":"missing_description","message":"У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.","operation_key":"GET /public","severity":"warning"},{"code":"missing_response_schema","message":"У метода не найдена схема успешного ответа, результат будет описан как общий объект.","operation_key":"GET /public","severity":"warning"},{"code":"no_input_fields","message":"У инструмента нет входных параметров. Это нормально для справочных методов, но проверьте, что агенту не нужно передавать фильтры.","operation_key":"GET /public","severity":"info"},{"code":"empty_output_schema","message":"В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.","operation_key":"GET /public","severity":"warning"},{"code":"weak_tool_description","message":"Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.","operation_key":"GET /public","severity":"warning"},{"code":"weak_tool_name","message":"Имя инструмента `public` выглядит слишком общим. Лучше использовать имя с конкретным действием и объектом.","operation_key":"GET /public","severity":"warning"}],"input_fields":0,"key":"GET /public","method":"GET","operation_id":"public","output_fields":0,"path":"/public","server_urls":["https://api.example.test"],"suggested_display_name":"Public","suggested_name":"public"},{"category":"imported","description":"Выполняет GET /secure","draft":{"category":"imported","display_name":"Secure","input_mapping":{},"input_schema":{"description":"Входные параметры MCP-инструмента","nullable":false,"required":true,"type":"object"},"name":"secure","output_mapping":{"rules":[{"required":true,"source":"$.response.body","target":"$.output"}]},"output_schema":{"description":"Успешный ответ без тела","nullable":false,"required":true,"type":"null"},"target":{"base_url":"https://api.example.test","method":"GET","path_template":"/secure","response_contract":[{"schema_variant":0,"status_code":200}]},"tool_description":{"description":"Выполняет GET /secure","examples":[{"input":{}}],"title":"Secure"},"wizard_state":{}},"findings":[{"code":"missing_summary","message":"У метода нет summary, отображаемое имя будет сгенерировано автоматически.","operation_key":"GET /secure","severity":"warning"},{"code":"missing_description","message":"У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.","operation_key":"GET /secure","severity":"warning"},{"code":"missing_response_schema","message":"У метода не найдена схема успешного ответа, результат будет описан как общий объект.","operation_key":"GET /secure","severity":"warning"},{"code":"no_input_fields","message":"У инструмента нет входных параметров. Это нормально для справочных методов, но проверьте, что агенту не нужно передавать фильтры.","operation_key":"GET /secure","severity":"info"},{"code":"empty_output_schema","message":"В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.","operation_key":"GET /secure","severity":"warning"},{"code":"weak_tool_description","message":"Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.","operation_key":"GET /secure","severity":"warning"},{"code":"weak_tool_name","message":"Имя инструмента `secure` выглядит слишком общим. Лучше использовать имя с конкретным действием и объектом.","operation_key":"GET /secure","severity":"warning"},{"code":"unsupported_security_requirement","message":"Требование безопасности должно быть привязано к настроенному Auth Profile перед публикацией этой операции; остальные операции не блокируются.","operation_key":"GET /secure","severity":"error"}],"input_fields":0,"key":"GET /secure","method":"GET","operation_id":"secure","output_fields":0,"path":"/secure","server_urls":["https://api.example.test"],"suggested_display_name":"Secure","suggested_name":"secure"}],"title":"Без группы"}],"source":{"format":"openapi","servers":["https://api.example.test"],"title":"Historical security","version":"3.0.3"}},"preview_digest":"85435e01b37d8da35ebc58910404fa364abf55e0089b6593e32fa285c45957c8","source":{"digest":"sha256:a7ef659aff5f5dde6ec38d689746ed4653b1db8038ceb630f6735de16802a999","source_id":"src_openapi_01a0f9b410007852872bb8888caf5690"}}"###; + #[tokio::test] #[serial] async fn external_relative_chain_survives_reversed_builder_order_and_apply_never_refetches() { @@ -35,7 +39,7 @@ async fn external_relative_chain_survives_reversed_builder_order_and_apply_never ); 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 server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); let origin = format!("http://{address}"); let registry = test_registry().await; @@ -99,13 +103,13 @@ paths: &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /items".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, ) .await .unwrap(); + server.abort(); assert_eq!(applied.created.len(), 1); assert_eq!(fetches.load(Ordering::SeqCst), 2); let active_sources: i64 = sqlx::query_scalar( @@ -120,53 +124,80 @@ paths: #[tokio::test] #[serial] -async fn external_literal_credential_is_rejected_before_snapshot_persistence() { - let canary = "external-atdd-secret-canary-2-10"; +async fn external_secret_document_is_rejected_before_blob_registration() { + let external_document = "Item: { type: object, x-api-key: external-secret-canary-42, properties: { id: { type: string } } }"; + let fetched = Arc::new(AtomicUsize::new(0)); + let fetch_count = Arc::clone(&fetched); let app = Router::new().route( "/schemas.yaml", - get(move || async move { - format!( - "Item: {{ type: object, properties: {{ token: {{ type: string, example: '{canary}' }} }} }}" - ) + get(move || { + let fetch_count = Arc::clone(&fetch_count); + async move { + fetch_count.fetch_add(1, Ordering::SeqCst); + external_document + } }), ); 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 server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); let origin = format!("http://{address}"); let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_external_secret_rejected"); let service = test_service_with_external_references( registry.clone(), - test_storage_root("openapi_import_external_literal_credential"), + storage_root.clone(), vec![format!("{origin}/")], ); let workspace_id = WorkspaceId::new("ws_default"); - let upload = OpenApiUpload { - bytes: format!( - r#" + let error = service + .preview_openapi_import( + &workspace_id, + OpenApiUpload { + bytes: format!( + r#" openapi: 3.1.0 -info: {{ title: External secret canary }} +info: {{ title: Safe primary with unsafe external }} +servers: [{{ url: https://api.example.test }}] paths: /items: get: + operationId: listItems responses: '200': description: ok - content: - application/json: - schema: {{ $ref: '{origin}/schemas.yaml#/Item' }} + content: {{ application/json: {{ schema: {{ $ref: '{origin}/schemas.yaml#/Item' }} }} }} "# + ) + .into_bytes(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + }, ) - .into_bytes(), - mime_type: "application/yaml".to_owned(), - locale: OpenApiUploadLocale::En, - }; - - let error = service - .preview_openapi_import(&workspace_id, upload) .await - .unwrap_err(); - assert!(!format!("{error:?}").contains(canary)); + .expect_err("unsafe external bundle must be rejected before source persistence"); + server.abort(); + assert_eq!(fetched.load(Ordering::SeqCst), 1); + let rendered_error = format!("{error:?}"); + assert!(rendered_error.contains("invalid_document")); + assert!(!rendered_error.contains("external-secret-canary-42")); + + // The raw dependency's content-addressed path must not exist even as an + // unregistered blob, and no other stored file may contain its literal. + let external_digest = format!("{:x}", Sha256::digest(external_document.as_bytes())); + let external_blob = storage_root + .join("sha256") + .join(&external_digest[..2]) + .join(&external_digest); + assert!( + !external_blob.exists(), + "secret-bearing external response was written to the artifact store" + ); + assert!(!storage_contains_literal( + &storage_root, + b"external-secret-canary-42" + )); + let dependency_sources: i64 = sqlx::query_scalar( "select count(*) from artifact_sources where workspace_id = $1 and source_id like 'src_openapi_dep_%'", @@ -175,14 +206,29 @@ paths: .fetch_one(registry.pool()) .await .unwrap(); - let import_jobs: i64 = - sqlx::query_scalar("select count(*) from import_jobs where workspace_id = $1") - .bind(workspace_id.as_str()) - .fetch_one(registry.pool()) - .await - .unwrap(); assert_eq!(dependency_sources, 0); - assert_eq!(import_jobs, 0); + let active_sources: i64 = sqlx::query_scalar( + "select count(*) from artifact_sources where workspace_id = $1 and lifecycle = 'active'", + ) + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!(active_sources, 0); +} + +fn storage_contains_literal(root: &std::path::Path, literal: &[u8]) -> bool { + std::fs::read_dir(root).unwrap().any(|entry| { + let path = entry.unwrap().path(); + if path.is_dir() { + storage_contains_literal(&path, literal) + } else { + std::fs::read(path) + .unwrap() + .windows(literal.len()) + .any(|window| window == literal) + } + }) } #[tokio::test] @@ -256,7 +302,6 @@ paths: &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /items".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, @@ -339,7 +384,6 @@ paths: &corrupt_preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /items".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, @@ -386,7 +430,7 @@ async fn external_materialization_uses_one_wall_clock_timeout_and_cleans_cancell ); 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 timeout_server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); let origin = format!("http://{address}"); let registry = test_registry().await; @@ -438,6 +482,7 @@ paths: ) .await .unwrap(); + timeout_server.abort(); assert!(started.elapsed() < std::time::Duration::from_millis(800)); assert_eq!(fetches.load(Ordering::SeqCst), 2); let job = registry @@ -473,7 +518,10 @@ paths: .fetch_one(registry.pool()) .await .unwrap(); - assert_eq!(detached_dependencies, 1); + assert_eq!( + detached_dependencies, 0, + "staged dependencies must not be persisted before the fetch chain completes" + ); } #[tokio::test] @@ -592,7 +640,6 @@ async fn exact_v2_contract_replays_persisted_preview_without_v3_or_dependency_re &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -663,7 +710,6 @@ async fn exact_v3_contract_replays_persisted_preview_without_v4_or_dependency_re &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, @@ -681,54 +727,58 @@ async fn exact_v3_contract_replays_persisted_preview_without_v4_or_dependency_re #[tokio::test] #[serial] -async fn exact_v4_contract_replays_legacy_preview_without_v5_reparse_or_dependency_reads() { - use sha2::Digest; - +async fn exact_v4_security_contract_replays_its_persisted_security_classification() { let registry = test_registry().await; let service = test_service( registry.clone(), - test_storage_root("openapi_import_v4_compatibility"), + test_storage_root("openapi_import_v4_security_compatibility"), test_auth_settings(), test_secret_crypto(), ); let workspace_id = WorkspaceId::new("ws_default"); + let upload = OpenApiUpload { + bytes: br#"{ + "openapi":"3.0.3", + "info":{"title":"Historical security","version":"1"}, + "servers":[{"url":"https://api.example.test"}], + "components":{"securitySchemes":{"basic":{"type":"http","scheme":"basic"}}}, + "security":[{"basic":[]}], + "paths":{ + "/secure":{"get":{"operationId":"secure","responses":{"200":{"description":"ok"}}}}, + "/public":{"get":{"operationId":"public","security":[],"responses":{"200":{"description":"ok"}}}} + } + }"# + .to_vec(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; let preview = service - .preview_openapi_import(&workspace_id, openapi_upload()) + .preview_openapi_import(&workspace_id, upload) .await .unwrap(); let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into(); - let mut job = registry + let job = registry .get_import_job(&workspace_id, &job_id) .await .unwrap() .unwrap(); - let missing_dependency = serde_json::json!([{ - "source_id": "src_openapi_dep_v4_must_not_be_read", - "digest": format!("sha256:{}", "0".repeat(64)), - "canonical_uri": "https://schemas.example.test/v4.yaml" - }]); - let mut persisted_preview = job.preview_payload["preview"].clone(); - for group in persisted_preview["groups"].as_array_mut().unwrap() { - for operation in group["operations"].as_array_mut().unwrap() { - let object = operation.as_object_mut().unwrap(); - object.remove("security_requirements"); - object.remove("security_schemes"); - } - } - let expected_preview_digest = format!( - "{:x}", - sha2::Sha256::digest(serde_json::to_vec(&persisted_preview).unwrap()) + // Keep the genuine baseline-v4 normalization/projection, preview and digest intact. + // Only bind its source envelope to this test's newly registered immutable source blob. + let mut payload: serde_json::Value = + serde_json::from_str(BASELINE_V4_SECURITY_PREVIEW_PAYLOAD).unwrap(); + assert_eq!( + payload.pointer("/source/digest"), + job.preview_payload.pointer("/source/digest"), + "baseline fixture must refer to the exact uploaded source bytes", ); - job.preview_payload["preview"] = persisted_preview; - job.preview_payload["preview_digest"] = serde_json::json!(expected_preview_digest); - job.preview_payload["normalization"]["normalizer_version"] = - serde_json::json!("normalized-ir-v4"); - job.preview_payload["normalization"]["projection_version"] = serde_json::json!("preview-v4"); - job.preview_payload["normalization"]["ir_fingerprint"] = serde_json::json!("a".repeat(64)); - job.preview_payload["dependency_snapshots"] = missing_dependency; - sqlx::query("update import_jobs set preview_payload = $1 where workspace_id = $2 and id = $3") - .bind(&job.preview_payload) - .bind(workspace_id.as_str()) + payload["source"]["source_id"] = job.preview_payload["source"]["source_id"].clone(); + assert_eq!( + payload["normalization"]["normalizer_version"], + "normalized-ir-v4" + ); + assert_eq!(payload["normalization"]["projection_version"], "preview-v4"); + sqlx::query("update import_jobs set preview_payload = $1::jsonb where id = $2") + .bind(serde_json::to_string(&payload).unwrap()) .bind(job_id.as_str()) .execute(registry.pool()) .await @@ -739,19 +789,25 @@ async fn exact_v4_contract_replays_legacy_preview_without_v5_reparse_or_dependen &workspace_id, &job_id, OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /v2/latest".to_owned()], - auth_mappings: vec![], - server_url: Some("https://api.frankfurter.dev".to_owned()), + selected_operation_keys: vec!["GET /secure".to_owned()], + server_url: Some("https://api.example.test".to_owned()), conflict_mode: "skip".to_owned(), }, ) - .await - .unwrap(); - assert_eq!(result.created.len(), 1); - let completed = registry - .get_import_job(&workspace_id, &job_id) - .await - .unwrap() - .unwrap(); - assert_eq!(completed.status, ImportJobStatus::Completed); + .await; + let error = + result.expect_err("historical v4 security blocker must refuse the selected candidate"); + assert!( + format!("{error:?}").contains("invalid_document"), + "v4 contract was rejected before candidate validation: {error:?}" + ); + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); } + +mod security_bundle; diff --git a/apps/admin-api/tests/integration/openapi_import/external_references/security_bundle.rs b/apps/admin-api/tests/integration/openapi_import/external_references/security_bundle.rs new file mode 100644 index 0000000..12a3bef --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/external_references/security_bundle.rs @@ -0,0 +1,413 @@ +use super::*; + +// --- Referenced security credentials RED regressions --- +// These tests prove that credential literals hidden behind local/external $ref +// security scheme + parameter splits are rejected before artifact persistence. + +#[tokio::test] +#[serial] +async fn referenced_security_credentials_local_scheme_ref_with_parameter_default_canary_rejected() { + let canary = b"local-ref-canary-001-Zx9qK"; + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_local_scheme_ref_canary"); + let service = test_service( + registry.clone(), + storage_root.clone(), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let upload = OpenApiUpload { + bytes: r##"{ + "openapi":"3.1.0", + "info":{"title":"Local ref scheme","version":"1"}, + "servers":[{"url":"https://api.example.test"}], + "x-scheme":{"type":"apiKey","in":"header","name":"X-Custom"}, + "x-key":{"type":"string","default":"local-ref-canary-001-Zx9qK"}, + "components":{ + "securitySchemes":{"custom":{"$ref":"#/x-scheme"}} + }, + "security":[{"custom":[]}], + "paths":{ + "/test":{"get":{ + "operationId":"test", "security":[{"custom":[]}], + "parameters":[{"name":"X-Custom","in":"header","schema":{"$ref":"#/x-key"}}], + "responses":{"200":{"description":"ok"}} + }} + } + }"## + .as_bytes() + .to_vec(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; + let result = service.preview_openapi_import(&workspace_id, upload).await; + + // Canary must NOT appear in the API response (error or preview). + let response_repr = match &result { + Ok(preview) => serde_json::to_string(preview).unwrap(), + Err(err) => format!("{err:?}"), + }; + assert!( + response_repr.contains("invalid_document"), + "unsafe local ref bundle was not rejected: {response_repr}" + ); + assert!( + !response_repr.contains(std::str::from_utf8(canary).unwrap()), + "credential canary leaked into API response" + ); + + // Canary must NOT be persisted to artifact storage. + assert!( + !storage_contains_literal(&storage_root, canary), + "raw credential literal from referenced local scheme ref persisted to storage" + ); + assert_rejected_bundle_unpersisted(®istry, &workspace_id, &storage_root).await; +} + +#[tokio::test] +#[serial] +async fn encoded_access_token_fragment_is_rejected_before_persistence() { + let canary = b"fragment-ingress-canary-004-Qm7vN"; + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_encoded_fragment_canary"); + let service = test_service( + registry.clone(), + storage_root.clone(), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let upload = OpenApiUpload { + bytes: br##"{ + "openapi":"3.1.0", + "info":{"title":"Encoded fragment credential","version":"1"}, + "servers":[{"url":"https://api.example.test/v1#%61%63%63%65%73%73%5F%74%6F%6B%65%6E=fragment-ingress-canary-004-Qm7vN"}], + "paths":{"/health":{"get":{"responses":{"200":{"description":"ok"}}}}} + }"## + .to_vec(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; + + let error = service + .preview_openapi_import(&workspace_id, upload) + .await + .expect_err("encoded access_token fragment must be rejected at ingress"); + let rendered_error = format!("{error:?}"); + assert!(rendered_error.contains("invalid_document")); + assert!(rendered_error.len() <= 512, "error must remain bounded"); + assert!(!rendered_error.contains("fragment-ingress-canary-004-Qm7vN")); + assert!(!rendered_error.contains("%61%63%63%65%73%73%5F%74%6F%6B%65%6E")); + assert_rejected_bundle_unpersisted(®istry, &workspace_id, &storage_root).await; + assert!(!storage_contains_literal(&storage_root, canary)); +} + +#[tokio::test] +#[serial] +async fn referenced_security_credentials_external_scheme_ref_local_parameter_default_canary_rejected() + { + let canary = b"split-ext-canary-002-Yw8pJ"; + // External document defines the apiKey security scheme with header X-Custom. + let external_doc = r#"{"x-scheme":{"type":"apiKey","in":"header","name":"X-Custom"}}"#; + let fetched = Arc::new(AtomicUsize::new(0)); + let fetch_count = Arc::clone(&fetched); + let app = Router::new().route( + "/scheme.yaml", + get(move || { + let fc = Arc::clone(&fetch_count); + async move { + fc.fetch_add(1, Ordering::SeqCst); + external_doc.to_owned() + } + }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + let origin = format!("http://{address}"); + + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_ext_scheme_ref_canary"); + let service = test_service_with_external_references( + registry.clone(), + storage_root.clone(), + vec![format!("{origin}/")], + ); + let workspace_id = WorkspaceId::new("ws_default"); + // Primary references the external scheme; contains a local parameter + // with name X-Custom and schema.default = canary. + let upload = OpenApiUpload { + bytes: format!( + r#"{{ + "openapi":"3.1.0", + "info":{{"title":"Ext scheme ref","version":"1"}}, + "servers":[{{"url":"https://api.example.test"}}], + "components":{{ + "securitySchemes":{{"custom":{{"$ref":"{origin}/scheme.yaml#/x-scheme"}}}} + }}, + "security":[{{"custom":[]}}], + "paths":{{ + "/test":{{"get":{{ + "operationId":"test", "security":[{{"custom":[]}}], + "parameters":[{{"name":"X-Custom","in":"header","schema":{{"type":"string","default":"{canary}"}}}}], + "responses":{{"200":{{"description":"ok"}}}} + }}}} + }} + }}"#, + origin = origin, + canary = std::str::from_utf8(canary).unwrap(), + ) + .into_bytes(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; + let result = service.preview_openapi_import(&workspace_id, upload).await; + server.abort(); + assert_eq!( + fetched.load(Ordering::SeqCst), + 1, + "external scheme must be fetched before the split is checked" + ); + + let response_repr = match &result { + Ok(preview) => serde_json::to_string(preview).unwrap(), + Err(err) => format!("{err:?}"), + }; + assert!( + response_repr.contains("invalid_document"), + "unsafe external scheme bundle was not rejected: {response_repr}" + ); + assert!( + !response_repr.contains(std::str::from_utf8(canary).unwrap()), + "credential canary leaked into API response" + ); + assert!( + !storage_contains_literal(&storage_root, canary), + "raw credential literal from split external scheme ref persisted to storage" + ); + assert_rejected_bundle_unpersisted(®istry, &workspace_id, &storage_root).await; +} + +#[tokio::test] +#[serial] +async fn referenced_security_credentials_local_scheme_external_parameter_default_canary_rejected() { + let canary = b"split-local-canary-003-Xv7oI"; + // External document defines a parameter schema whose default is the canary. + let external_doc = format!( + r#"{{"x-key":{{"type":"string","default":"{canary}"}}}}"#, + canary = std::str::from_utf8(canary).unwrap(), + ); + let fetched = Arc::new(AtomicUsize::new(0)); + let fetch_count = Arc::clone(&fetched); + let app = Router::new().route( + "/key.yaml", + get(move || { + let fc = Arc::clone(&fetch_count); + async move { + fc.fetch_add(1, Ordering::SeqCst); + external_doc.clone() + } + }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + let origin = format!("http://{address}"); + + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_local_scheme_ext_param_canary"); + let service = test_service_with_external_references( + registry.clone(), + storage_root.clone(), + vec![format!("{origin}/")], + ); + let workspace_id = WorkspaceId::new("ws_default"); + // Primary declares the scheme directly (no $ref); parameter schema is + // an external $ref whose resolved default would be the canary. + let upload = OpenApiUpload { + bytes: format!( + r#"{{ + "openapi":"3.1.0", + "info":{{"title":"Local scheme ext param","version":"1"}}, + "servers":[{{"url":"https://api.example.test"}}], + "components":{{ + "securitySchemes":{{"custom":{{"type":"apiKey","in":"header","name":"X-Custom"}}}} + }}, + "security":[{{"custom":[]}}], + "paths":{{ + "/test":{{"get":{{ + "operationId":"test", "security":[{{"custom":[]}}], + "parameters":[{{"name":"X-Custom","in":"header","schema":{{"$ref":"{origin}/key.yaml#/x-key"}}}}], + "responses":{{"200":{{"description":"ok"}}}} + }}}} + }} + }}"#, + origin = origin, + ) + .into_bytes(), + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + }; + let result = service.preview_openapi_import(&workspace_id, upload).await; + server.abort(); + assert_eq!( + fetched.load(Ordering::SeqCst), + 1, + "external parameter must be fetched before the split is checked" + ); + + let response_repr = match &result { + Ok(preview) => serde_json::to_string(preview).unwrap(), + Err(err) => format!("{err:?}"), + }; + assert!( + response_repr.contains("invalid_document"), + "unsafe external parameter bundle was not rejected: {response_repr}" + ); + assert!( + !response_repr.contains(std::str::from_utf8(canary).unwrap()), + "credential canary leaked into API response" + ); + assert!( + !storage_contains_literal(&storage_root, canary), + "raw credential literal from external referenced parameter persisted to storage" + ); + assert_rejected_bundle_unpersisted(®istry, &workspace_id, &storage_root).await; +} + +#[tokio::test] +#[serial] +async fn safe_external_security_scheme_and_parameter_are_preserved() { + let external_doc = r#"{"x-scheme":{"type":"apiKey","in":"header","name":"X-Custom"}}"#; + let fetched = Arc::new(AtomicUsize::new(0)); + let fetch_count = Arc::clone(&fetched); + let app = Router::new().route( + "/scheme.yaml", + get(move || { + let count = Arc::clone(&fetch_count); + async move { + count.fetch_add(1, Ordering::SeqCst); + external_doc + } + }), + ); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + let origin = format!("http://{address}"); + + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_safe_external_security_ref"); + let service = test_service_with_external_references( + registry.clone(), + storage_root, + vec![format!("{origin}/")], + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import( + &workspace_id, + OpenApiUpload { + bytes: format!( + r#"openapi: 3.1.0 +info: {{ title: Safe external scheme, version: '1' }} +servers: [{{ url: https://api.example.test }}] +components: + securitySchemes: + custom: {{ $ref: '{origin}/scheme.yaml#/x-scheme' }} +security: [{{ custom: [] }}] +paths: + /test: + get: + operationId: safeExternalSecurity + security: [{{ custom: [] }}] + parameters: + - name: X-Custom + in: header + schema: {{ type: string }} + - name: locale + in: query + schema: {{ type: string, default: en }} + responses: + '200': {{ description: ok }} +"# + ) + .into_bytes(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + }, + ) + .await + .expect("safe referenced schemes and parameters remain importable"); + server.abort(); + assert_eq!(fetched.load(Ordering::SeqCst), 1); + let operation = preview + .preview + .groups + .iter() + .flat_map(|group| &group.operations) + .find(|operation| operation.key == "GET /test") + .expect("operation preview is preserved"); + assert!(matches!( + &operation.security_schemes["custom"].kind, + crank_import::rest::NormalizedSecuritySchemeKind::ApiKey { + parameter_name, + location: crank_import::rest::SecurityParameterLocation::Header, + .. + } if parameter_name == "X-Custom" + )); + let input_schema = serde_json::to_value(&operation.draft.input_schema).unwrap(); + assert_eq!( + input_schema.pointer("/fields/locale/default_value"), + Some(&serde_json::json!("en")), + "benign locale default must remain visible in the preview" + ); + let job = registry + .get_import_job(&workspace_id, &preview.job_id.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!( + job.preview_payload["dependency_snapshots"] + .as_array() + .unwrap() + .len(), + 1 + ); +} + +async fn assert_rejected_bundle_unpersisted( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, + storage_root: &std::path::Path, +) { + let sources: i64 = + sqlx::query_scalar("select count(*) from artifact_sources where workspace_id = $1") + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + let jobs: i64 = sqlx::query_scalar("select count(*) from import_jobs where workspace_id = $1") + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!(sources, 0, "rejected bundle registered a source"); + assert_eq!(jobs, 0, "rejected bundle created an import job"); + assert!( + !storage_has_files(storage_root), + "rejected bundle stored artifact bytes" + ); +} + +fn storage_has_files(path: &std::path::Path) -> bool { + std::fs::read_dir(path).unwrap().any(|entry| { + let path = entry.unwrap().path(); + if path.is_dir() { + storage_has_files(&path) + } else { + true + } + }) +} diff --git a/apps/admin-api/tests/integration/openapi_import/http_contract.rs b/apps/admin-api/tests/integration/openapi_import/http_contract.rs index 34ee401..1801f9f 100644 --- a/apps/admin-api/tests/integration/openapi_import/http_contract.rs +++ b/apps/admin-api/tests/integration/openapi_import/http_contract.rs @@ -26,10 +26,17 @@ async fn http_contract_preview_apply_and_replay_preserve_json_multipart_schemas_ .unwrap() .preview_payload["preview"] .clone(); - assert_eq!( - persisted_preview_before_apply, - serde_json::to_value(&preview.preview).unwrap() - ); + // Readiness is workspace-aware response metadata, not part of the immutable + // projection persisted with the source job. + let mut response_projection = serde_json::to_value(&preview.preview).unwrap(); + for group in response_projection["groups"].as_array_mut().unwrap() { + for operation in group["operations"].as_array_mut().unwrap() { + assert_eq!(operation["status"], "actionable"); + operation.as_object_mut().unwrap().remove("status"); + } + } + assert!(preview.preview.conflicts.is_empty()); + assert_eq!(persisted_preview_before_apply, response_projection); let operations = preview .preview @@ -108,7 +115,6 @@ async fn http_contract_preview_apply_and_replay_preserve_json_multipart_schemas_ let payload = OpenApiImportCreatePayload { selected_operation_keys: vec![candidate.key.clone()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }; @@ -200,7 +206,6 @@ async fn xml_and_unsupported_media_block_apply_without_registry_mutation() { &preview.job_id.as_str().into(), OpenApiImportCreatePayload { selected_operation_keys: vec![candidate.key.clone()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, 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..4c26bea --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_apply.rs @@ -0,0 +1,991 @@ +use serde_json::{Value, json}; +use serial_test::serial; +use std::{sync::Arc, time::Duration}; + +use async_trait::async_trait; +use crank_community_mcp::session::InMemorySessionStore; +use crank_core::{ + MachineAccessMode, MachineCredentialVerifier, MachineCredentialVerifierError, + OperationSecurityLevel, PlatformApiKeyScope, VerifiedMachineCredential, +}; + +struct CorpusMcpCredentialVerifier; + +#[async_trait] +impl MachineCredentialVerifier for CorpusMcpCredentialVerifier { + async fn verify_bearer_token( + &self, + _workspace_slug: &str, + _agent_slug: &str, + token: &str, + ) -> Result, MachineCredentialVerifierError> { + Ok( + (token == "corpus-test-token").then_some(VerifiedMachineCredential { + machine_access_mode: MachineAccessMode::StaticAgentKey, + max_security_level: OperationSecurityLevel::Standard, + scopes: vec![PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], + platform_api_key_id: None, + }), + ) + } +} + +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_TWO_APIKEY_OPERATIONS: &str = r#" +openapi: 3.0.3 +info: { title: Authenticated Apply test, version: '1' } +servers: [{ url: https://api.example.test }] +components: { securitySchemes: { apiKey: { type: apiKey, in: header, name: X-Api-Key } } } +security: [{ apiKey: [] }] +paths: + /alpha: { get: { operationId: alpha, responses: { '200': { description: ok } } } } + /beta: { get: { operationId: beta, responses: { '200': { description: ok } } } } +"#; + +const OPENAPI_SINGLE_APIKEY_OPERATION: &str = r#" +openapi: 3.0.3 +info: { title: Authenticated Apply test, version: '1' } +servers: [{ url: https://api.example.test }] +components: { securitySchemes: { apiKey: { type: apiKey, in: header, name: X-Api-Key } } } +paths: + /alpha: { get: { operationId: alpha, security: [{ apiKey: [] }], 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, + preview: 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, + preview, + } +} + +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(), + preview: plan.preview.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_APIKEY_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_APIKEY_OPERATION, + 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_APIKEY_OPERATION.replace("X-Api-Key", "X-Changed-Key"), + 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"); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn profiles_sharing_active_secret_are_selectable_and_both_block_after_disable() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry.clone(), + test_storage_root("import_plan_shared_auth_secret"), + )) + .await; + let client = authorized_client(&base_url).await; + let secret_response = client + .post(format!("{base_url}/secrets")) + .json(&json!({ + "name": "shared-import-auth-token", + "kind": "token", + "value": { "token": "shared-import-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 make_profile = |name: &str| { + client + .post(format!("{base_url}/auth-profiles")) + .json(&json!({ + "name": name, + "kind": "api_key_header", + "config": { "api_key_header": { + "header_name": "X-Api-Key", + "secret_id": secret_id + }} + })) + }; + let first: Value = make_profile("shared-auth-a") + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + let second: Value = make_profile("shared-auth-b") + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + let document = r#"{ + "openapi":"3.0.3", + "info":{"title":"Shared profile test","version":"1"}, + "servers":[{"url":"https://api.example.test"}], + "components":{"securitySchemes":{"key":{"type":"apiKey","in":"header","name":"X-Api-Key"}}}, + "paths":{ + "/a":{"get":{"operationId":"sharedA","security":[{"key":[]}],"responses":{"200":{"description":"ok"}}}}, + "/b":{"get":{"operationId":"sharedB","security":[{"key":[]}],"responses":{"200":{"description":"ok"}}}} + } + }"#; + let plan = create_plan( + &client, + &base_url, + document, + json!({ + "selection_mode":"full", + "selected_operation_keys":[], + "decisions":[ + {"operation_key":"GET /a","action":"create","auth_profile_id":first["id"]}, + {"operation_key":"GET /b","action":"create","auth_profile_id":second["id"]} + ] + }), + ) + .await; + let (status, report) = apply_plan(&client, &base_url, &plan, "shared-secret-active").await; + assert_eq!(status, reqwest::StatusCode::OK, "{report}"); + assert_eq!(operation_list(&client, &base_url).await["total"], 2); + + let first_id = first["id"].as_str().unwrap(); + let second_id = second["id"].as_str().unwrap(); + let disabled_document = document + .replace("sharedA", "sharedAAfterDisable") + .replace("sharedB", "sharedBAfterDisable"); + let mut disabled_plans = Vec::new(); + for (key, profile_id) in [("GET /a", first_id), ("GET /b", second_id)] { + disabled_plans.push(( + key, + create_plan( + &client, + &base_url, + &disabled_document, + json!({ + "selection_mode":"partial", + "selected_operation_keys":[key], + "decisions":[{"operation_key":key,"action":"create","auth_profile_id":profile_id}] + }), + ) + .await, + )); + } + 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(); + for (key, plan) in disabled_plans { + let (status, error) = apply_plan( + &client, + &base_url, + &plan, + &format!("shared-secret-disabled-{key}"), + ) + .await; + assert_eq!(status, reqwest::StatusCode::CONFLICT, "{error}"); + assert_eq!( + error["error"]["context"]["error_code"], + "import_plan_auth_unavailable" + ); + assert_eq!(operation_list(&client, &base_url).await["total"], 2); + } +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn ac_4_corpus_operation_completes_the_real_http_to_mcp_workflow() { + use axum::{Json, Router}; + + let registry = test_registry().await; + let (request_tx, mut request_rx) = tokio::sync::mpsc::unbounded_channel(); + let upstream = + Router::new().fallback(move |method: axum::http::Method, uri: axum::http::Uri| { + let request_tx = request_tx.clone(); + async move { + request_tx.send((method, uri)).unwrap(); + Json(json!({"id":"corpus-0"})) + } + }); + let upstream_base = spawn_admin_api(upstream).await; + let storage = test_storage_root("corpus_ac4"); + let base_url = spawn_admin_api(build_test_app(registry.clone(), storage)).await; + let client = authorized_client(&base_url).await; + let fixture_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../crates/crank-import/tests/corpus/representative-00.json"); + let mut fixture: Value = serde_json::from_slice(&std::fs::read(fixture_path).unwrap()).unwrap(); + let upstream_origin = upstream_base + .as_ref() + .split("/api/admin/workspaces/") + .next() + .unwrap(); + fixture["servers"] = json!([{ "url": upstream_origin }]); + let document = serde_json::to_string(&fixture).unwrap(); + let plan = create_plan(&client, &base_url, &document, full_plan()).await; + assert_eq!(plan.body["counts"]["actionable"], 1); + let (status, report) = apply_plan(&client, &base_url, &plan, "corpus-ac4-import").await; + assert_eq!(status, reqwest::StatusCode::OK, "Apply failed: {report}"); + assert!(report_has_outcome_status(&report, "created")); + let operations = operation_list(&client, &base_url).await; + assert_eq!(operations["total"], 1); + assert_eq!(operations["items"][0]["status"], "draft"); + let operation_id = operations["items"][0]["id"].as_str().unwrap().to_owned(); + + // The admin test run performs the persisted Draft validation and a real + // HTTP execution against the local fixture server before publication. + let test_run = client + .post(format!("{base_url}/operations/{operation_id}/test-runs")) + .json(&json!({"version":1,"input":{}})) + .send() + .await + .unwrap(); + assert_eq!(test_run.status(), reqwest::StatusCode::OK); + let test_result: Value = test_run.json().await.unwrap(); + assert_eq!(test_result["ok"], true, "Draft test failed: {test_result}"); + let published = 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 + .unwrap(); + assert_eq!(published.status(), reqwest::StatusCode::OK); + + let agent = client.post(format!("{base_url}/agents")).json(&json!({ + "slug":"corpus-regression-agent","display_name":"Corpus Regression Agent", + "description":"Local corpus acceptance fixture","instructions":{},"tool_selection_policy":{} + })).send().await.unwrap().json::().await.unwrap(); + let agent_id = agent["agent_id"].as_str().unwrap(); + let bindings = client.post(format!("{base_url}/agents/{agent_id}/bindings")) + .header(reqwest::header::IF_MATCH, agent_etag(&client, &base_url, agent_id).await) + .json(&json!([{"operation_id":operation_id,"operation_version":1,"tool_name":"corpusOperation00","tool_title":"Synthetic operation 00","enabled":true}])) + .send().await.unwrap(); + assert_eq!(bindings.status(), reqwest::StatusCode::OK); + let agent_publish = client + .post(format!("{base_url}/agents/{agent_id}/publish")) + .header( + reqwest::header::IF_MATCH, + agent_etag(&client, &base_url, agent_id).await, + ) + .json(&json!({"version":1})) + .send() + .await + .unwrap(); + assert_eq!(agent_publish.status(), reqwest::StatusCode::OK); + + let mcp = crank_community_mcp::build_app( + registry, + Duration::ZERO, + None, + test_secret_crypto(), + crank_runtime::community_with_outbound_policy( + crank_runtime::OutboundHttpPolicy::allowing_hosts(["127.0.0.1"]), + ) + .build(), + crank_runtime::RequestRateLimiter::new( + crank_runtime::RequestRateLimitConfig::new(100, 100).unwrap(), + ), + Arc::new(crank_runtime::InMemoryCoordinationStateStore::default()), + Arc::new(InMemorySessionStore::default()), + Arc::new(CorpusMcpCredentialVerifier), + ); + 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, mcp).await.unwrap(); + }); + let mcp_base = format!("http://{address}"); + let mcp_url = format!("{mcp_base}/v1/default/corpus-regression-agent"); + let initialized = client.post(&mcp_url).bearer_auth("corpus-test-token") + .header("accept", "application/json, text/event-stream") + .json(&json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"corpus","version":"1"}}})) + .send().await.unwrap(); + assert_eq!(initialized.status(), reqwest::StatusCode::OK); + let session = initialized + .headers() + .get("mcp-session-id") + .unwrap() + .to_str() + .unwrap() + .to_owned(); + let notification = client + .post(&mcp_url) + .bearer_auth("corpus-test-token") + .header("accept", "application/json, text/event-stream") + .header("mcp-session-id", &session) + .header("mcp-protocol-version", "2025-11-25") + .json(&json!({"jsonrpc":"2.0","method":"notifications/initialized","params":{}})) + .send() + .await + .unwrap(); + assert_eq!(notification.status(), reqwest::StatusCode::ACCEPTED); + let called = client.post(&mcp_url).bearer_auth("corpus-test-token") + .header("accept", "application/json, text/event-stream").header("mcp-session-id", session) + .header("mcp-protocol-version", "2025-11-25") + .json(&json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"corpusOperation00","arguments":{}}})) + .send().await.unwrap(); + assert_eq!(called.status(), reqwest::StatusCode::OK); + let call: Value = called.json().await.unwrap(); + assert_eq!(call["result"]["isError"], false, "MCP call failed: {call}"); + assert_eq!(call["result"]["structuredContent"]["id"], "corpus-0"); + for stage in ["Draft test run", "MCP call"] { + let (method, uri) = request_rx + .try_recv() + .unwrap_or_else(|_| panic!("{stage} missed upstream")); + assert_eq!(method, axum::http::Method::GET, "{stage}"); + assert_eq!(uri.path(), "/items/0", "{stage}"); + } +} + +mod corpus_hostile; +mod locale; +mod security_source; +mod support_matrix; diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_apply/corpus_hostile.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/corpus_hostile.rs new file mode 100644 index 0000000..8f4cfdb --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/corpus_hostile.rs @@ -0,0 +1,292 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn ac_5_hostile_corpus_cases_are_bounded_atomic_and_redacted() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app(registry, test_storage_root("corpus_ac5"))).await; + let client = authorized_client(&base_url).await; + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../crates/crank-import/tests/corpus"); + let hostile_xml = std::fs::read_to_string(root.join("hostile-xml.json")).unwrap(); + let before = operation_list(&client, &base_url).await; + let started = std::time::Instant::now(); + let plan = create_plan(&client, &base_url, &hostile_xml, full_plan()).await; + assert!( + started.elapsed() < Duration::from_secs(5), + "hostile document exceeded bounded test budget" + ); + assert!( + plan.body["counts"]["blocked"].as_u64().unwrap_or(0) > 0, + "hostile XML must be a blocker: {}", + plan.body + ); + let xml_candidate = &plan.preview["preview"]["groups"][0]["operations"][0]; + assert_eq!(xml_candidate["key"], "GET /xml"); + assert!( + xml_candidate["findings"] + .as_array() + .is_some_and(|findings| findings.iter().any(|finding| finding["code"] + == "unsupported_xml_response_media_type" + && finding["severity"] == "error")), + "HTTP preview must expose the exact XML blocker: {}", + plan.preview + ); + assert_eq!( + plan.body["candidates"][0]["reason"], "unsupported_xml_response_media_type", + "HTTP plan must retain the XML blocker: {}", + plan.body + ); + let (status, report) = apply_plan(&client, &base_url, &plan, "corpus-ac5-hostile").await; + assert_eq!( + status, + reqwest::StatusCode::CONFLICT, + "blocker must reject atomically: {report}" + ); + assert_eq!(operation_list(&client, &base_url).await, before); + + // Credential-bearing sources are rejected before their original bytes + // can enter artifact storage, rather than producing a persisted plan. + for file in [ + "hostile-secret.json", + "hostile-malformed.json", + "hostile-oversized.json", + ] { + let source = std::fs::read(root.join(file)).unwrap(); + let started = std::time::Instant::now(); + let upload = reqwest::multipart::Part::bytes(source) + .file_name(file) + .mime_str("application/json") + .unwrap(); + let response = client + .post(format!("{base_url}/imports/openapi/preview")) + .multipart(reqwest::multipart::Form::new().part("file", upload)) + .send() + .await + .unwrap(); + let status = response.status(); + let body = response.text().await.unwrap_or_default(); + assert!( + !status.is_success(), + "{file} unexpectedly passed preview: {body}" + ); + assert!( + started.elapsed() < Duration::from_secs(5), + "{file} exceeded bounded upload/parse time" + ); + assert!( + !body.contains("sk_test_corpus_secret_canary"), + "hostile error response leaked a secret canary" + ); + assert_eq!( + operation_list(&client, &base_url).await, + before, + "{file} mutated the operation registry" + ); + } + + // Real HTTP preview+plan: a root security requirement combined with an + // explicit empty operation override must leave the public operation + // actionable and leave the inherited one unresolved without a profile. + let mixed_security: Value = serde_json::json!({ + "openapi": "3.0.3", + "info": { "title": "Mixed security plan probe", "version": "1" }, + "servers": [{ "url": "https://security.example.test" }], + "components": { "securitySchemes": { "probe": { "type": "http", "scheme": "basic" } } }, + "security": [{ "probe": [] }], + "paths": { + "/public": { "get": { "security": [], "operationId": "securityPublic", + "responses": { "200": { "description": "ok" } } } }, + "/secured": { "get": { "operationId": "securitySecured", + "responses": { "200": { "description": "ok" } } } } + } + }); + let security_mixed = serde_json::to_string(&mixed_security).unwrap(); + let security_plan = create_plan(&client, &base_url, &security_mixed, full_plan()).await; + assert_eq!( + security_plan.body["counts"]["selected"], 2, + "{}", + security_plan.body + ); + assert_eq!( + security_plan.body["counts"]["actionable"], 1, + "{}", + security_plan.body + ); + assert_eq!( + security_plan.body["counts"]["blocked"], 1, + "{}", + security_plan.body + ); + let security_candidates = security_plan.body["candidates"].as_array().unwrap(); + let by_key = |key: &str| { + security_candidates + .iter() + .find(|candidate| candidate["key"] == key) + .unwrap_or_else(|| panic!("candidate {key} missing: {}", security_plan.body)) + }; + let public = by_key("GET /public"); + let secured = by_key("GET /secured"); + assert_eq!( + public["state"].as_str(), + Some("actionable"), + "public candidate with empty security override must stay actionable: {}", + security_plan.body + ); + assert!( + public["reason"].is_null(), + "public candidate must not carry a security blocker: {}", + security_plan.body + ); + assert_eq!( + secured["state"].as_str(), + Some("blocked"), + "inherited security requirement must block the affected candidate: {}", + security_plan.body + ); + assert_eq!( + secured["reason"].as_str(), + Some("auth_profile_required"), + "secured candidate must expose its unresolved binding: {}", + security_plan.body + ); + assert_eq!( + operation_list(&client, &base_url).await, + before, + "security-scoped plan must not mutate the operation registry" + ); + let (blocked_status, blocked_report) = apply_plan( + &client, + &base_url, + &security_plan, + "corpus-ac5-security-blocked", + ) + .await; + assert_eq!( + blocked_status, + reqwest::StatusCode::CONFLICT, + "{blocked_report}" + ); + assert_eq!(operation_list(&client, &base_url).await, before); + + let public_plan = create_plan( + &client, + &base_url, + &security_mixed, + json!({ + "selection_mode": "partial", + "selected_operation_keys": ["GET /public"] + }), + ) + .await; + assert_eq!(public_plan.body["counts"]["actionable"], 1); + assert_eq!(public_plan.body["counts"]["blocked"], 0); + let (public_status, public_report) = apply_plan( + &client, + &base_url, + &public_plan, + "corpus-ac5-security-public", + ) + .await; + assert_eq!(public_status, reqwest::StatusCode::OK, "{public_report}"); + assert!(report_has_outcome_status(&public_report, "created")); + let after = operation_list(&client, &base_url).await; + assert_eq!(after["total"], 1, "{after}"); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn p0_api_malformed_local_security_is_atomic_across_openapi_versions() { + for (version, document) in [ + ( + "openapi-3.1", + serde_json::json!({ + "openapi": "3.1.0", + "info": { "title": "Security API regression", "version": "1" }, + "servers": [{ "url": "https://security.example.test" }], + "security": [{ "probe": [] }], + "paths": { + "/bad": { "get": { "security": "basic", "operationId": "badOp", + "responses": { "200": { "description": "ok" } } } }, + "/public": { "get": { "security": [], "operationId": "publicOp", + "responses": { "200": { "description": "ok" } } } } + } + }), + ), + ( + "swagger-2.0", + serde_json::json!({ + "swagger": "2.0", + "info": { "title": "Security API regression", "version": "1" }, + "host": "security.example.test", + "schemes": ["https"], + "security": [{ "apiKey": [] }], + "paths": { + "/bad": { "get": { "security": "apikey", "operationId": "badOp", + "responses": { "200": { "description": "ok" } } } }, + "/public": { "get": { "security": [], "operationId": "publicOp", + "responses": { "200": { "description": "ok" } } } } + } + }), + ), + ] { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root(&format!("security_api_{version}")), + )) + .await; + let client = authorized_client(&base_url).await; + let source = serde_json::to_string(&document).unwrap(); + let before = operation_list(&client, &base_url).await; + + let plan = create_plan(&client, &base_url, &source, full_plan()).await; + assert_eq!( + plan.body["counts"]["actionable"], 1, + "{version}: {}", + plan.body + ); + assert_eq!( + plan.body["counts"]["blocked"], 1, + "{version}: {}", + plan.body + ); + let candidates = plan.body["candidates"].as_array().unwrap(); + let bad = candidates + .iter() + .find(|item| item["key"] == "GET /bad") + .unwrap(); + let public = candidates + .iter() + .find(|item| item["key"] == "GET /public") + .unwrap(); + assert_eq!(bad["state"], "blocked", "{version}: {}", plan.body); + assert_eq!(bad["reason"], "invalid_security_requirements"); + assert_eq!(public["state"], "actionable", "{version}: {}", plan.body); + assert!(public["reason"].is_null()); + + let (status, report) = apply_plan(&client, &base_url, &plan, "security-full").await; + assert_eq!(status, reqwest::StatusCode::CONFLICT, "{version}: {report}"); + assert_eq!(operation_list(&client, &base_url).await, before); + + let public_plan = create_plan( + &client, + &base_url, + &source, + json!({ + "selection_mode": "partial", + "selected_operation_keys": ["GET /public"] + }), + ) + .await; + assert_eq!(public_plan.body["counts"]["actionable"], 1); + assert_eq!(public_plan.body["counts"]["blocked"], 0); + let (status, report) = + apply_plan(&client, &base_url, &public_plan, "security-public").await; + assert_eq!(status, reqwest::StatusCode::OK, "{version}: {report}"); + assert!(report_has_outcome_status(&report, "created")); + let after = operation_list(&client, &base_url).await; + assert_eq!(after["total"], 1, "{version}: {after}"); + assert_eq!(after["items"][0]["status"], "draft"); + } +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_apply/locale.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/locale.rs new file mode 100644 index 0000000..6292ab2 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/locale.rs @@ -0,0 +1,198 @@ +use super::*; +use crank_core::WorkspaceId; +use crank_registry::{CreateImportPlanRequest, ImportPlanId}; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_apply_localizes_repreview_guidance_by_accept_language() { + let registry = test_registry().await; + let base_url = spawn_admin_api(build_test_app( + registry, + test_storage_root("import_plan_apply_locale"), + )) + .await; + let client = authorized_client(&base_url).await; + let plan = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + let url = format!( + "{}/imports/openapi/{}/apply", + base_url.as_ref(), + plan.reference + ); + let wrong_digest = "0".repeat(64); + + // Russian session: Russian recovery guidance, identical machine code and + // recovery flags. + let ru = client + .post(&url) + .header("accept-language", "ru") + .json(&json!({ "plan_digest": wrong_digest, "idempotency_key": "locale-ru" })) + .send() + .await + .unwrap(); + assert_eq!(ru.status(), reqwest::StatusCode::CONFLICT); + let ru_body: Value = ru.json().await.unwrap(); + assert_eq!( + ru_body["error"]["context"]["error_code"], + "import_plan_digest_mismatch" + ); + assert_eq!( + ru_body["error"]["context"]["repreview_required"], + json!(true) + ); + assert_eq!( + ru_body["error"]["context"]["recovery"], + "repreview_required" + ); + assert!( + ru_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("создайте новый preview"), + "Russian session must receive Russian guidance: {ru_body:?}" + ); + + // No locale header keeps the English default behavior. + let en = client + .post(&url) + .json(&json!({ "plan_digest": wrong_digest, "idempotency_key": "locale-en" })) + .send() + .await + .unwrap(); + assert_eq!(en.status(), reqwest::StatusCode::CONFLICT); + let en_body: Value = en.json().await.unwrap(); + assert_eq!( + en_body["error"]["context"]["error_code"], + "import_plan_digest_mismatch" + ); + assert!( + en_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("create a fresh preview"), + "EN default must remain English: {en_body:?}" + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_apply_localizes_registry_name_conflict_by_accept_language() { + let registry = test_registry().await; + let storage_root = test_storage_root("import_plan_apply_locale_name_conflict"); + let service = test_service( + registry.clone(), + storage_root.clone(), + test_auth_settings(), + test_secret_crypto(), + ); + let base_url = spawn_admin_api(build_test_app(registry.clone(), storage_root)).await; + let client = authorized_client(&base_url).await; + let workspace_id = WorkspaceId::new("ws_default"); + let created = create_plan(&client, &base_url, OPENAPI_SINGLE, full_plan()).await; + + // Keep a historical v1-shaped plan to exercise the registry's typed + // name-conflict mapping directly. A v2 plan would stop earlier at the + // catalog-revision guard after the concurrent name claim below. + let original_id: ImportPlanId = created.reference.as_str().into(); + let original = registry + .get_import_plan(&workspace_id, &original_id) + .await + .unwrap() + .expect("HTTP-created plan is persisted"); + let legacy_id: ImportPlanId = "plan_locale_apply_name_conflict_v1".into(); + let mut legacy_payload = original.payload.clone(); + legacy_payload["schema_version"] = json!("import-plan-v1"); + legacy_payload["plan_reference"] = json!(legacy_id.as_str()); + let legacy_fields = legacy_payload.as_object_mut().unwrap(); + for field in [ + "catalog_revision", + "excluded_reasons", + "findings", + "support_matrix_version", + ] { + legacy_fields.remove(field); + } + let legacy = registry + .create_import_plan(CreateImportPlanRequest { + id: &legacy_id, + workspace_id: &workspace_id, + job_id: &original.job_id, + payload: &legacy_payload, + created_at: &original.created_at, + expires_at: &original.expires_at, + }) + .await + .unwrap(); + + let candidate_name = created.body["candidates"][0]["name"] + .as_str() + .expect("plan contains its candidate name"); + service + .create_operation( + &workspace_id, + test_operation_payload("https://api.example.test", candidate_name), + ) + .await + .unwrap(); + + let url = format!( + "{}/imports/openapi/{}/apply", + base_url.as_ref(), + legacy_id.as_str() + ); + for (locale, idempotency_key, expected_message) in [ + ( + Some("ru"), + "name-conflict-ru", + "имя операции изменилось после создания плана", + ), + ( + None, + "name-conflict-en", + "an operation name changed after the plan was created", + ), + ] { + let mut request = client.post(&url).json(&json!({ + "plan_digest": legacy.digest, + "idempotency_key": idempotency_key + })); + if let Some(locale) = locale { + request = request.header("accept-language", locale); + } + let response = request.send().await.unwrap(); + assert_eq!(response.status(), reqwest::StatusCode::CONFLICT); + let body: Value = response.json().await.unwrap(); + assert_eq!( + body["error"]["context"]["error_code"], + "import_plan_name_conflict" + ); + assert_eq!( + body["error"]["context"]["recovery"], + "confirm_skip_rename_or_new_version" + ); + assert_eq!(body["error"]["message"], expected_message); + } + + let matching_operations: i64 = + sqlx::query_scalar("select count(*) from operations where workspace_id = $1 and name = $2") + .bind(workspace_id.as_str()) + .bind(candidate_name) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!( + matching_operations, 1, + "Apply must not create another operation" + ); + let applications: i64 = sqlx::query_scalar( + "select count(*) from import_plan_applications where workspace_id = $1 and plan_id = $2", + ) + .bind(workspace_id.as_str()) + .bind(legacy_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!( + applications, 0, + "a rejected Apply must not persist an application" + ); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_apply/security_source.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/security_source.rs new file mode 100644 index 0000000..b86a587 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/security_source.rs @@ -0,0 +1,184 @@ +use super::*; + +const SECURITY_SOURCE_A: &str = r#" +openapi: 3.0.3 +info: { title: Security source A, version: '1' } +servers: [{ url: https://api.example.test }] +components: + securitySchemes: + apiKey: + type: apiKey + in: header + name: X-Api-Key +paths: + /secure: + get: + operationId: secureWithApiKey + security: [{ apiKey: [] }] + responses: { '200': { description: ok } } +"#; + +const SECURITY_SOURCE_B: &str = r#" +openapi: 3.0.3 +info: { title: Security source A, version: '1' } +servers: [{ url: https://api.example.test }] +components: + securitySchemes: + apiKey: + type: apiKey + in: header + name: X-Rotated-Key +paths: + /secure: + get: + operationId: secureWithApiKey + security: [{ apiKey: [] }] + responses: { '200': { description: ok } } +"#; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn changed_security_scheme_invalidates_old_plan_before_draft_creation() { + let registry = test_registry().await; + let storage_root = test_storage_root("import_plan_security_source_change"); + let base_url = spawn_admin_api(build_test_app(registry.clone(), storage_root)).await; + let client = authorized_client(&base_url).await; + + let secret_response = client + .post(format!("{base_url}/secrets")) + .json(&json!({ + "name": "security-source-test-secret", + "kind": "token", + "value": { "token": "source-change-canary" } + })) + .send() + .await + .unwrap(); + assert_eq!(secret_response.status(), reqwest::StatusCode::OK); + let secret: Value = secret_response.json().await.unwrap(); + let profile_response = client + .post(format!("{base_url}/auth-profiles")) + .json(&json!({ + "name": "security-source-test-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 plan = create_plan( + &client, + &base_url, + SECURITY_SOURCE_A, + json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /secure", + "action": "create", + "auth_profile_id": profile["id"] + }] + }), + ) + .await; + assert_eq!(plan.body["candidates"][0]["auth_profile_id"], profile["id"]); + assert_eq!(plan.body["candidates"][0]["security_alternative_index"], 0); + let stored_plan = registry + .get_import_plan( + &crank_core::WorkspaceId::new("ws_default"), + &plan.reference.clone().into(), + ) + .await + .unwrap() + .unwrap(); + assert_eq!( + stored_plan.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"], + profile["id"], + "the old plan must persist its explicit profile binding" + ); + assert_eq!( + stored_plan.payload["candidates"][0]["auth_requirement"]["parameter_name"], + "X-Api-Key" + ); + + // Register a second immutable source with only the security parameter + // changed. Its own upload/preview proves these are valid changed bytes, + // rather than corruption of the first content-addressed artifact. + let source_b_upload = reqwest::multipart::Part::bytes(SECURITY_SOURCE_B.as_bytes().to_vec()) + .file_name("security-b.yaml") + .mime_str("application/yaml") + .unwrap(); + let source_b_response = client + .post(format!("{base_url}/imports/openapi/preview")) + .multipart(reqwest::multipart::Form::new().part("file", source_b_upload)) + .send() + .await + .unwrap(); + assert_eq!(source_b_response.status(), reqwest::StatusCode::OK); + let source_b_preview: Value = source_b_response.json().await.unwrap(); + assert_eq!( + source_b_preview["preview"]["groups"][0]["operations"][0]["security_schemes"]["apiKey"]["kind"] + ["parameter_name"], + "X-Rotated-Key" + ); + let workspace_id = crank_core::WorkspaceId::new("ws_default"); + let original_job_id: crank_registry::ImportJobId = + plan.preview["job_id"].as_str().unwrap().into(); + let changed_job_id: crank_registry::ImportJobId = + source_b_preview["job_id"].as_str().unwrap().into(); + let original_job = registry + .get_import_job(&workspace_id, &original_job_id) + .await + .unwrap() + .unwrap(); + let changed_job = registry + .get_import_job(&workspace_id, &changed_job_id) + .await + .unwrap() + .unwrap(); + assert_ne!( + original_job.preview_payload["source"]["digest"], + changed_job.preview_payload["source"]["digest"], + "the changed scheme must come from a different verified source artifact" + ); + assert_eq!( + stored_plan.payload["source"]["digest"], original_job.preview_payload["source"]["digest"], + "the plan must remain anchored to source A" + ); + + // Simulate the existing new-source/re-preview condition on the anchored + // pending job while retaining its original plan. The replacement is a + // separately registered and digest-verified B snapshot; Apply must + // compare the newly derived fingerprint with the old plan fingerprint. + sqlx::query("update import_jobs set preview_payload = $3 where id = $1 and workspace_id = $2") + .bind(original_job_id.as_str()) + .bind(workspace_id.as_str()) + .bind(&changed_job.preview_payload) + .execute(registry.pool()) + .await + .unwrap(); + let replaced_job = registry + .get_import_job(&workspace_id, &original_job_id) + .await + .unwrap() + .unwrap(); + assert_eq!( + replaced_job.preview_payload["source"]["digest"], + changed_job.preview_payload["source"]["digest"] + ); + + let (status, error) = apply_plan(&client, &base_url, &plan, "security-source-changed").await; + assert_eq!(status, reqwest::StatusCode::CONFLICT, "{error}"); + assert_eq!( + error["error"]["context"]["error_code"], "import_plan_fingerprint_stale", + "the old plan must require re-preview after source security changes" + ); + assert_eq!(error["error"]["context"]["repreview_required"], true); + assert_eq!(operation_list(&client, &base_url).await["total"], 0); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_apply/support_matrix.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/support_matrix.rs new file mode 100644 index 0000000..a9ccf69 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_apply/support_matrix.rs @@ -0,0 +1,106 @@ +use super::*; + +use admin_api::error::ApiError; +use admin_api::service::{ + OpenApiImportPlanApplyPayload, OpenApiImportPlanCreatePayload, OpenApiUpload, + OpenApiUploadLocale, +}; +use crank_core::WorkspaceId; +use crank_registry::{CreateImportPlanRequest, ImportPlanId}; + +#[tokio::test] +#[serial] +async fn older_v2_support_matrix_rejects_new_apply_but_keeps_plan_auditable() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_old_support_matrix_apply"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import( + &workspace_id, + OpenApiUpload { + bytes: OPENAPI_SINGLE.as_bytes().to_vec(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + }, + ) + .await + .unwrap(); + let job_id = preview.job_id.as_str().into(); + let created = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + OpenApiImportPlanCreatePayload { + selection_mode: "full".to_owned(), + selected_operation_keys: Vec::new(), + server_url: None, + decisions: Vec::new(), + }, + ) + .await + .unwrap(); + let original_id: ImportPlanId = created.plan_reference.as_str().into(); + let original = registry + .get_import_plan(&workspace_id, &original_id) + .await + .unwrap() + .unwrap(); + + // Model an immutable plan saved under an earlier support matrix. The + // registry derives its valid semantic digest from the changed payload. + let old_id: ImportPlanId = "plan_older_support_matrix".into(); + let mut old_payload = original.payload.clone(); + old_payload["plan_reference"] = json!(old_id.as_str()); + old_payload["support_matrix_version"] = json!("1.0.2"); + let older = registry + .create_import_plan(CreateImportPlanRequest { + id: &old_id, + workspace_id: &workspace_id, + job_id: &original.job_id, + payload: &old_payload, + created_at: &original.created_at, + expires_at: &original.expires_at, + }) + .await + .unwrap(); + assert_ne!(older.digest, original.digest); + + let audit = service + .revalidate_openapi_import_plan(&workspace_id, &old_id) + .await + .unwrap(); + assert_eq!(audit.revalidation_status, "stale"); + assert!(audit.revalidation_required); + assert_eq!(audit.reason.as_deref(), Some("importer_version_changed")); + + let error = service + .apply_openapi_import_plan( + &workspace_id, + &old_id, + OpenApiImportPlanApplyPayload { + plan_digest: older.digest, + idempotency_key: "old-matrix-new-attempt".to_owned(), + }, + ) + .await + .expect_err("old support matrix must not authorize a new Apply"); + match error { + ApiError::Conflict { + context: Some(context), + .. + } => assert_eq!(context["error_code"], "importer_version_changed"), + other => panic!("expected typed re-preview conflict, got {other:?}"), + } + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); +} 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..e40bf72 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/creation.rs @@ -0,0 +1,997 @@ +use super::*; + +pub(crate) const OPENAPI_SINGLE_SECURITY: &str = r#" +openapi: 3.0.3 +info: { title: Import security, version: '1' } +servers: [{ url: https://api.example.test }] +components: + securitySchemes: + Key: { type: apiKey, in: header, name: X-Api-Key } + Bearer: { type: http, scheme: bearer } +paths: + /v2/latest: + get: + operationId: latestRates + security: [{ Key: [] }, { Bearer: [] }] + responses: { '200': { description: ok } } +"#; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn preview_name_conflicts_are_typed_and_deterministic() { + let registry = test_registry().await; + let upstream = spawn_upstream_server().await; + let service = test_service( + registry, + test_storage_root("openapi_preview_name_conflicts"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace = WorkspaceId::new("ws_default"); + service + .create_operation( + &workspace, + test_operation_payload(&upstream, "duplicate_id"), + ) + .await + .unwrap(); + let document = r#" +openapi: 3.0.3 +info: { title: Naming } +servers: [{ url: https://api.example.test }] +paths: + /one: + get: + operationId: duplicate_id + responses: { '200': { description: ok } } + /two: + get: + operationId: duplicate_id + responses: { '200': { description: ok } } + /bad: + get: + operationId: bad-id + responses: { '200': { description: ok } } +"#; + let first = service + .preview_openapi_import(&workspace, upload(document)) + .await + .unwrap(); + let repeated = service + .preview_openapi_import(&workspace, upload(document)) + .await + .unwrap(); + assert_eq!(first.preview.conflicts, repeated.preview.conflicts); + assert!( + first + .preview + .conflicts + .iter() + .any(|item| item.conflict_type == "duplicate_name") + ); + assert!( + first + .preview + .conflicts + .iter() + .any(|item| item.conflict_type == "invalid_name") + ); + assert!( + first + .preview + .conflicts + .iter() + .any(|item| item.conflict_type == "taken_name") + ); + assert!(first.preview.conflicts.iter().all(|item| { + item.source_location + .as_ref() + .is_some_and(|location| location.pointer.starts_with("/paths/")) + && !item.proposed_name.is_empty() + })); + assert_eq!(service.list_operations(&workspace).await.unwrap().len(), 1); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn reopening_plan_is_read_only_and_reports_catalog_drift() { + let registry = test_registry().await; + let upstream = spawn_upstream_server().await; + let service = test_service( + registry, + test_storage_root("openapi_plan_reopen_catalog"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan(&workspace, &job_id, plan_payload("full", vec![], vec![])) + .await + .unwrap(); + let plan_id = plan.plan_reference.as_str().into(); + let current = service + .revalidate_openapi_import_plan(&workspace, &plan_id) + .await + .unwrap(); + assert_eq!(current.revalidation_status, "current"); + assert!(!current.revalidation_required); + assert!( + service + .list_operations(&workspace) + .await + .unwrap() + .is_empty() + ); + + service + .create_operation( + &workspace, + test_operation_payload(&upstream, "unrelated_operation"), + ) + .await + .unwrap(); + let stale = service + .revalidate_openapi_import_plan(&workspace, &plan_id) + .await + .unwrap(); + assert_eq!(stale.revalidation_status, "stale"); + assert!(stale.revalidation_required); + assert_eq!(service.list_operations(&workspace).await.unwrap().len(), 1); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn full_plan_is_server_authored_with_bounded_confirmation_and_no_mutation() { + 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(); + assert_eq!(preview.preview.groups[0].operations[0].status, "actionable"); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let job = registry + .get_import_job(&workspace_id, &job_id) + .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.payload["schema_version"], "import-plan-v2"); + assert_eq!(stored.payload["catalog_revision"], plan.catalog_revision); + assert_eq!(stored.payload["support_matrix_version"], "1.0.3"); + assert!(stored.payload["findings"]["document"].is_array()); + assert!(stored.payload["findings"]["candidates"].is_array()); + assert_eq!(stored.expires_at, expires_at); + assert_eq!( + stored.payload["selection"]["selected"], + 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()); + let repeated = service + .create_openapi_import_plan(&workspace_id, &job_id, plan_payload("full", vec![], vec![])) + .await + .unwrap(); + assert_ne!(repeated.plan_reference, plan.plan_reference); + assert_eq!(repeated.plan_digest, plan.plan_digest); + let repeated_stored = registry + .get_import_plan(&workspace_id, &repeated.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_ne!( + repeated_stored.payload["candidates"][0]["operation"]["id"], + stored.payload["candidates"][0]["operation"]["id"] + ); + // Planning is non-mutating: no operation exists and the job stays pending. + assert!( + service + .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 expected_display_name = preview + .preview + .groups + .iter() + .flat_map(|group| &group.operations) + .find(|candidate| candidate.key == second) + .expect("excluded candidate is in preview") + .draft + .name + .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.name, expected_display_name); + assert_eq!(excluded.reason.as_deref(), Some("not_selected")); + let stored = registry + .get_import_plan(&workspace_id, &plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!( + stored.payload["excluded_reasons"], + json!([{ + "key": second, + "name": expected_display_name, + "reason": "not_selected", + }]) + ); + + let plan_id: crank_registry::ImportPlanId = plan.plan_reference.as_str().into(); + let revalidated = service + .revalidate_openapi_import_plan(&workspace_id, &plan_id) + .await + .unwrap(); + let revalidated_excluded = revalidated + .candidates + .iter() + .find(|candidate| candidate.key == second) + .expect("excluded candidate remains visible during revalidation"); + assert_eq!(revalidated_excluded.name, expected_display_name); + + 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-v2"); + 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"); + let reopen_error = service + .revalidate_openapi_import_plan(&secondary, &plan.plan_reference.as_str().into()) + .await + .expect_err("a foreign plan cannot be reopened"); + assert_http_status(reopen_error, axum::http::StatusCode::NOT_FOUND); +} + +#[tokio::test(flavor = "multi_thread")] +#[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 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/excluded_names.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/excluded_names.rs new file mode 100644 index 0000000..ddb8ff5 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/excluded_names.rs @@ -0,0 +1,135 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn partial_plan_applies_when_excluded_operation_name_exceeds_selected_name_limit() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_long_excluded_name"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let long_operation_id = "a".repeat(257); + let document = format!( + r#"openapi: 3.0.3 +info: + title: Long excluded operation name + version: "1" +servers: + - url: https://api.example.test +paths: + /selected: + get: + operationId: selectedMethod + responses: + "200": + description: ok + /excluded: + get: + operationId: {long_operation_id} + responses: + "200": + description: ok +"# + ); + let preview = service + .preview_openapi_import(&workspace_id, upload(&document)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let selected_key = "GET /selected"; + let excluded_key = "GET /excluded"; + let excluded_draft_name = preview + .preview + .groups + .iter() + .flat_map(|group| &group.operations) + .find(|candidate| candidate.key == excluded_key) + .expect("long-name method is present in preview") + .draft + .name + .clone(); + assert_eq!(excluded_draft_name, long_operation_id); + assert!(excluded_draft_name.len() > 256); + + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "partial", + vec![selected_key.to_owned()], + vec![decision(selected_key, "create", None, None, None)], + ), + ) + .await + .unwrap(); + let created_excluded = plan + .candidates + .iter() + .find(|candidate| candidate.key == excluded_key) + .expect("excluded method is returned by plan creation"); + assert_eq!(created_excluded.name, excluded_draft_name); + assert_eq!(plan.counts.selected, 1); + assert_eq!(plan.counts.excluded, 1); + + let plan_id = crank_registry::ImportPlanId::new(plan.plan_reference.clone()); + let stored = registry + .get_import_plan(&workspace_id, &plan_id) + .await + .unwrap() + .expect("created plan is persisted"); + assert_eq!( + stored.payload["excluded_reasons"][0]["name"], + excluded_draft_name + ); + + let revalidated = service + .revalidate_openapi_import_plan(&workspace_id, &plan_id) + .await + .unwrap(); + let revalidated_excluded = revalidated + .candidates + .iter() + .find(|candidate| candidate.key == excluded_key) + .expect("excluded method remains visible during revalidation"); + assert_eq!(revalidated_excluded.name, excluded_draft_name); + + let report = service + .apply_openapi_import_plan( + &workspace_id, + &plan_id, + admin_api::service::OpenApiImportPlanApplyPayload { + plan_digest: plan.plan_digest, + idempotency_key: "long-excluded-name-apply".to_owned(), + }, + ) + .await + .expect("excluded descriptive metadata must not invalidate a partial plan"); + assert_eq!( + report.status, + crank_registry::ImportPlanApplyStatus::Applied + ); + assert_eq!(report.counts.created, 1); + assert_eq!( + report + .outcomes + .iter() + .find(|outcome| outcome.key == selected_key) + .expect("selected method appears in apply report") + .outcome, + crank_registry::ImportPlanCandidateOutcome::Created + ); + let reported_excluded = report + .outcomes + .iter() + .find(|outcome| outcome.key == excluded_key) + .expect("apply report includes the excluded method"); + assert_eq!(reported_excluded.name, excluded_draft_name); + let operations = service.list_operations(&workspace_id).await.unwrap(); + assert_eq!(operations.len(), 1, "only the selected method is applied"); + assert_eq!(operations[0].status, crank_core::OperationStatus::Draft); + assert_eq!(operations[0].current_draft_version, 1); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/locale.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/locale.rs new file mode 100644 index 0000000..eade388 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/locale.rs @@ -0,0 +1,360 @@ +use super::*; + +/// Server-created plan reached through the public HTTP contract, so the locale +/// tests exercise the real route handlers rather than the service methods. +struct HttpPlan { + plan_reference: String, + plan_digest: String, +} + +async fn create_http_plan(client: &reqwest::Client, base_url: &str) -> HttpPlan { + let upload_part = reqwest::multipart::Part::bytes(OPENAPI_SINGLE.as_bytes().to_vec()) + .file_name("import.yaml") + .mime_str("application/yaml") + .unwrap(); + let preview = client + .post(format!("{base_url}/imports/openapi/preview")) + .multipart(reqwest::multipart::Form::new().part("file", upload_part)) + .send() + .await + .unwrap(); + assert_eq!(preview.status(), reqwest::StatusCode::OK); + let preview: Value = preview.json().await.unwrap(); + let job_id = preview["job_id"].as_str().unwrap().to_owned(); + + let plan = client + .post(format!("{base_url}/imports/openapi/{job_id}/plans")) + .json(&json!({ "selection_mode": "full", "selected_operation_keys": [] })) + .send() + .await + .unwrap(); + assert_eq!(plan.status(), reqwest::StatusCode::OK); + let plan: Value = plan.json().await.unwrap(); + HttpPlan { + plan_reference: plan["plan_reference"].as_str().unwrap().to_owned(), + plan_digest: plan["plan_digest"].as_str().unwrap().to_owned(), + } +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_create_localizes_repreview_guidance_by_accept_language() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_plan_locale_create"); + 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(); + + // Corrupt the persisted preview contract so plan creation must ask for a + // fresh preview. + 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) + .execute(registry.pool()) + .await + .unwrap(); + + let server = spawn_admin_api(app).await; + let client = authorized_client(&server).await; + let url = format!("{}/imports/openapi/{}/plans", server.as_ref(), job_id); + + // Russian session: Russian recovery guidance, identical machine code and + // recovery flags. + let ru = client + .post(&url) + .header("accept-language", "ru-RU, ru;q=0.9") + .json(&json!({ "selection_mode": "full", "selected_operation_keys": [] })) + .send() + .await + .unwrap(); + assert_eq!(ru.status(), reqwest::StatusCode::CONFLICT); + let ru_body: Value = ru.json().await.unwrap(); + assert_eq!( + ru_body["error"]["context"]["error_code"], + "import_replay_verification_failed" + ); + assert_eq!( + ru_body["error"]["context"]["repreview_required"], + json!(true) + ); + assert_eq!( + ru_body["error"]["context"]["recovery"], + "repreview_required" + ); + assert!( + ru_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("создайте новый preview"), + "Russian session must receive Russian guidance: {ru_body:?}" + ); + + // No locale header keeps the English default behavior. + let en = client + .post(&url) + .json(&json!({ "selection_mode": "full", "selected_operation_keys": [] })) + .send() + .await + .unwrap(); + assert_eq!(en.status(), reqwest::StatusCode::CONFLICT); + let en_body: Value = en.json().await.unwrap(); + assert_eq!( + en_body["error"]["context"]["error_code"], + "import_replay_verification_failed" + ); + assert!( + en_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("create a fresh preview"), + "EN default must remain English: {en_body:?}" + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_revalidate_accepts_locale_without_machine_drift() { + let registry = test_registry().await; + let app = build_test_app( + registry, + test_storage_root("openapi_import_plan_locale_revalidate"), + ); + let server = spawn_admin_api(app).await; + let client = authorized_client(&server).await; + let plan = create_http_plan(&client, server.as_ref()).await; + + let url = format!( + "{}/imports/openapi/plans/{}", + server.as_ref(), + plan.plan_reference + ); + let ru = client + .get(&url) + .header("accept-language", "ru") + .send() + .await + .unwrap(); + assert_eq!(ru.status(), reqwest::StatusCode::OK); + let ru: Value = ru.json().await.unwrap(); + assert_eq!(ru["plan_digest"], plan.plan_digest); + assert_eq!(ru["revalidation_status"], "current"); + assert_eq!(ru["revalidation_required"], false); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_create_localizes_expired_preview_by_accept_language() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_plan_locale_expired"); + 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(); + + // Expire the preview job without corrupting its replay contract. + sqlx::query("update import_jobs set expires_at = now() - interval '1 minute' where id = $1") + .bind(&job_id) + .execute(registry.pool()) + .await + .unwrap(); + + let server = spawn_admin_api(app).await; + let client = authorized_client(&server).await; + let url = format!("{}/imports/openapi/{}/plans", server.as_ref(), job_id); + let body = json!({ "selection_mode": "full", "selected_operation_keys": [] }); + + let ru = client + .post(&url) + .header("accept-language", "ru") + .json(&body) + .send() + .await + .unwrap(); + assert_eq!(ru.status(), reqwest::StatusCode::BAD_REQUEST); + let ru_body: Value = ru.json().await.unwrap(); + assert_eq!(ru_body["error"]["code"], "validation_error"); + assert!( + ru_body["error"].get("context").is_none(), + "expired-preview validation keeps no context: {ru_body:?}" + ); + assert!( + ru_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("истёк"), + "Russian session must receive Russian guidance: {ru_body:?}" + ); + + // No locale header keeps the English default behavior. + let en = client.post(&url).json(&body).send().await.unwrap(); + assert_eq!(en.status(), reqwest::StatusCode::BAD_REQUEST); + let en_body: Value = en.json().await.unwrap(); + assert_eq!(en_body["error"]["code"], "validation_error"); + assert!(en_body["error"].get("context").is_none()); + assert_eq!(en_body["error"]["message"], "import preview has expired"); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_create_localizes_name_conflict_guidance_by_accept_language() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_plan_locale_name_conflict"); + 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"); + + // Occupy the candidate's suggested name so an explicit `create` decision + // conflicts. + service + .create_operation( + &workspace_id, + test_operation_payload("https://api.example.test", "latest_rates"), + ) + .await + .unwrap(); + 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); + let body = json!({ + "selection_mode": "full", + "selected_operation_keys": [], + "decisions": [{ + "operation_key": "GET /v2/latest", + "action": "create", + }] + }); + + let ru = client + .post(&url) + .header("accept-language", "ru") + .json(&body) + .send() + .await + .unwrap(); + assert_eq!(ru.status(), reqwest::StatusCode::CONFLICT); + let ru_body: Value = ru.json().await.unwrap(); + assert_eq!( + ru_body["error"]["context"]["error_code"], + "import_plan_name_conflict" + ); + assert_eq!( + ru_body["error"]["context"]["recovery"], + "confirm_skip_rename_or_new_version" + ); + assert!( + ru_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("уже существует"), + "Russian session must receive Russian guidance: {ru_body:?}" + ); + + // No locale header keeps the exact English wording. + let en = client.post(&url).json(&body).send().await.unwrap(); + assert_eq!(en.status(), reqwest::StatusCode::CONFLICT); + let en_body: Value = en.json().await.unwrap(); + assert_eq!( + en_body["error"]["context"]["error_code"], + "import_plan_name_conflict" + ); + assert_eq!( + en_body["error"]["message"], + "operation with name latest_rates already exists" + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn legacy_create_localizes_expired_preview_by_accept_language() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_legacy_create_locale_expired"); + 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(); + + // Expire the preview job so the legacy create route /imports/openapi/{id}/create + // rejects before any selection work. + sqlx::query("update import_jobs set expires_at = now() - interval '1 minute' where id = $1") + .bind(&job_id) + .execute(registry.pool()) + .await + .unwrap(); + + let server = spawn_admin_api(app).await; + let client = authorized_client(&server).await; + let url = format!("{}/imports/openapi/{}/create", server.as_ref(), job_id); + let body = json!({ "selected_operation_keys": [] }); + + let ru = client + .post(&url) + .header("accept-language", "ru") + .json(&body) + .send() + .await + .unwrap(); + assert_eq!(ru.status(), reqwest::StatusCode::BAD_REQUEST); + let ru_body: Value = ru.json().await.unwrap(); + assert_eq!(ru_body["error"]["code"], "validation_error"); + assert!( + ru_body["error"].get("context").is_none(), + "legacy expired-preview validation keeps no context: {ru_body:?}" + ); + assert!( + ru_body["error"]["message"] + .as_str() + .unwrap_or_default() + .contains("истёк"), + "Russian session must receive Russian guidance: {ru_body:?}" + ); + + // No locale header keeps the exact English wording. + let en = client.post(&url).json(&body).send().await.unwrap(); + assert_eq!(en.status(), reqwest::StatusCode::BAD_REQUEST); + let en_body: Value = en.json().await.unwrap(); + assert_eq!(en_body["error"]["code"], "validation_error"); + assert_eq!(en_body["error"]["message"], "import preview has expired"); +} 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..de260f3 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/mod.rs @@ -0,0 +1,170 @@ +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), + security_alternative_index: None, + } +} + +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 excluded_names; +mod locale; +mod route_get; +pub(super) use creation::OPENAPI_SINGLE_SECURITY; +mod security_availability; +mod security_binding; +mod validation; diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/route_get.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/route_get.rs new file mode 100644 index 0000000..0b9e941 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/route_get.rs @@ -0,0 +1,87 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn persisted_plan_get_requires_owner_and_reports_catalog_drift() { + let registry = test_registry().await; + let storage_root = test_storage_root("openapi_import_plan_get_route"); + let service = test_service( + registry.clone(), + storage_root.clone(), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace, upload(OPENAPI_SINGLE)) + .await + .unwrap(); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan(&workspace, &job_id, plan_payload("full", vec![], vec![])) + .await + .unwrap(); + let plan_id = plan.plan_reference.clone(); + let plan_digest = plan.plan_digest.clone(); + + let foreign_workspace = WorkspaceId::new("ws_plan_get_foreign"); + let now = OffsetDateTime::now_utc(); + registry + .create_workspace(CreateWorkspaceRequest { + workspace: &Workspace { + id: foreign_workspace.clone(), + slug: "ws-plan-get-foreign".to_owned(), + display_name: "Foreign plan workspace".to_owned(), + status: WorkspaceStatus::Active, + settings: json!({}), + created_at: now, + updated_at: now, + }, + }) + .await + .unwrap(); + + let server = spawn_admin_api(build_test_app(registry, storage_root)).await; + let client = authorized_client(&server).await; + let url = format!("{server}/imports/openapi/plans/{plan_id}"); + let anonymous = reqwest::Client::new().get(&url).send().await.unwrap(); + assert_eq!(anonymous.status(), reqwest::StatusCode::UNAUTHORIZED); + let foreign = client + .get(url.replace("ws_default", foreign_workspace.as_str())) + .send() + .await + .unwrap(); + assert_eq!(foreign.status(), reqwest::StatusCode::FORBIDDEN); + + let current = client.get(&url).send().await.unwrap(); + assert_eq!(current.status(), reqwest::StatusCode::OK); + let current: Value = current.json().await.unwrap(); + assert_eq!(current["plan_reference"], plan_id); + assert_eq!(current["plan_digest"], plan_digest); + assert_eq!(current["revalidation_status"], "current"); + assert_eq!(current["revalidation_required"], false); + assert!( + service + .list_operations(&workspace) + .await + .unwrap() + .is_empty() + ); + + let upstream = spawn_upstream_server().await; + service + .create_operation( + &workspace, + test_operation_payload(&upstream, "plan_get_catalog_drift"), + ) + .await + .unwrap(); + let stale = client.get(&url).send().await.unwrap(); + assert_eq!(stale.status(), reqwest::StatusCode::OK); + let stale: Value = stale.json().await.unwrap(); + assert_eq!(stale["plan_reference"], plan_id); + assert_eq!(stale["plan_digest"], plan_digest); + assert_eq!(stale["revalidation_status"], "stale"); + assert_eq!(stale["revalidation_required"], true); + assert_eq!(service.list_operations(&workspace).await.unwrap().len(), 1); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/security_availability.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/security_availability.rs new file mode 100644 index 0000000..dc4d02c --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/security_availability.rs @@ -0,0 +1,292 @@ +use super::*; + +const OPENAPI_SECURITY_BINDING: &str = r#" +openapi: 3.0.3 +info: { title: Security binding availability, version: '1' } +servers: [{ url: https://api.example.test }] +components: + securitySchemes: + Key: { type: apiKey, in: header, name: X-Api-Key } + Bearer: { type: http, scheme: bearer } +paths: + /v2/latest: + get: + operationId: latestRates + security: [{ Key: [] }, { Bearer: [] }] + responses: { '200': { description: ok } } +"#; + +fn security_upload() -> OpenApiUpload { + OpenApiUpload { + bytes: OPENAPI_SECURITY_BINDING.as_bytes().to_vec(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + } +} + +fn binding_decision(auth_profile_id: &str) -> OpenApiImportPlanDecision { + let mut binding = decision( + "GET /v2/latest", + "create", + None, + None, + Some(auth_profile_id), + ); + binding.security_alternative_index = Some(0); + binding +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn security_binding_refuses_foreign_and_unavailable_auth_material_before_draft() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_plan_security_availability"), + test_auth_settings(), + test_secret_crypto(), + ); + let primary = WorkspaceId::new("ws_default"); + let foreign = WorkspaceId::new("ws_security_foreign"); + let created_at = OffsetDateTime::now_utc(); + registry + .create_workspace(CreateWorkspaceRequest { + workspace: &Workspace { + id: foreign.clone(), + slug: "ws-security-foreign".to_owned(), + display_name: "Foreign security workspace".to_owned(), + status: WorkspaceStatus::Active, + settings: json!({}), + created_at, + updated_at: created_at, + }, + }) + .await + .unwrap(); + + let foreign_canary = "foreign-security-profile-canary"; + let foreign_secret = service + .create_secret( + &foreign, + None, + SecretPayload { + name: "foreign-security-secret".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": foreign_canary }), + }, + None, + ) + .await + .unwrap(); + let foreign_profile = service + .create_auth_profile( + &foreign, + AuthProfilePayload { + name: "foreign-security-profile-name".to_owned(), + kind: AuthKind::ApiKeyHeader, + config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-Api-Key".to_owned(), + secret_id: SecretId::new(foreign_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + + // A foreign profile is absent from the current workspace's advertised + // choices and submission fails with a generic workspace-safe error. + let preview = service + .preview_openapi_import(&primary, security_upload()) + .await + .unwrap(); + let preview_text = serde_json::to_string(&preview).unwrap(); + for private_value in [ + foreign_profile.id.as_str(), + foreign_profile.name.as_str(), + foreign_secret.id.as_str(), + foreign_canary, + ] { + assert!( + !preview_text.contains(private_value), + "foreign profile metadata appeared in preview choices" + ); + } + assert!(preview.security_profile_choices.iter().all(|choice| { + choice + .profiles + .iter() + .all(|profile| profile.id != foreign_profile.id.as_str()) + })); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let foreign_error = service + .create_openapi_import_plan( + &primary, + &job_id, + plan_payload( + "full", + vec![], + vec![binding_decision(foreign_profile.id.as_str())], + ), + ) + .await + .expect_err("a profile from another workspace must not bind"); + assert_error_code(&foreign_error, "import_plan_auth_unavailable"); + let foreign_error_text = format!("{foreign_error:?}"); + assert_http_status(foreign_error, axum::http::StatusCode::UNPROCESSABLE_ENTITY); + for private_value in [ + foreign_profile.id.as_str(), + foreign_profile.name.as_str(), + foreign_secret.id.as_str(), + foreign_canary, + ] { + assert!( + !foreign_error_text.contains(private_value), + "foreign profile or secret metadata leaked: {foreign_error_text}" + ); + } + + // A previously valid local profile whose Secret is disabled is removed + // from choices and cannot be used in a newly confirmed ImportPlan. + let disabled_canary = "disabled-security-secret-canary"; + let disabled_secret = service + .create_secret( + &primary, + None, + SecretPayload { + name: "disabled-security-secret".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": disabled_canary }), + }, + None, + ) + .await + .unwrap(); + let disabled_profile = service + .create_auth_profile( + &primary, + AuthProfilePayload { + name: "disabled-security-profile".to_owned(), + kind: AuthKind::ApiKeyHeader, + config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-Api-Key".to_owned(), + secret_id: SecretId::new(disabled_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + sqlx::query( + "update secrets set status = 'disabled', updated_at = now() where workspace_id = $1 and id = $2", + ) + .bind(primary.as_str()) + .bind(disabled_secret.id.as_str()) + .execute(registry.pool()) + .await + .unwrap(); + let preview = service + .preview_openapi_import(&primary, security_upload()) + .await + .unwrap(); + assert!(preview.security_profile_choices.iter().all(|choice| { + choice + .profiles + .iter() + .all(|profile| profile.id != disabled_profile.id.as_str()) + })); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let disabled_error = service + .create_openapi_import_plan( + &primary, + &job_id, + plan_payload( + "full", + vec![], + vec![binding_decision(disabled_profile.id.as_str())], + ), + ) + .await + .expect_err("a disabled linked Secret must reject the profile binding"); + assert_error_code(&disabled_error, "secret_not_active"); + let disabled_error_text = format!("{disabled_error:?}"); + assert!(!disabled_error_text.contains(disabled_canary)); + + // A profile whose active Secret has no stored current version is equally + // unavailable. Direct SQL models the broken persisted reference while + // retaining the real Admin preview and plan validation paths. + let missing_version_canary = "missing-version-security-canary"; + let missing_version_secret = service + .create_secret( + &primary, + None, + SecretPayload { + name: "missing-version-security-secret".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": missing_version_canary }), + }, + None, + ) + .await + .unwrap(); + let missing_version_profile = service + .create_auth_profile( + &primary, + AuthProfilePayload { + name: "missing-version-security-profile".to_owned(), + kind: AuthKind::ApiKeyHeader, + config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-Api-Key".to_owned(), + secret_id: SecretId::new(missing_version_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + sqlx::query("delete from secret_versions where secret_id = $1 and version = 1") + .bind(missing_version_secret.id.as_str()) + .execute(registry.pool()) + .await + .unwrap(); + let preview = service + .preview_openapi_import(&primary, security_upload()) + .await + .unwrap(); + assert!(preview.security_profile_choices.iter().all(|choice| { + choice + .profiles + .iter() + .all(|profile| profile.id != missing_version_profile.id.as_str()) + })); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let missing_version_error = service + .create_openapi_import_plan( + &primary, + &job_id, + plan_payload( + "full", + vec![], + vec![binding_decision(missing_version_profile.id.as_str())], + ), + ) + .await + .expect_err("a Secret without a current version must reject the profile binding"); + assert_error_code(&missing_version_error, "operation_auth_profile_invalid"); + let missing_version_error_text = format!("{missing_version_error:?}"); + for private_value in [ + missing_version_profile.id.as_str(), + missing_version_profile.name.as_str(), + missing_version_secret.id.as_str(), + missing_version_canary, + ] { + assert!( + !missing_version_error_text.contains(private_value), + "missing-version Secret metadata leaked: {missing_version_error_text}" + ); + } + assert!( + service.list_operations(&primary).await.unwrap().is_empty(), + "no unavailable profile binding may create a Draft" + ); +} diff --git a/apps/admin-api/tests/integration/openapi_import/import_plan_create/security_binding.rs b/apps/admin-api/tests/integration/openapi_import/import_plan_create/security_binding.rs new file mode 100644 index 0000000..9d41579 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/security_binding.rs @@ -0,0 +1,770 @@ +use super::*; + +const OPENAPI_SINGLE_PUBLIC: &str = r#" +openapi: 3.0.3 +info: { title: Public import, version: '1' } +servers: [{ url: https://api.example.test }] +paths: + /v2/latest: + get: + operationId: latestRates + responses: { '200': { description: ok } } +"#; + +const OPENAPI_SINGLE_EXPLICIT_NO_AUTH: &str = r#" +openapi: 3.0.3 +info: { title: Explicit no auth import, version: '1' } +servers: [{ url: https://api.example.test }] +paths: + /v2/latest: + get: + operationId: latestRates + security: [] + responses: { '200': { description: ok } } +"#; + +const OPENAPI_BASIC_AND_QUERY_SECURITY: &str = r#" +openapi: 3.0.3 +info: { title: Basic and query import, version: '1' } +servers: [{ url: https://api.example.test }] +components: + securitySchemes: + Basic: { type: http, scheme: basic } + QueryKey: { type: apiKey, in: query, name: access_token } +paths: + /v2/basic: + get: + operationId: basicRates + security: [{ Basic: [] }, { QueryKey: [] }] + responses: { '200': { description: ok } } + /v2/query: + get: + operationId: queryRates + security: [{ Basic: [] }, { QueryKey: [] }] + responses: { '200': { description: ok } } +"#; + +#[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 bearer_secret = service + .create_secret( + &workspace_id, + None, + SecretPayload { + name: "plan-bearer-token".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": "bearer-choice-canary" }), + }, + None, + ) + .await + .unwrap(); + let bearer_profile = service + .create_auth_profile( + &workspace_id, + AuthProfilePayload { + name: "plan-bearer-profile".to_owned(), + kind: AuthKind::Bearer, + config: AuthConfig::Bearer(crank_core::BearerAuthConfig { + header_name: "Authorization".to_owned(), + secret_id: SecretId::new(bearer_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_SECURITY)) + .await + .unwrap(); + assert_eq!(preview.preview.groups[0].operations[0].status, "unresolved"); + let selectable = preview + .security_profile_choices + .iter() + .find(|choice| choice.operation_key == "GET /v2/latest" && choice.alternative_index == 0) + .expect("preview must expose choices for supported security alternatives"); + assert!(selectable.profiles.iter().any(|choice| { + choice.id == auth_profile.id.as_str() && choice.kind == AuthKind::ApiKeyHeader + })); + assert!( + selectable + .profiles + .iter() + .all(|choice| { choice.id != bearer_profile.id.as_str() }) + ); + let bearer_choice = preview + .security_profile_choices + .iter() + .find(|choice| choice.operation_key == "GET /v2/latest" && choice.alternative_index == 1) + .expect("preview must expose profile choices for the second OR alternative"); + assert!(bearer_choice.profiles.iter().any(|choice| { + choice.id == bearer_profile.id.as_str() && choice.kind == AuthKind::Bearer + })); + assert!( + bearer_choice + .profiles + .iter() + .all(|choice| { choice.id != auth_profile.id.as_str() }) + ); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let mut auth_decision = decision( + "GET /v2/latest", + "create", + None, + None, + Some(auth_profile.id.as_str()), + ); + auth_decision.security_alternative_index = Some(0); + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload("full", vec![], vec![auth_decision]), + ) + .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() + ); + assert_eq!( + stored.payload["candidates"][0]["decision"]["security_alternative_index"], + 0 + ); + // 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()) + ); + + let mut bearer_decision = decision( + "GET /v2/latest", + "create", + None, + None, + Some(bearer_profile.id.as_str()), + ); + bearer_decision.security_alternative_index = Some(1); + let bearer_plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload("full", vec![], vec![bearer_decision]), + ) + .await + .unwrap(); + let stored_bearer = registry + .get_import_plan(&workspace_id, &bearer_plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!( + stored_bearer.payload["candidates"][0]["operation"]["execution_config"]["auth_profile_ref"], + bearer_profile.id.as_str() + ); + assert_eq!( + stored_bearer.payload["candidates"][0]["decision"]["security_alternative_index"], + 1 + ); + assert!( + !stored_bearer + .payload + .to_string() + .contains("bearer-choice-canary") + ); + + // An unknown profile reference fails closed in the workspace scope check. + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_SECURITY)) + .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, "import_plan_auth_unavailable"); + 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_SECURITY)) + .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)); + + let public_preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_PUBLIC)) + .await + .unwrap(); + let public_job_id: ImportJobId = public_preview.job_id.as_str().into(); + let public_plan = service + .create_openapi_import_plan( + &workspace_id, + &public_job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "new_version", + Some(existing.operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .expect("a no-security source may inherit the target's existing profile"); + let public_stored = registry + .get_import_plan(&workspace_id, &public_plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert_eq!( + public_stored.payload["candidates"][0]["auth_requirement"]["kind"], "unconstrained", + "no source security declaration is represented explicitly for transactional Apply" + ); + let report = service + .apply_openapi_import_plan( + &workspace_id, + &crank_registry::ImportPlanId::new(public_plan.plan_reference.clone()), + admin_api::service::OpenApiImportPlanApplyPayload { + plan_digest: public_plan.plan_digest.clone(), + idempotency_key: "new-version-inherited-public".to_owned(), + }, + ) + .await + .expect("Apply must retain profile and Secret transactional validation"); + assert_eq!( + report.status, + crank_registry::ImportPlanApplyStatus::Applied + ); + assert_eq!( + report.outcomes[0].outcome, + crank_registry::ImportPlanCandidateOutcome::Versioned + ); + assert_eq!(report.outcomes[0].version, Some(2)); + let operation = service + .list_operations(&workspace_id) + .await + .unwrap() + .into_iter() + .find(|operation| operation.id == existing.operation_id.as_str()) + .unwrap(); + assert_eq!(operation.current_draft_version, 2); + + let explicit_no_auth_preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_SINGLE_EXPLICIT_NO_AUTH)) + .await + .unwrap(); + let explicit_no_auth_job_id: ImportJobId = explicit_no_auth_preview.job_id.as_str().into(); + let incompatible_profile_decision = decision( + "GET /v2/latest", + "new_version", + Some(existing.operation_id.as_str()), + None, + Some(auth_profile.id.as_str()), + ); + let error = service + .create_openapi_import_plan( + &workspace_id, + &explicit_no_auth_job_id, + plan_payload("full", vec![], vec![incompatible_profile_decision]), + ) + .await + .expect_err("explicit Auth Profile stays incompatible with security: []"); + assert_error_code(&error, "import_plan_auth_incompatible"); + + let explicit_no_auth_plan = service + .create_openapi_import_plan( + &workspace_id, + &explicit_no_auth_job_id, + plan_payload( + "full", + vec![], + vec![decision( + "GET /v2/latest", + "new_version", + Some(existing.operation_id.as_str()), + None, + None, + )], + ), + ) + .await + .expect("an explicit empty security requirement clears inherited auth"); + let no_auth_stored = registry + .get_import_plan( + &workspace_id, + &explicit_no_auth_plan.plan_reference.as_str().into(), + ) + .await + .unwrap() + .unwrap(); + assert!(no_auth_stored.payload["candidates"][0]["operation"]["execution_config"] + ["auth_profile_ref"] + .is_null()); + let no_auth_report = service + .apply_openapi_import_plan( + &workspace_id, + &crank_registry::ImportPlanId::new(explicit_no_auth_plan.plan_reference.clone()), + admin_api::service::OpenApiImportPlanApplyPayload { + plan_digest: explicit_no_auth_plan.plan_digest.clone(), + idempotency_key: "new-version-explicit-no-auth".to_owned(), + }, + ) + .await + .expect("explicit no-auth new_version applies without inherited credentials"); + assert_eq!( + no_auth_report.outcomes[0].outcome, + crank_registry::ImportPlanCandidateOutcome::Versioned + ); + assert_eq!(no_auth_report.outcomes[0].version, Some(3)); + let operation = service + .list_operations(&workspace_id) + .await + .unwrap() + .into_iter() + .find(|operation| operation.id == existing.operation_id.as_str()) + .unwrap(); + assert_eq!(operation.current_draft_version, 3); + let operation_id = OperationId::new(existing.operation_id.clone()); + let version = registry + .get_operation_version(&workspace_id, &operation_id, 3) + .await + .unwrap() + .unwrap(); + assert!(version.snapshot.execution_config.auth_profile_ref.is_none()); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn basic_and_query_profiles_bind_to_matching_security_alternatives_and_apply() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_basic_query_auth"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + + let username_secret = service + .create_secret( + &workspace_id, + None, + SecretPayload { + name: "basic-username".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": "plan-user" }), + }, + None, + ) + .await + .unwrap(); + let password_secret = service + .create_secret( + &workspace_id, + None, + SecretPayload { + name: "basic-password".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": "plan-password-canary" }), + }, + None, + ) + .await + .unwrap(); + let basic_profile = service + .create_auth_profile( + &workspace_id, + AuthProfilePayload { + name: "basic-profile".to_owned(), + kind: AuthKind::Basic, + config: AuthConfig::Basic(crank_core::BasicAuthConfig { + username_secret_id: SecretId::new(username_secret.id.as_str()), + password_secret_id: SecretId::new(password_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + let query_secret = service + .create_secret( + &workspace_id, + None, + SecretPayload { + name: "query-key".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": "query-key-canary" }), + }, + None, + ) + .await + .unwrap(); + let query_profile = service + .create_auth_profile( + &workspace_id, + AuthProfilePayload { + name: "query-profile".to_owned(), + kind: AuthKind::ApiKeyQuery, + config: AuthConfig::ApiKeyQuery(crank_core::ApiKeyQueryAuthConfig { + param_name: "access_token".to_owned(), + secret_id: SecretId::new(query_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + let incompatible_secret = service + .create_secret( + &workspace_id, + None, + SecretPayload { + name: "incompatible-token".to_owned(), + kind: SecretKind::Token, + value: json!({ "token": "incompatible-canary" }), + }, + None, + ) + .await + .unwrap(); + let incompatible_profile = service + .create_auth_profile( + &workspace_id, + AuthProfilePayload { + name: "incompatible-bearer-profile".to_owned(), + kind: AuthKind::Bearer, + config: AuthConfig::Bearer(crank_core::BearerAuthConfig { + header_name: "Authorization".to_owned(), + secret_id: SecretId::new(incompatible_secret.id.as_str()), + }), + }, + None, + ) + .await + .unwrap(); + + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_BASIC_AND_QUERY_SECURITY)) + .await + .unwrap(); + for operation_key in ["GET /v2/basic", "GET /v2/query"] { + let basic_alternative = preview + .security_profile_choices + .iter() + .find(|choice| choice.operation_key == operation_key && choice.alternative_index == 0) + .expect("Basic alternative must be represented in preview"); + assert!(basic_alternative.profiles.iter().any(|choice| { + choice.id == basic_profile.id.as_str() && choice.kind == AuthKind::Basic + })); + assert!( + basic_alternative + .profiles + .iter() + .all(|choice| choice.id != query_profile.id.as_str()) + ); + assert!( + basic_alternative + .profiles + .iter() + .all(|choice| choice.id != incompatible_profile.id.as_str()) + ); + + let query_alternative = preview + .security_profile_choices + .iter() + .find(|choice| choice.operation_key == operation_key && choice.alternative_index == 1) + .expect("query alternative must be represented in preview"); + assert!(query_alternative.profiles.iter().any(|choice| { + choice.id == query_profile.id.as_str() && choice.kind == AuthKind::ApiKeyQuery + })); + assert!( + query_alternative + .profiles + .iter() + .all(|choice| choice.id != basic_profile.id.as_str()) + ); + assert!( + query_alternative + .profiles + .iter() + .all(|choice| choice.id != incompatible_profile.id.as_str()) + ); + } + + let job_id: ImportJobId = preview.job_id.as_str().into(); + let mut basic_decision = decision( + "GET /v2/basic", + "create", + None, + None, + Some(basic_profile.id.as_str()), + ); + basic_decision.security_alternative_index = Some(0); + let basic_plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "partial", + vec!["GET /v2/basic".to_owned()], + vec![basic_decision], + ), + ) + .await + .unwrap(); + assert_eq!(basic_plan.counts.actionable, 1); + let basic_candidate = basic_plan + .candidates + .iter() + .find(|candidate| candidate.key == "GET /v2/basic") + .unwrap(); + assert_eq!( + basic_candidate.auth_profile_id.as_deref(), + Some(basic_profile.id.as_str()) + ); + + let mut query_decision = decision( + "GET /v2/query", + "create", + None, + None, + Some(query_profile.id.as_str()), + ); + query_decision.security_alternative_index = Some(1); + let query_plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload( + "partial", + vec!["GET /v2/query".to_owned()], + vec![query_decision.clone()], + ), + ) + .await + .unwrap(); + assert_eq!(query_plan.counts.actionable, 1); + let query_candidate = query_plan + .candidates + .iter() + .find(|candidate| candidate.key == "GET /v2/query") + .unwrap(); + assert_eq!( + query_candidate.auth_profile_id.as_deref(), + Some(query_profile.id.as_str()) + ); + + for (index, (plan, expected_profile, idempotency_key)) in [ + ( + &basic_plan, + basic_profile.id.as_str(), + "apply-basic-profile", + ), + ( + &query_plan, + query_profile.id.as_str(), + "apply-query-profile", + ), + ] + .into_iter() + .enumerate() + { + let fresh_plan; + let plan = if index == 1 { + let stale = service + .apply_openapi_import_plan( + &workspace_id, + &crank_registry::ImportPlanId::new(plan.plan_reference.clone()), + admin_api::service::OpenApiImportPlanApplyPayload { + plan_digest: plan.plan_digest.clone(), + idempotency_key: "stale-query-profile".to_owned(), + }, + ) + .await + .expect_err("the earlier query plan is stale after catalog mutation"); + assert_error_code(&stale, "import_plan_fingerprint_stale"); + let refreshed = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_BASIC_AND_QUERY_SECURITY)) + .await + .unwrap(); + fresh_plan = service + .create_openapi_import_plan( + &workspace_id, + &refreshed.job_id.as_str().into(), + plan_payload( + "partial", + vec!["GET /v2/query".to_owned()], + vec![query_decision.clone()], + ), + ) + .await + .unwrap(); + &fresh_plan + } else { + plan + }; + let report = service + .apply_openapi_import_plan( + &workspace_id, + &crank_registry::ImportPlanId::new(plan.plan_reference.clone()), + admin_api::service::OpenApiImportPlanApplyPayload { + plan_digest: plan.plan_digest.clone(), + idempotency_key: idempotency_key.to_owned(), + }, + ) + .await + .unwrap(); + assert_eq!( + report.status, + crank_registry::ImportPlanApplyStatus::Applied + ); + let operation_id = OperationId::new(report.outcomes[0].operation_id.clone().unwrap()); + let version = registry + .get_operation_version(&workspace_id, &operation_id, 1) + .await + .unwrap() + .unwrap(); + assert_eq!( + version + .snapshot + .execution_config + .auth_profile_ref + .as_ref() + .map(|profile_id| profile_id.as_str()), + Some(expected_profile) + ); + } + let basic_stored = registry + .get_import_plan(&workspace_id, &basic_plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert!( + !basic_stored + .payload + .to_string() + .contains("plan-password-canary") + ); + let query_stored = registry + .get_import_plan(&workspace_id, &query_plan.plan_reference.as_str().into()) + .await + .unwrap() + .unwrap(); + assert!( + !query_stored + .payload + .to_string() + .contains("query-key-canary") + ); +} 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..2fff462 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/import_plan_create/validation.rs @@ -0,0 +1,350 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn preview_rejects_candidate_2001_without_truncation_or_operations() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_preview_candidate_limit"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace = WorkspaceId::new("ws_default"); + let mut document = String::from( + "openapi: 3.0.3\ninfo: { title: Limit }\nservers: [{ url: https://api.example.test }]\npaths:\n", + ); + for index in 0..2001 { + document.push_str(&format!(" /p{index}:\n get:\n operationId: op{index}\n responses: {{ '200': {{ description: ok }} }}\n")); + } + let error = service + .preview_openapi_import(&workspace, upload(&document)) + .await + .expect_err("candidate 2001 must be rejected as a whole"); + assert_error_code(&error, "openapi_preview_too_many_candidates"); + assert!( + service + .list_operations(&workspace) + .await + .unwrap() + .is_empty() + ); + let active_sources: i64 = sqlx::query_scalar( + "select count(*) from artifact_sources where workspace_id = $1 and lifecycle = 'active'", + ) + .bind(workspace.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!(active_sources, 0); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn partial_plan_rejects_selected_blocker_before_persistence() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_plan_partial_blocker"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, upload(OPENAPI_GLOBAL_BLOCKER)) + .await + .unwrap(); + assert_eq!(preview.preview.groups[0].operations[0].status, "blocked"); + let job_id: ImportJobId = preview.job_id.as_str().into(); + let key = preview.preview.groups[0].operations[0].key.clone(); + let error = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + plan_payload("partial", vec![key], vec![]), + ) + .await + .expect_err("blocked partial selection must be rejected"); + assert_error_code(&error, "import_plan_partial_blocked"); + assert!( + service + .list_operations(&workspace_id) + .await + .unwrap() + .is_empty() + ); +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn explicit_targets_are_checked_even_when_the_candidate_is_blocked() { + 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/integration/openapi_import/large_preview.rs b/apps/admin-api/tests/integration/openapi_import/large_preview.rs new file mode 100644 index 0000000..8702916 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/large_preview.rs @@ -0,0 +1,142 @@ +use super::*; + +use admin_api::service::OpenApiImportPlanCreatePayload; +use std::fmt::Write as _; + +const OPERATION_COUNT: usize = 2_000; +const MAX_UPLOAD_BYTES: usize = 256 * 1024; +const MAX_STORED_PLAN_BYTES: usize = 16 * 1024 * 1024; + +fn large_openapi_document() -> String { + let mut document = String::from( + "openapi: 3.0.3\n\ + info: { title: Large preview, version: '1' }\n\ + servers: [{ url: 'https://api.example.test' }]\n\ + paths:\n", + ); + for index in 0..OPERATION_COUNT { + writeln!( + document, + " /p{index:04}:\n get:\n operationId: o{index:04}\n responses: {{ '200': {{ description: ok }} }}" + ) + .unwrap(); + } + document +} + +#[tokio::test] +#[serial] +async fn previews_and_plans_two_thousand_operations_without_catalog_mutation() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_large_preview"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let document = large_openapi_document(); + assert!( + document.len() <= MAX_UPLOAD_BYTES, + "generated document is {} bytes, over the 256 KiB upload limit", + document.len() + ); + + let preview = service + .preview_openapi_import( + &workspace_id, + OpenApiUpload { + bytes: document.into_bytes(), + mime_type: "application/yaml".to_owned(), + locale: OpenApiUploadLocale::En, + }, + ) + .await + .expect("valid 2,000-operation OpenAPI document should preview"); + let preview_candidates = preview + .preview + .groups + .iter() + .flat_map(|group| &group.operations) + .collect::>(); + assert_eq!(preview_candidates.len(), OPERATION_COUNT); + assert_eq!(preview_candidates[0].key, "GET /p0000"); + assert_eq!(preview_candidates[OPERATION_COUNT - 1].key, "GET /p1999"); + assert!(preview_candidates.iter().all(|candidate| { + candidate.status == "actionable" + && candidate.method == crank_core::HttpMethod::Get + && candidate.path.starts_with("/p") + })); + + let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into(); + let plan = service + .create_openapi_import_plan( + &workspace_id, + &job_id, + OpenApiImportPlanCreatePayload { + selection_mode: "full".to_owned(), + selected_operation_keys: Vec::new(), + server_url: None, + decisions: Vec::new(), + }, + ) + .await + .expect("full plan should include all 2,000 operations"); + assert_eq!(plan.counts.selected, OPERATION_COUNT); + assert_eq!(plan.counts.actionable, OPERATION_COUNT); + assert_eq!(plan.counts.excluded, 0); + assert_eq!(plan.counts.blocked, 0); + assert_eq!(plan.candidates.len(), OPERATION_COUNT); + assert!(plan.candidates.iter().all(|candidate| { + candidate.state == "actionable" && candidate.action.as_deref() == Some("create") + })); + + let plan_id = plan.plan_reference.as_str().into(); + let stored = registry + .get_import_plan(&workspace_id, &plan_id) + .await + .unwrap() + .expect("full plan should be persisted and readable"); + let stored_payload_bytes = serde_json::to_vec(&stored.payload).unwrap().len(); + assert!( + stored_payload_bytes <= MAX_STORED_PLAN_BYTES, + "stored plan payload is {stored_payload_bytes} bytes, over 16 MiB" + ); + let stored_candidates = stored.payload["candidates"] + .as_array() + .expect("stored plan contains its full candidate list"); + assert_eq!(stored_candidates.len(), OPERATION_COUNT); + assert!(stored_candidates.iter().all(|candidate| { + candidate["state"] == "actionable" && candidate["decision"]["action"] == "create" + })); + assert_eq!(stored.payload["counts"]["selected"], OPERATION_COUNT); + assert_eq!(stored.payload["counts"]["actionable"], OPERATION_COUNT); + + let revalidated = service + .revalidate_openapi_import_plan(&workspace_id, &plan_id) + .await + .expect("revalidating the unchanged plan should be read-only and current"); + assert_eq!(revalidated.revalidation_status, "current"); + assert!(!revalidated.revalidation_required); + assert_eq!(revalidated.counts.selected, OPERATION_COUNT); + assert_eq!(revalidated.counts.actionable, OPERATION_COUNT); + assert_eq!(revalidated.counts.excluded, 0); + assert_eq!(revalidated.counts.blocked, 0); + assert_eq!(revalidated.candidates.len(), OPERATION_COUNT); + + let (operations, operation_versions) = sqlx::query_as::<_, (i64, i64)>( + "select + (select count(*) from operations where workspace_id = $1), + (select count(*) from operation_versions ov + join operations o on o.id = ov.operation_id where o.workspace_id = $1)", + ) + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + assert_eq!(operations, 0, "preview and plan must not create operations"); + assert_eq!( + operation_versions, 0, + "preview and plan must not create operation versions" + ); +} diff --git a/apps/admin-api/tests/integration/openapi_import/security.rs b/apps/admin-api/tests/integration/openapi_import/security.rs deleted file mode 100644 index afb2b86..0000000 --- a/apps/admin-api/tests/integration/openapi_import/security.rs +++ /dev/null @@ -1,659 +0,0 @@ -use super::*; - -#[tokio::test] -#[serial] -async fn literal_credential_is_rejected_before_any_import_artifact_or_job_is_created() { - let registry = test_registry().await; - let service = test_service( - registry.clone(), - test_storage_root("openapi_import_literal_credential_rejected"), - test_auth_settings(), - test_secret_crypto(), - ); - let workspace_id = WorkspaceId::new("ws_default"); - let canary = "atdd-secret-canary-2-10-api"; - let upload = OpenApiUpload { - bytes: format!( - r#" -openapi: 3.0.3 -info: {{ title: Secret canary }} -paths: - /items: - get: - responses: - '200': {{ description: ok }} -x-api-key: {canary} -"# - ) - .into_bytes(), - mime_type: "application/yaml".to_owned(), - locale: OpenApiUploadLocale::En, - }; - - let error = service - .preview_openapi_import(&workspace_id, upload) - .await - .unwrap_err(); - assert!(!format!("{error:?}").contains(canary)); - let import_jobs: i64 = - sqlx::query_scalar("select count(*) from import_jobs where workspace_id = $1") - .bind(workspace_id.as_str()) - .fetch_one(registry.pool()) - .await - .unwrap(); - let artifact_sources: i64 = - sqlx::query_scalar("select count(*) from artifact_sources where workspace_id = $1") - .bind(workspace_id.as_str()) - .fetch_one(registry.pool()) - .await - .unwrap(); - assert_eq!(import_jobs, 0); - assert_eq!(artifact_sources, 0); -} - -#[tokio::test] -#[serial] -async fn auth_profiles_are_workspace_scoped_revalidated_and_shared_secret_availability_is_current() -{ - let registry = test_registry().await; - let service = test_service( - registry.clone(), - test_storage_root("openapi_import_auth_profile_binding"), - test_auth_settings(), - test_secret_crypto(), - ); - let workspace_id = WorkspaceId::new("ws_default"); - let secret = service - .create_secret( - &workspace_id, - None, - SecretPayload { - name: "shared-openapi-token".to_owned(), - kind: SecretKind::Token, - value: json!("integration-secret-canary"), - }, - None, - ) - .await - .unwrap(); - let ciphertext = registry - .get_current_secret_version(&workspace_id, &secret.id) - .await - .unwrap() - .unwrap() - .secret_version - .ciphertext; - let secret_hash = format!("{:x}", sha2::Sha256::digest(b"integration-secret-canary")); - let profile_a = create_bearer_profile(&service, &workspace_id, "profile-a", &secret.id).await; - let profile_b = create_bearer_profile(&service, &workspace_id, "profile-b", &secret.id).await; - - let preview = service - .preview_openapi_import(&workspace_id, two_protected_operations_upload()) - .await - .unwrap(); - assert_eq!(preview.auth_profile_choices.len(), 2); - for choice in &preview.auth_profile_choices { - assert!(choice.supported); - assert_eq!(choice.profiles.len(), 2); - } - let response = serde_json::to_string(&preview).unwrap(); - assert!(!response.contains("integration-secret-canary")); - - let applied = service - .create_openapi_import( - &workspace_id, - &preview.job_id.as_str().into(), - OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /a".to_owned(), "GET /b".to_owned()], - auth_mappings: vec![ - OpenApiAuthMapping { - operation_key: "GET /a".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile_a.id.as_str().to_owned()), - }, - OpenApiAuthMapping { - operation_key: "GET /b".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile_b.id.as_str().to_owned()), - }, - ], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - ) - .await - .unwrap(); - assert_eq!(applied.created.len(), 2); - for created in &applied.created { - let version = service - .get_operation_version( - &workspace_id, - &created.operation_id.as_str().into(), - created.version, - ) - .await - .unwrap(); - let profile_ref = version - .snapshot - .execution_config - .auth_profile_ref - .as_ref() - .unwrap(); - let serialized_operation = serde_json::to_string(&version.snapshot).unwrap(); - assert!(serialized_operation.contains(profile_ref.as_str())); - assert!(!serialized_operation.contains("integration-secret-canary")); - assert!(!serialized_operation.contains(&ciphertext)); - assert!(!serialized_operation.contains(&secret_hash)); - } - let changed_mapping_replay = service - .create_openapi_import( - &workspace_id, - &preview.job_id.as_str().into(), - OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /a".to_owned(), "GET /b".to_owned()], - auth_mappings: vec![ - OpenApiAuthMapping { - operation_key: "GET /a".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile_b.id.as_str().to_owned()), - }, - OpenApiAuthMapping { - operation_key: "GET /b".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile_a.id.as_str().to_owned()), - }, - ], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - ) - .await; - assert!(changed_mapping_replay.is_err()); - assert_eq!( - service.list_operations(&workspace_id).await.unwrap().len(), - 2 - ); - - let now = time::OffsetDateTime::now_utc(); - let foreign_workspace_id = WorkspaceId::new("ws_openapi_auth_foreign"); - let foreign_workspace = Workspace { - id: foreign_workspace_id.clone(), - slug: "openapi-auth-foreign".to_owned(), - display_name: "OpenAPI Auth Foreign".to_owned(), - status: WorkspaceStatus::Active, - settings: json!({}), - created_at: now, - updated_at: now, - }; - registry - .create_workspace(CreateWorkspaceRequest { - workspace: &foreign_workspace, - }) - .await - .unwrap(); - let foreign_secret = service - .create_secret( - &foreign_workspace_id, - None, - SecretPayload { - name: "foreign-token".to_owned(), - kind: SecretKind::Token, - value: json!("foreign-secret-canary"), - }, - None, - ) - .await - .unwrap(); - let foreign_profile = create_bearer_profile( - &service, - &foreign_workspace_id, - "foreign-profile", - &foreign_secret.id, - ) - .await; - let choices_preview = service - .preview_openapi_import(&workspace_id, two_protected_operations_upload()) - .await - .unwrap(); - assert!( - choices_preview - .auth_profile_choices - .iter() - .flat_map(|choice| &choice.profiles) - .all(|profile| profile.id != foreign_profile.id.as_str()) - ); - - let missing_error = - apply_first_protected_mapping(&service, &workspace_id, "auth_missing_profile") - .await - .unwrap_err(); - let foreign_error = - apply_first_protected_mapping(&service, &workspace_id, foreign_profile.id.as_str()) - .await - .unwrap_err(); - assert_eq!(format!("{missing_error:?}"), format!("{foreign_error:?}")); - assert!(!format!("{foreign_error:?}").contains(foreign_profile.id.as_str())); - - sqlx::query("update secrets set status = 'disabled' where workspace_id = $1 and id = $2") - .bind(workspace_id.as_str()) - .bind(secret.id.as_str()) - .execute(registry.pool()) - .await - .unwrap(); - let disabled_preview = service - .preview_openapi_import(&workspace_id, two_protected_operations_upload()) - .await - .unwrap(); - assert!( - disabled_preview - .auth_profile_choices - .iter() - .all(|choice| choice.profiles.is_empty()) - ); - let before_disabled_apply = service.list_operations(&workspace_id).await.unwrap().len(); - let disabled_error = service - .create_openapi_import_with_locale( - &workspace_id, - &disabled_preview.job_id.as_str().into(), - OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /a".to_owned()], - auth_mappings: vec![OpenApiAuthMapping { - operation_key: "GET /a".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile_a.id.as_str().to_owned()), - }], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - OpenApiUploadLocale::Ru, - ) - .await - .unwrap_err(); - match &disabled_error { - admin_api::error::ApiError::Unprocessable { message, context } => { - assert_eq!( - message, - "выбранный профиль аутентификации недоступен для этого импорта" - ); - assert_eq!( - context.as_ref().unwrap()["error_code"], - "openapi_auth_binding_unavailable" - ); - } - other => panic!("expected localized auth binding error, got {other:?}"), - } - let disabled_shared_profile_error = - apply_first_protected_mapping(&service, &workspace_id, profile_b.id.as_str()) - .await - .unwrap_err(); - assert_eq!( - format!("{disabled_shared_profile_error:?}"), - format!("{missing_error:?}") - ); - assert_eq!( - service.list_operations(&workspace_id).await.unwrap().len(), - before_disabled_apply - ); -} - -#[tokio::test] -#[serial] -async fn selected_supported_or_and_explicit_no_auth_alternatives_apply_without_collapsing_semantics() - { - let registry = test_registry().await; - let service = test_service( - registry, - test_storage_root("openapi_import_auth_alternatives"), - test_auth_settings(), - test_secret_crypto(), - ); - let workspace_id = WorkspaceId::new("ws_default"); - let secret = service - .create_secret( - &workspace_id, - None, - SecretPayload { - name: "or-token".to_owned(), - kind: SecretKind::Token, - value: json!("or-profile-secret"), - }, - None, - ) - .await - .unwrap(); - let profile = create_bearer_profile(&service, &workspace_id, "or-bearer", &secret.id).await; - let preview = service - .preview_openapi_import(&workspace_id, security_alternatives_upload()) - .await - .unwrap(); - let choices = preview - .auth_profile_choices - .iter() - .filter(|choice| choice.operation_key == "GET /choice") - .collect::>(); - assert_eq!(choices.len(), 2); - assert!(choices[0].supported); - assert_eq!(choices[0].profiles.len(), 1); - assert!(!choices[1].supported); - let either = preview - .auth_profile_choices - .iter() - .filter(|choice| choice.operation_key == "GET /either") - .collect::>(); - assert_eq!(either.len(), 2); - assert!(either[1].supported); - - let applied = service - .create_openapi_import( - &workspace_id, - &preview.job_id.as_str().into(), - OpenApiImportCreatePayload { - selected_operation_keys: vec![ - "GET /choice".to_owned(), - "GET /either".to_owned(), - "GET /public".to_owned(), - ], - auth_mappings: vec![ - OpenApiAuthMapping { - operation_key: "GET /choice".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile.id.as_str().to_owned()), - }, - OpenApiAuthMapping { - operation_key: "GET /either".to_owned(), - alternative_index: 1, - auth_profile_id: None, - }, - ], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - ) - .await - .unwrap(); - assert_eq!(applied.created.len(), 3); - for created in &applied.created { - let version = service - .get_operation_version( - &workspace_id, - &created.operation_id.as_str().into(), - created.version, - ) - .await - .unwrap(); - let auth_profile_ref = version.snapshot.execution_config.auth_profile_ref; - match created.operation_key.as_str() { - "GET /choice" => assert_eq!(auth_profile_ref, Some(profile.id.clone())), - "GET /either" | "GET /public" => assert!(auth_profile_ref.is_none()), - other => panic!("unexpected operation {other}"), - } - } -} - -#[tokio::test] -#[serial] -async fn changed_security_scheme_after_preview_fails_digest_check_without_draft_mutation() { - let registry = test_registry().await; - let service = test_service( - registry.clone(), - test_storage_root("openapi_import_security_scheme_drift"), - test_auth_settings(), - test_secret_crypto(), - ); - let workspace_id = WorkspaceId::new("ws_default"); - let secret = service - .create_secret( - &workspace_id, - None, - SecretPayload { - name: "drift-test-token".to_owned(), - kind: SecretKind::Token, - value: json!("scheme-drift-profile-secret"), - }, - None, - ) - .await - .unwrap(); - let profile = - create_bearer_profile(&service, &workspace_id, "drift-test-profile", &secret.id).await; - let bearer_preview = service - .preview_openapi_import(&workspace_id, security_scheme_upload(false)) - .await - .unwrap(); - let api_key_preview = service - .preview_openapi_import(&workspace_id, security_scheme_upload(true)) - .await - .unwrap(); - let bearer_job: crank_registry::ImportJobId = bearer_preview.job_id.as_str().into(); - let api_key_job: crank_registry::ImportJobId = api_key_preview.job_id.as_str().into(); - let api_key_source = registry - .get_import_job(&workspace_id, &api_key_job) - .await - .unwrap() - .unwrap() - .preview_payload["source"] - .clone(); - sqlx::query( - "update import_jobs - set preview_payload = jsonb_set( - jsonb_set(preview_payload, '{source,source_id}', $1, false), - '{source,digest}', $2, false - ) - where workspace_id = $3 and id = $4", - ) - .bind(&api_key_source["source_id"]) - .bind(&api_key_source["digest"]) - .bind(workspace_id.as_str()) - .bind(bearer_job.as_str()) - .execute(registry.pool()) - .await - .unwrap(); - - let error = service - .create_openapi_import( - &workspace_id, - &bearer_job, - OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /secure".to_owned()], - auth_mappings: vec![OpenApiAuthMapping { - operation_key: "GET /secure".to_owned(), - alternative_index: 0, - auth_profile_id: Some(profile.id.as_str().to_owned()), - }], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - ) - .await - .unwrap_err(); - match error { - admin_api::error::ApiError::Unprocessable { context, .. } => assert_eq!( - context.unwrap()["error_code"], - "openapi_upload.source_integrity" - ), - other => panic!("expected source integrity error, got {other:?}"), - } - let job = registry - .get_import_job(&workspace_id, &bearer_job) - .await - .unwrap() - .unwrap(); - assert_eq!( - job.error_text.as_deref(), - Some("import_replay_verification_failed") - ); - assert!( - service - .list_operations(&workspace_id) - .await - .unwrap() - .is_empty() - ); -} - -#[tokio::test] -#[serial] -async fn auth_mapping_for_unselected_operation_is_rejected_without_draft_mutation() { - let registry = test_registry().await; - let service = test_service( - registry, - test_storage_root("openapi_import_unselected_auth_mapping"), - test_auth_settings(), - test_secret_crypto(), - ); - let workspace_id = WorkspaceId::new("ws_default"); - let preview = service - .preview_openapi_import(&workspace_id, two_protected_operations_upload()) - .await - .unwrap(); - assert!( - service - .create_openapi_import( - &workspace_id, - &preview.job_id.as_str().into(), - OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /a".to_owned()], - auth_mappings: vec![OpenApiAuthMapping { - operation_key: "GET /b".to_owned(), - alternative_index: 0, - auth_profile_id: None, - }], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - ) - .await - .is_err() - ); - assert!( - service - .list_operations(&workspace_id) - .await - .unwrap() - .is_empty() - ); -} - -fn two_protected_operations_upload() -> OpenApiUpload { - OpenApiUpload { - bytes: br#" -openapi: 3.0.3 -info: { title: Protected API } -servers: [{ url: https://api.example.test }] -components: - securitySchemes: - bearerAuth: { type: http, scheme: bearer } -security: - - bearerAuth: [] -paths: - /a: - get: - responses: { '200': { description: ok } } - /b: - get: - responses: { '200': { description: ok } } -"# - .to_vec(), - mime_type: "application/yaml".to_owned(), - locale: OpenApiUploadLocale::En, - } -} - -fn security_alternatives_upload() -> OpenApiUpload { - OpenApiUpload { - bytes: br#" -openapi: 3.0.3 -info: { title: Security alternatives } -servers: [{ url: https://api.example.test }] -components: - securitySchemes: - bearerAuth: { type: http, scheme: bearer } - oauth: { type: oauth2, flows: { implicit: { authorizationUrl: https://auth.example.test, scopes: {} } } } -security: - - bearerAuth: [] - - oauth: [] -paths: - /choice: - get: - responses: { '200': { description: ok } } - /either: - get: - security: - - bearerAuth: [] - - {} - responses: { '200': { description: ok } } - /public: - get: - security: [] - responses: { '200': { description: ok } } -"# - .to_vec(), - mime_type: "application/yaml".to_owned(), - locale: OpenApiUploadLocale::En, - } -} - -fn security_scheme_upload(api_key: bool) -> OpenApiUpload { - let security_scheme = if api_key { - "auth: { type: apiKey, in: header, name: X-Token }" - } else { - "auth: { type: http, scheme: bearer }" - }; - OpenApiUpload { - bytes: format!( - "openapi: 3.0.3\ninfo: {{ title: Scheme drift }}\nservers: [{{ url: https://api.example.test }}]\ncomponents:\n securitySchemes:\n {security_scheme}\nsecurity:\n - auth: []\npaths:\n /secure:\n get:\n responses: {{ '200': {{ description: ok }} }}\n" - ) - .into_bytes(), - mime_type: "application/yaml".to_owned(), - locale: OpenApiUploadLocale::En, - } -} - -async fn create_bearer_profile( - service: &admin_api::service::AdminService, - workspace_id: &WorkspaceId, - name: &str, - secret_id: &SecretId, -) -> crank_core::AuthProfile { - service - .create_auth_profile( - workspace_id, - AuthProfilePayload { - name: name.to_owned(), - kind: AuthKind::Bearer, - config: AuthConfig::Bearer(BearerAuthConfig { - header_name: "Authorization".to_owned(), - secret_id: secret_id.clone(), - }), - }, - None, - ) - .await - .unwrap() -} - -async fn apply_first_protected_mapping( - service: &admin_api::service::AdminService, - workspace_id: &WorkspaceId, - auth_profile_id: &str, -) -> Result { - let preview = service - .preview_openapi_import(workspace_id, two_protected_operations_upload()) - .await?; - service - .create_openapi_import( - workspace_id, - &preview.job_id.as_str().into(), - OpenApiImportCreatePayload { - selected_operation_keys: vec!["GET /a".to_owned()], - auth_mappings: vec![OpenApiAuthMapping { - operation_key: "GET /a".to_owned(), - alternative_index: 0, - auth_profile_id: Some(auth_profile_id.to_owned()), - }], - server_url: None, - conflict_mode: "skip".to_owned(), - }, - ) - .await -} diff --git a/apps/admin-api/tests/integration/openapi_import/upload_outcomes.rs b/apps/admin-api/tests/integration/openapi_import/upload_outcomes.rs new file mode 100644 index 0000000..2ae1194 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/upload_outcomes.rs @@ -0,0 +1,262 @@ +use super::*; + +use admin_api::error::ApiError; +use crank_artifacts::MAX_ARTIFACT_BYTES; +use crank_core::HttpMethod; + +#[derive(Debug, PartialEq, Eq)] +struct CatalogCounts { + operations: i64, + operation_versions: i64, + auth_profiles: i64, + agent_operation_bindings: i64, +} + +async fn catalog_counts( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, +) -> CatalogCounts { + let (operations, operation_versions, auth_profiles, agent_operation_bindings) = + sqlx::query_as::<_, (i64, i64, i64, i64)>( + "select + (select count(*) from operations where workspace_id = $1), + (select count(*) from operation_versions ov + join operations o on o.id = ov.operation_id where o.workspace_id = $1), + (select count(*) from auth_profiles where workspace_id = $1), + (select count(*) from agent_operation_bindings b + join operations o on o.id = b.operation_id where o.workspace_id = $1)", + ) + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap(); + + CatalogCounts { + operations, + operation_versions, + auth_profiles, + agent_operation_bindings, + } +} + +fn upload(bytes: Vec) -> OpenApiUpload { + OpenApiUpload { + bytes, + mime_type: "application/json".to_owned(), + locale: OpenApiUploadLocale::En, + } +} + +const PREVIEWABLE_DOCUMENT: &[u8] = br#"{ + "openapi":"3.0.3", + "info":{"title":"Preview lifecycle","version":"1"}, + "paths":{"/ready":{"get":{"responses":{"200":{"description":"ok"}}}}} +}"#; + +async fn insert_malformed_auth_profile( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, +) { + sqlx::query( + "insert into auth_profiles + (id, workspace_id, name, kind, config_json, created_at, updated_at) + values ('auth_decode_failure', $1, 'decode-failure', 'bearer', 'null'::jsonb, now(), now())", + ) + .bind(workspace_id.as_str()) + .execute(registry.pool()) + .await + .unwrap(); +} + +async fn insert_catalog_overflow( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, +) { + sqlx::query( + "insert into operations + (id, workspace_id, name, display_name, protocol, status, created_at, updated_at) + select 'op_catalog_' || g, $1, 'catalog_op_' || g, 'Catalog Op ' || g, + 'rest', 'draft', now(), now() + from generate_series(1, 10001) as g", + ) + .bind(workspace_id.as_str()) + .execute(registry.pool()) + .await + .unwrap(); +} + +async fn durable_import_jobs( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, +) -> i64 { + sqlx::query_scalar("select count(*) from import_jobs where workspace_id = $1") + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap() +} + +async fn active_artifact_sources( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, +) -> i64 { + sqlx::query_scalar( + "select count(*) from artifact_sources where workspace_id = $1 and lifecycle = 'active'", + ) + .bind(workspace_id.as_str()) + .fetch_one(registry.pool()) + .await + .unwrap() +} + +fn assert_upload_error(error: ApiError, variant: &str, code: &str) { + let context = match (variant, error) { + ("validation", ApiError::Validation { context, .. }) => context, + ("payload_too_large", ApiError::PayloadTooLarge { context, .. }) => context, + (expected, actual) => panic!("expected {expected} error, got {actual:?}"), + } + .expect("OpenAPI upload errors carry a machine-readable code"); + assert_eq!(context["error_code"], format!("openapi_upload.{code}")); +} + +#[tokio::test] +#[serial] +async fn preview_outcomes_do_not_mutate_operation_catalog() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_upload_outcomes"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + + let before = catalog_counts(®istry, &workspace_id).await; + let preview = service + .preview_openapi_import( + &workspace_id, + upload( + br#"{ + "openapi":"3.0.3", + "info":{"title":"Upload outcomes","version":"1"}, + "paths":{"/ready":{"get":{"responses":{"200":{"description":"ok"}}}}} + }"# + .to_vec(), + ), + ) + .await + .expect("valid OpenAPI document should return a preview"); + let candidates = preview + .preview + .groups + .iter() + .flat_map(|group| &group.operations) + .collect::>(); + assert_eq!(candidates.len(), 1); + assert_eq!(candidates[0].method, HttpMethod::Get); + assert_eq!(candidates[0].path, "/ready"); + assert_eq!(candidates[0].key, "GET /ready"); + assert_eq!(catalog_counts(®istry, &workspace_id).await, before); + + let before = catalog_counts(®istry, &workspace_id).await; + let error = service + .preview_openapi_import(&workspace_id, upload(Vec::new())) + .await + .expect_err("empty upload should be rejected"); + assert_upload_error(error, "validation", "empty_file"); + assert_eq!(catalog_counts(®istry, &workspace_id).await, before); + + let before = catalog_counts(®istry, &workspace_id).await; + let error = service + .preview_openapi_import(&workspace_id, upload(b"{".to_vec())) + .await + .expect_err("malformed OpenAPI document should be rejected"); + assert_upload_error(error, "validation", "invalid_document"); + assert_eq!(catalog_counts(®istry, &workspace_id).await, before); + + let before = catalog_counts(®istry, &workspace_id).await; + let error = service + .preview_openapi_import(&workspace_id, upload(vec![b' '; MAX_ARTIFACT_BYTES + 1])) + .await + .expect_err("oversized upload should be rejected"); + assert_upload_error(error, "payload_too_large", "file_too_large"); + assert_eq!(catalog_counts(®istry, &workspace_id).await, before); + + let before = catalog_counts(®istry, &workspace_id).await; + let error = service + .preview_openapi_import( + &workspace_id, + upload( + br#"{ + "openapi":"3.0.3", + "info":{"title":"No methods","version":"1"}, + "paths":{} + }"# + .to_vec(), + ), + ) + .await + .expect_err("document without supported methods should be rejected"); + assert_upload_error(error, "validation", "no_methods"); + assert_eq!(catalog_counts(®istry, &workspace_id).await, before); +} + +#[tokio::test] +#[serial] +async fn preview_profile_decode_failure_leaves_no_pending_job_or_source() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_upload_profile_decode_failure"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + insert_malformed_auth_profile(®istry, &workspace_id).await; + + service + .preview_openapi_import(&workspace_id, upload(PREVIEWABLE_DOCUMENT.to_vec())) + .await + .expect_err("malformed auth profile must fail preview enrichment"); + + assert_eq!( + durable_import_jobs(®istry, &workspace_id).await, + 0, + "profile lookup failure must not leave a durable import job" + ); + assert_eq!( + active_artifact_sources(®istry, &workspace_id).await, + 0, + "profile lookup failure must not leave attached sources" + ); +} + +#[tokio::test] +#[serial] +async fn preview_catalog_limit_failure_leaves_no_pending_job_or_source() { + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_upload_catalog_limit_failure"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + insert_catalog_overflow(®istry, &workspace_id).await; + + service + .preview_openapi_import(&workspace_id, upload(PREVIEWABLE_DOCUMENT.to_vec())) + .await + .expect_err("catalog over the import limit must fail preview enrichment"); + + assert_eq!( + durable_import_jobs(®istry, &workspace_id).await, + 0, + "catalog lookup failure must not leave a durable import job" + ); + assert_eq!( + active_artifact_sources(®istry, &workspace_id).await, + 0, + "catalog lookup failure must not leave attached sources" + ); +} diff --git a/apps/admin-api/tests/integration/openapi_import/v5_replay.rs b/apps/admin-api/tests/integration/openapi_import/v5_replay.rs new file mode 100644 index 0000000..f7538d4 --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_import/v5_replay.rs @@ -0,0 +1,93 @@ +use super::*; + +#[tokio::test] +#[serial] +async fn exact_v5_contract_replays_saved_preview_without_source_locations() { + use sha2::{Digest, Sha256}; + + fn remove_source_locations(value: &mut serde_json::Value) { + match value { + serde_json::Value::Object(object) => { + object.remove("source_location"); + for value in object.values_mut() { + remove_source_locations(value); + } + } + serde_json::Value::Array(values) => { + for value in values { + remove_source_locations(value); + } + } + _ => {} + } + } + + let registry = test_registry().await; + let service = test_service( + registry.clone(), + test_storage_root("openapi_import_v5_compatibility"), + test_auth_settings(), + test_secret_crypto(), + ); + let workspace_id = WorkspaceId::new("ws_default"); + let preview = service + .preview_openapi_import(&workspace_id, openapi_upload()) + .await + .unwrap(); + let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into(); + let mut payload = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap() + .preview_payload; + + // A preview persisted under v5 has the then-current projection and digest, + // without the source_location fields added by v6. + let persisted_preview = payload + .get_mut("preview") + .expect("newly saved job includes its preview"); + assert!(persisted_preview.to_string().contains("source_location")); + remove_source_locations(persisted_preview); + let digest = format!( + "{:x}", + Sha256::digest(serde_json::to_vec(persisted_preview).unwrap()) + ); + payload["normalization"]["normalizer_version"] = "normalized-ir-v5".into(); + payload["normalization"]["projection_version"] = "preview-v5".into(); + payload["normalization"]["config"]["normalizer_version"] = "normalized-ir-v5".into(); + payload["normalization"]["config"]["projection_version"] = "preview-v5".into(); + payload["preview_digest"] = digest.into(); + // If v5 is reparsed, this deliberately absent dependency makes the replay fail. + payload["dependency_snapshots"] = serde_json::json!([{ + "source_id": "src_openapi_dep_v5_must_not_be_read", + "digest": format!("sha256:{}", "0".repeat(64)), + "canonical_uri": "https://schemas.example.test/v5.yaml" + }]); + sqlx::query("update import_jobs set preview_payload = $1::jsonb where id = $2") + .bind(serde_json::to_string(&payload).unwrap()) + .bind(job_id.as_str()) + .execute(registry.pool()) + .await + .unwrap(); + + let result = service + .create_openapi_import( + &workspace_id, + &job_id, + OpenApiImportCreatePayload { + selected_operation_keys: vec!["GET /v2/latest".to_owned()], + server_url: Some("https://api.frankfurter.dev".to_owned()), + conflict_mode: "skip".to_owned(), + }, + ) + .await + .unwrap(); + assert_eq!(result.created.len(), 1); + let completed = registry + .get_import_job(&workspace_id, &job_id) + .await + .unwrap() + .unwrap(); + assert_eq!(completed.status, ImportJobStatus::Completed); +} diff --git a/apps/admin-api/tests/integration/openapi_source.rs b/apps/admin-api/tests/integration/openapi_source.rs index e0ef434..f250b53 100644 --- a/apps/admin-api/tests/integration/openapi_source.rs +++ b/apps/admin-api/tests/integration/openapi_source.rs @@ -12,6 +12,7 @@ use serial_test::serial; use time::{Duration, OffsetDateTime}; mod apply_failures; +mod body_limit; mod telemetry; const OPENAPI: &str = r#" @@ -512,7 +513,6 @@ async fn apply_rereads_the_scoped_verified_source_and_expiry_detaches_it() { &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /health".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, diff --git a/apps/admin-api/tests/integration/openapi_source/apply_failures.rs b/apps/admin-api/tests/integration/openapi_source/apply_failures.rs index b994662..589fe5a 100644 --- a/apps/admin-api/tests/integration/openapi_source/apply_failures.rs +++ b/apps/admin-api/tests/integration/openapi_source/apply_failures.rs @@ -245,7 +245,6 @@ async fn apply_health_operation( job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /health".to_owned()], - auth_mappings: vec![], server_url: None, conflict_mode: "skip".to_owned(), }, diff --git a/apps/admin-api/tests/integration/openapi_source/body_limit.rs b/apps/admin-api/tests/integration/openapi_source/body_limit.rs new file mode 100644 index 0000000..ffed82f --- /dev/null +++ b/apps/admin-api/tests/integration/openapi_source/body_limit.rs @@ -0,0 +1,142 @@ +use super::*; + +const DEFAULT_HTTP_BODY_BYTES: usize = 2 * 1024 * 1024; +const MAX_STORED_PLAN_BYTES: usize = 16 * 1024 * 1024; +const PATH_COUNT: usize = 49; +const PATH_LEN: usize = 3100; +const METHODS: [&str; 7] = ["get", "post", "put", "patch", "delete", "head", "options"]; +const OPERATION_COUNT: usize = PATH_COUNT * METHODS.len(); + +// Complete descriptions and schemas avoid multiplying quality findings for +// long paths. The test must pass the stored-plan cap before exercising HTTP. +fn long_path_document_and_keys() -> (String, Vec) { + let mut paths = serde_json::Map::new(); + let mut keys = Vec::new(); + for index in 0..PATH_COUNT { + let path = format!("/{index:04}{}", "a".repeat(PATH_LEN - 5)); + let mut item = serde_json::Map::new(); + item.insert( + "parameters".to_owned(), + serde_json::json!([{ + "name": "filter", "in": "query", "description": "Record filter", + "schema": { "type": "string" } + }]), + ); + for (method_index, method) in METHODS.iter().enumerate() { + keys.push(format!("{} {path}", method.to_uppercase())); + item.insert( + (*method).to_owned(), + serde_json::json!({ + "operationId": format!("read_record_{index}_{method_index}"), + "summary": "Read record", + "description": "Returns a matching record for the requested filter.", + "responses": { "200": { + "description": "Matched record", + "content": { "application/json": { "schema": { + "type": "object", "properties": { "result": { "type": "string" } } + } } } + } } + }), + ); + } + paths.insert(path, Value::Object(item)); + } + ( + serde_json::json!({ + "openapi": "3.0.3", "info": { "title": "Long path keys", "version": "1" }, + "servers": [{ "url": "https://api.example.test" }], "paths": paths + }) + .to_string(), + keys, + ) +} + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn plan_http_body_limit_accepts_valid_large_confirmation() { + let (document, keys) = long_path_document_and_keys(); + assert!(document.len() <= MAX_ARTIFACT_BYTES); + assert_eq!(keys.len(), OPERATION_COUNT); + assert!(keys.iter().all(|key| key.len() <= 4096)); + println!( + "B1_FIXTURE document_bytes={} operations={}", + document.len(), + keys.len() + ); + + let registry = test_registry().await; + let app = build_test_app( + registry.clone(), + test_storage_root("openapi_plan_body_limit"), + ); + let server = spawn_admin_api(app).await; + let client = authorized_client(&server).await; + let response = client + .post(format!("{server}/imports/openapi/preview")) + .multipart(Form::new().part( + "file", + file_part(document.as_bytes(), "openapi.json", "application/json"), + )) + .send() + .await + .unwrap(); + let status = response.status(); + let preview: Value = response.json().await.unwrap(); + assert_eq!(status, reqwest::StatusCode::OK, "{preview}"); + let job_id = preview["job_id"].as_str().unwrap(); + + // The smaller control proves actual persistence. Explicit create decisions + // add no semantic change, but repeat the keys in a larger HTTP request. + for explicit_decisions in [false, true] { + let decisions: Vec = if explicit_decisions { + keys.iter() + .map(|key| { + serde_json::json!({ + "operation_key": key, "action": "create" + }) + }) + .collect() + } else { + Vec::new() + }; + let payload = serde_json::json!({ + "selection_mode": "full", "selected_operation_keys": keys, "decisions": decisions + }); + let request_bytes = serde_json::to_vec(&payload).unwrap().len(); + assert_eq!(request_bytes > DEFAULT_HTTP_BODY_BYTES, explicit_decisions); + let response = client + .post(format!("{server}/imports/openapi/{job_id}/plans")) + .json(&payload) + .send() + .await + .unwrap(); + let status = response.status(); + let body = response.text().await.unwrap(); + println!( + "B1_HTTP explicit_decisions={explicit_decisions} request_bytes={request_bytes} status={status}" + ); + assert_eq!( + status, + reqwest::StatusCode::OK, + "valid plan rejected: {}", + body.chars().take(2000).collect::() + ); + let plan: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(plan["counts"]["selected"], OPERATION_COUNT); + assert_eq!(plan["counts"]["actionable"], OPERATION_COUNT); + assert_eq!(plan["counts"]["blocked"], 0); + assert_eq!(plan["counts"]["excluded"], 0); + let plan_id: crank_registry::ImportPlanId = plan["plan_reference"].as_str().unwrap().into(); + let stored = registry + .get_import_plan(&WorkspaceId::new("ws_default"), &plan_id) + .await + .unwrap() + .unwrap(); + let stored_bytes = serde_json::to_vec(&stored.payload).unwrap().len(); + println!( + "B1_PERSISTED explicit_decisions={explicit_decisions} stored_bytes={stored_bytes}" + ); + assert!(stored_bytes <= MAX_STORED_PLAN_BYTES); + assert_eq!(stored.payload["counts"]["selected"], OPERATION_COUNT); + } +} diff --git a/apps/admin-api/tests/migration_command.rs b/apps/admin-api/tests/migration_command.rs index 91e2ca3..5a65de5 100644 --- a/apps/admin-api/tests/migration_command.rs +++ b/apps/admin-api/tests/migration_command.rs @@ -40,7 +40,20 @@ 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(), 15); + let target = command(&["plan", "--target-version"], None); + assert!( + target.status.success(), + "{}", + String::from_utf8_lossy(&target.stderr) + ); + let last_version = plan["sequence"].as_array().unwrap().last().unwrap()["version"] + .as_i64() + .unwrap(); + assert_eq!( + String::from_utf8(target.stdout).unwrap(), + format!("{last_version}\n") + ); let checked = command(&["plan", "--check"], None); assert!( @@ -50,6 +63,15 @@ fn plan_is_deterministic_and_committed_contract_is_current() { ); } +#[test] +fn invalid_plan_option_is_rejected_without_success_output() { + let output = command(&["plan", "--not-an-option"], None); + assert!(!output.status.success()); + assert!(output.stdout.is_empty()); + let diagnostic: serde_json::Value = serde_json::from_slice(&output.stderr).unwrap(); + assert_eq!(diagnostic["code"], "invalid_command"); +} + #[test] fn invalid_command_is_bounded_and_does_not_echo_arguments() { let canary = "secret-command-canary"; @@ -82,7 +104,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"], 15); } #[tokio::test] diff --git a/apps/ui/css/onboarding.css b/apps/ui/css/onboarding.css index a4f8df1..4aabc96 100644 --- a/apps/ui/css/onboarding.css +++ b/apps/ui/css/onboarding.css @@ -64,6 +64,15 @@ .onboarding-first-call-evidence dt { color: var(--text-muted, #8b949e); } .onboarding-first-call-evidence dd { margin: 0; font-family: var(--font-mono, monospace); overflow-wrap: anywhere; } .onboarding-deep-link-stale { position: fixed; left: 50%; top: 76px; z-index: 810; width: min(520px, calc(100vw - 28px)); transform: translateX(-50%); padding: 14px; border: 1px solid var(--amber, #d29922); border-radius: 10px; background: var(--surface-raised, #161b22); color: var(--text-primary, #f0f6fc); box-shadow: 0 12px 36px rgba(0, 0, 0, .35); } +.onboarding-openapi-path { margin-top: 14px; padding: 12px; border: 1px dashed var(--border-color, #30363d); border-radius: 9px; } +.onboarding-openapi-hint { margin: 0 0 8px; color: var(--text-muted, #8b949e); font-size: 12px; } +.onboarding-import-report { margin-top: 12px; padding: 12px; border: 1px solid var(--border-color, #30363d); border-radius: 9px; } +.onboarding-import-report h3 { margin: 0 0 8px; font-size: 14px; } +.onboarding-import-report-list { display: grid; gap: 8px; margin: 0; padding-left: 20px; } +.onboarding-import-report-item { display: grid; gap: 4px; overflow-wrap: anywhere; font-size: 12px; } +.onboarding-import-drift, .onboarding-import-error { color: var(--amber, #d29922); font-size: 12px; } +.onboarding-import-selected { color: var(--success, #3fb950); font-size: 12px; } +.onboarding-return-context { display: none; } .onboarding-footer { display: flex; justify-content: space-between; gap: 8px; margin-top: 12px; } .onboarding-link-button { border: 0; background: transparent; color: var(--text-muted, #8b949e); text-decoration: underline; cursor: pointer; font: inherit; font-size: 12px; } diff --git a/apps/ui/css/openapi-import.css b/apps/ui/css/openapi-import.css index bbaf990..58e3868 100644 --- a/apps/ui/css/openapi-import.css +++ b/apps/ui/css/openapi-import.css @@ -160,6 +160,124 @@ 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-findings summary { + cursor: pointer; + color: var(--text-secondary); + font-weight: 700; +} + +.openapi-import-findings-page { + display: grid; + gap: 4px; + padding-top: 6px; +} + +.openapi-import-findings-pagination { + display: flex; + align-items: center; + justify-content: flex-start; + gap: 8px; + padding-top: 6px; + color: var(--text-secondary); +} + +.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 +303,6 @@ } #openapi-import-server, -#openapi-import-conflict-mode, #openapi-import-search, #openapi-import-method-filter, .openapi-import-server-custom { @@ -238,6 +355,30 @@ gap: 14px; } +.openapi-import-partial-toggle { + display: flex; + align-items: center; + gap: 9px; + width: fit-content; + color: var(--text-primary); + font-size: 13px; + font-weight: 700; +} + +.openapi-import-pagination { + display: flex; + align-items: center; + justify-content: center; + gap: 12px; + padding: 4px 0; + color: var(--text-secondary); + font-size: 13px; +} + +.openapi-import-pagination[hidden] { + display: none; +} + .openapi-import-group-header { display: flex; align-items: center; @@ -266,12 +407,109 @@ } .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-security-status { + padding: 6px 0 6px 28px; + color: var(--text-secondary); + font-size: 13px; + font-weight: 700; +} + +.openapi-import-decision-controls, +.openapi-import-security-controls { + display: flex; + flex-wrap: wrap; + gap: 10px 14px; + padding-left: 28px; +} + +.openapi-import-decision-controls label, +.openapi-import-security-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, +.openapi-import-security-controls select, +.openapi-import-security-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-security { + display: grid; + gap: 4px; +} + +.openapi-import-security-op-status { + padding-left: 28px; + color: var(--text-secondary); + font-size: 12px; + font-weight: 700; +} + +.openapi-import-security-op-status[data-status="blocked"] { + color: var(--red); +} + +.openapi-import-security-op-status[data-status="resolved"] { + color: var(--green, var(--text-secondary)); +} + +.openapi-import-security-findings { + align-self: center; + color: var(--amber, var(--text-secondary)); + font-size: 12px; +} + +.openapi-import-security-guidance { + color: var(--text-secondary); + font-size: 12px; + font-weight: 400; + max-width: 360px; +} + +.openapi-import-plan-blocked-help { + margin: 0; + color: var(--red); + font-weight: 700; +} + +.openapi-import-plan-version, +.openapi-import-plan-revalidation { + color: var(--text-secondary); + font-size: 12px; + overflow-wrap: anywhere; +} + +.openapi-import-plan-stale { + padding: 10px 12px; + border: 1px solid var(--red); + border-radius: 10px; + color: var(--red); + font-weight: 700; } .openapi-import-operation:last-child { @@ -288,6 +526,25 @@ font-size: 12px; } +.openapi-import-source-location { + color: var(--text-muted); + font-size: 11px; + overflow-wrap: anywhere; +} + +.openapi-import-conflict { + display: flex; + flex-wrap: wrap; + align-items: baseline; + gap: 6px; + color: var(--amber); + font-size: 12px; +} + +.openapi-import-conflict-proposal { + color: var(--text-primary); +} + .openapi-import-method { padding: 4px 8px; border-radius: 999px; @@ -297,6 +554,26 @@ font-weight: 800; } +.openapi-import-operation-readiness { + justify-self: start; + padding: 4px 8px; + border-radius: 999px; + background: var(--surface-muted, #21262d); + color: var(--text-secondary); + font-size: 11px; + font-weight: 800; +} + +.openapi-import-operation-readiness-actionable { + background: var(--green-glow, rgba(63, 185, 80, 0.16)); + color: var(--green); +} + +.openapi-import-operation-readiness-blocked { + background: var(--red-glow, rgba(248, 81, 73, 0.16)); + color: var(--red); +} + .openapi-import-findings { grid-column: 2 / -1; display: grid; @@ -304,6 +581,10 @@ font-size: 12px; } +.openapi-import-operation .openapi-import-findings { + grid-column: auto; +} + .openapi-import-finding { display: inline-flex; align-items: baseline; @@ -461,6 +742,17 @@ font-size: 13px; } +.openapi-import-result-skipped-page { + margin-top: 6px; +} + +.openapi-import-result-skipped-page ul { + max-height: 320px; + overflow: auto; + margin: 0; + padding-left: 22px; +} + .openapi-import-created-list { display: grid; gap: 6px; @@ -475,6 +767,68 @@ text-decoration: none; } +.openapi-import-blockers-summary { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; + margin-top: 12px; + padding: 10px 12px; + border: 1px solid var(--danger, #f85149); + border-radius: 8px; + color: var(--danger, #f85149); + font-size: 12px; +} + +.openapi-import-blockers-summary[hidden] { + display: none; +} + +.openapi-import-blockers-count { + font-weight: 700; +} + +.openapi-import-blocker-code { + padding: 2px 6px; + border-radius: 5px; + background: rgba(248, 81, 73, .12); + font-family: var(--font-mono, monospace); +} +.onboarding-import-draft-choice { + margin-top: 14px; + padding: 12px; + border: 1px solid var(--accent, #58a6ff); + border-radius: 9px; +} + +.onboarding-import-draft-choice-title { + display: block; + margin-bottom: 8px; +} + +.onboarding-import-draft-choice-list { + display: grid; + gap: 8px; +} + +.onboarding-import-draft-option-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + padding: 8px 0; + border-top: 1px solid var(--border-color, #30363d); + overflow-wrap: anywhere; +} + +.onboarding-import-drift { + color: var(--amber, #d29922); +} + +.onboarding-import-draft-selected { + color: var(--success, #3fb950); +} + @media (max-width: 720px) { .openapi-import-toolbar { grid-template-columns: 1fr; @@ -484,7 +838,7 @@ justify-content: flex-start; } - .openapi-import-operation { + .openapi-import-operation-selection { grid-template-columns: auto 1fr; } diff --git a/apps/ui/html/wizard/index.html b/apps/ui/html/wizard/index.html index 5b553f3..2d7bbae 100644 --- a/apps/ui/html/wizard/index.html +++ b/apps/ui/html/wizard/index.html @@ -178,7 +178,19 @@ -
+
+ +
+
diff --git a/apps/ui/html/wizard/step5.html b/apps/ui/html/wizard/step5.html index 7db9ebc..f25531f 100644 --- a/apps/ui/html/wizard/step5.html +++ b/apps/ui/html/wizard/step5.html @@ -131,17 +131,6 @@ tls:
- -
diff --git a/apps/ui/index.html b/apps/ui/index.html index a0af1ba..50d06da 100644 --- a/apps/ui/index.html +++ b/apps/ui/index.html @@ -417,13 +417,6 @@
-
- - -
@@ -440,13 +433,43 @@
+
+ - + + diff --git a/apps/ui/js/api.js b/apps/ui/js/api.js index e93bb28..aecbaef 100644 --- a/apps/ui/js/api.js +++ b/apps/ui/js/api.js @@ -328,6 +328,12 @@ expected_revision: expectedRevision, }); }, + registerOnboardingImportContext: function(workspaceId, payload) { + return post('/workspaces/' + encodeURIComponent(workspaceId) + '/onboarding/import-context', payload); + }, + selectOnboardingImportDraft: function(workspaceId, payload) { + return post('/workspaces/' + encodeURIComponent(workspaceId) + '/onboarding/select-draft', payload); + }, updateWorkspace: function(workspaceId, payload) { return patch('/workspaces/' + encodeURIComponent(workspaceId), payload); }, @@ -436,6 +442,35 @@ 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, + }); + }, + revalidateOpenApiImportPlan: function(workspaceId, planReference, options) { + return request(API_BASE + '/workspaces/' + encodeURIComponent(workspaceId) + '/imports/openapi/plans/' + encodeURIComponent(planReference), { + method: 'GET', + headers: headers({ 'Accept-Language': localStorage.getItem('crank_lang') === 'ru' ? 'ru' : 'en' }), + 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..4e8fa89 100644 --- a/apps/ui/js/i18n.js +++ b/apps/ui/js/i18n.js @@ -611,6 +611,27 @@ 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.', + 'openapi.security.alternative_label': 'Security alternative', + 'openapi.security.alternative_placeholder': 'Choose a security alternative', + 'openapi.security.no_auth': 'No authentication', + 'openapi.security.unsupported': 'unsupported', + 'openapi.security.profile_label': 'Auth Profile', + 'openapi.security.profile_placeholder': 'Choose an Auth Profile', + 'openapi.security.status_no_auth': 'No authentication', + 'openapi.security.status_required': 'Authentication required', + 'openapi.security.status_unresolved': 'Unresolved', + 'openapi.security.status_unverified': 'Unverified', + 'openapi.security.status_resolved': 'Resolved', + 'openapi.security.status_blocked': 'Blocked', + 'openapi.security.empty_profiles': 'No compatible Auth Profiles for the selected alternative. Choose another alternative or create a plan to revalidate.', + 'openapi.finding.unsupported_security_scheme': 'Some security requirements could not be imported. Choose a supported alternative or remove the unsupported requirement.', + 'openapi.finding.unsupported_security_and_requirement': 'This security requirement cannot be satisfied with a supported scheme or an available Auth Profile.', + 'openapi.error.security_with_skip': 'A skipped operation cannot be bound to an Auth Profile or a security alternative.', + 'openapi.error.auth_unavailable': 'The selected Auth Profile is unavailable or its required Secret is missing. Choose another compatible profile and create a new plan.', + 'openapi.error.auth_incompatible': 'The Auth Profile scheme does not match the selected security alternative. Choose a compatible alternative or profile and create a new plan.', + 'openapi.error.auth_profile_required': 'This operation requires an Auth Profile. Choose a compatible profile and create a new plan.', + 'openapi.error.security_alternative_required': 'The Auth Profile matches multiple security alternatives. Select the exact alternative and create a new plan.', '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 +1679,27 @@ var TRANSLATIONS = { 'wizard.step2.auth_mode.none': 'Без авторизации', 'wizard.step2.auth_mode.existing': 'Использовать существующий профиль авторизации', 'wizard.step2.auth_mode.create': 'Создать профиль авторизации сейчас', + 'openapi.error.plan_too_large': 'Выберите не более 512 операций и оставьте не более 512 исключённых операций.', + 'openapi.security.alternative_label': 'Вариант безопасности', + 'openapi.security.alternative_placeholder': 'Выберите вариант безопасности', + 'openapi.security.no_auth': 'Без аутентификации', + 'openapi.security.unsupported': 'не поддерживается', + 'openapi.security.profile_label': 'Профиль авторизации', + 'openapi.security.profile_placeholder': 'Выберите профиль авторизации', + 'openapi.security.status_no_auth': 'Без аутентификации', + 'openapi.security.status_required': 'Требуется аутентификация', + 'openapi.security.status_unresolved': 'Не разрешено', + 'openapi.security.status_unverified': 'Не проверено', + 'openapi.security.status_resolved': 'Разрешено', + 'openapi.security.status_blocked': 'Заблокировано', + 'openapi.security.empty_profiles': 'Для выбранного варианта нет совместимых профилей авторизации. Выберите другой вариант или создайте план для повторной проверки.', + 'openapi.finding.unsupported_security_scheme': 'Некоторые security requirements нельзя импортировать. Выберите поддерживаемый вариант или уберите неподдерживаемое требование.', + 'openapi.finding.unsupported_security_and_requirement': 'Это security requirement невозможно выполнить со схемой, поддержанной Crank, или доступным профилем авторизации.', + 'openapi.error.security_with_skip': 'Пропускаемая операция не может быть связана с профилем авторизации или вариантом безопасности.', + 'openapi.error.auth_unavailable': 'Выбранный профиль авторизации недоступен, либо отсутствует его обязательный Secret. Выберите другой совместимый профиль и создайте новый план.', + 'openapi.error.auth_incompatible': 'Схема профиля авторизации не совпадает с выбранным вариантом безопасности. Выберите совместимый вариант или профиль и создайте новый план.', + 'openapi.error.auth_profile_required': 'Для этой операции требуется профиль авторизации. Выберите совместимый профиль и создайте новый план.', + 'openapi.error.security_alternative_required': 'Профиль авторизации подходит к нескольким вариантам безопасности. Выберите точный вариант и создайте новый план.', 'wizard.step2.auth_profile': 'Профиль авторизации', 'wizard.step2.auth_profile_hint': 'Выбранный профиль применяется автоматически при каждом вызове инструмента.', 'wizard.step2.auth_profile_empty': 'Профилей авторизации пока нет. Создайте профиль ниже.', @@ -2092,12 +2134,81 @@ Object.assign(TRANSLATIONS.en, { 'openapi.method.all': 'All methods', 'openapi.select_visible': 'Select visible', 'openapi.clear_visible': 'Clear visible', + 'openapi.partial_mode': 'Enable partial import', + 'openapi.pagination.previous': 'Previous', + 'openapi.pagination.next': 'Next', + 'openapi.pagination.label': 'Preview pages', + 'openapi.pagination.plan_label': 'Import plan candidates', + 'openapi.pagination.result_label': 'Import results', + 'openapi.findings.summary': 'Findings: {count}', + 'openapi.pagination.status': 'Page {page} of {pages} · {total} matching operations', '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.reopen': 'Reopen and check current plan', + 'openapi.plan.version': 'Review {version} · reference {reference} · digest {digest}', + 'openapi.plan.revalidation_pending': 'Checking this saved plan…', + 'openapi.plan.revalidation_current': 'The saved plan is current.', + 'openapi.plan.revalidation_stale': 'The saved plan is stale.', + 'openapi.plan.revalidation_error': 'The saved plan could not be verified.', + 'openapi.plan.revalidation_guidance': 'Return to the preview and create a new plan before applying.', + '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}', - 'openapi.group.select': 'Select group', + 'openapi.group.select': 'Select group on this page', 'openapi.operation.fields': 'inputs: {input} · outputs: {output}', + 'openapi.operation.readiness_actionable': 'Actionable', + 'openapi.operation.readiness_blocked': 'Blocked', + 'openapi.operation.readiness_unresolved': 'Authentication required', 'openapi.mapping.path': 'Path', 'openapi.mapping.query': 'Query', 'openapi.mapping.header': 'Header', @@ -2106,6 +2217,8 @@ Object.assign(TRANSLATIONS.en, { 'openapi.source.summary': '{name} · {size} · methods: {count}', 'openapi.status.file_ready': 'File is ready for preview.', 'openapi.status.previewing': 'Uploading and parsing the document…', + 'openapi.status.uploading': 'Uploading the document…', + 'openapi.status.parsing': 'Parsing the OpenAPI document…', 'openapi.status.preview_ready': 'Document is ready. Select methods to import.', 'openapi.status.creating': 'Creating drafts…', 'openapi.status.created': 'Created: {created}; skipped: {skipped}.', @@ -2150,6 +2263,21 @@ Object.assign(TRANSLATIONS.en, { 'openapi.result.fix': 'Fix in wizard', 'openapi.result.open': 'Open in wizard', 'openapi.result.skipped': 'Skipped', + 'openapi.result.skipped_count': 'Skipped: {count} candidates', + 'openapi.blockers.summary': 'Blocked operations: {count}', + 'onboarding.openapi_path': 'Start from an OpenAPI document', + 'onboarding.openapi_hint': 'Use the canonical OpenAPI import: preview, review the exact plan, then apply it atomically.', + 'onboarding.import.choose_draft': 'Choose one Draft to continue with test and publish', + 'onboarding.import.report_title': 'OpenAPI import report and Draft choices', + 'onboarding.import.continue': 'Continue with this Draft', + 'onboarding.import.selected': 'Selected for onboarding', + 'onboarding.import.drift': 'The selected Draft changed or is unavailable. Review the report and choose an available Draft again.', + 'onboarding.import.registration_pending': 'Import report is being registered. Refresh onboarding before selecting.', + 'onboarding.import.registration_failed': 'The import was applied, but its onboarding context could not be confirmed. Refresh to recover the report before continuing.', + 'onboarding.import.selection_failed': 'This Draft could not be selected. Refresh the report and review the current Draft choices.', + 'onboarding.import.conflict_reload': 'The onboarding state changed. The latest report has been loaded; review it before trying again.', + 'onboarding.import.retry_registration': 'Retry report recovery', + 'onboarding.import.view_report': 'View the full import report', }); @@ -2208,12 +2336,81 @@ Object.assign(TRANSLATIONS.ru, { 'openapi.method.all': 'Все методы', 'openapi.select_visible': 'Выбрать видимые', 'openapi.clear_visible': 'Снять видимые', + 'openapi.partial_mode': 'Включить частичный импорт', + 'openapi.pagination.previous': 'Назад', + 'openapi.pagination.next': 'Далее', + 'openapi.pagination.label': 'Страницы предпросмотра', + 'openapi.pagination.plan_label': 'Кандидаты плана импорта', + 'openapi.pagination.result_label': 'Результаты импорта', + 'openapi.findings.summary': 'Замечаний: {count}', + 'openapi.pagination.status': 'Страница {page} из {pages} · операций по фильтру: {total}', 'openapi.create': 'Создать черновики', + 'openapi.plan.create': 'Проверить план импорта', + 'openapi.plan.confirm_title': 'Подтвердите план импорта', + 'openapi.plan.confirm_intro': 'Перед применением проверьте точный план сервера.', + 'openapi.plan.reopen': 'Повторно открыть и проверить план', + 'openapi.plan.version': 'Проверка {version} · ссылка {reference} · digest {digest}', + 'openapi.plan.revalidation_pending': 'Проверяем сохранённый план…', + 'openapi.plan.revalidation_current': 'Сохранённый план актуален.', + 'openapi.plan.revalidation_stale': 'Сохранённый план устарел.', + 'openapi.plan.revalidation_error': 'Не удалось проверить сохранённый план.', + 'openapi.plan.revalidation_guidance': 'Вернитесь к предпросмотру и создайте новый план перед применением.', + '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}', - 'openapi.group.select': 'Выбрать группу', + 'openapi.group.select': 'Выбрать группу на этой странице', 'openapi.operation.fields': 'входов: {input} · выходов: {output}', + 'openapi.operation.readiness_actionable': 'Можно импортировать', + 'openapi.operation.readiness_blocked': 'Заблокировано', + 'openapi.operation.readiness_unresolved': 'Требуется авторизация', 'openapi.mapping.path': 'Path', 'openapi.mapping.query': 'Query', 'openapi.mapping.header': 'Header', @@ -2222,6 +2419,8 @@ Object.assign(TRANSLATIONS.ru, { 'openapi.source.summary': '{name} · {size} · методов: {count}', 'openapi.status.file_ready': 'Файл готов к разбору.', 'openapi.status.previewing': 'Загружаю и разбираю документ…', + 'openapi.status.uploading': 'Загружаю документ…', + 'openapi.status.parsing': 'Разбираю документ OpenAPI…', 'openapi.status.preview_ready': 'Документ готов. Выберите методы для импорта.', 'openapi.status.creating': 'Создаю черновики…', 'openapi.status.created': 'Создано: {created}; пропущено: {skipped}.', @@ -2266,7 +2465,21 @@ Object.assign(TRANSLATIONS.ru, { 'openapi.result.fix': 'Исправить в мастере', 'openapi.result.open': 'Открыть в мастере', 'openapi.result.skipped': 'Пропущено', - + 'openapi.result.skipped_count': 'Пропущено кандидатов: {count}', + 'openapi.blockers.summary': 'Заблокированные операции: {count}', + 'onboarding.openapi_path': 'Начать с документа OpenAPI', + 'onboarding.openapi_hint': 'Используйте канонический импорт OpenAPI: предпросмотр, проверка точного плана и атомарное применение.', + 'onboarding.import.choose_draft': 'Выберите один черновик для продолжения тестирования и публикации', + 'onboarding.import.report_title': 'Отчёт OpenAPI и выбор черновика', + 'onboarding.import.continue': 'Продолжить с этим черновиком', + 'onboarding.import.selected': 'Выбран для onboarding', + 'onboarding.import.drift': 'Выбранный черновик изменился или недоступен. Проверьте отчёт и явно выберите доступный черновик.', + 'onboarding.import.registration_pending': 'Отчёт импорта регистрируется. Обновите onboarding перед выбором.', + 'onboarding.import.registration_failed': 'Импорт применён, но его контекст onboarding не подтверждён. Обновите страницу, чтобы восстановить отчёт.', + 'onboarding.import.selection_failed': 'Не удалось выбрать этот черновик. Обновите отчёт и проверьте актуальные варианты.', + 'onboarding.import.conflict_reload': 'Состояние onboarding изменилось. Загружен актуальный отчёт; проверьте его перед новой попыткой.', + 'onboarding.import.retry_registration': 'Повторить восстановление отчёта', + 'onboarding.import.view_report': 'Открыть полный отчёт импорта', }); diff --git a/apps/ui/js/onboarding.js b/apps/ui/js/onboarding.js index 6fb9ce7..bcb54e5 100644 --- a/apps/ui/js/onboarding.js +++ b/apps/ui/js/onboarding.js @@ -12,6 +12,7 @@ refreshPromise: null, refreshQueued: false, startedRequested: false, + importError: null, }; var channel = null; var trigger = null; @@ -79,7 +80,7 @@ function actionLabel(id) { return tKey('onboarding.action.' + id); } function deepLink(step) { var snapshot = state.snapshot || {}; - var returnTo = encodeURIComponent(window.location.pathname + window.location.search); + var returnTo = encodeURIComponent(boundedReturnContext()); if (step.id === 'operation') return '/wizard/?onboarding=1&return=' + returnTo; if (step.id === 'test' || step.id === 'publish_operation') { if (!snapshot.operation_id) return '/wizard/?onboarding=1&return=' + returnTo; @@ -100,6 +101,220 @@ return '/logs?agent_id=' + encodeURIComponent(snapshot.agent_id || ''); } function navigate(step) { window.location.href = deepLink(step); } + function safeReturnContext(raw) { + if (typeof raw !== 'string' || raw.charAt(0) !== '/' || raw.indexOf('//') === 0 + || /[\u0000-\u001f\\]/.test(raw) || raw.length > 2048) return null; + var url; + try { url = new URL(raw, window.location.origin); } catch (_error) { return null; } + if (url.origin !== window.location.origin) return null; + var allowed = { + '/': [], + '/agents': ['onboarding', 'action', 'operationId', 'operationVersion'], + '/api-keys': ['onboarding', 'action', 'agentId', 'agentRevision'], + '/logs': ['log_id', 'agent_id', 'operation_id'], + '/secrets': [], + '/settings': [], + '/usage': [], + '/workspace-setup': [], + '/wizard/': ['mode', 'operationId', 'step', 'onboarding'], + }; + var keys = allowed[url.pathname]; + if (!keys) return null; + var safe = new URLSearchParams(); + url.searchParams.forEach(function(value, key) { + if (keys.indexOf(key) < 0) return; + if (key === 'onboarding' && value === '1') safe.set(key, value); + else if (key === 'action' && value === 'create') safe.set(key, value); + else if (key === 'mode' && (value === 'create' || value === 'edit')) safe.set(key, value); + else if ((key === 'operationVersion' || key === 'agentRevision' || key === 'step') + && /^[0-9]{1,16}$/.test(value)) safe.set(key, value); + else if (/^(?:operationId|agentId|log_id|agent_id|operation_id)$/.test(key) + && /^[A-Za-z0-9_-]{1,128}$/.test(value)) safe.set(key, value); + }); + var context = url.pathname + (safe.size ? '?' + safe.toString() : ''); + return context.length <= 256 ? context : url.pathname; + } + function boundedReturnContext() { + return safeReturnContext(window.location.pathname + window.location.search) || '/'; + } + function returnContextFromQuery() { + var candidate = new URLSearchParams(window.location.search).get('return'); + return safeReturnContext(candidate) || boundedReturnContext(); + } + function openOpenApiPath() { + if (!window.CrankOpenApiImport || typeof window.CrankOpenApiImport.open !== 'function') { + window.location.href = '/?onboarding_openapi=1&return=' + encodeURIComponent(boundedReturnContext()); + return; + } + window.CrankOpenApiImport.open({ + workspaceId: state.workspaceId, + onboarding: true, + returnContext: returnContextFromQuery(), + importContext: state.snapshot && state.snapshot.import_context, + onImported: function(identity) { return registerImportedApplication(identity); }, + onSelectDraft: function(operationId, version, identity) { return selectImportedDraft(operationId, version, identity); }, + }); + } + async function registerImportedApplication(identity) { + var workspaceId = state.workspaceId; + var epoch = state.epoch; + if (!identity || !identity.applicationIdempotencyKey || !identity.planDigest + || !window.CrankApi || typeof window.CrankApi.registerOnboardingImportContext !== 'function') { + throw new Error(tKey('onboarding.import.registration_failed')); + } + try { + var authoritative = await window.CrankApi.getOnboarding(workspaceId); + if (epoch !== state.epoch || workspaceId !== state.workspaceId || workspaceId !== (currentWorkspace() && currentWorkspace().id)) { + throw new Error(tKey('onboarding.import.registration_failed')); + } + state.snapshot = authoritative; + render(); + var response = await window.CrankApi.registerOnboardingImportContext(workspaceId, { + application_idempotency_key: identity.applicationIdempotencyKey, + plan_digest: identity.planDigest, + expected_revision: Number(authoritative && authoritative.revision || 0), + }); + if (epoch !== state.epoch || workspaceId !== state.workspaceId || workspaceId !== (currentWorkspace() && currentWorkspace().id)) { + throw new Error(tKey('onboarding.import.registration_failed')); + } + var current = response && response.onboarding ? response.onboarding : response; + if (!current || !current.import_context) throw new Error(tKey('onboarding.import.registration_failed')); + state.snapshot = current; + render(); + return current; + } catch (error) { + if (epoch === state.epoch && workspaceId === state.workspaceId) { + await refresh(); + var recovered = state.snapshot && state.snapshot.import_context; + if (recovered + && recovered.application_idempotency_key === identity.applicationIdempotencyKey + && recovered.plan_digest === identity.planDigest) return state.snapshot; + } + throw error; + } + } + async function selectImportedDraft(operationId, version, identity) { + if (!operationId || !window.CrankApi || typeof window.CrankApi.selectOnboardingImportDraft !== 'function') return null; + var workspaceId = state.workspaceId; + var epoch = state.epoch; + var context = state.snapshot && state.snapshot.import_context; + var applicationKey = identity && identity.applicationIdempotencyKey || (context && context.application_idempotency_key); + var digest = identity && identity.planDigest || (context && context.plan_digest); + if (!applicationKey || !digest || !Number.isInteger(version) || version < 1) { + throw new Error(tKey('onboarding.import.selection_failed')); + } + try { + var response = await window.CrankApi.selectOnboardingImportDraft(workspaceId, { + application_idempotency_key: applicationKey, + plan_digest: digest, + operation_id: operationId, + operation_version: version, + expected_revision: Number(state.snapshot && state.snapshot.revision || 0), + }); + var current = response && response.onboarding ? response.onboarding : response; + if (epoch !== state.epoch || workspaceId !== state.workspaceId || workspaceId !== (currentWorkspace() && currentWorkspace().id)) { + throw new Error(tKey('onboarding.import.selection_failed')); + } + if (!current || !current.import_context + || current.import_context.selected_operation_id !== operationId + || Number(current.import_context.selected_operation_version) !== version) { + throw new Error(tKey('onboarding.import.selection_failed')); + } + if (current && typeof current === 'object' && 'revision' in current) { + state.snapshot = current; + render(); + } + return response; + } catch (error) { + if (error && error.status === 409 && epoch === state.epoch && workspaceId === state.workspaceId) { + await refresh(); + } + throw error; + } + } + function renderOpenApiPath() { + var section = document.createElement('div'); + section.className = 'onboarding-openapi-path'; + var hint = document.createElement('p'); + hint.className = 'onboarding-openapi-hint'; + hint.textContent = tKey('onboarding.openapi_hint'); + var openButton = button(tKey('onboarding.openapi_path'), 'onboarding-openapi-path', openOpenApiPath, 'onboarding-step-action'); + section.appendChild(hint); + section.appendChild(openButton); + return section; + } + async function continueWithImportedDraft(choice) { + var context = state.snapshot && state.snapshot.import_context; + if (!context || !choice || choice.selectable !== true) return; + state.importError = null; + render(); + try { + await selectImportedDraft(choice.operation_id, choice.version, null); + window.location.href = '/wizard/?mode=edit&operationId=' + encodeURIComponent(choice.operation_id) + + '&operationVersion=' + encodeURIComponent(choice.version) + '&onboarding=1' + + '&return=' + encodeURIComponent(boundedReturnContext()); + } catch (_error) { + state.importError = tKey('onboarding.import.selection_failed'); + render(); + } + } + function renderImportContext() { + var context = state.snapshot && state.snapshot.import_context; + if (!context) return null; + var section = document.createElement('section'); + section.className = 'onboarding-import-report'; + section.dataset.testid = 'onboarding-import-context'; + var title = document.createElement('h3'); + title.textContent = tKey('onboarding.import.report_title'); + section.appendChild(title); + if (context.drift) { + var drift = document.createElement('p'); + drift.className = 'onboarding-import-drift'; + drift.setAttribute('role', 'alert'); + drift.textContent = tKey('onboarding.import.drift'); + section.appendChild(drift); + } + if (state.importError) { + var error = document.createElement('p'); + error.className = 'onboarding-import-error'; + error.setAttribute('role', 'alert'); + error.textContent = state.importError; + section.appendChild(error); + } + var choices = Array.isArray(context.choices) ? context.choices : []; + function outcomeLabel(outcome) { + var key = 'openapi.plan.outcome.' + String(outcome || ''); + return tKey(key) === key ? tKey('openapi.plan.outcome.rejected') : tKey(key); + } + function reasonLabel(reason) { + if (!reason) return ''; + return window.CrankOpenApiImport && typeof window.CrankOpenApiImport.describeFinding === 'function' + ? window.CrankOpenApiImport.describeFinding(reason) + : tKey('openapi.finding.generic'); + } + var list = document.createElement('ul'); + list.className = 'onboarding-import-report-list'; + choices.forEach(function(choice) { + var item = document.createElement('li'); + item.className = 'onboarding-import-report-item'; + var summary = document.createElement('span'); + summary.textContent = [choice.name || choice.key || '', outcomeLabel(choice.outcome), choice.version ? 'v' + choice.version : '', reasonLabel(choice.reason)] + .filter(Boolean).join(' · '); + item.appendChild(summary); + if (choice.selectable === true && (!context.selected_operation_id || context.drift)) { + item.appendChild(button(tKey('onboarding.import.continue'), null, function() { continueWithImportedDraft(choice); }, 'onboarding-step-action')); + } else if (choice.operation_id === context.selected_operation_id && Number(choice.version) === Number(context.selected_operation_version)) { + var selected = document.createElement('span'); + selected.className = 'onboarding-import-selected'; + selected.textContent = tKey('onboarding.import.selected'); + item.appendChild(selected); + } + list.appendChild(item); + }); + section.appendChild(list); + section.appendChild(button(tKey('onboarding.import.view_report'), null, openOpenApiPath, 'onboarding-link-button')); + return section; + } function recordStarted() { if (state.startedRequested || !state.snapshot || state.snapshot.completed) return; state.startedRequested = true; @@ -240,6 +455,17 @@ }); body.appendChild(list); } + var returnContext = document.createElement('span'); + returnContext.className = 'onboarding-return-context'; + returnContext.dataset.testid = 'onboarding-return-context'; + returnContext.hidden = true; + returnContext.textContent = returnContextFromQuery(); + body.appendChild(returnContext); + if (!state.snapshot.completed) { + body.appendChild(renderOpenApiPath()); + } + var importReport = renderImportContext(); + if (importReport) body.appendChild(importReport); var footer = document.createElement('div'); footer.className = 'onboarding-footer'; footer.appendChild(button(tKey('onboarding.refresh'), 'onboarding-refresh', refresh, 'onboarding-link-button')); @@ -295,6 +521,7 @@ var snapshot = await window.CrankApi.getOnboarding(workspaceId); if (epoch !== state.epoch || workspaceId !== (currentWorkspace() && currentWorkspace().id)) return; state.snapshot = snapshot; + state.importError = null; var preference = safePreference(); if (window.location.pathname === '/' && !preference.opened @@ -304,6 +531,21 @@ setOpen(true); } renderDeepLinkRecovery(snapshot); + if (window.location.pathname === '/' && new URLSearchParams(window.location.search).get('onboarding_openapi') === '1' + && window.CrankOpenApiImport && typeof window.CrankOpenApiImport.open === 'function') { + var openUrl = new URL(window.location.href); + openUrl.searchParams.delete('onboarding_openapi'); + window.history.replaceState(window.history.state, '', openUrl.pathname + openUrl.search + openUrl.hash); + window.CrankOpenApiImport.open({ + workspaceId: workspaceId, + onboarding: true, + returnContext: returnContextFromQuery(), + importContext: snapshot.import_context || null, + onImported: function(identity) { return registerImportedApplication(identity); }, + onSelectDraft: function(operationId, version, identity) { return selectImportedDraft(operationId, version, identity); }, + }); + } + return snapshot; } catch (error) { if (epoch !== state.epoch) return; state.error = error; diff --git a/apps/ui/js/openapi-import.js b/apps/ui/js/openapi-import.js index a120d01..caed3aa 100644 --- a/apps/ui/js/openapi-import.js +++ b/apps/ui/js/openapi-import.js @@ -1,17 +1,40 @@ (function() { var MAX_FILE_BYTES = 256 * 1024; - var APPLY_REPLAY_STORAGE_KEY = 'crank_openapi_apply_replay_v1'; + var OPERATIONS_PER_PAGE = 100; + var APPLY_REPLAY_STORAGE_KEY = 'crank_openapi_plan_apply_replay_v2'; var restoredApplyRequest = loadApplyReplay(); var state = { workspaceId: null, onImported: null, + onboarding: false, + returnContext: null, + onSelectDraft: null, + importContext: null, + importIdentity: null, file: null, jobId: null, preview: null, + existingOperations: [], + operationsReady: false, + decisions: Object.create(null), + plan: null, + securityChoices: [], + securityStatus: 'unresolved', + securityByOperation: Object.create(null), filterQuery: '', filterMethod: '', + selectedOperationKeys: new Set(), + operationGroups: [], + pageIndex: 0, + partialMode: false, + planPageIndex: 0, + resultPageIndex: 0, + planVersion: 0, + planRevalidationGeneration: 0, + planRevalidationState: 'current', revision: 0, previewController: null, + planController: null, applyController: null, applyInFlight: false, applyRequest: restoredApplyRequest, @@ -25,23 +48,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() { @@ -125,10 +134,174 @@ } function selectedKeys() { - return Array.from(document.querySelectorAll('[data-openapi-operation]:checked')) - .map(function(input) { - return input.value; + return importOperations() + .map(function(operation) { return operation.key; }) + .filter(function(key) { return state.selectedOperationKeys.has(key); }); + } + + function importOperations() { + return (state.preview && state.preview.groups || []).flatMap(function(group) { + return group.operations || []; + }); + } + + function operationConflicts(operationKey) { + return (state.preview && state.preview.conflicts || []).filter(function(conflict) { + return conflict && conflict.operation_key === operationKey; + }); + } + + function sourcePointer(location) { + return location && typeof location.pointer === 'string' ? location.pointer : ''; + } + + function hasTypedError(findings) { + return (findings || []).some(function(finding) { + return String(finding && finding.severity || '').toLowerCase() === 'error'; + }); + } + + function operationIsBlocked(operation) { + return String(operation && operation.status || '').toLowerCase() === 'blocked' + || hasTypedError(operation && operation.findings) + || hasTypedError((state.preview && state.preview.findings || []).filter(function(finding) { + return operation && finding && finding.operation_key === operation.key; + })); + } + + function operationReadiness(operation) { + var status = String(operation && operation.status || '').toLowerCase(); + var labelKey = { + actionable: 'openapi.operation.readiness_actionable', + blocked: 'openapi.operation.readiness_blocked', + unresolved: 'openapi.operation.readiness_unresolved', + }[status]; + if (!labelKey) return ''; + return '' + escapeHtml(tKey(labelKey)) + ''; + } + + function documentIsBlocked() { + var preview = state.preview || {}; + return String(preview.status || '').toLowerCase() === 'blocked' + || hasTypedError((preview.findings || []).filter(function(finding) { + return !finding || !finding.operation_key; + })); + } + + function decisionFor(operationKey) { + if (!state.decisions[operationKey]) { + var conflicts = operationConflicts(operationKey); + state.decisions[operationKey] = { + action: conflicts.length ? 'rename' : 'create', + targetOperationId: '', + confirmedName: conflicts.length ? String(conflicts[0].proposed_name || '') : '', + securityAlternativeIndex: null, + authProfileId: null, + }; + } + 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); + renderSecurityControls(operation, row); + updateSecuritySummary(); + }); + 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; + if (operationConflicts(operation.key).length) renameInput.dataset.testid = 'openapi-import-rename-proposal'; + renameInput.addEventListener('input', function() { + candidateDecision.confirmedName = renameInput.value; + }); + renameLabel.appendChild(renameInput); + controls.appendChild(renameLabel); + } + + row.appendChild(controls); } function operationRows() { @@ -141,10 +314,23 @@ }); } + function updateCreateAvailability() { + var noOnboardingSelection = state.onboarding + && state.operationsReady + && selectedKeys().length === 0; + var blockedFullImport = state.onboarding + && !state.partialMode + && blockedOperationCount() > 0; + qs('openapi-import-create').disabled = state.planController !== null + || state.applyInFlight + || noOnboardingSelection + || blockedFullImport; + } + function updateSelection() { var node = qs('openapi-import-selection'); var selectedCount = selectedKeys().length; - var totalCount = document.querySelectorAll('[data-openapi-operation]').length; + var totalCount = importOperations().length; var visibleCount = visibleOperationRows().length; if (node) { @@ -164,10 +350,10 @@ var selectedVisible = visibleInputs.filter(function(input) { return input.checked; }).length; - var selectedTotal = rows.filter(function(row) { + var selectedTotal = rows.reduce(function(count, row) { var input = row.querySelector('[data-openapi-operation]'); - return input && input.checked; - }).length; + return count + (input && state.selectedOperationKeys.has(input.value) ? 1 : 0); + }, 0); var checkbox = groupNode.querySelector('[data-openapi-group]'); var counter = groupNode.querySelector('[data-openapi-group-count]'); @@ -184,39 +370,180 @@ }); } }); + + updateSecuritySummary(); + updateCreateAvailability(); } function applyFilters() { - var query = state.filterQuery.toLowerCase(); - var method = state.filterMethod.toLowerCase(); - - document.querySelectorAll('.openapi-import-group').forEach(function(groupNode) { - var visibleInGroup = 0; - - groupNode.querySelectorAll('.openapi-import-operation').forEach(function(row) { - var matchesMethod = !method || String(row.dataset.openapiMethod).toLowerCase() === method; - var matchesQuery = !query || String(row.dataset.openapiSearch).toLowerCase().indexOf(query) >= 0; - var visible = matchesMethod && matchesQuery; - - row.hidden = !visible; - if (visible) visibleInGroup += 1; - }); - - groupNode.hidden = visibleInGroup === 0; - }); - + state.pageIndex = 0; + renderOperationPage(); updateSelection(); } function setVisibleSelection(checked) { visibleOperationRows().forEach(function(row) { var input = row.querySelector('[data-openapi-operation]'); - if (input) input.checked = checked; + if (!input) return; + if (checked && input.disabled) return; + input.checked = checked; + if (checked) state.selectedOperationKeys.add(input.value); + else state.selectedOperationKeys.delete(input.value); }); + renderOperationPage(); updateSelection(); } + function filteredOperationGroups() { + var query = state.filterQuery.toLowerCase(); + var method = state.filterMethod.toLowerCase(); + return state.operationGroups.map(function(group) { + return { + group: group, + operations: group.operations.filter(function(operation) { + var matchesMethod = !method || String(operation.method).toLowerCase() === method; + var searchable = [group.title, operation.key, operation.suggested_name, + operation.suggested_display_name, operation.path, operation.method].join(' ').toLowerCase(); + return matchesMethod && (!query || searchable.indexOf(query) >= 0); + }), + }; + }).filter(function(group) { return group.operations.length > 0; }); + } + + function createOperationRow(operation, groupTitle) { + 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 = [groupTitle, operation.key, operation.suggested_name, + operation.suggested_display_name, operation.path, operation.method].join(' '); + 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) + '' + + operationReadiness(operation); + row.appendChild(selectionLabel); + var operationLocation = sourcePointer(operation.source_location); + if (operationLocation) { + var locationNode = document.createElement('div'); + locationNode.className = 'openapi-import-source-location'; + locationNode.dataset.testid = 'openapi-import-operation-location'; + locationNode.textContent = operationLocation; + row.appendChild(locationNode); + } + operationConflicts(operation.key).forEach(function(conflict) { + var conflictNode = document.createElement('div'); + var conflictType = document.createElement('strong'); + conflictNode.className = 'openapi-import-conflict'; + conflictNode.dataset.testid = 'openapi-import-conflict'; + conflictType.textContent = String(conflict.conflict_type || ''); + conflictNode.appendChild(conflictType); + if (conflict.proposed_name) { + var proposal = document.createElement('span'); + proposal.className = 'openapi-import-conflict-proposal'; + proposal.textContent = String(conflict.proposed_name); + conflictNode.appendChild(proposal); + } + var conflictLocation = sourcePointer(conflict.source_location); + if (conflictLocation) { + var source = document.createElement('code'); + source.className = 'openapi-import-source-location'; + source.dataset.testid = 'openapi-import-conflict-location'; + source.textContent = conflictLocation; + conflictNode.appendChild(source); + } + row.appendChild(conflictNode); + }); + row.appendChild(renderOperationMappingPreview(operation)); + var findingsPanel = renderFindingsPanel(operation.findings); + if (findingsPanel) row.appendChild(findingsPanel); + renderDecisionControls(operation, row); + renderSecurityControls(operation, row); + selectionLabel.querySelector('[data-openapi-operation]').addEventListener('change', function(event) { + if (event.target.checked) state.selectedOperationKeys.add(operation.key); + else state.selectedOperationKeys.delete(operation.key); + updateSelection(); + }); + return row; + } + + function renderOperationPage() { + var groupsNode = qs('openapi-import-groups'); + if (!groupsNode) return; + var filtered = filteredOperationGroups(); + var total = filtered.reduce(function(sum, group) { return sum + group.operations.length; }, 0); + var pageCount = Math.max(1, Math.ceil(total / OPERATIONS_PER_PAGE)); + state.pageIndex = Math.min(Math.max(0, state.pageIndex), pageCount - 1); + var start = state.pageIndex * OPERATIONS_PER_PAGE; + var end = start + OPERATIONS_PER_PAGE; + var cursor = 0; + var pageGroups = []; + filtered.forEach(function(entry) { + var groupStart = cursor; + cursor += entry.operations.length; + var from = Math.max(start, groupStart); + var to = Math.min(end, cursor); + if (from < to) pageGroups.push({ entry: entry, operations: entry.operations.slice(from - groupStart, to - groupStart) }); + }); + + groupsNode.replaceChildren(); + var docFindings = state.preview && state.preview.findings || []; + if (docFindings.length) { + var documentFindings = document.createElement('div'); + documentFindings.className = 'openapi-import-document-findings'; + var findingsPanel = renderFindingsPanel(docFindings); + if (findingsPanel) documentFindings.appendChild(findingsPanel); + groupsNode.appendChild(documentFindings); + } + pageGroups.forEach(function(pageGroup) { + var group = pageGroup.entry.group; + var groupNode = document.createElement('section'); + var header = document.createElement('div'); + var selectedCount = pageGroup.operations.reduce(function(count, operation) { + return count + (state.selectedOperationKeys.has(operation.key) ? 1 : 0); + }, 0); + groupNode.className = 'openapi-import-group'; + groupNode.dataset.selectedCount = String(selectedCount); + header.className = 'openapi-import-group-header'; + header.innerHTML = '' + escapeHtml(group.title) + ' · ' + pageGroup.entry.operations.length + '' + + '' + + ''; + groupNode.appendChild(header); + pageGroup.operations.forEach(function(operation) { + groupNode.appendChild(createOperationRow(operation, group.title)); + }); + groupNode.querySelector('[data-openapi-group]').addEventListener('change', function(event) { + pageGroup.operations.forEach(function(operation) { + if (event.target.checked && operationIsBlocked(operation)) return; + if (event.target.checked) state.selectedOperationKeys.add(operation.key); + else state.selectedOperationKeys.delete(operation.key); + }); + renderOperationPage(); + updateSelection(); + }); + groupsNode.appendChild(groupNode); + }); + var pager = qs('openapi-import-pagination'); + if (pager) { + pager.hidden = total <= OPERATIONS_PER_PAGE; + qs('openapi-import-page-status').textContent = tfKey('openapi.pagination.status', { + page: state.pageIndex + 1, pages: pageCount, total: total, + }); + qs('openapi-import-page-previous').disabled = state.pageIndex === 0; + qs('openapi-import-page-next').disabled = state.pageIndex + 1 >= pageCount; + } + renderBlockersSummary(groupsNode); + resetSecurityStatuses(); + } + function escapeHtml(value) { return String(value || '') .replace(/&/g, '&') @@ -225,22 +552,180 @@ .replace(/"/g, '"'); } + function blockerCodes() { + var codes = []; + var seen = Object.create(null); + importOperations().forEach(function(operation) { + if (!operationIsBlocked(operation)) return; + (operation.findings || []).forEach(function(finding) { + if (String(finding && finding.severity || '').toLowerCase() !== 'error') return; + var code = String(finding && finding.code || ''); + if (code && !seen[code]) { + seen[code] = true; + codes.push(code); + } + }); + }); + return codes; + } + + function blockedOperationCount() { + return importOperations().filter(operationIsBlocked).length; + } + + function renderBlockersSummary(container) { + if (!container) return; + var count = blockedOperationCount(); + var node = document.createElement('div'); + node.className = 'openapi-import-blockers-summary'; + node.dataset.testid = 'openapi-import-blockers'; + node.hidden = count === 0; + if (count > 0) { + var label = document.createElement('span'); + label.className = 'openapi-import-blockers-count'; + label.textContent = tfKey('openapi.blockers.summary', { count: count }); + node.appendChild(label); + blockerCodes().forEach(function(code) { + var chip = document.createElement('code'); + chip.className = 'openapi-import-blocker-code'; + chip.dataset.testid = 'openapi-import-finding-code'; + chip.textContent = code; + node.appendChild(chip); + }); + } + container.appendChild(node); + } + function renderFinding(finding) { var severity = String(finding && finding.severity || 'warning').toLowerCase(); var label = severity === 'info' ? 'i' : severity === 'error' ? '×' : '!'; + var code = String(finding && finding.code || ''); + var pointer = sourcePointer(finding && finding.source_location); return '' + '' + label + ' ' + + (code ? '' + escapeHtml(code) + ' ' : '') + escapeHtml(findingMessage(finding)) + + (pointer ? ' ' + + escapeHtml(pointer) + '' : '') + ''; } + function renderSkippedPanel(items) { + var page = 0; + var details = document.createElement('details'); + var summary = document.createElement('summary'); + var content = document.createElement('div'); + var pageSize = OPERATIONS_PER_PAGE; + details.className = 'openapi-import-result-skipped'; + summary.textContent = tfKey('openapi.result.skipped_count', { count: items.length }); + content.className = 'openapi-import-result-skipped-page'; + function renderPage() { + var list = document.createElement('ul'); + items.slice(page * pageSize, (page + 1) * pageSize).forEach(function(item) { + var entry = document.createElement('li'); + entry.textContent = (item.name || item.operation_key || tKey('openapi.result.unnamed')) + ' — ' + + findingMessage({ code: item.code || item.reason_code || item.reason }); + list.appendChild(entry); + }); + content.replaceChildren(list); + if (items.length > pageSize) { + content.appendChild(createPageNavigation(page, items.length, 'openapi.pagination.result_label', function(nextPage) { + page = nextPage; + renderPage(); + })); + } + } + details.addEventListener('toggle', function() { + if (details.open && !content.childElementCount) renderPage(); + }); + details.append(summary, content); + return details; + } + + function renderFindingsPanel(findings) { + if (!findings || !findings.length) return null; + var pageSize = 5; + var page = 0; + var details = document.createElement('details'); + var summary = document.createElement('summary'); + var content = document.createElement('div'); + var pagination = document.createElement('div'); + var previous = document.createElement('button'); + var status = document.createElement('span'); + var next = document.createElement('button'); + details.className = 'openapi-import-findings'; + summary.textContent = tfKey('openapi.findings.summary', { count: findings.length }); + content.className = 'openapi-import-findings-page'; + pagination.className = 'openapi-import-findings-pagination'; + pagination.hidden = findings.length <= pageSize; + previous.type = 'button'; + previous.className = 'btn-secondary'; + previous.textContent = tKey('openapi.pagination.previous'); + status.setAttribute('role', 'status'); + status.setAttribute('aria-live', 'polite'); + status.setAttribute('aria-atomic', 'true'); + next.type = 'button'; + next.className = 'btn-secondary'; + next.textContent = tKey('openapi.pagination.next'); + function renderPage() { + var pages = Math.max(1, Math.ceil(findings.length / pageSize)); + content.innerHTML = findings.slice(page * pageSize, (page + 1) * pageSize).map(renderFinding).join(''); + status.textContent = tfKey('openapi.pagination.status', { page: page + 1, pages: pages, total: findings.length }); + previous.disabled = page === 0; + next.disabled = page + 1 >= pages; + } + previous.addEventListener('click', function() { page = Math.max(0, page - 1); renderPage(); }); + next.addEventListener('click', function() { + page = Math.min(Math.ceil(findings.length / pageSize) - 1, page + 1); + renderPage(); + }); + details.addEventListener('toggle', function() { + if (details.open && !content.childElementCount) renderPage(); + }); + pagination.append(previous, status, next); + details.append(summary, content, pagination); + return details; + } + + function createPageNavigation(page, total, labelKey, onPage) { + var pageCount = Math.max(1, Math.ceil(total / OPERATIONS_PER_PAGE)); + var navigation = document.createElement('nav'); + var previous = document.createElement('button'); + var status = document.createElement('span'); + var next = document.createElement('button'); + navigation.className = 'openapi-import-pagination'; + navigation.setAttribute('aria-label', tKey(labelKey)); + navigation.setAttribute('role', 'navigation'); + previous.type = 'button'; + previous.className = 'btn-secondary'; + previous.textContent = tKey('openapi.pagination.previous'); + status.setAttribute('role', 'status'); + status.setAttribute('aria-live', 'polite'); + status.setAttribute('aria-atomic', 'true'); + status.textContent = tfKey('openapi.pagination.status', { + page: page + 1, pages: pageCount, total: total, + }); + next.type = 'button'; + next.className = 'btn-secondary'; + next.textContent = tKey('openapi.pagination.next'); + previous.disabled = page === 0; + next.disabled = page + 1 >= pageCount; + previous.addEventListener('click', function() { onPage(Math.max(0, page - 1)); }); + next.addEventListener('click', function() { onPage(Math.min(pageCount - 1, page + 1)); }); + navigation.append(previous, status, next); + return navigation; + } + function findingMessage(finding) { var code = String(finding && finding.code || '').replace(/^.*\./, ''); var known = { missing_servers: 'openapi.finding.missing_servers', multiple_servers: 'openapi.finding.multiple_servers', unsupported_request_body: 'openapi.finding.unsupported_request_body', + unsupported_security_scheme: 'openapi.finding.unsupported_security_scheme', + unsupported_security_and_requirement: 'openapi.finding.unsupported_security_and_requirement', + auth_profile_required: 'openapi.error.auth_profile_required', operation_name_renamed: 'openapi.finding.operation_name_renamed', operation_name_conflict: 'openapi.finding.operation_name_conflict', weak_tool_description: 'openapi.finding.weak_tool_description', @@ -249,6 +734,11 @@ return tKey(known[code] || 'openapi.finding.generic'); } + function outcomeMessage(outcome) { + var key = 'openapi.plan.outcome.' + String(outcome || ''); + return tKey(key) === key ? tKey('openapi.plan.outcome.rejected') : tKey(key); + } + function appendMappingGroup(root, label, values) { if (!values || !values.length) return; @@ -361,16 +851,334 @@ }); } + var SECURITY_STATUS_KEYS = { + no_auth: 'openapi.security.status_no_auth', + required: 'openapi.security.status_required', + unresolved: 'openapi.security.status_unresolved', + unverified: 'openapi.security.status_unverified', + resolved: 'openapi.security.status_resolved', + blocked: 'openapi.security.status_blocked', + }; + var SECURITY_PLAN_ERROR_CODES = [ + 'import_plan_auth_unavailable', + 'import_plan_auth_incompatible', + 'import_plan_security_alternative_required', + ]; + + function securityStatusLabel(status) { + return tKey(SECURITY_STATUS_KEYS[status] || SECURITY_STATUS_KEYS.unresolved); + } + + function renderSecurityStatus() { + var node = qs('openapi-import-security-status'); + if (!node) return; + node.textContent = securityStatusLabel(state.securityStatus); + } + + function setSecurityStatus(status) { + state.securityStatus = status; + renderSecurityStatus(); + } + + function securityOperationNode(operationKey) { + // Compare dataset values against a constant selector: imported operation + // keys are untrusted and must never be interpolated into a CSS selector. + var nodes = document.querySelectorAll('[data-openapi-security-op-status]'); + for (var i = 0; i < nodes.length; i += 1) { + if (nodes[i].dataset.openapiSecurityOpStatus === operationKey) return nodes[i]; + } + return null; + } + + function renderOperationSecurityStatus(operationKey) { + var node = securityOperationNode(operationKey); + if (!node) return; + var status = state.securityByOperation[operationKey] || 'unresolved'; + node.dataset.status = status; + node.textContent = securityStatusLabel(status); + } + + function setOperationSecurityStatus(operationKey, status) { + state.securityByOperation[operationKey] = status; + renderOperationSecurityStatus(operationKey); + } + + // Summary only: the per-operation nodes carry the authority, this aggregate + // id reports whether any selected operation still needs attention. + function updateSecuritySummary() { + var selected = new Set(selectedKeys()); + var statuses = []; + importOperations().forEach(function(operation) { + if (!selected.has(operation.key)) return; + statuses.push(state.securityByOperation[operation.key] || baselineSecurityStatus(operation)); + }); + var summary = 'unresolved'; + if (statuses.indexOf('blocked') >= 0) summary = 'blocked'; + else if (statuses.indexOf('required') >= 0) summary = 'required'; + else if (statuses.indexOf('unverified') >= 0) summary = 'unverified'; + else if (statuses.indexOf('resolved') >= 0) summary = 'resolved'; + else if (statuses.length && statuses.every(function(status) { return status === 'no_auth'; })) summary = 'no_auth'; + setSecurityStatus(summary); + } + + function securityAlternativesFor(operation) { + var requirements = operation && operation.security_requirements; + return requirements && Array.isArray(requirements.alternatives) ? requirements.alternatives : []; + } + + function alternativeFor(operation, index) { + if (index === null || index === undefined) return null; + return securityAlternativesFor(operation)[Number(index)] || null; + } + + function isAnonymousAlternative(alternative) { + if (!alternative || alternative.supported === false) return false; + var schemes = Array.isArray(alternative.schemes) ? alternative.schemes : []; + return schemes.length === 0; + } + + function operationRequiresAuth(operation) { + return securityAlternativesFor(operation).some(function(alternative) { + return alternative && alternative.supported !== false + && Array.isArray(alternative.schemes) && alternative.schemes.length > 0; + }); + } + + function securityProfilesFor(operation, alternativeIndex) { + var profiles = []; + (state.securityChoices || []).forEach(function(choice) { + if (!choice || choice.operation_key !== operation.key) return; + // Filter to the exact selected alternative. While no alternative is + // chosen the safe union supports profile-first selection and lets the + // API own compatibility validation. + if (alternativeIndex !== null && alternativeIndex !== undefined + && Number(choice.alternative_index) !== Number(alternativeIndex)) return; + if (Array.isArray(choice.profiles)) profiles = profiles.concat(choice.profiles); + }); + // Keep a stable order across alternatives: first occurrence wins. + var seen = {}; + return profiles.filter(function(profile) { + if (!profile || typeof profile.id !== 'string' || !profile.id || seen[profile.id]) return false; + seen[profile.id] = true; + return true; + }); + } + + // Local expectation only; resolved/blocked never originate here. + function baselineSecurityStatus(operation) { + var alternatives = securityAlternativesFor(operation); + if (!alternatives.length) return 'no_auth'; + var decision = decisionFor(operation.key); + if (decision.action === 'skip') return 'unresolved'; + if (!alternatives.some(function(alternative) { return alternative && alternative.supported !== false; })) { + return 'blocked'; + } + if (!operationRequiresAuth(operation)) return 'no_auth'; + var alternative = alternativeFor(operation, decision.securityAlternativeIndex); + if (isAnonymousAlternative(alternative)) return 'unresolved'; + if (decision.authProfileId) return 'unresolved'; + return 'required'; + } + + function resetSecurityStatuses() { + importOperations().forEach(function(operation) { + state.securityByOperation[operation.key] = baselineSecurityStatus(operation); + }); + document.querySelectorAll('[data-openapi-security-op-status]').forEach(function(node) { + var key = node.dataset.openapiSecurityOpStatus; + var status = state.securityByOperation[key] || 'unresolved'; + node.dataset.status = status; + node.textContent = securityStatusLabel(status); + }); + updateSecuritySummary(); + } + + function operationRowFor(operation) { + return Array.from(document.querySelectorAll('.openapi-import-operation')).find(function(row) { + var input = row.querySelector('[data-openapi-operation]'); + return input && input.value === operation.key; + }) || null; + } + + function invalidatePendingPlan() { + // Even without a stored plan a prior blocked state must be cleared: the + // edited selection is unverified until the next plan response arrives. + if (state.plan) { + state.planRevalidationGeneration += 1; + state.plan = null; + state.planRevalidationState = 'current'; + 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-stale').hidden = true; + qs('openapi-import-plan-revalidation-status').textContent = ''; + } + resetSecurityStatuses(); + } + + function renderSecurityControls(operation, row) { + var previous = row.querySelector('.openapi-import-security'); + if (previous) previous.remove(); + + var decision = decisionFor(operation.key); + var alternatives = securityAlternativesFor(operation); + + if (decision.action === 'skip') { + // A skipped operation keeps the existing operation untouched: no + // security binding may be attached, so clear it instead of trapping the + // user in a local validation error. + decision.authProfileId = null; + decision.securityAlternativeIndex = null; + setOperationSecurityStatus(operation.key, 'unresolved'); + return; + } + + var wrapper = document.createElement('div'); + wrapper.className = 'openapi-import-security'; + + var statusNode = document.createElement('span'); + statusNode.className = 'openapi-import-security-op-status'; + statusNode.setAttribute('data-openapi-security-op-status', operation.key); + statusNode.setAttribute('data-testid', 'openapi-security-op-status'); + // Announce per-operation changes even when the aggregate text is unchanged. + statusNode.setAttribute('role', 'status'); + statusNode.setAttribute('aria-live', 'polite'); + wrapper.appendChild(statusNode); + + if (alternatives.length) { + var controls = document.createElement('div'); + controls.className = 'openapi-import-security-controls'; + + // Only the alternative index and the typed profile id are stored locally. + // The API remains the authority for OR/AND semantics and compatibility. + var alternativeLabel = document.createElement('label'); + var alternativeSelect = document.createElement('select'); + alternativeLabel.textContent = tKey('openapi.security.alternative_label'); + alternativeSelect.dataset.openapiSecurityAlternative = operation.key; + + var alternativePlaceholder = document.createElement('option'); + alternativePlaceholder.value = ''; + alternativePlaceholder.textContent = tKey('openapi.security.alternative_placeholder'); + alternativeSelect.appendChild(alternativePlaceholder); + + var findingsNode = null; + alternatives.forEach(function(alternative, index) { + var option = document.createElement('option'); + var schemes = alternative && Array.isArray(alternative.schemes) ? alternative.schemes : []; + option.value = String(index); + option.textContent = schemes.length ? schemes.join(' + ') : tKey('openapi.security.no_auth'); + if (alternative && alternative.supported === false) { + option.disabled = true; + option.textContent += ' · ' + tKey('openapi.security.unsupported'); + var finding = Array.isArray(alternative.findings) && alternative.findings[0]; + // Surface the blocker as a safe, localized message next to the control. + if (finding) { + if (!findingsNode) { + findingsNode = document.createElement('span'); + findingsNode.className = 'openapi-import-security-findings'; + } + var findingSpan = document.createElement('span'); + findingSpan.className = 'openapi-import-finding openapi-import-finding-warning'; + findingSpan.textContent = findingMessage({ code: finding }); + findingsNode.appendChild(findingSpan); + } + } + alternativeSelect.appendChild(option); + }); + + alternativeSelect.value = decision.securityAlternativeIndex === null + ? '' + : String(decision.securityAlternativeIndex); + alternativeSelect.addEventListener('change', function() { + // Switching the alternative invalidates the previous profile binding and + // any server-confirmed plan: the API revalidates the current selection. + decision.securityAlternativeIndex = alternativeSelect.value === '' + ? null + : Number(alternativeSelect.value); + decision.authProfileId = null; + invalidatePendingPlan(); + renderSecurityControls(operation, row); + }); + alternativeLabel.appendChild(alternativeSelect); + controls.appendChild(alternativeLabel); + if (findingsNode) controls.appendChild(findingsNode); + + var selectedAlternative = alternativeFor(operation, decision.securityAlternativeIndex); + var anonymous = isAnonymousAlternative(selectedAlternative); + var profiles = securityProfilesFor(operation, decision.securityAlternativeIndex); + + // An anonymous alternative must offer no profile at all. + if (!anonymous) { + var profileLabel = document.createElement('label'); + var profileSelect = document.createElement('select'); + profileLabel.textContent = tKey('openapi.security.profile_label'); + profileSelect.dataset.openapiSecurityProfile = operation.key; + + var profilePlaceholder = document.createElement('option'); + profilePlaceholder.value = ''; + profilePlaceholder.textContent = tKey('openapi.security.profile_placeholder'); + profileSelect.appendChild(profilePlaceholder); + + profiles.forEach(function(profile) { + // Safe metadata only: id, name, kind. Never credential material. + var option = document.createElement('option'); + option.value = profile.id; + option.textContent = String(profile.name || profile.id); + if (profile.kind) option.textContent += ' · ' + profile.kind; + profileSelect.appendChild(option); + }); + + var emptyProfiles = decision.securityAlternativeIndex !== null + && selectedAlternative && selectedAlternative.supported !== false + && !anonymous && profiles.length === 0; + if (emptyProfiles) { + profileSelect.disabled = true; + var guidance = document.createElement('span'); + guidance.className = 'openapi-import-security-guidance'; + guidance.textContent = tKey('openapi.security.empty_profiles'); + profileLabel.appendChild(guidance); + } + + profileSelect.value = decision.authProfileId || ''; + profileSelect.addEventListener('change', function() { + decision.authProfileId = profileSelect.value || null; + invalidatePendingPlan(); + }); + profileLabel.appendChild(profileSelect); + controls.appendChild(profileLabel); + } + + wrapper.appendChild(controls); + } + + row.appendChild(wrapper); + setOperationSecurityStatus(operation.key, baselineSecurityStatus(operation)); + } + function renderPreview(response) { var preview = response.preview; var count = (preview.groups || []).reduce(function(sum, group) { return sum + (group.operations || []).length; }, 0); var source = qs('openapi-import-source'); - var groupsNode = qs('openapi-import-groups'); state.jobId = response.job_id; state.preview = preview; + state.existingOperations = []; + state.operationsReady = false; + state.decisions = Object.create(null); + state.securityChoices = Array.isArray(response.security_profile_choices) + ? response.security_profile_choices + : []; + state.securityByOperation = Object.create(null); + state.operationGroups = preview.groups || []; + state.selectedOperationKeys = new Set(importOperations().map(function(operation) { return operation.key; })); + state.pageIndex = 0; + state.partialMode = false; + qs('openapi-import-partial-mode').checked = false; + importOperations().forEach(function(operation) { decisionFor(operation.key); }); state.filterQuery = ''; state.filterMethod = ''; qs('openapi-import-preview-panel').hidden = false; @@ -384,73 +1192,8 @@ qs('openapi-import-search').value = ''; populateServers(preview); populateMethodFilter(preview); - groupsNode.replaceChildren(); - - if (preview.findings && preview.findings.length) { - var documentFindings = document.createElement('div'); - documentFindings.className = 'openapi-import-document-findings'; - documentFindings.innerHTML = preview.findings.map(renderFinding).join(''); - groupsNode.appendChild(documentFindings); - } - - (preview.groups || []).forEach(function(group) { - var groupNode = document.createElement('section'); - var header = document.createElement('div'); - - groupNode.className = 'openapi-import-group'; - header.className = 'openapi-import-group-header'; - header.innerHTML = '' + escapeHtml(group.title) + ' · ' + group.operations.length + '' - + '' - + ''; - groupNode.appendChild(header); - - group.operations.forEach(function(operation) { - var row = document.createElement('label'); - - row.className = 'openapi-import-operation'; - row.dataset.openapiMethod = operation.method; - row.dataset.openapiSearch = [ - group.title, - operation.key, - operation.suggested_name, - operation.suggested_display_name, - operation.path, - operation.method, - ].join(' '); - row.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(renderOperationMappingPreview(operation)); - - if (operation.findings && operation.findings.length) { - var findings = document.createElement('div'); - findings.className = 'openapi-import-findings'; - findings.innerHTML = operation.findings.map(renderFinding).join(''); - row.appendChild(findings); - } - - groupNode.appendChild(row); - }); - - groupNode.querySelector('[data-openapi-group]').addEventListener('change', function(event) { - groupNode.querySelectorAll('.openapi-import-operation').forEach(function(row) { - var input = row.querySelector('[data-openapi-operation]'); - if (!row.hidden && input) input.checked = event.target.checked; - }); - updateSelection(); - }); - groupsNode.appendChild(groupNode); - }); - - groupsNode.querySelectorAll('[data-openapi-operation]').forEach(function(input) { - input.addEventListener('change', updateSelection); - }); - applyFilters(); + renderOperationPage(); + updateSelection(); } function validateFile(file) { @@ -478,8 +1221,23 @@ } function clearPreview() { + state.planRevalidationGeneration += 1; + state.planVersion = 0; + state.planRevalidationState = 'current'; state.jobId = null; state.preview = null; + state.existingOperations = []; + state.operationsReady = false; + state.decisions = Object.create(null); + state.securityChoices = []; + state.securityStatus = 'unresolved'; + state.securityByOperation = Object.create(null); + state.operationGroups = []; + state.selectedOperationKeys = new Set(); + state.pageIndex = 0; + state.partialMode = false; + qs('openapi-import-partial-mode').checked = false; + state.plan = null; state.applyRequest = null; state.applyMetadata = []; state.applyResult = null; @@ -488,7 +1246,21 @@ 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-plan-pagination').hidden = true; + qs('openapi-import-versioning-consequences').hidden = true; + qs('openapi-import-plan-blocked-help').hidden = true; + qs('openapi-import-plan-stale').hidden = true; + qs('openapi-import-plan-revalidation-status').textContent = ''; qs('openapi-import-groups').replaceChildren(); + qs('openapi-import-pagination').hidden = true; + qs('openapi-import-page-previous').disabled = true; + qs('openapi-import-page-next').disabled = true; + qs('openapi-import-select-visible').disabled = true; + qs('openapi-import-clear-visible').disabled = true; + renderSecurityStatus(); qs('openapi-import-result').hidden = true; qs('openapi-import-result').replaceChildren(); updateSelection(); @@ -502,12 +1274,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 +1305,61 @@ 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 || !state.partialMode; + qs('openapi-import-clear-visible').disabled = locked || !state.partialMode; + qs('openapi-import-partial-mode').disabled = locked || documentIsBlocked(); + qs('openapi-import-page-previous').disabled = locked || state.pageIndex === 0; + qs('openapi-import-page-next').disabled = locked || state.pageIndex + 1 >= Math.ceil(filteredOperationGroups().reduce(function(sum, group) { return sum + group.operations.length; }, 0) / OPERATIONS_PER_PAGE); + document.querySelectorAll('[data-openapi-operation], [data-openapi-group]').forEach(function(input) { + var row = input.closest('.openapi-import-operation'); + var operation = row && importOperations().find(function(candidate) { + return candidate.key === input.value; + }); + input.disabled = locked || !state.partialMode || documentIsBlocked() + || (input.matches('[data-openapi-operation]') && operationIsBlocked(operation)); + }); + document.querySelectorAll('.openapi-import-decision-controls select, .openapi-import-decision-controls input').forEach(function(input) { + input.disabled = locked; + }); + document.querySelectorAll('.openapi-import-security-controls select, .openapi-import-security-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 || !state.partialMode; + qs('openapi-import-clear-visible').disabled = locked || !state.partialMode; + qs('openapi-import-partial-mode').disabled = locked || documentIsBlocked(); + qs('openapi-import-page-previous').disabled = locked || state.pageIndex === 0; + qs('openapi-import-page-next').disabled = locked || state.pageIndex + 1 >= Math.ceil(filteredOperationGroups().reduce(function(sum, group) { return sum + group.operations.length; }, 0) / OPERATIONS_PER_PAGE); + document.querySelectorAll('[data-openapi-operation], [data-openapi-group]').forEach(function(input) { + var row = input.closest('.openapi-import-operation'); + var operation = row && importOperations().find(function(candidate) { + return candidate.key === input.value; + }); + input.disabled = locked || !state.partialMode || documentIsBlocked() + || (input.matches('[data-openapi-operation]') && operationIsBlocked(operation)); + }); + document.querySelectorAll('.openapi-import-decision-controls select, .openapi-import-decision-controls input').forEach(function(input) { + input.disabled = locked; + }); + document.querySelectorAll('.openapi-import-security-controls select, .openapi-import-security-controls input').forEach(function(input) { + input.disabled = locked; + }); } function markApplyOutcomeUnknown(options) { @@ -539,14 +1370,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 +1388,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(); @@ -578,6 +1419,9 @@ 'openapi_upload.storage_unavailable': 'openapi.error.storage_unavailable', 'openapi_upload.source_integrity': 'openapi.error.source_integrity', 'openapi_upload.source_unavailable': 'openapi.error.source_unavailable', + 'import_plan_auth_unavailable': 'openapi.error.auth_unavailable', + 'import_plan_auth_incompatible': 'openapi.error.auth_incompatible', + 'import_plan_security_alternative_required': 'openapi.error.security_alternative_required', }; var message = tKey(known[error && error.code] || fallback); var identifiers = []; @@ -614,13 +1458,33 @@ qs('openapi-import-preview').disabled = true; qs('openapi-import-cancel').hidden = false; showRetry(false); - setStatus(tKey('openapi.status.previewing')); + setStatus(tKey('openapi.status.uploading')); try { var response = await window.CrankApi.previewOpenApiImport(workspaceId, state.file, { signal: controller.signal }); if (!isCurrent(revision, workspaceId, language)) return; + setStatus(tKey('openapi.status.parsing')); + await new Promise(function(resolve) { window.requestAnimationFrame(resolve); }); + 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 = operationRowFor(operation); + if (row) { + renderDecisionControls(operation, row); + renderSecurityControls(operation, row); + } + }); + updateCreateAvailability(); setStatus(tKey('openapi.status.preview_ready')); } catch (error) { if (!isCurrent(revision, workspaceId, language)) return; @@ -687,18 +1551,7 @@ return; } - findings.slice(0, 4).forEach(function(finding) { - var wrapper = document.createElement('div'); - wrapper.innerHTML = renderFinding(finding); - root.appendChild(wrapper.firstElementChild); - }); - - if (findings.length > 4) { - var more = document.createElement('span'); - more.className = 'openapi-import-result-more'; - more.textContent = tfKey('openapi.result.more', { count: findings.length - 4 }); - root.appendChild(more); - } + root.appendChild(renderFindingsPanel(findings)); } function renderResult(response) { @@ -727,7 +1580,10 @@ + '
' + escapeHtml(tKey('openapi.result.action')) + '
' + ''; - response.created.forEach(function(operation) { + var created = Array.isArray(response.created) ? response.created : []; + var resultPageCount = Math.max(1, Math.ceil(created.length / OPERATIONS_PER_PAGE)); + state.resultPageIndex = Math.min(Math.max(0, state.resultPageIndex), resultPageCount - 1); + created.slice(state.resultPageIndex * OPERATIONS_PER_PAGE, (state.resultPageIndex + 1) * OPERATIONS_PER_PAGE).forEach(function(operation) { var previewOperation = previewOperationForCreated(operation); var row = document.createElement('div'); var nameCell = document.createElement('div'); @@ -762,44 +1618,492 @@ }); node.appendChild(table); + if (created.length > OPERATIONS_PER_PAGE) { + node.appendChild(createPageNavigation(state.resultPageIndex, created.length, 'openapi.pagination.result_label', function(page) { + state.resultPageIndex = page; + renderResult(response); + })); + } } if (response.skipped && response.skipped.length) { - var skippedNode = document.createElement('div'); - skippedNode.className = 'openapi-import-result-skipped'; - skippedNode.textContent = tKey('openapi.result.skipped') + ': ' + response.skipped.map(function(item) { - return (item.name || item.operation_key || tKey('openapi.result.unnamed')) + ' — ' - + findingMessage({ code: item.code || item.reason_code || item.reason }); - }).join('; '); - node.appendChild(skippedNode); + node.appendChild(renderSkippedPanel(response.skipped)); } if (response.findings && response.findings.length) { - var findings = document.createElement('div'); - findings.className = 'openapi-import-result-findings'; - findings.innerHTML = response.findings.map(renderFinding).join(''); - node.appendChild(findings); + var findingsPanel = renderFindingsPanel(response.findings); + findingsPanel.classList.add('openapi-import-result-findings'); + node.appendChild(findingsPanel); } node.hidden = false; } 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, }); } - async function notifyImported(revision, workspaceId, language) { + 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 updatePlanRevalidationUi(plan, hasBlocked) { + var statusNode = qs('openapi-import-plan-revalidation-status'); + var staleNode = qs('openapi-import-plan-stale'); + var reopenButton = qs('openapi-import-plan-reopen'); + var statusKey = 'openapi.plan.revalidation_' + state.planRevalidationState; + statusNode.textContent = tKey(statusKey); + var unsafe = state.planRevalidationState === 'stale' + || state.planRevalidationState === 'error' + || state.planRevalidationState === 'pending' + || !!plan.revalidation_required + || state.applyOutcomeUnknown; + staleNode.hidden = !unsafe || state.planRevalidationState === 'pending' || state.applyOutcomeUnknown; + staleNode.textContent = unsafe && !staleNode.hidden ? tKey('openapi.plan.revalidation_guidance') : ''; + reopenButton.disabled = state.applyInFlight || state.applyOutcomeUnknown; + reopenButton.setAttribute('aria-busy', state.planRevalidationState === 'pending' ? 'true' : 'false'); + qs('openapi-import-plan-apply').disabled = hasBlocked || unsafe; + } + + function renderImportPlan(plan, pageOnly) { + 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 operationsByKey = Object.create(null); + importOperations().forEach(function(operation) { operationsByKey[operation.key] = operation; }); + + if (!pageOnly) { + // The plan API is the sole authority for each operation's binding outcome. + (Array.isArray(plan.candidates) ? plan.candidates : []).forEach(function(candidate) { + if (!candidate || !candidate.key) return; + var operation = operationsByKey[candidate.key]; + var alternativeIndex = candidate.security_alternative_index; + var anonymous = alternativeIndex !== null && alternativeIndex !== undefined + && isAnonymousAlternative(alternativeFor(operation, alternativeIndex)); + var status; + if (candidate.state === 'blocked') { + status = 'blocked'; + } else if (candidate.state === 'actionable') { + // Only a genuine auth binding confirmed by the API is resolved; a + // no-auth/anonymous or unbound candidate must never read as resolved. + if (candidate.auth_profile_id) status = 'resolved'; + else if (operation && !operationRequiresAuth(operation)) status = 'no_auth'; + else if (anonymous) status = 'no_auth'; + else status = 'unresolved'; + } else { + // Excluded/skipped candidates keep the operation untouched. + status = 'unresolved'; + } + state.securityByOperation[candidate.key] = status; + }); + document.querySelectorAll('[data-openapi-security-op-status]').forEach(function(node) { + var status = state.securityByOperation[node.dataset.openapiSecurityOpStatus] || 'unresolved'; + node.dataset.status = status; + node.textContent = securityStatusLabel(status); + }); + updateSecuritySummary(); + } + var countSpecs = [ + ['selected', 'openapi.plan.selected'], + ['excluded', 'openapi.plan.excluded'], + ['actionable', 'openapi.plan.actionable'], + ['blocked', 'openapi.plan.blocked'], + ]; + + var planCandidates = Array.isArray(plan.candidates) ? plan.candidates : []; + var planPageCount = Math.max(1, Math.ceil(planCandidates.length / OPERATIONS_PER_PAGE)); + state.planPageIndex = Math.min(Math.max(0, state.planPageIndex), planPageCount - 1); + state.planCandidates = planCandidates; + countsNode.replaceChildren(); + candidatesNode.replaceChildren(); + var planPagination = qs('openapi-import-plan-pagination'); + planPagination.hidden = planCandidates.length <= OPERATIONS_PER_PAGE; + qs('openapi-import-plan-page-status').textContent = tfKey('openapi.pagination.status', { + page: state.planPageIndex + 1, + pages: planPageCount, + total: planCandidates.length, + }); + qs('openapi-import-plan-page-previous').disabled = state.planPageIndex === 0; + qs('openapi-import-plan-page-next').disabled = state.planPageIndex + 1 >= planPageCount; + 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 = planCandidates.some(function(candidate) { + return candidate.action === 'new_version' && candidate.state !== 'blocked' && candidate.state !== 'excluded'; + }); + qs('openapi-import-plan-version').textContent = tfKey('openapi.plan.version', { + version: state.planVersion, + reference: plan.plan_reference, + digest: plan.plan_digest, + }); + planCandidates.slice(state.planPageIndex * OPERATIONS_PER_PAGE, (state.planPageIndex + 1) * OPERATIONS_PER_PAGE).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.reason) { + var reason = document.createElement('p'); + reason.className = 'openapi-import-plan-reason'; + if (candidate.state === 'excluded') reason.dataset.testid = 'openapi-import-excluded-reason'; + reason.textContent = findingMessage({ code: candidate.reason }); + if (candidate.state === 'excluded') reason.textContent += ' (' + 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; + updatePlanRevalidationUi(plan, hasBlocked); + qs('openapi-import-plan-back').textContent = tKey(hasBlocked ? 'openapi.plan.back_fix' : 'openapi.plan.back'); + if (!pageOnly) lockPlanEditing(true); + } + + async function revalidateCurrentPlan() { + if (!state.plan || state.applyInFlight || state.applyOutcomeUnknown) return; + var planReference = state.plan.plan_reference; + var generation = ++state.planRevalidationGeneration; + state.planRevalidationState = 'pending'; + updatePlanRevalidationUi(state.plan, (Number(state.plan.counts && state.plan.counts.blocked) || 0) > 0); + try { + var response = await window.CrankApi.revalidateOpenApiImportPlan( + state.workspaceId, + planReference, + {} + ); + if (generation !== state.planRevalidationGeneration + || !state.plan || state.plan.plan_reference !== planReference) return; + if (!response || response.plan_reference !== planReference + || !/^[a-f0-9]{64}$/.test(String(response.plan_digest || '')) + || response.plan_digest !== state.plan.plan_digest + || !response.counts || !Array.isArray(response.candidates) + || typeof response.revalidation_required !== 'boolean') { + throw new Error('Backend returned an invalid ImportPlan revalidation response'); + } + state.plan = Object.assign({}, state.plan, response); + if (response.revalidation_status === 'stale' || response.revalidation_required) { + state.planRevalidationState = 'stale'; + } else if (response.revalidation_status === 'current' && !response.revalidation_required) { + state.planRevalidationState = 'current'; + } else { + state.planRevalidationState = 'error'; + } + renderImportPlan(state.plan); + } catch (_error) { + if (generation !== state.planRevalidationGeneration + || !state.plan || state.plan.plan_reference !== planReference) return; + state.planRevalidationState = 'error'; + updatePlanRevalidationUi(state.plan, (Number(state.plan.counts && state.plan.counts.blocked) || 0) > 0); + } + } + + 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'; + var pageCount = Math.max(1, Math.ceil(outcomes.length / OPERATIONS_PER_PAGE)); + state.resultPageIndex = Math.min(Math.max(0, state.resultPageIndex), pageCount - 1); + outcomes.slice(state.resultPageIndex * OPERATIONS_PER_PAGE, (state.resultPageIndex + 1) * OPERATIONS_PER_PAGE).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); + if (outcomes.length > OPERATIONS_PER_PAGE) { + node.appendChild(createPageNavigation(state.resultPageIndex, outcomes.length, 'openapi.pagination.result_label', function(page) { + state.resultPageIndex = page; + renderImportPlanReport(report); + })); + } + } + + if (state.onboarding) renderOnboardingDraftChoice(node, report); + + node.hidden = false; + qs('openapi-import-plan-confirmation').hidden = true; + } + + function renderOnboardingDraftChoice(node, report) { + var previous = node.querySelector('[data-testid="onboarding-import-draft-choice"]'); + if (previous) previous.remove(); + var outcomes = Array.isArray(report.outcomes) ? report.outcomes : []; + var context = state.importContext; + var choices = context && Array.isArray(context.choices) ? context.choices : []; + if (!choices.length) { + choices = outcomes.map(function(outcome) { + return { + key: outcome.key, + name: outcome.name, + operation_id: outcome.operation_id, + version: outcome.version, + outcome: outcome.status || outcome.outcome, + selectable: false, + reason: tKey('onboarding.import.registration_pending'), + }; + }); + } + if (!choices.length) return; + var chooser = document.createElement('div'); + chooser.className = 'onboarding-import-draft-choice'; + chooser.dataset.testid = 'onboarding-import-draft-choice'; + var heading = document.createElement('strong'); + heading.className = 'onboarding-import-draft-choice-title'; + heading.textContent = tKey('onboarding.import.choose_draft'); + chooser.appendChild(heading); + var list = document.createElement('div'); + list.className = 'onboarding-import-draft-choice-list'; + choices.forEach(function(outcome) { + var wrapper = document.createElement('div'); + wrapper.className = 'onboarding-import-draft-option-row'; + var label = document.createElement('span'); + label.textContent = [ + outcome.name || outcome.key || tKey('openapi.result.unnamed'), + outcomeMessage(outcome.outcome), + outcome.version ? 'v' + outcome.version : '', + outcome.reason ? findingMessage({ code: outcome.reason }) : '', + ] + .filter(Boolean).join(' · '); + wrapper.appendChild(label); + var alreadySelected = context && context.selected_operation_id === outcome.operation_id + && Number(context.selected_operation_version) === Number(outcome.version); + if (alreadySelected) { + var selected = document.createElement('span'); + selected.className = 'onboarding-import-draft-selected'; + selected.textContent = tKey('onboarding.import.selected'); + wrapper.appendChild(selected); + } else if (outcome.selectable === true && !(context && context.selected_operation_id && !context.drift)) { + var choice = document.createElement('button'); + choice.type = 'button'; + choice.className = 'btn-secondary onboarding-import-draft-option'; + choice.textContent = tKey('onboarding.import.continue'); + choice.addEventListener('click', function() { chooseDraft(outcome); }); + wrapper.appendChild(choice); + } + list.appendChild(wrapper); + }); + if (context && context.drift) { + var drift = document.createElement('p'); + drift.className = 'onboarding-import-drift'; + drift.setAttribute('role', 'alert'); + drift.textContent = tKey('onboarding.import.drift'); + chooser.appendChild(drift); + } + chooser.appendChild(list); + node.appendChild(chooser); + } + + async function chooseDraft(outcome) { + var operationId = outcome && outcome.operation_id; + var version = outcome && Number.isInteger(outcome.version) ? outcome.version : null; + if (!operationId || !Number.isInteger(version) || version < 1 || !state.importContext) return; + var revision = state.revision; + var workspaceId = state.workspaceId; + var language = locale(); + if (!isCurrent(revision, workspaceId, language)) return; + try { + var response = typeof state.onSelectDraft === 'function' + ? await state.onSelectDraft(operationId, version, { + applicationIdempotencyKey: state.importContext.application_idempotency_key, + planDigest: state.importContext.plan_digest, + }) + : null; + if (!isCurrent(revision, workspaceId, language)) return; + var current = response && response.onboarding ? response.onboarding : response; + if (!current || !current.import_context + || current.import_context.selected_operation_id !== operationId + || Number(current.import_context.selected_operation_version) !== version) { + throw new Error(tKey('onboarding.import.selection_failed')); + } + state.importContext = current.import_context; + renderImportPlanReport(current.import_context.report || state.applyResult); + var target = ((window.CrankRoutes && window.CrankRoutes.wizard) || '/wizard/') + + '?mode=edit&operationId=' + encodeURIComponent(operationId) + + '&operationVersion=' + encodeURIComponent(version) + '&onboarding=1' + + (state.returnContext ? '&return=' + encodeURIComponent(state.returnContext) : ''); + window.location.href = target; + } catch (error) { + if (!isCurrent(revision, workspaceId, language)) return; + if (error && error.status === 409 && window.CrankOnboarding && typeof window.CrankOnboarding.refresh === 'function') { + var refreshed = await window.CrankOnboarding.refresh(); + if (!isCurrent(revision, workspaceId, language)) return; + if (refreshed && refreshed.import_context) { + state.importContext = refreshed.import_context; + renderImportPlanReport(refreshed.import_context.report || state.applyResult); + } + } + setStatus(error && error.status === 409 ? tKey('onboarding.import.conflict_reload') : tKey('onboarding.import.selection_failed'), true, 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, request) { if (typeof state.onImported !== 'function') return; + if (!state.onboarding) { + try { + await state.onImported(); + } catch (_refreshError) { + if (isCurrent(revision, workspaceId, language) && state.applyResult) { + state.applyRefreshFailed = true; + setStatus(createdStatus(state.applyResult) + ' ' + tKey('openapi.status.refresh_failed'), false, true); + } + } + return; + } + if (!request || !state.applyResult || state.applyResult.status !== 'applied') return; try { - await state.onImported(); + var identity = { + applicationIdempotencyKey: state.applyResult.idempotency_key, + planDigest: state.applyResult.plan_digest, + }; + if (identity.applicationIdempotencyKey !== request.idempotencyKey || identity.planDigest !== request.planDigest) { + throw new Error(tKey('onboarding.import.registration_failed')); + } + state.importIdentity = identity; + var current = await state.onImported(identity); + if (!isCurrent(revision, workspaceId, language)) return; + if (current && current.import_context) { + state.importContext = current.import_context; + renderImportPlanReport(current.import_context.report || state.applyResult); + } } catch (_error) { - state.applyRefreshFailed = true; if (isCurrent(revision, workspaceId, language) && state.applyResult) { - setStatus(createdStatus(state.applyResult) + ' ' + tKey('openapi.status.refresh_failed'), false, true); + state.applyRefreshFailed = true; + var status = qs('openapi-import-status'); + setStatus(createdStatus(state.applyResult) + ' ' + tKey('onboarding.import.registration_failed'), true, true); + if (state.importIdentity && status) { + var retry = document.createElement('button'); + retry.type = 'button'; + retry.className = 'btn-secondary onboarding-import-registration-retry'; + retry.textContent = tKey('onboarding.import.retry_registration'); + retry.addEventListener('click', async function() { + var retryRevision = state.revision; + var retryWorkspaceId = state.workspaceId; + var retryLanguage = locale(); + if (!isCurrent(retryRevision, retryWorkspaceId, retryLanguage)) return; + retry.disabled = true; + try { + var current = await state.onImported(state.importIdentity); + if (!isCurrent(retryRevision, retryWorkspaceId, retryLanguage)) return; + if (!current || !current.import_context) throw new Error(tKey('onboarding.import.registration_failed')); + state.importContext = current.import_context; + renderImportPlanReport(current.import_context.report || state.applyResult); + state.applyRefreshFailed = false; + setStatus(tKey('openapi.status.applied')); + } catch (_retryError) { + if (!isCurrent(retryRevision, retryWorkspaceId, retryLanguage)) return; + retry.disabled = false; + setStatus(tKey('onboarding.import.registration_failed'), true, true); + status.appendChild(retry); + } + }); + status.appendChild(retry); + } } } } @@ -819,13 +2123,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. @@ -846,12 +2153,24 @@ return; } state.applyResult = response; + state.resultPageIndex = 0; state.applyRequest = null; + state.plan = null; if (isCurrent(revision, workspaceId, language)) { - setStatus(createdStatus(response)); - renderResult(response); + if (Array.isArray(response.outcomes)) renderImportPlanReport(response); + else renderResult(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); + await notifyImported(revision, workspaceId, language, request); } catch (error) { // A cancelled attempt may settle after an idempotent retry has already // installed a new controller. It must not overwrite the newer state. @@ -859,9 +2178,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 +2192,38 @@ state.applyRequest = null; clearApplyReplay(); showRetry(true); + var report = reportFromPayload(error.payload); + if (report && isCurrent(revision, workspaceId, language)) { + state.applyResult = report; + state.resultPageIndex = 0; + renderImportPlanReport(report); + } + var recovery = recoveryCode(error.payload); + state.plan = null; + qs('openapi-import-plan-confirmation').hidden = true; + lockPlanEditing(false); + // A rejected/repreview Apply voids the prior resolved security review. + resetSecurityStatuses(); 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 +2235,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 > 2000 || importOperations().length - keys.length > 2000) { + 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 +2258,184 @@ 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)) return; + var decision = decisionFor(operation.key); + // Skip decisions keep the existing operation untouched: no security + // binding may be attached to them. + if (decision.action === 'skip' + && (decision.authProfileId || decision.securityAlternativeIndex !== null)) { + invalidDecision = 'openapi.error.security_with_skip'; + } + }); + 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; + } + // Security binding is a typed reference only: alternative index and, + // when chosen, the profile id. Resolution stays server-side. + if (decision.action !== 'skip') { + if (decision.authProfileId) output.auth_profile_id = decision.authProfileId; + if (decision.securityAlternativeIndex !== null) { + output.security_alternative_index = decision.securityAlternativeIndex; + } + } + if (decision.action !== 'create' + || decision.authProfileId + || decision.securityAlternativeIndex !== null) { + decisions.push(output); + } + }); + if (invalidDecision) { + setStatus(tKey(invalidDecision), true, true); + return; + } + + var selectionMode = state.partialMode ? 'partial' : 'full'; + var payload = { + selection_mode: selectionMode, + selected_operation_keys: 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.planRevalidationGeneration += 1; + state.planRevalidationState = 'current'; + 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; + state.planVersion += 1; + state.planPageIndex = 0; + state.planRevalidationState = plan.revalidation_status === 'stale' || plan.revalidation_required + ? 'stale' : 'current'; + 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); + // A typed API rejection of a security binding is authoritative blocked; + // a transport/network failure only means the selection is unverified. + var securityRejection = Number.isInteger(error && error.status) + && error.status >= 400 && error.status < 500 + && SECURITY_PLAN_ERROR_CODES.indexOf(String(error.code)) >= 0; + importOperations().forEach(function(operation) { + if (!selectedSet.has(operation.key) || !securityAlternativesFor(operation).length) return; + setOperationSecurityStatus(operation.key, securityRejection ? 'blocked' : 'unverified'); + }); + updateSecuritySummary(); + } 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; + if (state.planRevalidationState !== 'current' || state.plan.revalidation_required) 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.planRevalidationGeneration += 1; + state.plan = null; + state.planRevalidationState = 'current'; + // Returning to editing voids the previous review: revalidation is done by + // the next plan request. + resetSecurityStatuses(); + 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-stale').hidden = true; + qs('openapi-import-plan-revalidation-status').textContent = ''; + qs('openapi-import-plan-back').textContent = tKey('openapi.plan.back'); + lockPlanEditing(false); + setStatus(tKey('openapi.status.preview_ready')); } function retryCurrent() { @@ -944,31 +2446,60 @@ } function reset() { + state.importContext = null; + state.importIdentity = null; invalidate({ clearFile: true }); showRetry(false); setStatus(''); qs('openapi-import-server-custom').value = ''; - qs('openapi-import-conflict-mode').value = 'rename'; } function open(options) { + var changedWorkspace = state.workspaceId !== options.workspaceId; if (state.workspaceId && state.workspaceId !== options.workspaceId && !(state.applyOutcomeUnknown && state.applyRequest)) { invalidate({ clearFile: true }); } + if (changedWorkspace) state.importIdentity = null; state.workspaceId = options.workspaceId; state.onImported = options.onImported; + state.onboarding = options.onboarding === true; + if (Object.prototype.hasOwnProperty.call(options, 'importContext')) { + state.importContext = options.importContext || null; + } else if (changedWorkspace) { + state.importContext = null; + } + state.returnContext = typeof options.returnContext === 'string' ? options.returnContext : null; + state.onSelectDraft = typeof options.onSelectDraft === 'function' ? options.onSelectDraft : null; state.restoreFocus = document.activeElement; 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 (state.importContext && state.importContext.report) { + renderImportPlanReport(state.importContext.report); + setStatus(tKey('onboarding.import.report_title')); + } else if (state.applyResult) { + if (state.importContext && state.importContext.report) { + renderImportPlanReport(state.importContext.report); + } else 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; } @@ -1002,6 +2533,8 @@ } function selectFile(file) { + state.importContext = null; + state.importIdentity = null; clearServerSelection(); invalidate(); state.file = file || null; @@ -1022,6 +2555,18 @@ renderFileName(); + // Dedicated screen-reader output for the API-authoritative binding state. + var groupsNode = qs('openapi-import-groups'); + if (!qs('openapi-import-security-status') && groupsNode && groupsNode.parentNode) { + var securityStatusNode = document.createElement('div'); + securityStatusNode.id = 'openapi-import-security-status'; + securityStatusNode.className = 'openapi-import-security-status'; + securityStatusNode.setAttribute('data-testid', 'openapi-security-status'); + securityStatusNode.setAttribute('role', 'status'); + securityStatusNode.setAttribute('aria-live', 'polite'); + groupsNode.parentNode.insertBefore(securityStatusNode, groupsNode); + } + qs('openapi-import-file-select').addEventListener('click', function(event) { event.preventDefault(); qs('openapi-import-file').click(); @@ -1031,7 +2576,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) { @@ -1048,6 +2595,41 @@ qs('openapi-import-clear-visible').addEventListener('click', function() { setVisibleSelection(false); }); + qs('openapi-import-partial-mode').addEventListener('change', function(event) { + state.partialMode = event.target.checked && !documentIsBlocked(); + event.target.checked = state.partialMode; + if (!state.partialMode) state.selectedOperationKeys = new Set(importOperations().map(function(operation) { return operation.key; })); + else importOperations().forEach(function(operation) { + if (operationIsBlocked(operation)) state.selectedOperationKeys.delete(operation.key); + }); + renderOperationPage(); + qs('openapi-import-select-visible').disabled = !state.partialMode; + qs('openapi-import-clear-visible').disabled = !state.partialMode; + lockPlanEditing(false); + updateSelection(); + }); + qs('openapi-import-page-previous').addEventListener('click', function() { + if (state.pageIndex <= 0) return; + state.pageIndex -= 1; + renderOperationPage(); + updateSelection(); + }); + qs('openapi-import-page-next').addEventListener('click', function() { + state.pageIndex += 1; + renderOperationPage(); + updateSelection(); + }); + qs('openapi-import-plan-page-previous').addEventListener('click', function() { + if (state.planPageIndex <= 0 || !state.plan) return; + state.planPageIndex -= 1; + renderImportPlan(state.plan, true); + }); + qs('openapi-import-plan-page-next').addEventListener('click', function() { + if (!state.plan || state.planPageIndex + 1 >= Math.ceil((state.plan.candidates || []).length / OPERATIONS_PER_PAGE)) return; + state.planPageIndex += 1; + renderImportPlan(state.plan, true); + }); + qs('openapi-import-plan-reopen').addEventListener('click', revalidateCurrentPlan); document.querySelectorAll('[data-openapi-close]').forEach(function(node) { node.addEventListener('click', close); }); @@ -1082,7 +2664,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 +2673,32 @@ setStatus(tKey('openapi.status.context_changed')); } } - window.addEventListener('crank:langchange', function() { + function handleLanguageChange() { + // While an Apply owns the locked controls, re-rendering would re-enable + // them. Route straight to the context-change path, which marks the apply + // outcome unknown and keeps the controls locked. + if (state.applyInFlight || state.applyController || (state.applyOutcomeUnknown && state.applyRequest)) { + renderSecurityStatus(); invalidateForContext(); + return; + } + importOperations().forEach(function(operation) { + var row = operationRowFor(operation); + if (row) { + renderDecisionControls(operation, row); + renderSecurityControls(operation, row); + } }); + if (state.preview) renderOperationPage(); + if (!qs('openapi-import-modal').hidden && state.plan) { + renderImportPlan(state.plan); + setStatus(tKey('openapi.status.plan_ready')); + return; + } + updateSecuritySummary(); + 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 +2706,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 +2729,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 }); } @@ -1135,5 +2740,6 @@ open: open, close: close, reset: reset, + describeFinding: function(code) { return findingMessage({ code: code }); }, }; }()); diff --git a/apps/ui/js/wizard-live.js b/apps/ui/js/wizard-live.js index 9567015..d007f0e 100644 --- a/apps/ui/js/wizard-live.js +++ b/apps/ui/js/wizard-live.js @@ -1,6 +1,8 @@ var wizardOperationLoadGeneration = 0; var wizardLifecycleActionBusy = false; var wizardTestConfirmationToken = null; +var wizardSavedDraftPayload = null; +var wizardPinnedVersionStale = false; function operationErrorCode(error) { return error && error.payload && error.payload.error @@ -130,12 +132,33 @@ async function saveOperation(stayOnPage) { async function loadOperationForEdit() { if (!wizardWorkspaceId || !wizardEditId) return; var generation = ++wizardOperationLoadGeneration; + wizardCurrentOperation = null; + wizardCurrentVersion = null; + wizardSavedDraftPayload = null; + wizardPinnedVersionStale = false; + var pinnedVersionValue = new URLSearchParams(window.location.search).get('operationVersion'); + var pinnedVersion = null; + if (pinnedVersionValue !== null) { + pinnedVersion = Number(pinnedVersionValue); + if (!/^[0-9]{1,10}$/.test(pinnedVersionValue) || !Number.isSafeInteger(pinnedVersion) || pinnedVersion < 1) { + wizardPinnedVersionStale = true; + showWizardLiveStatus(tKey('wizard.stale.title'), tKey('wizard.stale.body'), true); + return; + } + } var requestedWorkspaceId = wizardWorkspaceId; var requestedOperationId = wizardEditId; var detail = await window.CrankApi.getOperation(requestedWorkspaceId, requestedOperationId); if (generation !== wizardOperationLoadGeneration || requestedWorkspaceId !== wizardWorkspaceId || requestedOperationId !== wizardEditId) return; + var detailDraftVersion = Number(detail.draft_version_ref && detail.draft_version_ref.version + || detail.current_draft_version); + if (pinnedVersion !== null && detailDraftVersion !== pinnedVersion) { + wizardPinnedVersionStale = true; + showWizardLiveStatus(tKey('wizard.stale.title'), tKey('wizard.stale.body'), true); + return; + } wizardProtocol = detail.protocol || 'rest'; await loadWizardPanels([3]); var draftVersion = await window.CrankApi.getOperationVersion( @@ -146,9 +169,24 @@ async function loadOperationForEdit() { if (generation !== wizardOperationLoadGeneration || requestedWorkspaceId !== wizardWorkspaceId || requestedOperationId !== wizardEditId) return; + if (pinnedVersion !== null) { + var currentDetail = await window.CrankApi.getOperation(requestedWorkspaceId, requestedOperationId); + if (generation !== wizardOperationLoadGeneration + || requestedWorkspaceId !== wizardWorkspaceId + || requestedOperationId !== wizardEditId) return; + var currentDraftVersion = Number(currentDetail.draft_version_ref && currentDetail.draft_version_ref.version + || currentDetail.current_draft_version); + if (currentDraftVersion !== pinnedVersion) { + wizardPinnedVersionStale = true; + showWizardLiveStatus(tKey('wizard.stale.title'), tKey('wizard.stale.body'), true); + return; + } + detail = currentDetail; + } wizardCurrentOperation = detail; wizardCurrentVersion = draftVersion; prefillWizardFromEdit(detail, draftVersion); + wizardSavedDraftPayload = updateOperationPayload(collectWizardPayload()); renderAgentFacingPreview(); renderImportQualityFindings(draftVersion); } @@ -375,6 +413,27 @@ function updateOperationPayload(payload) { }; } +function canonicalDraftPayload(payload) { + var editablePayload = updateOperationPayload(payload); + delete editablePayload.category; + + function sortKeys(value) { + if (Array.isArray(value)) return value.map(sortKeys); + if (!value || typeof value !== 'object') return value; + return Object.keys(value).sort().reduce(function(sorted, key) { + sorted[key] = sortKeys(value[key]); + return sorted; + }, {}); + } + + return JSON.stringify(sortKeys(editablePayload)); +} + +function draftPayloadMatchesBaseline(payload) { + return wizardSavedDraftPayload !== null + && canonicalDraftPayload(payload) === canonicalDraftPayload(wizardSavedDraftPayload); +} + async function refreshCurrentOperationState() { if (!wizardWorkspaceId || !wizardEditId) return null; var detail = await window.CrankApi.getOperation(wizardWorkspaceId, wizardEditId); @@ -396,6 +455,9 @@ async function refreshCurrentOperationState() { } async function persistCurrentDraft(stayOnPage) { + if (wizardPinnedVersionStale) { + throw new Error(tKey('wizard.stale.body')); + } if (!wizardWorkspaceId) { throw new Error(tKey('wizard.error.no_workspace')); } @@ -405,14 +467,33 @@ async function persistCurrentDraft(stayOnPage) { var createdNow = false; if (wizardMode === 'edit' && wizardEditId) { + if (draftPayloadMatchesBaseline(payload)) { + if (stayOnPage) { + var unchangedSaveDraftBtn = document.querySelector('.btn-save-draft'); + if (unchangedSaveDraftBtn) { + var unchangedLabel = unchangedSaveDraftBtn.textContent; + unchangedSaveDraftBtn.textContent = tKey('wizard.save.saved'); + setTimeout(function() { + unchangedSaveDraftBtn.textContent = unchangedLabel; + }, 1400); + } + } + return { + operation_id: wizardEditId, + version: currentDraftVersion(), + status: 'draft', + }; + } response = await window.CrankApi.updateOperation( wizardWorkspaceId, wizardEditId, updateOperationPayload(payload) ); + wizardSavedDraftPayload = updateOperationPayload(payload); } else { response = await window.CrankApi.createOperation(wizardWorkspaceId, payload); createdNow = true; + wizardSavedDraftPayload = updateOperationPayload(payload); wizardEditId = response.operation_id; wizardMode = 'edit'; history.replaceState( diff --git a/apps/ui/js/wizard-mapping.js b/apps/ui/js/wizard-mapping.js index dadadb0..2a1dfa9 100644 --- a/apps/ui/js/wizard-mapping.js +++ b/apps/ui/js/wizard-mapping.js @@ -253,7 +253,7 @@ var source = mappingEntrySource(parsed[outputField]); return { output: outputField, - responsePath: responseSourceToPath(source) || outputField, + responsePath: responseSourceToPath(source), }; }); } @@ -492,7 +492,7 @@ responseRows.forEach(function(row) { var responsePath = valueIn(row, 'responsePath'); var output = valueIn(row, 'output') || outputNameFromPath(responsePath); - if (!responsePath || !output) return; + if (!output) return; responseMapping[output] = joinJsonPath('$.response.body', responsePath); }); writeText('tool-output-mapping', dumpYaml(responseMapping)); diff --git a/apps/ui/js/wizard-upstreams.js b/apps/ui/js/wizard-upstreams.js index 4c54520..b8d0872 100644 --- a/apps/ui/js/wizard-upstreams.js +++ b/apps/ui/js/wizard-upstreams.js @@ -371,7 +371,7 @@ function startNewUpstream() { setValue('new-auth-profile-password-secret', ''); updateUpstreamAuthUi(); var first = form.querySelector('input'); - if (first) setTimeout(function() { first.focus(); }, 50); + if (first) first.focus(); } } diff --git a/apps/ui/nginx.conf b/apps/ui/nginx.conf index 68d8d8d..b7c457d 100644 --- a/apps/ui/nginx.conf +++ b/apps/ui/nginx.conf @@ -21,6 +21,16 @@ server { return 302 /; } + location ~ ^/api/admin/workspaces/[^/]+/imports/openapi/[^/]+/plans$ { + client_max_body_size 16m; + proxy_pass http://admin-api:3001; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + location /api/ { proxy_pass http://admin-api:3001; proxy_http_version 1.1; diff --git a/apps/ui/tests/e2e/onboarding.spec.js b/apps/ui/tests/e2e/onboarding.spec.js index deff609..1515e18 100644 --- a/apps/ui/tests/e2e/onboarding.spec.js +++ b/apps/ui/tests/e2e/onboarding.spec.js @@ -133,6 +133,59 @@ test('optional Getting Started checklist is server-derived, resumable, bilingual await expect(page.getByTestId('onboarding-checklist')).toHaveCSS('scroll-behavior', 'auto'); }); +test('onboarding return context drops unknown query secrets and bounds allowlisted navigation context', async ({ page }) => { + await login(page); + const workspace = await getCurrentWorkspace(page); + await routeOnboarding(page, async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(onboardingSnapshot(workspace.id)), + }); + }); + + await page.goto('/agents?onboarding=1&token=crk_return_secret_canary'); + if (!await page.getByTestId('onboarding-checklist').isVisible()) await page.getByTestId('onboarding-trigger').click(); + const returnContext = page.getByTestId('onboarding-return-context'); + await expect(returnContext).toHaveText('/agents?onboarding=1'); + await expect(returnContext).not.toContainText('crk_return_secret_canary'); + + await page.goto('/logs?log_id=' + 'a'.repeat(128) + '&agent_id=' + 'b'.repeat(128) + '&operation_id=' + 'c'.repeat(128)); + if (!await page.getByTestId('onboarding-checklist').isVisible()) await page.getByTestId('onboarding-trigger').click(); + await expect(page.getByTestId('onboarding-return-context')).toHaveText('/logs'); + await page.goto('/secrets?token=crk_return_secret_canary'); + if (!await page.getByTestId('onboarding-checklist').isVisible()) await page.getByTestId('onboarding-trigger').click(); + await expect(page.getByTestId('onboarding-return-context')).toHaveText('/secrets'); +}); + +test('a stale onboarding-pinned wizard version is rejected before loading or saving another Draft', async ({ page }) => { + await login(page); + const workspace = await getCurrentWorkspace(page); + const operationId = 'op_pinned'; + let versionReads = 0; + let writes = 0; + await page.route(`**/workspaces/${workspace.id}/operations/${operationId}`, async (route) => { + if (route.request().method() !== 'GET') writes += 1; + await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ + id: operationId, protocol: 'rest', current_draft_version: 3, + draft_version_ref: { version: 3 }, + }) }); + }); + await page.route(`**/operations/${operationId}/versions/*`, async (route) => { + versionReads += 1; + await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ version: 3 }) }); + }); + await page.goto(`/wizard/?mode=edit&operationId=${operationId}&operationVersion=2`); + await page.waitForFunction(() => window.CrankWizardReady === true); + await expect(page.locator('#wizard-live-status')).toBeVisible(); + await expect(page.locator('#tool-name')).not.toHaveValue('wrong newer Draft'); + await page.locator('.btn-save-draft').click(); + expect(versionReads).toBe(0); + expect(writes).toBe(0); + expect(await page.evaluate(() => window.wizardCurrentOperation)).toBeNull(); + +}); + test('onboarding error exposes safe support correlation and retries without a client completion bypass', async ({ page }) => { await login(page); const workspace = await getCurrentWorkspace(page); @@ -156,6 +209,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/); if (!await page.getByTestId('onboarding-checklist').isVisible()) { await page.getByTestId('onboarding-trigger').click(); } @@ -183,7 +237,7 @@ test('validation, upstream and compatibility errors recover from the last author ]; let failureIndex = -1; await routeOnboarding(page, async (route) => { - if (failureIndex >= 0) { + if (failureIndex >= 0 && route.request().method() === 'GET') { const failure = failures[failureIndex]; await route.fulfill({ status: failure.status, @@ -201,6 +255,7 @@ test('validation, upstream and compatibility errors recover from the last author }); await page.goto('/'); + await expect(page.getByTestId('onboarding-trigger')).toContainText(/3\s*\/\s*7/); if (!await page.getByTestId('onboarding-checklist').isVisible()) { await page.getByTestId('onboarding-trigger').click(); } @@ -237,7 +292,9 @@ test('authentication recovery returns to an incomplete authoritative onboarding }); await page.goto('/'); - await page.getByTestId('onboarding-trigger').click(); + if (!await page.getByTestId('onboarding-checklist').isVisible()) { + await page.getByTestId('onboarding-trigger').click(); + } rejectOnce = true; await page.getByTestId('onboarding-refresh').click(); await expect(page).toHaveURL(/\/login/); 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..847e1d3 --- /dev/null +++ b/apps/ui/tests/e2e/openapi-import-plan.spec.js @@ -0,0 +1,279 @@ +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(); + await page.locator('#openapi-import-partial-mode').check(); + 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/openapi-onboarding.spec.js b/apps/ui/tests/e2e/openapi-onboarding.spec.js new file mode 100644 index 0000000..186c8d5 --- /dev/null +++ b/apps/ui/tests/e2e/openapi-onboarding.spec.js @@ -0,0 +1,943 @@ +// Story 2.15 browser acceptance: OpenAPI branch in the guided onboarding. +// +// STATUS: ACTIVE. All leaves are activated and assert the behavior promised by the +// approved spec `_bmad-output/implementation-artifacts/spec-2-15-openapi-guided-onboarding.md`. +// +// Backend authority (immutable ImportPlan apply/replay, onboarding selection and +// event rollups, migration/schema guards) is covered by the Rust integration +// suites mapped in +// `_bmad-output/test-artifacts/atdd-checklist-2-15-openapi-guided-onboarding.md`. +// This file only covers user-observable browser behavior. + +const { test, expect } = require('@playwright/test'); +const { browserJson, getCurrentWorkspace, login, localized, uniqueName } = require('./helpers'); + +const SECRET_CANARY = 'crk_onboarding_raw_secret_canary'; +const REPRESENTATIVE_OPENAPI = [ + 'openapi: 3.0.3', + "info: { title: 'Onboarding representative document', version: '1' }", + 'servers: [{ url: http://127.0.0.1:3310 }]', + 'paths:', + ' /rates:', + ' get:', + ' operationId: listRates', + ' responses:', + " '200':", + ' description: JSON exchange rates', + ' content:', + ' application/json:', + ' schema:', + ' type: object', + ' properties:', + ' amount: { type: number }', + ' base: { type: string }', + ' date: { type: string }', + ' rates:', + ' type: object', + ' properties:', + ' EUR: { type: number }', + ' /health:', + ' get:', + ' operationId: getHealth', + ' responses:', + " '200':", + ' description: JSON service health', + ' content:', + ' application/json:', + ' schema:', + ' type: object', + ' properties:', + ' status: { type: string }', + '', +].join('\n'); + +function representativeOpenApi(suffix) { + const safeSuffix = suffix.replace(/[^a-zA-Z0-9_]/g, '_'); + return REPRESENTATIVE_OPENAPI + .replace('operationId: listRates', `operationId: listRates_${safeSuffix}`) + .replace('operationId: getHealth', `operationId: getHealth_${safeSuffix}`); +} + +function importContext(report, overrides = {}) { + return Object.assign({ + application_idempotency_key: report.idempotency_key, + plan_digest: report.plan_digest, + report, + choices: report.outcomes.map((outcome) => ({ + key: outcome.key, + name: outcome.name, + outcome: outcome.outcome, + operation_id: outcome.operation_id, + version: outcome.version, + reason: outcome.reason, + selectable: ['created', 'versioned', 'renamed'].includes(outcome.outcome), + })), + selected_operation_id: null, + selected_operation_version: null, + drift: null, + }, overrides); +} + +function registeredResponse(snapshot, context) { + return Object.assign({}, snapshot, { + registered: true, + revision: snapshot.revision + 1, + import_context: context, + }); +} + +function selectedResponse(snapshot, context, operationId, version) { + return Object.assign({}, snapshot, { + selected: true, + revision: snapshot.revision + 1, + operation_id: operationId, + operation_version: version, + import_context: Object.assign({}, context, { + selected_operation_id: operationId, + selected_operation_version: version, + }), + }); +} + +function onboardingSnapshot(workspaceId, overrides = {}) { + return { + schema_version: 1, + workspace_id: workspaceId, + revision: 7, + status: 'in_progress', + completed: false, + eligible_since: '2026-10-03T08:00:00Z', + steps: [ + { id: 'operation', completed: false, status: 'current', action_code: 'create_operation', reason_code: 'authoritative_evidence_missing' }, + { id: 'test', completed: false, status: 'pending', action_code: 'test_operation', reason_code: 'prerequisite_incomplete' }, + { id: 'publish_operation', completed: false, status: 'pending', action_code: 'publish_operation', reason_code: 'prerequisite_incomplete' }, + { id: 'agent', completed: false, status: 'pending', action_code: 'publish_agent', reason_code: 'prerequisite_incomplete' }, + { id: 'key', completed: false, status: 'pending', action_code: 'create_mcp_key', reason_code: 'prerequisite_incomplete' }, + { id: 'mcp_connection', completed: false, status: 'pending', action_code: 'connect_mcp_client', reason_code: 'prerequisite_incomplete' }, + { id: 'first_call', completed: false, status: 'pending', action_code: 'call_tool', reason_code: 'prerequisite_incomplete' }, + ], + operation_id: null, + operation_version: null, + agent_id: null, + catalog_revision: null, + platform_api_key_id: null, + mcp_endpoint: 'http://127.0.0.1:3302/mcp', + first_call: null, + ...overrides, + }; +} + +function candidate(name, overrides = {}) { + const draft = { + name, + display_name: name, + category: 'default', + target: { method: 'GET', path: '/' + name, server_url: 'https://api.example.test' }, + input_schema: { type: 'object', properties: {} }, + output_schema: { type: 'object', properties: {} }, + input_mapping: [], + output_mapping: [], + tool_description: {}, + wizard_state: null, + }; + return Object.assign( + { + key: 'GET /' + name, + method: 'GET', + path: '/' + name, + status: 'actionable', + operation_id: null, + suggested_name: name, + suggested_display_name: name, + description: 'ATDD candidate', + category: 'default', + input_fields: 0, + output_fields: 1, + server_urls: ['https://api.example.test'], + source_location: { pointer: '/paths/~1' + name + '/get' }, + findings: [], + security_requirements: { source: 'none', alternatives: [] }, + security_schemes: {}, + draft, + }, + overrides, + ); +} + +function previewResponse(operations, extra = {}) { + const base = { + job_id: 'imp_e2e_onboarding_215', + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + preview: { + source: { format: 'openapi', version: '3.0.3', title: 'Onboarding ATDD', servers: ['https://api.example.test'] }, + groups: [{ key: 'default', title: 'Default', operations }], + findings: [], + conflicts: [], + }, + security_profile_choices: [], + }; + return Object.assign(base, extra, { + preview: Object.assign(base.preview, extra.preview || {}), + }); +} + +function planResponse(candidates, overrides = {}) { + return Object.assign( + { + plan_reference: 'plan_e2e_onboarding_215', + plan_digest: 'a'.repeat(64), + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + selection_mode: 'full', + counts: { selected: candidates.length, excluded: 0, actionable: candidates.length, blocked: 0 }, + candidates, + }, + overrides, + ); +} + +function applyReport(outcomes, overrides = {}) { + return Object.assign( + { + schema_version: 'import-plan-apply-report-v1', + status: 'applied', + plan_id: 'plan_e2e_onboarding_215', + plan_digest: 'a'.repeat(64), + job_id: 'imp_e2e_onboarding_215', + idempotency_key: 'onboarding:apply:1', + source_id: 'src_e2e_onboarding', + source_digest: 'b'.repeat(64), + dependencies: [], + counts: { created: outcomes.length, versioned: 0, renamed: 0, skipped: 0, excluded: 0, rejected: 0 }, + outcomes, + summary: 'Plan applied', + recovery: 'none', + applied_at: '2026-10-03T08:05:00Z', + }, + overrides, + ); +} + +async function routeOnboarding(page, snapshot) { + await page.route( + /\/api\/admin\/workspaces\/[^/]+\/onboarding(?:\/events|\/reset-selection)?(?:\?.*)?$/, + async (route) => { + if (route.request().method() === 'GET') { + await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(snapshot) }); + return; + } + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(Object.assign({ accepted: true }, snapshot)), + }); + }, + ); +} + +async function routePreview(page, body, status = 200) { + await page.route(/\/imports\/openapi\/preview(?:\?.*)?$/, (route) => route.fulfill({ + status, + contentType: 'application/json', + body: JSON.stringify(body), + })); +} + +async function routePlanCreate(page, body, status = 200) { + await page.route(/\/imports\/openapi\/[^/]+\/plans(?:\?.*)?$/, (route) => route.fulfill({ + status, + contentType: 'application/json', + body: JSON.stringify(body), + })); +} + +async function routeApply(page, body, status = 200) { + await page.route(/\/imports\/openapi\/[^/]+\/apply(?:\?.*)?$/, async (route) => { + const requestBody = route.request().postDataJSON(); + const responseBody = typeof body === 'function' ? body(requestBody) : body; + await route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(responseBody) }); + }); +} + +async function openOnboarding(page) { + await login(page); + const workspace = await getCurrentWorkspace(page); + await routeOnboarding(page, onboardingSnapshot(workspace.id)); + await page.goto('/?onboarding=1'); + const checklist = page.getByTestId('onboarding-checklist'); + if (!(await checklist.isVisible())) { + await page.getByTestId('onboarding-trigger').click(); + } + await expect(checklist).toBeVisible(); + return workspace; +} + +async function chooseOpenApiPath(page) { + await page.getByTestId('onboarding-openapi-path').click(); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); +} + +async function uploadPreview(page, document = REPRESENTATIVE_OPENAPI) { + await page.locator('#openapi-import-file').setInputFiles({ + name: 'openapi.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(document), + }); + await page.locator('#openapi-import-preview').click(); +} + +function parseMcpResponse(response) { + return response.text().then((text) => { + const dataLine = text.split(/\r?\n/).find((line) => line.startsWith('data:')); + return JSON.parse(dataLine ? dataLine.slice(5).trim() : text); + }); +} + +async function mcpPost(request, endpoint, key, payload, sessionId) { + const headers = { + Authorization: `Bearer ${key}`, + Accept: 'application/json, text/event-stream', + 'Content-Type': 'application/json', + }; + if (sessionId) { + headers['MCP-Session-Id'] = sessionId; + headers['MCP-Protocol-Version'] = '2025-06-18'; + } + return request.post(endpoint, { headers, data: payload }); +} + +test.describe('Story 2.15 OpenAPI guided onboarding acceptance', () => { + test('[P1] AC-1 a protected non-Operations entry safely routes to canonical OpenAPI and preserves same-origin return context', async ({ page }) => { + await login(page); + const workspace = await getCurrentWorkspace(page); + await routeOnboarding(page, onboardingSnapshot(workspace.id)); + await page.goto('/agents?onboarding=1'); + const trigger = page.getByTestId('onboarding-trigger'); + if (!await trigger.isVisible()) await expect(trigger).toBeVisible(); + if (!await page.getByTestId('onboarding-checklist').isVisible()) await trigger.click(); + await expect(page.getByTestId('onboarding-openapi-path')).toBeVisible(); + expect(await page.evaluate(() => Boolean(window.CrankOpenApiImport))).toBe(false); + + // Without the Operations importer bundle, the visible onboarding entry + // routes to Operations with a bounded, same-origin return hint. + const operationsNavigation = page.waitForRequest((request) => { + const url = new URL(request.url()); + return request.isNavigationRequest() + && url.pathname === '/' + && url.searchParams.get('onboarding_openapi') === '1'; + }); + await page.getByTestId('onboarding-openapi-path').click(); + const navigation = await operationsNavigation; + expect(new URL(navigation.url()).origin).toBe(new URL(process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:3300').origin); + expect(new URL(page.url()).pathname).toBe('/'); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); + await expect(page.locator('#openapi-import-file')).toBeAttached(); + const returnHint = new URL(page.url()).searchParams.get('return'); + expect(returnHint).toBe('/agents?onboarding=1'); + expect(returnHint.length).toBeLessThanOrEqual(256); + const returnContext = await page.getByTestId('onboarding-return-context').textContent(); + expect(returnContext).toBe('/agents?onboarding=1'); + expect(returnContext).not.toContain('://'); + expect(new URL(page.url()).origin).toBe(new URL(process.env.PLAYWRIGHT_BASE_URL || 'http://127.0.0.1:3300').origin); + }); + + test('[P1] AC-2 guided preview shows methods, findings, the exact proposed Operation and apply consequences without hiding blockers', async ({ page }) => { + await openOnboarding(page); + const actionable = candidate('onboarding-rates'); + const blocked = candidate('onboarding-blocked', { + status: 'blocked', + findings: [{ code: 'unsupported_xml_body', severity: 'Error', message: 'XML bodies are unsupported', operation_key: 'GET /onboarding-blocked', source_location: { pointer: '/paths/~1onboarding-blocked/post' } }], + }); + await routePreview(page, previewResponse([actionable, blocked], { + preview: { findings: [{ code: 'unsupported_xml_body', severity: 'Error', message: 'XML bodies are unsupported', operation_key: blocked.key, source_location: { pointer: '/paths/~1onboarding-blocked/post' } }] }, + })); + await chooseOpenApiPath(page); + await uploadPreview(page); + + // Criterion-defining assertion: the exact proposed Operation is visible for the selected candidate. + await expect(page.locator('#openapi-import-groups')).toContainText(actionable.suggested_name); + await expect(page.locator('#openapi-import-groups')).toContainText('GET'); + await expect(page.locator('[data-testid="openapi-import-finding-code"]')).toContainText('unsupported_xml_body'); + // A blocker cannot be hidden to continue: the blocker summary is shown and + // creation stays unavailable until the finding is resolved. + await expect(page.locator('[data-testid="openapi-import-blockers"]')).toBeVisible(); + await expect(page.locator('#openapi-import-create')).toBeDisabled(); + }); + + test('[P1] AC-3 security mapping requires only a canonical Auth Profile reference and never stores a raw credential', async ({ page }) => { + await openOnboarding(page); + const workspace = await getCurrentWorkspace(page); + const profileName = uniqueName('onboarding_auth_profile'); + const secretValue = SECRET_CANARY + '_' + uniqueName('value'); + const secret = await browserJson( + page, + 'POST', + `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/secrets`, + { name: uniqueName('onboarding_secret'), kind: 'token', value: secretValue }, + ); + const profile = await browserJson( + page, + 'POST', + `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/auth-profiles`, + { + name: profileName, + kind: 'bearer', + config: { bearer: { header_name: 'Authorization', secret_id: secret.id } }, + }, + ); + await chooseOpenApiPath(page); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'onboarding-security.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from([ + 'openapi: 3.0.3', + `info: { title: ${profileName}, version: "1" }`, + 'servers: [{ url: "http://127.0.0.1:3310" }]', + 'paths:', + ' /rates:', + ' get:', + ' operationId: onboarding_secured_rates', + ' security:', + ' - bearerAuth: []', + ' responses:', + " '200': { description: OK }", + 'components:', + ' securitySchemes:', + ' bearerAuth: { type: http, scheme: bearer }', + ].join('\n')), + }); + const previewResponsePromise = page.waitForResponse((response) => response.request().method() === 'POST' && response.url().includes('/imports/openapi/preview')); + await page.locator('#openapi-import-preview').click(); + const previewResponseReal = await previewResponsePromise; + expect(previewResponseReal.status()).toBe(200); + const previewArtifact = await previewResponseReal.json(); + expect(JSON.stringify(previewArtifact)).not.toContain(secretValue); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + + // Criterion-defining assertion: the canonical Auth Profile picker is shown. + const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const bearerOption = alternative.locator('option', { hasText: 'bearerAuth' }); + await bearerOption.waitFor({ state: 'attached' }); + const bearerIndex = await bearerOption.getAttribute('value'); + expect(bearerIndex).toMatch(/^\d+$/); + await alternative.selectOption(bearerIndex); + const profilePicker = page.locator('#openapi-import-groups [data-openapi-security-profile]').first(); + await expect(profilePicker).toBeVisible(); + await profilePicker.selectOption(profile.id); + + const planRequest = page.waitForRequest((request) => request.method() === 'POST' && /\/imports\/openapi\/[^/]+\/plans$/.test(request.url())); + await page.locator('#openapi-import-create').click(); + const sentPlanRequest = await planRequest; + const planPayload = sentPlanRequest.postDataJSON(); + expect(planPayload.decisions[0]).toMatchObject({ auth_profile_id: profile.id }); + expect(planPayload.decisions[0].security_alternative_index).toEqual(expect.any(Number)); + const planResponseReal = await sentPlanRequest.response(); + expect(planResponseReal.ok(), await planResponseReal.text()).toBe(true); + const planArtifact = await planResponseReal.json(); + expect(JSON.stringify(planPayload)).not.toContain(secretValue); + + // The canonical artifact, onboarding projection and browser state carry the + // typed reference only. Secret material remains server-side. + const onboarding = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); + const browserState = await page.evaluate(() => JSON.stringify({ + local: Object.values(localStorage), + session: Object.values(sessionStorage), + url: location.href, + body: document.body.innerText, + })); + for (const value of [JSON.stringify(previewArtifact), JSON.stringify(planArtifact), JSON.stringify(onboarding), browserState, JSON.stringify(planPayload)]) { + expect(value).not.toContain(secretValue); + } + expect(JSON.stringify(planPayload)).toContain(profile.id); + expect(JSON.stringify(planArtifact)).toContain(profile.id); + await expect(page.locator('body')).not.toContainText(secretValue); + }); + + test('[P1] AC-4 after atomic apply of multiple Drafts the onboarding explicitly selects one actionable Draft and never auto-publishes', async ({ page }) => { + await openOnboarding(page); + const first = candidate('onboarding-rates'); + const second = candidate('onboarding-currencies'); + await routePreview(page, previewResponse([first, second])); + await routePlanCreate(page, planResponse([ + { key: first.key, name: first.suggested_name, state: 'actionable', action: 'create' }, + { key: second.key, name: second.suggested_name, state: 'actionable', action: 'create' }, + ])); + const outcomes = [ + { key: first.key, name: first.suggested_name, outcome: 'created', operation_id: 'op_onboarding_rates', version: 1, reason: null }, + { key: second.key, name: second.suggested_name, outcome: 'created', operation_id: 'op_onboarding_currencies', version: 1, reason: null }, + ]; + let report; + const initial = onboardingSnapshot((await getCurrentWorkspace(page)).id); + let context; + const registrationRequests = []; + const selectionRequests = []; + let latestOnboarding = initial; + await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding(?:\?.*)?$/, async (route) => { + if (route.request().method() !== 'GET') return route.fallback(); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(latestOnboarding) }); + }); + await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/import-context$/, async (route) => { + registrationRequests.push(route.request().postDataJSON()); + context = importContext(report); + const response = registeredResponse(initial, context); + latestOnboarding = response; + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(response) }); + }); + await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/select-draft$/, async (route) => { + const payload = route.request().postDataJSON(); + selectionRequests.push(payload); + const response = selectedResponse(latestOnboarding, context, payload.operation_id, payload.operation_version); + latestOnboarding = response; + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(response) }); + }); + await routeApply(page, (payload) => { + report = applyReport(outcomes, { + idempotency_key: payload.idempotency_key, + plan_digest: payload.plan_digest, + }); + return report; + }); + + const publishRequests = []; + page.on('request', (request) => { + if (/\/publish(?:es)?(?:\?|$)/.test(new URL(request.url()).pathname)) publishRequests.push(request.url()); + }); + + await chooseOpenApiPath(page); + await uploadPreview(page); + await page.locator('#openapi-import-create').click(); + await page.locator('[data-testid="openapi-import-plan-apply"]').click(); + + // Criterion-defining assertion: an explicit Draft chooser lists the created Drafts. + const chooser = page.getByTestId('onboarding-import-draft-choice'); + await expect(chooser).toBeVisible(); + await expect(chooser).toContainText(first.suggested_name); + await expect(chooser).toContainText(second.suggested_name); + // The full import report remains available and nothing is auto-published. + await expect(page.locator('#openapi-import-result')).toBeVisible(); + expect(publishRequests).toEqual([]); + await expect.poll(() => registrationRequests.length).toBe(1); + expect(registrationRequests[0]).toEqual({ + application_idempotency_key: report.idempotency_key, + plan_digest: report.plan_digest, + expected_revision: initial.revision, + }); + expect(context.report).toEqual(report); + + const chosen = page.waitForRequest((request) => request.method() === 'POST' && /\/onboarding\/select-draft$/.test(request.url())); + const firstChoice = chooser.locator('.onboarding-import-draft-option-row') + .filter({ hasText: first.suggested_name }).getByRole('button'); + await firstChoice.click(); + const selectedRequest = await chosen; + expect(selectedRequest.postDataJSON()).toEqual({ + application_idempotency_key: report.idempotency_key, + plan_digest: report.plan_digest, + operation_id: 'op_onboarding_rates', + operation_version: 1, + expected_revision: initial.revision + 1, + }); + }); + + test('[P1] AC-5 refresh or source correction restores the checklist from authoritative state without duplicating a version and requires a new preview for a changed digest', async ({ page }) => { + await login(page); + const workspace = await getCurrentWorkspace(page); + const pendingReport = applyReport([ + { key: 'GET /onboarding-rates', name: 'onboarding_rates', outcome: 'created', operation_id: 'op_imported_rates', version: 1, reason: null }, + { key: 'GET /onboarding-blocked', name: 'onboarding_blocked', outcome: 'rejected', operation_id: null, version: null, reason: 'unsupported_xml_body' }, + ], { + idempotency_key: 'ws_default:apply:openapi_onboarding:v1', + counts: { created: 1, versioned: 0, renamed: 0, skipped: 0, excluded: 0, rejected: 1 }, + }); + const imported = onboardingSnapshot(workspace.id, { + revision: 9, + operation_id: null, + operation_version: null, + import_context: importContext(pendingReport), + steps: [ + { id: 'operation', completed: false, status: 'current', action_code: 'create_operation', reason_code: 'authoritative_evidence_missing' }, + { id: 'test', completed: false, status: 'pending', action_code: 'test_operation', reason_code: 'prerequisite_incomplete' }, + { id: 'publish_operation', completed: false, status: 'pending', action_code: 'publish_operation', reason_code: 'prerequisite_incomplete' }, + { id: 'agent', completed: false, status: 'pending', action_code: 'publish_agent', reason_code: 'prerequisite_incomplete' }, + { id: 'key', completed: false, status: 'pending', action_code: 'create_mcp_key', reason_code: 'prerequisite_incomplete' }, + { id: 'mcp_connection', completed: false, status: 'pending', action_code: 'connect_mcp_client', reason_code: 'prerequisite_incomplete' }, + { id: 'first_call', completed: false, status: 'pending', action_code: 'call_tool', reason_code: 'prerequisite_incomplete' }, + ], + }); + await routeOnboarding(page, imported); + await page.goto('/?onboarding=1'); + + const checklist = page.getByTestId('onboarding-checklist'); + if (!(await checklist.isVisible())) await page.getByTestId('onboarding-trigger').click(); + await expect(checklist.locator('[aria-current="step"]')).toContainText(localized('Create an Operation', 'Создать операцию')); + await expect(page.getByTestId('onboarding-import-context')).toContainText('onboarding_rates'); + + // A full page refresh must recover the pending report and every outcome + // from the authoritative onboarding projection before explicit selection. + await page.reload(); + const resumed = page.getByTestId('onboarding-checklist'); + if (!(await resumed.isVisible())) await page.getByTestId('onboarding-trigger').click(); + await expect(resumed.locator('[aria-current="step"]')).toContainText(localized('Create an Operation', 'Создать операцию')); + await page.getByTestId('onboarding-openapi-path').click(); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); + await expect(page.locator('#openapi-import-result')).toBeVisible(); + await expect(page.locator('#openapi-import-result .openapi-import-plan-outcome[data-outcome="created"]')).toContainText('onboarding_rates'); + await expect(page.locator('#openapi-import-result .openapi-import-plan-outcome[data-outcome="rejected"]')).toContainText('onboarding_blocked'); + const restoredChooser = page.getByTestId('onboarding-import-draft-choice'); + await expect(restoredChooser).toContainText('onboarding_rates'); + await expect(restoredChooser).toContainText(localized( + 'This imported item needs review in the wizard.', + 'Этот импортированный элемент нужно проверить в мастере.', + )); + + // A changed source digest yields visible 409 recovery and disables Apply + // until a fresh preview/confirmation is created. + const resumeCandidate = candidate('onboarding-rates'); + await page.locator('[data-openapi-close]').last().click(); + await page.getByTestId('onboarding-openapi-path').click(); + await routePreview(page, previewResponse([resumeCandidate])); + await uploadPreview(page); + await routePlanCreate(page, planResponse([ + { key: resumeCandidate.key, name: resumeCandidate.suggested_name, state: 'actionable', action: 'create' }, + ], { plan_reference: 'plan_e2e_onboarding_215_fresh', plan_digest: 'c'.repeat(64) })); + 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(resumeCandidate.suggested_name); + await page.route(/\/imports\/openapi\/plans\/[^/]+(?:\?.*)?$/, (route) => route.fulfill({ + status: 409, + contentType: 'application/json', + body: JSON.stringify({ error: { code: 'import_plan_stale', message: 'Import source changed', context: { recovery: 'preview', reason: 'source_digest_changed' } } }), + })); + await page.locator('[data-testid="openapi-import-plan-reopen"]').click(); + await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toBeVisible(); + await expect(page.locator('[data-testid="openapi-import-plan-apply"]')).toBeDisabled(); + await expect(page.locator('#openapi-import-status')).not.toHaveText(''); + + await page.locator('[data-openapi-close]').last().click(); + await page.getByTestId('onboarding-openapi-path').click(); + await routePreview(page, previewResponse([resumeCandidate], { job_id: 'imp_e2e_onboarding_215_recovered' })); + await uploadPreview(page); + await routePlanCreate(page, planResponse([ + { key: resumeCandidate.key, name: resumeCandidate.suggested_name, state: 'actionable', action: 'create' }, + ], { plan_reference: 'plan_e2e_onboarding_215_recovered', plan_digest: 'd'.repeat(64) })); + 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(resumeCandidate.suggested_name); + await page.locator('[data-openapi-close]').last().click(); + await page.getByTestId('onboarding-openapi-path').click(); + const selectionRequests = []; + await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/select-draft$/, async (route) => { + const payload = route.request().postDataJSON(); + selectionRequests.push(payload); + if (selectionRequests.length === 1) { + await route.fulfill({ + status: 409, + contentType: 'application/json', + body: JSON.stringify({ error: { code: 'onboarding_stale_revision', message: 'Reload required', context: { recovery: 'reload' } } }), + }); + return; + } + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(selectedResponse(imported, importContext(pendingReport), payload.operation_id, payload.operation_version)) }); + }); + const continueButton = page.getByTestId('onboarding-import-draft-choice').getByRole('button').first(); + await continueButton.click(); + await expect(page.locator('#openapi-import-status')).toContainText(/reload|latest report|state changed|обнов|измен/i); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); + await expect(continueButton).toBeVisible(); + await continueButton.click(); + await page.waitForURL(/\/wizard\/\?mode=edit/); + expect(selectionRequests).toHaveLength(2); + for (const payload of selectionRequests) { + expect(payload).toEqual({ + application_idempotency_key: pendingReport.idempotency_key, + plan_digest: pendingReport.plan_digest, + operation_id: 'op_imported_rates', + operation_version: 1, + expected_revision: imported.revision, + }); + } + }); + + test('[P1] AC-6 presentation event requests carry only the bounded client contract and make no external calls', async ({ page }) => { + const requests = []; + page.on('request', (request) => requests.push({ url: request.url(), postData: request.postData() })); + const workspace = await openOnboarding(page); + const actionable = candidate('onboarding-rates'); + await routePreview(page, previewResponse([actionable])); + await routePlanCreate(page, planResponse([{ key: actionable.key, name: actionable.suggested_name, state: 'actionable', action: 'create' }])); + let report; + await routeApply(page, (payload) => { + report = applyReport([ + { key: actionable.key, name: actionable.suggested_name, outcome: 'created', operation_id: 'op_onboarding_rates', version: 1, reason: null }, + ], { idempotency_key: payload.idempotency_key, plan_digest: payload.plan_digest }); + return report; + }); + await page.route(/\/api\/admin\/workspaces\/[^/]+\/onboarding\/import-context$/, async (route) => { + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(registeredResponse(onboardingSnapshot(workspace.id), importContext(report))) }); + }); + + await chooseOpenApiPath(page); + await uploadPreview(page); + await page.locator('#openapi-import-create').click(); + await page.locator('[data-testid="openapi-import-plan-apply"]').click(); + + // Presentation requests are client-owned UI signals with a closed payload. + const eventPosts = requests.filter((item) => /\/onboarding\/events(?:\?|$)/.test(item.url)); + expect(eventPosts.length).toBeGreaterThan(0); + eventPosts.forEach((item) => { + const payload = JSON.parse(item.postData || '{}'); + expect(Object.keys(payload).sort()).toEqual(['event', 'expected_revision', 'idempotency_key']); + expect(payload.event).toMatch(/^(started|resumed|dismissed|abandoned)$/); + expect(payload.idempotency_key).toMatch(/^ui:/); + expect(typeof payload.expected_revision).toBe('number'); + expect(JSON.stringify(payload)).not.toContain('openapi: 3.0'); + expect(JSON.stringify(payload)).not.toContain(SECRET_CANARY); + expect(Object.keys(payload)).not.toContain('user_id'); + }); + // ...and keep all telemetry local unless the operator opts in (no external egress). + const external = requests.filter((item) => !/^https?:\/\/(?:127\.0\.0\.1|localhost)(?::\d+)?\//.test(item.url)); + expect(external).toEqual([]); + // Server-owned properties for all three funnel milestones, including the + // exact keys and raw-data exclusion, are asserted against stored PG rows by + // `crates/crank-registry/tests/integration/onboarding_import_selection.rs`. + }); + + for (const locale of ['ru', 'en']) { + test(`[P1] AC-7 ${locale} OpenAPI import reaches an authoritative public MCP call after selection reset`, async ({ page, request }) => { + test.setTimeout(180_000); + await login(page); + const workspace = await getCurrentWorkspace(page); + // The shared E2E admin workspace may have terminal evidence from an earlier + // suite. Reset only its canonical onboarding selection; every milestone + // below is recreated through the real import/UI/public endpoint journey. + let reset; + for (let attempt = 0; attempt < 5; attempt += 1) { + const prior = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); + reset = await browserJson( + page, + 'POST', + `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding/reset-selection`, + { expected_revision: prior.revision }, + [200, 409], + ); + if (!reset.error) break; + expect(reset.error.code).toBe('onboarding_stale_revision'); + } + expect(reset.error).toBeUndefined(); + const resetProjection = reset.onboarding || reset; + expect(resetProjection.status).not.toBe('complete'); + expect(resetProjection.import_context).toBeNull(); + expect(resetProjection.first_call).toBeNull(); + + const suffix = uniqueName('openapi_onboarding').replace(/_/g, '-'); + const agentSlug = `onboarding-${suffix}`.toLowerCase().replace(/[^a-z0-9-]/g, '-'); + + await page.goto('/?onboarding_openapi=1&return=%2F'); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); + await page.evaluate((language) => window.setLang(language), locale); + await expect(page.locator('#openapi-import-modal')).toContainText(locale === 'ru' ? 'Импорт OpenAPI' : 'Import OpenAPI'); + await uploadPreview(page, representativeOpenApi(suffix)); + + // Import preview, atomic apply of multiple candidates, and full report are + // all exercised against the real import API. + await expect(page.locator('#openapi-import-groups')).toContainText(/list.?rates/i); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + const importContextRequest = page.waitForRequest((r) => r.method() === 'POST' && /\/onboarding\/import-context$/.test(r.url())); + await page.locator('[data-testid="openapi-import-plan-apply"]').click(); + await expect(page.locator('#openapi-import-result')).toBeVisible(); + + const registration = await importContextRequest; + expect(registration.postDataJSON()).toMatchObject({ + application_idempotency_key: expect.any(String), + plan_digest: expect.stringMatching(/^[a-f0-9]{64}$/), + expected_revision: expect.any(Number), + }); + expect(await registration.response().then((response) => response.ok())).toBe(true); + + // Reload while the import context is still pending, then recover the full + // immutable report and every selectable/non-selectable outcome from GET. + await page.reload(); + const checklistAfterReload = page.getByTestId('onboarding-checklist'); + if (!await checklistAfterReload.isVisible()) await page.getByTestId('onboarding-trigger').click(); + await expect(page.getByTestId('onboarding-import-context')).toContainText(/list.?rates/i); + await page.getByTestId('onboarding-openapi-path').click(); + await expect(page.locator('#openapi-import-modal')).toBeVisible(); + await expect(page.locator('#openapi-import-result')).toBeVisible(); + await expect(page.locator('#openapi-import-result .openapi-import-plan-outcomes')).toContainText(/list.?rates/i); + const pendingContext = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); + expect(pendingContext.import_context.report.outcomes.length).toBeGreaterThan(1); + expect(pendingContext.import_context.selected_operation_id).toBeNull(); + expect(pendingContext.import_context.selected_operation_version).toBeNull(); + expect(pendingContext.import_context.choices).toHaveLength(2); + expect(pendingContext.import_context.choices.every((choice) => choice.selectable)).toBe(true); + expect(pendingContext.import_context.report.outcomes.map((outcome) => outcome.outcome).sort()).toEqual(['created', 'created']); + const selectedChoice = pendingContext.import_context.choices.find((choice) => /list.?rates/i.test(choice.name)); + expect(selectedChoice).toBeTruthy(); + + const chooser = page.getByTestId('onboarding-import-draft-choice'); + await expect(chooser).toBeVisible(); + const draftChoice = chooser.locator('.onboarding-import-draft-choice-list .onboarding-import-draft-option-row') + .filter({ hasText: /list.?rates/i }).getByRole('button'); + await expect(draftChoice).toBeVisible(); + const selectDraftRequest = page.waitForRequest((r) => r.method() === 'POST' && /\/onboarding\/select-draft$/.test(r.url())); + const selectedVersionResponse = page.waitForResponse((r) => r.request().method() === 'GET' + && r.url().includes(`/operations/${selectedChoice.operation_id}/versions/${selectedChoice.version}`)); + await draftChoice.click(); + const selection = await selectDraftRequest; + const loadedVersion = await selectedVersionResponse; + expect(loadedVersion.ok()).toBe(true); + const importedVersion = await loadedVersion.json(); + expect(importedVersion.snapshot.target).toMatchObject({ kind: 'rest', method: 'GET', path_template: '/rates', base_url: 'http://127.0.0.1:3310' }); + expect(selection.postDataJSON()).toMatchObject({ + application_idempotency_key: registration.postDataJSON().application_idempotency_key, + plan_digest: registration.postDataJSON().plan_digest, + operation_id: expect.any(String), + operation_version: expect.any(Number), + expected_revision: expect.any(Number), + }); + + await page.waitForFunction(() => window.CrankWizardReady === true && window.currentStep === 1); + await expect(page.locator('[data-testid="wizard-protocol-rest"]')).toBeVisible(); + const selectedOperationId = selection.postDataJSON().operation_id; + const selectedVersion = selection.postDataJSON().operation_version; + await expect(page).toHaveURL(new RegExp(`operationId=${selectedOperationId}`)); + expect(new URL(page.url()).searchParams.get('operationVersion')).toBe(String(selectedVersion)); + await expect(page.locator('#tool-name')).toHaveValue(/list.?rates/i); + const operationWrites = []; + page.on('request', (r) => { + const path = new URL(r.url()).pathname; + const createsOperation = r.method() === 'POST' && /\/api\/admin\/workspaces\/[^/]+\/operations$/.test(path); + const updatesOperation = r.method() === 'PATCH' && /\/api\/admin\/workspaces\/[^/]+\/operations\/[^/]+$/.test(path); + const createsVersion = r.method() === 'POST' && /\/api\/admin\/workspaces\/[^/]+\/operations\/[^/]+\/versions$/.test(path); + if (createsOperation || updatesOperation || createsVersion) operationWrites.push({ method: r.method(), url: r.url() }); + }); + // The selected imported Draft is edited in normal wizard edit mode. A + // no-op lifecycle save must retain its exact version without creating a + // duplicate, then test and publish that same version. + const testedVersions = []; + const publishedVersions = []; + page.on('request', (r) => { + if (r.method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/test-runs`)) { + testedVersions.push(r.postDataJSON().version); + } + if (r.method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/publish`)) { + publishedVersions.push(r.postDataJSON().version); + } + }); + const testResponse = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/test-runs`)); + await page.evaluate(() => window.CrankWizardShell.doGoToStep(5)); + await page.locator('#wizard-run-test').click(); + const tested = await testResponse; + expect(tested.ok(), await tested.text()).toBe(true); + const testResult = await tested.json(); + expect(testResult.ok, JSON.stringify(testResult)).toBe(true); + await expect.poll(() => testedVersions).toEqual([selectedVersion]); + await expect(page.locator('#wizard-test-request-id')).not.toHaveText(''); + + page.once('dialog', (dialog) => dialog.accept()); + const qualityResponse = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes('/operations/analyze-quality')); + const publishResponse = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes(`/operations/${selectedOperationId}/publish`)); + await page.locator('#wizard-publish-operation').click(); + const quality = await qualityResponse; + expect(quality.ok(), await quality.text()).toBe(true); + expect((await publishResponse).ok()).toBe(true); + await expect.poll(() => publishedVersions).toEqual([selectedVersion]); + expect(operationWrites).toEqual([]); + + const agentPage = `/agents?onboarding=1&action=create&operationId=${encodeURIComponent(selectedOperationId)}&operationVersion=${selectedVersion}`; + await page.goto(agentPage); + await expect(page.locator('.drawer')).toHaveClass(/open/); + const identityInputs = page.locator('.drawer-section').first().locator('input.form-input'); + await identityInputs.nth(0).fill('OpenAPI onboarding Agent'); + await identityInputs.nth(1).fill(agentSlug); + await page.locator('.drawer-section').first().locator('textarea').fill('Agent created from an explicitly selected OpenAPI Draft.'); + await expect(page.locator('.ops-picker-item.selected')).toContainText(/list.?rates/i); + const createAgentResponse = page.waitForResponse((r) => r.request().method() === 'POST' && /\/api\/admin\/workspaces\/[^/]+\/agents$/.test(r.url())); + const bindAgentResponse = page.waitForResponse((r) => r.request().method() === 'POST' && /\/agents\/[^/]+\/bindings$/.test(r.url())); + const publishAgentResponse = page.waitForResponse((r) => r.request().method() === 'POST' && /\/agents\/[^/]+\/publish$/.test(r.url())); + await page.locator('.drawer-footer .btn-primary-sm').click(); + const agentResponse = await createAgentResponse; + expect(agentResponse.ok(), await agentResponse.text()).toBe(true); + expect((await bindAgentResponse).ok()).toBe(true); + expect((await publishAgentResponse).ok()).toBe(true); + const agentId = (await agentResponse.json()).agent_id; + + await page.goto(`/api-keys?onboarding=1&action=create&agentId=${encodeURIComponent(agentId)}`); + await expect(page.locator('#agent-select')).toHaveValue(agentId); + await expect(page.locator('#modal-create')).toHaveClass(/open/); + await page.locator('#new-key-name').fill(uniqueName('openapi_onboarding_key')); + const keyResponsePromise = page.waitForResponse((r) => r.request().method() === 'POST' && /\/platform-api-keys$/.test(r.url())); + await page.locator('#modal-confirm-btn').click(); + const keyResponse = await keyResponsePromise; + expect(keyResponse.ok(), await keyResponse.text()).toBe(true); + const keyBody = await keyResponse.json(); + const rawKey = await page.locator('#reveal-key-value').textContent(); + const keyId = keyBody.api_key.id || keyBody.api_key.api_key.id; + const endpoint = await page.locator('#onboarding-connection-clients > code').textContent(); + expect(rawKey).toMatch(/^crk_/); + await expect(page.locator('body')).not.toContainText(SECRET_CANARY); + + // An unauthorized public call must not advance the authoritative checklist. + const denied = await mcpPost(request, endpoint, SECRET_CANARY, { + jsonrpc: '2.0', id: 90, method: 'tools/call', params: { name: 'unavailable', arguments: {} }, + }); + expect([401, 403]).toContain(denied.status()); + const beforeSuccessfulCall = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); + expect(beforeSuccessfulCall.status).not.toBe('complete'); + expect(beforeSuccessfulCall.first_call).toBeNull(); + + const initialize = await mcpPost(request, endpoint, rawKey, { + jsonrpc: '2.0', id: 1, method: 'initialize', params: { + protocolVersion: '2025-06-18', capabilities: {}, + clientInfo: { name: 'crank-openapi-onboarding-playwright', version: '1.0.0' }, + }, + }); + const initializeBody = await parseMcpResponse(initialize); + expect(initialize.ok(), JSON.stringify(initializeBody)).toBe(true); + const sessionId = initialize.headers()['mcp-session-id']; + expect(sessionId).toBeTruthy(); + const initialized = await mcpPost(request, endpoint, rawKey, { + jsonrpc: '2.0', method: 'notifications/initialized', params: {}, + }, sessionId); + expect([200, 202]).toContain(initialized.status()); + const listed = await mcpPost(request, endpoint, rawKey, { + jsonrpc: '2.0', id: 2, method: 'tools/list', params: {}, + }, sessionId); + expect(listed.ok()).toBe(true); + const listedBody = await parseMcpResponse(listed); + const selectedTool = listedBody.result.tools.find((tool) => /list.?rates/i.test(tool.name)); + expect(selectedTool).toBeTruthy(); + const toolName = selectedTool.name; + const called = await mcpPost(request, endpoint, rawKey, { + jsonrpc: '2.0', id: 3, method: 'tools/call', params: { name: toolName, arguments: {} }, + }, sessionId); + expect(called.ok()).toBe(true); + const calledBody = await parseMcpResponse(called); + expect(calledBody.error).toBeUndefined(); + expect(calledBody.result.isError).not.toBe(true); + + const progress = await browserJson(page, 'GET', `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/onboarding`); + expect(progress.status).toBe('complete'); + expect(progress.operation_id).toBe(selectedOperationId); + expect(progress.operation_version).toBe(selectedVersion); + expect(progress.agent_id).toBe(agentId); + expect(progress.platform_api_key_id).toBe(keyId); + expect(progress.first_call.agent_id).toBe(agentId); + expect(progress.first_call.key_id).toBe(keyId); + expect(progress.first_call.operation_id).toBe(selectedOperationId); + expect(progress.first_call.operation_version).toBe(selectedVersion); + expect(progress.first_call.tool_name).toBe(toolName); + expect(progress.first_call.request_id).toBeTruthy(); + expect(progress.first_call.trace_id).toMatch(/^[0-9a-f]{32}$/); + + await page.goto('/'); + await expect(page.getByTestId('onboarding-trigger')).toContainText(/7\s*\/\s*7/); + await page.getByTestId('onboarding-trigger').click(); + await expect(page.getByTestId('onboarding-completion')).toContainText(toolName); + await expect(page.getByTestId('onboarding-completion')).toContainText(progress.first_call.request_id); + await page.evaluate((language) => window.setLang(language), locale); + await expect(page.getByTestId('onboarding-completion')).toBeVisible(); + await page.reload(); + await expect(page.getByTestId('onboarding-trigger')).toContainText(/7\s*\/\s*7/); + }); + } +}); diff --git a/apps/ui/tests/e2e/openapi-preview-partial.spec.js b/apps/ui/tests/e2e/openapi-preview-partial.spec.js new file mode 100644 index 0000000..65698bc --- /dev/null +++ b/apps/ui/tests/e2e/openapi-preview-partial.spec.js @@ -0,0 +1,1184 @@ +// Story 2.12 acceptance tests for full preview and explicit partial mode. +// +// All mandatory UI leaves are active; backend-only contracts are verified in +// the Rust integration and unit suites. +// +// This file covers user-observable behavior; see +// `_bmad-output/test-artifacts/atdd-checklist-2-12-preview-partial-mode.md` +// for the complete AC-to-test evidence map. + +const { test, expect } = require('@playwright/test'); +const { getCurrentWorkspace, 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, paths) { + const lines = [ + 'openapi: 3.0.3', + "info: { title: 'Preview partial ATDD', version: '1' }", + 'servers: [{ url: https://api.example.test }]', + 'paths:', + ]; + for (const [path, operationId] of paths) { + lines.push(' /' + path + ':'); + lines.push(' get:'); + lines.push(' operationId: ' + operationId); + lines.push(' responses:'); + lines.push(" '200': { description: OK }"); + } + return lines.join('\n'); +} + +function candidate(prefix, suffix, overrides) { + return Object.assign( + { + key: 'GET /' + prefix + '-' + suffix, + method: 'GET', + path: '/' + prefix + '-' + suffix, + operation_id: prefix + '_' + suffix, + suggested_name: prefix + '_' + suffix, + suggested_display_name: prefix + ' ' + suffix, + description: 'ATDD candidate', + category: 'default', + input_fields: 0, + output_fields: 1, + server_urls: ['https://api.example.test'], + // Proposed Story 2.12 additions (not produced by the baseline API yet). + source_location: { pointer: '/paths/~1' + prefix + '-' + suffix + '/get' }, + findings: [], + security_requirements: { source: 'none', alternatives: [] }, + security_schemes: {}, + draft: { + name: prefix + '_' + suffix, + display_name: prefix + ' ' + suffix, + category: 'default', + target: { method: 'GET', path: '/' + prefix + '-' + suffix, server_url: 'https://api.example.test' }, + input_schema: { type: 'object', properties: {} }, + output_schema: { type: 'object', properties: {} }, + input_mapping: [], + output_mapping: [], + tool_description: {}, + wizard_state: null, + }, + }, + overrides || {}, + ); +} + +function previewResponse(prefix, operations, extra) { + return Object.assign( + { + job_id: 'imp_e2e_preview_partial', + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + preview: { + source: { + format: 'openapi', + version: '3.0.3', + title: 'Preview partial ATDD', + servers: ['https://api.example.test'], + }, + groups: [{ key: 'default', title: 'Default', operations }], + findings: [], + // Proposed Story 2.12 addition. + conflicts: [], + }, + security_profile_choices: [], + }, + extra || {}, + ); +} + +function planResponse(prefix, candidates, overrides) { + return Object.assign( + { + plan_reference: 'plan_e2e_preview_partial_212', + plan_digest: 'c'.repeat(64), + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + selection_mode: 'full', + counts: { selected: candidates.length, excluded: 0, actionable: candidates.length, blocked: 0 }, + candidates, + }, + overrides || {}, + ); +} + +async function routeOperationsEmpty(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 }), + }); + }); +} + +async function openImportModal(page, locale) { + 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(); +} + +async function uploadAndPreview(page, prefix, paths) { + await page.locator('#openapi-import-file').setInputFiles({ + name: 'preview-partial.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(importDocument(prefix, paths)), + }); + const previewResponseWaiter = page.waitForResponse((response) => + response.request().method() === 'POST' + && response.url().includes('/imports/openapi/preview'), + ); + await page.locator('#openapi-import-preview').click(); + await previewResponseWaiter; + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); +} + +// AC-1: preview must expose source locations and stable finding codes for +// every candidate, finding and conflict. Backend emission is owned by the +// Rust preview suite; this leaf asserts the UI renders what the API returns. +test('[P1] AC-1 preview renders source locations and stable finding codes for candidates and conflicts', async ({ page }) => { + const prefix = uniqueName('preview_location'); + const operations = [ + candidate(prefix, 'located', { + findings: [ + { + code: 'missing_operation_id', + severity: 'warning', + message: 'No operationId', + operation_key: 'GET /' + prefix + '-located', + source_location: { pointer: '/paths/~1' + prefix + '-located/get' }, + }, + ], + }), + ]; + const response = previewResponse(prefix, operations); + response.preview.conflicts = [ + { + operation_key: 'GET /' + prefix + '-located', + conflict_type: 'duplicate_name', + proposed_name: prefix + '_located_2', + source_location: { pointer: '/paths/~1' + prefix + '-located/get/operationId' }, + }, + ]; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(response), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [[prefix + '-located', prefix + '_located']]); + + const row = page.locator('.openapi-import-operation').first(); + await row.locator('.openapi-import-findings summary').click(); + await expect(row.locator('[data-testid="openapi-import-operation-location"]')).toContainText('/paths/'); + await expect(row.locator('[data-testid="openapi-import-finding-code"]')).toContainText('missing_operation_id'); + await expect(row.locator('[data-testid="openapi-import-finding-location"]')).toContainText('/paths/'); + await expect(row.locator('[data-testid="openapi-import-conflict"]')).toContainText('duplicate_name'); + await expect(row.locator('[data-testid="openapi-import-conflict-location"]')).toContainText('/operationId'); +}); + +// AC-4: duplicate/invalid/taken names must show a deterministic rename +// proposal and conflict type, and the snapshot must stay unchanged. +test('[P1] AC-4 preview shows conflict type and deterministic rename proposal without mutating the snapshot', async ({ page }) => { + const prefix = uniqueName('preview_naming'); + const operations = [candidate(prefix, 'clash', { suggested_name: 'shared_name' })]; + const response = previewResponse(prefix, operations); + const planCalls = []; + response.preview.conflicts = [ + { + operation_key: 'GET /' + prefix + '-clash', + conflict_type: 'taken_name', + proposed_name: 'shared_name_2', + source_location: { pointer: '/paths/~1' + prefix + '-clash/get' }, + }, + ]; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(response), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + planCalls.push(route.request().postDataJSON()); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, [{ + key: 'GET /' + prefix + '-clash', + name: 'reviewed_name', + state: 'actionable', + action: 'rename', + target_operation_id: null, + confirmed_name: 'reviewed_name', + auth_profile_id: null, + reason: null, + }], { selection_mode: 'full', counts: { selected: 1, excluded: 0, actionable: 1, blocked: 0 } })), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [[prefix + '-clash', prefix + '_clash']]); + + const conflict = page.locator('[data-testid="openapi-import-conflict"]').first(); + await expect(conflict).toContainText('taken_name'); + const proposal = page.locator('[data-testid="openapi-import-rename-proposal"]').first(); + await expect(proposal).toHaveValue('shared_name_2'); + await page.locator('[data-openapi-decision-action]').selectOption('rename'); + await proposal.fill('reviewed_name'); + // The original snapshot name is still shown verbatim next to the proposal. + await expect(page.locator('.openapi-import-operation').first()).toContainText('clash'); + expect(operations[0].suggested_name).toBe('shared_name'); + expect(operations[0].draft.name).toBe(prefix + '_clash'); + 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(); + expect(planCalls).toHaveLength(1); + expect(planCalls[0].decisions).toContainEqual(expect.objectContaining({ + operation_key: 'GET /' + prefix + '-clash', + action: 'rename', + confirmed_name: 'reviewed_name', + })); +}); + +// AC-6: any blocker in full mode forbids Apply for the whole document while +// showing the full blocker count and affected candidates. +test('[P1] AC-6 full mode disables Apply and lists blocker count and affected candidates', async ({ page }) => { + const prefix = uniqueName('preview_blocker'); + const operations = [ + candidate(prefix, 'valid'), + candidate(prefix, 'broken', { + findings: [ + { + code: 'unsupported_schema', + severity: 'error', + message: 'Unsupported schema', + operation_key: 'GET /' + prefix + '-broken', + source_location: { pointer: '/paths/~1' + prefix + '-broken/get' }, + }, + ], + }), + ]; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + const response = previewResponse(prefix, operations); + response.preview.findings = [{ code: 'unsupported_document', severity: 'error', message: 'Document blocker' }]; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(response), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, [ + { + key: 'GET /' + prefix + '-valid', + name: 'valid', + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + }, + { + key: 'GET /' + prefix + '-broken', + name: 'broken', + state: 'blocked', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: 'unsupported_schema', + }, + ], { + selection_mode: 'full', + counts: { selected: 2, excluded: 0, actionable: 1, blocked: 1 }, + })), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [ + [prefix + '-valid', prefix + '_valid'], + [prefix + '-broken', prefix + '_broken'], + ]); + await page.locator('#openapi-import-create').click(); + + const blockers = page.locator('[data-testid="openapi-import-blockers"]'); + await expect(blockers).toContainText('1'); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toContainText('broken'); + await expect(page.locator('#openapi-import-plan-apply')).toBeDisabled(); + await expect(page.locator('#openapi-import-partial-mode')).toBeDisabled(); + await expect(page.locator('[data-testid="openapi-import-apply-result"]')).toBeHidden(); +}); + +// AC-7: partial mode is enabled explicitly and accepts only an actionable, +// dependency-closed subset; excluded/blocked reasons stay visible. +test('[P1] AC-7 explicit partial mode submits only the actionable closed subset and shows excluded reasons', async ({ page }) => { + const prefix = uniqueName('preview_partial'); + const planCalls = []; + const operations = [ + candidate(prefix, 'keep'), + candidate(prefix, 'drop'), + candidate(prefix, 'blocked', { status: 'blocked' }), + candidate(prefix, 'typed_blocked'), + ]; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + const response = previewResponse(prefix, operations); + response.preview.findings = [{ + code: 'unsupported_schema', + severity: 'error', + operation_key: 'GET /' + prefix + '-typed_blocked', + }]; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(response), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + planCalls.push(route.request().postDataJSON()); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, [ + { + key: 'GET /' + prefix + '-keep', + name: prefix + '_keep', + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + }, + { + key: 'GET /' + prefix + '-drop', + name: prefix + '_drop', + state: 'excluded', + action: null, + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: 'not_selected', + }, + ], { + selection_mode: 'partial', + counts: { selected: 1, excluded: 1, actionable: 1, blocked: 0 }, + })), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [ + [prefix + '-keep', prefix + '_keep'], + [prefix + '-drop', prefix + '_drop'], + [prefix + '-blocked', prefix + '_blocked'], + [prefix + '-typed_blocked', prefix + '_typed_blocked'], + ]); + + const partialToggle = page.locator('#openapi-import-partial-mode'); + await expect(partialToggle).toBeVisible(); + await partialToggle.check(); + const blockedInput = page.locator('[data-openapi-operation][value="GET /' + prefix + '-blocked"]'); + await expect(blockedInput).toBeDisabled(); + const typedBlockedInput = page.locator('[data-openapi-operation][value="GET /' + prefix + '-typed_blocked"]'); + await expect(typedBlockedInput).toBeDisabled(); + await expect(partialToggle).toBeChecked(); + await page.locator('#openapi-import-select-visible').click(); + await expect(blockedInput).not.toBeChecked(); + await expect(typedBlockedInput).not.toBeChecked(); + for (const checkbox of await page.locator('[data-openapi-operation]').all()) { + const key = await checkbox.inputValue(); + if (!key.includes('-keep') && !(await checkbox.isDisabled())) await checkbox.uncheck(); + } + 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(); + + expect(planCalls).toHaveLength(1); + expect(planCalls[0].selection_mode).toBe('partial'); + expect(planCalls[0].selected_operation_keys).toEqual(['GET /' + prefix + '-keep']); + await expect(page.locator('[data-testid="openapi-import-excluded-reason"]')).toContainText('not_selected'); +}); + +// AC-8: a changed selection/name/mapping/policy creates a new immutable plan +// version with a new digest while the previous version stays readable. +test('[P1] AC-8 changed selection and rename decision create an immutable plan version; old plan stays readable', async ({ page }) => { + const prefix = uniqueName('preview_version'); + const operations = [candidate(prefix, 'one'), candidate(prefix, 'two')]; + const references = ['plan_previous_e2e', 'plan_current_e2e']; + const digests = ['d'.repeat(64), 'e'.repeat(64)]; + const planCalls = []; + const planReads = []; + let workspaceId = ''; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(previewResponse(prefix, operations)), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + const payload = route.request().postDataJSON(); + planCalls.push(payload); + workspaceId = decodeURIComponent(new URL(route.request().url()).pathname.match(/\/workspaces\/([^/]+)/)[1]); + const index = Math.min(planCalls.length - 1, digests.length - 1); + const planCandidates = index === 0 + ? operations.map((operation) => ({ + key: operation.key, + name: operation.suggested_name, + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + })) + : [ + { + key: operations[0].key, + name: prefix + '_renamed', + state: 'actionable', + action: 'rename', + target_operation_id: null, + confirmed_name: prefix + '_renamed', + auth_profile_id: null, + reason: null, + }, + { + key: operations[1].key, + name: operations[1].suggested_name, + state: 'excluded', + action: null, + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: 'not_selected', + }, + ]; + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, planCandidates, { + plan_reference: references[index], + plan_digest: digests[index], + selection_mode: index === 0 ? 'full' : 'partial', + counts: index === 0 + ? { selected: 2, excluded: 0, actionable: 2, blocked: 0 } + : { selected: 1, excluded: 1, actionable: 1, blocked: 0 }, + })), + }); + }); + await page.route(/\/imports\/openapi\/plans\/[^/]+$/, async (route) => { + const reference = decodeURIComponent(new URL(route.request().url()).pathname.split('/').pop()); + planReads.push(reference); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, operations.map((operation) => ({ + key: operation.key, + name: operation.suggested_name, + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + })), { + plan_reference: reference, + plan_digest: digests[0], + revalidation_status: 'current', + revalidation_required: false, + reason: null, + })), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [[prefix + '-one', prefix + '_one'], [prefix + '-two', prefix + '_two']]); + + 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-back').click(); + await page.locator('#openapi-import-partial-mode').check(); + await page.locator('[data-openapi-operation][value="GET /' + prefix + '-two"]').uncheck(); + await page.locator('[data-openapi-decision-action]').first().selectOption('rename'); + await page.locator('[data-openapi-decision-name]').first().fill(prefix + '_renamed'); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + + expect(planCalls).toHaveLength(2); + expect(planCalls[0].selected_operation_keys).toEqual(['GET /' + prefix + '-one', 'GET /' + prefix + '-two']); + expect(planCalls[1].selection_mode).toBe('partial'); + expect(planCalls[1].selected_operation_keys).toEqual(['GET /' + prefix + '-one']); + expect(planCalls[1].decisions).toContainEqual(expect.objectContaining({ + operation_key: 'GET /' + prefix + '-one', action: 'rename', confirmed_name: prefix + '_renamed', + })); + await expect(page.locator('[data-testid="openapi-import-plan-version"]')).toContainText('2'); + await expect(page.locator('[data-testid="openapi-import-plan-version"]')).toContainText(references[1]); + await expect(page.locator('[data-testid="openapi-import-plan-version"]')).toContainText(digests[1]); + + const previousPlan = await page.evaluate(async ({ workspace, reference }) => { + return window.CrankApi.revalidateOpenApiImportPlan(workspace, reference, {}); + }, { workspace: workspaceId, reference: references[0] }); + expect(planReads).toEqual([references[0]]); + expect(previousPlan.plan_reference).toBe(references[0]); + expect(previousPlan.plan_digest).toBe(digests[0]); + expect(previousPlan.candidates).toHaveLength(2); +}); + +// AC-10: re-opening a stored plan after catalog/Auth Profile drift must show +// stale/revalidation-required authority instead of a guaranteed Apply. +test('[P1] AC-10 reopens plans read-only, fails closed when stale/errors, and ignores an older GET response', async ({ page }) => { + const prefix = uniqueName('preview_stale'); + const planReference = 'plan_stale_e2e'; + const planDigest = 'f'.repeat(64); + const revalidationCalls = []; + let finishFirstRead; + const firstReadGate = new Promise((resolve) => { finishFirstRead = resolve; }); + let applyCalls = 0; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(previewResponse(prefix, [candidate(prefix, 'one')])), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, [ + { + key: 'GET /' + prefix + '-one', + name: prefix + '_one', + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + }, + ], { plan_reference: planReference, plan_digest: planDigest })), + }); + }); + await page.route(/\/imports\/openapi\/plans\/[^/]+$/, async (route) => { + const call = revalidationCalls.length + 1; + const reference = decodeURIComponent(new URL(route.request().url()).pathname.split('/').pop()); + revalidationCalls.push(reference); + if (call === 1) await firstReadGate; + if (call === 4) { + await route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ message: 'temporary read failure' }) }); + return; + } + const current = call === 2; + const response = planResponse(prefix, [{ + key: 'GET /' + prefix + '-one', + name: prefix + '_one', + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + }], { + plan_reference: reference, + plan_digest: planDigest, + revalidation_status: current ? 'current' : 'stale', + revalidation_required: !current, + reason: current ? null : 'catalog_changed', + }); + await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(response) }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/apply$/, async (route) => { + applyCalls += 1; + await route.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ message: 'Apply must not run' }) }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [[prefix + '-one', prefix + '_one']]); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + + const reopen = page.locator('[data-testid="openapi-import-plan-reopen"]'); + await expect(reopen).toHaveAccessibleName('Reopen and check current plan'); + const firstRequest = page.waitForRequest((request) => request.method() === 'GET' && request.url().includes('/imports/openapi/plans/' + planReference)); + await reopen.click(); + await firstRequest; + const secondResponse = page.waitForResponse((response) => response.request().method() === 'GET' && response.url().includes('/imports/openapi/plans/' + planReference)); + await reopen.click(); + await secondResponse; + await expect(page.locator('[data-testid="openapi-import-plan-revalidation-status"]')).toContainText(localized('current', 'актуален')); + await expect(page.locator('#openapi-import-plan-apply')).toBeEnabled(); + const firstResponse = page.waitForResponse((response) => response.request().method() === 'GET' && response.url().includes('/imports/openapi/plans/' + planReference)); + finishFirstRead(); + await firstResponse; + await expect(page.locator('[data-testid="openapi-import-plan-revalidation-status"]')).toContainText(localized('current', 'актуален')); + await expect(page.locator('#openapi-import-plan-apply')).toBeEnabled(); + + await reopen.click(); + await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toBeVisible(); + await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toContainText(localized('create a new plan', 'создайте новый план')); + await expect(page.locator('#openapi-import-plan-apply')).toBeDisabled(); + await reopen.click(); + await expect(page.locator('[data-testid="openapi-import-plan-revalidation-status"]')).toContainText(localized('could not be verified', 'Не удалось проверить')); + await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toContainText(localized('create a new plan', 'создайте новый план')); + await expect(page.locator('#openapi-import-plan-apply')).toBeDisabled(); + expect(revalidationCalls).toEqual([planReference, planReference, planReference, planReference]); + expect(applyCalls).toBe(0); +}); + +// AC-11: a large preview must render bounded pages over up to 2,000 candidates +// without an unbounded DOM, while search/filter/grouping and selection survive +// page changes. +test('[P1] AC-11 renders bounded pages for 2000 candidates and preserves selection across pages', async ({ page }, testInfo) => { + test.setTimeout(120_000); + const prefix = uniqueName('preview_large'); + const operations = []; + for (let index = 0; index < 2000; index += 1) { + operations.push(candidate(prefix, 'op_' + index)); + } + const planCalls = []; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + const response = previewResponse(prefix, operations); + response.preview.findings = [{ + code: 'document_notice', severity: 'warning', message: 'Document-wide sample finding', + }]; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(response), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + planCalls.push(route.request().postDataJSON()); + const candidates = operations.map((operation, index) => { + const excluded = index === 0 || index === 100; + return { + key: operation.key, + name: operation.suggested_name, + state: excluded ? 'excluded' : 'actionable', + action: excluded ? null : 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: excluded ? 'not_selected' : null, + }; + }); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, candidates, { + selection_mode: 'partial', + counts: { selected: 1998, excluded: 2, actionable: 1998, blocked: 0 }, + })), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [[prefix + '-op_0', prefix + '_op_0']]); + + const pager = page.locator('[data-testid="openapi-import-pagination"]'); + await expect(pager).toBeVisible(); + const rendered = page.locator('.openapi-import-operation'); + expect(await rendered.count()).toBeLessThanOrEqual(100); + await expect(page.locator('[data-testid="openapi-import-page-status"]')).toContainText('2000'); + + await page.locator('#openapi-import-partial-mode').check(); + const groupToggle = page.locator('.openapi-import-group [data-openapi-group]'); + await expect(page.locator('.openapi-import-group-toggle')).toContainText('Select group on this page'); + const russianLabel = await page.evaluate(() => { + localStorage.setItem('crank_lang', 'ru'); + const label = window.t('openapi.group.select'); + localStorage.setItem('crank_lang', 'en'); + return label; + }); + expect(russianLabel).toBe('Выбрать группу на этой странице'); + const firstPageFirst = page.locator('[data-openapi-operation]').first(); + const firstPageFirstKey = await firstPageFirst.inputValue(); + await firstPageFirst.uncheck(); + await groupToggle.check(); + await groupToggle.uncheck(); + await expect(page.locator('#openapi-import-selection')).toContainText('Selected: 1900'); + await pager.getByRole('button', { name: localized('Next', 'Далее') }).click(); + await expect(page.locator('[data-openapi-operation]').first()).toBeChecked(); + await pager.getByRole('button', { name: localized('Previous', 'Назад') }).click(); + await groupToggle.check(); + await firstPageFirst.uncheck(); + await pager.getByRole('button', { name: localized('Next', 'Далее') }).click(); + await expect(page.locator('[data-testid="openapi-import-page-status"]')).toContainText(localized('Page 2', 'Страница 2')); + const documentFindings = page.locator('.openapi-import-document-findings'); + await expect(documentFindings.locator('summary')).toContainText('Findings: 1'); + await documentFindings.locator('summary').click(); + await expect(documentFindings).toContainText('document_notice'); + expect(await rendered.count()).toBeLessThanOrEqual(100); + const secondPageFirst = page.locator('[data-openapi-operation]').first(); + const secondPageFirstKey = await secondPageFirst.inputValue(); + await secondPageFirst.uncheck(); + await pager.getByRole('button', { name: localized('Previous', 'Назад') }).click(); + await expect(page.locator(`[data-openapi-operation][value="${firstPageFirstKey}"]`)).not.toBeChecked(); + await pager.getByRole('button', { name: localized('Next', 'Далее') }).click(); + await expect(page.locator(`[data-openapi-operation][value="${secondPageFirstKey}"]`)).not.toBeChecked(); + + await page.locator('#openapi-import-search').fill('op_1999'); + await expect(page.locator('[data-openapi-operation]')).toHaveCount(1); + await expect(page.locator('[data-openapi-operation]').first()).toBeChecked(); + await expect(page.locator('[data-testid="openapi-import-page-status"]')).toContainText('1 matching operations'); + const previewMetrics = await page.evaluate(() => { + const search = document.querySelector('#openapi-import-search'); + const next = document.querySelector('#openapi-import-page-next'); + const previous = document.querySelector('#openapi-import-page-previous'); + const samples = { search_filter_ms: [], page_change_ms: [] }; + for (let index = 0; index < 20; index += 1) { + const startSearch = performance.now(); + search.value = 'op_' + (index * 10); + search.dispatchEvent(new Event('input', { bubbles: true })); + samples.search_filter_ms.push(Number((performance.now() - startSearch).toFixed(3))); + search.value = ''; + search.dispatchEvent(new Event('input', { bubbles: true })); + const startPage = performance.now(); + next.click(); + samples.page_change_ms.push(Number((performance.now() - startPage).toFixed(3))); + previous.click(); + } + return { + user_agent: navigator.userAgent, + platform: navigator.platform, + hardware_concurrency: navigator.hardwareConcurrency || null, + viewport: { width: innerWidth, height: innerHeight }, + search_filter_ms: samples.search_filter_ms, + page_change_ms: samples.page_change_ms, + }; + }); + await page.locator('#openapi-import-search').fill('op_1999'); + await expect(page.locator('[data-openapi-operation]')).toHaveCount(1); + await expect(page.locator('[data-openapi-operation]').first()).toBeChecked(); + await expect(page.locator('[data-testid="openapi-import-page-status"]')).toContainText('1 matching operations'); + await page.locator('#openapi-import-search').fill(''); + 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(); + expect(planCalls).toHaveLength(1); + expect(planCalls[0].selection_mode).toBe('partial'); + expect(planCalls[0].selected_operation_keys).toHaveLength(1998); + const planRows = page.locator('.openapi-import-plan-candidate'); + expect(await planRows.count()).toBeLessThanOrEqual(100); + await expect(page.locator('[data-testid="openapi-import-plan-page-status"]')).toContainText('2000'); + await page.locator('#openapi-import-plan-page-next').click(); + expect(await planRows.count()).toBeLessThanOrEqual(100); + await expect(planRows.first()).toContainText(prefix + '_op_100'); + const planMetrics = await page.evaluate(() => { + const next = document.querySelector('#openapi-import-plan-page-next'); + const previous = document.querySelector('#openapi-import-plan-page-previous'); + const samples = []; + for (let index = 0; index < 10; index += 1) { + let start = performance.now(); + previous.click(); + samples.push(Number((performance.now() - start).toFixed(3))); + start = performance.now(); + next.click(); + samples.push(Number((performance.now() - start).toFixed(3))); + } + return samples; + }); + await testInfo.attach('ac11-local-shared-host-metrics.json', { + body: Buffer.from(JSON.stringify({ + scope: 'LOCAL shared-host browser-handler samples; not Reference Deployment LAN evidence', + environment: { + user_agent: previewMetrics.user_agent, + platform: previewMetrics.platform, + hardware_concurrency: previewMetrics.hardware_concurrency, + viewport: previewMetrics.viewport, + }, + candidates: 2000, + max_operation_rows: 100, + max_plan_candidate_rows: 100, + samples: { + search_filter_ms: previewMetrics.search_filter_ms, + preview_page_change_ms: previewMetrics.page_change_ms, + plan_page_change_ms: planMetrics, + }, + }, null, 2)), + contentType: 'application/json', + }); + console.log('AC11_LOCAL_SHARED_HOST_METRICS ' + JSON.stringify({ + environment: { + user_agent: previewMetrics.user_agent, + platform: previewMetrics.platform, + hardware_concurrency: previewMetrics.hardware_concurrency, + viewport: previewMetrics.viewport, + }, + candidates: 2000, + max_operation_rows: 100, + max_plan_candidate_rows: 100, + samples: { + search_filter_ms: previewMetrics.search_filter_ms, + preview_page_change_ms: previewMetrics.page_change_ms, + plan_page_change_ms: planMetrics, + }, + })); +}); + +test('[P1] AC-11 keeps skipped apply outcomes accessible through bounded pages', async ({ page }) => { + const prefix = uniqueName('preview_skipped_pages'); + const operation = candidate(prefix, 'one'); + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(previewResponse(prefix, [operation])) }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/plans$/, async (route) => { + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, [{ + key: operation.key, + name: operation.suggested_name, + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + reason: null, + }], { counts: { selected: 1, excluded: 0, actionable: 1, blocked: 0 } })), + }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/apply$/, async (route) => { + const skipped = Array.from({ length: 250 }, (_, index) => ({ + operation_key: 'GET /' + prefix + '-skipped-' + index, + name: prefix + '_skipped_' + index, + reason: 'not_selected', + })); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ created: [], skipped, findings: [] }), + }); + }); + await openImportModal(page, 'en'); + await uploadAndPreview(page, prefix, [[prefix + '-one', prefix + '_one']]); + 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(); + + const skipped = page.locator('.openapi-import-result-skipped'); + await expect(skipped.locator('summary')).toContainText('250'); + await skipped.locator('summary').click(); + await expect(skipped.locator('li')).toHaveCount(100); + await expect(skipped.locator('[role="status"]')).toContainText('Page 1 of 3'); + await skipped.getByRole('button', { name: localized('Next', 'Далее') }).click(); + await expect(skipped.locator('li').first()).toContainText(prefix + '_skipped_100'); + await expect(skipped.locator('li')).toHaveCount(100); +}); + +// AC-12: RU/EN upload states (uploading, parsing, loading, empty/no-methods, +// error, stale, confirmation) plus a request-generation guard that prevents an +// older preview from overwriting a newer file or selection. +test('[P1] AC-12 exposes RU/EN live import states and discards stale preview/revalidation responses', async ({ page }) => { + const prefix = uniqueName('preview_states'); + const firstStaleCandidate = candidate(prefix, 'stale_file'); + const actionable = candidate(prefix, 'actionable', { status: 'actionable' }); + const blocked = candidate(prefix, 'blocked', { status: 'blocked' }); + const operations = [actionable, blocked]; + let releaseFirstPreview; + let finishFirstPreview; + let releaseOperations; + let releaseSelectionRead; + let releaseCurrentPreview; + let releaseRuPreview; + let releaseRuOperations; + let gateRuOperations = false; + const firstPreviewGate = new Promise((resolve) => { releaseFirstPreview = resolve; }); + const operationsGate = new Promise((resolve) => { releaseOperations = resolve; }); + const selectionReadGate = new Promise((resolve) => { releaseSelectionRead = resolve; }); + const currentPreviewGate = new Promise((resolve) => { releaseCurrentPreview = resolve; }); + const ruPreviewGate = new Promise((resolve) => { releaseRuPreview = resolve; }); + const ruOperationsGate = new Promise((resolve) => { releaseRuOperations = resolve; }); + const firstPreviewFinished = new Promise((resolve) => { finishFirstPreview = resolve; }); + let previewCalls = 0; + let planCalls = 0; + let revalidationCalls = 0; + let staleApplyCalls = 0; + await routeOperationsEmpty(page); + await page.route('**/api/admin/workspaces/*/imports/openapi/preview', async (route) => { + previewCalls += 1; + if (previewCalls === 1) { + await firstPreviewGate; + try { + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(previewResponse(prefix, [firstStaleCandidate])) }); + } catch (_error) { + // The selected file replacement aborts this older request. + } finally { + finishFirstPreview(); + } + return; + } + if (previewCalls === 2 || previewCalls === 5) { + if (previewCalls === 2) await currentPreviewGate; + if (previewCalls === 5) await ruPreviewGate; + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(previewResponse(prefix, operations)) }); + return; + } + const code = previewCalls === 4 || previewCalls === 7 ? 'openapi_upload.no_methods' : 'openapi_upload.invalid_document'; + await route.fulfill({ + status: 400, + contentType: 'application/json', + body: JSON.stringify({ error: { code, message: code } }), + }); + }); + await page.route('**/api/admin/workspaces/*/operations', async (route) => { + if (gateRuOperations) await ruOperationsGate; + else await operationsGate; + 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 += 1; + const second = planCalls > 1; + const candidates = second + ? [{ key: actionable.key, name: actionable.suggested_name, state: 'actionable', action: 'create', target_operation_id: null, confirmed_name: null, auth_profile_id: null, reason: null }, + { key: blocked.key, name: blocked.suggested_name, state: 'excluded', action: null, target_operation_id: null, confirmed_name: null, auth_profile_id: null, reason: 'not_selected' }] + : operations.map((operation) => ({ key: operation.key, name: operation.suggested_name, state: operation.status, action: operation.status === 'actionable' ? 'create' : null, target_operation_id: null, confirmed_name: null, auth_profile_id: null, reason: operation.status === 'blocked' ? 'unsupported_schema' : null })); + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify(planResponse(prefix, candidates, { + plan_reference: 'plan_ac12_' + planCalls, + plan_digest: String(planCalls).padStart(64, 'a'), + selection_mode: second ? 'partial' : 'full', + counts: second + ? { selected: 1, excluded: 1, actionable: 1, blocked: 0 } + : { selected: 2, excluded: 0, actionable: 1, blocked: 1 }, + })), + }); + }); + await page.route(/\/imports\/openapi\/plans\/[^/]+$/, async (route) => { + revalidationCalls += 1; + const stale = { plan_reference: 'plan_ac12_1', plan_digest: '1'.padStart(64, 'a'), revalidation_status: 'stale', revalidation_required: true, reason: 'catalog_changed', counts: { selected: 2, excluded: 0, actionable: 1, blocked: 1 }, candidates: [] }; + if (revalidationCalls === 1) { + await selectionReadGate; + await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(stale) }); + return; + } + const currentPlan = 'plan_ac12_' + planCalls; + await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(Object.assign({}, stale, { + plan_reference: currentPlan, + plan_digest: String(planCalls).padStart(64, 'a'), + })) }); + }); + await page.route(/\/imports\/openapi\/[^/]+\/apply$/, async (route) => { + staleApplyCalls += 1; + await route.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ message: 'Apply must not run' }) }); + }); + await openImportModal(page, 'en'); + + await page.locator('#openapi-import-file').setInputFiles({ + name: 'states-a.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(importDocument(prefix, [[prefix + '-stale_file', prefix + '_stale_file']])), + }); + const firstPreview = page.waitForRequest((request) => request.method() === 'POST' && request.url().includes('/imports/openapi/preview')); + const previewButton = page.locator('#openapi-import-preview'); + await previewButton.focus(); + await expect(previewButton).toBeFocused(); + await previewButton.press('Enter'); + const status = page.locator('#openapi-import-status'); + await expect(status).toHaveAttribute('role', 'status'); + await expect(status).toHaveAttribute('aria-live', 'polite'); + await page.evaluate(() => { + const node = document.querySelector('#openapi-import-status'); + window.__openapiStatusHistory = [node.textContent]; + new MutationObserver(() => window.__openapiStatusHistory.push(node.textContent)) + .observe(node, { childList: true, characterData: true, subtree: true }); + }); + await expect(status).toContainText('Uploading'); + await firstPreview; + + // Replace the file while the first preview is still in flight. + await page.locator('#openapi-import-file').setInputFiles({ + name: 'states-current.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(importDocument(prefix, [[prefix + '-actionable', prefix + '_actionable'], [prefix + '-blocked', prefix + '_blocked']])), + }); + const currentPreview = page.waitForResponse((response) => response.request().method() === 'POST' && response.url().includes('/imports/openapi/preview')); + await page.locator('#openapi-import-preview').click(); + await expect(status).toContainText('Uploading'); + releaseCurrentPreview(); + await currentPreview; + await expect(status).toContainText('Checking existing workspace operations'); + releaseOperations(); + await expect(status).toContainText('Document is ready'); + const enStatusHistory = await page.evaluate(() => window.__openapiStatusHistory); + expect(enStatusHistory.some((message) => message.includes('Parsing the OpenAPI document'))).toBe(true); + await expect(page.locator('[data-testid="openapi-import-operation-readiness"][data-state="actionable"]')).toHaveAccessibleName('Actionable'); + await expect(page.locator('[data-testid="openapi-import-operation-readiness"][data-state="blocked"]')).toHaveAccessibleName('Blocked'); + releaseFirstPreview(); + await firstPreviewFinished; + await expect(status).toContainText('Document is ready'); + await expect(page.locator('.openapi-import-operation').first()).not.toContainText('stale_file'); + + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await expect(status).toContainText('Review the server plan'); + const reopen = page.locator('#openapi-import-plan-reopen'); + await reopen.click(); + await page.locator('#openapi-import-plan-back').click(); + await page.locator('#openapi-import-partial-mode').check(); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-plan-version"]')).toContainText('plan_ac12_2'); + releaseSelectionRead(); + await expect(page.locator('[data-testid="openapi-import-plan-version"]')).toContainText('plan_ac12_2'); + await expect(page.locator('[data-testid="openapi-import-plan-revalidation-status"]')).toContainText('current'); + expect(planCalls).toBe(2); + + await reopen.click(); + await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toBeVisible(); + await expect(page.locator('#openapi-import-plan-apply')).toBeDisabled(); + await page.locator('#openapi-import-plan-back').click(); + + await page.locator('#openapi-import-file').setInputFiles({ name: 'invalid.yaml', mimeType: 'application/yaml', buffer: Buffer.from('openapi: [') }); + await page.locator('#openapi-import-preview').click(); + await expect(status).toContainText(localized('not a valid', 'некоррект')); + await page.locator('#openapi-import-file').setInputFiles({ name: 'empty.yaml', mimeType: 'application/yaml', buffer: Buffer.from('') }); + await expect(status).toContainText(localized('empty', 'пуст')); + await page.locator('#openapi-import-file').setInputFiles({ name: 'large.yaml', mimeType: 'application/yaml', buffer: Buffer.alloc(256 * 1024 + 1, 32) }); + await expect(status).toContainText('256'); + await page.locator('#openapi-import-file').setInputFiles({ name: 'no-methods.yaml', mimeType: 'application/yaml', buffer: Buffer.from("openapi: 3.0.3\ninfo: {title: Empty, version: '1'}\npaths: {}\n") }); + await page.locator('#openapi-import-preview').click(); + await expect(status).toContainText(localized('does not contain importable methods', 'нет поддерживаемых методов')); + expect(previewCalls).toBe(4); + expect(staleApplyCalls).toBe(0); + + await page.evaluate(() => window.setLang('ru')); + gateRuOperations = true; + await page.locator('#openapi-import-file').setInputFiles({ + name: 'states-ru.yaml', mimeType: 'application/yaml', + buffer: Buffer.from(importDocument(prefix, [[prefix + '-actionable', prefix + '_actionable'], [prefix + '-blocked', prefix + '_blocked']])), + }); + const ruPreview = page.waitForResponse((response) => response.request().method() === 'POST' && response.url().includes('/imports/openapi/preview')); + await page.locator('#openapi-import-preview').click(); + await expect(status).toContainText('Загружаю'); + releaseRuPreview(); + await ruPreview; + await expect(status).toContainText('Проверяю существующие операции'); + releaseRuOperations(); + await expect(status).toContainText('Документ готов'); + const ruStatusHistory = await page.evaluate(() => window.__openapiStatusHistory); + expect(ruStatusHistory.some((message) => message.includes('Разбираю документ OpenAPI'))).toBe(true); + await expect(page.locator('[data-testid="openapi-import-operation-readiness"][data-state="actionable"]')).toHaveAccessibleName('Можно импортировать'); + await expect(page.locator('[data-testid="openapi-import-operation-readiness"][data-state="blocked"]')).toHaveAccessibleName('Заблокировано'); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-testid="openapi-import-apply-confirmation"]')).toBeVisible(); + await expect(status).toContainText('Проверьте план сервера'); + await page.locator('#openapi-import-plan-reopen').click(); + await expect(page.locator('[data-testid="openapi-import-plan-stale"]')).toContainText('создайте новый план'); + await expect(page.locator('#openapi-import-plan-apply')).toBeDisabled(); + await page.locator('#openapi-import-plan-back').click(); + await page.locator('#openapi-import-file').setInputFiles({ name: 'empty-ru.yaml', mimeType: 'application/yaml', buffer: Buffer.from('') }); + await expect(status).toContainText('пуст'); + await page.locator('#openapi-import-file').setInputFiles({ name: 'large-ru.yaml', mimeType: 'application/yaml', buffer: Buffer.alloc(256 * 1024 + 1, 32) }); + await expect(status).toContainText('256'); + await page.locator('#openapi-import-file').setInputFiles({ name: 'invalid-ru.yaml', mimeType: 'application/yaml', buffer: Buffer.from('openapi: [') }); + await page.locator('#openapi-import-preview').click(); + await expect(status).toContainText('не является корректным'); + await page.locator('#openapi-import-file').setInputFiles({ name: 'no-methods-ru.yaml', mimeType: 'application/yaml', buffer: Buffer.from("openapi: 3.0.3\ninfo: {title: Empty, version: '1'}\npaths: {}\n") }); + await page.locator('#openapi-import-preview').click(); + await expect(status).toContainText('нет методов для импорта'); + expect(previewCalls).toBe(7); + expect(revalidationCalls).toBe(3); +}); + +// AC-13: end-to-end upload regression across valid, invalid, empty, oversized +// and no-method documents, with no Operations created before Apply. +test('[P1] AC-13 real upload round-trip validates YAML and rejects invalid inputs without Operation mutations', async ({ page }) => { + const prefix = uniqueName('preview_upload'); + await openImportModal(page, 'en'); + const workspace = await getCurrentWorkspace(page); + expect(workspace && workspace.id).toBeTruthy(); + const readOperations = async () => page.evaluate(async (workspaceId) => window.CrankApi.listOperations(workspaceId), workspace.id); + const operationSignature = (result) => ({ + total: result.total, + items: result.items.map((item) => ({ id: item.id, name: item.name, operation_id: item.operation_id, version: item.version })), + }); + const baseline = operationSignature(await readOperations()); + let previewPosts = 0; + let operationPosts = 0; + page.on('request', (request) => { + const url = new URL(request.url()); + if (request.method() === 'POST' && url.pathname.endsWith('/imports/openapi/preview')) previewPosts += 1; + if (request.method() === 'POST' && /\/operations\/?$/.test(url.pathname)) operationPosts += 1; + }); + + // A YAML key containing quotes exercises operation-key rendering without CSS-selector interpolation. + const specialPath = '/' + prefix + '-"quoted"-path'; + const validYaml = [ + 'openapi: 3.0.3', + "info: { title: 'Preview local round trip', version: '1' }", + "servers: [{ url: 'https://api.example.test' }]", + 'paths:', + " '" + specialPath + "':", + ' post:', + ' operationId: ' + prefix + '_special', + ' responses:', + " '200': { description: OK }", + '', + ].join('\n'); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'valid.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(validYaml), + }); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + const row = page.locator('.openapi-import-operation').first(); + await expect(row).toContainText(specialPath); + await expect(row).toContainText('POST'); + await expect(page.locator('[data-openapi-operation]').first()).toBeChecked(); + await expect(row.locator('[data-testid="openapi-import-operation-readiness"]')).toHaveAccessibleName('Actionable'); + + // Malformed documents are rejected by the real preview endpoint. + await page.locator('#openapi-import-file').setInputFiles({ + name: 'invalid.yaml', mimeType: 'application/yaml', buffer: Buffer.from('openapi: [') + }); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-status')).toContainText(localized('not a valid', 'некоррект')); + + // Empty and oversized files are rejected locally and must not POST previews. + await page.locator('#openapi-import-file').setInputFiles({ + name: 'empty.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from(''), + }); + await expect(page.locator('#openapi-import-status')).toContainText(localized('empty', 'пуст')); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-status')).toContainText(localized('empty', 'пуст')); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'oversized.yaml', mimeType: 'application/yaml', buffer: Buffer.alloc(256 * 1024 + 1, 32), + }); + await expect(page.locator('#openapi-import-status')).toContainText('256'); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-status')).toContainText('256'); + + // A syntactically valid document with no supported methods reaches the real endpoint. + await page.locator('#openapi-import-file').setInputFiles({ + name: 'no-methods.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from("openapi: 3.0.3\ninfo: { title: Empty, version: '1' }\npaths: {}\n"), + }); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-status')).toContainText(localized('does not contain importable methods', 'нет поддерживаемых методов')); + + expect(previewPosts).toBe(3); + expect(operationPosts).toBe(0); + const after = operationSignature(await readOperations()); + expect(after).toEqual(baseline); +}); diff --git a/apps/ui/tests/e2e/openapi-preview-reference.spec.js b/apps/ui/tests/e2e/openapi-preview-reference.spec.js new file mode 100644 index 0000000..e17cae8 --- /dev/null +++ b/apps/ui/tests/e2e/openapi-preview-reference.spec.js @@ -0,0 +1,273 @@ +// Operator-run AC-11 evidence against a Reference Deployment. This spec is +// intentionally separate from the local/mocked Playwright matrix. +const fs = require('node:fs'); +const { test, expect } = require('@playwright/test'); +const { localized, login } = require('./helpers'); + +// Remote operator credentials must not enter retry artifacts or failure captures. +test.use({ trace: 'off', video: 'off', screenshot: 'off' }); +test.describe.configure({ retries: 0 }); + +const referenceBaseUrl = process.env.PLAYWRIGHT_BASE_URL; +const SAMPLE_COUNT = 20; +const OPERATION_COUNT = 2000; +const MAX_UPLOAD_BYTES = 256 * 1024; +const MAX_P95_MS = 2000; + +function openApiDocument(operationCount) { + const lines = [ + 'openapi: 3.0.3', + 'info:', + ' title: Crank Reference Preview Benchmark', + ' version: "1.0.0"', + 'servers:', + ' - url: https://api.example.test', + 'paths:', + ]; + + for (let index = 0; index < operationCount; index += 1) { + lines.push(` /op-${index}:`); + lines.push(' get:'); + lines.push(' responses:'); + lines.push(' "200":'); + lines.push(' description: OK'); + } + + return Buffer.from(lines.join('\n') + '\n', 'utf8'); +} + +function percentile95(samples) { + const sorted = [...samples].sort((left, right) => left - right); + return sorted[Math.ceil(sorted.length * 0.95) - 1]; +} + +function safeReferenceOrigin(rawUrl) { + const url = new URL(rawUrl); + if (!['http:', 'https:'].includes(url.protocol)) { + throw new Error('PLAYWRIGHT_BASE_URL must use http or https'); + } + if (url.username || url.password || url.search || url.hash) { + throw new Error('PLAYWRIGHT_BASE_URL must not contain credentials, query parameters, or fragments'); + } + if (['localhost', '127.0.0.1', '::1'].includes(url.hostname)) { + throw new Error('AC-11 Reference benchmark requires a remote PLAYWRIGHT_BASE_URL'); + } + return url.origin; +} + +function readReferenceManifest(manifestPath) { + if (!manifestPath) { + throw new Error('CRANK_AC11_REFERENCE_ENV_JSON is required when PLAYWRIGHT_BASE_URL is set'); + } + + let manifest; + try { + manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); + } catch (_error) { + throw new Error('CRANK_AC11_REFERENCE_ENV_JSON must point to a readable valid JSON manifest'); + } + + const deployment = manifest && manifest.deployment; + const loadGenerator = manifest && manifest.load_generator; + const browser = manifest && manifest.browser; + const nonempty = (value) => typeof value === 'string' && value.trim().length > 0; + const finiteNumber = (value) => typeof value === 'number' && Number.isFinite(value); + const versionHasMajor = (value, major) => nonempty(value) && new RegExp(`^${major}(?:\\.|$)`).test(value.trim()); + + if (!deployment || !loadGenerator || !browser) { + throw new Error('Reference manifest requires deployment, load_generator, and browser objects'); + } + for (const [name, host] of [['deployment', deployment], ['load_generator', loadGenerator], ['browser', browser]]) { + if (!nonempty(host.host_id)) throw new Error(`Reference manifest ${name}.host_id is required`); + } + if (deployment.host_id.trim() === loadGenerator.host_id.trim() + || deployment.host_id.trim() === browser.host_id.trim()) { + throw new Error('Reference manifest requires deployment and load generator/browser on separate hosts'); + } + if (!nonempty(deployment.cpu_model) || !nonempty(deployment.storage_type) + || deployment.storage_type.trim().toLowerCase() !== 'ssd' + || !finiteNumber(deployment.cpu_cores) + || !finiteNumber(deployment.ram_gib) || !finiteNumber(deployment.ssd_iops) + || !finiteNumber(deployment.ssd_throughput_mb_s) || !finiteNumber(deployment.ssd_capacity_gb) + || !finiteNumber(deployment.lan_mbps) + || !versionHasMajor(deployment.postgres_version, 16) + || !versionHasMajor(deployment.valkey_version, 8)) { + throw new Error('Reference manifest is missing valid CPU, RAM, database, SSD, or LAN fields'); + } + if (deployment.cpu_cores !== 4 || deployment.ram_gib !== 8 + || deployment.ssd_capacity_gb < 100 || deployment.ssd_iops < 3000 + || deployment.ssd_throughput_mb_s < 100 + || deployment.lan_mbps < 1000) { + throw new Error('Reference deployment must meet 4 CPU / 8 GiB RAM, SSD 3000 IOPS / 100 MB/s, and 1 Gbit LAN'); + } + if (!finiteNumber(loadGenerator.cpu_cores) || !finiteNumber(loadGenerator.ram_gib) + || loadGenerator.cpu_cores !== 4 || loadGenerator.ram_gib !== 8) { + throw new Error('Reference load_generator must have 4 CPU and 8 GiB RAM'); + } + + // Emit only the fields needed for benchmark interpretation. Do not copy + // arbitrary manifest keys (which could contain credentials) into reports. + return { + provenance: 'operator_manifest', + deployment: { + cpu_model: deployment.cpu_model.trim(), + cpu_cores: deployment.cpu_cores, + ram_gib: deployment.ram_gib, + postgres_version: deployment.postgres_version.trim(), + valkey_version: deployment.valkey_version.trim(), + storage_type: 'SSD', + ssd_capacity_gb: deployment.ssd_capacity_gb, + ssd_iops: deployment.ssd_iops, + ssd_throughput_mb_s: deployment.ssd_throughput_mb_s, + lan_mbps: deployment.lan_mbps, + }, + load_generator: { cpu_cores: loadGenerator.cpu_cores, ram_gib: loadGenerator.ram_gib }, + browser_host_separate_from_deployment: true, + }; +} + +test('[P1][reference] AC-11 measures 20 real 2000-operation preview round trips', async ({ page }, testInfo) => { + test.skip(!referenceBaseUrl, 'Set PLAYWRIGHT_BASE_URL to a Reference Deployment to run AC-11 evidence'); + test.setTimeout(300_000); + + expect(process.env.PLAYWRIGHT_SKIP_WEB_SERVER).toBe('1'); + const referenceOrigin = safeReferenceOrigin(referenceBaseUrl); + const referenceManifest = readReferenceManifest(process.env.CRANK_AC11_REFERENCE_ENV_JSON); + const yaml = openApiDocument(OPERATION_COUNT); + expect(yaml.byteLength).toBeLessThan(MAX_UPLOAD_BYTES); + + await login(page); + await page.waitForFunction(() => Boolean(document.querySelector('[data-testid="onboarding-dismiss"]'))); + await page.locator('[data-testid="onboarding-dismiss"]').click(); + await expect(page.locator('#crank-onboarding-panel')).toBeHidden(); + await page.evaluate(() => window.setLang('en')); + 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: 'reference-preview-2000.yaml', + mimeType: 'application/yaml', + buffer: yaml, + }); + + const previewButton = page.locator('#openapi-import-preview'); + const createButton = page.locator('#openapi-import-create'); + const rows = page.locator('.openapi-import-operation'); + const samplesMs = []; + let browserEnvironment; + + // Warm the preview once before timing. Warm-up still uses the real remote + // preview API. + const warmupResponsePromise = page.waitForResponse((response) => + response.request().method() === 'POST' + && response.url().includes('/imports/openapi/preview'), + ); + await previewButton.click(); + const warmupResponse = await warmupResponsePromise; + expect(warmupResponse.status(), 'warm-up preview status').toBe(200); + expect(new URL(warmupResponse.url()).origin).toBe(referenceOrigin); + await expect(page.locator('[data-testid="openapi-import-page-status"]')).toContainText('2000'); + await expect(createButton).toBeEnabled(); + + const previewResponses = []; + page.on('response', (response) => { + if (response.request().method() === 'POST' && response.url().includes('/imports/openapi/preview')) { + previewResponses.push({ status: response.status(), url: response.url() }); + } + }); + + for (let sampleIndex = 0; sampleIndex < SAMPLE_COUNT; sampleIndex += 1) { + await expect(previewButton).toBeEnabled(); + await page.evaluate((index) => { + const measurements = window.__ac11Measurements || (window.__ac11Measurements = []); + const sample = { start: null, elapsed: null }; + measurements[index] = sample; + const ready = () => { + const panel = document.querySelector('#openapi-import-preview-panel'); + const button = document.querySelector('#openapi-import-preview'); + const status = document.querySelector('[data-testid="openapi-import-page-status"]'); + const create = document.querySelector('#openapi-import-create'); + const rowCount = document.querySelectorAll('.openapi-import-operation').length; + if (sample.start === null || !button || button.disabled || !panel || panel.hidden + || !status || !status.textContent.includes('2000') || !create || create.disabled + || rowCount < 1 || rowCount > 100) return; + sample.elapsed = Number((performance.now() - sample.start).toFixed(3)); + observer.disconnect(); + }; + const observer = new MutationObserver(ready); + observer.observe(document.body, { childList: true, subtree: true, attributes: true, characterData: true }); + document.addEventListener('click', (event) => { + if (event.target.closest('#openapi-import-preview')) sample.start = performance.now(); + }, { capture: true, once: true }); + }, sampleIndex); + const responsePromise = page.waitForResponse((response) => + response.request().method() === 'POST' + && response.url().includes('/imports/openapi/preview'), + ); + await previewButton.click(); + const previewResponse = await responsePromise; + expect(previewResponse.status(), `preview sample ${sampleIndex + 1} status`).toBe(200); + expect(new URL(previewResponse.url()).origin).toBe(referenceOrigin); + + await page.waitForFunction((index) => { + const sample = window.__ac11Measurements && window.__ac11Measurements[index]; + return sample && sample.elapsed !== null; + }, sampleIndex, { polling: 'raf' }); + const elapsed = await page.evaluate((index) => window.__ac11Measurements[index].elapsed, sampleIndex); + + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + await expect(page.locator('[data-testid="openapi-import-page-status"]')).toContainText('2000'); + await expect(rows.first()).toBeVisible(); + expect(await rows.count()).toBeGreaterThan(0); + expect(await rows.count()).toBeLessThanOrEqual(100); + await expect(createButton).toBeEnabled(); + + samplesMs.push(elapsed); + browserEnvironment = await page.evaluate(() => ({ + user_agent: navigator.userAgent, + platform: navigator.platform, + language: navigator.language, + hardware_concurrency: navigator.hardwareConcurrency || null, + viewport: { width: innerWidth, height: innerHeight }, + timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, + })); + } + + expect(previewResponses).toHaveLength(SAMPLE_COUNT); + expect(previewResponses.every((item) => item.status === 200)).toBe(true); + const p95Ms = percentile95(samplesMs); + const report = { + benchmark: 'story-2.12-ac-11-reference-preview', + generated_at: new Date().toISOString(), + result: { sample_count: samplesMs.length, p95_ms: p95Ms, threshold_ms: MAX_P95_MS }, + workload: { + operations: OPERATION_COUNT, + yaml_bytes: yaml.byteLength, + max_yaml_bytes: MAX_UPLOAD_BYTES, + preview_http_statuses: previewResponses.map((item) => item.status), + rendered_operation_rows: await rows.count(), + max_rendered_operation_rows: 100, + enabled_interaction: 'Create button enabled after preview', + interaction_enabled: true, + measurement_scope: 'click through real HTTP request, parsing, populated first page, and enabled Create button', + }, + samples_ms: samplesMs, + environment: { + reference_origin: referenceOrigin, + playwright_base_url_configured: true, + playwright_skip_web_server: process.env.PLAYWRIGHT_SKIP_WEB_SERVER === '1', + node_version: process.version, + os_platform: process.platform, + os_arch: process.arch, + browser: browserEnvironment, + reference_manifest: referenceManifest, + }, + }; + + const rawReport = JSON.stringify(report, null, 2); + await testInfo.attach('ac11-reference-preview-report.json', { + body: Buffer.from(rawReport), + contentType: 'application/json', + }); + console.log('AC11_REFERENCE_PREVIEW_REPORT ' + JSON.stringify(report)); + expect(p95Ms, 'Reference preview p95 must be at most 2 seconds').toBeLessThanOrEqual(MAX_P95_MS); +}); diff --git a/apps/ui/tests/e2e/openapi-security-mapping.spec.js b/apps/ui/tests/e2e/openapi-security-mapping.spec.js new file mode 100644 index 0000000..92bcfe3 --- /dev/null +++ b/apps/ui/tests/e2e/openapi-security-mapping.spec.js @@ -0,0 +1,979 @@ +const { test, expect } = require('@playwright/test'); +const { browserJson, getCurrentWorkspace, login, localized, uniqueName } = require('./helpers'); + +const operationKey = 'get:/protected'; +const profileId = 'auth_profile_safe_bearer'; +const secretCanary = 'SECRET_CANARY_do_not_render_or_store'; + +function securityPreview() { + return { + job_id: 'job_security_mapping_atdd', + expires_at: '2099-01-01T00:00:00Z', + preview: { + source: { servers: ['https://api.example.test'] }, + findings: [], + groups: [{ + title: 'protected operations', + operations: [{ + key: operationKey, + method: 'GET', + path: '/protected', + suggested_name: 'protected_operation', + suggested_display_name: 'Protected operation', + input_fields: 0, + output_fields: 0, + draft: { input_mapping: { rules: [] }, output_mapping: { rules: [] } }, + findings: [{ + code: 'unsupported_security_scheme', + severity: 'warning', + message: 'Unsupported scheme details are intentionally opaque', + construct_id: 'security_scheme_unsupported', + location: { path: '/paths/~1protected/get/security' }, + operation_key: operationKey, + }], + security_requirements: { + source: 'document', + alternatives: [ + { schemes: ['bearerAuth'], supported: true, findings: [] }, + { schemes: [], supported: true, findings: [] }, + { schemes: ['oauthAuth', 'apiKeyAuth'], supported: false, findings: ['unsupported_security_and_requirement'] }, + ], + }, + }], + }], + }, + security_profile_choices: [ + { + operation_key: operationKey, + alternative_index: 0, + schemes: ['bearerAuth'], + profiles: [{ id: profileId, name: 'Production bearer', kind: 'bearer' }], + }, + ], + }; +} + +function protectedOperation(key, suggestedName, alternatives) { + return { + key, + method: 'GET', + path: '/' + key.split('/').pop(), + suggested_name: suggestedName, + suggested_display_name: suggestedName, + input_fields: 0, + output_fields: 0, + draft: { input_mapping: { rules: [] }, output_mapping: { rules: [] } }, + findings: [], + security_requirements: { source: 'document', alternatives }, + }; +} + +function previewWith(operations, choices) { + return { + job_id: 'job_security_multi_atdd', + expires_at: '2099-01-01T00:00:00Z', + preview: { + source: { servers: ['https://api.example.test'] }, + findings: [], + groups: [{ title: 'protected operations', operations }], + }, + security_profile_choices: choices, + }; +} + +async function dismissOnboardingIfOpen(page) { + // Same harness hygiene as openapi-import-plan.spec.js and operations.spec.js: + // the overlay intercepts pointer events on a fresh E2E stack. + 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(); +} + +async function openSecurityPreview(page, locale = 'en', previewPayload = securityPreview()) { + // Register the preview boundary before navigation so the upload is deterministic. + await page.route('**/imports/openapi/preview', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(previewPayload), + }); + }); + + 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 page.locator('#openapi-import-file').setInputFiles({ + name: 'security-mapping.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from('openapi: 3.0.3\ninfo: { title: Security mapping, version: "1" }\npaths: {}\n'), + }); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); +} + +function actionablePlan(decision) { + return { + plan_reference: 'plan_security_mapping_atdd', + plan_digest: 'c'.repeat(64), + expires_at: '2099-01-01T00:00:00Z', + selection_mode: 'full', + counts: { selected: 1, excluded: 0, actionable: 1, blocked: 0 }, + candidates: [{ + key: operationKey, + name: 'protected_operation', + state: 'actionable', + action: decision.action, + target_operation_id: null, + confirmed_name: null, + auth_profile_id: decision.auth_profile_id || null, + security_alternative_index: decision.security_alternative_index, + reason: null, + }], + }; +} + +function containsCredentialField(value) { + if (!value || typeof value !== 'object') return false; + return Object.entries(value).some(([key, nested]) => + /^(password|access_token|refresh_token|secret_value|credential_value|raw_credentials?)$/i.test(key) + || containsCredentialField(nested)); +} + +test('[P1] AC-1 sends the selected OR alternative and Auth Profile, then renders API authority', async ({ page }) => { + // Given an OpenAPI preview with OR security choices and safe workspace profile metadata + const planRequests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + planRequests.push(payload); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(actionablePlan(payload.decisions[0])) }); + }); + await openSecurityPreview(page); + + // When an administrator selects a supported alternative and its compatible profile + await page.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await page.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + await page.locator('#openapi-import-create').click(); + + // Then the existing plan API receives only the typed profile reference and alternative index + await expect.poll(() => planRequests.length).toBe(1); + expect(planRequests[0].decisions[0]).toMatchObject({ + operation_key: operationKey, + auth_profile_id: profileId, + security_alternative_index: 0, + }); + await expect(page.getByTestId('openapi-security-status')).toHaveText(localized('Resolved', 'Разрешено')); + expect(JSON.stringify(planRequests[0])).not.toContain(secretCanary); + expect(containsCredentialField(planRequests[0])).toBe(false); +}); + +test('[P1] AC-2 replaces a prior resolved plan with a blocker after profile revalidation fails', async ({ page }) => { + // Given an API-confirmed resolved plan for a profile that becomes unavailable before revalidation + const planRequests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + planRequests.push(payload); + if (planRequests.length === 1) { + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(actionablePlan(payload.decisions[0])) }); + return; + } + await route.fulfill({ + status: 422, + contentType: 'application/json', + body: JSON.stringify({ error: { code: 'import_plan_auth_unavailable', message: secretCanary } }), + }); + }); + await openSecurityPreview(page); + await page.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await page.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + + // When the API first confirms the selection and then rejects the stale profile + await page.locator('#openapi-import-create').click(); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeVisible(); + await expect(page.getByTestId('openapi-security-status')).toHaveText(localized('Resolved', 'Разрешено')); + await page.locator('#openapi-import-plan-back').click(); + await page.locator('#openapi-import-create').click(); + + // Then the old resolved value is replaced by the exact API-authoritative blocker state + await expect.poll(() => planRequests.length).toBe(2); + await expect(page.getByTestId('openapi-security-status')).toHaveText(localized('Blocked', 'Заблокировано')); + await expect(page.locator('body')).not.toContainText(secretCanary); + expect(planRequests[0].decisions[0]).toMatchObject({ auth_profile_id: profileId, security_alternative_index: 0 }); +}); + +test('[P1] AC-3 shows safe security guidance in RU and EN without exposing error or secret canaries', async ({ page }) => { + // Given safe profile metadata and an unsupported AND alternative in the preview + let previewCalls = 0; + await page.route('**/imports/openapi/preview', async (route) => { + previewCalls += 1; + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(securityPreview()) }); + }); + await page.route('**/imports/openapi/*/plans', async (route) => { + await route.fulfill({ + status: 422, + contentType: 'application/json', + body: JSON.stringify({ error: { code: 'import_plan_security_alternative_required', message: secretCanary } }), + }); + }); + await login(page); + await dismissOnboardingIfOpen(page); + await page.evaluate(() => window.setLang('ru')); + await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'security-mapping.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from('openapi: 3.0.3\ninfo: { title: Security mapping, version: "1" }\npaths: {}\n'), + }); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + + // When the administrator inspects both locales, the unsupported AND alternative is visible but disabled + // Native option elements are not visually visible inside a select; the safe + // metadata must be present and selectable. + await expect(page.getByLabel(localized('Auth Profile', 'Профиль авторизации')).locator('option[value="' + profileId + '"]')).toBeAttached(); + await expect(page.getByLabel(localized('Security alternative', 'Вариант безопасности')).locator('option[value="2"]')).toHaveAttribute('disabled', ''); + await page.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await page.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + await page.locator('#openapi-import-create').click(); + await expect(page.getByTestId('openapi-security-status')).toHaveText(localized('Blocked', 'Заблокировано')); + await expect(page.locator('body')).not.toContainText(secretCanary); + await page.evaluate(() => window.setLang('en')); + await expect(page.locator('#openapi-import-retry')).toBeVisible(); + await page.locator('#openapi-import-retry').click(); + await expect.poll(() => previewCalls).toBe(2); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + // A fresh preview resets the selection, so the operation is required again, + // never a leftover unresolved/resolved binding from the rejected plan. + await expect(page.getByTestId('openapi-security-status')).toHaveText( + localized('Authentication required', 'Требуется аутентификация'), + ); + await expect(page.locator('body')).not.toContainText(secretCanary); + const exposed = await page.evaluate(() => ({ + html: document.documentElement.innerText, + local: JSON.stringify(localStorage), + session: JSON.stringify(sessionStorage), + })); + expect(JSON.stringify(exposed)).not.toContain(secretCanary); +}); + +/* ── Mandatory matrix branches (green phase) ─────────────────────────── */ + +test('[P1] anonymous alternative submits only the OR index, never an unrequested profile', async ({ page }) => { + const planRequests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + planRequests.push(payload); + const decision = payload.decisions[0]; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(actionablePlan({ + action: decision.action, + auth_profile_id: decision.auth_profile_id || null, + security_alternative_index: decision.security_alternative_index === undefined ? null : decision.security_alternative_index, + })), + }); + }); + await openSecurityPreview(page, 'en'); + + // The anonymous (empty-scheme) alternative is selectable; selecting it must + // keep the profile unbound. + const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); + await alternative.selectOption('1'); + // The anonymous alternative must not offer any Auth Profile control. + await expect(page.getByLabel(localized('Auth Profile', 'Профиль авторизации'))).toHaveCount(0); + await page.locator('#openapi-import-create').click(); + + await expect.poll(() => planRequests.length).toBe(1); + const decision = planRequests[0].decisions[0]; + expect(decision).toMatchObject({ operation_key: operationKey, security_alternative_index: 1 }); + expect(decision).not.toHaveProperty('auth_profile_id'); + // A confirmed anonymous alternative is no-auth, never a resolved auth binding. + await expect(page.locator('[data-openapi-security-op-status="' + operationKey + '"]')).toHaveText('No authentication'); + await expect(page.getByTestId('openapi-security-status')).toHaveText(localized('No authentication', 'Без аутентификации')); + expect(JSON.stringify(planRequests[0])).not.toContain(profileId); +}); + +test('[P1] changing the alternative clears the profile and invalidates a prior plan', async ({ page }) => { + const planRequests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + planRequests.push(payload); + const decision = payload.decisions[0]; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(actionablePlan({ + action: decision.action, + auth_profile_id: decision.auth_profile_id || null, + security_alternative_index: decision.security_alternative_index === undefined ? null : decision.security_alternative_index, + })), + }); + }); + await openSecurityPreview(page, 'en'); + + const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profile = page.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + await alternative.selectOption('0'); + await profile.selectOption(profileId); + + // A confirmed plan exists for (alternative 0, profile). + await page.locator('#openapi-import-create').click(); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeVisible(); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Resolved'); + + // Back to editing: the selection survives, the plan does not. + await page.locator('#openapi-import-plan-back').click(); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeHidden(); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Unresolved'); + + // Switching the alternative drops the bound profile; the anonymous choice + // hides the profile control entirely and the new plan carries only the index. + await alternative.selectOption('1'); + await expect(profile).toHaveCount(0); + await page.locator('#openapi-import-create').click(); + await expect.poll(() => planRequests.length).toBe(2); + + const decision = planRequests[1].decisions[0]; + expect(decision).toMatchObject({ operation_key: operationKey, security_alternative_index: 1 }); + expect(decision).not.toHaveProperty('auth_profile_id'); + await expect(page.locator('[data-openapi-security-op-status="' + operationKey + '"]')).toHaveText('No authentication'); + await expect(page.getByTestId('openapi-security-status')).toHaveText(localized('No authentication', 'Без аутентификации')); +}); + +test('[P1] empty compatible-profile list keeps the selection safe and the API blocker authoritative', async ({ page }) => { + const emptyPreview = securityPreview(); + emptyPreview.security_profile_choices = []; + const planRequests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + planRequests.push(route.request().postDataJSON()); + // Actual public contract: a 200 plan whose candidate is blocked with the + // safe reason the API owns, not a synthesized top-level error code. + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + plan_reference: 'plan_empty_profiles_atdd', + plan_digest: 'f'.repeat(64), + expires_at: '2099-01-01T00:00:00Z', + selection_mode: 'full', + counts: { selected: 1, excluded: 0, actionable: 0, blocked: 1 }, + candidates: [{ + key: operationKey, + name: 'protected_operation', + state: 'blocked', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: null, + security_alternative_index: 0, + reason: 'auth_profile_required', + }], + }), + }); + }); + await openSecurityPreview(page, 'en', emptyPreview); + + // The empty compatible-profile list disables the profile control and shows + // safe localized guidance instead of offering an arbitrary error payload. + const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profileSelect = page.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + await expect(profileSelect.locator('option')).toHaveCount(1); + await alternative.selectOption('0'); + await expect(profileSelect).toBeDisabled(); + await expect(page.locator('.openapi-import-security-guidance')).toHaveText( + 'No compatible Auth Profiles for the selected alternative. Choose another alternative or create a plan to revalidate.'); + await page.locator('#openapi-import-create').click(); + + await expect.poll(() => planRequests.length).toBe(1); + expect(planRequests[0].decisions[0]).toMatchObject({ + operation_key: operationKey, + security_alternative_index: 0, + }); + expect(planRequests[0].decisions[0]).not.toHaveProperty('auth_profile_id'); + // The API candidate is authoritative: blocked, with safe translated guidance. + await expect(page.locator('.openapi-import-security-op-status')).toHaveText('Blocked'); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Blocked'); + await expect(page.locator('.openapi-import-plan-reason')).toHaveText( + 'This operation requires an Auth Profile. Choose a compatible profile and create a new plan.'); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeVisible(); +}); + +test('[P2] security controls keep accessible names, keyboard focus and a live status', async ({ page }) => { + await openSecurityPreview(page, 'en'); + + const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profile = page.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + await expect(alternative).toBeEditable(); + await expect(profile).toBeEditable(); + + // Keyboard-only selection: focus the native select and change it with keys, + // never programmatically. + await alternative.focus(); + expect(await alternative.evaluate((el) => el === document.activeElement)).toBe(true); + await alternative.press('ArrowDown'); + await expect(alternative).toHaveValue('0'); + await profile.focus(); + expect(await profile.evaluate((el) => el === document.activeElement)).toBe(true); + await profile.press('ArrowDown'); + await expect(profile).toHaveValue(profileId); + + const status = page.getByTestId('openapi-security-status'); + await expect(status).toHaveAttribute('role', 'status'); + await expect(status).toHaveAttribute('aria-live', 'polite'); + const opStatus = page.locator('.openapi-import-security-op-status'); + await expect(opStatus).toHaveAttribute('role', 'status'); + await expect(opStatus).toHaveAttribute('aria-live', 'polite'); + + await page.locator('body').press('Escape'); + await expect(page.locator('#openapi-import-modal')).toBeHidden(); +}); + +test('[P1] a delayed preview response cannot restore state after the user cancelled', async ({ page }) => { + let releasePreview; + const previewReleased = new Promise((resolve) => { + releasePreview = resolve; + }); + let previewFulfilled = false; + await page.route('**/imports/openapi/preview', async (route) => { + await previewReleased; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(securityPreview()), + }); + previewFulfilled = true; + }); + + await login(page); + await dismissOnboardingIfOpen(page); + await page.evaluate(() => window.setLang('en')); + await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'security-mapping.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from('openapi: 3.0.3\ninfo: { title: Security mapping, version: "1" }\npaths: {}\n'), + }); + const previewRequest = page.waitForRequest((request) => + request.method() === 'POST' && request.url().includes('/imports/openapi/preview')); + await page.locator('#openapi-import-preview').click(); + await previewRequest; + await page.locator('#openapi-import-cancel').click(); + + // Release the stale response and wait for the handler to settle instead of a + // fixed timeout, then prove it could not restore the panel. + releasePreview(); + await expect.poll(() => previewFulfilled).toBe(true); + await expect(page.locator('#openapi-import-preview-panel')).toBeHidden(); + await expect(page.locator('#openapi-import-cancel')).toBeHidden(); +}); + +/* ── Per-operation authority and selection lifecycle ────────────────── */ + +test('[P1] offers only profiles compatible with the chosen OR alternative and hides them for anonymous', async ({ page }) => { + const key = 'get:/multi'; + await openSecurityPreview(page, 'en', previewWith([ + protectedOperation(key, 'multi_operation', [ + { schemes: ['bearerAuth'], supported: true, findings: [] }, + { schemes: ['basicAuth'], supported: true, findings: [] }, + { schemes: [], supported: true, findings: [] }, + { schemes: ['oauthAuth'], supported: false, findings: ['unsupported_security_and_requirement'] }, + ]), + ], [ + { operation_key: key, alternative_index: 0, schemes: ['bearerAuth'], profiles: [{ id: 'prof_bearer', name: 'Bearer profile', kind: 'bearer' }] }, + { operation_key: key, alternative_index: 1, schemes: ['basicAuth'], profiles: [{ id: 'prof_basic', name: 'Basic profile', kind: 'basic' }] }, + ])); + + const row = page.locator('.openapi-import-operation').filter({ hasText: 'multi_operation' }); + const alternative = row.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profile = row.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + + // Profile-first selection may offer the safe union while no alternative is chosen. + await expect(profile.locator('option[value="prof_bearer"]')).toBeAttached(); + await expect(profile.locator('option[value="prof_basic"]')).toBeAttached(); + await expect(alternative.locator('option[value="3"]')).toHaveAttribute('disabled', ''); + + await alternative.selectOption('0'); + await expect(profile.locator('option[value="prof_bearer"]')).toBeAttached(); + await expect(profile.locator('option[value="prof_basic"]')).toHaveCount(0); + + await alternative.selectOption('1'); + await expect(profile.locator('option[value="prof_basic"]')).toBeAttached(); + await expect(profile.locator('option[value="prof_bearer"]')).toHaveCount(0); + + // An anonymous alternative must offer no profile control at all. + await alternative.selectOption('2'); + await expect(row.locator('.openapi-import-security-controls select')).toHaveCount(1); + await expect(row.getByLabel(localized('Auth Profile', 'Профиль авторизации'))).toHaveCount(0); +}); + +test('[P1] reports per-operation resolved and blocked authority for a mixed plan', async ({ page }) => { + const keyA = 'get:/alpha'; + const keyB = 'get:/beta'; + await page.route('**/imports/openapi/*/plans', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + plan_reference: 'plan_multi_mixed_atdd', + plan_digest: 'd'.repeat(64), + expires_at: '2099-01-01T00:00:00Z', + selection_mode: 'full', + counts: { selected: 2, excluded: 0, actionable: 1, blocked: 1 }, + candidates: [ + { key: keyA, name: 'alpha_operation', state: 'actionable', action: 'create', auth_profile_id: 'prof_a', security_alternative_index: 0, reason: null }, + { key: keyB, name: 'beta_operation', state: 'blocked', action: 'create', auth_profile_id: 'prof_b', security_alternative_index: 0, reason: 'auth_profile_required' }, + ], + }), + }); + }); + await openSecurityPreview(page, 'en', previewWith([ + protectedOperation(keyA, 'alpha_operation', [{ schemes: ['bearerAuth'], supported: true, findings: [] }]), + protectedOperation(keyB, 'beta_operation', [{ schemes: ['bearerAuth'], supported: true, findings: [] }]), + ], [ + { operation_key: keyA, alternative_index: 0, schemes: ['bearerAuth'], profiles: [{ id: 'prof_a', name: 'A profile', kind: 'bearer' }] }, + { operation_key: keyB, alternative_index: 0, schemes: ['bearerAuth'], profiles: [{ id: 'prof_b', name: 'B profile', kind: 'bearer' }] }, + ])); + + // Before the plan both protected operations require an explicit binding. + await expect(page.getByTestId('openapi-security-status')).toHaveText('Authentication required'); + + const rowA = page.locator('.openapi-import-operation').filter({ hasText: 'alpha_operation' }); + const rowB = page.locator('.openapi-import-operation').filter({ hasText: 'beta_operation' }); + await rowA.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await rowA.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption('prof_a'); + await rowB.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await rowB.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption('prof_b'); + + await page.locator('#openapi-import-create').click(); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeVisible(); + await expect(page.locator('[data-openapi-security-op-status="' + keyA + '"]')).toHaveText('Resolved'); + await expect(page.locator('[data-openapi-security-op-status="' + keyB + '"]')).toHaveText('Blocked'); + // The summary must surface the blocked operation, not mask it behind a resolved one. + await expect(page.getByTestId('openapi-security-status')).toHaveText('Blocked'); +}); + +test('[P1] editing the alternative after an authoritative blocker clears the blocked authority', async ({ page }) => { + const key = 'get:/reset'; + await page.route('**/imports/openapi/*/plans', async (route) => { + await route.fulfill({ + status: 422, + contentType: 'application/json', + body: JSON.stringify({ error: { code: 'import_plan_auth_unavailable', message: secretCanary } }), + }); + }); + await openSecurityPreview(page, 'en', previewWith([ + protectedOperation(key, 'reset_operation', [ + { schemes: ['bearerAuth'], supported: true, findings: [] }, + { schemes: ['basicAuth'], supported: true, findings: [] }, + ]), + ], [ + { operation_key: key, alternative_index: 0, schemes: ['bearerAuth'], profiles: [{ id: 'prof_bearer', name: 'Bearer profile', kind: 'bearer' }] }, + { operation_key: key, alternative_index: 1, schemes: ['basicAuth'], profiles: [{ id: 'prof_basic', name: 'Basic profile', kind: 'basic' }] }, + ])); + + const row = page.locator('.openapi-import-operation').filter({ hasText: 'reset_operation' }); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await row.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption('prof_bearer'); + await page.locator('#openapi-import-create').click(); + await expect(page.locator('[data-openapi-security-op-status="' + key + '"]')).toHaveText('Blocked'); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Blocked'); + + // Editing must void the blocked authority and clear the bound profile. + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('1'); + await expect(row.getByLabel(localized('Auth Profile', 'Профиль авторизации'))).toHaveValue(''); + await expect(page.locator('[data-openapi-security-op-status="' + key + '"]')).toHaveText('Authentication required'); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Authentication required'); + await expect(page.locator('body')).not.toContainText(secretCanary); +}); + +test('[P1] binding then choosing skip removes the auth fields from the plan payload', async ({ page }) => { + const suggestedName = 'protected_operation'; + const planRequests = []; + await page.route('**/api/admin/workspaces/*/operations', async (route) => { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + items: [{ id: 'op_skip_target', name: suggestedName, protocol: 'rest', status: 'draft' }], + page: 1, + page_size: 1, + total: 1, + }), + }); + }); + await page.route('**/imports/openapi/*/plans', async (route) => { + planRequests.push(route.request().postDataJSON()); + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + plan_reference: 'plan_skip_atdd', + plan_digest: 'e'.repeat(64), + expires_at: '2099-01-01T00:00:00Z', + selection_mode: 'full', + counts: { selected: 1, excluded: 0, actionable: 0, blocked: 0 }, + candidates: [{ + key: operationKey, + name: suggestedName, + state: 'excluded', + action: 'skip', + target_operation_id: 'op_skip_target', + confirmed_name: null, + auth_profile_id: null, + security_alternative_index: null, + reason: 'skipped', + }], + }), + }); + }); + await openSecurityPreview(page, 'en'); + + const row = page.locator('.openapi-import-operation').filter({ hasText: suggestedName }); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await row.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + + await row.locator('[data-openapi-decision-action]').selectOption('skip'); + await expect(row.locator('.openapi-import-security-controls')).toHaveCount(0); + + await page.locator('#openapi-import-create').click(); + await expect.poll(() => planRequests.length).toBe(1); + const decision = planRequests[0].decisions[0]; + expect(decision).toMatchObject({ operation_key: operationKey, action: 'skip', target_operation_id: 'op_skip_target' }); + expect(decision).not.toHaveProperty('auth_profile_id'); + expect(decision).not.toHaveProperty('security_alternative_index'); +}); + +test('[P1] language change during a pending Apply keeps security controls disabled', async ({ page }) => { + let releaseApply; + const applyHeld = new Promise((resolve) => { + releaseApply = resolve; + }); + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(actionablePlan(payload.decisions[0])) }); + }); + await page.route('**/imports/openapi/*/apply', async (route) => { + await applyHeld; + try { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + plan_digest: 'c'.repeat(64), + counts: { created: 1 }, + outcomes: [{ key: operationKey, name: 'protected_operation', status: 'created' }], + }), + }); + } catch (_error) { + // The context change aborts the request; it must not update the modal. + } + }); + await openSecurityPreview(page, 'en'); + + const row = page.locator('.openapi-import-operation').filter({ hasText: 'protected_operation' }); + const alternative = row.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profile = row.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + await alternative.selectOption('0'); + await profile.selectOption(profileId); + await page.locator('#openapi-import-create').click(); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Resolved'); + + await page.locator('#openapi-import-plan-apply').click(); + await expect(alternative).toBeDisabled(); + // A language change while Apply is in flight must not re-render and re-enable + // the security controls. + await page.evaluate(() => window.setLang('ru')); + await expect(alternative).toBeDisabled(); + await expect(profile).toBeDisabled(); + await expect(page.locator('#openapi-import-file-select')).toBeDisabled(); + releaseApply(); +}); + +test('[P1] a rejected Apply clears a previously resolved security authority', async ({ page }) => { + await page.route('**/imports/openapi/*/plans', async (route) => { + const payload = route.request().postDataJSON(); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify(actionablePlan(payload.decisions[0])) }); + }); + await page.route('**/imports/openapi/*/apply', async (route) => { + await route.fulfill({ + status: 422, + contentType: 'application/json', + body: JSON.stringify({ error: { code: 'import_plan_auth_unavailable', message: secretCanary }, recovery: 'repreview_required' }), + }); + }); + await openSecurityPreview(page, 'en'); + + const row = page.locator('.openapi-import-operation').filter({ hasText: 'protected_operation' }); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await row.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + await page.locator('#openapi-import-create').click(); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Resolved'); + + await page.locator('#openapi-import-plan-apply').click(); + // The rejected Apply must not retain the prior resolved authority. + await expect(page.getByTestId('openapi-security-status')).toHaveText('Unresolved'); + await expect(page.locator('body')).not.toContainText(secretCanary); +}); + +test('[P1] a delayed plan response cannot restore authority after cancel', async ({ page }) => { + let releasePlan; + const planHeld = new Promise((resolve) => { + releasePlan = resolve; + }); + let planFulfilled = false; + await page.route('**/imports/openapi/*/plans', async (route) => { + await planHeld; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(actionablePlan({ action: 'create', auth_profile_id: profileId, security_alternative_index: 0 })), + }); + planFulfilled = true; + }); + await openSecurityPreview(page, 'en'); + + const row = page.locator('.openapi-import-operation').filter({ hasText: 'protected_operation' }); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await row.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + + const planRequest = page.waitForRequest((request) => + request.method() === 'POST' && /\/imports\/openapi\/[^/]+\/plans$/.test(request.url())); + await page.locator('#openapi-import-create').click(); + await planRequest; + await page.locator('#openapi-import-cancel').click(); + + releasePlan(); + await expect.poll(() => planFulfilled).toBe(true); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeHidden(); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Unresolved'); +}); + +test('[P1] a quoted or backslash operation key cannot break per-operation status', async ({ page }) => { + const key = 'get:/quoted"path\\segment'; + const planRequests = []; + await page.route('**/imports/openapi/*/plans', async (route) => { + planRequests.push(route.request().postDataJSON()); + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + plan_reference: 'plan_quoted_key_atdd', + plan_digest: 'a'.repeat(64), + expires_at: '2099-01-01T00:00:00Z', + selection_mode: 'full', + counts: { selected: 1, excluded: 0, actionable: 1, blocked: 0 }, + candidates: [{ + key, + name: 'quoted_operation', + state: 'actionable', + action: 'create', + target_operation_id: null, + confirmed_name: null, + auth_profile_id: profileId, + security_alternative_index: 0, + reason: null, + }], + }), + }); + }); + await openSecurityPreview(page, 'en', previewWith([ + protectedOperation(key, 'quoted_operation', [{ schemes: ['bearerAuth'], supported: true, findings: [] }]), + ], [ + { operation_key: key, alternative_index: 0, schemes: ['bearerAuth'], profiles: [{ id: profileId, name: 'Bearer profile', kind: 'bearer' }] }, + ])); + + // Rendering a CSS-hostile key must not throw and the preview must render. + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + const row = page.locator('.openapi-import-operation').filter({ hasText: 'quoted_operation' }); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await row.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + await page.locator('#openapi-import-create').click(); + + await expect.poll(() => planRequests.length).toBe(1); + expect(planRequests[0].decisions[0].operation_key).toBe(key); + // Status is matched by dataset value, never by interpolating the key into CSS. + await expect(row.locator('.openapi-import-security-op-status')).toHaveText('Resolved'); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Resolved'); +}); + +test('[P1] localizes unsupported findings and empty-profile guidance in RU and EN', async ({ page }) => { + const key = 'get:/locale'; + await openSecurityPreview(page, 'ru', previewWith([ + protectedOperation(key, 'locale_operation', [ + { schemes: ['bearerAuth'], supported: true, findings: [] }, + { schemes: ['oauthAuth', 'apiKeyAuth'], supported: false, findings: ['unsupported_security_and_requirement'] }, + ]), + ], [])); + + const row = page.locator('.openapi-import-operation').filter({ hasText: 'locale_operation' }); + const alternative = row.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profile = row.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + + // RU: exact unsupported finding copy and empty-profile guidance. + await expect(row.locator('.openapi-import-security-findings')).toContainText( + 'Это security requirement невозможно выполнить со схемой, поддержанной Crank, или доступным профилем авторизации.'); + await alternative.selectOption('0'); + await expect(profile).toBeDisabled(); + await expect(row.locator('.openapi-import-security-guidance')).toHaveText( + 'Для выбранного варианта нет совместимых профилей авторизации. Выберите другой вариант или создайте план для повторной проверки.'); + const opStatus = row.locator('.openapi-import-security-op-status'); + await expect(opStatus).toHaveAttribute('role', 'status'); + await expect(opStatus).toHaveAttribute('aria-live', 'polite'); + + // EN: switching language invalidates an unconfirmed preview, so re-preview + // and assert the exact translated copy in the second locale. + await page.evaluate(() => window.setLang('en')); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await expect(row.locator('.openapi-import-security-findings')).toContainText( + 'This security requirement cannot be satisfied with a supported scheme or an available Auth Profile.'); + await expect(row.getByLabel(localized('Auth Profile', 'Профиль авторизации'))).toBeDisabled(); + await expect(row.locator('.openapi-import-security-guidance')).toHaveText( + 'No compatible Auth Profiles for the selected alternative. Choose another alternative or create a plan to revalidate.'); +}); + +test('[P1] a stale security plan cannot restore authority after a language or workspace change', async ({ page }) => { + const releases = []; + let planReleased = 0; + await page.route('**/imports/openapi/*/plans', async (route) => { + await new Promise((resolve) => releases.push(resolve)); + planReleased += 1; + try { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify(actionablePlan({ action: 'create', auth_profile_id: profileId, security_alternative_index: 0 })), + }); + } catch (_error) { + // The context change aborts the request; it must not update the modal. + } + }); + await openSecurityPreview(page, 'en'); + + async function bindSelection() { + const row = page.locator('.openapi-import-operation').filter({ hasText: 'protected_operation' }); + await row.getByLabel(localized('Security alternative', 'Вариант безопасности')).selectOption('0'); + await row.getByLabel(localized('Auth Profile', 'Профиль авторизации')).selectOption(profileId); + } + async function startPlan() { + const request = page.waitForRequest((r) => r.method() === 'POST' && /\/imports\/openapi\/[^/]+\/plans$/.test(r.url())); + await page.locator('#openapi-import-create').click(); + await request; + await expect.poll(() => releases.length).toBeGreaterThanOrEqual(1); + } + + // 1) Language change while a security plan is pending. + await bindSelection(); + await startPlan(); + await page.evaluate(() => window.setLang('ru')); + releases.shift()(); + await expect.poll(() => planReleased).toBeGreaterThanOrEqual(1); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Не разрешено'); + await expect(page.locator('.openapi-import-security-controls')).toHaveCount(0); + + // 2) Workspace change while a security plan is pending. + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + await bindSelection(); + await startPlan(); + await page.evaluate(() => { + window.dispatchEvent(new CustomEvent('crank:workspacechange', { detail: { id: 'ws-stale-security' } })); + }); + releases.shift()(); + await expect.poll(() => planReleased).toBeGreaterThanOrEqual(2); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Не разрешено'); + await expect(page.locator('.openapi-import-security-controls')).toHaveCount(0); + await expect(page.locator('#openapi-import-preview-panel')).toBeHidden(); +}); + +/* ── Real API evidence ───────────────────────────────────────────────── */ + +test('[P1][api] real preview/plan round-trip resolves a typed reference (real secret + bearer profile)', async ({ page }) => { + await login(page); + await dismissOnboardingIfOpen(page); + await page.evaluate(() => window.setLang('en')); + + const workspace = await getCurrentWorkspace(page); + const prefix = uniqueName('secmap'); + const secretValue = 'secmap-bearer-' + prefix; + const secret = await browserJson( + page, + 'POST', + `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/secrets`, + { name: prefix + '_secret', kind: 'token', value: secretValue }, + ); + let profile = null; + try { + profile = await browserJson( + page, + 'POST', + `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/auth-profiles`, + { + name: prefix + '_profile', + kind: 'bearer', + config: { bearer: { header_name: 'Authorization', secret_id: secret.id } }, + }, + ); + expect(String(profile.id)).toMatch(/^auth_/); + + await page.getByRole('button', { name: localized('Import OpenAPI', 'Импорт OpenAPI') }).click(); + await page.locator('#openapi-import-file').setInputFiles({ + name: 'security-mapping-real.yaml', + mimeType: 'application/yaml', + buffer: Buffer.from([ + 'openapi: 3.0.3', + 'info: { title: Security mapping real, version: "1" }', + 'servers: [{ url: "https://api.example.test" }]', + 'paths:', + ' /' + prefix + ':', + ' get:', + ' operationId: ' + prefix + '_operation', + ' security:', + ' - bearerAuth: []', + ' - {}', + ' responses:', + ' \'200\': { description: OK }', + 'components:', + ' securitySchemes:', + ' bearerAuth: { type: http, scheme: bearer }', + ].join('\n')), + }); + await page.locator('#openapi-import-preview').click(); + await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); + + const alternative = page.getByLabel(localized('Security alternative', 'Вариант безопасности')); + const profileSelect = page.getByLabel(localized('Auth Profile', 'Профиль авторизации')); + const bearerOption = alternative.locator('option', { hasText: 'bearerAuth' }); + await bearerOption.waitFor({ state: 'attached' }); + const bearerIndex = await bearerOption.getAttribute('value'); + expect(bearerIndex).toMatch(/^\d+$/); // option value, not the select's current value + await alternative.selectOption(bearerIndex); + const realProfileOption = profileSelect.locator('option[value="' + profile.id + '"]'); + await expect(realProfileOption).toBeAttached(); + await profileSelect.selectOption(profile.id); + + const planRequest = page.waitForRequest((request) => + request.method() === 'POST' && /\/imports\/openapi\/[^/]+\/plans$/.test(request.url()), + ); + await page.locator('#openapi-import-create').click(); + const sent = await planRequest; + const response = await sent.response(); + + // Happy path is deterministic: the created profile matches the bearer + // scheme, so the real API must accept the typed reference. + expect(response.status()).toBe(200); + const payload = await sent.postDataJSON(); + const planDecision = (payload.decisions || [])[0]; + expect(planDecision).toMatchObject({ auth_profile_id: profile.id, security_alternative_index: Number(bearerIndex) }); + await expect(page.locator('#openapi-import-plan-confirmation')).toBeVisible(); + await expect(page.getByTestId('openapi-security-status')).toHaveText('Resolved'); + await expect(page.locator('body')).not.toContainText(secretValue); + } finally { + // Best-effort cleanup. The Secret is guarded (409) while the live profile + // references it; the referenced Auth Profile has no delete endpoint, so we + // only attempt to release the Secret and never widen the backend surface. + try { + await browserJson( + page, + 'DELETE', + `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/secrets/${encodeURIComponent(secret.id)}`, + [], + [404, 409], + ); + } catch (_error) {} + } +}); diff --git a/apps/ui/tests/e2e/operations.spec.js b/apps/ui/tests/e2e/operations.spec.js index 2cce6bd..262eff6 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', 'Операции')); @@ -100,7 +142,9 @@ paths: const request = await previewRequest; expect(request.headers()['content-type']).toMatch(/^multipart\/form-data; boundary=/i); await expect(page.locator('#openapi-import-preview-panel')).toBeVisible(); - await expect(page.locator('.openapi-import-document-findings')).toContainText(/base URL/i); + const documentFindings = page.locator('.openapi-import-document-findings'); + await documentFindings.locator('summary').click(); + await expect(documentFindings).toContainText(/base URL/i); await expect(page.locator('.openapi-import-group').filter({ hasText: 'currency' })).toBeVisible(); const importedOperation = page.locator('.openapi-import-operation').filter({ hasText: 'Получить курсы валют' }); await expect(importedOperation).toBeVisible(); @@ -118,6 +162,7 @@ paths: await page.locator('#openapi-import-search').fill('status'); await expect(page.locator('.openapi-import-operation:visible')).toHaveCount(1); await expect(page.locator('.openapi-import-operation:visible').filter({ hasText: 'Проверить статус' })).toBeVisible(); + await page.locator('#openapi-import-partial-mode').check(); await page.locator('#openapi-import-clear-visible').click(); await expect(page.locator('#openapi-import-selection')).toContainText(localized('Selected: 1', 'Выбрано: 1')); await page.locator('#openapi-import-search').fill(''); @@ -127,18 +172,21 @@ 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}'); + const operationFindings = importedOperation.locator('.openapi-import-findings'); + await operationFindings.locator('summary').click(); + await expect(operationFindings).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 }) => { @@ -230,7 +278,7 @@ test('OpenAPI upload recovers from pagehide and a preview server error', async ( buffer: Buffer.from('openapi: 3.0.3'), }); await page.locator('#openapi-import-preview').click(); - await expect(page.locator('#openapi-import-status')).toContainText(localized('Uploading and parsing', 'Загружаю и разбираю')); + await expect(page.locator('#openapi-import-status')).toContainText(localized('Uploading the document', 'Загружаю документ')); await page.evaluate(() => window.dispatchEvent(new PageTransitionEvent('pagehide', { persisted: true }))); await expect(page.locator('#openapi-import-status')).toHaveText(''); @@ -246,6 +294,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 +318,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 +343,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 +358,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 +389,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 +426,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 +457,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 +467,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 +484,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 +581,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 +656,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-focus.spec.js b/apps/ui/tests/e2e/wizard-focus.spec.js new file mode 100644 index 0000000..4c91bc3 --- /dev/null +++ b/apps/ui/tests/e2e/wizard-focus.spec.js @@ -0,0 +1,41 @@ +const { test, expect } = require('@playwright/test'); +const { login } = require('./helpers'); + +test('delayed new-upstream autofocus must not steal focus from the URL field', async ({ page }) => { + await login(page); + await page.goto('/wizard/'); + await page.waitForFunction(() => window.CrankWizardReady === true); + + await page.locator('[data-testid="wizard-protocol-rest"]').click(); + await page.locator('#btn-continue').click(); + await expect(page.locator('#upstream-new-trigger')).toBeVisible(); + + // Take manual control of page time before the new-upstream form is opened so + // the deferred autofocus timer scheduled by startNewUpstream cannot fire on + // its own while Playwright fills the two fields. + const installTime = new Date('2026-05-01T12:00:00.000Z'); + await page.clock.install({ time: installTime }); + await page.clock.pauseAt(new Date(installTime.getTime() + 1000)); + + await page.locator('#upstream-new-trigger').click(); + + await page.locator('#new-upstream-name').fill('focus-red-upstream'); + await page.locator('#new-upstream-url').fill('http://127.0.0.1:3310'); + + // Both values landed exactly and focus is explicitly on the URL field before + // the deferred autofocus runs. + await expect(page.locator('#new-upstream-name')).toHaveValue('focus-red-upstream'); + await expect(page.locator('#new-upstream-url')).toHaveValue('http://127.0.0.1:3310'); + await expect(page.locator('#new-upstream-url')).toBeFocused(); + + // Explicitly execute the old 50ms autofocus timer. + await page.clock.runFor(100); + + // The delayed autofocus must not move focus back to the name field. + await expect(page.locator('#new-upstream-url')).toBeFocused(); + + // Correct field still receives text after the timer ran; no network mutation. + await page.keyboard.insertText('-probe'); + await expect(page.locator('#new-upstream-url')).toHaveValue('http://127.0.0.1:3310-probe'); + await expect(page.locator('#new-upstream-name')).toHaveValue('focus-red-upstream'); +}); diff --git a/apps/ui/tests/e2e/wizard-noop-save.spec.js b/apps/ui/tests/e2e/wizard-noop-save.spec.js new file mode 100644 index 0000000..0039ba8 --- /dev/null +++ b/apps/ui/tests/e2e/wizard-noop-save.spec.js @@ -0,0 +1,165 @@ +const { test, expect } = require('@playwright/test'); +const { getCurrentWorkspace, login } = require('./helpers'); + +function draftSnapshot(operationId) { + return { + id: operationId, + name: operationId, + display_name: 'Imported rates', + category: 'general', + protocol: 'rest', + target: { + kind: 'rest', + base_url: 'https://api.example.test', + method: 'GET', + path_template: '/rates', + static_headers: {}, + }, + input_schema: { + type: 'object', + description: null, + required: false, + nullable: false, + default_value: null, + fields: {}, + items: null, + enum_values: [], + variants: [], + }, + output_schema: { + type: 'object', + description: null, + required: false, + nullable: false, + default_value: null, + fields: {}, + items: null, + enum_values: [], + variants: [], + }, + input_mapping: { rules: [] }, + output_mapping: { + rules: [ + { source: '$.response.body', target: '$.output', required: true }, + { source: '$.response.body.rate', target: '$.output.rate', required: true }, + ], + }, + execution_config: { + timeout_ms: 10000, + retry_policy: null, + auth_profile_ref: null, + headers: {}, + approval_policy: null, + }, + tool_description: { title: 'Imported rates', description: 'Get rates.', tags: [], examples: [] }, + wizard_state: { input_sample: {}, output_sample: {}, test_input: {}, import_findings: [] }, + }; +} + +test('edit wizard skips no-op saves and refreshes its baseline after a real edit', async ({ page }) => { + await login(page); + const workspace = await getCurrentWorkspace(page); + const operationId = 'imported_rates_noop'; + const operationPath = `/api/admin/workspaces/${encodeURIComponent(workspace.id)}/operations/${operationId}`; + const versionPath = `${operationPath}/versions/*`; + let version = 4; + let updateCount = 0; + const testedVersions = []; + const publishedVersions = []; + const analyzedPayloads = []; + + await page.route(operationPath, async (route) => { + if (route.request().method() === 'PATCH') { + updateCount += 1; + version += 1; + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ operation_id: operationId, workspace_id: workspace.id, version, status: 'draft' }), + }); + return; + } + + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + id: operationId, + workspace_id: workspace.id, + name: operationId, + display_name: 'Imported rates', + category: 'general', + protocol: 'rest', + status: 'draft', + current_draft_version: version, + draft_version_ref: { version, status: 'draft' }, + published_version_ref: null, + agent_refs: [], + }), + }); + }); + + await page.route(versionPath, async (route) => { + const requestedVersion = Number(new URL(route.request().url()).pathname.split('/').pop()); + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ + operation_id: operationId, + workspace_id: workspace.id, + version: requestedVersion, + status: 'draft', + snapshot: draftSnapshot(operationId), + }), + }); + }); + + await page.route(`**/operations/${operationId}/test-runs`, async (route) => { + testedVersions.push(route.request().postDataJSON().version); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ ok: true, errors: [] }) }); + }); + await page.route('**/operations/analyze-quality', async (route) => { + analyzedPayloads.push(route.request().postDataJSON()); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ blocking: false, findings: [] }) }); + }); + await page.route(`**/operations/${operationId}/publish`, async (route) => { + const payload = route.request().postDataJSON(); + publishedVersions.push(payload.version); + await route.fulfill({ contentType: 'application/json', body: JSON.stringify({ published_version: payload.version }) }); + }); + page.on('dialog', (dialog) => dialog.accept()); + + await page.goto(`/wizard/?mode=edit&operationId=${operationId}`); + await expect(page.locator('#tool-display-name')).toHaveValue('Imported rates'); + + const loadedOutputMapping = await page.evaluate(() => ( + window.jsyaml.load(document.getElementById('tool-output-mapping').value) + )); + expect(loadedOutputMapping['$.output']).toBe('$.response.body'); + expect(loadedOutputMapping.rate).toBe('$.response.body.rate'); + expect(JSON.stringify(loadedOutputMapping)).not.toContain('$.response.body.$.output'); + + await page.evaluate(() => window.CrankWizardShell.doGoToStep(5)); + + await page.locator('#wizard-run-test').click(); + await expect.poll(() => testedVersions).toEqual([4]); + expect(updateCount).toBe(0); + + await page.evaluate(() => window.CrankWizardShell.doGoToStep(4)); + await page.locator('#tool-display-name').fill('Edited rates'); + await page.locator('.btn-save-draft').click(); + await expect.poll(() => updateCount).toBe(1); + + await page.evaluate(() => window.CrankWizardShell.doGoToStep(5)); + await page.locator('#wizard-run-test').click(); + await expect.poll(() => testedVersions).toEqual([4, 5]); + expect(updateCount).toBe(1); + + await page.locator('#wizard-publish-operation').click(); + await expect.poll(() => publishedVersions).toEqual([5]); + expect(updateCount).toBe(1); + + const publishedRules = analyzedPayloads[analyzedPayloads.length - 1].output_mapping.rules; + expect(publishedRules).toEqual(expect.arrayContaining([ + expect.objectContaining({ source: '$.response.body', target: '$.output' }), + expect.objectContaining({ source: '$.response.body.rate', target: '$.output.rate' }), + ])); + expect(JSON.stringify(publishedRules)).not.toContain('$.response.body.$.output'); +}); diff --git a/apps/ui/tests/e2e/wizard.spec.js b/apps/ui/tests/e2e/wizard.spec.js index d20ce58..3dcb582 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)); @@ -881,10 +881,12 @@ test('wizard edit mode preserves explicit request mapping targets on save', asyn await expect(page.locator('#approval-risk-level')).toHaveCount(0); await expect(page.locator('#approval-ttl-seconds')).toHaveValue('180'); await expect(page.locator('#approval-payload-preview-mode')).toHaveCount(0); + await page.evaluate(() => window.CrankWizardShell.doGoToStep(4)); + await page.locator('#tool-display-name').fill('Frankfurter latest rate edited'); await page.locator('.btn-save-draft').click(); await expect.poll(() => updatePayload).not.toBeNull(); - expect(updatePayload.display_name).toBe('Frankfurter latest rate'); + expect(updatePayload.display_name).toBe('Frankfurter latest rate edited'); expect(updatePayload.target).toEqual({ kind: 'rest', base_url: 'https://api.frankfurter.app', diff --git a/crates/crank-core/src/product_event.rs b/crates/crank-core/src/product_event.rs index e577c66..c5d551d 100644 --- a/crates/crank-core/src/product_event.rs +++ b/crates/crank-core/src/product_event.rs @@ -16,16 +16,22 @@ pub enum ProductEventKind { OnboardingDismissed, OnboardingAbandoned, OnboardingCompleted, + OnboardingImportEligible, + OnboardingImportPublished, + OnboardingAgentActivated, } impl ProductEventKind { - pub const ALL: [Self; 6] = [ + pub const ALL: [Self; 9] = [ Self::OnboardingEligible, Self::OnboardingStarted, Self::OnboardingResumed, Self::OnboardingDismissed, Self::OnboardingAbandoned, Self::OnboardingCompleted, + Self::OnboardingImportEligible, + Self::OnboardingImportPublished, + Self::OnboardingAgentActivated, ]; pub const fn as_str(self) -> &'static str { @@ -36,8 +42,23 @@ impl ProductEventKind { Self::OnboardingDismissed => "onboarding_dismissed", Self::OnboardingAbandoned => "onboarding_abandoned", Self::OnboardingCompleted => "onboarding_completed", + Self::OnboardingImportEligible => "onboarding_import_eligible", + Self::OnboardingImportPublished => "onboarding_import_published", + Self::OnboardingAgentActivated => "onboarding_agent_activated", } } + + /// Server-owned import funnel milestones are never part of the closed + /// client-presentation vocabulary, so Admin must reject them on the + /// public event endpoint. + pub const fn is_server_owned_import_milestone(self) -> bool { + matches!( + self, + Self::OnboardingImportEligible + | Self::OnboardingImportPublished + | Self::OnboardingAgentActivated + ) + } } #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -101,7 +122,7 @@ mod tests { #[test] fn vocabulary_is_closed_and_stable() { - assert_eq!(ProductEventKind::ALL.len(), 6); + assert_eq!(ProductEventKind::ALL.len(), 9); assert_eq!( ProductEventKind::OnboardingCompleted.as_str(), "onboarding_completed" diff --git a/crates/crank-import/Cargo.toml b/crates/crank-import/Cargo.toml index 1c13ebe..185340b 100644 --- a/crates/crank-import/Cargo.toml +++ b/crates/crank-import/Cargo.toml @@ -13,5 +13,6 @@ crank-schema = { path = "../crank-schema" } serde.workspace = true serde_json.workspace = true serde_yaml.workspace = true +sha2.workspace = true thiserror.workspace = true url.workspace = true diff --git a/crates/crank-import/src/rest/decode_budget.rs b/crates/crank-import/src/rest/decode_budget.rs new file mode 100644 index 0000000..d4591c4 --- /dev/null +++ b/crates/crank-import/src/rest/decode_budget.rs @@ -0,0 +1,650 @@ +//! Bounded, no-materialization preflight for JSON/YAML source documents. +//! +//! `serde_yaml` materializes alias expansions into owned `Value` trees, so the +//! existing post-decode `validate_value_limits` runs too late: a small source +//! with few aliases can expand to many MiB before the limit is observed. This +//! module walks the same logical, alias-expanded structure with a +//! `serde::de::Visitor` that stores no values, strings, keys or collection +//! entries, charging an aggregate expanded string/key budget as it goes. +//! +//! Semantics preserved: +//! - the preflight is permissive about duplicate mapping keys, non-finite +//! numbers, tags and non-standard keys; the existing strict/legacy +//! materializers decide those (so their errors are unchanged); +//! - each JSON or YAML parse attempt uses fresh per-document counters, while +//! the shared aggregate budget is not reset and is committed only after a +//! document fully passes, so an abandoned fallback attempt cannot be +//! charged twice; +//! - a budget failure is reported as `ImportParseError::LimitExceeded`, while +//! syntax/duplicate/conversion failures stay `InvalidDocument`. + +use serde::de::{ + self, DeserializeSeed, Deserializer, EnumAccess, IgnoredAny, MapAccess, SeqAccess, + VariantAccess, Visitor, +}; +use std::marker::PhantomData; + +use super::model::{ExternalDocumentSnapshot, NormalizationConfig}; +use super::normalize::ImportParseError; + +/// Internal hard ceiling for expanded string/key bytes in one document set. +/// This is deliberately not a `NormalizationConfig` field. +const HARD_EXPANDED_BYTES_CAP: u64 = 64 * 1024 * 1024; +/// Aggregate allowance per unit of the applicable raw byte limit. +const EXPANSION_FACTOR: u64 = 16; + +#[derive(Clone, Copy)] +pub(super) struct ShapeLimits { + pub max_nodes: u64, + pub max_depth: u64, + pub max_collection_items: u64, + pub max_scalar_bytes: u64, +} + +impl ShapeLimits { + pub(super) fn from_config(config: &NormalizationConfig) -> Self { + Self { + max_nodes: config.max_nodes as u64, + max_depth: config.max_depth as u64, + max_collection_items: config.max_collection_items as u64, + max_scalar_bytes: config.max_scalar_bytes as u64, + } + } +} + +/// `min(16 * max(raw_limit, max_scalar_bytes), 64 MiB)` with checked overflow. +pub(super) fn allowance( + raw_limit: usize, + max_scalar_bytes: usize, +) -> Result { + let base = (raw_limit as u64).max(max_scalar_bytes as u64); + match base.checked_mul(EXPANSION_FACTOR) { + Some(value) => Ok(value.min(HARD_EXPANDED_BYTES_CAP)), + None => Err(ImportParseError::LimitExceeded), + } +} + +/// Shared expanded-byte budget across one document set. `used` is only +/// advanced after a document passes preflight. +pub(super) struct AggregateBudget { + limit: u64, + used: u64, +} + +impl AggregateBudget { + pub(super) fn empty() -> Self { + Self { limit: 0, used: 0 } + } + + pub(super) fn reserve(&mut self, allowance: u64) -> Result<(), ImportParseError> { + self.limit = self + .limit + .checked_add(allowance) + .ok_or(ImportParseError::LimitExceeded)? + .min(HARD_EXPANDED_BYTES_CAP); + Ok(()) + } +} + +struct State { + shape: ShapeLimits, + allowance: u64, + bytes: u64, + nodes: u64, + exceeded: bool, +} + +impl State { + fn new(shape: ShapeLimits, allowance: u64) -> Self { + Self { + shape, + allowance, + bytes: 0, + nodes: 0, + exceeded: false, + } + } +} + +pub(super) fn preflight_document( + document: &str, + shape: ShapeLimits, + allowance: u64, + aggregate: &mut AggregateBudget, +) -> Result<(), ImportParseError> { + match attempt_json(document, shape, allowance, aggregate) { + Attempt::Ok => return Ok(()), + Attempt::Budget => return Err(ImportParseError::LimitExceeded), + Attempt::Syntax => {} + } + match attempt_yaml(document, shape, allowance, aggregate) { + Attempt::Ok => Ok(()), + Attempt::Budget => Err(ImportParseError::LimitExceeded), + Attempt::Syntax => Err(ImportParseError::InvalidDocument), + } +} + +pub(super) fn preflight_single( + document: &str, + shape: ShapeLimits, + raw_limit: usize, +) -> Result<(), ImportParseError> { + let allowance = allowance(raw_limit, shape.max_scalar_bytes as usize)?; + let mut aggregate = AggregateBudget::empty(); + aggregate.reserve(allowance)?; + preflight_document(document, shape, allowance, &mut aggregate) +} + +/// Preflights a primary document using `config.max_bytes`. +pub(super) fn preflight_primary( + document: &str, + config: &NormalizationConfig, +) -> Result<(), ImportParseError> { + preflight_single(document, ShapeLimits::from_config(config), config.max_bytes) +} + +/// Preflights a primary document and its supplied external snapshots as one +/// set with a shared aggregate budget. Raw byte/alias guards remain the +/// caller's responsibility. +pub(super) fn preflight_bundle( + primary: &str, + snapshots: &[ExternalDocumentSnapshot], + config: &NormalizationConfig, +) -> Result<(), ImportParseError> { + let shape = ShapeLimits::from_config(config); + let primary_allowance = allowance(config.max_bytes, config.max_scalar_bytes)?; + let external_allowance = + allowance(config.max_external_document_bytes, config.max_scalar_bytes)?; + let mut aggregate = AggregateBudget::empty(); + aggregate.reserve(primary_allowance)?; + for _ in snapshots { + aggregate.reserve(external_allowance)?; + } + preflight_document(primary, shape, primary_allowance, &mut aggregate)?; + for snapshot in snapshots { + preflight_document( + &snapshot.document, + shape, + external_allowance, + &mut aggregate, + )?; + } + Ok(()) +} + +/// Preflights a raw bundle where every document shares `config.max_bytes`. +pub(super) fn preflight_raw_bundle( + documents: &[&str], + config: &NormalizationConfig, +) -> Result<(), ImportParseError> { + let shape = ShapeLimits::from_config(config); + let allowance = allowance(config.max_bytes, config.max_scalar_bytes)?; + let mut aggregate = AggregateBudget::empty(); + for _ in documents { + aggregate.reserve(allowance)?; + } + for document in documents { + preflight_document(document, shape, allowance, &mut aggregate)?; + } + Ok(()) +} + +enum Attempt { + Ok, + Budget, + Syntax, +} + +fn attempt_json( + document: &str, + shape: ShapeLimits, + allowance: u64, + aggregate: &mut AggregateBudget, +) -> Attempt { + let mut state = State::new(shape, allowance); + let mut de = serde_json::Deserializer::from_str(document); + let seed = Seed { + state: &mut state, + aggregate, + depth: 0, + mode: Mode::Value, + }; + let result = match seed.deserialize(&mut de) { + Ok(()) => de.end(), + Err(error) => Err(error), + }; + finish(result, &state, aggregate) +} + +fn attempt_yaml( + document: &str, + shape: ShapeLimits, + allowance: u64, + aggregate: &mut AggregateBudget, +) -> Attempt { + let mut state = State::new(shape, allowance); + let seed = Seed { + state: &mut state, + aggregate, + depth: 0, + mode: Mode::Value, + }; + let result = seed.deserialize(serde_yaml::Deserializer::from_str(document)); + finish(result, &state, aggregate) +} + +fn finish(result: Result<(), E>, state: &State, aggregate: &mut AggregateBudget) -> Attempt { + if result.is_err() { + return if state.exceeded { + Attempt::Budget + } else { + Attempt::Syntax + }; + } + match aggregate.used.checked_add(state.bytes) { + Some(total) if total <= aggregate.limit => { + aggregate.used = total; + Attempt::Ok + } + _ => Attempt::Budget, + } +} + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Mode { + Value, + Key, +} + +struct Seed<'a> { + state: &'a mut State, + aggregate: &'a mut AggregateBudget, + depth: u64, + mode: Mode, +} + +impl<'de, 'a> DeserializeSeed<'de> for Seed<'a> { + type Value = (); + + fn deserialize(self, deserializer: D) -> Result<(), D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_any(Probe { + state: self.state, + aggregate: self.aggregate, + depth: self.depth, + mode: self.mode, + }) + } +} + +struct Probe<'a> { + state: &'a mut State, + aggregate: &'a mut AggregateBudget, + depth: u64, + mode: Mode, +} + +impl<'a> Probe<'a> { + fn fail(&mut self) -> E { + self.state.exceeded = true; + E::custom("normalization decode budget exceeded") + } + + /// Charges expanded string/key bytes and the per-scalar limit. Returns an + /// internal error that the caller turns into `fail()`. + fn charge(&mut self, len: u64) -> Result<(), ()> { + if len > self.state.shape.max_scalar_bytes { + return Err(()); + } + self.state.bytes = self.state.bytes.checked_add(len).ok_or(())?; + if self.state.bytes > self.state.allowance { + return Err(()); + } + let total = self + .aggregate + .used + .checked_add(self.state.bytes) + .ok_or(())?; + if total > self.aggregate.limit { + return Err(()); + } + Ok(()) + } + + fn enter_value(&mut self) -> Result<(), E> { + if self.depth > self.state.shape.max_depth { + return Err(self.fail()); + } + match self.state.nodes.checked_add(1) { + Some(nodes) => self.state.nodes = nodes, + None => return Err(self.fail()), + } + if self.state.nodes > self.state.shape.max_nodes { + return Err(self.fail()); + } + Ok(()) + } + + /// Bounds depth for composite keys without counting a value node, so + /// ordinary scalar mapping keys keep their existing accounting. + fn check_depth(&mut self) -> Result<(), E> { + if self.depth > self.state.shape.max_depth { + return Err(self.fail()); + } + Ok(()) + } + + fn charge_scalar(&mut self, len: usize) -> Result<(), E> { + if self.charge(len as u64).is_err() { + return Err(self.fail()); + } + Ok(()) + } + + fn value_or_scalar(&mut self, len: Option) -> Result<(), E> { + if self.mode == Mode::Value { + self.enter_value::()?; + } + match len { + Some(len) => self.charge_scalar::(len), + None => Ok(()), + } + } +} + +impl<'de, 'a> Visitor<'de> for Probe<'a> { + type Value = (); + + fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("a JSON- or YAML-compatible value") + } + + fn visit_bool(mut self, _value: bool) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_i64(mut self, _value: i64) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_u64(mut self, _value: u64) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_i128(mut self, _value: i128) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_u128(mut self, _value: u128) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_f64(mut self, _value: f64) -> Result<(), E> { + // Non-finite numbers are deliberately accepted here; the materializer + // decides their validity. + self.value_or_scalar(None) + } + + fn visit_char(mut self, value: char) -> Result<(), E> { + self.value_or_scalar(Some(value.len_utf8())) + } + + fn visit_str(mut self, value: &str) -> Result<(), E> { + self.value_or_scalar(Some(value.len())) + } + + fn visit_string(mut self, value: String) -> Result<(), E> { + self.value_or_scalar(Some(value.len())) + } + + fn visit_bytes(mut self, value: &[u8]) -> Result<(), E> { + self.value_or_scalar(Some(value.len())) + } + + fn visit_byte_buf(mut self, value: Vec) -> Result<(), E> { + self.value_or_scalar(Some(value.len())) + } + + fn visit_unit(mut self) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_none(mut self) -> Result<(), E> { + self.value_or_scalar(None) + } + + fn visit_some(self, deserializer: D) -> Result<(), D::Error> + where + D: Deserializer<'de>, + { + Seed { + state: self.state, + aggregate: self.aggregate, + depth: self.depth, + mode: self.mode, + } + .deserialize(deserializer) + } + + fn visit_newtype_struct(self, deserializer: D) -> Result<(), D::Error> + where + D: Deserializer<'de>, + { + Seed { + state: self.state, + aggregate: self.aggregate, + depth: self.depth + 1, + mode: self.mode, + } + .deserialize(deserializer) + } + + fn visit_seq(mut self, mut seq: A) -> Result<(), A::Error> + where + A: SeqAccess<'de>, + { + match self.mode { + Mode::Value => self.enter_value::()?, + Mode::Key => self.check_depth::()?, + } + let collection_limit = self.state.shape.max_collection_items; + if seq + .size_hint() + .is_some_and(|hint| hint as u64 > collection_limit) + { + return Err(self.fail()); + } + let mut count: u64 = 0; + while seq + .next_element_seed(Seed { + state: &mut *self.state, + aggregate: &mut *self.aggregate, + depth: self.depth + 1, + mode: Mode::Value, + })? + .is_some() + { + count += 1; + if count > self.state.shape.max_collection_items { + return Err(self.fail()); + } + } + Ok(()) + } + + fn visit_map(mut self, mut map: A) -> Result<(), A::Error> + where + A: MapAccess<'de>, + { + match self.mode { + Mode::Value => self.enter_value::()?, + Mode::Key => self.check_depth::()?, + } + let collection_limit = self.state.shape.max_collection_items; + if map + .size_hint() + .is_some_and(|hint| hint as u64 > collection_limit) + { + return Err(self.fail()); + } + let mut count: u64 = 0; + while map + .next_key_seed(Seed { + state: &mut *self.state, + aggregate: &mut *self.aggregate, + depth: self.depth + 1, + mode: Mode::Key, + })? + .is_some() + { + count += 1; + if count > self.state.shape.max_collection_items { + return Err(self.fail()); + } + map.next_value_seed(Seed { + state: &mut *self.state, + aggregate: &mut *self.aggregate, + depth: self.depth + 1, + mode: Mode::Value, + })?; + } + Ok(()) + } + + fn visit_enum(self, data: A) -> Result<(), A::Error> + where + A: EnumAccess<'de>, + { + // Tagged values are traversed permissively and bounded; the + // materializer keeps the final say on tag semantics. + let (_variant, variant) = data.variant_seed(PhantomData::)?; + variant.newtype_variant_seed(Seed { + state: self.state, + aggregate: self.aggregate, + depth: self.depth + 1, + mode: self.mode, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn shape( + max_nodes: u64, + max_depth: u64, + max_collection_items: u64, + max_scalar_bytes: u64, + ) -> ShapeLimits { + ShapeLimits { + max_nodes, + max_depth, + max_collection_items, + max_scalar_bytes, + } + } + + fn wide() -> ShapeLimits { + shape(1_000, 64, 1_000, 1_000) + } + + fn run( + document: &str, + shape: ShapeLimits, + allowance: u64, + limit: u64, + ) -> Result<(), ImportParseError> { + let mut aggregate = AggregateBudget::empty(); + aggregate.limit = limit; + preflight_document(document, shape, allowance, &mut aggregate) + } + + #[test] + fn exact_aggregate_boundary_is_inclusive() { + let shape = wide(); + assert!(run("\"abc\"", shape, 3, 3).is_ok()); + assert_eq!( + run("\"abc\"", shape, 2, 3), + Err(ImportParseError::LimitExceeded) + ); + } + + #[test] + fn scalar_depth_collection_and_node_limits_fail_before_materialization() { + let scalar = shape(1_000, 64, 1_000, 2); + assert_eq!( + run("\"abc\"", scalar, 1_000, 1_000), + Err(ImportParseError::LimitExceeded) + ); + + let depth = shape(1_000, 1, 1_000, 1_000); + assert_eq!( + run("{\"a\":{\"b\":1}}", depth, 1_000, 1_000), + Err(ImportParseError::LimitExceeded) + ); + + let collection = shape(1_000, 64, 1, 1_000); + assert_eq!( + run("[1,2]", collection, 1_000, 1_000), + Err(ImportParseError::LimitExceeded) + ); + + let nodes = shape(1, 64, 1_000, 1_000); + assert_eq!( + run("{\"a\":1}", nodes, 1_000, 1_000), + Err(ImportParseError::LimitExceeded) + ); + } + + #[test] + fn mapping_keys_do_not_consume_value_nodes() { + let node_limited = shape(3, 64, 1_000, 1_000); + assert!(run("{\"a\":1,\"b\":2}", node_limited, 1_000, 1_000).is_ok()); + let stricter = shape(2, 64, 1_000, 1_000); + assert_eq!( + run("{\"a\":1,\"b\":2}", stricter, 1_000, 1_000), + Err(ImportParseError::LimitExceeded) + ); + } + + #[test] + fn composite_mapping_keys_are_depth_bounded() { + // A mapping used as a mapping key, whose own key is again a mapping, + // must not evade `max_depth`; the fix increases key-child depth. + let document = "? {? {a: b}: c}\n: value\n"; + let shallow = shape(1_000, 1, 1_000, 1_000); + assert_eq!( + run(document, shallow, 1_000, 1_000), + Err(ImportParseError::LimitExceeded) + ); + let deep = shape(1_000, 64, 1_000, 1_000); + assert!(run(document, deep, 1_000, 1_000).is_ok()); + } + + #[test] + fn shared_aggregate_is_not_reset_between_documents() { + let shape = wide(); + let mut aggregate = AggregateBudget::empty(); + aggregate.limit = 7; + assert!(preflight_document("\"abcd\"", shape, 5, &mut aggregate).is_ok()); + assert_eq!( + preflight_document("\"efgh\"", shape, 5, &mut aggregate), + Err(ImportParseError::LimitExceeded) + ); + } + + #[test] + fn reserve_caps_and_allowance_is_overflow_safe() { + let mut aggregate = AggregateBudget::empty(); + aggregate.reserve(HARD_EXPANDED_BYTES_CAP).unwrap(); + aggregate.reserve(HARD_EXPANDED_BYTES_CAP).unwrap(); + assert_eq!(aggregate.limit, HARD_EXPANDED_BYTES_CAP); + assert_eq!( + allowance(usize::MAX, usize::MAX), + Err(ImportParseError::LimitExceeded) + ); + } +} diff --git a/crates/crank-import/src/rest/http_methods.rs b/crates/crank-import/src/rest/http_methods.rs index 3633857..9fd3ba8 100644 --- a/crates/crank-import/src/rest/http_methods.rs +++ b/crates/crank-import/src/rest/http_methods.rs @@ -21,5 +21,6 @@ pub(super) fn invalid_path_item() -> ImportFinding { severity: ImportFindingSeverity::Error, message: "Path Item имеет неверную структуру и не был интерпретирован.".to_owned(), operation_key: None, + source_location: None, } } diff --git a/crates/crank-import/src/rest/legacy_payload.rs b/crates/crank-import/src/rest/legacy_payload.rs index 93c7558..7c168da 100644 --- a/crates/crank-import/src/rest/legacy_payload.rs +++ b/crates/crank-import/src/rest/legacy_payload.rs @@ -127,8 +127,10 @@ pub fn candidate_from_operation( key: operation.key.clone(), method: operation.method, path: operation.path.clone(), + status: String::new(), + source_location: operation.source_location.clone(), security_requirements: Default::default(), - security_schemes: Default::default(), + security_schemes: BTreeMap::new(), operation_id: operation.operation_id.clone(), suggested_name: name, suggested_display_name: display_name, diff --git a/crates/crank-import/src/rest/metadata_findings.rs b/crates/crank-import/src/rest/metadata_findings.rs index 5d23e00..8f09256 100644 --- a/crates/crank-import/src/rest/metadata_findings.rs +++ b/crates/crank-import/src/rest/metadata_findings.rs @@ -1,70 +1,444 @@ use serde_json::Value; +use std::collections::BTreeSet; use crate::rest::{ model::{ImportFinding, SourceLocation}, recommendations::document_finding, }; -pub(super) fn collect_secret_like_examples( - value: &Value, +pub(super) const REDACTED_LITERAL: &str = "[REDACTED]"; + +/// Redacts credential-like literals before they enter the normalized tree, +/// while retaining stable source locations for review findings. +pub(super) fn redact_secret_like_literals( + value: &mut Value, pointer: &str, - inside_example: bool, findings: &mut Vec, locations: &mut Vec>, ) { + let declared_names = credential_names(std::iter::once(&*value)); + redact_secret_like_literals_with_names(value, pointer, &declared_names, findings, locations); +} + +pub(super) fn redact_secret_like_literals_with_names( + value: &mut Value, + pointer: &str, + declared_names: &CredentialNames, + findings: &mut Vec, + locations: &mut Vec>, +) { + visit( + value, + pointer, + VisitContext { + in_secret_example: false, + sensitive_parameter_schema: false, + force_redact: false, + declared_names, + }, + findings, + locations, + ); +} + +#[derive(Default)] +pub(super) struct CredentialNames { + header: BTreeSet, + query: BTreeSet, + cookie: BTreeSet, +} + +#[derive(Clone, Copy)] +struct VisitContext<'a> { + in_secret_example: bool, + sensitive_parameter_schema: bool, + force_redact: bool, + declared_names: &'a CredentialNames, +} + +impl CredentialNames { + fn contains(&self, location: &str, name: &str) -> bool { + let names = match location.to_ascii_lowercase().as_str() { + "header" => &self.header, + "query" => &self.query, + "cookie" => &self.cookie, + _ => return false, + }; + names.contains(&name.to_ascii_lowercase()) + } +} + +pub(super) fn credential_names<'a>(roots: impl IntoIterator) -> CredentialNames { + let mut names = CredentialNames::default(); + for root in roots { + collect_credential_names(root, &mut names); + } + names +} + +fn collect_credential_names(value: &Value, names: &mut CredentialNames) { match value { Value::Object(fields) => { + if fields.get("type").and_then(Value::as_str) == Some("apiKey") + && let (Some(location), Some(name)) = ( + fields.get("in").and_then(Value::as_str), + fields.get("name").and_then(Value::as_str), + ) + { + let destination = match location.to_ascii_lowercase().as_str() { + "header" => Some(&mut names.header), + "query" => Some(&mut names.query), + "cookie" => Some(&mut names.cookie), + _ => None, + }; + if let Some(destination) = destination { + destination.insert(name.to_ascii_lowercase()); + } + } + for child in fields.values() { + collect_credential_names(child, names); + } + } + Value::Array(items) => { + for child in items { + collect_credential_names(child, names); + } + } + _ => {} + } +} + +fn visit( + value: &mut Value, + pointer: &str, + context: VisitContext<'_>, + findings: &mut Vec, + locations: &mut Vec>, +) { + if context.force_redact + || value + .as_str() + .is_some_and(|text| url_has_secret_named(text, context.declared_names)) + || (context.in_secret_example && value.as_str() == Some(REDACTED_LITERAL)) + || (context.in_secret_example && scalar_looks_secret(value)) + { + redact(value, context.force_redact, context.declared_names); + findings.push(document_finding( + "secret_like_example", + "В исходном документе обнаружено значение, похожее на секрет; оно удалено из импортируемых данных.", + )); + locations.push(Some(SourceLocation { + pointer: pointer.to_owned(), + })); + return; + } + + match value { + Value::Object(fields) => { + let sensitive_parameter = + fields + .get("in") + .and_then(Value::as_str) + .is_some_and(|location| { + ["header", "query", "cookie"] + .iter() + .any(|allowed| location.eq_ignore_ascii_case(allowed)) + }) + && fields + .get("name") + .and_then(Value::as_str) + .is_some_and(|name| { + is_sensitive_name(name) + || fields.get("in").and_then(Value::as_str).is_some_and( + |location| context.declared_names.contains(location, name), + ) + }); for (key, child) in fields { let child_pointer = format!( "{pointer}/{}", super::normalize_coverage::escape_pointer(key) ); - collect_secret_like_examples( + let key_lower = key.to_ascii_lowercase(); + let in_example = context.in_secret_example + || matches!( + key_lower.as_str(), + "default" + | "example" + | "examples" + | "x-example" + | "x-examples" + | "bearerformat" + | "x-format-hint" + ); + let sensitive_header_definition = pointer.ends_with("/headers") + && (is_sensitive_name(key) || context.declared_names.contains("header", key)); + let parameter_value = (sensitive_parameter || sensitive_header_definition) + && matches!( + key_lower.as_str(), + "example" | "examples" | "x-example" | "x-examples" + ); + let parameter_schema = context.sensitive_parameter_schema + || sensitive_header_definition + || (sensitive_parameter && key_lower == "schema"); + let parent_is_secret_property = + pointer.rsplit('/').next().is_some_and(is_sensitive_name); + // Names in schema/scheme maps and security requirements are + // identifiers, not credential values. Traverse their metadata + // and redact the actual defaults/examples instead. + let identifier_map = (child.is_object() + && (pointer.ends_with("/properties") + || pointer.ends_with("/headers") + || pointer == "/components/schemas" + || pointer == "/definitions" + || pointer == "/components/securitySchemes" + || pointer == "/securityDefinitions")) + || (child.is_array() + && pointer.rsplit_once('/').is_some_and(|(parent, index)| { + parent.ends_with("/security") && index.parse::().is_ok() + })); + let force_child = (is_sensitive_name(key) && !identifier_map) + || (matches!(key_lower.as_str(), "default" | "example" | "examples") + && parent_is_secret_property) + || parameter_value + || (sensitive_parameter + && matches!(key_lower.as_str(), "default" | "example" | "examples")) + || (parameter_schema + && matches!(key_lower.as_str(), "default" | "example" | "examples")); + visit( child, &child_pointer, - inside_example || matches!(key.as_str(), "example" | "examples"), + VisitContext { + in_secret_example: in_example, + sensitive_parameter_schema: parameter_schema, + force_redact: force_child, + ..context + }, findings, locations, ); } } Value::Array(items) => { - for (index, child) in items.iter().enumerate() { - collect_secret_like_examples( + for (index, child) in items.iter_mut().enumerate() { + visit( child, &format!("{pointer}/{index}"), - inside_example, + VisitContext { + force_redact: false, + ..context + }, findings, locations, ); } } - Value::String(value) if inside_example && looks_secret_like(pointer, value) => { - findings.push(document_finding( - "secret_like_example", - "Example содержит значение, похожее на секрет, и не включается в executable contract.", - )); - locations.push(Some(SourceLocation { - pointer: pointer.to_owned(), - })); - } _ => {} } } -fn looks_secret_like(pointer: &str, value: &str) -> bool { - let pointer = pointer.to_ascii_lowercase(); +fn is_sensitive_name(name: &str) -> bool { + let lower = name.to_ascii_lowercase(); + if lower.starts_with('/') + || matches!( + lower.as_str(), + "url" + | "tokenurl" + | "authorizationurl" + | "refreshurl" + | "openidconnecturl" + | "clientcredentials" + | "authorizationcode" + ) + { + return false; + } + let compact = lower.replace(['-', '_'], ""); + [ + "token", + "secret", + "password", + "credential", + "authorization", + "apikey", + "accesskey", + "ciphertext", + "privatekey", + "passwd", + ] + .iter() + .any(|marker| compact.contains(marker)) +} + +fn scalar_looks_secret(value: &Value) -> bool { + let Value::String(value) = value else { + return false; + }; let value = value.to_ascii_lowercase(); [ "token", "secret", "password", + "credential", + "authorization", "api_key", "apikey", - "authorization", ] .iter() - .any(|marker| pointer.contains(marker) || value.contains(marker)) + .any(|marker| value.contains(marker)) || value.starts_with("bearer ") || value.starts_with("sk-") + || looks_like_jwt(&value) +} + +fn looks_like_jwt(value: &str) -> bool { + let mut segments = value.split('.'); + let (Some(header), Some(payload), Some(signature), None) = ( + segments.next(), + segments.next(), + segments.next(), + segments.next(), + ) else { + return false; + }; + header.starts_with("eyj") + && !payload.is_empty() + && signature.len() >= 8 + && [header, payload, signature].iter().all(|segment| { + segment + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')) + }) +} + +pub(super) fn url_has_secret(value: &str) -> bool { + url_has_secret_named(value, &CredentialNames::default()) +} + +fn url_has_secret_named(value: &str, declared_names: &CredentialNames) -> bool { + url_userinfo(value).is_some() + || query_has_secret_parameter(value, &declared_names.query) + || fragment_has_secret_parameter(value) +} + +fn fragment_has_secret_parameter(value: &str) -> bool { + let Some((_, fragment)) = value.split_once('#') else { + return false; + }; + if fragment.starts_with('/') { + return false; + } + url::form_urlencoded::parse(fragment.as_bytes()).any(|(name, _)| is_sensitive_name(&name)) +} + +fn redact_url_fragment(value: &str) -> String { + let Some((base, fragment)) = value.split_once('#') else { + return value.to_owned(); + }; + if fragment.starts_with('/') || !fragment.contains('=') { + return value.to_owned(); + } + let redacted = fragment + .split('&') + .map(|pair| { + let (name, parameter_value) = pair.split_once('=').unwrap_or((pair, "")); + let decoded_name = url::form_urlencoded::parse(format!("{name}=").as_bytes()) + .next() + .map(|(name, _)| name.into_owned()) + .unwrap_or_default(); + if is_sensitive_name(&decoded_name) { + format!("{name}={REDACTED_LITERAL}") + } else if pair.contains('=') { + format!("{name}={parameter_value}") + } else { + name.to_owned() + } + }) + .collect::>() + .join("&"); + format!("{base}#{redacted}") +} + +fn url_userinfo(value: &str) -> Option<(&str, &str, &str)> { + let (scheme, rest) = value + .split_once("://") + .or_else(|| value.strip_prefix("//").map(|rest| ("", rest)))?; + let (authority, suffix) = rest + .find(['/', '?', '#']) + .map_or((rest, ""), |index| (&rest[..index], &rest[index..])); + let (userinfo, host) = authority.split_once('@')?; + if userinfo.is_empty() || host.is_empty() { + return None; + } + Some((scheme, host, suffix)) +} + +fn query_has_secret_parameter(value: &str, declared_query_names: &BTreeSet) -> bool { + let Some((_, query)) = value.split_once('?') else { + return false; + }; + let query = query.split('#').next().unwrap_or(query); + url::form_urlencoded::parse(query.as_bytes()).any(|(name, _)| { + is_sensitive_name(&name) || declared_query_names.contains(&name.to_ascii_lowercase()) + }) +} + +fn redact(value: &mut Value, force: bool, declared_names: &CredentialNames) { + if force + || !value + .as_str() + .is_some_and(|text| url_has_secret_named(text, declared_names)) + { + *value = Value::String(REDACTED_LITERAL.to_owned()); + return; + } + if let Value::String(text) = value { + if force { + *text = REDACTED_LITERAL.to_owned(); + return; + } + let mut sanitized = text.clone(); + if let Some((scheme, host, suffix)) = url_userinfo(&sanitized) { + sanitized = if scheme.is_empty() { + format!("//{host}{suffix}") + } else { + format!("{scheme}://{host}{suffix}") + }; + } + if query_has_secret_parameter(&sanitized, &declared_names.query) + && let Some((base, query)) = sanitized.split_once('?') + { + let (query, fragment) = query + .split_once('#') + .map_or((query, None), |(query, fragment)| (query, Some(fragment))); + let redacted = query + .split('&') + .map(|pair| { + let (name, value) = pair.split_once('=').unwrap_or((pair, "")); + let decoded_name = url::form_urlencoded::parse(format!("{name}=").as_bytes()) + .next() + .map(|(name, _)| name.into_owned()) + .unwrap_or_default(); + if is_sensitive_name(&decoded_name) + || declared_names + .query + .contains(&decoded_name.to_ascii_lowercase()) + { + format!("{name}={REDACTED_LITERAL}") + } else if pair.contains('=') { + format!("{name}={value}") + } else { + name.to_owned() + } + }) + .collect::>() + .join("&"); + sanitized = match fragment { + Some(fragment) => format!("{base}?{redacted}#{fragment}"), + None => format!("{base}?{redacted}"), + }; + } + *text = redact_url_fragment(&sanitized); + } else { + *value = Value::String(REDACTED_LITERAL.to_owned()); + } } diff --git a/crates/crank-import/src/rest/mod.rs b/crates/crank-import/src/rest/mod.rs index 53134d1..1ddcfb8 100644 --- a/crates/crank-import/src/rest/mod.rs +++ b/crates/crank-import/src/rest/mod.rs @@ -1,36 +1,39 @@ +mod decode_budget; mod http_methods; mod legacy_mapping; mod legacy_payload; mod legacy_schema; mod mapping; +mod metadata_findings; pub mod model; mod naming; mod normalize; mod normalize_coverage; mod normalize_finding_validation; mod normalize_limits; -mod normalize_preview; mod normalize_projection; mod normalize_schema; +mod normalize_security; mod openapi3; mod openapi3_findings; -mod openapi3_legacy; +mod parser_findings; mod payload; mod recommendations; mod reference; +mod reference_budget; mod schema; mod security; -mod security_literals; +mod source_safety; +mod strict_decode; mod swagger2; mod swagger2_findings; -mod swagger2_legacy; pub use model::{ ExternalDocumentSnapshot, HISTORICAL_NORMALIZER_VERSION, HISTORICAL_PROJECTION_VERSION, - ImportFinding, ImportFindingSeverity, ImportGroupPreview, ImportOperationCandidate, - ImportPreview, ImportSourcePreview, NORMALIZER_VERSION, NormalizationConfig, NormalizedFinding, - NormalizedIr, NormalizedOperation, NormalizedParameter, NormalizedReference, - NormalizedRequestBodyVariant, NormalizedResponseVariant, NormalizedSchema, + ImportFinding, ImportFindingSeverity, ImportGroupPreview, ImportNameConflict, + ImportOperationCandidate, ImportPreview, ImportSourcePreview, NORMALIZER_VERSION, + NormalizationConfig, NormalizedFinding, NormalizedIr, NormalizedOperation, NormalizedParameter, + NormalizedReference, NormalizedRequestBodyVariant, NormalizedResponseVariant, NormalizedSchema, NormalizedSchemaConstraints, NormalizedSchemaKind, NormalizedSecurityRequirements, NormalizedSecurityScheme, NormalizedSecuritySchemeKind, PROJECTION_VERSION, ResolvedReferenceEdge, ResolvedReferenceGraph, ResolvedReferenceNode, RestImportCandidate, @@ -42,7 +45,9 @@ pub use model::{ pub use normalize::{ ImportParseError, external_reference_uris, normalize_verified_bundle, normalize_verified_document, preview_document, preview_document_legacy_v1, preview_from_ir, - reference_uris, validate_normalized_ir, + reference_uris, validate_document_source_safe, validate_normalized_ir, }; pub use payload::operation_draft_from_candidate; -pub use security_literals::{SecretLiteralFinding, scan_secret_literals}; +pub use source_safety::{ + validate_document_bundle_source_safe, validate_resolved_bundle_source_safe, +}; diff --git a/crates/crank-import/src/rest/model.rs b/crates/crank-import/src/rest/model.rs index 648aac6..1bfeca5 100644 --- a/crates/crank-import/src/rest/model.rs +++ b/crates/crank-import/src/rest/model.rs @@ -10,7 +10,7 @@ use serde_json::Value; /// deliberately travel with an import job: changing either contract must not /// silently reinterpret a pending preview. pub const NORMALIZER_VERSION: &str = "normalized-ir-v5"; -pub const PROJECTION_VERSION: &str = "preview-v5"; +pub const PROJECTION_VERSION: &str = "preview-v6"; pub const HISTORICAL_NORMALIZER_VERSION: &str = "normalized-ir-v4"; pub const HISTORICAL_PROJECTION_VERSION: &str = "preview-v4"; @@ -496,6 +496,10 @@ pub struct ImportFinding { pub message: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub operation_key: Option, + /// RFC 6901 source pointer of the finding. Optional so preview payloads + /// persisted before this field was introduced still deserialize. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub source_location: Option, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -514,6 +518,17 @@ pub struct ImportPreview { pub groups: Vec, #[serde(default)] pub findings: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub conflicts: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ImportNameConflict { + pub operation_key: String, + pub conflict_type: String, + pub proposed_name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub source_location: Option, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -528,6 +543,13 @@ pub struct ImportOperationCandidate { pub key: String, pub method: HttpMethod, pub path: String, + /// Workspace-aware readiness is attached by the API after immutable replay. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub status: String, + /// RFC 6901 pointer of the source operation. Optional so preview payloads + /// persisted before this field was introduced still deserialize. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub source_location: Option, #[serde( default, skip_serializing_if = "NormalizedSecurityRequirements::is_empty" @@ -583,6 +605,10 @@ pub struct RestImportOperation { pub key: String, pub method: HttpMethod, pub path: String, + /// Authoritative RFC 6901 pointer of the operation when it was produced by + /// the normalizer. Parser state only; never persisted. + #[serde(skip)] + pub source_location: Option, #[serde( default, skip_serializing_if = "NormalizedSecurityRequirements::is_empty" diff --git a/crates/crank-import/src/rest/normalize.rs b/crates/crank-import/src/rest/normalize.rs index 131b725..f02287e 100644 --- a/crates/crank-import/src/rest/normalize.rs +++ b/crates/crank-import/src/rest/normalize.rs @@ -15,6 +15,7 @@ use crate::rest::{ openapi3, swagger2, }; +use super::decode_budget; use super::normalize_coverage; use super::normalize_limits; @@ -61,9 +62,9 @@ pub fn external_reference_uris( pub fn preview_document_legacy_v1(document: &str) -> Result { let root = decode_legacy_v1(document)?; let mut parsed = match root.get("openapi") { - Some(_) => super::openapi3_legacy::parse_document_legacy_v1(&root)?, + Some(_) => openapi3::parse_document_legacy_v1(&root)?, None if root.get("swagger").and_then(Value::as_str) == Some("2.0") => { - super::swagger2_legacy::parse_document_legacy_v1(&root)? + swagger2::parse_document_legacy_v1(&root)? } None => return Err(ImportParseError::UnsupportedDocument), }; @@ -80,7 +81,7 @@ pub fn preview_document_legacy_v1(document: &str) -> Result Result { @@ -90,6 +91,7 @@ fn decode_legacy_v1(document: &str) -> Result { { return Err(ImportParseError::LimitExceeded); } + decode_budget::preflight_primary(document, &config)?; let root = decode(document)?; validate_value_limits(&root, &config)?; Ok(root) @@ -103,6 +105,39 @@ pub fn normalize_verified_document( normalize_verified_bundle(document, digest, &[], config) } +/// Rejects source documents containing credential-like literals before a +/// caller persists their original bytes. Accepted source bytes remain verbatim. +pub fn validate_document_source_safe( + document: &str, + config: &NormalizationConfig, +) -> Result<(), ImportParseError> { + config + .validate_versions() + .map_err(|_| ImportParseError::UnsupportedDocument)?; + if document.len() > config.max_bytes + || normalize_limits::alias_count(document) > config.max_aliases + { + return Err(ImportParseError::LimitExceeded); + } + decode_budget::preflight_primary(document, config)?; + let mut root = + super::strict_decode::decode(document).map_err(|_| ImportParseError::InvalidDocument)?; + validate_value_limits(&root, config)?; + let mut findings = Vec::new(); + let mut locations = Vec::new(); + super::metadata_findings::redact_secret_like_literals( + &mut root, + "", + &mut findings, + &mut locations, + ); + if findings.is_empty() { + Ok(()) + } else { + Err(ImportParseError::InvalidDocument) + } +} + /// Normalizes a verified primary document together with immutable external /// snapshots. This function is pure: callers must perform all URL policy, /// fetching, artifact persistence and digest verification beforehand. @@ -120,12 +155,24 @@ pub fn normalize_verified_bundle( { return Err(ImportParseError::LimitExceeded); } + if snapshots.len() > config.max_reference_documents { + return Err(ImportParseError::LimitExceeded); + } + for snapshot in snapshots { + if snapshot.document.len() > config.max_external_document_bytes + || normalize_limits::alias_count(&snapshot.document) > config.max_aliases + { + return Err(ImportParseError::LimitExceeded); + } + } + decode_budget::preflight_bundle(document, snapshots, config)?; let source_syntax = if serde_json::from_str::(document).is_ok() { SourceSyntax::Json } else { SourceSyntax::Yaml }; - let root = decode(document)?; + let root = + super::strict_decode::decode(document).map_err(|_| ImportParseError::InvalidDocument)?; validate_value_limits(&root, config)?; for snapshot in snapshots { if snapshot.document.len() > config.max_external_document_bytes { @@ -136,7 +183,14 @@ pub fn normalize_verified_bundle( let mut root = resolution.root; validate_value_limits(&root, config)?; - let redacted_literals = super::security_literals::inspect_and_redact(&mut root); + let mut literal_findings = Vec::new(); + let mut literal_locations = Vec::new(); + super::metadata_findings::redact_secret_like_literals( + &mut root, + "", + &mut literal_findings, + &mut literal_locations, + ); let mut parsed = match root.get("openapi").and_then(Value::as_str) { Some(version) if supported_oas_version(version) => openapi3::parse_document(&root)?, @@ -146,15 +200,8 @@ pub fn normalize_verified_bundle( } None => return Err(ImportParseError::UnsupportedDocument), }; - for literal in redacted_literals { - parsed.findings.push(super::recommendations::document_finding( - "secret_literal_redacted", - "Документ содержит literal credential; значение удалено из normalized representation.", - )); - parsed - .internal_finding_locations - .push(Some(literal.location)); - } + parsed.findings.extend(literal_findings); + parsed.internal_finding_locations.extend(literal_locations); if parsed.operations.is_empty() && !parsed @@ -176,7 +223,11 @@ pub fn normalize_verified_bundle( } pub fn preview_from_ir(ir: &NormalizedIr) -> ImportPreview { - super::normalize_preview::preview_from_ir(ir) + super::normalize_projection::preview_from_ir(ir) +} + +fn preview_from_document(document: RestImportDocument) -> ImportPreview { + super::normalize_projection::preview_from_document(document) } pub(super) fn decode(document: &str) -> Result { @@ -205,7 +256,6 @@ fn canonicalize( reference_graph: crate::rest::model::ResolvedReferenceGraph, resolution_findings: Vec, ) -> Result { - let security_schemes = document.security_schemes.clone(); let source = ImportSourcePreview { format: document.format, version: document.version, @@ -505,8 +555,6 @@ fn canonicalize( key: operation.key, method: operation.method, path: operation.path, - security_requirements: operation.security_requirements, - security_schemes: security_schemes.clone(), operation_id: operation.operation_id, summary: operation.summary, description: operation.description, @@ -517,6 +565,8 @@ fn canonicalize( request_body_variants, response_variants, servers: operation.servers, + security_requirements: operation.security_requirements, + security_schemes: document.security_schemes.clone(), findings: operation_findings, }) }) @@ -708,11 +758,11 @@ fn canonicalize( } pub fn validate_normalized_ir(ir: &NormalizedIr) -> Result<(), ImportParseError> { - let current = - ir.normalizer_version == NORMALIZER_VERSION && ir.projection_version == PROJECTION_VERSION; - let historical_v4 = ir.normalizer_version == super::model::HISTORICAL_NORMALIZER_VERSION - && ir.projection_version == super::model::HISTORICAL_PROJECTION_VERSION; - if !current && !historical_v4 { + if !((ir.normalizer_version == NORMALIZER_VERSION + && ir.projection_version == PROJECTION_VERSION) + || (ir.normalizer_version == crate::rest::model::HISTORICAL_NORMALIZER_VERSION + && ir.projection_version == crate::rest::model::HISTORICAL_PROJECTION_VERSION)) + { return Err(ImportParseError::InvalidDocument); } validate_reference_graph(ir)?; diff --git a/crates/crank-import/src/rest/normalize_coverage.rs b/crates/crank-import/src/rest/normalize_coverage.rs index 03186c8..0969abf 100644 --- a/crates/crank-import/src/rest/normalize_coverage.rs +++ b/crates/crank-import/src/rest/normalize_coverage.rs @@ -4,9 +4,9 @@ use serde_json::Value; use crate::rest::model::{ CanonicalSourceNode, CanonicalSourceValue, CoverageDisposition, CoverageEntry, - ImportSourcePreview, NORMALIZER_VERSION, NormalizedIr, NormalizedOperation, NormalizedPath, - NormalizedReference, NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION, - SourceLocation, + HISTORICAL_NORMALIZER_VERSION, HISTORICAL_PROJECTION_VERSION, ImportSourcePreview, + NORMALIZER_VERSION, NormalizedIr, NormalizedOperation, NormalizedPath, NormalizedReference, + NormalizedSchema, NormalizedSchemaKind, PROJECTION_VERSION, SourceLocation, }; use super::ImportParseError; @@ -16,11 +16,11 @@ pub(super) use super::normalize_schema::{ }; pub(super) fn validate_coverage(ir: &NormalizedIr) -> Result<(), ImportParseError> { - let current = - ir.normalizer_version == NORMALIZER_VERSION && ir.projection_version == PROJECTION_VERSION; - let historical_v4 = ir.normalizer_version == super::model::HISTORICAL_NORMALIZER_VERSION - && ir.projection_version == super::model::HISTORICAL_PROJECTION_VERSION; - if !current && !historical_v4 { + if !((ir.normalizer_version == NORMALIZER_VERSION + && ir.projection_version == PROJECTION_VERSION) + || (ir.normalizer_version == HISTORICAL_NORMALIZER_VERSION + && ir.projection_version == HISTORICAL_PROJECTION_VERSION)) + { return Err(ImportParseError::InvalidDocument); } let mut source_nodes = BTreeMap::new(); @@ -98,15 +98,17 @@ pub(super) fn validate_coverage(ir: &NormalizedIr) -> Result<(), ImportParseErro .flat_map(|operation| &operation.findings), ) .collect::>(); - if findings.iter().any(|finding| { - is_security_finding_code(&finding.code) - && !super::normalize_finding_validation::matches_source(finding, ir, &source_nodes) - }) { - return Err(ImportParseError::InvalidDocument); - } for finding in &findings { - if super::normalize_finding_validation::matches_source(finding, ir, &source_nodes) - && finding.construct_id == format!("{version}:{}", finding.location.pointer) + let source_match = + super::normalize_finding_validation::matches_source(finding, ir, &source_nodes); + if matches!( + finding.code.as_str(), + "path_parameter_not_declared" | "unsupported_compound_default" + ) && !source_match + { + return Err(ImportParseError::InvalidDocument); + } + if source_match && finding.construct_id == format!("{version}:{}", finding.location.pointer) { insert_real_expected( &mut expected, @@ -155,20 +157,6 @@ pub(super) fn validate_coverage(ir: &NormalizedIr) -> Result<(), ImportParseErro Ok(()) } -fn is_security_finding_code(code: &str) -> bool { - matches!( - code, - "invalid_security_requirements" - | "invalid_security_requirement" - | "unresolved_security_scheme" - | "unsupported_security_and_requirement" - | "unsupported_security_scheme" - | "unsupported_security_requirements" - | "invalid_security_scopes" - | "unexpected_security_scopes" - ) -} - #[derive(Clone, Debug, PartialEq, Eq)] struct ExpectedCoverage { location: SourceLocation, diff --git a/crates/crank-import/src/rest/normalize_finding_validation.rs b/crates/crank-import/src/rest/normalize_finding_validation.rs index 6d804ee..bfb1906 100644 --- a/crates/crank-import/src/rest/normalize_finding_validation.rs +++ b/crates/crank-import/src/rest/normalize_finding_validation.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; use crate::rest::model::{ CanonicalSourceNode, CanonicalSourceValue, ImportFindingSeverity, NormalizedFinding, - NormalizedIr, NormalizedOperation, NormalizedSecuritySchemeKind, SecurityRequirementsSource, + NormalizedIr, }; pub(super) fn matches_source( @@ -48,164 +48,184 @@ pub(super) fn matches_source( finding.operation_key.as_deref() == Some(operation.key.as_str()) && matches!(&node.value, CanonicalSourceValue::Object(fields) if matches!(fields.get("name").map(|node| &node.value), Some(CanonicalSourceValue::String(name)) if !operation.path.contains(&format!("{{{name}}}")))) }), + "path_parameter_not_declared" if is_error => { + path_parameter_missing_in_source(finding, ir, node, nodes) + } + "unsupported_compound_default" if is_error => { + pointer.ends_with("/default") + && matches!(node.value, CanonicalSourceValue::Object(_) | CanonicalSourceValue::Array(_)) + } "unsupported_schema_keyword" if is_error => schema_keyword(pointer, node), "unsupported_schema_enum" if is_error => { pointer.ends_with("/enum") && matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().any(|item| !matches!(item.value, CanonicalSourceValue::String(_)))) } "secret_like_example" if finding.severity == ImportFindingSeverity::Warning => { - pointer.contains("/example") && matches!(&node.value, CanonicalSourceValue::String(value) if secret_like(pointer, value)) + matches!(&node.value, CanonicalSourceValue::String(value) + if secret_like(pointer, value) + || super::metadata_findings::url_has_secret(value) + || (pointer.ends_with("/url") && !value.contains('@')) + || value == super::metadata_findings::REDACTED_LITERAL + || sanitized_secret_url(value)) } - "secret_literal_redacted" if finding.severity == ImportFindingSeverity::Warning => { - matches!(&node.value, CanonicalSourceValue::String(value) if value == "[REDACTED]") - } - "invalid_security_requirements" - | "invalid_security_requirement" - | "unresolved_security_scheme" - | "unsupported_security_and_requirement" - | "unsupported_security_scheme" - | "unsupported_security_requirements" - | "invalid_security_scopes" - | "unexpected_security_scopes" => security_finding(finding, ir, nodes), "invalid_server" if is_error => invalid_server(pointer, node), + "unsupported_security_requirement" if is_error => { + matches!(&node.value, CanonicalSourceValue::Object(fields) if !fields.is_empty()) + || invalid_security_requirement(pointer, node) + } + "invalid_security_requirements" if is_error => { + !matches!(&node.value, CanonicalSourceValue::Array(_)) + || matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().any(|item| !matches!(item.value, CanonicalSourceValue::Object(_)))) + } + "invalid_security_requirement" if finding.severity == ImportFindingSeverity::Warning => { + !matches!(&node.value, CanonicalSourceValue::Object(_)) + } + "unsupported_security_requirements" if is_error => ir + .operations + .iter() + .find(|operation| finding.operation_key.as_deref() == Some(operation.key.as_str())) + .is_some_and(|operation| { + !operation.security_requirements.alternatives.is_empty() + && operation + .security_requirements + .alternatives + .iter() + .all(|alternative| !alternative.supported) + }), + "unsupported_security_and_requirement" + if finding.severity == ImportFindingSeverity::Warning => + { + matches!(&node.value, CanonicalSourceValue::Object(fields) if fields.len() > 1) + } + "unsupported_security_scheme" if finding.severity == ImportFindingSeverity::Warning => { + let scheme_name = pointer + .rsplit('/') + .next() + .unwrap_or_default() + .replace("~1", "/") + .replace("~0", "~"); + ir.security_schemes.get(&scheme_name).is_some_and(|scheme| { + matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { .. }) + || matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::ApiKey { location: crate::rest::model::SecurityParameterLocation::Cookie, .. }) + }) && matches!(&node.value, CanonicalSourceValue::Array(_)) + } + "unsupported_security_scheme_definition" + if finding.severity == ImportFindingSeverity::Warning => + { + let scheme_name = pointer + .rsplit('/') + .next() + .unwrap_or_default() + .replace("~1", "/") + .replace("~0", "~"); + ir.security_schemes.get(&scheme_name).is_some_and(|scheme| { + matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { .. }) + }) && matches!(&node.value, CanonicalSourceValue::Object(_)) + } + "unresolved_security_scheme" if finding.severity == ImportFindingSeverity::Warning => { + let scheme_name = pointer + .rsplit('/') + .next() + .unwrap_or_default() + .replace("~1", "/") + .replace("~0", "~"); + !ir.security_schemes.contains_key(&scheme_name) + } + "invalid_security_scopes" if finding.severity == ImportFindingSeverity::Warning => { + !matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_)))) + } + "unexpected_security_scopes" if finding.severity == ImportFindingSeverity::Warning => { + let scheme_name = pointer + .rsplit('/') + .next() + .unwrap_or_default() + .replace("~1", "/") + .replace("~0", "~"); + let supports_scopes = ir.security_schemes.get(&scheme_name).is_some_and(|scheme| { + matches!(&scheme.kind, crate::rest::model::NormalizedSecuritySchemeKind::Unsupported { scheme_type: Some(kind) } if matches!(kind.as_str(), "oauth2" | "openIdConnect")) + }); + !supports_scopes + && matches!(&node.value, CanonicalSourceValue::Array(items) if !items.is_empty()) + } _ => false, } } -fn security_finding( +fn path_placeholders(path: &str) -> Vec { + let mut placeholders = Vec::new(); + let mut remaining = path; + while let Some(open) = remaining.find('{') { + let after_open = &remaining[open + 1..]; + let Some(close) = after_open.find('}') else { + break; + }; + if close > 0 { + placeholders.push(after_open[..close].to_owned()); + } + remaining = &after_open[close + 1..]; + } + placeholders +} + +fn path_parameter_missing_in_source( finding: &NormalizedFinding, ir: &NormalizedIr, + operation_node: &CanonicalSourceNode, nodes: &BTreeMap, ) -> bool { - let Some(operation) = ir - .operations - .iter() - .find(|operation| finding.operation_key.as_deref() == Some(operation.key.as_str())) - else { - return false; - }; - let Some(requirements_pointer) = requirements_pointer(operation) else { - return false; - }; let pointer = finding.location.pointer.as_str(); - let node = nodes.get(pointer).copied(); - - match finding.code.as_str() { - "invalid_security_requirements" => { - finding.severity == ImportFindingSeverity::Error - && pointer == requirements_pointer - && node.is_some_and(|node| !matches!(&node.value, CanonicalSourceValue::Array(_))) - } - "invalid_security_requirement" => { - finding.severity == ImportFindingSeverity::Warning - && alternative_at(operation, pointer).is_some_and(|_| { - node.is_some_and(|node| !matches!(&node.value, CanonicalSourceValue::Object(_))) - }) - } - "unresolved_security_scheme" => { - finding.severity == ImportFindingSeverity::Warning - && scheme_at(operation, pointer) - .is_some_and(|name| node.is_some() && !ir.security_schemes.contains_key(name)) - } - "unsupported_security_and_requirement" => { - finding.severity == ImportFindingSeverity::Warning - && alternative_at(operation, pointer) - .is_some_and(|alternative| alternative.schemes.len() > 1) - && node.is_some_and(|node| matches!(&node.value, CanonicalSourceValue::Object(_))) - } - "unsupported_security_scheme" => { - finding.severity == ImportFindingSeverity::Warning - && scheme_at(operation, pointer).is_some_and(|name| { - let Some(scheme) = ir.security_schemes.get(name) else { - return false; - }; - node.is_some() - && matches!( - &scheme.kind, - NormalizedSecuritySchemeKind::Unsupported { .. } - | NormalizedSecuritySchemeKind::ApiKey { - location: crate::rest::model::SecurityParameterLocation::Cookie, - .. - } - ) - }) - } - "unsupported_security_requirements" => { - finding.severity == ImportFindingSeverity::Error - && pointer == requirements_pointer - && !operation.security_requirements.alternatives.is_empty() - && operation - .security_requirements - .alternatives - .iter() - .all(|alternative| !alternative.supported) - && node.is_some_and(|node| matches!(&node.value, CanonicalSourceValue::Array(_))) - } - "invalid_security_scopes" | "unexpected_security_scopes" => { - if finding.severity != ImportFindingSeverity::Warning { - return false; - } - let Some(name) = scheme_at(operation, pointer) else { + let segments = pointer.split('/').collect::>(); + if segments.len() != 4 || segments[1] != "paths" { + return false; + } + let path = segments[2].replace("~1", "/").replace("~0", "~"); + let method = segments[3]; + let expected_key = format!("{} {path}", method.to_uppercase()); + let Some(operation) = ir.operations.iter().find(|operation| { + operation.key == expected_key + && operation.path == path + && operation.location.pointer == pointer + }) else { + return false; + }; + let Some(path_item) = nodes.get(&format!("/paths/{}", segments[2])).copied() else { + return false; + }; + let has_declared_parameter = |owner: &CanonicalSourceNode, name: &str| { + let CanonicalSourceValue::Object(fields) = &owner.value else { + return false; + }; + let Some(CanonicalSourceNode { + value: CanonicalSourceValue::Array(parameters), + .. + }) = fields.get("parameters") + else { + return false; + }; + parameters.iter().any(|parameter| { + let CanonicalSourceValue::Object(fields) = ¶meter.value else { return false; }; - let Some(node) = node else { + if fields.contains_key("$ref") { return false; - }; - let valid_scopes = matches!(&node.value, CanonicalSourceValue::Array(items) if items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_)))); - if finding.code == "invalid_security_scopes" { - !valid_scopes - } else { - let nonempty = matches!(&node.value, CanonicalSourceValue::Array(items) if !items.is_empty() && items.iter().all(|item| matches!(item.value, CanonicalSourceValue::String(_)))); - let oauth_scoped = ir.security_schemes.get(name).is_some_and(|scheme| { - matches!( - &scheme.kind, - NormalizedSecuritySchemeKind::Unsupported { - scheme_type: Some(scheme_type) - } if matches!(scheme_type.as_str(), "oauth2" | "openIdConnect") - ) - }); - nonempty && !oauth_scoped } - } - _ => false, - } -} - -fn requirements_pointer(operation: &NormalizedOperation) -> Option { - match operation.security_requirements.source { - SecurityRequirementsSource::Document => Some("/security".to_owned()), - SecurityRequirementsSource::Operation => { - Some(format!("{}/security", operation.location.pointer)) - } - SecurityRequirementsSource::None => None, - } -} - -fn alternative_at<'a>( - operation: &'a NormalizedOperation, - pointer: &str, -) -> Option<&'a crate::rest::model::SecurityRequirementAlternative> { - operation - .security_requirements - .alternatives - .iter() - .find(|alternative| { - alternative - .source_location - .as_ref() - .is_some_and(|location| location.pointer == pointer) + matches!(fields.get("name").map(|node| &node.value), Some(CanonicalSourceValue::String(value)) if value == name) + && matches!(fields.get("in").map(|node| &node.value), Some(CanonicalSourceValue::String(value)) if value == "path") }) + }; + path_placeholders(&operation.path).into_iter().any(|name| { + !has_declared_parameter(path_item, &name) + && !has_declared_parameter(operation_node, &name) + && !operation.parameters.iter().any(|parameter| { + parameter.name == name + && parameter.location == crate::rest::model::RestParameterLocation::Path + }) + }) && finding.operation_key.as_deref() == Some(expected_key.as_str()) } -fn scheme_at<'a>(operation: &'a NormalizedOperation, pointer: &str) -> Option<&'a str> { - let (alternative_pointer, scheme) = pointer.rsplit_once('/')?; - let alternative = alternative_at(operation, alternative_pointer)?; - let name = scheme.replace("~1", "/").replace("~0", "~"); - alternative - .schemes - .iter() - .find(|candidate| **candidate == name) - .map(String::as_str) +fn sanitized_secret_url(value: &str) -> bool { + value.contains("=[REDACTED]") + || ((value.contains("://") || value.starts_with("//")) && !value.contains('@')) } fn parameter_style( @@ -348,6 +368,31 @@ fn secret_like(pointer: &str, value: &str) -> bool { || value.starts_with("sk-") } +/// Accepts malformed `security` shapes at their scoped source location only: +/// the document `security` member, an operation `security` member, or a +/// non-object item of the corresponding security array. An object is valid +/// only as an array item, including the empty `{}` public alternative. +fn invalid_security_requirement(pointer: &str, node: &CanonicalSourceNode) -> bool { + const METHODS: &[&str] = &[ + "get", "post", "put", "patch", "delete", "head", "options", "trace", "connect", + ]; + let is_index = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit()); + let segments: Vec<&str> = pointer.split('/').collect(); + match segments.as_slice() { + ["", "security"] => true, + ["", "security", index] => { + is_index(index) && !matches!(&node.value, CanonicalSourceValue::Object(_)) + } + ["", "paths", _, method, "security"] => METHODS.contains(method), + ["", "paths", _, method, "security", index] => { + METHODS.contains(method) + && is_index(index) + && !matches!(&node.value, CanonicalSourceValue::Object(_)) + } + _ => false, + } +} + fn invalid_server(pointer: &str, node: &CanonicalSourceNode) -> bool { if pointer == "/host" { return !matches!(&node.value, CanonicalSourceValue::String(host) if !host.is_empty() && !host.contains('@') && !host.chars().any(char::is_whitespace)); diff --git a/crates/crank-import/src/rest/normalize_preview.rs b/crates/crank-import/src/rest/normalize_preview.rs deleted file mode 100644 index d810c7c..0000000 --- a/crates/crank-import/src/rest/normalize_preview.rs +++ /dev/null @@ -1,97 +0,0 @@ -use std::collections::{BTreeMap, BTreeSet}; - -use crate::rest::model::{ - ImportFinding, ImportGroupPreview, ImportPreview, ImportSourcePreview, NormalizedIr, - NormalizedSecurityScheme, RestImportDocument, -}; - -pub(super) fn preview_from_ir(ir: &NormalizedIr) -> ImportPreview { - let operations = ir - .operations - .iter() - .map(super::normalize_projection::operation_from_normalized) - .collect::>(); - let findings = ir - .findings - .iter() - .map(|finding| ImportFinding { - code: finding.code.clone(), - severity: finding.severity.clone(), - message: finding.message.clone(), - operation_key: finding.operation_key.clone(), - }) - .collect(); - preview_from_operations( - &ir.source, - &ir.security_schemes, - operations.iter(), - findings, - false, - ) -} - -pub(super) fn preview_from_document(document: RestImportDocument) -> ImportPreview { - let source = ImportSourcePreview { - format: document.format, - version: document.version, - title: document.title, - servers: document.servers, - }; - preview_from_operations( - &source, - &document.security_schemes, - document.operations.iter(), - document.findings, - true, - ) -} - -fn preview_from_operations<'a>( - source: &ImportSourcePreview, - security_schemes: &BTreeMap, - operations: impl Iterator, - mut findings: Vec, - legacy: bool, -) -> ImportPreview { - let mut groups: BTreeMap = BTreeMap::new(); - let mut used_names = BTreeSet::new(); - - for operation in operations { - let candidate = if legacy { - super::legacy_payload::candidate_from_operation( - operation, - &source.servers, - &mut used_names, - ) - } else { - super::payload::candidate_from_operation( - operation, - security_schemes, - &source.servers, - &mut used_names, - ) - }; - let group_title = operation - .tags - .first() - .cloned() - .unwrap_or_else(|| "Без группы".to_owned()); - let group_key = crate::rest::naming::snake_name(&group_title); - groups - .entry(group_key.clone()) - .or_insert_with(|| ImportGroupPreview { - key: group_key, - title: group_title, - operations: Vec::new(), - }) - .operations - .push(candidate); - } - - super::normalize::sort_findings(&mut findings); - ImportPreview { - source: source.clone(), - groups: groups.into_values().collect(), - findings, - } -} diff --git a/crates/crank-import/src/rest/normalize_projection.rs b/crates/crank-import/src/rest/normalize_projection.rs index 5410a53..addb0f1 100644 --- a/crates/crank-import/src/rest/normalize_projection.rs +++ b/crates/crank-import/src/rest/normalize_projection.rs @@ -1,18 +1,115 @@ -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use serde_json::Value; use crate::rest::model::{ - ImportFinding, NormalizedLiteral, NormalizedOperation, NormalizedScalarKind, NormalizedSchema, - NormalizedSchemaKind, RestImportOperation, RestImportParameter, RestRequestBodyVariant, - RestResponseVariant, + ImportFinding, ImportGroupPreview, ImportOperationCandidate, ImportPreview, + ImportSourcePreview, NormalizedLiteral, NormalizedOperation, NormalizedScalarKind, + NormalizedSchema, NormalizedSchemaKind, NormalizedSecurityScheme, RestImportDocument, + RestImportOperation, RestImportParameter, RestRequestBodyVariant, RestResponseVariant, }; +pub(super) fn preview_from_ir(ir: &crate::rest::model::NormalizedIr) -> ImportPreview { + let operations = ir + .operations + .iter() + .map(operation_from_normalized) + .collect::>(); + let findings = ir + .findings + .iter() + .map(|finding| ImportFinding { + code: finding.code.clone(), + severity: finding.severity.clone(), + message: finding.message.clone(), + operation_key: finding.operation_key.clone(), + source_location: Some(finding.location.clone()), + }) + .collect(); + preview_from_operations( + &ir.source, + operations.iter(), + findings, + &ir.security_schemes, + false, + ) +} + +pub(super) fn preview_from_document(document: RestImportDocument) -> ImportPreview { + let security_schemes = document.security_schemes.clone(); + let source = ImportSourcePreview { + format: document.format, + version: document.version, + title: document.title, + servers: document.servers, + }; + preview_from_operations( + &source, + document.operations.iter(), + document.findings, + &security_schemes, + true, + ) +} + +fn preview_from_operations<'a>( + source: &ImportSourcePreview, + operations: impl Iterator, + mut findings: Vec, + security_schemes: &BTreeMap, + legacy: bool, +) -> ImportPreview { + let mut groups: BTreeMap = BTreeMap::new(); + let mut used_names = BTreeSet::new(); + + for operation in operations { + let candidate = if legacy { + super::legacy_payload::candidate_from_operation( + operation, + &source.servers, + &mut used_names, + ) + } else { + crate::rest::payload::candidate_from_operation( + operation, + security_schemes, + &source.servers, + &mut used_names, + ) + }; + let group_title = operation + .tags + .first() + .cloned() + .unwrap_or_else(|| "Без группы".to_owned()); + let group_key = crate::rest::naming::snake_name(&group_title); + groups + .entry(group_key.clone()) + .or_insert_with(|| ImportGroupPreview { + key: group_key, + title: group_title, + operations: Vec::::new(), + }) + .operations + .push(candidate); + } + + super::normalize_security::attach_operation_findings(&mut groups, &findings); + super::normalize::sort_findings(&mut findings); + ImportPreview { + source: source.clone(), + groups: groups.into_values().collect(), + findings, + conflicts: Vec::new(), + } +} + pub(super) fn operation_from_normalized(operation: &NormalizedOperation) -> RestImportOperation { RestImportOperation { key: operation.key.clone(), method: operation.method, path: operation.path.clone(), + source_location: Some(operation.location.clone()), security_requirements: operation.security_requirements.clone(), operation_id: operation.operation_id.clone(), summary: operation.summary.clone(), @@ -68,6 +165,7 @@ pub(super) fn operation_from_normalized(operation: &NormalizedOperation) -> Rest severity: finding.severity.clone(), message: finding.message.clone(), operation_key: finding.operation_key.clone(), + source_location: Some(finding.location.clone()), }) .collect(), internal_finding_locations: Vec::new(), diff --git a/crates/crank-import/src/rest/normalize_security.rs b/crates/crank-import/src/rest/normalize_security.rs new file mode 100644 index 0000000..738bb92 --- /dev/null +++ b/crates/crank-import/src/rest/normalize_security.rs @@ -0,0 +1,27 @@ +//! Attaches document-level findings to their affected operation candidates. + +use super::model::{ImportFinding, ImportFindingSeverity, ImportGroupPreview}; + +pub(super) fn attach_operation_findings( + groups: &mut std::collections::BTreeMap, + findings: &[ImportFinding], +) { + for group in groups.values_mut() { + for candidate in &mut group.operations { + let additions = findings + .iter() + .filter(|finding| { + finding.severity == ImportFindingSeverity::Error + && finding.operation_key.as_deref() == Some(candidate.key.as_str()) + && !candidate.findings.iter().any(|existing| { + existing.code == finding.code + && existing.severity == finding.severity + && existing.message == finding.message + }) + }) + .cloned() + .collect::>(); + candidate.findings.extend(additions); + } + } +} diff --git a/crates/crank-import/src/rest/openapi3.rs b/crates/crank-import/src/rest/openapi3.rs index 4267c39..6b46800 100644 --- a/crates/crank-import/src/rest/openapi3.rs +++ b/crates/crank-import/src/rest/openapi3.rs @@ -1,6 +1,7 @@ use crank_core::HttpMethod; use serde_json::Value; +use super::parser_findings::append_unused_security_scheme_findings as append_unused; use crate::rest::{ model::{ ImportFinding, RestImportDocument, RestImportOperation, RestImportParameter, @@ -15,6 +16,7 @@ pub fn parse_document(root: &Value) -> Result Result { + let security_schemes = super::security::schemes(root, false); let version = root .get("openapi") .and_then(Value::as_str) @@ -25,21 +27,16 @@ fn parse_document_v2(root: &Value) -> Result 1 { @@ -47,7 +44,7 @@ fn parse_document_v2(root: &Value) -> Result Result Result Result Result Result Result Result Result Result Result) { parameters.reverse(); } +pub fn parse_document_legacy_v1(root: &Value) -> Result { + let version = root + .get("openapi") + .and_then(Value::as_str) + .map(ToOwned::to_owned); + let title = root + .pointer("/info/title") + .and_then(Value::as_str) + .unwrap_or("Imported API") + .to_owned(); + let servers = legacy_servers(root.get("servers")); + let mut findings = Vec::new(); + if servers.is_empty() { + findings.push(document_finding( + "missing_servers", + "В документе не указаны servers, base URL нужно будет выбрать вручную.", + )); + } else if servers.len() > 1 { + findings.push(document_finding( + "multiple_servers", + "В документе несколько servers, при импорте нужно выбрать нужный base URL.", + )); + } + + let mut operations = Vec::new(); + let paths = root + .get("paths") + .and_then(Value::as_object) + .ok_or(ImportParseError::UnsupportedDocument)?; + for (path, path_item) in paths { + let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1")); + let path_parameters = legacy_parameters( + root, + path_item.get("parameters"), + &format!("{path_pointer}/parameters"), + ); + for method_name in ["get", "post", "put", "patch", "delete"] { + let Some(operation) = path_item.get(method_name) else { + continue; + }; + let Some(method) = super::http_methods::from_lower(method_name) else { + continue; + }; + let operation_pointer = format!("{path_pointer}/{method_name}"); + let mut parameters = path_parameters.clone(); + parameters.extend(legacy_parameters( + root, + operation.get("parameters"), + &format!("{operation_pointer}/parameters"), + )); + operations.push(RestImportOperation { + key: format!("{} {}", method_name.to_uppercase(), path), + method, + path: path.clone(), + source_location: None, + security_requirements: Default::default(), + operation_id: operation + .get("operationId") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + summary: operation + .get("summary") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + description: operation + .get("description") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + tags: legacy_tags(operation.get("tags")), + parameters, + request_body_schema: legacy_request_body_schema(root, operation), + request_body_schema_location: None, + request_body_variants: Vec::new(), + response_schema: legacy_response_schema(root, operation), + response_schema_location: None, + response_variants: Vec::new(), + servers: legacy_servers(operation.get("servers")), + findings: legacy_operation_findings(operation), + internal_finding_locations: Vec::new(), + }); + } + } + Ok(RestImportDocument { + format: "openapi".to_owned(), + version, + title, + servers, + security_schemes: Default::default(), + operations, + findings, + internal_finding_locations: Vec::new(), + }) +} + +fn legacy_servers(value: Option<&Value>) -> Vec { + value + .and_then(Value::as_array) + .map(|items| { + items + .iter() + .filter_map(|item| item.get("url").and_then(Value::as_str)) + .map(|url| url.trim_end_matches('/').to_owned()) + .collect() + }) + .unwrap_or_default() +} + +fn legacy_tags(value: Option<&Value>) -> Vec { + value + .and_then(Value::as_array) + .map(|items| { + items + .iter() + .filter_map(Value::as_str) + .map(ToOwned::to_owned) + .collect() + }) + .unwrap_or_default() +} + +fn legacy_parameters( + root: &Value, + value: Option<&Value>, + base_pointer: &str, +) -> Vec { + value + .and_then(Value::as_array) + .map(|items| { + items + .iter() + .enumerate() + .filter_map(|(index, item)| { + let item = crate::rest::normalize::resolve_local_ref(root, item, 0); + let location = match item.get("in").and_then(Value::as_str)? { + "path" => RestParameterLocation::Path, + "query" => RestParameterLocation::Query, + "header" => RestParameterLocation::Header, + _ => return None, + }; + Some(RestImportParameter { + name: item.get("name").and_then(Value::as_str)?.to_owned(), + location, + required: item + .get("required") + .and_then(Value::as_bool) + .unwrap_or(false) + || location == RestParameterLocation::Path, + description: item + .get("description") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + schema: item.get("schema").map(|schema| { + crate::rest::normalize::resolve_local_ref(root, schema, 0) + }), + source_location: crate::rest::model::SourceLocation { + pointer: format!("{base_pointer}/{index}"), + }, + schema_source_location: None, + }) + }) + .collect() + }) + .unwrap_or_default() +} + +fn legacy_request_body_schema(root: &Value, operation: &Value) -> Option { + let body = crate::rest::normalize::resolve_local_ref(root, operation.get("requestBody")?, 0); + let content = body.get("content")?.as_object()?; + for content_type in ["application/json", "application/*+json"] { + if let Some(schema) = content + .get(content_type) + .and_then(|media| media.get("schema")) + { + return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); + } + } + content + .iter() + .find(|(content_type, _)| content_type.contains("json")) + .and_then(|(_, media)| media.get("schema")) + .map(|schema| crate::rest::normalize::resolve_local_ref(root, schema, 0)) +} + +fn legacy_response_schema(root: &Value, operation: &Value) -> Option { + let responses = operation.get("responses")?.as_object()?; + for code in ["200", "201", "202", "default"] { + let Some(response) = responses.get(code) else { + continue; + }; + let response = crate::rest::normalize::resolve_local_ref(root, response, 0); + let Some(content) = response.get("content").and_then(Value::as_object) else { + continue; + }; + for content_type in ["application/json", "application/*+json"] { + if let Some(schema) = content + .get(content_type) + .and_then(|media| media.get("schema")) + { + return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); + } + } + if let Some(schema) = content + .iter() + .find(|(content_type, _)| content_type.contains("json")) + .and_then(|(_, media)| media.get("schema")) + { + return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); + } + } + None +} + +fn legacy_operation_findings(operation: &Value) -> Vec { + if operation.get("requestBody").is_some() + && legacy_request_body_schema(&Value::Null, operation).is_none() + { + vec![document_finding( + "unsupported_request_body", + "У метода есть requestBody, но JSON schema не найдена.", + )] + } else { + Vec::new() + } +} + struct ParsedParameters { parameters: Vec, findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>, @@ -488,17 +708,6 @@ fn parameter_blocker( ) } -fn append_findings_with_locations( - findings: &mut Vec, - locations: &mut Vec>, - parameter_findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>, -) { - for (finding, location) in parameter_findings { - findings.push(finding); - locations.push(Some(location)); - } -} - fn request_body_schema( operation: &Value, operation_pointer: &str, diff --git a/crates/crank-import/src/rest/openapi3_findings.rs b/crates/crank-import/src/rest/openapi3_findings.rs index b9dd4d3..4fcb8cf 100644 --- a/crates/crank-import/src/rest/openapi3_findings.rs +++ b/crates/crank-import/src/rest/openapi3_findings.rs @@ -299,6 +299,17 @@ pub(super) fn schema_findings( pointer: &str, findings: &mut Vec<(ImportFinding, SourceLocation)>, ) { + if schema + .get("default") + .is_some_and(|value| value.is_object() || value.is_array()) + { + push( + findings, + "unsupported_compound_default", + "Object и array schema defaults не поддерживаются executable import contract.", + &format!("{pointer}/default"), + ); + } if let Some(types) = schema.get("type").and_then(Value::as_array) { let concrete = types .iter() @@ -389,8 +400,9 @@ pub(super) fn schema_findings( pub(super) fn path_parameters( path: &str, parameters: &[RestImportParameter], + operation_pointer: &str, ) -> Vec<(ImportFinding, SourceLocation)> { - parameters + let mut findings = parameters .iter() .filter(|parameter| parameter.location == RestParameterLocation::Path) .filter_map(|parameter| { @@ -415,7 +427,39 @@ pub(super) fn path_parameters( parameter.source_location.clone(), )) }) - .collect() + .collect::>(); + for name in path_placeholders(path) { + if !parameters.iter().any(|parameter| { + parameter.location == RestParameterLocation::Path && parameter.name == name + }) { + findings.push(( + document_blocker( + "path_parameter_not_declared", + format!("Path placeholder {name} не объявлен как path parameter."), + ), + SourceLocation { + pointer: operation_pointer.to_owned(), + }, + )); + } + } + findings +} + +fn path_placeholders(path: &str) -> Vec { + let mut placeholders = Vec::new(); + let mut remaining = path; + while let Some(open) = remaining.find('{') { + let after_open = &remaining[open + 1..]; + let Some(close) = after_open.find('}') else { + break; + }; + if close > 0 { + placeholders.push(after_open[..close].to_owned()); + } + remaining = &after_open[close + 1..]; + } + placeholders } pub(super) fn unsupported_methods(path_item: &Value) -> Vec<&str> { diff --git a/crates/crank-import/src/rest/openapi3_legacy.rs b/crates/crank-import/src/rest/openapi3_legacy.rs deleted file mode 100644 index 343780d..0000000 --- a/crates/crank-import/src/rest/openapi3_legacy.rs +++ /dev/null @@ -1,236 +0,0 @@ -use serde_json::Value; - -use crate::rest::{ - model::{ - ImportFinding, RestImportDocument, RestImportOperation, RestImportParameter, - RestParameterLocation, - }, - normalize::ImportParseError, - recommendations::document_finding, -}; - -pub(super) fn parse_document_legacy_v1( - root: &Value, -) -> Result { - let version = root - .get("openapi") - .and_then(Value::as_str) - .map(ToOwned::to_owned); - let title = root - .pointer("/info/title") - .and_then(Value::as_str) - .unwrap_or("Imported API") - .to_owned(); - let servers = legacy_servers(root.get("servers")); - let mut findings = Vec::new(); - if servers.is_empty() { - findings.push(document_finding( - "missing_servers", - "В документе не указаны servers, base URL нужно будет выбрать вручную.", - )); - } else if servers.len() > 1 { - findings.push(document_finding( - "multiple_servers", - "В документе несколько servers, при импорте нужно выбрать нужный base URL.", - )); - } - - let mut operations = Vec::new(); - let paths = root - .get("paths") - .and_then(Value::as_object) - .ok_or(ImportParseError::UnsupportedDocument)?; - for (path, path_item) in paths { - let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1")); - let path_parameters = legacy_parameters( - root, - path_item.get("parameters"), - &format!("{path_pointer}/parameters"), - ); - for method_name in ["get", "post", "put", "patch", "delete"] { - let Some(operation) = path_item.get(method_name) else { - continue; - }; - let Some(method) = super::http_methods::from_lower(method_name) else { - continue; - }; - let operation_pointer = format!("{path_pointer}/{method_name}"); - let mut parameters = path_parameters.clone(); - parameters.extend(legacy_parameters( - root, - operation.get("parameters"), - &format!("{operation_pointer}/parameters"), - )); - operations.push(RestImportOperation { - key: format!("{} {}", method_name.to_uppercase(), path), - method, - path: path.clone(), - security_requirements: Default::default(), - operation_id: operation - .get("operationId") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - summary: operation - .get("summary") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - description: operation - .get("description") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - tags: legacy_tags(operation.get("tags")), - parameters, - request_body_schema: legacy_request_body_schema(root, operation), - request_body_schema_location: None, - request_body_variants: Vec::new(), - response_schema: legacy_response_schema(root, operation), - response_schema_location: None, - response_variants: Vec::new(), - servers: legacy_servers(operation.get("servers")), - findings: legacy_operation_findings(operation), - internal_finding_locations: Vec::new(), - }); - } - } - Ok(RestImportDocument { - format: "openapi".to_owned(), - version, - title, - servers, - security_schemes: Default::default(), - operations, - findings, - internal_finding_locations: Vec::new(), - }) -} - -fn legacy_servers(value: Option<&Value>) -> Vec { - value - .and_then(Value::as_array) - .map(|items| { - items - .iter() - .filter_map(|item| item.get("url").and_then(Value::as_str)) - .map(|url| url.trim_end_matches('/').to_owned()) - .collect() - }) - .unwrap_or_default() -} - -fn legacy_tags(value: Option<&Value>) -> Vec { - value - .and_then(Value::as_array) - .map(|items| { - items - .iter() - .filter_map(Value::as_str) - .map(ToOwned::to_owned) - .collect() - }) - .unwrap_or_default() -} - -fn legacy_parameters( - root: &Value, - value: Option<&Value>, - base_pointer: &str, -) -> Vec { - value - .and_then(Value::as_array) - .map(|items| { - items - .iter() - .enumerate() - .filter_map(|(index, item)| { - let item = crate::rest::normalize::resolve_local_ref(root, item, 0); - let location = match item.get("in").and_then(Value::as_str)? { - "path" => RestParameterLocation::Path, - "query" => RestParameterLocation::Query, - "header" => RestParameterLocation::Header, - _ => return None, - }; - Some(RestImportParameter { - name: item.get("name").and_then(Value::as_str)?.to_owned(), - location, - required: item - .get("required") - .and_then(Value::as_bool) - .unwrap_or(false) - || location == RestParameterLocation::Path, - description: item - .get("description") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - schema: item.get("schema").map(|schema| { - crate::rest::normalize::resolve_local_ref(root, schema, 0) - }), - source_location: crate::rest::model::SourceLocation { - pointer: format!("{base_pointer}/{index}"), - }, - schema_source_location: None, - }) - }) - .collect() - }) - .unwrap_or_default() -} - -fn legacy_request_body_schema(root: &Value, operation: &Value) -> Option { - let body = crate::rest::normalize::resolve_local_ref(root, operation.get("requestBody")?, 0); - let content = body.get("content")?.as_object()?; - for content_type in ["application/json", "application/*+json"] { - if let Some(schema) = content - .get(content_type) - .and_then(|media| media.get("schema")) - { - return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); - } - } - content - .iter() - .find(|(content_type, _)| content_type.contains("json")) - .and_then(|(_, media)| media.get("schema")) - .map(|schema| crate::rest::normalize::resolve_local_ref(root, schema, 0)) -} - -fn legacy_response_schema(root: &Value, operation: &Value) -> Option { - let responses = operation.get("responses")?.as_object()?; - for code in ["200", "201", "202", "default"] { - let Some(response) = responses.get(code) else { - continue; - }; - let response = crate::rest::normalize::resolve_local_ref(root, response, 0); - let Some(content) = response.get("content").and_then(Value::as_object) else { - continue; - }; - for content_type in ["application/json", "application/*+json"] { - if let Some(schema) = content - .get(content_type) - .and_then(|media| media.get("schema")) - { - return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); - } - } - if let Some(schema) = content - .iter() - .find(|(content_type, _)| content_type.contains("json")) - .and_then(|(_, media)| media.get("schema")) - { - return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); - } - } - None -} - -fn legacy_operation_findings(operation: &Value) -> Vec { - if operation.get("requestBody").is_some() - && legacy_request_body_schema(&Value::Null, operation).is_none() - { - vec![document_finding( - "unsupported_request_body", - "У метода есть requestBody, но JSON schema не найдена.", - )] - } else { - Vec::new() - } -} diff --git a/crates/crank-import/src/rest/parser_findings.rs b/crates/crank-import/src/rest/parser_findings.rs new file mode 100644 index 0000000..01ce0f0 --- /dev/null +++ b/crates/crank-import/src/rest/parser_findings.rs @@ -0,0 +1,28 @@ +use crate::rest::model::NormalizedSecurityScheme; +use crate::rest::model::{ImportFinding, SourceLocation}; +use serde_json::Value; +use std::collections::BTreeMap; + +pub(super) fn append_located( + findings: &mut Vec, + locations: &mut Vec>, + entries: Vec<(ImportFinding, SourceLocation)>, +) { + for (finding, location) in entries { + findings.push(finding); + locations.push(Some(location)); + } +} + +pub(super) fn append_unused_security_scheme_findings( + root: &Value, + schemes: &BTreeMap, + findings: &mut Vec, + locations: &mut Vec>, +) { + append_located( + findings, + locations, + super::security::unused_unsupported_scheme_findings(root, schemes), + ); +} diff --git a/crates/crank-import/src/rest/payload.rs b/crates/crank-import/src/rest/payload.rs index 64ede95..0c8da65 100644 --- a/crates/crank-import/src/rest/payload.rs +++ b/crates/crank-import/src/rest/payload.rs @@ -10,8 +10,7 @@ use serde_json::json; use crate::rest::{ mapping::{BodyFieldMapping, ParameterFieldMapping, input_mapping, output_mapping}, model::{ - ImportOperationCandidate, NormalizedSecurityScheme, RestImportCandidate, - RestImportOperation, RestParameterLocation, + ImportOperationCandidate, RestImportCandidate, RestImportOperation, RestParameterLocation, }, naming::{fallback_operation_seed, title_from_name, unique_name}, recommendations::{candidate_recommendations, operation_recommendations}, @@ -22,7 +21,7 @@ use crate::rest::{ pub fn candidate_from_operation( operation: &RestImportOperation, - security_schemes: &BTreeMap, + security_schemes: &BTreeMap, document_servers: &[String], used_names: &mut BTreeSet, ) -> ImportOperationCandidate { @@ -156,6 +155,8 @@ pub fn candidate_from_operation( key: operation.key.clone(), method: operation.method, path: operation.path.clone(), + status: String::new(), + source_location: operation.source_location.clone(), security_requirements: operation.security_requirements.clone(), security_schemes: security_schemes.clone(), operation_id: operation.operation_id.clone(), diff --git a/crates/crank-import/src/rest/recommendations.rs b/crates/crank-import/src/rest/recommendations.rs index 66e4bec..8638600 100644 --- a/crates/crank-import/src/rest/recommendations.rs +++ b/crates/crank-import/src/rest/recommendations.rs @@ -8,6 +8,7 @@ pub fn document_finding(code: &str, message: impl Into) -> ImportFinding severity: ImportFindingSeverity::Warning, message: message.into(), operation_key: None, + source_location: None, } } @@ -17,6 +18,7 @@ pub fn document_blocker(code: &str, message: impl Into) -> ImportFinding severity: ImportFindingSeverity::Error, message: message.into(), operation_key: None, + source_location: None, } } @@ -30,6 +32,7 @@ pub fn operation_finding( severity: ImportFindingSeverity::Warning, message: message.into(), operation_key: Some(operation_key.to_owned()), + source_location: None, } } @@ -43,6 +46,7 @@ pub fn operation_info( severity: ImportFindingSeverity::Info, message: message.into(), operation_key: Some(operation_key.to_owned()), + source_location: None, } } @@ -125,7 +129,7 @@ pub fn operation_recommendations(operation: &RestImportOperation) -> Vec, + operation: &RestImportOperation, +) -> Vec { + for finding in &mut findings { + if finding.source_location.is_none() { + finding.source_location = operation.source_location.clone(); + } + } findings } diff --git a/crates/crank-import/src/rest/reference.rs b/crates/crank-import/src/rest/reference.rs index 0fdd09c..9edfbc1 100644 --- a/crates/crank-import/src/rest/reference.rs +++ b/crates/crank-import/src/rest/reference.rs @@ -8,7 +8,8 @@ use crate::rest::model::{ SourceLocation, }; -use super::{ImportParseError, normalize_coverage::escape_pointer}; +use super::reference_budget::{CopyBudget, count_branch_bytes, count_copy_bytes, merge_object}; +use super::{ImportParseError, decode_budget, normalize_coverage::escape_pointer}; pub(super) struct ResolutionResult { pub root: Value, @@ -61,6 +62,11 @@ fn reference_uris_with_max_bytes( { return Err(ImportParseError::LimitExceeded); } + decode_budget::preflight_single( + document, + decode_budget::ShapeLimits::from_config(config), + max_bytes, + )?; let root = decode_snapshot(document)?; super::normalize::validate_value_limits(&root, config)?; let mut uris = BTreeSet::new(); @@ -95,6 +101,7 @@ struct Resolver<'a> { findings: Vec, references: usize, expanded_nodes: usize, + copy_budget: CopyBudget, } pub(super) fn resolve( @@ -139,6 +146,10 @@ pub(super) fn resolve( root, }); } + let copy_limit = decode_budget::allowance( + config.max_bytes.max(config.max_external_document_bytes), + config.max_scalar_bytes, + )?; let mut resolver = Resolver { config, documents, @@ -150,7 +161,10 @@ pub(super) fn resolve( findings: Vec::new(), references: 0, expanded_nodes: 0, + copy_budget: CopyBudget::new(copy_limit), }; + let primary_copy_bytes = count_copy_bytes(&resolver.documents[0].root); + resolver.copy_budget.charge(primary_copy_bytes)?; let root = resolver.documents[0].root.clone(); let root = resolver.expand_value( 0, @@ -320,7 +334,7 @@ impl Resolver<'_> { return Ok(Value::Object(source_object)); } let Some((target_document, target_pointer)) = target.ok().flatten() else { - let external = reference.starts_with("http://") || reference.starts_with("https://"); + let external = is_external_http_scheme(reference); let code = if external { if self.config.external_references_enabled { "external_reference_unavailable" @@ -360,6 +374,18 @@ impl Resolver<'_> { // producing an opaque object for the finite preview projection. return Ok(Value::Object(Map::new())); } + let Some(target_bytes) = self.documents[target_document] + .root + .pointer(&target_pointer) + .map(count_copy_bytes) + else { + self.push_finding( + "reference_target_missing", + &format!("{}/$ref", location.projection), + ); + return Ok(Value::Object(source_object)); + }; + self.copy_budget.charge(target_bytes)?; let Some(target_value) = self.documents[target_document] .root .pointer(&target_pointer) @@ -471,6 +497,8 @@ impl Resolver<'_> { return Ok(Value::Object(object)); } }; + let branch_bytes = count_branch_bytes(&branches); + self.copy_budget.charge(branch_bytes)?; let original = branches.clone(); for branch in branches { let Some(fields) = branch.as_object() else { @@ -478,7 +506,7 @@ impl Resolver<'_> { self.push_finding("all_of_conflict", &format!("{pointer}/allOf")); return Ok(Value::Object(object)); }; - if !merge_object(&mut object, fields) { + if !merge_object(&mut object, fields, &mut self.copy_budget)? { object.insert("allOf".to_owned(), Value::Array(original)); self.push_finding("all_of_conflict", &format!("{pointer}/allOf")); return Ok(Value::Object(object)); @@ -517,122 +545,6 @@ impl Resolver<'_> { } } -fn merge_object(target: &mut Map, source: &Map) -> bool { - for (key, value) in source { - match key.as_str() { - "properties" => { - let Some(source_properties) = value.as_object() else { - return false; - }; - let properties = target - .entry(key.clone()) - .or_insert_with(|| Value::Object(Map::new())); - let Some(target_properties) = properties.as_object_mut() else { - return false; - }; - for (name, schema) in source_properties { - if let Some(existing) = target_properties.get(name) { - let (Some(existing), Some(schema)) = - (existing.as_object(), schema.as_object()) - else { - return false; - }; - let mut merged = existing.clone(); - if !merge_object(&mut merged, schema) { - return false; - } - target_properties.insert(name.clone(), Value::Object(merged)); - } else { - target_properties.insert(name.clone(), schema.clone()); - } - } - } - "required" => { - let Some(source_required) = value.as_array() else { - return false; - }; - let required = target - .entry(key.clone()) - .or_insert_with(|| Value::Array(Vec::new())); - let Some(target_required) = required.as_array_mut() else { - return false; - }; - target_required.extend(source_required.iter().cloned()); - target_required.sort_by(|left, right| left.as_str().cmp(&right.as_str())); - target_required.dedup(); - } - "minimum" | "maximum" => { - let Some(source_value) = value.as_f64() else { - return false; - }; - if target.contains_key(key) && target.get(key).and_then(Value::as_f64).is_none() { - return false; - } - let merged = match (key.as_str(), target.get(key).and_then(Value::as_f64)) { - ("minimum", Some(current)) => current.max(source_value), - ("maximum", Some(current)) => current.min(source_value), - _ => source_value, - }; - let Some(number) = serde_json::Number::from_f64(merged) else { - return false; - }; - target.insert(key.clone(), Value::Number(number)); - if constraint_contradiction(target, "minimum", "maximum") { - return false; - } - } - "minLength" | "maxLength" => { - let Some(source_value) = value.as_u64() else { - return false; - }; - if target.contains_key(key) && target.get(key).and_then(Value::as_u64).is_none() { - return false; - } - let merged = match (key.as_str(), target.get(key).and_then(Value::as_u64)) { - ("minLength", Some(current)) => current.max(source_value), - ("maxLength", Some(current)) => current.min(source_value), - _ => source_value, - }; - target.insert(key.clone(), Value::Number(merged.into())); - if integer_constraint_contradiction(target, "minLength", "maxLength") { - return false; - } - } - _ => { - if target.get(key).is_some_and(|existing| existing != value) { - return false; - } - target.insert(key.clone(), value.clone()); - } - } - } - true -} - -fn constraint_contradiction(target: &Map, minimum: &str, maximum: &str) -> bool { - match ( - target.get(minimum).and_then(Value::as_f64), - target.get(maximum).and_then(Value::as_f64), - ) { - (Some(minimum), Some(maximum)) => minimum > maximum, - _ => false, - } -} - -fn integer_constraint_contradiction( - target: &Map, - minimum: &str, - maximum: &str, -) -> bool { - match ( - target.get(minimum).and_then(Value::as_u64), - target.get(maximum).and_then(Value::as_u64), - ) { - (Some(minimum), Some(maximum)) => minimum > maximum, - _ => false, - } -} - fn edge_key(edge: &ResolvedReferenceEdge) -> (&str, &str, &str, &str, bool) { ( edge.source.snapshot_digest.as_str(), @@ -644,12 +556,7 @@ fn edge_key(edge: &ResolvedReferenceEdge) -> (&str, &str, &str, &str, bool) { } fn decode_snapshot(document: &str) -> Result { - if let Ok(value) = serde_json::from_str(document) { - return Ok(value); - } - let yaml: serde_yaml::Value = - serde_yaml::from_str(document).map_err(|_| ImportParseError::InvalidDocument)?; - serde_json::to_value(yaml).map_err(|_| ImportParseError::InvalidDocument) + super::strict_decode::decode(document).map_err(|_| ImportParseError::InvalidDocument) } fn is_oas31(root: &Value) -> bool { @@ -774,14 +681,37 @@ fn canonical_authority(authority: &str, scheme: &str) -> String { "https" => Some("443"), _ => None, }; - if let Some(default_port) = default_port + let authority = if let Some(default_port) = default_port && let Some((host, port)) = authority.rsplit_once(':') && port == default_port && (!host.contains(':') || host.ends_with(']')) { - return host.to_owned(); + host.to_owned() + } else { + authority + }; + punycode_authority(authority, scheme) +} + +/// Applies the same IDNA/punycode host identity that the external-snapshot +/// materializer gets from `url::Url`, so a Unicode `$ref` and the stored +/// punycode `canonical_uri` resolve to the same snapshot. ASCII-only +/// authorities (including IPv6 literals) and authorities with userinfo keep the +/// previous ASCII-lowercase rendering, preserving RFC 3986 behavior. +fn punycode_authority(authority: String, scheme: &str) -> String { + if authority.is_ascii() || !matches!(scheme, "http" | "https") || authority.contains('@') { + return authority; + } + let Ok(parsed) = url::Url::parse(&format!("{scheme}://{authority}")) else { + return authority; + }; + let Some(host) = parsed.host_str() else { + return authority; + }; + match parsed.port() { + Some(port) => format!("{host}:{port}"), + None => host.to_owned(), } - authority } fn has_uri_scheme(value: &str) -> bool { @@ -790,6 +720,14 @@ fn has_uri_scheme(value: &str) -> bool { .is_some_and(|(candidate, _)| is_uri_scheme(candidate)) } +/// URI schemes are case-insensitive (RFC 3986 §3.1), so the failed-target +/// classification must not depend on the literal case of `http`/`https`. +fn is_external_http_scheme(reference: &str) -> bool { + reference.split_once(':').is_some_and(|(scheme, _)| { + scheme.eq_ignore_ascii_case("http") || scheme.eq_ignore_ascii_case("https") + }) +} + fn is_uri_scheme(candidate: &str) -> bool { let Some(first) = candidate.as_bytes().first() else { return false; diff --git a/crates/crank-import/src/rest/reference_budget.rs b/crates/crank-import/src/rest/reference_budget.rs new file mode 100644 index 0000000..073193c --- /dev/null +++ b/crates/crank-import/src/rest/reference_budget.rs @@ -0,0 +1,235 @@ +//! Aggregate copy budget for string/key payloads allocated while resolving +//! `$ref`s and merging `allOf` branches (Gitea #88). +//! +//! The budget is shared by the whole `resolve` call and never reset, so many +//! individually small copies cannot multiply without bound. Checked arithmetic +//! maps overflow to `ImportParseError::LimitExceeded`. `merge_object` charges +//! every borrowed schema/required/value/key before it is cloned, so budget +//! exhaustion is reported as `LimitExceeded` and never replaced by an +//! `all_of_conflict` finding. + +use serde_json::{Map, Value}; + +use super::normalize::ImportParseError; + +pub(super) struct CopyBudget { + limit: u64, + used: u64, +} + +impl CopyBudget { + pub(super) fn new(limit: u64) -> Self { + Self { limit, used: 0 } + } + + pub(super) fn charge(&mut self, bytes: u64) -> Result<(), ImportParseError> { + self.used = self + .used + .checked_add(bytes) + .ok_or(ImportParseError::LimitExceeded)?; + if self.used > self.limit { + return Err(ImportParseError::LimitExceeded); + } + Ok(()) + } +} + +/// Sums the UTF-8 bytes of every string and object key in a borrowed value, +/// mirroring the payload a later `clone` allocates. +pub(super) fn count_copy_bytes(value: &Value) -> u64 { + match value { + Value::String(text) => text.len() as u64, + Value::Array(items) => items.iter().fold(0u64, |total, item| { + total.saturating_add(count_copy_bytes(item)) + }), + Value::Object(object) => count_object_copy_bytes(object), + _ => 0, + } +} + +pub(super) fn count_object_copy_bytes(object: &Map) -> u64 { + object.iter().fold(0u64, |total, (key, item)| { + total + .saturating_add(key.len() as u64) + .saturating_add(count_copy_bytes(item)) + }) +} + +pub(super) fn count_branch_bytes(branches: &[Value]) -> u64 { + branches.iter().fold(0u64, |total, branch| { + total.saturating_add(count_copy_bytes(branch)) + }) +} + +pub(super) fn merge_object( + target: &mut Map, + source: &Map, + budget: &mut CopyBudget, +) -> Result { + for (key, value) in source { + match key.as_str() { + "properties" => { + let Some(source_properties) = value.as_object() else { + return Ok(false); + }; + budget.charge(key.len() as u64)?; + let properties = target + .entry(key.clone()) + .or_insert_with(|| Value::Object(Map::new())); + let Some(target_properties) = properties.as_object_mut() else { + return Ok(false); + }; + for (name, schema) in source_properties { + budget.charge(name.len() as u64)?; + if let Some(existing) = target_properties.get(name) { + let (Some(existing), Some(schema)) = + (existing.as_object(), schema.as_object()) + else { + return Ok(false); + }; + budget.charge(count_object_copy_bytes(existing))?; + let mut merged = existing.clone(); + if !merge_object(&mut merged, schema, budget)? { + return Ok(false); + } + target_properties.insert(name.clone(), Value::Object(merged)); + } else { + budget.charge(count_copy_bytes(schema))?; + target_properties.insert(name.clone(), schema.clone()); + } + } + } + "required" => { + let Some(source_required) = value.as_array() else { + return Ok(false); + }; + budget.charge(key.len() as u64)?; + let required = target + .entry(key.clone()) + .or_insert_with(|| Value::Array(Vec::new())); + let Some(target_required) = required.as_array_mut() else { + return Ok(false); + }; + budget.charge(count_branch_bytes(source_required))?; + target_required.extend(source_required.iter().cloned()); + target_required.sort_by(|left, right| left.as_str().cmp(&right.as_str())); + target_required.dedup(); + } + "minimum" | "maximum" => { + let Some(source_value) = value.as_f64() else { + return Ok(false); + }; + if target.contains_key(key) && target.get(key).and_then(Value::as_f64).is_none() { + return Ok(false); + } + let merged = match (key.as_str(), target.get(key).and_then(Value::as_f64)) { + ("minimum", Some(current)) => current.max(source_value), + ("maximum", Some(current)) => current.min(source_value), + _ => source_value, + }; + let Some(number) = serde_json::Number::from_f64(merged) else { + return Ok(false); + }; + budget.charge(key.len() as u64)?; + target.insert(key.clone(), Value::Number(number)); + if constraint_contradiction(target, "minimum", "maximum") { + return Ok(false); + } + } + "minLength" | "maxLength" => { + let Some(source_value) = value.as_u64() else { + return Ok(false); + }; + if target.contains_key(key) && target.get(key).and_then(Value::as_u64).is_none() { + return Ok(false); + } + let merged = match (key.as_str(), target.get(key).and_then(Value::as_u64)) { + ("minLength", Some(current)) => current.max(source_value), + ("maxLength", Some(current)) => current.min(source_value), + _ => source_value, + }; + budget.charge(key.len() as u64)?; + target.insert(key.clone(), Value::Number(merged.into())); + if integer_constraint_contradiction(target, "minLength", "maxLength") { + return Ok(false); + } + } + _ => match target.get(key) { + Some(existing) if existing != value => return Ok(false), + // Equal value already present: skip the redundant copy. + Some(_) => {} + None => { + budget.charge(key.len() as u64)?; + budget.charge(count_copy_bytes(value))?; + target.insert(key.clone(), value.clone()); + } + }, + } + } + Ok(true) +} + +fn constraint_contradiction(target: &Map, minimum: &str, maximum: &str) -> bool { + match ( + target.get(minimum).and_then(Value::as_f64), + target.get(maximum).and_then(Value::as_f64), + ) { + (Some(minimum), Some(maximum)) => minimum > maximum, + _ => false, + } +} + +fn integer_constraint_contradiction( + target: &Map, + minimum: &str, + maximum: &str, +) -> bool { + match ( + target.get(minimum).and_then(Value::as_u64), + target.get(maximum).and_then(Value::as_u64), + ) { + (Some(minimum), Some(maximum)) => minimum > maximum, + _ => false, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn object(value: Value) -> Map { + value.as_object().cloned().expect("object") + } + + #[test] + fn merge_object_charges_existing_property_object_copy() { + // The target already has the property, so merging clones the whole + // existing property object. With a small remaining quota this must be + // rejected by the budget, not silently allocated. + let mut target = object(json!({ + "properties": { "id": { "type": "string", "description": "x".repeat(64) } } + })); + let source = object(json!({ + "properties": { "id": { "type": "number" } } + })); + let mut budget = CopyBudget::new(16); + assert_eq!( + merge_object(&mut target, &source, &mut budget), + Err(ImportParseError::LimitExceeded) + ); + } + + #[test] + fn merge_object_charges_required_extension_copy() { + let mut target = object(json!({ "required": ["x"] })); + let source = object(json!({ + "required": ["aaaaaaaa", "bbbbbbbb", "cccccccc", "dddddddd"] + })); + let mut budget = CopyBudget::new(16); + assert_eq!( + merge_object(&mut target, &source, &mut budget), + Err(ImportParseError::LimitExceeded) + ); + } +} diff --git a/crates/crank-import/src/rest/security.rs b/crates/crank-import/src/rest/security.rs index 8b610f6..f261af3 100644 --- a/crates/crank-import/src/rest/security.rs +++ b/crates/crank-import/src/rest/security.rs @@ -1,14 +1,28 @@ +//! Typed security scheme extraction and effective requirement +//! classification for OpenAPI 3.x and Swagger 2.0 documents. +//! +//! The module is pure: it never reads Auth Profiles or Secrets and never +//! captures credential values. Requirements are classified alternative by +//! alternative (OR semantics preserved): a supported single-scheme +//! alternative stays resolvable via a compatible Auth Profile; malformed +//! requirements, unresolvable schemes and AND combinations mark the +//! affected alternative as unsupported; an operation is blocked only when +//! none of its alternatives is selectable. use std::collections::BTreeMap; use serde_json::Value; use super::model::{ - NormalizedSecurityRequirements, NormalizedSecurityScheme, NormalizedSecuritySchemeKind, - SecurityParameterLocation, SecurityRequirementAlternative, SecurityRequirementsSource, - SourceLocation, + ImportFinding, NormalizedSecurityRequirements, NormalizedSecurityScheme, + NormalizedSecuritySchemeKind, SecurityParameterLocation, SecurityRequirementAlternative, + SecurityRequirementsSource, SourceLocation, }; use super::recommendations::{document_blocker, document_finding}; +/// Extracts typed schema metadata from `components.securitySchemes` +/// (OpenAPI) or `securityDefinitions` (Swagger 2.0). Only scheme names, +/// kinds, locations and safe format hints are preserved; credential values +/// are never captured. pub(super) fn schemes(root: &Value, swagger: bool) -> BTreeMap { let (container, prefix) = if swagger { (root.get("securityDefinitions"), "/securityDefinitions") @@ -84,6 +98,56 @@ pub(super) fn schemes(root: &Value, swagger: bool) -> BTreeMap, +) -> Vec<(ImportFinding, SourceLocation)> { + let mut used = std::collections::BTreeSet::new(); + collect_requirement_names(root.get("security"), &mut used); + if let Some(paths) = root.get("paths").and_then(Value::as_object) { + for path_item in paths.values().filter_map(Value::as_object) { + for operation in path_item.values().filter_map(Value::as_object) { + collect_requirement_names(operation.get("security"), &mut used); + } + } + } + + schemes + .iter() + .filter_map(|(name, scheme)| { + if used.contains(name) + || !matches!( + &scheme.kind, + NormalizedSecuritySchemeKind::Unsupported { .. } + ) + { + return None; + } + Some(( + document_finding( + "unsupported_security_scheme_definition", + format!( + "Объявленная схема {name} пока не поддерживается и не используется операциями; настройте authentication вручную, если она потребуется." + ), + ), + scheme.source_location.clone(), + )) + }) + .collect() +} + +fn collect_requirement_names(value: Option<&Value>, used: &mut std::collections::BTreeSet) { + let Some(alternatives) = value.and_then(Value::as_array) else { + return; + }; + for alternative in alternatives.iter().filter_map(Value::as_object) { + used.extend(alternative.keys().cloned()); + } +} + fn api_key(definition: &Value, allow_cookie: bool) -> NormalizedSecuritySchemeKind { let location = match definition.get("in").and_then(Value::as_str) { Some("header") => Some(SecurityParameterLocation::Header), @@ -92,15 +156,17 @@ fn api_key(definition: &Value, allow_cookie: bool) -> NormalizedSecuritySchemeKi _ => None, }; match (location, definition.get("name").and_then(Value::as_str)) { - (Some(location), Some(parameter_name)) => NormalizedSecuritySchemeKind::ApiKey { - location, - parameter_name: parameter_name.to_owned(), - format_hint: definition - .get("x-format-hint") - .and_then(Value::as_str) - .filter(|value| safe_format_hint(value) && !looks_secret_hint(value)) - .map(ToOwned::to_owned), - }, + (Some(location), Some(parameter_name)) if !parameter_name.is_empty() => { + NormalizedSecuritySchemeKind::ApiKey { + location, + parameter_name: parameter_name.to_owned(), + format_hint: definition + .get("x-format-hint") + .and_then(Value::as_str) + .filter(|value| safe_format_hint(value) && !looks_secret_hint(value)) + .map(ToOwned::to_owned), + } + } _ => NormalizedSecuritySchemeKind::Unsupported { scheme_type: Some("apiKey".to_owned()), }, @@ -124,6 +190,11 @@ fn looks_secret_hint(value: &str) -> bool { || value.starts_with("bearer ") } +/// Computes the effective requirement for one operation: the operation +/// `security` member wins over the document root; a missing member means no +/// requirement. Every array item is one OR alternative and is classified +/// independently, so a single unsupported alternative never collapses +/// supported siblings into a false AND. pub(super) fn effective_requirements( root: &Value, operation: &Value, @@ -146,19 +217,16 @@ pub(super) fn effective_requirements( "/security".to_owned(), ) } else { - (None, SecurityRequirementsSource::None, String::new()) - }; - let mut findings = Vec::new(); - let Some(value) = value else { return ( NormalizedSecurityRequirements { - source, + source: SecurityRequirementsSource::None, alternatives: Vec::new(), }, - findings, + Vec::new(), ); }; - let Some(alternatives) = value.as_array() else { + let mut findings = Vec::new(); + let Some(alternatives) = value.and_then(Value::as_array) else { findings.push(( document_blocker( "invalid_security_requirements", @@ -176,24 +244,27 @@ pub(super) fn effective_requirements( }; let mut projected = Vec::new(); let mut supported_alternatives = 0usize; + let mut has_malformed_alternative = false; for (alternative_index, alternative) in alternatives.iter().enumerate() { let alternative_pointer = format!("{pointer}/{alternative_index}"); let Some(requirement) = alternative.as_object() else { - let source_location = SourceLocation { - pointer: alternative_pointer, - }; + has_malformed_alternative = true; findings.push(( document_finding( "invalid_security_requirement", "Security alternative имеет неверную структуру и недоступна для выбора.", ), - source_location.clone(), + SourceLocation { + pointer: alternative_pointer.clone(), + }, )); projected.push(SecurityRequirementAlternative { schemes: Vec::new(), supported: false, findings: vec!["invalid_security_requirement".to_owned()], - source_location: Some(source_location), + source_location: Some(SourceLocation { + pointer: alternative_pointer, + }), }); continue; }; @@ -212,7 +283,10 @@ pub(super) fn effective_requirements( "Security scopes должны быть массивом строк; эта альтернатива недоступна для выбора.", ), SourceLocation { - pointer: format!("{alternative_pointer}/{}", super::normalize_coverage::escape_pointer(name)), + pointer: format!( + "{alternative_pointer}/{}", + super::normalize_coverage::escape_pointer(name) + ), }, )); } else if scopes.is_some_and(|scopes| !scopes.is_empty()) @@ -225,7 +299,10 @@ pub(super) fn effective_requirements( format!("Схема {name} не поддерживает OAuth scopes; непустой список блокирует эту alternative."), ), SourceLocation { - pointer: format!("{alternative_pointer}/{}", super::normalize_coverage::escape_pointer(name)), + pointer: format!( + "{alternative_pointer}/{}", + super::normalize_coverage::escape_pointer(name) + ), }, )); } @@ -259,7 +336,7 @@ pub(super) fn effective_requirements( }, )); } - if let Some((_unsupported, message)) = names.iter().find_map(|name| { + if let Some((unsupported, message)) = names.iter().find_map(|name| { schemes.get(name).and_then(|scheme| match &scheme.kind { NormalizedSecuritySchemeKind::Unsupported { .. } => Some(( name, @@ -277,12 +354,12 @@ pub(super) fn effective_requirements( }) { alternative_findings.push("unsupported_security_scheme".to_owned()); findings.push(( - document_finding( - "unsupported_security_scheme", - message, - ), + document_finding("unsupported_security_scheme", message), SourceLocation { - pointer: format!("{alternative_pointer}/{}", super::normalize_coverage::escape_pointer(_unsupported)), + pointer: format!( + "{alternative_pointer}/{}", + super::normalize_coverage::escape_pointer(unsupported) + ), }, )); } @@ -299,11 +376,19 @@ pub(super) fn effective_requirements( }), }); } - if !projected.is_empty() && supported_alternatives == 0 { + if has_malformed_alternative || (!projected.is_empty() && supported_alternatives == 0) { findings.push(( document_blocker( - "unsupported_security_requirements", - "Ни одну authentication alternative нельзя безопасно выбрать; импорт операции заблокирован.", + if has_malformed_alternative { + "invalid_security_requirements" + } else { + "unsupported_security_requirements" + }, + if has_malformed_alternative { + "Security requirements содержат malformed alternative и блокируют импорт операции." + } else { + "Ни одну authentication alternative нельзя безопасно выбрать; импорт операции заблокирован." + }, ), SourceLocation { pointer }, )); @@ -317,17 +402,19 @@ pub(super) fn effective_requirements( ) } +/// Convenience wrapper keeping the parser call site short. The operation +/// value must be the method object (`paths.{path}.{method}`); pointers use +/// the same JSON-pointer escaping as the rest of the importer. pub(super) fn collect_operation_requirements( root: &Value, operation: &Value, - pointer: &str, + operation_pointer: &str, schemes: &BTreeMap, - findings: &mut Vec<(super::model::ImportFinding, SourceLocation)>, -) -> NormalizedSecurityRequirements { - let (requirements, security_findings) = - effective_requirements(root, operation, pointer, schemes); - findings.extend(security_findings); - requirements +) -> ( + NormalizedSecurityRequirements, + Vec<(super::model::ImportFinding, SourceLocation)>, +) { + effective_requirements(root, operation, operation_pointer, schemes) } fn supports_named_scopes(scheme: &NormalizedSecurityScheme) -> bool { @@ -338,3 +425,38 @@ fn supports_named_scopes(scheme: &NormalizedSecurityScheme) -> bool { } if matches!(scheme_type.as_str(), "oauth2" | "openIdConnect") ) } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn empty_api_key_parameter_name_is_unsupported() { + for swagger in [false, true] { + let definition = json!({ "type": "apiKey", "in": "header", "name": "" }); + let root = if swagger { + json!({ "securityDefinitions": { "Key": definition } }) + } else { + json!({ "components": { "securitySchemes": { "Key": definition } } }) + }; + let schemes = schemes(&root, swagger); + assert!(matches!( + schemes["Key"].kind, + NormalizedSecuritySchemeKind::Unsupported { .. } + )); + let (requirements, findings) = effective_requirements( + &root, + &json!({ "security": [{ "Key": [] }] }), + "/paths/~1secure/get", + &schemes, + ); + assert!(!requirements.alternatives[0].supported); + assert!( + findings + .iter() + .any(|(finding, _)| { finding.code == "unsupported_security_requirements" }) + ); + } + } +} diff --git a/crates/crank-import/src/rest/security_literals.rs b/crates/crank-import/src/rest/security_literals.rs deleted file mode 100644 index c277b2a..0000000 --- a/crates/crank-import/src/rest/security_literals.rs +++ /dev/null @@ -1,287 +0,0 @@ -use serde_json::Value; - -use super::model::SourceLocation; - -#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -pub struct SecretLiteralFinding { - pub location: SourceLocation, -} - -/// Scans UTF-8 JSON or YAML and returns source locations without literal values. -pub fn scan_secret_literals( - bytes: &[u8], -) -> Result, super::normalize::ImportParseError> { - let document = std::str::from_utf8(bytes) - .map_err(|_| super::normalize::ImportParseError::InvalidDocument)?; - let mut root = super::normalize::decode(document)?; - Ok(inspect_and_redact(&mut root)) -} - -pub(super) fn inspect_and_redact(root: &mut Value) -> Vec { - let mut findings = Vec::new(); - walk(root, "", false, false, &mut findings, true, false); - findings -} - -fn walk( - value: &mut Value, - pointer: &str, - sensitive_context: bool, - example_context: bool, - findings: &mut Vec, - redact: bool, - cookie_context: bool, -) { - if example_context - && value - .as_object() - .is_some_and(|fields| fields.keys().any(|key| is_jwt(key))) - { - findings.push(SecretLiteralFinding { - location: SourceLocation { - pointer: pointer.to_owned(), - }, - }); - if redact { - *value = Value::String("[REDACTED]".to_owned()); - } - return; - } - match value { - Value::Object(fields) => { - let header_parameter = fields - .get("in") - .and_then(Value::as_str) - .is_some_and(|location| location.eq_ignore_ascii_case("header")); - let parameter_name = fields.get("name").and_then(Value::as_str).unwrap_or(""); - let named_sensitive_parameter = - sensitive_key(parameter_name) && fields.get("in").and_then(Value::as_str).is_some(); - let named_cookie_header = is_cookie_header(parameter_name); - for (key, child) in fields { - let child_pointer = format!( - "{pointer}/{}", - super::normalize_coverage::escape_pointer(key) - ); - let property_definition = (pointer.contains("/properties/") - || pointer.ends_with("/properties")) - && !key.to_ascii_lowercase().starts_with("x-"); - let sensitive = sensitive_context - || (sensitive_key(key) - && !property_definition - && !is_oauth_flow_name(pointer, key)); - let example = example_context - || matches!( - key.to_ascii_lowercase().as_str(), - "example" | "examples" | "default" - ); - let header_example = header_parameter - && matches!( - key.to_ascii_lowercase().as_str(), - "example" | "examples" | "default" - ); - let named_header = is_cookie_header(key); - let credential_example = matches!( - key.to_ascii_lowercase().as_str(), - "example" | "examples" | "default" - ) && (named_sensitive_parameter - || (cookie_context && !property_definition) - || (named_cookie_header && !property_definition)); - walk( - child, - &child_pointer, - sensitive || header_example || credential_example, - example, - findings, - redact, - cookie_context || named_cookie_header || named_header, - ); - } - } - Value::Array(items) => { - for (index, child) in items.iter_mut().enumerate() { - walk( - child, - &format!("{pointer}/{index}"), - sensitive_context, - example_context, - findings, - redact, - cookie_context, - ); - } - } - Value::String(string) => { - let userinfo = url_userinfo_range(string); - let query_secret = sensitive_query_value(string); - let likely_example_secret = (example_context - && (looks_secret_like(pointer, string) || is_jwt(string))) - || looks_secret_value(string) - || ((pointer.ends_with("/bearerFormat") || pointer.ends_with("/x-format-hint")) - && looks_secret_like(pointer, string)); - if sensitive_context || likely_example_secret || userinfo.is_some() || query_secret { - findings.push(SecretLiteralFinding { - location: SourceLocation { - pointer: pointer.to_owned(), - }, - }); - if redact { - if query_secret { - *string = "[REDACTED]".to_owned(); - } else if let Some((start, end)) = userinfo { - string.replace_range(start..end, "[REDACTED]"); - } else { - *string = "[REDACTED]".to_owned(); - } - } - } - } - Value::Number(_) | Value::Bool(_) if sensitive_context => { - findings.push(SecretLiteralFinding { - location: SourceLocation { - pointer: pointer.to_owned(), - }, - }); - if redact { - *value = Value::String("[REDACTED]".to_owned()); - } - } - _ => {} - } -} - -fn sensitive_key(key: &str) -> bool { - let mut normalized = String::new(); - let mut previous_lower = false; - for ch in key.chars() { - if ch.is_ascii_uppercase() && previous_lower { - normalized.push('_'); - } - if ch == '-' || ch == ' ' { - normalized.push('_'); - } else { - normalized.push(ch.to_ascii_lowercase()); - } - previous_lower = ch.is_ascii_lowercase() || ch.is_ascii_digit(); - } - let key = normalized; - if matches!(key.as_str(), "authorization_url" | "token_url") { - return false; - } - [ - "token", - "secret", - "secret_hash", - "secrethash", - "password", - "password_hash", - "passwordhash", - "passwd", - "api_key", - "apikey", - "authorization", - "credential", - "credentials", - "access_key", - "private_key", - "privatekey", - "ciphertext", - ] - .iter() - .any(|marker| { - key == *marker - || key.ends_with(&format!("_{marker}")) - || key.starts_with(&format!("{marker}_")) - }) -} - -fn is_oauth_flow_name(parent_pointer: &str, key: &str) -> bool { - if !parent_pointer.ends_with("/flows") { - return false; - } - let compact_key: String = key - .chars() - .filter(|character| character.is_ascii_alphanumeric()) - .map(|character| character.to_ascii_lowercase()) - .collect(); - matches!( - compact_key.as_str(), - "implicit" | "password" | "clientcredentials" | "authorizationcode" - ) -} - -fn is_cookie_header(value: &str) -> bool { - value.eq_ignore_ascii_case("cookie") || value.eq_ignore_ascii_case("set-cookie") -} - -fn is_jwt(value: &str) -> bool { - let mut parts = value.split('.'); - let Some(header) = parts.next() else { - return false; - }; - let Some(payload) = parts.next() else { - return false; - }; - let Some(signature) = parts.next() else { - return false; - }; - parts.next().is_none() - && header.starts_with("eyJ") - && payload.starts_with("eyJ") - && header.len() >= 8 - && payload.len() >= 16 - && signature.len() >= 12 - && [header, payload, signature].iter().all(|part| { - !part.is_empty() - && part - .bytes() - .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-') - }) -} - -fn sensitive_query_value(value: &str) -> bool { - let Some(query_start) = value.find('?') else { - return false; - }; - value[query_start + 1..] - .split('#') - .next() - .unwrap_or_default() - .split(['&', ';']) - .any(|pair| { - let (key, value) = pair.split_once('=').unwrap_or((pair, "")); - !value.is_empty() && sensitive_key(key) - }) -} - -fn looks_secret_like(pointer: &str, value: &str) -> bool { - let pointer = pointer.to_ascii_lowercase(); - let value = value.to_ascii_lowercase(); - [ - "token", - "secret", - "password", - "api_key", - "apikey", - "authorization", - ] - .iter() - .any(|marker| pointer.contains(marker) || value.contains(marker)) - || value.starts_with("bearer ") - || value.starts_with("sk-") -} - -fn looks_secret_value(value: &str) -> bool { - let value = value.to_ascii_lowercase(); - value.starts_with("bearer ") || value.starts_with("sk-") -} - -fn url_userinfo_range(value: &str) -> Option<(usize, usize)> { - let scheme_end = value.find("://")? + 3; - let authority_end = value[scheme_end..] - .find('/') - .map(|index| scheme_end + index) - .unwrap_or(value.len()); - let authority = &value[scheme_end..authority_end]; - let at = authority.find('@')?; - (at > 0).then_some((scheme_end, scheme_end + at)) -} diff --git a/crates/crank-import/src/rest/source_safety.rs b/crates/crank-import/src/rest/source_safety.rs new file mode 100644 index 0000000..311c621 --- /dev/null +++ b/crates/crank-import/src/rest/source_safety.rs @@ -0,0 +1,103 @@ +use crate::rest::model::{ExternalDocumentSnapshot, SourceDigest}; + +use super::model::NormalizationConfig; +use super::{ + decode_budget, metadata_findings, + normalize::{ImportParseError, decode, validate_value_limits}, + normalize_limits, reference, +}; + +/// Rejects a source bundle containing credential-like literals before any +/// primary or dependency bytes are persisted. Credential names are collected +/// across all raw documents so split `$ref` targets receive the same checks. +pub fn validate_document_bundle_source_safe( + documents: &[&str], + config: &NormalizationConfig, +) -> Result<(), ImportParseError> { + config + .validate_versions() + .map_err(|_| ImportParseError::UnsupportedDocument)?; + if config + .max_reference_documents + .checked_add(1) + .is_some_and(|cap| documents.len() > cap) + { + return Err(ImportParseError::LimitExceeded); + } + for document in documents { + if document.len() > config.max_bytes + || normalize_limits::alias_count(document) > config.max_aliases + { + return Err(ImportParseError::LimitExceeded); + } + } + decode_budget::preflight_raw_bundle(documents, config)?; + let mut roots = Vec::with_capacity(documents.len()); + for document in documents { + let root = decode(document)?; + validate_value_limits(&root, config)?; + roots.push(root); + } + let declared_names = metadata_findings::credential_names(roots.iter()); + for mut root in roots { + let mut findings = Vec::new(); + let mut locations = Vec::new(); + metadata_findings::redact_secret_like_literals_with_names( + &mut root, + "", + &declared_names, + &mut findings, + &mut locations, + ); + if !findings.is_empty() { + return Err(ImportParseError::InvalidDocument); + } + } + Ok(()) +} + +/// Validates the resolved source tree with the same security-aware traversal +/// used by normalization, before a caller persists any bytes in the bundle. +pub fn validate_resolved_bundle_source_safe( + document: &str, + digest: SourceDigest, + snapshots: &[ExternalDocumentSnapshot], + config: &NormalizationConfig, +) -> Result<(), ImportParseError> { + config + .validate_versions() + .map_err(|_| ImportParseError::UnsupportedDocument)?; + if document.len() > config.max_bytes + || normalize_limits::alias_count(document) > config.max_aliases + { + return Err(ImportParseError::LimitExceeded); + } + if snapshots.len() > config.max_reference_documents { + return Err(ImportParseError::LimitExceeded); + } + for snapshot in snapshots { + if snapshot.document.len() > config.max_external_document_bytes + || normalize_limits::alias_count(&snapshot.document) > config.max_aliases + { + return Err(ImportParseError::LimitExceeded); + } + } + decode_budget::preflight_bundle(document, snapshots, config)?; + let root = decode(document)?; + validate_value_limits(&root, config)?; + for snapshot in snapshots { + if snapshot.document.len() > config.max_external_document_bytes { + return Err(ImportParseError::LimitExceeded); + } + } + let resolution = reference::resolve(root, digest, snapshots, config)?; + let mut root = resolution.root; + validate_value_limits(&root, config)?; + let mut findings = Vec::new(); + let mut locations = Vec::new(); + metadata_findings::redact_secret_like_literals(&mut root, "", &mut findings, &mut locations); + if !findings.is_empty() { + return Err(ImportParseError::InvalidDocument); + } + Ok(()) +} diff --git a/crates/crank-import/src/rest/strict_decode.rs b/crates/crank-import/src/rest/strict_decode.rs new file mode 100644 index 0000000..68effe1 --- /dev/null +++ b/crates/crank-import/src/rest/strict_decode.rs @@ -0,0 +1,105 @@ +use serde::de::{self, MapAccess, SeqAccess, Visitor}; +use serde::{Deserialize, Deserializer}; +use serde_json::{Map, Value}; + +/// Decodes JSON or YAML into a JSON value while rejecting duplicate mapping +/// keys at every depth. This is used for new primary documents and immutable +/// external snapshots; the isolated legacy v1 decoder keeps its old behavior. +pub(super) fn decode(document: &str) -> Result { + let value = serde_json::from_str::(document) + .or_else(|_| serde_yaml::from_str::(document)) + .map_err(|_| ())?; + Ok(value.0) +} + +struct StrictValue(Value); + +impl<'de> Deserialize<'de> for StrictValue { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_any(StrictValueVisitor) + } +} + +struct StrictValueVisitor; + +impl<'de> Visitor<'de> for StrictValueVisitor { + type Value = StrictValue; + + fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("a JSON-compatible value without duplicate object keys") + } + + fn visit_bool(self, value: bool) -> Result { + Ok(StrictValue(Value::Bool(value))) + } + + fn visit_i64(self, value: i64) -> Result { + Ok(StrictValue(Value::Number(value.into()))) + } + + fn visit_u64(self, value: u64) -> Result { + Ok(StrictValue(Value::Number(value.into()))) + } + + fn visit_f64(self, value: f64) -> Result + where + E: de::Error, + { + serde_json::Number::from_f64(value) + .map(Value::Number) + .map(StrictValue) + .ok_or_else(|| E::custom("value contains a non-finite number")) + } + + fn visit_str(self, value: &str) -> Result { + Ok(StrictValue(Value::String(value.to_owned()))) + } + + fn visit_string(self, value: String) -> Result { + Ok(StrictValue(Value::String(value))) + } + + fn visit_none(self) -> Result { + Ok(StrictValue(Value::Null)) + } + + fn visit_unit(self) -> Result { + Ok(StrictValue(Value::Null)) + } + + fn visit_some(self, deserializer: D) -> Result + where + D: Deserializer<'de>, + { + StrictValue::deserialize(deserializer) + } + + fn visit_seq(self, mut sequence: A) -> Result + where + A: SeqAccess<'de>, + { + let mut values = Vec::new(); + while let Some(value) = sequence.next_element::()? { + values.push(value.0); + } + Ok(StrictValue(Value::Array(values))) + } + + fn visit_map(self, mut mapping: A) -> Result + where + A: MapAccess<'de>, + { + let mut values = Map::new(); + while let Some(key) = mapping.next_key::()? { + if values.contains_key(&key) { + return Err(de::Error::custom("duplicate object key")); + } + let value = mapping.next_value::()?; + values.insert(key, value.0); + } + Ok(StrictValue(Value::Object(values))) + } +} diff --git a/crates/crank-import/src/rest/swagger2.rs b/crates/crank-import/src/rest/swagger2.rs index 8d1d91a..5c7b881 100644 --- a/crates/crank-import/src/rest/swagger2.rs +++ b/crates/crank-import/src/rest/swagger2.rs @@ -15,16 +15,16 @@ pub fn parse_document(root: &Value) -> Result Result { + let security_schemes = super::security::schemes(root, true); let title = root .pointer("/info/title") .and_then(Value::as_str) .unwrap_or("Imported API") .to_owned(); let servers = swagger_servers(root); - let security_schemes = super::security::schemes(root, true); let mut findings = Vec::new(); let mut internal_finding_locations = Vec::new(); - append_parameter_findings( + super::parser_findings::append_located( &mut findings, &mut internal_finding_locations, super::swagger2_findings::servers(root), @@ -74,7 +74,7 @@ fn parse_document_v2(root: &Value) -> Result Result Result Result Result) { parameters.reverse(); } -pub(super) fn swagger_servers(root: &Value) -> Vec { +pub fn parse_document_legacy_v1(root: &Value) -> Result { + let title = root + .pointer("/info/title") + .and_then(Value::as_str) + .unwrap_or("Imported API") + .to_owned(); + let servers = swagger_servers(root); + let mut findings = Vec::new(); + if servers.is_empty() { + findings.push(document_finding( + "missing_servers", + "В Swagger 2.0 документе не указаны host/schemes, base URL нужно будет выбрать вручную.", + )); + } + let mut operations = Vec::new(); + let paths = root + .get("paths") + .and_then(Value::as_object) + .ok_or(ImportParseError::UnsupportedDocument)?; + for (path, path_item) in paths { + let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1")); + let path_parameters = legacy_parameters( + root, + path_item.get("parameters"), + &format!("{path_pointer}/parameters"), + ); + for method_name in ["get", "post", "put", "patch", "delete"] { + let Some(operation) = path_item.get(method_name) else { + continue; + }; + let Some(method) = super::http_methods::from_lower(method_name) else { + continue; + }; + let operation_pointer = format!("{path_pointer}/{method_name}"); + let mut parameters = path_parameters.clone(); + parameters.extend(legacy_parameters( + root, + operation.get("parameters"), + &format!("{operation_pointer}/parameters"), + )); + let request_body_schema = parameters + .iter() + .find(|parameter| parameter.name == "body") + .and_then(|parameter| parameter.schema.clone()); + let parameters = parameters + .into_iter() + .filter(|parameter| parameter.name != "body") + .collect(); + operations.push(RestImportOperation { + key: format!("{} {}", method_name.to_uppercase(), path), + method, + path: path.clone(), + source_location: None, + security_requirements: Default::default(), + operation_id: operation + .get("operationId") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + summary: operation + .get("summary") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + description: operation + .get("description") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + tags: legacy_tags(operation.get("tags")), + parameters, + request_body_schema, + request_body_schema_location: None, + request_body_variants: Vec::new(), + response_schema: legacy_response_schema(root, operation), + response_schema_location: None, + response_variants: Vec::new(), + servers: Vec::new(), + findings: super::swagger2_findings::legacy_operation(path, operation), + internal_finding_locations: Vec::new(), + }); + } + } + Ok(RestImportDocument { + format: "swagger".to_owned(), + version: Some("2.0".to_owned()), + title, + servers, + security_schemes: Default::default(), + operations, + findings, + internal_finding_locations: Vec::new(), + }) +} + +fn legacy_tags(value: Option<&Value>) -> Vec { + value + .and_then(Value::as_array) + .map(|items| { + items + .iter() + .filter_map(Value::as_str) + .map(ToOwned::to_owned) + .collect() + }) + .unwrap_or_default() +} + +fn legacy_parameters( + root: &Value, + value: Option<&Value>, + base_pointer: &str, +) -> Vec { + value + .and_then(Value::as_array) + .map(|items| { + items + .iter() + .enumerate() + .filter_map(|(index, item)| { + let item = crate::rest::normalize::resolve_local_ref(root, item, 0); + let raw_location = item.get("in").and_then(Value::as_str)?; + let pointer = format!("{base_pointer}/{index}"); + if raw_location == "body" { + return Some(RestImportParameter { + name: "body".to_owned(), + location: RestParameterLocation::Query, + required: item + .get("required") + .and_then(Value::as_bool) + .unwrap_or(false), + description: item + .get("description") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + schema: item.get("schema").map(|schema| { + crate::rest::normalize::resolve_local_ref(root, schema, 0) + }), + source_location: crate::rest::model::SourceLocation { pointer }, + schema_source_location: None, + }); + } + let location = match raw_location { + "path" => RestParameterLocation::Path, + "query" => RestParameterLocation::Query, + "header" => RestParameterLocation::Header, + _ => return None, + }; + Some(RestImportParameter { + name: item.get("name").and_then(Value::as_str)?.to_owned(), + location, + required: item + .get("required") + .and_then(Value::as_bool) + .unwrap_or(false) + || location == RestParameterLocation::Path, + description: item + .get("description") + .and_then(Value::as_str) + .map(ToOwned::to_owned), + schema: legacy_parameter_schema(root, &item), + source_location: crate::rest::model::SourceLocation { pointer }, + schema_source_location: None, + }) + }) + .collect() + }) + .unwrap_or_default() +} + +fn legacy_parameter_schema(root: &Value, parameter: &Value) -> Option { + if let Some(schema) = parameter.get("schema") { + return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); + } + let mut schema = serde_json::Map::new(); + for key in ["type", "format", "items", "enum", "default", "description"] { + if let Some(value) = parameter.get(key) { + schema.insert( + key.to_owned(), + crate::rest::normalize::resolve_local_ref(root, value, 0), + ); + } + } + (!schema.is_empty()).then_some(Value::Object(schema)) +} + +fn legacy_response_schema(root: &Value, operation: &Value) -> Option { + let responses = operation.get("responses")?.as_object()?; + for code in ["200", "201", "202", "default"] { + let Some(response) = responses.get(code) else { + continue; + }; + let response = crate::rest::normalize::resolve_local_ref(root, response, 0); + if let Some(schema) = response.get("schema") { + return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); + } + } + None +} + +fn swagger_servers(root: &Value) -> Vec { let Some(host) = root.get("host").and_then(Value::as_str) else { return Vec::new(); }; @@ -467,17 +673,6 @@ fn parameter_blocker( ) } -fn append_parameter_findings( - findings: &mut Vec, - locations: &mut Vec>, - parameter_findings: Vec<(ImportFinding, crate::rest::model::SourceLocation)>, -) { - for (finding, location) in parameter_findings { - findings.push(finding); - locations.push(Some(location)); - } -} - fn swagger_parameter_schema(_root: &Value, parameter: &Value) -> Option { if let Some(schema) = parameter.get("schema") { return Some(schema.clone()); diff --git a/crates/crank-import/src/rest/swagger2_findings.rs b/crates/crank-import/src/rest/swagger2_findings.rs index 0873268..082f83a 100644 --- a/crates/crank-import/src/rest/swagger2_findings.rs +++ b/crates/crank-import/src/rest/swagger2_findings.rs @@ -1,7 +1,7 @@ use serde_json::Value; use crate::rest::{ - model::{ImportFinding, RestImportParameter, RestParameterLocation, SourceLocation}, + model::{ImportFinding, RestImportParameter, SourceLocation}, recommendations::{document_blocker, operation_finding}, }; @@ -175,33 +175,9 @@ pub(super) fn legacy_operation(path: &str, operation: &Value) -> Vec Vec<(ImportFinding, SourceLocation)> { - parameters - .iter() - .filter(|parameter| parameter.location == RestParameterLocation::Path) - .filter_map(|parameter| { - let finding = if !parameter.required { - Some(( - "path_parameter_not_required", - format!("Path parameter {} должен быть required.", parameter.name), - )) - } else if !path.contains(&format!("{{{}}}", parameter.name)) { - Some(( - "path_parameter_placeholder_missing", - format!( - "Path parameter {} не имеет placeholder в пути.", - parameter.name - ), - )) - } else { - None - }?; - Some(( - document_blocker(finding.0, finding.1), - parameter.source_location.clone(), - )) - }) - .collect() + super::openapi3_findings::path_parameters(path, parameters, operation_pointer) } pub(super) fn unsupported_methods(path_item: &Value) -> Vec<&str> { diff --git a/crates/crank-import/src/rest/swagger2_legacy.rs b/crates/crank-import/src/rest/swagger2_legacy.rs deleted file mode 100644 index 6bba2ab..0000000 --- a/crates/crank-import/src/rest/swagger2_legacy.rs +++ /dev/null @@ -1,206 +0,0 @@ -use serde_json::Value; - -use super::swagger2::swagger_servers; -use crate::rest::{ - model::{RestImportDocument, RestImportOperation, RestImportParameter, RestParameterLocation}, - normalize::ImportParseError, - recommendations::document_finding, -}; - -pub(super) fn parse_document_legacy_v1( - root: &Value, -) -> Result { - let title = root - .pointer("/info/title") - .and_then(Value::as_str) - .unwrap_or("Imported API") - .to_owned(); - let servers = swagger_servers(root); - let mut findings = Vec::new(); - if servers.is_empty() { - findings.push(document_finding( - "missing_servers", - "В Swagger 2.0 документе не указаны host/schemes, base URL нужно будет выбрать вручную.", - )); - } - let mut operations = Vec::new(); - let paths = root - .get("paths") - .and_then(Value::as_object) - .ok_or(ImportParseError::UnsupportedDocument)?; - for (path, path_item) in paths { - let path_pointer = format!("/paths/{}", path.replace('~', "~0").replace('/', "~1")); - let path_parameters = legacy_parameters( - root, - path_item.get("parameters"), - &format!("{path_pointer}/parameters"), - ); - for method_name in ["get", "post", "put", "patch", "delete"] { - let Some(operation) = path_item.get(method_name) else { - continue; - }; - let Some(method) = super::http_methods::from_lower(method_name) else { - continue; - }; - let operation_pointer = format!("{path_pointer}/{method_name}"); - let mut parameters = path_parameters.clone(); - parameters.extend(legacy_parameters( - root, - operation.get("parameters"), - &format!("{operation_pointer}/parameters"), - )); - let request_body_schema = parameters - .iter() - .find(|parameter| parameter.name == "body") - .and_then(|parameter| parameter.schema.clone()); - let parameters = parameters - .into_iter() - .filter(|parameter| parameter.name != "body") - .collect(); - operations.push(RestImportOperation { - key: format!("{} {}", method_name.to_uppercase(), path), - method, - path: path.clone(), - security_requirements: Default::default(), - operation_id: operation - .get("operationId") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - summary: operation - .get("summary") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - description: operation - .get("description") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - tags: legacy_tags(operation.get("tags")), - parameters, - request_body_schema, - request_body_schema_location: None, - request_body_variants: Vec::new(), - response_schema: legacy_response_schema(root, operation), - response_schema_location: None, - response_variants: Vec::new(), - servers: Vec::new(), - findings: super::swagger2_findings::legacy_operation(path, operation), - internal_finding_locations: Vec::new(), - }); - } - } - Ok(RestImportDocument { - format: "swagger".to_owned(), - version: Some("2.0".to_owned()), - title, - servers, - security_schemes: Default::default(), - operations, - findings, - internal_finding_locations: Vec::new(), - }) -} - -fn legacy_tags(value: Option<&Value>) -> Vec { - value - .and_then(Value::as_array) - .map(|items| { - items - .iter() - .filter_map(Value::as_str) - .map(ToOwned::to_owned) - .collect() - }) - .unwrap_or_default() -} - -fn legacy_parameters( - root: &Value, - value: Option<&Value>, - base_pointer: &str, -) -> Vec { - value - .and_then(Value::as_array) - .map(|items| { - items - .iter() - .enumerate() - .filter_map(|(index, item)| { - let item = crate::rest::normalize::resolve_local_ref(root, item, 0); - let raw_location = item.get("in").and_then(Value::as_str)?; - let pointer = format!("{base_pointer}/{index}"); - if raw_location == "body" { - return Some(RestImportParameter { - name: "body".to_owned(), - location: RestParameterLocation::Query, - required: item - .get("required") - .and_then(Value::as_bool) - .unwrap_or(false), - description: item - .get("description") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - schema: item.get("schema").map(|schema| { - crate::rest::normalize::resolve_local_ref(root, schema, 0) - }), - source_location: crate::rest::model::SourceLocation { pointer }, - schema_source_location: None, - }); - } - let location = match raw_location { - "path" => RestParameterLocation::Path, - "query" => RestParameterLocation::Query, - "header" => RestParameterLocation::Header, - _ => return None, - }; - Some(RestImportParameter { - name: item.get("name").and_then(Value::as_str)?.to_owned(), - location, - required: item - .get("required") - .and_then(Value::as_bool) - .unwrap_or(false) - || location == RestParameterLocation::Path, - description: item - .get("description") - .and_then(Value::as_str) - .map(ToOwned::to_owned), - schema: legacy_parameter_schema(root, &item), - source_location: crate::rest::model::SourceLocation { pointer }, - schema_source_location: None, - }) - }) - .collect() - }) - .unwrap_or_default() -} - -fn legacy_parameter_schema(root: &Value, parameter: &Value) -> Option { - if let Some(schema) = parameter.get("schema") { - return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); - } - let mut schema = serde_json::Map::new(); - for key in ["type", "format", "items", "enum", "default", "description"] { - if let Some(value) = parameter.get(key) { - schema.insert( - key.to_owned(), - crate::rest::normalize::resolve_local_ref(root, value, 0), - ); - } - } - (!schema.is_empty()).then_some(Value::Object(schema)) -} - -fn legacy_response_schema(root: &Value, operation: &Value) -> Option { - let responses = operation.get("responses")?.as_object()?; - for code in ["200", "201", "202", "default"] { - let Some(response) = responses.get(code) else { - continue; - }; - let response = crate::rest::normalize::resolve_local_ref(root, response, 0); - if let Some(schema) = response.get("schema") { - return Some(crate::rest::normalize::resolve_local_ref(root, schema, 0)); - } - } - None -} diff --git a/crates/crank-import/tests/corpus.rs b/crates/crank-import/tests/corpus.rs new file mode 100644 index 0000000..c9e1df3 --- /dev/null +++ b/crates/crank-import/tests/corpus.rs @@ -0,0 +1,962 @@ +//! Offline regression gates for the versioned OpenAPI support matrix and corpus. +use crank_import::rest::model::CoverageDisposition; +use crank_import::rest::model::{CanonicalSourceNode, CanonicalSourceValue}; +use crank_import::rest::{ + ImportParseError, NormalizationConfig, SourceDigest, normalize_verified_document, + preview_from_ir, validate_normalized_ir, +}; +use serde::Deserialize; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use std::{ + fs, + path::{Path, PathBuf}, +}; + +#[path = "corpus/matrix.rs"] +mod matrix; + +fn tests_root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests") +} +fn hex(bytes: impl AsRef<[u8]>) -> String { + Sha256::digest(bytes) + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} +fn canonical_json(value: &Value) -> Vec { + serde_json::to_vec(value).expect("JSON values serialize") +} +fn escape_pointer(value: &str) -> String { + value.replace('~', "~0").replace('/', "~1") +} +fn source_ledger( + value: &Value, + pointer: &str, + ledger: &mut std::collections::BTreeMap, +) { + assert!(ledger.insert(pointer.to_owned(), value.clone()).is_none()); + match value { + Value::Array(items) => { + for (index, item) in items.iter().enumerate() { + source_ledger(item, &format!("{pointer}/{index}"), ledger); + } + } + Value::Object(fields) => { + for (key, item) in fields { + source_ledger(item, &format!("{pointer}/{}", escape_pointer(key)), ledger); + } + } + _ => {} + } +} +fn canonical_source_ledger( + node: &CanonicalSourceNode, + ledger: &mut std::collections::BTreeMap, +) -> Value { + let value = match &node.value { + CanonicalSourceValue::Null => Value::Null, + CanonicalSourceValue::Boolean(value) => json!(value), + CanonicalSourceValue::Number(value) => { + serde_json::from_str(value).expect("canonical number") + } + CanonicalSourceValue::String(value) => json!(value), + CanonicalSourceValue::Array(items) => Value::Array( + items + .iter() + .map(|item| canonical_source_ledger(item, ledger)) + .collect(), + ), + CanonicalSourceValue::Object(items) => { + let mut fields = serde_json::Map::new(); + for (key, item) in items { + fields.insert(key.clone(), canonical_source_ledger(item, ledger)); + } + Value::Object(fields) + } + }; + assert!( + ledger + .insert(node.location.pointer.clone(), value.clone()) + .is_none() + ); + value +} + +#[derive(Debug, Deserialize)] +struct Manifest { + corpus_version: String, + matrix_version: String, + compatibility_impact: String, + documents: Vec, +} +#[derive(Debug, Deserialize)] +struct Document { + id: String, + file: String, + sha256: String, + canonical_digest: Option, + origin: String, + license: String, + specification_version: String, + classification: String, + expected_preview: Value, +} +fn manifest() -> Manifest { + serde_json::from_slice( + &fs::read(tests_root().join("corpus/manifest.json")).expect("manifest exists"), + ) + .expect("manifest JSON is valid") +} +fn normalize(source: &str) -> Result { + let source_digest = SourceDigest::parse(hex(source.as_bytes())).expect("sha256 is valid"); + normalize_verified_document(source, source_digest, &NormalizationConfig::default()) +} + +#[test] +fn ac_2_manifest_covers_fifty_provenanced_specs_with_exact_expectations() { + let manifest = manifest(); + assert_eq!(manifest.corpus_version, "1.0.4"); + assert_eq!(manifest.matrix_version, "1.0.3"); + assert!(!manifest.compatibility_impact.trim().is_empty()); + assert!(manifest.documents.len() >= 50); + let root = tests_root().join("corpus"); + let mut versions = std::collections::BTreeSet::new(); + let mut representative_count = 0; + for document in &manifest.documents { + assert!(!document.id.is_empty()); + assert!( + document.origin.starts_with("synthetic:"), + "{} lacks provenance", + document.id + ); + assert!( + document.license.contains("CC0"), + "{} lacks license", + document.id + ); + assert_eq!(document.sha256.len(), 64); + versions.insert(document.specification_version.as_str()); + let source = fs::read(root.join(&document.file)) + .unwrap_or_else(|e| panic!("{}: {e}", document.file)); + assert_eq!( + hex(&source), + document.sha256, + "checksum mismatch for {}", + document.file + ); + if let Ok(value) = serde_json::from_slice::(&source) { + let digest = hex(canonical_json(&value)); + assert_eq!( + document.canonical_digest.as_deref(), + Some(digest.as_str()), + "canonical digest mismatch for {}", + document.file + ); + } else { + assert!( + document.canonical_digest.is_none(), + "malformed JSON has no canonical digest" + ); + } + if document.classification == "representative" { + representative_count += 1; + let text = std::str::from_utf8(&source).expect("fixtures are UTF-8"); + let ir = + normalize(text).unwrap_or_else(|e| panic!("{} failed parser: {e}", document.file)); + validate_normalized_ir(&ir).expect("normalized IR has explicit source coverage"); + let preview = preview_from_ir(&ir); + let repeated_preview = + preview_from_ir(&normalize(text).expect("repeat normalization succeeds")); + assert_eq!( + serde_json::to_value(&preview).unwrap(), + serde_json::to_value(&repeated_preview).unwrap(), + "deterministic preview diff for {}:\nexpected={}\nactual={}", + document.file, + serde_json::to_string_pretty(&preview).unwrap(), + serde_json::to_string_pretty(&repeated_preview).unwrap(), + ); + let candidates = preview + .groups + .iter() + .flat_map(|g| &g.operations) + .collect::>(); + assert_eq!( + candidates.len(), + document.expected_preview["operation_count"] + .as_u64() + .unwrap() as usize, + "operation count in {}", + document.file + ); + assert_eq!( + candidates.len(), + 1, + "corpus fixtures are single-operation exact oracles" + ); + let expected = &document.expected_preview["operations"][0]; + let mut actual_candidate = serde_json::to_value(candidates[0]).unwrap(); + // Corpus snapshots freeze the pre-location semantic candidate. + // Source provenance is covered by dedicated preview assertions. + actual_candidate + .as_object_mut() + .unwrap() + .remove("source_location"); + for finding in actual_candidate["findings"].as_array_mut().unwrap() { + finding.as_object_mut().unwrap().remove("source_location"); + } + assert_eq!( + actual_candidate, + expected["candidate"], + "frozen candidate diff for {}:\nexpected={}\nactual={}", + document.file, + serde_json::to_string_pretty(&expected["candidate"]).unwrap(), + serde_json::to_string_pretty(&actual_candidate).unwrap(), + ); + let blocking_codes = candidates[0] + .findings + .iter() + .filter(|finding| { + finding.severity == crank_import::rest::ImportFindingSeverity::Error + }) + .map(|finding| finding.code.clone()) + .collect::>(); + assert_eq!(json!(blocking_codes), expected["blocker_codes"]); + assert_eq!( + blocking_codes.is_empty(), + expected["actionable"].as_bool().unwrap() + ); + assert_eq!( + hex(canonical_json(&actual_candidate)), + expected["candidate_digest"], + "candidate canonical digest in {}", + document.file + ); + let mut actual_preview = serde_json::to_value(&preview).unwrap(); + for group in actual_preview["groups"].as_array_mut().unwrap() { + for candidate in group["operations"].as_array_mut().unwrap() { + candidate.as_object_mut().unwrap().remove("source_location"); + for finding in candidate["findings"].as_array_mut().unwrap() { + finding.as_object_mut().unwrap().remove("source_location"); + } + } + } + for finding in actual_preview["findings"].as_array_mut().unwrap() { + finding.as_object_mut().unwrap().remove("source_location"); + } + assert_eq!( + hex(canonical_json(&actual_preview)), + document.expected_preview["canonical_preview_digest"], + "preview canonical digest in {}", + document.file + ); + assert_eq!( + actual_preview, + document.expected_preview["canonical_preview"], + "frozen readable preview diff for {}:\nexpected={}\nactual={}", + document.file, + serde_json::to_string_pretty(&document.expected_preview["canonical_preview"]) + .unwrap(), + serde_json::to_string_pretty(&actual_preview).unwrap(), + ); + assert_eq!( + serde_json::to_value(candidates[0].method).unwrap().as_str(), + expected["method"].as_str() + ); + assert_eq!(candidates[0].path, expected["path"].as_str().unwrap()); + assert_eq!( + candidates[0].operation_id.as_deref(), + expected["operation_id"].as_str() + ); + assert!( + preview.findings.is_empty(), + "unexpected preview findings in {}: {:?}", + document.file, + preview.findings + ); + let mut actual_findings = candidates[0] + .findings + .iter() + .map(|f| f.code.as_str()) + .collect::>(); + let mut expected_findings = document.expected_preview["finding_codes"] + .as_array() + .unwrap() + .iter() + .map(|v| v.as_str().unwrap()) + .collect::>(); + actual_findings.sort_unstable(); + expected_findings.sort_unstable(); + assert_eq!( + actual_findings, expected_findings, + "finding codes in {}", + document.file + ); + } else { + assert_eq!(document.classification, "hostile"); + let text = std::str::from_utf8(&source).unwrap(); + let parsed = normalize(text); + assert_eq!( + parsed, + normalize(text), + "hostile parser result changed on deterministic rerun for {}", + document.file + ); + if [ + "hostile-empty.json", + "hostile-malformed.json", + "hostile-oversized.json", + "hostile-deep.json", + "hostile-aliases.yaml", + ] + .contains(&document.file.as_str()) + { + if [ + "hostile-oversized.json", + "hostile-deep.json", + "hostile-aliases.yaml", + ] + .contains(&document.file.as_str()) + { + assert!( + matches!(parsed, Err(ImportParseError::LimitExceeded)), + "{} must fail within configured bounds: {parsed:?}", + document.file + ); + } else { + assert!(parsed.is_err(), "{} must be rejected", document.file); + } + } else if document.file == "hostile-trace.json" { + let ir = parsed.expect("TRACE is reported instead of silently discarded"); + assert!( + ir.findings + .iter() + .any(|f| f.code == "unsupported_http_method") + ); + assert!(ir.coverage.iter().any(|c| c.construct_id.contains("trace") + && c.disposition == CoverageDisposition::Finding)); + } else if document.file == "hostile-xml.json" { + let ir = parsed.expect("XML is represented by an explicit finding"); + let preview = preview_from_ir(&ir); + let codes = preview + .groups + .iter() + .flat_map(|g| &g.operations) + .flat_map(|o| &o.findings) + .map(|f| f.code.as_str()) + .collect::>(); + assert!( + codes + .iter() + .any(|code| code.contains("xml") || code.contains("media")), + "XML findings: {codes:?}" + ); + } else if document.file == "hostile-secret.json" { + let ir = parsed.expect("secret-like examples remain inspectable"); + let evidence = serde_json::to_string(&preview_from_ir(&ir)).unwrap(); + assert!( + !evidence.contains("sk_test_corpus_secret_canary"), + "secret canary leaked into preview evidence" + ); + } else { + let ir = parsed.unwrap_or_else(|error| { + panic!( + "{} must have an explicit finding or bounded rejection: {error}", + document.file + ) + }); + validate_normalized_ir(&ir).expect("hostile construct coverage remains explicit"); + assert!( + !ir.coverage.is_empty(), + "{} has no construct ledger", + document.file + ); + let preview = preview_from_ir(&ir); + match document.file.as_str() { + "hostile-cycle.json" => { + assert_eq!( + ir.operations.len(), + 1, + "recursive schema must not drop the source operation" + ); + assert!( + ir.findings + .iter() + .chain(ir.operations.iter().flat_map(|op| &op.findings)) + .all(|finding| { + finding.severity + != crank_import::rest::ImportFindingSeverity::Error + }), + "bounded cycle policy must not yield an unexplained blocker: {:?} {:?}", + ir.findings, + ir.operations.iter().map(|op| &op.findings) + ); + assert!( + ir.unresolved_references.is_empty(), + "resolved recursion must not be reported as unresolvable: {:?}", + ir.unresolved_references + ); + let cycle_edge = |recursive: bool| { + ir.reference_graph.edges.iter().any(|edge| { + edge.recursive == recursive + && edge.source.location.pointer + == "/components/schemas/Node/properties/next/$ref" + && edge.target.location.pointer == "/components/schemas/Node" + }) + }; + assert!( + cycle_edge(false) && cycle_edge(true), + "Node.next must resolve through both bounded and recursive edges: {:?}", + ir.reference_graph.edges + ); + let response = ir.operations[0] + .response_variants + .iter() + .find(|variant| variant.status_code == 200) + .expect("200 response is retained"); + assert!( + matches!( + &response.schema, + Some(schema) if matches!( + &schema.kind, + crank_import::rest::NormalizedSchemaKind::Object { + properties, .. + } if properties.contains_key("next") + ) + ), + "recursive node schema must be projected: {:?}", + response.schema + ); + assert!( + ir.coverage.iter().any(|entry| { + entry.location.pointer + == "/components/schemas/Node/properties/next/properties/next" + && entry.disposition == CoverageDisposition::Mapped + }), + "bounded recursion must remain Mapped in the resolved source ledger: {:?}", + ir.coverage + ); + assert_eq!( + document.expected_preview["mapped_recursive_pointer"].as_str(), + Some("/components/schemas/Node/properties/next/properties/next") + ); + assert_eq!( + ir.findings + .iter() + .map(|f| (f.code.clone(), f.severity.clone())) + .collect::>(), + vec![( + "missing_servers".to_owned(), + crank_import::rest::ImportFindingSeverity::Warning + )], + "cycle fixture must expose only the exact bounded warning set: {:?}", + ir.findings + ); + assert_eq!( + document.expected_preview["operation_count"].as_u64(), + Some(1) + ); + assert_eq!( + document.expected_preview["recursive_edge"]["source"].as_str(), + Some("/components/schemas/Node/properties/next/$ref") + ); + assert_eq!( + document.expected_preview["recursive_edge"]["target"].as_str(), + Some("/components/schemas/Node") + ); + assert_eq!(document.expected_preview["blocker_findings"], json!([])); + assert_eq!( + document.expected_preview["unresolved_references"], + json!([]) + ); + assert_eq!( + document.expected_preview["warning_findings"], + json!([{"code":"missing_servers","severity":"Warning","operation_key":null}]) + ); + } + "hostile-external-ref.json" => assert!( + ir.findings.iter() + .chain(ir.operations.iter().flat_map(|op| &op.findings)) + .any(|finding| finding.code == "external_reference_disabled" + && finding.location.pointer == "/paths/~1hostile/get/responses/200/content/application~1json/schema/$ref"), + "external ref must be denied at its source: {:?}", + ir.findings + ), + "hostile-duplicate-ids.json" => assert_eq!( + ir.operations.len(), + 2, + "duplicate IDs must not drop a source operation" + ), + "hostile-composition-conflict.json" => { + assert_eq!( + ir.operations.len(), + 1, + "conflicting allOf must keep the source operation explicit" + ); + let conflict = ir.operations[0] + .findings + .iter() + .find(|finding| finding.code == "all_of_conflict") + .unwrap_or_else(|| { + panic!( + "allOf conflict must be named: {:?}", + ir.operations[0].findings + ) + }); + assert_eq!( + conflict.severity, + crank_import::rest::ImportFindingSeverity::Error, + "allOf conflict must block the candidate: {:?}", + conflict + ); + assert_eq!( + conflict.operation_key.as_deref(), + Some("GET /hostile"), + "conflict must belong to the affected candidate: {:?}", + conflict + ); + assert_eq!( + conflict.location.pointer, + "/paths/~1hostile/get/responses/200/content/application~1json/schema/allOf", + "conflict must point at the conflicting allOf: {:?}", + conflict + ); + assert!( + ir.coverage + .iter() + .any(|entry| { + entry.location + .pointer + == "/paths/~1hostile/get/responses/200/content/application~1json/schema/allOf" + }) + && ir + .operations[0] + .findings + .iter() + .any(|finding| finding.code == "all_of_conflict" + && finding.location.pointer + == "/paths/~1hostile/get/responses/200/content/application~1json/schema/allOf"), + "conflicting allOf must stay in the source ledger bound to the explicit finding: {:?}", + ir.coverage + ); + let response = ir.operations[0] + .response_variants + .iter() + .find(|variant| variant.status_code == 200) + .expect("200 response is retained"); + assert!( + matches!( + &response.schema, + Some(schema) if matches!( + &schema.kind, + crank_import::rest::NormalizedSchemaKind::Composition { + operator, variants + } if operator == "allOf" && variants.len() == 2 + ) + ), + "conflict branches must be preserved verbatim: {:?}", + response.schema + ); + assert_eq!( + ir.findings + .iter() + .map(|f| (f.code.clone(), f.severity.clone())) + .collect::>(), + vec![( + "missing_servers".to_owned(), + crank_import::rest::ImportFindingSeverity::Warning + )], + "composition conflict fixture must keep the exact warning set: {:?}", + ir.findings + ); + assert_eq!( + document.expected_preview["operation_count"].as_u64(), + Some(1) + ); + assert_eq!( + document.expected_preview["blocker_findings"], + json!([{"code":"all_of_conflict","severity":"Error", + "operation_key":"GET /hostile", + "location":"/paths/~1hostile/get/responses/200/content/application~1json/schema/allOf"}]) + ); + assert_eq!( + document.expected_preview["composition"], + json!({"operator":"allOf","branch_count":2}) + ); + assert_eq!( + document.expected_preview["warning_findings"], + json!([{"code":"missing_servers","severity":"Warning","operation_key":null}]) + ); + } + "hostile-security-scope.json" => { + assert_eq!( + ir.operations.len(), + 2, + "security-scope fixture must keep both candidates explicit: {:?}", + ir.operations.iter().map(|op| &op.key).collect::>() + ); + let requirements = ir.findings.iter().filter(|finding| { + finding.code == "unsupported_security_requirements" + }).collect::>(); + assert!(requirements.is_empty(), "supported inherited basic requirement must remain selectable: {:?}", ir.findings); + let secured_operation = ir.operations.iter().find(|operation| operation.key == "GET /secure").unwrap(); + assert!(secured_operation.security_requirements.alternatives[0].supported); + assert_eq!(secured_operation.security_requirements.alternatives[0].source_location.as_ref().unwrap().pointer, "/security/0"); + let mut by_key = std::collections::BTreeMap::new(); + for group in &preview.groups { + for candidate in &group.operations { + by_key.insert(candidate.key.clone(), candidate); + } + } + let public = by_key.get("GET /public").expect("public candidate present"); + assert!( + public.findings.iter().all(|finding| finding.severity + != crank_import::rest::ImportFindingSeverity::Error), + "public operation with security: [] must stay actionable: {:?}", + public.findings + ); + assert!(by_key.get("GET /secure").expect("secured candidate present").findings.iter().all(|finding| { + finding.severity != crank_import::rest::ImportFindingSeverity::Error + }), "supported security must remain selectable before API binding: {:?}", by_key.get("GET /secure").unwrap().findings); + assert!( + preview + .findings + .iter() + .all(|finding| finding.operation_key.is_some()), + "no document-global blocker may be produced: {:?}", + preview.findings + ); + assert!( + ir.coverage + .iter() + .any(|entry| entry.location.pointer == "/security/0" + && entry.disposition == CoverageDisposition::Mapped), + "inherited requirement must stay mapped in the source ledger: {:?}", + ir.coverage + ); + let mut actual_operation_keys = preview + .groups + .iter() + .flat_map(|group| &group.operations) + .map(|op| op.key.clone()) + .collect::>(); + actual_operation_keys.sort(); + assert_eq!( + json!(actual_operation_keys), + document.expected_preview["operation_keys"], + "security-scope operation oracle for {}", + document.file + ); + let actual_blockers = requirements + .iter() + .map(|finding| { + json!({"code": finding.code.clone(), "severity": "Error", + "operation_key": finding.operation_key.clone(), + "location": finding.location.pointer.clone()}) + }) + .collect::>(); + assert_eq!( + actual_blockers, + document.expected_preview["blocker_findings"] + .as_array() + .unwrap() + .clone(), + "security-scope blocker oracle for {}: actual {actual_blockers:?}", + document.file + ); + let actual_warnings = ir + .findings + .iter() + .filter(|finding| { + finding.severity + == crank_import::rest::ImportFindingSeverity::Warning + }) + .map(|finding| { + json!({"code": finding.code.clone(), "severity": "Warning", + "operation_key": finding.operation_key.clone()}) + }) + .collect::>(); + assert_eq!( + actual_warnings, + document.expected_preview["warning_findings"] + .as_array() + .unwrap() + .clone(), + "security-scope warning oracle for {}: actual {actual_warnings:?}", + document.file + ); + } + _ => unreachable!("hostile fixture must have a dedicated expectation"), + } + } + } + } + // Exact file set: manifest rows are unique and every corpus data file is + // accounted exactly once, so duplicates or orphan fixtures cannot inflate + // or hide coverage. + let mut ids = std::collections::BTreeSet::new(); + let mut files = std::collections::BTreeSet::new(); + for document in &manifest.documents { + assert!( + ids.insert(document.id.clone()), + "duplicate manifest document id {}", + document.id + ); + assert!( + files.insert(document.file.clone()), + "duplicate manifest file {}", + document.file + ); + } + let expected_files = files + .into_iter() + .chain(std::iter::once("manifest.json".to_owned())) + .chain(std::iter::once("matrix.rs".to_owned())) + .collect::>(); + let actual_files = fs::read_dir(&root) + .expect("corpus directory exists") + .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) + .collect::>(); + assert_eq!( + actual_files, expected_files, + "corpus directory must equal the manifest file set exactly" + ); + assert_eq!(representative_count, 50); + assert!( + versions + .iter() + .any(|version| version.starts_with("OpenAPI 3.0")) + && versions + .iter() + .any(|version| version.starts_with("OpenAPI 3.1")) + && versions.contains("Swagger 2.0") + ); +} + +#[test] +fn ac_3_evidence_reports_actionable_denominator_exclusions_and_zero_silent_loss() { + let manifest = manifest(); + let mut denominator = 0_u64; + let mut actionable_count = 0_u64; + let mut operation_count = 0_u64; + let mut source_construct_count = 0_u64; + let mut covered_source_construct_count = 0_u64; + let mut semantic_coverage_count = 0_u64; + let mut silent_loss = Vec::new(); + let mut exclusions = Vec::new(); + let mut finding_count = 0_u64; + for document in &manifest.documents { + if document.classification != "representative" { + continue; + } + let source_bytes = fs::read(tests_root().join("corpus").join(&document.file)).unwrap(); + let source: Value = + serde_json::from_slice(&source_bytes).expect("representative corpus source is JSON"); + let mut raw_ledger = std::collections::BTreeMap::new(); + source_ledger(&source, "", &mut raw_ledger); + let source_text = std::str::from_utf8(&source_bytes).unwrap(); + let ir = normalize(source_text).unwrap(); + validate_normalized_ir(&ir).unwrap(); + let mut normalized_ledger = std::collections::BTreeMap::new(); + let _ = canonical_source_ledger(&ir.source_tree, &mut normalized_ledger); + assert_eq!( + normalized_ledger, raw_ledger, + "normalizer source-tree ledger differs from raw document for {}", + document.file + ); + source_construct_count += raw_ledger.len() as u64; + let coverage_by_id = ir + .coverage + .iter() + .map(|entry| (entry.construct_id.as_str(), entry)) + .collect::>(); + for pointer in raw_ledger.keys() { + let source_id = format!("source:{pointer}"); + match coverage_by_id.get(source_id.as_str()) { + Some(entry) + if entry.location.pointer == *pointer + && entry.disposition == CoverageDisposition::Mapped => + { + covered_source_construct_count += 1; + } + _ => silent_loss.push(json!({"document":document.id,"source_pointer":pointer,"coverage_id":source_id})), + } + } + let findings = ir + .findings + .iter() + .chain( + ir.operations + .iter() + .flat_map(|operation| &operation.findings), + ) + .collect::>(); + for entry in ir + .coverage + .iter() + .filter(|entry| !entry.construct_id.starts_with("source:")) + { + semantic_coverage_count += 1; + assert!( + raw_ledger.contains_key(&entry.location.pointer), + "coverage {} points outside raw source tree for {}", + entry.construct_id, + document.file + ); + match entry.disposition { + CoverageDisposition::Mapped => {} + CoverageDisposition::Finding => assert!( + findings + .iter() + .any(|finding| finding.construct_id == entry.construct_id + && finding.location == entry.location), + "finding coverage {} has no matching finding in {}", + entry.construct_id, + document.file + ), + } + } + for operation in &ir.operations { + denominator += 1; + operation_count += 1; + if operation + .findings + .iter() + .all(|f| f.severity != crank_import::rest::ImportFindingSeverity::Error) + { + actionable_count += 1; + } else { + exclusions.push(json!({"document":document.id,"operation":operation.key,"reason":"blocking_finding","findings":operation.findings.iter().map(|f| &f.code).collect::>()})); + } + } + finding_count += ir.findings.len() as u64; + } + assert!(denominator > 0); + assert!( + actionable_count * 100 >= denominator * 95, + "actionable {actionable_count}/{denominator}; exclusions={exclusions:?}" + ); + let silent_loss_count = silent_loss.len() as u64; + assert_eq!( + source_construct_count, covered_source_construct_count, + "independently enumerated raw source constructs lack coverage: {silent_loss:?}" + ); + let mut reversed_counts = (0_u64, 0_u64, 0_u64); + for document in manifest + .documents + .iter() + .rev() + .filter(|document| document.classification == "representative") + { + let source = fs::read_to_string(tests_root().join("corpus").join(&document.file)).unwrap(); + let ir = normalize(&source).unwrap(); + reversed_counts.0 += ir.operations.len() as u64; + reversed_counts.1 += ir + .operations + .iter() + .filter(|operation| { + operation.findings.iter().all(|finding| { + finding.severity != crank_import::rest::ImportFindingSeverity::Error + }) + }) + .count() as u64; + reversed_counts.2 += ir.findings.len() as u64; + } + assert_eq!( + reversed_counts, + (operation_count, actionable_count, finding_count), + "aggregate evidence must not depend on manifest order" + ); + let manifest_bytes = fs::read(tests_root().join("corpus/manifest.json")).unwrap(); + let manifest_digest = hex(&manifest_bytes); + let matrix_bytes = fs::read(tests_root().join("support-matrix.yaml")).unwrap(); + let matrix_digest = hex(&matrix_bytes); + let matrix_value: serde_yaml::Value = serde_yaml::from_slice(&matrix_bytes).unwrap(); + let matrix_row_count = matrix_value["constructs"].as_sequence().unwrap().len(); + let evidence_identity = hex(format!( + "{}\n{}\n{}\n{}", + manifest.corpus_version, manifest.matrix_version, manifest_digest, matrix_digest + )); + let mut evidence = json!({ + "evidence_version":"1.0.0", "corpus_version":manifest.corpus_version, "matrix_version":manifest.matrix_version, + "evidence_identity":evidence_identity, + "content_digest_algorithm":"sha256", + "content_digest_definition":"SHA-256 of compact key-sorted JSON before the content_digest field is added.", + "manifest_digest":manifest_digest, + "matrix_digest":matrix_digest, + "denominator":denominator, "actionable_count":actionable_count, "exclusions":exclusions, + "raw_counts":{"documents":manifest.documents.len(),"representative_operations":operation_count,"findings":finding_count,"source_constructs":source_construct_count,"covered_source_constructs":covered_source_construct_count,"semantic_coverage_entries":semantic_coverage_count}, + "matrix_contract":{"construct_rows":matrix_row_count,"semantic_probes":"AC-1 runs a generated normalizer probe for every matrix row, including every 2xx status in all three formats."}, + "uncovered_source_constructs":silent_loss, + "silent_loss_count":silent_loss_count, + "method":"real normalize_verified_document + preview_from_ir; every NormalizedIr passed validate_normalized_ir" + }); + let content_digest = hex(canonical_json(&evidence)); + evidence["content_digest"] = json!(content_digest); + let artifact = Path::new(env!("CARGO_MANIFEST_DIR")).join(format!( + "target/openapi-corpus/evidence-{evidence_identity}.json" + )); + let target_dir = artifact.parent().unwrap(); + fs::create_dir_all(target_dir).unwrap(); + let bytes = serde_json::to_vec_pretty(&evidence).unwrap(); + // Each process needs its own temp file: concurrent corpus runs must not + // truncate each other's pending evidence before atomic publication. + let mut nonce = 0u32; + let (publish, mut file) = loop { + let path = target_dir.join(format!( + "evidence.json.tmp-{}-{}-{nonce}", + &evidence_identity[..16], + std::process::id() + )); + match fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&path) + { + Ok(file) => break (path, file), + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists && nonce < 1024 => { + nonce += 1; + } + Err(error) => panic!( + "cannot create evidence temp file {}: {error}", + path.display() + ), + } + }; + { + use std::io::Write; + file.write_all(&bytes).unwrap(); + file.sync_all().unwrap(); + } + // hard_link fails atomically when the final identity already exists. + // Rename would silently overwrite after an exists-check race, while + // create_new on the final path would expose partial bytes after a crash. + let published = match fs::hard_link(&publish, &artifact) { + Ok(()) => true, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => false, + Err(error) => panic!( + "cannot publish immutable evidence artifact {}: {error}", + artifact.display() + ), + }; + fs::remove_file(&publish).ok(); + if !published { + let existing = fs::read(&artifact).unwrap(); + assert_eq!( + existing, + bytes, + "immutable evidence identity {evidence_identity} already has conflicting contents at {}", + artifact.display() + ); + } + if let Ok(directory) = fs::File::open(target_dir) { + let _ = directory.sync_all(); + } + let reread: Value = serde_json::from_slice(&fs::read(artifact).unwrap()).unwrap(); + assert_eq!(reread, evidence); + assert_eq!(reread["silent_loss_count"], 0); + assert_eq!(source_construct_count, covered_source_construct_count); + assert_eq!(reread["denominator"], denominator); + assert_eq!(reread["actionable_count"], actionable_count); +} diff --git a/crates/crank-import/tests/corpus/hostile-aliases.yaml b/crates/crank-import/tests/corpus/hostile-aliases.yaml new file mode 100644 index 0000000..ef279bf --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-aliases.yaml @@ -0,0 +1,5 @@ +openapi: 3.1.0 +info: {title: Aliases, version: 1} +paths: {} +x-anchor: &anchor {type: string} +x-aliases: [*anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor, *anchor] diff --git a/crates/crank-import/tests/corpus/hostile-composition-conflict.json b/crates/crank-import/tests/corpus/hostile-composition-conflict.json new file mode 100644 index 0000000..5b68531 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-composition-conflict.json @@ -0,0 +1,43 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Hostile generated", + "version": "1" + }, + "paths": { + "/hostile": { + "get": { + "operationId": "hostileOperation", + "responses": { + "200": { + "description": "ok", + "content": { + "application/json": { + "schema": { + "allOf": [ + { + "type": "object", + "properties": { + "id": { + "type": "string" + } + } + }, + { + "type": "object", + "properties": { + "id": { + "type": "integer" + } + } + } + ] + } + } + } + } + } + } + } + } +} diff --git a/crates/crank-import/tests/corpus/hostile-cycle.json b/crates/crank-import/tests/corpus/hostile-cycle.json new file mode 100644 index 0000000..4f0f07d --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-cycle.json @@ -0,0 +1,38 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Hostile generated", + "version": "1" + }, + "paths": { + "/hostile": { + "get": { + "operationId": "hostileOperation", + "responses": { + "200": { + "description": "ok", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Node" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Node": { + "type": "object", + "properties": { + "next": { + "$ref": "#/components/schemas/Node" + } + } + } + } + } +} diff --git a/crates/crank-import/tests/corpus/hostile-deep.json b/crates/crank-import/tests/corpus/hostile-deep.json new file mode 100644 index 0000000..b8d0139 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-deep.json @@ -0,0 +1 @@ +{"openapi":"3.1.0","info":{"title":"Hostile generated","version":"1"},"paths":{"/hostile":{"get":{"operationId":"hostileOperation","responses":{"200":{"description":"ok","content":{"application/json":{"schema":{"type":"object"}}}}}}}},"components":{"schemas":{"Deep":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"array","items":{"type":"string"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} diff --git a/crates/crank-import/tests/corpus/hostile-duplicate-ids.json b/crates/crank-import/tests/corpus/hostile-duplicate-ids.json new file mode 100644 index 0000000..321ad3f --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-duplicate-ids.json @@ -0,0 +1,36 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Hostile generated", + "version": "1" + }, + "paths": { + "/hostile": { + "get": { + "operationId": "hostileOperation", + "responses": { + "200": { + "description": "ok", + "content": { + "application/json": { + "schema": { + "$ref": "https://external.example.test/schema.json" + } + } + } + } + } + } + }, + "/other": { + "post": { + "operationId": "hostileOperation", + "responses": { + "200": { + "description": "ok" + } + } + } + } + } +} diff --git a/crates/crank-import/tests/corpus/hostile-empty.json b/crates/crank-import/tests/corpus/hostile-empty.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-empty.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/crates/crank-import/tests/corpus/hostile-external-ref.json b/crates/crank-import/tests/corpus/hostile-external-ref.json new file mode 100644 index 0000000..975ded4 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-external-ref.json @@ -0,0 +1,26 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Hostile generated", + "version": "1" + }, + "paths": { + "/hostile": { + "get": { + "operationId": "hostileOperation", + "responses": { + "200": { + "description": "ok", + "content": { + "application/json": { + "schema": { + "$ref": "https://external.example.test/schema.json" + } + } + } + } + } + } + } + } +} diff --git a/crates/crank-import/tests/corpus/hostile-malformed.json b/crates/crank-import/tests/corpus/hostile-malformed.json new file mode 100644 index 0000000..b4a79a2 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-malformed.json @@ -0,0 +1 @@ +{"openapi": diff --git a/crates/crank-import/tests/corpus/hostile-oversized.json b/crates/crank-import/tests/corpus/hostile-oversized.json new file mode 100644 index 0000000..f144b3a --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-oversized.json @@ -0,0 +1,12 @@ +{ + "openapi": "3.1.0", + "info": {"title": "Hostile generated", "version": "1"}, + "paths": {"/hostile": {"get": {"operationId": "hostileOperation", "responses": {"200": {"description": "ok", "content": {"application/json": {"schema": {"type": "string"}}}}}}}}, + "x-padding-0": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +} diff --git a/crates/crank-import/tests/corpus/hostile-secret.json b/crates/crank-import/tests/corpus/hostile-secret.json new file mode 100644 index 0000000..a9aac79 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-secret.json @@ -0,0 +1,32 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Secret hostile", + "version": "1" + }, + "paths": { + "/secret": { + "get": { + "operationId": "secretOperation", + "responses": { + "200": { + "description": "ok", + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + } + }, + "x-example": "sk_test_corpus_secret_canary" + } + } + }, + "servers": [ + { + "url": "https://api.example.test" + } + ] +} diff --git a/crates/crank-import/tests/corpus/hostile-security-scope.json b/crates/crank-import/tests/corpus/hostile-security-scope.json new file mode 100644 index 0000000..34acb78 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-security-scope.json @@ -0,0 +1,48 @@ +{ + "openapi": "3.0.3", + "info": { + "title": "Hostile security scope", + "version": "1" + }, + "servers": [ + { + "url": "https://corpus.example.test" + } + ], + "security": [ + { + "corpusProbe": [] + } + ], + "components": { + "securitySchemes": { + "corpusProbe": { + "type": "http", + "scheme": "basic" + } + } + }, + "paths": { + "/public": { + "get": { + "operationId": "publicProbe", + "security": [], + "responses": { + "200": { + "description": "ok" + } + } + } + }, + "/secure": { + "get": { + "operationId": "secureProbe", + "responses": { + "200": { + "description": "ok" + } + } + } + } + } +} diff --git a/crates/crank-import/tests/corpus/hostile-trace.json b/crates/crank-import/tests/corpus/hostile-trace.json new file mode 100644 index 0000000..a5391fe --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-trace.json @@ -0,0 +1,18 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "TRACE hostile", + "version": "1" + }, + "paths": { + "/trace": { + "trace": { + "responses": { + "200": { + "description": "unsupported" + } + } + } + } + } +} diff --git a/crates/crank-import/tests/corpus/hostile-xml.json b/crates/crank-import/tests/corpus/hostile-xml.json new file mode 100644 index 0000000..e8078d8 --- /dev/null +++ b/crates/crank-import/tests/corpus/hostile-xml.json @@ -0,0 +1,31 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "XML hostile", + "version": "1" + }, + "paths": { + "/xml": { + "get": { + "operationId": "xmlOperation", + "responses": { + "200": { + "description": "ok", + "content": { + "application/xml": { + "schema": { + "type": "string" + } + } + } + } + } + } + } + }, + "servers": [ + { + "url": "https://api.example.test" + } + ] +} diff --git a/crates/crank-import/tests/corpus/manifest.json b/crates/crank-import/tests/corpus/manifest.json new file mode 100644 index 0000000..03c5bc5 --- /dev/null +++ b/crates/crank-import/tests/corpus/manifest.json @@ -0,0 +1,14413 @@ +{ + "corpus_version": "1.0.4", + "matrix_version": "1.0.3", + "digest_algorithm": "sha256", + "canonical_digest_definition": "SHA-256 of compact JSON with lexicographically ordered keys; null for malformed JSON", + "documents": [ + { + "id": "rep-00", + "file": "representative-00.json", + "sha256": "a8e84701e08750fef5adbcc6ae0e8341b2cdb3b8fc6100f81d4bb5a38aa222c0", + "canonical_digest": "8b3b7206da3930cd4b3447002a3e7580dc5a57283ebd1568d985251c82859a5b", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/0", + "operation_id": "corpusOperation00", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 00", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 00", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p00", + "target": "$.request.query.p00" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p00": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation00", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/0", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 00", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 00" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/0", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/0", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/0", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/0", + "method": "GET", + "operation_id": "corpusOperation00", + "output_fields": 1, + "path": "/items/0", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 00", + "suggested_name": "corpus_operation00" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "a28f86380d510df8d46018cd3767d445c7450bea42563702de1b9c6ecaac0f53" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "df4c7068366ad3bcecb756f3553128eec9e200557d2500df21c6fccc7bcef7fe", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 00", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 00", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p00", + "target": "$.request.query.p00" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p00": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation00", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/0", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 00", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 00" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/0", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/0", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/0", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/0", + "method": "GET", + "operation_id": "corpusOperation00", + "output_fields": 1, + "path": "/items/0", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 00", + "suggested_name": "corpus_operation00" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 00", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-01", + "file": "representative-01.json", + "sha256": "0f80248628bef2e17896fa44f6e7c12ddcd979f94e88911117083b92b0b772ad", + "canonical_digest": "6f0c11966664ebe46373a3fbf175e16899482d3ab5c0ef2a4db3a1e0ea2c1809", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/{p01}", + "operation_id": "corpusOperation01", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 01", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 01", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p01", + "target": "$.request.path.p01" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p01": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation01", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/{p01}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 01", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 01" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/{p01}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/{p01}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/{p01}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/{p01}", + "method": "POST", + "operation_id": "corpusOperation01", + "output_fields": 1, + "path": "/items/{p01}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 01", + "suggested_name": "corpus_operation01" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "5988988cb6e412ab6852e253f559358c4d064ee65f60254ca42fe74025fa3c03" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "07e209a48e95e864137be08e89f28228c66f04ade44fbb1ae33d1e27cced170f", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 01", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 01", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p01", + "target": "$.request.path.p01" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p01": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation01", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/{p01}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 01", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 01" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/{p01}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/{p01}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/{p01}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/{p01}", + "method": "POST", + "operation_id": "corpusOperation01", + "output_fields": 1, + "path": "/items/{p01}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 01", + "suggested_name": "corpus_operation01" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 01", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-02", + "file": "representative-02.json", + "sha256": "3afda2ee298e08f5279979a3df3181d5606f3b15201a26fe6375855c3a5c5b7b", + "canonical_digest": "be75a434d7e0db1b5d58a6d671e4437f47344a019b1c697984acdd952aae3c8f", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/2", + "operation_id": "corpusOperation02", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 02", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 02", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p02", + "target": "$.request.headers.p02" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p02": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation02", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/2", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 02", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 02" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/2", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/2", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/2", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PUT /items/2", + "method": "PUT", + "operation_id": "corpusOperation02", + "output_fields": 1, + "path": "/items/2", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 02", + "suggested_name": "corpus_operation02" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "ab005a5b770e045b69e106df6723aa09c2fc1ad7bba7bab29bf77aa0f2519f5b" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "421c76a662b054f31be9e2749cb6e27a89e8648b9493bc171c3b70b934b3413b", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 02", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 02", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p02", + "target": "$.request.headers.p02" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p02": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation02", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/2", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 02", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 02" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/2", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/2", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/2", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PUT /items/2", + "method": "PUT", + "operation_id": "corpusOperation02", + "output_fields": 1, + "path": "/items/2", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 02", + "suggested_name": "corpus_operation02" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 02", + "version": "2.0" + } + } + } + }, + { + "id": "rep-03", + "file": "representative-03.json", + "sha256": "30729f295f999512173010ae0572bc8516133120cf40e3830633f717ee356985", + "canonical_digest": "d3fbeb735d0cf4c4b5a748a34ea0def45839f4e28cb214f218277f322a49340b", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/3", + "operation_id": "corpusOperation03", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 03", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 03", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p03", + "target": "$.request.cookies.p03" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p03": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation03", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/3", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 03", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 03" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/3", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/3", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/3", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/3", + "method": "PATCH", + "operation_id": "corpusOperation03", + "output_fields": 1, + "path": "/items/3", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 03", + "suggested_name": "corpus_operation03" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "9cf76cd4e95147f53f47df5dfedd1ecf355657f923a9b99e6c67412057864497" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "4f457e379c47a5a729beec4ab176644396da1da3c818c1a31ff133e28e3ca40b", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 03", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 03", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p03", + "target": "$.request.cookies.p03" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p03": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation03", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/3", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 03", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 03" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/3", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/3", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/3", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/3", + "method": "PATCH", + "operation_id": "corpusOperation03", + "output_fields": 1, + "path": "/items/3", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 03", + "suggested_name": "corpus_operation03" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 03", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-04", + "file": "representative-04.json", + "sha256": "b8768a80266f80ddbd7a0364cca2d6c229a2df084597ebf650dc43d36a7464d8", + "canonical_digest": "565f07ac02fc6a6ba63f0356cba1e4c50bd8df12f203f23416762eede585dde6", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/4", + "operation_id": "corpusOperation04", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 04", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 04", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p04", + "target": "$.request.query.p04" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p04": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation04", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/4", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 04", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 04" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/4", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/4", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/4", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/4", + "method": "DELETE", + "operation_id": "corpusOperation04", + "output_fields": 1, + "path": "/items/4", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 04", + "suggested_name": "corpus_operation04" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "7218c206fc70f05c45577ad776d0d3e0b921b5aa55c6bd9289584936824be8a6" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "74e51e9f9d45beca41ed66e2551500b9cb5f79d735fa5e75bf581ad6e582b6a6", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 04", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 04", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p04", + "target": "$.request.query.p04" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p04": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation04", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/4", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 04", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 04" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/4", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/4", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/4", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/4", + "method": "DELETE", + "operation_id": "corpusOperation04", + "output_fields": 1, + "path": "/items/4", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 04", + "suggested_name": "corpus_operation04" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 04", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-05", + "file": "representative-05.json", + "sha256": "59ed9af86a5af47a0b17580e73755cb2532a7227d183554357ad6dbf93f4b21f", + "canonical_digest": "201bfee79e759ab0147f8e25b08302f2bc407a6d3c21a498a1dbab3321e38075", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/{p05}", + "operation_id": "corpusOperation05", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 05", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 05", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p05", + "target": "$.request.path.p05" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p05": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation05", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/{p05}", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 05", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 05" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/{p05}", + "method": "HEAD", + "operation_id": "corpusOperation05", + "output_fields": 0, + "path": "/items/{p05}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 05", + "suggested_name": "corpus_operation05" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "56d427fad224c749ff27ff7ef34e921087ec35e72220bea16bc21a94e87c2335" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "fc92474b58c11b7699420c51cc3fb4bd23e9cb49b91b8bc720039adb25d43232", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 05", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 05", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p05", + "target": "$.request.path.p05" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p05": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation05", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/{p05}", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 05", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 05" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/{p05}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/{p05}", + "method": "HEAD", + "operation_id": "corpusOperation05", + "output_fields": 0, + "path": "/items/{p05}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 05", + "suggested_name": "corpus_operation05" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 05", + "version": "2.0" + } + } + } + }, + { + "id": "rep-06", + "file": "representative-06.json", + "sha256": "33e51175dddcff1db2e0181d1ce668e5b15b911db1fbcdfe1f511d76182c79c1", + "canonical_digest": "f113ef8f17ed25ab368fcade4d5407d7de7af9ebe543b4c4ec9f49928c615d09", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/6", + "operation_id": "corpusOperation06", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 06", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 06", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p06", + "target": "$.request.headers.p06" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p06": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation06", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/6", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 06", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 06" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/6", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/6", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/6", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/6", + "method": "OPTIONS", + "operation_id": "corpusOperation06", + "output_fields": 1, + "path": "/items/6", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 06", + "suggested_name": "corpus_operation06" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "d1648614cdcbbec99c607dc54c678fafb15f15700daaa599c8cc58caa8128fc0" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "92bc8783bb53e7adb550278b2e1a0855534dbe105c80a0f6c8972dc2d62d1d3f", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 06", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 06", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p06", + "target": "$.request.headers.p06" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p06": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation06", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/6", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 06", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 06" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/6", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/6", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/6", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/6", + "method": "OPTIONS", + "operation_id": "corpusOperation06", + "output_fields": 1, + "path": "/items/6", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 06", + "suggested_name": "corpus_operation06" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 06", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-07", + "file": "representative-07.json", + "sha256": "69748e81d96e393314f00123129c6ed60102577affd82ac848e318383f0839e1", + "canonical_digest": "49c4710d9f183c408ad519e87ba3f1a815bc4bded067e033292d4da5177fe3cc", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/7", + "operation_id": "corpusOperation07", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 07", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 07", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p07", + "target": "$.request.cookies.p07" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p07": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation07", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/7", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 07", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 07" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/7", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/7", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/7", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/7", + "method": "GET", + "operation_id": "corpusOperation07", + "output_fields": 1, + "path": "/items/7", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 07", + "suggested_name": "corpus_operation07" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "aa444c9def0b6082b3176a5f0d038dfad0f08ea03dfb2f2f40617f84ba138d38" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "b2caa897c0b8f6e13d3305909baf57bd5f855f5c3cee0c850d7f99ac60334bbb", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 07", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 07", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p07", + "target": "$.request.cookies.p07" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p07": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation07", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/7", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 07", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 07" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/7", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/7", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/7", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/7", + "method": "GET", + "operation_id": "corpusOperation07", + "output_fields": 1, + "path": "/items/7", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 07", + "suggested_name": "corpus_operation07" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 07", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-08", + "file": "representative-08.json", + "sha256": "11bd5d0d7548c7253cff2ff273d0fae61b6a8940f0229742c7cf44baa1909a69", + "canonical_digest": "531402e8e128476dbde20b8b33a827ebd9eb718d6a1aaefe726970f9df5088a9", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/8", + "operation_id": "corpusOperation08", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 08", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 08", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p08", + "target": "$.request.query.p08" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p08": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation08", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/8", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 08", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 08" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/8", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/8", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/8", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "POST /items/8", + "method": "POST", + "operation_id": "corpusOperation08", + "output_fields": 1, + "path": "/items/8", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 08", + "suggested_name": "corpus_operation08" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "1fdc42135574b4441687c29eda605e3b0968ff5e81d57036cc9882d76bf2863a" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "31c5b656c35c67ddac77dbd50d98ec4df589b750cc349df7c95b5095affe6d17", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 08", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 08", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p08", + "target": "$.request.query.p08" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p08": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation08", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/8", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 08", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 08" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/8", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/8", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/8", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "POST /items/8", + "method": "POST", + "operation_id": "corpusOperation08", + "output_fields": 1, + "path": "/items/8", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 08", + "suggested_name": "corpus_operation08" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 08", + "version": "2.0" + } + } + } + }, + { + "id": "rep-09", + "file": "representative-09.json", + "sha256": "948fc7a26da9b5b208439fe63fffff00dfc1c503fd788a81b7b00d7677e9a915", + "canonical_digest": "104e4324e17551fd637d72387b515c8b4394ed0acf4efb931fbcfca1ad33c001", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/{p09}", + "operation_id": "corpusOperation09", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 09", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 09", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p09", + "target": "$.request.path.p09" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p09": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation09", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/{p09}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 09", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 09" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/{p09}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/{p09}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/{p09}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/{p09}", + "method": "PUT", + "operation_id": "corpusOperation09", + "output_fields": 1, + "path": "/items/{p09}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 09", + "suggested_name": "corpus_operation09" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "e2c1a893420f728d047f9d45c79422508c4f1145c446000589286d0e48a10c94" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "6e03a5d02c10c559f99d92ab33e18e03fe5fd98db11b28392c9acf5d84d735a6", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 09", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 09", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p09", + "target": "$.request.path.p09" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p09": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation09", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/{p09}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 09", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 09" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/{p09}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/{p09}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/{p09}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/{p09}", + "method": "PUT", + "operation_id": "corpusOperation09", + "output_fields": 1, + "path": "/items/{p09}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 09", + "suggested_name": "corpus_operation09" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 09", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-10", + "file": "representative-10.json", + "sha256": "ca78eb0bc6f6998d3aabc09bc8fb54c850fe7f1721b425163da99d71e2fe5761", + "canonical_digest": "fcd89d029f22285de06d7ad0ac1659cb034ee60ce0c457a538bdd43be4278928", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/10", + "operation_id": "corpusOperation10", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 10", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 10", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p10", + "target": "$.request.headers.p10" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p10": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation10", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/10", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 10", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 10" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/10", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/10", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/10", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/10", + "method": "PATCH", + "operation_id": "corpusOperation10", + "output_fields": 1, + "path": "/items/10", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 10", + "suggested_name": "corpus_operation10" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "55afdf9db0d37a4869b71753e8d8f616c21eb2357658b36955a4270b3b3624db" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "7cd9de4a739474521ad0fc4cf5ed121a352d8f5a38cad50833ecc9bb2e176009", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 10", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 10", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p10", + "target": "$.request.headers.p10" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p10": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation10", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/10", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 10", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 10" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/10", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/10", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/10", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/10", + "method": "PATCH", + "operation_id": "corpusOperation10", + "output_fields": 1, + "path": "/items/10", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 10", + "suggested_name": "corpus_operation10" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 10", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-11", + "file": "representative-11.json", + "sha256": "980eee3cf0c769b6cf4982a61cb886079db00ce09ca11558c4583b54d19bda01", + "canonical_digest": "6c2bf27c95b93401dff6e5e0cd9a0c329195772b9b43c0f71f5343511d339c60", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/11", + "operation_id": "corpusOperation11", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 11", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 11", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p11", + "target": "$.request.headers.p11" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p11": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation11", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/11", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 11", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 11" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/11", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/11", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/11", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/11", + "method": "DELETE", + "operation_id": "corpusOperation11", + "output_fields": 1, + "path": "/items/11", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 11", + "suggested_name": "corpus_operation11" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "00fb4c9d745090be6cac2912dcdbe7bcb68a2863712fad5a7158efae3106924c" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "da05134ab7b9d72b2ea4ee20e520b4ed426573c1d78d3eb88551a264d3b1004d", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 11", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 11", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p11", + "target": "$.request.headers.p11" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p11": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation11", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/11", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 11", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 11" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/11", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/11", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/11", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/11", + "method": "DELETE", + "operation_id": "corpusOperation11", + "output_fields": 1, + "path": "/items/11", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 11", + "suggested_name": "corpus_operation11" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 11", + "version": "2.0" + } + } + } + }, + { + "id": "rep-12", + "file": "representative-12.json", + "sha256": "16254d5a72364229703cdaf5b61b6e9168c3b4ce43dc16662e1ef6aa91285414", + "canonical_digest": "231275deaf1f6bda870e25926713be784c4b2e6df8decf019079894409410b84", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/12", + "operation_id": "corpusOperation12", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 12", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 12", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p12", + "target": "$.request.query.p12" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p12": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation12", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/12", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 12", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 12" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/12", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/12", + "method": "HEAD", + "operation_id": "corpusOperation12", + "output_fields": 0, + "path": "/items/12", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 12", + "suggested_name": "corpus_operation12" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "2ac514cb9fc89bf6a45b6296db5ec26c8775209f72b57bcc3cd83b015a49eba4" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "95daac5f79ee78cfeb2aadbcfc7ae83a87fb6299e7d98711d841a18fff3da381", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 12", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 12", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p12", + "target": "$.request.query.p12" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p12": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation12", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/12", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 12", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 12" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/12", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/12", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/12", + "method": "HEAD", + "operation_id": "corpusOperation12", + "output_fields": 0, + "path": "/items/12", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 12", + "suggested_name": "corpus_operation12" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 12", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-13", + "file": "representative-13.json", + "sha256": "a5e161d898394e99272965eb9b30794dbed2293d4996f11a096a2702c507ef6c", + "canonical_digest": "50a544a935677c9f08406594e0c1f304887f6ef9cb4c4ae97c158eeadd8dc4dc", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/{p13}", + "operation_id": "corpusOperation13", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 13", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 13", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p13", + "target": "$.request.path.p13" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p13": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation13", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/{p13}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 13", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 13" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/{p13}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/{p13}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/{p13}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/{p13}", + "method": "OPTIONS", + "operation_id": "corpusOperation13", + "output_fields": 1, + "path": "/items/{p13}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 13", + "suggested_name": "corpus_operation13" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "896a31e5034b25725c557d5303f0603d34b151ca8e0a3bd003ab73dd9039d7f1" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "228e5a47701346c19986be6f6d4cc9c72c802e6c9bc61c296d46aa1fb6bf5d78", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 13", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 13", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p13", + "target": "$.request.path.p13" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p13": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation13", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/{p13}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 13", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 13" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/{p13}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/{p13}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/{p13}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/{p13}", + "method": "OPTIONS", + "operation_id": "corpusOperation13", + "output_fields": 1, + "path": "/items/{p13}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 13", + "suggested_name": "corpus_operation13" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 13", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-14", + "file": "representative-14.json", + "sha256": "0356a40a8f459128a06ac482e0c0c8d52460b4cb919590d9a25e5d9a109564d1", + "canonical_digest": "ff9a0a9705967ed011811ac7a940a15bb37fe96cb5219c4547412663bb3e27ae", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/14", + "operation_id": "corpusOperation14", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 14", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 14", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p14", + "target": "$.request.headers.p14" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p14": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation14", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/14", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 14", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 14" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/14", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/14", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/14", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/14", + "method": "GET", + "operation_id": "corpusOperation14", + "output_fields": 1, + "path": "/items/14", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 14", + "suggested_name": "corpus_operation14" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "e51a6adfa2e9dae225e7a8a0e8af3dffb4e16875cf4152ab75d10d354ce0f3d6" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "0decad1890ca88c5de98b1f7ade621a4b479b7c99662cdd715fd7876f4c7bcf1", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 14", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 14", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p14", + "target": "$.request.headers.p14" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p14": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation14", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/14", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 14", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 14" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/14", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/14", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/14", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/14", + "method": "GET", + "operation_id": "corpusOperation14", + "output_fields": 1, + "path": "/items/14", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 14", + "suggested_name": "corpus_operation14" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 14", + "version": "2.0" + } + } + } + }, + { + "id": "rep-15", + "file": "representative-15.json", + "sha256": "b8fde33d4265c2449465edf3d2d6b816eb9143e2d32b13bd25c094dce0a6993e", + "canonical_digest": "b579168cbebe23326e35e40a2a4548fb317825851d9b476294109a4854489587", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/15", + "operation_id": "corpusOperation15", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 15", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 15", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p15", + "target": "$.request.cookies.p15" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p15": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation15", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/15", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 15", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 15" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/15", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/15", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/15", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/15", + "method": "POST", + "operation_id": "corpusOperation15", + "output_fields": 1, + "path": "/items/15", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 15", + "suggested_name": "corpus_operation15" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "fc45211e445a1be1af69caec56a404912e46bbc4b9b0f5438b9df0823d14808e" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "9399dc82788787382104e3193c35ec0638d9b513d2f346262a001f82ae59fb0b", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 15", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 15", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p15", + "target": "$.request.cookies.p15" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p15": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation15", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/15", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 15", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 15" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/15", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/15", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/15", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/15", + "method": "POST", + "operation_id": "corpusOperation15", + "output_fields": 1, + "path": "/items/15", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 15", + "suggested_name": "corpus_operation15" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 15", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-16", + "file": "representative-16.json", + "sha256": "9ec04bb01c04ca65c9bb314a09f340f4e4e3c531656c32429efc732a72b540b6", + "canonical_digest": "29ebb860b71eb95834a68d28c6fe7822ab7baf3f98fe53e99f10606a66deeee2", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/16", + "operation_id": "corpusOperation16", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 16", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 16", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p16", + "target": "$.request.query.p16" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p16": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation16", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/16", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 16", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 16" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/16", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/16", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/16", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/16", + "method": "PUT", + "operation_id": "corpusOperation16", + "output_fields": 1, + "path": "/items/16", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 16", + "suggested_name": "corpus_operation16" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "221b6b0a2113fae03334cdb49f557fa21ce605682fe622bf14e5033d738f492b" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "585b949c63d44c51428e3fc3e048e34b4186ec2c3c438e08d5f026517f755e06", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 16", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 16", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p16", + "target": "$.request.query.p16" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p16": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation16", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/16", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 16", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 16" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/16", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/16", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/16", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/16", + "method": "PUT", + "operation_id": "corpusOperation16", + "output_fields": 1, + "path": "/items/16", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 16", + "suggested_name": "corpus_operation16" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 16", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-17", + "file": "representative-17.json", + "sha256": "4e9c7c16873ca0d5bcf737c06b81eb31e4b643a4ca4493846cdbb5e385382d16", + "canonical_digest": "346ea375d8963acb1b03b33267c7e807e2a1ad55dc23cd15363f097e6778e44b", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/{p17}", + "operation_id": "corpusOperation17", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 17", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 17", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p17", + "target": "$.request.path.p17" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p17": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation17", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/{p17}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 17", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 17" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/{p17}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/{p17}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/{p17}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PATCH /items/{p17}", + "method": "PATCH", + "operation_id": "corpusOperation17", + "output_fields": 1, + "path": "/items/{p17}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 17", + "suggested_name": "corpus_operation17" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "980e6cad722b7b7c5a59c9c91071e11319ba3d89bef9d6d57886355b7112a1e4" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "822d40bcfdda3a67c4e5e23a1fb1f32ff815f706f3caf4f7b1ecbe00f907c055", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 17", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 17", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p17", + "target": "$.request.path.p17" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p17": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation17", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/{p17}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 17", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 17" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/{p17}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/{p17}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/{p17}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PATCH /items/{p17}", + "method": "PATCH", + "operation_id": "corpusOperation17", + "output_fields": 1, + "path": "/items/{p17}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 17", + "suggested_name": "corpus_operation17" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 17", + "version": "2.0" + } + } + } + }, + { + "id": "rep-18", + "file": "representative-18.json", + "sha256": "42cb88fef4bf85cb33b2a62a9e78e185857b5b881aaede01f29c071de161b22d", + "canonical_digest": "553546cf62e3521d51ea585a85ace95bd1450803ceb02b924f064d9be745a813", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/18", + "operation_id": "corpusOperation18", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 18", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 18", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p18", + "target": "$.request.headers.p18" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p18": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation18", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/18", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 18", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 18" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/18", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/18", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/18", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/18", + "method": "DELETE", + "operation_id": "corpusOperation18", + "output_fields": 1, + "path": "/items/18", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 18", + "suggested_name": "corpus_operation18" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "6388c4d905b7cd2be74a86db43165943913c21b2e25d435b7e758e7a5f5b2273" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "233384b489f99d9ec8df78def0fa29ef18c3a5a37047195d404d32c41a20cd7b", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 18", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 18", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p18", + "target": "$.request.headers.p18" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p18": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation18", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/18", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 18", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 18" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/18", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/18", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/18", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/18", + "method": "DELETE", + "operation_id": "corpusOperation18", + "output_fields": 1, + "path": "/items/18", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 18", + "suggested_name": "corpus_operation18" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 18", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-19", + "file": "representative-19.json", + "sha256": "779cb787f5ee44230625c3d95bb6c08a9477adb324f51beeb3120cfb80aee440", + "canonical_digest": "f9453f2f8978cf1eec54083dca3ac65ec6670b2d49f38669f5e8094d7cfbd2f7", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/19", + "operation_id": "corpusOperation19", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 19", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 19", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p19", + "target": "$.request.cookies.p19" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p19": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation19", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/19", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 19", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 19" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/19", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/19", + "method": "HEAD", + "operation_id": "corpusOperation19", + "output_fields": 0, + "path": "/items/19", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 19", + "suggested_name": "corpus_operation19" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "1a2dcadd88dba27b1cbacacdb6223a11e633ee4436205fe0d36285f7e6645300" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "2c321fa76e556169b068c5fdbdc6576da95f061fbcf8c8dc9a4128ec2664e5f3", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 19", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 19", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p19", + "target": "$.request.cookies.p19" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p19": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation19", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/19", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 19", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 19" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/19", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/19", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/19", + "method": "HEAD", + "operation_id": "corpusOperation19", + "output_fields": 0, + "path": "/items/19", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 19", + "suggested_name": "corpus_operation19" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 19", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-20", + "file": "representative-20.json", + "sha256": "e8f285c6b4a0b6e50c5dddb9b661f253d6433d0d87ee64677a69dbce7ce55e69", + "canonical_digest": "369f23fa8b4f1f7180f53f462f4137ada9f3065782d4affda7b6314b512734f3", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/20", + "operation_id": "corpusOperation20", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 20", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 20", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p20", + "target": "$.request.query.p20" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p20": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation20", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/20", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 20", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 20" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/20", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/20", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/20", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/20", + "method": "OPTIONS", + "operation_id": "corpusOperation20", + "output_fields": 1, + "path": "/items/20", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 20", + "suggested_name": "corpus_operation20" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "0d8a8374dbbfe81d9ddbc514ec9723f4e66ee304524b0aed31bcd1217e302682" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "625aafb27628a9901227c6d56911bf6eafe18b8fa82d1137ef67272b420c95d5", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 20", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 20", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p20", + "target": "$.request.query.p20" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p20": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation20", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/20", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 20", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 20" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/20", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/20", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/20", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/20", + "method": "OPTIONS", + "operation_id": "corpusOperation20", + "output_fields": 1, + "path": "/items/20", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 20", + "suggested_name": "corpus_operation20" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 20", + "version": "2.0" + } + } + } + }, + { + "id": "rep-21", + "file": "representative-21.json", + "sha256": "acfa0c990794d8767f7636101680fcdb3ab6f0f2b3d3298964b8c76b6f02e8c0", + "canonical_digest": "9b1796863b8c39a5eac8a7463675dd02f2acabe00ee81b063aa4fd4963263886", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/{p21}", + "operation_id": "corpusOperation21", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 21", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 21", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p21", + "target": "$.request.path.p21" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p21": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation21", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/{p21}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 21", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 21" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/{p21}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/{p21}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/{p21}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/{p21}", + "method": "GET", + "operation_id": "corpusOperation21", + "output_fields": 1, + "path": "/items/{p21}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 21", + "suggested_name": "corpus_operation21" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "6958720bdd39a877be5a8d399533b1bfab6ea191f4e3f26e187c3ba62f146ebf" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "24d9da484cfa2e2370a2d119564e289c5038dcb0f0536c98e1aad44ca7b82f27", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 21", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 21", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p21", + "target": "$.request.path.p21" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p21": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation21", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/{p21}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 21", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 21" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/{p21}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/{p21}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/{p21}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/{p21}", + "method": "GET", + "operation_id": "corpusOperation21", + "output_fields": 1, + "path": "/items/{p21}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 21", + "suggested_name": "corpus_operation21" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 21", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-22", + "file": "representative-22.json", + "sha256": "f7524fe669b795a8a294a8ec066233ee33aafa42480466e356995ad39fb7e844", + "canonical_digest": "95a86bf62de2b35e3ff093ef3589e7d0e9bef1814072f84d4676bea18ab4687f", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/22", + "operation_id": "corpusOperation22", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 22", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 22", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p22", + "target": "$.request.headers.p22" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p22": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation22", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/22", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 22", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 22" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/22", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/22", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/22", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/22", + "method": "POST", + "operation_id": "corpusOperation22", + "output_fields": 1, + "path": "/items/22", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 22", + "suggested_name": "corpus_operation22" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "18a26335be93086e4a74fcac6e8e1dc9430ea5d7bc06b20a7903281944a8ec27" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "cea999bcec66e9448170e5c6c2f5de1964307d2c6d759c9920ff8aee2919c50d", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 22", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 22", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p22", + "target": "$.request.headers.p22" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p22": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation22", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/22", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 22", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 22" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/22", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/22", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/22", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/22", + "method": "POST", + "operation_id": "corpusOperation22", + "output_fields": 1, + "path": "/items/22", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 22", + "suggested_name": "corpus_operation22" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 22", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-23", + "file": "representative-23.json", + "sha256": "906c974b0ebc5e271dbc0f84860c58f6177ba1aec9f70fee34607f8c374facf7", + "canonical_digest": "5b2900541809a626b66279eb826f546d33a3875a05eb784f872ca9d0ba89f394", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/23", + "operation_id": "corpusOperation23", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 23", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 23", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p23", + "target": "$.request.headers.p23" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p23": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation23", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/23", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 23", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 23" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/23", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/23", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/23", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PUT /items/23", + "method": "PUT", + "operation_id": "corpusOperation23", + "output_fields": 1, + "path": "/items/23", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 23", + "suggested_name": "corpus_operation23" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "df2b0b63eac5950ab0861a6a8878d57d6f5054959559cef533e7c335790f3a46" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "3440f8b83dace0271d32474757e55a9955649bb79a3382542157089ee217e60c", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 23", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 23", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p23", + "target": "$.request.headers.p23" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p23": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation23", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/23", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 23", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 23" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/23", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/23", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/23", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PUT /items/23", + "method": "PUT", + "operation_id": "corpusOperation23", + "output_fields": 1, + "path": "/items/23", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 23", + "suggested_name": "corpus_operation23" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 23", + "version": "2.0" + } + } + } + }, + { + "id": "rep-24", + "file": "representative-24.json", + "sha256": "f1896301ec6d2a7bdefedbde03b12e11eab8c2c52892f1f6e6208de5f5dde40c", + "canonical_digest": "20faf7e5ff709ac571212eb0d54f6ae9dcad8f4693187202a6ee59b97d25c894", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/24", + "operation_id": "corpusOperation24", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 24", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 24", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p24", + "target": "$.request.query.p24" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p24": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation24", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/24", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 24", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 24" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/24", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/24", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/24", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/24", + "method": "PATCH", + "operation_id": "corpusOperation24", + "output_fields": 1, + "path": "/items/24", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 24", + "suggested_name": "corpus_operation24" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "65ee0a168653b0dbf54743cad77da4f54c9fa6728d9a1238e8723ae0a9c2bc46" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "86056b12cb45d441017d3d3575eed16e03fe5b16ebdc92b74c24bc5ab3f7a503", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 24", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 24", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p24", + "target": "$.request.query.p24" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p24": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation24", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/24", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 24", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 24" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/24", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/24", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/24", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/24", + "method": "PATCH", + "operation_id": "corpusOperation24", + "output_fields": 1, + "path": "/items/24", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 24", + "suggested_name": "corpus_operation24" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 24", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-25", + "file": "representative-25.json", + "sha256": "2a15bdd4e4aad1a9038e292daac366f9c3dbfd53d54988774bed474365893b21", + "canonical_digest": "95975636e300fdd54fd8e298bfd842be17dda051cd69f644cbac68d0e3556892", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/{p25}", + "operation_id": "corpusOperation25", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 25", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 25", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p25", + "target": "$.request.path.p25" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p25": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation25", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/{p25}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 25", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 25" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/{p25}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/{p25}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/{p25}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/{p25}", + "method": "DELETE", + "operation_id": "corpusOperation25", + "output_fields": 1, + "path": "/items/{p25}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 25", + "suggested_name": "corpus_operation25" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "c965d4dce4dad170b2a93bde2534a54eebd0ed9d2ac5ab2696aa9585f4794a74" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "17887c73d178ce009a9da0af29a4aadfb1e4dd6f6ef96b4f41df2a941cec5e4d", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 25", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 25", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p25", + "target": "$.request.path.p25" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p25": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation25", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/{p25}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 25", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 25" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/{p25}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/{p25}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/{p25}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/{p25}", + "method": "DELETE", + "operation_id": "corpusOperation25", + "output_fields": 1, + "path": "/items/{p25}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 25", + "suggested_name": "corpus_operation25" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 25", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-26", + "file": "representative-26.json", + "sha256": "6abe1a26901ea129842921dfaa4f5de96fa8bb8f76e24f6240e8f0a87fd9b198", + "canonical_digest": "bcdad59cb79703c34e19b93fff6d0a88e5f567483ff922b9a19892ccb6428cc7", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/26", + "operation_id": "corpusOperation26", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 26", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 26", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p26", + "target": "$.request.headers.p26" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p26": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation26", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/26", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 26", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 26" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/26", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/26", + "method": "HEAD", + "operation_id": "corpusOperation26", + "output_fields": 0, + "path": "/items/26", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 26", + "suggested_name": "corpus_operation26" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "0489bed5c29e4a2342bf0fb98818cc509b38f7f3425c1270f75816e9821cff70" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "fcb0c48dbea568412ea865a854572e5b306474e2ff1115fc2455d125135343c5", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 26", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 26", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p26", + "target": "$.request.headers.p26" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p26": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation26", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/26", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 26", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 26" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/26", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/26", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/26", + "method": "HEAD", + "operation_id": "corpusOperation26", + "output_fields": 0, + "path": "/items/26", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 26", + "suggested_name": "corpus_operation26" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 26", + "version": "2.0" + } + } + } + }, + { + "id": "rep-27", + "file": "representative-27.json", + "sha256": "ee9520f1b77a97c26f20575030ccbc5f012dad7de3ad677da5acbfff1117ae11", + "canonical_digest": "ae30324ee31dac383ef6881aeb48d377ff642ce2898dc1626cdf60645a1ae22e", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/27", + "operation_id": "corpusOperation27", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 27", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 27", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p27", + "target": "$.request.cookies.p27" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p27": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation27", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/27", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 27", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 27" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/27", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/27", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/27", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/27", + "method": "OPTIONS", + "operation_id": "corpusOperation27", + "output_fields": 1, + "path": "/items/27", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 27", + "suggested_name": "corpus_operation27" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "6c3270dafaceae5b55a2f911e7b1aaa2e6dc706c4489645965d1da93a4de07dc" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "db34a8368d69bf43aa4b6b7a76c273cf18c11182c193dbb7b8db7359f5872b92", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 27", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 27", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p27", + "target": "$.request.cookies.p27" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p27": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation27", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/27", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 27", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 27" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/27", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/27", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/27", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/27", + "method": "OPTIONS", + "operation_id": "corpusOperation27", + "output_fields": 1, + "path": "/items/27", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 27", + "suggested_name": "corpus_operation27" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 27", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-28", + "file": "representative-28.json", + "sha256": "9949eca03f9f5d603f51536ef8882eec1c48d3f51c1ea4d4651b130766589443", + "canonical_digest": "8b9cb181cc7e6ca0576511d2771fb747c0ec49bdaa77041d50275b9f6a8ac4d1", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/28", + "operation_id": "corpusOperation28", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 28", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 28", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p28", + "target": "$.request.query.p28" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p28": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation28", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/28", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 28", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 28" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/28", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/28", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/28", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/28", + "method": "GET", + "operation_id": "corpusOperation28", + "output_fields": 1, + "path": "/items/28", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 28", + "suggested_name": "corpus_operation28" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "c528641e585e4f5630f3a5704d091812a2a18971caa37242282eb3f16da4c67a" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "d3adb23d7a667a9af5d811f23629a70be015dc23092f285a37a24ca81a304d13", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 28", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 28", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p28", + "target": "$.request.query.p28" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p28": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation28", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/28", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 28", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 28" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/28", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/28", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/28", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/28", + "method": "GET", + "operation_id": "corpusOperation28", + "output_fields": 1, + "path": "/items/28", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 28", + "suggested_name": "corpus_operation28" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 28", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-29", + "file": "representative-29.json", + "sha256": "80339041ae250b644679606648cf6d38a618c0d62138aee044bb5c91aa761e7d", + "canonical_digest": "107816923a913e89784a25319ecc05059729095fc01c5076f01600fc6a5f075a", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/{p29}", + "operation_id": "corpusOperation29", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 29", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 29", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p29", + "target": "$.request.path.p29" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p29": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation29", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/{p29}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 29", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 29" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/{p29}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/{p29}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/{p29}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "POST /items/{p29}", + "method": "POST", + "operation_id": "corpusOperation29", + "output_fields": 1, + "path": "/items/{p29}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 29", + "suggested_name": "corpus_operation29" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "a74a24b3424c5f9e523891395908353043bd9596c817a05e35ae02b83fa20d41" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "09bb975376a094ccaf4c56d959ebd503bbbdf2ffa826cce6191b088c94204741", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 29", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 29", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p29", + "target": "$.request.path.p29" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p29": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation29", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/{p29}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 29", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 29" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/{p29}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/{p29}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/{p29}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "POST /items/{p29}", + "method": "POST", + "operation_id": "corpusOperation29", + "output_fields": 1, + "path": "/items/{p29}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 29", + "suggested_name": "corpus_operation29" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 29", + "version": "2.0" + } + } + } + }, + { + "id": "rep-30", + "file": "representative-30.json", + "sha256": "2c82a403311cce4c8c4241c68a45d469dbefc4527aada4e384ebc72fdb641359", + "canonical_digest": "f589dd931b7e659ed2b5716309fa956e82d30f13c139e8c013f6c7bc690655ae", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/30", + "operation_id": "corpusOperation30", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 30", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 30", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p30", + "target": "$.request.headers.p30" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p30": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation30", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/30", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 30", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 30" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/30", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/30", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/30", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/30", + "method": "PUT", + "operation_id": "corpusOperation30", + "output_fields": 1, + "path": "/items/30", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 30", + "suggested_name": "corpus_operation30" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "bdda992f77480c88566611002815e365c56c86f2f4140d731fc842b5e445fcb4" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "b210c43409a8ec7c22c0b18f4fbb0cfc5bf883114bd9f4eeba0a0fb17aeda36e", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 30", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 30", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p30", + "target": "$.request.headers.p30" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p30": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation30", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/30", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 30", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 30" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/30", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/30", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/30", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/30", + "method": "PUT", + "operation_id": "corpusOperation30", + "output_fields": 1, + "path": "/items/30", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 30", + "suggested_name": "corpus_operation30" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 30", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-31", + "file": "representative-31.json", + "sha256": "2884e3e566aa6cfb0130534b361dbe33c95bc06fbef748b19956da9bfbfdf8d0", + "canonical_digest": "800ae791e9308ccd6115f90a69fe15d3441deccdee50c32b4d0ba12b519d6640", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/31", + "operation_id": "corpusOperation31", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 31", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 31", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p31", + "target": "$.request.cookies.p31" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p31": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation31", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/31", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 31", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 31" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/31", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/31", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/31", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/31", + "method": "PATCH", + "operation_id": "corpusOperation31", + "output_fields": 1, + "path": "/items/31", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 31", + "suggested_name": "corpus_operation31" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "bb5a48106f54194d8477be02e7f502b804781267c8918321582238824c216387" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "4a5f4ea802f160c99bb81ef391fad9f7d73f7a7ec6a35860ca3bca61d96ac153", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 31", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 31", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p31", + "target": "$.request.cookies.p31" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p31": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation31", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/31", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 31", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 31" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/31", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/31", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/31", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/31", + "method": "PATCH", + "operation_id": "corpusOperation31", + "output_fields": 1, + "path": "/items/31", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 31", + "suggested_name": "corpus_operation31" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 31", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-32", + "file": "representative-32.json", + "sha256": "4954da505785f5c63b26387ef199db5eb7e497215ab8499dd3f24720a73cc8e5", + "canonical_digest": "0ef86109a68a36b3ae6a67cbd7f9c1df92bdc3f6f6840248ae0a4713340ea981", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/32", + "operation_id": "corpusOperation32", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 32", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 32", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p32", + "target": "$.request.query.p32" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p32": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation32", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/32", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 32", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 32" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/32", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/32", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/32", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/32", + "method": "DELETE", + "operation_id": "corpusOperation32", + "output_fields": 1, + "path": "/items/32", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 32", + "suggested_name": "corpus_operation32" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "ff3ef935a42e4860c953a7426783eb30e990b201f581b377b7efcd59e2d98e7d" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "1d3050a3ed6aa76e4fa0c36f2624f6ada83fc302953b606f6f60a76d63c7ec54", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 32", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 32", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p32", + "target": "$.request.query.p32" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p32": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation32", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/32", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 32", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 32" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/32", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/32", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/32", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/32", + "method": "DELETE", + "operation_id": "corpusOperation32", + "output_fields": 1, + "path": "/items/32", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 32", + "suggested_name": "corpus_operation32" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 32", + "version": "2.0" + } + } + } + }, + { + "id": "rep-33", + "file": "representative-33.json", + "sha256": "ff8c48b74ba9e3bbbe24140928411c07965d1486da4f12061a448e616bd92d5c", + "canonical_digest": "7217032f264ff4aec219d513f0adf7b7ad71a0192ef0f7dea6f793700a41b752", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/{p33}", + "operation_id": "corpusOperation33", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 33", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 33", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p33", + "target": "$.request.path.p33" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p33": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation33", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/{p33}", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 33", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 33" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/{p33}", + "method": "HEAD", + "operation_id": "corpusOperation33", + "output_fields": 0, + "path": "/items/{p33}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 33", + "suggested_name": "corpus_operation33" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "1c31ab36cbad661ea96a114ec0728b4754163f962c7b01bef77771cef46e4927" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "f01480c764a3922951cdc437c4e9425413d34796f96eedb94d01d9707e922c13", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 33", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 33", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p33", + "target": "$.request.path.p33" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p33": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation33", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/{p33}", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 33", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 33" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/{p33}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/{p33}", + "method": "HEAD", + "operation_id": "corpusOperation33", + "output_fields": 0, + "path": "/items/{p33}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 33", + "suggested_name": "corpus_operation33" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 33", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-34", + "file": "representative-34.json", + "sha256": "e386caf8964036b2e8b564d9deb026b809c264910cb6e1aa3ada993de19ac891", + "canonical_digest": "9e340330f24b88f38f54c126b2fe683617076009cb08858b40f09bd29e135421", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/34", + "operation_id": "corpusOperation34", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 34", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 34", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p34", + "target": "$.request.headers.p34" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p34": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation34", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/34", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 34", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 34" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/34", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/34", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/34", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/34", + "method": "OPTIONS", + "operation_id": "corpusOperation34", + "output_fields": 1, + "path": "/items/34", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 34", + "suggested_name": "corpus_operation34" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "4f54f1279047b5a447b71c6c9ce099000c9af45e4f2ca3e151e635e4bdcf24dc" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "69db31657eebcb3a63e0f3b8cdc0ea78ac2c7abdc578e30b21e74bbc76e6b8f5", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 34", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 34", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p34", + "target": "$.request.headers.p34" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p34": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation34", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/34", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 34", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 34" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/34", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/34", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/34", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/34", + "method": "OPTIONS", + "operation_id": "corpusOperation34", + "output_fields": 1, + "path": "/items/34", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 34", + "suggested_name": "corpus_operation34" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 34", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-35", + "file": "representative-35.json", + "sha256": "77b4f388d7095d8bb8cd17d4d355e371fd2674388cae8f1b7095d408db6ac539", + "canonical_digest": "28e1f54e1a032932cad8d8dad18482f3c54e76b519138db294f16e32bd4a02ed", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/35", + "operation_id": "corpusOperation35", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 35", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 35", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p35", + "target": "$.request.headers.p35" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p35": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation35", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/35", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 35", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 35" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/35", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/35", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/35", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/35", + "method": "GET", + "operation_id": "corpusOperation35", + "output_fields": 1, + "path": "/items/35", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 35", + "suggested_name": "corpus_operation35" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "8a587830d6aaa8f1a6545c134aa7da676af8911cd25608eaf92e1b12cef7553a" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "0a0f9531c9f9746d93b517139ccd15ef6c27d7c2d6291cf1442a13efb00bbc58", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 35", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 35", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p35", + "target": "$.request.headers.p35" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p35": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation35", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/35", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 35", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 35" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/35", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/35", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/35", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/35", + "method": "GET", + "operation_id": "corpusOperation35", + "output_fields": 1, + "path": "/items/35", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 35", + "suggested_name": "corpus_operation35" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 35", + "version": "2.0" + } + } + } + }, + { + "id": "rep-36", + "file": "representative-36.json", + "sha256": "1510b01b4470fbf98bc5b9b0132d2a9066a2f2a3601f6fb265c1365d2d957ee0", + "canonical_digest": "7da2a19e53f5ba2a1bba354f5689a37c97f2747423d210ab46faadfc3257d19c", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/36", + "operation_id": "corpusOperation36", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 36", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 36", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p36", + "target": "$.request.query.p36" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p36": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation36", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/36", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 36", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 36" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/36", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/36", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/36", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/36", + "method": "POST", + "operation_id": "corpusOperation36", + "output_fields": 1, + "path": "/items/36", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 36", + "suggested_name": "corpus_operation36" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "88c4e3f6fefc2ac6ceb82fdd43abcb7ff79a06e6fed736d782a31ee38a6a0faa" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "1bdaa0245b65a270e2f867694622d0d74745f39f66d2a05c744ccf7e7be6c3f8", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 36", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 36", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p36", + "target": "$.request.query.p36" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p36": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation36", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/36", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 36", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 36" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/36", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/36", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/36", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/36", + "method": "POST", + "operation_id": "corpusOperation36", + "output_fields": 1, + "path": "/items/36", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 36", + "suggested_name": "corpus_operation36" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 36", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-37", + "file": "representative-37.json", + "sha256": "7f27d6a6c2bca84003046cc4395d007ec5be0fd8622c23d20d47a89563de1ac1", + "canonical_digest": "b7cba36014f0c3947453cd0c54662a16f65a9e5bcd9dba084b8b6c0d26746044", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/{p37}", + "operation_id": "corpusOperation37", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 37", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 37", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p37", + "target": "$.request.path.p37" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p37": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation37", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/{p37}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 37", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 37" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/{p37}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/{p37}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/{p37}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/{p37}", + "method": "PUT", + "operation_id": "corpusOperation37", + "output_fields": 1, + "path": "/items/{p37}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 37", + "suggested_name": "corpus_operation37" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "8bf33bc3a06c02f561642bbfba5b0e86bb014a96751cc52969c1bb00f04183c9" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "5f0eafaa7ac0b07a5e463a8e0fea25690e34f2ac00d4516cac9bbdb7ad368312", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 37", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 37", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p37", + "target": "$.request.path.p37" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p37": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation37", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/{p37}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 37", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 37" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/{p37}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/{p37}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/{p37}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PUT /items/{p37}", + "method": "PUT", + "operation_id": "corpusOperation37", + "output_fields": 1, + "path": "/items/{p37}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 37", + "suggested_name": "corpus_operation37" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 37", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-38", + "file": "representative-38.json", + "sha256": "dfd009a76b885c9ea5ccf1921dc346445c04be898b27ea0a7de6fff7de760bc0", + "canonical_digest": "868111ff9a499a6b86641b51b68dc4ecb1e97b9b331ab7ef256940f8660f18c9", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/38", + "operation_id": "corpusOperation38", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 38", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 38", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p38", + "target": "$.request.headers.p38" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p38": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation38", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/38", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 38", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 38" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/38", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/38", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/38", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PATCH /items/38", + "method": "PATCH", + "operation_id": "corpusOperation38", + "output_fields": 1, + "path": "/items/38", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 38", + "suggested_name": "corpus_operation38" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "b1649cd1aa0e12ed9d623bb2c3353bd29a06daeb9d4c075b5d83975bbaaccf34" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "3dfccec8c5b6325c3b353567f1817fc7684b573cf41f330e9127e5bce4193cec", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 38", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 38", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p38", + "target": "$.request.headers.p38" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p38": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation38", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/38", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 38", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 38" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/38", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/38", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/38", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PATCH /items/38", + "method": "PATCH", + "operation_id": "corpusOperation38", + "output_fields": 1, + "path": "/items/38", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 38", + "suggested_name": "corpus_operation38" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 38", + "version": "2.0" + } + } + } + }, + { + "id": "rep-39", + "file": "representative-39.json", + "sha256": "5958032799fd1f4f45fce184ed40c75bf570c14028eb8f7d11498bcb7b065878", + "canonical_digest": "892de65450f650a6213749b28ef1285d65e30ea795c73e69f2f0411222279f6c", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/39", + "operation_id": "corpusOperation39", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 39", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 39", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p39", + "target": "$.request.cookies.p39" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p39": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation39", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/39", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 39", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 39" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/39", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/39", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/39", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/39", + "method": "DELETE", + "operation_id": "corpusOperation39", + "output_fields": 1, + "path": "/items/39", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 39", + "suggested_name": "corpus_operation39" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "e5ac58943ddf4d0cc84a236e9d6fde19afb322648ffe923a6bea3fe9fd97a06e" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "8b48ad70a2d60b0eff4659cbcfc63e3d1fbd7b8a9a0171a7d8b2b9f33363596a", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 39", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 39", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p39", + "target": "$.request.cookies.p39" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p39": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation39", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/39", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 39", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 39" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/39", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/39", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/39", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/39", + "method": "DELETE", + "operation_id": "corpusOperation39", + "output_fields": 1, + "path": "/items/39", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 39", + "suggested_name": "corpus_operation39" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 39", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-40", + "file": "representative-40.json", + "sha256": "d14a5e5d3567a8ca9c18266b42f7b254488e5820b0f7b01c1800b960011dbcdc", + "canonical_digest": "a0cd9b0e278ca496440c0a1da05edc5273d5d866721f99f574f45cf03af9bb5c", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/40", + "operation_id": "corpusOperation40", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 40", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 40", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p40", + "target": "$.request.query.p40" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p40": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation40", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/40", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 40", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 40" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/40", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/40", + "method": "HEAD", + "operation_id": "corpusOperation40", + "output_fields": 0, + "path": "/items/40", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 40", + "suggested_name": "corpus_operation40" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "15e33407ec019f8d44d6c3bbfbda67a5948b006663d5d3a713bfa6c0a5b846a0" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "305bde53db0152f2569d0bacd83e18dfd50c268e8f8a8a993a0e86a17de1ad2b", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 40", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 40", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p40", + "target": "$.request.query.p40" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p40": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation40", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/40", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 40", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 40" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/40", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/40", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/40", + "method": "HEAD", + "operation_id": "corpusOperation40", + "output_fields": 0, + "path": "/items/40", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 40", + "suggested_name": "corpus_operation40" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 40", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-41", + "file": "representative-41.json", + "sha256": "b2bcd396bb24c4b0f4cc24dae5c91ddcee158232f6b7104953712d3145530502", + "canonical_digest": "7ee5b1e2d9c260094e9df2472e6dafa88c89c9ddc61e9244b4f9879a8b9a5996", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/{p41}", + "operation_id": "corpusOperation41", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 41", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 41", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p41", + "target": "$.request.path.p41" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p41": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation41", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/{p41}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 41", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 41" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/{p41}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/{p41}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/{p41}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/{p41}", + "method": "OPTIONS", + "operation_id": "corpusOperation41", + "output_fields": 1, + "path": "/items/{p41}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 41", + "suggested_name": "corpus_operation41" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "b8edb69ee0103e45c29174033b741c40f0a36758f91657279f917694044f4478" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "e527f5b56f345b31f6e06beb129f79b4d5d997ec38dd0193360106373241944d", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 41", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 41", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p41", + "target": "$.request.path.p41" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p41": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation41", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/{p41}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 41", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 41" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/{p41}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/{p41}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/{p41}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/{p41}", + "method": "OPTIONS", + "operation_id": "corpusOperation41", + "output_fields": 1, + "path": "/items/{p41}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 41", + "suggested_name": "corpus_operation41" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 41", + "version": "2.0" + } + } + } + }, + { + "id": "rep-42", + "file": "representative-42.json", + "sha256": "c0edaf4bb1d59e6f7d57a1049e0dab90b4a9d91b80bb0d0ee29158fad40fcf16", + "canonical_digest": "c24174b2ad9c521c6a6aa68bff2f821e4b923f6f0bdef87973f0fb57016a6d57", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/42", + "operation_id": "corpusOperation42", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 42", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 42", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p42", + "target": "$.request.headers.p42" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p42": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation42", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/42", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 42", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 42" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/42", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/42", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/42", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/42", + "method": "GET", + "operation_id": "corpusOperation42", + "output_fields": 1, + "path": "/items/42", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 42", + "suggested_name": "corpus_operation42" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "088fa592fb2fadecde5c80a7929564b7fe07f977cc1489ba11faef17d737a250" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "c8bd86851b9b5f86b0e8f9c8cdadb2070917f86332368dc54dbe778c845e120a", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 42", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 42", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p42", + "target": "$.request.headers.p42" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p42": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation42", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/42", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 42", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 42" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/42", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/42", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/42", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/42", + "method": "GET", + "operation_id": "corpusOperation42", + "output_fields": 1, + "path": "/items/42", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 42", + "suggested_name": "corpus_operation42" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 42", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-43", + "file": "representative-43.json", + "sha256": "344059b33f831de8d8f859618bbba1b27dbd962185f83df29820729417d7789a", + "canonical_digest": "3821ceba0fa62cc6a42ef99131c57035ee7985406261505277c6cd94a2b7cfb4", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "POST", + "path": "/items/43", + "operation_id": "corpusOperation43", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 43", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 43", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p43", + "target": "$.request.cookies.p43" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p43": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation43", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/43", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 43", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 43" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/43", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/43", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/43", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/43", + "method": "POST", + "operation_id": "corpusOperation43", + "output_fields": 1, + "path": "/items/43", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 43", + "suggested_name": "corpus_operation43" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "fff9f2f539e9044fa1ea5d1a77f6979b97e9013b2a71026b7ad1c8811b501563" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "d44b8c3644f74050b6132e10c81bae01fbb9c5cee368e0ef358591299a249ead", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 43", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 43", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p43", + "target": "$.request.cookies.p43" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p43": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation43", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "POST", + "path_template": "/items/43", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 43", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 43" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "POST /items/43", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "POST /items/43", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "POST /items/43", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "POST /items/43", + "method": "POST", + "operation_id": "corpusOperation43", + "output_fields": 1, + "path": "/items/43", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 43", + "suggested_name": "corpus_operation43" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 43", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-44", + "file": "representative-44.json", + "sha256": "b317a43fa89e2431447d7059a1567f25e9c0a6eecad7076806447299cce6c15d", + "canonical_digest": "b7e8e18dc1aecd1f27aa8e6e0fc346912fb3d1062cf253920164dcb246f5d2ca", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PUT", + "path": "/items/44", + "operation_id": "corpusOperation44", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 44", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 44", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p44", + "target": "$.request.query.p44" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p44": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation44", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/44", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 44", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 44" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/44", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/44", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/44", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PUT /items/44", + "method": "PUT", + "operation_id": "corpusOperation44", + "output_fields": 1, + "path": "/items/44", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 44", + "suggested_name": "corpus_operation44" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "b59e60ac3579cd88daf718920878ec1293e1d59d3aa9ed75c2de01e3f8074ff8" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "74544bedddfe51d1df3ea632e689986ff9e0249b0b3c6132a88ede3c56285fa7", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 44", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 44", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p44", + "target": "$.request.query.p44" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p44": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation44", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PUT", + "path_template": "/items/44", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 44", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 44" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PUT /items/44", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PUT /items/44", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PUT /items/44", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "PUT /items/44", + "method": "PUT", + "operation_id": "corpusOperation44", + "output_fields": 1, + "path": "/items/44", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 44", + "suggested_name": "corpus_operation44" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 44", + "version": "2.0" + } + } + } + }, + { + "id": "rep-45", + "file": "representative-45.json", + "sha256": "df903f2fb729e3e7c3c577fe4f62845ff4a3a983d62b86f7bad28953c86febfd", + "canonical_digest": "5cf55018af288f9df4cbba10dcf2d1bd55137ca9a208f58b4c3a04877fee7568", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "PATCH", + "path": "/items/{p45}", + "operation_id": "corpusOperation45", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 45", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 45", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p45", + "target": "$.request.path.p45" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p45": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation45", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/{p45}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 45", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 45" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/{p45}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/{p45}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/{p45}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/{p45}", + "method": "PATCH", + "operation_id": "corpusOperation45", + "output_fields": 1, + "path": "/items/{p45}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 45", + "suggested_name": "corpus_operation45" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "65d6ae2e5bb1fc4cd59b5e39887b1b07098cb7cae43dcb316fe4196e3be88fea" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "bb4e8bf0d0be34be887c03b21fee3356a46b2aee43049acbebf51368d5fbbc13", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 45", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 45", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p45", + "target": "$.request.path.p45" + }, + { + "required": true, + "source": "$.mcp.body", + "target": "$.request.body" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "body": { + "description": "Тело запроса (application/json)", + "fields": { + "name": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "p45": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation45", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "PATCH", + "path_template": "/items/{p45}", + "request_contract": { + "body_field": "body", + "body_required": true, + "variants": [ + { + "media_type": "application/json", + "schema_variant": 0 + } + ], + "version": "1" + }, + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 45", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 45" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "PATCH /items/{p45}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "PATCH /items/{p45}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "PATCH /items/{p45}", + "severity": "warning" + } + ], + "input_fields": 2, + "key": "PATCH /items/{p45}", + "method": "PATCH", + "operation_id": "corpusOperation45", + "output_fields": 1, + "path": "/items/{p45}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 45", + "suggested_name": "corpus_operation45" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 45", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-46", + "file": "representative-46.json", + "sha256": "bd257bca997125e9b59864c80f7417ef97723fcee72591ed183d633405959a9e", + "canonical_digest": "410269ecb03265d89c29547e1d6e42b0673275db116ed8c9ac4c96f69b2f051a", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "DELETE", + "path": "/items/46", + "operation_id": "corpusOperation46", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 46", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 46", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p46", + "target": "$.request.headers.p46" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p46": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation46", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/46", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 46", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 46" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/46", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/46", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/46", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/46", + "method": "DELETE", + "operation_id": "corpusOperation46", + "output_fields": 1, + "path": "/items/46", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 46", + "suggested_name": "corpus_operation46" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "c6ca23d3fd84146ec1a9ac2e55abcb216085dbf2a880b4356eb05cc058ba6ed4" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "829c70f4fe337392061fee3009dbe235a19fe063b57f6c88f2c1b7aed8413be1", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 46", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 46", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p46", + "target": "$.request.headers.p46" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p46": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation46", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "DELETE", + "path_template": "/items/46", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 202 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 46", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 46" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "DELETE /items/46", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "DELETE /items/46", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "DELETE /items/46", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "DELETE /items/46", + "method": "DELETE", + "operation_id": "corpusOperation46", + "output_fields": 1, + "path": "/items/46", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 46", + "suggested_name": "corpus_operation46" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 46", + "version": "3.1.0" + } + } + } + }, + { + "id": "rep-47", + "file": "representative-47.json", + "sha256": "c0534a712bc1656553e242688433e09c0969b8daf9c620f9282600f1fccbc47d", + "canonical_digest": "a07081e8892a089dc8909b36fd691701bdcfb58561db163eb208fab8ce495624", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "Swagger 2.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "HEAD", + "path": "/items/47", + "operation_id": "corpusOperation47", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 47", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 47", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p47", + "target": "$.request.headers.p47" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p47": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation47", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/47", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 47", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 47" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/47", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/47", + "method": "HEAD", + "operation_id": "corpusOperation47", + "output_fields": 0, + "path": "/items/47", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 47", + "suggested_name": "corpus_operation47" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "e8dfae85504a7fc342ab0c86a3bed2e60e783b85f6d5f4d84b48fb7b6ccb8b47" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description", + "missing_response_schema", + "empty_output_schema" + ], + "canonical_preview_digest": "001b1abf3fc6829704588a861ac5d4f51663c5781220ee36bb2647a082b40e54", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 47", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 47", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p47", + "target": "$.request.headers.p47" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p47": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation47", + "output_mapping": { + "rules": [ + { + "required": true, + "source": "$.response.body", + "target": "$.output" + } + ] + }, + "output_schema": { + "description": "Успешный ответ без тела", + "nullable": false, + "required": true, + "type": "null" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "HEAD", + "path_template": "/items/47", + "response_contract": [ + { + "schema_variant": 0, + "status_code": 204 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 47", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 47" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "missing_response_schema", + "message": "У метода не найдена схема успешного ответа, результат будет описан как общий объект.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "empty_output_schema", + "message": "В ответе не найдено отдельных полей. Перед публикацией проверьте схему ответа и маппинг результата.", + "operation_key": "HEAD /items/47", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "HEAD /items/47", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "HEAD /items/47", + "method": "HEAD", + "operation_id": "corpusOperation47", + "output_fields": 0, + "path": "/items/47", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 47", + "suggested_name": "corpus_operation47" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "swagger", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 47", + "version": "2.0" + } + } + } + }, + { + "id": "rep-48", + "file": "representative-48.json", + "sha256": "900c2c23f50671a0c69254938829396a44fc1139743aec5ea7032d06de3e611b", + "canonical_digest": "343b7374576c4792b83e0c28b1d925376a93f8bd44a4ba4b0fb313fbcf46f68a", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "OPTIONS", + "path": "/items/48", + "operation_id": "corpusOperation48", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 48", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 48", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p48", + "target": "$.request.query.p48" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p48": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation48", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/48", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 48", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 48" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/48", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/48", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/48", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/48", + "method": "OPTIONS", + "operation_id": "corpusOperation48", + "output_fields": 1, + "path": "/items/48", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 48", + "suggested_name": "corpus_operation48" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "8a20820455e4be26069ce5241ea43711c7f4e7365d88be485ba280b5dd9fc72c" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "5b1c2d40ea513010bc2fecde834c881afd6ce783374cca5a04162444f4c1ab47", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 48", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 48", + "input_mapping": { + "rules": [ + { + "required": false, + "source": "$.mcp.p48", + "target": "$.request.query.p48" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p48": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation48", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "OPTIONS", + "path_template": "/items/48", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 200 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 48", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 48" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "OPTIONS /items/48", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "OPTIONS /items/48", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "OPTIONS /items/48", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "OPTIONS /items/48", + "method": "OPTIONS", + "operation_id": "corpusOperation48", + "output_fields": 1, + "path": "/items/48", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 48", + "suggested_name": "corpus_operation48" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 48", + "version": "3.0.3" + } + } + } + }, + { + "id": "rep-49", + "file": "representative-49.json", + "sha256": "137b9555072bcf5c8652926fae0e0eff224d01a1d2416f3071c8e3d5fef49f16", + "canonical_digest": "b84c37ab3958d37ab21e7a750d7525760aaac0a1be425cf8dfa2e2e0133fa8fa", + "origin": "synthetic: Story 2.14 deterministic corpus generator", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "representative", + "expected_preview": { + "operation_count": 1, + "operations": [ + { + "method": "GET", + "path": "/items/{p49}", + "operation_id": "corpusOperation49", + "candidate": { + "category": "corpus", + "description": "Synthetic operation 49", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 49", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p49", + "target": "$.request.path.p49" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p49": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation49", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/{p49}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 49", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 49" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/{p49}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/{p49}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/{p49}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/{p49}", + "method": "GET", + "operation_id": "corpusOperation49", + "output_fields": 1, + "path": "/items/{p49}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 49", + "suggested_name": "corpus_operation49" + }, + "actionable": true, + "blocker_codes": [], + "candidate_digest": "86e4df8c54da258c60b63799a0d4abc36327147abdd33ace73fca1cb90cd585b" + } + ], + "finding_codes": [ + "missing_description", + "parameter_descriptions_missing", + "weak_tool_description" + ], + "canonical_preview_digest": "02adff9a3309768b70d17788ebe16f7a13ef442b1b6483f4c45047d1d7305e8f", + "canonical_preview": { + "findings": [], + "groups": [ + { + "key": "corpus", + "operations": [ + { + "category": "corpus", + "description": "Synthetic operation 49", + "draft": { + "category": "corpus", + "display_name": "Synthetic operation 49", + "input_mapping": { + "rules": [ + { + "required": true, + "source": "$.mcp.p49", + "target": "$.request.path.p49" + } + ] + }, + "input_schema": { + "description": "Входные параметры MCP-инструмента", + "fields": { + "p49": { + "nullable": false, + "required": true, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "name": "corpus_operation49", + "output_mapping": { + "rules": [ + { + "required": false, + "source": "$.response.body.id", + "target": "$.output.id" + } + ] + }, + "output_schema": { + "description": "Ответ API", + "fields": { + "id": { + "nullable": false, + "required": false, + "type": "string" + } + }, + "nullable": false, + "required": true, + "type": "object" + }, + "target": { + "base_url": "https://api.example.test/v1", + "method": "GET", + "path_template": "/items/{p49}", + "response_contract": [ + { + "media_type": "application/json", + "schema_variant": 0, + "status_code": 201 + } + ] + }, + "tool_description": { + "description": "Synthetic operation 49", + "examples": [ + { + "input": {} + } + ], + "tags": [ + "corpus" + ], + "title": "Synthetic operation 49" + }, + "wizard_state": {} + }, + "findings": [ + { + "code": "missing_description", + "message": "У метода нет description. Перед публикацией лучше описать, когда агенту стоит вызывать этот инструмент.", + "operation_key": "GET /items/{p49}", + "severity": "warning" + }, + { + "code": "parameter_descriptions_missing", + "message": "У 1 входных параметров нет описания. Модели будет сложнее понять, какие значения туда передавать.", + "operation_key": "GET /items/{p49}", + "severity": "warning" + }, + { + "code": "weak_tool_description", + "message": "Описание инструмента слишком короткое или техническое. Перед публикацией добавьте, когда агент должен вызывать инструмент и что будет в успешном ответе.", + "operation_key": "GET /items/{p49}", + "severity": "warning" + } + ], + "input_fields": 1, + "key": "GET /items/{p49}", + "method": "GET", + "operation_id": "corpusOperation49", + "output_fields": 1, + "path": "/items/{p49}", + "server_urls": [ + "https://api.example.test/v1" + ], + "suggested_display_name": "Synthetic operation 49", + "suggested_name": "corpus_operation49" + } + ], + "title": "corpus" + } + ], + "source": { + "format": "openapi", + "servers": [ + "https://api.example.test/v1" + ], + "title": "Synthetic corpus 49", + "version": "3.1.0" + } + } + } + }, + { + "id": "hostile-00", + "file": "hostile-empty.json", + "sha256": "44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", + "canonical_digest": "44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "unsupported_document", + "operation_count": 0 + } + }, + { + "id": "hostile-01", + "file": "hostile-malformed.json", + "sha256": "a9d15d823d7991d6a03ea7d8f3164917801394e565a13ce4c703c4e9cebae97e", + "canonical_digest": null, + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "invalid_document", + "operation_count": 0 + } + }, + { + "id": "hostile-02", + "file": "hostile-trace.json", + "sha256": "93c38cbc25c13bf6bf214cb3e5bec7f58bb6f8130c9b683d5e8aa944bdf399cc", + "canonical_digest": "d0f1a5461674d9247e7cd17edddc1759107e7bd2c3fb275f21d98a35f1d4b7d4", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "unsupported_http_method", + "operation_count": 0 + } + }, + { + "id": "hostile-03", + "file": "hostile-xml.json", + "sha256": "91c8c8596b4f3e457b3bb0ef2a7ca7f4444ee1bbaaa6266d5b3ef57a40998dd1", + "canonical_digest": "b57c0402a3a0af3ca2a564fdfe2a5aab480b6eb391b77505283ccdc84804a0ae", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "unsupported_media_type", + "operation_count": 0 + } + }, + { + "id": "hostile-04", + "file": "hostile-secret.json", + "sha256": "21742b0719fa4130bea7c52a2e805ad2b97cd12b8ca7ce716c33d01caf8d0d34", + "canonical_digest": "2265d8405b5f7b8cc699fe4c7bc7424c76b5888e92420e2b983dd8bb90020341", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "secret_like_example", + "operation_count": 0 + } + }, + { + "id": "hostile-05", + "file": "hostile-oversized.json", + "sha256": "99e2643db10661f1a23ddc14e9f05cb8b615dfec0c470de6f69c0aa073f4a4fc", + "canonical_digest": "25dda3bfdff11690d47dd35c13286a92679a83139f1554eaa56b978b31f653d8", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "limit_exceeded", + "operation_count": 0 + } + }, + { + "id": "hostile-06", + "file": "hostile-deep.json", + "sha256": "2a8d551e95929c31f8a66a40021467a42ce95245766d173eefc751f4b87963f5", + "canonical_digest": "fbbb9b8f33072e4cc407621455788d71fc2e2335f5cad7201efd0c7d1c13e354", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "limit_exceeded", + "operation_count": 0 + } + }, + { + "id": "hostile-07", + "file": "hostile-aliases.yaml", + "sha256": "eb1c1c07236b70550ad487d32470c7791928930d9e62a047be2d1c858c917eae", + "canonical_digest": null, + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "limit_exceeded", + "operation_count": 0 + } + }, + { + "id": "hostile-08", + "file": "hostile-cycle.json", + "sha256": "6398f8694b9bdd9ca8d262520339eb080a7cfbaf7424a6da1767bb3c39e64d99", + "canonical_digest": "a27a1d13b10680f27f7d73714da1278eadf263afab629eba3c69191b32c1d155", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "parse_error": null, + "operation_count": 1, + "warning_findings": [ + { + "code": "missing_servers", + "severity": "Warning", + "operation_key": null + } + ], + "blocker_findings": [], + "unresolved_references": [], + "recursive_edge": { + "source": "/components/schemas/Node/properties/next/$ref", + "target": "/components/schemas/Node" + }, + "mapped_recursive_pointer": "/components/schemas/Node/properties/next/properties/next", + "response_schema": "object_with_next_property" + } + }, + { + "id": "hostile-09", + "file": "hostile-external-ref.json", + "sha256": "19415da83546af15f12fbcf006a0c0a751a75a4d3442ab75b368ced31eb800bb", + "canonical_digest": "acfd7aab72d67bee3c48bb4b45886ef70d16661bfceb36e368f93169a0715ab7", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "external_reference_not_fetched", + "operation_count": 0 + } + }, + { + "id": "hostile-10", + "file": "hostile-duplicate-ids.json", + "sha256": "ad11729cb05dd86dedf73e4ff7b823161efbbf79f9f9451fd55e96669577a6aa", + "canonical_digest": "9aae7f895869f3a6e7b186ea507e96439dabf0bec15dd52e4f635c376e1e3d45", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "error_or_finding": "duplicate_operation_id", + "operation_count": 0 + } + }, + { + "id": "hostile-11", + "file": "hostile-composition-conflict.json", + "sha256": "d4a1120da066f6e3ba22c131e306a277f6a2dc6d1286f38c951bb8574e330c1f", + "canonical_digest": "a187c94230e068ae97d1bddc5bc6c65af3e6cca9109e8964cac4a5d68bc4bdd2", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.1.0", + "classification": "hostile", + "expected_preview": { + "parse_error": null, + "operation_count": 1, + "warning_findings": [ + { + "code": "missing_servers", + "severity": "Warning", + "operation_key": null + } + ], + "blocker_findings": [ + { + "code": "all_of_conflict", + "severity": "Error", + "operation_key": "GET /hostile", + "location": "/paths/~1hostile/get/responses/200/content/application~1json/schema/allOf" + } + ], + "composition": { + "operator": "allOf", + "branch_count": 2 + }, + "response_schema": "allOf_conflict_branches_preserved" + } + }, + { + "id": "hostile-12", + "file": "hostile-security-scope.json", + "sha256": "ff745a5c8c613790e64672162973ea0d2e77f2b70ae715841cf397ee4951da1e", + "canonical_digest": "f2f9627d754ed9f77cf41d910de84f525c00bd3a0f9f2ed511a9c388b1813db9", + "origin": "synthetic: Story 2.14 hostile input", + "license": "CC0-1.0 synthetic; no third-party content", + "specification_version": "OpenAPI 3.0.3", + "classification": "hostile", + "expected_preview": { + "parse_error": null, + "operation_keys": [ + "GET /public", + "GET /secure" + ], + "warning_findings": [], + "blocker_findings": [] + } + } + ], + "compatibility_impact": "Corpus 1.0.4 is a metadata-only SemVer increment aligning its manifest with support matrix 1.0.3; fixture contents and checksums are unchanged. Corpus 1.0.3 added hostile-security-scope.json: a document-level basic requirement remains selectable on the inherited candidate while an explicit security: [] override stays public; malformed or unsupported requirements retain scoped blockers. Support matrix 1.0.3 adds executable blockers for undeclared path placeholders and object/array schema defaults across OpenAPI 3.0, OpenAPI 3.1, and Swagger 2.0 while preserving scalar defaults and previously supported behavior." +} diff --git a/crates/crank-import/tests/corpus/matrix.rs b/crates/crank-import/tests/corpus/matrix.rs new file mode 100644 index 0000000..0097dfc --- /dev/null +++ b/crates/crank-import/tests/corpus/matrix.rs @@ -0,0 +1,924 @@ +//! Executable normalizer probes for every normative support-matrix row. +use super::*; +use crank_import::rest::{NormalizedSchemaKind, normalize_verified_bundle}; + +fn assert_actionable(ir: &crank_import::rest::NormalizedIr, context: &str) { + let preview = preview_from_ir(ir); + assert!( + !preview.findings.iter().any(|finding| { + finding.severity == crank_import::rest::ImportFindingSeverity::Error + }), + "{context} has a document blocker: {:?}", + preview.findings + ); + assert!( + preview + .groups + .iter() + .flat_map(|group| &group.operations) + .all(|candidate| { + !candidate.findings.iter().any(|finding| { + finding.severity == crank_import::rest::ImportFindingSeverity::Error + }) + }), + "{context} unexpectedly blocked: {:?}", + preview.groups + ); +} + +#[test] +fn ac_1_support_matrix_rows_have_exact_construct_contracts() { + let matrix_text = fs::read_to_string(tests_root().join("support-matrix.yaml")) + .expect("support matrix exists"); + let matrix: serde_yaml::Value = serde_yaml::from_str(&matrix_text).expect("valid YAML"); + assert_eq!(matrix["matrix_version"].as_str(), Some("1.0.3")); + assert!( + matrix["compatibility_impact"] + .as_str() + .is_some_and(|s| !s.trim().is_empty()) + ); + let rows = matrix["constructs"].as_sequence().expect("construct rows"); + assert!(rows.len() >= 375); + let mut construct_keys = std::collections::BTreeSet::new(); + for row in rows { + assert!( + row["specification_version"].as_str().is_some_and(|s| [ + "OpenAPI 3.0", + "OpenAPI 3.1", + "Swagger 2.0" + ] + .contains(&s)) + ); + assert!(row["construct_id"].as_str().is_some_and(|s| !s.is_empty())); + assert!(matches!( + row["status"].as_str(), + Some("supported" | "unsupported") + )); + assert!( + row["preserved_semantics"] + .as_str() + .is_some_and(|s| !s.trim().is_empty()) + ); + assert!( + row["expected_finding"] + .as_str() + .is_some_and(|s| !s.trim().is_empty()) + ); + let key = format!( + "{}:{}", + row["specification_version"].as_str().unwrap(), + row["construct_id"].as_str().unwrap() + ); + assert!( + construct_keys.insert(key.clone()), + "duplicate normative construct row: {key}" + ); + if row["status"] == "supported" { + assert_eq!( + row["expected_finding"], "none", + "supported construct has a finding: {key}" + ); + } else { + assert_ne!( + row["expected_finding"], "none", + "unsupported construct has no finding: {key}" + ); + } + } + for version in ["OpenAPI 3.0", "OpenAPI 3.1", "Swagger 2.0"] { + // Exhaustive accounting: every normative row of this format must be + // matched by an executed probe before the loop completes. + let mut unprobed = rows + .iter() + .filter(|row| row["specification_version"] == version) + .map(|row| { + ( + version.to_owned(), + row["construct_id"].as_str().unwrap().to_owned(), + ) + }) + .collect::>(); + let mut probed = |construct: &str| { + let taken = unprobed.remove(&(version.to_owned(), construct.to_owned())); + assert!(taken, "{version} double-probes or misses {construct}"); + }; + let ids = rows + .iter() + .filter(|r| r["specification_version"] == version) + .filter_map(|r| r["construct_id"].as_str()) + .collect::>(); + for id in [ + "http.method.get", + "http.method.post", + "http.method.put", + "http.method.patch", + "http.method.delete", + "http.method.head", + "http.method.options", + "parameter.location.path", + "parameter.location.query", + "parameter.location.header", + "parameter.location.cookie", + "request_body.application/json", + "request_body.application/x-www-form-urlencoded", + "request_body.multipart/form-data", + "reference.local", + "reference.external_allowlisted", + "composition.allOf", + "composition.oneOf", + "composition.anyOf", + "security.bearer", + "security.basic", + "security.api_key", + "media_type.application_xml.request", + "media_type.application_xml.response", + "http.method.trace", + "http.method.connect", + "parameter.path.declaration", + "schema.default.compound", + ] { + assert!(ids.contains(&id), "{version} matrix misses {id}"); + } + for security_kind in ["bearer", "basic", "api_key"] { + let construct = format!("security.{security_kind}"); + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let probe = if version == "Swagger 2.0" { + json!({ + "swagger":"2.0", "info":{"title":"Security probe","version":"1"}, + "securityDefinitions":{"probe": if security_kind == "bearer" { + json!({"type":"oauth2","flow":"implicit","authorizationUrl":"https://auth.example.test","scopes":{}}) + } else if security_kind == "api_key" { + json!({"type":"apiKey","name":"X-Key","in":"header"}) + } else { + json!({"type":"basic"}) + }}, + "paths":{"/probe":{"get":{"security":[{"probe":[]}],"operationId":"securityProbe","responses":{"200":{"description":"ok"}}}}} + }) + } else { + let scheme = match security_kind { + "bearer" => json!({"type":"http","scheme":"bearer"}), + "basic" => json!({"type":"http","scheme":"basic"}), + "api_key" => json!({"type":"apiKey","name":"X-Key","in":"header"}), + _ => unreachable!(), + }; + json!({ + "openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Security probe","version":"1"}, + "servers":[{"url":"https://matrix.example.test"}], + "components":{"securitySchemes":{"probe":scheme}}, + "paths":{"/probe":{"get":{"security":[{"probe":[]}],"operationId":"securityProbe","responses":{"200":{"description":"ok"}}}}} + }) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + let expected_finding = row["expected_finding"].as_str().unwrap(); + let preview = preview_from_ir(&ir); + let candidate = &preview.groups[0].operations[0]; + let requirement = &ir.operations[0].security_requirements.alternatives[0]; + if expected_finding == "none" { + assert!( + requirement.supported, + "{version} {construct} is not selectable" + ); + assert!( + candidate.findings.iter().all(|finding| { + finding.severity != crank_import::rest::ImportFindingSeverity::Error + }), + "{version} {construct} unexpectedly blocked: {:?}", + candidate.findings + ); + assert!(ir.security_schemes.contains_key("probe")); + assert!( + ir.coverage.iter().any(|entry| { + entry.location.pointer == "/paths/~1probe/get/security/0" + && entry.disposition == CoverageDisposition::Mapped + }), + "{version} {construct} requirement has no mapped source coverage" + ); + } else { + let finding = ir + .findings + .iter() + .chain( + ir.operations + .iter() + .flat_map(|operation| operation.findings.iter()), + ) + .find(|finding| { + finding.code == expected_finding + && finding.severity == crank_import::rest::ImportFindingSeverity::Error + }) + .unwrap_or_else(|| { + panic!("{version} {construct} missing blocker: {:?}", ir.findings) + }); + assert_eq!(finding.operation_key.as_deref(), Some("GET /probe")); + assert_eq!(finding.location.pointer, "/paths/~1probe/get/security"); + assert!( + candidate + .findings + .iter() + .any(|finding| finding.code == expected_finding + && finding.severity + == crank_import::rest::ImportFindingSeverity::Error) + ); + assert!(ir.coverage.iter().any(|entry| entry.location.pointer + == "/paths/~1probe/get/security" + && entry.disposition == CoverageDisposition::Finding)); + } + probed(&construct); + } + { + let construct = "security.override_empty"; + let override_probe = |path: &str| { + if version == "Swagger 2.0" { + json!({ + "swagger":"2.0", "info":{"title":"Security override probe","version":"1"}, + "securityDefinitions":{"probe":{"type":"basic"}}, + "security":[{"probe":[]}], + "paths":{path:{"get":{"security":[],"operationId":"securityOverride","responses":{"200":{"description":"ok"}}}}} + }) + } else { + json!({ + "openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Security override probe","version":"1"}, + "servers":[{"url":"https://matrix.example.test"}], + "components":{"securitySchemes":{"probe":{"type":"http","scheme":"basic"}}}, + "security":[{"probe":[]}], + "paths":{path:{"get":{"security":[],"operationId":"securityOverride","responses":{"200":{"description":"ok"}}}}} + }) + } + }; + let source = serde_json::to_string(&override_probe("/override")).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + assert!( + ir.findings + .iter() + .all(|finding| finding.code != "unsupported_security_requirement"), + "explicit empty security override must disable inheritance: {:?}", + ir.findings + ); + let preview = preview_from_ir(&ir); + let candidate = &preview.groups[0].operations[0]; + assert!( + candidate + .findings + .iter() + .all(|finding| finding.severity + != crank_import::rest::ImportFindingSeverity::Error), + "overridden operation must remain actionable: {:?}", + candidate.findings + ); + probed(construct); + } + { + let construct = "security.mixed_operations"; + let mixed_probe = if version == "Swagger 2.0" { + json!({ + "swagger":"2.0", "info":{"title":"Mixed security probe","version":"1"}, + "securityDefinitions":{"probe":{"type":"basic"}}, + "security":[{"probe":[]}], + "paths":{ + "/public":{"get":{"security":[],"operationId":"publicProbe","responses":{"200":{"description":"ok"}}}}, + "/secured":{"get":{"operationId":"securedProbe","responses":{"200":{"description":"ok"}}}} + } + }) + } else { + json!({ + "openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Mixed security probe","version":"1"}, + "servers":[{"url":"https://matrix.example.test"}], + "components":{"securitySchemes":{"probe":{"type":"http","scheme":"basic"}}}, + "security":[{"probe":[]}], + "paths":{ + "/public":{"get":{"security":[],"operationId":"publicProbe","responses":{"200":{"description":"ok"}}}}, + "/secured":{"get":{"operationId":"securedProbe","responses":{"200":{"description":"ok"}}}} + } + }) + }; + let source = serde_json::to_string(&mixed_probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + let expected_finding = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap()["expected_finding"] + .as_str() + .unwrap(); + assert_eq!(expected_finding, "none"); + let secured_operation = ir + .operations + .iter() + .find(|operation| operation.key == "GET /secured") + .unwrap(); + assert!(secured_operation.security_requirements.alternatives[0].supported); + assert_eq!( + secured_operation.security_requirements.alternatives[0] + .source_location + .as_ref() + .unwrap() + .pointer, + "/security/0" + ); + let preview = preview_from_ir(&ir); + let mut by_key = std::collections::BTreeMap::new(); + for group in &preview.groups { + for candidate in &group.operations { + by_key.insert(candidate.key.clone(), candidate); + } + } + let public = by_key.get("GET /public").expect("public candidate present"); + assert!( + public + .findings + .iter() + .all(|finding| finding.severity + != crank_import::rest::ImportFindingSeverity::Error), + "public operation must stay actionable: {:?}", + public.findings + ); + let secured = by_key + .get("GET /secured") + .expect("secured candidate present"); + assert!( + secured + .findings + .iter() + .all(|finding| finding.severity + != crank_import::rest::ImportFindingSeverity::Error), + "supported secured operation must remain selectable before API binding: {:?}", + secured.findings + ); + assert!( + ir.coverage + .iter() + .any(|entry| entry.location.pointer == "/security/0" + && entry.disposition == CoverageDisposition::Mapped), + "mixed probe has no mapped security coverage" + ); + probed(construct); + } + for method in [ + "get", "post", "put", "patch", "delete", "head", "options", "trace", "connect", + ] { + let construct = format!("http.method.{method}"); + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let mut path_item = serde_json::Map::new(); + path_item.insert( + method.to_owned(), + json!({ + "operationId":"methodProbe", "responses":{"200":{"description":"ok"}} + }), + ); + let probe = if version == "Swagger 2.0" { + json!({"swagger":"2.0","info":{"title":"Method probe","version":"1"},"paths":{"/probe":path_item,"/normal":{"get":{"operationId":"normalProbe","responses":{"200":{"description":"ok"}}}}}}) + } else { + json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Method probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":path_item,"/normal":{"get":{"operationId":"normalProbe","responses":{"200":{"description":"ok"}}}}}}) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + if row["status"] == "supported" { + assert!( + ir.operations + .iter() + .any( + |operation| serde_json::to_value(operation.method).unwrap().as_str() + == Some(&method.to_ascii_uppercase()) + ), + "{version} {construct} was not preserved" + ); + assert_eq!(row["expected_finding"], "none"); + assert_actionable(&ir, &format!("{version} {construct}")); + } else { + let expected_finding = row["expected_finding"].as_str().unwrap(); + assert!( + ir.operations + .iter() + .any(|operation| operation.path == "/normal"), + "{version} {construct} must not discard a supported neighbour" + ); + assert!( + ir.findings + .iter() + .any(|finding| finding.code == expected_finding + && finding.severity + == crank_import::rest::ImportFindingSeverity::Error), + "{version} {construct} missing exact finding {expected_finding}: {:?}", + ir.findings + ); + assert!( + ir.coverage + .iter() + .any(|entry| entry.construct_id.ends_with(&format!("/{method}")) + && entry.disposition == CoverageDisposition::Finding), + "{version} {construct} has no finding coverage" + ); + } + probed(&construct); + } + for location in ["path", "query", "header", "cookie"] { + let construct = format!("parameter.location.{location}"); + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let parameter = if version == "Swagger 2.0" { + json!({"name":"probe","in":location,"required":location == "path","type":"string"}) + } else { + json!({"name":"probe","in":location,"required":location == "path","schema":{"type":"string"}}) + }; + let path = if location == "path" { + "/probe/{probe}" + } else { + "/probe" + }; + let operation = json!({"operationId":"parameterProbe","parameters":[parameter],"responses":{"200":{"description":"ok"}}}); + let probe = if version == "Swagger 2.0" { + json!({"swagger":"2.0","info":{"title":"Parameter probe","version":"1"},"paths":{path:{"get":operation}}}) + } else { + json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Parameter probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{path:{"get":operation}}}) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + if row["status"] == "supported" { + let parameter = ir.operations[0] + .parameters + .iter() + .find(|parameter| parameter.name == "probe") + .unwrap(); + assert_eq!( + serde_json::to_value(parameter.location).unwrap(), + location, + "{version} {construct} location was changed" + ); + assert!( + parameter.schema.is_some(), + "{version} {construct} schema was lost" + ); + assert_eq!(parameter.required, location == "path"); + let preview = preview_from_ir(&ir); + let candidate = &preview.groups[0].operations[0]; + let expected_root = match location { + "path" => "$.request.path.", + "query" => "$.request.query.", + "header" => "$.request.headers.", + "cookie" => "$.request.cookies.", + _ => unreachable!(), + }; + assert!( + candidate + .draft + .input_mapping + .rules + .iter() + .any(|rule| rule.source == "$.mcp.probe" + && rule.target.starts_with(expected_root)), + "{version} {construct} input mapping missing {expected_root}: {:?}", + candidate.draft.input_mapping + ); + assert_actionable(&ir, &format!("{version} {construct}")); + } else { + let expected_finding = row["expected_finding"].as_str().unwrap(); + assert!( + ir.operations[0].findings.iter().any(|finding| finding.code + == expected_finding + && finding.severity == crank_import::rest::ImportFindingSeverity::Error + && finding.location.pointer == "/paths/~1probe/get/parameters/0"), + "{version} {construct} missing exact finding {expected_finding}" + ); + } + probed(&construct); + } + for media_type in [ + "application/json", + "application/x-www-form-urlencoded", + "multipart/form-data", + ] { + let construct = format!("request_body.{media_type}"); + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let operation = if version == "Swagger 2.0" { + let mut operation = + json!({"operationId":"bodyProbe","responses":{"200":{"description":"ok"}}}); + operation["consumes"] = json!([media_type]); + if media_type == "application/json" { + operation["parameters"] = json!([{"name":"body","in":"body","required":true,"schema":{"type":"object","properties":{"value":{"type":"string"}}}}]); + } else { + operation["parameters"] = + json!([{"name":"value","in":"formData","required":true,"type":"string"}]); + } + operation + } else { + json!({"operationId":"bodyProbe","requestBody":{"required":true,"content":{ + (media_type):{"schema":{"type":"object","properties":{"value":{"type":"string"}}}} + }},"responses":{"200":{"description":"ok"}}}) + }; + let probe = if version == "Swagger 2.0" { + json!({"swagger":"2.0","info":{"title":"Body probe","version":"1"},"paths":{"/probe":{"post":operation}}}) + } else { + json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Body probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"post":operation}}}) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + if row["status"] == "supported" { + let variant = ir.operations[0] + .request_body_variants + .iter() + .find(|variant| variant.content_type == media_type) + .expect("request body variant retained"); + assert!( + variant.schema.is_some(), + "{version} {construct} schema was lost: {:?}", + variant + ); + assert!( + variant.required, + "{version} {construct} required request body contract was lost: {variant:?}" + ); + let preview = preview_from_ir(&ir); + let contract = preview.groups[0].operations[0] + .draft + .target + .request_contract + .as_ref() + .expect("draft request contract"); + assert!(contract.body_required); + assert!( + contract + .variants + .iter() + .any(|variant| variant.media_type == media_type) + ); + assert_actionable(&ir, &format!("{version} {construct}")); + } else { + let expected_finding = row["expected_finding"].as_str().unwrap(); + assert!( + ir.operations[0].findings.iter().any(|finding| finding.code + == expected_finding + && finding.severity == crank_import::rest::ImportFindingSeverity::Error) + ); + } + probed(&construct); + } + for (construct, is_request) in [ + ("media_type.application_xml.request", true), + ("media_type.application_xml.response", false), + ] { + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let operation = if version == "Swagger 2.0" { + let mut operation = json!({"operationId":"xmlProbe","responses":{"200":{"description":"ok","schema":{"type":"string"}}}}); + if is_request { + operation["consumes"] = json!(["application/xml"]); + operation["parameters"] = json!([{"name":"body","in":"body","required":true,"schema":{"type":"string"}}]); + } else { + operation["produces"] = json!(["application/xml"]); + } + operation + } else if is_request { + json!({"operationId":"xmlProbe","requestBody":{"content":{"application/xml":{"schema":{"type":"string"}}}},"responses":{"200":{"description":"ok"}}}) + } else { + json!({"operationId":"xmlProbe","responses":{"200":{"description":"ok","content":{"application/xml":{"schema":{"type":"string"}}}}}}) + }; + let probe = if version == "Swagger 2.0" { + json!({"swagger":"2.0","info":{"title":"XML probe","version":"1"},"paths":{"/probe":{"post":operation}}}) + } else { + json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"XML probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"post":operation}}}) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + let expected_finding = row["expected_finding"].as_str().unwrap(); + assert_eq!(row["status"], "unsupported"); + let expected_pointer = if version == "Swagger 2.0" { + if is_request { + "/paths/~1probe/post/consumes/0" + } else { + "/paths/~1probe/post/produces/0" + } + } else if is_request { + "/paths/~1probe/post/requestBody/content/application~1xml" + } else { + "/paths/~1probe/post/responses/200/content/application~1xml" + }; + assert!( + ir.operations[0] + .findings + .iter() + .any(|finding| finding.code == expected_finding + && finding.severity == crank_import::rest::ImportFindingSeverity::Error + && finding.location.pointer == expected_pointer), + "{version} {construct} missing exact blocker {expected_finding}: {:?}", + ir.operations[0].findings + ); + probed(construct); + } + for composition in ["allOf", "oneOf", "anyOf"] { + let construct = format!("composition.{composition}"); + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let schema = json!({(composition):[{"type":"object","properties":{"id":{"type":"string"}}},{"type":"object","properties":{"name":{"type":"string"}}}]}); + let response = if version == "Swagger 2.0" { + json!({"description":"ok","schema":schema}) + } else { + json!({"description":"ok","content":{"application/json":{"schema":schema}}}) + }; + let probe = if version == "Swagger 2.0" { + json!({"swagger":"2.0","info":{"title":"Composition probe","version":"1"},"paths":{"/probe":{"get":{"operationId":"compositionProbe","responses":{"200":response}}}}}) + } else { + json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Composition probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"get":{"operationId":"compositionProbe","responses":{"200":response}}}}}) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source).unwrap(); + validate_normalized_ir(&ir).unwrap(); + assert_eq!(row["status"], "supported"); + assert!(ir.operations[0].response_variants[0].schema.as_ref().is_some_and(|schema| + if composition == "allOf" { + matches!(&schema.kind, NormalizedSchemaKind::Object { properties, .. } + if properties.contains_key("id") && properties.contains_key("name")) + } else { + matches!(&schema.kind, NormalizedSchemaKind::Composition { operator, variants } + if operator == composition && variants.len() == 2 + && matches!(&variants[0].kind, NormalizedSchemaKind::Object { properties, .. } + if properties.contains_key("id")) + && matches!(&variants[1].kind, NormalizedSchemaKind::Object { properties, .. } + if properties.contains_key("name"))) + }), + "{version} {construct} composition was not retained: {:?}", ir.operations[0].response_variants); + assert_eq!(row["expected_finding"], "none"); + assert_actionable(&ir, &format!("{version} {construct}")); + probed(&construct); + } + for external in [false, true] { + let construct = if external { + "reference.external_allowlisted" + } else { + "reference.local" + }; + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + let reference = if external { + "https://schemas.example.test/root.yaml#/Item" + } else if version == "Swagger 2.0" { + "#/definitions/Item" + } else { + "#/components/schemas/Item" + }; + let schema = json!({"$ref":reference}); + let response = if version == "Swagger 2.0" { + json!({"description":"ok","schema":schema}) + } else { + json!({"description":"ok","content":{"application/json":{"schema":schema}}}) + }; + let probe = if version == "Swagger 2.0" { + let mut root = json!({"swagger":"2.0","info":{"title":"Reference probe","version":"1"},"paths":{"/probe":{"get":{"operationId":"referenceProbe","responses":{"200":response}}}}}); + if !external { + root["definitions"] = + json!({"Item":{"type":"object","properties":{"id":{"type":"string"}}}}); + } + root + } else { + let mut root = json!({"openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Reference probe","version":"1"},"servers":[{"url":"https://matrix.example.test"}],"paths":{"/probe":{"get":{"operationId":"referenceProbe","responses":{"200":response}}}}}); + if !external { + root["components"] = json!({"schemas":{"Item":{"type":"object","properties":{"id":{"type":"string"}}}}}); + } + root + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = if external { + let external_source = + "Item: { type: object, properties: { id: { type: string } } }"; + let snapshot = crank_import::rest::ExternalDocumentSnapshot { + canonical_uri: "https://schemas.example.test/root.yaml".to_owned(), + digest: SourceDigest::parse(hex(external_source.as_bytes())).unwrap(), + document: external_source.to_owned(), + }; + let digest = SourceDigest::parse(hex(source.as_bytes())).unwrap(); + normalize_verified_bundle( + &source, + digest, + &[snapshot], + &NormalizationConfig::default(), + ) + .unwrap() + } else { + normalize(&source).unwrap() + }; + validate_normalized_ir(&ir).unwrap(); + assert_eq!(row["status"], "supported"); + assert!( + !ir.reference_graph.edges.is_empty(), + "{version} {construct} did not resolve through a source-backed edge" + ); + assert!( + ir.unresolved_references.is_empty(), + "{version} {construct} remains unresolved" + ); + assert!( + matches!( + ir.operations[0].response_variants[0].schema.as_ref().map(|schema| &schema.kind), + Some(NormalizedSchemaKind::Object { properties, .. }) if properties.contains_key("id") + ), + "{version} {construct} resolved schema was not projected into the operation" + ); + assert_actionable(&ir, &format!("{version} {construct}")); + assert_eq!(row["expected_finding"], "none"); + probed(construct); + } + for code in 200..300 { + let construct = format!("response.{code}"); + let response_row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap_or_else(|| panic!("{version} matrix misses {construct}")); + assert!( + ids.contains(&construct.as_str()), + "{version} matrix misses {construct}" + ); + assert_eq!(response_row["status"], "supported"); + assert_eq!(response_row["expected_finding"], "none"); + let has_body = !matches!(code, 204 | 205); + let source = if version == "Swagger 2.0" { + json!({ + "swagger":"2.0", "info":{"title":"Matrix probe","version":"1"}, + "paths":{"/probe":{"get":{"operationId":"matrixProbe","responses":{ + code.to_string(): if has_body {json!({"description":"success","schema":{"type":"object","properties":{"id":{"type":"string"}}}})} else {json!({"description":"success"})} + }}}} + }) + } else { + json!({ + "openapi": if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Matrix probe","version":"1"}, + "servers":[{"url":"https://matrix.example.test"}], + "paths":{"/probe":{"get":{"operationId":"matrixProbe","responses":{ + code.to_string(): if has_body {json!({"description":"success","content":{"application/json":{"schema":{"type":"object","properties":{"id":{"type":"string"}}}}}})} else {json!({"description":"success"})} + }}}} + }) + }; + let source = serde_json::to_string(&source).unwrap(); + let ir = normalize(&source) + .unwrap_or_else(|error| panic!("{version} {construct} probe rejected: {error}")); + validate_normalized_ir(&ir).unwrap_or_else(|error| { + panic!("{version} {construct} probe has invalid ledger: {error}") + }); + let response = ir.operations[0] + .response_variants + .iter() + .find(|variant| variant.status_code == code) + .unwrap_or_else(|| panic!("{version} {construct} was not preserved")); + assert_eq!( + response.schema.is_some(), + has_body, + "{version} {construct} body contract differs from 204/205 bodyless response semantics" + ); + assert!( + !ir.operations[0] + .findings + .iter() + .any(|finding| finding.severity + == crank_import::rest::ImportFindingSeverity::Error), + "{version} {construct} unexpectedly blocked: {:?}", + ir.operations[0].findings + ); + probed(&construct); + } + for construct in ["parameter.path.declaration", "schema.default.compound"] { + let row = rows + .iter() + .find(|row| { + row["specification_version"] == version && row["construct_id"] == construct + }) + .unwrap(); + assert_eq!(row["status"], "unsupported"); + let operation = if construct == "parameter.path.declaration" { + json!({ + "operationId":"pathDeclarationProbe", + "responses":{"200":{"description":"ok"}} + }) + } else { + let schema = json!({ + "type":"object", + "properties":{ + "compound":{"type":"object","default":{"nested":"value"}}, + "list":{"type":"array","items":{"type":"string"},"default":["a","b"]}, + "scalar":{"type":"string","default":"kept"} + } + }); + if version == "Swagger 2.0" { + json!({ + "operationId":"compoundDefaultProbe", + "responses":{"200":{"description":"ok","schema":schema}} + }) + } else { + json!({ + "operationId":"compoundDefaultProbe", + "responses":{"200":{"description":"ok","content":{"application/json":{"schema":schema}}}} + }) + } + }; + let path = if construct == "parameter.path.declaration" { + "/probe/{item_id}" + } else { + "/probe" + }; + let probe = if version == "Swagger 2.0" { + json!({ + "swagger":"2.0", "info":{"title":"Matrix probe","version":"1"}, + "paths":{path:{"get":operation}} + }) + } else { + json!({ + "openapi":if version == "OpenAPI 3.0" {"3.0.3"} else {"3.1.0"}, + "info":{"title":"Matrix probe","version":"1"}, + "servers":[{"url":"https://matrix.example.test"}], + "paths":{path:{"get":operation}} + }) + }; + let source = serde_json::to_string(&probe).unwrap(); + let ir = normalize(&source) + .unwrap_or_else(|error| panic!("{version} {construct} probe rejected: {error}")); + validate_normalized_ir(&ir).unwrap_or_else(|error| { + panic!("{version} {construct} probe has invalid ledger: {error}") + }); + let expected_finding = row["expected_finding"].as_str().unwrap(); + assert!( + ir.operations[0] + .findings + .iter() + .any(|finding| finding.code == expected_finding + && finding.severity == crank_import::rest::ImportFindingSeverity::Error), + "{version} {construct} lacks {expected_finding}: {:?}", + ir.operations[0].findings + ); + if construct == "schema.default.compound" { + assert_eq!( + ir.operations[0] + .findings + .iter() + .filter(|finding| finding.code == expected_finding) + .count(), + 2, + "{version} must block the object and array defaults" + ); + let schema = ir.operations[0].response_variants[0] + .schema + .as_ref() + .unwrap(); + assert!(matches!( + &schema.kind, + NormalizedSchemaKind::Object { properties, .. } + if matches!(&properties["scalar"].kind, + NormalizedSchemaKind::Scalar { default_value: Some(value), .. } + if value == &crank_import::rest::model::NormalizedLiteral::String("kept".to_owned())) + )); + } + probed(construct); + } + assert!( + unprobed.is_empty(), + "normative {version} rows lack an executed probe: {unprobed:?}" + ); + } +} diff --git a/crates/crank-import/tests/corpus/representative-00.json b/crates/crank-import/tests/corpus/representative-00.json new file mode 100644 index 0000000..c2054cc --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-00.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 00", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/0": { + "get": { + "operationId": "corpusOperation00", + "parameters": [ + { + "in": "query", + "name": "p00", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 00", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-01.json b/crates/crank-import/tests/corpus/representative-01.json new file mode 100644 index 0000000..4455caa --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-01.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 01", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/{p01}": { + "post": { + "operationId": "corpusOperation01", + "parameters": [ + { + "in": "path", + "name": "p01", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 01", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-02.json b/crates/crank-import/tests/corpus/representative-02.json new file mode 100644 index 0000000..5a920e1 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-02.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 02", + "version": "1.0" + }, + "paths": { + "/items/2": { + "put": { + "operationId": "corpusOperation02", + "parameters": [ + { + "in": "header", + "name": "p02", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "202": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 02", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-03.json b/crates/crank-import/tests/corpus/representative-03.json new file mode 100644 index 0000000..2c24ece --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-03.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 03", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/3": { + "patch": { + "operationId": "corpusOperation03", + "parameters": [ + { + "in": "cookie", + "name": "p03", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 03", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-04.json b/crates/crank-import/tests/corpus/representative-04.json new file mode 100644 index 0000000..6a26ba2 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-04.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 04", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/4": { + "delete": { + "operationId": "corpusOperation04", + "parameters": [ + { + "in": "query", + "name": "p04", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 04", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-05.json b/crates/crank-import/tests/corpus/representative-05.json new file mode 100644 index 0000000..f73fbdd --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-05.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 05", + "version": "1.0" + }, + "paths": { + "/items/{p05}": { + "head": { + "operationId": "corpusOperation05", + "parameters": [ + { + "in": "path", + "name": "p05", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 05", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-06.json b/crates/crank-import/tests/corpus/representative-06.json new file mode 100644 index 0000000..5feecad --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-06.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 06", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/6": { + "options": { + "operationId": "corpusOperation06", + "parameters": [ + { + "in": "header", + "name": "p06", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 06", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-07.json b/crates/crank-import/tests/corpus/representative-07.json new file mode 100644 index 0000000..910c286 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-07.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 07", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/7": { + "get": { + "operationId": "corpusOperation07", + "parameters": [ + { + "in": "cookie", + "name": "p07", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 07", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-08.json b/crates/crank-import/tests/corpus/representative-08.json new file mode 100644 index 0000000..e2a82e6 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-08.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 08", + "version": "1.0" + }, + "paths": { + "/items/8": { + "post": { + "operationId": "corpusOperation08", + "parameters": [ + { + "in": "query", + "name": "p08", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 08", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-09.json b/crates/crank-import/tests/corpus/representative-09.json new file mode 100644 index 0000000..1367954 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-09.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 09", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/{p09}": { + "put": { + "operationId": "corpusOperation09", + "parameters": [ + { + "in": "path", + "name": "p09", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 09", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-10.json b/crates/crank-import/tests/corpus/representative-10.json new file mode 100644 index 0000000..b71a9b9 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-10.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 10", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/10": { + "patch": { + "operationId": "corpusOperation10", + "parameters": [ + { + "in": "header", + "name": "p10", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 10", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-11.json b/crates/crank-import/tests/corpus/representative-11.json new file mode 100644 index 0000000..1f03774 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-11.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 11", + "version": "1.0" + }, + "paths": { + "/items/11": { + "delete": { + "operationId": "corpusOperation11", + "parameters": [ + { + "in": "header", + "name": "p11", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 11", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-12.json b/crates/crank-import/tests/corpus/representative-12.json new file mode 100644 index 0000000..6cb78a4 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-12.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 12", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/12": { + "head": { + "operationId": "corpusOperation12", + "parameters": [ + { + "in": "query", + "name": "p12", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 12", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-13.json b/crates/crank-import/tests/corpus/representative-13.json new file mode 100644 index 0000000..92e0088 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-13.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 13", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/{p13}": { + "options": { + "operationId": "corpusOperation13", + "parameters": [ + { + "in": "path", + "name": "p13", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 13", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-14.json b/crates/crank-import/tests/corpus/representative-14.json new file mode 100644 index 0000000..e8af539 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-14.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 14", + "version": "1.0" + }, + "paths": { + "/items/14": { + "get": { + "operationId": "corpusOperation14", + "parameters": [ + { + "in": "header", + "name": "p14", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 14", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-15.json b/crates/crank-import/tests/corpus/representative-15.json new file mode 100644 index 0000000..6a30322 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-15.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 15", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/15": { + "post": { + "operationId": "corpusOperation15", + "parameters": [ + { + "in": "cookie", + "name": "p15", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 15", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-16.json b/crates/crank-import/tests/corpus/representative-16.json new file mode 100644 index 0000000..88e11b5 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-16.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 16", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/16": { + "put": { + "operationId": "corpusOperation16", + "parameters": [ + { + "in": "query", + "name": "p16", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 16", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-17.json b/crates/crank-import/tests/corpus/representative-17.json new file mode 100644 index 0000000..a69109d --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-17.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 17", + "version": "1.0" + }, + "paths": { + "/items/{p17}": { + "patch": { + "operationId": "corpusOperation17", + "parameters": [ + { + "in": "path", + "name": "p17", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 17", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-18.json b/crates/crank-import/tests/corpus/representative-18.json new file mode 100644 index 0000000..381fe6c --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-18.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 18", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/18": { + "delete": { + "operationId": "corpusOperation18", + "parameters": [ + { + "in": "header", + "name": "p18", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 18", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-19.json b/crates/crank-import/tests/corpus/representative-19.json new file mode 100644 index 0000000..ea8aeee --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-19.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 19", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/19": { + "head": { + "operationId": "corpusOperation19", + "parameters": [ + { + "in": "cookie", + "name": "p19", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 19", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-20.json b/crates/crank-import/tests/corpus/representative-20.json new file mode 100644 index 0000000..55761d5 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-20.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 20", + "version": "1.0" + }, + "paths": { + "/items/20": { + "options": { + "operationId": "corpusOperation20", + "parameters": [ + { + "in": "query", + "name": "p20", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 20", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-21.json b/crates/crank-import/tests/corpus/representative-21.json new file mode 100644 index 0000000..9944d43 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-21.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 21", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/{p21}": { + "get": { + "operationId": "corpusOperation21", + "parameters": [ + { + "in": "path", + "name": "p21", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 21", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-22.json b/crates/crank-import/tests/corpus/representative-22.json new file mode 100644 index 0000000..26a8ea9 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-22.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 22", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/22": { + "post": { + "operationId": "corpusOperation22", + "parameters": [ + { + "in": "header", + "name": "p22", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 22", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-23.json b/crates/crank-import/tests/corpus/representative-23.json new file mode 100644 index 0000000..af3e70a --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-23.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 23", + "version": "1.0" + }, + "paths": { + "/items/23": { + "put": { + "operationId": "corpusOperation23", + "parameters": [ + { + "in": "header", + "name": "p23", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "204": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 23", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-24.json b/crates/crank-import/tests/corpus/representative-24.json new file mode 100644 index 0000000..52266d2 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-24.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 24", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/24": { + "patch": { + "operationId": "corpusOperation24", + "parameters": [ + { + "in": "query", + "name": "p24", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 24", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-25.json b/crates/crank-import/tests/corpus/representative-25.json new file mode 100644 index 0000000..62abb1a --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-25.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 25", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/{p25}": { + "delete": { + "operationId": "corpusOperation25", + "parameters": [ + { + "in": "path", + "name": "p25", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 25", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-26.json b/crates/crank-import/tests/corpus/representative-26.json new file mode 100644 index 0000000..68cc56c --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-26.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 26", + "version": "1.0" + }, + "paths": { + "/items/26": { + "head": { + "operationId": "corpusOperation26", + "parameters": [ + { + "in": "header", + "name": "p26", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 26", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-27.json b/crates/crank-import/tests/corpus/representative-27.json new file mode 100644 index 0000000..3ce0a9c --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-27.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 27", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/27": { + "options": { + "operationId": "corpusOperation27", + "parameters": [ + { + "in": "cookie", + "name": "p27", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 27", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-28.json b/crates/crank-import/tests/corpus/representative-28.json new file mode 100644 index 0000000..bb52153 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-28.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 28", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/28": { + "get": { + "operationId": "corpusOperation28", + "parameters": [ + { + "in": "query", + "name": "p28", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 28", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-29.json b/crates/crank-import/tests/corpus/representative-29.json new file mode 100644 index 0000000..5370da0 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-29.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 29", + "version": "1.0" + }, + "paths": { + "/items/{p29}": { + "post": { + "operationId": "corpusOperation29", + "parameters": [ + { + "in": "path", + "name": "p29", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "201": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 29", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-30.json b/crates/crank-import/tests/corpus/representative-30.json new file mode 100644 index 0000000..02788f8 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-30.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 30", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/30": { + "put": { + "operationId": "corpusOperation30", + "parameters": [ + { + "in": "header", + "name": "p30", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 30", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-31.json b/crates/crank-import/tests/corpus/representative-31.json new file mode 100644 index 0000000..2c14859 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-31.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 31", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/31": { + "patch": { + "operationId": "corpusOperation31", + "parameters": [ + { + "in": "cookie", + "name": "p31", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 31", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-32.json b/crates/crank-import/tests/corpus/representative-32.json new file mode 100644 index 0000000..48555d7 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-32.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 32", + "version": "1.0" + }, + "paths": { + "/items/32": { + "delete": { + "operationId": "corpusOperation32", + "parameters": [ + { + "in": "query", + "name": "p32", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 32", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-33.json b/crates/crank-import/tests/corpus/representative-33.json new file mode 100644 index 0000000..6151829 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-33.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 33", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/{p33}": { + "head": { + "operationId": "corpusOperation33", + "parameters": [ + { + "in": "path", + "name": "p33", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 33", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-34.json b/crates/crank-import/tests/corpus/representative-34.json new file mode 100644 index 0000000..57a7ecd --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-34.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 34", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/34": { + "options": { + "operationId": "corpusOperation34", + "parameters": [ + { + "in": "header", + "name": "p34", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 34", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-35.json b/crates/crank-import/tests/corpus/representative-35.json new file mode 100644 index 0000000..ac83d90 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-35.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 35", + "version": "1.0" + }, + "paths": { + "/items/35": { + "get": { + "operationId": "corpusOperation35", + "parameters": [ + { + "in": "header", + "name": "p35", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 35", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-36.json b/crates/crank-import/tests/corpus/representative-36.json new file mode 100644 index 0000000..137d91e --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-36.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 36", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/36": { + "post": { + "operationId": "corpusOperation36", + "parameters": [ + { + "in": "query", + "name": "p36", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 36", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-37.json b/crates/crank-import/tests/corpus/representative-37.json new file mode 100644 index 0000000..bc8a388 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-37.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 37", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/{p37}": { + "put": { + "operationId": "corpusOperation37", + "parameters": [ + { + "in": "path", + "name": "p37", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 37", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-38.json b/crates/crank-import/tests/corpus/representative-38.json new file mode 100644 index 0000000..294aa36 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-38.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 38", + "version": "1.0" + }, + "paths": { + "/items/38": { + "patch": { + "operationId": "corpusOperation38", + "parameters": [ + { + "in": "header", + "name": "p38", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "202": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 38", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-39.json b/crates/crank-import/tests/corpus/representative-39.json new file mode 100644 index 0000000..1eaaaad --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-39.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 39", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/39": { + "delete": { + "operationId": "corpusOperation39", + "parameters": [ + { + "in": "cookie", + "name": "p39", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 39", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-40.json b/crates/crank-import/tests/corpus/representative-40.json new file mode 100644 index 0000000..8b59a5c --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-40.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 40", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/40": { + "head": { + "operationId": "corpusOperation40", + "parameters": [ + { + "in": "query", + "name": "p40", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 40", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-41.json b/crates/crank-import/tests/corpus/representative-41.json new file mode 100644 index 0000000..5e3a966 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-41.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 41", + "version": "1.0" + }, + "paths": { + "/items/{p41}": { + "options": { + "operationId": "corpusOperation41", + "parameters": [ + { + "in": "path", + "name": "p41", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 41", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-42.json b/crates/crank-import/tests/corpus/representative-42.json new file mode 100644 index 0000000..528a306 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-42.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 42", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/42": { + "get": { + "operationId": "corpusOperation42", + "parameters": [ + { + "in": "header", + "name": "p42", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 42", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-43.json b/crates/crank-import/tests/corpus/representative-43.json new file mode 100644 index 0000000..52c6eae --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-43.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 43", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/43": { + "post": { + "operationId": "corpusOperation43", + "parameters": [ + { + "in": "cookie", + "name": "p43", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "204": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 43", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-44.json b/crates/crank-import/tests/corpus/representative-44.json new file mode 100644 index 0000000..bf9f1c8 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-44.json @@ -0,0 +1,67 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 44", + "version": "1.0" + }, + "paths": { + "/items/44": { + "put": { + "operationId": "corpusOperation44", + "parameters": [ + { + "in": "query", + "name": "p44", + "required": false, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 44", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-45.json b/crates/crank-import/tests/corpus/representative-45.json new file mode 100644 index 0000000..399589e --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-45.json @@ -0,0 +1,65 @@ +{ + "info": { + "title": "Synthetic corpus 45", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/{p45}": { + "patch": { + "operationId": "corpusOperation45", + "parameters": [ + { + "in": "path", + "name": "p45", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "properties": { + "name": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "required": true + }, + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 45", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-46.json b/crates/crank-import/tests/corpus/representative-46.json new file mode 100644 index 0000000..35cf339 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-46.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 46", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/46": { + "delete": { + "operationId": "corpusOperation46", + "parameters": [ + { + "in": "header", + "name": "p46", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "202": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 46", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-47.json b/crates/crank-import/tests/corpus/representative-47.json new file mode 100644 index 0000000..44c7cfc --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-47.json @@ -0,0 +1,52 @@ +{ + "basePath": "/v1", + "consumes": [ + "application/json" + ], + "host": "api.example.test", + "info": { + "title": "Synthetic corpus 47", + "version": "1.0" + }, + "paths": { + "/items/47": { + "head": { + "operationId": "corpusOperation47", + "parameters": [ + { + "in": "header", + "name": "p47", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "synthetic success", + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "summary": "Synthetic operation 47", + "tags": [ + "corpus" + ] + } + } + }, + "produces": [ + "application/json" + ], + "schemes": [ + "https" + ], + "swagger": "2.0" +} diff --git a/crates/crank-import/tests/corpus/representative-48.json b/crates/crank-import/tests/corpus/representative-48.json new file mode 100644 index 0000000..c3028e0 --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-48.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 48", + "version": "1.0" + }, + "openapi": "3.0.3", + "paths": { + "/items/48": { + "options": { + "operationId": "corpusOperation48", + "parameters": [ + { + "in": "query", + "name": "p48", + "required": false, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 48", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/corpus/representative-49.json b/crates/crank-import/tests/corpus/representative-49.json new file mode 100644 index 0000000..04b444b --- /dev/null +++ b/crates/crank-import/tests/corpus/representative-49.json @@ -0,0 +1,50 @@ +{ + "info": { + "title": "Synthetic corpus 49", + "version": "1.0" + }, + "openapi": "3.1.0", + "paths": { + "/items/{p49}": { + "get": { + "operationId": "corpusOperation49", + "parameters": [ + { + "in": "path", + "name": "p49", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "properties": { + "id": { + "type": "string" + } + }, + "type": "object" + } + } + }, + "description": "synthetic success" + } + }, + "summary": "Synthetic operation 49", + "tags": [ + "corpus" + ] + } + } + }, + "servers": [ + { + "url": "https://api.example.test/v1" + } + ] +} diff --git a/crates/crank-import/tests/decode_limits.rs b/crates/crank-import/tests/decode_limits.rs new file mode 100644 index 0000000..d9bbab4 --- /dev/null +++ b/crates/crank-import/tests/decode_limits.rs @@ -0,0 +1,372 @@ +//! Regression coverage for the bounded YAML/JSON materialization guard +//! (Gitea #87). +//! +//! Alias expansions are charged against an aggregate expanded string/key +//! budget *before* `serde_yaml` materializes an owned `Value`. The public +//! entry points must return the controlled `ImportParseError::LimitExceeded` +//! instead of materializing an oversized tree. Small, ordinary alias usage, +//! duplicate keys, non-finite numbers and legacy behaviour are unchanged. +//! +//! The fixtures here are deliberately modest (a 16 KiB scalar with eight +//! doubling levels expands to a few MiB, well under the risky 256 MiB +//! reproduction) so the suite is safe to run in-process. Exact boundary +//! arithmetic is covered by unit tests inside `decode_budget`. + +use crank_import::rest::{ + ExternalDocumentSnapshot, ImportParseError, NormalizationConfig, SourceDigest, + external_reference_uris, normalize_verified_bundle, normalize_verified_document, + preview_document_legacy_v1, reference_uris, validate_document_bundle_source_safe, + validate_document_source_safe, validate_resolved_bundle_source_safe, +}; + +fn digest(byte: char) -> SourceDigest { + SourceDigest::parse(byte.to_string().repeat(64)).expect("64-char digest is valid") +} + +/// Minimal valid OpenAPI 3.1 document with one GET operation. `extra` is +/// inserted at the root, ahead of `servers`, for extension fields. +fn valid_openapi(extra: &str) -> String { + format!( + "openapi: 3.1.0\n\ + info: {{ title: Alias budget, version: '1' }}\n\ + {extra}\ + servers: [{{ url: https://api.example.test }}]\n\ + paths:\n\ + \x20 /items:\n\ + \x20 get:\n\ + \x20 operationId: listItems\n\ + \x20 responses:\n\ + \x20 '200': {{ description: ok }}\n" + ) +} + +/// Root extension whose value doubles the previous alias `levels` times, +/// starting from a 16 KiB scalar. Every per-item default limit is respected; +/// only the aggregate materialized size grows. +fn doubling_alias_document(levels: usize) -> String { + let scalar = "a".repeat(16 * 1024); + let mut extra = format!("x-expansion-0: &expansion0 \"{scalar}\"\n"); + for level in 1..levels { + let previous = level - 1; + extra.push_str(&format!( + "x-expansion-{level}: &expansion{level} [*expansion{previous}, *expansion{previous}]\n" + )); + } + let previous = levels - 1; + extra.push_str(&format!( + "x-expansion: &expansion{levels} [*expansion{previous}, *expansion{previous}]\n" + )); + valid_openapi(&extra) +} + +/// A compact alias chain for configurable boundary tests. The top-level value +/// repeats the scalar `2^levels` times through aliases. +fn alias_chain(scalar_len: usize, levels: usize) -> String { + let scalar = "b".repeat(scalar_len); + let mut document = format!("k0: &a0 \"{scalar}\"\n"); + for level in 1..=levels { + let previous = level - 1; + document.push_str(&format!( + "k{level}: &a{level} [*a{previous}, *a{previous}]\n" + )); + } + document +} + +/// Ordinary, small alias usage that must remain accepted. +fn small_alias_document() -> String { + valid_openapi( + "x-anchor: &anchor { type: string }\n\ + x-aliases: [*anchor, *anchor, *anchor]\n", + ) +} + +const LEVELS: usize = 8; + +#[test] +fn primary_source_safety_rejects_doubling_aliases_over_aggregate_budget() { + let document = doubling_alias_document(LEVELS); + assert_eq!( + validate_document_source_safe(&document, &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn bundle_source_safety_rejects_doubling_aliases_over_aggregate_budget() { + let document = doubling_alias_document(LEVELS); + assert_eq!( + validate_document_bundle_source_safe(&[document.as_str()], &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn resolved_source_safety_rejects_doubling_aliases_over_aggregate_budget() { + let document = doubling_alias_document(LEVELS); + assert_eq!( + validate_resolved_bundle_source_safe( + &document, + digest('a'), + &[], + &NormalizationConfig::default() + ), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn normalization_rejects_doubling_aliases_over_aggregate_budget() { + let document = doubling_alias_document(LEVELS); + assert!(matches!( + normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + )); +} + +#[test] +fn primary_reference_uri_scan_rejects_doubling_aliases_over_aggregate_budget() { + let document = doubling_alias_document(LEVELS); + assert_eq!( + reference_uris(&document, &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn external_reference_uri_scan_rejects_doubling_aliases_over_aggregate_budget() { + let document = doubling_alias_document(LEVELS); + assert_eq!( + external_reference_uris(&document, &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn supplied_external_snapshot_rejects_doubling_aliases_over_aggregate_budget() { + let primary = valid_openapi(""); + let snapshot = ExternalDocumentSnapshot { + canonical_uri: "https://schemas.example.test/root.yaml".to_owned(), + digest: digest('b'), + document: doubling_alias_document(LEVELS), + }; + assert!(matches!( + normalize_verified_bundle( + &primary, + digest('a'), + &[snapshot], + &NormalizationConfig::default() + ), + Err(ImportParseError::LimitExceeded) + )); +} + +#[test] +fn resolved_source_safety_rejects_doubling_aliases_in_supplied_snapshot() { + let primary = valid_openapi(""); + let snapshot = ExternalDocumentSnapshot { + canonical_uri: "https://schemas.example.test/root.yaml".to_owned(), + digest: digest('b'), + document: doubling_alias_document(LEVELS), + }; + assert_eq!( + validate_resolved_bundle_source_safe( + &primary, + digest('a'), + &[snapshot], + &NormalizationConfig::default() + ), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn small_aliases_remain_accepted_across_public_entry_points() { + let document = small_alias_document(); + let config = NormalizationConfig::default(); + + assert_eq!(validate_document_source_safe(&document, &config), Ok(())); + assert_eq!( + validate_document_bundle_source_safe(&[document.as_str()], &config), + Ok(()) + ); + assert_eq!( + validate_resolved_bundle_source_safe(&document, digest('a'), &[], &config), + Ok(()) + ); + assert!(normalize_verified_document(&document, digest('a'), &config).is_ok()); + assert_eq!(reference_uris(&document, &config), Ok(Vec::new())); + assert_eq!(external_reference_uris(&document, &config), Ok(Vec::new())); +} + +#[test] +fn duplicate_keys_are_still_rejected_as_invalid_document() { + let document = valid_openapi("x-dup: 1\nx-dup: 2\n"); + assert_eq!( + validate_document_source_safe(&document, &NormalizationConfig::default()), + Err(ImportParseError::InvalidDocument) + ); +} + +#[test] +fn non_finite_yaml_scalars_are_still_rejected_as_invalid_document() { + let document = valid_openapi("x-nonfinite: .inf\n"); + assert_eq!( + validate_document_source_safe(&document, &NormalizationConfig::default()), + Err(ImportParseError::InvalidDocument) + ); +} + +#[test] +fn alias_expansion_below_boundary_is_accepted_and_above_is_rejected() { + // allowance = 16 * max(max_bytes, max_scalar_bytes) = 16 * 4096 = 65536. + let config = NormalizationConfig { + max_bytes: 4096, + max_scalar_bytes: 64, + ..NormalizationConfig::default() + }; + let below = alias_chain(60, 9); // ~61_400 expanded string bytes + let above = alias_chain(60, 10); // ~122_800 expanded string bytes + assert!( + below.len() <= config.max_bytes, + "fixture must fit raw limit" + ); + assert!( + above.len() <= config.max_bytes, + "fixture must fit raw limit" + ); + + assert_eq!(reference_uris(&below, &config), Ok(Vec::new())); + assert_eq!( + reference_uris(&above, &config), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn ordinary_alias_output_matches_expanded_equivalent() { + let aliased = valid_openapi("x-shared: &shared { type: string }\nx-copy: *shared\n"); + let expanded = valid_openapi("x-shared: { type: string }\nx-copy: { type: string }\n"); + let config = NormalizationConfig::default(); + let aliased = normalize_verified_document(&aliased, digest('a'), &config) + .expect("aliased fixture parses"); + let expanded = normalize_verified_document(&expanded, digest('a'), &config) + .expect("expanded fixture parses"); + assert_eq!(aliased, expanded); +} + +#[test] +fn legacy_duplicate_keys_keep_historical_invalid_document() { + // The legacy `serde_yaml::Value` mapping rejects duplicate keys, the same + // as the strict decoder; the preflight must not change that. + let document = "openapi: 3.0.3\n\ + info: { title: Legacy, version: '1' }\n\ + info: { title: Second, version: '1' }\n\ + paths:\n\ + \x20 /x:\n\ + \x20 get:\n\ + \x20 responses:\n\ + \x20 '200': { description: ok }\n"; + assert_eq!( + preview_document_legacy_v1(document), + Err(ImportParseError::InvalidDocument) + ); +} + +#[test] +fn legacy_tagged_extension_keeps_historical_mapping_conversion() { + let document = "openapi: 3.0.3\n\ + info: { title: Legacy tag, version: '1' }\n\ + x-tagged: !Thing value\n\ + paths:\n\ + \x20 /x:\n\ + \x20 get:\n\ + \x20 responses:\n\ + \x20 '200': { description: ok }\n"; + assert!(preview_document_legacy_v1(document).is_ok()); +} + +#[test] +fn legacy_non_finite_numbers_keep_historical_null_conversion() { + // The legacy path converts through `serde_yaml::Value` and then JSON, so a + // non-finite number becomes `null` and stays accepted; the strict decoder + // rejects it (covered above). The permissive preflight must preserve this. + let document = "openapi: 3.0.3\n\ + info: { title: Legacy non-finite, version: '1' }\n\ + x-nonfinite: .inf\n\ + paths:\n\ + \x20 /x:\n\ + \x20 get:\n\ + \x20 responses:\n\ + \x20 '200': { description: ok }\n"; + assert!(preview_document_legacy_v1(document).is_ok()); +} + +fn snapshot(document: &str) -> ExternalDocumentSnapshot { + ExternalDocumentSnapshot { + canonical_uri: "https://schemas.example.test/root.yaml".to_owned(), + digest: digest('b'), + document: document.to_owned(), + } +} + +#[test] +fn raw_bundle_document_count_is_capped_before_preflight() { + // Without the early count guard the empty documents would fail later as + // InvalidDocument; the explicit cap must win with LimitExceeded. + let config = NormalizationConfig { + max_reference_documents: 1, + ..NormalizationConfig::default() + }; + let documents = ["", "", ""]; + assert_eq!( + validate_document_bundle_source_safe(&documents, &config), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn resolved_bundle_snapshot_count_is_capped_before_preflight() { + let config = NormalizationConfig { + max_reference_documents: 0, + ..NormalizationConfig::default() + }; + let primary = valid_openapi(""); + assert_eq!( + validate_resolved_bundle_source_safe(&primary, digest('a'), &[snapshot("{}")], &config), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn bundle_snapshot_raw_bytes_and_aliases_are_guarded_before_preflight() { + let primary = valid_openapi(""); + let bytes_config = NormalizationConfig { + max_external_document_bytes: 4, + ..NormalizationConfig::default() + }; + assert!(matches!( + normalize_verified_bundle( + &primary, + digest('a'), + &[snapshot("{\"a\":1}")], + &bytes_config + ), + Err(ImportParseError::LimitExceeded) + )); + + let alias_config = NormalizationConfig { + max_aliases: 8, + ..NormalizationConfig::default() + }; + let aliased = "a: &x 1\nb: [*x, *x, *x, *x, *x, *x, *x, *x, *x]\n"; + assert_eq!( + validate_resolved_bundle_source_safe( + &primary, + digest('a'), + &[snapshot(aliased)], + &alias_config + ), + Err(ImportParseError::LimitExceeded) + ); +} diff --git a/crates/crank-import/tests/http_contract_regressions.rs b/crates/crank-import/tests/http_contract_regressions.rs index c1d7966..ab383f7 100644 --- a/crates/crank-import/tests/http_contract_regressions.rs +++ b/crates/crank-import/tests/http_contract_regressions.rs @@ -1,5 +1,5 @@ use crank_core::HttpMethod; -use crank_import::rest::model::NormalizedScalarKind; +use crank_import::rest::model::{NormalizedFinding, NormalizedLiteral, NormalizedScalarKind}; use crank_import::rest::{ ImportFindingSeverity, NormalizationConfig, NormalizedSchemaKind, SourceDigest, normalize_verified_document, validate_normalized_ir, @@ -14,6 +14,217 @@ fn normalize(document: &str) -> crank_import::rest::NormalizedIr { .unwrap() } +#[test] +fn undeclared_path_placeholders_are_exact_blockers_in_all_supported_formats() { + let openapi31 = r#" +openapi: 3.1.0 +info: {title: Missing path declaration, version: '1'} +servers: [{url: https://example.test}] +paths: + /items/{item_id}: + get: + operationId: getItem + responses: {'200': {description: ok}} +"#; + let openapi30 = openapi31.replace("3.1.0", "3.0.3"); + let swagger2 = r#" +swagger: '2.0' +info: {title: Missing path declaration, version: '1'} +host: example.test +schemes: [https] +paths: + /items/{item_id}: + get: + operationId: getItem + responses: {'200': {description: ok}} +"#; + let documents = [ + ("OpenAPI 3.1", openapi31), + ("OpenAPI 3.0", openapi30.as_str()), + ("Swagger 2.0", swagger2), + ]; + + for (version, document) in documents { + let ir = normalize(document); + validate_normalized_ir(&ir).expect("the source-attested path blocker must validate"); + let operation = &ir.operations[0]; + assert!( + operation.findings.iter().any(|finding| { + finding.code == "path_parameter_not_declared" + && finding.severity == ImportFindingSeverity::Error + && finding.operation_key.as_deref() == Some("GET /items/{item_id}") + && finding.location.pointer == "/paths/~1items~1{item_id}/get" + }), + "{version} must report an undeclared path placeholder at the existing operation node: {:?}", + operation.findings + ); + } + + let declared = r#" +openapi: 3.1.0 +info: {title: Declared path parameter, version: '1'} +servers: [{url: https://example.test}] +paths: + /items/{item_id}: + get: + operationId: getItem + parameters: + - {name: item_id, in: path, required: true, schema: {type: string}} + responses: {'200': {description: ok}} +"#; + let mut ir = normalize(declared); + let operation = &mut ir.operations[0]; + operation.findings.push(NormalizedFinding { + code: "path_parameter_not_declared".to_owned(), + severity: ImportFindingSeverity::Error, + message: "fabricated blocker".to_owned(), + construct_id: operation.stable_id.clone(), + location: operation.location.clone(), + operation_key: Some(operation.key.clone()), + }); + assert!( + validate_normalized_ir(&ir).is_err(), + "a path blocker must not validate when the canonical source declares the placeholder" + ); + + let referenced = r#" +openapi: 3.1.0 +info: {title: Referenced path parameter, version: '1'} +servers: [{url: https://example.test}] +paths: + /items/{item_id}: + get: + operationId: getItem + parameters: [{$ref: '#/components/parameters/ItemId'}] + responses: {'200': {description: ok}} +components: + parameters: + ItemId: {name: item_id, in: path, required: true, schema: {type: string}} +"#; + let mut ir = normalize(referenced); + validate_normalized_ir(&ir).expect("a resolved referenced path parameter must validate"); + let operation = &mut ir.operations[0]; + assert!(operation.parameters.iter().any(|parameter| { + parameter.name == "item_id" + && parameter.location == crank_import::rest::model::RestParameterLocation::Path + })); + operation.findings.push(NormalizedFinding { + code: "path_parameter_not_declared".to_owned(), + severity: ImportFindingSeverity::Error, + message: "fabricated blocker".to_owned(), + construct_id: operation.stable_id.clone(), + location: operation.location.clone(), + operation_key: Some(operation.key.clone()), + }); + assert!( + validate_normalized_ir(&ir).is_err(), + "a blocker must not validate when a referenced parameter is effective" + ); +} + +#[test] +fn compound_schema_defaults_are_blocked_without_changing_scalar_defaults() { + let openapi31 = r#" +openapi: 3.1.0 +info: {title: Compound defaults, version: '1'} +servers: [{url: https://example.test}] +paths: + /defaults: + get: + operationId: getDefaults + responses: + '200': + description: ok + content: + application/json: + schema: + type: object + properties: + compound: {type: object, default: {nested: value}} + list: {type: array, items: {type: string}, default: [a, b]} + scalar: {type: string, default: original} +"#; + let openapi30 = openapi31.replace("3.1.0", "3.0.3"); + let swagger2 = r#" +swagger: '2.0' +info: {title: Compound defaults, version: '1'} +host: example.test +schemes: [https] +paths: + /defaults: + get: + operationId: getDefaults + responses: + '200': + description: ok + schema: + type: object + properties: + compound: {type: object, default: {nested: value}} + list: {type: array, items: {type: string}, default: [a, b]} + scalar: {type: string, default: original} +"#; + let documents = [ + ("OpenAPI 3.1", openapi31), + ("OpenAPI 3.0", openapi30.as_str()), + ("Swagger 2.0", swagger2), + ]; + + for (version, document) in documents { + let ir = normalize(document); + validate_normalized_ir(&ir).expect("source-attested compound blockers must validate"); + let operation = &ir.operations[0]; + let mut blockers = operation + .findings + .iter() + .filter(|finding| finding.code == "unsupported_compound_default") + .collect::>(); + blockers.sort_by(|left, right| left.location.pointer.cmp(&right.location.pointer)); + assert_eq!( + blockers.len(), + 2, + "{version} should block only object/array defaults: {:?}", + operation.findings + ); + assert!(blockers.iter().all(|finding| { + finding.severity == ImportFindingSeverity::Error + && finding.location.pointer.ends_with("/default") + && finding.operation_key.as_deref() == Some("GET /defaults") + })); + let Some(NormalizedSchemaKind::Object { properties, .. }) = operation + .response_variants + .first() + .and_then(|variant| variant.schema.as_ref()) + .map(|schema| &schema.kind) + else { + panic!("{version} response schema should remain an object"); + }; + assert!(matches!( + &properties["scalar"].kind, + NormalizedSchemaKind::Scalar { default_value: Some(value), .. } + if value == &NormalizedLiteral::String("original".to_owned()) + )); + + let mut tampered = ir.clone(); + let operation = &mut tampered.operations[0]; + let scalar_default = operation + .findings + .iter_mut() + .find(|finding| finding.code == "unsupported_compound_default") + .unwrap(); + let scalar_pointer = if version == "Swagger 2.0" { + "/paths/~1defaults/get/responses/200/schema/properties/scalar/default" + } else { + "/paths/~1defaults/get/responses/200/content/application~1json/schema/properties/scalar/default" + }; + scalar_default.location.pointer = scalar_pointer.to_owned(); + assert!( + validate_normalized_ir(&tampered).is_err(), + "{version} must reject a compound-default finding moved onto a scalar source value" + ); + } +} + #[test] fn variants_keep_exact_schema_coverage_and_reject_tampered_provenance() { let document = r#" diff --git a/crates/crank-import/tests/path_parameter_inheritance.rs b/crates/crank-import/tests/path_parameter_inheritance.rs new file mode 100644 index 0000000..df940d5 --- /dev/null +++ b/crates/crank-import/tests/path_parameter_inheritance.rs @@ -0,0 +1,67 @@ +use crank_import::rest::{NormalizationConfig, SourceDigest, normalize_verified_document}; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; + +#[test] +fn path_item_parameters_are_inherited_and_operation_parameters_override_by_name_and_location() { + for version in ["3.0.3", "3.1.0", "2.0"] { + let parameter = |name: &str, location: &str, kind: &str| { + if version == "2.0" { + json!({"name":name,"in":location,"required":location == "path","type":kind}) + } else { + json!({"name":name,"in":location,"required":location == "path","schema":{"type":kind}}) + } + }; + let mut document = if version == "2.0" { + json!({"swagger":"2.0","info":{"title":"Path parameters","version":"1"}}) + } else { + json!({"openapi":version,"info":{"title":"Path parameters","version":"1"},"servers":[{"url":"https://example.test"}]}) + }; + document["paths"] = json!({ + "/items/{id}": { + "parameters": [parameter("id", "path", "string"), parameter("filter", "query", "string")], + "get": {"operationId":"readItem","responses":{"200":{"description":"ok"}}}, + "post": {"operationId":"updateItem","parameters":[parameter("filter", "query", "integer")], + "responses":{"200":{"description":"ok"}}} + } + }); + let source = serde_json::to_string(&document).unwrap(); + let digest = + SourceDigest::parse(format!("{:x}", Sha256::digest(source.as_bytes()))).unwrap(); + let ir = + normalize_verified_document(&source, digest, &NormalizationConfig::default()).unwrap(); + for (key, expected_kind) in [ + ("GET /items/{id}", "string"), + ("POST /items/{id}", "integer"), + ] { + let operation = ir + .operations + .iter() + .find(|operation| operation.key == key) + .unwrap(); + assert_eq!( + operation.parameters.len(), + 2, + "{version} {key}: {:?}", + operation.parameters + ); + let id = operation + .parameters + .iter() + .find(|parameter| parameter.name == "id") + .unwrap(); + assert_eq!(id.location, crank_import::rest::RestParameterLocation::Path); + let filter = operation + .parameters + .iter() + .find(|parameter| parameter.name == "filter") + .unwrap(); + let schema = serde_json::to_value(filter.schema.as_ref().unwrap()).unwrap(); + assert_eq!( + schema["kind"]["scalar_type"], + Value::String(expected_kind.to_owned()), + "{version} {key}: {schema}" + ); + } + } +} diff --git a/crates/crank-import/tests/preview.rs b/crates/crank-import/tests/preview.rs index 9edde77..13db186 100644 --- a/crates/crank-import/tests/preview.rs +++ b/crates/crank-import/tests/preview.rs @@ -96,6 +96,19 @@ definitions: assert_eq!(operation.draft.input_mapping.rules.len(), 2); } + #[test] + fn historical_preview_without_locations_still_deserializes() { + let old = serde_json::to_value(preview_document_legacy_v1(OPENAPI3).unwrap()).unwrap(); + let replay: crank_import::rest::ImportPreview = serde_json::from_value(old).unwrap(); + assert!(replay.groups[0].operations[0].source_location.is_none()); + assert!( + replay.groups[0].operations[0] + .findings + .iter() + .all(|finding| finding.source_location.is_none()) + ); + } + #[test] fn previews_swagger2_and_resolves_local_definitions() { let preview = preview_document(SWAGGER2).unwrap(); @@ -511,6 +524,26 @@ paths: assert!(codes.contains(&"missing_summary")); assert!(codes.contains(&"missing_description")); assert!(codes.contains(&"missing_response_schema")); + assert_eq!( + operation + .source_location + .as_ref() + .map(|location| location.pointer.as_str()), + Some("/paths/~1items/get") + ); + assert!(operation.findings.iter().any(|finding| { + finding.code == "missing_operation_id" + && finding + .source_location + .as_ref() + .is_some_and(|location| location.pointer == "/paths/~1items/get/operationId") + })); + assert!( + operation + .findings + .iter() + .all(|finding| finding.source_location.is_some()) + ); } #[test] @@ -656,6 +689,13 @@ paths: .filter(|finding| finding.code == "unsupported_http_method") .count(); assert_eq!(unsupported, 2); + assert!(preview.findings.iter().any(|finding| { + finding.code == "unsupported_http_method" + && finding + .source_location + .as_ref() + .is_some_and(|location| location.pointer == "/paths/~1health/trace") + })); } #[test] diff --git a/crates/crank-import/tests/reference_memory_limits.rs b/crates/crank-import/tests/reference_memory_limits.rs new file mode 100644 index 0000000..90d42e3 --- /dev/null +++ b/crates/crank-import/tests/reference_memory_limits.rs @@ -0,0 +1,174 @@ +//! Regressions for the `$ref` expansion aggregate string/key byte budget +//! (Gitea #88). +//! +//! `reference::resolve` clones the referenced target subtree per `$ref` +//! occurrence. Many individually small (but not tiny) targets therefore +//! multiply into a large materialized tree while staying under +//! `max_references`, `max_expanded_nodes`, `max_nodes`, `max_collection_items` +//! and `max_scalar_bytes`; a bounded probe reproduced a child address-space +//! abort for such an input. The aggregate copy budget must reject these with +//! `ImportParseError::LimitExceeded` before the copies allocate, for both local +//! and supplied external references, while small ordinary usage keeps working. +//! +//! The fixture is deliberately modest (16 KiB payload, 400 references → ≈6.4 +//! MiB expanded, far below the risky 256 MiB reproduction and below 20 MiB). + +use crank_import::rest::{ + ExternalDocumentSnapshot, ImportParseError, NormalizationConfig, SourceDigest, + normalize_verified_document, validate_resolved_bundle_source_safe, +}; +use serde_json::{Map, Value, json}; + +const DESCRIPTION_BYTES: usize = 16 * 1024; +const EXPANDING_REFS: usize = 400; +const EXTERNAL_URI: &str = "https://schemas.example.test/root.yaml"; + +// The fixture must stay comfortably below the unsafe reproduction size. +const _: () = assert!(DESCRIPTION_BYTES * EXPANDING_REFS < 20 * 1024 * 1024); + +fn digest(byte: char) -> SourceDigest { + SourceDigest::parse(byte.to_string().repeat(64)).expect("64-char digest is valid") +} + +fn big_schema() -> Value { + json!({ + "type": "object", + "description": "x".repeat(DESCRIPTION_BYTES), + "properties": { "id": { "type": "string" } } + }) +} + +fn big_schema_with_large_property_key() -> Value { + let mut properties = Map::new(); + properties.insert("k".repeat(DESCRIPTION_BYTES), json!({ "type": "string" })); + json!({ "type": "object", "properties": Value::Object(properties) }) +} + +fn document_with_refs(ref_uri: &str, ref_count: usize) -> String { + let all_of: Vec = (0..ref_count).map(|_| json!({ "$ref": ref_uri })).collect(); + let document = json!({ + "openapi": "3.1.0", + "info": { "title": "Ref budget", "version": "1" }, + "servers": [{ "url": "https://api.example.test" }], + "paths": { + "/items": { + "get": { + "operationId": "listItems", + "responses": { + "200": { + "description": "ok", + "content": { + "application/json": { + "schema": { "allOf": all_of } + } + } + } + } + } + } + } + }); + serde_json::to_string(&document).expect("document serializes") +} + +fn local_ref_document(ref_count: usize) -> String { + local_ref_document_with(big_schema(), ref_count) +} + +fn local_ref_document_with(schema: Value, ref_count: usize) -> String { + let mut document: Value = + serde_json::from_str(&document_with_refs("#/components/schemas/Big", ref_count)) + .expect("valid JSON"); + document["components"] = json!({ "schemas": { "Big": schema } }); + serde_json::to_string(&document).expect("document serializes") +} + +fn external_ref_document(ref_count: usize) -> String { + let uri = format!("{EXTERNAL_URI}#/Big"); + document_with_refs(&uri, ref_count) +} + +fn external_snapshot() -> ExternalDocumentSnapshot { + let document = + serde_json::to_string(&json!({ "Big": big_schema() })).expect("snapshot serializes"); + ExternalDocumentSnapshot { + canonical_uri: EXTERNAL_URI.to_owned(), + digest: digest('b'), + document, + } +} + +fn assert_fits_raw_limit(document: &str) { + assert!( + document.len() < NormalizationConfig::default().max_bytes, + "fixture must stay under the raw byte limit, got {} bytes", + document.len() + ); +} + +#[test] +fn local_ref_expansion_over_aggregate_budget_is_rejected() { + let document = local_ref_document(EXPANDING_REFS); + assert_fits_raw_limit(&document); + assert!(matches!( + normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + )); +} + +#[test] +fn local_ref_expansion_counts_large_object_keys() { + let document = local_ref_document_with(big_schema_with_large_property_key(), EXPANDING_REFS); + assert_fits_raw_limit(&document); + assert!(matches!( + normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()), + Err(ImportParseError::LimitExceeded) + )); +} + +#[test] +fn supplied_external_ref_expansion_over_aggregate_budget_is_rejected() { + let primary = external_ref_document(EXPANDING_REFS); + assert_fits_raw_limit(&primary); + assert_eq!( + validate_resolved_bundle_source_safe( + &primary, + digest('a'), + &[external_snapshot()], + &NormalizationConfig::default() + ), + Err(ImportParseError::LimitExceeded) + ); +} + +#[test] +fn a_single_local_ref_stays_within_budget() { + let document = local_ref_document(1); + let ir = normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()) + .expect("a single reference must stay within the expanded budget"); + assert_eq!(ir.operations.len(), 1); + assert_eq!(ir.reference_graph.edges.len(), 1); +} + +#[test] +fn several_local_refs_stay_within_budget() { + let document = local_ref_document(4); + assert!( + normalize_verified_document(&document, digest('a'), &NormalizationConfig::default()) + .is_ok() + ); +} + +#[test] +fn a_single_supplied_external_ref_stays_within_budget() { + let primary = external_ref_document(1); + assert_eq!( + validate_resolved_bundle_source_safe( + &primary, + digest('a'), + &[external_snapshot()], + &NormalizationConfig::default() + ), + Ok(()) + ); +} diff --git a/crates/crank-import/tests/reference_resolution.rs b/crates/crank-import/tests/reference_resolution.rs index 0edfaec..ea0e398 100644 --- a/crates/crank-import/tests/reference_resolution.rs +++ b/crates/crank-import/tests/reference_resolution.rs @@ -1,7 +1,7 @@ use crank_import::rest::{ - ExternalDocumentSnapshot, ImportFindingSeverity, NormalizationConfig, NormalizedSchemaKind, - SourceDigest, external_reference_uris, normalize_verified_bundle, normalize_verified_document, - preview_from_ir, reference_uris, + ExternalDocumentSnapshot, ImportFindingSeverity, ImportParseError, NormalizationConfig, + NormalizedSchemaKind, SourceDigest, external_reference_uris, normalize_verified_bundle, + normalize_verified_document, preview_from_ir, reference_uris, }; fn digest(byte: char) -> SourceDigest { @@ -189,6 +189,162 @@ paths: assert!(first.unresolved_references.is_empty()); } +#[test] +fn rejects_duplicate_keys_in_external_snapshot_json_and_yaml_fallback() { + let document = r#" +openapi: 3.1.0 +info: { title: Duplicate snapshot keys } +servers: [{ url: https://api.example.test }] +paths: + /items: + get: + operationId: listItems + responses: + '200': + description: ok + content: { application/json: { schema: { $ref: 'https://schemas.example.test/root.yaml#/Item' } } } +"#; + let snapshots = [ + r#"{"Item":{"type":"string","type":"integer"}}"#, + "Item: { type: string }\nItem: { type: integer }", + // JSON flow syntax is also accepted by the YAML fallback, so a strict + // JSON duplicate failure must not become a last-key-wins YAML parse. + r#"{"Item":{"type":"string","type":"integer"},"title":"snapshot"}"#, + ]; + + for snapshot in snapshots { + let result = normalize_verified_bundle( + document, + digest('a'), + &[ExternalDocumentSnapshot { + canonical_uri: "https://schemas.example.test/root.yaml".to_owned(), + digest: digest('b'), + document: snapshot.to_owned(), + }], + &NormalizationConfig::default(), + ); + assert!( + result.is_err(), + "duplicate snapshot keys must fail closed: {snapshot}" + ); + } +} + +#[test] +fn rejects_duplicate_keys_in_new_primary_json_documents() { + let document = r#"{"openapi":"3.1.0","info":{"title":"duplicates","version":"1"},"servers":[{"url":"https://api.example.test"}],"paths":{"/items":{"get":{"operationId":"firstName","operationId":"lastName","responses":{"200":{"description":"ok"}}}}}}"#; + assert!(matches!( + normalize_verified_document(document, digest('a'), &NormalizationConfig::default()), + Err(ImportParseError::InvalidDocument) + )); +} + +#[test] +fn strict_primary_decoder_preserves_yaml_flow_syntax() { + let document = "openapi: 3.1.0\ninfo: { title: Flow, version: '1' }\nservers: [{ url: https://api.example.test }]\npaths: { /items: { get: { operationId: listItems, responses: { '200': { description: ok } } } } }\n"; + assert!( + normalize_verified_document(document, digest('a'), &NormalizationConfig::default()).is_ok() + ); +} + +#[test] +fn uppercase_external_scheme_is_classified_as_external_reference() { + let document = r#" +openapi: 3.1.0 +info: { title: Uppercase external } +servers: [{ url: https://api.example.test }] +paths: + /items: + get: + operationId: listItems + responses: + '200': + description: ok + content: { application/json: { schema: { $ref: 'HTTPS://missing.example/schema.json' } } } +"#; + let ir = normalize(document); + assert!( + ir.operations[0] + .findings + .iter() + .any(|finding| finding.code == "external_reference_disabled"), + "uppercase HTTPS ref must be classified as an external reference, got: {:?}", + ir.operations[0].findings + ); +} + +#[test] +fn enabled_uppercase_https_ref_with_fragment_is_unavailable() { + let document = r#" +openapi: 3.1.0 +info: { title: Uppercase external enabled } +servers: [{ url: https://api.example.test }] +paths: + /items: + get: + operationId: listItems + responses: + '200': + description: ok + content: { application/json: { schema: { $ref: 'HTTPS://missing.example/schema.json#/Item' } } } +"#; + let config = NormalizationConfig { + external_references_enabled: true, + ..NormalizationConfig::default() + }; + let ir = normalize_verified_document(document, digest('a'), &config).unwrap(); + assert!( + ir.operations[0] + .findings + .iter() + .any(|finding| finding.code == "external_reference_unavailable"), + "enabled uppercase HTTPS ref with fragment must be classified unavailable, got: {:?}", + ir.operations[0].findings + ); +} + +#[test] +fn unicode_idn_reference_matches_punycode_canonical_snapshot() { + let document = r#" +openapi: 3.1.0 +info: { title: IDN identity } +servers: [{ url: https://api.example.test }] +paths: + /items: + get: + operationId: listItems + responses: + '200': + description: ok + content: { application/json: { schema: { $ref: 'https://пример.example/schema.json#/Item' } } } +"#; + let snapshots = vec![ExternalDocumentSnapshot { + canonical_uri: "https://xn--e1afmkfd.example/schema.json".to_owned(), + digest: digest('b'), + document: "Item: { type: object, properties: { id: { type: string } } }".to_owned(), + }]; + let config = NormalizationConfig { + external_references_enabled: true, + ..NormalizationConfig::default() + }; + let ir = normalize_verified_bundle(document, digest('a'), &snapshots, &config).unwrap(); + assert!( + matches!( + ir.operations[0].response_schema.as_ref().map(|schema| &schema.kind), + Some(NormalizedSchemaKind::Object { properties, .. }) if properties.contains_key("id") + ), + "Unicode IDN ref must match the punycode canonical snapshot" + ); + assert!( + ir.operations[0] + .findings + .iter() + .all(|finding| !finding.code.starts_with("external_reference")), + "resolved IDN snapshot must not produce external findings: {:?}", + ir.operations[0].findings + ); +} + #[test] fn recursion_is_a_stable_graph_edge_without_unbounded_expansion() { let document = r#" diff --git a/crates/crank-import/tests/security_schemes.rs b/crates/crank-import/tests/security_schemes.rs index e323d78..e603441 100644 --- a/crates/crank-import/tests/security_schemes.rs +++ b/crates/crank-import/tests/security_schemes.rs @@ -1,780 +1,799 @@ -mod security_schemes { - use crank_import::rest::{ - ImportFindingSeverity, ImportParseError, NormalizationConfig, NormalizedSecuritySchemeKind, - SecurityParameterLocation, SecurityRequirementsSource, normalize_verified_document, - preview_from_ir, scan_secret_literals, validate_normalized_ir, - }; +use crank_import::rest::{ + ImportFindingSeverity, ImportOperationCandidate, NormalizationConfig, + NormalizedSecuritySchemeKind, SecurityParameterLocation, SecurityRequirementsSource, + SourceDigest, normalize_verified_document, preview_from_ir, validate_normalized_ir, +}; +use serde_json::{Value, json}; - fn normalize_document( - document: &str, - config: &NormalizationConfig, - ) -> Result { - normalize_verified_document( - document, - crank_import::rest::SourceDigest::parse("b".repeat(64)).unwrap(), - config, - ) +fn normalize(document: &Value) -> crank_import::rest::NormalizedIr { + let text = serde_json::to_string(document).expect("fixture serializes"); + normalize_verified_document( + &text, + SourceDigest::parse("b".repeat(64)).expect("digest is valid"), + &NormalizationConfig::default(), + ) + .expect("security fixture normalizes") +} + +fn candidate<'a>( + candidates: &'a [ImportOperationCandidate], + key: &str, +) -> &'a ImportOperationCandidate { + candidates + .iter() + .find(|candidate| candidate.key == key) + .unwrap_or_else(|| panic!("candidate {key} missing: {candidates:?}")) +} + +fn candidates(ir: &crank_import::rest::NormalizedIr) -> Vec { + preview_from_ir(ir) + .groups + .into_iter() + .flat_map(|group| group.operations) + .collect() +} + +fn operation<'a>( + ir: &'a crank_import::rest::NormalizedIr, + path: &str, +) -> &'a crank_import::rest::NormalizedOperation { + ir.operations + .iter() + .find(|operation| operation.path == path) + .unwrap_or_else(|| panic!("operation {path} missing: {ir:?}")) +} + +#[test] +fn openapi_30_and_31_project_typed_schemes_and_safe_hints() { + for version in ["3.0.3", "3.1.0"] { + let document = json!({ + "openapi": version, + "info": { "title": "Security metadata", "version": "1" }, + "components": { "securitySchemes": { + "Bearer": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }, + "Basic": { "type": "http", "scheme": "basic" }, + "HeaderKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key" }, + "QueryKey": { "type": "apiKey", "in": "query", "name": "access_token" }, + "CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" }, + "UnsafeHint": { "type": "http", "scheme": "bearer", "bearerFormat": "sk-private-value" } + }}, + "paths": { "/private": { "get": { + "security": [{ "Bearer": [] }], + "responses": { "200": { "description": "ok" } } + }}} + }); + let ir = normalize(&document); + assert!(matches!(ir.security_schemes["Bearer"].kind, + NormalizedSecuritySchemeKind::Http { ref scheme, ref format_hint } + if scheme == "bearer" && format_hint.as_deref() == Some("JWT"))); + assert!(matches!(ir.security_schemes["Basic"].kind, + NormalizedSecuritySchemeKind::Http { ref scheme, .. } if scheme == "basic")); + assert!(matches!(ir.security_schemes["HeaderKey"].kind, + NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Header, ref parameter_name, .. } + if parameter_name == "X-Api-Key")); + assert!(matches!(ir.security_schemes["QueryKey"].kind, + NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. } + if parameter_name == "access_token")); + assert!(matches!(ir.security_schemes["CookieKey"].kind, + NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Cookie, ref parameter_name, .. } + if parameter_name == "session")); + assert!(matches!( + ir.security_schemes["UnsafeHint"].kind, + NormalizedSecuritySchemeKind::Http { + format_hint: None, + .. + } + )); + let requirement = &operation(&ir, "/private") + .security_requirements + .alternatives[0]; + assert_eq!(requirement.schemes, ["Bearer"]); + assert!(requirement.supported); } +} - #[test] - fn ac_1_projects_openapi_security_scheme_identity_onto_operation_metadata() { - let document = r#" -openapi: 3.1.0 -info: { title: Security metadata } -components: - securitySchemes: - WorkspaceBearer: - type: http - scheme: bearer - bearerFormat: JWT -paths: - /private: - get: - operationId: private - security: [{ WorkspaceBearer: [] }] - responses: { '200': { description: ok } } -"#; - - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let operation = ir - .operations - .iter() - .find(|operation| operation.path == "/private") - .unwrap(); - let operation_contract = serde_json::to_string(operation).unwrap(); - - // This assertion deliberately inspects the operation projection, not the - // canonical source tree, so copying the original document is insufficient. - assert!(operation_contract.contains("WorkspaceBearer")); - assert!(operation_contract.contains("bearer")); - assert!(operation_contract.contains("JWT")); - } - - #[test] - fn ac_6_never_copies_literal_secret_canaries_into_normalized_ir() { - const CANARY: &str = "atdd-secret-canary-2-10"; - let document = format!( - r#" -openapi: 3.0.3 -info: {{ title: Secret canary }} -components: - securitySchemes: - Bearer: - type: http - scheme: bearer - x-example-token: {CANARY} - x-credentials: {CANARY} - x-ciphertext: {CANARY} -paths: - /private: - get: - security: [{{ Bearer: [] }}] - responses: - '200': - description: ok - content: - application/json: - example: {{ token: {CANARY} }} -"# - ); - - let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap(); - let normalized = serde_json::to_string(&ir).unwrap(); - - assert!( - !normalized.contains(CANARY), - "normalized IR must not retain literal credential canaries" - ); - } - - #[test] - fn security_requirements_inherit_and_operation_empty_array_disables_auth() { - let document = r#" -openapi: 3.0.3 -info: { title: Requirements } -components: - securitySchemes: - Key: { type: apiKey, in: header, name: X-Workspace-Key } -security: [{ Key: [] }] -paths: - /inherited: - get: { responses: { '200': { description: ok } } } - /public: - get: - security: [] - responses: { '200': { description: ok } } - /and: - get: - security: [{ Key: [], Other: [] }] - responses: { '200': { description: ok } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let inherited = ir - .operations - .iter() - .find(|operation| operation.path == "/inherited") - .unwrap(); - assert_eq!( - inherited.security_requirements.source, - SecurityRequirementsSource::Document - ); - assert_eq!( - inherited.security_requirements.alternatives[0].schemes, - ["Key"] - ); - let public = ir - .operations - .iter() - .find(|operation| operation.path == "/public") - .unwrap(); - assert_eq!( - public.security_requirements.source, - SecurityRequirementsSource::Operation - ); - assert!(public.security_requirements.alternatives.is_empty()); - let and = ir - .operations - .iter() - .find(|operation| operation.path == "/and") - .unwrap(); - assert!( - and.findings - .iter() - .any(|finding| finding.code == "unsupported_security_and_requirement") - ); - assert_eq!( - and.security_requirements.alternatives[0].schemes, - ["Key", "Other"] - ); - } - - #[test] - fn security_alternatives_preserve_or_and_and_preview_exposes_typed_schemes() { - let document = r#" -openapi: 3.1.0 -info: { title: Alternatives } -components: - securitySchemes: - Bearer: - type: http - scheme: bearer - bearerFormat: JWT - User: - type: apiKey - in: query - name: access_token - CookieKey: - type: apiKey - in: cookie - name: session -security: - - Bearer: [] - - User: [] - - CookieKey: [] -paths: - /private: - get: { responses: { '200': { description: ok } } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let first = preview_from_ir(&ir); - let second = preview_from_ir(&ir); - assert_eq!( - serde_json::to_vec(&first).unwrap(), - serde_json::to_vec(&second).unwrap() - ); - let candidate = &first.groups[0].operations[0]; - assert_eq!(candidate.security_requirements.alternatives.len(), 3); - assert!( - matches!(candidate.security_schemes["Bearer"].kind, NormalizedSecuritySchemeKind::Http { ref format_hint, .. } if format_hint.as_deref() == Some("JWT")) - ); - assert!( - matches!(candidate.security_schemes["User"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. } if parameter_name == "access_token") - ); - assert!( - matches!(candidate.security_schemes["CookieKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Cookie, ref parameter_name, .. } if parameter_name == "session") - ); - assert!(!candidate.security_requirements.alternatives[2].supported); - } - - #[test] - fn swagger2_security_definitions_project_basic_and_api_key_header_and_query() { - let document = r#" -swagger: '2.0' -info: { title: Swagger security } -securityDefinitions: - BasicAuth: { type: basic } - HeaderKey: { type: apiKey, in: header, name: X-Api-Key } - QueryKey: { type: apiKey, in: query, name: api_key } -security: [{ BasicAuth: [] }, { HeaderKey: [] }] -paths: - /private: - get: { responses: { '200': { description: ok } } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - assert!( - matches!(ir.security_schemes["BasicAuth"].kind, NormalizedSecuritySchemeKind::Http { ref scheme, .. } if scheme == "basic") - ); - assert!( - matches!(ir.security_schemes["HeaderKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Header, ref parameter_name, .. } if parameter_name == "X-Api-Key") - ); - assert!( - matches!(ir.security_schemes["QueryKey"].kind, NormalizedSecuritySchemeKind::ApiKey { location: SecurityParameterLocation::Query, ref parameter_name, .. } if parameter_name == "api_key") - ); - assert_eq!(ir.operations[0].security_requirements.alternatives.len(), 2); - } - - #[test] - fn unsupported_security_schemes_create_blockers_without_copying_credentials() { - let document = r#" -openapi: 3.0.3 -info: { title: Unsupported } -components: - securitySchemes: - OAuth: { type: oauth2, flows: {} } - Oidc: { type: openIdConnect, openIdConnectUrl: https://issuer.test } - Mutual: { type: mutualTLS } -paths: - /private: - get: - security: [{ OAuth: [read] }, { Oidc: [] }, { Mutual: [] }, { Unknown: [] }] - responses: { '200': { description: ok } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let codes = ir.operations[0] - .findings - .iter() - .map(|finding| finding.code.as_str()) - .collect::>(); - assert!(codes.contains(&"unsupported_security_scheme")); - assert!(codes.contains(&"unresolved_security_scheme")); - assert!(!codes.contains(&"invalid_security_scopes")); - assert!(!codes.contains(&"unexpected_security_scopes")); - assert!(ir.operations[0].findings.iter().any(|finding| { - finding.code == "unsupported_security_requirements" - && finding.severity == ImportFindingSeverity::Error - })); - assert!( - ir.operations[0] - .security_requirements - .alternatives - .iter() - .all(|alternative| !alternative.supported) - ); - } - - #[test] - fn normalized_ir_rejects_unrelated_security_findings_and_accepts_valid_security_ir() { - let document = r#" -openapi: 3.0.3 -info: { title: Validation } -components: - securitySchemes: - OAuth: { type: oauth2, flows: {} } -paths: - /private: - get: - security: [{ OAuth: [] }] - responses: { '200': { description: ok } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - validate_normalized_ir(&ir).unwrap(); - - let mut tampered = ir; - let finding = tampered.operations[0] - .findings - .iter_mut() - .find(|finding| finding.code == "unsupported_security_scheme") - .unwrap(); - finding.location.pointer = "/info/title".to_owned(); - finding.construct_id = "3.0.3:/info/title".to_owned(); - assert_eq!( - validate_normalized_ir(&tampered), - Err(ImportParseError::InvalidDocument) - ); - } - - #[test] - fn unsupported_or_alternative_does_not_block_a_supported_choice() { - let document = r#" -openapi: 3.0.3 -info: { title: Mixed alternatives } -components: - securitySchemes: - Bearer: { type: http, scheme: bearer } - OAuth: { type: oauth2, flows: {} } -security: [{ OAuth: [] }, { Bearer: [] }] -paths: - /private: - get: { responses: { '200': { description: ok } } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let operation = &ir.operations[0]; - assert_eq!(operation.security_requirements.alternatives.len(), 2); - assert!(!operation.security_requirements.alternatives[0].supported); - assert!(operation.security_requirements.alternatives[1].supported); - assert!( - !operation - .findings - .iter() - .any(|finding| finding.code == "unsupported_security_requirements") - ); - assert!(operation.findings.iter().any(|finding| { - finding.code == "unsupported_security_scheme" - && finding.severity == ImportFindingSeverity::Warning - })); - } - - #[test] - fn openapi_invalid_http_scopes_are_selection_aware_and_valid_or_sibling_survives() { - let document = r#" -openapi: 3.0.3 -info: { title: Invalid HTTP scopes } -components: - securitySchemes: - Bearer: { type: http, scheme: bearer } -security: - - Bearer: [read] - - Bearer: "read" - - Bearer: [1] - - Bearer: [] -paths: - /private: - get: { responses: { '200': { description: ok } } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let operation = &ir.operations[0]; - let alternatives = &operation.security_requirements.alternatives; - assert_eq!(alternatives.len(), 4); - assert!( - alternatives[..3] - .iter() - .all(|alternative| !alternative.supported) - ); - assert!(alternatives[3].supported); - assert!(operation.findings.iter().any(|finding| { - finding.code == "unexpected_security_scopes" - && finding.severity == ImportFindingSeverity::Warning - })); - assert!(operation.findings.iter().any(|finding| { - finding.code == "invalid_security_scopes" - && finding.severity == ImportFindingSeverity::Warning - })); - assert!( - !operation - .findings - .iter() - .any(|finding| { finding.code == "unsupported_security_requirements" }) - ); - } - - #[test] - fn swagger_invalid_api_key_scopes_do_not_hide_valid_or_sibling() { - let document = r#" -swagger: '2.0' -info: { title: Invalid Swagger scopes } -securityDefinitions: - HeaderKey: { type: apiKey, in: header, name: X-Api-Key } - BasicAuth: { type: basic } -security: - - HeaderKey: [read] - - BasicAuth: [] -paths: - /private: - get: { responses: { '200': { description: ok } } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let operation = &ir.operations[0]; - assert!(!operation.security_requirements.alternatives[0].supported); - assert!(operation.security_requirements.alternatives[1].supported); - assert!(operation.findings.iter().any(|finding| { - finding.code == "unexpected_security_scopes" - && finding.severity == ImportFindingSeverity::Warning - })); - assert!( - !operation - .findings - .iter() - .any(|finding| { finding.code == "unsupported_security_requirements" }) - ); - } - - #[test] - fn scanner_finds_examples_defaults_extensions_and_url_userinfo_without_values() { - const CANARIES: [&str; 5] = [ - "example-token-secret", - "default-password-secret", - "extension-api-key-secret", - "url-userinfo-secret", - "header-example-secret", - ]; - let document = r#" -openapi: 3.0.3 -info: { title: Redaction } -servers: [{ url: 'https://operator:url-userinfo-secret@example.test' }] -components: - securitySchemes: - Bearer: { type: http, scheme: bearer, x-example-token: extension-api-key-secret } -paths: - /private: - get: - parameters: - - name: token - in: header - example: header-example-secret - schema: { type: string, default: default-password-secret } - responses: - '200': - description: ok - content: - application/json: - example: { token: example-token-secret } -"#; - let findings = scan_secret_literals(document.as_bytes()).unwrap(); - assert!(findings.len() >= CANARIES.len()); - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let serialized = serde_json::to_string(&ir).unwrap(); - assert!(CANARIES.iter().all(|canary| !serialized.contains(canary))); - let report = serde_json::to_string(&findings).unwrap(); - assert!(CANARIES.iter().all(|canary| !report.contains(canary))); - } - - #[test] - fn scanner_does_not_mistake_password_or_token_schema_properties_for_credentials() { - let document = r#" -openapi: 3.0.3 -info: { title: Password fields } -paths: - /login: - post: - requestBody: - content: - application/json: - schema: - type: object - properties: - password: { type: string, format: password } - access_token: { type: string } - responses: { '204': { description: ok } } -"#; - assert!( - scan_secret_literals(document.as_bytes()) - .unwrap() - .is_empty() - ); - } - - #[test] - fn scanner_keeps_version_examples_and_null_credential_defaults() { - let document = r#" -openapi: 3.0.3 -info: { title: Benign examples } -components: - securitySchemes: - Bearer: { type: http, scheme: bearer, clientSecret: null } -paths: - /version: - get: - responses: - '200': - description: ok - content: - text/plain: - example: 1.2.3 -"#; - assert!( - scan_secret_literals(document.as_bytes()) - .unwrap() - .is_empty() - ); - } - - #[test] - fn scanner_allows_oauth_endpoint_urls_but_redacts_secret_query_parameters() { - let safe_document = r#" -openapi: 3.0.3 -info: { title: OAuth URLs } -components: - securitySchemes: - OAuth: - type: oauth2 - flows: - authorizationCode: - authorizationUrl: https://auth.example.test/authorize - tokenUrl: https://auth.example.test/token - scopes: {} - password: - tokenUrl: https://auth.example.test/password-token - scopes: {} - clientCredentials: - tokenUrl: https://auth.example.test/client-token - scopes: {} -"#; - assert!( - scan_secret_literals(safe_document.as_bytes()) - .unwrap() - .is_empty() - ); - - let secret_document = safe_document - .replace( - "https://auth.example.test/token", - "https://auth.example.test/token?access_token=oauth-url-canary", - ) - .replace( - "https://auth.example.test/password-token", - "https://auth.example.test/password-token?access_token=password-flow-canary", - ) - .replace( - "https://auth.example.test/client-token", - "https://auth.example.test/client-token?access_token=client-flow-canary", - ); - let findings = scan_secret_literals(secret_document.as_bytes()).unwrap(); - assert!(findings.iter().any(|finding| { - finding.location.pointer - == "/components/securitySchemes/OAuth/flows/authorizationCode/tokenUrl" - })); - assert!(findings.iter().any(|finding| { - finding.location.pointer == "/components/securitySchemes/OAuth/flows/password/tokenUrl" - })); - assert!(findings.iter().any(|finding| { - finding.location.pointer - == "/components/securitySchemes/OAuth/flows/clientCredentials/tokenUrl" - })); - let report = serde_json::to_string(&findings).unwrap(); - assert!(!report.contains("oauth-url-canary")); - assert!(!report.contains("password-flow-canary")); - assert!(!report.contains("client-flow-canary")); - } - - #[test] - fn scanner_redacts_contextual_credentials_non_strings_jwts_and_cookie_examples() { - const CANARIES: [&str; 8] = [ - "982734651", - "true", - "opaque-query-canary", - "cookie-session-canary", - "set-cookie-canary", - "query-param-canary", - "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl", - "opaque-jwt-object-key-canary", - ]; - const JWT_KEY: &str = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl"; - let document = format!( - r#" -openapi: 3.0.3 -info: {{ title: Contextual redaction }} -servers: [{{ url: 'https://api.test/items?access_token=opaque-query-canary' }}] -components: - securitySchemes: - Custom: {{ type: http, scheme: bearer, x-clientSecret: 982734651, x-accessToken: true }} -paths: - /login: - post: - parameters: - - name: api_key - in: query - schema: {{ type: string }} - example: query-param-canary - - name: Cookie - in: header - schema: {{ type: string }} - example: cookie-session-canary - responses: - '200': - description: ok - headers: - Set-Cookie: - schema: {{ type: string }} - example: set-cookie-canary - content: - application/json: - example: {{{JWT_KEY}: opaque-jwt-object-key-canary}} - examples: - compact: - value: {{{JWT_KEY}}} -"# - ); - let findings = scan_secret_literals(document.as_bytes()).unwrap(); - let location_pointers: Vec<_> = findings - .iter() - .map(|finding| finding.location.pointer.as_str()) - .collect(); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/x-clientSecret")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/x-accessToken")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/servers/0/url")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/parameters/0/example")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/parameters/1/example")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/headers/Set-Cookie/example")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer.ends_with("/content/application~1json/example")) - ); - assert!( - location_pointers - .iter() - .any(|pointer| pointer - .ends_with("/content/application~1json/examples/compact/value")) - ); - - let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap(); - let serialized = serde_json::to_string(&ir).unwrap(); - for canary in CANARIES { - assert!(!serialized.contains(canary), "IR retained canary {canary}"); +#[test] +fn swagger2_projects_basic_and_api_key_header_and_query() { + let document = json!({ + "swagger": "2.0", + "info": { "title": "Swagger security", "version": "1" }, + "host": "security.example.test", + "schemes": ["https"], + "securityDefinitions": { + "BasicAuth": { "type": "basic" }, + "HeaderKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key" }, + "QueryKey": { "type": "apiKey", "in": "query", "name": "api_key" }, + "CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" } + }, + "security": [{ "BasicAuth": [] }, { "HeaderKey": [] }, { "QueryKey": [] }], + "paths": { "/private": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let ir = normalize(&document); + assert!(matches!(ir.security_schemes["BasicAuth"].kind, + NormalizedSecuritySchemeKind::Http { ref scheme, .. } if scheme == "basic")); + assert!(matches!( + ir.security_schemes["HeaderKey"].kind, + NormalizedSecuritySchemeKind::ApiKey { + location: SecurityParameterLocation::Header, + .. } - let redacted_locations: Vec<_> = ir + )); + assert!(matches!( + ir.security_schemes["QueryKey"].kind, + NormalizedSecuritySchemeKind::ApiKey { + location: SecurityParameterLocation::Query, + .. + } + )); + assert!(matches!( + ir.security_schemes["CookieKey"].kind, + NormalizedSecuritySchemeKind::Unsupported { .. } + )); + assert_eq!( + operation(&ir, "/private") + .security_requirements + .alternatives + .len(), + 3 + ); +} + +#[test] +fn effective_requirements_inherit_override_and_allow_explicit_no_auth() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Requirements", "version": "1" }, + "components": { "securitySchemes": { + "Key": { "type": "apiKey", "in": "header", "name": "X-Key" }, + "Bearer": { "type": "http", "scheme": "bearer" } + }}, + "security": [{ "Key": [] }], + "paths": { + "/inherited": { "get": { "responses": { "200": { "description": "ok" } } } }, + "/override": { "get": { "security": [{ "Bearer": [] }], "responses": { "200": { "description": "ok" } } } }, + "/public": { "get": { "security": [], "responses": { "200": { "description": "ok" } } } }, + "/unsecured": { "get": { "responses": { "200": { "description": "ok" } } } } + } + }); + let ir = normalize(&document); + let inherited = &operation(&ir, "/inherited").security_requirements; + assert_eq!(inherited.source, SecurityRequirementsSource::Document); + assert_eq!(inherited.alternatives[0].schemes, ["Key"]); + let overridden = &operation(&ir, "/override").security_requirements; + assert_eq!(overridden.source, SecurityRequirementsSource::Operation); + assert_eq!(overridden.alternatives[0].schemes, ["Bearer"]); + let public = &operation(&ir, "/public").security_requirements; + assert_eq!(public.source, SecurityRequirementsSource::Operation); + assert!(public.alternatives.is_empty()); + assert_eq!( + operation(&ir, "/unsecured").security_requirements.source, + SecurityRequirementsSource::Document + ); +} + +#[test] +fn or_alternatives_are_preserved_and_unsupported_and_is_scoped() { + let document = json!({ + "openapi": "3.1.0", + "info": { "title": "Alternatives", "version": "1" }, + "components": { "securitySchemes": { + "Bearer": { "type": "http", "scheme": "bearer" }, + "One": { "type": "apiKey", "in": "header", "name": "X-One" }, + "Two": { "type": "apiKey", "in": "query", "name": "two" } + }}, + "paths": { + "/mixed": { "get": { "security": [ + { "One": [], "Two": [] }, { "Bearer": [] } + ], "responses": { "200": { "description": "ok" } } } }, + "/and-only": { "get": { "security": [ + { "One": [], "Two": [] } + ], "responses": { "200": { "description": "ok" } } } }, + "/or": { "get": { "security": [ + { "Bearer": [] }, { "One": [] }, { "Two": [] } + ], "responses": { "200": { "description": "ok" } } } } + } + }); + let ir = normalize(&document); + let preview = preview_from_ir(&ir); + let candidates = preview + .groups + .iter() + .flat_map(|group| &group.operations) + .cloned() + .collect::>(); + + let mixed = candidate(&candidates, "GET /mixed"); + assert_eq!(mixed.security_requirements.alternatives.len(), 2); + assert!(!mixed.security_requirements.alternatives[0].supported); + assert!( + mixed.security_requirements.alternatives[0] + .findings + .contains(&"unsupported_security_and_requirement".to_owned()) + ); + assert_eq!( + mixed.security_requirements.alternatives[0].schemes, + ["One", "Two"] + ); + assert!(mixed.security_requirements.alternatives[1].supported); + assert_eq!( + mixed.security_requirements.alternatives[1].schemes, + ["Bearer"] + ); + assert!( + mixed .findings .iter() - .filter(|finding| finding.code == "secret_literal_redacted") - .map(|finding| finding.location.pointer.as_str()) - .collect(); + .all(|finding| finding.severity != ImportFindingSeverity::Error) + ); + + let and_only = candidate(&candidates, "GET /and-only"); + assert!(and_only.findings.iter().any(|finding| { + finding.code == "unsupported_security_requirements" + && finding.severity == ImportFindingSeverity::Error + })); + assert!(and_only.findings.iter().any(|finding| { + finding.code == "unsupported_security_and_requirement" + && finding.severity == ImportFindingSeverity::Warning + && finding.message.contains("One, Two") + })); + + let or = candidate(&candidates, "GET /or"); + assert_eq!(or.security_requirements.alternatives.len(), 3); + assert!( + or.security_requirements + .alternatives + .iter() + .all(|alternative| alternative.supported) + ); +} + +#[test] +fn unsupported_schemes_produce_manual_configuration_findings_without_typed_credentials() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Unsupported", "version": "1" }, + "components": { "securitySchemes": { + "OAuth": { "type": "oauth2", "flows": { "clientCredentials": { + "tokenUrl": "https://issuer.example/token?token=embedded", + "scopes": { "read": "read" }, + "x-client-secret": "never-copy-this" + }}, "x-example-token": "also-never-copy" }, + "Oidc": { "type": "openIdConnect", "openIdConnectUrl": "https://issuer.example" }, + "Mutual": { "type": "mutualTLS" }, + "Unknown": { "type": "vendorAuth", "x-api-key": "vendor-secret" } + }}, + "paths": { "/private": { "get": { + "security": [{ "OAuth": ["read"] }, { "Oidc": [] }, { "Mutual": [] }, { "Unknown": [] }], + "responses": { "200": { "description": "ok" } } + }}} + }); + let ir = normalize(&document); + let findings = &operation(&ir, "/private").findings; + assert!( + findings + .iter() + .any(|finding| finding.code == "unsupported_security_scheme") + ); + assert!( + findings + .iter() + .any(|finding| finding.code == "unsupported_security_requirements") + ); + assert!( + operation(&ir, "/private") + .security_requirements + .alternatives + .iter() + .all(|alternative| !alternative.supported) + ); + let typed = serde_json::to_string(&ir.security_schemes).unwrap(); + for secret_field in [ + "flows", + "tokenUrl", + "x-client-secret", + "x-example-token", + "x-api-key", + ] { assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/x-clientSecret")) + !typed.contains(secret_field), + "typed metadata retained {secret_field}" ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/x-accessToken")) - ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/servers/0/url")) - ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/parameters/0/example")) - ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/parameters/1/example")) - ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/headers/Set-Cookie/example")) - ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer.ends_with("/content/application~1json/example")) - ); - assert!( - redacted_locations - .iter() - .any(|pointer| pointer - .ends_with("/content/application~1json/examples/compact/value")) - ); - let report = serde_json::to_string(&ir.findings).unwrap(); - assert!(report.contains("secret_literal_redacted")); - for canary in CANARIES { + } + assert!(!typed.contains("never-copy-this")); + assert!(!typed.contains("vendor-secret")); +} + +#[test] +fn normalized_ir_accepts_historical_v4_contract_without_security_fields() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Historical", "version": "1" }, + "paths": { "/health": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let mut ir = normalize(&document); + ir.normalizer_version = crank_import::rest::HISTORICAL_NORMALIZER_VERSION.to_owned(); + ir.projection_version = crank_import::rest::HISTORICAL_PROJECTION_VERSION.to_owned(); + ir.security_schemes.clear(); + ir.operations[0].security_schemes.clear(); + ir.operations[0].security_requirements = Default::default(); + validate_normalized_ir(&ir).expect("historical v4 shape remains valid"); + + let serialized = serde_json::to_value(&ir).unwrap(); + let operation = &serialized["operations"][0]; + assert!(operation.get("security_requirements").is_none()); + assert!(operation.get("security_schemes").is_none()); +} + +#[test] +fn non_array_security_is_rejected_as_an_import_error_surface() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Malformed", "version": "1" }, + "security": "Bearer", + "paths": { "/private": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let ir = normalize(&document); + assert!(operation(&ir, "/private").findings.iter().any(|finding| { + finding.code == "invalid_security_requirements" + && finding.severity == ImportFindingSeverity::Error + && finding.location.pointer == "/security" + })); + let candidates = candidates(&ir); + assert!( + candidate(&candidates, "GET /private") + .findings + .iter() + .any(|finding| { + finding.code == "invalid_security_requirements" + && finding.severity == ImportFindingSeverity::Error + }) + ); +} + +#[test] +fn secret_like_literals_are_redacted_from_ir_preview_and_source_snapshot() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Redaction", "version": "1" }, + "servers": [{ "url": "https://user:secret-canary@api.example.test" }], + "paths": { "/private": { "get": { + "parameters": [{ + "name": "Authorization", + "in": "header", + "example": "Bearer header-canary" + }, { + "name": "X-Api-Key", + "in": "header", + "example": "opaque-canary-42", + "schema": { "type": "string" } + }, { + "name": "X-Trace", + "in": "header", + "example": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjYW5hcnktNDIifQ.c2lnbmF0dXJlMTIzNDU2", + "schema": { "type": "string" } + }], + "requestBody": { "content": { "application/json": { "schema": { + "type": "object", + "properties": { + "password": { "type": "string", "default": "default-canary" }, + "token": { "type": "integer", "default": 4242 }, + "passwd": { "type": "boolean", "default": true }, + "ordinaryDefault": { "type": "string", "default": "Bearer default-literal-canary" }, + "ordinary": { "type": "string", "example": "token-example-canary" } + } + }}}}, + "parameters": [{ + "name": "X-Api-Key", "in": "header", + "schema": { "type": "string", "default": "opaque-header-default-canary" } + }, { + "name": "api_key", "in": "query", + "schema": { "type": "string", "default": "opaque-query-default-canary" } + }, { + "name": "session-token", "in": "cookie", + "schema": { "type": "string", "default": "opaque-cookie-default-canary" } + }], + "responses": { "200": { "description": "ok" } }, + "x-api-key": "extension-canary", + "x-ciphertext": "opaque-ciphertext-canary", + "x-private-key": "private-key-canary" + }}} + }); + let ir = normalize(&document); + let serialized_ir = serde_json::to_string(&ir).unwrap(); + let serialized_preview = serde_json::to_string(&preview_from_ir(&ir)).unwrap(); + for output in [&serialized_ir, &serialized_preview] { + for secret in [ + "secret-canary", + "header-canary", + "opaque-canary-42", + "default-canary", + "default-literal-canary", + "opaque-header-default-canary", + "opaque-query-default-canary", + "opaque-cookie-default-canary", + "token-example-canary", + "extension-canary", + "opaque-ciphertext-canary", + "private-key-canary", + "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjYW5hcnktNDIifQ.c2lnbmF0dXJlMTIzNDU2", + "4242", + ] { assert!( - !report.contains(canary), - "finding report retained canary {canary}" + !output.contains(secret), + "output retained {secret}: {output}" ); } } + assert!(!serialized_ir.contains("\"default\":true")); + assert!(serialized_ir.contains("[REDACTED]")); + assert!( + ir.findings + .iter() + .any(|finding| finding.code == "secret_like_example") + ); +} - #[test] - fn unsafe_bearer_format_canary_is_redacted_and_not_projected() { - const CANARY: &str = "Bearer sk-format-canary"; - let document = format!( - r#" -openapi: 3.0.3 -info: {{ title: Format redaction }} -components: - securitySchemes: - Bearer: - type: http - scheme: bearer - bearerFormat: "{CANARY}" -paths: - /private: - get: {{ security: [{{ Bearer: [] }}], responses: {{ '200': {{ description: ok }} }} }} -"# - ); - let ir = normalize_document(&document, &NormalizationConfig::default()).unwrap(); - let serialized = serde_json::to_string(&ir).unwrap(); - assert!(!serialized.contains(CANARY)); - let scan_findings = scan_secret_literals(document.as_bytes()).unwrap(); - assert!(scan_findings.iter().any(|finding| { - finding.location.pointer == "/components/securitySchemes/Bearer/bearerFormat" - })); - let redaction_findings: Vec<_> = ir +#[test] +fn encoded_sensitive_url_query_names_are_detected_and_sanitized() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Encoded URL credentials", "version": "1" }, + "servers": [{ "url": "https://api.example.test/?%61pi%5Fkey=encoded-query-canary®ion=west" }], + "paths": { "/health": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let source = serde_json::to_string(&document).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err() + ); + + let ir = normalize(&document); + let serialized = serde_json::to_string(&ir).unwrap(); + assert!(!serialized.contains("encoded-query-canary")); + assert!(serialized.contains("%61pi%5Fkey=[REDACTED]")); + assert!(serialized.contains("region=west")); +} + +#[test] +fn declared_api_key_names_seed_parameter_and_url_redaction_context() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Declared API keys", "version": "1" }, + "servers": [{ "url": "https://api.example.test/?key=query-canary®ion=west" }], + "components": { "securitySchemes": { + "ClientKey": { "type": "apiKey", "in": "header", "name": "X-Client" }, + "QueryKey": { "type": "apiKey", "in": "query", "name": "key" }, + "CookieKey": { "type": "apiKey", "in": "cookie", "name": "session" } + }}, + "paths": { "/public": { + "get": { + "parameters": [ + { "name": "X-Client", "in": "header", "schema": { "type": "string", "default": "header-canary" }, "example": "header-example-canary" }, + { "name": "session", "in": "cookie", "schema": { "type": "string", "default": "cookie-canary" } } + ], + "responses": { "200": { "description": "ok" } } + } + }} + }); + let source = serde_json::to_string(&document).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err() + ); + + let ir = normalize(&document); + let serialized = serde_json::to_string(&ir).unwrap(); + for canary in [ + "query-canary", + "header-canary", + "header-example-canary", + "cookie-canary", + ] { + assert!(!serialized.contains(canary), "{canary} leaked into IR"); + } + assert!(serialized.contains("key=[REDACTED]")); + assert!(serialized.contains("region=west")); + + let mixed_containers = json!({ + "openapi": "3.0.3", + "info": { "title": "Legacy security map", "version": "1" }, + "components": { "securitySchemes": {} }, + "securityDefinitions": { + "QueryKey": { "type": "apiKey", "in": "query", "name": "key" } + }, + "servers": [{ "url": "https://api.example.test/?key=legacy-container-canary" }], + "paths": { "/public": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let source = serde_json::to_string(&mixed_containers).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err() + ); + let ir = normalize(&mixed_containers); + let serialized = serde_json::to_string(&ir).unwrap(); + assert!(!serialized.contains("legacy-container-canary")); + assert!(serialized.contains("key=[REDACTED]")); +} + +#[test] +fn credential_like_security_format_hints_are_redacted_but_jwt_is_safe() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Format hints", "version": "1" }, + "components": { "securitySchemes": { + "Bearer": { "type": "http", "scheme": "bearer", "bearerFormat": "sk-private-value" }, + "HeaderKey": { "type": "apiKey", "in": "header", "name": "X-Api-Key", "x-format-hint": "token-format-canary" }, + "SafeBearer": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" } + }, "headers": { + "X-Api-Key": { "schema": { "type": "string", "default": "opaque-component-header-canary" } } + }}, + "paths": { "/health": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let source = serde_json::to_string(&document).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err() + ); + + let ir = normalize(&document); + let serialized = serde_json::to_string(&ir).unwrap(); + assert!(!serialized.contains("sk-private-value")); + assert!(!serialized.contains("token-format-canary")); + assert!(!serialized.contains("opaque-component-header-canary")); + assert!(serialized.contains("X-Api-Key")); + let source_tree = serde_json::to_value(&ir.source_tree).unwrap(); + let schema = &source_tree["value"]["value"]["components"]["value"]["value"]["headers"]["value"] + ["value"]["X-Api-Key"]["value"]["value"]["schema"]["value"]["value"]; + assert_eq!(schema["type"]["value"]["value"], "string"); + assert_eq!(schema["default"]["value"]["value"], "[REDACTED]"); + assert!(matches!( + ir.security_schemes["Bearer"].kind, + NormalizedSecuritySchemeKind::Http { + format_hint: None, + .. + } + )); + assert!(matches!( + ir.security_schemes["HeaderKey"].kind, + NormalizedSecuritySchemeKind::ApiKey { + format_hint: None, + .. + } + )); + assert!(matches!(ir.security_schemes["SafeBearer"].kind, + NormalizedSecuritySchemeKind::Http { ref format_hint, .. } + if format_hint.as_deref() == Some("JWT"))); +} + +#[test] +fn unused_unsupported_openapi_definitions_get_located_manual_warning_without_blocking_public() { + for version in ["3.0.3", "3.1.0"] { + let document = json!({ + "openapi": version, + "info": { "title": "Unused OAuth definition", "version": "1" }, + "components": { "securitySchemes": { + "OAuth": { "type": "oauth2", "flows": {} } + }}, + "paths": { "/public": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let ir = normalize(&document); + let warning = ir .findings .iter() - .filter(|finding| finding.code == "secret_literal_redacted") - .collect(); - assert_eq!(redaction_findings.len(), 1); + .find(|finding| finding.code == "unsupported_security_scheme_definition") + .expect("unused unsupported definition needs a manual configuration warning"); + assert_eq!(warning.severity, ImportFindingSeverity::Warning); assert_eq!( - redaction_findings[0].location.pointer, - "/components/securitySchemes/Bearer/bearerFormat" + warning.location.pointer, + "/components/securitySchemes/OAuth" ); - let report = serde_json::to_string(&ir.findings).unwrap(); - assert!(report.contains("secret_literal_redacted")); - assert!(report.contains("/components/securitySchemes/Bearer/bearerFormat")); - assert!(!report.contains(CANARY)); - } - - #[test] - fn historical_v4_ir_without_security_fields_remains_valid() { - let document = r#" -openapi: 3.0.3 -info: { title: Replay } -paths: - /old: - get: { responses: { '200': { description: ok } } } -"#; - let ir = normalize_document(document, &NormalizationConfig::default()).unwrap(); - let mut value = serde_json::to_value(ir).unwrap(); - value["normalizer_version"] = serde_json::json!("normalized-ir-v4"); - value["projection_version"] = serde_json::json!("preview-v4"); - value.as_object_mut().unwrap().remove("security_schemes"); - for operation in value["operations"].as_array_mut().unwrap() { - operation - .as_object_mut() - .unwrap() - .remove("security_schemes"); - operation - .as_object_mut() - .unwrap() - .remove("security_requirements"); - } - let replay: crank_import::rest::NormalizedIr = serde_json::from_value(value).unwrap(); - validate_normalized_ir(&replay).unwrap(); + assert!(warning.operation_key.is_none()); assert!( - replay.operations[0] - .security_requirements - .alternatives - .is_empty() + candidate(&candidates(&ir), "GET /public") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error) ); } } + +#[test] +fn unused_unsupported_swagger_definition_gets_located_manual_warning_without_blocking_public() { + let document = json!({ + "swagger": "2.0", + "info": { "title": "Unused OAuth definition", "version": "1" }, + "host": "security.example.test", + "schemes": ["https"], + "securityDefinitions": { + "OAuth": { "type": "oauth2", "flow": "application", "tokenUrl": "https://issuer.example/token", "scopes": {} } + }, + "paths": { "/public": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let ir = normalize(&document); + let warning = ir + .findings + .iter() + .find(|finding| finding.code == "unsupported_security_scheme_definition") + .expect("unused unsupported definition needs a manual configuration warning"); + assert_eq!(warning.severity, ImportFindingSeverity::Warning); + assert_eq!(warning.location.pointer, "/securityDefinitions/OAuth"); + assert!(warning.operation_key.is_none()); + assert!( + candidate(&candidates(&ir), "GET /public") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error) + ); +} + +#[test] +fn url_credentials_are_redacted_without_discarding_safe_oauth_endpoints() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "URL redaction", "version": "1" }, + "components": { "securitySchemes": { + "OAuth": { "type": "oauth2", "flows": { "clientCredentials": { + "tokenUrl": "https://issuer.example/token", + "authorizationUrl": "https://issuer.example/authorize?client_id=public-client" + }} } + }}, + "servers": [ + { "url": "https://bare-canary@api.example.test/v1?api_key=query-canary®ion=west" }, + { "url": "//relative-userinfo-canary@api.example.test/v1" } + ], + "paths": { "/health": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let source = serde_json::to_string(&document).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err() + ); + let ir = normalize(&document); + let serialized = serde_json::to_string(&ir).unwrap(); + assert!(!serialized.contains("bare-canary")); + assert!(!serialized.contains("relative-userinfo-canary")); + assert!(!serialized.contains("query-canary")); + assert!(serialized.contains("api_key=[REDACTED]")); + assert!(serialized.contains("https://issuer.example/token")); + assert!(serialized.contains("https://issuer.example/authorize?client_id=public-client")); +} + +#[test] +fn sanitized_userinfo_examples_and_declared_query_urls_validate_after_redaction() { + let document = json!({ + "openapi": "3.0.3", + "info": { + "title": "Sanitized URL contexts", + "version": "1", + "description": "Docs: https://api.example.test/?key=description-query-canary" + }, + "components": { "securitySchemes": { + "QueryKey": { "type": "apiKey", "in": "query", "name": "key" } + }}, + "paths": { "/health": { "get": { + "requestBody": { "content": { "application/json": { "schema": { + "type": "string", + "example": "https://example-user:example-password@api.example.test/path" + }}}}, + "responses": { "200": { "description": "ok" } + }}}} + }); + let source = serde_json::to_string(&document).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err() + ); + let ir = normalize(&document); + let serialized = serde_json::to_string(&ir).unwrap(); + assert!(!serialized.contains("description-query-canary")); + assert!(!serialized.contains("example-user")); + assert!(!serialized.contains("example-password")); + assert!(serialized.contains("key=[REDACTED]")); + assert!(serialized.contains("https://api.example.test/path")); +} + +#[test] +fn credential_named_url_defaults_and_url_fragments_are_redacted() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Credential URLs", "version": "1" }, + "components": { "schemas": { + "Config": { "type": "object", "properties": { + "credentialUrl": { "type": "string", "default": "opaque-credential-url-canary" }, + "x-credential-url": "opaque-extension-url-canary", + "callback": { "type": "string", "default": "https://issuer.example/callback#access_token=fragment-canary&state=safe" }, + "reference": { "type": "string", "default": "https://issuer.example/#/components/securitySchemes/AccessToken" } + }} + }}, + "paths": { "/health": { "get": { + "responses": { "200": { "description": "ok" } } + }}} + }); + let source = serde_json::to_string(&document).unwrap(); + assert!( + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .is_err(), + "credentialUrl and URL fragment credentials must fail source preflight" + ); + let serialized = serde_json::to_string(&normalize(&document)).unwrap(); + assert!(!serialized.contains("opaque-credential-url-canary")); + assert!(!serialized.contains("opaque-extension-url-canary")); + assert!(!serialized.contains("fragment-canary")); + assert!(serialized.contains("[REDACTED]")); + assert!(serialized.contains("#access_token=[REDACTED]&state=safe")); + let safe_pointer = json!({ + "openapi": "3.0.3", + "info": { "title": "JSON pointer", "version": "1" }, + "paths": { "/health": { "get": { + "description": "#/components/securitySchemes/AccessToken", + "responses": { "200": { "description": "ok" } } + }}} + }); + crank_import::rest::validate_document_source_safe( + &serde_json::to_string(&safe_pointer).unwrap(), + &NormalizationConfig::default(), + ) + .expect("JSON-pointer fragments are references, not credential parameters"); +} + +#[test] +fn sensitive_identifiers_preserve_schemes_requirements_and_schema_metadata() { + let document = json!({ + "openapi": "3.0.3", + "info": { "title": "Safe identifiers", "version": "1" }, + "components": { + "securitySchemes": { + "AccessToken": { "type": "http", "scheme": "bearer" } + }, + "schemas": { "Password": { "type": "string" } } + }, + "security": [{ "AccessToken": [] }], + "paths": { "/login": { "post": { + "requestBody": { "content": { "application/json": { "schema": { + "type": "object", "properties": { + "password": { "type": "string", "description": "Password input" } + } + }}}}, + "responses": { "200": { "description": "ok" } } + }}} + }); + let source = serde_json::to_string(&document).unwrap(); + crank_import::rest::validate_document_source_safe(&source, &NormalizationConfig::default()) + .expect("names and schema declarations contain no literal credentials"); + let ir = normalize(&document); + assert!(ir.security_schemes.contains_key("AccessToken")); + assert!(operation(&ir, "/login").security_requirements.alternatives[0].supported); + let schema = operation(&ir, "/login") + .request_body_schema + .as_ref() + .unwrap(); + let crank_import::rest::NormalizedSchemaKind::Object { properties, .. } = &schema.kind else { + panic!("request object schema must survive redaction"); + }; + assert_eq!( + properties["password"].description.as_deref(), + Some("Password input") + ); + assert!(matches!( + properties["password"].kind, + crank_import::rest::NormalizedSchemaKind::Scalar { .. } + )); +} diff --git a/crates/crank-import/tests/security_scope.rs b/crates/crank-import/tests/security_scope.rs new file mode 100644 index 0000000..d51f837 --- /dev/null +++ b/crates/crank-import/tests/security_scope.rs @@ -0,0 +1,700 @@ +//! Focused regressions for the effective security scope +//! (story 2.14 review findings): +//! +//! - an OR array with an empty requirement object `{}` exposes the public +//! alternative and must keep the affected operation actionable, without +//! touching other operations; +//! - supported non-empty requirements remain resolvable for profile binding; +//! - malformed security shapes (non-array value, non-object array item) must +//! produce a source-located operation blocker instead of silently +//! disappearing. + +use crank_import::rest::{ + ImportFindingSeverity, ImportOperationCandidate, NormalizationConfig, NormalizedFinding, + SourceDigest, normalize_verified_document, preview_from_ir, +}; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; + +fn hex(bytes: impl AsRef<[u8]>) -> String { + Sha256::digest(bytes) + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} + +fn run( + source: &Value, +) -> ( + crank_import::rest::NormalizedIr, + crank_import::rest::ImportPreview, +) { + let text = serde_json::to_string(source).unwrap(); + let source_digest = SourceDigest::parse(hex(text.as_bytes())).expect("sha256 is valid"); + let ir = normalize_verified_document(&text, source_digest, &NormalizationConfig::default()) + .expect("security-scope fixture parses"); + let preview = preview_from_ir(&ir); + (ir, preview) +} + +fn candidate<'a>( + preview: &'a crank_import::rest::ImportPreview, + key: &str, +) -> &'a ImportOperationCandidate { + preview + .groups + .iter() + .flat_map(|group| &group.operations) + .find(|candidate| candidate.key == key) + .unwrap_or_else(|| panic!("{key} candidate missing in {preview:?}")) +} + +fn security_findings<'a>( + ir: &'a crank_import::rest::NormalizedIr, + key: &str, +) -> Vec<&'a NormalizedFinding> { + all_findings(ir) + .into_iter() + .filter(|finding| { + finding.code == "invalid_security_requirements" + && finding.severity == ImportFindingSeverity::Error + && finding.operation_key.as_deref() == Some(key) + }) + .collect() +} + +fn all_findings(ir: &crank_import::rest::NormalizedIr) -> Vec<&NormalizedFinding> { + ir.findings + .iter() + .chain( + ir.operations + .iter() + .flat_map(|operation| &operation.findings), + ) + .collect() +} + +#[test] +fn empty_or_alternative_makes_the_operation_public() { + // Document-level OR array: [{"probe": []}, {}] offers an explicit public + // alternative, so the inherited operation must stay actionable. + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "components": { + "securitySchemes": { "probe": { "type": "http", "scheme": "basic" } } + }, + "security": [{ "probe": [] }, {}], + "paths": { + "/public": { "get": { "operationId": "publicInherit", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + let public = candidate(&preview, "GET /public"); + assert!( + public + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error), + "empty OR alternative must keep the inherited operation public: {:?}", + public.findings + ); + assert!( + security_findings(&ir, "GET /public").is_empty(), + "no security blocker may belong to the public operation: {:?}", + ir.findings + ); + assert!( + preview.findings.iter().all(|finding| { + !(finding.severity == ImportFindingSeverity::Error && finding.operation_key.is_none()) + }), + "no document-global blocker may be produced: {preview:?}" + ); +} +#[test] +fn or_alternative_keeps_only_that_operation_public() { + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "components": { + "securitySchemes": { "probe": { "type": "http", "scheme": "basic" } } + }, + "paths": { + "/open": { "get": { "security": [{ "probe": [] }, {}], "operationId": "openOp", + "responses": { "200": { "description": "ok" } } } }, + "/closed": { "get": { "security": [{ "probe": [] }], "operationId": "closedOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + let open = candidate(&preview, "GET /open"); + assert!( + open.findings + .iter() + .all(|finding| { finding.severity != ImportFindingSeverity::Error }), + "operation with a public OR alternative must stay actionable: {:?}", + open.findings + ); + let closed = candidate(&preview, "GET /closed"); + assert!( + closed + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error), + "well-formed supported requirement must remain resolvable: {:?}", + closed.findings + ); + assert!(security_findings(&ir, "GET /closed").is_empty()); + assert!(closed.security_requirements.alternatives[0].supported); + assert_eq!( + closed.security_requirements.alternatives[0].schemes, + ["probe"] + ); + assert!( + security_findings(&ir, "GET /open").is_empty(), + "the public alternative must not leak into the open operation: {:?}", + ir.findings + ); +} + +#[test] +fn well_formed_requirement_stays_resolvable_for_auth_profile_binding() { + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "components": { + "securitySchemes": { "probe": { "type": "http", "scheme": "basic" } } + }, + "security": [{ "probe": [] }], + "paths": { + "/secured": { "get": { "operationId": "securedOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + assert!(security_findings(&ir, "GET /secured").is_empty()); + let secured = candidate(&preview, "GET /secured"); + assert!( + secured + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error) + ); + assert!(secured.security_requirements.alternatives[0].supported); + assert_eq!( + secured.security_requirements.source, + crank_import::rest::SecurityRequirementsSource::Document + ); +} + +#[test] +fn malformed_security_shapes_block_the_affected_operation() { + // Non-array security value: both inherited operations are affected. + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "security": "basic", + "paths": { + "/one": { "get": { "operationId": "oneOp", + "responses": { "200": { "description": "ok" } } } }, + "/two": { "get": { "operationId": "twoOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + for key in ["GET /one", "GET /two"] { + let blocker = security_findings(&ir, key) + .into_iter() + .next() + .cloned() + .unwrap_or_else(|| panic!("non-array security must block {key}: {:?}", ir.findings)); + assert_eq!( + blocker.location.pointer, "/security", + "blocker must point at the malformed root security: {:?}", + blocker + ); + assert!( + candidate(&preview, key).findings.iter().any(|finding| { + finding.code == "invalid_security_requirements" + && finding.severity == ImportFindingSeverity::Error + }), + "{key} candidate must be blocked" + ); + } + + // Non-object array item: the array is fail-closed and the item has a + // source-located warning. + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "security": ["basic"], + "paths": { + "/one": { "get": { "operationId": "oneOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, _) = run(&source); + let blocker = security_findings(&ir, "GET /one") + .into_iter() + .next() + .cloned() + .unwrap_or_else(|| panic!("non-object security item must block: {:?}", ir.findings)); + assert_eq!( + blocker.location.pointer, "/security", + "blocker must point at the malformed array: {:?}", + blocker + ); + assert!(all_findings(&ir).iter().any(|finding| { + finding.code == "invalid_security_requirement" + && finding.severity == ImportFindingSeverity::Warning + && finding.location.pointer == "/security/0" + })); + + // Malformed operation-level security must not affect the neighbour. + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "paths": { + "/bad": { "get": { "security": "basic", "operationId": "badOp", + "responses": { "200": { "description": "ok" } } } }, + "/fine": { "get": { "operationId": "fineOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + let blocker = security_findings(&ir, "GET /bad") + .into_iter() + .next() + .cloned() + .unwrap_or_else(|| panic!("malformed local security must block: {:?}", ir.findings)); + assert_eq!( + blocker.location.pointer, "/paths/~1bad/get/security", + "blocker must point at the malformed local security: {:?}", + blocker + ); + assert!( + security_findings(&ir, "GET /fine").is_empty(), + "the neighbour operation must not inherit a local defect: {:?}", + ir.findings + ); + assert!( + candidate(&preview, "GET /fine") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error), + "neighbour operation must stay actionable" + ); +} + +#[test] +fn empty_alternative_needs_a_well_formed_array() { + // A malformed item in the same array invalidates the public + // alternative: every alternative is reported, and the operation + // stays blocked instead of silently becoming public. + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "components": { + "securitySchemes": { "probe": { "type": "http", "scheme": "basic" } } + }, + "security": [{ "probe": [] }, {}, "basic"], + "paths": { + "/mixed": { "get": { "operationId": "mixedOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + let at_pointer = |pointer: &str, code: &str| { + all_findings(&ir) + .into_iter() + .find(|finding| { + finding.code == code + && finding.operation_key.as_deref() == Some("GET /mixed") + && finding.location.pointer == pointer + }) + .unwrap_or_else(|| panic!("{code} finding at {pointer} missing: {:?}", ir.findings)) + }; + assert_eq!( + at_pointer("/security", "invalid_security_requirements").severity, + ImportFindingSeverity::Error + ); + assert_eq!( + at_pointer("/security/2", "invalid_security_requirement").severity, + ImportFindingSeverity::Warning + ); + assert!( + candidate(&preview, "GET /mixed") + .security_requirements + .alternatives[0] + .supported + ); + assert!( + candidate(&preview, "GET /mixed") + .findings + .iter() + .any(|finding| finding.severity == ImportFindingSeverity::Error), + "the operation must not become public because of a malformed sibling: {preview:?}" + ); +} + +fn security_fixture_31() -> serde_json::Map { + security_fixture_openapi("3.1.0") +} + +#[test] +fn object_instead_of_security_array_has_scoped_coverage() { + for version in ["3.0.3", "3.1.0", "2.0"] { + for local in [false, true] { + let mut fixture = if version == "2.0" { + security_fixture_swagger2() + } else { + security_fixture_openapi(version) + }; + if !local { + fixture.insert("security".to_owned(), json!({})); + } + fixture.insert( + "paths".to_owned(), + json!({ + "/bad": get200("badObject", local.then(|| json!({}))), + "/public": get200("publicObject", Some(json!([]))) + }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + let expected = if local { + "/paths/~1bad/get/security" + } else { + "/security" + }; + let blocker = security_findings(&ir, "GET /bad"); + assert_eq!(blocker.len(), 1, "{version} {local}: {:?}", ir.findings); + assert_eq!(blocker[0].location.pointer, expected); + assert!(security_findings(&ir, "GET /public").is_empty()); + assert!( + candidate(&preview, "GET /public") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error) + ); + crank_import::rest::validate_normalized_ir(&ir).unwrap(); + } + } +} + +fn security_fixture_openapi(version: &str) -> serde_json::Map { + let mut fixture = serde_json::Map::new(); + fixture.insert("openapi".to_owned(), json!(version)); + fixture.insert( + "info".to_owned(), + json!({ "title": "Security scope regression", "version": "1" }), + ); + fixture.insert( + "servers".to_owned(), + json!([{ "url": "https://security-scope.example.test" }]), + ); + fixture +} + +fn security_fixture_swagger2() -> serde_json::Map { + let mut fixture = serde_json::Map::new(); + fixture.insert("swagger".to_owned(), json!("2.0")); + fixture.insert( + "info".to_owned(), + json!({ "title": "Security scope regression" }), + ); + fixture.insert("host".to_owned(), json!("security-scope.example.test")); + fixture.insert("schemes".to_owned(), json!(["https"])); + fixture +} + +fn get200(operation_id: &str, security: Option) -> Value { + let mut get = serde_json::Map::from_iter([ + ("operationId".to_owned(), json!(operation_id)), + ( + "responses".to_owned(), + json!({ "200": { "description": "ok" } }), + ), + ]); + if let Some(security) = security { + get.insert("security".to_owned(), security); + } + let mut path_item = serde_json::Map::new(); + path_item.insert("get".to_owned(), Value::Object(get)); + Value::Object(path_item) +} + +#[test] +fn openapi_31_non_array_root_security_blocks_inherited_operations() { + let mut fixture = security_fixture_31(); + fixture.insert("security".to_owned(), json!("basic")); + fixture.insert( + "paths".to_owned(), + json!({ + "/one": get200("oneOp31", None), + "/two": get200("twoOp31", None) + }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + for key in ["GET /one", "GET /two"] { + let blockers = security_findings(&ir, key); + assert_eq!( + blockers.len(), + 1, + "exactly one blocker for {key}: {:?}", + ir.findings + ); + let blocker = &blockers[0]; + assert_eq!( + blocker.code, "invalid_security_requirements", + "stable finding code: {blocker:?}" + ); + assert_eq!( + blocker.severity, + ImportFindingSeverity::Error, + "stable finding severity: {blocker:?}" + ); + assert_eq!( + blocker.location.pointer, "/security", + "blocker must point at the malformed root security: {blocker:?}" + ); + assert!( + candidate(&preview, key).findings.iter().any(|finding| { + finding.code == "invalid_security_requirements" + && finding.severity == ImportFindingSeverity::Error + }), + "{key} candidate must be blocked" + ); + } +} + +#[test] +fn openapi_31_malformed_local_security_affects_only_that_operation() { + let mut fixture = security_fixture_31(); + // A well-formed root requirement proves priority: the malformed local + // security wins for /bad (no root blocker may be added), while /public + // relies on an explicit empty override to stay public. + fixture.insert("security".to_owned(), json!([{ "probe": [] }])); + fixture.insert( + "paths".to_owned(), + json!({ + "/bad": get200("badOp31", Some(json!("basic"))), + "/public": get200("publicOp31", Some(json!([]))) + }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + let blockers = security_findings(&ir, "GET /bad"); + assert_eq!(blockers.len(), 1, "exactly one blocker: {:?}", ir.findings); + assert_eq!( + blockers[0].code, "invalid_security_requirements", + "stable finding code" + ); + assert_eq!( + blockers[0].severity, + ImportFindingSeverity::Error, + "stable finding severity" + ); + assert_eq!( + blockers[0].location.pointer, "/paths/~1bad/get/security", + "blocker must point at the malformed local security: {:?}", + blockers[0] + ); + assert!( + security_findings(&ir, "GET /public").is_empty(), + "the public neighbour must not inherit a local defect: {:?}", + ir.findings + ); + assert!( + candidate(&preview, "GET /public") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error), + "public neighbour must stay actionable: {preview:?}" + ); +} + +#[test] +fn openapi_31_public_alternative_with_malformed_sibling_does_not_go_public() { + let mut fixture = security_fixture_31(); + fixture.insert("security".to_owned(), json!([{}, "basic"])); + fixture.insert( + "paths".to_owned(), + json!({ "/mixed": get200("mixedOp31", None) }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + let blockers = security_findings(&ir, "GET /mixed"); + assert_eq!( + blockers.len(), + 1, + "only the malformed sibling blocks: {:?}", + ir.findings + ); + assert_eq!( + blockers[0].location.pointer, "/security", + "fail-closed blocker must point at the malformed array: {:?}", + blockers[0] + ); + assert!(all_findings(&ir).iter().any(|finding| { + finding.code == "invalid_security_requirement" + && finding.severity == ImportFindingSeverity::Warning + && finding.location.pointer == "/security/1" + })); + assert!( + candidate(&preview, "GET /mixed") + .findings + .iter() + .any(|finding| finding.severity == ImportFindingSeverity::Error), + "the operation must not become public because of a malformed sibling: {preview:?}" + ); +} + +#[test] +fn swagger2_non_array_root_security_blocks_inherited_operations() { + let mut fixture = security_fixture_swagger2(); + fixture.insert("security".to_owned(), json!("apikey")); + fixture.insert( + "paths".to_owned(), + json!({ + "/one": get200("oneOpS2", None), + "/two": get200("twoOpS2", None) + }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + for key in ["GET /one", "GET /two"] { + let blockers = security_findings(&ir, key); + assert_eq!( + blockers.len(), + 1, + "exactly one blocker for {key}: {:?}", + ir.findings + ); + let blocker = &blockers[0]; + assert_eq!( + blocker.code, "invalid_security_requirements", + "stable finding code: {blocker:?}" + ); + assert_eq!( + blocker.severity, + ImportFindingSeverity::Error, + "stable finding severity: {blocker:?}" + ); + assert_eq!( + blocker.location.pointer, "/security", + "blocker must point at the malformed root security: {blocker:?}" + ); + assert!( + candidate(&preview, key).findings.iter().any(|finding| { + finding.code == "invalid_security_requirements" + && finding.severity == ImportFindingSeverity::Error + }), + "{key} candidate must be blocked" + ); + } +} + +#[test] +fn swagger2_malformed_local_security_affects_only_that_operation() { + let mut fixture = security_fixture_swagger2(); + // A well-formed root requirement proves priority: the malformed local + // security wins for /bad (no root blocker may be added), while /public + // relies on an explicit empty override to stay public. + fixture.insert("security".to_owned(), json!([{ "apiKey": [] }])); + fixture.insert( + "paths".to_owned(), + json!({ + "/bad": get200("badOpS2", Some(json!("apikey"))), + "/public": get200("publicOpS2", Some(json!([]))) + }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + let blockers = security_findings(&ir, "GET /bad"); + assert_eq!(blockers.len(), 1, "exactly one blocker: {:?}", ir.findings); + assert_eq!(blockers[0].code, "invalid_security_requirements"); + assert_eq!(blockers[0].severity, ImportFindingSeverity::Error); + assert_eq!( + blockers[0].location.pointer, "/paths/~1bad/get/security", + "blocker must point at the malformed local security: {:?}", + blockers[0] + ); + assert!( + security_findings(&ir, "GET /public").is_empty(), + "the public neighbour must not inherit a local defect: {:?}", + ir.findings + ); + assert!( + candidate(&preview, "GET /public") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error), + "public neighbour must stay actionable: {preview:?}" + ); +} + +#[test] +fn swagger2_public_alternative_with_malformed_sibling_does_not_go_public() { + let mut fixture = security_fixture_swagger2(); + fixture.insert("security".to_owned(), json!([{}, "apikey"])); + fixture.insert( + "paths".to_owned(), + json!({ "/mixed": get200("mixedOpS2", None) }), + ); + let (ir, preview) = run(&Value::Object(fixture)); + let blockers = security_findings(&ir, "GET /mixed"); + assert_eq!( + blockers.len(), + 1, + "only the malformed sibling blocks: {:?}", + ir.findings + ); + assert_eq!( + blockers[0].location.pointer, "/security", + "fail-closed blocker must point at the malformed array: {:?}", + blockers[0] + ); + assert!(all_findings(&ir).iter().any(|finding| { + finding.code == "invalid_security_requirement" + && finding.severity == ImportFindingSeverity::Warning + && finding.location.pointer == "/security/1" + })); + assert!( + candidate(&preview, "GET /mixed") + .findings + .iter() + .any(|finding| finding.severity == ImportFindingSeverity::Error), + "the operation must not become public because of a malformed sibling: {preview:?}" + ); +} + +#[test] +fn empty_security_array_stays_public() { + let source = json!({ + "openapi": "3.0.3", + "info": { "title": "Security scope regression", "version": "1" }, + "servers": [{ "url": "https://security-scope.example.test" }], + "security": [{ "probe": [] }], + "paths": { + "/public": { "get": { "security": [], "operationId": "publicOp", + "responses": { "200": { "description": "ok" } } } } + } + }); + let (ir, preview) = run(&source); + assert!( + security_findings(&ir, "GET /public").is_empty(), + "explicit empty override must stay public: {:?}", + ir.findings + ); + assert!( + candidate(&preview, "GET /public") + .findings + .iter() + .all(|finding| finding.severity != ImportFindingSeverity::Error), + "overridden operation must remain actionable" + ); +} diff --git a/crates/crank-import/tests/support-matrix.yaml b/crates/crank-import/tests/support-matrix.yaml new file mode 100644 index 0000000..0b8666b --- /dev/null +++ b/crates/crank-import/tests/support-matrix.yaml @@ -0,0 +1,1953 @@ +matrix_version: 1.0.3 +compatibility_impact: "1.0.3 adds executable blockers for undeclared path placeholders and object/array schema defaults in OpenAPI 3.0, OpenAPI 3.1, and Swagger 2.0. The 1.0.2 effective-security inheritance/override behavior remains covered; scalar defaults and previously supported importer behavior remain unchanged." +constructs: + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.get" + status: supported + preserved_semantics: "GET method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.post" + status: supported + preserved_semantics: "POST method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.put" + status: supported + preserved_semantics: "PUT method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.patch" + status: supported + preserved_semantics: "PATCH method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.delete" + status: supported + preserved_semantics: "DELETE method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.head" + status: supported + preserved_semantics: "HEAD method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.options" + status: supported + preserved_semantics: "OPTIONS method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "parameter.location.path" + status: supported + preserved_semantics: "path parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "parameter.location.query" + status: supported + preserved_semantics: "query parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "parameter.location.header" + status: supported + preserved_semantics: "header parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "parameter.location.cookie" + status: supported + preserved_semantics: "cookie parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "request_body.application/json" + status: supported + preserved_semantics: "application/json request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "request_body.application/x-www-form-urlencoded" + status: supported + preserved_semantics: "application/x-www-form-urlencoded request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "request_body.multipart/form-data" + status: supported + preserved_semantics: "multipart/form-data request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.200" + status: supported + preserved_semantics: "Success response status 200 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.201" + status: supported + preserved_semantics: "Success response status 201 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.202" + status: supported + preserved_semantics: "Success response status 202 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.203" + status: supported + preserved_semantics: "Success response status 203 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.204" + status: supported + preserved_semantics: "Success response status 204 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.205" + status: supported + preserved_semantics: "Success response status 205 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.206" + status: supported + preserved_semantics: "Success response status 206 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.207" + status: supported + preserved_semantics: "Success response status 207 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.208" + status: supported + preserved_semantics: "Success response status 208 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.209" + status: supported + preserved_semantics: "Success response status 209 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.210" + status: supported + preserved_semantics: "Success response status 210 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.211" + status: supported + preserved_semantics: "Success response status 211 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.212" + status: supported + preserved_semantics: "Success response status 212 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.213" + status: supported + preserved_semantics: "Success response status 213 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.214" + status: supported + preserved_semantics: "Success response status 214 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.215" + status: supported + preserved_semantics: "Success response status 215 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.216" + status: supported + preserved_semantics: "Success response status 216 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.217" + status: supported + preserved_semantics: "Success response status 217 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.218" + status: supported + preserved_semantics: "Success response status 218 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.219" + status: supported + preserved_semantics: "Success response status 219 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.220" + status: supported + preserved_semantics: "Success response status 220 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.221" + status: supported + preserved_semantics: "Success response status 221 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.222" + status: supported + preserved_semantics: "Success response status 222 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.223" + status: supported + preserved_semantics: "Success response status 223 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.224" + status: supported + preserved_semantics: "Success response status 224 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.225" + status: supported + preserved_semantics: "Success response status 225 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.226" + status: supported + preserved_semantics: "Success response status 226 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.227" + status: supported + preserved_semantics: "Success response status 227 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.228" + status: supported + preserved_semantics: "Success response status 228 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.229" + status: supported + preserved_semantics: "Success response status 229 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.230" + status: supported + preserved_semantics: "Success response status 230 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.231" + status: supported + preserved_semantics: "Success response status 231 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.232" + status: supported + preserved_semantics: "Success response status 232 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.233" + status: supported + preserved_semantics: "Success response status 233 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.234" + status: supported + preserved_semantics: "Success response status 234 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.235" + status: supported + preserved_semantics: "Success response status 235 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.236" + status: supported + preserved_semantics: "Success response status 236 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.237" + status: supported + preserved_semantics: "Success response status 237 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.238" + status: supported + preserved_semantics: "Success response status 238 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.239" + status: supported + preserved_semantics: "Success response status 239 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.240" + status: supported + preserved_semantics: "Success response status 240 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.241" + status: supported + preserved_semantics: "Success response status 241 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.242" + status: supported + preserved_semantics: "Success response status 242 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.243" + status: supported + preserved_semantics: "Success response status 243 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.244" + status: supported + preserved_semantics: "Success response status 244 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.245" + status: supported + preserved_semantics: "Success response status 245 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.246" + status: supported + preserved_semantics: "Success response status 246 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.247" + status: supported + preserved_semantics: "Success response status 247 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.248" + status: supported + preserved_semantics: "Success response status 248 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.249" + status: supported + preserved_semantics: "Success response status 249 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.250" + status: supported + preserved_semantics: "Success response status 250 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.251" + status: supported + preserved_semantics: "Success response status 251 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.252" + status: supported + preserved_semantics: "Success response status 252 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.253" + status: supported + preserved_semantics: "Success response status 253 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.254" + status: supported + preserved_semantics: "Success response status 254 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.255" + status: supported + preserved_semantics: "Success response status 255 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.256" + status: supported + preserved_semantics: "Success response status 256 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.257" + status: supported + preserved_semantics: "Success response status 257 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.258" + status: supported + preserved_semantics: "Success response status 258 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.259" + status: supported + preserved_semantics: "Success response status 259 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.260" + status: supported + preserved_semantics: "Success response status 260 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.261" + status: supported + preserved_semantics: "Success response status 261 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.262" + status: supported + preserved_semantics: "Success response status 262 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.263" + status: supported + preserved_semantics: "Success response status 263 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.264" + status: supported + preserved_semantics: "Success response status 264 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.265" + status: supported + preserved_semantics: "Success response status 265 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.266" + status: supported + preserved_semantics: "Success response status 266 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.267" + status: supported + preserved_semantics: "Success response status 267 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.268" + status: supported + preserved_semantics: "Success response status 268 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.269" + status: supported + preserved_semantics: "Success response status 269 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.270" + status: supported + preserved_semantics: "Success response status 270 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.271" + status: supported + preserved_semantics: "Success response status 271 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.272" + status: supported + preserved_semantics: "Success response status 272 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.273" + status: supported + preserved_semantics: "Success response status 273 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.274" + status: supported + preserved_semantics: "Success response status 274 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.275" + status: supported + preserved_semantics: "Success response status 275 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.276" + status: supported + preserved_semantics: "Success response status 276 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.277" + status: supported + preserved_semantics: "Success response status 277 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.278" + status: supported + preserved_semantics: "Success response status 278 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.279" + status: supported + preserved_semantics: "Success response status 279 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.280" + status: supported + preserved_semantics: "Success response status 280 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.281" + status: supported + preserved_semantics: "Success response status 281 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.282" + status: supported + preserved_semantics: "Success response status 282 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.283" + status: supported + preserved_semantics: "Success response status 283 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.284" + status: supported + preserved_semantics: "Success response status 284 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.285" + status: supported + preserved_semantics: "Success response status 285 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.286" + status: supported + preserved_semantics: "Success response status 286 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.287" + status: supported + preserved_semantics: "Success response status 287 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.288" + status: supported + preserved_semantics: "Success response status 288 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.289" + status: supported + preserved_semantics: "Success response status 289 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.290" + status: supported + preserved_semantics: "Success response status 290 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.291" + status: supported + preserved_semantics: "Success response status 291 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.292" + status: supported + preserved_semantics: "Success response status 292 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.293" + status: supported + preserved_semantics: "Success response status 293 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.294" + status: supported + preserved_semantics: "Success response status 294 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.295" + status: supported + preserved_semantics: "Success response status 295 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.296" + status: supported + preserved_semantics: "Success response status 296 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.297" + status: supported + preserved_semantics: "Success response status 297 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.298" + status: supported + preserved_semantics: "Success response status 298 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "response.299" + status: supported + preserved_semantics: "Success response status 299 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "reference.local" + status: supported + preserved_semantics: "$ref within the uploaded document resolves without network access." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "reference.external_allowlisted" + status: supported + preserved_semantics: "Materialized allowlisted external snapshot resolves by immutable URI and digest without runtime fetch." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "composition.allOf" + status: supported + preserved_semantics: "allOf members are merged while preserving property, required, and constraint semantics." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "composition.oneOf" + status: supported + preserved_semantics: "oneOf alternatives remain explicit request/response variants." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "composition.anyOf" + status: supported + preserved_semantics: "anyOf alternatives remain explicit request/response variants." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "security.bearer" + status: supported + preserved_semantics: "Bearer scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "security.basic" + status: supported + preserved_semantics: "Basic scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "security.api_key" + status: supported + preserved_semantics: "API key location/name and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "security.override_empty" + status: supported + preserved_semantics: "An operation-level empty security array disables document inheritance; the operation remains an actionable candidate without a security blocker." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "security.mixed_operations" + status: supported + preserved_semantics: "Effective security metadata is retained on the inherited operation; explicit empty overrides remain public and actionable." + expected_finding: "none" + - specification_version: "OpenAPI 3.0" + construct_id: "media_type.application_xml.request" + status: unsupported + preserved_semantics: "XML request bodies are unsupported as actionable Drafts." + expected_finding: "unsupported_xml_request_media_type" + - specification_version: "OpenAPI 3.0" + construct_id: "media_type.application_xml.response" + status: unsupported + preserved_semantics: "XML response bodies are unsupported as actionable Drafts." + expected_finding: "unsupported_xml_response_media_type" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.trace" + status: unsupported + preserved_semantics: "TRACE operations are excluded and reported as a source-located blocker." + expected_finding: "unsupported_http_method" + - specification_version: "OpenAPI 3.0" + construct_id: "http.method.connect" + status: unsupported + preserved_semantics: "CONNECT operations are excluded and reported as a source-located blocker." + expected_finding: "unsupported_http_method" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.get" + status: supported + preserved_semantics: "GET method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.post" + status: supported + preserved_semantics: "POST method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.put" + status: supported + preserved_semantics: "PUT method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.patch" + status: supported + preserved_semantics: "PATCH method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.delete" + status: supported + preserved_semantics: "DELETE method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.head" + status: supported + preserved_semantics: "HEAD method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.options" + status: supported + preserved_semantics: "OPTIONS method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "parameter.location.path" + status: supported + preserved_semantics: "path parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "parameter.location.query" + status: supported + preserved_semantics: "query parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "parameter.location.header" + status: supported + preserved_semantics: "header parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "parameter.location.cookie" + status: supported + preserved_semantics: "cookie parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "request_body.application/json" + status: supported + preserved_semantics: "application/json request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "request_body.application/x-www-form-urlencoded" + status: supported + preserved_semantics: "application/x-www-form-urlencoded request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "request_body.multipart/form-data" + status: supported + preserved_semantics: "multipart/form-data request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.200" + status: supported + preserved_semantics: "Success response status 200 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.201" + status: supported + preserved_semantics: "Success response status 201 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.202" + status: supported + preserved_semantics: "Success response status 202 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.203" + status: supported + preserved_semantics: "Success response status 203 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.204" + status: supported + preserved_semantics: "Success response status 204 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.205" + status: supported + preserved_semantics: "Success response status 205 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.206" + status: supported + preserved_semantics: "Success response status 206 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.207" + status: supported + preserved_semantics: "Success response status 207 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.208" + status: supported + preserved_semantics: "Success response status 208 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.209" + status: supported + preserved_semantics: "Success response status 209 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.210" + status: supported + preserved_semantics: "Success response status 210 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.211" + status: supported + preserved_semantics: "Success response status 211 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.212" + status: supported + preserved_semantics: "Success response status 212 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.213" + status: supported + preserved_semantics: "Success response status 213 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.214" + status: supported + preserved_semantics: "Success response status 214 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.215" + status: supported + preserved_semantics: "Success response status 215 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.216" + status: supported + preserved_semantics: "Success response status 216 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.217" + status: supported + preserved_semantics: "Success response status 217 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.218" + status: supported + preserved_semantics: "Success response status 218 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.219" + status: supported + preserved_semantics: "Success response status 219 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.220" + status: supported + preserved_semantics: "Success response status 220 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.221" + status: supported + preserved_semantics: "Success response status 221 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.222" + status: supported + preserved_semantics: "Success response status 222 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.223" + status: supported + preserved_semantics: "Success response status 223 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.224" + status: supported + preserved_semantics: "Success response status 224 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.225" + status: supported + preserved_semantics: "Success response status 225 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.226" + status: supported + preserved_semantics: "Success response status 226 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.227" + status: supported + preserved_semantics: "Success response status 227 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.228" + status: supported + preserved_semantics: "Success response status 228 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.229" + status: supported + preserved_semantics: "Success response status 229 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.230" + status: supported + preserved_semantics: "Success response status 230 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.231" + status: supported + preserved_semantics: "Success response status 231 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.232" + status: supported + preserved_semantics: "Success response status 232 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.233" + status: supported + preserved_semantics: "Success response status 233 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.234" + status: supported + preserved_semantics: "Success response status 234 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.235" + status: supported + preserved_semantics: "Success response status 235 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.236" + status: supported + preserved_semantics: "Success response status 236 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.237" + status: supported + preserved_semantics: "Success response status 237 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.238" + status: supported + preserved_semantics: "Success response status 238 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.239" + status: supported + preserved_semantics: "Success response status 239 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.240" + status: supported + preserved_semantics: "Success response status 240 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.241" + status: supported + preserved_semantics: "Success response status 241 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.242" + status: supported + preserved_semantics: "Success response status 242 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.243" + status: supported + preserved_semantics: "Success response status 243 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.244" + status: supported + preserved_semantics: "Success response status 244 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.245" + status: supported + preserved_semantics: "Success response status 245 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.246" + status: supported + preserved_semantics: "Success response status 246 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.247" + status: supported + preserved_semantics: "Success response status 247 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.248" + status: supported + preserved_semantics: "Success response status 248 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.249" + status: supported + preserved_semantics: "Success response status 249 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.250" + status: supported + preserved_semantics: "Success response status 250 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.251" + status: supported + preserved_semantics: "Success response status 251 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.252" + status: supported + preserved_semantics: "Success response status 252 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.253" + status: supported + preserved_semantics: "Success response status 253 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.254" + status: supported + preserved_semantics: "Success response status 254 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.255" + status: supported + preserved_semantics: "Success response status 255 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.256" + status: supported + preserved_semantics: "Success response status 256 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.257" + status: supported + preserved_semantics: "Success response status 257 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.258" + status: supported + preserved_semantics: "Success response status 258 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.259" + status: supported + preserved_semantics: "Success response status 259 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.260" + status: supported + preserved_semantics: "Success response status 260 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.261" + status: supported + preserved_semantics: "Success response status 261 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.262" + status: supported + preserved_semantics: "Success response status 262 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.263" + status: supported + preserved_semantics: "Success response status 263 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.264" + status: supported + preserved_semantics: "Success response status 264 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.265" + status: supported + preserved_semantics: "Success response status 265 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.266" + status: supported + preserved_semantics: "Success response status 266 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.267" + status: supported + preserved_semantics: "Success response status 267 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.268" + status: supported + preserved_semantics: "Success response status 268 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.269" + status: supported + preserved_semantics: "Success response status 269 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.270" + status: supported + preserved_semantics: "Success response status 270 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.271" + status: supported + preserved_semantics: "Success response status 271 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.272" + status: supported + preserved_semantics: "Success response status 272 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.273" + status: supported + preserved_semantics: "Success response status 273 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.274" + status: supported + preserved_semantics: "Success response status 274 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.275" + status: supported + preserved_semantics: "Success response status 275 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.276" + status: supported + preserved_semantics: "Success response status 276 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.277" + status: supported + preserved_semantics: "Success response status 277 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.278" + status: supported + preserved_semantics: "Success response status 278 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.279" + status: supported + preserved_semantics: "Success response status 279 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.280" + status: supported + preserved_semantics: "Success response status 280 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.281" + status: supported + preserved_semantics: "Success response status 281 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.282" + status: supported + preserved_semantics: "Success response status 282 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.283" + status: supported + preserved_semantics: "Success response status 283 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.284" + status: supported + preserved_semantics: "Success response status 284 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.285" + status: supported + preserved_semantics: "Success response status 285 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.286" + status: supported + preserved_semantics: "Success response status 286 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.287" + status: supported + preserved_semantics: "Success response status 287 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.288" + status: supported + preserved_semantics: "Success response status 288 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.289" + status: supported + preserved_semantics: "Success response status 289 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.290" + status: supported + preserved_semantics: "Success response status 290 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.291" + status: supported + preserved_semantics: "Success response status 291 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.292" + status: supported + preserved_semantics: "Success response status 292 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.293" + status: supported + preserved_semantics: "Success response status 293 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.294" + status: supported + preserved_semantics: "Success response status 294 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.295" + status: supported + preserved_semantics: "Success response status 295 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.296" + status: supported + preserved_semantics: "Success response status 296 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.297" + status: supported + preserved_semantics: "Success response status 297 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.298" + status: supported + preserved_semantics: "Success response status 298 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "response.299" + status: supported + preserved_semantics: "Success response status 299 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "reference.local" + status: supported + preserved_semantics: "$ref within the uploaded document resolves without network access." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "reference.external_allowlisted" + status: supported + preserved_semantics: "Materialized allowlisted external snapshot resolves by immutable URI and digest without runtime fetch." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "composition.allOf" + status: supported + preserved_semantics: "allOf members are merged while preserving property, required, and constraint semantics." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "composition.oneOf" + status: supported + preserved_semantics: "oneOf alternatives remain explicit request/response variants." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "composition.anyOf" + status: supported + preserved_semantics: "anyOf alternatives remain explicit request/response variants." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "security.bearer" + status: supported + preserved_semantics: "Bearer scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "security.basic" + status: supported + preserved_semantics: "Basic scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "security.api_key" + status: supported + preserved_semantics: "API key location/name and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "security.override_empty" + status: supported + preserved_semantics: "An operation-level empty security array disables document inheritance; the operation remains an actionable candidate without a security blocker." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "security.mixed_operations" + status: supported + preserved_semantics: "Effective security metadata is retained on the inherited operation; explicit empty overrides remain public and actionable." + expected_finding: "none" + - specification_version: "OpenAPI 3.1" + construct_id: "media_type.application_xml.request" + status: unsupported + preserved_semantics: "XML request bodies are unsupported as actionable Drafts." + expected_finding: "unsupported_xml_request_media_type" + - specification_version: "OpenAPI 3.1" + construct_id: "media_type.application_xml.response" + status: unsupported + preserved_semantics: "XML response bodies are unsupported as actionable Drafts." + expected_finding: "unsupported_xml_response_media_type" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.trace" + status: unsupported + preserved_semantics: "TRACE operations are excluded and reported as a source-located blocker." + expected_finding: "unsupported_http_method" + - specification_version: "OpenAPI 3.1" + construct_id: "http.method.connect" + status: unsupported + preserved_semantics: "CONNECT operations are excluded and reported as a source-located blocker." + expected_finding: "unsupported_http_method" + - specification_version: "Swagger 2.0" + construct_id: "http.method.get" + status: supported + preserved_semantics: "GET method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "http.method.post" + status: supported + preserved_semantics: "POST method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "http.method.put" + status: supported + preserved_semantics: "PUT method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "http.method.patch" + status: supported + preserved_semantics: "PATCH method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "http.method.delete" + status: supported + preserved_semantics: "DELETE method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "http.method.head" + status: supported + preserved_semantics: "HEAD method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "http.method.options" + status: supported + preserved_semantics: "OPTIONS method and source path are retained as the candidate HTTP method and source path." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "parameter.location.path" + status: supported + preserved_semantics: "path parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "parameter.location.query" + status: supported + preserved_semantics: "query parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "parameter.location.header" + status: supported + preserved_semantics: "header parameter name, schema, required flag, and source location are retained in the typed input mapping." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "parameter.location.cookie" + status: unsupported + preserved_semantics: "Swagger 2.0 cookie parameters are not imported as actionable Drafts." + expected_finding: "unsupported_cookie_parameter" + - specification_version: "Swagger 2.0" + construct_id: "request_body.application/json" + status: supported + preserved_semantics: "application/json request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "request_body.application/x-www-form-urlencoded" + status: supported + preserved_semantics: "application/x-www-form-urlencoded request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "request_body.multipart/form-data" + status: supported + preserved_semantics: "multipart/form-data request schema and content type are retained in the request contract." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.200" + status: supported + preserved_semantics: "Success response status 200 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.201" + status: supported + preserved_semantics: "Success response status 201 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.202" + status: supported + preserved_semantics: "Success response status 202 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.203" + status: supported + preserved_semantics: "Success response status 203 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.204" + status: supported + preserved_semantics: "Success response status 204 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.205" + status: supported + preserved_semantics: "Success response status 205 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.206" + status: supported + preserved_semantics: "Success response status 206 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.207" + status: supported + preserved_semantics: "Success response status 207 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.208" + status: supported + preserved_semantics: "Success response status 208 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.209" + status: supported + preserved_semantics: "Success response status 209 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.210" + status: supported + preserved_semantics: "Success response status 210 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.211" + status: supported + preserved_semantics: "Success response status 211 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.212" + status: supported + preserved_semantics: "Success response status 212 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.213" + status: supported + preserved_semantics: "Success response status 213 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.214" + status: supported + preserved_semantics: "Success response status 214 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.215" + status: supported + preserved_semantics: "Success response status 215 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.216" + status: supported + preserved_semantics: "Success response status 216 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.217" + status: supported + preserved_semantics: "Success response status 217 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.218" + status: supported + preserved_semantics: "Success response status 218 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.219" + status: supported + preserved_semantics: "Success response status 219 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.220" + status: supported + preserved_semantics: "Success response status 220 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.221" + status: supported + preserved_semantics: "Success response status 221 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.222" + status: supported + preserved_semantics: "Success response status 222 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.223" + status: supported + preserved_semantics: "Success response status 223 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.224" + status: supported + preserved_semantics: "Success response status 224 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.225" + status: supported + preserved_semantics: "Success response status 225 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.226" + status: supported + preserved_semantics: "Success response status 226 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.227" + status: supported + preserved_semantics: "Success response status 227 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.228" + status: supported + preserved_semantics: "Success response status 228 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.229" + status: supported + preserved_semantics: "Success response status 229 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.230" + status: supported + preserved_semantics: "Success response status 230 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.231" + status: supported + preserved_semantics: "Success response status 231 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.232" + status: supported + preserved_semantics: "Success response status 232 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.233" + status: supported + preserved_semantics: "Success response status 233 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.234" + status: supported + preserved_semantics: "Success response status 234 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.235" + status: supported + preserved_semantics: "Success response status 235 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.236" + status: supported + preserved_semantics: "Success response status 236 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.237" + status: supported + preserved_semantics: "Success response status 237 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.238" + status: supported + preserved_semantics: "Success response status 238 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.239" + status: supported + preserved_semantics: "Success response status 239 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.240" + status: supported + preserved_semantics: "Success response status 240 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.241" + status: supported + preserved_semantics: "Success response status 241 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.242" + status: supported + preserved_semantics: "Success response status 242 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.243" + status: supported + preserved_semantics: "Success response status 243 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.244" + status: supported + preserved_semantics: "Success response status 244 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.245" + status: supported + preserved_semantics: "Success response status 245 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.246" + status: supported + preserved_semantics: "Success response status 246 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.247" + status: supported + preserved_semantics: "Success response status 247 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.248" + status: supported + preserved_semantics: "Success response status 248 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.249" + status: supported + preserved_semantics: "Success response status 249 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.250" + status: supported + preserved_semantics: "Success response status 250 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.251" + status: supported + preserved_semantics: "Success response status 251 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.252" + status: supported + preserved_semantics: "Success response status 252 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.253" + status: supported + preserved_semantics: "Success response status 253 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.254" + status: supported + preserved_semantics: "Success response status 254 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.255" + status: supported + preserved_semantics: "Success response status 255 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.256" + status: supported + preserved_semantics: "Success response status 256 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.257" + status: supported + preserved_semantics: "Success response status 257 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.258" + status: supported + preserved_semantics: "Success response status 258 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.259" + status: supported + preserved_semantics: "Success response status 259 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.260" + status: supported + preserved_semantics: "Success response status 260 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.261" + status: supported + preserved_semantics: "Success response status 261 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.262" + status: supported + preserved_semantics: "Success response status 262 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.263" + status: supported + preserved_semantics: "Success response status 263 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.264" + status: supported + preserved_semantics: "Success response status 264 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.265" + status: supported + preserved_semantics: "Success response status 265 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.266" + status: supported + preserved_semantics: "Success response status 266 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.267" + status: supported + preserved_semantics: "Success response status 267 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.268" + status: supported + preserved_semantics: "Success response status 268 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.269" + status: supported + preserved_semantics: "Success response status 269 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.270" + status: supported + preserved_semantics: "Success response status 270 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.271" + status: supported + preserved_semantics: "Success response status 271 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.272" + status: supported + preserved_semantics: "Success response status 272 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.273" + status: supported + preserved_semantics: "Success response status 273 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.274" + status: supported + preserved_semantics: "Success response status 274 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.275" + status: supported + preserved_semantics: "Success response status 275 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.276" + status: supported + preserved_semantics: "Success response status 276 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.277" + status: supported + preserved_semantics: "Success response status 277 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.278" + status: supported + preserved_semantics: "Success response status 278 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.279" + status: supported + preserved_semantics: "Success response status 279 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.280" + status: supported + preserved_semantics: "Success response status 280 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.281" + status: supported + preserved_semantics: "Success response status 281 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.282" + status: supported + preserved_semantics: "Success response status 282 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.283" + status: supported + preserved_semantics: "Success response status 283 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.284" + status: supported + preserved_semantics: "Success response status 284 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.285" + status: supported + preserved_semantics: "Success response status 285 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.286" + status: supported + preserved_semantics: "Success response status 286 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.287" + status: supported + preserved_semantics: "Success response status 287 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.288" + status: supported + preserved_semantics: "Success response status 288 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.289" + status: supported + preserved_semantics: "Success response status 289 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.290" + status: supported + preserved_semantics: "Success response status 290 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.291" + status: supported + preserved_semantics: "Success response status 291 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.292" + status: supported + preserved_semantics: "Success response status 292 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.293" + status: supported + preserved_semantics: "Success response status 293 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.294" + status: supported + preserved_semantics: "Success response status 294 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.295" + status: supported + preserved_semantics: "Success response status 295 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.296" + status: supported + preserved_semantics: "Success response status 296 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.297" + status: supported + preserved_semantics: "Success response status 297 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.298" + status: supported + preserved_semantics: "Success response status 298 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "response.299" + status: supported + preserved_semantics: "Success response status 299 and its response schema are retained; bodyless responses stay bodyless." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "reference.local" + status: supported + preserved_semantics: "$ref within the uploaded document resolves without network access." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "reference.external_allowlisted" + status: supported + preserved_semantics: "Materialized allowlisted external snapshot resolves by immutable URI and digest without runtime fetch." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "composition.allOf" + status: supported + preserved_semantics: "allOf members are merged while preserving property, required, and constraint semantics." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "composition.oneOf" + status: supported + preserved_semantics: "oneOf alternatives remain explicit request/response variants." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "composition.anyOf" + status: supported + preserved_semantics: "anyOf alternatives remain explicit request/response variants." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "security.bearer" + status: unsupported + preserved_semantics: "Swagger OAuth2 semantics require manual setup; OAuth credentials are never copied into the import preview." + expected_finding: "unsupported_security_requirements" + - specification_version: "Swagger 2.0" + construct_id: "security.basic" + status: supported + preserved_semantics: "Basic scheme metadata and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "security.api_key" + status: supported + preserved_semantics: "API key location/name and OR alternatives are retained without credentials; the import API resolves an Auth Profile binding." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "security.override_empty" + status: supported + preserved_semantics: "An operation-level empty security array disables document inheritance; the operation remains an actionable candidate without a security blocker." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "security.mixed_operations" + status: supported + preserved_semantics: "Effective basic security metadata is retained on the inherited operation; explicit empty overrides remain public and actionable." + expected_finding: "none" + - specification_version: "Swagger 2.0" + construct_id: "media_type.application_xml.request" + status: unsupported + preserved_semantics: "XML request bodies are unsupported as actionable Drafts." + expected_finding: "unsupported_consumes" + - specification_version: "Swagger 2.0" + construct_id: "media_type.application_xml.response" + status: unsupported + preserved_semantics: "XML response bodies are unsupported as actionable Drafts." + expected_finding: "unsupported_produces" + - specification_version: "Swagger 2.0" + construct_id: "http.method.trace" + status: unsupported + preserved_semantics: "TRACE operations are excluded and reported as a source-located blocker." + expected_finding: "unsupported_http_method" + - specification_version: "Swagger 2.0" + construct_id: "http.method.connect" + status: unsupported + preserved_semantics: "CONNECT operations are excluded and reported as a source-located blocker." + expected_finding: "unsupported_http_method" + - specification_version: "OpenAPI 3.0" + construct_id: "parameter.path.declaration" + status: unsupported + preserved_semantics: "A path placeholder without an effective path parameter is reported at its existing operation source node." + expected_finding: "path_parameter_not_declared" + - specification_version: "OpenAPI 3.1" + construct_id: "parameter.path.declaration" + status: unsupported + preserved_semantics: "A path placeholder without an effective path parameter is reported at its existing operation source node." + expected_finding: "path_parameter_not_declared" + - specification_version: "Swagger 2.0" + construct_id: "parameter.path.declaration" + status: unsupported + preserved_semantics: "A path placeholder without an effective path parameter is reported at its existing operation source node." + expected_finding: "path_parameter_not_declared" + - specification_version: "OpenAPI 3.0" + construct_id: "schema.default.compound" + status: unsupported + preserved_semantics: "Object and array schema defaults are blocked; scalar defaults remain preserved." + expected_finding: "unsupported_compound_default" + - specification_version: "OpenAPI 3.1" + construct_id: "schema.default.compound" + status: unsupported + preserved_semantics: "Object and array schema defaults are blocked; scalar defaults remain preserved." + expected_finding: "unsupported_compound_default" + - specification_version: "Swagger 2.0" + construct_id: "schema.default.compound" + status: unsupported + preserved_semantics: "Object and array schema defaults are blocked; scalar defaults remain preserved." + expected_finding: "unsupported_compound_default" diff --git a/crates/crank-metrics/src/schema.rs b/crates/crank-metrics/src/schema.rs index 5102fad..429fe1e 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_563; +pub const MAX_RENDERED_SERIES_PER_PROCESS: usize = 17_638; pub const MAX_EXPOSITION_BYTES: usize = 8 * 1024 * 1024; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -180,9 +180,14 @@ pub const HTTP_ROUTE_DOMAIN: &[&str] = &[ "/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}/onboarding/import-context", + "/api/admin/workspaces/{workspace_id}/onboarding/select-draft", "/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/plans/{plan_id}", + "/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 +309,7 @@ const METRIC_SCHEMA: &[MetricDefinition] = &[ ], &[], BOTH, - 67 * 10 * 6, + HTTP_ROUTE_DOMAIN.len() * 10 * 6, "Total HTTP requests.", ), metric( @@ -318,7 +323,7 @@ const METRIC_SCHEMA: &[MetricDefinition] = &[ ], DURATION_BUCKETS_SECONDS, BOTH, - 67 * 10, + HTTP_ROUTE_DOMAIN.len() * 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..9929167 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_562, 17_637).is_ok()); + assert!(validate_budget(5_563, 17_638).is_ok()); + assert!(validate_budget(5_564, 16_000).is_err()); + assert!(validate_budget(5_200, 17_639).is_err()); + assert_eq!(crank_metrics::max_exemplar_slots(), 11_270); } #[test] fn every_registered_route_round_trips_without_dynamic_segments() { - assert_eq!(HTTP_ROUTE_DOMAIN.len(), 67); + assert_eq!(HTTP_ROUTE_DOMAIN.len(), 72); 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_563, + rendered_series: 17_638 }) ); } diff --git a/crates/crank-registry/src/error.rs b/crates/crank-registry/src/error.rs index e849908..f8e3e9d 100644 --- a/crates/crank-registry/src/error.rs +++ b/crates/crank-registry/src/error.rs @@ -136,8 +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("import auth profile binding is unavailable")] - ImportAuthBindingUnavailable, + #[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}")] @@ -150,6 +182,14 @@ pub enum RegistryError { OnboardingStaleRevision, #[error("onboarding domain steps are not complete")] OnboardingIncomplete, + #[error( + "onboarding import Draft is not an actionable Draft produced by a committed import apply" + )] + OnboardingImportDraftUnavailable, + #[error("no committed import apply report matches the requested application identity")] + OnboardingImportApplicationUnavailable, + #[error("the workspace has no durable import context matching the requested application")] + OnboardingImportContextUnavailable, #[error("artifact source {source_id} was not found")] SourceNotFound { source_id: String }, #[error("artifact source {source_id} conflicts with immutable metadata or lifecycle")] diff --git a/crates/crank-registry/src/lib.rs b/crates/crank-registry/src/lib.rs index 34bb276..42e9d96 100644 --- a/crates/crank-registry/src/lib.rs +++ b/crates/crank-registry/src/lib.rs @@ -21,41 +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, ImportAuthRequirement, ImportConflictMode, - ImportJobSourceEnvelope, ImportOperationDraft, ListApprovalRequestsQuery, - ListArtifactSourcesQuery, ListInvocationLogsQuery, ListProductEventsQuery, - MasterKeyIdentityCandidate, PublishAgentRequest, PublishRequest, + 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, + RecoverAdminPasswordRequest, RegisterOnboardingImportContextRequest, RotateSecretRequest, + SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, + SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, + UpdateAgentSummaryRequest, UpdateWorkspaceRequest, UsageQuery, }; } @@ -71,36 +74,41 @@ 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, ImportAuthRequirement, - ImportConflictMode, ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId, - ImportJobKind, ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, + ExpireApprovalRequest, FinishApprovalRequest, FinishImportJobRequest, ImportConflictMode, + ImportJob, ImportJobApplyResult, ImportJobCleanupReport, ImportJobId, ImportJobKind, + 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, - SaveWorkspaceUpstreamRequest, SecretRecord, SecretVersionRecord, SessionRecord, + MasterKeyRotationStatus, MembershipRecord, OnboardingEventSnapshot, OnboardingImportChoice, + OnboardingImportContext, OnboardingImportDrift, OnboardingMilestoneResult, + OnboardingPresentationMilestone, OnboardingSnapshot, OperationAgentRef, + OperationSampleMetadata, OperationStateExpectation, OperationSummary, OperationUsageSummary, + OperationVersionRecord, Page, PlatformApiKeyRecord, ProductEventRecord, PublishAgentRequest, + PublishRequest, PublishedAgentCatalog, PublishedAgentTool, RecordOnboardingCompletionRequest, + RecordOnboardingMilestoneRequest, RecoverAdminPasswordRequest, + RegisterOnboardingImportContextRequest, RegistryOperation, RotateSecretRequest, SampleKind, + SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, + SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, + SecretRecord, SecretVersionRecord, SelectOnboardingImportDraftRequest, SessionRecord, SkippedImportOperation, UpdateAgentSummaryRequest, UpdateWorkspaceRequest, UsageAgentBreakdown, UsageBucket, UsageOperationBreakdown, UsageOutcomeBreakdown, UsageOutcomeGroup, UsageQuery, UsageRollupRecord, UsageSummary, UsageTimelinePoint, VerifiedArtifactSource, diff --git a/crates/crank-registry/src/migrations.rs b/crates/crank-registry/src/migrations.rs index 9973ba2..3edda8d 100644 --- a/crates/crank-registry/src/migrations.rs +++ b/crates/crank-registry/src/migrations.rs @@ -6,7 +6,9 @@ mod artifact_metadata_v12; mod authority; mod baseline_v1; mod execution_outcome_v5; +mod import_plan_v14; mod master_key_identity_v7; +mod onboarding_import_selection_v15; mod onboarding_product_events_v11; mod owned_relations; mod platform_key_name_reuse_v6; @@ -16,6 +18,8 @@ mod schema_guard_v10; mod schema_guard_v11; mod schema_guard_v12; mod schema_guard_v13; +mod schema_guard_v14; +mod schema_guard_v15; 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..9df4779 100644 --- a/crates/crank-registry/src/migrations/authority.rs +++ b/crates/crank-registry/src/migrations/authority.rs @@ -4,7 +4,9 @@ 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_import_selection_v15; use super::onboarding_product_events_v11; use super::owned_relations; use super::platform_key_name_reuse_v6; @@ -25,8 +27,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 = 15; +const IMPLEMENTED_VERSIONS: &[i64] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const BASELINE_SOURCE_SHA256: &str = "eb1656fc5b4b5be9ee390d237d1d58e4b2274ae5ba9b7ba06a2f3f860dfda675"; const CONSOLIDATION_SOURCE: &str = include_str!("consolidation_v2.sql"); @@ -264,6 +266,13 @@ impl MigrationAuthority { artifact_cleanup_indexes_v13::SOURCE_SHA256, 12, ), + expand_descriptor(14, "import-plan-v14", import_plan_v14::SOURCE_SHA256, 13), + expand_descriptor( + 15, + "onboarding-import-selection-v15", + onboarding_import_selection_v15::SOURCE_SHA256, + 14, + ), ] } pub fn validate_sequence() -> Result<(), MigrationError> { @@ -392,6 +401,12 @@ 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?; + } + if from < 15 { + onboarding_import_selection_v15::apply(&mut transaction, &Self::sequence()[14]).await?; + } match inspect(&mut transaction).await? { MigrationPreflight::Current { version: CURRENT_VERSION, @@ -558,6 +573,14 @@ 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, 15] ); 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/onboarding_import_selection_v15.rs b/crates/crank-registry/src/migrations/onboarding_import_selection_v15.rs new file mode 100644 index 0000000..fadf01b --- /dev/null +++ b/crates/crank-registry/src/migrations/onboarding_import_selection_v15.rs @@ -0,0 +1,44 @@ +use sqlx::{Postgres, Transaction, query}; + +use super::authority::{MigrationDescriptor, MigrationError}; + +pub(super) const SOURCE: &str = include_str!("onboarding_import_selection_v15.sql"); +pub(super) const SOURCE_SHA256: &str = + "0dbe85c649ca227f15ac141e7bf004948d3240c3495489b30e2197980ecd0c5e"; + +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.onboarding_import_selection", + Some(15), + "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(15), + "restore_known_good_backup", + ) + })?; + Ok(()) +} diff --git a/crates/crank-registry/src/migrations/onboarding_import_selection_v15.sql b/crates/crank-registry/src/migrations/onboarding_import_selection_v15.sql new file mode 100644 index 0000000..cde6585 --- /dev/null +++ b/crates/crank-registry/src/migrations/onboarding_import_selection_v15.sql @@ -0,0 +1,81 @@ +-- Guided onboarding OpenAPI import context. +-- +-- A workspace has at most one durable import context. It stores only the +-- identity of an already committed immutable apply report +-- (`application_idempotency_key`, `plan_digest`) plus the explicitly selected +-- operation/version and timestamps. The immutable report itself is never +-- copied or mutated here; recovery re-reads it from +-- `import_plan_applications` by (workspace_id, idempotency_key) and verifies +-- the exact plan digest. This keeps the context alive after the import job and +-- plan are garbage collected. +-- +-- Pending context has null selected operation/version and suppresses generic +-- automatic Operation selection until the administrator explicitly picks one +-- actionable Draft. Reset deletes the context without touching the terminal +-- `onboarding_selections` evidence boundary. + +alter table operations + add constraint operations_workspace_id_unique unique (workspace_id, id); + +create table onboarding_import_selections ( + workspace_id text primary key references workspaces(id) on delete cascade, + application_idempotency_key text not null, + plan_digest text not null, + selected_operation_id text null, + selected_operation_version integer null, + registered_at timestamptz not null, + selected_at timestamptz null, + constraint onboarding_import_selections_application_key_check + check (octet_length(application_idempotency_key) between 1 and 256), + constraint onboarding_import_selections_plan_digest_check + check (plan_digest ~ '^[0-9a-f]{64}$'), + constraint onboarding_import_selections_version_check + check (selected_operation_version is null or selected_operation_version > 0), + constraint onboarding_import_selections_selection_shape_check check ( + ( + selected_operation_id is null + and selected_operation_version is null + and selected_at is null + ) or ( + selected_operation_id is not null + and selected_operation_version is not null + and selected_at is not null + ) + ), + -- The selected Operation is a durable report anchor. Hard-deleting it must + -- fail until the administrator explicitly resets/clears the import context; + -- normal archive keeps the row and surfaces drift. `no action` (checked at + -- statement end) still lets a single workspace deletion cascade-clean the + -- whole context together with its operations. + constraint onboarding_import_selections_operation_fk + foreign key (workspace_id, selected_operation_id) + references operations(workspace_id, id) + on delete no action, + constraint onboarding_import_selections_application_fk + foreign key (workspace_id, application_idempotency_key) + references import_plan_applications(workspace_id, idempotency_key) + on delete cascade +); + +create index onboarding_import_selections_operation_idx + on onboarding_import_selections(workspace_id, selected_operation_id); + +alter table product_events + drop constraint product_events_name_check; + +alter table product_events + add constraint product_events_name_check check (event_name in ( + 'onboarding_eligible', 'onboarding_started', 'onboarding_resumed', 'onboarding_dismissed', + 'onboarding_abandoned', 'onboarding_completed', + 'onboarding_import_eligible', 'onboarding_import_published', 'onboarding_agent_activated' + )); + +alter table product_event_daily_rollups + drop constraint product_event_daily_rollups_name_check; + +alter table product_event_daily_rollups + add constraint product_event_daily_rollups_name_check check (event_name in ( + 'onboarding_eligible', 'onboarding_started', 'onboarding_resumed', 'onboarding_dismissed', + 'onboarding_abandoned', 'onboarding_completed', + 'onboarding_import_eligible', 'onboarding_import_published', 'onboarding_agent_activated' + )); diff --git a/crates/crank-registry/src/migrations/owned_relations.rs b/crates/crank-registry/src/migrations/owned_relations.rs index b851b4e..74ddd93 100644 --- a/crates/crank-registry/src/migrations/owned_relations.rs +++ b/crates/crank-registry/src/migrations/owned_relations.rs @@ -31,8 +31,12 @@ pub(super) const ONBOARDING_PRODUCT_EVENTS: &[&str] = &[ "onboarding_selections", ]; +pub(super) const ONBOARDING_IMPORT_SELECTION: &[&str] = &["onboarding_import_selections"]; + 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..396baf8 100644 --- a/crates/crank-registry/src/migrations/schema_guard.rs +++ b/crates/crank-registry/src/migrations/schema_guard.rs @@ -37,6 +37,9 @@ pub(super) const OWNED_RELATIONS: &[&str] = &[ "usage_rollups", "artifact_blobs", "artifact_sources", + "import_plans", + "import_plan_applications", + "onboarding_import_selections", ]; const REQUIRED_COLUMNS: &[(&str, &[&str])] = &[ @@ -645,6 +648,20 @@ 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?; + } + if current_version < 15 { + if !super::schema_guard_v15::validate_v15_absent(connection).await? { + return Err(schema_error(current_version)); + } + } else { + super::schema_guard_v15::validate_v15_onboarding_import_selection(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/migrations/schema_guard_v15.rs b/crates/crank-registry/src/migrations/schema_guard_v15.rs new file mode 100644 index 0000000..1cfe53b --- /dev/null +++ b/crates/crank-registry/src/migrations/schema_guard_v15.rs @@ -0,0 +1,334 @@ +use sqlx::{PgConnection, Row, query}; + +use super::authority::MigrationError; +use super::schema_guard::{ + constraint_expression, normalize_definition, relation_exists, schema_error, +}; + +const IMPORT_EVENT_NAMES: &[&str] = &[ + "onboarding_import_eligible", + "onboarding_import_published", + "onboarding_agent_activated", +]; + +const CONTEXT_COLUMNS: &[(&str, &str, bool)] = &[ + ("workspace_id", "text", false), + ("application_idempotency_key", "text", false), + ("plan_digest", "text", false), + ("selected_operation_id", "text", true), + ("selected_operation_version", "integer", true), + ("registered_at", "timestamp with time zone", false), + ("selected_at", "timestamp with time zone", true), +]; + +pub(super) async fn validate_v15_absent( + connection: &mut PgConnection, +) -> Result { + if relation_exists(connection, "onboarding_import_selections").await? { + return Ok(false); + } + if named_constraint_exists(connection, "operations", "operations_workspace_id_unique").await? { + return Ok(false); + } + if !relation_exists(connection, "product_events").await? { + return Ok(true); + } + let name_check = + constraint_expression(connection, "product_events", "product_events_name_check").await?; + Ok(!normalize_definition(&name_check).contains("onboarding_import_eligible")) +} + +pub(super) async fn validate_v15_onboarding_import_selection( + connection: &mut PgConnection, +) -> Result<(), MigrationError> { + validate_context_columns(connection).await?; + validate_context_constraints(connection).await?; + validate_context_index(connection).await?; + if !named_constraint_exists(connection, "operations", "operations_workspace_id_unique").await? { + return Err(schema_error(15)); + } + for (table, constraint) in [ + ("product_events", "product_events_name_check"), + ( + "product_event_daily_rollups", + "product_event_daily_rollups_name_check", + ), + ] { + let definition = + normalize_definition(&constraint_expression(connection, table, constraint).await?); + for event_name in IMPORT_EVENT_NAMES { + if !definition.contains(event_name) { + return Err(schema_error(15)); + } + } + } + Ok(()) +} + +async fn validate_context_columns(connection: &mut PgConnection) -> Result<(), MigrationError> { + for (name, data_type, nullable) in CONTEXT_COLUMNS { + let row = query( + "select data_type, is_nullable, column_default, datetime_precision, collation_name + from information_schema.columns + where table_schema = current_schema() + and table_name = 'onboarding_import_selections' + and column_name = $1", + ) + .bind(name) + .fetch_optional(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let Some(row) = row else { + return Err(schema_error(15)); + }; + let actual_type = row + .try_get::("data_type") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let actual_nullable = row + .try_get::("is_nullable") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let default = row + .try_get::, _>("column_default") + .map_err(|_| MigrationError::storage("preflight.schema"))?; + let 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 actual_type != *data_type + || actual_nullable != if *nullable { "YES" } else { "NO" } + || default.is_some() + || (*data_type == "timestamp with time zone" && precision != Some(6)) + || collation.is_some() + { + return Err(schema_error(15)); + } + } + Ok(()) +} + +async fn validate_context_constraints(connection: &mut PgConnection) -> Result<(), MigrationError> { + for (name, kind, required) in [ + ( + "onboarding_import_selections_pkey", + "p", + &["primarykeyworkspace_id"][..], + ), + ( + "onboarding_import_selections_workspace_id_fkey", + "f", + &[ + "foreignkeyworkspace_id", + "referencesworkspacesid", + "ondeletecascade", + ][..], + ), + ( + "onboarding_import_selections_operation_fk", + "f", + &[ + "foreignkeyworkspace_id,selected_operation_id", + "referencesoperationsworkspace_id,id", + ][..], + ), + ( + "onboarding_import_selections_application_fk", + "f", + &[ + "foreignkeyworkspace_id,application_idempotency_key", + "referencesimport_plan_applicationsworkspace_id,idempotency_key", + "ondeletecascade", + ][..], + ), + ] { + let definition = constraint_definition(connection, name).await?; + if !constraint_is_type(connection, name, kind).await? { + return Err(schema_error(15)); + } + for snippet in required { + if !definition.contains(snippet) { + return Err(schema_error(15)); + } + } + } + // `ON DELETE NO ACTION` is the FK default and Postgres omits it from + // `pg_get_constraintdef`; assert the raw delete action instead so a + // selected Operation hard-delete cannot silently cascade the report. + if constraint_delete_action(connection, "onboarding_import_selections_operation_fk") + .await? + .as_deref() + != Some("a") + { + return Err(schema_error(15)); + } + for (name, required) in [ + ( + "onboarding_import_selections_application_key_check", + &[ + "octet_lengthapplication_idempotency_key>=1", + "octet_lengthapplication_idempotency_key<=256", + ][..], + ), + ( + "onboarding_import_selections_plan_digest_check", + &["plan_digest~'^[0-9a-f]{64}$'"][..], + ), + ( + "onboarding_import_selections_version_check", + &[ + "selected_operation_versionisnull", + "selected_operation_version>0", + ][..], + ), + ( + "onboarding_import_selections_selection_shape_check", + &["selected_operation_idisnull", "selected_atisnull"][..], + ), + ] { + let definition = normalize_definition( + &constraint_expression(connection, "onboarding_import_selections", name).await?, + ); + if required + .iter() + .any(|snippet| !definition.contains(&normalize_definition(snippet))) + { + return Err(schema_error(15)); + } + } + Ok(()) +} + +async fn validate_context_index(connection: &mut PgConnection) -> Result<(), MigrationError> { + let row = query( + "select t.relname as table_name, am.amname as access_method, + i.indisvalid, i.indisready, i.indisunique, i.indnkeyatts, + 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 = 'onboarding_import_selections_operation_idx'", + ) + .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("onboarding_import_selections") + && 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(false) + && row.try_get::("indnkeyatts").ok() == Some(2) + && row.try_get::("first_column").ok().as_deref() == Some("workspace_id") + && row.try_get::("second_column").ok().as_deref() + == Some("selected_operation_id") + && row + .try_get::, _>("predicate") + .ok() + .flatten() + .is_none() + }); + if valid { Ok(()) } else { Err(schema_error(15)) } +} + +async fn constraint_definition( + connection: &mut PgConnection, + constraint: &str, +) -> Result { + query( + "select 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 = 'onboarding_import_selections' + and c.conname = $1", + ) + .bind(constraint) + .fetch_optional(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))? + .ok_or_else(|| schema_error(15))? + .try_get::("definition") + .map(|definition| normalize_definition(&definition)) + .map_err(|_| MigrationError::storage("preflight.schema")) +} + +async fn constraint_delete_action( + connection: &mut PgConnection, + constraint: &str, +) -> Result, MigrationError> { + query( + "select c.confdeltype::text as action + 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 = 'onboarding_import_selections' + and c.conname = $1", + ) + .bind(constraint) + .fetch_optional(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))? + .map(|row| { + row.try_get::("action") + .map_err(|_| MigrationError::storage("preflight.schema")) + }) + .transpose() +} + +async fn constraint_is_type( + connection: &mut PgConnection, + constraint: &str, + kind: &str, +) -> Result { + 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 = 'onboarding_import_selections' + and c.conname = $1 + and c.contype = $2 + ) as present", + ) + .bind(constraint) + .bind(kind) + .fetch_one(&mut *connection) + .await + .map_err(|_| MigrationError::storage("preflight.schema"))? + .try_get("present") + .map_err(|_| MigrationError::storage("preflight.schema")) +} + +async fn named_constraint_exists( + connection: &mut PgConnection, + table: &str, + constraint: &str, +) -> Result { + 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 = $1 + and c.conname = $2 + ) as present", + ) + .bind(table) + .bind(constraint) + .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 334fe95..66c4a92 100644 --- a/crates/crank-registry/src/model.rs +++ b/crates/crank-registry/src/model.rs @@ -1,13 +1,13 @@ use crank_artifacts::ArtifactRef; use crank_core::{ Agent, AgentId, AgentOperationBinding, AgentStatus, AgentVersion, ApprovalRequest, - ApprovalRequestId, ApprovalRequestStatus, AuthKind, AuthProfile, DescriptorId, - ExecutionErrorCode, ExportMode, InvitationToken, InvocationLevel, InvocationLog, - InvocationLogId, InvocationSource, InvocationStatus, MembershipRole, OnboardingProjection, - Operation, OperationId, OperationSecurityLevel, OperationStatus, PlatformApiKey, - PlatformApiKeyId, ProductEvent, ProductEventId, ProductEventKind, Protocol, SampleId, Secret, - SecretId, SecretVersion, ToolSelectionPolicy, UsagePeriod, UsageRollup, User, UserId, - UserSessionId, Workspace, WorkspaceId, + ApprovalRequestId, ApprovalRequestStatus, AuthProfile, DescriptorId, ExecutionErrorCode, + ExportMode, InvitationToken, InvocationLevel, InvocationLog, InvocationLogId, InvocationSource, + InvocationStatus, MembershipRole, OnboardingProjection, Operation, OperationId, + OperationSecurityLevel, OperationStatus, PlatformApiKey, PlatformApiKeyId, ProductEvent, + ProductEventId, ProductEventKind, Protocol, SampleId, Secret, SecretId, SecretVersion, + ToolSelectionPolicy, UsagePeriod, UsageRollup, User, UserId, UserSessionId, Workspace, + WorkspaceId, }; use crank_mapping::MappingSet; use crank_schema::Schema; @@ -48,8 +48,17 @@ mod artifact_source; pub use artifact_source::*; +mod import_plan; + +pub use import_plan::*; + +mod onboarding_import; + +pub use onboarding_import::*; + define_registry_id!(YamlImportJobId); define_registry_id!(ImportJobId); +define_registry_id!(ImportPlanId); define_registry_id!(WorkspaceUpstreamId); pub type RegistryOperation = Operation; @@ -648,29 +657,6 @@ pub enum ImportConflictMode { pub struct ImportOperationDraft { pub operation_key: String, pub operation: RegistryOperation, - /// The effective single-scheme requirement selected for this operation. - /// The registry checks it against the referenced profile inside Apply. - pub auth_requirement: Option, -} - -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[serde(tag = "kind", rename_all = "snake_case")] -pub enum ImportAuthRequirement { - Bearer, - Basic, - ApiKeyHeader { parameter_name: String }, - ApiKeyQuery { parameter_name: String }, -} - -impl ImportAuthRequirement { - pub const fn kind(&self) -> AuthKind { - match self { - Self::Bearer => AuthKind::Bearer, - Self::Basic => AuthKind::Basic, - Self::ApiKeyHeader { .. } => AuthKind::ApiKeyHeader, - Self::ApiKeyQuery { .. } => AuthKind::ApiKeyQuery, - } - } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] 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/model/onboarding_import.rs b/crates/crank-registry/src/model/onboarding_import.rs new file mode 100644 index 0000000..775c10f --- /dev/null +++ b/crates/crank-registry/src/model/onboarding_import.rs @@ -0,0 +1,71 @@ +use super::*; + +#[derive(Clone, Debug)] +pub struct RegisterOnboardingImportContextRequest<'a> { + pub workspace_id: &'a WorkspaceId, + pub application_idempotency_key: &'a str, + pub plan_digest: &'a str, + pub expected_revision: i64, + pub occurred_at: OffsetDateTime, +} + +#[derive(Clone, Debug)] +pub struct SelectOnboardingImportDraftRequest<'a> { + pub workspace_id: &'a WorkspaceId, + pub application_idempotency_key: &'a str, + pub plan_digest: &'a str, + pub operation_id: &'a OperationId, + pub operation_version: u32, + pub expected_revision: i64, + pub occurred_at: OffsetDateTime, +} + +/// A projection and its durable import context read from one authoritative +/// registry transaction/snapshot. Pairing them avoids returning a projection +/// revision that belongs to an older context state. +#[derive(Clone, Debug)] +pub struct OnboardingSnapshot { + pub projection: OnboardingProjection, + pub import_context: Option, +} + +/// Result of recording a presentation/completion milestone together with the +/// import context read from the same transaction. Returning both avoids a +/// second post-projection pool read that could pair a revision with a newer +/// context. +#[derive(Clone, Debug)] +pub struct OnboardingEventSnapshot { + pub accepted: bool, + pub snapshot: OnboardingSnapshot, +} + +/// Durable workspace import context resolved from an immutable committed +/// apply report. The report and choices are read-only evidence; only the +/// selected operation/version and its timestamps live in mutable state. +#[derive(Clone, Debug)] +pub struct OnboardingImportContext { + pub application_idempotency_key: String, + pub plan_digest: String, + pub report: Value, + pub choices: Vec, + pub selected_operation_id: Option, + pub selected_operation_version: Option, + pub drift: Option, +} + +#[derive(Clone, Debug)] +pub struct OnboardingImportChoice { + pub key: String, + pub name: String, + pub outcome: String, + pub operation_id: Option, + pub version: Option, + pub reason: Option, + pub selectable: bool, +} + +#[derive(Clone, Debug)] +pub struct OnboardingImportDrift { + pub reason: String, + pub current_operation_version: Option, +} diff --git a/crates/crank-registry/src/postgres/import_job.rs b/crates/crank-registry/src/postgres/import_job.rs index d24b3f7..694e605 100644 --- a/crates/crank-registry/src/postgres/import_job.rs +++ b/crates/crank-registry/src/postgres/import_job.rs @@ -1,12 +1,26 @@ +mod catalog; +mod cleanup; +mod legacy_auth; +mod plan_apply; +mod semantic; +use cleanup::{cleanup_source_ids, detach_source_if_unreferenced_in_transaction}; +use semantic::plan_digest_from_payload; + use super::*; -use crate::{ArtifactSourceId, ImportJobSourceEnvelope}; +use crate::{ + ArtifactSourceId, CreateImportPlanRequest, ImportJobKind, ImportJobSourceEnvelope, + ImportOperationDraft, ImportPlan, ImportPlanId, +}; use crank_artifacts::ArtifactRef; -use crank_core::{AuthConfig, AuthProfile, AuthProfileId, SecretId}; -use std::collections::BTreeMap; +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 = 16 * 1024 * 1024; +const MAX_LEGACY_IMPORT_PLAN_PAYLOAD_BYTES: usize = 4 * 1024 * 1024; const DANGLING_OPENAPI_SOURCE_GRACE: time::Duration = time::Duration::minutes(5); impl PostgresRegistry { @@ -81,6 +95,224 @@ 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 v2 = request + .payload + .get("schema_version") + .and_then(Value::as_str) + == Some("import-plan-v2"); + if v2 { + semantic::validate_v2_metadata(request.payload)?; + plan_apply::validate_v2_creation(&request, &digest)?; + if request + .payload + .get("plan_reference") + .and_then(Value::as_str) + != Some(request.id.as_str()) + || request.payload.get("job_id").and_then(Value::as_str) + != Some(request.job_id.as_str()) + { + return Err(RegistryError::InvalidImportPlan { field: "identity" }); + } + } + + 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(), + }); + } + if v2 { + semantic::validate_v2_sources(request.payload, &preview_payload)?; + } + + sqlx::query("select id from workspaces where id = $1 for update") + .bind(request.workspace_id.as_str()) + .fetch_one(&mut *transaction) + .await?; + + if v2 + && request + .payload + .get("catalog_revision") + .and_then(Value::as_str) + != Some( + catalog::catalog_revision(&mut *transaction, request.workspace_id, true) + .await? + .as_str(), + ) + { + return Err(RegistryError::ImportPlanFingerprintStale { + plan_id: request.id.as_str().to_owned(), + }); + } + + // 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<'_>, @@ -135,11 +367,12 @@ impl PostgresRegistry { // bounded source identities and must not roll back the terminal // state because some other payload field was corrupted. for source_id in cleanup_source_ids(&payload) { - let _ = detach_source_in_transaction( + let _ = detach_source_if_unreferenced_in_transaction( &mut transaction, &workspace_id, &source_id, *request.finished_at, + &[request.id.as_str().to_owned()], ) .await?; } @@ -205,18 +438,26 @@ impl PostgresRegistry { .bind(limit) .fetch_all(&mut *transaction) .await?; - let mut expired_ids = Vec::with_capacity(expired.len()); + let expired_ids = expired + .iter() + .map(|row| row.try_get::("id")) + .collect::, _>>()?; let mut detached_sources = 0; for row in &expired { - expired_ids.push(row.try_get::("id")?); let workspace_id = WorkspaceId::new(row.try_get::("workspace_id")?); let payload = row.try_get::("preview_payload")?; // The job itself is expired regardless of whether a legacy or // corrupt payload can be decoded. Do not let one bad row roll // back cleanup for every workspace. for source_id in cleanup_source_ids(&payload) { - if detach_source_in_transaction(&mut transaction, &workspace_id, &source_id, now) - .await? + if detach_source_if_unreferenced_in_transaction( + &mut transaction, + &workspace_id, + &source_id, + now, + &expired_ids, + ) + .await? { detached_sources += 1; } @@ -251,7 +492,13 @@ impl PostgresRegistry { j.preview_payload -> 'source' ->> 'source_id' = s.source_id or exists ( select 1 - from jsonb_array_elements(coalesce(j.preview_payload -> 'dependencies', '[]'::jsonb)) d + from jsonb_array_elements( + case + when jsonb_typeof(j.preview_payload -> 'dependencies') = 'array' + then j.preview_payload -> 'dependencies' + else '[]'::jsonb + end + ) d where d ->> 'source_id' = s.source_id ) ) @@ -390,9 +637,7 @@ async fn apply_import_job_transaction( } } - // Validate the complete binding set before the first Operation insert. - // This transaction-level check closes the gap between preview and Apply. - validate_import_auth_bindings(tx, request.workspace_id, request.operations).await?; + legacy_auth::verify_legacy_import_auth(tx, request.workspace_id, request.operations).await?; let mut result = ImportJobApplyResult { application_key: request.application_key.to_owned(), @@ -491,190 +736,6 @@ async fn apply_import_job_transaction( Ok(result) } -async fn validate_import_auth_bindings( - tx: &mut Transaction<'_, Postgres>, - workspace_id: &WorkspaceId, - operations: &[crate::ImportOperationDraft], -) -> Result<(), RegistryError> { - let mut requirements_by_profile = BTreeMap::>::new(); - for draft in operations { - match ( - draft.auth_requirement.as_ref(), - draft.operation.execution_config.auth_profile_ref.as_ref(), - ) { - (None, None) => {} - (Some(requirement), Some(profile_id)) => requirements_by_profile - .entry(profile_id.as_str().to_owned()) - .or_default() - .push(requirement), - _ => return Err(RegistryError::ImportAuthBindingUnavailable), - } - } - if requirements_by_profile.is_empty() { - return Ok(()); - } - - // Read the current workspace's profile config first to discover Secret - // references. Secret rows are locked in stable order before profile rows, - // matching save_auth_profile's secret-then-profile write order. - let mut observed_profiles = BTreeMap::::new(); - let mut secret_ids = BTreeMap::::new(); - for profile_id in requirements_by_profile.keys() { - let profile = read_import_auth_profile(tx, workspace_id, profile_id, false) - .await? - .ok_or(RegistryError::ImportAuthBindingUnavailable)?; - let requirements = &requirements_by_profile[profile_id]; - if !requirements - .iter() - .all(|requirement| auth_profile_matches(profile_id, &profile, requirement)) - { - return Err(RegistryError::ImportAuthBindingUnavailable); - } - for secret_id in profile.config.secret_ids() { - secret_ids - .entry(secret_id.as_str().to_owned()) - .or_insert_with(|| secret_id.clone()); - } - observed_profiles.insert(profile_id.clone(), profile); - } - - for secret_id in secret_ids.values() { - let row = sqlx::query( - "select s.status, - exists ( - select 1 from secret_versions sv - where sv.secret_id = s.id and sv.version = s.current_version - ) as has_current_version - from secrets s - where s.workspace_id = $1 and s.id = $2 - for update of s", - ) - .bind(workspace_id.as_str()) - .bind(secret_id.as_str()) - .fetch_optional(&mut **tx) - .await?; - let Some(row) = row else { - return Err(RegistryError::ImportAuthBindingUnavailable); - }; - if row.try_get::("status")? != "active" - || !row.try_get::("has_current_version")? - { - return Err(RegistryError::ImportAuthBindingUnavailable); - } - } - - // The profile may have changed while its former Secret rows were being - // locked. Lock profile rows only after Secrets (the order used by profile - // updates), then compare the binding config and revalidate compatibility. - for (profile_id, observed) in &observed_profiles { - let current = read_import_auth_profile(tx, workspace_id, profile_id, true) - .await? - .ok_or(RegistryError::ImportAuthBindingUnavailable)?; - if current.id != observed.id - || current.workspace_id != observed.workspace_id - || current.kind != observed.kind - || current.config != observed.config - || !requirements_by_profile[profile_id] - .iter() - .all(|requirement| auth_profile_matches(profile_id, ¤t, requirement)) - { - return Err(RegistryError::ImportAuthBindingUnavailable); - } - } - - Ok(()) -} - -async fn read_import_auth_profile( - tx: &mut Transaction<'_, Postgres>, - workspace_id: &WorkspaceId, - profile_id: &str, - lock: bool, -) -> Result, RegistryError> { - let query = if lock { - "select id, workspace_id, name, kind, config_json, created_at, updated_at - from auth_profiles - where workspace_id = $1 and id = $2 - for update" - } else { - "select id, workspace_id, name, kind, config_json, created_at, updated_at - from auth_profiles - where workspace_id = $1 and id = $2" - }; - let row = sqlx::query(query) - .bind(workspace_id.as_str()) - .bind(profile_id) - .fetch_optional(&mut **tx) - .await?; - row.map(|row| { - super::build_auth_profile( - row.try_get("id")?, - row.try_get("workspace_id")?, - row.try_get("name")?, - row.try_get("kind")?, - row.try_get("config_json")?, - row.try_get("created_at")?, - row.try_get("updated_at")?, - ) - }) - .transpose() -} - -fn auth_profile_matches( - expected_profile_id: &str, - profile: &AuthProfile, - requirement: &crate::ImportAuthRequirement, -) -> bool { - if profile.id != AuthProfileId::new(expected_profile_id) - || profile.kind != profile.config.kind() - || profile.kind != requirement.kind() - { - return false; - } - match (requirement, &profile.config) { - (crate::ImportAuthRequirement::Bearer, AuthConfig::Bearer(_)) - | (crate::ImportAuthRequirement::Basic, AuthConfig::Basic(_)) => true, - ( - crate::ImportAuthRequirement::ApiKeyHeader { parameter_name }, - AuthConfig::ApiKeyHeader(config), - ) => { - is_valid_auth_header_name(&config.header_name) - && is_valid_auth_header_name(parameter_name) - && config.header_name.eq_ignore_ascii_case(parameter_name) - } - ( - crate::ImportAuthRequirement::ApiKeyQuery { parameter_name }, - AuthConfig::ApiKeyQuery(config), - ) => config.param_name == *parameter_name, - _ => false, - } -} - -fn is_valid_auth_header_name(value: &str) -> bool { - value.len() <= 128 - && !value.is_empty() - && value.bytes().all(|byte| { - byte.is_ascii_alphanumeric() - || matches!( - byte, - b'!' | b'#' - | b'$' - | b'%' - | b'&' - | b'\'' - | b'*' - | b'+' - | b'-' - | b'.' - | b'^' - | b'_' - | b'`' - | b'|' - | b'~' - ) - }) -} - fn validate_source_envelope( source: &ImportJobSourceEnvelope, preview_payload: &Value, @@ -778,29 +839,6 @@ fn dependencies_from_payload( Ok(dependencies) } -fn cleanup_source_ids(payload: &Value) -> Vec { - let mut identities = std::collections::BTreeSet::new(); - if let Some(source_id) = payload - .pointer("/source/source_id") - .and_then(Value::as_str) - .filter(|value| value.len() <= 132) - { - identities.insert(source_id.to_owned()); - } - if let Some(dependencies) = payload.get("dependencies").and_then(Value::as_array) { - for source_id in dependencies - .iter() - .take(MAX_IMPORT_JOB_DOCUMENTS) - .filter_map(|item| item.get("source_id")) - .filter_map(Value::as_str) - .filter(|value| value.len() <= 132) - { - identities.insert(source_id.to_owned()); - } - } - identities.into_iter().map(ArtifactSourceId::new).collect() -} - async fn detach_source_in_transaction( transaction: &mut Transaction<'_, Postgres>, workspace_id: &WorkspaceId, @@ -863,3 +901,81 @@ 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")?, + }) +} + +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.payload == *request.payload + && 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/catalog.rs b/crates/crank-registry/src/postgres/import_job/catalog.rs new file mode 100644 index 0000000..19f2def --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/catalog.rs @@ -0,0 +1,68 @@ +use super::*; + +// A bounded metadata read, independent of the size of stored schemas/Drafts. +const MAX_IMPORT_CATALOG_ENTRIES: i64 = 10_000; + +impl PostgresRegistry { + pub async fn list_import_catalog_names( + &self, + workspace_id: &WorkspaceId, + ) -> Result, RegistryError> { + let names = sqlx::query_scalar::<_, String>( + "select name from operations where workspace_id = $1 order by name limit $2", + ) + .bind(workspace_id.as_str()) + .bind(MAX_IMPORT_CATALOG_ENTRIES + 1) + .fetch_all(&self.pool) + .await?; + if names.len() > MAX_IMPORT_CATALOG_ENTRIES as usize { + return Err(RegistryError::InvalidImportPlan { + field: "catalog_revision_limit", + }); + } + Ok(names) + } + /// Deterministic workspace catalog identity for naming and version targets. + /// Referenced authentication state is separately bound in plan snapshots. + pub async fn import_catalog_revision( + &self, + workspace_id: &WorkspaceId, + ) -> Result { + catalog_revision(&self.pool, workspace_id, false).await + } +} + +pub(super) async fn catalog_revision<'c, E>( + executor: E, + workspace_id: &WorkspaceId, + lock: bool, +) -> Result +where + E: Executor<'c, Database = Postgres>, +{ + let query = if lock { + "select jsonb_build_array(id, name, protocol, status, current_draft_version, latest_published_version) as metadata + from operations where workspace_id = $1 order by id limit $2 for share" + } else { + "select jsonb_build_array(id, name, protocol, status, current_draft_version, latest_published_version) as metadata + from operations where workspace_id = $1 order by id limit $2" + }; + let rows = sqlx::query(query) + .bind(workspace_id.as_str()) + .bind(MAX_IMPORT_CATALOG_ENTRIES + 1) + .fetch_all(executor) + .await?; + if rows.len() > MAX_IMPORT_CATALOG_ENTRIES as usize { + return Err(RegistryError::InvalidImportPlan { + field: "catalog_revision_limit", + }); + } + let metadata = rows + .iter() + .map(|row| row.try_get::("metadata")) + .collect::, _>>()?; + Ok(format!( + "{:x}", + Sha256::digest(serde_json::to_vec(&metadata)?) + )) +} diff --git a/crates/crank-registry/src/postgres/import_job/cleanup.rs b/crates/crank-registry/src/postgres/import_job/cleanup.rs new file mode 100644 index 0000000..423b35a --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/cleanup.rs @@ -0,0 +1,69 @@ +use super::*; + +const OPENAPI_SOURCE_ID_PREFIX: &str = "src_openapi_"; + +pub(super) fn cleanup_source_ids(payload: &Value) -> Vec { + let mut identities = std::collections::BTreeSet::new(); + if let Some(source_id) = payload + .pointer("/source/source_id") + .and_then(Value::as_str) + .filter(|value| value.len() <= 132 && value.starts_with(OPENAPI_SOURCE_ID_PREFIX)) + { + identities.insert(source_id.to_owned()); + } + if let Some(dependencies) = payload.get("dependencies").and_then(Value::as_array) { + for source_id in dependencies + .iter() + .take(MAX_IMPORT_JOB_DOCUMENTS) + .filter_map(|item| item.get("source_id")) + .filter_map(Value::as_str) + .filter(|value| value.len() <= 132 && value.starts_with(OPENAPI_SOURCE_ID_PREFIX)) + { + identities.insert(source_id.to_owned()); + } + } + identities.into_iter().map(ArtifactSourceId::new).collect() +} + +pub(super) async fn detach_source_if_unreferenced_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + source_id: &ArtifactSourceId, + detached_at: OffsetDateTime, + excluded_job_ids: &[String], +) -> Result { + let detached = sqlx::query( + "update artifact_sources + set lifecycle = 'detached', updated_at = $1, detached_at = $1 + where workspace_id = $2 and source_id = $3 + and lifecycle = 'active' + and not exists ( + select 1 + from import_jobs j + where j.workspace_id = $2 + and not (j.id = any($4::text[])) + and ( + j.preview_payload -> 'source' ->> 'source_id' = $3 + or exists ( + select 1 + from jsonb_array_elements( + case + when jsonb_typeof(j.preview_payload -> 'dependencies') = 'array' + then j.preview_payload -> 'dependencies' + else '[]'::jsonb + end + ) d + where d ->> 'source_id' = $3 + ) + ) + )", + ) + .bind(detached_at) + .bind(workspace_id.as_str()) + .bind(source_id.as_str()) + .bind(excluded_job_ids) + .execute(&mut **transaction) + .await? + .rows_affected(); + Ok(detached == 1) +} diff --git a/crates/crank-registry/src/postgres/import_job/legacy_auth.rs b/crates/crank-registry/src/postgres/import_job/legacy_auth.rs new file mode 100644 index 0000000..9910aca --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/legacy_auth.rs @@ -0,0 +1,144 @@ +use super::*; + +/// Legacy Apply has no plan-time auth snapshot, but it still must not create +/// operations from a stale or cross-workspace profile reference. Lock and +/// validate every referenced profile and its secret rows before inserting +/// any operation; ImportPlan Apply performs the stricter snapshot comparison. +pub(super) async fn verify_legacy_import_auth( + tx: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + operations: &[ImportOperationDraft], +) -> Result<(), RegistryError> { + let mut profiles = std::collections::BTreeMap::>::new(); + let mut profile_snapshots = std::collections::BTreeMap::::new(); + for draft in operations { + if let Some(profile_id) = draft.operation.execution_config.auth_profile_ref.as_ref() { + profiles.entry(profile_id.as_str().to_owned()).or_default(); + } + } + if profiles.is_empty() { + return Ok(()); + } + + let unavailable = |id: &str| RegistryError::ImportPlanAuthUnavailable { + auth_profile_id: id.to_owned(), + }; + let profile_ids: Vec = profiles.keys().cloned().collect(); + for profile_id in &profile_ids { + 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_id_value = row.try_get::("id")?; + let profile_kind = row.try_get::("kind")?; + let config_json = row.try_get::("config_json")?; + let profile = build_auth_profile( + profile_id_value, + workspace_id.as_str().to_owned(), + row.try_get::("name")?, + profile_kind.clone(), + config_json.clone(), + row.try_get::("created_at")?, + row.try_get::("updated_at")?, + ) + .map_err(|_| unavailable(profile_id))?; + if profile.kind != profile.config.kind() { + return Err(unavailable(profile_id)); + } + let mut secret_ids = profile + .config + .secret_ids() + .into_iter() + .map(|secret| secret.as_str().to_owned()) + .collect::>(); + secret_ids.sort(); + secret_ids.dedup(); + profiles.insert(profile_id.clone(), secret_ids); + profile_snapshots.insert(row.try_get::("id")?, (profile_kind, config_json)); + } + + let secret_ids: std::collections::BTreeSet = + profiles.values().flatten().cloned().collect(); + for secret_id in secret_ids { + let owner = profiles + .iter() + .find_map(|(profile_id, ids)| ids.contains(&secret_id).then_some(profile_id.as_str())) + .unwrap_or_default(); + let row = sqlx::query( + "select status, current_version + 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")?; + let version: i32 = row.try_get("current_version")?; + 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(version) + .fetch_one(&mut **tx) + .await?; + if status != "active" || !version_present { + return Err(unavailable(owner)); + } + } + + // Keep the established secret-then-profile lock order. The profile may + // have changed while secret rows were locked; compare its live config and + // secret set with the initial read before Apply mutates any operation. + for profile_id in profile_ids { + 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 kind: String = row.try_get("kind")?; + let config: Value = row.try_get("config_json")?; + let Some((expected_kind, expected_config)) = profile_snapshots.get(&profile_id) else { + return Err(unavailable(&profile_id)); + }; + if &kind != expected_kind || &config != expected_config { + return Err(unavailable(&profile_id)); + } + let profile = build_auth_profile( + row.try_get::("id")?, + workspace_id.as_str().to_owned(), + row.try_get::("name")?, + kind, + config, + row.try_get::("created_at")?, + row.try_get::("updated_at")?, + ) + .map_err(|_| unavailable(&profile_id))?; + let mut live_secret_ids = profile + .config + .secret_ids() + .into_iter() + .map(|secret| secret.as_str().to_owned()) + .collect::>(); + live_secret_ids.sort(); + live_secret_ids.dedup(); + if profiles.get(&profile_id) != Some(&live_secret_ids) { + return Err(unavailable(&profile_id)); + } + } + Ok(()) +} 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..cfafa5f --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply.rs @@ -0,0 +1,430 @@ +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 revalidate; +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 = 2_000; +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` (16 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; + +pub(super) fn validate_v2_creation( + request: &CreateImportPlanRequest<'_>, + digest: &str, +) -> Result<(), RegistryError> { + let fingerprint = + request + .payload + .get("fingerprint") + .ok_or(RegistryError::InvalidImportPlan { + field: "fingerprint", + })?; + let validation = ApplyImportPlanRequest { + workspace_id: request.workspace_id, + plan_id: request.id, + plan_digest: digest, + idempotency_key: "creation-validation", + verified_fingerprint: fingerprint, + applied_at: request.created_at, + }; + read_and_validate_plan_material( + request.payload, + &validation, + digest, + request.job_id.as_str(), + )?; + Ok(()) +} + +/// 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)?; + + if plan.schema_version == "import-plan-v2" + && plan.catalog_revision.as_deref() + != Some( + super::catalog::catalog_revision(&mut **tx, request.workspace_id, true) + .await + .map_err(fail)? + .as_str(), + ) + { + return Err(fail(RegistryError::ImportPlanFingerprintStale { + plan_id: plan_id.clone(), + })); + } + + // 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..7c8e9e4 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/material.rs @@ -0,0 +1,654 @@ +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, + #[serde(default)] + pub(crate) catalog_revision: Option, + #[serde(default)] + pub(crate) excluded_reasons: Vec, +} + +#[derive(Deserialize)] +pub(super) struct StoredExcludedReason { + pub(crate) key: String, + #[serde(default)] + pub(crate) name: String, + pub(crate) reason: String, +} + +impl StoredExcludedReason { + pub(crate) fn name_or_key(&self) -> &str { + if self.name.is_empty() { + &self.key + } else { + &self.name + } + } +} + +#[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, + /// Source-derived selected scheme requirement. Apply checks the live + /// profile against this independently of the captured profile snapshot, + /// closing a profile-change race between compatibility lookup and capture. + #[serde(default)] + pub(crate) auth_requirement: Option, +} + +#[derive(Debug, Deserialize, PartialEq, Eq)] +pub(super) struct StoredAuthRequirement { + pub(crate) kind: String, + #[serde(default)] + pub(crate) parameter_name: 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()); + } + let v2 = payload.get("schema_version").and_then(Value::as_str) == Some("import-plan-v2"); + let selected_limit = if v2 { MAX_IMPORT_PLAN_KEYS } else { 512 }; + let all_limit = if v2 { MAX_IMPORT_PLAN_KEYS } else { 1_024 }; + if payload + .pointer("/selection/selected") + .and_then(Value::as_array) + .is_some_and(|items| items.len() > selected_limit) + || payload + .pointer("/selection/all") + .and_then(Value::as_array) + .is_some_and(|items| items.len() > all_limit) + || payload + .pointer("/selection/excluded") + .and_then(Value::as_array) + .is_some_and(|items| items.len() > selected_limit) + { + 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 !matches!( + plan.schema_version.as_str(), + PLAN_PAYLOAD_SCHEMA_VERSION | "import-plan-v2" + ) || plan.plan_reference.as_str() != request.plan_id.as_str() + || plan.job_id != stored_job_id + { + return Err(corrupt()); + } + if plan.schema_version == "import-plan-v2" { + super::super::semantic::validate_v2_metadata(payload).map_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(); + if plan.schema_version == "import-plan-v2" { + let findings = payload + .pointer("/findings/candidates") + .and_then(Value::as_array) + .ok_or_else(corrupt)?; + let keys = findings + .iter() + .map(|item| { + if !item.get("findings").is_some_and(Value::is_array) { + return Err(corrupt()); + } + item.get("key").and_then(Value::as_str).ok_or_else(corrupt) + }) + .collect::, _>>()?; + let expected = plan + .selection + .all + .iter() + .map(String::as_str) + .collect::>(); + if keys != expected || keys.len() != findings.len() { + return Err(corrupt()); + } + } + 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()); + } + if plan.schema_version == "import-plan-v2" { + let reasons = plan + .excluded_reasons + .iter() + .map(|item| &item.key) + .collect::>(); + if reasons != excluded + || reasons.len() != plan.excluded_reasons.len() + || plan.excluded_reasons.iter().any(|item| { + item.reason.is_empty() || item.reason.len() > MAX_IMPORT_PLAN_REASON_BYTES + }) + || !plan + .excluded_reasons + .windows(2) + .all(|items| items[0].key < items[1].key) + { + return Err(corrupt()); + } + } + if plan.schema_version == "import-plan-v2" + && plan.selection.mode == "partial" + && plan + .candidates + .iter() + .any(|candidate| candidate.state != "actionable") + { + 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 (&candidate.auth_requirement, &auth_ref, actionable) { + (Some(requirement), Some(_), true) + if requirement.kind == "unconstrained" + && requirement.parameter_name.is_none() + && decision.action == "new_version" => {} + (Some(requirement), Some(_), true) + if matches!(requirement.kind.as_str(), "bearer" | "basic") + && requirement.parameter_name.is_none() + || matches!( + requirement.kind.as_str(), + "api_key_header" | "api_key_query" + ) && requirement + .parameter_name + .as_deref() + .is_some_and(|name| !name.is_empty() && name.len() <= 256) => {} + (None, None, _) | (None, Some(_), false) => {} + _ => 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(()) +} + +#[cfg(test)] +mod tests { + use super::StoredExcludedReason; + use serde_json::json; + + #[test] + fn historic_excluded_reason_without_display_name_deserializes_and_uses_key() { + let reason: StoredExcludedReason = serde_json::from_value(json!({ + "key": "GET /legacy", + "reason": "not_selected" + })) + .unwrap(); + + assert_eq!(reason.name, ""); + assert_eq!(reason.name_or_key(), "GET /legacy"); + } +} 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..bef0c44 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/mutations.rs @@ -0,0 +1,378 @@ +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: plan + .excluded_reasons + .iter() + .find(|item| item.key == *key) + .map(|item| item.name_or_key()) + .unwrap_or(key) + .to_owned(), + outcome: ImportPlanCandidateOutcome::Excluded, + operation_id: None, + version: None, + reason: Some( + plan.excluded_reasons + .iter() + .find(|item| item.key == *key) + .map(|item| item.reason.clone()) + .unwrap_or_else(|| "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/revalidate.rs b/crates/crank-registry/src/postgres/import_job/plan_apply/revalidate.rs new file mode 100644 index 0000000..7265963 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/revalidate.rs @@ -0,0 +1,114 @@ +use super::*; + +impl PostgresRegistry { + /// Reopens immutable plan evidence without applying it or writing a report. + /// The result is current validation evidence, never a promise about a later + /// Apply; Apply independently repeats its checks inside its transaction. + pub async fn revalidate_import_plan( + &self, + workspace_id: &WorkspaceId, + plan_id: &ImportPlanId, + verified_fingerprint: &Value, + ) -> Result<(), RegistryError> { + let plan = self + .get_import_plan(workspace_id, plan_id) + .await? + .ok_or_else(|| RegistryError::ImportPlanNotFound { + plan_id: plan_id.as_str().to_owned(), + })?; + let now = OffsetDateTime::now_utc(); + if plan.expires_at <= now { + return Err(RegistryError::ImportPlanExpired { + plan_id: plan_id.as_str().to_owned(), + }); + } + let request = ApplyImportPlanRequest { + workspace_id, + plan_id, + plan_digest: &plan.digest, + idempotency_key: "read-only-revalidation", + verified_fingerprint, + applied_at: &now, + }; + let material = read_and_validate_plan_material( + &plan.payload, + &request, + &plan.digest, + plan.job_id.as_str(), + )?; + let job = self + .get_import_job(workspace_id, &plan.job_id) + .await? + .ok_or_else(|| RegistryError::ImportPlanJobUnavailable { + job_id: plan.job_id.as_str().to_owned(), + })?; + if job.status != ImportJobStatus::Pending || job.expires_at <= now { + return Err(RegistryError::ImportPlanJobTerminal { + job_id: job.id.as_str().to_owned(), + }); + } + verify_job_source_consistency(&job.preview_payload, &material, plan_id.as_str())?; + let mut tx = self.pool.begin().await?; + let mut envelopes = material + .dependencies + .iter() + .chain(std::iter::once(&material.source)) + .map(|source| ImportJobSourceEnvelope { + source_id: ArtifactSourceId::new(source.source_id.clone()), + digest: ArtifactRef::parse(&source.digest).expect("material was validated"), + }) + .collect::>(); + envelopes.sort_by(|a, b| a.source_id.as_str().cmp(b.source_id.as_str())); + for envelope in &envelopes { + lock_and_verify_envelope(&mut tx, workspace_id, envelope).await?; + } + lock_and_verify_auth(&mut tx, workspace_id, &material.candidates).await?; + if material.schema_version == "import-plan-v2" + && material.catalog_revision.as_deref() + != Some( + super::super::catalog::catalog_revision(&mut *tx, workspace_id, true) + .await? + .as_str(), + ) + { + return Err(RegistryError::ImportPlanFingerprintStale { + plan_id: plan_id.as_str().to_owned(), + }); + } + for candidate in &material.candidates { + if let Some(target) = &candidate.target { + let row = sqlx::query("select name, status, current_draft_version, latest_published_version from operations where workspace_id = $1 and id = $2 for share") + .bind(workspace_id.as_str()).bind(&target.operation_id).fetch_optional(&mut *tx).await?; + let matching = match row { + Some(row) => { + row.try_get::("name")? == target.name + && deserialize_enum_text::( + row.try_get("status")?, + "status", + )? == target.expected_state.status + && row.try_get::("current_draft_version")? as u32 + == target.expected_state.current_draft_version + && row + .try_get::, _>("latest_published_version")? + .map(|value| value as u32) + == target.expected_state.latest_published_version + } + None => false, + }; + if !matching { + return Err(RegistryError::ImportPlanTargetStale { + operation_id: target.operation_id.clone(), + }); + } + } else if candidate.state == "actionable" + && operation_name_exists(&mut tx, workspace_id, &candidate.name).await? + { + return Err(RegistryError::ImportPlanNameConflict { + name: candidate.name.clone(), + }); + } + } + tx.rollback().await?; + Ok(()) + } +} 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..bf7cca6 --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/plan_apply/verify.rs @@ -0,0 +1,335 @@ +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::{StoredAuthRequirement, 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(); + let mut requirements: 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()))?; + let requirement = candidate + .auth_requirement + .as_ref() + .ok_or_else(|| unavailable(ref_id.as_str()))?; + requirements + .entry(ref_id.as_str().to_owned()) + .or_default() + .push(requirement); + 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)); + } + if requirements.get(profile_id).is_none_or(|expected| { + expected + .iter() + .any(|requirement| !auth_config_satisfies_requirement(&profile.config, requirement)) + }) { + 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(()) +} + +fn auth_config_satisfies_requirement( + config: &crank_core::AuthConfig, + requirement: &material::StoredAuthRequirement, +) -> bool { + use crank_core::AuthConfig; + match (config, requirement.kind.as_str()) { + (_, "unconstrained") => requirement.parameter_name.is_none(), + (AuthConfig::Bearer(_), "bearer") | (AuthConfig::Basic(_), "basic") => { + requirement.parameter_name.is_none() + } + (AuthConfig::ApiKeyHeader(config), "api_key_header") => requirement + .parameter_name + .as_deref() + .is_some_and(|name| config.header_name.eq_ignore_ascii_case(name)), + (AuthConfig::ApiKeyQuery(config), "api_key_query") => requirement + .parameter_name + .as_deref() + .is_some_and(|name| config.param_name == name), + _ => false, + } +} + +#[cfg(test)] +mod tests { + use super::auth_config_satisfies_requirement; + use crate::postgres::import_job::plan_apply::material::StoredAuthRequirement; + use crank_core::{ApiKeyHeaderAuthConfig, AuthConfig, SecretId}; + + #[test] + fn source_selected_header_name_is_rechecked_against_live_profile_config() { + let source_requirement = StoredAuthRequirement { + kind: "api_key_header".to_owned(), + parameter_name: Some("X-A".to_owned()), + }; + let compatible = AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "x-a".to_owned(), + secret_id: SecretId::new("secret_a"), + }); + let changed_after_resolution = AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { + header_name: "X-B".to_owned(), + secret_id: SecretId::new("secret_a"), + }); + + assert!(auth_config_satisfies_requirement( + &compatible, + &source_requirement + )); + assert!(!auth_config_satisfies_requirement( + &changed_after_resolution, + &source_requirement + )); + } +} diff --git a/crates/crank-registry/src/postgres/import_job/semantic.rs b/crates/crank-registry/src/postgres/import_job/semantic.rs new file mode 100644 index 0000000..b429c0e --- /dev/null +++ b/crates/crank-registry/src/postgres/import_job/semantic.rs @@ -0,0 +1,230 @@ +use super::*; + +/// 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. +pub(super) fn plan_digest_from_payload(payload: &Value) -> Result { + if !payload.is_object() { + return Err(RegistryError::InvalidImportPlan { field: "payload" }); + } + let limit = if payload.get("schema_version").and_then(Value::as_str) == Some("import-plan-v2") { + MAX_IMPORT_PLAN_PAYLOAD_BYTES + } else { + MAX_LEGACY_IMPORT_PLAN_PAYLOAD_BYTES + }; + if serde_json::to_vec(payload)?.len() > limit { + return Err(RegistryError::InvalidImportPlan { field: "payload" }); + } + let canonical = serde_json::to_vec(&canonical_material(payload))?; + Ok(format!("{:x}", Sha256::digest(canonical))) +} + +pub(super) fn validate_v2_sources(payload: &Value, preview: &Value) -> Result<(), RegistryError> { + let dependency_set = + |value: &Value| -> Result, RegistryError> { + Ok(dependencies_from_payload(value)? + .iter() + .map(|source| { + ( + source.source_id.as_str().to_owned(), + source.digest.as_str().to_owned(), + ) + }) + .collect()) + }; + if source_from_payload(payload)? != source_from_payload(preview)? + || dependency_set(payload)? != dependency_set(preview)? + { + return Err(RegistryError::InvalidImportPlan { field: "source" }); + } + Ok(()) +} + +/// Removes only server-generated envelope fields from v2 semantic material. +/// Paths are explicit: equally named fields in a user schema remain semantic. +pub(super) fn canonical_material(payload: &Value) -> Value { + let mut material = payload.clone(); + if payload.get("schema_version").and_then(Value::as_str) != Some("import-plan-v2") { + return material; + } + let object = material.as_object_mut().expect("validated object"); + object.remove("plan_reference"); + object.remove("job_id"); + if let Some(source) = object.get_mut("source").and_then(Value::as_object_mut) { + source.remove("source_id"); + } + if let Some(dependencies) = object.get_mut("dependencies").and_then(Value::as_array_mut) { + for dependency in dependencies.iter_mut() { + if let Some(source) = dependency.as_object_mut() { + source.remove("source_id"); + } + } + dependencies.sort_by_key(Value::to_string); + } + if let Some(candidates) = object.get_mut("candidates").and_then(Value::as_array_mut) { + for candidate in candidates { + let generated = matches!( + candidate + .pointer("/decision/action") + .and_then(Value::as_str), + Some("create" | "rename") + ); + if let Some(operation) = candidate + .get_mut("operation") + .and_then(Value::as_object_mut) + { + if generated { + operation.remove("id"); + } + operation.remove("created_at"); + operation.remove("updated_at"); + operation.remove("published_at"); + } + if let Some(snapshot) = candidate + .get_mut("auth_snapshot") + .and_then(Value::as_object_mut) + { + snapshot.remove("updated_at"); + if let Some(secrets) = snapshot.get_mut("secrets").and_then(Value::as_array_mut) { + for secret in secrets { + if let Some(secret) = secret.as_object_mut() { + secret.remove("updated_at"); + } + } + } + } + } + } + material +} + +pub(super) fn validate_v2_metadata(payload: &Value) -> Result<(), RegistryError> { + let invalid = || RegistryError::InvalidImportPlan { field: "metadata" }; + let revision = payload + .get("catalog_revision") + .and_then(Value::as_str) + .ok_or_else(invalid)?; + if revision.len() != 64 + || !revision + .bytes() + .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) + { + return Err(invalid()); + } + let matrix = payload + .get("support_matrix_version") + .and_then(Value::as_str) + .ok_or_else(invalid)?; + if matrix.is_empty() || matrix.len() > 64 { + return Err(invalid()); + } + let findings = payload + .get("findings") + .and_then(Value::as_object) + .ok_or_else(invalid)?; + if !findings.get("document").is_some_and(Value::is_array) + || !findings.get("candidates").is_some_and(Value::is_array) + { + return Err(invalid()); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::super::plan_digest_from_payload; + use serde_json::{Value, json}; + + fn payload() -> Value { + json!({ + "schema_version": "import-plan-v2", "plan_reference": "plan_a", "job_id": "job_a", + "catalog_revision": "a".repeat(64), "support_matrix_version": "1.0.2", + "source": {"source_id": "src_a", "digest": format!("sha256:{}", "b".repeat(64))}, + "dependencies": [{"source_id": "dep_a", "digest": format!("sha256:{}", "c".repeat(64))}], + "findings": {"document": [], "candidates": []}, + "selection": {"mode": "full", "selected": ["GET /a"], "excluded": [], "all": ["GET /a"]}, + "candidates": [{ + "key": "GET /a", "decision": {"action": "create"}, + "operation": {"id": "op_a", "created_at": "time_a", "updated_at": "time_a", + "input_schema": {"properties": {"id": {"default": "semantic_id"}, "created_at": {"default": "semantic_time"}}}}, + "auth_snapshot": {"profile_id": "auth_a", "updated_at": "time_a", "secrets": [ + {"id": "secret_a", "current_version": 1, "status": "active", "updated_at": "time_a"} + ]} + }] + }) + } + + #[test] + fn equivalent_v2_material_ignores_only_identity_envelope_and_runtime_instants() { + let first = payload(); + let mut second = first.clone(); + second["plan_reference"] = json!("plan_b"); + second["job_id"] = json!("job_b"); + second["source"]["source_id"] = json!("src_b"); + second["dependencies"][0]["source_id"] = json!("dep_b"); + second["candidates"][0]["operation"]["id"] = json!("op_b"); + second["candidates"][0]["operation"]["created_at"] = json!("time_b"); + second["candidates"][0]["operation"]["updated_at"] = json!("time_b"); + second["candidates"][0]["auth_snapshot"]["updated_at"] = json!("time_b"); + second["candidates"][0]["auth_snapshot"]["secrets"][0]["updated_at"] = json!("time_b"); + assert_eq!( + plan_digest_from_payload(&first).unwrap(), + plan_digest_from_payload(&second).unwrap() + ); + } + + #[test] + fn v2_semantic_changes_and_schema_fields_named_like_runtime_fields_change_digest() { + let first = payload(); + for pointer in [ + "/catalog_revision", + "/support_matrix_version", + "/source/digest", + "/candidates/0/decision/action", + "/candidates/0/auth_snapshot/secrets/0/current_version", + "/candidates/0/operation/input_schema/properties/id/default", + "/candidates/0/operation/input_schema/properties/created_at/default", + ] { + let mut changed = first.clone(); + *changed.pointer_mut(pointer).unwrap() = json!("changed"); + assert_ne!( + plan_digest_from_payload(&first).unwrap(), + plan_digest_from_payload(&changed).unwrap(), + "{pointer}" + ); + } + let mut target = first.clone(); + target["candidates"][0]["decision"]["action"] = json!("new_version"); + let mut changed_target = target.clone(); + changed_target["candidates"][0]["operation"]["id"] = json!("different_existing_target"); + assert_ne!( + plan_digest_from_payload(&target).unwrap(), + plan_digest_from_payload(&changed_target).unwrap() + ); + } + + #[test] + fn legacy_payload_digest_still_covers_runtime_and_identity_fields() { + let mut first = payload(); + first["schema_version"] = json!("import-plan-v1"); + let mut second = first.clone(); + second["plan_reference"] = json!("other"); + assert_ne!( + plan_digest_from_payload(&first).unwrap(), + plan_digest_from_payload(&second).unwrap() + ); + } + + #[test] + fn v2_payload_budget_grows_without_changing_the_legacy_budget() { + let mut large = + json!({"schema_version": "import-plan-v1", "evidence": "x".repeat(5 * 1024 * 1024)}); + assert!(plan_digest_from_payload(&large).is_err()); + large["schema_version"] = json!("import-plan-v2"); + assert!(plan_digest_from_payload(&large).is_ok()); + large["evidence"] = json!("x".repeat(16 * 1024 * 1024)); + assert!(plan_digest_from_payload(&large).is_err()); + } +} diff --git a/crates/crank-registry/src/postgres/mod.rs b/crates/crank-registry/src/postgres/mod.rs index 239e2f5..234f1b9 100644 --- a/crates/crank-registry/src/postgres/mod.rs +++ b/crates/crank-registry/src/postgres/mod.rs @@ -9,6 +9,7 @@ mod import_job; mod master_key; mod observability; mod onboarding; +mod onboarding_import; mod operation; mod operation_artifact; mod operation_published; @@ -51,14 +52,16 @@ use crate::{ InvocationRetentionPolicy, InvocationRetentionStatus, ListApprovalRequestsQuery, ListInvocationLogsQuery, ListProductEventsQuery, MasterKeyIdentityCandidate, MasterKeyIdentityRecord, MasterKeyRotationRecord, MasterKeyRotationStatus, - MembershipRecord, OnboardingMilestoneResult, OnboardingPresentationMilestone, - OperationAgentRef, OperationSampleMetadata, OperationStateExpectation, OperationSummary, - OperationUsageSummary, OperationVersionRecord, PlatformApiKeyRecord, ProductEventRecord, - PublishAgentRequest, PublishRequest, PublishedAgentCatalog, PublishedAgentTool, - RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest, RegistryOperation, - RotateSecretRequest, SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, - SaveAuthProfileRequest, SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, - SaveWorkspaceUpstreamRequest, SecretRecord, SecretVersionRecord, SessionRecord, + MembershipRecord, OnboardingEventSnapshot, OnboardingImportChoice, OnboardingImportContext, + OnboardingImportDrift, OnboardingMilestoneResult, OnboardingPresentationMilestone, + OnboardingSnapshot, OperationAgentRef, OperationSampleMetadata, OperationStateExpectation, + OperationSummary, OperationUsageSummary, OperationVersionRecord, PlatformApiKeyRecord, + ProductEventRecord, PublishAgentRequest, PublishRequest, PublishedAgentCatalog, + PublishedAgentTool, RecordOnboardingCompletionRequest, RecordOnboardingMilestoneRequest, + RegisterOnboardingImportContextRequest, RegistryOperation, RotateSecretRequest, + SaveAgentBindingsRequest, SaveAgentCatalogConfigRequest, SaveAuthProfileRequest, + SaveDescriptorMetadataRequest, SaveSampleMetadataRequest, SaveWorkspaceUpstreamRequest, + SecretRecord, SecretVersionRecord, SelectOnboardingImportDraftRequest, SessionRecord, SkippedImportOperation, UpdateWorkspaceRequest, UsageAgentBreakdown, UsageOperationBreakdown, UsageOutcomeBreakdown, UsageQuery, UsageRollupRecord, UsageSummary, UsageTimelinePoint, WorkspaceMembershipRecord, WorkspaceRecord, diff --git a/crates/crank-registry/src/postgres/observability.rs b/crates/crank-registry/src/postgres/observability.rs index b6a8b42..efeab61 100644 --- a/crates/crank-registry/src/postgres/observability.rs +++ b/crates/crank-registry/src/postgres/observability.rs @@ -186,7 +186,7 @@ impl PostgresRegistry { field: "operation_version", value: i64::MAX, })?; - let should_anchor_onboarding = request.log.source + let mut should_anchor_onboarding = request.log.source == crank_core::InvocationSource::AgentToolCall && request.log.status == crank_core::InvocationStatus::Ok && request.log.execution_stage == Some(crank_core::ExecutionStage::Runtime) @@ -200,6 +200,21 @@ impl PostgresRegistry { .bind(request.log.workspace_id.as_str()) .fetch_one(&mut *transaction) .await?; + // A durable import context governs terminal anchoring: a pending + // context suppresses the generic anchor entirely, and an explicit + // selection only anchors the exact selected Operation/version. + // Without an import context the existing generic anchoring stays. + if let Some((selected_operation_id, selected_operation_version)) = + super::onboarding_import::onboarding_import_selection_in_transaction( + &mut transaction, + &request.log.workspace_id, + ) + .await? + { + should_anchor_onboarding = selected_operation_id.as_deref() + == Some(request.log.operation_id.as_str()) + && selected_operation_version == Some(operation_version); + } } sqlx::query( "insert into invocation_logs ( @@ -404,6 +419,14 @@ impl PostgresRegistry { .await?; } } + // Record import funnel publication/activation at the authoritative + // call boundary (idempotent; no-op without a selected import). + super::onboarding_import::record_import_funnel_milestones_in_transaction( + &mut transaction, + &request.log.workspace_id, + request.log.created_at, + ) + .await?; } transaction.commit().await?; diff --git a/crates/crank-registry/src/postgres/onboarding.rs b/crates/crank-registry/src/postgres/onboarding.rs index 081589a..99f548d 100644 --- a/crates/crank-registry/src/postgres/onboarding.rs +++ b/crates/crank-registry/src/postgres/onboarding.rs @@ -7,20 +7,58 @@ use sqlx::Row; use super::*; +/// Bounded whole-transaction retries for transient PostgreSQL serialization +/// failures. A `REPEATABLE READ` transaction that reads a row later updated by +/// a concurrent transaction aborts with SQLSTATE `40001`; retrying from the +/// start re-reads a consistent snapshot instead of continuing a partial, +/// aborted transaction. +const MAX_ONBOARDING_SERIALIZATION_ATTEMPTS: usize = 4; + +/// Whether a registry error is a transient PostgreSQL serialization/deadlock +/// failure that must abort and retry the whole transaction. +pub(super) fn is_onboarding_serialization_failure(error: &RegistryError) -> bool { + matches!( + error, + RegistryError::Storage(sqlx::Error::Database(database)) + if matches!(database.code().as_deref(), Some("40001") | Some("40P01")) + ) +} + +/// Begins an onboarding transaction with a true, consistent snapshot. +/// +/// The onboarding projection aggregates many relations and is paired with a +/// separately-read durable import context; the default `READ COMMITTED` would +/// let each statement see a different snapshot and pair a revision with newer +/// choices. `REPEATABLE READ` fixes one snapshot for the whole transaction. +pub(super) async fn begin_onboarding_transaction<'a>( + pool: &'a sqlx::PgPool, +) -> Result, RegistryError> { + let mut transaction = pool.begin().await?; + sqlx::query("set transaction isolation level repeatable read") + .execute(&mut *transaction) + .await?; + Ok(transaction) +} + const ONBOARDING_PROJECTION_SQL: &str = r#" with selection_state as ( select operation_id, operation_version, agent_id, catalog_revision, platform_api_key_id, invocation_log_id, test_log_id, evidence_after, selected_at from onboarding_selections where workspace_id = $1 +), import_selection as ( + select application_idempotency_key, plan_digest, selected_operation_id, + selected_operation_version, registered_at, selected_at + from onboarding_import_selections + where workspace_id = $1 ), operation_candidates as ( select o.id, o.current_draft_version, o.latest_published_version, o.updated_at, - coalesce(ss.operation_version, o.latest_published_version, o.current_draft_version) as target_version, + coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version, o.current_draft_version) as target_version, (o.status <> 'archived') as operation_active, exists ( select 1 from invocation_logs l where l.workspace_id = $1 and l.operation_id = o.id - and l.operation_version = coalesce(ss.operation_version, o.latest_published_version, o.current_draft_version) + and l.operation_version = coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version, o.current_draft_version) and (ss.test_log_id is null or l.id = ss.test_log_id) and l.created_at >= coalesce(ss.evidence_after, '-infinity'::timestamptz) and l.source = 'admin_test_run' and l.status = 'ok' @@ -30,14 +68,14 @@ with selection_state as ( exists ( select 1 from published_operations po where po.operation_id = o.id - and po.version = coalesce(ss.operation_version, o.latest_published_version) + and po.version = coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version) ) as published, exists ( select 1 from agent_operation_bindings b join published_agents pa on pa.agent_id = b.agent_id and pa.version = b.agent_version join agents a on a.id = pa.agent_id where b.operation_id = o.id - and b.operation_version = coalesce(ss.operation_version, o.latest_published_version) + and b.operation_version = coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version) and (ss.agent_id is null or a.id = ss.agent_id) and (ss.catalog_revision is null or pa.catalog_revision = ss.catalog_revision) and b.enabled and a.workspace_id = $1 and a.status = 'published' @@ -49,7 +87,7 @@ with selection_state as ( join agents a on a.id = pa.agent_id join platform_api_keys k on k.workspace_id = $1 and k.agent_id = a.id where b.operation_id = o.id - and b.operation_version = coalesce(ss.operation_version, o.latest_published_version) + and b.operation_version = coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version) and (ss.agent_id is null or a.id = ss.agent_id) and (ss.catalog_revision is null or pa.catalog_revision = ss.catalog_revision) and (ss.platform_api_key_id is null or k.id = ss.platform_api_key_id) @@ -67,7 +105,7 @@ with selection_state as ( on l.workspace_id = $1 and l.agent_id = a.id and l.platform_api_key_id = k.id and l.operation_id = o.id and l.operation_version = b.operation_version where b.operation_id = o.id - and b.operation_version = coalesce(ss.operation_version, o.latest_published_version) + and b.operation_version = coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version) and (ss.agent_id is null or a.id = ss.agent_id) and (ss.catalog_revision is null or pa.catalog_revision = ss.catalog_revision) and (ss.platform_api_key_id is null or k.id = ss.platform_api_key_id) @@ -91,7 +129,7 @@ with selection_state as ( on l.workspace_id = $1 and l.agent_id = a.id and l.platform_api_key_id = k.id and l.operation_id = o.id and l.operation_version = b.operation_version where b.operation_id = o.id - and b.operation_version = coalesce(ss.operation_version, o.latest_published_version) + and b.operation_version = coalesce(ss.operation_version, case when imp.selected_operation_id = o.id then imp.selected_operation_version end, o.latest_published_version) and (ss.agent_id is null or a.id = ss.agent_id) and (ss.catalog_revision is null or pa.catalog_revision = ss.catalog_revision) and (ss.platform_api_key_id is null or k.id = ss.platform_api_key_id) @@ -106,7 +144,14 @@ with selection_state as ( ) as historical_first_call from operations o left join selection_state ss on true - where o.workspace_id = $1 and (ss.operation_id is null or o.id = ss.operation_id) + left join import_selection imp on true + where o.workspace_id = $1 + and (ss.operation_id is null or o.id = ss.operation_id) + and ( + ss.operation_id is not null + or (imp.selected_operation_id is not null and o.id = imp.selected_operation_id) + or (imp.selected_operation_id is null and not exists (select 1 from import_selection)) + ) ), selected_operation as ( select o.id, o.current_draft_version, o.latest_published_version, o.updated_at, o.target_version, o.operation_active @@ -248,9 +293,39 @@ select os.id as operation_id, os.operation_active, os.tested, os.published, os.p coalesce(sk.expires_at::text, ''), coalesce(sk.key_active::text, ''), coalesce(fc.id, ''), coalesce(es.event_count::text, ''), coalesce(es.last_event_at::text, ''), coalesce(es.last_event_id, ''), - coalesce((select evidence_after::text from selection_state), ''), - coalesce((select selected_at::text from selection_state), '') - ), 0) & 9223372036854775807) as revision + coalesce((select evidence_after::text from selection_state), ''), + coalesce((select selected_at::text from selection_state), ''), + coalesce((select application_idempotency_key from import_selection), ''), + coalesce((select plan_digest from import_selection), ''), + coalesce((select selected_operation_id from import_selection), ''), + coalesce((select selected_operation_version::text from import_selection), ''), + coalesce((select registered_at::text from import_selection), ''), + coalesce((select selected_at::text from import_selection), ''), + coalesce((select o.current_draft_version::text from operations o + where o.workspace_id = $1 + and o.id = (select selected_operation_id from import_selection)), ''), + coalesce(( + select string_agg( + candidate.operation_id || ':' || coalesce(o.status, 'missing') || ':' || + coalesce(o.current_draft_version::text, 'null'), + ',' order by candidate.operation_id + ) + from ( + select distinct outcome ->> 'operation_id' as operation_id + from import_plan_applications a + cross join lateral jsonb_array_elements(a.report -> 'outcomes') as outcome + where a.workspace_id = $1 + and a.idempotency_key = ( + select application_idempotency_key from import_selection + ) + and a.plan_digest = (select plan_digest from import_selection) + and outcome ->> 'outcome' in ('created', 'versioned', 'renamed') + and outcome ->> 'operation_id' is not null + ) candidate + left join operations o + on o.workspace_id = $1 and o.id = candidate.operation_id + ), '') + ), 0) & 9007199254740991) as revision from event_state es left join operation_state os on true left join selected_agent sa on true @@ -259,41 +334,120 @@ select os.id as operation_id, os.operation_active, os.tested, os.published, os.p "#; impl PostgresRegistry { + /// Retries a whole onboarding transaction on transient serialization + /// failures. Each attempt starts from scratch, so an aborted transaction is + /// never continued partially. Exhausted retries surface as a truthful + /// revision conflict so the client reloads and revalidates. + pub(super) async fn with_onboarding_retry( + &self, + mut operation: F, + ) -> Result + where + F: FnMut() -> Fut, + Fut: std::future::Future>, + { + let mut attempt = 0; + loop { + match operation().await { + Err(error) if is_onboarding_serialization_failure(&error) => { + attempt += 1; + if attempt >= MAX_ONBOARDING_SERIALIZATION_ATTEMPTS { + return Err(RegistryError::OnboardingStaleRevision); + } + } + result => return result, + } + } + } + /// Ensures the workspace enters the server-owned onboarding cohort exactly once. pub async fn ensure_onboarding_eligibility( &self, workspace_id: &WorkspaceId, occurred_at: OffsetDateTime, ) -> Result { - let mut transaction = self.pool.begin().await?; - lock_onboarding_workspace(&mut transaction, workspace_id).await?; - let eligible = super::product_event::get_product_event_by_idempotency_key_in_transaction( - &mut transaction, - workspace_id, - "onboarding:eligible:v1", - ) - .await?; - let eligible_since = if let Some(record) = eligible { - record.event.eligible_since.unwrap_or(occurred_at) - } else { - let event = ProductEvent { - id: ProductEventId::new(format!("pe_{}", uuid::Uuid::now_v7().simple())), - workspace_id: workspace_id.clone(), - kind: ProductEventKind::OnboardingEligible, - schema_version: PRODUCT_EVENT_SCHEMA_VERSION, - milestone: None, - eligible: true, - eligible_since: Some(occurred_at), - idempotency_key: "onboarding:eligible:v1".to_owned(), + Ok(self + .ensure_onboarding_eligibility_snapshot(workspace_id, occurred_at) + .await? + .projection) + } + + /// Ensures eligibility and returns the authoritative projection together + /// with the durable import context from the same locked transaction. + pub async fn ensure_onboarding_eligibility_snapshot( + &self, + workspace_id: &WorkspaceId, + occurred_at: OffsetDateTime, + ) -> Result { + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, workspace_id).await?; + let eligible_since = ensure_onboarding_eligible_in_transaction( + &mut transaction, + workspace_id, occurred_at, - }; - super::product_event::append_product_event_in_transaction(&mut transaction, &event) + ) + .await?; + super::onboarding_import::record_import_funnel_milestones_in_transaction( + &mut transaction, + workspace_id, + occurred_at, + ) + .await?; + let mut projection = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; + if projection.completed && !projection.was_completed { + let event = ProductEvent { + id: ProductEventId::new(format!("pe_{}", uuid::Uuid::now_v7().simple())), + workspace_id: workspace_id.clone(), + kind: ProductEventKind::OnboardingCompleted, + schema_version: PRODUCT_EVENT_SCHEMA_VERSION, + milestone: None, + eligible: true, + eligible_since: Some(eligible_since), + idempotency_key: "onboarding:completed:v1".to_owned(), + occurred_at: projection.first_call_at.unwrap_or(occurred_at), + }; + super::product_event::append_product_event_in_transaction(&mut transaction, &event) + .await?; + projection = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id) + .await?; + } + let import_context = + super::onboarding_import::get_onboarding_import_context_in_transaction( + &mut transaction, + workspace_id, + ) .await?; - occurred_at - }; - let mut projection = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - if projection.completed && !projection.was_completed { + transaction.commit().await?; + Ok(OnboardingSnapshot { + projection, + import_context, + }) + }) + .await + } + + /// Records the server-owned completion once the authoritative projection is terminal. + pub async fn ensure_onboarding_completion( + &self, + workspace_id: &WorkspaceId, + _occurred_at: OffsetDateTime, + ) -> Result { + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, workspace_id).await?; + let projection = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; + if !projection.completed { + transaction.commit().await?; + return Ok(projection); + } + let Some(eligible_since) = projection.eligible_since else { + transaction.commit().await?; + return Ok(projection); + }; let event = ProductEvent { id: ProductEventId::new(format!("pe_{}", uuid::Uuid::now_v7().simple())), workspace_id: workspace_id.clone(), @@ -303,55 +457,20 @@ impl PostgresRegistry { eligible: true, eligible_since: Some(eligible_since), idempotency_key: "onboarding:completed:v1".to_owned(), - occurred_at: projection.first_call_at.unwrap_or(occurred_at), + occurred_at: projection.first_call_at.ok_or( + RegistryError::InvalidExecutionRecord { + field: "onboarding.first_call_at", + }, + )?, }; super::product_event::append_product_event_in_transaction(&mut transaction, &event) .await?; - projection = + let projection = get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - } - transaction.commit().await?; - Ok(projection) - } - - /// Records the server-owned completion once the authoritative projection is terminal. - pub async fn ensure_onboarding_completion( - &self, - workspace_id: &WorkspaceId, - _occurred_at: OffsetDateTime, - ) -> Result { - let mut transaction = self.pool.begin().await?; - lock_onboarding_workspace(&mut transaction, workspace_id).await?; - let projection = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - if !projection.completed { transaction.commit().await?; - return Ok(projection); - } - let Some(eligible_since) = projection.eligible_since else { - transaction.commit().await?; - return Ok(projection); - }; - let event = ProductEvent { - id: ProductEventId::new(format!("pe_{}", uuid::Uuid::now_v7().simple())), - workspace_id: workspace_id.clone(), - kind: ProductEventKind::OnboardingCompleted, - schema_version: PRODUCT_EVENT_SCHEMA_VERSION, - milestone: None, - eligible: true, - eligible_since: Some(eligible_since), - idempotency_key: "onboarding:completed:v1".to_owned(), - occurred_at: projection - .first_call_at - .ok_or(RegistryError::InvalidExecutionRecord { - field: "onboarding.first_call_at", - })?, - }; - super::product_event::append_product_event_in_transaction(&mut transaction, &event).await?; - let projection = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - transaction.commit().await?; - Ok(projection) + Ok(projection) + }) + .await } pub async fn get_onboarding_projection( @@ -365,44 +484,107 @@ impl PostgresRegistry { map_projection(workspace_id, &row) } + /// Reads the authoritative projection and the durable import context from + /// one workspace-locked transaction so revision and context cannot diverge. + pub async fn get_onboarding_snapshot( + &self, + workspace_id: &WorkspaceId, + ) -> Result { + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, workspace_id).await?; + let snapshot = + get_onboarding_snapshot_in_transaction(&mut transaction, workspace_id).await?; + transaction.commit().await?; + Ok(snapshot) + }) + .await + } + pub async fn reset_onboarding_selection( &self, workspace_id: &WorkspaceId, expected_revision: i64, occurred_at: OffsetDateTime, ) -> Result { - let mut transaction = self.pool.begin().await?; - lock_onboarding_workspace(&mut transaction, workspace_id).await?; - let current = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - if current.revision != expected_revision { - return Err(RegistryError::OnboardingStaleRevision); - } - sqlx::query( - "insert into onboarding_selections ( - workspace_id, operation_id, operation_version, agent_id, catalog_revision, - platform_api_key_id, invocation_log_id, test_log_id, evidence_after, selected_at - ) values ($1, null, null, null, null, null, null, null, $2, null) - on conflict (workspace_id) do update - set operation_id = null, operation_version = null, agent_id = null, - catalog_revision = null, platform_api_key_id = null, - invocation_log_id = null, test_log_id = null, - evidence_after = excluded.evidence_after, selected_at = null", - ) - .bind(workspace_id.as_str()) - .bind(occurred_at) - .execute(&mut *transaction) - .await?; - let projection = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - transaction.commit().await?; - Ok(projection) + Ok(self + .reset_onboarding_selection_snapshot(workspace_id, expected_revision, occurred_at) + .await? + .projection) + } + + /// Resets the terminal/import selection and returns the resulting + /// projection with its (now absent) import context from one snapshot. + pub async fn reset_onboarding_selection_snapshot( + &self, + workspace_id: &WorkspaceId, + expected_revision: i64, + occurred_at: OffsetDateTime, + ) -> Result { + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, workspace_id).await?; + let current = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; + if current.revision != expected_revision { + return Err(RegistryError::OnboardingStaleRevision); + } + sqlx::query( + "insert into onboarding_selections ( + workspace_id, operation_id, operation_version, agent_id, catalog_revision, + platform_api_key_id, invocation_log_id, test_log_id, evidence_after, selected_at + ) values ($1, null, null, null, null, null, null, null, $2, null) + on conflict (workspace_id) do update + set operation_id = null, operation_version = null, agent_id = null, + catalog_revision = null, platform_api_key_id = null, + invocation_log_id = null, test_log_id = null, + evidence_after = excluded.evidence_after, selected_at = null", + ) + .bind(workspace_id.as_str()) + .bind(occurred_at) + .execute(&mut *transaction) + .await?; + // Reset clears the durable import context as well, but keeps the + // existing evidence boundary (evidence_after) intact. + sqlx::query("delete from onboarding_import_selections where workspace_id = $1") + .bind(workspace_id.as_str()) + .execute(&mut *transaction) + .await?; + let projection = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; + let import_context = + super::onboarding_import::get_onboarding_import_context_in_transaction( + &mut transaction, + workspace_id, + ) + .await?; + transaction.commit().await?; + Ok(OnboardingSnapshot { + projection, + import_context, + }) + }) + .await } pub async fn record_onboarding_milestone( &self, request: RecordOnboardingMilestoneRequest<'_>, ) -> Result { + let snapshot = self.record_onboarding_milestone_snapshot(request).await?; + Ok(OnboardingMilestoneResult { + accepted: snapshot.accepted, + projection: snapshot.snapshot.projection, + }) + } + + /// Records a presentation milestone and returns the projection and import + /// context read from the same transaction, avoiding a post-mutation pool + /// read that could pair the revision with a newer context. + pub async fn record_onboarding_milestone_snapshot( + &self, + request: RecordOnboardingMilestoneRequest<'_>, + ) -> Result { let kind = match request.milestone { OnboardingPresentationMilestone::Eligible => ProductEventKind::OnboardingEligible, OnboardingPresentationMilestone::Started => ProductEventKind::OnboardingStarted, @@ -410,7 +592,7 @@ impl PostgresRegistry { OnboardingPresentationMilestone::Dismissed => ProductEventKind::OnboardingDismissed, OnboardingPresentationMilestone::Abandoned => ProductEventKind::OnboardingAbandoned, }; - self.record_onboarding_event( + self.record_onboarding_event_snapshot( request.workspace_id, request.event_id, kind, @@ -426,7 +608,20 @@ impl PostgresRegistry { &self, request: RecordOnboardingCompletionRequest<'_>, ) -> Result { - self.record_onboarding_event( + let snapshot = self.record_onboarding_completion_snapshot(request).await?; + Ok(OnboardingMilestoneResult { + accepted: snapshot.accepted, + projection: snapshot.snapshot.projection, + }) + } + + /// Records the authoritative completion and returns the projection and + /// import context from the same transaction. + pub async fn record_onboarding_completion_snapshot( + &self, + request: RecordOnboardingCompletionRequest<'_>, + ) -> Result { + self.record_onboarding_event_snapshot( request.workspace_id, request.event_id, ProductEventKind::OnboardingCompleted, @@ -439,7 +634,7 @@ impl PostgresRegistry { } #[allow(clippy::too_many_arguments)] - async fn record_onboarding_event( + async fn record_onboarding_event_snapshot( &self, workspace_id: &WorkspaceId, event_id: &ProductEventId, @@ -448,60 +643,114 @@ impl PostgresRegistry { expected_revision: i64, occurred_at: OffsetDateTime, eligible_since: Option, - ) -> Result { + ) -> Result { if idempotency_key.starts_with(PRODUCT_EVENT_SERVER_IDEMPOTENCY_PREFIX) { return Err(RegistryError::InvalidExecutionRecord { field: "product_event.reserved_idempotency_key", }); } - let mut transaction = self.pool.begin().await?; - lock_onboarding_workspace(&mut transaction, workspace_id).await?; - let current = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - if kind == ProductEventKind::OnboardingCompleted && !current.completed { - return Err(RegistryError::OnboardingIncomplete); - } - let event = ProductEvent { - id: event_id.clone(), - workspace_id: workspace_id.clone(), - kind, - schema_version: PRODUCT_EVENT_SCHEMA_VERSION, - milestone: None, - eligible: eligible_since.is_some(), - eligible_since, - idempotency_key: idempotency_key.to_owned(), - occurred_at, - }; - if current.revision != expected_revision { - let replay = super::product_event::get_product_event_by_idempotency_key_in_transaction( - &mut transaction, - workspace_id, - idempotency_key, - ) - .await?; - if replay.is_some_and(|record| event.is_semantic_replay_of(&record.event)) { - transaction.commit().await?; - return Ok(OnboardingMilestoneResult { - accepted: false, - projection: current, - }); + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, workspace_id).await?; + let current = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; + if kind == ProductEventKind::OnboardingCompleted && !current.completed { + return Err(RegistryError::OnboardingIncomplete); } - return Err(RegistryError::OnboardingStaleRevision); - } - let outcome = - super::product_event::append_product_event_in_transaction(&mut transaction, &event) + let event = ProductEvent { + id: event_id.clone(), + workspace_id: workspace_id.clone(), + kind, + schema_version: PRODUCT_EVENT_SCHEMA_VERSION, + milestone: None, + eligible: eligible_since.is_some(), + eligible_since, + idempotency_key: idempotency_key.to_owned(), + occurred_at, + }; + if current.revision != expected_revision { + let replay = + super::product_event::get_product_event_by_idempotency_key_in_transaction( + &mut transaction, + workspace_id, + idempotency_key, + ) + .await?; + if replay.is_some_and(|record| event.is_semantic_replay_of(&record.event)) { + let import_context = + super::onboarding_import::get_onboarding_import_context_in_transaction( + &mut transaction, + workspace_id, + ) + .await?; + transaction.commit().await?; + return Ok(OnboardingEventSnapshot { + accepted: false, + snapshot: OnboardingSnapshot { + projection: current, + import_context, + }, + }); + } + return Err(RegistryError::OnboardingStaleRevision); + } + let outcome = + super::product_event::append_product_event_in_transaction(&mut transaction, &event) + .await?; + let projection = + get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; + let import_context = + super::onboarding_import::get_onboarding_import_context_in_transaction( + &mut transaction, + workspace_id, + ) .await?; - let projection = - get_onboarding_projection_in_transaction(&mut transaction, workspace_id).await?; - transaction.commit().await?; - Ok(OnboardingMilestoneResult { - accepted: outcome == AppendProductEventOutcome::Recorded, - projection, + transaction.commit().await?; + Ok(OnboardingEventSnapshot { + accepted: outcome == AppendProductEventOutcome::Recorded, + snapshot: OnboardingSnapshot { + projection, + import_context, + }, + }) }) + .await } } -async fn lock_onboarding_workspace( +/// Ensures the workspace is in the server-owned onboarding eligible cohort and +/// returns its authoritative `eligible_since` timestamp. Idempotent: the +/// cohort event is created at most once per workspace. +pub(super) async fn ensure_onboarding_eligible_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + occurred_at: OffsetDateTime, +) -> Result { + let eligible = super::product_event::get_product_event_by_idempotency_key_in_transaction( + transaction, + workspace_id, + "onboarding:eligible:v1", + ) + .await?; + if let Some(record) = eligible { + return Ok(record.event.eligible_since.unwrap_or(occurred_at)); + } + let event = ProductEvent { + id: ProductEventId::new(format!("pe_{}", uuid::Uuid::now_v7().simple())), + workspace_id: workspace_id.clone(), + kind: ProductEventKind::OnboardingEligible, + schema_version: PRODUCT_EVENT_SCHEMA_VERSION, + milestone: None, + eligible: true, + eligible_since: Some(occurred_at), + idempotency_key: "onboarding:eligible:v1".to_owned(), + occurred_at, + }; + super::product_event::append_product_event_in_transaction(transaction, &event).await?; + Ok(occurred_at) +} + +pub(super) async fn lock_onboarding_workspace( transaction: &mut Transaction<'_, Postgres>, workspace_id: &WorkspaceId, ) -> Result<(), RegistryError> { @@ -529,6 +778,22 @@ pub(super) async fn get_onboarding_projection_in_transaction( map_projection(workspace_id, &row) } +pub(super) async fn get_onboarding_snapshot_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, +) -> Result { + let projection = get_onboarding_projection_in_transaction(transaction, workspace_id).await?; + let import_context = super::onboarding_import::get_onboarding_import_context_in_transaction( + transaction, + workspace_id, + ) + .await?; + Ok(OnboardingSnapshot { + projection, + import_context, + }) +} + fn map_projection( workspace_id: &WorkspaceId, row: &sqlx::postgres::PgRow, diff --git a/crates/crank-registry/src/postgres/onboarding_import.rs b/crates/crank-registry/src/postgres/onboarding_import.rs new file mode 100644 index 0000000..fa01835 --- /dev/null +++ b/crates/crank-registry/src/postgres/onboarding_import.rs @@ -0,0 +1,749 @@ +use std::collections::HashMap; + +use crank_core::{ + OnboardingProjection, OnboardingStepId, OperationId, PRODUCT_EVENT_SCHEMA_VERSION, + ProductEvent, ProductEventId, ProductEventKind, WorkspaceId, +}; +use serde_json::Value; +use sqlx::{Postgres, Row, Transaction}; +use time::OffsetDateTime; + +use super::onboarding::{ + begin_onboarding_transaction, ensure_onboarding_eligible_in_transaction, + get_onboarding_projection_in_transaction, lock_onboarding_workspace, +}; +use super::*; + +const MAX_APPLICATION_KEY_BYTES: usize = 128; + +struct ImportContextRow { + application_idempotency_key: String, + plan_digest: String, + selected_operation_id: Option, + selected_operation_version: Option, +} + +impl PostgresRegistry { + /// Registers the durable workspace import context for an already committed + /// immutable apply report. + /// + /// Only the application identity (`application_idempotency_key`, + /// `plan_digest`) is persisted; the report itself is never copied. The + /// context starts pending (no selected operation) and suppresses generic + /// automatic Operation selection until the administrator explicitly picks + /// one actionable Draft. Exact re-registration is idempotent and keeps any + /// existing selection. + pub async fn register_onboarding_import_context( + &self, + request: RegisterOnboardingImportContextRequest<'_>, + ) -> Result { + Ok(self + .register_onboarding_import_context_snapshot(request) + .await? + .projection) + } + + /// Registers the durable context and returns the authoritative projection + /// together with the context read from the same transaction/snapshot. + pub async fn register_onboarding_import_context_snapshot( + &self, + request: RegisterOnboardingImportContextRequest<'_>, + ) -> Result { + validate_application_key(request.application_idempotency_key)?; + validate_plan_digest(request.plan_digest)?; + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, request.workspace_id).await?; + let current = + get_onboarding_projection_in_transaction(&mut transaction, request.workspace_id) + .await?; + if current.revision != request.expected_revision { + return Err(RegistryError::OnboardingStaleRevision); + } + let Some(application) = load_application( + &mut transaction, + request.workspace_id, + request.application_idempotency_key, + ) + .await? + else { + return Err(RegistryError::OnboardingImportApplicationUnavailable); + }; + if application.0 != request.plan_digest || !report_is_applied(&application.1) { + return Err(RegistryError::OnboardingImportApplicationUnavailable); + } + let actionable = report_has_actionable_outcome(&application.1); + let existing = load_context(&mut transaction, request.workspace_id).await?; + let same_identity = existing.as_ref().is_some_and(|context| { + context.application_idempotency_key == request.application_idempotency_key + && context.plan_digest == request.plan_digest + }); + if !same_identity { + sqlx::query( + "insert into onboarding_import_selections ( + workspace_id, application_idempotency_key, plan_digest, + selected_operation_id, selected_operation_version, + registered_at, selected_at + ) values ($1, $2, $3, null, null, $4, null) + on conflict (workspace_id) do update + set application_idempotency_key = excluded.application_idempotency_key, + plan_digest = excluded.plan_digest, + selected_operation_id = null, + selected_operation_version = null, + registered_at = excluded.registered_at, + selected_at = null", + ) + .bind(request.workspace_id.as_str()) + .bind(request.application_idempotency_key) + .bind(request.plan_digest) + .bind(request.occurred_at) + .execute(&mut *transaction) + .await?; + } + if actionable { + record_import_eligible_in_transaction( + &mut transaction, + request.workspace_id, + request.application_idempotency_key, + request.occurred_at, + ) + .await?; + } + let projection = + get_onboarding_projection_in_transaction(&mut transaction, request.workspace_id) + .await?; + let import_context = get_onboarding_import_context_in_transaction( + &mut transaction, + request.workspace_id, + ) + .await?; + transaction.commit().await?; + Ok(OnboardingSnapshot { + projection, + import_context, + }) + }) + .await + } + + /// Explicitly anchors one actionable Draft produced by the registered + /// immutable apply report as the OpenAPI onboarding target. + /// + /// The selection must carry the exact registered application identity and + /// plan digest, and the Draft must be the operation's live current Draft + /// with the exact strict version. Forged, cross-workspace, published, + /// archived or drifted choices fail without mutation. + pub async fn select_onboarding_import_draft( + &self, + request: SelectOnboardingImportDraftRequest<'_>, + ) -> Result { + Ok(self + .select_onboarding_import_draft_snapshot(request) + .await? + .projection) + } + + /// Selects one actionable imported Draft and returns the authoritative + /// projection together with the context from the same snapshot. + pub async fn select_onboarding_import_draft_snapshot( + &self, + request: SelectOnboardingImportDraftRequest<'_>, + ) -> Result { + validate_application_key(request.application_idempotency_key)?; + validate_plan_digest(request.plan_digest)?; + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + lock_onboarding_workspace(&mut transaction, request.workspace_id).await?; + let current = + get_onboarding_projection_in_transaction(&mut transaction, request.workspace_id) + .await?; + if current.revision != request.expected_revision { + return Err(RegistryError::OnboardingStaleRevision); + } + let Some(context) = load_context(&mut transaction, request.workspace_id).await? else { + return Err(RegistryError::OnboardingImportContextUnavailable); + }; + if context.application_idempotency_key != request.application_idempotency_key + || context.plan_digest != request.plan_digest + { + return Err(RegistryError::OnboardingImportContextUnavailable); + } + let Some(application) = load_application( + &mut transaction, + request.workspace_id, + request.application_idempotency_key, + ) + .await? + else { + return Err(RegistryError::OnboardingImportApplicationUnavailable); + }; + if application.0 != request.plan_digest || !report_is_applied(&application.1) { + return Err(RegistryError::OnboardingImportApplicationUnavailable); + } + if successful_outcome_version(&application.1, request.operation_id.as_str()) + != Some(request.operation_version) + { + return Err(RegistryError::OnboardingImportDraftUnavailable); + } + // Lock the exact selected Draft row and validate exact + // workspace/status/version under that lock so a concurrent archive + // or edit cannot slip between validation and selection. + match current_draft_version( + &mut transaction, + request.workspace_id, + request.operation_id.as_str(), + ) + .await? + { + Some((status, version)) + if status == "draft" && version == request.operation_version => {} + _ => return Err(RegistryError::OnboardingImportDraftUnavailable), + } + sqlx::query( + "update onboarding_import_selections + set selected_operation_id = $2, + selected_operation_version = $3, + selected_at = $4 + where workspace_id = $1", + ) + .bind(request.workspace_id.as_str()) + .bind(request.operation_id.as_str()) + .bind(i32::try_from(request.operation_version).map_err(|_| { + RegistryError::InvalidNumericValue { + field: "onboarding.import.operation_version", + value: i64::from(request.operation_version), + } + })?) + .bind(request.occurred_at) + .execute(&mut *transaction) + .await?; + record_import_funnel_milestones_in_transaction( + &mut transaction, + request.workspace_id, + request.occurred_at, + ) + .await?; + let projection = + get_onboarding_projection_in_transaction(&mut transaction, request.workspace_id) + .await?; + let import_context = get_onboarding_import_context_in_transaction( + &mut transaction, + request.workspace_id, + ) + .await?; + transaction.commit().await?; + Ok(OnboardingSnapshot { + projection, + import_context, + }) + }) + .await + } + + /// Resolves the complete authoritative import context for a workspace, + /// re-reading the immutable report from `import_plan_applications` by the + /// durable identity and verifying the exact plan digest. Pending context + /// has `selected_operation_id`/`selected_operation_version` as `None`. + pub async fn get_onboarding_import_context( + &self, + workspace_id: &WorkspaceId, + ) -> Result, RegistryError> { + self.with_onboarding_retry(|| async { + let mut transaction = begin_onboarding_transaction(&self.pool).await?; + let context = + get_onboarding_import_context_in_transaction(&mut transaction, workspace_id) + .await?; + transaction.commit().await?; + Ok(context) + }) + .await + } +} + +/// Reads the durable import context and re-reads the immutable report from +/// `import_plan_applications` on the same transaction/snapshot as the caller's +/// projection, so report/choices/drift can never pair with a stale revision. +pub(super) async fn get_onboarding_import_context_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, +) -> Result, RegistryError> { + let Some(context) = sqlx::query( + "select application_idempotency_key, plan_digest, selected_operation_id, + selected_operation_version + from onboarding_import_selections + where workspace_id = $1", + ) + .bind(workspace_id.as_str()) + .fetch_optional(&mut **transaction) + .await? + else { + return Ok(None); + }; + let application_idempotency_key: String = context.try_get("application_idempotency_key")?; + let plan_digest: String = context.try_get("plan_digest")?; + let selected_operation_id: Option = context.try_get("selected_operation_id")?; + let selected_operation_version: Option = context.try_get("selected_operation_version")?; + let Some(application) = sqlx::query( + "select plan_digest, report + from import_plan_applications + where workspace_id = $1 and idempotency_key = $2 + for key share", + ) + .bind(workspace_id.as_str()) + .bind(&application_idempotency_key) + .fetch_optional(&mut **transaction) + .await? + else { + return Err(RegistryError::OnboardingImportApplicationUnavailable); + }; + let stored_digest: String = application.try_get("plan_digest")?; + if stored_digest != plan_digest { + return Err(RegistryError::OnboardingImportApplicationUnavailable); + } + let report: Value = application.try_get("report")?; + let live = live_operations(transaction, workspace_id).await?; + let choices = report_choices(&report, &live); + let selected_operation_id = selected_operation_id.map(OperationId::new); + let selected_operation_version = selected_operation_version + .map(|value| { + u32::try_from(value).map_err(|_| RegistryError::InvalidNumericValue { + field: "onboarding.import.operation_version", + value: i64::from(value), + }) + }) + .transpose()?; + let drift = compute_drift( + &live, + selected_operation_id.as_ref(), + selected_operation_version, + ); + Ok(Some(OnboardingImportContext { + application_idempotency_key, + plan_digest, + report, + choices, + selected_operation_id, + selected_operation_version, + drift, + })) +} + +/// Records the import funnel conversion milestones exactly once per durable +/// application identity. `import-to-publish` requires publication of the +/// explicitly selected operation/version and `agent activation` requires the +/// authoritative successful public first call on that exact operation/version +/// (a published Agent alone never counts). Properties stay redacted and there +/// is no outbound egress: these are local append-only rows feeding local daily +/// rollups. +pub(super) async fn record_import_funnel_milestones_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + occurred_at: OffsetDateTime, +) -> Result<(), RegistryError> { + let Some(context) = load_context(transaction, workspace_id).await? else { + return Ok(()); + }; + let (Some(operation_id), Some(operation_version)) = ( + context.selected_operation_id, + context.selected_operation_version, + ) else { + return Ok(()); + }; + // Published/activated milestones inherit the authoritative server-owned + // cohort so their funnel properties always carry `eligible=true` and the + // same `eligible_since` as the eligibility denominator. + let eligible_since = + ensure_onboarding_eligible_in_transaction(transaction, workspace_id, occurred_at).await?; + let projection = get_onboarding_projection_in_transaction(transaction, workspace_id).await?; + // An existing terminal tuple takes precedence over later import contexts. + // Its evidence must never be attributed to a different imported target. + if projection.operation_id.as_ref().map(OperationId::as_str) != Some(operation_id.as_str()) + || projection.operation_version != u32::try_from(operation_version).ok() + { + return Ok(()); + } + let published = projection + .step(OnboardingStepId::PublishOperation) + .is_some_and(|step| step.completed); + let activated = projection + .step(OnboardingStepId::FirstCall) + .is_some_and(|step| step.completed); + if published { + // The conversion is anchored to the exact published version's own + // `published_at`, not the observation time. + let Some(published_at) = selected_version_published_at( + transaction, + workspace_id, + operation_id.as_str(), + operation_version, + ) + .await? + else { + return Ok(()); + }; + append_funnel_event_with_cohort( + transaction, + workspace_id, + ProductEventKind::OnboardingImportPublished, + format!( + "onboarding:import-published:v1:{}:{}:{}", + context.application_idempotency_key, operation_id, operation_version + ), + published_at, + Some(eligible_since), + ) + .await?; + } + if activated { + // Activation is anchored to the authoritative successful public first + // call timestamp from the same projection, not the observation time. + let Some(first_call_at) = projection.first_call_at else { + return Ok(()); + }; + append_funnel_event_with_cohort( + transaction, + workspace_id, + ProductEventKind::OnboardingAgentActivated, + format!( + "onboarding:agent-activated:v1:{}:{}:{}", + context.application_idempotency_key, operation_id, operation_version + ), + first_call_at, + Some(eligible_since), + ) + .await?; + } + Ok(()) +} + +async fn selected_version_published_at( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + operation_id: &str, + version: i32, +) -> Result, RegistryError> { + sqlx::query_scalar( + "select po.published_at + from published_operations po + join operations o on o.id = po.operation_id and o.workspace_id = $1 + where po.operation_id = $2 and po.version = $3", + ) + .bind(workspace_id.as_str()) + .bind(operation_id) + .bind(version) + .fetch_optional(&mut **transaction) + .await + .map_err(RegistryError::from) +} + +async fn record_import_eligible_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + application_idempotency_key: &str, + occurred_at: OffsetDateTime, +) -> Result<(), RegistryError> { + let eligible_since = + ensure_onboarding_eligible_in_transaction(transaction, workspace_id, occurred_at).await?; + let idempotency_key = format!( + "onboarding:import-eligible:v1:{}", + application_idempotency_key + ); + append_funnel_event_with_cohort( + transaction, + workspace_id, + ProductEventKind::OnboardingImportEligible, + idempotency_key, + occurred_at, + Some(eligible_since), + ) + .await +} + +async fn append_funnel_event_with_cohort( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + kind: ProductEventKind, + idempotency_key: String, + occurred_at: OffsetDateTime, + eligible_since: Option, +) -> Result<(), RegistryError> { + let event = ProductEvent { + id: ProductEventId::new(format!("pe_{}", uuid::Uuid::now_v7().simple())), + workspace_id: workspace_id.clone(), + kind, + schema_version: PRODUCT_EVENT_SCHEMA_VERSION, + milestone: None, + eligible: eligible_since.is_some(), + eligible_since, + idempotency_key, + occurred_at, + }; + super::product_event::append_product_event_in_transaction(transaction, &event).await?; + Ok(()) +} + +/// Returns the durable import context's explicit selection, if any, for +/// terminal-anchor gating. `Some((None, None))` means a pending context that +/// suppresses generic anchoring. +pub(super) async fn onboarding_import_selection_in_transaction( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, +) -> Result, Option)>, RegistryError> { + let row = sqlx::query( + "select selected_operation_id, selected_operation_version + from onboarding_import_selections + where workspace_id = $1", + ) + .bind(workspace_id.as_str()) + .fetch_optional(&mut **transaction) + .await?; + row.map(|row| { + Ok(( + row.try_get::, _>("selected_operation_id")?, + row.try_get::, _>("selected_operation_version")?, + )) + }) + .transpose() +} + +async fn load_context( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, +) -> Result, RegistryError> { + let row = sqlx::query( + "select application_idempotency_key, plan_digest, selected_operation_id, + selected_operation_version + from onboarding_import_selections + where workspace_id = $1", + ) + .bind(workspace_id.as_str()) + .fetch_optional(&mut **transaction) + .await?; + row.map(|row| { + Ok(ImportContextRow { + application_idempotency_key: row.try_get("application_idempotency_key")?, + plan_digest: row.try_get("plan_digest")?, + selected_operation_id: row.try_get("selected_operation_id")?, + selected_operation_version: row.try_get("selected_operation_version")?, + }) + }) + .transpose() +} + +async fn load_application( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + idempotency_key: &str, +) -> Result, RegistryError> { + 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(&mut **transaction) + .await?; + row.map(|row| Ok((row.try_get("plan_digest")?, row.try_get("report")?))) + .transpose() +} + +async fn current_draft_version( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, + operation_id: &str, +) -> Result, RegistryError> { + let row = sqlx::query( + "select status, current_draft_version + from operations + where workspace_id = $1 and id = $2 + for update", + ) + .bind(workspace_id.as_str()) + .bind(operation_id) + .fetch_optional(&mut **transaction) + .await?; + let Some(row) = row else { + return Ok(None); + }; + let status: String = row.try_get("status")?; + let version: i32 = row.try_get("current_draft_version")?; + let version = u32::try_from(version).map_err(|_| RegistryError::InvalidNumericValue { + field: "onboarding.import.operation_version", + value: i64::from(version), + })?; + Ok(Some((status, version))) +} + +async fn live_operations( + transaction: &mut Transaction<'_, Postgres>, + workspace_id: &WorkspaceId, +) -> Result, RegistryError> { + let rows = sqlx::query( + "select id, status, current_draft_version + from operations + where workspace_id = $1", + ) + .bind(workspace_id.as_str()) + .fetch_all(&mut **transaction) + .await?; + let mut live = HashMap::new(); + for row in rows { + let id: String = row.try_get("id")?; + let status: String = row.try_get("status")?; + let version: i32 = row.try_get("current_draft_version")?; + let version = u32::try_from(version).map_err(|_| RegistryError::InvalidNumericValue { + field: "onboarding.import.operation_version", + value: i64::from(version), + })?; + live.insert(id, (status, version)); + } + Ok(live) +} + +fn report_is_applied(report: &Value) -> bool { + report.get("status").and_then(Value::as_str) == Some("applied") +} + +fn report_outcomes(report: &Value) -> Vec<&Value> { + report + .get("outcomes") + .and_then(Value::as_array) + .map(|outcomes| outcomes.iter().collect()) + .unwrap_or_default() +} + +fn outcome_kind(outcome: &Value) -> Option<&str> { + outcome.get("outcome").and_then(Value::as_str) +} + +fn is_successful_outcome(outcome: &str) -> bool { + matches!(outcome, "created" | "versioned" | "renamed") +} + +fn report_has_actionable_outcome(report: &Value) -> bool { + report_outcomes(report).into_iter().any(|outcome| { + outcome_kind(outcome).is_some_and(is_successful_outcome) + && outcome + .get("operation_id") + .and_then(Value::as_str) + .is_some() + && outcome.get("version").and_then(Value::as_u64).is_some() + }) +} + +fn successful_outcome_version(report: &Value, operation_id: &str) -> Option { + report_outcomes(report).into_iter().find_map(|outcome| { + let matches_operation = + outcome.get("operation_id").and_then(Value::as_str) == Some(operation_id); + let successful = outcome_kind(outcome).is_some_and(is_successful_outcome); + if matches_operation && successful { + outcome + .get("version") + .and_then(Value::as_u64) + .and_then(|value| u32::try_from(value).ok()) + } else { + None + } + }) +} + +fn report_choices( + report: &Value, + live: &HashMap, +) -> Vec { + let mut choices = Vec::new(); + for outcome in report_outcomes(report) { + let Some(outcome_kind) = outcome_kind(outcome) else { + continue; + }; + let operation_id = outcome + .get("operation_id") + .and_then(Value::as_str) + .map(str::to_owned); + let version = match outcome.get("version") { + Some(value) if !value.is_null() => value.as_u64().and_then(|v| u32::try_from(v).ok()), + _ => None, + }; + let selectable = is_successful_outcome(outcome_kind) + && operation_id + .as_deref() + .zip(version) + .is_some_and(|(operation_id, version)| { + live.get(operation_id) + .is_some_and(|(status, current)| status == "draft" && *current == version) + }); + choices.push(OnboardingImportChoice { + key: outcome + .get("key") + .and_then(Value::as_str) + .unwrap_or_default() + .to_owned(), + name: outcome + .get("name") + .and_then(Value::as_str) + .unwrap_or_default() + .to_owned(), + outcome: outcome_kind.to_owned(), + operation_id, + version, + reason: outcome + .get("reason") + .and_then(Value::as_str) + .map(str::to_owned), + selectable, + }); + } + choices +} + +fn compute_drift( + live: &HashMap, + selected_operation_id: Option<&OperationId>, + selected_operation_version: Option, +) -> Option { + let operation_id = selected_operation_id?; + let selected_version = selected_operation_version?; + match live.get(operation_id.as_str()) { + None => Some(OnboardingImportDrift { + reason: "selected_operation_unavailable".to_owned(), + current_operation_version: None, + }), + // A newer Draft/version of the selected operation is drift. + Some((_, current)) if *current != selected_version => Some(OnboardingImportDrift { + reason: "selected_operation_version_drift".to_owned(), + current_operation_version: Some(*current), + }), + // An exact-version publication (or test) is the expected onboarding + // continuation, not blocking drift; only archive remains a visible + // non-version drift. The chooser already stops offering it as a Draft + // because `report_choices` requires live status `draft`. + Some((status, _)) if status == "archived" => Some(OnboardingImportDrift { + reason: "selected_operation_not_draft".to_owned(), + current_operation_version: Some(selected_version), + }), + _ => None, + } +} + +fn validate_application_key(value: &str) -> Result<(), RegistryError> { + if !(1..=MAX_APPLICATION_KEY_BYTES).contains(&value.len()) { + return Err(RegistryError::InvalidImportPlan { + field: "idempotency_key", + }); + } + Ok(()) +} + +fn validate_plan_digest(value: &str) -> Result<(), RegistryError> { + let valid = value.len() == 64 + && value + .bytes() + .all(|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()); + if !valid { + return Err(RegistryError::InvalidImportPlan { + field: "plan_digest", + }); + } + Ok(()) +} diff --git a/crates/crank-registry/src/postgres/operation.rs b/crates/crank-registry/src/postgres/operation.rs index 8fe98be..bbffa9c 100644 --- a/crates/crank-registry/src/postgres/operation.rs +++ b/crates/crank-registry/src/postgres/operation.rs @@ -812,6 +812,13 @@ impl PostgresRegistry { expected_state: Option<&OperationStateExpectation>, ) -> Result<(), RegistryError> { let mut tx = self.pool.begin().await?; + // Onboarding selection locks workspace state before an Operation row. + // Keep that lock order here while remaining compatible with the + // workspace KEY SHARE lock used by Operation foreign keys. + sqlx::query("select id from workspaces where id = $1 for no key update") + .bind(request.workspace_id.as_str()) + .fetch_optional(&mut *tx) + .await?; let row = sqlx::query( "select status, current_draft_version, latest_published_version from operations @@ -844,6 +851,12 @@ impl PostgresRegistry { .map(|value| from_db_version(value, "latest_published_version")) .transpose()?; if latest == Some(request.version) && current == request.version { + super::onboarding_import::record_import_funnel_milestones_in_transaction( + &mut tx, + request.workspace_id, + *request.published_at, + ) + .await?; tx.commit().await?; return Ok(()); } @@ -953,6 +966,13 @@ impl PostgresRegistry { .execute(&mut *tx) .await?; + super::onboarding_import::record_import_funnel_milestones_in_transaction( + &mut tx, + request.workspace_id, + *request.published_at, + ) + .await?; + tx.commit().await?; Ok(()) } @@ -989,7 +1009,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 ad2dd46..71abcf7 100644 --- a/crates/crank-registry/tests/integration.rs +++ b/crates/crank-registry/tests/integration.rs @@ -5,7 +5,7 @@ mod integration { mod artifact_sources; mod common; mod credential_touch; - mod import_auth_binding; + mod import_plan; mod master_key_identity; mod migrations; mod observability; diff --git a/crates/crank-registry/tests/integration/import_auth_binding.rs b/crates/crank-registry/tests/integration/import_auth_binding.rs deleted file mode 100644 index a3191c8..0000000 --- a/crates/crank-registry/tests/integration/import_auth_binding.rs +++ /dev/null @@ -1,570 +0,0 @@ -use super::common::*; - -use std::{ - fs, - os::unix::fs::PermissionsExt, - path::PathBuf, - sync::atomic::{AtomicU64, Ordering}, -}; - -use crank_artifacts::{ArtifactRef, ArtifactStore}; -use crank_core::{ - ApiKeyHeaderAuthConfig, AuthConfig, AuthKind, AuthProfile, BearerAuthConfig, ExecutionConfig, - Secret, SecretKind, SecretStatus, Workspace, WorkspaceId, WorkspaceStatus, -}; -use crank_registry::{ - ApplyImportJobRequest, ArtifactSourceId, ArtifactSourceSensitivity, - CreateArtifactSourceRequest, CreateImportJobRequest, CreateSecretRequest, - CreateWorkspaceRequest, ImportAuthRequirement, ImportConflictMode, ImportJobId, ImportJobKind, - ImportJobSourceEnvelope, ImportJobStatus, ImportOperationDraft, MASTER_KEY_CIPHER_CONTRACT, - MasterKeyIdentityCandidate, PostgresRegistry, RegistryError, RegistryOperation, - SaveAuthProfileRequest, -}; -use serde_json::json; -use time::OffsetDateTime; -use tokio::time::{Duration as TokioDuration, sleep}; - -static NEXT_ROOT: AtomicU64 = AtomicU64::new(0); - -struct ImportSourceRoot(PathBuf); - -impl ImportSourceRoot { - fn new() -> Self { - let path = std::env::temp_dir().join(format!( - "crank-registry-import-auth-{}-{}", - std::process::id(), - NEXT_ROOT.fetch_add(1, Ordering::Relaxed) - )); - fs::create_dir(&path).unwrap(); - fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap(); - Self(path) - } -} - -impl Drop for ImportSourceRoot { - fn drop(&mut self) { - let _ = fs::set_permissions(&self.0, fs::Permissions::from_mode(0o700)); - let _ = fs::remove_dir_all(&self.0); - } -} - -fn timestamp() -> OffsetDateTime { - OffsetDateTime::now_utc() -} - -async fn create_workspace(registry: &PostgresRegistry, id: &str) -> WorkspaceId { - let now = timestamp(); - let workspace_id = WorkspaceId::new(id); - 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: now, - updated_at: now, - }, - }) - .await - .unwrap(); - workspace_id -} - -async fn create_secret(registry: &PostgresRegistry, id: &str, workspace_id: &WorkspaceId) { - let now = timestamp(); - registry - .verify_or_register_master_key_identity(MasterKeyIdentityCandidate { - epoch: 1, - fingerprint: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", - cipher_contract: MASTER_KEY_CIPHER_CONTRACT, - observed_at: &now, - }) - .await - .unwrap(); - let secret = Secret { - id: id.into(), - workspace_id: workspace_id.clone(), - name: format!("secret-{id}"), - kind: SecretKind::Token, - status: SecretStatus::Active, - current_version: 1, - created_at: now, - updated_at: now, - last_used_at: None, - }; - registry - .create_secret(CreateSecretRequest { - secret: &secret, - ciphertext: "opaque-test-ciphertext", - key_version: "test-key-v1", - master_key_epoch: 1, - created_by: None, - }) - .await - .unwrap(); -} - -fn profile( - id: &str, - workspace_id: &WorkspaceId, - kind: AuthKind, - config: AuthConfig, -) -> AuthProfile { - let now = timestamp(); - AuthProfile { - id: id.into(), - workspace_id: workspace_id.clone(), - name: format!("profile-{id}"), - kind, - config, - created_at: now, - updated_at: now, - } -} - -async fn save_profile(registry: &PostgresRegistry, profile: &AuthProfile) { - registry - .save_auth_profile(SaveAuthProfileRequest { - workspace_id: &profile.workspace_id, - profile, - }) - .await - .unwrap(); -} - -async fn create_import_job( - registry: &PostgresRegistry, - root: &ImportSourceRoot, - job_number: usize, -) -> (ImportJobId, ArtifactSourceId, ArtifactRef) { - let store = ArtifactStore::open(&root.0).unwrap(); - let artifact = store.put_registered(b"openapi: 3.1.0\n").unwrap(); - let source_id = ArtifactSourceId::new(format!("src_import_auth_{job_number}")); - let now = timestamp(); - let workspace_id = test_workspace_id(); - registry - .create_artifact_source(CreateArtifactSourceRequest { - workspace_id: &workspace_id, - source_id: &source_id, - artifact: &artifact, - mime_type: "application/yaml", - sensitivity: ArtifactSourceSensitivity::Internal, - created_at: now, - }) - .await - .unwrap(); - - let source = ImportJobSourceEnvelope { - source_id: source_id.clone(), - digest: artifact.artifact_ref().clone(), - }; - let payload = json!({ - "source": { - "source_id": source.source_id.as_str(), - "digest": source.digest.as_str(), - } - }); - let id = ImportJobId::new(format!("job_import_auth_{job_number}")); - registry - .create_import_job(CreateImportJobRequest { - id: &id, - workspace_id: &workspace_id, - kind: ImportJobKind::OpenApi, - source_format: "openapi", - source_version: Some("v5"), - status: ImportJobStatus::Pending, - source: &source, - preview_payload: &payload, - created_at: &now, - expires_at: &(now + time::Duration::hours(1)), - }) - .await - .unwrap(); - (id, source_id, artifact.artifact_ref().clone()) -} - -fn operation(id: &str, profile_id: Option<&str>) -> RegistryOperation { - let mut operation = test_operation(id, 1, crank_core::OperationStatus::Draft); - operation.execution_config = ExecutionConfig { - auth_profile_ref: profile_id.map(Into::into), - ..operation.execution_config - }; - operation -} - -async fn apply( - registry: &PostgresRegistry, - job_id: &ImportJobId, - drafts: &[ImportOperationDraft], -) -> Result { - let workspace_id = test_workspace_id(); - let now = timestamp(); - registry - .apply_import_job(ApplyImportJobRequest { - id: job_id, - workspace_id: &workspace_id, - application_key: "test-app-key", - conflict_mode: ImportConflictMode::Rename, - operations: drafts, - pre_skipped: &[], - finished_at: &now, - }) - .await -} - -async fn operation_count(database: &TestDatabase, ids: &[&str]) -> i64 { - let pool = database.raw_pool().await; - let count = sqlx::query_scalar::<_, i64>("select count(*) from operations where id = any($1)") - .bind(ids) - .fetch_one(&pool) - .await - .unwrap(); - pool.close().await; - count -} - -#[tokio::test] -async fn apply_accepts_current_compatible_profile_and_shared_secret_then_rejects_disabled_secret() { - let database = TestDatabase::new().await; - let registry = database.registry().await; - let workspace_id = test_workspace_id(); - let secret_id = "secret_import_shared"; - create_secret(®istry, secret_id, &workspace_id).await; - for profile_id in ["auth_import_a", "auth_import_b"] { - save_profile( - ®istry, - &profile( - profile_id, - &workspace_id, - AuthKind::ApiKeyHeader, - AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { - header_name: "x-api-key".to_owned(), - secret_id: secret_id.into(), - }), - ), - ) - .await; - } - - let root = ImportSourceRoot::new(); - let (job_id, _, _) = create_import_job(®istry, &root, 1).await; - let drafts = [ - ImportOperationDraft { - operation_key: "first".to_owned(), - operation: operation("op_import_auth_first", Some("auth_import_a")), - auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader { - parameter_name: "X-Api-Key".to_owned(), - }), - }, - ImportOperationDraft { - operation_key: "second".to_owned(), - operation: operation("op_import_auth_second", Some("auth_import_b")), - auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader { - parameter_name: "X-Api-Key".to_owned(), - }), - }, - ]; - let result = apply(®istry, &job_id, &drafts).await.unwrap(); - assert_eq!(result.created.len(), 2); - assert_eq!( - operation_count( - &database, - &["op_import_auth_first", "op_import_auth_second"] - ) - .await, - 2 - ); - - let pool = database.raw_pool().await; - sqlx::query("update secrets set status = 'disabled' where id = $1") - .bind(secret_id) - .execute(&pool) - .await - .unwrap(); - pool.close().await; - - let (job_id, _, _) = create_import_job(®istry, &root, 2).await; - let disabled_error = apply(®istry, &job_id, &drafts).await.unwrap_err(); - assert_eq!( - disabled_error.to_string(), - "import auth profile binding is unavailable" - ); - assert_eq!( - operation_count( - &database, - &["op_import_auth_first", "op_import_auth_second"] - ) - .await, - 2 - ); - database.cleanup().await; -} - -#[tokio::test] -async fn missing_and_foreign_profiles_have_the_same_safe_error_and_insert_no_operation() { - let database = TestDatabase::new().await; - let registry = database.registry().await; - let foreign_workspace = create_workspace(®istry, "ws_import_auth_foreign").await; - create_secret(®istry, "secret_import_foreign", &foreign_workspace).await; - save_profile( - ®istry, - &profile( - "auth_import_foreign", - &foreign_workspace, - AuthKind::Bearer, - AuthConfig::Bearer(BearerAuthConfig { - header_name: "Authorization".to_owned(), - secret_id: "secret_import_foreign".into(), - }), - ), - ) - .await; - - let root = ImportSourceRoot::new(); - let (missing_job, _, _) = create_import_job(®istry, &root, 3).await; - let missing = [ImportOperationDraft { - operation_key: "missing".to_owned(), - operation: operation("op_import_auth_missing", Some("auth_import_absent")), - auth_requirement: Some(ImportAuthRequirement::Bearer), - }]; - let missing_error = apply(®istry, &missing_job, &missing).await.unwrap_err(); - - let (foreign_job, _, _) = create_import_job(®istry, &root, 4).await; - let foreign = [ImportOperationDraft { - operation_key: "foreign".to_owned(), - operation: operation("op_import_auth_foreign_ref", Some("auth_import_foreign")), - auth_requirement: Some(ImportAuthRequirement::Bearer), - }]; - let foreign_error = apply(®istry, &foreign_job, &foreign).await.unwrap_err(); - assert_eq!( - missing_error.to_string(), - "import auth profile binding is unavailable" - ); - assert_eq!(missing_error.to_string(), foreign_error.to_string()); - assert_eq!( - operation_count( - &database, - &["op_import_auth_missing", "op_import_auth_foreign_ref"] - ) - .await, - 0 - ); - database.cleanup().await; -} - -#[tokio::test] -async fn apply_rejects_kind_parameter_and_missing_current_version_before_mutation() { - let database = TestDatabase::new().await; - let registry = database.registry().await; - let workspace_id = test_workspace_id(); - create_secret(®istry, "secret_import_mismatch", &workspace_id).await; - save_profile( - ®istry, - &profile( - "auth_import_mismatch", - &workspace_id, - AuthKind::ApiKeyHeader, - AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { - header_name: "X-Api-Key".to_owned(), - secret_id: "secret_import_mismatch".into(), - }), - ), - ) - .await; - let root = ImportSourceRoot::new(); - let bad_requirements = [ - ImportAuthRequirement::Bearer, - ImportAuthRequirement::ApiKeyHeader { - parameter_name: "X-Different".to_owned(), - }, - ]; - for (index, requirement) in bad_requirements.into_iter().enumerate() { - let (job_id, _, _) = create_import_job(®istry, &root, 5 + index).await; - let operation_id = format!("op_import_auth_mismatch_{index}"); - let draft = [ImportOperationDraft { - operation_key: operation_id.clone(), - operation: operation(&operation_id, Some("auth_import_mismatch")), - auth_requirement: Some(requirement), - }]; - let error = apply(®istry, &job_id, &draft).await.unwrap_err(); - assert_eq!( - error.to_string(), - "import auth profile binding is unavailable" - ); - assert_eq!(operation_count(&database, &[&operation_id]).await, 0); - } - - let pool = database.raw_pool().await; - sqlx::query("update secrets set current_version = 2 where id = $1") - .bind("secret_import_mismatch") - .execute(&pool) - .await - .unwrap(); - pool.close().await; - let (job_id, _, _) = create_import_job(®istry, &root, 7).await; - let draft = [ImportOperationDraft { - operation_key: "missing-current".to_owned(), - operation: operation( - "op_import_auth_missing_current", - Some("auth_import_mismatch"), - ), - auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader { - parameter_name: "X-Api-Key".to_owned(), - }), - }]; - let error = apply(®istry, &job_id, &draft).await.unwrap_err(); - assert_eq!( - error.to_string(), - "import auth profile binding is unavailable" - ); - assert_eq!( - operation_count(&database, &["op_import_auth_missing_current"]).await, - 0 - ); - database.cleanup().await; -} - -#[tokio::test] -async fn no_auth_requires_no_profile_reference() { - let database = TestDatabase::new().await; - let registry = database.registry().await; - let root = ImportSourceRoot::new(); - let (job_id, _, _) = create_import_job(®istry, &root, 8).await; - let draft = [ImportOperationDraft { - operation_key: "inconsistent".to_owned(), - operation: operation("op_import_auth_no_requirement", Some("auth_nonexistent")), - auth_requirement: None, - }]; - let error = apply(®istry, &job_id, &draft).await.unwrap_err(); - assert_eq!( - error.to_string(), - "import auth profile binding is unavailable" - ); - assert_eq!( - operation_count(&database, &["op_import_auth_no_requirement"]).await, - 0 - ); - database.cleanup().await; -} - -#[tokio::test] -async fn apply_waiting_on_secret_disable_rejects_without_partial_draft_mutation() { - let database = TestDatabase::new().await; - let registry = database.registry().await; - let workspace_id = test_workspace_id(); - create_secret(®istry, "secret_import_race", &workspace_id).await; - save_profile( - ®istry, - &profile( - "auth_import_race", - &workspace_id, - AuthKind::ApiKeyHeader, - AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { - header_name: "X-Api-Key".to_owned(), - secret_id: "secret_import_race".into(), - }), - ), - ) - .await; - let root = ImportSourceRoot::new(); - let (job_id, _, _) = create_import_job(®istry, &root, 9).await; - let drafts = vec![ - ImportOperationDraft { - operation_key: "race-first".to_owned(), - operation: operation("op_import_auth_race_first", Some("auth_import_race")), - auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader { - parameter_name: "X-Api-Key".to_owned(), - }), - }, - ImportOperationDraft { - operation_key: "race-second".to_owned(), - operation: operation("op_import_auth_race_second", Some("auth_import_race")), - auth_requirement: Some(ImportAuthRequirement::ApiKeyHeader { - parameter_name: "X-Api-Key".to_owned(), - }), - }, - ]; - - let pool = database.raw_pool().await; - let mut disable_tx = pool.begin().await.unwrap(); - sqlx::query("select id from secrets where workspace_id = $1 and id = $2 for update") - .bind(workspace_id.as_str()) - .bind("secret_import_race") - .fetch_one(&mut *disable_tx) - .await - .unwrap(); - - let concurrent_registry = registry.clone(); - let concurrent_job_id = job_id.clone(); - let (started_tx, started_rx) = tokio::sync::oneshot::channel(); - let apply_task = tokio::spawn(async move { - let _ = started_tx.send(()); - let workspace_id = test_workspace_id(); - let finished_at = timestamp(); - concurrent_registry - .apply_import_job(ApplyImportJobRequest { - id: &concurrent_job_id, - workspace_id: &workspace_id, - application_key: "race-app-key", - conflict_mode: ImportConflictMode::Rename, - operations: &drafts, - pre_skipped: &[], - finished_at: &finished_at, - }) - .await - }); - started_rx.await.unwrap(); - - // Use a separate pool because the transaction holding the lock occupies - // the raw pool's only connection. - let observer_pool = database.raw_pool().await; - let mut waiting_for_secret_lock = false; - for _ in 0..100 { - waiting_for_secret_lock = sqlx::query_scalar::<_, bool>( - "select exists ( - select 1 from pg_stat_activity - where wait_event_type = 'Lock' - and query like 'select s.status,%' - and query like '%for update of s%' - )", - ) - .fetch_one(&observer_pool) - .await - .unwrap(); - if waiting_for_secret_lock { - break; - } - sleep(TokioDuration::from_millis(10)).await; - } - assert!( - waiting_for_secret_lock, - "Apply should be waiting on the locked Secret row" - ); - observer_pool.close().await; - - sqlx::query("update secrets set status = 'disabled' where workspace_id = $1 and id = $2") - .bind(workspace_id.as_str()) - .bind("secret_import_race") - .execute(&mut *disable_tx) - .await - .unwrap(); - disable_tx.commit().await.unwrap(); - - let result = apply_task.await.unwrap(); - assert_eq!( - result.unwrap_err().to_string(), - "import auth profile binding is unavailable" - ); - assert_eq!( - operation_count( - &database, - &["op_import_auth_race_first", "op_import_auth_race_second"] - ) - .await, - 0 - ); - pool.close().await; - database.cleanup().await; -} 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..07cf0ed --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan.rs @@ -0,0 +1,80 @@ +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 cleanup; +mod create; +mod preview_v2; +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..6401865 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/auth.rs @@ -0,0 +1,761 @@ +//! 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, ApiKeyQueryAuthConfig, AuthConfig, AuthKind, AuthProfile, + BasicAuthConfig, 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" }, + "auth_requirement": { "kind": "api_key_header", "parameter_name": "X-Apply-Key" }, + "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] + }) +} + +fn typed_auth_plan_payload( + plan_id: &str, + job_id: &str, + fingerprint: &Value, + auth_profile_id: &str, + requirement_kind: &str, + parameter_name: Option<&str>, + snapshot: Value, +) -> Value { + let mut operation = plan_operation("op_typed_auth"); + operation.execution_config.auth_profile_ref = Some(auth_profile_id.into()); + let mut requirement = json!({ "kind": requirement_kind }); + if let Some(parameter_name) = parameter_name { + requirement["parameter_name"] = json!(parameter_name); + } + 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": ["typed"], "selected": ["typed"], "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": "typed", + "name": operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "auth_requirement": requirement, + "operation": operation, + "auth_snapshot": snapshot + }] + }) +} + +async fn typed_auth_base( + database: &TestDatabase, + workspace_tag: &str, + profile_id: &str, + config: AuthConfig, +) -> ( + PostgresRegistry, + WorkspaceId, + ImportJobId, + Value, + AuthProfile, + Value, +) { + let registry = database.registry().await; + registry + .verify_or_register_master_key_identity(MasterKeyIdentityCandidate { + epoch: 1, + fingerprint: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", + cipher_contract: MASTER_KEY_CIPHER_CONTRACT, + observed_at: &ts(), + }) + .await + .unwrap(); + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, workspace_tag).await; + let job = create_pending_openapi_job( + ®istry, + &ws, + &format!("job_typed_auth_{workspace_tag}"), + now, + now + Duration::hours(1), + ) + .await; + activate_source(&database.raw_pool().await, &ws, now).await; + + let mut secrets = Vec::new(); + for (index, secret_id) in config.secret_ids().into_iter().enumerate() { + let secret = Secret { + id: secret_id.clone(), + workspace_id: ws.clone(), + name: format!("Typed auth secret {index}"), + 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(); + secrets.push(secret); + } + let profile = AuthProfile { + id: profile_id.into(), + workspace_id: ws.clone(), + name: "Typed auth profile".to_owned(), + kind: config.kind(), + config, + created_at: ts(), + updated_at: ts(), + }; + registry + .save_auth_profile(SaveAuthProfileRequest { + workspace_id: &ws, + profile: &profile, + }) + .await + .unwrap(); + let snapshot = 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": secrets.iter().map(|secret| json!({ + "id": secret.id.as_str(), + "status": secret.status, + "current_version": secret.current_version, + "updated_at": secret.updated_at.format(&Rfc3339).unwrap() + })).collect::>() + }); + (registry, ws, job, fingerprint(), profile, snapshot) +} + +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; +} + +#[tokio::test] +async fn apply_rechecks_live_profile_against_source_requirement_even_when_snapshot_matches() { + let database = TestDatabase::new().await; + let (registry, raw_pool, ws, job, fp, _snapshot_a) = + auth_base(&database, "a_source_requirement").await; + + // Reproduce the race: source compatibility was resolved while the + // profile used X-Apply-Key, then the profile changed before the later + // snapshot capture, so the stored snapshot itself accurately says X-B. + sqlx::query( + "update auth_profiles + set config_json = jsonb_set(config_json, '{api_key_header,header_name}', '\"X-B\"'), + updated_at = now() + where workspace_id = $1 and id = $2", + ) + .bind(ws.as_str()) + .bind("auth_apply") + .execute(&raw_pool) + .await + .unwrap(); + let profile = capture_profile_fixture(®istry, &ws).await; + let secret = registry + .get_secret(&ws, &SecretId::new("sec_apply")) + .await + .unwrap() + .unwrap() + .secret; + let snapshot_b = capture_snapshot(&profile, &secret); + assert_eq!(snapshot_b["config"]["api_key_header"]["header_name"], "X-B"); + + // The source-derived requirement remains X-Apply-Key even though the + // plan-time snapshot now matches the live X-B profile exactly. + let payload = auth_plan_payload("plan_auth_source_req", job.as_str(), &fp, Some(snapshot_b)); + let (plan, digest) = + store_plan(®istry, &ws, &job, "plan_auth_source_req", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let result = registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_auth_source_req", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await; + assert!(matches!( + result, + Err(RegistryError::ImportPlanAuthUnavailable { auth_profile_id }) + if auth_profile_id == "auth_apply" + )); + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_apply_auth")) + .await + .unwrap() + .is_none() + ); + 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; +} + +#[tokio::test] +async fn apply_accepts_basic_auth_with_matching_snapshot_and_source_requirement() { + let database = TestDatabase::new().await; + let profile_id = "auth_basic_apply"; + let (registry, ws, job, fp, _profile, snapshot) = typed_auth_base( + &database, + "a_basic_ok", + profile_id, + AuthConfig::Basic(BasicAuthConfig { + username_secret_id: SecretId::new("sec_basic_user"), + password_secret_id: SecretId::new("sec_basic_password"), + }), + ) + .await; + let payload = typed_auth_plan_payload( + "plan_basic_ok", + job.as_str(), + &fp, + profile_id, + "basic", + None, + snapshot, + ); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_basic_ok", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let result = registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_basic_ok", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await + .unwrap(); + assert_eq!(result.report.status, ImportPlanApplyStatus::Applied); + assert_eq!(result.report.counts.created, 1); + let summary = registry + .get_operation_summary(&ws, &OperationId::new("op_typed_auth")) + .await + .unwrap() + .unwrap(); + assert_eq!(summary.current_draft_version, 1); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_accepts_query_key_with_matching_snapshot_and_source_requirement() { + let database = TestDatabase::new().await; + let profile_id = "auth_query_apply"; + let (registry, ws, job, fp, _profile, snapshot) = typed_auth_base( + &database, + "a_query_ok", + profile_id, + AuthConfig::ApiKeyQuery(ApiKeyQueryAuthConfig { + param_name: "access_token".to_owned(), + secret_id: SecretId::new("sec_query_key"), + }), + ) + .await; + let payload = typed_auth_plan_payload( + "plan_query_ok", + job.as_str(), + &fp, + profile_id, + "api_key_query", + Some("access_token"), + snapshot, + ); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_query_ok", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let result = registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_query_ok", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await + .unwrap(); + assert_eq!(result.report.status, ImportPlanApplyStatus::Applied); + assert_eq!(result.report.counts.created, 1); + let summary = registry + .get_operation_summary(&ws, &OperationId::new("op_typed_auth")) + .await + .unwrap() + .unwrap(); + assert_eq!(summary.current_draft_version, 1); + database.cleanup().await; +} + +#[tokio::test] +async fn apply_rejects_query_key_source_mismatch_with_matching_live_snapshot() { + let database = TestDatabase::new().await; + let profile_id = "auth_query_mismatch"; + let (registry, ws, job, fp, _profile, snapshot) = typed_auth_base( + &database, + "a_query_mismatch", + profile_id, + AuthConfig::ApiKeyQuery(ApiKeyQueryAuthConfig { + param_name: "access_token".to_owned(), + secret_id: SecretId::new("sec_query_mismatch"), + }), + ) + .await; + let payload = typed_auth_plan_payload( + "plan_query_mismatch", + job.as_str(), + &fp, + profile_id, + "api_key_query", + Some("other_token"), + snapshot, + ); + let (plan, digest) = + store_plan(®istry, &ws, &job, "plan_query_mismatch", &fp, payload).await; + let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap(); + let result = registry + .apply_import_plan(&ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_query_mismatch", + verified_fingerprint: &fp, + applied_at: &applied_at, + }) + .await; + assert!(matches!( + result, + Err(RegistryError::ImportPlanAuthUnavailable { auth_profile_id }) + if auth_profile_id == profile_id + )); + assert!( + registry + .get_operation_summary(&ws, &OperationId::new("op_typed_auth")) + .await + .unwrap() + .is_none(), + "query source mismatch must fail before creating an operation" + ); + 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..5d17363 --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/apply/partial.rs @@ -0,0 +1,103 @@ +//! 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"] + }, + "excluded_reasons": [{ + "key": "not_picked", + "name": "Not Picked Display Label", + "reason": "not_selected" + }], + "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.name, "Not Picked Display Label"); + 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/cleanup.rs b/crates/crank-registry/tests/integration/import_plan/cleanup.rs new file mode 100644 index 0000000..3b92d3c --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/cleanup.rs @@ -0,0 +1,289 @@ +use super::super::common::TestDatabase; +use super::{create_pending_openapi_job, create_workspace}; +use crank_artifacts::ArtifactRef; +use crank_core::WorkspaceId; +use crank_registry::{ + ArtifactSourceId, CreateImportJobRequest, ImportJobId, ImportJobKind, ImportJobSourceEnvelope, + ImportJobStatus, +}; +use serde_json::json; +use time::{Duration, OffsetDateTime}; + +async fn insert_active_source( + raw: &sqlx::PgPool, + workspace_id: &WorkspaceId, + source_id: &str, + blob_digest: &str, + created_at: OffsetDateTime, +) { + sqlx::query( + "insert into artifact_blobs (digest, artifact_ref, size_bytes, created_at, updated_at) + values ($1, 'sha256:' || $1, 16, $2, $2) + on conflict (digest) do nothing", + ) + .bind(blob_digest) + .bind(created_at) + .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(blob_digest) + .bind(created_at) + .execute(raw) + .await + .unwrap(); +} + +async fn source_lifecycle( + raw: &sqlx::PgPool, + workspace_id: &WorkspaceId, + source_id: &str, +) -> String { + sqlx::query_scalar( + "select lifecycle from artifact_sources where workspace_id = $1 and source_id = $2", + ) + .bind(workspace_id.as_str()) + .bind(source_id) + .fetch_one(raw) + .await + .unwrap() +} + +#[tokio::test] +async fn cleanup_ignores_non_array_dependencies_and_preserves_referenced_source() { + 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_object_deps").await; + + // A non-expired job whose live primary source is old enough to be a + // dangling candidate, plus an unreferenced peer so the dangling sweep must + // evaluate the malformed `dependencies` branch for a non-matching source. + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_object_deps", + now - Duration::hours(2), + now + Duration::hours(1), + ) + .await; + let old = now - Duration::minutes(10); + insert_active_source(&raw, &ws, "src_openapi_test", &"ab".repeat(32), old).await; + insert_active_source(&raw, &ws, "src_openapi_orphan", &"ac".repeat(32), old).await; + + // Corrupt the active job's payload so `dependencies` is an object, not an + // array; the sweep must not abort on it. + sqlx::query( + "update import_jobs + set preview_payload = jsonb_set(preview_payload, '{dependencies}', '{}'::jsonb) + where id = $1 and workspace_id = $2", + ) + .bind(job.as_str()) + .bind(ws.as_str()) + .execute(&raw) + .await + .unwrap(); + + let cleanup = registry + .cleanup_expired_import_jobs(8) + .await + .expect("dangling cleanup must not abort on a non-array dependencies payload"); + assert_eq!( + cleanup.deleted_jobs, 0, + "the active job must survive cleanup" + ); + assert_eq!( + cleanup.detached_sources, 1, + "the dangling sweep must still run and detach exactly the unreferenced peer" + ); + assert_eq!( + source_lifecycle(&raw, &ws, "src_openapi_test").await, + "active", + "the live primary source referenced by the job must be preserved" + ); + assert_eq!( + source_lifecycle(&raw, &ws, "src_openapi_orphan").await, + "detached", + "the unreferenced peer must be swept once the malformed branch is guarded" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn cleanup_ignores_non_openapi_sources_named_by_expired_jobs() { + 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_foreign_source").await; + + let old = now - Duration::hours(2); + let blob_digest = "cd".repeat(32); + insert_active_source(&raw, &ws, "src_confidential_leak", &blob_digest, old).await; + + // Expired OpenAPI job naming a same-workspace source outside the OpenAPI + // namespace. + let job = ImportJobId::new("job_foreign_source"); + let source_id = ArtifactSourceId::new("src_confidential_leak"); + let digest = ArtifactRef::from_digest_hex(&blob_digest).unwrap(); + let preview_payload = json!({ + "source": { + "source_id": source_id.as_str(), + "digest": digest.as_str(), + }, + "preview": { "ops": [] } + }); + registry + .create_import_job(CreateImportJobRequest { + id: &job, + workspace_id: &ws, + kind: ImportJobKind::OpenApi, + source_format: "openapi", + source_version: Some("3.1.0"), + status: ImportJobStatus::Pending, + source: &ImportJobSourceEnvelope { + source_id: source_id.clone(), + digest: digest.clone(), + }, + preview_payload: &preview_payload, + created_at: &old, + expires_at: &(now - Duration::minutes(10)), + }) + .await + .unwrap(); + + let cleanup = registry + .cleanup_expired_import_jobs(8) + .await + .expect("expired OpenAPI cleanup must succeed"); + assert_eq!(cleanup.deleted_jobs, 1, "the expired job must be deleted"); + assert_eq!( + source_lifecycle(&raw, &ws, "src_confidential_leak").await, + "active", + "an OpenAPI job must not detach a source outside its namespace" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn cleanup_keeps_source_referenced_by_nonexpired_peer_job() { + 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_shared_source").await; + let old = now - Duration::hours(2); + let shared_source = "src_openapi_shared"; + + let expired = create_pending_openapi_job( + ®istry, + &ws, + "job_expired_shared_source", + old, + now - Duration::minutes(10), + ) + .await; + let peer = create_pending_openapi_job( + ®istry, + &ws, + "job_live_shared_source", + old, + now + Duration::hours(1), + ) + .await; + insert_active_source(&raw, &ws, shared_source, &"ab".repeat(32), old).await; + for job_id in [expired.as_str(), peer.as_str()] { + sqlx::query( + "update import_jobs + set preview_payload = jsonb_set( + preview_payload, '{source,source_id}', to_jsonb($2::text) + ) + where id = $1 and workspace_id = $3", + ) + .bind(job_id) + .bind(shared_source) + .bind(ws.as_str()) + .execute(&raw) + .await + .unwrap(); + } + + let cleanup = registry + .cleanup_expired_import_jobs(8) + .await + .expect("expired job cleanup must preserve a source referenced by a live peer"); + assert_eq!(cleanup.deleted_jobs, 1); + assert_eq!( + source_lifecycle(&raw, &ws, shared_source).await, + "active", + "the source remains owned by the non-expired peer job" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn failed_job_finalization_keeps_source_referenced_by_peer_job() { + 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_failed_shared_source").await; + let source_id = "src_openapi_shared"; + let failing = create_pending_openapi_job( + ®istry, + &ws, + "job_failing_shared_source", + now - Duration::hours(1), + now + Duration::hours(1), + ) + .await; + let peer = create_pending_openapi_job( + ®istry, + &ws, + "job_peer_shared_source", + now - Duration::hours(1), + now + Duration::hours(1), + ) + .await; + insert_active_source(&raw, &ws, source_id, &"ab".repeat(32), now).await; + for job_id in [failing.as_str(), peer.as_str()] { + sqlx::query( + "update import_jobs + set preview_payload = jsonb_set( + preview_payload, '{source,source_id}', to_jsonb($2::text) + ) + where id = $1 and workspace_id = $3", + ) + .bind(job_id) + .bind(source_id) + .bind(ws.as_str()) + .execute(&raw) + .await + .unwrap(); + } + + registry + .finish_import_job(crank_registry::FinishImportJobRequest { + id: &failing, + status: ImportJobStatus::Failed, + created_operation_ids: &json!([]), + error_text: Some("test_failure"), + finished_at: &now, + }) + .await + .unwrap(); + assert_eq!( + source_lifecycle(&raw, &ws, source_id).await, + "active", + "failed finalization must not detach a source still referenced by a peer job" + ); + 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/import_plan/preview_v2.rs b/crates/crank-registry/tests/integration/import_plan/preview_v2.rs new file mode 100644 index 0000000..96b355c --- /dev/null +++ b/crates/crank-registry/tests/integration/import_plan/preview_v2.rs @@ -0,0 +1,787 @@ +//! Story 2.12 evidence for v2 semantic digests, catalog drift and Apply. + +use super::super::common::{TestDatabase, test_operation}; +use super::{create_pending_openapi_job, create_workspace, parse_ts}; +use crank_artifacts::ArtifactRef; +use crank_core::{OperationStatus, WorkspaceId}; +use crank_registry::{ + ApplyImportPlanRequest, ArtifactSourceId, CreateImportJobRequest, CreateImportPlanRequest, + ImportJobId, ImportJobKind, ImportJobSourceEnvelope, ImportJobStatus, ImportPlanApplyStatus, + ImportPlanCandidateOutcome, ImportPlanId, RegistryError, +}; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use time::{Duration, OffsetDateTime}; + +const SOURCE_ID: &str = "src_openapi_test"; +const SOURCE_DIGEST: &str = + "sha256:abababababababababababababababababababababababababababababababab"; + +fn fingerprint() -> Value { + json!({"importer": "registry-preview-v2-test", "preview_digest": "fp-v2-1"}) +} + +fn operation( + id: &str, + name: &str, + created_at: &str, + updated_at: &str, +) -> crank_registry::RegistryOperation { + let mut operation = test_operation(id, 1, OperationStatus::Draft); + operation.name = name.to_owned(); + operation.display_name = "Candidate display name".to_owned(); + operation.execution_config.auth_profile_ref = None; + operation.created_at = parse_ts(created_at); + operation.updated_at = parse_ts(updated_at); + operation +} + +fn v2_payload( + plan_id: &str, + job_id: &str, + source_id: &str, + operation: crank_registry::RegistryOperation, + catalog_revision: &str, +) -> Value { + let key = "candidate"; + json!({ + "schema_version": "import-plan-v2", + "plan_reference": plan_id, + "job_id": job_id, + "fingerprint": fingerprint(), + "catalog_revision": catalog_revision, + "support_matrix_version": "1.0.2", + "source": { "source_id": source_id, "digest": SOURCE_DIGEST }, + "dependencies": [], + "excluded_reasons": [], + "findings": { "document": [], "candidates": [{ "key": key, "findings": [] }] }, + "selection": { + "mode": "full", "all": [key], "selected": [key], "excluded": [] + }, + "counts": { "selected": 1, "excluded": 0, "actionable": 1, "blocked": 0 }, + "candidates": [{ + "key": key, + "name": operation.name, + "state": "actionable", + "decision": { "action": "create" }, + "findings": [], + "operation": operation + }] + }) +} + +async fn store_plan( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, + job_id: &ImportJobId, + plan_id: &str, + payload: &Value, + created_at: OffsetDateTime, +) -> (ImportPlanId, String) { + let plan_id = ImportPlanId::new(plan_id); + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan_id, + workspace_id, + job_id, + payload, + created_at: &created_at, + expires_at: &(created_at + Duration::minutes(30)), + }) + .await + .unwrap(); + (plan_id, created.digest) +} + +async fn activate_source(raw: &sqlx::PgPool, workspace_id: &WorkspaceId, now: OffsetDateTime) { + let hex = "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(&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(); +} + +async fn create_job_with_source( + registry: &crank_registry::PostgresRegistry, + workspace_id: &WorkspaceId, + job_id: &str, + source_id: &str, + now: OffsetDateTime, +) -> ImportJobId { + let job_id = ImportJobId::new(job_id); + let source_id = ArtifactSourceId::new(source_id); + 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": {"source": "integration-test"} + }); + registry + .create_import_job(CreateImportJobRequest { + id: &job_id, + workspace_id, + kind: ImportJobKind::OpenApi, + source_format: "openapi", + source_version: Some("3.1.0"), + status: ImportJobStatus::Pending, + source: &ImportJobSourceEnvelope { source_id, digest }, + preview_payload: &preview_payload, + created_at: &now, + expires_at: &(now + Duration::hours(1)), + }) + .await + .unwrap(); + job_id +} + +fn raw_digest(payload: &Value) -> String { + format!("{:x}", Sha256::digest(serde_json::to_vec(payload).unwrap())) +} + +#[tokio::test] +async fn v2_digest_ignores_job_plan_generated_operation_ids_and_runtime_timestamps() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let ws = create_workspace(®istry, "ws_v2_digest").await; + let first_time = + OffsetDateTime::from_unix_timestamp(OffsetDateTime::now_utc().unix_timestamp()).unwrap(); + let second_time = first_time + Duration::minutes(2); + let first_job = + create_job_with_source(®istry, &ws, "job_v2_digest_a", "src_v2_a", first_time).await; + let second_job = + create_job_with_source(®istry, &ws, "job_v2_digest_b", "src_v2_b", second_time).await; + let revision = registry.import_catalog_revision(&ws).await.unwrap(); + let first_payload = v2_payload( + "plan_v2_digest_a", + first_job.as_str(), + "src_v2_a", + operation( + "op_generated_a", + "candidate_tool", + "2026-03-25T10:00:00Z", + "2026-03-25T10:00:00Z", + ), + &revision, + ); + let second_payload = v2_payload( + "plan_v2_digest_b", + second_job.as_str(), + "src_v2_b", + operation( + "op_generated_b", + "candidate_tool", + "2026-03-25T10:02:00Z", + "2026-03-25T10:02:00Z", + ), + &revision, + ); + + let (_, first_digest) = store_plan( + ®istry, + &ws, + &first_job, + "plan_v2_digest_a", + &first_payload, + first_time, + ) + .await; + let (_, second_digest) = store_plan( + ®istry, + &ws, + &second_job, + "plan_v2_digest_b", + &second_payload, + second_time, + ) + .await; + assert_eq!( + first_digest, second_digest, + "server identities and runtime instants must not change semantic identity" + ); + assert_eq!( + registry + .get_import_plan(&ws, &ImportPlanId::new("plan_v2_digest_a")) + .await + .unwrap() + .unwrap() + .payload, + first_payload, + "canonical hashing must leave immutable v2 evidence untouched" + ); + let mut replay_with_new_generated_id = first_payload.clone(); + replay_with_new_generated_id["candidates"][0]["operation"]["id"] = json!("op_generated_retry"); + let plan_id = ImportPlanId::new("plan_v2_digest_a"); + let conflict = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan_id, + workspace_id: &ws, + job_id: &first_job, + payload: &replay_with_new_generated_id, + created_at: &first_time, + expires_at: &(first_time + Duration::minutes(30)), + }) + .await + .expect_err("replay identity still compares immutable raw payload after semantic hashing"); + assert!(matches!(conflict, RegistryError::ImportPlanConflict { .. })); + database.cleanup().await; +} + +#[tokio::test] +async fn v2_digest_changes_for_selection_name_and_conflict_decision() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_v2_semantics").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_v2_semantics", + now, + now + Duration::hours(1), + ) + .await; + let revision = registry.import_catalog_revision(&ws).await.unwrap(); + let base = v2_payload( + "plan_semantic_base", + job.as_str(), + SOURCE_ID, + operation( + "op_base", + "candidate_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ), + &revision, + ); + let mut changed_selection = base.clone(); + changed_selection["plan_reference"] = json!("plan_semantic_selection"); + changed_selection["selection"]["mode"] = json!("partial"); + changed_selection["selection"]["all"] = json!(["candidate", "other"]); + changed_selection["selection"]["excluded"] = json!(["other"]); + changed_selection["counts"]["excluded"] = json!(1); + changed_selection["excluded_reasons"] = json!([{"key": "other", "reason": "not_selected"}]); + changed_selection["findings"]["candidates"] = json!([ + {"key": "candidate", "findings": []}, {"key": "other", "findings": []} + ]); + let mut changed_name = base.clone(); + changed_name["plan_reference"] = json!("plan_semantic_name"); + changed_name["candidates"][0]["name"] = json!("renamed_tool"); + changed_name["candidates"][0]["operation"]["name"] = json!("renamed_tool"); + let mut changed_decision = base.clone(); + changed_decision["plan_reference"] = json!("plan_semantic_decision"); + changed_decision["candidates"][0]["decision"] = + json!({"action": "rename", "confirmed_name": "candidate_tool"}); + let mut changed_support_matrix = base.clone(); + changed_support_matrix["plan_reference"] = json!("plan_semantic_support"); + changed_support_matrix["support_matrix_version"] = json!("1.0.3"); + + let (_, digest) = store_plan(®istry, &ws, &job, "plan_semantic_base", &base, now).await; + let (_, selection_digest) = store_plan( + ®istry, + &ws, + &job, + "plan_semantic_selection", + &changed_selection, + now, + ) + .await; + let (_, name_digest) = store_plan( + ®istry, + &ws, + &job, + "plan_semantic_name", + &changed_name, + now, + ) + .await; + let (_, decision_digest) = store_plan( + ®istry, + &ws, + &job, + "plan_semantic_decision", + &changed_decision, + now, + ) + .await; + let (_, support_matrix_digest) = store_plan( + ®istry, + &ws, + &job, + "plan_semantic_support", + &changed_support_matrix, + now, + ) + .await; + assert_ne!(digest, selection_digest, "selection changes are semantic"); + assert_ne!( + digest, name_digest, + "the confirmed operation name is semantic" + ); + assert_ne!( + digest, decision_digest, + "conflict resolution choices are semantic" + ); + assert_ne!( + digest, support_matrix_digest, + "the support matrix version is semantic evidence" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn legacy_v1_digest_and_stored_payload_remain_unchanged_and_readable() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let now = OffsetDateTime::now_utc(); + let ws = create_workspace(®istry, "ws_v1_compat").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_v1_compat", + now, + now + Duration::hours(1), + ) + .await; + let plan_id = ImportPlanId::new("plan_v1_compat"); + let payload = json!({ + "schema_version": "import-plan-v1", "plan_reference": plan_id.as_str(), "job_id": job.as_str(), + "fingerprint": {"legacy": true}, "source": {"source_id": SOURCE_ID, "digest": SOURCE_DIGEST}, + "dependencies": [], "selection": {"mode": "full", "all": ["legacy"], "selected": ["legacy"], "excluded": []}, + "counts": {"selected": 1, "excluded": 0, "actionable": 1, "blocked": 0}, + "candidates": [{"key": "legacy", "name": "legacy_tool", "state": "actionable", "decision": {"action": "create"}}] + }); + let created = registry + .create_import_plan(CreateImportPlanRequest { + id: &plan_id, + workspace_id: &ws, + job_id: &job, + payload: &payload, + created_at: &now, + expires_at: &(now + Duration::minutes(30)), + }) + .await + .unwrap(); + assert_eq!( + created.digest, + raw_digest(&payload), + "legacy v1 keeps byte-for-byte digest rules" + ); + let read = registry + .get_import_plan(&ws, &plan_id) + .await + .unwrap() + .expect("stored v1 plan remains readable"); + assert_eq!(read.payload, payload); + assert_eq!(read.digest, created.digest); + database.cleanup().await; +} + +#[tokio::test] +async fn catalog_revision_ignores_timestamps_and_foreign_workspace_but_tracks_semantics() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let raw = database.raw_pool().await; + let ws = create_workspace(®istry, "ws_catalog_revision").await; + let foreign = create_workspace(®istry, "ws_catalog_foreign").await; + let mut local_op = operation( + "op_catalog_local", + "local_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ); + registry + .create_operation(&ws, &local_op, None) + .await + .unwrap(); + let baseline = registry.import_catalog_revision(&ws).await.unwrap(); + + sqlx::query("update operations set updated_at = $1 where workspace_id = $2 and id = $3") + .bind(parse_ts("2026-10-03T11:00:00Z")) + .bind(ws.as_str()) + .bind(local_op.id.as_str()) + .execute(&raw) + .await + .unwrap(); + assert_eq!( + registry.import_catalog_revision(&ws).await.unwrap(), + baseline, + "runtime timestamps are excluded" + ); + + let foreign_op = operation( + "op_catalog_foreign", + "foreign_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ); + registry + .create_operation(&foreign, &foreign_op, None) + .await + .unwrap(); + assert_eq!( + registry.import_catalog_revision(&ws).await.unwrap(), + baseline, + "foreign scope is excluded" + ); + + sqlx::query( + "update operations set name = 'renamed_local_tool' where workspace_id = $1 and id = $2", + ) + .bind(ws.as_str()) + .bind(local_op.id.as_str()) + .execute(&raw) + .await + .unwrap(); + let renamed = registry.import_catalog_revision(&ws).await.unwrap(); + assert_ne!(renamed, baseline, "catalog names affect the revision"); + sqlx::query( + "update operations set current_draft_version = 2 where workspace_id = $1 and id = $2", + ) + .bind(ws.as_str()) + .bind(local_op.id.as_str()) + .execute(&raw) + .await + .unwrap(); + let versioned = registry.import_catalog_revision(&ws).await.unwrap(); + assert_ne!( + versioned, renamed, + "catalog version pointers affect the revision" + ); + sqlx::query("update operations set status = 'published' where workspace_id = $1 and id = $2") + .bind(ws.as_str()) + .bind(local_op.id.as_str()) + .execute(&raw) + .await + .unwrap(); + let status_changed = registry.import_catalog_revision(&ws).await.unwrap(); + assert_ne!( + status_changed, versioned, + "catalog lifecycle affects the revision" + ); + // Avoid accidental use of stale local fixture state if this case grows. + local_op.name = "renamed_local_tool".to_owned(); + database.cleanup().await; +} + +#[tokio::test] +async fn revalidation_and_apply_reject_catalog_drift_without_plan_mutation() { + 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_v2_drift").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_v2_drift", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw, &ws, now).await; + let fp = fingerprint(); + let revision = registry.import_catalog_revision(&ws).await.unwrap(); + let plan_id_text = "plan_v2_drift"; + let payload = v2_payload( + plan_id_text, + job.as_str(), + SOURCE_ID, + operation( + "op_v2_planned", + "planned_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ), + &revision, + ); + let (plan, digest) = store_plan(®istry, &ws, &job, plan_id_text, &payload, now).await; + registry + .revalidate_import_plan(&ws, &plan, &fp) + .await + .unwrap(); + + let catalog_change = operation( + "op_v2_catalog_change", + "catalog_change_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ); + registry + .create_operation(&ws, &catalog_change, None) + .await + .unwrap(); + assert!( + matches!( + registry.revalidate_import_plan(&ws, &plan, &fp).await, + Err(RegistryError::ImportPlanFingerprintStale { .. }) + ), + "read-only revalidation must flag catalog drift" + ); + + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_v2_drift", + verified_fingerprint: &fp, + applied_at: &now, + }; + assert!( + matches!( + registry.apply_import_plan(&request).await, + Err(RegistryError::ImportPlanFingerprintStale { .. }) + ), + "fresh Apply must independently reject the stale catalog revision" + ); + assert!( + registry + .get_operation_summary(&ws, &crank_core::OperationId::new("op_v2_planned")) + .await + .unwrap() + .is_none(), + "catalog drift must fail before creating the planned operation" + ); + assert_eq!( + registry + .get_import_plan_application(&ws, "idem_v2_drift") + .await + .unwrap(), + None, + "a stale apply must not persist application evidence" + ); + database.cleanup().await; +} + +#[tokio::test] +async fn v2_apply_creates_operation_and_reports_the_confirmed_candidate() { + 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_v2_apply").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_v2_apply", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw, &ws, now).await; + let fp = fingerprint(); + let revision = registry.import_catalog_revision(&ws).await.unwrap(); + let payload = v2_payload( + "plan_v2_apply", + job.as_str(), + SOURCE_ID, + operation( + "op_v2_apply", + "v2_apply_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ), + &revision, + ); + let (plan, digest) = store_plan(®istry, &ws, &job, "plan_v2_apply", &payload, now).await; + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_v2_apply", + verified_fingerprint: &fp, + applied_at: &now, + }; + 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); + assert_eq!(result.report.outcomes[0].key, "candidate"); + assert_eq!( + result.report.outcomes[0].outcome, + ImportPlanCandidateOutcome::Created + ); + assert_eq!( + result.report.outcomes[0].operation_id.as_deref(), + Some("op_v2_apply") + ); + assert_eq!( + registry + .get_operation_summary(&ws, &crank_core::OperationId::new("op_v2_apply")) + .await + .unwrap() + .unwrap() + .current_draft_version, + 1 + ); + database.cleanup().await; +} + +#[tokio::test] +async fn partial_v2_apply_preserves_the_reason_for_an_excluded_candidate() { + 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_v2_excluded_reason").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_v2_excluded_reason", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw, &ws, now).await; + let fp = fingerprint(); + let revision = registry.import_catalog_revision(&ws).await.unwrap(); + let mut payload = v2_payload( + "plan_v2_excluded_reason", + job.as_str(), + SOURCE_ID, + operation( + "op_v2_excluded_reason", + "selected_tool", + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ), + &revision, + ); + payload["selection"] = json!({ + "mode": "partial", "all": ["candidate", "unsupported"], + "selected": ["candidate"], "excluded": ["unsupported"] + }); + payload["counts"] = json!({"selected": 1, "excluded": 1, "actionable": 1, "blocked": 0}); + payload["excluded_reasons"] = json!([{"key": "unsupported", "reason": "unsupported_schema"}]); + payload["findings"]["candidates"] = json!([ + {"key": "candidate", "findings": []}, + {"key": "unsupported", "findings": [{"code": "unsupported_schema"}]} + ]); + let (plan, digest) = store_plan( + ®istry, + &ws, + &job, + "plan_v2_excluded_reason", + &payload, + now, + ) + .await; + let request = ApplyImportPlanRequest { + workspace_id: &ws, + plan_id: &plan, + plan_digest: &digest, + idempotency_key: "idem_v2_excluded_reason", + verified_fingerprint: &fp, + applied_at: &now, + }; + let result = registry.apply_import_plan(&request).await.unwrap(); + assert_eq!(result.report.status, ImportPlanApplyStatus::Applied); + assert_eq!(result.report.counts.excluded, 1); + let excluded = result + .report + .outcomes + .iter() + .find(|item| item.key == "unsupported") + .unwrap(); + assert_eq!(excluded.outcome, ImportPlanCandidateOutcome::Excluded); + assert_eq!(excluded.reason.as_deref(), Some("unsupported_schema")); + assert!( + registry + .get_operation_summary(&ws, &crank_core::OperationId::new("op_v2_excluded_reason")) + .await + .unwrap() + .is_some() + ); + database.cleanup().await; +} + +#[tokio::test] +async fn plan_store_accepts_the_full_two_thousand_candidate_limit() { + 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_v2_limit").await; + let job = create_pending_openapi_job( + ®istry, + &ws, + "job_v2_limit", + now, + now + Duration::hours(1), + ) + .await; + activate_source(&raw, &ws, now).await; + let revision = registry.import_catalog_revision(&ws).await.unwrap(); + let mut keys = Vec::with_capacity(2_000); + let mut candidates = Vec::with_capacity(2_000); + let mut candidate_findings = Vec::with_capacity(2_000); + for index in 0..2_000 { + let key = format!("candidate-{index:04}"); + let name = format!("candidate_{index:04}_tool"); + let operation = operation( + &format!("op_limit_{index:04}"), + &name, + "2026-10-03T10:00:00Z", + "2026-10-03T10:00:00Z", + ); + keys.push(key.clone()); + candidates.push(json!({ + "key": key, "name": name, "state": "actionable", "decision": {"action": "create"}, + "findings": [], "operation": operation + })); + candidate_findings.push(json!({"key": key, "findings": []})); + } + let payload = json!({ + "schema_version": "import-plan-v2", "plan_reference": "plan_v2_limit", "job_id": job.as_str(), + "fingerprint": fingerprint(), "catalog_revision": revision, "support_matrix_version": "1.0.2", + "source": {"source_id": SOURCE_ID, "digest": SOURCE_DIGEST}, "dependencies": [], + "excluded_reasons": [], + "findings": {"document": [], "candidates": candidate_findings}, + "selection": {"mode": "full", "all": keys, "selected": keys, "excluded": []}, + "counts": {"selected": 2_000, "excluded": 0, "actionable": 2_000, "blocked": 0}, + "candidates": candidates + }); + let encoded = serde_json::to_vec(&payload).unwrap(); + assert!( + encoded.len() < 16 * 1024 * 1024, + "fixture itself must remain under the payload cap" + ); + let (plan_id, _) = store_plan(®istry, &ws, &job, "plan_v2_limit", &payload, now).await; + let stored = registry + .get_import_plan(&ws, &plan_id) + .await + .unwrap() + .unwrap(); + assert_eq!( + stored.payload["candidates"].as_array().unwrap().len(), + 2_000 + ); + assert_eq!( + stored.payload["selection"]["selected"] + .as_array() + .unwrap() + .len(), + 2_000 + ); + registry + .revalidate_import_plan(&ws, &plan_id, &fingerprint()) + .await + .expect("the maximum valid candidate set must pass registry material validation"); + database.cleanup().await; +} diff --git a/crates/crank-registry/tests/integration/migrations.rs b/crates/crank-registry/tests/integration/migrations.rs index 00a38eb..8bea08f 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,8 @@ 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"), + (15, "onboarding-import-selection-v15"), ]; assert_eq!(rows.len(), expected.len()); for (row, (version, name)) in rows.iter().zip(expected) { @@ -106,9 +110,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: 15 } ); } #[tokio::test] @@ -232,7 +247,7 @@ async fn ledgerless_legacy_baseline_is_consolidated_without_data_loss() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 15, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -345,13 +360,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: 15, } ); MigrationAuthority::apply(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 15 } ); let trace_column: bool = sqlx::query_scalar( "select exists ( @@ -394,7 +409,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: 15, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -454,7 +469,7 @@ async fn healthy_v4_upgrades_to_v5_without_fabricating_legacy_outcomes() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 4, - target: 13, + target: 15, } ); MigrationAuthority::apply(&pool).await.unwrap(); @@ -708,7 +723,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: 15 } ); } async fn remove_v3_schema(pool: &sqlx::PgPool) { @@ -862,7 +877,41 @@ async fn remove_v11_schema(pool: &sqlx::PgPool) { .await .unwrap(); } +async fn remove_v14_schema(pool: &sqlx::PgPool) { + remove_v15_schema(pool).await; + 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_v15_schema(pool: &sqlx::PgPool) { + sqlx::raw_sql( + "drop table if exists onboarding_import_selections; + alter table operations drop constraint if exists operations_workspace_id_unique; + alter table product_events drop constraint if exists product_events_name_check; + alter table product_events add constraint product_events_name_check check (event_name in ( + 'onboarding_eligible', 'onboarding_started', 'onboarding_resumed', + 'onboarding_dismissed', 'onboarding_abandoned', 'onboarding_completed' + )); + alter table product_event_daily_rollups drop constraint if exists product_event_daily_rollups_name_check; + alter table product_event_daily_rollups add constraint product_event_daily_rollups_name_check check (event_name in ( + 'onboarding_eligible', 'onboarding_started', 'onboarding_resumed', + 'onboarding_dismissed', 'onboarding_abandoned', 'onboarding_completed' + )); + delete from __crank_migrations where version = 15;", + ) + .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 +927,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..c789a40 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: 15, } ); @@ -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: 15 }, "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: 15 }, "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..81c75f7 --- /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: 15 } + ); + 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: 15, + } + ); + 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..52bf69e 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: 15, } ); 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: 15 } ); } @@ -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: 15, } ); 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: 15 } ); } @@ -262,7 +262,7 @@ async fn published_initial_mcp_layout_is_accepted() { MigrationAuthority::preflight(&pool).await.unwrap(), MigrationPreflight::MigrationRequired { current: 0, - target: 13, + target: 15, } ); 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: 15 } ); } @@ -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: 15, } ); MigrationAuthority::apply(&pool).await.unwrap(); assert_eq!( MigrationAuthority::preflight(&pool).await.unwrap(), - MigrationPreflight::Current { version: 13 } + MigrationPreflight::Current { version: 15 } ); } 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..e07dca2 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: 15, } ); } diff --git a/crates/crank-registry/tests/integration/onboarding.rs b/crates/crank-registry/tests/integration/onboarding.rs index c183b52..819dd6a 100644 --- a/crates/crank-registry/tests/integration/onboarding.rs +++ b/crates/crank-registry/tests/integration/onboarding.rs @@ -9,13 +9,20 @@ use crank_core::{ }; use crank_registry::{ CreateAgentRequest, CreateInvocationLogRequest, CreatePlatformApiKeyRequest, - CreateWorkspaceRequest, OnboardingPresentationMilestone, PublishAgentRequest, PublishRequest, - RecordOnboardingMilestoneRequest, + CreateVersionRequest, CreateWorkspaceRequest, OnboardingPresentationMilestone, + PublishAgentRequest, PublishRequest, RecordOnboardingMilestoneRequest, + RegisterOnboardingImportContextRequest, RegistryError, SelectOnboardingImportDraftRequest, }; use serde_json::json; use sqlx::Row; use time::{Duration, OffsetDateTime}; +#[path = "onboarding_import_concurrency.rs"] +mod onboarding_import_concurrency; +#[path = "onboarding_import_contract.rs"] +mod onboarding_import_contract; +#[path = "onboarding_import_selection.rs"] +mod onboarding_import_selection; #[path = "onboarding_lifecycle.rs"] mod onboarding_lifecycle; diff --git a/crates/crank-registry/tests/integration/onboarding_import_concurrency.rs b/crates/crank-registry/tests/integration/onboarding_import_concurrency.rs new file mode 100644 index 0000000..f18dc5b --- /dev/null +++ b/crates/crank-registry/tests/integration/onboarding_import_concurrency.rs @@ -0,0 +1,395 @@ +use std::time::Duration; + +use super::onboarding_import_selection::{ + context_count_for, event_count, insert_import_application, selected_version, +}; +use super::*; + +/// A concurrent archive of the exact pending candidate must never be selected. +/// +/// The blocker holds the candidate's `operations` row lock while the selection +/// reads its snapshot and blocks on the same row lock; the archive then commits +/// before the selection updates durable state. With only `READ COMMITTED` and a +/// lock-free validation the selection would record a drifted, archived +/// operation; the selection must instead abort (whole-transaction retry) and +/// surface a stale-revision or unavailable conflict without mutation. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn concurrent_archive_vs_select_rejects_without_mutation() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let observer = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_conc_archive", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_conc_archive", + &[( + "candidate", + "createLead", + "created", + "op_conc_archive", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_conc_archive", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + + // Hold the exact candidate row lock in an independent transaction. + let mut blocker = pool.begin().await.unwrap(); + let blocker_pid: i32 = sqlx::query_scalar("select pg_backend_pid()") + .fetch_one(&mut *blocker) + .await + .unwrap(); + sqlx::query("select id from operations where workspace_id = $1 and id = $2 for update") + .bind("ws_default") + .bind("op_conc_archive") + .fetch_one(&mut *blocker) + .await + .unwrap(); + + let select_registry = registry.clone(); + let select_workspace = workspace_id.clone(); + let select_task = tokio::spawn(async move { + select_registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &select_workspace, + application_idempotency_key: "idem_conc_archive", + plan_digest: &"b".repeat(64), + operation_id: &crank_core::OperationId::new("op_conc_archive"), + operation_version: 1, + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + }); + + // Observe the in-flight selection waiting on the exact blocker. + if select_task.is_finished() { + panic!( + "selection finished before the blocker: {:?}", + select_task.await.unwrap() + ); + } + wait_for_blocked_reader(&observer, blocker_pid).await; + sqlx::query( + "update operations set status = 'archived', updated_at = now() + where workspace_id = 'ws_default' and id = 'op_conc_archive'", + ) + .execute(&mut *blocker) + .await + .unwrap(); + blocker.commit().await.unwrap(); + + let error = select_task + .await + .unwrap() + .expect_err("an archived pending candidate must never be selected"); + assert!( + matches!( + error, + RegistryError::OnboardingStaleRevision + | RegistryError::OnboardingImportDraftUnavailable + ), + "unexpected selection error: {error:?}" + ); + assert_eq!(selected_version(&pool).await, None); + assert_eq!(context_count_for(&pool, "ws_default").await, 1); + + database.cleanup().await; +} + +/// A concurrent version edit of the exact pending candidate must never be +/// selected with the stale version. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn concurrent_version_edit_vs_select_rejects_without_mutation() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let observer = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_conc_edit", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_conc_edit", + &[( + "candidate", + "createLead", + "created", + "op_conc_edit", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_conc_edit", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + + let mut blocker = pool.begin().await.unwrap(); + let blocker_pid: i32 = sqlx::query_scalar("select pg_backend_pid()") + .fetch_one(&mut *blocker) + .await + .unwrap(); + sqlx::query("select id from operations where workspace_id = $1 and id = $2 for update") + .bind("ws_default") + .bind("op_conc_edit") + .fetch_one(&mut *blocker) + .await + .unwrap(); + + let select_registry = registry.clone(); + let select_workspace = workspace_id.clone(); + let select_task = tokio::spawn(async move { + select_registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &select_workspace, + application_idempotency_key: "idem_conc_edit", + plan_digest: &"b".repeat(64), + operation_id: &crank_core::OperationId::new("op_conc_edit"), + operation_version: 1, + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + }); + + if select_task.is_finished() { + panic!( + "selection finished before the blocker: {:?}", + select_task.await.unwrap() + ); + } + wait_for_blocked_reader(&observer, blocker_pid).await; + sqlx::query( + "update operations set current_draft_version = 2, updated_at = now() + where workspace_id = 'ws_default' and id = 'op_conc_edit'", + ) + .execute(&mut *blocker) + .await + .unwrap(); + blocker.commit().await.unwrap(); + + let error = select_task + .await + .unwrap() + .expect_err("a drifted pending candidate version must never be selected"); + assert!( + matches!( + error, + RegistryError::OnboardingStaleRevision + | RegistryError::OnboardingImportDraftUnavailable + ), + "unexpected selection error: {error:?}" + ); + assert_eq!(selected_version(&pool).await, None); + + database.cleanup().await; +} + +/// Concurrent first observations must enter the server-owned cohort exactly +/// once without aborting, and every caller must observe the same authoritative +/// `eligible_since`. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn concurrent_first_eligibility_is_single_and_consistent() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + + let barrier = std::sync::Arc::new(tokio::sync::Barrier::new(9)); + let mut tasks = Vec::new(); + for index in 0..8 { + // Legacy connect() deliberately has one connection per registry. + // Independent pools and a barrier make these transactions overlap. + let registry = if index == 0 { + registry.clone() + } else { + database.registry().await + }; + let ready = barrier.clone(); + tasks.push(tokio::spawn(async move { + ready.wait().await; + registry + .ensure_onboarding_eligibility( + &WorkspaceId::new("ws_default"), + OffsetDateTime::now_utc(), + ) + .await + })); + } + barrier.wait().await; + let mut eligible_since = None; + for task in tasks { + let projection = task + .await + .unwrap() + .expect("concurrent first observation must not abort"); + let since = projection + .eligible_since + .expect("every observer must see the server-owned cohort"); + match &eligible_since { + None => eligible_since = Some(since), + Some(existing) => assert_eq!(existing, &since), + } + } + assert_eq!(event_count(&pool, "onboarding_eligible").await, 1); + + database.cleanup().await; +} + +/// A snapshot must always pair its projection revision with matching import +/// choices, even while the only candidate is concurrently archived. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn concurrent_archive_never_pairs_stale_revision_with_newer_choices() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let observer = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_conc_snapshot", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_conc_snapshot", + &[( + "candidate", + "createLead", + "created", + "op_conc_snapshot", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_conc_snapshot", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + let draft_revision = registered.revision; + + // The projection reads the old catalog before its context read waits on + // the immutable application. No report mutation or production test hook. + let mut blocker = pool.begin().await.unwrap(); + let blocker_pid: i32 = sqlx::query_scalar("select pg_backend_pid()") + .fetch_one(&mut *blocker) + .await + .unwrap(); + sqlx::query( + "select idempotency_key from import_plan_applications + where workspace_id = $1 and idempotency_key = $2 for update", + ) + .bind("ws_default") + .bind("idem_conc_snapshot") + .fetch_one(&mut *blocker) + .await + .unwrap(); + + let reader = registry.clone(); + let reader_workspace = workspace_id.clone(); + let read_task = tokio::spawn(async move { + reader + .get_onboarding_snapshot(&reader_workspace) + .await + .unwrap() + }); + wait_for_blocked_reader(&observer, blocker_pid).await; + sqlx::query( + "update operations set status = 'archived', updated_at = now() + where workspace_id = 'ws_default' and id = 'op_conc_snapshot'", + ) + .execute(&mut *blocker) + .await + .unwrap(); + blocker.commit().await.unwrap(); + + let snapshot = read_task.await.unwrap(); + assert_eq!( + snapshot.projection.revision, draft_revision, + "the projection was read before the concurrent commit" + ); + let context = snapshot.import_context.expect("the context must survive"); + assert!( + context.choices[0].selectable, + "choices must use the same old snapshot as the revision" + ); + let current = registry + .get_onboarding_snapshot(&workspace_id) + .await + .unwrap(); + assert_ne!(current.projection.revision, draft_revision); + assert!(!current.import_context.unwrap().choices[0].selectable); + + database.cleanup().await; +} + +/// Wait for an observed database lock, never assume a task started after a sleep. +async fn wait_for_blocked_reader(pool: &sqlx::PgPool, blocker_pid: i32) { + tokio::time::timeout(Duration::from_secs(10), async { + loop { + let blocked: bool = sqlx::query_scalar( + "select exists(select 1 from pg_stat_activity + where datname = current_database() and pid <> $1 + and $1 = any(pg_blocking_pids(pid)))", + ) + .bind(blocker_pid) + .fetch_one(pool) + .await + .unwrap(); + if blocked { + break; + } + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await + .unwrap_or_else(|_| panic!("the reader must contend on blocker {blocker_pid}")); +} diff --git a/crates/crank-registry/tests/integration/onboarding_import_contract.rs b/crates/crank-registry/tests/integration/onboarding_import_contract.rs new file mode 100644 index 0000000..e81aa4f --- /dev/null +++ b/crates/crank-registry/tests/integration/onboarding_import_contract.rs @@ -0,0 +1,785 @@ +#![allow(dead_code, unused_imports)] + +use super::*; +use serde_json::Value; + +use super::onboarding_import_selection::{ + attach_published_agent_and_key, context_count_for, event_count, insert_import_application, + selected_version, +}; + +#[tokio::test] +async fn selected_operation_hard_delete_is_blocked_and_workspace_delete_cascades() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_import_fk", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_fk", + &[( + "candidate", + "createLead", + "created", + "op_import_fk", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_fk", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + let selected = registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_fk", + plan_digest: &"b".repeat(64), + operation_id: &operation.id, + operation_version: 1, + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(selected.operation_id, Some(operation.id.clone())); + + // `ON DELETE NO ACTION` blocks a direct hard delete of the selected + // Operation while the durable report context still anchors it. + let blocked = sqlx::query( + "delete from operations where workspace_id = 'ws_default' and id = 'op_import_fk'", + ) + .execute(&pool) + .await + .unwrap_err(); + let database_error = blocked.as_database_error().expect("foreign key violation"); + assert_eq!(database_error.code().as_deref(), Some("23503")); + assert_eq!( + database_error.constraint(), + Some("onboarding_import_selections_operation_fk") + ); + assert_eq!(context_count_for(&pool, "ws_default").await, 1); + + // An explicit reset clears the context; the Operation may then be deleted. + registry + .reset_onboarding_selection(&workspace_id, selected.revision, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert_eq!(context_count_for(&pool, "ws_default").await, 0); + sqlx::query("delete from operations where workspace_id = 'ws_default' and id = 'op_import_fk'") + .execute(&pool) + .await + .expect("after reset the selected Operation is no longer anchored"); + + // Workspace deletion still cascade-cleans the full context. + create_workspace(®istry, "ws_import_cascade", "import-cascade").await; + let other = WorkspaceId::new("ws_import_cascade"); + let other_operation = test_operation("op_cascade", 1, OperationStatus::Draft); + registry + .create_operation(&other, &other_operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_import_cascade", + "idem_cascade", + &[("candidate", "createLead", "created", "op_cascade", Some(1))], + ) + .await; + let other_initial = registry.get_onboarding_projection(&other).await.unwrap(); + let other_registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &other, + application_idempotency_key: "idem_cascade", + plan_digest: &"b".repeat(64), + expected_revision: other_initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &other, + application_idempotency_key: "idem_cascade", + plan_digest: &"b".repeat(64), + operation_id: &other_operation.id, + operation_version: 1, + expected_revision: other_registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(context_count_for(&pool, "ws_import_cascade").await, 1); + sqlx::query("delete from workspaces where id = 'ws_import_cascade'") + .execute(&pool) + .await + .unwrap(); + assert_eq!(context_count_for(&pool, "ws_import_cascade").await, 0); + + database.cleanup().await; +} + +#[tokio::test] +async fn exact_publication_is_continuation_while_archive_and_version_drift_stay_visible() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_import_drift", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_drift", + &[( + "candidate", + "createLead", + "created", + "op_import_drift", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_drift", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_drift", + plan_digest: &"b".repeat(64), + operation_id: &operation.id, + operation_version: 1, + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + + // Exact-version normal publication is the expected continuation: no drift, + // and the chooser no longer offers the published Operation as a Draft. + registry + .publish_operation(PublishRequest { + workspace_id: &workspace_id, + operation_id: &operation.id, + version: 1, + published_at: &OffsetDateTime::now_utc(), + published_by: Some("onboarding-import-drift-test"), + }) + .await + .unwrap(); + let context = registry + .get_onboarding_import_context(&workspace_id) + .await + .unwrap() + .unwrap(); + assert!( + context.drift.is_none(), + "exact-version publication is a continuation, not drift" + ); + let choice = context + .choices + .iter() + .find(|choice| choice.outcome == "created") + .unwrap(); + assert!( + !choice.selectable, + "a published Operation is no longer an actionable Draft choice" + ); + assert_eq!( + registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap() + .operation_id, + Some(operation.id.clone()) + ); + + // Archive keeps the report/context visible as unavailable drift. + registry + .archive_operation(&workspace_id, &operation.id, &OffsetDateTime::now_utc()) + .await + .unwrap(); + let archived = registry + .get_onboarding_import_context(&workspace_id) + .await + .unwrap() + .unwrap(); + assert_eq!( + archived.drift.as_ref().map(|drift| drift.reason.as_str()), + Some("selected_operation_not_draft") + ); + + // A newer Draft version is version drift even when the selected row exists. + create_workspace(®istry, "ws_import_drift2", "import-drift2").await; + let other = WorkspaceId::new("ws_import_drift2"); + let versioned = test_operation("op_versioned", 1, OperationStatus::Draft); + registry + .create_operation(&other, &versioned, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_import_drift2", + "idem_drift2", + &[( + "candidate", + "createLead", + "created", + "op_versioned", + Some(1), + )], + ) + .await; + let versioned_initial = registry.get_onboarding_projection(&other).await.unwrap(); + let versioned_registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &other, + application_idempotency_key: "idem_drift2", + plan_digest: &"b".repeat(64), + expected_revision: versioned_initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &other, + application_idempotency_key: "idem_drift2", + plan_digest: &"b".repeat(64), + operation_id: &versioned.id, + operation_version: 1, + expected_revision: versioned_registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + let snapshot = test_operation("op_versioned", 2, OperationStatus::Draft); + registry + .create_version(CreateVersionRequest { + workspace_id: &other, + snapshot: &snapshot, + change_note: None, + created_by: None, + }) + .await + .unwrap(); + let drifted = registry + .get_onboarding_import_context(&other) + .await + .unwrap() + .unwrap(); + assert_eq!( + drifted.drift.as_ref().map(|drift| drift.reason.as_str()), + Some("selected_operation_version_drift") + ); + + database.cleanup().await; +} + +#[tokio::test] +async fn pending_candidate_edit_or_archive_changes_revision_and_rejects_stale_select() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_pending_cas", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_pending_cas", + &[( + "candidate", + "createLead", + "created", + "op_pending_cas", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending_cas", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!( + registered.operation_id, None, + "a pending import context suppresses generic auto-selection" + ); + + // Editing the pending candidate changes the projection revision while no + // Operation is selected, so the old expected_revision is rejected. + let pending_revision = registered.revision; + registry + .create_version(CreateVersionRequest { + workspace_id: &workspace_id, + snapshot: &test_operation("op_pending_cas", 2, OperationStatus::Draft), + change_note: None, + created_by: None, + }) + .await + .unwrap(); + let edited = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_ne!( + edited.revision, pending_revision, + "a pending candidate edit must change the revision" + ); + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending_cas", + plan_digest: &"b".repeat(64), + operation_id: &operation.id, + operation_version: 1, + expected_revision: pending_revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingStaleRevision + )); + assert_eq!(selected_version(&pool).await, None); + + // Archiving the pending candidate also changes the revision and rejects + // the previous expected_revision without mutation. + registry + .archive_operation(&workspace_id, &operation.id, &OffsetDateTime::now_utc()) + .await + .unwrap(); + let archived = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_ne!(archived.revision, edited.revision); + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending_cas", + plan_digest: &"b".repeat(64), + operation_id: &operation.id, + operation_version: 2, + expected_revision: edited.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingStaleRevision + )); + // The archived candidate is no longer actionable even with a fresh CAS. + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending_cas", + plan_digest: &"b".repeat(64), + operation_id: &operation.id, + operation_version: 2, + expected_revision: archived.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportDraftUnavailable + )); + assert_eq!(selected_version(&pool).await, None); + + database.cleanup().await; +} + +#[tokio::test] +async fn import_context_report_survives_job_and_plan_gc() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_import_gc", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + sqlx::query( + "insert into import_jobs ( + id, workspace_id, kind, source_format, source_version, status, + preview_payload, created_operation_ids, error_text, created_at, expires_at, finished_at + ) values ('job_gc', 'ws_default', 'openapi', 'openapi', null, 'applied', + '{}'::jsonb, '[]'::jsonb, null, now(), now() + interval '1 day', now())", + ) + .execute(&pool) + .await + .unwrap(); + sqlx::query( + "insert into import_plans ( + workspace_id, id, job_id, digest, payload, created_at, expires_at + ) values ('ws_default', 'plan_gc', 'job_gc', $1, '{}'::jsonb, now(), now() + interval '1 day')", + ) + .bind("b".repeat(64)) + .execute(&pool) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_gc", + &[( + "candidate", + "createLead", + "created", + "op_import_gc", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_gc", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + + // GC the job; the plan cascades. The immutable application report stays + // recoverable by the durable identity. + sqlx::query("delete from import_jobs where id = 'job_gc'") + .execute(&pool) + .await + .unwrap(); + let plan_count: i64 = + sqlx::query_scalar("select count(*) from import_plans where id = 'plan_gc'") + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(plan_count, 0); + + let context = registry + .get_onboarding_import_context(&workspace_id) + .await + .unwrap() + .expect("report survives job/plan GC"); + assert_eq!(context.application_idempotency_key, "idem_gc"); + assert_eq!(context.plan_digest, "b".repeat(64)); + assert_eq!( + context.report.get("status").and_then(Value::as_str), + Some("applied") + ); + assert_eq!( + context + .choices + .iter() + .find(|choice| choice.outcome == "created") + .and_then(|choice| choice.operation_id.as_deref()), + Some("op_import_gc") + ); + + database.cleanup().await; +} + +#[tokio::test] +async fn import_context_governs_terminal_anchor_until_exact_selected_call() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let selected = test_operation("op_anchor_a", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &selected, None) + .await + .unwrap(); + let unrelated = test_operation("op_anchor_b", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &unrelated, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_anchor", + &[ + ("ca", "createA", "created", "op_anchor_a", Some(1)), + ("cb", "createB", "created", "op_anchor_b", Some(1)), + ], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let pending = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_anchor", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(pending.operation_id, None); + + // Publish the unrelated Operation and make a successful public call while + // the context is still pending: generic anchoring must be suppressed. + registry + .publish_operation(PublishRequest { + workspace_id: &workspace_id, + operation_id: &unrelated.id, + version: 1, + published_at: &OffsetDateTime::now_utc(), + published_by: Some("onboarding-anchor-test"), + }) + .await + .unwrap(); + let (unrelated_agent, unrelated_key) = + attach_published_agent_and_key(®istry, &unrelated.id, "agent_anchor_b", "pk_anchor_b") + .await; + record_onboarding_call( + ®istry, + &unrelated.id, + &unrelated_agent, + &unrelated_key, + "anchor_b_pending", + OffsetDateTime::now_utc(), + ) + .await; + assert_eq!(terminal_operation(&pool).await, None); + assert_eq!( + registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap() + .operation_id, + None, + "a pending import context suppresses generic Operation selection" + ); + + // Explicitly select A, then an unrelated B call must still not anchor. + // Publishing the unrelated candidate moved the revision (candidate live + // snapshot), so use the current authoritative revision for the CAS. + let before_select = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_ne!(before_select.revision, pending.revision); + let selected_projection = registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_anchor", + plan_digest: &"b".repeat(64), + operation_id: &selected.id, + operation_version: 1, + expected_revision: before_select.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(selected_projection.operation_id, Some(selected.id.clone())); + + registry + .publish_operation(PublishRequest { + workspace_id: &workspace_id, + operation_id: &selected.id, + version: 1, + published_at: &OffsetDateTime::now_utc(), + published_by: Some("onboarding-anchor-test"), + }) + .await + .unwrap(); + let (selected_agent, selected_key) = + attach_published_agent_and_key(®istry, &selected.id, "agent_anchor_a", "pk_anchor_a") + .await; + // A second unrelated call cannot replace the chosen path or complete it. + record_onboarding_call( + ®istry, + &unrelated.id, + &unrelated_agent, + &unrelated_key, + "anchor_b_selected", + OffsetDateTime::now_utc(), + ) + .await; + assert_eq!(terminal_operation(&pool).await, None); + let still_selected = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_eq!(still_selected.operation_id, Some(selected.id.clone())); + assert!(!still_selected.completed); + + // Only the exact selected Operation/version call completes the path once. + record_onboarding_test_and_call( + ®istry, + &selected.id, + &selected_agent, + &selected_key, + "anchor_a", + OffsetDateTime::now_utc(), + ) + .await; + assert_eq!( + terminal_operation(&pool).await, + Some(selected.id.as_str().to_owned()) + ); + let completed = registry + .ensure_onboarding_eligibility(&workspace_id, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert!(completed.completed); + assert!( + completed + .step(OnboardingStepId::FirstCall) + .is_some_and(|step| step.completed) + ); + assert_eq!(event_count(&pool, "onboarding_agent_activated").await, 1); + + // Re-observing is idempotent: the exact path stays single. + registry + .ensure_onboarding_eligibility(&workspace_id, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert_eq!(event_count(&pool, "onboarding_agent_activated").await, 1); + + // A later imported target cannot inherit A's completed terminal evidence. + let later = test_operation("op_after_terminal", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &later, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_after_terminal", + &[( + "later", + "createLater", + "created", + "op_after_terminal", + Some(1), + )], + ) + .await; + let before = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_after_terminal", + plan_digest: &"b".repeat(64), + expected_revision: before.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_after_terminal", + plan_digest: &"b".repeat(64), + operation_id: &later.id, + operation_version: 1, + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + registry + .publish_operation(PublishRequest { + workspace_id: &workspace_id, + operation_id: &later.id, + version: 1, + published_at: &OffsetDateTime::now_utc(), + published_by: None, + }) + .await + .unwrap(); + let preserved = registry + .ensure_onboarding_eligibility(&workspace_id, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert_eq!(preserved.operation_id, Some(selected.id.clone())); + assert!( + preserved.completed, + "the existing terminal tuple remains intact" + ); + assert_eq!( + event_count(&pool, "onboarding_import_published").await, + 1, + "A's publication must not be attributed to the later import" + ); + assert_eq!( + event_count(&pool, "onboarding_agent_activated").await, + 1, + "the later import has no successful public first call" + ); + + database.cleanup().await; +} + +async fn terminal_operation(pool: &sqlx::PgPool) -> Option { + sqlx::query_scalar::<_, Option>( + "select operation_id from onboarding_selections where workspace_id = 'ws_default'", + ) + .fetch_optional(pool) + .await + .unwrap() + .flatten() +} diff --git a/crates/crank-registry/tests/integration/onboarding_import_selection.rs b/crates/crank-registry/tests/integration/onboarding_import_selection.rs new file mode 100644 index 0000000..cc9243c --- /dev/null +++ b/crates/crank-registry/tests/integration/onboarding_import_selection.rs @@ -0,0 +1,731 @@ +use super::*; +use serde_json::{Value, json}; + +#[tokio::test] +async fn import_context_registration_is_revision_guarded_pending_and_recovers_report() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_import_pending", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_pending", + &[ + ( + "candidate_create", + "createLead", + "created", + "op_import_pending", + Some(1), + ), + ("candidate_skip", "skipMe", "skipped", "", None), + ], + ) + .await; + + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_eq!( + initial.operation_id, + Some(operation.id.clone()), + "without an import context the generic projection auto-selects the draft" + ); + + // A forged application identity has no committed immutable apply report. + assert!(matches!( + registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_missing", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportApplicationUnavailable + )); + assert_eq!(context_count(&pool).await, 0); + + // A mismatched plan digest is rejected without mutation. + assert!(matches!( + registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending", + plan_digest: &"d".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportApplicationUnavailable + )); + assert_eq!(context_count(&pool).await, 0); + + // Stale revision cannot register a context. + assert!(matches!( + registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision + 1, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingStaleRevision + )); + assert_eq!(context_count(&pool).await, 0); + + // A valid registration stores a pending context and suppresses the + // generic automatic Operation selection. + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(registered.operation_id, None); + assert_eq!(event_count(&pool, "onboarding_import_eligible").await, 1); + + // Recovery re-reads the immutable report and all outcome choices. + let context = registry + .get_onboarding_import_context(&workspace_id) + .await + .unwrap() + .expect("pending import context must be recoverable"); + assert!(context.selected_operation_id.is_none()); + assert_eq!(context.plan_digest, "b".repeat(64)); + assert_eq!( + context.report.get("schema_version").and_then(Value::as_str), + Some("import-plan-apply-report-v1") + ); + let created = context + .choices + .iter() + .find(|choice| choice.outcome == "created") + .expect("created choice"); + assert!(created.selectable); + assert_eq!(created.operation_id.as_deref(), Some("op_import_pending")); + let skipped = context + .choices + .iter() + .find(|choice| choice.outcome == "skipped") + .expect("skipped choice"); + assert!( + !skipped.selectable, + "blocked outcomes stay visible but not selectable" + ); + + // Exact re-registration is idempotent and keeps the single durable row. + let again = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_pending", + plan_digest: &"b".repeat(64), + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(again.operation_id, None); + assert_eq!(context_count(&pool).await, 1); + assert_eq!(event_count(&pool, "onboarding_import_eligible").await, 1); + + database.cleanup().await; +} + +#[tokio::test] +async fn import_selection_requires_exact_successful_outcome_and_live_draft() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let anchor = test_operation("op_import_anchor", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &anchor, None) + .await + .unwrap(); + let later = test_operation("op_import_later", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &later, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_select", + &[ + ( + "candidate_anchor", + "createLead", + "created", + "op_import_anchor", + Some(1), + ), + ( + "candidate_later", + "createTask", + "created", + "op_import_later", + Some(1), + ), + ], + ) + .await; + + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_select", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + let pending = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + + // Wrong application identity cannot select against this context. + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_other", + plan_digest: &"b".repeat(64), + operation_id: &anchor.id, + operation_version: 1, + expected_revision: pending.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportContextUnavailable + )); + + // A forged operation that is not a successful outcome of the report. + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_select", + plan_digest: &"b".repeat(64), + operation_id: &crank_core::OperationId::new("op_forged"), + operation_version: 1, + expected_revision: pending.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportDraftUnavailable + )); + + // A version drift from the exact successful outcome is rejected. + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_select", + plan_digest: &"b".repeat(64), + operation_id: &anchor.id, + operation_version: 7, + expected_revision: pending.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportDraftUnavailable + )); + assert_eq!(selected_version(&pool).await, None); + + // A foreign workspace with no registered context cannot claim it. + create_workspace(®istry, "ws_import_other", "import-other").await; + let other = WorkspaceId::new("ws_import_other"); + let other_revision = registry + .get_onboarding_projection(&other) + .await + .unwrap() + .revision; + assert!(matches!( + registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &other, + application_idempotency_key: "idem_select", + plan_digest: &"b".repeat(64), + operation_id: &anchor.id, + operation_version: 1, + expected_revision: other_revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap_err(), + RegistryError::OnboardingImportContextUnavailable + )); + assert_eq!(selected_version(&pool).await, None); + + // A valid selection anchors the exact Draft version. + let selected = registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_select", + plan_digest: &"b".repeat(64), + operation_id: &anchor.id, + operation_version: 1, + expected_revision: pending.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(selected.operation_id, Some(anchor.id.clone())); + assert_eq!(selected_version(&pool).await, Some(1)); + + // A second, otherwise eligible import Draft must not replace the explicit + // target. + let anchored = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_eq!(anchored.operation_id, Some(anchor.id.clone())); + + // A new Draft version of the selected operation is surfaced as drift, not + // silently rebound. + let snapshot = test_operation("op_import_anchor", 2, OperationStatus::Draft); + registry + .create_version(CreateVersionRequest { + workspace_id: &workspace_id, + snapshot: &snapshot, + change_note: None, + created_by: None, + }) + .await + .unwrap(); + let drifted = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + assert_eq!(drifted.operation_id, Some(anchor.id.clone())); + assert_eq!( + selected_version(&pool).await, + Some(1), + "the anchored import version must not drift with a new Draft" + ); + let context = registry + .get_onboarding_import_context(&workspace_id) + .await + .unwrap() + .unwrap(); + assert_eq!( + context.drift.as_ref().map(|drift| drift.reason.as_str()), + Some("selected_operation_version_drift") + ); + + // Reset clears the durable import context but keeps the evidence boundary. + let _ = registry + .reset_onboarding_selection(&workspace_id, drifted.revision, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert_eq!(context_count(&pool).await, 0); + + database.cleanup().await; +} + +#[tokio::test] +async fn import_funnel_milestones_require_exact_public_call() { + let database = TestDatabase::new().await; + let registry = database.registry().await; + let pool = database.raw_pool().await; + let workspace_id = WorkspaceId::new("ws_default"); + + let operation = test_operation("op_import_funnel", 1, OperationStatus::Draft); + registry + .create_operation(&workspace_id, &operation, None) + .await + .unwrap(); + insert_import_application( + &pool, + "ws_default", + "idem_funnel", + &[( + "candidate", + "createLead", + "created", + "op_import_funnel", + Some(1), + )], + ) + .await; + let initial = registry + .get_onboarding_projection(&workspace_id) + .await + .unwrap(); + let registered = registry + .register_onboarding_import_context(RegisterOnboardingImportContextRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_funnel", + plan_digest: &"b".repeat(64), + expected_revision: initial.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(event_count(&pool, "onboarding_import_eligible").await, 1); + assert_eq!(event_count(&pool, "onboarding_import_published").await, 0); + assert_eq!(event_count(&pool, "onboarding_agent_activated").await, 0); + + let selected = registry + .select_onboarding_import_draft(SelectOnboardingImportDraftRequest { + workspace_id: &workspace_id, + application_idempotency_key: "idem_funnel", + plan_digest: &"b".repeat(64), + operation_id: &operation.id, + operation_version: 1, + expected_revision: registered.revision, + occurred_at: OffsetDateTime::now_utc(), + }) + .await + .unwrap(); + assert_eq!(selected.operation_id, Some(operation.id.clone())); + + // Publish the exact selected operation/version. + let published_at = OffsetDateTime::now_utc(); + registry + .publish_operation(PublishRequest { + workspace_id: &workspace_id, + operation_id: &operation.id, + version: 1, + published_at: &published_at, + published_by: Some("onboarding-funnel-test"), + }) + .await + .unwrap(); + assert_eq!( + event_count(&pool, "onboarding_import_published").await, + 1, + "publishing the explicitly selected version materializes its funnel event immediately" + ); + let (agent_id, key_id) = attach_published_agent_and_key( + ®istry, + &operation.id, + "agent_import_funnel", + "pk_import_funnel", + ) + .await; + // Agent publication alone must not activate the funnel milestone. + registry + .ensure_onboarding_eligibility(&workspace_id, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert_eq!(event_count(&pool, "onboarding_import_published").await, 1); + assert_eq!( + event_count(&pool, "onboarding_agent_activated").await, + 0, + "Agent publication without a public call is not activation" + ); + + // The authoritative successful public first call on the exact selected + // operation/version with its Agent/key binding activates the milestone. + record_onboarding_test_and_call( + ®istry, + &operation.id, + &agent_id, + &key_id, + "import_funnel", + OffsetDateTime::now_utc(), + ) + .await; + let projection = registry + .ensure_onboarding_eligibility(&workspace_id, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert!( + projection.completed, + "the selected import path must reach the authoritative terminal tuple" + ); + assert_eq!(event_count(&pool, "onboarding_import_published").await, 1); + assert_eq!(event_count(&pool, "onboarding_agent_activated").await, 1); + + // The conversion milestones are anchored to authoritative evidence, not to + // the observation time: publication uses the exact selected version's + // `published_at`, activation uses the exact successful public first call. + let published_event_at: OffsetDateTime = sqlx::query_scalar( + "select occurred_at from product_events + where workspace_id = 'ws_default' and event_name = 'onboarding_import_published'", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!( + published_event_at, + super::truncate_to_micros(published_at), + "import-published must inherit the exact selected version published_at" + ); + let first_call_at = projection + .first_call_at + .expect("the completed projection carries the authoritative first call"); + let activated_event_at: OffsetDateTime = sqlx::query_scalar( + "select occurred_at from product_events + where workspace_id = 'ws_default' and event_name = 'onboarding_agent_activated'", + ) + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!( + activated_event_at, first_call_at, + "agent-activated must inherit the exact successful public first_call_at" + ); + let properties: Vec = sqlx::query_scalar( + "select properties_json::text from product_events + where workspace_id = $1 and event_name in ( + 'onboarding_import_eligible', 'onboarding_import_published', + 'onboarding_agent_activated')", + ) + .bind("ws_default") + .fetch_all(&pool) + .await + .unwrap(); + assert_eq!(properties.len(), 3); + for value in properties { + let stored: Value = serde_json::from_str(&value).unwrap(); + let object = stored + .as_object() + .expect("server event properties are bounded objects"); + let mut keys: Vec<&str> = object.keys().map(String::as_str).collect(); + keys.sort_unstable(); + assert_eq!( + keys, + ["eligible", "eligible_since", "milestone"], + "server-owned properties must never carry source bytes, credentials or actor IDs" + ); + assert!(!value.contains("ws_default")); + assert!(!value.contains("openapi:")); + assert!(!value.contains("secret")); + } + + // Every funnel milestone inherits the authoritative eligible cohort; none + // is appended with `eligible=null/false`. + let cohort: Vec<(String, bool, Option)> = sqlx::query_as( + "select event_name, + (properties_json ->> 'eligible')::boolean, + properties_json ->> 'eligible_since' + from product_events + where workspace_id = $1 and event_name in ( + 'onboarding_import_eligible', 'onboarding_import_published', + 'onboarding_agent_activated') + order by event_name", + ) + .bind("ws_default") + .fetch_all(&pool) + .await + .unwrap(); + assert_eq!(cohort.len(), 3); + let eligible_since = cohort + .iter() + .find(|(name, _, _)| name == "onboarding_import_eligible") + .and_then(|(_, _, since)| since.clone()) + .expect("eligibility denominator has eligible_since"); + for (name, eligible, since) in &cohort { + assert!(*eligible, "{name} must inherit eligible=true"); + assert_eq!( + since.as_deref(), + Some(eligible_since.as_str()), + "{name} must inherit the authoritative eligible_since" + ); + } + + let rolled_up: i64 = sqlx::query_scalar( + "select events_total from product_event_daily_rollups + where workspace_id = $1 and event_name = 'onboarding_import_published'", + ) + .bind("ws_default") + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(rolled_up, 1); + // The rollup day follows the authoritative evidence day, so a publication + // observed later still rolls up on its exact published_at date. + let rollup_day: String = sqlx::query_scalar( + "select day::text from product_event_daily_rollups + where workspace_id = $1 and event_name = 'onboarding_import_published'", + ) + .bind("ws_default") + .fetch_one(&pool) + .await + .unwrap(); + assert_eq!(rollup_day, published_at.date().to_string()); + + // Re-observing the projection is idempotent: milestones stay single. + registry + .ensure_onboarding_eligibility(&workspace_id, OffsetDateTime::now_utc()) + .await + .unwrap(); + assert_eq!(event_count(&pool, "onboarding_import_eligible").await, 1); + assert_eq!(event_count(&pool, "onboarding_import_published").await, 1); + assert_eq!(event_count(&pool, "onboarding_agent_activated").await, 1); + + database.cleanup().await; +} +pub(super) async fn attach_published_agent_and_key( + registry: &crank_registry::PostgresRegistry, + operation_id: &crank_core::OperationId, + agent_id: &str, + key_id: &str, +) -> (crank_core::AgentId, PlatformApiKeyId) { + let workspace_id = WorkspaceId::new("ws_default"); + let agent = test_agent(agent_id, AgentStatus::Draft); + let version = test_agent_version(&agent.id, 1, AgentStatus::Draft); + let bindings = vec![AgentOperationBinding { + operation_id: operation_id.clone(), + operation_version: 1, + tool_name: "create_lead".to_owned(), + tool_title: "Create lead".to_owned(), + tool_description_override: None, + enabled: true, + }]; + registry + .create_agent(CreateAgentRequest { + agent: &agent, + version: &version, + bindings: &bindings, + }) + .await + .unwrap(); + registry + .publish_agent(PublishAgentRequest { + workspace_id: &workspace_id, + agent_id: &agent.id, + version: 1, + published_at: &OffsetDateTime::now_utc(), + published_by: Some("onboarding-funnel-test"), + expected_state: None, + }) + .await + .unwrap(); + let key_id = PlatformApiKeyId::new(key_id); + let key = PlatformApiKey { + id: key_id.clone(), + workspace_id, + agent_id: Some(agent.id.clone()), + key_kind: PlatformApiKeyKind::McpClient, + name: format!("onboarding-{key_id}-key"), + prefix: format!("crk_{}", key_id.as_str()), + scopes: vec![PlatformApiKeyScope::Read, PlatformApiKeyScope::Write], + status: PlatformApiKeyStatus::Active, + created_at: OffsetDateTime::now_utc(), + last_used_at: None, + expires_at: None, + allowed_origins: Vec::new(), + }; + registry + .create_platform_api_key(CreatePlatformApiKeyRequest { + api_key: &key, + secret_hash: "funnel-secret-hash", + }) + .await + .unwrap(); + (agent.id, key_id) +} + +async fn context_count(pool: &sqlx::PgPool) -> i64 { + context_count_for(pool, "ws_default").await +} + +pub(super) async fn context_count_for(pool: &sqlx::PgPool, workspace_id: &str) -> i64 { + sqlx::query_scalar("select count(*) from onboarding_import_selections where workspace_id = $1") + .bind(workspace_id) + .fetch_one(pool) + .await + .unwrap() +} + +pub(super) async fn selected_version(pool: &sqlx::PgPool) -> Option { + sqlx::query_scalar( + "select selected_operation_version from onboarding_import_selections where workspace_id = 'ws_default'", + ) + .fetch_optional(pool) + .await + .unwrap() + .flatten() +} + +pub(super) async fn event_count(pool: &sqlx::PgPool, event_name: &str) -> i64 { + sqlx::query_scalar( + "select count(*) from product_events where workspace_id = 'ws_default' and event_name = $1", + ) + .bind(event_name) + .fetch_one(pool) + .await + .unwrap() +} + +pub(super) type OutcomeSpec<'a> = (&'a str, &'a str, &'a str, &'a str, Option); + +pub(super) async fn insert_import_application( + pool: &sqlx::PgPool, + workspace_id: &str, + idempotency_key: &str, + outcomes: &[OutcomeSpec<'_>], +) { + let outcomes = outcomes + .iter() + .map(|(key, name, outcome, operation_id, version)| { + json!({ + "key": key, + "name": name, + "outcome": outcome, + "operation_id": if operation_id.is_empty() { Value::Null } else { Value::String((*operation_id).to_owned()) }, + "version": version, + }) + }) + .collect::>(); + let report = json!({ + "schema_version": "import-plan-apply-report-v1", + "status": "applied", + "plan_id": "plan_import", + "plan_digest": "b".repeat(64), + "job_id": "job_import", + "idempotency_key": idempotency_key, + "source_id": "source_import", + "source_digest": "c".repeat(64), + "dependencies": [], + "counts": {"created": 1, "versioned": 0, "renamed": 0, "skipped": 0, "excluded": 0, "rejected": 0}, + "outcomes": outcomes, + "summary": "import applied", + "recovery": "none", + "applied_at": "2026-10-03T00:00:00Z", + }); + sqlx::query( + "insert into import_plan_applications ( + workspace_id, idempotency_key, plan_id, plan_digest, report, created_at + ) values ($1, $2, 'plan_import', $3, $4, now())", + ) + .bind(workspace_id) + .bind(idempotency_key) + .bind("b".repeat(64)) + .bind(report) + .execute(pool) + .await + .unwrap(); +} diff --git a/docs/README.md b/docs/README.md index ac2a4f3..1364330 100644 --- a/docs/README.md +++ b/docs/README.md @@ -47,6 +47,7 @@ Crank превращает REST API endpoint-ы в MCP-инструменты, - [Архитектура](./architecture.md) - [Модель данных](./data-model.md) - [Тестирование](./testing-strategy.md) +- [План приёмочного тестирования всех восьми эпиков](./acceptance-test-plan.md) — сценарии, пороги и шаблоны evidence для отдельной приёмки. В Inventory только статус `implemented` означает готовность. Статусы `planned`, `gap` и `blocked` остаются непроходными и не являются заявлением о наличии diff --git a/docs/capability-baseline/manifest.json b/docs/capability-baseline/manifest.json index 3ac9ac0..9e5b6ba 100644 --- a/docs/capability-baseline/manifest.json +++ b/docs/capability-baseline/manifest.json @@ -3,7 +3,7 @@ { "kind": "inventory", "path": "docs/capability-inventory.json", - "sha256": "9abb9106812cc0a82e98e39cf9646aade1355495dea737c86ee95cc6d514e2ef" + "sha256": "83742e83715deeac32033c1b103a8e94e53d863a90203f96ed9d57521c8bb1f2" }, { "kind": "required_surfaces", diff --git a/docs/capability-inventory.json b/docs/capability-inventory.json index 751fba5..cc6a876 100644 --- a/docs/capability-inventory.json +++ b/docs/capability-inventory.json @@ -841,14 +841,15 @@ ], "evidence": { "automated": [ - "apps/ui/tests/e2e/onboarding.spec.js" + "apps/ui/tests/e2e/onboarding.spec.js", + "apps/ui/tests/e2e/openapi-onboarding.spec.js" ], "manual": [ "docs/manual-regression-checklist.md" ] }, "id": "ui-onboarding-first-call", - "notes": "Automated and five-participant manual evidence must be recorded against a clean revision. The manual protocol is explicitly not_run in this baseline and does not constitute a release usability pass.", + "notes": "The OpenAPI Playwright source combines intercepted UI contracts with real-stack Auth Profile mapping and RU/EN public MCP call cases; server integration tests cover authoritative selection and private funnel properties; source presence is not a test result. Automated results and the five-participant OpenAPI protocol in docs/manual-regression-checklist.md section 5.10 must be recorded against a clean revision. Human usability evidence remains not_run until an actual eligible run; this flow remains blocked and does not constitute a usability pass.", "owner": "onboarding-ui-community", "requirements": [ "FR-1", @@ -857,7 +858,7 @@ ], "status": "blocked", "type": "ui", - "user_outcome": "A new self-hosted administrator can resume the optional Getting Started journey from manual REST Operation to an authoritative public MCP Tool call without retaining raw key material." + "user_outcome": "A new self-hosted administrator can resume the optional Getting Started journey from a manual REST Operation or one explicitly selected OpenAPI-imported Draft to an authoritative public MCP Tool call without retaining raw key material." } ], "product": "crank-community", diff --git a/docs/manual-regression-checklist.md b/docs/manual-regression-checklist.md index 0451ad0..32efb42 100644 --- a/docs/manual-regression-checklist.md +++ b/docs/manual-regression-checklist.md @@ -10,6 +10,10 @@ - отделить автоматический baseline от ручного smoke на стенде; - не заставлять следующего агента заново собирать сценарии по всему проекту. +## Этап исполнения + +Ручные сценарии этого checklist выполняются в отдельном приёмочном запуске. Для завершения разработки требуются готовые сценарии, fixtures/harness, пороги и шаблоны результатов, а не участие людей или внешние измерения в coding session. Единый [план приёмочного тестирования всех восьми эпиков](acceptance-test-plan.md) задаёт покрытие и порядок запуска. Реальные usability/performance/qualification outcomes остаются `not_run` до исполнения; release gates требуют фактического evidence. + ## 2. Автоматический baseline Локальный regression baseline на `2026-04-07`: @@ -202,6 +206,93 @@ call не более 15 минут. Незавершивший участник numerator публикуются раздельно. До фактического пятиучастникового запуска результат этого протокола остаётся `not_run`, а не `pass`. +### 5.10. Getting Started: ветка готового OpenAPI для пяти участников + +Этот отдельный сценарий проверяет ветку готовой спецификации по Story 2.15. +Он дополняет сценарий 5.9 и не заменяет его: manual REST и OpenAPI результаты +записываются отдельно. До запуска подготовьте доступную тестовую REST-службу с +безопасными синтетическими данными и OpenAPI-файл, соответствующий текущим +ограничениям импорта. Все его actionable методы должны иметь проверяемый +результат; если требуется аутентификация, заранее настройте совместимый +workspace Auth Profile с тестовым Secret. Не используйте production credentials +или данные. Не включайте значения Secret в файл спецификации. + +Проведите пять независимых прогонов на свежих self-hosted установках и новых +Admin accounts. Участникам разрешены только продукт, подготовленный файл и +обычное MCP client окружение. До каждого прогона оператор проверяет доступность +тестовой службы, файла и MCP client, но не показывает участнику дальнейшие шаги. +Назначьте язык интерфейса до начала таймера (как минимум один прогон на русском +и один на английском); не переключайте язык во время прогона. Каждый участник +сам выполняет действия: + +1. Откройте **Getting Started**, выберите путь OpenAPI, загрузите подготовленный + файл и запустите preview. +2. Осмотрите методы, findings, security requirements и предлагаемые Operations. + Убедитесь, что blocker и unsupported findings видны. Выберите подходящую + policy; для защищённого метода используйте совместимый Auth Profile, то + есть только ссылку на Secret. +3. Проверьте точный план и последствия для конфликтующих Operations, подтвердите + Apply и осмотрите полный report. Если импорт создал несколько Drafts, явно + выберите одну actionable Draft для продолжения. Убедитесь, что публикация не + произошла автоматически. +4. Обновите страницу и возобновите onboarding. Сверьте выбранную Draft и report; + убедитесь, что повторное открытие или продолжение не создало новую версию. + При surfaced stale/error recovery следуйте UI: перечитайте актуальные данные, + создайте новый preview/plan при изменившемся digest и подтвердите его заново. + Запишите фактически увиденную ошибку и восстановление; не имитируйте успешный + recovery, если его не удалось пройти. +5. Через обычный Operation wizard проверьте Draft, выполните test и явно + опубликуйте Operation. Создайте Agent с этой опубликованной версией, + опубликуйте Agent, выпустите MCP client key через one-time reveal , затем + выполните публичные `initialize`, `notifications/initialized`, `tools/list` + и `tools/call` по показанному MCP endpoint. +6. Завершите прогон только после подтверждения first call в authoritative + checklist или Invocation History с Agent, key, Tool, Request ID и Trace ID. + В процессе также проверьте refresh/resume после test, publication и создания + key; raw key не должен повторно раскрываться. + +Запустите таймер при первом authenticated рендере protected Admin UI и +остановите при authoritative first call. В таблице фиксируйте только +обезличенный label участника (`P1`–`P5`), eligibility, язык, завершение, +длительность, наблюдённые recovery states и краткий безопасный результат. Не +записывайте имя, email, account/workspace IDs, credentials, raw key, payload, +исходную спецификацию, endpoint/private URL, Request ID, Trace ID или скриншоты. +Для незавершённого eligible прогона укажите `30 min (censored)` независимо от +момента остановки; он остаётся в denominator. Рассчитайте median по пяти +eligible значениям времени с этим 30-минутным пределом; completion numerator +публикуйте отдельно от eligibility denominator. Цель usability — median не +более 15 минут. Не записывайте протокол как `pass`, если не выполнены все пять +прогонов и цель не достигнута. + +Перед запуском заполните идентификатор чистой ревизии и безопасные сведения о +среде. Запись ревизии допускается только для чистой checkout без незакоммиченных +изменений; фиксируйте фактически запущенный commit. Шаблон остаётся `not_run`, +пока реальные значения не внесены после проведения исследования: + +| Поле | Значение | +|---|---| +| Статус | `not_run` | +| Чистая ревизия приложения | `` | +| Среда | `` | +| Eligible участников | `0 / 5` | +| Завершившие до 30 min | `0 / 5` | +| Длительности P1–P5 | `` | +| Median (censored, max 30 min) | `` | +| Recovery после refresh/stale/error | `` | +| Итог и ограничения | `` | + +| Участник | Eligible | Язык | Завершил | Время (max 30 min) | Refresh/recovery и результат | +|---|---|---|---|---|---| +| P1 | `` | `` | `` | `` | `` | +| P2 | `` | `` | `` | `` | `` | +| P3 | `` | `` | `` | `` | `` | +| P4 | `` | `` | `` | `` | `` | +| P5 | `` | `` | `` | `` | `` | + +Отсутствующий участник не считается нулём: публикуйте eligibility denominator и +completion numerator раздельно. Не подменяйте этот human run результатом +автоматического теста или документации. + ## 6. Protocol smoke pass Для smoke-проверки MCP использовать готовый public target из [public-smoke-targets.md](public-smoke-targets.md). diff --git a/docs/openapi-import.md b/docs/openapi-import.md index b674963..fa7fe90 100644 --- a/docs/openapi-import.md +++ b/docs/openapi-import.md @@ -2,39 +2,168 @@ Crank умеет импортировать REST API из OpenAPI 3.x и Swagger 2.0 документов. Импорт создает черновики MCP-инструментов, которые можно проверить и доработать в мастере операции перед публикацией. +## Начало работы из OpenAPI + +В **Getting Started** можно выбрать OpenAPI как источник первой Operation. Эта +ветка открывает тот же ограниченный импорт, что и раздел **Операции**: отдельного +парсера для onboarding нет. После preview проверьте methods, findings, security +references, конфликты и точное предложение плана. Устраните blockers или явно +исключите затронутые методы согласно выбранной policy; blocker не скрывается +переходом к частичному применению. Для security используйте совместимый Auth +Profile и ссылку на Secret, не копируйте credential в onboarding. + +После Apply внимательно проверьте полный import report. Apply может создать +несколько Drafts; onboarding продолжает путь только после явного выбора одной +актуальной actionable Draft. Никакая Operation не публикуется автоматически. +Далее откройте выбранную Draft в обычном мастере, проверьте её contract и +выполните test, затем отдельно опубликуйте Operation. Создайте и опубликуйте +Agent с опубликованной версией, выпустите MCP client key и выполните первый +публичный `tools/call` через обычный MCP client. Checklist подтверждает шаги по +серверному состоянию; сам факт открытия страницы или возврат из import modal не +считается завершением. + +При refresh возобновите Getting Started и проверьте authoritative выбор Draft и +результат импорта. Неизвестный исход Apply восстанавливайте повтором того же +плана с исходной digest/idempotency identity. При stale plan, source correction +или изменившемся digest создайте новый preview и заново подтвердите последствия; +не повторяйте Apply со старым планом и не считайте локальный return context +источником истины. Для защищённых методов проверьте, что onboarding хранит +только Auth Profile reference и серверные идентификаторы; исходный документ +хранится через canonical artifact flow, а secret value не включается в +onboarding state. + +Человеческий пятиучастниковый OpenAPI usability run и его обезличенный шаблон +описаны в [ручном regression checklist, раздел 5.10](manual-regression-checklist.md#510-getting-started-ветка-готового-openapi-для-пяти-участников). +Выполнение этого сценария относится к отдельной приёмке, а development done требует готового [плана всех эпиков](acceptance-test-plan.md), протокола и шаблона evidence. +Этот сценарий описывает ожидаемый путь и требования к записи результатов; он не +является свидетельством проведённого запуска. До реального прогона на чистой +ревизии его статус — `not_run`. + ## Как импортировать 1. Откройте раздел **Операции**. 2. Нажмите **Импорт OpenAPI** рядом с кнопкой **Новая операция**. 3. Загрузите ровно один UTF-8 `.yaml`, `.yml` или `.json` файл размером от 1 B до 256 KiB. Вставка текста не поддерживается. 4. Нажмите **Разобрать документ**. -5. Проверьте preview: сгенерированный contract сохраняет parameter locations, body formats и варианты успешного ответа. + Preview принимает до 2 000 methods; документ с большим числом кандидатов отклоняется целиком с typed ошибкой, без скрытого усечения. +5. Проверьте preview: сгенерированный contract сохраняет parameter locations, body formats, варианты успешного ответа и effective security requirements. + API помечает метод `actionable`, `unresolved` или `blocked` по typed findings и доступным security choices; окончательное разрешение выбранного Auth Profile происходит при создании плана. 6. Выберите нужные группы и методы. 7. Проверьте `Base URL`. Если в документе нет `servers`, укажите URL вручную. -8. Выберите поведение при конфликте имени: - - **Создать копию с новым именем** — Crank добавит суффикс `_2`, `_3` и так далее. - - **Пропустить** — существующие операции не будут изменены. -9. Нажмите **Создать черновики**. +8. Для уже существующей операции подтвердите решение отдельно: пропустить точную операцию, создать отдельную операцию с указанным вами свободным именем или добавить новую черновую версию к выбранной REST-операции. Автоматический подбор имени при применении плана не выполняется. +9. Создайте план и проверьте число выбранных, исключённых, доступных и заблокированных методов. По умолчанию действует полный режим: любой blocker запрещает Apply всего документа. Для частичного импорта явно включите режим и выберите только методы без blocker findings с разрешёнными зависимостями и аутентификацией. +10. Для защищённых методов выберите security alternative (OR-варианты preview; неподдерживаемые варианты, включая AND/OAuth, показаны заблокированными) и, если требуется аутентификация, совместимый Auth Profile текущего workspace — по безопасным метаданным `id/name/kind` из preview. Вариант без схем — анонимный вызов без профиля. Статус привязки показывает только API: `Unresolved` до проверки, `Resolved` после подтверждения планом, `Blocked` при отказе; UI не вычисляет resolved локально. Незакрытое обязательное требование или недоступный профиль оставляет метод заблокированным. +11. Подтвердите план и примените его. Создание плана не меняет операции. Применение выполняет все выбранные изменения одной транзакцией и показывает результат для каждого метода. + +## Прямые запросы к API плана + +Все маршруты ниже имеют префикс `/api/admin/workspaces/{workspace_id}`. +Используйте session cookie и `x-csrf-token` для POST, как описано в +[авторизации Admin API](admin-api.md#авторизация). Идентификаторы, digest, +ключи методов и версии берите из ответов сервера, а не вычисляйте самостоятельно. + +После multipart preview создайте план через +`POST /imports/openapi/{job_id}/plans`. Например, для одного анонимного метода +без конфликта: + +```json +{ + "selection_mode": "partial", + "selected_operation_keys": ["GET /pets"], + "server_url": "https://api.example.com", + "decisions": [{"operation_key": "GET /pets", "action": "create"}] +} +``` + +Ключ `GET /pets` здесь — пример; используйте точный `key` из preview. +`full` требует полного выбора и не допускает blockers; `partial` явно исключает +невыбранные методы. `server_url` можно не передавать, если URL получен из документа. +Решение `rename` требует `confirmed_name`, `skip` и `new_version` — точный +`target_operation_id`. Для защищённого метода укажите совместимый +`auth_profile_id` и выбранный `security_alternative_index`; значения секретов +в этих запросах не передаются. + +Ответ содержит `plan_reference`, `plan_digest`, `catalog_revision`, +`support_matrix_version`, `expires_at`, `selection_mode`, `counts` и `candidates`. +Само создание плана не создаёт Draft. При повторном открытии выполните +`GET /imports/openapi/plans/{plan_reference}` без тела запроса: ответ возвращает +`revalidation_status` (`current` или `stale`), `revalidation_required`, `reason`, +исходные `plan_digest`, `counts` и `candidates`. Срок действия плана ограничен +сроком preview job; после истечения или изменения исходных условий создайте +новый preview и подтвердите новый план. + +Применение: `POST /imports/openapi/{plan_reference}/apply`: + +```json +{ + "plan_digest": "", + "idempotency_key": "import-pets-001" +} +``` + +Сохраните этот ключ вместе с digest до отправки. При потере ответа повторите +тот же запрос с теми же значениями: сервер возвращает сохранённый отчёт без +повторного создания версий. Ответ содержит `status`, `plan_id`, `plan_digest`, +`idempotency_key`, `counts`, `outcomes` и `recovery`; проверяйте результат каждого +метода, включая excluded/rejected. При отказе не подменяйте digest и не повторяйте +старый план с новым ключом в надежде обойти причину отказа. + +Чтобы продолжить Getting Started, получите актуальный `revision` через +`GET /onboarding`, затем вызовите `POST /onboarding/import-context`: + +```json +{ + "application_idempotency_key": "import-pets-001", + "plan_digest": "<тот же plan_digest>", + "expected_revision": 1 +} +``` + +Вместо `1` используйте текущий `revision`. Сервер возвращает `registered` и +поля актуального onboarding, включая `revision` и `import_context` с вариантами +выбора. Затем `POST /onboarding/select-draft` явно выбирает одну доступную Draft: + +```json +{ + "application_idempotency_key": "import-pets-001", + "plan_digest": "<тот же plan_digest>", + "operation_id": "", + "operation_version": 1, + "expected_revision": 2 +} +``` + +Обе числовые величины здесь — примеры: возьмите версию Draft из серверного +отчёта и revision из последнего onboarding-ответа. Ответ содержит `selected` и +обновлённый onboarding. При конфликте revision перечитайте состояние перед +повтором. Выбор не публикует операцию: test, publish, Agent/key и публичный +MCP-вызов остаются отдельными шагами. ## Загрузка и восстановление Браузер отправляет выбранный файл как `multipart/form-data`; границу multipart формирует сам браузер. Файл остаётся только в памяти текущего окна импорта. При замене файла, сбросе, закрытии окна, смене workspace/языка или уходе со страницы текущий запрос отменяется, а поздний ответ игнорируется. При `pagehide` (включая BFCache) выбранный файл и Base URL очищаются: после возврата нужно выбрать файл заново, повтор старой загрузки не предлагается. -Для ошибки можно выбрать файл заново или нажать **Повторить**. Диагностика показывает только ограниченные имя/размер файла и, если backend их вернул, `Request ID`/`Trace ID`; содержимое спецификации, digest, source ID и внутренние пути не выводятся. Повторное нажатие **Создать черновики** во время выполнения не создаёт вторую мутацию. +До сохранения source snapshot импорт проверяет основной документ и загруженные allowlisted `$ref`-документы вместе, включая разрешённые ссылки. Если такая проверка находит literal credential, загрузка отклоняется; значение не попадает в artifact storage, preview или текст ошибки. Безопасные внешние ссылки сохраняются как digest-проверенные immutable snapshots и не загружаются заново при Apply. -## Контракт API импорта и аутентификация +Перед разбором документ проходит ограниченную предварительную проверку без материализации YAML-aliases. Для каждого документа допуск на развёрнутые строки и ключи равен `min(16 × max(предел байтов, предел скаляра), 64 MiB)` (при значениях по умолчанию ≈4 MiB), а для набора основной документ + внешние snapshots — `min(сумма допусков, 64 MiB)`. Действуют и позлементные пределы: 256 KiB на документ, 256 KiB на скаляр/ключ, 128 aliases, 50 000 узлов, глубина 64, 10 000 элементов коллекции. Превышение возвращает контролируемую typed-ошибку предела без частичного импорта; обычные небольшие aliases, duplicate keys, non-finite значения и legacy-разбор сохраняют прежнее поведение. -В workspace API доступны `POST /imports/openapi/preview` (multipart upload) и `POST /imports/openapi/{job_id}/create` (JSON). Preview сохраняет effective security requirements по операциям и предлагает `auth_profile_choices`: для каждой OR-alternative он указывает индекс, имена схем, признак `supported` и только совместимые профили текущего workspace. Варианты профиля содержат ID, имя и тип; значения Secret и другие credential material не возвращаются. +Раскрытие `$ref` в preview учитывает отдельный общий бюджет копируемых строк и ключей: `min(16 × max(предел документа, предел внешнего документа, предел скаляра), 64 MiB)` (по умолчанию ≈4 MiB). Бюджет общий для всего разрешения ссылок и не сбрасывается на каждую ссылку; превышение даёт контролируемую typed-ошибку предела до выделения памяти, а обычные документы, циклы и семантика `$ref` (включая sibling-поля OAS 3.1) не меняются. -При создании черновиков отправляйте `auth_mappings` с одной записью на выбранную защищённую операцию: `{ "operation_key": "GET /items", "alternative_index": 0, "auth_profile_id": "auth_…" }`. `alternative_index` начинается с нуля. Внешний список security alternatives означает OR; все схемы внутри одной alternative означают AND. Текущий runtime применяет одну Auth Profile, поэтому неподдерживаемый AND-вариант нельзя выбрать. Для явно пустой alternative можно передать `auth_profile_id: null`; отсутствие mappings остаётся совместимым для операций без security requirements. Неуказанное или unresolved required mapping блокирует создание Draft. +Для ошибки можно выбрать файл заново или нажать **Повторить**. Диагностика загрузки показывает только ограниченные имя/размер файла и, если backend их вернул, `Request ID`/`Trace ID`; содержимое спецификации и внутренние пути не выводятся. Подтверждение плана показывает счётчики и решения по кандидатам, но не исходный документ и не учётные данные. Digest плана передаётся в Apply автоматически. -API принимает Bearer, Basic и API key в header/query, если соответствующая Auth Profile принадлежит текущему workspace, параметры схемы совпадают и все связанные Secrets активны с доступной current version. Cookie API key, OAuth2, OpenID Connect, mutual TLS, неизвестные схемы и несовместимые AND-варианты отображаются как unsupported и требуют ручной конфигурации. Apply повторяет проверку профиля и Secret внутри registry transaction; отсутствующие, foreign, несовместимые профили и недоступные Secrets возвращают одну sanitized ошибку без metadata профиля. +При неизвестном результате Apply, например после разрыва соединения, повторите запрос с теми же ссылкой на план, digest и ключом идемпотентности. Повтор после commit возвращает сохранённый отчёт и не создаёт ещё одну версию; тот же ключ с другим digest даёт конфликт. Пока Apply выполняется, повторное нажатие кнопки не отправляет вторую логическую операцию. -Изменение требований безопасности после preview делает старый preview непригодным для применения: digest включает выбранные mapping-значения, а исходный документ повторно проверяется перед Apply. Literal credentials в поддерживаемых source locations отклоняются до сохранения upload или внешнего snapshot; ответ содержит только безопасный error code и не включает значение. Отдельный экран выбора Auth Profile в мастере относится к Story 2.11. +Если с момента создания плана изменились источник или внешние документы, версия импортера, workspace catalog либо состояние выбранной операции, а также конфигурация выбранного Auth Profile или версия его секрета, Apply отклоняет план без частичных изменений и предлагает заново разобрать документ. Read-only `GET /api/admin/workspaces/{workspace_id}/imports/openapi/plans/{plan_id}` проверяет сохранённый план при повторном открытии и возвращает `current` или `stale` с `revalidation_required`; проверка не создаёт операции и не делает запрос к изменяемому внешнему `$ref`. Отозванный или недоступный профиль также приводит к отказу. Срок жизни плана ограничен сроком исходного preview job. + +Новые планы `import-plan-v2` содержат source/external snapshot digests, версии normalizer/projection и support matrix, catalog revision, полный список кандидатов и selections, findings, решения по конфликтам и причины исключений. Digest вычисляется из semantic material: случайные идентификаторы plan/job/source/новой операции и время создания в него не входят. Сохранённый payload остаётся неизменяемым, а новый выбор или решение создаёт новую запись. Старые `import-plan-v1` продолжают проверяться по исходному полному digest payload. + +Workspace catalog для revision и naming читается целиком до 10 000 записей; превышение лимита даёт ошибку вместо усечения. Revision учитывает релевантные метаданные операций, а состояние выбранных Auth Profile и Secret проверяется по безопасным snapshot-метаданным без чтения raw/ciphertext credentials. Для `import-plan-v2` предел сохранённого payload — 16 MiB при общем пределе 2 000 candidates; исторический `import-plan-v1` сохраняет свой предел 4 MiB. Запрос создания плана ограничен 16 MiB как в API, так и в веб-прокси; предел применяется только к этому маршруту и не отменяет проверки содержимого плана. ## Что создается -Для каждого выбранного метода Crank создает отдельную операцию в статусе черновика: +Для каждого нового выбранного метода Crank создает отдельную операцию в статусе черновика. Для `new_version` новая Draft Version добавляется к точно выбранной операции; текущая опубликованная версия, прежние версии, привязки и история остаются прежними. `skip` не меняет выбранную существующую операцию. Исключённые методы не создаются, но перечисляются в отчёте. + +Контракт нового черновика: - имя инструмента берется из `operationId`, а если его нет — строится из HTTP-метода и пути; - отображаемое имя берется из `summary`; @@ -83,6 +212,70 @@ Semantic loss получает blocker с source location: такой candidate Один endpoint с несколькими request formats остаётся одним Tool. Поля `content_type` и `body` выбирают одну schema и соответствующую сериализацию; неизвестный selector или body другой ветви отклоняются до отправки запроса. Для единственного формата selector не нужен. Подробный inline-file contract описан в [REST](protocols/rest.md). -Новые imports используют `normalized-ir-v4` / `preview-v4`. Сохранённые v2/v3 jobs воспроизводят сохранённый preview, а legacy-v1 использует отдельную историческую projection. Обновление importer не меняет inputs существующих jobs. +Новые imports используют `normalized-ir-v5` / `preview-v6`: source-derived candidate, finding и naming conflict включают безопасный RFC 6901 `source_location`; typed findings сохраняют стабильный `code`. Схемы и маппинги принадлежат candidate с его source pointer и проверяемой provenance в IR/reference graph. Производные группы, counts и summary связаны с исходником через membership candidates; единственный pointer или finding code для такой агрегации был бы ложной provenance. Исторические v2/v3/v4/v5 jobs воспроизводят сохранённый preview, а legacy-v1 использует отдельную историческую projection. Обновление importer не меняет inputs существующих jobs. Другие форматы спецификаций и протоколов в Community-импорт не входят. + +## Проверка больших preview на Reference Deployment + +Этот benchmark выполняется на этапе отдельной приёмки; для завершения разработки поставляются harness, параметры среды, порог и evidence template, а не фактический внешний run. Проверка включена в [единый план приёмки](acceptance-test-plan.md). + +Локальная проверка пагинации использует mock API и измеряет только browser handler; она не подтверждает требование AC-11 о полной интерактивности ≤2 s p95. Для операторской проверки нужен отдельный Reference Deployment: Linux x86_64, сервисы с PostgreSQL 16 и Valkey 8 на 4 vCPU/8 GiB, SSD не менее 100 GB, 3000 IOPS и 100 MB/s, LAN не менее 1 Gbit/s, и отдельный эквивалентный load-generator с браузером. Перед запуском сохраните подтверждённые параметры без секретов в JSON-манифесте, например: + +```json +{ + "deployment": { + "host_id": "reference-server", "cpu_model": "record actual model", + "cpu_cores": 4, "ram_gib": 8, "postgres_version": "16.0", + "valkey_version": "8.0", "storage_type": "SSD", + "ssd_capacity_gb": 100, "ssd_iops": 3000, + "ssd_throughput_mb_s": 100, "lan_mbps": 1000 + }, + "load_generator": { "host_id": "reference-runner", "cpu_cores": 4, "ram_gib": 8 }, + "browser": { "host_id": "reference-runner" } +} +``` + +На load-generator задайте `PLAYWRIGHT_BASE_URL` для удалённого origin, `PLAYWRIGHT_SKIP_WEB_SERVER=1`, абсолютный путь `CRANK_AC11_REFERENCE_ENV_JSON` и учётные данные через `CRANK_E2E_ADMIN_EMAIL`/`CRANK_E2E_ADMIN_PASSWORD` в окружении; затем выполните из `apps/ui`: + +```sh +npx playwright test tests/e2e/openapi-preview-reference.spec.js --workers=1 +``` + +Harness отправляет реальный `openapi.yaml` с 2 000 operations (157 023 B), делает warm-up и 20 независимых preview-запросов без mock routes. Каждый замер начинается при нажатии Preview и заканчивается после ответа API, разбора и отображения первой страницы с доступной кнопкой создания плана. JSON attachment/stdout сохраняет все 20 samples, p95, DOM bound и sanitized сведения об окружении. Отсутствующий URL пропускает только отдельный операторский тест; при заданном URL отсутствующий или неподходящий манифест вызывает ошибку. Сам манифест — декларация оператора; результат требует фактического запуска в указанной инфраструктуре. + +Локальное ограниченное evidence: `cd apps/ui && npx playwright test tests/e2e/openapi-preview-partial.spec.js --grep 'AC-11' --workers=1` выводит `AC11_LOCAL_SHARED_HOST_METRICS` с сырыми samples. На shared host Linux x86_64 (Intel Core i5-4300U, 4 logical CPUs), headless Chromium 149, viewport 1280×720, mock fixture 2 000 candidates показал максимум 100 строк preview и 100 строк плана. Время ниже — длительность browser event handlers (`performance.now()`, ms), без API и LAN: + +```json +{ + "search_filter_ms": [7, 26, 12.7, 9.6, 5.3, 5.2, 6.5, 7.8, 5.5, 7.8, 5.6, 9.8, 8.2, 5.2, 7.9, 11.6, 6.9, 5.3, 6.6, 6], + "preview_page_change_ms": [50.1, 30.1, 23.3, 18.8, 21.3, 24.2, 30, 20.6, 69, 23.9, 22.3, 19.5, 18.8, 20.3, 25.1, 20.8, 20.6, 21.2, 22.4, 24.8], + "plan_page_change_ms": [2.8, 1.9, 1.9, 4.1, 2.9, 3.6, 2.9, 3.9, 2.4, 1.8, 4, 4.7, 1.9, 2, 1.7, 2.1, 1.8, 2.1, 1.7, 3.3] +} +``` + +## Нормативная матрица и regression corpus + +Машинная матрица версии `1.0.3` хранится в [`crates/crank-import/tests/support-matrix.yaml`](../crates/crank-import/tests/support-matrix.yaml); corpus version — `1.0.4`. Матрица фиксирует контракт отдельно для OpenAPI 3.0, OpenAPI 3.1 и Swagger 2.0. Acceptance test запускает нормализатор на отдельном probe для каждой строки, включая все 100 успешных кодов в каждом формате, и сверяет сохранённую semantics или точный blocker; выполненные probes списываются со всех строк версии, поэтому неподвергнутая проверкой строка матрицы падает gate. Corpus version `1.0.4` — metadata-only SemVer increment для согласования manifest с неизменной matrix `1.0.3`; fixture contents и checksums не менялись. Предыдущий corpus release `1.0.3` добавил hostile fixture `hostile-security-scope.json`: document-level требование безопасности применяется только к унаследовавшему его secured candidate (`GET /secure`, location `/security/0`), а операция с явным `security: []` (`GET /public`) остаётся actionable без глобального blocker; oversized hostile JSON остаётся тем же canonical JSON с ожидаемым parser rejection. + +| Конструкция | OpenAPI 3.0/3.1 | Swagger 2.0 | Сохраняемый контракт или finding | +|---|---|---|---| +| `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, `OPTIONS` | Поддерживается | Поддерживается | Метод и исходный path переходят в Operation | +| `path`, `query`, `header`, `cookie` параметры | Поддерживается | `path`, `query`, `header` поддерживаются; `cookie` явно unsupported | Имя, schema, required и source location сохраняются; Swagger cookie получает `unsupported_cookie_parameter` | +| Placeholder без объявленного path parameter | Blocker | Blocker | `path_parameter_not_declared` указывает точный operation source node; path-level и operation-level declarations, включая разрешённый `$ref`, учитываются | +| Schema defaults | Scalar сохраняется; object/array — blocker | Scalar сохраняется; object/array — blocker | `unsupported_compound_default` указывает точный `/default`; составное значение не превращается в lossy Draft | +| JSON, urlencoded, multipart | Поддерживается | Поддерживается по `consumes`/body parameter | Schema и content type остаются частью request contract | +| Успешные `2xx`, включая `204` | Поддерживается | Поддерживается | Каждая строка 200–299 проверяется отдельным probe; status сохраняется, а 204/205 остаются bodyless | +| Локальные и allowlisted external `$ref` | Поддерживается | Поддерживается | Разрешение только из загруженного документа или digest-проверенного snapshot; тесты не загружают fixture по сети | +| `allOf`, `oneOf`, `anyOf` | Поддерживается | Поддерживается | Совместимые `allOf` ветви объединяются; `oneOf` и `anyOf` остаются вариантами schema | +| bearer/basic/API key security schemes | Поддерживается | Поддерживается для basic и API key header/query | Схемы типизируются без credential values; preview предлагает только совместимые workspace Auth Profiles с активными current secrets | +| Security requirements с несколькими alternatives | Поддерживается | Поддерживается | Каждая OR alternative сохраняется отдельно и получает собственный список совместимых Auth Profiles; unsupported alternative не блокирует доступную соседнюю alternative | +| Несколько schemes в одной security alternative (AND) | Blocker для этой alternative | Blocker для этой alternative | Составное требование сохраняет имена схем в finding; оно не становится actionable через single-scheme Auth Profile | +| OAuth2, OpenID Connect, mutual TLS и неизвестные схемы | Manual configuration | Manual configuration | Явный unsupported finding; схема не извлекает token/client secret и не становится actionable автоматически | +| security override: operation-level `security: []` | Поддерживается | Поддерживается | Явный пустой массив отключает наследование: только operations с эффективным requirement получают source-located blocker, связанные операции остаются actionable | +| Микс public/secured operations | Поддерживается | Подвергается проверке | Blocker принадлежит только затронутому candidate; global blocker не создаётся для требований одного operation | +| XML, `TRACE`, `CONNECT` | Unsupported | Unsupported | Source-located finding; blocker не становится actionable Draft | + +Матрица `1.0.3` добавляет исполняемые probes обоих blockers во всех трёх форматах; coverage validation отвергает неподтверждённые source findings. Новые primary документы и external snapshots отвергают повторённые JSON/YAML mapping keys вместо last-key-wins; корректный YAML flow syntax сохраняется. HTTP(S) scheme регистронезависима, а Unicode hostname сопоставляется с punycode snapshot identity. Historical legacy v1 decoder и уже сохранённые completed reports не переписываются; новый preview с изменившейся semantics требует нового подтверждённого плана. + +Offline corpus в `crates/crank-import/tests/corpus/` содержит 50 synthetic representative документов и hostile subset. Manifest фиксирует SHA-256 исходных файлов, canonical digest JSON, происхождение и лицензию; для representative документов он также фиксирует полный canonical preview и frozen candidates (schemas, mappings, actionable/blocker), а для hostile — ожидаемые отказы и findings. Команда `just openapi-corpus` прогоняет настоящий normalizer, сравнивает независимо перечисленные raw source pointers с canonical source tree и coverage ledger, проверяет порог 95% actionable от всех representative Operations и отдельно нулевой silent loss. Evidence связывает digest manifest и matrix, включает content digest и создаётся без перезаписи; конфликт содержимого для того же identity останавливает gate. CI прикладывает `crates/crank-import/target/openapi-corpus/evidence-.json`. Artifact не содержит загруженный исходник или значения secret-like examples. diff --git a/docs/schemas/metrics-registry-v1.json b/docs/schemas/metrics-registry-v1.json index 05c91da..0a060f6 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": 5563, "rendered_series": 17638, "max_logical_series": 5563, "max_rendered_series": 17638}, "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}/onboarding/import-context", "/api/admin/workspaces/{workspace_id}/onboarding/select-draft", "/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/plans/{plan_id}", "/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": 4320, "max_rendered_series": 4320}, + {"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}/onboarding/import-context", "/api/admin/workspaces/{workspace_id}/onboarding/select-draft", "/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/plans/{plan_id}", "/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": 720, "max_rendered_series": 11520}, {"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..1a46ab3 100644 --- a/docs/schemas/migration-sequence.json +++ b/docs/schemas/migration-sequence.json @@ -208,6 +208,38 @@ "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 + }, + { + "backfill": { + "kind": "none" + }, + "checksum": "0dbe85c649ca227f15ac141e7bf004948d3240c3495489b30e2197980ecd0c5e", + "compatibility": "n-minus-one-readable", + "contract_evidence": null, + "name": "onboarding-import-selection-v15", + "owner": "crank-registry", + "phase": "expand", + "readable_schema_max": 15, + "readable_schema_min": 14, + "source_digest": "0dbe85c649ca227f15ac141e7bf004948d3240c3495489b30e2197980ecd0c5e", + "transactional": true, + "version": 15 } ] } diff --git a/justfile b/justfile index 1d312c5..7a796b3 100644 --- a/justfile +++ b/justfile @@ -44,6 +44,12 @@ clippy: test: cargo test --workspace --all-targets -- --test-threads=1 +openapi-corpus: + cargo test -p crank-import --test corpus -- --test-threads=1 + +openapi-matrix-compat base: + python3 scripts/check-openapi-matrix-compatibility.py --base "{{base}}" + sqlx-prepare database_url: DATABASE_URL={{database_url}} cargo sqlx prepare --workspace -- --all-targets diff --git a/scripts/check-openapi-matrix-compatibility.py b/scripts/check-openapi-matrix-compatibility.py new file mode 100755 index 0000000..09a9f94 --- /dev/null +++ b/scripts/check-openapi-matrix-compatibility.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python3 +"""Require versioned, explained compatibility changes to OpenAPI contracts.""" + +from __future__ import annotations + +import argparse +import json +import re +import subprocess +import sys +from typing import Any + +MATRIX_PATH = "crates/crank-import/tests/support-matrix.yaml" +MANIFEST_PATH = "crates/crank-import/tests/corpus/manifest.json" +DOCS_PATH = "docs/openapi-import.md" + + +def git_show(revision: str, path: str) -> str | None: + result = subprocess.run( + ["git", "show", f"{revision}:{path}"], + check=False, + capture_output=True, + text=True, + ) + return result.stdout if result.returncode == 0 else None + + +def matrix_version(contents: str | None) -> str | None: + if contents is None: + return None + match = re.search(r"(?m)^matrix_version:\s*['\"]?([^\s'\"]+)", contents) + return match.group(1) if match else None + + +def parse_manifest(contents: str | None, label: str) -> dict[str, Any] | None: + if contents is None: + return None + try: + value = json.loads(contents) + except json.JSONDecodeError as error: + raise ValueError(f"{label} is not valid JSON: {error}") from error + if not isinstance(value, dict): + raise ValueError(f"{label} must be a JSON object") + return value + + +def require(condition: bool, message: str, errors: list[str]) -> None: + if not condition: + errors.append(message) + + +def valid_version(value: Any) -> bool: + return isinstance(value, str) and re.fullmatch( + r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)", value + ) is not None + + +def version_increases(old: Any, new: Any) -> bool: + """Compare the numeric contract versions used by the matrix and corpus.""" + if not valid_version(old) or not valid_version(new): + return False + return tuple(map(int, new.split("."))) > tuple(map(int, old.split("."))) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base", required=True, help="base commit or ref to compare against") + args = parser.parse_args() + + try: + subprocess.run( + ["git", "rev-parse", "--verify", f"{args.base}^{{commit}}"], + check=True, + capture_output=True, + text=True, + ) + except subprocess.CalledProcessError: + print(f"OpenAPI compatibility guard: base revision {args.base!r} is unavailable", file=sys.stderr) + return 2 + + try: + old_matrix_text = git_show(args.base, MATRIX_PATH) + old_manifest_text = git_show(args.base, MANIFEST_PATH) + with open(MATRIX_PATH, encoding="utf-8") as matrix_file: + new_matrix_text = matrix_file.read() + with open(MANIFEST_PATH, encoding="utf-8") as manifest_file: + new_manifest_text = manifest_file.read() + with open(DOCS_PATH, encoding="utf-8") as docs_file: + new_docs_text = docs_file.read() + old_manifest = parse_manifest(old_manifest_text, "base manifest") + new_manifest = parse_manifest(new_manifest_text, "working manifest") + except (OSError, ValueError) as error: + print(f"OpenAPI compatibility guard: {error}", file=sys.stderr) + return 2 + + matrix_changed = old_matrix_text != new_matrix_text + manifest_changed = old_manifest_text != new_manifest_text + + errors: list[str] = [] + old_matrix_version = matrix_version(old_matrix_text) + new_matrix_version = matrix_version(new_matrix_text) + new_corpus_version = new_manifest.get("corpus_version") + new_impact = new_manifest.get("compatibility_impact") + require(bool(new_matrix_version), "support matrix must declare matrix_version", errors) + require(bool(new_corpus_version), "corpus manifest must declare corpus_version", errors) + require(valid_version(new_matrix_version), "matrix_version must be numeric SemVer", errors) + require(valid_version(new_corpus_version), "corpus_version must be numeric SemVer", errors) + require( + new_manifest.get("matrix_version") == new_matrix_version, + "manifest matrix_version must match support matrix", + errors, + ) + require( + isinstance(new_impact, str) and bool(new_impact.strip()), + "corpus manifest must include a non-empty compatibility_impact note", + errors, + ) + + if matrix_changed and old_matrix_text is not None: + require( + version_increases(old_matrix_version, new_matrix_version), + "support-matrix.yaml changed without a matrix_version bump (must increase SemVer)", + errors, + ) + + old_matrix_was_present = old_matrix_text is not None + if matrix_changed and old_matrix_was_present: + old_impact = old_manifest.get("compatibility_impact") if old_manifest else None + require( + new_impact != old_impact, + "support matrix changed without updating compatibility_impact", + errors, + ) + + # The published documentation states concrete matrix/corpus versions; + # a stale or mixed version in docs must not outlive the machine source. + matrix_decls = re.findall( + r"матриц[аы] версии `([0-9]+\.[0-9]+\.[0-9]+)`", new_docs_text, re.IGNORECASE + ) + corpus_decls = re.findall( + r"corpus version — `([0-9]+\.[0-9]+\.[0-9]+)`", new_docs_text, re.IGNORECASE + ) + require( + bool(matrix_decls) and all(version == new_matrix_version for version in matrix_decls), + f"{DOCS_PATH} matrix version declaration must state matrix_version {new_matrix_version}", + errors, + ) + require( + bool(corpus_decls) and all(version == new_corpus_version for version in corpus_decls), + f"{DOCS_PATH} corpus version declaration must state corpus_version {new_corpus_version}", + errors, + ) + require( + bool(matrix_decls) and bool(corpus_decls) + and all(version == new_matrix_version for version in matrix_decls) + and all(version == new_corpus_version for version in corpus_decls), + f"{DOCS_PATH} must state the current support matrix and corpus versions", + errors, + ) + if old_manifest is not None: + if old_manifest != new_manifest: + require( + version_increases(old_manifest.get("corpus_version"), new_corpus_version), + "corpus manifest changed without a corpus_version bump (must increase SemVer)", + errors, + ) + require( + new_impact != old_manifest.get("compatibility_impact"), + "corpus manifest changed without updating compatibility_impact", + errors, + ) + if errors: + print("OpenAPI compatibility guard failed:", file=sys.stderr) + for error in errors: + print(f"- {error}", file=sys.stderr) + return 1 + + print( + "OpenAPI compatibility guard passed: " + f"matrix {old_matrix_version or ''} -> {new_matrix_version}; " + f"corpus {old_manifest.get('corpus_version') if old_manifest else ''} -> {new_corpus_version}" + ) + if not matrix_changed and not manifest_changed: + print("OpenAPI compatibility guard: matrix and corpus manifest are unchanged") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/deploy-community.sh b/scripts/deploy-community.sh index dd477f0..29ce885 100755 --- a/scripts/deploy-community.sh +++ b/scripts/deploy-community.sh @@ -153,15 +153,40 @@ validate_backup_migration() { return 1 fi + shadow_target_file="$(mktemp)" + if ! timeout 2m docker run --rm \ + "$admin_image" crank-migrate plan --target-version > "$shadow_target_file"; then + rm -f "$shadow_target_file" + echo "Migration plan target version could not be read" >&2 + return 1 + fi + if ! IFS= read -r shadow_target_version < "$shadow_target_file" \ + || ! printf '%s\n' "$shadow_target_version" | cmp -s - "$shadow_target_file"; then + rm -f "$shadow_target_file" + echo "Migration plan returned an invalid target version" >&2 + return 1 + fi + rm -f "$shadow_target_file" + case "$shadow_target_version" in + ''|0*|*[!0-9]*) + echo "Migration plan returned an invalid target version" >&2 + return 1 + ;; + esac + shadow_id="crank-migration-check-$$" shadow_network="${shadow_id}-network" shadow_postgres="${shadow_id}-postgres" shadow_password="crank-shadow-migration-password" shadow_admin="crank_shadow_admin" shadow_user="crank_shadow_owner" + shadow_preflight_file="" shadow_cleanup() { docker rm -fv "$shadow_postgres" >/dev/null 2>&1 || true docker network rm "$shadow_network" >/dev/null 2>&1 || true + if [ -n "$shadow_preflight_file" ]; then + rm -f "$shadow_preflight_file" + fi } trap shadow_cleanup EXIT trap 'exit 130' HUP INT TERM @@ -206,10 +231,18 @@ validate_backup_migration() { timeout 10m docker run --rm --network "$shadow_network" \ -e CRANK_DATABASE_URL="$shadow_database_url" \ "$admin_image" crank-migrate apply - shadow_preflight="$(timeout 2m docker run --rm --network "$shadow_network" \ + shadow_preflight_file="$(mktemp)" + if ! timeout 2m docker run --rm --network "$shadow_network" \ -e CRANK_DATABASE_URL="$shadow_database_url" \ - "$admin_image" crank-migrate preflight)" - printf '%s\n' "$shadow_preflight" | grep -Fxq '{"status":"current","version":13}' + "$admin_image" crank-migrate preflight > "$shadow_preflight_file"; then + echo "Shadow migration preflight command failed" >&2 + return 1 + fi + if ! printf '%s\n' "{\"status\":\"current\",\"version\":${shadow_target_version}}" \ + | cmp -s - "$shadow_preflight_file"; then + echo "Shadow migration preflight did not confirm the target schema" >&2 + return 1 + fi shadow_cleanup trap - EXIT HUP INT TERM diff --git a/scripts/validate-capability-run.py b/scripts/validate-capability-run.py index f3e0e76..96b2299 100644 --- a/scripts/validate-capability-run.py +++ b/scripts/validate-capability-run.py @@ -30,6 +30,10 @@ def reject_duplicates(pairs: list[tuple[str, Any]]) -> dict[str, Any]: return result +def reject_constant(_value: str) -> Any: + raise ValidationError('/', 'invalid JSON constant') + + def load_json(path: Path) -> Any: try: raw = path.read_bytes() @@ -38,7 +42,11 @@ def load_json(path: Path) -> Any: if len(raw) > MAX_INPUT_BYTES: raise ValidationError('/', 'input exceeds 64 KiB') try: - return json.loads(raw.decode('utf-8'), object_pairs_hook=reject_duplicates) + return json.loads( + raw.decode('utf-8'), + object_pairs_hook=reject_duplicates, + parse_constant=reject_constant, + ) except (UnicodeDecodeError, json.JSONDecodeError, RecursionError) as error: raise ValidationError('/', 'invalid JSON') from error diff --git a/tests/unit/test_check_openapi_matrix_compatibility.py b/tests/unit/test_check_openapi_matrix_compatibility.py new file mode 100644 index 0000000..16479a1 --- /dev/null +++ b/tests/unit/test_check_openapi_matrix_compatibility.py @@ -0,0 +1,271 @@ +import json +import subprocess +import tempfile +import unittest +from pathlib import Path + + +SCRIPT = Path(__file__).parents[2] / "scripts" / "check-openapi-matrix-compatibility.py" + +MATRIX_PATH = "crates/crank-import/tests/support-matrix.yaml" +MANIFEST_PATH = "crates/crank-import/tests/corpus/manifest.json" +DOCS_PATH = "docs/openapi-import.md" + +BASE_MATRIX = """matrix_version: '1.0.0' +constructs: + - id: baseline +""" +BASE_DOC = "Матрица версии `1.0.0`; corpus version — `1.0.0`.\n" + + +def base_manifest() -> dict: + return { + "corpus_version": "1.0.0", + "matrix_version": "1.0.0", + "compatibility_impact": "initial contract", + "documents": [ + { + "id": "rep-00", + "file": "representative-00.json", + "sha256": "a" * 64, + "origin": "synthetic:cc0", + "license": "CC0-1.0", + "specification_version": "OpenAPI 3.0", + "classification": "representative", + "expected_preview": {}, + } + ], + } + + +class OpenApiMatrixCompatibilityGuardTests(unittest.TestCase): + def setUp(self) -> None: + self._directory = tempfile.TemporaryDirectory() + self.root = Path(self._directory.name) + subprocess.run( + ["git", "init", "-q"], cwd=self.root, check=True, capture_output=True + ) + for relative in (MATRIX_PATH, MANIFEST_PATH, DOCS_PATH): + target = self.root / relative + target.parent.mkdir(parents=True, exist_ok=True) + (self.root / MATRIX_PATH).write_text(BASE_MATRIX, encoding="utf-8") + (self.root / MANIFEST_PATH).write_text( + json.dumps(base_manifest()), encoding="utf-8" + ) + (self.root / DOCS_PATH).write_text(BASE_DOC, encoding="utf-8") + self.commit("base") + self.base = self.head() + + def tearDown(self) -> None: + self._directory.cleanup() + + def self_git(self, *args: str) -> None: + subprocess.run( + ["git", "-c", "user.email=guard@test", "-c", "user.name=guard", *args], + cwd=self.root, + check=True, + capture_output=True, + ) + + def commit(self, message: str) -> None: + self.self_git("add", "-A") + self.self_git("commit", "-m", message) + + def head(self) -> str: + result = subprocess.run( + ["git", "rev-parse", "HEAD"], + cwd=self.root, + check=True, + capture_output=True, + text=True, + ) + return result.stdout.strip() + + def run_guard(self) -> subprocess.CompletedProcess[str]: + return subprocess.run( + ["python3", str(SCRIPT), "--base", self.base], + cwd=self.root, + text=True, + capture_output=True, + check=False, + ) + + def test_rejects_docs_only_edit_with_stale_version(self) -> None: + # matrix and manifest are untouched relative to the base, but a docs + # edit that pins a stale contract version must still be rejected + (self.root / DOCS_PATH).write_text( + "Матрица версии `0.9.0`; corpus version — `0.9.0`.\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn( + "must state the current support matrix and corpus versions", result.stderr + ) + + def test_rejects_docs_with_reversed_matrix_and_corpus_versions(self) -> None: + (self.root / MATRIX_PATH).write_text( + BASE_MATRIX.replace("'1.0.0'", "'1.1.0'") + " - id: addition\n", + encoding="utf-8", + ) + manifest = base_manifest() + manifest["matrix_version"] = "1.1.0" + manifest["corpus_version"] = "1.0.1" + manifest["compatibility_impact"] = "bumped release" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + (self.root / DOCS_PATH).write_text( + "Матрица версии `1.0.1`; corpus version — `1.1.0`.\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn( + "matrix version declaration must state matrix_version 1.1.0", result.stderr + ) + + def test_rejects_contract_change_without_version_bump_on_existing_base(self) -> None: + # The base already contains the contract: appending a matrix row must be + # rejected even though the versions and impact note stay untouched. + (self.root / MATRIX_PATH).write_text( + BASE_MATRIX + " - id: addition\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("changed without a matrix_version bump", result.stderr) + self.assertIn("without updating compatibility_impact", result.stderr) + + def test_rejects_matrix_version_downgrade(self) -> None: + (self.root / MATRIX_PATH).write_text( + BASE_MATRIX.replace("'1.0.0'", "'0.9.0'") + " - id: addition\n", + encoding="utf-8", + ) + manifest = base_manifest() + manifest["matrix_version"] = "0.9.0" + manifest["compatibility_impact"] = "downgraded matrix" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + (self.root / DOCS_PATH).write_text( + "Матрица версии `0.9.0`; corpus version — `1.0.0`.\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("matrix_version bump (must increase SemVer)", result.stderr) + + def test_rejects_manifest_change_without_corpus_bump_on_existing_base(self) -> None: + manifest = base_manifest() + manifest["documents"][0]["file"] = "representative-99.json" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("without a corpus_version bump", result.stderr) + self.assertIn("without updating compatibility_impact", result.stderr) + + def test_rejects_corpus_version_downgrade(self) -> None: + manifest = base_manifest() + manifest["corpus_version"] = "0.9.0" + manifest["compatibility_impact"] = "downgraded corpus" + manifest["documents"][0]["file"] = "representative-99.json" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + (self.root / DOCS_PATH).write_text( + "Матрица версии `1.0.0`; corpus version — `0.9.0`.\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("corpus_version bump (must increase SemVer)", result.stderr) + + def test_rejects_stale_versions_in_published_docs(self) -> None: + matrix = BASE_MATRIX.replace("'1.0.0'", "'1.1.0'") + " - id: addition\n" + (self.root / MATRIX_PATH).write_text(matrix, encoding="utf-8") + manifest = base_manifest() + manifest["matrix_version"] = "1.1.0" + manifest["corpus_version"] = "1.0.1" + manifest["compatibility_impact"] = "matrix 1.1.0 release" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + # docs still pin the previous `1.0.0` release + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("must state the current support matrix and corpus versions", result.stderr) + + def test_rejects_docs_citing_versions_missing_from_release(self) -> None: + (self.root / MATRIX_PATH).write_text( + BASE_MATRIX + " - id: addition\n", encoding="utf-8" + ) + manifest = base_manifest() + manifest["matrix_version"] = "1.1.0" + manifest["corpus_version"] = "1.1.0" + manifest["compatibility_impact"] = "matrix 1.1.0 release" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + (self.root / DOCS_PATH).write_text( + "Матрица версии `1.1.0`; corpus version — `0.9.0`.\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("must state the current support matrix and corpus versions", result.stderr) + + def test_allows_other_semver_in_docs(self) -> None: + (self.root / DOCS_PATH).write_text( + "Матрица версии `1.0.0`; corpus version — `1.0.0`. OpenAPI `3.1.0`.\n", + encoding="utf-8", + ) + result = self.run_guard() + self.assertEqual(result.returncode, 0, result.stderr) + + def test_rejects_conflicting_second_version_declaration(self) -> None: + (self.root / DOCS_PATH).write_text( + BASE_DOC + "Матрица версии `0.9.0`; corpus version — `0.9.0`.\n", + encoding="utf-8", + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("must state the current support matrix and corpus versions", result.stderr) + + def test_rejects_leading_zero_in_contract_version(self) -> None: + (self.root / MATRIX_PATH).write_text( + BASE_MATRIX.replace("'1.0.0'", "'01.0.1'") + " - id: addition\n", + encoding="utf-8", + ) + manifest = base_manifest() + manifest["matrix_version"] = "01.0.1" + manifest["compatibility_impact"] = "invalid version" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + (self.root / DOCS_PATH).write_text( + "Матрица версии `01.0.1`; corpus version — `1.0.0`.\n", + encoding="utf-8", + ) + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("matrix_version bump (must increase SemVer)", result.stderr) + + def test_rejects_manifest_metadata_change_without_bump(self) -> None: + manifest = base_manifest() + manifest["canonical_digest_definition"] = "changed contract" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("without a corpus_version bump", result.stderr) + + def test_rejects_corpus_version_only_downgrade(self) -> None: + manifest = base_manifest() + manifest["corpus_version"] = "0.9.0" + manifest["compatibility_impact"] = "downgrade" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + result = self.run_guard() + self.assertEqual(result.returncode, 1, result.stderr) + self.assertIn("corpus_version bump (must increase SemVer)", result.stderr) + + def test_passes_consistent_bumped_release_with_updated_docs(self) -> None: + (self.root / MATRIX_PATH).write_text( + BASE_MATRIX.replace("'1.0.0'", "'1.1.0'") + " - id: addition\n", + encoding="utf-8", + ) + manifest = base_manifest() + manifest["matrix_version"] = "1.1.0" + manifest["corpus_version"] = "1.0.1" + manifest["compatibility_impact"] = "matrix 1.1.0 release" + (self.root / MANIFEST_PATH).write_text(json.dumps(manifest), encoding="utf-8") + (self.root / DOCS_PATH).write_text( + "Матрица версии `1.1.0`; corpus version — `1.0.1`.\n", encoding="utf-8" + ) + result = self.run_guard() + self.assertEqual(result.returncode, 0, result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/test_deploy_community.py b/tests/unit/test_deploy_community.py new file mode 100644 index 0000000..2b638c2 --- /dev/null +++ b/tests/unit/test_deploy_community.py @@ -0,0 +1,159 @@ +import json +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[2] +DEPLOY = ROOT / "scripts" / "deploy-community.sh" + + +class DeployCommunityMigrationGuardTests(unittest.TestCase): + def run_deploy( + self, + *, + version: int = 15, + preflight: str | None = None, + apply_success: bool = True, + plan: str | None = None, + ) -> tuple[subprocess.CompletedProcess[str], list[list[str]], Path]: + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + root = Path(temporary.name) + bin_dir = root / "bin" + bin_dir.mkdir() + tmp_dir = root / "tmp" + tmp_dir.mkdir() + log = root / "docker.jsonl" + if preflight is None: + preflight = json.dumps( + {"status": "current", "version": version}, separators=(",", ":") + ) + (root / ".env").write_text( + "POSTGRES_HOST=postgres\nPOSTGRES_PASSWORD=test-password\nCRANK_ADMIN_API_IMAGE=crank:test\n", + encoding="utf-8", + ) + (root / "docker-compose.yml").write_text("services: {}\n", encoding="utf-8") + + docker = r'''#!/usr/bin/env python3 +import json, os, pathlib, sys +args = sys.argv[1:] +with open(os.environ["FAKE_DOCKER_LOG"], "a", encoding="utf-8") as stream: + stream.write(json.dumps(args) + "\n") +if args[:2] == ["compose", "config"] and args[2:3] == ["--services"]: + print("admin-api") +elif args[:2] == ["compose", "ps"]: + pass +elif args[:2] == ["compose", "up"]: + pass +elif args[:2] == ["network", "create"]: + print(args[2]) +elif args[:2] == ["network", "rm"] or args[:2] == ["rm", "-fv"]: + pass +elif args[:1] == ["logs"]: + print("PostgreSQL init process complete") +elif args[:1] == ["exec"] and "pg_isready" in args: + pass +elif args[:1] == ["exec"] and "psql" in args: + pass +elif args[:1] == ["run"]: + if "pg_dump" in args: + mount = next(value for value in args if value.startswith("/") and ":/backup" in value) + pathlib.Path(mount.split(":/backup", 1)[0], "postgres.dump").write_bytes(b"fake dump") + elif "crank-migrate" in args: + command = args[args.index("crank-migrate") + 1:] + if command == ["apply"]: + sys.exit(0 if os.environ.get("FAKE_APPLY_SUCCESS") == "1" else 1) + if command == ["plan", "--target-version"]: + sys.stdout.write(os.environ["FAKE_PLAN_OUTPUT"] + "\n") + elif command == ["preflight"]: + sys.stdout.write(os.environ["FAKE_PREFLIGHT"] + "\n") +elif args[:1] == ["inspect"] and args[1:2] == ["--format"]: + print("") +sys.exit(0) +''' + curl = "#!/bin/sh\nexit 0\n" + sleep = "#!/bin/sh\nexit 0\n" + for name, contents in (("docker", docker), ("curl", curl), ("sleep", sleep)): + path = bin_dir / name + path.write_text(contents, encoding="utf-8") + path.chmod(0o755) + + env = os.environ.copy() + env.update( + { + "PATH": f"{bin_dir}:{env['PATH']}", + "FAKE_DOCKER_LOG": str(log), + "FAKE_PREFLIGHT": preflight, + "FAKE_APPLY_SUCCESS": "1" if apply_success else "0", + "FAKE_PLAN_OUTPUT": plan if plan is not None else str(version), + "TMPDIR": str(tmp_dir), + } + ) + result = subprocess.run( + ["sh", str(DEPLOY), str(root)], + text=True, + capture_output=True, + env=env, + check=False, + ) + calls = [json.loads(line) for line in log.read_text(encoding="utf-8").splitlines()] + return result, calls, root + + def test_current_v13_shadow_migration_allows_compose_up(self) -> None: + result, calls, root = self.run_deploy(version=13) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue(any(call[:2] == ["compose", "up"] for call in calls)) + self.assertTrue(any(call[:2] == ["rm", "-fv"] for call in calls)) + self.assertTrue(any(call[:2] == ["network", "rm"] for call in calls)) + self.assertEqual(list((root / "tmp").iterdir()), []) + + def test_current_v15_shadow_migration_allows_compose_up(self) -> None: + result, calls, root = self.run_deploy(version=15) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue(any(call[:2] == ["compose", "up"] for call in calls)) + self.assertEqual(list((root / "tmp").iterdir()), []) + + def test_future_current_shadow_migration_allows_compose_up(self) -> None: + result, calls, root = self.run_deploy(version=27) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue(any(call[:2] == ["compose", "up"] for call in calls)) + self.assertEqual(list((root / "tmp").iterdir()), []) + + def test_invalid_preflight_responses_and_failed_apply_never_start_compose(self) -> None: + cases = ( + ("", True), + ("not-json", True), + ('noise\n{"status":"current","version":15}', True), + ('{"status":"current","version":15}\nnoise', True), + ('{"status":"current","version":15}\n', True), + ('{"status":"applied","version":15}', True), + ('{"status":"current","version":13}', True), + ('{"status":"current","version":15,"extra":true}', True), + ('{"status":"current","version":"15"}', True), + ('{"status":"current","version":15}', False), + ) + for preflight, apply_success in cases: + with self.subTest(preflight=preflight, apply_success=apply_success): + result, calls, root = self.run_deploy( + version=15, preflight=preflight, apply_success=apply_success + ) + self.assertNotEqual(result.returncode, 0) + self.assertFalse(any(call[:2] == ["compose", "up"] for call in calls)) + self.assertTrue(any(call[:2] == ["rm", "-fv"] for call in calls)) + self.assertTrue(any(call[:2] == ["network", "rm"] for call in calls)) + self.assertEqual(list((root / "tmp").iterdir()), []) + + def test_invalid_target_version_is_rejected_before_shadow_database(self) -> None: + for target in ("", "0", "-1", "version", "15\n16", "15\n", "015"): + with self.subTest(target=target): + result, calls, _ = self.run_deploy(version=15, plan=target) + self.assertNotEqual(result.returncode, 0) + self.assertFalse(any(call[:2] == ["compose", "up"] for call in calls)) + self.assertFalse(any(call[:2] == ["network", "create"] for call in calls)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/unit/test_validate_capability_run.py b/tests/unit/test_validate_capability_run.py index c593117..b8bddff 100644 --- a/tests/unit/test_validate_capability_run.py +++ b/tests/unit/test_validate_capability_run.py @@ -81,6 +81,36 @@ class CapabilityRunValidatorTests(unittest.TestCase): self.assertNotEqual(result.returncode, 0) self.assertIn("duplicate JSON key", result.stderr) + def test_nonfinite_json_constants_are_rejected(self) -> None: + for constant, token in ( + (float("nan"), "NaN"), + (float("inf"), "Infinity"), + (float("-inf"), "-Infinity"), + ): + with self.subTest(constant=token): + value = candidate() + value["summary"] = {"passed": 1, "anomaly": constant} + result = self.validate(json.dumps(value)) + self.assertNotEqual(result.returncode, 0) + self.assertIn("INVALID_CAPABILITY_RUN", result.stderr) + self.assertIn("invalid JSON constant", result.stderr) + self.assertNotIn("Traceback", result.stderr) + self.assertNotIn(token, result.stderr) + + def test_deep_malformed_json_is_rejected_without_raw_input(self) -> None: + malformed_deep_value = "[" * 1_500 + "RAW_DEEP_SENTINEL" + encoded = json.dumps(candidate()).replace( + '"summary": {"passed": 1}', + f'"summary": {{"passed": 1, "deep": {malformed_deep_value}}}', + ) + self.assertLess(len(encoded.encode("utf-8")), 65_536) + result = self.validate(encoded) + self.assertNotEqual(result.returncode, 0) + self.assertIn("INVALID_CAPABILITY_RUN", result.stderr) + self.assertIn("invalid JSON", result.stderr) + self.assertNotIn("Traceback", result.stderr) + self.assertNotIn("RAW_DEEP_SENTINEL", result.stderr) + def test_oversized_candidate_is_rejected(self) -> None: result = self.validate(b" " * (65_536 + 1)) self.assertNotEqual(result.returncode, 0)