appcore-peer-rpc 1.0.0

Peer-to-peer RPC contracts and validation for distributed AppCore.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// =============================================================================
//        #######
//     ###       ###     F: client.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/07/22 15:41:18 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/02 12:48:56 by dnettoRaw
//      ###########      S: 1.0.1-rc.8
// =============================================================================

use super::*;
use crate::transport::http_status_error;

/// Retry limits and exponential backoff bounds for peer requests.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PeerRpcRetryPolicy {
    /// Maximum attempts, including the initial request.
    pub max_attempts: usize,
    /// Delay before the first retry.
    pub initial_backoff_ms: u64,
    /// Maximum delay between attempts.
    pub max_backoff_ms: u64,
}

impl Default for PeerRpcRetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: 2,
            initial_backoff_ms: 50,
            max_backoff_ms: 500,
        }
    }
}

/// Runtime limits used by [`PeerRpcClient`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PeerRpcClientConfig {
    /// Per-attempt network timeout.
    pub request_timeout_ms: u64,
    /// Lifetime assigned to signed request envelopes.
    pub envelope_ttl_ms: u64,
    /// Maximum accepted response body size.
    pub max_response_bytes: usize,
    /// Retry behavior for transport failures.
    pub retry_policy: PeerRpcRetryPolicy,
}

impl Default for PeerRpcClientConfig {
    fn default() -> Self {
        Self {
            request_timeout_ms: 5_000,
            envelope_ttl_ms: 60_000,
            max_response_bytes: 1_048_576,
            retry_policy: PeerRpcRetryPolicy::default(),
        }
    }
}

/// HTTP request emitted through a [`PeerTransportProvider`].
#[derive(Clone, PartialEq, Eq)]
pub struct PeerRpcHttpRequest {
    /// HTTP method.
    pub method: String,
    /// Stable peer RPC endpoint path.
    pub path: String,
    /// Encoded request body.
    pub body: Vec<u8>,
    /// Optional bearer credential; debug output always redacts it.
    pub bearer_token: Option<String>,
    /// Per-attempt network timeout.
    pub timeout_ms: u64,
    /// Maximum accepted response body size.
    pub max_response_bytes: usize,
}

impl std::fmt::Debug for PeerRpcHttpRequest {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PeerRpcHttpRequest")
            .field("method", &self.method)
            .field("path", &self.path)
            .field("body_bytes", &self.body.len())
            .field(
                "bearer_token",
                &self.bearer_token.as_ref().map(|_| "REDACTED"),
            )
            .field("timeout_ms", &self.timeout_ms)
            .field("max_response_bytes", &self.max_response_bytes)
            .finish()
    }
}

/// Bounded response returned by a [`PeerTransportProvider`].
#[derive(Clone, PartialEq, Eq)]
pub struct PeerRpcHttpResponse {
    /// HTTP status code.
    pub status_code: u16,
    /// Raw response body.
    pub body: Vec<u8>,
}

impl std::fmt::Debug for PeerRpcHttpResponse {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PeerRpcHttpResponse")
            .field("status_code", &self.status_code)
            .field("body_bytes", &self.body.len())
            .finish()
    }
}

/// Peer transport provider used by RPC clients to reach a peer endpoint.
pub trait PeerTransportProvider: Send + Sync {
    /// Sends one request to the peer base URL.
    fn send(
        &self,
        base_url: &str,
        request: PeerRpcHttpRequest,
    ) -> Result<PeerRpcHttpResponse, PeerRpcError>;

    /// Sends one request with cooperative cancellation.
    fn send_cancellable(
        &self,
        base_url: &str,
        request: PeerRpcHttpRequest,
        cancellation: &CancellationToken,
    ) -> Result<PeerRpcHttpResponse, PeerRpcError> {
        if cancellation.is_cancelled() {
            return Err(PeerRpcError::EndpointUnavailable);
        }
        self.send(base_url, request)
    }
}

