liminal-rs 0.2.1

A conversation-based messaging bus built on beamr
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
use std::sync::Arc;
use std::time::{Duration, Instant};

use beamr::process::registry::ProcessHandle;

use crate::channel::ChannelMode;
use crate::envelope::Envelope;
use crate::error::LiminalError;
use crate::tracing::{ConversationSpan, FinishedSpan, TraceContext};

#[derive(Debug)]
pub struct Conversation {
    span: ConversationSpan,
}

impl Conversation {
    #[must_use]
    pub fn start(conversation_id: impl Into<String>) -> Self {
        Self {
            span: ConversationSpan::root(conversation_id),
        }
    }

    #[must_use]
    pub fn spawn_child(&self, conversation_id: impl Into<String>) -> Self {
        Self {
            span: self.span.child(conversation_id),
        }
    }

    #[must_use]
    pub const fn message<Payload>(&self, payload: Payload) -> ConversationMessage<Payload> {
        ConversationMessage::new(payload, self.span.message_context())
    }

    #[must_use]
    pub fn name(&self) -> &str {
        self.span.name()
    }

    #[must_use]
    pub const fn trace_context(&self) -> TraceContext {
        self.span.context()
    }

    #[must_use]
    pub const fn parent_trace_context(&self) -> Option<TraceContext> {
        self.span.parent()
    }

    #[must_use]
    pub fn finish(self) -> FinishedSpan {
        self.span.finish()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConversationMessage<Payload> {
    payload: Payload,
    trace_context: TraceContext,
}

impl<Payload> ConversationMessage<Payload> {
    const fn new(payload: Payload, trace_context: TraceContext) -> Self {
        Self {
            payload,
            trace_context,
        }
    }

    #[must_use]
    pub const fn trace_context(&self) -> TraceContext {
        self.trace_context
    }

    #[must_use]
    pub const fn payload(&self) -> &Payload {
        &self.payload
    }

    #[must_use]
    pub fn into_payload(self) -> Payload {
        self.payload
    }

    #[must_use]
    pub fn map<NextPayload>(
        self,
        map_payload: impl FnOnce(Payload) -> NextPayload,
    ) -> ConversationMessage<NextPayload> {
        ConversationMessage {
            payload: map_payload(self.payload),
            trace_context: self.trace_context,
        }
    }
}

/// PID of a beamr process participating in a conversation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ParticipantPid(u64);

impl ParticipantPid {
    /// Creates a participant PID wrapper from a raw beamr PID.
    #[must_use]
    pub const fn new(pid: u64) -> Self {
        Self(pid)
    }

    /// Returns the raw beamr PID.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }
}

impl From<u64> for ParticipantPid {
    fn from(pid: u64) -> Self {
        Self::new(pid)
    }
}

impl From<ProcessHandle> for ParticipantPid {
    fn from(handle: ProcessHandle) -> Self {
        Self::new(handle.pid())
    }
}

/// Policy applied when a linked participant exits unexpectedly.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CrashPolicy {
    /// Fail the conversation immediately.
    Fail,
    /// Record that routing should select another participant in a later brief.
    RouteToNext,
    /// Record that compensation should run in a later brief.
    Compensate,
}

/// Required configuration for a conversation actor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConversationConfig {
    /// Participant process identifiers linked to the conversation actor.
    pub participants: Vec<ParticipantPid>,
    /// Optional in-memory deadline for the conversation.
    pub timeout: Option<Duration>,
    /// Durability mode marker; durable persistence is implemented elsewhere.
    pub mode: ChannelMode,
    /// Participant-crash policy. This has no default and must be provided.
    pub on_crash: CrashPolicy,
}

impl ConversationConfig {
    /// Creates conversation configuration from all required fields.
    #[must_use]
    pub const fn new(
        participants: Vec<ParticipantPid>,
        timeout: Option<Duration>,
        mode: ChannelMode,
        on_crash: CrashPolicy,
    ) -> Self {
        Self {
            participants,
            timeout,
            mode,
            on_crash,
        }
    }
}

/// Lifecycle phase of a conversation actor.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConversationPhase {
    /// Actor exists but no exchange has started.
    Created,
    /// Messages are being exchanged.
    Active,
    /// Normal close has begun.
    Completing,
    /// Normal close finished.
    Closed,
    /// The conversation failed.
    Failed,
}

/// Liveness recorded for one linked participant.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParticipantHealth {
    /// The participant has not emitted an EXIT signal.
    Alive,
    /// The participant emitted an EXIT signal.
    Dead,
}

