CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 31m21s
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 2s
CI / Deploy (pull_request) Skipped
332 lines
11 KiB
Rust
332 lines
11 KiB
Rust
//! #107 envelope migration: independent real HTTP wire and SDK participation controls.
|
|
use super::{CounterSet, observe, support::*};
|
|
|
|
use serde_json::{Value, json};
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
// Reuse the existing real build_app, published catalog, auth and loopback execution fixture.
|
|
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
|
|
|
|
fn counts(c: &CounterSet) -> [usize; 2] {
|
|
[c.sdk_jsonrpc_response(), c.sdk_jsonrpc_error()]
|
|
}
|
|
|
|
async fn wire(
|
|
response: reqwest::Response,
|
|
revision: &str,
|
|
id: &Value,
|
|
sse: bool,
|
|
session: Option<&str>,
|
|
) -> Value {
|
|
assert_eq!(
|
|
response.status(),
|
|
200,
|
|
"envelope id={id}, SSE={sse}, revision={revision}"
|
|
);
|
|
assert_eq!(response.headers()["MCP-Protocol-Version"], revision);
|
|
assert_eq!(
|
|
response
|
|
.headers()
|
|
.get("MCP-Session-Id")
|
|
.map(|v| v.to_str().unwrap()),
|
|
if sse { session } else { None }
|
|
);
|
|
let request_id = response.headers()["x-request-id"]
|
|
.to_str()
|
|
.unwrap()
|
|
.to_owned();
|
|
let trace_id = response.headers()["x-trace-id"]
|
|
.to_str()
|
|
.unwrap()
|
|
.to_owned();
|
|
let content_type = response.headers()["content-type"].to_str().unwrap();
|
|
assert!(content_type.starts_with(if sse {
|
|
"text/event-stream"
|
|
} else {
|
|
"application/json"
|
|
}));
|
|
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let body: Value = if sse {
|
|
let frames: Vec<Value> = raw
|
|
.lines()
|
|
.filter_map(|line| line.strip_prefix("data:"))
|
|
.map(|data| serde_json::from_str(data.trim()).unwrap())
|
|
.collect();
|
|
let terminals: Vec<_> = frames.iter().filter(|f| f.get("id").is_some()).collect();
|
|
assert_eq!(terminals.len(), 1, "one terminal envelope: {raw}");
|
|
assert_eq!(frames.last(), Some(terminals[0]), "terminal must be last");
|
|
for frame in &frames[..frames.len() - 1] {
|
|
assert_eq!(frame["method"], "notifications/progress");
|
|
assert_eq!(frame["params"]["progressToken"], "envelope-stream");
|
|
}
|
|
terminals[0].clone()
|
|
} else {
|
|
serde_json::from_str(&raw).unwrap()
|
|
};
|
|
assert_eq!(body["jsonrpc"], "2.0");
|
|
assert_eq!(body["id"], *id);
|
|
assert_eq!(
|
|
body["id"].to_string(),
|
|
id.to_string(),
|
|
"preserve numeric form"
|
|
);
|
|
assert_eq!(
|
|
body.as_object().unwrap().len(),
|
|
3,
|
|
"no envelope extensions: {body}"
|
|
);
|
|
assert!(body.get("method").is_none());
|
|
if let Some(error) = body.get("error") {
|
|
assert!(
|
|
body.get("result").is_none(),
|
|
"protocol error, not tool isError"
|
|
);
|
|
assert_eq!(error["code"], -32602);
|
|
if sse {
|
|
// Existing stream polling has no request correlation scope; preserve omission.
|
|
assert!(
|
|
error.get("data").is_none(),
|
|
"existing SSE omission: {error}"
|
|
);
|
|
assert_eq!(error.as_object().unwrap().len(), 2);
|
|
} else {
|
|
assert_eq!(
|
|
error["data"],
|
|
json!({"request_id":request_id,"trace_id":trace_id})
|
|
);
|
|
assert_eq!(error.as_object().unwrap().len(), 3);
|
|
}
|
|
} else {
|
|
assert!(body.get("result").is_some());
|
|
}
|
|
body
|
|
}
|
|
|
|
async fn control(revision: &str) -> Vec<[usize; 2]> {
|
|
// Own the process-wide disposable DB before the reused fixture's older ownership hint.
|
|
crank_test_support::postgres_schema_url_for_task("envelopes_owner", "t_b27f5c9d").await;
|
|
let h = Endpoint::started().await;
|
|
let session = h.session(revision).await;
|
|
let mut deltas = vec![];
|
|
for id in [
|
|
json!("opaque/Ю"),
|
|
json!(""),
|
|
json!(i64::MIN),
|
|
json!(i64::MAX),
|
|
json!(9007199254740993_u64),
|
|
json!(u64::MAX),
|
|
json!(1.5),
|
|
json!(1.0),
|
|
json!(-0.0),
|
|
] {
|
|
for invalid in [false, true] {
|
|
let mut params = if invalid {
|
|
json!({"cursor":"not-a-cursor"})
|
|
} else {
|
|
json!({"cursor":"1"})
|
|
};
|
|
if revision == MODERN_REVISION {
|
|
params["_meta"] = modern_meta();
|
|
}
|
|
let before = counts(&h.counters);
|
|
let response = h
|
|
.request(
|
|
revision,
|
|
session.as_deref(),
|
|
&h.key,
|
|
"application/json",
|
|
json!({"jsonrpc":"2.0","id":id,"method":"tools/list","params":params}),
|
|
)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let body = wire(response, revision, &id, false, session.as_deref()).await;
|
|
if invalid {
|
|
assert_eq!(body["error"]["message"], "invalid tools/list cursor");
|
|
} else {
|
|
let mut expected = json!({"tools":[]});
|
|
if revision == MODERN_REVISION {
|
|
expected["resultType"] = json!("complete");
|
|
expected["cacheScope"] = json!("private");
|
|
expected["ttlMs"] = json!(0);
|
|
}
|
|
assert_eq!(
|
|
body["result"], expected,
|
|
"empty result and omission of nextCursor"
|
|
);
|
|
}
|
|
let after = counts(&h.counters);
|
|
deltas.push([after[0] - before[0], after[1] - before[1]]);
|
|
}
|
|
}
|
|
// Both revisions support streamed calls with the combined Accept negotiation.
|
|
// Missing tool resolves inside the shared Tools command, after the stream opens.
|
|
for sse in [false, true] {
|
|
for invalid in [false, true] {
|
|
let id = if invalid {
|
|
json!(u64::MAX)
|
|
} else {
|
|
json!(-0.0)
|
|
};
|
|
let mut params =
|
|
json!({"name":TOOL_NAME,"arguments":{"email":"envelopes@example.test"}});
|
|
if revision == MODERN_REVISION {
|
|
params["_meta"] = modern_meta();
|
|
}
|
|
if invalid {
|
|
params["name"] = json!("missing_tool");
|
|
} else {
|
|
h.gate.add_permits(1);
|
|
}
|
|
if sse {
|
|
params["_meta"]["progressToken"] = json!("envelope-stream");
|
|
}
|
|
let before = counts(&h.counters);
|
|
let mut request = h
|
|
.request(
|
|
revision,
|
|
session.as_deref(),
|
|
&h.key,
|
|
if sse {
|
|
"text/event-stream, application/json"
|
|
} else {
|
|
"application/json"
|
|
},
|
|
json!({"jsonrpc":"2.0","id":id,"method":"tools/call","params":params}),
|
|
)
|
|
.build()
|
|
.unwrap();
|
|
if revision == MODERN_REVISION {
|
|
request.headers_mut().insert(
|
|
"Mcp-Name",
|
|
if invalid { "missing_tool" } else { TOOL_NAME }
|
|
.parse()
|
|
.unwrap(),
|
|
);
|
|
}
|
|
let response = reqwest::Client::new().execute(request).await.unwrap();
|
|
let body = wire(response, revision, &id, sse, session.as_deref()).await;
|
|
if invalid {
|
|
assert_eq!(body["error"]["message"], "tool missing_tool was not found");
|
|
} else {
|
|
assert_eq!(
|
|
body["result"].as_object().unwrap().len(),
|
|
if revision == MODERN_REVISION { 4 } else { 3 }
|
|
);
|
|
assert_eq!(body["result"]["isError"], false);
|
|
assert_eq!(
|
|
body["result"]["structuredContent"],
|
|
json!({"id":"lead_progress"})
|
|
);
|
|
assert_eq!(
|
|
body["result"]["content"],
|
|
json!([{"type":"text","text":serde_json::to_string_pretty(&json!({"id":"lead_progress"})).unwrap()}])
|
|
);
|
|
if revision == MODERN_REVISION {
|
|
assert_eq!(body["result"]["resultType"], "complete");
|
|
} else {
|
|
assert!(body["result"].get("resultType").is_none());
|
|
}
|
|
}
|
|
let after = counts(&h.counters);
|
|
deltas.push([after[0] - before[0], after[1] - before[1]]);
|
|
}
|
|
}
|
|
eprintln!(
|
|
"existing GREEN envelopes wire {revision}: exact id forms, JSON success/protocol error, terminal SSE success/protocol error; SDK deltas={deltas:?}"
|
|
);
|
|
deltas
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_envelopes_wire_legacy() {
|
|
control(LEGACY_REVISION).await;
|
|
}
|
|
#[tokio::test]
|
|
async fn rmcp_envelopes_wire_modern() {
|
|
control(MODERN_REVISION).await;
|
|
}
|
|
#[tokio::test]
|
|
async fn rmcp_envelopes_sdk_legacy() {
|
|
let actual = control(LEGACY_REVISION).await;
|
|
let expected: Vec<_> = (0..actual.len())
|
|
.map(|i| if i % 2 == 0 { [1, 0] } else { [0, 1] })
|
|
.collect();
|
|
assert_eq!(
|
|
actual, expected,
|
|
"real SDK envelope serialization after complete HTTP body"
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn rmcp_envelopes_sdk_modern() {
|
|
let actual = control(MODERN_REVISION).await;
|
|
let expected: Vec<_> = (0..actual.len())
|
|
.map(|i| if i % 2 == 0 { [1, 0] } else { [0, 1] })
|
|
.collect();
|
|
assert_eq!(
|
|
actual, expected,
|
|
"real SDK envelope serialization after complete HTTP body"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rmcp_envelopes_observer_positive_control() {
|
|
let c = Arc::new(CounterSet::default());
|
|
observe(c.clone(), async {
|
|
super::sdk_jsonrpc_response_serialized();
|
|
super::sdk_jsonrpc_error_serialized();
|
|
})
|
|
.await;
|
|
assert_eq!(counts(&c), [1, 1]);
|
|
super::sdk_jsonrpc_response_serialized();
|
|
super::sdk_jsonrpc_error_serialized();
|
|
assert_eq!(counts(&c), [1, 1], "out-of-scope markers are no-ops");
|
|
}
|
|
|
|
#[test]
|
|
fn rmcp_envelopes_helper_lossless_control() {
|
|
use crate::jsonrpc::{jsonrpc_error, jsonrpc_result};
|
|
let values = [
|
|
json!(null),
|
|
json!(""),
|
|
json!("Ю"),
|
|
json!(i64::MIN),
|
|
json!(i64::MAX),
|
|
json!(9007199254740993_u64),
|
|
json!(u64::MAX),
|
|
json!(1.5),
|
|
json!(1.0),
|
|
json!(-0.0),
|
|
json!([null, {"nested":u64::MAX}]),
|
|
json!({"x":null}),
|
|
];
|
|
for id in &values {
|
|
for result in &values {
|
|
let actual = jsonrpc_result(id.clone(), result.clone());
|
|
let expected = json!({"jsonrpc":"2.0","id":id,"result":result});
|
|
assert_eq!(actual, expected);
|
|
assert_eq!(actual.to_string(), expected.to_string());
|
|
}
|
|
for code in [
|
|
i64::MIN,
|
|
i32::MIN as i64 - 1,
|
|
i32::MIN as i64,
|
|
-32602,
|
|
i32::MAX as i64,
|
|
i32::MAX as i64 + 1,
|
|
i64::MAX,
|
|
] {
|
|
for message in ["", "Ошибка Ю"] {
|
|
let actual = jsonrpc_error(id.clone(), code, message);
|
|
let expected =
|
|
json!({"jsonrpc":"2.0","id":id,"error":{"code":code,"message":message}});
|
|
assert_eq!(actual, expected, "data absent outside correlation scope");
|
|
assert_eq!(actual.to_string(), expected.to_string());
|
|
}
|
|
}
|
|
}
|
|
}
|