rswarm 0.1.8

A Rust implementation of the Swarm framework
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Agent-to-agent communication primitives.
//!
//! Provides typed message passing between agents within a single process via
//! [`InProcessChannel`], which uses Tokio broadcast channels for delivery and
//! oneshot channels for request-reply correlation.

use crate::error::{SwarmError, SwarmResult};
use crate::types::AgentRef;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{broadcast, oneshot, Mutex};
use uuid::Uuid;

// ---------------------------------------------------------------------------
// MessageId
// ---------------------------------------------------------------------------

/// Opaque unique identifier for a single [`AgentMessage`].
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct MessageId(Uuid);

impl MessageId {
    pub fn new() -> Self {
        MessageId(Uuid::new_v4())
    }
}

impl Default for MessageId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for MessageId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

// ---------------------------------------------------------------------------
// AgentMessage
// ---------------------------------------------------------------------------

/// A typed message envelope for agent-to-agent communication.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AgentMessage {
    pub id: MessageId,
    pub from: AgentRef,
    pub to: AgentRef,
    pub payload: Value,
    pub timestamp: DateTime<Utc>,
    /// Present on request messages; echoed by the replier so the requester
    /// can correlate the response via the pending-reply registry.
    pub correlation_id: Option<MessageId>,
    /// `true` when this message is a reply to a prior [`AgentChannel::request`] call.
    pub is_reply: bool,
}

impl AgentMessage {
    /// Create a new outbound message with a fresh [`MessageId`].
    pub fn new(from: AgentRef, to: AgentRef, payload: Value) -> Self {
        AgentMessage {
            id: MessageId::new(),
            from,
            to,
            payload,
            timestamp: Utc::now(),
            correlation_id: None,
            is_reply: false,
        }
    }

    /// Build a reply to `original`. Sets `is_reply = true` and echoes
    /// `original.id` as `correlation_id` so the requester can correlate it.
    pub fn reply(original: &AgentMessage, from: AgentRef, payload: Value) -> Self {
        AgentMessage {
            id: MessageId::new(),
            from,
            to: original.from.clone(),
            payload,
            timestamp: Utc::now(),
            correlation_id: Some(original.id.clone()),
            is_reply: true,
        }
    }
}

// ---------------------------------------------------------------------------
// AgentChannel trait
// ---------------------------------------------------------------------------

/// Typed async channel for agent-to-agent communication.
#[async_trait]
pub trait AgentChannel: Send + Sync {
    /// Send a message to the agent identified by `msg.to`.
    async fn send(&self, msg: AgentMessage) -> SwarmResult<()>;

    /// Receive the next message addressed to this agent.
    async fn recv(&self) -> SwarmResult<AgentMessage>;

    /// Send `msg` and await a correlated reply within `timeout`.
    ///
    /// Returns [`SwarmError::TimeoutError`] if no reply arrives in time.
    async fn request(&self, msg: AgentMessage, timeout: Duration) -> SwarmResult<AgentMessage>;

    /// The [`AgentRef`] this channel belongs to.
    fn agent_ref(&self) -> &AgentRef;
}

// ---------------------------------------------------------------------------
// ChannelRegistry
// ---------------------------------------------------------------------------

struct RegistryInner {
    senders: HashMap<AgentRef, broadcast::Sender<AgentMessage>>,
    pending_replies: HashMap<MessageId, oneshot::Sender<AgentMessage>>,
}

/// Shared registry for all in-process agent channels.
///
/// Holds broadcast senders (for normal messages) and oneshot senders (for
/// request-reply correlation). Create once with [`ChannelRegistry::new`] and
/// share via `Arc`.
pub struct ChannelRegistry {
    inner: Mutex<RegistryInner>,
}

impl ChannelRegistry {
    const BROADCAST_CAPACITY: usize = 64;

    pub fn new() -> Arc<Self> {
        Arc::new(ChannelRegistry {
            inner: Mutex::new(RegistryInner {
                senders: HashMap::new(),
                pending_replies: HashMap::new(),
            }),
        })
    }

    /// Register a new agent and return its broadcast receiver.
    ///
    /// Registration is exclusive while a live receiver exists for `agent`.
    /// Once all receivers are dropped, the same [`AgentRef`] may be registered
    /// again to establish a fresh mailbox.
    pub async fn register(
        &self,
        agent: &AgentRef,
    ) -> SwarmResult<broadcast::Receiver<AgentMessage>> {
        let mut guard = self.inner.lock().await;
        if let Some(existing) = guard.senders.get(agent) {
            if existing.receiver_count() > 0 {
                return Err(SwarmError::Other(format!(
                    "Agent '{}' is already registered",
                    agent
                )));
            }
        }
        let (tx, rx) = broadcast::channel(Self::BROADCAST_CAPACITY);
        guard.senders.insert(agent.clone(), tx);
        Ok(rx)
    }

