Merge pull request 'MCP: deliver progress through SDK Peer without raw-token TRACE' (#144) from delivery/rmcp107-progress-peer into main
CI / UI Checks (push) Successful in 9s
CI / Rust Checks (push) Successful in 1h1m48s
CI / Rust Feature Validation (push) Skipped
CI / Frontend E2E (push) Skipped
CI / Community Image Smoke (push) Skipped
CI / Required Checks (push) Successful in 2s
CI / Deploy (push) Skipped
Community Release / release (push) Successful in 1h25m50s

This commit was merged in pull request #144.
This commit is contained in:
bsodfather committed 2026-11-14 12:06:51 +00:00
commit 1ce3c2bb97
12 files changed
+974 -83

No files matched your search

+1
View File
@@ -46,6 +46,7 @@ mod rmcp_negotiation;
mod rmcp_notifications;
#[cfg(test)]
mod rmcp_progress_cancel;
mod rmcp_progress_peer;
mod rmcp_prompts;
mod rmcp_prompts_projection;
mod rmcp_resources;
+112 -67
View File
@@ -34,8 +34,7 @@ use axum::{
};
use crank_core::{CorrelationContext, ExecutionLocale, VerifiedMachineCredential};
use futures_util::stream;
use rmcp::model::{NumberOrString, ProgressNotificationParam};
use serde_json::{Number, Value, json};
use serde_json::{Number, Value};
use tokio::sync::{mpsc, watch};
use super::rmcp_dispatch::handle_tools_command;
@@ -83,15 +82,23 @@ impl ProgressToken {
}
}
fn sdk_token(&self) -> NumberOrString {
#[cfg(test)]
pub(super) fn sdk_token(&self) -> rmcp::model::NumberOrString {
match self {
Self::Text(text) => NumberOrString::String(text.clone().into()),
Self::Text(text) => rmcp::model::NumberOrString::String(text.clone().into()),
Self::Number(number) => number.as_i64().map_or_else(
|| NumberOrString::String(number.to_string().into()),
NumberOrString::Number,
|| rmcp::model::NumberOrString::String(number.to_string().into()),
rmcp::model::NumberOrString::Number,
),
}
}
fn raw(&self) -> Value {
match self {
Self::Text(text) => Value::String(text.clone()),
Self::Number(number) => Value::Number(number.clone()),
}
}
}
/// The progress token of one request parameters object, already validated and bounded. The adapters
@@ -133,28 +140,18 @@ pub(crate) struct ProgressEvent {
message: &'static str,
}
/// The pinned `notifications/progress` frame. Both supported revisions declare the same parameters,
/// so the projection is shared; the token is echoed verbatim and never validated as a credential.
pub(super) fn progress_frame(token: &ProgressToken, event: &ProgressEvent) -> Value {
let sdk_params = ProgressNotificationParam::new(
rmcp::model::ProgressToken(token.sdk_token()),
event.progress as f64,
)
.with_message(event.message);
let mut params = serde_json::to_value(sdk_params)
.expect("finite progress and string/number token are serializable SDK params");
#[cfg(test)]
super::dispatch_observer::sdk_progress_notification_serialized();
// SDK uses i64 tokens and f64 progress; restore the original JSON numbers before wire delivery.
if let ProgressToken::Number(number) = token {
params["progressToken"] = Value::Number(number.clone());
#[cfg(test)]
impl ProgressEvent {
pub(super) fn sdk_params(
&self,
token: &ProgressToken,
) -> rmcp::model::ProgressNotificationParam {
rmcp::model::ProgressNotificationParam::new(
rmcp::model::ProgressToken(token.sdk_token()),
self.progress as f64,
)
.with_message(self.message)
}
params["progress"] = Value::from(event.progress);
json!({
"jsonrpc": "2.0",
"method": "notifications/progress",
"params": params,
})
}
#[derive(Clone)]
@@ -421,7 +418,8 @@ pub(crate) async fn run_tool_call(
call,
events: progress_events,
cancel: lifecycle.cancel.clone(),
token,
peer: Some(super::rmcp_progress_peer::ProgressPeer::start(token.raw())),
outcome: None,
message,
protocol_version: protocol_version.clone(),
pending: VecDeque::new(),
@@ -474,7 +472,8 @@ struct CallStream {
call: Pin<Box<dyn Future<Output = super::rmcp_tools_runtime::Outcome> + Send>>,
events: mpsc::Receiver<ProgressEvent>,
cancel: CancelSignal,
token: ProgressToken,
peer: Option<super::rmcp_progress_peer::ProgressPeer>,
outcome: Option<super::rmcp_tools_runtime::Outcome>,
message: Value,
protocol_version: String,
pending: VecDeque<Event>,
@@ -511,55 +510,101 @@ async fn next_event_in_scope(
return None;
}
let step = {
let CallStream {
call,
events,
cancel,
..
} = &mut call;
tokio::select! {
biased;
Some(event) = events.recv() => Step::Progress(event),
outcome = call => Step::Outcome(outcome),
_ = cancel.cancelled() => Step::Cancelled,
}
};
loop {
let step = if let Some(outcome) = call.outcome.take() {
Step::Outcome(outcome)
} else {
let CallStream {
call,
events,
cancel,
..
} = &mut call;
tokio::select! {
biased;
Some(event) = events.recv() => Step::Progress(event),
outcome = call => Step::Outcome(outcome),
_ = cancel.cancelled() => Step::Cancelled,
}
};
match step {
Step::Progress(event) => {
let frame = progress_frame(&call.token, &event);
Some((Ok(Event::default().data(frame.to_string())), call))
}
Step::Outcome(outcome) => {
call.finished = true;
let payload = match sdk_tools_payload(&call.message, &call.protocol_version, outcome) {
ToolCommandPayload::Ok(payload) => payload,
ToolCommandPayload::Internal(_) => jsonrpc_internal_error(&call.message),
};
// The same projection point the single response uses, so a streamed answer delivers the
// same closed `notifications/message` (Story 3.18) before its terminal frame.
crate::transport::note_logging_outcome(&payload);
for notification in crate::protocol::take_notifications() {
match step {
Step::Progress(event) => {
let Some(peer) = &mut call.peer else { continue };
// One send in flight; keep driving the original domain future/deadline, never a task.
let delivery = {
let notify = peer.notify(event.progress, event.message);
tokio::pin!(notify);
loop {
tokio::select! {
biased;
result = &mut notify => break Some(result),
outcome = &mut call.call, if call.outcome.is_none() => {
let expired = is_deadline(&outcome);
call.outcome = Some(outcome);
if expired { break None; }
}
_ = call.cancel.cancelled() => return None,
}
}
};
match delivery {
Some(Ok(frame)) => {
return Some((Ok(Event::default().data(frame.to_string())), call));
}
// Progress is optional: no retry, handwritten fallback or replay of the Tool.
Some(Err(_)) | None => {
if let Some(peer) = call.peer.take() {
peer.finish().await;
}
call.events.close();
}
}
}
Step::Outcome(outcome) => {
call.finished = true;
if let Some(peer) = call.peer.take() {
peer.finish().await;
}
let payload =
match sdk_tools_payload(&call.message, &call.protocol_version, outcome) {
ToolCommandPayload::Ok(payload) => payload,
ToolCommandPayload::Internal(_) => jsonrpc_internal_error(&call.message),
};
// The same projection point the single response uses, so a streamed answer delivers the
// same closed `notifications/message` (Story 3.18) before its terminal frame.
crate::transport::note_logging_outcome(&payload);
for notification in crate::protocol::take_notifications() {
call.pending
.push_back(Event::default().data(notification.to_string()));
}
call.pending
.push_back(Event::default().data(notification.to_string()));
.push_back(Event::default().data(payload.to_string()));
return Some((Ok(call.pending.pop_front().expect("queued frame")), call));
}
Step::Cancelled => {
call.finished = true;
call.pending.clear();
return None;
}
call.pending
.push_back(Event::default().data(payload.to_string()));
Some((Ok(call.pending.pop_front().expect("queued frame")), call))
}
Step::Cancelled => {
call.finished = true;
call.pending.clear();
None
}
}
}
fn is_deadline(outcome: &super::rmcp_tools_runtime::Outcome) -> bool {
matches!(outcome, super::rmcp_tools_runtime::Outcome::Call(result)
if result.structured_content.as_ref().is_some_and(|value|
value["error"]["code"] == "upstream_timeout"))
}
fn jsonrpc_internal_error(message: &Value) -> Value {
crate::jsonrpc::jsonrpc_error(request_id(message), -32603, "internal server error")
}
#[cfg(test)]
#[path = "rmcp_progress_stream_tests.rs"]
mod stream_tests;
#[cfg(test)]
#[path = "lifecycle_tests.rs"]
mod tests;
@@ -2,13 +2,14 @@
//! progress queue, the pinned frame shape and the ownership of the in-flight cancellation table.
use super::*;
use serde_json::json;
fn token(value: Value) -> Option<ProgressToken> {
progress_token(&json!({ "_meta": { "progressToken": value } }))
}
#[test]
fn rmcp_progress_cancel_frame_preserves_serialized_numbers_and_omissions() {
#[tokio::test]
async fn rmcp_progress_cancel_frame_preserves_serialized_numbers_and_omissions() {
for wire_token in [
"\"token\"",
"-7",
@@ -19,13 +20,9 @@ fn rmcp_progress_cancel_frame_preserves_serialized_numbers_and_omissions() {
] {
let value: Value = serde_json::from_str(wire_token).unwrap();
for progress in [2, 9_007_199_254_740_993, u64::MAX] {
let frame = progress_frame(
&token(value.clone()).unwrap(),
&ProgressEvent {
progress,
message: "execution_dispatched",
},
);
let mut peer = super::super::rmcp_progress_peer::ProgressPeer::start(value.clone());
let frame = peer.notify(progress, "execution_dispatched").await.unwrap();
peer.finish().await;
assert_eq!(frame["params"]["progressToken"].to_string(), wire_token);
assert_eq!(
frame["params"]["progress"].to_string(),
@@ -123,14 +120,15 @@ async fn without_a_token_no_progress_is_produced_at_all() {
/// The pinned `notifications/progress` frame (schema `ProgressNotification`, both supported
/// revisions): `progressToken` echoed verbatim, `progress` increasing, `message` optional and
/// `total` absent because this server never invents one.
#[test]
fn the_progress_frame_matches_the_pinned_notification_shape() {
#[tokio::test]
async fn the_progress_frame_matches_the_pinned_notification_shape() {
let event = ProgressEvent {
progress: 2,
message: Phase::Authorized.message(),
};
let mut peer = super::super::rmcp_progress_peer::ProgressPeer::start(json!("tok-2"));
assert_eq!(
progress_frame(&ProgressToken::Text("tok-2".to_owned()), &event),
peer.notify(event.progress, event.message).await.unwrap(),
json!({
"jsonrpc": "2.0",
"method": "notifications/progress",
@@ -141,10 +139,13 @@ fn the_progress_frame_matches_the_pinned_notification_shape() {
}
})
);
peer.finish().await;
let mut peer = super::super::rmcp_progress_peer::ProgressPeer::start(json!(7));
assert_eq!(
progress_frame(&ProgressToken::Number(7.into()), &event)["params"]["progressToken"],
peer.notify(event.progress, event.message).await.unwrap()["params"]["progressToken"],
json!(7)
);
peer.finish().await;
}
#[tokio::test]
@@ -0,0 +1,250 @@
//! Request-owned outbound SDK peer for progress notifications.
use std::{
io,
sync::{Arc, Mutex},
};
use futures_util::{sink, stream};
use rmcp::{
RoleServer, ServerHandler,
model::{
ClientJsonRpcMessage, ProgressNotificationParam, ServerJsonRpcMessage, ServerNotification,
},
};
use serde_json::Value;
use tokio::sync::{mpsc, oneshot, watch};
#[cfg(test)]
#[path = "rmcp_progress_peer_controls.rs"]
pub(crate) mod controls;
const PROGRESS_TOKEN: &str = "progressToken";
// The SDK traces outbound params; client tokens must stay outside its pump.
const SDK_PROGRESS_TOKEN: &str = "crank-internal-progress";
struct Bootstrap;
impl ServerHandler for Bootstrap {}
struct SinkState {
sender: mpsc::Sender<Value>,
#[cfg(test)]
control: Option<controls::Controls>,
}
pub(super) struct ProgressPeer {
token: Value,
receiver: mpsc::Receiver<Value>,
shutdown: Option<oneshot::Sender<()>>,
owner: Option<tokio::task::JoinHandle<()>>,
peer: watch::Receiver<Option<rmcp::service::Peer<RoleServer>>>,
#[cfg(test)]
controls: Option<controls::Controls>,
progress_sidecar: Arc<Mutex<Option<u64>>>,
}
impl ProgressPeer {
pub(super) fn start(token: Value) -> Self {
#[cfg(test)]
let controls = controls::Controls::current();
let (sender, receiver) = mpsc::channel(1);
let owner_sender = sender.clone();
let (shutdown, shutdown_rx) = oneshot::channel();
let (peer_tx, peer) = watch::channel(None);
let progress_sidecar = Arc::new(Mutex::new(None::<u64>));
let sink_sidecar = progress_sidecar.clone();
let raw_token = token.clone();
let output = sink::unfold(
SinkState {
sender: sender.clone(),
#[cfg(test)]
control: controls.clone(),
},
move |state, message| {
let progress_sidecar = sink_sidecar.clone();
let raw_token = raw_token.clone();
async move {
#[cfg(test)]
if let Some(control) = &state.control {
control.entered();
if control.should_fail_send() {
return Err(io::Error::other("controlled progress send failure"));
}
}
let ServerJsonRpcMessage::Notification(notification) = &message else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"unexpected SDK progress output",
));
};
let ServerNotification::ProgressNotification(_) = &notification.notification
else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"unexpected SDK progress notification",
));
};
let mut actual = serde_json::to_value(&message).map_err(io::Error::other)?;
if actual["params"][PROGRESS_TOKEN] != SDK_PROGRESS_TOKEN {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"unexpected SDK progress token",
));
}
let progress = progress_sidecar
.lock()
.expect("progress sidecar lock")
.take()
.ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData, "missing progress sidecar")
})?;
actual["params"]["progress"] = Value::from(progress);
actual["params"][PROGRESS_TOKEN] = raw_token;
#[cfg(test)]
if state
.control
.as_ref()
.is_some_and(controls::Controls::should_mutate_message)
{
actual["params"]["message"] = Value::from("controlled output mutation");
}
state.sender.try_send(actual).map_err(|error| match error {
mpsc::error::TrySendError::Full(_) => {
io::Error::new(io::ErrorKind::WouldBlock, "progress output full")
}
mpsc::error::TrySendError::Closed(_) => {
io::Error::new(io::ErrorKind::BrokenPipe, "progress output closed")
}
})?;
#[cfg(test)]
if let Some(control) = &state.control {
control.queued();
control.wait_gate().await;
}
Ok::<_, io::Error>(state)
}
},
);
#[cfg(test)]
let owner_controls = controls.clone();
let owner = tokio::spawn(async move {
let running = rmcp::service::serve_directly(
Bootstrap,
(Box::pin(output), stream::pending::<ClientJsonRpcMessage>()),
None,
);
let peer = running.peer().clone();
let _ = peer_tx.send(Some(peer));
let stop = running.cancellation_token();
let pump = running.waiting();
tokio::pin!(pump);
let mut pump_done = false;
tokio::select! {
_ = owner_sender.closed() => {},
_ = shutdown_rx => {},
_ = &mut pump => { pump_done = true; },
}
#[cfg(test)]
if let Some(control) = &owner_controls {
control.release_sink();
}
stop.cancel();
if !pump_done {
let _ = pump.await;
}
#[cfg(test)]
if let Some(control) = owner_controls {
control.completed();
}
});
Self {
token,
receiver,
shutdown: Some(shutdown),
owner: Some(owner),
peer,
#[cfg(test)]
controls,
progress_sidecar,
}
}
pub(super) async fn notify(
&mut self,
progress: u64,
message: &'static str,
) -> Result<Value, io::Error> {
if !matches!(self.token, Value::String(_) | Value::Number(_)) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid progress token",
));
}
let sdk_token = rmcp::model::ProgressToken(rmcp::model::NumberOrString::String(
SDK_PROGRESS_TOKEN.into(),
));
let params =
ProgressNotificationParam::new(sdk_token, progress as f64).with_message(message);
*self.progress_sidecar.lock().expect("progress sidecar lock") = Some(progress);
let mut peer = self.peer.clone();
while peer.borrow().is_none() {
peer.changed().await.map_err(io::Error::other)?;
}
let peer = peer.borrow().as_ref().expect("peer initialized").clone();
#[cfg(test)]
if let Some(control) = &self.controls {
control.sent();
}
let send = peer.notify_progress(params);
tokio::pin!(send);
let mut output = None;
let send_result = loop {
tokio::select! {
result = &mut send => break result,
received = self.receiver.recv(), if output.is_none() => {
output = Some(received.ok_or_else(|| io::Error::new(io::ErrorKind::BrokenPipe, "progress output closed"))?);
#[cfg(test)]
if let Some(control) = &self.controls { control.received(); }
}
}
};
send_result.map_err(io::Error::other)?;
let actual = match output {
Some(output) => output,
None => self.receiver.recv().await.ok_or_else(|| {
io::Error::new(io::ErrorKind::BrokenPipe, "progress output closed")
})?,
};
#[cfg(test)]
if let Some(control) = &self.controls {
control.consumed();
}
#[cfg(test)]
super::dispatch_observer::sdk_progress_notification_serialized();
Ok(actual)
}
pub(super) async fn finish(mut self) {
self.close();
if let Some(owner) = self.owner.take() {
let _ = owner.await;
}
}
fn close(&mut self) {
self.receiver.close();
if let Some(shutdown) = self.shutdown.take() {
let _ = shutdown.send(());
}
}
}
impl Drop for ProgressPeer {
fn drop(&mut self) {
self.close();
}
}
#[cfg(test)]
#[path = "../rmcp_progress_peer_tests.rs"]
mod tests;
@@ -0,0 +1,103 @@
//! Request-local test controls for the actual SDK progress peer.
#![cfg(test)]
use std::sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
};
use tokio::sync::Semaphore;
tokio::task_local! {
static CURRENT: Controls;
}
#[derive(Clone, Default)]
pub(crate) struct Controls(Arc<State>);
#[derive(Default)]
struct State {
actual_send: AtomicUsize,
sink_entered: AtomicUsize,
output_consumed: AtomicUsize,
output_received: AtomicUsize,
output_queued: AtomicUsize,
owner_completed: AtomicUsize,
fail_send: AtomicBool,
mutate_message: AtomicBool,
gate: std::sync::OnceLock<Arc<Semaphore>>,
}
impl Controls {
/// Installs controls only for this request future; `ProgressPeer::start` captures them.
pub(crate) async fn scope<F: std::future::Future>(controls: Controls, future: F) -> F::Output {
CURRENT.scope(controls, future).await
}
pub(crate) fn actual_send(&self) -> usize {
self.0.actual_send.load(Ordering::SeqCst)
}
pub(crate) fn sink_entered(&self) -> usize {
self.0.sink_entered.load(Ordering::SeqCst)
}
pub(crate) fn output_consumed(&self) -> usize {
self.0.output_consumed.load(Ordering::SeqCst)
}
pub(crate) fn output_queued(&self) -> usize {
self.0.output_queued.load(Ordering::SeqCst)
}
pub(crate) fn output_received(&self) -> usize {
self.0.output_received.load(Ordering::SeqCst)
}
pub(crate) fn owner_completed(&self) -> usize {
self.0.owner_completed.load(Ordering::SeqCst)
}
pub(crate) fn fail_next_send(&self) {
self.0.fail_send.store(true, Ordering::SeqCst);
}
pub(crate) fn mutate_output_message(&self) {
self.0.mutate_message.store(true, Ordering::SeqCst);
}
pub(crate) fn hold_sink(&self) {
let _ = self.gate();
}
pub(crate) fn release_sink(&self) {
self.gate().close();
}
pub(super) fn current() -> Option<Self> {
CURRENT.try_with(Clone::clone).ok()
}
pub(super) fn sent(&self) {
self.0.actual_send.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn entered(&self) {
self.0.sink_entered.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn consumed(&self) {
self.0.output_consumed.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn queued(&self) {
self.0.output_queued.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn received(&self) {
self.0.output_received.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn completed(&self) {
self.0.owner_completed.fetch_add(1, Ordering::SeqCst);
}
pub(super) fn should_fail_send(&self) -> bool {
self.0.fail_send.swap(false, Ordering::SeqCst)
}
pub(super) fn should_mutate_message(&self) -> bool {
self.0.mutate_message.swap(false, Ordering::SeqCst)
}
pub(super) async fn wait_gate(&self) {
if let Some(gate) = self.0.gate.get() {
let _ = gate.acquire().await;
}
}
fn gate(&self) -> &Arc<Semaphore> {
self.0.gate.get_or_init(|| Arc::new(Semaphore::new(0)))
}
}
@@ -0,0 +1,183 @@
//! Request-stream ownership controls without an external service or a second domain task.
use super::super::rmcp_progress_peer::{ProgressPeer, controls::Controls};
use super::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
struct DomainLease(Arc<AtomicBool>);
impl Drop for DomainLease {
fn drop(&mut self) {
self.0.store(true, Ordering::SeqCst);
}
}
fn stream_call(
controls: &Controls,
deadline: bool,
) -> (
CallStream,
Arc<AtomicBool>,
Arc<InFlightCalls>,
InFlightScope,
) {
controls.hold_sink();
let dropped = Arc::new(AtomicBool::new(false));
let lease = DomainLease(dropped.clone());
let registry = Arc::new(InFlightCalls::new(1));
let scope = InFlightScope {
session_id: "owned".into(),
owner: "key".into(),
};
let entry = registry.register(&scope, &serde_json::json!(1)).unwrap();
let (lifecycle, events) = ToolCallLifecycle::new(Some(ProgressToken::Text("owned".into())));
let domain = async move {
let _lease = lease;
lifecycle.advance(Phase::Resolved);
if deadline {
tokio::time::sleep(Duration::from_millis(30)).await;
let mut result = rmcp::model::CallToolResult::error(vec![]);
result.structured_content =
Some(serde_json::json!({"error":{"code":"upstream_timeout"}}));
super::super::rmcp_tools_runtime::Outcome::Call(result)
} else {
std::future::pending().await
}
};
let call = CallStream {
call: Box::pin(domain),
events: events.unwrap(),
cancel: entry.cancel_signal(),
peer: Some(ProgressPeer::start(serde_json::json!("owned"))),
outcome: None,
message: serde_json::json!({"jsonrpc":"2.0","id":1}),
protocol_version: crate::jsonrpc::LEGACY_PROTOCOL_VERSION.into(),
pending: VecDeque::new(),
finished: false,
logging: crate::protocol::carry_scope(),
_in_flight: Some(entry),
};
(call, dropped, registry, scope)
}
async fn joined(controls: &Controls) {
tokio::time::timeout(Duration::from_secs(2), async {
while controls.owner_completed() != 1 {
tokio::task::yield_now().await;
}
})
.await
.expect("request owner must join SDK pump");
}
#[tokio::test]
async fn rmcp_progress_peer_unpolled_stream_drop_releases_owner_domain_and_registration() {
let controls = Controls::default();
Controls::scope(controls.clone(), async {
let (call, dropped, registry, scope) = stream_call(&controls, false);
drop(call);
joined(&controls).await;
assert!(dropped.load(Ordering::SeqCst));
assert_eq!(controls.actual_send(), 0);
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
})
.await;
}
#[tokio::test]
async fn rmcp_progress_peer_pending_send_cancel_stops_domain_and_joins_pump() {
let controls = Controls::default();
Controls::scope(controls.clone(), async {
let (call, dropped, registry, scope) = stream_call(&controls, false);
let next = next_event(call);
tokio::pin!(next);
tokio::select! {
_ = &mut next => panic!("held send cannot produce a frame"),
_ = async { while controls.sink_entered() == 0 { tokio::task::yield_now().await; } } => {}
}
assert!(registry.cancel(&scope, &serde_json::json!(1)));
assert!(tokio::time::timeout(Duration::from_secs(2), &mut next).await.unwrap().is_none());
joined(&controls).await;
assert!(dropped.load(Ordering::SeqCst));
assert_eq!(controls.output_consumed(), 0);
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
}).await;
}
#[tokio::test]
async fn rmcp_progress_peer_pending_send_body_drop_stops_domain_and_joins_pump() {
let controls = Controls::default();
Controls::scope(controls.clone(), async {
let (call, dropped, registry, scope) = stream_call(&controls, false);
let mut next = Box::pin(next_event(call));
tokio::time::timeout(Duration::from_secs(2), async {
tokio::select! {
_ = &mut next => panic!("held send cannot produce a frame"),
_ = async { while controls.sink_entered() == 0 { tokio::task::yield_now().await; } } => {}
}
}).await.unwrap();
drop(next);
joined(&controls).await;
assert!(dropped.load(Ordering::SeqCst));
assert_eq!(controls.output_consumed(), 0);
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
}).await;
}
#[tokio::test]
async fn rmcp_progress_peer_failure_after_first_frame_preserves_terminal_without_fallback() {
let controls = Controls::default();
Controls::scope(controls.clone(), async {
let (call, _, _, _) = stream_call(&controls, false);
controls.release_sink();
let (_, mut call) = next_event(call).await.expect("first actual SDK progress");
assert_eq!(controls.output_consumed(), 1);
controls.fail_next_send();
let (sender, events) = mpsc::channel(PROGRESS_CAPACITY);
sender
.try_send(ProgressEvent {
progress: 2,
message: "execution_dispatched",
})
.unwrap();
call.events = events;
call.call = Box::pin(async {
super::super::rmcp_tools_runtime::Outcome::Call(rmcp::model::CallToolResult::success(
vec![],
))
});
let (_, call) = next_event(call)
.await
.expect("original terminal after send error");
assert!(
call.finished,
"no fallback progress in place of failed SDK send"
);
assert!(call.peer.is_none());
assert_eq!(controls.actual_send(), 2);
assert_eq!(controls.output_consumed(), 1);
assert_eq!(controls.owner_completed(), 1);
assert!(next_event(call).await.is_none(), "terminal stays last");
})
.await;
}
#[tokio::test]
async fn rmcp_progress_peer_pending_send_keeps_original_domain_deadline() {
let controls = Controls::default();
Controls::scope(controls.clone(), async {
let (call, dropped, registry, scope) = stream_call(&controls, true);
let (_, call) = tokio::time::timeout(Duration::from_secs(2), next_event(call))
.await
.expect("domain deadline must not wait for held send")
.unwrap();
assert!(call.finished);
assert!(call.peer.is_none());
assert_eq!(controls.sink_entered(), 1);
assert_eq!(controls.output_consumed(), 0);
joined(&controls).await;
assert!(dropped.load(Ordering::SeqCst));
drop(call);
assert!(registry.register(&scope, &serde_json::json!(1)).is_some());
})
.await;
}
@@ -196,8 +196,7 @@ impl ServerHandler for RmcpTools {
event = async { match events.as_mut() { Some(events) => events.recv().await, None => std::future::pending().await } } => {
if let Some(event) = event {
if let Some(token) = &token {
let params = serde_json::from_value(super::lifecycle::progress_frame(token, &event)["params"].clone()).map_err(|_| McpError::internal_error("invalid progress event", None))?;
let _ = context.peer.notify_progress(params).await;
let _ = context.peer.notify_progress(event.sdk_params(token)).await;
}
} else { events = None; }
}
@@ -9,6 +9,7 @@ use tokio::sync::Semaphore;
pub(in crate::app::dispatch_observer) struct Endpoint {
pub registry: crank_registry::PostgresRegistry,
pub counters: Arc<CounterSet>,
pub peer_controls: crate::app::rmcp_progress_peer::controls::Controls,
pub sessions: Arc<CountedSessionStore>,
pub key: String,
pub other_key: String,
@@ -110,11 +111,18 @@ impl Endpoint {
Arc::new(crate::auth::CommunityMachineCredentialVerifier),
);
let scope = counters.clone();
let peer_controls = crate::app::rmcp_progress_peer::controls::Controls::default();
let peer_scope = peer_controls.clone();
let app = app.layer(axum::middleware::from_fn(
move |request: axum::extract::Request, next: axum::middleware::Next| {
let c = scope.clone();
let control = peer_scope.clone();
async move {
let response = observe(c.clone(), next.run(request)).await;
let response = crate::app::rmcp_progress_peer::controls::Controls::scope(
control,
observe(c.clone(), next.run(request)),
)
.await;
observe_body(response, c)
}
},
@@ -130,6 +138,7 @@ impl Endpoint {
Self {
registry,
counters,
peer_controls,
sessions,
key,
other_key,
@@ -8,6 +8,9 @@ use std::{sync::Arc, time::Duration};
pub(super) mod fixtures;
use fixtures::Endpoint;
#[path = "rmcp_progress_peer_runtime_tests.rs"]
mod peer_runtime;
fn observe_body(response: Response, counters: Arc<CounterSet>) -> Response {
let (parts, body) = response.into_parts();
let body = stream::unfold(
@@ -0,0 +1,77 @@
//! Actual SDK send/output controls through the ordinary HTTP/SSE consumer.
use super::*;
use std::sync::atomic::Ordering;
async fn peer_output_control(revision: &str) {
let h = Endpoint::started().await;
let session = h.session(revision).await;
let controls = &h.peer_controls;
controls.mutate_output_message();
h.gate.add_permits(1);
let reply = frames(
h.call(
revision,
session.as_deref(),
json!(61),
Some(json!(1.0)),
"text/event-stream",
)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(reply.len(), 4);
assert_eq!(
reply[0]["params"]["message"], "controlled output mutation",
"SSE must consume actual SDK output, not a parallel handwritten frame"
);
assert_eq!(reply[0]["params"]["progressToken"].to_string(), "1.0");
terminal(&reply, revision, json!(61));
assert_eq!(controls.actual_send(), 3);
assert_eq!(controls.sink_entered(), 3);
assert_eq!(controls.output_consumed(), 3);
assert_eq!(controls.owner_completed(), 1);
assert_eq!(h.started.load(Ordering::SeqCst), 1);
controls.fail_next_send();
h.gate.add_permits(1);
let reply = frames(
h.call(
revision,
session.as_deref(),
json!(62),
Some(json!("failed-send")),
"text/event-stream",
)
.send()
.await
.unwrap(),
)
.await;
assert_eq!(
reply.len(),
1,
"failed SDK send disables progress without fallback"
);
terminal(&reply, revision, json!(62));
assert_eq!(controls.actual_send(), 4, "no SDK retry/restart");
assert_eq!(controls.sink_entered(), 4);
assert_eq!(
controls.output_consumed(),
3,
"failed output cannot count as delivered"
);
assert_eq!(controls.owner_completed(), 2);
assert_eq!(h.started.load(Ordering::SeqCst), 2, "same Tool, no replay");
}
#[tokio::test]
async fn rmcp_progress_peer_runtime_legacy() {
peer_output_control(LEGACY_REVISION).await;
}
#[tokio::test]
async fn rmcp_progress_peer_runtime_modern() {
peer_output_control(MODERN_REVISION).await;
}
@@ -0,0 +1,218 @@
use super::ProgressPeer;
use super::controls::Controls;
use serde_json::{Value, json};
use std::{
io::Write,
sync::{Arc, Mutex},
};
#[derive(Clone, Default)]
struct LogCapture(Arc<Mutex<Vec<u8>>>);
impl Write for LogCapture {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for LogCapture {
type Writer = Self;
fn make_writer(&'a self) -> Self {
self.clone()
}
}
#[test]
fn sdk_trace_keeps_raw_string_and_numeric_tokens_private_without_disabling_logs() {
let capture = LogCapture::default();
let subscriber = tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.without_time()
.with_ansi(false)
.with_writer(capture.clone())
.finish();
let dispatch = tracing::Dispatch::new(subscriber);
let _guard = tracing::dispatcher::set_default(&dispatch);
// All spawned pump tasks run under this thread's TRACE capture, not just the caller.
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
for raw in [
"\"private-progress-canary-716283941\"",
"-716283941",
"1.572839416",
"1.0",
"-0.0",
"18446744073709551615",
"7.182736451928374651e+27",
] {
let token: Value = serde_json::from_str(raw).unwrap();
let mut peer = ProgressPeer::start(token.clone());
let output = peer.notify(u64::MAX, "running").await.unwrap();
assert_eq!(
output["params"]["progressToken"].to_string(),
token.to_string()
);
assert_eq!(output["params"]["progress"], json!(u64::MAX));
peer.finish().await;
}
tracing::info!("application-log-still-enabled");
});
let logs = String::from_utf8(capture.0.lock().unwrap().clone()).unwrap();
assert!(logs.contains("application-log-still-enabled"));
let notifications: Vec<_> = logs
.lines()
.filter(|line| line.contains("ProxyMessage(Notification"))
.collect();
assert_eq!(notifications.len(), 7, "actual SDK TRACE must be captured");
assert!(
notifications
.iter()
.all(|line| line.contains("ProgressToken(String(\"crank-internal-progress\"))"))
);
for raw in [
"private-progress-canary-716283941",
"-716283941",
"1.572839416",
"18446744073709551615",
"7.182736451928374651",
] {
assert!(!logs.contains(raw), "raw token leaked into SDK TRACE");
}
}
#[tokio::test]
async fn emits_real_sdk_progress_notification_with_exact_raw_numbers() {
let token: Value = serde_json::from_str("1.0").unwrap();
let mut peer = ProgressPeer::start(token);
let output = peer.notify(9_007_199_254_740_993, "running").await.unwrap();
assert_eq!(output["method"], "notifications/progress");
assert_eq!(output["params"]["progressToken"].to_string(), "1.0");
assert_eq!(
output["params"]["progress"],
json!(9_007_199_254_740_993u64)
);
assert_eq!(output["params"]["message"], "running");
peer.finish().await;
}
#[tokio::test]
async fn reports_output_send_failure_instead_of_claiming_delivery() {
let mut peer = ProgressPeer::start(json!(4));
peer.receiver.close();
assert!(peer.notify(2, "running").await.is_err());
peer.finish().await;
}
#[tokio::test]
async fn request_controls_observe_mutated_actual_sdk_output_and_owner_shutdown() {
let controls = Controls::default();
controls.mutate_output_message();
Controls::scope(controls.clone(), async {
let mut peer = ProgressPeer::start(json!(4));
let output = peer.notify(2, "running").await.unwrap();
assert_eq!(output["params"]["message"], "controlled output mutation");
peer.finish().await;
})
.await;
assert_eq!(controls.actual_send(), 1);
assert_eq!(controls.sink_entered(), 1);
assert_eq!(controls.output_consumed(), 1);
assert_eq!(controls.owner_completed(), 1);
}
#[tokio::test]
async fn failed_sdk_send_does_not_claim_output() {
let controls = Controls::default();
controls.fail_next_send();
Controls::scope(controls.clone(), async {
let mut peer = ProgressPeer::start(json!(4));
assert!(peer.notify(2, "running").await.is_err());
peer.finish().await;
})
.await;
assert_eq!(controls.actual_send(), 1);
assert_eq!(controls.sink_entered(), 1);
assert_eq!(controls.output_consumed(), 0);
}
#[tokio::test]
async fn sink_gate_holds_responder_after_output_and_cleans_up_on_release() {
let controls = Controls::default();
controls.hold_sink();
Controls::scope(controls.clone(), async {
let mut peer = ProgressPeer::start(json!(4));
{
let send = peer.notify(2, "running");
tokio::pin!(send);
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while controls.output_received() == 0 {
tokio::select! {
result = &mut send => panic!("send completed before gate release: {result:?}"),
_ = tokio::task::yield_now() => {}
}
}
}).await.expect("SDK output received while responder is gated");
assert_eq!(controls.output_consumed(), 0);
controls.release_sink();
assert!(send.await.is_ok());
}
peer.finish().await;
})
.await;
assert_eq!(controls.owner_completed(), 1);
}
#[tokio::test]
async fn dropping_pending_gated_send_releases_and_joins_owner() {
let controls = Controls::default();
controls.hold_sink();
Controls::scope(controls.clone(), async {
let mut peer = ProgressPeer::start(json!(4));
{
let send = peer.notify(2, "running");
tokio::pin!(send);
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while controls.output_queued() == 0 {
tokio::select! {
result = &mut send => panic!("send completed before drop: {result:?}"),
_ = tokio::task::yield_now() => {}
}
}
})
.await
.expect("SDK output queued before pending send drop");
}
peer.finish().await;
})
.await;
assert_eq!(controls.owner_completed(), 1);
}
#[tokio::test]
async fn raw_numeric_tokens_and_counts_survive_sdk_projection() {
for token in ["-7", "1.5", "-0", "18446744073709551615"] {
let token: Value = serde_json::from_str(token).unwrap();
let mut peer = ProgressPeer::start(token.clone());
let output = peer.notify(u64::MAX, "running").await.unwrap();
assert_eq!(output["params"]["progressToken"], token);
assert_eq!(output["params"]["progress"], json!(u64::MAX));
peer.finish().await;
}
let token: Value = serde_json::from_str("1.0").unwrap();
let mut peer = ProgressPeer::start(token.clone());
let output = peer.notify(9_007_199_254_740_993, "running").await.unwrap();
assert_eq!(output["params"]["progressToken"], token);
assert_eq!(
output["params"]["progress"],
json!(9_007_199_254_740_993u64)
);
peer.finish().await;
}
+2
View File
@@ -45,6 +45,8 @@ Logging-handler-срез #107 поверх rc.38/rmcp3.9.0 подключает
Progress/cancel-срез #107 на базе rc.23/rmcp3.9.0 подключает штатный SSE `lifecycle::progress_frame` обеих revisions к сериализации `ProgressNotificationParam`, а legacy `handle_cancelled_notification` — к декодированию `CancelledNotificationParam.request_id`. Локальная projection+restore сохраняет исходные JSON numbers токена/ID и целочисленный u64 progress, включая значения вне i64/exact-f64; message и omissions берутся из SDK. Decoder передаёт SDK только requestId, не валидируя reason/_meta/extensions. Session+credential ownership, bounded queues/slots, transport, auth и modern stream-close cancellation не меняются. Runtime ATDD подтверждает SDK seams отдельно от wire; unit regression проверяет точную числовую форму. Decoder ignore действует только после HTTP admission: `_meta:[]` сохраняет прежний HTTP400/-32020 до SDK decoder. Отдельная HTTP-регрессия проверяет отказ без отмены активного вызова, а допустимый `_meta:{}` с reason/extensions — успешную SDK-отмену. Это частичная миграция params, не завершение #107, feature qualification или production deployment.
Progress-Peer-срез #107 поверх rc.40/rmcp3.9.0 заменяет DTO-only `lifecycle::progress_frame`: действующий `build_app` SSE обеих revisions вызывает request-local `Peer::notify_progress`, потребляет actual SDK outbound notification и передаёт её прежнему SSE consumer. Один endpoint принадлежит body; single-flight send и output1 не расширяют producer queue8/drop-newest и cancellation table256. SDK получает только постоянный внутренний progress token, чтобы его TRACE не раскрывал клиентское значение; после проверки actual output token восстанавливаются исходный string/numeric token и точный u64 progress, а message/omissions остаются SDK output. Глобальные TRACE/application logs не отключаются. Domain Tool не выполняется второй раз: send failure отключает только progress, без retry/fallback. Pending send одновременно poll-ит прежний domain future и cancellation; исходный upstream timeout закрывает bridge без нового deadline budget. Drop body закрывает endpoint, managed owner отменяет и join-ит SDK pump. JSON/no-token/invalid-token/no-carrier не создают endpoint; carried logging и terminal projection остаются прежними. Это перенос outbound progress, не новый HTTP transport/session manager и не завершение #107; full/load/external-client qualification и production не входят в срез.
Catalog-change signals/subscriptions-срез #107 на базе rc.24/rmcp3.9.0 использует прежний `protocol/signals.rs` watcher: штатный modern `subscriptions/listen` декодирует notifications через `SubscriptionsListenRequestParams`/`SubscriptionFilter`, а acknowledgment и три list-change notification сериализуются настоящими SDK notification types в `protocol/signals_sdk.rs`. Тонкая входная projection сохраняет прежний запрет explicit null известных filter fields и игнорирование extensions; `_meta` по-прежнему проверяется только HTTP admission до decoder. SDK `NotificationMetaObject` хранит исходный JSON subscriptionId без сужения через RequestId; legacy frames остаются без params, modern ack первым сообщает только true accepted subset. URI-content subscriptions остаются unsupported, bounds32/2048 и64-byte id bound сохранены. Ближайший потребитель уже подключён: действующий `build_app` modern listen SSE и legacy GET SSE после committed catalog changes. HTTP ATDD независимо проверяет SDK execution и wire обеих revisions; прежние signals integration сохраняют Resource/Prompt/Tool delivery, scope, coalescing/recovery. Handwritten filter field DTO и frame builders заменены; fingerprint, watcher timing/queue/drop, auth/session/ownership/TTL/capabilities и transport не меняются. Полный #107, external-client/feature qualification и production deployment этим срезом не завершены.
Subscriptions-handler-срез #107 поверх rc.39/rmcp3.9.0 подключает modern `app/dispatch.rs::SubscriptionsListen` к единственному request-owned default `Service::handle_request` в `app/rmcp_subscriptions.rs`. Живой локальный SDK Peer обслуживает только outbound notifications: actual SDK ACK и последовательные `SubscriptionSink` list-change notifications поступают через `app/rmcp_subscriptions_output.rs` в прежнюю очередь4 и SSE consumer. Registry capabilities и SDK requested/candidate/advertised intersection определяют accepted subset; URI subscriptions не включаются. Единственный общий watcher сохраняет baseline до ACK, scoped catalog reads, Retry/Gone, cadence/coalescing и legacy delivery. JSON subscriptionId восстанавливается без числового сужения; SDK final Result/ErrorData потребляется owner, не отправляется новым terminal frame. Закрытие body/отмена до ACK дропает pending watcher и завершает/join-ит SDK pump; permit остаётся у body. Это runtime-перенос действующей подписки, не замена HTTP transport/session и не full/load/external-client qualification всего #107.