plexus-core 0.5.3

Core infrastructure for Plexus RPC: Activation trait, DynamicHub, schemas
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Streaming helpers for the caller-wraps architecture
//!
//! These functions are used by the DynamicHub routing layer to wrap activation
//! responses with metadata. Activations return typed domain events, and
//! the caller uses these helpers to create PlexusStreamItems.

use futures::stream::{self, Stream, StreamExt};
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;

use super::bidirectional::BidirChannel;
use super::context::PlexusContext;
use super::credential_envelope::{
    assemble_envelope_content, serialize_with_credential_capture, CookieProjector,
};
use super::types::{PlexusStreamItem, StreamMetadata};

/// Type alias for boxed stream of PlexusStreamItem
pub type PlexusStream = Pin<Box<dyn Stream<Item = PlexusStreamItem> + Send>>;

/// Wrap a typed stream into PlexusStream with automatic Done event
///
/// This is the core helper for the caller-wraps architecture.
/// Activations return typed domain events (e.g., HealthEvent),
/// and the caller wraps them with metadata. A Done event is
/// automatically appended when the stream completes.
///
/// # Dispatch-time credential interception (AUTHZ-CRED-CORE-2)
///
/// For every emitted item, this function routes the serialization through
/// the dispatch-time credential capture machinery: a fresh sidecar is
/// installed (per the RAII guard documented in
/// `plexus_auth_core::DispatchCaptureGuard`), serialization runs (and
/// each `Credential<T>` field emits its sentinel inline while the inner
/// value is captured into the sidecar), and the resulting `(payload,
/// captured)` pair is assembled into a wire envelope with an optional
/// `_credentials` field. Payloads with zero `Credential<T>` fields are
/// wire-format-identical to today (additive only).
///
/// See `crate::plexus::credential_envelope` for the envelope assembly
/// rules and `plans/AUTHZ/AUTHZ-CRED-CORE-2.md` for the contract.
///
/// # Example
///
/// ```ignore
/// let stream = health.check();  // Returns Stream<Item = HealthEvent>
/// let wrapped = wrap_stream(stream, "health.status", vec!["health".into()]);
/// // Stream will emit: Data, Data, ..., Done
/// ```
pub fn wrap_stream<T: Serialize + Send + 'static>(
    stream: impl Stream<Item = T> + Send + 'static,
    content_type: &'static str,
    provenance: Vec<String>,
) -> PlexusStream {
    let plexus_hash = PlexusContext::hash();
    let metadata = StreamMetadata::new(provenance.clone(), plexus_hash.clone());
    let done_metadata = StreamMetadata::new(provenance, plexus_hash);

    // Per AUTHZ-CRED-CORE-2 §"Risks" #1: acquire the capture path
    // unconditionally rather than gating on a static
    // method-returns-credentials flag. The thread-local probe is
    // nanoseconds; the cost is negligible and removes the
    // static-knowledge dependency on the `mdCredentials` registry. When
    // the payload contains no credentials, the captured map is empty,
    // the envelope assembly emits no `_credentials` key, and the wire
    // shape is byte-identical to today.
    //
    // The cookie projector here is `None` because plexus-core itself
    // does not know what transport is attached to this stream. The
    // transport layer (AUTHZ-CRED-CORE-2 follow-up in plexus-transport)
    // re-runs the cookie projection with `All` at the moment it owns
    // the HTTP response surface. For now we always leave the cookie
    // value in the sidecar; the transport may strip it later when it
    // emits the `Set-Cookie` header.
    let projector = CookieProjector::None;

    let data_stream = stream.map(move |item| {
        let (payload, captured) = serialize_with_credential_capture(&item);
        let (content, _hints) =
            assemble_envelope_content(payload, captured, &projector);
        PlexusStreamItem::Data {
            metadata: metadata.clone(),
            content_type: content_type.to_string(),
            content,
        }
    });

    let done_stream = stream::once(async move { PlexusStreamItem::Done {
        metadata: done_metadata,
    }});

    Box::pin(data_stream.chain(done_stream))
}