    /// Route a message to its destination.
    ///
    /// If `msg.is_reply`, the message is delivered through the pending-reply
    /// oneshot keyed by `msg.correlation_id`. Otherwise it is broadcast to the
    /// registered channel for `msg.to`.
    pub async fn send(&self, msg: AgentMessage) -> SwarmResult<()> {
        tracing::debug!(
            from = %msg.from,
            to = %msg.to,
            message_id = %msg.id,
            is_reply = msg.is_reply,
            "routing agent message",
        );
        let mut guard = self.inner.lock().await;
        if msg.is_reply {
            let cid = msg.correlation_id.as_ref().ok_or_else(|| {
                SwarmError::Other("Reply message missing correlation_id".to_string())
            })?;
            let tx = guard.pending_replies.remove(cid).ok_or_else(|| {
                SwarmError::Other(format!(
                    "No pending request found for correlation id '{}'",
                    cid
                ))
            })?;
            tx.send(msg).map_err(|_| {
                SwarmError::Other("Reply receiver dropped before delivery".to_string())
            })
        } else {
            let to = msg.to.clone();
            match guard.senders.get(&to) {
                Some(tx) => tx.send(msg).map(|_| ()).map_err(|_| {
                    SwarmError::Other(format!("No active receivers for agent '{}'", to))
                }),
                None => Err(SwarmError::AgentNotFoundError(format!(
                    "No channel registered for agent '{}'",
                    to
                ))),
            }
        }
    }

    /// Send `msg` and await a correlated reply within `timeout`.
    ///
    /// This is the registry-level equivalent of [`AgentChannel::request`] and
    /// is useful for higher-level runtimes that want request-reply semantics
    /// without managing a dedicated [`InProcessChannel`] handle for the caller.
    pub async fn request(
        &self,
        mut msg: AgentMessage,
        timeout: Duration,
    ) -> SwarmResult<AgentMessage> {
        let correlation_id = msg.id.clone();
        msg.correlation_id = Some(correlation_id.clone());

        let reply_rx = self.register_pending(correlation_id.clone()).await;
        self.send(msg).await?;

        match tokio::time::timeout(timeout, reply_rx).await {
            Ok(Ok(reply)) => Ok(reply),
            Ok(Err(_)) => Err(SwarmError::Other(
                "Reply oneshot closed unexpectedly".to_string(),
            )),
            Err(_elapsed) => {
                self.cancel_pending(&correlation_id).await;
                Err(SwarmError::TimeoutError(format!(
                    "request timed out after {}ms",
                    timeout.as_millis()
                )))
            }
        }
    }

    /// Send one message to each distinct agent in `recipients`.
    ///
    /// Recipients are validated up front to avoid partial delivery when a
    /// target is missing or has no live receivers.
    pub async fn multicast(
        &self,
        from: AgentRef,
        recipients: impl IntoIterator<Item = AgentRef>,
        payload: Value,
    ) -> SwarmResult<Vec<MessageId>> {
        let mut seen = HashSet::new();
        let recipients: Vec<AgentRef> = recipients
            .into_iter()
            .filter(|recipient| seen.insert(recipient.clone()))
            .collect();

        let senders = {
            let guard = self.inner.lock().await;
            let mut senders = Vec::with_capacity(recipients.len());
            for recipient in &recipients {
                match guard.senders.get(recipient) {
                    Some(tx) if tx.receiver_count() > 0 => {
                        senders.push((recipient.clone(), tx.clone()));
                    }
                    Some(_) => {
                        return Err(SwarmError::Other(format!(
                            "No active receivers for agent '{}'",
                            recipient
                        )));
                    }
                    None => {
                        return Err(SwarmError::AgentNotFoundError(format!(
                            "No channel registered for agent '{}'",
                            recipient
                        )));
                    }
                }
            }
            senders
        };

        let mut message_ids = Vec::with_capacity(senders.len());
        for (recipient, tx) in senders {
            let msg = AgentMessage::new(from.clone(), recipient, payload.clone());
            let message_id = msg.id.clone();
            tx.send(msg).map_err(|_| {
                SwarmError::Other("A multicast recipient lost its active receiver".to_string())
            })?;
            message_ids.push(message_id);
        }

        Ok(message_ids)
    }

    /// Broadcast to every currently registered agent.
    pub async fn broadcast(
        &self,
        from: AgentRef,
        payload: Value,
        include_sender: bool,
    ) -> SwarmResult<Vec<MessageId>> {
        let recipients = {
            let guard = self.inner.lock().await;
            guard
                .senders
                .keys()
                .filter(|recipient| include_sender || **recipient != from)
                .cloned()
                .collect::<Vec<_>>()
        };
        self.multicast(from, recipients, payload).await
    }