/// Authenticated client for stable peer query, command, and diagnostic endpoints.
pub struct PeerRpcClient<T, I> {
    source_identity: CoreIdentity,
    config: PeerRpcClientConfig,
    transport: T,
    token_issuer: I,
    cancellation: CancellationToken,
}
impl<T, I> PeerRpcClient<T, I>
where
    T: PeerTransportProvider,
    I: PeerRpcTokenIssuer,
{
    /// Creates an authenticated peer RPC client.
    pub fn new(
        source_identity: CoreIdentity,
        config: PeerRpcClientConfig,
        transport: T,
        token_issuer: I,
    ) -> Self {
        Self {
            source_identity,
            config,
            transport,
            token_issuer,
            cancellation: CancellationToken::new(),
        }
    }

    /// Replaces the shared cancellation token used by I/O and retry waits.
    pub fn with_cancellation_token(mut self, cancellation: CancellationToken) -> Self {
        self.cancellation = cancellation;
        self
    }

    /// Cancels active official transport I/O and future retries.
    pub fn cancel(&self) {
        self.cancellation.cancel();
    }

    /// Reports whether this client has been cancelled.
    pub fn is_cancelled(&self) -> bool {
        self.cancellation.is_cancelled()
    }

    /// Executes a peer query.
    pub fn query(
        &self,
        endpoint_url: &str,
        request: PeerRpcOutboundRequest,
    ) -> Result<PeerRpcResponse, PeerRpcError> {
        self.call_peer(endpoint_url, PeerRpcCallKind::Query, request)
    }

    /// Executes a peer command.
    pub fn command(
        &self,
        endpoint_url: &str,
        request: PeerRpcOutboundRequest,
    ) -> Result<PeerRpcResponse, PeerRpcError> {
        self.call_peer(endpoint_url, PeerRpcCallKind::Command, request)
    }

    /// Reads the authenticated peer health endpoint.
    pub fn health(&self, endpoint_url: &str) -> Result<PeerHealthResponse, PeerRpcError> {
        let request_id = format!("health-{}-{}", std::process::id(), now_ms());
        let token = self.token_issuer.issue_peer_token(
            &request_id,
            None,
            now_ms(),
            self.config.envelope_ttl_ms,
        )?;
        let response = self.send_with_retry(
            endpoint_url,
            PeerRpcHttpRequest {
                method: "GET".to_string(),
                path: PEER_HEALTH_PATH.to_string(),
                body: Vec::new(),
                bearer_token: Some(token),
                timeout_ms: self.config.request_timeout_ms,
                max_response_bytes: self.config.max_response_bytes,
            },
        )?;
        serde_json::from_slice(&response.body)
            .map_err(|error| PeerRpcError::InvalidResponse(error.to_string()))
    }

    /// Reads and returns the authenticated peer runtime manifest.
    /// Loads the versioned public advertisement exposed by a peer.
    pub fn advertisement(&self, endpoint_url: &str) -> Result<PeerAdvertisementV1, PeerRpcError> {
        let request_id = format!("manifest-{}-{}", std::process::id(), now_ms());
        let token = self.token_issuer.issue_peer_token(
            &request_id,
            None,
            now_ms(),
            self.config.envelope_ttl_ms,
        )?;
        let response = self.send_with_retry(
            endpoint_url,
            PeerRpcHttpRequest {
                method: "GET".to_string(),
                path: PEER_MANIFEST_PATH.to_string(),
                body: Vec::new(),
                bearer_token: Some(token),
                timeout_ms: self.config.request_timeout_ms,
                max_response_bytes: self.config.max_response_bytes,
            },
        )?;
        let response = serde_json::from_slice::<PeerManifestResponse>(&response.body)
            .map_err(|error| PeerRpcError::InvalidResponse(error.to_string()))?;
        Ok(response.advertisement)
    }

    fn build_envelope(&self, request: &PeerRpcOutboundRequest) -> PeerRpcEnvelope {
        let now = now_ms();
        let trace_id = request
            .trace
            .as_ref()
            .map(|trace| trace.trace_id.clone())
            .unwrap_or_else(|| request.request_id.clone());
        let mut envelope = PeerRpcEnvelope::new(
            request.request_id.clone(),
            trace_id,
            self.source_identity.core_id.clone(),
            request.target_core_id.clone(),
            self.source_identity.tenant_id.clone(),
            self.source_identity.cluster_id.clone(),
            now,
            now.saturating_add(self.config.envelope_ttl_ms.max(1)),
            next_outbound_nonce(&request.request_id, now),
            request.capability.clone(),
            request.payload.clone(),
            request.idempotency_key.clone(),
            request.trace.clone(),
        );
        envelope.protocol_version = self.source_identity.protocol_version;
        envelope
    }

    fn send_with_retry(
        &self,
        endpoint_url: &str,
        request: PeerRpcHttpRequest,
    ) -> Result<PeerRpcHttpResponse, PeerRpcError> {
        let attempts = self.config.retry_policy.max_attempts.max(1);
        let mut backoff_ms = self.config.retry_policy.initial_backoff_ms;
        let mut last_error = PeerRpcError::EndpointUnavailable;
        for attempt in 0..attempts {
            match self
                .transport
                .send_cancellable(endpoint_url, request.clone(), &self.cancellation)
            {
                Ok(response) if (200..300).contains(&response.status_code) => return Ok(response),
                Ok(response) => {
                    last_error = http_status_error(response.status_code, response.body);
                }
                Err(error) => last_error = error,
            }
            if attempt + 1 < attempts {
                if self
                    .cancellation
                    .wait_timeout(Duration::from_millis(backoff_ms))
                {
                    return Err(PeerRpcError::EndpointUnavailable);
                }
                backoff_ms = backoff_ms
                    .saturating_mul(2)
                    .min(self.config.retry_policy.max_backoff_ms);
            }
        }
        Err(last_error)
    }
}

