//! AT-3.14-02 / AT-3.14-03 / AT-3.14-04: `prompts/get` renders one exact bound Published Prompt //! Version deterministically, rejects every invalid argument before rendering, keeps an archived //! bound version readable, and never serves another Agent's snapshot. Every case runs on both //! supported protocol revisions: the legacy era resolves workspace and Agent from the initialized //! session, so its snapshot selection is proven separately below. use crank_core::prompt::MAX_TEXT_BYTES; use crank_registry::RegistryError; use crank_registry::model::AgentPromptBinding; use serde_json::{Value, json}; use super::fixtures::*; fn messages(body: &Value) -> &Vec { body["result"]["messages"] .as_array() .unwrap_or_else(|| panic!("prompts/get must carry messages: {body}")) } fn greeting_messages() -> Vec { vec![ json!({"role": "system", "content": {"type": "text", "text": "Be brief."}}), json!({"role": "user", "content": {"type": "text", "text": "Hello Ada"}}), ] } #[tokio::test] async fn get_renders_typed_arguments_deterministically_on_both_revisions() { let endpoint = prompt_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let body = endpoint .on_revision( revision, "prompts/get", json!({"name": "greeting", "arguments": {"name": "Ada"}}), Some("greeting"), ) .await; assert_eq!( body["result"]["description"], json!("Greets the customer"), "the bound description is served on {revision}: {body}" ); assert_eq!( messages(&body), &greeting_messages(), "the typed renderer output is served unchanged on {revision}: {body}" ); let repeated = endpoint .on_revision( revision, "prompts/get", json!({"name": "greeting", "arguments": {"name": "Ada"}}), Some("greeting"), ) .await; assert_eq!( body, repeated, "rendering is deterministic on {revision}: {repeated}" ); // The rendered enum/default argument path is accepted too. let with_tone = endpoint .on_revision( revision, "prompts/get", json!({"name": "greeting", "arguments": {"name": "Ada", "tone": "dry"}}), Some("greeting"), ) .await; assert_eq!( messages(&with_tone)[1]["content"], json!({"type": "text", "text": "Hello Ada"}), "an explicit enum value still renders the bound template on {revision}: {with_tone}" ); } } #[tokio::test] async fn get_rejects_each_invalid_argument_before_rendering_without_disclosure() { let endpoint = prompt_endpoint().await; let oversized = "x".repeat(MAX_TEXT_BYTES + 1); for revision in [MODERN_REVISION, LEGACY_REVISION] { for (arguments, reason, argument) in [ (json!({}), "missing_argument", "name"), ( json!({"name": "Ada", "extra": "x"}), "unknown_argument", "extra", ), (json!({"name": 5}), "argument_invalid", "name"), ( json!({"name": "Ada", "tone": "loud"}), "argument_invalid", "tone", ), (json!({"name": oversized}), "argument_too_large", "name"), ] { let body = endpoint .on_revision( revision, "prompts/get", json!({"name": "greeting", "arguments": arguments}), Some("greeting"), ) .await; assert_eq!( body["error"]["code"], json!(-32602), "{reason} on {revision}: a typed argument rejection is invalid params: {body}" ); assert_eq!( body["error"]["data"]["reason"], json!(reason), "{reason} on {revision}: {body}" ); assert_eq!( body["error"]["data"]["argument"], json!(argument), "{reason} on {revision}: {body}" ); // No partial rendering and no foreign metadata: neither bound content nor any prompt // message may appear anywhere in the error envelope. let serialized = body.to_string(); for leaked in [ "Be brief.", "Hello ", "Archived but bound.", "Draft content.", ] { assert!( !serialized.contains(leaked), "{reason} on {revision}: the rejection must not disclose {leaked:?}: {serialized}" ); } assert!( body.get("result").is_none(), "{reason} on {revision}: a rejected request carries no result: {body}" ); } } } #[tokio::test] async fn get_serves_the_archived_bound_version_and_archive_blocks_new_bindings() { let endpoint = prompt_endpoint().await; // AT-3.14-03: the immutable snapshot behaviour survives the archive, on every revision. for revision in [MODERN_REVISION, LEGACY_REVISION] { let serving = endpoint .on_revision( revision, "prompts/get", json!({"name": "archived-prompt"}), Some("archived-prompt"), ) .await; assert_eq!( messages(&serving), &vec![ json!({"role": "user", "content": {"type": "text", "text": "Archived but bound."}}) ], "an archived Prompt Version bound before the archive keeps serving on {revision}: {serving}" ); } // Archive forbids new bindings: the exact version may not be attached to another Agent. let other_agent = crank_core::AgentId::new(format!("agent_{OTHER_AGENT_SLUG}")); let rebind = bind_prompts( &endpoint.registry, &other_agent, &[AgentPromptBinding { prompt_id: endpoint.archived_id.clone(), prompt_version: 1, }], ) .await; assert!( matches!(rebind, Err(RegistryError::PromptArchived { .. })), "archive must refuse new bindings, got {rebind:?}" ); // Archive forbids new publications of the same Prompt (archived is terminal lifecycle). let publication = endpoint .registry .publish_prompt_draft(crank_registry::model::PublishPromptRequest { workspace_id: crank_core::WorkspaceId::new("ws_default"), prompt_id: endpoint.archived_id.clone(), expected_updated_at: endpoint.archived_updated_at, published_at: time::OffsetDateTime::now_utc(), }) .await; assert!( matches!(publication, Err(RegistryError::PromptNotDraft { .. })), "archive must refuse new publications, got {publication:?}" ); } #[tokio::test] async fn get_of_an_unknown_unbound_or_foreign_prompt_is_indistinguishable() { let endpoint = prompt_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { for name in [ "draft-prompt", "other-agent-prompt", "prm_a_greeting", "never-existed", ] { let body = endpoint .on_revision(revision, "prompts/get", json!({"name": name}), Some(name)) .await; assert_eq!( body["error"]["code"], json!(-32602), "{name} on {revision}: an unaddressable Prompt is invalid params: {body}" ); assert_eq!( body["error"]["message"], json!("the requested prompt is not available"), "Draft, unbound, foreign and unknown names share one safe answer on {revision}: {body}" ); let serialized = body.to_string(); for leaked in ["Other agent secret.", "Draft content.", "Be brief."] { assert!( !serialized.contains(leaked), "{name} on {revision}: leaked {leaked:?}: {serialized}" ); } } } } #[tokio::test] async fn get_serves_the_exact_bound_version_after_a_draft_edit() { let endpoint = prompt_endpoint().await; edit_greeting_draft(&endpoint).await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let body = endpoint .on_revision( revision, "prompts/get", json!({"name": "greeting", "arguments": {"name": "Ada"}}), Some("greeting"), ) .await; assert_eq!( messages(&body)[1]["content"], json!({"type": "text", "text": "Hello Ada"}), "the bound v1 snapshot stays immutable after a Draft edit on {revision}: {body}" ); let listed = endpoint .on_revision(revision, "prompts/list", json!({}), None) .await; assert_eq!( listed["result"]["prompts"][0]["description"], json!("Greets the customer"), "discovery still describes the bound version, not the new Draft, on {revision}: {listed}" ); } } /// The legacy era takes workspace and Agent from the *initialized session*, not from the request /// body. A session of the other Agent therefore serves that Agent's snapshot, and a Prompt bound to /// the sales Agent is indistinguishable from an unknown name on it. #[tokio::test] async fn legacy_session_serves_only_its_own_agent_snapshot() { let endpoint = prompt_endpoint().await; let other_key = endpoint.other_agent_key().await; let session = endpoint .legacy_session_for(OTHER_AGENT_SLUG, &other_key) .await; let own = endpoint .legacy_for( OTHER_AGENT_SLUG, &other_key, &session, "prompts/get", json!({"name": "other-agent-prompt"}), ) .await; assert_eq!( messages(&own), &vec![json!({"role": "user", "content": {"type": "text", "text": "Other agent secret."}})], "the session's own bound snapshot is served: {own}" ); let listed = endpoint .legacy_for( OTHER_AGENT_SLUG, &other_key, &session, "prompts/list", json!({}), ) .await; assert_eq!( listed["result"]["prompts"] .as_array() .expect("the session lists its own prompts") .iter() .map(|prompt| prompt["name"].as_str().unwrap_or_default().to_owned()) .collect::>(), vec!["other-agent-prompt"], "the legacy session lists only its own bound Prompt: {listed}" ); // Every Prompt bound to the sales Agent is foreign to this session. for name in ["greeting", "review", "archived-prompt"] { let body = endpoint .legacy_for( OTHER_AGENT_SLUG, &other_key, &session, "prompts/get", json!({"name": name}), ) .await; assert_eq!( body["error"]["code"], json!(-32602), "{name}: a foreign bound Prompt is invalid params on the legacy era: {body}" ); assert_eq!( body["error"]["message"], json!("the requested prompt is not available"), "{name}: a foreign bound Prompt shares the safe answer: {body}" ); assert!( body.get("result").is_none(), "{name}: a foreign bound Prompt carries no result: {body}" ); let serialized = body.to_string(); for leaked in ["Be brief.", "Hello ", "Archived but bound."] { assert!( !serialized.contains(leaked), "{name}: the foreign rejection must not disclose {leaked:?}: {serialized}" ); } } }