    /// Register a pending-reply slot and return the receiver half.
    ///
    /// Call this *before* sending the request to avoid a race where the reply
    /// arrives before the slot is registered.
    pub async fn register_pending(&self, id: MessageId) -> oneshot::Receiver<AgentMessage> {
        let (tx, rx) = oneshot::channel();
        self.inner.lock().await.pending_replies.insert(id, tx);
        rx
    }

    /// Remove an orphaned pending-reply slot (e.g. after a timeout).
    pub async fn cancel_pending(&self, id: &MessageId) {
        self.inner.lock().await.pending_replies.remove(id);
    }
}

impl Default for ChannelRegistry {
    fn default() -> Self {
        ChannelRegistry {
            inner: Mutex::new(RegistryInner {
                senders: HashMap::new(),
                pending_replies: HashMap::new(),
            }),
        }
    }
}

// ---------------------------------------------------------------------------
// InProcessChannel
// ---------------------------------------------------------------------------

/// In-process implementation of [`AgentChannel`] backed by Tokio broadcast
/// and oneshot channels.
pub struct InProcessChannel {
    agent_ref: AgentRef,
    registry: Arc<ChannelRegistry>,
    /// Wrapped in `Mutex` so `recv()` can take `&self`.
    receiver: Mutex<broadcast::Receiver<AgentMessage>>,
}

impl InProcessChannel {
    pub async fn new(agent_ref: AgentRef, registry: Arc<ChannelRegistry>) -> SwarmResult<Self> {
        let receiver = registry.register(&agent_ref).await?;
        Ok(InProcessChannel {
            agent_ref,
            registry,
            receiver: Mutex::new(receiver),
        })
    }
}

#[async_trait]
impl AgentChannel for InProcessChannel {
    async fn send(&self, msg: AgentMessage) -> SwarmResult<()> {
        self.registry.send(msg).await
    }

    async fn recv(&self) -> SwarmResult<AgentMessage> {
        let mut guard = self.receiver.lock().await;
        loop {
            match guard.recv().await {
                Ok(msg) => {
                    tracing::debug!(
                        by = %self.agent_ref,
                        from = %msg.from,
                        message_id = %msg.id,
                        is_reply = msg.is_reply,
                        "received agent message",
                    );
                    return Ok(msg);
                }
                Err(broadcast::error::RecvError::Lagged(n)) => {
                    tracing::warn!(
                        agent = %self.agent_ref,
                        "Agent channel lagged, {} messages dropped",
                        n
                    );
                    continue;
                }
                Err(broadcast::error::RecvError::Closed) => {
                    return Err(SwarmError::Other("Agent channel closed".to_string()));
                }
            }
        }
    }

    async fn request(&self, msg: AgentMessage, timeout: Duration) -> SwarmResult<AgentMessage> {
        let destination = msg.to.clone();
        let correlation_id = msg.id.clone();
        match self.registry.request(msg, timeout).await {
            Ok(reply) => Ok(reply),
            Err(err) => {
                if matches!(err, SwarmError::TimeoutError(_)) {
                    tracing::warn!(
                        from = %self.agent_ref,
                        to = %destination,
                        correlation_id = %correlation_id,
                        timeout_ms = timeout.as_millis() as u64,
                        "agent request timed out",
                    );
                }
                Err(err)
            }
        }
    }

