blob: 99935b232acc54c3c2ea2a2899bcd61a80234915 [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//! Lightweight Maka Host protocol client. QUIC streams and binary WebSocket
//! channels carry the same length-prefixed Host frames.
use crate::{RootId, link::Link};
use bytes::Bytes;
use futures_util::{Sink, Stream};
use maka_remote_pairing::HostPlatform;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::{Value, json, value::RawValue};
use std::{
borrow::Cow,
collections::BTreeMap,
io,
pin::Pin,
sync::Arc,
task::{Context, Poll, ready},
time::Duration,
};
use thiserror::Error;
use tokio::io::{
AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader, ReadBuf,
};
use tokio_tungstenite::{WebSocketStream, tungstenite::Message};
use uuid::Uuid;
const HOST_PROTOCOL_VERSION: u32 = 1;
const RESOURCE_CHUNK_LIMIT: usize = 64 * 1024;
// Local memory bound for untrusted replies, not a Host wire limit.
const MAX_CONTROL_FRAME_BYTES: usize = 16 * 1024 * 1024;
pub(crate) trait StreamIo: AsyncRead + AsyncWrite + Unpin + Send + Sync + 'static {}
impl<S: AsyncRead + AsyncWrite + Unpin + Send + Sync + 'static> StreamIo for S {}
pub(crate) type BoxedStream = Box<dyn StreamIo>;
#[derive(Debug, Error)]
pub enum RemoteClientError {
#[error("empty field '{0}'")]
EmptyField(&'static str),
#[error("transport I/O error: {0}")]
Io(#[from] io::Error),
#[error("remote transport failed: {0}")]
Transport(#[source] Box<dyn std::error::Error + Send + Sync>),
#[error(transparent)]
RelayAuth(#[from] maka_remote_transport::RelayAuthRejected),
#[error(transparent)]
Pairing(#[from] maka_remote_pairing::PairingError),
#[error("Host Root ID mismatch: expected '{expected}', Host advertised '{actual}'")]
RootIdMismatch { expected: String, actual: String },
#[error("unknown Root '{0}'")]
UnknownRoot(RootId),
#[error("operation '{0}' already identifies a different request")]
OperationConflict(Uuid),
#[error("operation '{0}' is pending; reconcile it if the original call was interrupted")]
OperationInProgress(Uuid),
#[error("operation '{0}' requires reconciliation")]
OperationNeedsReconciliation(Uuid),
#[error("operation '{0}' has a different Host reference")]
OperationReferenceChanged(Uuid),
#[error("unknown operation '{op_id}' for Root '{root_id}'")]
UnknownOperation { root_id: RootId, op_id: Uuid },
#[error("pending operation journal: {0}")]
Journal(#[from] rusqlite::Error),
#[error("pending operation journal task failed: {0}")]
JournalTask(#[from] tokio::task::JoinError),
#[error("pending operation journal is closed")]
JournalClosed,
#[error("unsupported pending operation journal schema {found}; expected at most {supported}")]
UnsupportedJournalSchema { found: i64, supported: i64 },
#[error("Host protocol violation: {0}")]
Protocol(String),
#[error("Host rejected call or control request: {0}")]
HostRejected(String),
#[error("peer access revoked")]
PeerAccessRevoked,
#[error("unknown Host endpoint '{0}'")]
UnknownEndpoint(String),
#[error("serialization error: {0}")]
Codec(#[from] serde_json::Error),
#[error("remote mutation was prevented before start")]
MutationNotExecuted,
#[error("Host mutation completed with cleanup error: {diagnostic}")]
MutationCleanup {
diagnostic: String,
reference: Value,
/// A recorded result, independent of post-mutation cleanup.
result: Box<Result<Value, RemoteClientError>>,
},
#[error("Host recorded the mutation result but cleanup is unconfirmed")]
MutationCleanupUnconfirmed {
reference: Value,
result: Box<Result<Value, RemoteClientError>>,
},
#[error("Host mutation result was not acknowledged: {cause}")]
MutationUncertain {
reference: Value,
#[source]
cause: Box<RemoteClientError>,
},
#[error(
"signed invitations require a native peer route; WebSocket requires a separately provisioned Bearer"
)]
NoNativeRoute,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoteDeploymentRef {
pub state_domain: String,
pub deployment: Uuid,
pub revision: u64,
}
#[derive(Serialize)]
struct Opening {
version: u32,
epoch: Option<Uuid>,
client_instance: Option<String>,
request: WireRequest,
}
#[derive(Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum WireRequest {
Connect,
FinalizeAccessForRoot {
expected_root_id: String,
},
Call {
session: Uuid,
},
PeerPairingInfo,
RedeemPeerPairing {
invite_secret: String,
device_label: Option<String>,
},
}
#[derive(Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum SessionControl {
Close,
}
#[derive(Deserialize)]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
enum WireResponse {
Connected(WireConnected),
Finalized(Result<(), String>),
Accepted,
Closed,
PeerAccessRevoked,
PeerPairingInfo(Option<HostPlatform>),
PeerPairingReceipt(Result<PeerPairingReceipt, String>),
#[serde(other)]
Other,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct PeerPairingReceipt {
pub root_id: String,
pub peer_id: String,
}
#[derive(Deserialize)]
struct WireConnected {
version: u32,
epoch: Uuid,
session: Uuid,
source: RemoteDeploymentRef,
consumers: BTreeMap<String, Vec<Uuid>>,
catalogue: WireCatalogue,
}
#[derive(Deserialize)]
struct WireCatalogue {
endpoints: BTreeMap<String, WireEndpoint>,
}
#[derive(Clone, Deserialize)]
struct WireEndpoint {
registration_epoch: Option<Uuid>,
}
#[derive(Serialize)]
struct WireCallSource {
deployment: RemoteDeploymentRef,
consumer: Vec<Uuid>,
mount: Uuid,
request: Uuid,
}
#[derive(Serialize)]
struct WireCallOpen {
endpoint: String,
registration_epoch: Uuid,
source: WireCallSource,
method: String,
input: Value,
}
/// Named, immutable request-local resources.
pub type Attachments = BTreeMap<String, ReadResource>;
/// An in-memory Host resource. Its contents are never serialized or formatted.
pub struct ReadResource(ResourceData);
enum ResourceData {
Utf8(String),
Binary(Bytes),
}
impl ReadResource {
pub fn utf8(text: String) -> Self {
Self(ResourceData::Utf8(text))
}
pub fn binary(bytes: Bytes) -> Self {
Self(ResourceData::Binary(bytes))
}
fn descriptor(&self) -> Value {
let (encoding, name, length) = match &self.0 {
ResourceData::Utf8(text) => ("utf8", "maka.text.utf8", text.len()),
ResourceData::Binary(bytes) => ("bytes", "maka.bytes", bytes.len()),
};
json!({
"encoding": encoding,
"resource": {"name": name, "version": 1},
"length": length,
"metadata": {},
})
}
async fn write_to(&self, stream: &mut (impl AsyncWrite + Unpin)) -> io::Result<()> {
let bytes = match &self.0 {
ResourceData::Utf8(text) => text.as_bytes(),
ResourceData::Binary(bytes) => bytes.as_ref(),
};
let mut start = 0;
while start < bytes.len() {
let end = start + (bytes.len() - start).min(RESOURCE_CHUNK_LIMIT);
let end = match &self.0 {
ResourceData::Utf8(text) => text.floor_char_boundary(end),
ResourceData::Binary(_) => end,
};
let chunk = &bytes[start..end];
stream.write_u32(chunk.len() as u32).await?;
stream.write_all(chunk).await?;
start = end;
}
Ok(())
}
}
#[derive(Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum WireControl {
Pull,
Cancel,
Confirm { recovery: Value },
}
#[derive(Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum WireReply {
Ready,
Subscribed,
Prepared,
Item {
value: Value,
},
Progress {
value: Value,
},
Finished {
result: Result<Value, String>,
cleanup: Result<(), String>,
},
Mutation {
reply: Value,
cleanup: Result<(), String>,
},
Aborted {
reason: String,
},
BindingRetired {
reason: String,
},
#[serde(other)]
Other,
}
pub(crate) struct WsByteChannel<S> {
socket: WebSocketStream<S>,
pending: Bytes,
flush: bool,
}
impl<S> WsByteChannel<S> {
pub(crate) fn new(socket: WebSocketStream<S>) -> Self {
Self {
socket,
pending: Bytes::new(),
flush: false,
}
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for WsByteChannel<S> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
output: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
if output.remaining() == 0 {
return Poll::Ready(Ok(()));
}
loop {
if !self.pending.is_empty() {
let count = self.pending.len().min(output.remaining());
output.put_slice(&self.pending.split_to(count));
return Poll::Ready(Ok(()));
}
if self.flush {
ready!(Pin::new(&mut self.socket).poll_flush(cx)).map_err(io::Error::other)?;
self.flush = false;
}
match ready!(Pin::new(&mut self.socket).poll_next(cx)) {
Some(Ok(Message::Binary(bytes))) => self.pending = bytes,
Some(Ok(Message::Ping(_))) => self.flush = true,
Some(Ok(Message::Pong(_))) => {}
Some(Ok(Message::Close(_))) | None => return Poll::Ready(Ok(())),
Some(Ok(_)) => {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::InvalidData,
"Host WebSocket requires binary messages",
)));
}
Some(Err(err)) => return Poll::Ready(Err(io::Error::other(err))),
}
}
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for WsByteChannel<S> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bytes: &[u8],
) -> Poll<io::Result<usize>> {
if bytes.is_empty() {
return Poll::Ready(Ok(0));
}
ready!(Pin::new(&mut self.socket).poll_ready(cx)).map_err(io::Error::other)?;
let len = bytes.len().min(RESOURCE_CHUNK_LIMIT);
Pin::new(&mut self.socket)
.start_send(Message::Binary(Bytes::copy_from_slice(&bytes[..len])))
.map_err(io::Error::other)?;
Poll::Ready(Ok(len))
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.socket)
.poll_flush(cx)
.map_err(io::Error::other)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.socket)
.poll_close(cx)
.map_err(io::Error::other)
}
}
async fn write_frame<W: AsyncWrite + Unpin, T: Serialize>(
writer: &mut W,
value: &T,
) -> Result<(), RemoteClientError> {
let encoded = serde_json::to_vec(value)?;
let length = u32::try_from(encoded.len())
.map_err(|_| RemoteClientError::Protocol("frame exceeds u32 length".into()))?;
writer.write_u32(length).await?;
writer.write_all(&encoded).await?;
writer.flush().await?;
Ok(())
}
async fn read_frame<R: AsyncRead + Unpin, T: DeserializeOwned>(
reader: &mut BufReader<R>,
) -> Result<T, RemoteClientError> {
read_optional_frame(reader)
.await?
.ok_or_else(|| io::Error::from(io::ErrorKind::UnexpectedEof).into())
}
async fn read_optional_frame<R: AsyncRead + Unpin, T: DeserializeOwned>(
reader: &mut BufReader<R>,
) -> Result<Option<T>, RemoteClientError> {
let mut prefix = [0u8; 4];
if reader.read(&mut prefix[..1]).await? == 0 {
return Ok(None);
}
if let Err(error) = reader.read_exact(&mut prefix[1..]).await {
return Err(if error.kind() == io::ErrorKind::UnexpectedEof {
RemoteClientError::Protocol("Host closed channel mid-frame header".into())
} else {
error.into()
});
}
let length = u32::from_be_bytes(prefix) as usize;
if !(1..=MAX_CONTROL_FRAME_BYTES).contains(&length) {
return Err(RemoteClientError::Protocol(format!(
"invalid Host frame length {length}"
)));
}
let mut body = Vec::with_capacity(length.min(64 * 1024));
while body.len() < length {
let chunk = reader.fill_buf().await?;
if chunk.is_empty() {
return Err(RemoteClientError::Protocol(
"Host closed channel mid-frame body".into(),
));
}
let take = chunk.len().min(length - body.len());
body.extend_from_slice(&chunk[..take]);
reader.consume(take);
}
Ok(Some(serde_json::from_slice(&body)?))
}
pub(crate) async fn pairing_platform(
link: &Link,
) -> Result<Option<HostPlatform>, RemoteClientError> {
tokio::time::timeout(Duration::from_secs(10), async {
let mut stream = link.open().await?;
write_frame(
&mut stream,
&Opening {
version: HOST_PROTOCOL_VERSION,
epoch: None,
client_instance: None,
request: WireRequest::PeerPairingInfo,
},
)
.await?;
match read_frame(&mut BufReader::new(stream)).await? {
WireResponse::PeerPairingInfo(platform) => Ok(platform),
_ => Err(RemoteClientError::Protocol(
"Host did not return pairing info".into(),
)),
}
})
.await
.map_err(|cause| io::Error::new(io::ErrorKind::TimedOut, cause))?
}
pub(crate) async fn redeem_pairing_invite(
link: &Link,
invite_secret: String,
device_label: Option<String>,
) -> Result<PeerPairingReceipt, RemoteClientError> {
let mut stream = link.open().await?;
write_frame(
&mut stream,
&Opening {
version: HOST_PROTOCOL_VERSION,
epoch: None,
client_instance: None,
request: WireRequest::RedeemPeerPairing {
invite_secret,
device_label,
},
},
)
.await?;
match read_frame(&mut BufReader::new(stream)).await? {
WireResponse::PeerPairingReceipt(result) => result.map_err(RemoteClientError::HostRejected),
_ => Err(RemoteClientError::Protocol(
"Host did not return a pairing receipt".into(),
)),
}
}
pub(crate) async fn finalize_websocket_access(
link: &Link,
client_instance: &str,
expected_root_id: &str,
) -> Result<(), RemoteClientError> {
let mut channel = link.open().await?;
write_frame(
&mut channel,
&Opening {
version: HOST_PROTOCOL_VERSION,
epoch: None,
client_instance: Some(client_instance.to_owned()),
request: WireRequest::FinalizeAccessForRoot {
expected_root_id: expected_root_id.to_owned(),
},
},
)
.await?;
match read_frame::<_, WireResponse>(&mut BufReader::new(channel)).await? {
WireResponse::Finalized(result) => result.map_err(RemoteClientError::HostRejected),
_ => Err(RemoteClientError::Protocol(
"Host did not return Finalized".into(),
)),
}
}
/// Connect and admit a remote Maka Host, enforcing that the advertised
/// state_domain matches expected_root_id fail-closed.
impl RemoteHostConnection {
pub(crate) async fn connect(
link: Link,
expected_root_id: &str,
client_instance: Arc<str>,
) -> Result<RemoteHostConnection, RemoteClientError> {
if expected_root_id.trim().is_empty() {
return Err(RemoteClientError::EmptyField("expected_root_id"));
}
if client_instance.trim().is_empty() {
return Err(RemoteClientError::EmptyField("client_instance"));
}
let handshake = async {
let mut channel = link.open().await?;
write_frame(
&mut channel,
&Opening {
version: HOST_PROTOCOL_VERSION,
epoch: None,
client_instance: Some(client_instance.to_string()),
request: WireRequest::Connect,
},
)
.await?;
let mut anchor = BufReader::new(channel);
match read_frame::<_, WireResponse>(&mut anchor).await? {
WireResponse::Connected(connected) => Ok((connected, anchor)),
WireResponse::PeerAccessRevoked => Err(RemoteClientError::PeerAccessRevoked),
_ => Err(RemoteClientError::Protocol(
"Host did not return Connected handshake".into(),
)),
}
}
.await;
let (connected, anchor) = handshake.map_err(|cause| {
if link.access_revoked() {
RemoteClientError::PeerAccessRevoked
} else {
cause
}
})?;
if connected.version != HOST_PROTOCOL_VERSION
|| connected.epoch.is_nil()
|| connected.session.is_nil()
{
return Err(RemoteClientError::Protocol(
"Host returned invalid protocol version, epoch, or session id".into(),
));
}
if connected.source.state_domain != expected_root_id {
return Err(RemoteClientError::RootIdMismatch {
expected: expected_root_id.to_owned(),
actual: connected.source.state_domain,
});
}
Ok(RemoteHostConnection {
handle: RemoteCallHandle {
link,
client_instance,
epoch: connected.epoch,
session: connected.session,
source: connected.source,
consumers: Arc::new(connected.consumers),
endpoints: Arc::new(connected.catalogue.endpoints),
},
anchor,
})
}
}
/// Lock-free, cloneable handle for opening independent authenticated RPC call
/// channels against an already admitted Host session.
#[derive(Clone)]
pub struct RemoteCallHandle {
link: Link,
client_instance: Arc<str>,
epoch: Uuid,
session: Uuid,
source: RemoteDeploymentRef,
consumers: Arc<BTreeMap<String, Vec<Uuid>>>,
endpoints: Arc<BTreeMap<String, WireEndpoint>>,
}
impl RemoteCallHandle {
pub fn root_id(&self) -> &str {
&self.source.state_domain
}
/// Open a sibling wire channel to this authenticated peer. Application
/// clients use the same transport while retaining their own admission.
pub async fn open_channel(
&self,
) -> Result<impl AsyncRead + AsyncWrite + Unpin + Send + Sync + 'static, RemoteClientError>
{
self.link.open().await
}
pub fn client_instance(&self) -> Arc<str> {
self.client_instance.clone()
}
pub fn is_authenticated_peer(&self) -> bool {
matches!(self.link, Link::Peer(_))
}
async fn open_call_channel(
&self,
endpoint: &str,
method: &str,
input: Value,
attachments: &Attachments,
request: Uuid,
) -> Result<BufReader<BoxedStream>, RemoteClientError> {
let Some(ep) = self.endpoints.get(endpoint) else {
return Err(RemoteClientError::UnknownEndpoint(endpoint.to_owned()));
};
let Some(registration_epoch) = ep.registration_epoch else {
return Err(RemoteClientError::Protocol(format!(
"endpoint '{endpoint}' is not registered on Host"
)));
};
let Some(consumer) = self.consumers.get(endpoint) else {
return Err(RemoteClientError::UnknownEndpoint(endpoint.to_owned()));
};
let mut channel = self.link.open().await?;
write_frame(
&mut channel,
&Opening {
version: HOST_PROTOCOL_VERSION,
epoch: Some(self.epoch),
client_instance: Some(self.client_instance.to_string()),
request: WireRequest::Call {
session: self.session,
},
},
)
.await?;
let mut reader = BufReader::new(channel);
if !matches!(
read_frame::<_, WireResponse>(&mut reader).await?,
WireResponse::Accepted
) {
return Err(RemoteClientError::Protocol(
"Host did not accept Call channel".into(),
));
}
let open_msg = WireCallOpen {
endpoint: endpoint.to_owned(),
registration_epoch,
source: WireCallSource {
deployment: self.source.clone(),
consumer: consumer.clone(),
mount: self.session,
request,
},
method: method.to_owned(),
input,
};
let descriptors: BTreeMap<_, _> = attachments
.iter()
.map(|(slot, resource)| (slot, resource.descriptor()))
.collect();
write_frame(reader.get_mut(), &open_msg).await?;
write_frame(reader.get_mut(), &descriptors).await?;
match read_frame::<_, WireReply>(&mut reader).await? {
WireReply::Ready => {}
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
_ => {
return Err(RemoteClientError::Protocol(
"Host did not reply Ready to call open".into(),
));
}
}
// The Host reserves the descriptors before accepting any payload bytes.
for resource in attachments.values() {
resource.write_to(reader.get_mut()).await?;
}
reader.get_mut().flush().await?;
Ok(reader)
}
/// Invoke a query and return its terminal result without retaining progress frames.
/// Prompt the live QUIC endpoint to refresh its network paths.
pub async fn network_change(&self) {
self.link.network_change().await;
}
pub fn path_summary(&self) -> Option<maka_remote_transport::PathSummary> {
self.link.path_summary()
}
pub async fn query(
&self,
endpoint: &str,
method: &str,
input: Value,
) -> Result<Value, RemoteClientError> {
let mut reader = self
.open_call_channel(endpoint, method, input, &Attachments::new(), Uuid::new_v4())
.await?;
loop {
match read_frame::<_, WireReply>(&mut reader).await? {
WireReply::Progress { .. } => {}
WireReply::Finished { result, cleanup } => {
cleanup.map_err(RemoteClientError::HostRejected)?;
return result.map_err(RemoteClientError::HostRejected);
}
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
_ => {
return Err(RemoteClientError::Protocol(
"unexpected Reply frame during Host query".into(),
));
}
}
}
}
/// Invoke a mutation method over the authenticated public Host gateway
/// (Open -> Ready -> Prepared -> Confirm -> Mutation) and return the
/// recorded handler output value. Transport failure after Confirm does
/// not imply rejection; the caller must reconcile before retrying.
pub async fn mutate(
&self,
endpoint: &str,
method: &str,
input: Value,
) -> Result<Value, RemoteClientError> {
self.mutate_with(endpoint, method, input, Attachments::new(), |_| {})
.await
}
/// Send request-local attachments out of band, and observe progress in
/// wire order. The terminal return still includes receipt/cleanup errors.
pub async fn mutate_with(
&self,
endpoint: &str,
method: &str,
input: Value,
attachments: Attachments,
on_progress: impl FnMut(Value),
) -> Result<Value, RemoteClientError> {
let mut on_progress = on_progress;
self.mutate_with_progress(endpoint, method, input, attachments, |value| {
on_progress(value);
std::future::ready(())
})
.await
}
/// Like [Self::mutate_with], with an async callback for each progress frame.
pub async fn mutate_with_progress<F: Future<Output = ()>>(
&self,
endpoint: &str,
method: &str,
input: Value,
attachments: Attachments,
on_progress: impl FnMut(Value) -> F,
) -> Result<Value, RemoteClientError> {
self.prepare_mutation(endpoint, method, input, attachments)
.await?
.confirm(on_progress)
.await
}
/// A prepared mutation has not executed. Dropping it without confirming
/// closes the call channel without submitting the write.
pub async fn prepare_mutation<'a>(
&self,
endpoint: &'a str,
method: &'a str,
input: Value,
attachments: Attachments,
) -> Result<PreparedMutation<'a>, RemoteClientError> {
self.prepare_mutation_with_request(
endpoint.into(),
method.into(),
input,
attachments,
Uuid::new_v4(),
)
.await
}
pub(crate) async fn prepare_mutation_with_request<'a>(
&self,
endpoint: Cow<'a, str>,
method: Cow<'a, str>,
input: Value,
attachments: Attachments,
request: Uuid,
) -> Result<PreparedMutation<'a>, RemoteClientError> {
let mut reader = self
.open_call_channel(&endpoint, &method, input, &attachments, request)
.await?;
drop(attachments);
let reply: Box<RawValue> = read_frame(&mut reader).await?;
let reference = match serde_json::from_str(reply.get())? {
WireReply::Prepared => {
#[derive(Deserialize)]
struct Prepared {
reference: Box<RawValue>,
}
serde_json::from_str::<Prepared>(reply.get())?.reference
}
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
WireReply::Finished { .. } | WireReply::Subscribed => {
return Err(RemoteClientError::Protocol(format!(
"endpoint method '{endpoint}.{method}' is not a mutation"
)));
}
_ => {
return Err(RemoteClientError::Protocol(
"Host did not reply Prepared for remote mutation".into(),
));
}
};
Ok(PreparedMutation {
reader,
endpoint,
method,
reference,
})
}
/// Open a demand-driven streaming subscription on an admitted Host endpoint
/// (Open -> Ready -> Subscribed -> pull/cancel loop).
pub async fn subscribe(
&self,
endpoint: &str,
method: &str,
input: Value,
) -> Result<RemoteSubscription, RemoteClientError> {
let mut reader = self
.open_call_channel(endpoint, method, input, &Attachments::new(), Uuid::new_v4())
.await?;
match read_frame::<_, WireReply>(&mut reader).await? {
WireReply::Subscribed => {}
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
WireReply::Finished { .. } | WireReply::Prepared => {
return Err(RemoteClientError::Protocol(format!(
"endpoint method '{endpoint}.{method}' is not a subscription"
)));
}
_ => {
return Err(RemoteClientError::Protocol(
"Host did not reply Subscribed for remote subscription".into(),
));
}
}
Ok(RemoteSubscription {
channel: Some(reader),
})
}
}
pub struct PreparedMutation<'a> {
reader: BufReader<BoxedStream>,
endpoint: Cow<'a, str>,
method: Cow<'a, str>,
reference: Box<RawValue>,
}
impl PreparedMutation<'_> {
/// Exact compact JSON emitted by the Host, including field order.
pub fn reference(&self) -> &RawValue {
&self.reference
}
pub async fn confirm<F: Future<Output = ()>>(
self,
on_progress: impl FnMut(Value) -> F,
) -> Result<Value, RemoteClientError> {
let reference = serde_json::from_str(self.reference.get())?;
self.confirm_with_reference(reference, on_progress).await
}
pub(crate) async fn confirm_with_reference<F: Future<Output = ()>>(
mut self,
reference: Value,
on_progress: impl FnMut(Value) -> F,
) -> Result<Value, RemoteClientError> {
confirm_mutation(
&mut self.reader,
&self.endpoint,
&self.method,
reference,
on_progress,
)
.await
}
}
async fn confirm_mutation<S: AsyncRead + AsyncWrite + Unpin, F: Future<Output = ()>>(
reader: &mut BufReader<S>,
endpoint: &str,
method: &str,
reference: Value,
mut on_progress: impl FnMut(Value) -> F,
) -> Result<Value, RemoteClientError> {
let recovery = json!({
"kind": "maka_remote_mutation",
"version": 1,
"endpoint": endpoint,
"method": method,
"reference": reference,
});
let result = async {
write_frame(reader.get_mut(), &WireControl::Confirm { recovery }).await?;
loop {
match read_frame::<_, WireReply>(reader).await? {
WireReply::Progress { value } => on_progress(value).await,
reply @ (WireReply::Mutation { .. }
| WireReply::Aborted { .. }
| WireReply::BindingRetired { .. }) => {
if let WireReply::Mutation { reply, .. } = &reply
&& reply.get("reference") != Some(&reference)
{
return Err(RemoteClientError::Protocol(
"Host returned a different operation reference".into(),
));
}
return Ok(reply);
}
_ => {
return Err(RemoteClientError::Protocol(
"unexpected Reply frame while awaiting mutation completion".into(),
));
}
}
}
}
.await;
let (reply, cleanup) = match result.map_err(|cause| RemoteClientError::MutationUncertain {
reference,
cause: Box::new(cause),
})? {
WireReply::Mutation { reply, cleanup } => (reply, cleanup),
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
_ => unreachable!("only terminal replies exit the mutation wait"),
};
mutation_reply_result(reply, Some(cleanup))
}
pub(crate) fn mutation_reply_result(
reply: Value,
completion_cleanup: Option<Result<(), String>>,
) -> Result<Value, RemoteClientError> {
let receipt = &reply["receipt"];
let unknown = match receipt["kind"].as_str() {
Some("unknown" | "unavailable" | "not_recorded") => Some(receipt),
Some("recorded")
if matches!(
receipt["outcome"]["kind"].as_str(),
Some("unknown" | "unresolved")
) =>
{
Some(&receipt["outcome"])
}
_ => None,
};
if let Some(unknown) = unknown {
return Err(RemoteClientError::MutationUncertain {
reference: reply["reference"].clone(),
cause: Box::new(RemoteClientError::Protocol(format!(
"Host receipt is non-terminal: {}",
unknown["diagnostic"]
.as_str()
.or_else(|| unknown["kind"].as_str())
.unwrap_or("unknown")
))),
});
}
let completion_confirmed = matches!(completion_cleanup, Some(Ok(())));
let cleanup = completion_cleanup.and_then(Result::err).or_else(|| {
let receipt_cleanup = &receipt["cleanup"];
(receipt_cleanup["kind"] == "failed").then(|| {
receipt_cleanup["diagnostic"]
.as_str()
.unwrap_or("mutation cleanup failed")
.to_owned()
})
});
if let Some(diagnostic) = cleanup {
let reference = reply["reference"].clone();
return Err(RemoteClientError::MutationCleanup {
diagnostic,
reference,
result: Box::new(extract_mutation_value(reply)),
});
}
match receipt["cleanup"]["kind"].as_str() {
Some("confirmed") => extract_mutation_value(reply),
// Only the original RPC has a separate cleanup acknowledgement.
None if completion_confirmed => extract_mutation_value(reply),
Some("unconfirmed") => Err(RemoteClientError::MutationCleanupUnconfirmed {
reference: reply["reference"].clone(),
result: Box::new(extract_mutation_value(reply)),
}),
kind => Err(RemoteClientError::MutationUncertain {
reference: reply["reference"].clone(),
cause: Box::new(RemoteClientError::Protocol(format!(
"unexpected mutation cleanup kind: {kind:?}"
))),
}),
}
}
fn extract_mutation_value(reply: Value) -> Result<Value, RemoteClientError> {
let receipt = reply.get("receipt").ok_or_else(|| {
RemoteClientError::Protocol("Host mutation reply missing receipt field".into())
})?;
match receipt.get("kind").and_then(Value::as_str) {
Some("recorded") => {
let outcome = receipt.get("outcome").ok_or_else(|| {
RemoteClientError::Protocol("recorded mutation receipt missing outcome".into())
})?;
match outcome.get("kind").and_then(Value::as_str) {
Some("returned") => {
let result = outcome.get("result").ok_or_else(|| {
RemoteClientError::Protocol("returned outcome missing result".into())
})?;
match result.get("kind").and_then(Value::as_str) {
Some("success") => Ok(result.get("value").cloned().unwrap_or(Value::Null)),
Some("failure") => {
let diagnostic = result
.get("diagnostic")
.and_then(Value::as_str)
.unwrap_or("remote mutation failed");
Err(RemoteClientError::HostRejected(diagnostic.to_owned()))
}
other => Err(RemoteClientError::Protocol(format!(
"unexpected mutation result kind: {other:?}"
))),
}
}
Some("not_executed") => Err(RemoteClientError::MutationNotExecuted),
other => Err(RemoteClientError::Protocol(format!(
"unexpected mutation outcome kind: {other:?}"
))),
}
}
other => Err(RemoteClientError::Protocol(format!(
"unexpected mutation receipt kind: {other:?}"
))),
}
}
/// Demand-driven remote Host subscription. Dropping the stream requests
/// cancellation; only a terminal Host reply acknowledges cleanup.
pub struct RemoteSubscription {
channel: Option<BufReader<BoxedStream>>,
}
impl RemoteSubscription {
/// Pull one item. If the caller cancels this future, the subscription is closed.
pub async fn next(&mut self) -> Result<Option<Value>, RemoteClientError> {
let Some(mut channel) = self.channel.take() else {
return Ok(None);
};
write_frame(channel.get_mut(), &WireControl::Pull).await?;
loop {
match read_frame::<_, WireReply>(&mut channel).await? {
WireReply::Item { value } => {
self.channel = Some(channel);
return Ok(Some(value));
}
WireReply::Progress { .. } => {}
WireReply::Finished { result, cleanup } => {
cleanup.map_err(RemoteClientError::HostRejected)?;
result.map_err(RemoteClientError::HostRejected)?;
return Ok(None);
}
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
_ => {
return Err(RemoteClientError::Protocol(
"unexpected Reply frame during remote subscription".into(),
));
}
}
}
}
/// Request cancellation and wait for the Host's cleanup acknowledgement.
pub async fn cancel(self) -> Result<(), RemoteClientError> {
let Some(mut channel) = self.channel else {
return Ok(());
};
tokio::time::timeout(Duration::from_secs(3), async {
write_frame(channel.get_mut(), &WireControl::Cancel).await?;
loop {
match read_frame::<_, WireReply>(&mut channel).await? {
WireReply::Item { .. } | WireReply::Progress { .. } => {}
WireReply::Finished { cleanup, .. } => {
return cleanup.map_err(RemoteClientError::HostRejected);
}
WireReply::Aborted { reason } | WireReply::BindingRetired { reason } => {
return Err(RemoteClientError::HostRejected(reason));
}
_ => {
return Err(RemoteClientError::Protocol(
"unexpected Reply frame during subscription cancellation".into(),
));
}
}
}
})
.await
.map_err(|error| io::Error::new(io::ErrorKind::TimedOut, error))?
}
}
/// Active admitted remote Host session holding the anchor control stream.
pub struct RemoteHostConnection {
handle: RemoteCallHandle,
anchor: BufReader<BoxedStream>,
}
impl RemoteHostConnection {
pub fn epoch(&self) -> Uuid {
self.handle.epoch
}
pub fn session(&self) -> Uuid {
self.handle.session
}
pub fn root_id(&self) -> &str {
self.handle.root_id()
}
pub fn source(&self) -> &RemoteDeploymentRef {
&self.handle.source
}
pub fn endpoint_names(&self) -> Vec<String> {
self.handle.endpoints.keys().cloned().collect()
}
/// Obtain a cloneable call handle that can run concurrent RPC calls without
/// locking the anchor session.
pub fn call_handle(&self) -> RemoteCallHandle {
self.handle.clone()
}
/// Invoke a query method on an admitted Host endpoint and return its JSON output.
pub async fn query(
&self,
endpoint: &str,
method: &str,
input: Value,
) -> Result<Value, RemoteClientError> {
self.handle.query(endpoint, method, input).await
}
/// Invoke a mutation method on an admitted Host endpoint and return its recorded output.
pub async fn mutate(
&self,
endpoint: &str,
method: &str,
input: Value,
) -> Result<Value, RemoteClientError> {
self.handle.mutate(endpoint, method, input).await
}
/// Invoke a mutation with out-of-band resources and a progress observer.
pub async fn mutate_with(
&self,
endpoint: &str,
method: &str,
input: Value,
attachments: Attachments,
on_progress: impl FnMut(Value),
) -> Result<Value, RemoteClientError> {
self.handle
.mutate_with(endpoint, method, input, attachments, on_progress)
.await
}
/// Open a demand-driven streaming subscription on an admitted Host endpoint.
pub async fn subscribe(
&self,
endpoint: &str,
method: &str,
input: Value,
) -> Result<RemoteSubscription, RemoteClientError> {
self.handle.subscribe(endpoint, method, input).await
}
/// Inspect the currently selected QUIC route. WebSocket has no QUIC path.
pub fn path_summary(&self) -> Option<maka_remote_transport::PathSummary> {
self.handle.link.path_summary()
}
/// Wait for anchor disconnection, including Host-side grant revocation.
pub async fn disconnected(&mut self) -> Result<(), RemoteClientError> {
// Both transports must observe the session anchor: QUIC can close one
// session without closing its shared peer connection.
let reply = read_optional_frame::<_, WireResponse>(&mut self.anchor);
let result = match &self.handle.link {
Link::Peer(peer) => tokio::select! {
biased;
error = peer.wait_for_disconnect() => return Err(error),
result = reply => result,
},
Link::WebSocket(_) => reply.await,
};
match result {
Err(error) => Err(error),
Ok(None | Some(WireResponse::Closed)) => Ok(()),
Ok(Some(WireResponse::PeerAccessRevoked)) => Err(RemoteClientError::PeerAccessRevoked),
Ok(_) => Err(RemoteClientError::Protocol(
"unexpected anchor response".into(),
)),
}
}
/// Gracefully detach this remote session without stopping the Host.
pub async fn close(mut self) -> Result<(), RemoteClientError> {
write_frame(self.anchor.get_mut(), &SessionControl::Close).await?;
loop {
match read_frame::<_, WireResponse>(&mut self.anchor).await? {
WireResponse::Closed => {
return Ok(());
}
WireResponse::Other => continue,
_ => {
return Err(RemoteClientError::Protocol(
"unexpected Host response while closing session".into(),
));
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
async fn assert_resource_chunks(resource: ReadResource, expected: &[u8], lengths: &[usize]) {
let mut wire = Vec::new();
resource.write_to(&mut wire).await.unwrap();
let mut wire = wire.as_slice();
let mut remaining = expected;
for &length in lengths {
assert_eq!(wire.read_u32().await.unwrap() as usize, length);
assert_eq!(&wire[..length], &remaining[..length]);
wire = &wire[length..];
remaining = &remaining[length..];
}
assert!(wire.is_empty());
assert!(remaining.is_empty());
}
#[tokio::test]
async fn binary_resources_keep_raw_bytes_and_chunk_at_the_wire_limit() {
for (size, chunks) in [
(0, &[][..]),
(65_536, &[65_536][..]),
(65_537, &[65_536, 1][..]),
] {
let bytes = Bytes::from((0..size).map(|n| (n + 255) as u8).collect::<Vec<_>>());
let resource = ReadResource::binary(bytes.clone());
assert_eq!(
resource.descriptor(),
json!({
"encoding": "bytes",
"resource": {"name": "maka.bytes", "version": 1},
"length": size,
"metadata": {},
})
);
assert_resource_chunks(resource, &bytes, chunks).await;
}
}
#[tokio::test]
async fn utf8_credential_chunks_end_at_character_boundaries() {
let credential = format!("{}🦀密钥", "x".repeat(65_535));
let resource = ReadResource::utf8(credential.clone());
assert_eq!(
resource.descriptor(),
json!({
"encoding": "utf8",
"resource": {"name": "maka.text.utf8", "version": 1},
"length": credential.len(),
"metadata": {},
})
);
assert_resource_chunks(resource, credential.as_bytes(), &[65_535, 10]).await;
}
#[tokio::test]
async fn only_eof_at_a_frame_boundary_is_a_clean_anchor_disconnect() {
let mut closed = BufReader::new(&[][..]);
assert!(
read_optional_frame::<_, WireResponse>(&mut closed)
.await
.unwrap()
.is_none()
);
for truncated in [&[0, 0][..], &[0, 0, 0, 20, b'{'][..]] {
let mut anchor = BufReader::new(truncated);
assert!(matches!(
read_optional_frame::<_, WireResponse>(&mut anchor).await,
Err(RemoteClientError::Protocol(_))
));
}
}
#[test]
fn legacy_hosts_cannot_dispatch_root_bound_finalization() {
#[derive(Debug, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
enum LegacyRequest {
FinalizeAccess,
}
// The old tagged unit variant accepted extra fields.
let old = json!({"kind":"finalize_access", "expected_root_id":"root"});
assert!(matches!(
serde_json::from_value(old),
Ok(LegacyRequest::FinalizeAccess)
));
let request = serde_json::to_value(WireRequest::FinalizeAccessForRoot {
expected_root_id: "root".into(),
})
.unwrap();
let error = serde_json::from_value::<LegacyRequest>(request).unwrap_err();
assert!(
error
.to_string()
.contains("unknown variant `finalize_access_for_root`")
);
}
#[tokio::test]
async fn lost_mutation_reply_preserves_the_reference_instead_of_asserting_failure() {
let (client, host) = tokio::io::duplex(2048);
let mut channel = BufReader::new(client);
let reference = json!({"operation":Uuid::new_v4()});
let (result, ()) = tokio::join!(
confirm_mutation(&mut channel, "agent", "send", reference.clone(), |_| {
std::future::ready(())
}),
async {
let confirm: Value = read_frame(&mut BufReader::new(host)).await.unwrap();
assert_eq!(confirm["kind"], "confirm");
assert_eq!(confirm["recovery"]["reference"], reference);
// The Host received Confirm, but the connection lost the receipt.
},
);
assert!(
matches!(result, Err(RemoteClientError::MutationUncertain { reference: saved, .. }) if saved == reference)
);
}
#[tokio::test]
async fn progress_does_not_hide_a_terminal_cleanup_error() {
let (client, host) = tokio::io::duplex(2048);
let mut channel = BufReader::new(client);
let reference = json!({"operation":Uuid::new_v4()});
let mut progress = Vec::new();
let (result, ()) =
tokio::join!(
confirm_mutation(
&mut channel,
"setup",
"discoverModels",
reference.clone(),
|value| {
progress.push(value);
std::future::ready(())
}
),
async {
let mut host = BufReader::new(host);
let _: Value = read_frame(&mut host).await.unwrap();
write_frame(
host.get_mut(),
&json!({"kind":"progress","value":{"step":"discovering"}}),
)
.await
.unwrap();
write_frame(
host.get_mut(),
&json!({
"kind":"mutation",
"reply":{"reference": reference, "receipt":{"kind":"recorded","outcome":{
"kind":"returned","result":{"kind":"success","value":{}}
}}},
"cleanup":{"Err":"fixture cleanup failed"},
}),
).await.unwrap();
},
);
assert_eq!(progress, [json!({"step":"discovering"})]);
assert!(
matches!(result, Err(RemoteClientError::MutationCleanup { diagnostic, reference: received, result }) if diagnostic == "fixture cleanup failed" && received == reference && matches!(result.as_ref(), Ok(value) if value == &json!({})))
);
}
#[tokio::test]
async fn nonterminal_host_receipts_preserve_the_original_reference() {
for receipt in [
json!({"kind":"not_recorded"}),
json!({"kind":"unavailable", "diagnostic":"busy"}),
json!({"kind":"recorded", "outcome":{"kind":"unknown"}, "cleanup":{"kind":"failed"}}),
json!({"kind":"recorded", "outcome":{"kind":"unresolved"}}),
] {
let (client, host) = tokio::io::duplex(2048);
let mut channel = BufReader::new(client);
let reference = json!({"operation":Uuid::new_v4()});
let (result, ()) = tokio::join!(
confirm_mutation(&mut channel, "agent", "send", reference.clone(), |_| {
std::future::ready(())
}),
async {
let mut host = BufReader::new(host);
let _: Value = read_frame(&mut host).await.unwrap();
write_frame(
host.get_mut(),
&json!({
"kind":"mutation",
"reply":{"reference": reference, "receipt": receipt},
"cleanup":{"Ok":null},
}),
)
.await
.unwrap();
},
);
assert!(
matches!(result, Err(RemoteClientError::MutationUncertain { reference: saved, .. }) if saved == reference)
);
}
}
}