/// Participant liveness snapshot.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ParticipantStatus {
    /// Participant PID.
    pub participant: ParticipantPid,
    /// Last known participant liveness.
    pub health: ParticipantHealth,
    /// Instant the participant's EXIT signal was observed, set when the
    /// participant is marked dead. Replayed to a late exit-notifier registrant
    /// so a crash that lands before registration is not lost.
    pub exited_at: Option<Instant>,
}

impl ParticipantStatus {
    /// Creates an alive participant status.
    #[must_use]
    pub const fn alive(participant: ParticipantPid) -> Self {
        Self {
            participant,
            health: ParticipantHealth::Alive,
            exited_at: None,
        }
    }

    /// Marks this participant dead, recording the instant its EXIT was observed.
    pub const fn mark_dead_at(&mut self, at: Instant) {
        self.health = ParticipantHealth::Dead;
        self.exited_at = Some(at);
    }
}

/// Opaque conversation context accumulated by the actor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConversationContextEntry {
    /// Envelope delivered to the conversation.
    Sent(Envelope),
    /// Envelope delivered from the conversation to a receiver.
    Received(Envelope),
    /// Participant crash handled according to the configured policy.
    ParticipantCrashed {
        /// Crashed participant PID.
        participant: ParticipantPid,
        /// Policy applied by the actor.
        policy: CrashPolicy,
    },
}

/// Queryable state snapshot owned by a conversation actor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConversationState {
    /// Current lifecycle phase.
    pub current_phase: ConversationPhase,
    /// Accumulated exchanged messages and lifecycle references.
    pub context: Vec<ConversationContextEntry>,
    /// Optional in-memory deadline.
    pub deadline: Option<Instant>,
    /// Last known participant liveness.
    pub participants: Vec<ParticipantStatus>,
    /// Durability mode marker retained for diagnostics and future resume work.
    pub mode: ChannelMode,
}

impl ConversationState {
    /// Creates initial state for a conversation config.
    #[must_use]
    pub fn from_config(config: &ConversationConfig, now: Instant) -> Self {
        let deadline = config.timeout.map(|timeout| now + timeout);
        let participants = config
            .participants
            .iter()
            .copied()
            .map(ParticipantStatus::alive)
            .collect();

        Self {
            current_phase: ConversationPhase::Created,
            context: Vec::new(),
            deadline,
            participants,
            mode: config.mode,
        }
    }

    /// Transitions Created to Active. Active is accepted as idempotent.
    ///
    /// # Errors
    ///
    /// Returns [`LiminalError::ConversationFailed`] when the state cannot become active.
    pub fn activate(&mut self) -> Result<(), LiminalError> {
        match self.current_phase {
            ConversationPhase::Created => {
                self.current_phase = ConversationPhase::Active;
                Ok(())
            }
            ConversationPhase::Active => Ok(()),
            phase => Err(invalid_transition(phase, ConversationPhase::Active)),
        }
    }

    /// Transitions Active to Completing.
    ///
    /// # Errors
    ///
    /// Returns [`LiminalError::ConversationFailed`] when the state is not active.
    pub fn begin_completing(&mut self) -> Result<(), LiminalError> {
        match self.current_phase {
            ConversationPhase::Active => {
                self.current_phase = ConversationPhase::Completing;
                Ok(())
            }
            ConversationPhase::Completing => Ok(()),
            phase => Err(invalid_transition(phase, ConversationPhase::Completing)),
        }
    }

    /// Transitions Completing to Closed.
    ///
    /// # Errors
    ///
    /// Returns [`LiminalError::ConversationFailed`] when completion has not begun.
    pub fn close(&mut self) -> Result<(), LiminalError> {
        match self.current_phase {
            ConversationPhase::Completing => {
                self.current_phase = ConversationPhase::Closed;
                Ok(())
            }
            ConversationPhase::Closed => Ok(()),
            phase => Err(invalid_transition(phase, ConversationPhase::Closed)),
        }
    }

    /// Transitions any phase to Failed.
    pub const fn fail(&mut self) {
        self.current_phase = ConversationPhase::Failed;
    }

    /// Records an envelope sent into the conversation.
    pub fn record_sent(&mut self, envelope: Envelope) {
        self.context.push(ConversationContextEntry::Sent(envelope));
    }

    /// Records an envelope received from the conversation.
    pub fn record_received(&mut self, envelope: Envelope) {
        self.context
            .push(ConversationContextEntry::Received(envelope));
    }