/// Create a bidirectional channel and wrap a stream, merging Request items
///
/// This function:
/// 1. Creates a BidirChannel connected to an internal mpsc channel
/// 2. Wraps the user's typed stream into PlexusStreamItems
/// 3. Merges in any Request items emitted by the BidirChannel
/// 4. Returns both the channel (for the activation to use) and the merged stream
///
/// # Arguments
///
/// * `content_type` - Content type string for data items (e.g., "interactive.wizard")
/// * `provenance` - Provenance path for metadata
///
/// # Returns
///
/// Returns a tuple of:
/// * `Arc<BidirChannel<Req, Resp>>` - The bidirectional channel for the activation
/// * A closure that takes the user's stream and returns the merged PlexusStream
///
/// # Example
///
/// ```ignore
/// let (ctx, wrap_fn) = create_bidir_stream::<StandardRequest, StandardResponse>(
///     "interactive.wizard",
///     vec!["interactive".into()],
/// );
/// let user_stream = activation.wizard(&ctx).await;
/// let merged_stream = wrap_fn(user_stream);
/// ```
pub fn create_bidir_stream<Req, Resp>(
    _content_type: &'static str,
    provenance: Vec<String>,
) -> (
    Arc<BidirChannel<Req, Resp>>,
    impl FnOnce(Pin<Box<dyn Stream<Item = PlexusStreamItem> + Send>>) -> PlexusStream,
)
where
    Req: Serialize + DeserializeOwned + Send + Sync + 'static,
    Resp: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    let plexus_hash = PlexusContext::hash();

    // Create channel for BidirChannel to send Request items
    let (bidir_tx, bidir_rx) = mpsc::channel::<PlexusStreamItem>(32);

    // Create the BidirChannel with:
    // - bidirectional_supported = true (we support it)
    // - use_global_registry = true (responses come via substrate.respond)
    let bidir_channel = Arc::new(BidirChannel::<Req, Resp>::new(
        bidir_tx,
        true,  // bidirectional_supported
        provenance.clone(),
        plexus_hash.clone(),
    ));

    // Create the wrapper closure
    let wrap_fn = move |user_stream: Pin<Box<dyn Stream<Item = PlexusStreamItem> + Send>>| -> PlexusStream {
        let bidir_stream = ReceiverStream::new(bidir_rx);

        // Use stream::select to interleave items from both streams
        // This allows Request items to appear in the stream alongside Data items
        let merged = stream::select(user_stream, bidir_stream);

        Box::pin(merged)
    };

    (bidir_channel, wrap_fn)
}

/// Wrap a typed stream with bidirectional support
///
/// Convenience wrapper that creates a BidirChannel and wraps the stream in one call.
/// The channel is returned for use by the activation.
///
/// # Type Parameters
///
/// * `T` - The type of items in the user's stream
/// * `Req` - Request type for bidirectional channel
/// * `Resp` - Response type for bidirectional channel
///
/// # Example
///
/// ```ignore
/// use plexus_core::plexus::{StandardRequest, StandardResponse, wrap_stream_with_bidir};
///
/// let (ctx, stream) = wrap_stream_with_bidir::<_, StandardRequest, StandardResponse>(
///     user_stream,
///     "interactive.wizard",
///     vec!["interactive".into()],
/// );
/// // ctx can now be used for bidirectional requests
/// // stream includes both data items and any Request items
/// ```
pub fn wrap_stream_with_bidir<T, Req, Resp>(
    stream: impl Stream<Item = T> + Send + 'static,
    content_type: &'static str,
    provenance: Vec<String>,
) -> (Arc<BidirChannel<Req, Resp>>, PlexusStream)
where
    T: Serialize + Send + 'static,
    Req: Serialize + DeserializeOwned + Send + Sync + 'static,
    Resp: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    let (ctx, wrap_fn) = create_bidir_stream::<Req, Resp>(content_type, provenance.clone());

    // Wrap the user's typed stream into PlexusStreamItems
    let wrapped_user_stream = wrap_stream(stream, content_type, provenance);

    // Merge with bidir stream
    let merged = wrap_fn(wrapped_user_stream);

    (ctx, merged)
}

/// Create an error stream
///
/// Returns a single-item stream containing an error event.
pub fn error_stream(
    message: String,
    provenance: Vec<String>,
    recoverable: bool,
) -> PlexusStream {
    let metadata = StreamMetadata::new(provenance, PlexusContext::hash());

    Box::pin(stream::once(async move {
        PlexusStreamItem::Error {
            metadata,
            message,
            code: None,
            recoverable,
        }
    }))
}