impl<T, I> PeerRpcClientExecutor for PeerRpcClient<T, I>
where
    T: PeerTransportProvider,
    I: PeerRpcTokenIssuer,
{
    fn call_peer(
        &self,
        endpoint_url: &str,
        kind: PeerRpcCallKind,
        request: PeerRpcOutboundRequest,
    ) -> Result<PeerRpcResponse, PeerRpcError> {
        let configured_attempts = self.config.retry_policy.max_attempts.max(1);
        let attempts = if kind == PeerRpcCallKind::Command && request.idempotency_key.is_none() {
            1
        } else {
            configured_attempts
        };
        let mut backoff_ms = self.config.retry_policy.initial_backoff_ms;
        let mut last_error = PeerRpcError::EndpointUnavailable;

        for attempt in 0..attempts {
            if self.cancellation.is_cancelled() {
                return Err(PeerRpcError::EndpointUnavailable);
            }
            match self.call_peer_once(endpoint_url, kind, &request) {
                Ok(response) => return Ok(response),
                Err(error) => {
                    let retryable = peer_error_is_retryable(&error);
                    last_error = error;
                    if !retryable {
                        break;
                    }
                }
            }
            if attempt + 1 < attempts {
                if self
                    .cancellation
                    .wait_timeout(Duration::from_millis(backoff_ms))
                {
                    return Err(PeerRpcError::EndpointUnavailable);
                }
                backoff_ms = backoff_ms
                    .saturating_mul(2)
                    .min(self.config.retry_policy.max_backoff_ms);
            }
        }
        Err(last_error)
    }
}

impl<T, I> PeerRpcClient<T, I>
where
    T: PeerTransportProvider,
    I: PeerRpcTokenIssuer,
{
    fn call_peer_once(
        &self,
        endpoint_url: &str,
        kind: PeerRpcCallKind,
        request: &PeerRpcOutboundRequest,
    ) -> Result<PeerRpcResponse, PeerRpcError> {
        let envelope = self.build_envelope(request);
        let envelope_hash = envelope_signing_hash(&envelope);
        let token = self.token_issuer.issue_peer_token(
            &envelope.request_id,
            Some(&envelope_hash),
            now_ms(),
            self.config.envelope_ttl_ms,
        )?;
        let body = serde_json::to_vec(&envelope)
            .map_err(|error| PeerRpcError::InvalidEnvelope(error.to_string()))?;
        let response = self.transport.send_cancellable(
            endpoint_url,
            PeerRpcHttpRequest {
                method: "POST".to_string(),
                path: match kind {
                    PeerRpcCallKind::Query => PEER_QUERY_PATH,
                    PeerRpcCallKind::Command => PEER_COMMAND_PATH,
                }
                .to_string(),
                body,
                bearer_token: Some(token),
                timeout_ms: self.config.request_timeout_ms,
                max_response_bytes: self.config.max_response_bytes,
            },
            &self.cancellation,
        )?;
        if !(200..300).contains(&response.status_code) {
            return Err(http_status_error(response.status_code, response.body));
        }
        let response = serde_json::from_slice::<PeerRpcResponse>(&response.body)
            .map_err(|error| PeerRpcError::InvalidResponse(error.to_string()))?;
        if response.request_id != request.request_id {
            return Err(PeerRpcError::InvalidResponse(
                "peer response request_id mismatch".to_string(),
            ));
        }
        Ok(response)
    }
}

fn next_outbound_nonce(request_id: &str, now_ms: u64) -> String {
    // appcore-norm: allow(global-state) reason: atomic sequence prevents process-local nonce reuse
    static NONCE_COUNTER: AtomicU64 = AtomicU64::new(0);
    let counter = NONCE_COUNTER.fetch_add(1, Ordering::Relaxed);
    format!(
        "{}-{}-{}-{}",
        request_id,
        now_ms,
        std::process::id(),
        counter
    )
}

fn peer_error_is_retryable(error: &PeerRpcError) -> bool {
    matches!(
        error,
        PeerRpcError::EndpointUnavailable | PeerRpcError::Transport(_)
    )
}

pub(crate) fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_millis() as u64)
        .unwrap_or(0)
}