- request-scoped lifecycle одного tools/call: валидированный `_meta.progressToken` (строковый/числовой, 1..=128 байт) и bounded очередь реальных этапов (tool_resolved → authorization_resolved → execution_dispatched) в собственный SSE-stream запроса; без token или без stream-carrier результат не меняется; - сериализация `notifications/progress` по pinned схеме обеих revisions (progressToken/progress/message, total не изобретается); - отмена по revision: 2025-11-25 — `notifications/cancelled` через bounded in-flight таблицу (владение = session + platform credential, не progress token); 2026-07-28 — закрытие response stream; в обоих случаях drop будущего вызова = кооперативная остановка, terminal-сообщение не отправляется; - streamed ответ сохраняет проекцию Story 3.18: closed `notifications/message` перед финальным кадром (перенос logging-scope в тело стрима); - ATDD: 12 end-to-end тестов на реальном endpoint/каталоге/counted upstream (обе revisions, malformed/oversized/reused token, owner/foreign/late cancel, disconnect, ambiguous outbound, representative client smoke) + 7 unit-тестов границ lifecycle; entry в scripts/ci-story-checks.sh.
295 lines
11 KiB
Rust
295 lines
11 KiB
Rust
//! AT-3.4-03: a cancellation stops a call that can still be stopped, releases it, and never yields a
|
|
//! `completed` result or history row. The harness of the suite lives in `super`.
|
|
|
|
use super::*;
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// AT-3.4-03: a cancellation stops a call that can still be stopped, releases it, and never yields a
|
|
// `completed` result or history row.
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
#[tokio::test]
|
|
async fn at_3_4_03_legacy_cancellation_stops_the_call_and_never_completes_it() {
|
|
let fixture = fixture("lifecycle-legacy-cancel", LONG_HOLD_MS, 30_000).await;
|
|
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
|
|
|
|
let call_session = session.clone();
|
|
let call = tokio::spawn({
|
|
let fixture_client = fixture.client.clone();
|
|
let mcp_url = fixture.mcp_url.clone();
|
|
let api_key = fixture.api_key.clone();
|
|
let params = fixture.call_params(Some(json!("tok-cancel")), Revision::Legacy);
|
|
async move {
|
|
let response = fixture_client
|
|
.post(mcp_url)
|
|
.header(reqwest::header::ACCEPT, "text/event-stream")
|
|
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
|
|
.header("MCP-Protocol-Version", LEGACY)
|
|
.header("MCP-Session-Id", call_session)
|
|
.header("x-request-id", "req-legacy-cancel")
|
|
.json(&json!({
|
|
"jsonrpc": "2.0",
|
|
"id": 601,
|
|
"method": "tools/call",
|
|
"params": params,
|
|
}))
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
(response.status(), response.text().await.unwrap())
|
|
}
|
|
});
|
|
|
|
fixture.wait_until_dispatched().await;
|
|
assert_eq!(
|
|
fixture.cancel(&fixture.api_key, &session, json!(601)).await,
|
|
reqwest::StatusCode::ACCEPTED,
|
|
"an accepted cancellation is acknowledged without a body"
|
|
);
|
|
|
|
let (status, body) = tokio::time::timeout(Duration::from_secs(10), call)
|
|
.await
|
|
.expect("the cancelled call is released, it does not hang")
|
|
.expect("the request task joins");
|
|
assert_eq!(status, reqwest::StatusCode::OK);
|
|
let reply = Reply::parse(status, String::new(), body);
|
|
assert!(
|
|
reply
|
|
.frames
|
|
.iter()
|
|
.all(|frame| frame.get("method").is_some()),
|
|
"a cancelled request gets no terminal message, only the progress it already saw: {}",
|
|
reply.raw
|
|
);
|
|
assert_eq!(
|
|
reply.progress_frames().len(),
|
|
3,
|
|
"the client really saw the real steps before the stop: {}",
|
|
reply.raw
|
|
);
|
|
|
|
tokio::time::sleep(Duration::from_millis(CANCEL_GRACE_MS)).await;
|
|
assert_eq!(
|
|
fixture.upstream.completed.load(Ordering::SeqCst),
|
|
0,
|
|
"the abandoned upstream response must never complete the call"
|
|
);
|
|
assert_eq!(
|
|
fixture.upstream.started.load(Ordering::SeqCst),
|
|
1,
|
|
"the write really was dispatched and must not be retried automatically"
|
|
);
|
|
|
|
let history = fixture.history("req-legacy-cancel").await;
|
|
assert!(
|
|
history.iter().all(|row| row.status != InvocationStatus::Ok),
|
|
"a cancelled call must never be recorded as completed, got {history:?}"
|
|
);
|
|
// The record contract admits a non-`ok` row only with a closed execution error code, and a
|
|
// cancellation is not one of them: the honest terminal state of this call is "no record" instead
|
|
// of an invented error code. Either way nothing claims that the call completed.
|
|
assert!(
|
|
history.is_empty(),
|
|
"a stopped call has no terminal outcome to record, got {history:?}"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn at_3_4_03_a_foreign_or_unknown_cancellation_never_touches_the_active_call() {
|
|
let fixture = fixture("lifecycle-foreign-cancel", HOLD_MS, 5_000).await;
|
|
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
|
|
let foreign_session =
|
|
initialize_session(&fixture.client, &fixture.mcp_url, &fixture.other_api_key).await;
|
|
|
|
let call = tokio::spawn({
|
|
let client = fixture.client.clone();
|
|
let mcp_url = fixture.mcp_url.clone();
|
|
let api_key = fixture.api_key.clone();
|
|
let call_session = session.clone();
|
|
let params = fixture.call_params(Some(json!("tok-owner")), Revision::Legacy);
|
|
async move {
|
|
let response = client
|
|
.post(mcp_url)
|
|
.header(reqwest::header::ACCEPT, "text/event-stream")
|
|
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
|
|
.header("MCP-Protocol-Version", LEGACY)
|
|
.header("MCP-Session-Id", call_session)
|
|
.header("x-request-id", "req-owner")
|
|
.json(&json!({
|
|
"jsonrpc": "2.0", "id": 602, "method": "tools/call", "params": params,
|
|
}))
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
response.text().await.unwrap()
|
|
}
|
|
});
|
|
|
|
fixture.wait_until_dispatched().await;
|
|
// A foreign credential of the same agent, and an unknown request id: neither may cancel this call.
|
|
assert_eq!(
|
|
fixture
|
|
.cancel(&fixture.other_api_key, &session, json!(602))
|
|
.await,
|
|
reqwest::StatusCode::ACCEPTED
|
|
);
|
|
assert_eq!(
|
|
fixture
|
|
.cancel(&fixture.api_key, &session, json!(999_999))
|
|
.await,
|
|
reqwest::StatusCode::ACCEPTED
|
|
);
|
|
// A foreign session of the same credential cannot reach this call either.
|
|
assert_eq!(
|
|
fixture
|
|
.cancel(&fixture.api_key, &foreign_session, json!(602))
|
|
.await,
|
|
reqwest::StatusCode::ACCEPTED
|
|
);
|
|
|
|
let body = tokio::time::timeout(Duration::from_secs(10), call)
|
|
.await
|
|
.expect("the untouched call completes")
|
|
.expect("the request task joins");
|
|
let reply = Reply::parse(reqwest::StatusCode::OK, String::new(), body);
|
|
assert_eq!(
|
|
reply.terminal()["result"]["structuredContent"],
|
|
json!({ "id": "lead_123" }),
|
|
"a foreign cancellation must not stop the owner's call, got {}",
|
|
reply.raw
|
|
);
|
|
let history = fixture.history("req-owner").await;
|
|
assert_eq!(history.len(), 1, "got {history:?}");
|
|
assert_eq!(history[0].status, InvocationStatus::Ok);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn at_3_4_03_a_late_cancellation_after_the_result_is_ignored() {
|
|
let fixture = fixture("lifecycle-late-cancel", HOLD_MS, 5_000).await;
|
|
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
|
|
|
|
let reply = fixture
|
|
.call_stream(
|
|
Revision::Legacy,
|
|
603,
|
|
fixture.call_params(Some(json!("tok-late")), Revision::Legacy),
|
|
Some(&session),
|
|
"req-late-cancel",
|
|
)
|
|
.await;
|
|
reply.assert_stream();
|
|
assert_eq!(reply.terminal()["result"]["isError"], json!(false));
|
|
|
|
assert_eq!(
|
|
fixture.cancel(&fixture.api_key, &session, json!(603)).await,
|
|
reqwest::StatusCode::ACCEPTED
|
|
);
|
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
|
|
let history = fixture.history("req-late-cancel").await;
|
|
assert_eq!(
|
|
history.len(),
|
|
1,
|
|
"a cancellation of a finished call must not add a second record, got {history:?}"
|
|
);
|
|
assert_eq!(history[0].status, InvocationStatus::Ok);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn at_3_4_03_a_plain_json_call_is_cancellable_too() {
|
|
let fixture = fixture("lifecycle-json-cancel", LONG_HOLD_MS, 30_000).await;
|
|
let session = initialize_session(&fixture.client, &fixture.mcp_url, &fixture.api_key).await;
|
|
|
|
let call = tokio::spawn({
|
|
let client = fixture.client.clone();
|
|
let mcp_url = fixture.mcp_url.clone();
|
|
let api_key = fixture.api_key.clone();
|
|
let call_session = session.clone();
|
|
let params = fixture.call_params(None, Revision::Legacy);
|
|
async move {
|
|
let response = client
|
|
.post(mcp_url)
|
|
.header(reqwest::header::ACCEPT, "application/json")
|
|
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
|
|
.header("MCP-Protocol-Version", LEGACY)
|
|
.header("MCP-Session-Id", call_session)
|
|
.header("x-request-id", "req-json-cancel")
|
|
.json(&json!({
|
|
"jsonrpc": "2.0", "id": 605, "method": "tools/call", "params": params,
|
|
}))
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let status = response.status();
|
|
(status, response.text().await.unwrap())
|
|
}
|
|
});
|
|
|
|
fixture.wait_until_dispatched().await;
|
|
assert_eq!(
|
|
fixture.cancel(&fixture.api_key, &session, json!(605)).await,
|
|
reqwest::StatusCode::ACCEPTED
|
|
);
|
|
|
|
let (status, body) = tokio::time::timeout(Duration::from_secs(10), call)
|
|
.await
|
|
.expect("the cancelled call is released")
|
|
.expect("the request task joins");
|
|
assert_eq!(status, reqwest::StatusCode::OK);
|
|
let reply = Reply::parse(status, String::new(), body);
|
|
assert!(
|
|
reply.frames.is_empty(),
|
|
"a cancelled request gets no terminal message, got {}",
|
|
reply.raw
|
|
);
|
|
|
|
tokio::time::sleep(Duration::from_millis(CANCEL_GRACE_MS)).await;
|
|
assert_eq!(fixture.upstream.completed.load(Ordering::SeqCst), 0);
|
|
let history = fixture.history("req-json-cancel").await;
|
|
assert!(
|
|
history.iter().all(|row| row.status != InvocationStatus::Ok),
|
|
"a cancelled call must never be recorded as completed, got {history:?}"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn at_3_4_03_closing_the_modern_response_stream_cancels_the_call() {
|
|
let fixture = fixture("lifecycle-modern-disconnect", LONG_HOLD_MS, 30_000).await;
|
|
|
|
let response = fixture
|
|
.request(
|
|
"text/event-stream",
|
|
&fixture.api_key,
|
|
&fixture.headers(Revision::Modern, None),
|
|
json!({
|
|
"jsonrpc": "2.0",
|
|
"id": 604,
|
|
"method": "tools/call",
|
|
"params": fixture.call_params(Some(json!("tok-disconnect")), Revision::Modern),
|
|
}),
|
|
)
|
|
.header("x-request-id", "req-modern-disconnect")
|
|
.send()
|
|
.await
|
|
.expect("the stream opens before the call finishes");
|
|
assert_eq!(response.status(), reqwest::StatusCode::OK);
|
|
fixture.wait_until_dispatched().await;
|
|
|
|
// The pinned `2026-07-28` cancellation signal: closing the request's own response stream.
|
|
drop(response);
|
|
|
|
tokio::time::sleep(Duration::from_millis(CANCEL_GRACE_MS)).await;
|
|
assert_eq!(
|
|
fixture.upstream.completed.load(Ordering::SeqCst),
|
|
0,
|
|
"the closed stream must stop the dispatched call instead of letting it finish"
|
|
);
|
|
assert_eq!(fixture.upstream.started.load(Ordering::SeqCst), 1);
|
|
|
|
let history = fixture.history("req-modern-disconnect").await;
|
|
assert!(
|
|
history.iter().all(|row| row.status != InvocationStatus::Ok),
|
|
"a cancelled call must never be recorded as completed, got {history:?}"
|
|
);
|
|
}
|