CI / UI Checks (pull_request) Successful in 14s
CI / Rust Checks (pull_request) Successful in 37m47s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
393 lines
15 KiB
Rust
393 lines
15 KiB
Rust
//! Synthetic replies exercise real trait results, restoration and structured cancellation.
|
|
use super::*;
|
|
use serde_json::json;
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct Domain {
|
|
outcome: Mutex<Option<PromptsOutcome>>,
|
|
calls: AtomicUsize,
|
|
pending: bool,
|
|
started: tokio::sync::Notify,
|
|
release: tokio::sync::Notify,
|
|
dropped: AtomicBool,
|
|
finished: AtomicBool,
|
|
}
|
|
struct DropWitness<'a>(&'a AtomicBool);
|
|
impl Drop for DropWitness<'_> {
|
|
fn drop(&mut self) {
|
|
self.0.store(true, SeqCst);
|
|
}
|
|
}
|
|
impl Domain {
|
|
pub(super) async fn prompts(&self, command: PromptsCommand) -> PromptsOutcome {
|
|
self.calls.fetch_add(1, SeqCst);
|
|
match command {
|
|
PromptsCommand::List(list) => {
|
|
assert_eq!(list.limit, Some(7));
|
|
assert_eq!(list.cursor.as_deref(), Some("opaque"));
|
|
}
|
|
PromptsCommand::Get(get) => {
|
|
assert_eq!(get.name, "bound-prompt");
|
|
assert_eq!(
|
|
get.arguments,
|
|
Some(
|
|
json!({"number":42,"enabled":true,"nested":[null]})
|
|
.as_object()
|
|
.unwrap()
|
|
.clone()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
let _guard = DropWitness(&self.dropped);
|
|
if self.pending {
|
|
self.started.notify_one();
|
|
self.release.notified().await;
|
|
}
|
|
self.finished.store(true, SeqCst);
|
|
self.outcome
|
|
.lock()
|
|
.unwrap()
|
|
.take()
|
|
.expect("exactly one domain execution")
|
|
}
|
|
}
|
|
fn scope(revision: &str) -> PromptScope {
|
|
PromptScope {
|
|
workspace_slug: "fresh-workspace".into(),
|
|
agent_slug: "fresh-agent".into(),
|
|
credential: VerifiedMachineCredential {
|
|
machine_access_mode: crank_core::MachineAccessMode::StaticAgentKey,
|
|
max_security_level: crank_core::OperationSecurityLevel::Standard,
|
|
scopes: vec![crank_core::PlatformApiKeyScope::Read],
|
|
platform_api_key_id: Some("fresh-key".into()),
|
|
},
|
|
correlation: CorrelationContext::generate(),
|
|
continuation: ContinuationSupport::from_supported(false),
|
|
locale: ExecutionLocale::En,
|
|
revision: revision.into(),
|
|
limit: Some(7),
|
|
}
|
|
}
|
|
async fn state() -> Arc<AppState> {
|
|
crank_test_support::postgres_schema_url_for_task("prompts_handler_local", "t_2d797361").await;
|
|
super::super::rmcp_tools_tests::test_state().await
|
|
}
|
|
fn handler(state: &Arc<AppState>, outcome: PromptsOutcome) -> PromptHandler {
|
|
let mut handler = PromptHandler::new(state);
|
|
handler.domain = Some(Arc::new(Domain {
|
|
outcome: Mutex::new(Some(outcome)),
|
|
..Default::default()
|
|
}));
|
|
handler
|
|
}
|
|
fn page() -> Option<PaginatedRequestParams> {
|
|
Some(PaginatedRequestParams::default().with_cursor(Some("opaque".into())))
|
|
}
|
|
fn get() -> GetPromptRequestParams {
|
|
let mut request = GetPromptRequestParams::new("bound-prompt");
|
|
request.arguments = Some(
|
|
json!({"number":42,"enabled":true,"nested":[null]})
|
|
.as_object()
|
|
.unwrap()
|
|
.clone(),
|
|
);
|
|
request
|
|
}
|
|
async fn context(handler: &PromptHandler, revision: &str) -> RequestContext<RoleServer> {
|
|
let context = request_context(handler, scope(revision)).await.unwrap();
|
|
assert!(context.peer.is_transport_closed());
|
|
assert_eq!(
|
|
Arc::strong_count(&handler.reply),
|
|
1,
|
|
"seed service released handler"
|
|
);
|
|
assert_eq!(handler.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
|
let actual = PromptHandler::scope(&context).unwrap();
|
|
assert_eq!(actual.workspace_slug, "fresh-workspace");
|
|
assert_eq!(actual.agent_slug, "fresh-agent");
|
|
assert_eq!(
|
|
actual
|
|
.credential
|
|
.platform_api_key_id
|
|
.as_ref()
|
|
.map(|id| id.as_str()),
|
|
Some("fresh-key")
|
|
);
|
|
assert_eq!(actual.revision, revision);
|
|
context
|
|
}
|
|
fn assert_list(outcome: PromptsOutcome, raw: Vec<Value>, cursor: Option<&str>) {
|
|
match outcome {
|
|
PromptsOutcome::Listed {
|
|
prompts,
|
|
next_cursor,
|
|
} => {
|
|
assert_eq!(prompts, raw);
|
|
assert_eq!(next_cursor.as_deref(), cursor);
|
|
}
|
|
_ => panic!("list restore failed"),
|
|
}
|
|
}
|
|
fn assert_get(outcome: PromptsOutcome, raw: Vec<Value>, expected: Option<&str>) {
|
|
match outcome {
|
|
PromptsOutcome::Got {
|
|
description,
|
|
messages,
|
|
} => {
|
|
assert_eq!(messages, raw);
|
|
assert_eq!(description.as_deref(), expected);
|
|
}
|
|
_ => panic!("get restore failed"),
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_prompts_trait_lossless_and_actual_typed_mutations_consumed() {
|
|
let state = state().await;
|
|
for revision in ["2025-11-25", "2026-07-28"] {
|
|
let raw = vec![
|
|
json!({"name":"a","description":null,"icons":{"not":"SDK array"},"annotations":false,"x":[null,{"nested":true}],"arguments":[{"name":"n","required":null,"description":null,"type":"integer","default":42,"enum":[1,42],"x":null}]}),
|
|
json!({"name":"b","arguments":null}),
|
|
json!({"name":"c"}),
|
|
];
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Listed {
|
|
prompts: raw.clone(),
|
|
next_cursor: Some("next".into()),
|
|
},
|
|
);
|
|
let c = context(&h, revision).await;
|
|
let mut result = ServerHandler::list_prompts(&h, page(), c).await.unwrap();
|
|
result.prompts[0].title = Some("typed-title".into());
|
|
result.prompts[0].arguments.as_mut().unwrap()[0].title = Some("typed-argument".into());
|
|
result.next_cursor = Some("typed-cursor".into());
|
|
let mut expected = raw;
|
|
expected[0]["title"] = json!("typed-title");
|
|
expected[0]["arguments"][0]["title"] = json!("typed-argument");
|
|
assert_list(restore_list(&h, Ok(result)), expected, Some("typed-cursor"));
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
|
let text = serde_json::to_string_pretty(&json!({"exact":[null,true]})).unwrap();
|
|
let raw = vec![
|
|
json!({"role":"system","content":{"type":"text","text":text,"annotations":[null,42],"x":null},"x":{"nested":true}}),
|
|
json!({"role":"user","content":{"type":"image","data":"AAEC/w==","mimeType":"image/png","annotations":false,"x":[1,null]}}),
|
|
json!({"role":"assistant","content":{"type":"audio","data":"AAEC/w==","mimeType":"audio/wav"}}),
|
|
json!({"role":"user","content":{"type":"resource","resource":{"uri":"crank://text","text":"exact\nbytes","mimeType":null,"x":[null]},"annotations":{"arbitrary":true}}}),
|
|
json!({"role":"user","content":{"type":"resource","resource":{"uri":"crank://blob","blob":"AAEC/w==","x":false},"x":null}}),
|
|
json!({"role":"user","content":{"type":"resource_link","uri":"crank://link","name":"link","description":null,"icons":false,"annotations":42,"x":{"a":null}}}),
|
|
];
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Got {
|
|
description: None,
|
|
messages: raw.clone(),
|
|
},
|
|
);
|
|
let c = context(&h, revision).await;
|
|
let GetPromptResponse::Complete(mut result) =
|
|
ServerHandler::get_prompt(&h, get(), c).await.unwrap()
|
|
else {
|
|
panic!("expected complete prompt")
|
|
};
|
|
result.description = Some("typed-description".into());
|
|
// Content is mutated through the SDK result, not the original domain JSON.
|
|
let mut message = serde_json::to_value(&result.messages[0]).unwrap();
|
|
message["content"]["text"] = json!("typed-text");
|
|
result.messages[0] = serde_json::from_value(message).unwrap();
|
|
let mut expected = raw;
|
|
expected[0]["content"]["text"] = json!("typed-text");
|
|
assert_get(
|
|
restore_get(&h, Ok(result.into())),
|
|
expected,
|
|
Some("typed-description"),
|
|
);
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Got {
|
|
description: None,
|
|
messages: vec![],
|
|
},
|
|
);
|
|
let c = context(&h, revision).await;
|
|
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
|
assert_get(restore_get(&h, result), vec![], None);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_prompts_trait_errors_fail_closed_without_reexecution() {
|
|
let state = state().await;
|
|
for is_list in [true, false] {
|
|
for outcome in [
|
|
PromptsOutcome::InvalidParams("exact-detail".into()),
|
|
PromptsOutcome::NotAvailable,
|
|
PromptsOutcome::ArgumentsRejected {
|
|
reason: "wrong_type",
|
|
argument: Some("number".into()),
|
|
},
|
|
PromptsOutcome::ArgumentsRejected {
|
|
reason: "missing",
|
|
argument: None,
|
|
},
|
|
PromptsOutcome::DependencyUnavailable { position: 13 },
|
|
PromptsOutcome::CatalogUnavailable,
|
|
] {
|
|
let expected = std::mem::discriminant(&outcome);
|
|
let h = handler(&state, outcome);
|
|
let c = context(&h, "2026-07-28").await;
|
|
let actual = if is_list {
|
|
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
|
assert!(result.is_err());
|
|
restore_list(&h, result)
|
|
} else {
|
|
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
|
assert!(result.is_err());
|
|
restore_get(&h, result)
|
|
};
|
|
assert_eq!(std::mem::discriminant(&actual), expected);
|
|
match actual {
|
|
PromptsOutcome::InvalidParams(detail) => assert_eq!(detail, "exact-detail"),
|
|
PromptsOutcome::ArgumentsRejected { reason, argument } => {
|
|
assert_eq!(
|
|
reason,
|
|
if argument.is_some() {
|
|
"wrong_type"
|
|
} else {
|
|
"missing"
|
|
}
|
|
);
|
|
if let Some(argument) = argument {
|
|
assert_eq!(argument, "number");
|
|
}
|
|
}
|
|
PromptsOutcome::DependencyUnavailable { position } => assert_eq!(position, 13),
|
|
_ => {}
|
|
}
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
|
}
|
|
}
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Listed {
|
|
prompts: vec![json!({"description":"missing name"})],
|
|
next_cursor: None,
|
|
},
|
|
);
|
|
let c = context(&h, "2026-07-28").await;
|
|
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
|
assert!(result.is_err());
|
|
assert!(matches!(
|
|
restore_list(&h, result),
|
|
PromptsOutcome::CatalogUnavailable
|
|
));
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
|
for message in [
|
|
json!({"role":"invalid","content":{"type":"text","text":"x"}}),
|
|
json!({"role":"user","content":{"type":"text"}}),
|
|
] {
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Got {
|
|
description: None,
|
|
messages: vec![message],
|
|
},
|
|
);
|
|
let c = context(&h, "2026-07-28").await;
|
|
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
|
assert!(result.is_err());
|
|
assert!(matches!(
|
|
restore_get(&h, result),
|
|
PromptsOutcome::CatalogUnavailable
|
|
));
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
|
}
|
|
for change in 0..3 {
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Listed {
|
|
prompts: vec![json!({"name":"a","x":1}), json!({"name":"b","x":2})],
|
|
next_cursor: None,
|
|
},
|
|
);
|
|
let mut c = context(&h, "2026-07-28").await;
|
|
c.extensions = Default::default();
|
|
assert!(ServerHandler::list_prompts(&h, page(), c).await.is_err());
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 0);
|
|
let c = context(&h, "2026-07-28").await;
|
|
let mut result = ServerHandler::list_prompts(&h, page(), c).await.unwrap();
|
|
let result = match change {
|
|
0 => {
|
|
result.prompts.clear();
|
|
Ok(result)
|
|
}
|
|
1 => {
|
|
result.prompts.swap(0, 1);
|
|
Ok(result)
|
|
}
|
|
_ => Err(internal()),
|
|
};
|
|
assert!(matches!(
|
|
restore_list(&h, result),
|
|
PromptsOutcome::CatalogUnavailable
|
|
));
|
|
assert_eq!(h.domain.as_ref().unwrap().calls.load(SeqCst), 1);
|
|
}
|
|
let h = handler(
|
|
&state,
|
|
PromptsOutcome::Got {
|
|
description: None,
|
|
messages: vec![],
|
|
},
|
|
);
|
|
let c = context(&h, "2026-07-28").await;
|
|
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
|
assert!(result.is_err());
|
|
assert!(matches!(
|
|
restore_list(&h, result),
|
|
PromptsOutcome::CatalogUnavailable
|
|
));
|
|
assert!(matches!(
|
|
restore_get(&h, Err(internal())),
|
|
PromptsOutcome::CatalogUnavailable
|
|
));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_prompts_parent_drop_cancels_domain_and_bootstrap_is_closed() {
|
|
let state = state().await;
|
|
for is_list in [true, false] {
|
|
let mut h = PromptHandler::new(&state);
|
|
let domain = Arc::new(Domain {
|
|
pending: true,
|
|
..Default::default()
|
|
});
|
|
h.domain = Some(domain.clone());
|
|
let c = context(&h, "2026-07-28").await;
|
|
let mut parent = Box::pin(async {
|
|
if is_list {
|
|
let result = ServerHandler::list_prompts(&h, page(), c).await;
|
|
restore_list(&h, result)
|
|
} else {
|
|
let result = ServerHandler::get_prompt(&h, get(), c).await;
|
|
restore_get(&h, result)
|
|
}
|
|
});
|
|
tokio::select! { _ = &mut parent => panic!("domain should wait"), _ = domain.started.notified() => {} }
|
|
assert!(!domain.dropped.load(SeqCst));
|
|
drop(parent);
|
|
assert!(
|
|
domain.dropped.load(SeqCst),
|
|
"domain future dropped synchronously with parent"
|
|
);
|
|
domain.release.notify_one();
|
|
tokio::task::yield_now().await;
|
|
assert!(!domain.finished.load(SeqCst), "no detached continuation");
|
|
assert!(h.take().is_none());
|
|
assert_eq!(domain.calls.load(SeqCst), 1);
|
|
assert_eq!(Arc::strong_count(&domain), 2);
|
|
}
|
|
}
|