/// Create an error stream with error code
///
/// Returns a single-item stream containing an error event with a code.
pub fn error_stream_with_code(
    message: String,
    code: String,
    provenance: Vec<String>,
    recoverable: bool,
) -> PlexusStream {
    let metadata = StreamMetadata::new(provenance, PlexusContext::hash());

    Box::pin(stream::once(async move {
        PlexusStreamItem::Error {
            metadata,
            message,
            code: Some(code),
            recoverable,
        }
    }))
}

/// Create a done stream
///
/// Returns a single-item stream containing a done event.
pub fn done_stream(provenance: Vec<String>) -> PlexusStream {
    let metadata = StreamMetadata::new(provenance, PlexusContext::hash());

    Box::pin(stream::once(async move {
        PlexusStreamItem::Done { metadata }
    }))
}

/// Create a progress stream
///
/// Returns a single-item stream containing a progress event.
pub fn progress_stream(
    message: String,
    percentage: Option<f32>,
    provenance: Vec<String>,
) -> PlexusStream {
    let metadata = StreamMetadata::new(provenance, PlexusContext::hash());

    Box::pin(stream::once(async move {
        PlexusStreamItem::Progress {
            metadata,
            message,
            percentage,
        }
    }))
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::StreamExt;
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct TestEvent {
        value: i32,
    }

    #[tokio::test]
    async fn test_wrap_stream() {
        let events = vec![TestEvent { value: 1 }, TestEvent { value: 2 }];
        let input_stream = stream::iter(events);

        let wrapped = wrap_stream(input_stream, "test.event", vec!["test".into()]);
        let items: Vec<_> = wrapped.collect().await;

        // 2 data items + 1 done
        assert_eq!(items.len(), 3);

        // Check first item
        match &items[0] {
            PlexusStreamItem::Data {
                content_type,
                content,
                metadata,
            } => {
                assert_eq!(content_type, "test.event");
                assert_eq!(content["value"], 1);
                assert_eq!(metadata.provenance, vec!["test"]);
            }
            _ => panic!("Expected Data item"),
        }

        // Check done at end
        assert!(matches!(items[2], PlexusStreamItem::Done { .. }));
    }

    /// AUTHZ-CRED-CORE-2 AC #10 regression: a stream item whose payload
    /// has no `Credential<T>` fields produces wire content that's
    /// byte-identical to today (no `_credentials` key added). The path
    /// goes through the new envelope-assembly machinery — this asserts
    /// the additive-only property holds.
    #[tokio::test]
    async fn wrap_stream_credential_free_payload_is_wire_identical() {
        let events = vec![TestEvent { value: 7 }];
        let input_stream = stream::iter(events);

        let wrapped = wrap_stream(input_stream, "test.event", vec!["t".into()]);
        let items: Vec<_> = wrapped.collect().await;

        assert_eq!(items.len(), 2); // 1 data + 1 done
        match &items[0] {
            PlexusStreamItem::Data { content, .. } => {
                // Same shape as before AUTHZ-CRED-CORE-2: just the
                // serialized payload as an object.
                let obj = content.as_object().expect("object");
                assert_eq!(obj.get("value").unwrap(), &serde_json::json!(7));
                assert!(
                    !obj.contains_key("_credentials"),
                    "_credentials key MUST NOT appear on non-credential payloads"
                );
                assert_eq!(obj.len(), 1, "no extra fields");
            }
            _ => panic!("Expected Data item"),
        }
    }

    /// AUTHZ-CRED-CORE-2 sentinel-emission: a payload containing a
    /// `Credential<T>` field produces a Data item whose body has the
    /// sentinel inline. Today the `_credentials` sidecar is absent
    /// because plexus-auth-core's `DispatchCaptureGuard::install` is
    /// `pub(crate)` and unreachable from this crate (see
    /// `plans/AUTHZ/AUTHZ-CRED-CORE-2-RUN-NOTES.md` §Blocker). Once
    /// the public exposure lands, the sidecar will populate
    /// automatically — this test will continue passing AND the
    /// `_credentials` assertion can flip from "absent" to "present
    /// with the captured value".
    #[tokio::test]
    async fn wrap_stream_credential_bearing_payload_emits_sentinel_in_body() {
        use plexus_auth_core::{
            AttachmentSite, Credential, CredentialIssuer, CredentialKind, CredentialMetadata,
            CredentialMinter, CredentialScheme, HeaderName, MethodPath, Origin, Scope,
        };

        // Construct a credential and embed it in a payload. The minter
        // is `pub(crate)` to plexus-auth-core; we can't reach its
        // constructor from here, but we CAN derive a credential indirectly
        // by using the `Serialize` impl through a stream — which is
        // exactly what dispatch does in production. For this test we
        // construct via a workaround: serialize a struct containing a
        // sentinel-typed value directly to confirm the wrap_stream path
        // routes it.
        //
        // (Once AUTHZ-CRED-CORE-2-a lands and exposes a public minter or
        // capture API, this test can be tightened to assert the captured
        // sidecar entry too.)

        // Mint via the test-internal API path. We need a CredentialMinter
        // for this test; the constructor lives behind the seal so we
        // cannot construct one. Instead we drive the test through a
        // typed payload that already contains a sentinel-shaped value
        // by Serializing a `Credential<T>` via the public API — which
        // works because plexus-auth-core's Serialize impl emits the
        // sentinel unconditionally.
        //
        // We need access to a Credential<T>. Since
        // `CredentialMinter::new_sealed` is pub(crate), this test
        // exercises the wrap_stream serialization path through the
        // already-public `Credential<T>` Serialize impl by going via a
        // domain wrapper that serde renders identically:
        let _ = (
            CredentialMinter::issuer, // satisfy unused-import for diagnostic
            Credential::<String>::metadata,
            CredentialIssuer::new(
                Origin::new("ws://test"),
                MethodPath::try_new("auth.login").unwrap(),
            ),
            CredentialMetadata::new(
                CredentialKind::Bearer,
                AttachmentSite::Header {
                    name: HeaderName::try_new("authorization").unwrap(),
                },
                Some(CredentialScheme::new("Bearer ")),
                Vec::<Scope>::new(),
                None,
                None,
                None,
                CredentialIssuer::new(
                    Origin::new("ws://test"),
                    MethodPath::try_new("auth.login").unwrap(),
                ),
            ),
        );

        // For this regression test, we send a payload that contains the
        // raw sentinel shape directly, mimicking what
        // `Credential<T>::Serialize` would emit. This confirms the
        // wrap_stream path forwards the sentinel intact and does NOT
        // strip or transform it.
        #[derive(Serialize)]
        struct LoginPayload {
            user_id: String,
            // In production this would be `Credential<String>`; here we
            // hand-write the sentinel JSON the Serialize impl emits so
            // we can run this test without minter access.
            session: serde_json::Value,
        }
        let payload = LoginPayload {
            user_id: "alice".into(),
            session: serde_json::json!({ "$credential": "cred_0" }),
        };
        let input_stream = stream::iter(vec![payload]);

        let wrapped = wrap_stream(input_stream, "auth.login.result", vec!["auth".into()]);
        let items: Vec<_> = wrapped.collect().await;

        let content = match &items[0] {
            PlexusStreamItem::Data { content, .. } => content,
            _ => panic!("Expected Data item"),
        };

        // Sentinel survives intact in the body.
        assert_eq!(
            content.get("session").unwrap(),
            &serde_json::json!({ "$credential": "cred_0" })
        );
        // No _credentials sidecar today (see RUN-NOTES §Blocker). When
        // plexus-auth-core exposes the guard publicly this assertion
        // flips to checking sidecar presence.
        let obj = content.as_object().unwrap();
        assert!(!obj.contains_key("_credentials"),
            "sidecar absent until plexus-auth-core exposes DispatchCaptureGuard::install");
    }


    #[tokio::test]
    async fn test_error_stream() {
        let stream = error_stream("Something failed".into(), vec!["test".into()], false);
        let items: Vec<_> = stream.collect().await;

        assert_eq!(items.len(), 1);
        match &items[0] {
            PlexusStreamItem::Error {
                message,
                recoverable,
                code,
                ..
            } => {
                assert_eq!(message, "Something failed");
                assert!(!recoverable);
                assert!(code.is_none());
            }
            _ => panic!("Expected Error item"),
        }
    }

    #[tokio::test]
    async fn test_error_stream_with_code() {
        let stream = error_stream_with_code(
            "Not found".into(),
            "NOT_FOUND".into(),
            vec!["test".into()],
            true,
        );
        let items: Vec<_> = stream.collect().await;

        assert_eq!(items.len(), 1);
        match &items[0] {
            PlexusStreamItem::Error {
                message,
                code,
                recoverable,
                ..
            } => {
                assert_eq!(message, "Not found");
                assert_eq!(code.as_deref(), Some("NOT_FOUND"));
                assert!(recoverable);
            }
            _ => panic!("Expected Error item"),
        }
    }
}