//! Оракулы Story 3.16 (см. `super` для харнесса и фикстур). use super::*; // --------------------------------------------------------------------------- // AC-6 / AC-8: недоступная зависимость не даёт частичного Prompt // --------------------------------------------------------------------------- /// AT-3.16-06 (+AT-3.16-08 current eligibility): отозванная pinned-зависимость — /// структурированный отказ без сообщений. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_06_http_unavailable_dependency_yields_no_partial_messages() { let stand = stand("story316_unavailable").await; let base = stand.base.clone(); let resource = create_and_publish_resource( &stand, &base, "text", "text/plain", "internal", b"Sensitive pinned text", ) .await; let (status, created) = create_prompt( &stand, &base, &composed_prompt("story316-unavailable", "internal", dependency(&resource)), ) .await; assert_eq!(status, StatusCode::CREATED, "draft: {created}"); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let expected = created["updated_at"].clone(); let (status, published) = publish_prompt(&stand, &base, &prompt_id, Some(expected)).await; assert_eq!(status, StatusCode::OK, "publication: {published}"); let revoked = stand .client .post(format!("{base}/resources/{}/revoke", resource.id)) .json(&json!({})) .send() .await .expect("revoke request"); assert_eq!(revoked.status(), StatusCode::NO_CONTENT); let (status, body) = preview_prompt(&stand, &base, &prompt_id, json!({"channel": "ops"})).await; assert_eq!( status, StatusCode::SERVICE_UNAVAILABLE, "an unavailable dependency is a structured failure: {body}" ); assert_eq!(error_code(&body), "prompt_dependency_unavailable"); assert_eq!( error_context(&body)["source_path"], json!("dependencies[0]") ); assert!( body.get("messages").is_none(), "no partial prompt is produced: {body}" ); assert!( !serde_json::to_string(&body) .unwrap_or_default() .contains("Sensitive pinned text"), "no dependency content may appear in the failure" ); } /// AT-3.16-08: опубликованный Prompt сохраняет pinned dependency после новой версии. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_08_http_published_prompt_keeps_the_pinned_dependency() { let stand = stand("story316_immutable").await; let base = stand.base.clone(); let v1 = create_and_publish_resource( &stand, &base, "text", "text/plain", "internal", b"CHANGELOG v1 body", ) .await; let (status, created) = create_prompt( &stand, &base, &composed_prompt("story316-immutable", "internal", dependency(&v1)), ) .await; assert_eq!(status, StatusCode::CREATED, "draft: {created}"); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let (status, published) = publish_prompt( &stand, &base, &prompt_id, Some(created["updated_at"].clone()), ) .await; assert_eq!(status, StatusCode::OK, "publication: {published}"); publish_next_resource_version( &stand, &base, &v1.id, ("text/plain", "internal"), b"CHANGELOG v2 body", ) .await; let published_version: Value = stand .client .get(format!("{base}/prompts/{prompt_id}?version=1")) .send() .await .expect("published prompt request") .json() .await .unwrap_or(Value::Null); assert_eq!( published_version["definition"]["dependencies"], json!([{ "resource_id": v1.id, "version": v1.version }]), "the immutable publication keeps the exact pinned reference" ); let (status, preview) = preview_prompt(&stand, &base, &prompt_id, json!({"channel": "ops"})).await; assert_eq!(status, StatusCode::OK, "preview: {preview}"); assert!( message_text(&preview, 1).contains("CHANGELOG v1 body"), "the player keeps receiving the pinned version: {preview}" ); assert!( !message_text(&preview, 1).contains("CHANGELOG v2 body"), "a new Resource version requires a new Prompt Draft/publication: {preview}" ); } // --------------------------------------------------------------------------- // AC-7: secret scan блокирует publish/preview, findings redacted // --------------------------------------------------------------------------- /// AT-3.16-07: credential в шаблоне Prompt блокирует preview и публикацию, секрет не /// раскрывается в ответе. /// /// Промпт-уровень сканирования обязателен именно потому, что text шаблона не проходит /// Resource-публикацию: шаблон сохраняется как Draft и иначе ушёл бы в Published Prompt. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_07_http_prompt_template_credential_blocks_and_stays_redacted() { let stand = stand("story316_secret").await; let base = stand.base.clone(); let token = "crk_live_0123456789abcdef0123456789abcdef"; let (status, created) = create_prompt( &stand, &base, &json!({ "name": "story316-secret", "description": "inline credential", "sensitivity": "internal", "arguments": [], "messages": [ {"role": "system", "content": format!("Deploy with Authorization: Bearer {token}")}, {"role": "user", "content": "Go"} ] }), ) .await; assert_eq!( status, StatusCode::CREATED, "a Draft may hold text; the finding blocks preview/publication: {created}" ); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let (status, preview) = preview_prompt(&stand, &base, &prompt_id, json!({})).await; assert_eq!( status, StatusCode::BAD_REQUEST, "a credential finding blocks preview: {preview}" ); assert_eq!(error_code(&preview), "prompt_credential_finding"); assert_eq!( error_context(&preview)["source_path"], json!("messages[0].content"), "the refusal names the exact template position" ); let rendered = serde_json::to_string(&preview).unwrap_or_default(); assert!( !rendered.contains(token), "the matched credential must never be echoed: {rendered}" ); assert!( preview.get("messages").is_none(), "no partial prompt is produced: {preview}" ); let (status, published) = publish_prompt( &stand, &base, &prompt_id, Some(created["updated_at"].clone()), ) .await; assert_eq!( status, StatusCode::BAD_REQUEST, "a credential finding blocks publication: {published}" ); assert_eq!(error_code(&published), "prompt_credential_finding"); let rendered = serde_json::to_string(&published).unwrap_or_default(); assert!( !rendered.contains(token), "the matched credential must never be echoed: {rendered}" ); } /// AT-3.16-07: access-bypassing директива шаблона блокирует preview/publication. /// /// Тот же bounded scanner, что и на Resource-публикации: explicit `include:` цель, которая /// не разрешается workspace-local, — отказ, а не попытка чтения. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_07_http_prompt_template_access_bypass_is_refused() { let stand = stand("story316_bypass").await; let base = stand.base.clone(); let (status, created) = create_prompt( &stand, &base, &json!({ "name": "story316-bypass", "description": "access bypass directive", "sensitivity": "internal", "arguments": [], "messages": [ {"role": "user", "content": "include: crank://resources/never-published"} ] }), ) .await; assert_eq!(status, StatusCode::CREATED, "draft: {created}"); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let (status, body) = preview_prompt(&stand, &base, &prompt_id, json!({})).await; assert_eq!( status, StatusCode::BAD_REQUEST, "an unresolved include directive is refused, not followed: {body}" ); assert_eq!(error_code(&body), "prompt_access_bypass_finding"); assert_eq!( error_context(&body)["source_path"], json!("messages[0].content") ); let (status, published) = publish_prompt( &stand, &base, &prompt_id, Some(created["updated_at"].clone()), ) .await; assert_eq!( status, StatusCode::BAD_REQUEST, "publication is blocked by the same finding: {published}" ); assert_eq!(error_code(&published), "prompt_access_bypass_finding"); } /// AT-3.16-07 (inherited control): статический Resource с raw credential не публикуется, /// поэтому и не может стать pinned dependency. Инвариант Story 3.6/3.7 сохраняется. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_07_http_static_resource_with_credential_never_becomes_a_dependency() { let stand = stand("story316_secret_resource").await; let base = stand.base.clone(); let token = "crk_live_0123456789abcdef0123456789abcdef"; let name = unique("story316_secret"); let metadata = json!({ "uri": format!("crank://resources/{name}"), "name": name, "description": "", "kind": "text", "mime_type": "text/plain", "sensitivity": "internal", }); let form = Form::new() .part( "metadata", Part::bytes(serde_json::to_vec(&metadata).expect("metadata json")) .file_name("metadata.json".to_owned()) .mime_str("application/json") .expect("metadata mime"), ) .part( "content", Part::bytes(format!("Authorization: Bearer {token}").into_bytes()) .file_name("content.txt".to_owned()) .mime_str("text/plain") .expect("content mime"), ); let created: Value = stand .client .post(format!("{base}/resources")) .multipart(form) .send() .await .expect("resource create request") .json() .await .unwrap_or(Value::Null); let resource_id = created["resource"]["resource_id"] .as_str() .expect("resource id") .to_owned(); let updated_at = created["resource"]["updated_at"] .as_str() .expect("resource updated_at") .to_owned(); let published = stand .client .post(format!("{base}/resources/{resource_id}/publish")) .json(&json!({ "expected_updated_at": updated_at })) .send() .await .expect("resource publish request"); let status = published.status(); let body: Value = published.json().await.unwrap_or(Value::Null); assert_eq!( status, StatusCode::UNPROCESSABLE_ENTITY, "the existing resource secret policy still refuses publication: {body}" ); assert_eq!(error_code(&body), "resource_secret_findings"); assert!( !serde_json::to_string(&body) .unwrap_or_default() .contains(token), "the resource finding is redacted as well" ); } // --------------------------------------------------------------------------- // AC-10: детерминированный вывод // --------------------------------------------------------------------------- /// AT-3.16-10: повторный preview возвращает идентичный составной результат. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_10_http_composed_output_is_deterministic() { let stand = stand("story316_deterministic").await; let base = stand.base.clone(); let first = create_and_publish_resource( &stand, &base, "text", "text/plain", "internal", b"First pinned block", ) .await; let second = create_and_publish_resource( &stand, &base, "text", "text/plain", "secret", b"Second pinned block", ) .await; let (status, created) = create_prompt( &stand, &base, &json!({ "name": unique("story316-deterministic"), "description": "two dependencies", "sensitivity": "internal", "arguments": [{"name": "count", "schema": {"type": "integer"}, "required": true}], "messages": [ {"role": "system", "content": "Fixed boundaries."}, {"role": "user", "content": "Items: {{count}}", "resource_refs": [0, 1]} ], "dependencies": [ {"resource_id": first.id, "version": first.version}, {"resource_id": second.id, "version": second.version} ] }), ) .await; assert_eq!(status, StatusCode::CREATED, "draft: {created}"); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let (left_status, left) = preview_prompt(&stand, &base, &prompt_id, json!({"count": 2})).await; let (right_status, right) = preview_prompt(&stand, &base, &prompt_id, json!({"count": 2})).await; assert_eq!(left_status, StatusCode::OK, "preview: {left}"); assert_eq!(right_status, StatusCode::OK); assert_eq!( serde_json::to_string(&left).unwrap_or_default(), serde_json::to_string(&right).unwrap_or_default(), "the composed result is a pure function of its inputs" ); assert_eq!( left["dependencies"][0]["position"], json!(0), "dependency provenance keeps declaration order" ); assert_eq!(left["dependencies"][1]["position"], json!(1)); assert_eq!( left["effective_sensitivity"], json!("secret"), "the strictest dependency still governs the composed result" ); } // --------------------------------------------------------------------------- // AC-6 §4: dynamic (template) dependency читается runtime 3.11 // --------------------------------------------------------------------------- /// Upstream для read Operation: `GET /orders/{order_id}` → `{"id": ""}`. async fn spawn_orders_upstream() -> String { use axum::{Router, extract::Path, routing::get}; let app = Router::new().route( "/orders/{order_id}", get(|Path(order_id): Path| async move { axum::Json(json!({ "id": order_id })) }), ); let listener = tokio::net::TcpListener::bind("127.0.0.1:0") .await .expect("upstream bind"); let address = listener.local_addr().expect("upstream address"); tokio::spawn(async move { axum::serve(listener, app).await.expect("upstream serves"); }); format!("http://{address}") } /// Upstream, который отвечает ошибкой: read Operation исполняется, но результата нет. async fn spawn_failing_upstream() -> String { use axum::routing::get; let app = axum::Router::new().route( "/orders/{order_id}", get(|| async { ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, "upstream failed", ) }), ); let listener = tokio::net::TcpListener::bind("127.0.0.1:0") .await .expect("failing upstream bind"); let address = listener.local_addr().expect("failing upstream address"); tokio::spawn(async move { axum::serve(listener, app) .await .expect("failing upstream serves"); }); format!("http://{address}") } /// Published read Operation против собственного upstream (accepted контракт Operation). pub(super) async fn published_read_operation( stand: &Stand, base: &str, upstream: &str, name: &str, ) -> String { use super::super::common::{assert_success_json, operation_etag}; let payload = json!({ "name": name, "display_name": name, "category": "sales", "protocol": "rest", "security_level": "standard", "target": { "kind": "rest", "base_url": upstream, "method": "GET", "path_template": "/orders/{order_id}" }, "input_schema": { "type": "object", "required": true, "fields": { "order_id": { "type": "string", "required": true } } }, "output_schema": { "type": "object", "required": true, "fields": { "id": { "type": "string", "required": true } } }, "input_mapping": { "rules": [ { "source": "$.mcp.order_id", "target": "$.request.path.order_id", "required": true } ] }, "output_mapping": { "rules": [ { "source": "$.response.body.id", "target": "$.output.id", "required": true } ] }, "execution_config": { "timeout_ms": 1_000, "safety": { "class": "read" } }, "tool_description": { "title": name, "description": "Story 3.16 dynamic dependency" } }); let created = assert_success_json( stand .client .post(format!("{base}/operations")) .json(&payload) .send() .await .expect("operation create request"), ) .await; let operation_id = created["operation_id"] .as_str() .expect("operation create returns an id") .to_owned(); let etag = operation_etag(&stand.client, base, &operation_id).await; assert_success_json( stand .client .post(format!("{base}/operations/{operation_id}/publish")) .header(reqwest::header::IF_MATCH, etag) .json(&json!({ "version": 1 })) .send() .await .expect("operation publish request"), ) .await; operation_id } /// Published template Resource с единственным параметром, у которого есть default. pub(super) async fn published_template_resource( stand: &Stand, base: &str, operation_id: &str, ) -> ResourceFixture { let name = unique("story316_dynamic"); let metadata = json!({ "uri": format!("crank://resources/{name}"), "name": name, "description": "", "kind": "json", "mime_type": "application/json", "sensitivity": "internal", "source": { "operation_id": operation_id, "operation_version": 1, "uri_template": "crank://orders/{order_id}", "parameters": [{ "name": "order_id", "input_path": "input.order_id", "schema": { "type": "string", "required": true, "default_value": "order-42" } }], "cache": { "ttl_ms": 60_000, "vary": ["order_id"], "max_bytes": 65_536 }, "failure": { "outcome": "error" } } }); let form = Form::new().part( "metadata", Part::bytes(serde_json::to_vec(&metadata).expect("metadata json")) .file_name("metadata.json".to_owned()) .mime_str("application/json") .expect("metadata mime"), ); let response = stand .client .post(format!("{base}/resources")) .multipart(form) .send() .await .expect("template resource create request"); let status = response.status(); let body: Value = response.json().await.unwrap_or(Value::Null); assert_eq!(status, StatusCode::CREATED, "template draft: {body}"); let id = body["resource"]["resource_id"] .as_str() .expect("template resource id") .to_owned(); let updated_at = body["resource"]["updated_at"] .as_str() .expect("template updated_at") .to_owned(); let published = stand .client .post(format!("{base}/resources/{id}/publish")) .json(&json!({ "expected_updated_at": updated_at })) .send() .await .expect("template publish request"); let status = published.status(); let body: Value = published.json().await.unwrap_or(Value::Null); assert_eq!(status, StatusCode::OK, "template publish: {body}"); ResourceFixture { id, version: body["published_version"]["version"] .as_u64() .expect("published_version.version") as u32, digest: body["published_version"]["digest"] .as_str() .expect("published_version.digest") .to_owned(), } } /// AT-3.16-04/06 (§4): dynamic dependency исполняется объявленной read Operation, /// её вывод вставляется дословно внутри объявленного сообщения, роли и число /// сообщений не меняются, provenance помечает `source_class: dynamic`. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_06_http_dynamic_template_dependency_is_read_by_the_resource_runtime() { let stand = stand("story316_dynamic_dep").await; let base = stand.base.clone(); let upstream = spawn_orders_upstream().await; let operation_id = published_read_operation(&stand, &base, &upstream, &unique("story316_read")).await; let template = published_template_resource(&stand, &base, &operation_id).await; let (status, created) = create_prompt( &stand, &base, &composed_prompt("story316-dynamic", "internal", dependency(&template)), ) .await; assert_eq!(status, StatusCode::CREATED, "draft: {created}"); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let (status, body) = preview_prompt(&stand, &base, &prompt_id, json!({"channel": "ops"})).await; assert_eq!(status, StatusCode::OK, "dynamic dependency preview: {body}"); assert_eq!( body["messages"].as_array().map(Vec::len), Some(2), "content cannot add or remove a declared message: {body}" ); assert_eq!(body["messages"][0]["role"], json!("system")); assert_eq!( message_text(&body, 0), "Summarise only the attached document.", "a dependency never leaks into another message" ); assert_eq!(body["messages"][1]["role"], json!("user")); assert_eq!( message_text(&body, 1), "Channel: ops\n\n{\"id\":\"order-42\"}", "the pinned template output is inserted verbatim after one blank line: {body}" ); assert_eq!( body["dependencies"][0]["source_class"], json!("dynamic"), "provenance names the dynamic source class: {body}" ); assert_eq!(body["dependencies"][0]["position"], json!(0)); assert_eq!(body["dependencies"][0]["version"], json!(template.version)); assert_eq!(body["effective_sensitivity"], json!("internal")); } /// AT-3.16-06 (§4, §10.3): сбой динамической зависимости — тот же структурированный /// отказ без сообщений, а не частичный Prompt с определением вместо содержимого. #[tokio::test(flavor = "multi_thread")] #[serial] async fn at_3_16_06_http_dynamic_dependency_failure_has_no_partial_messages() { let stand = stand("story316_dynamic_fail").await; let base = stand.base.clone(); let upstream = spawn_failing_upstream().await; let operation_id = published_read_operation(&stand, &base, &upstream, &unique("story316_fail_read")).await; let template = published_template_resource(&stand, &base, &operation_id).await; let (status, created) = create_prompt( &stand, &base, &composed_prompt("story316-dynamic-fail", "internal", dependency(&template)), ) .await; assert_eq!(status, StatusCode::CREATED, "draft: {created}"); let prompt_id = created["prompt_id"].as_str().expect("prompt id").to_owned(); let (status, body) = preview_prompt(&stand, &base, &prompt_id, json!({"channel": "ops"})).await; assert_eq!( status, StatusCode::SERVICE_UNAVAILABLE, "a failed dynamic read is a structured failure: {body}" ); assert_eq!(error_code(&body), "prompt_dependency_unavailable"); assert_eq!( error_context(&body)["source_path"], json!("dependencies[0]"), "the refusal names the exact dependency position: {body}" ); assert!( body.get("messages").is_none(), "no partial prompt is produced: {body}" ); }