    fn agent_ref(&self) -> &AgentRef {
        &self.agent_ref
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    async fn make_channel(registry: Arc<ChannelRegistry>, name: &str) -> InProcessChannel {
        InProcessChannel::new(AgentRef::new(name), registry)
            .await
            .unwrap()
    }

    #[tokio::test]
    async fn test_send_recv() {
        let reg = ChannelRegistry::new();
        let alice = make_channel(reg.clone(), "alice").await;
        let bob = make_channel(reg.clone(), "bob").await;

        let msg = AgentMessage::new(
            AgentRef::new("alice"),
            AgentRef::new("bob"),
            json!({"hello": "world"}),
        );
        alice.send(msg.clone()).await.unwrap();

        let received = bob.recv().await.unwrap();
        assert_eq!(received.id, msg.id);
        assert_eq!(received.payload, json!({"hello": "world"}));
    }

    #[tokio::test]
    async fn test_request_reply() {
        let reg = ChannelRegistry::new();
        let alice = make_channel(reg.clone(), "alice").await;
        let bob = make_channel(reg.clone(), "bob").await;

        let reg2 = reg.clone();
        tokio::spawn(async move {
            let incoming = bob.recv().await.unwrap();
            let reply = AgentMessage::reply(&incoming, AgentRef::new("bob"), json!({"ack": true}));
            reg2.send(reply).await.unwrap();
        });

        let request = AgentMessage::new(
            AgentRef::new("alice"),
            AgentRef::new("bob"),
            json!({"ping": 1}),
        );
        let reply = alice
            .request(request, Duration::from_secs(2))
            .await
            .unwrap();
        assert_eq!(reply.payload, json!({"ack": true}));
        assert!(reply.is_reply);
        assert!(reply.correlation_id.is_some());
    }

    #[tokio::test]
    async fn test_request_timeout() {
        let reg = ChannelRegistry::new();
        let alice = make_channel(reg.clone(), "alice").await;
        let _bob = make_channel(reg.clone(), "bob").await; // registered but never replies

        let request = AgentMessage::new(
            AgentRef::new("alice"),
            AgentRef::new("bob"),
            json!({"ping": 1}),
        );
        let result = alice.request(request, Duration::from_millis(50)).await;
        assert!(matches!(result, Err(SwarmError::TimeoutError(ref s)) if s.contains("timed out")));
    }

    #[tokio::test]
    async fn test_send_to_unregistered_agent_errors() {
        let reg = ChannelRegistry::new();
        let alice = make_channel(reg.clone(), "alice").await;

        let msg = AgentMessage::new(AgentRef::new("alice"), AgentRef::new("nobody"), json!(null));
        let result = alice.send(msg).await;
        assert!(matches!(result, Err(SwarmError::AgentNotFoundError(_))));
    }

    #[tokio::test]
    async fn test_duplicate_registration_errors_while_original_is_alive() {
        let reg = ChannelRegistry::new();
        let _alice = make_channel(reg.clone(), "alice").await;

        let duplicate = InProcessChannel::new(AgentRef::new("alice"), reg.clone()).await;
        assert!(
            matches!(duplicate, Err(SwarmError::Other(ref s)) if s.contains("already registered"))
        );
    }

    #[tokio::test]
    async fn test_reregister_after_drop_is_allowed() {
        let reg = ChannelRegistry::new();
        {
            let _alice = make_channel(reg.clone(), "alice").await;
        }

        let alice = InProcessChannel::new(AgentRef::new("alice"), reg.clone())
            .await
            .expect("dropped receiver should allow fresh registration");
        let bob = make_channel(reg.clone(), "bob").await;
        bob.send(AgentMessage::new(
            AgentRef::new("bob"),
            AgentRef::new("alice"),
            json!({"hello": "again"}),
        ))
        .await
        .unwrap();

        let received = alice.recv().await.unwrap();
        assert_eq!(received.payload, json!({"hello": "again"}));
    }

    #[tokio::test]
    async fn test_reply_without_pending_slot_errors() {
        let reg = ChannelRegistry::new();
        let alice = make_channel(reg.clone(), "alice").await;
        let reply = AgentMessage {
            id: MessageId::new(),
            from: AgentRef::new("alice"),
            to: AgentRef::new("bob"),
            payload: json!({"ack": true}),
            timestamp: Utc::now(),
            correlation_id: Some(MessageId::new()),
            is_reply: true,
        };

        let result = alice.send(reply).await;
        assert!(
            matches!(result, Err(SwarmError::Other(ref s)) if s.contains("No pending request"))
        );
    }

    #[tokio::test]
    async fn test_multicast_delivers_to_requested_agents() {
        let reg = ChannelRegistry::new();
        let _alice = make_channel(reg.clone(), "alice").await;
        let bob = make_channel(reg.clone(), "bob").await;
        let carol = make_channel(reg.clone(), "carol").await;

        let ids = reg
            .multicast(
                AgentRef::new("alice"),
                vec![AgentRef::new("bob"), AgentRef::new("carol")],
                json!({"topic": "update"}),
            )
            .await
            .unwrap();

        assert_eq!(ids.len(), 2);
        assert_eq!(
            bob.recv().await.unwrap().payload,
            json!({"topic": "update"})
        );
        assert_eq!(
            carol.recv().await.unwrap().payload,
            json!({"topic": "update"})
        );
    }

    #[tokio::test]
    async fn test_broadcast_skips_sender_when_requested() {
        let reg = ChannelRegistry::new();
        let alice = make_channel(reg.clone(), "alice").await;
        let bob = make_channel(reg.clone(), "bob").await;
        let carol = make_channel(reg.clone(), "carol").await;

        let ids = reg
            .broadcast(AgentRef::new("alice"), json!({"kind": "broadcast"}), false)
            .await
            .unwrap();

        assert_eq!(ids.len(), 2);
        assert_eq!(
            bob.recv().await.unwrap().payload,
            json!({"kind": "broadcast"})
        );
        assert_eq!(
            carol.recv().await.unwrap().payload,
            json!({"kind": "broadcast"})
        );
        assert!(
            tokio::time::timeout(Duration::from_millis(25), alice.recv())
                .await
                .is_err()
        );
    }
}