    /// Records participant crash handling and marks that participant dead,
    /// stamping `exited_at` as the instant the EXIT signal was observed.
    pub fn record_participant_crash(
        &mut self,
        participant: ParticipantPid,
        policy: CrashPolicy,
        exited_at: Instant,
    ) {
        for status in &mut self.participants {
            if status.participant == participant {
                status.mark_dead_at(exited_at);
            }
        }
        self.context
            .push(ConversationContextEntry::ParticipantCrashed {
                participant,
                policy,
            });
    }
}

/// Cloneable handle for interacting with a conversation actor.
#[derive(Clone, Debug)]
pub struct ConversationHandle {
    backend: Arc<dyn ConversationHandleBackend>,
}

impl ConversationHandle {
    pub(crate) fn new(backend: Arc<dyn ConversationHandleBackend>) -> Self {
        Self { backend }
    }

    /// Sends a message envelope to the conversation actor.
    ///
    /// # Errors
    ///
    /// Returns a [`LiminalError`] when the actor cannot accept or process the message.
    pub fn send(&self, message: impl Into<Envelope>) -> Result<(), LiminalError> {
        self.backend.send(message.into())
    }

    /// Receives the next available envelope from the conversation actor.
    ///
    /// # Errors
    ///
    /// Returns a [`LiminalError`] when the actor is closed, failed, or unavailable.
    pub fn receive(&self) -> Result<Envelope, LiminalError> {
        self.backend.receive()
    }

    /// Closes the conversation normally.
    ///
    /// # Errors
    ///
    /// Returns a [`LiminalError`] when the actor cannot close normally.
    pub fn close(&self) -> Result<(), LiminalError> {
        self.backend.close()
    }

    /// Queries the actor state for diagnostics.
    ///
    /// # Errors
    ///
    /// Returns a [`LiminalError`] when the state cannot be queried.
    pub fn query_state(&self) -> Result<ConversationState, LiminalError> {
        self.backend.query_state()
    }

    /// Returns the current actor PID.
    ///
    /// # Errors
    ///
    /// Returns a [`LiminalError`] when the supervisor cannot inspect the actor.
    pub fn actor_pid(&self) -> Result<ParticipantPid, LiminalError> {
        self.backend.actor_pid()
    }
}

pub(crate) trait ConversationHandleBackend: std::fmt::Debug + Send + Sync {
    fn send(&self, message: Envelope) -> Result<(), LiminalError>;
    fn receive(&self) -> Result<Envelope, LiminalError>;
    fn close(&self) -> Result<(), LiminalError>;
    fn query_state(&self) -> Result<ConversationState, LiminalError>;
    fn actor_pid(&self) -> Result<ParticipantPid, LiminalError>;
}

fn invalid_transition(from: ConversationPhase, to: ConversationPhase) -> LiminalError {
    LiminalError::ConversationFailed {
        message: format!("invalid conversation phase transition from {from:?} to {to:?}"),
    }
}

#[cfg(test)]
mod tests {
    use super::{Conversation, ConversationHandle};

    #[test]
    fn starting_conversation_creates_named_span_with_fresh_trace_context() {
        let first = Conversation::start("conversation-1");
        let second = Conversation::start("conversation-2");

        assert_eq!(first.name(), "conversation-1");
        assert_eq!(first.parent_trace_context(), None);
        assert_ne!(first.trace_context().trace_id(), 0);
        assert_ne!(first.trace_context().span_id(), 0);
        assert_ne!(
            first.trace_context().trace_id(),
            second.trace_context().trace_id()
        );
    }

    #[test]
    fn messages_inherit_conversation_trace_context_automatically() {
        let conversation = Conversation::start("conversation");
        let message = conversation.message("payload");

        assert_eq!(message.payload(), &"payload");
        assert_eq!(message.trace_context(), conversation.trace_context());
    }

    #[test]
    fn child_conversation_references_parent_trace_context() {
        let parent = Conversation::start("parent");
        let child = parent.spawn_child("child");

        assert_eq!(child.name(), "child");
        assert_eq!(child.parent_trace_context(), Some(parent.trace_context()));
        assert_eq!(
            child.trace_context().trace_id(),
            parent.trace_context().trace_id()
        );
        assert_ne!(
            child.trace_context().span_id(),
            parent.trace_context().span_id()
        );
    }

    #[test]
    fn message_mapping_preserves_trace_context() {
        let conversation = Conversation::start("conversation");
        let context = conversation.trace_context();
        let mapped = conversation.message(1_u8).map(u16::from);

        assert_eq!(mapped.payload(), &1_u16);
        assert_eq!(mapped.trace_context(), context);
    }

    #[test]
    fn conversation_handle_is_clone_send_sync() {
        fn assert_clone_send_sync<T: Clone + Send + Sync>() {}

        assert_clone_send_sync::<ConversationHandle>();
    }
}