clawft-core 0.2.1

Core engine for clawft (agent loop, message bus, context management)
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
//! Inter-agent communication bus.
//!
//! [`AgentBus`] provides per-agent inboxes for agent-to-agent messaging
//! with TTL enforcement and agent-scoped delivery (agents can only
//! read from their own inbox).
//!
//! # Security
//!
//! - Messages are tagged with agent IDs.
//! - Agents cannot read other agents' messages (inbox scoping).
//! - Bounded inbox size prevents memory exhaustion.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::{Mutex, mpsc};
use tracing::{debug, warn};

use clawft_types::agent_bus::{AgentBusError, InterAgentMessage};

/// Default inbox capacity per agent.
const DEFAULT_INBOX_CAPACITY: usize = 256;

/// Handle for an agent's inbox receiver.
///
/// This is returned from [`AgentBus::register_agent`] and provides
/// the only way to read messages from the agent's inbox.
pub struct AgentInbox {
    agent_id: String,
    rx: mpsc::Receiver<InterAgentMessage>,
}

impl AgentInbox {
    /// The agent ID that owns this inbox.
    pub fn agent_id(&self) -> &str {
        &self.agent_id
    }

    /// Receive the next message from this inbox.
    ///
    /// Returns `None` if the bus has been dropped or the agent
    /// has been unregistered.
    pub async fn recv(&mut self) -> Option<InterAgentMessage> {
        loop {
            match self.rx.recv().await {
                Some(msg) => {
                    if msg.is_expired() {
                        warn!(
                            msg_id = %msg.id,
                            from = %msg.from_agent,
                            to = %msg.to_agent,
                            "dropping expired inter-agent message"
                        );
                        continue;
                    }
                    return Some(msg);
                }
                None => return None,
            }
        }
    }

    /// Try to receive a message without blocking.
    ///
    /// Returns `None` if no message is available.
    pub fn try_recv(&mut self) -> Option<InterAgentMessage> {
        loop {
            match self.rx.try_recv() {
                Ok(msg) => {
                    if msg.is_expired() {
                        warn!(
                            msg_id = %msg.id,
                            from = %msg.from_agent,
                            to = %msg.to_agent,
                            "dropping expired inter-agent message"
                        );
                        continue;
                    }
                    return Some(msg);
                }
                Err(_) => return None,
            }
        }
    }
}

/// Inter-agent communication bus with per-agent inboxes.
///
/// Agents register on the bus to get an inbox. Messages are delivered
/// to the recipient's inbox. Agents can only read their own inbox
/// (enforced by the [`AgentInbox`] handle).
///
/// # Thread safety
///
/// The bus uses `Arc<Mutex<..>>` internally for concurrent access.
/// Send operations use `try_send` to avoid blocking callers.
pub struct AgentBus {
    /// Per-agent inbox senders: agent_id -> sender.
    inboxes: Arc<Mutex<HashMap<String, mpsc::Sender<InterAgentMessage>>>>,
    /// Inbox capacity for new registrations.
    inbox_capacity: usize,
}

impl AgentBus {
    /// Create a new agent bus with default inbox capacity (256).
    pub fn new() -> Self {
        Self {
            inboxes: Arc::new(Mutex::new(HashMap::new())),
            inbox_capacity: DEFAULT_INBOX_CAPACITY,
        }
    }

    /// Create a new agent bus with custom inbox capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inboxes: Arc::new(Mutex::new(HashMap::new())),
            inbox_capacity: capacity,
        }
    }

    /// Register an agent on the bus and return its inbox handle.
    ///
    /// If the agent is already registered, the old inbox is replaced
    /// (messages in the old inbox are lost).
    pub async fn register_agent(&self, agent_id: &str) -> AgentInbox {
        let (tx, rx) = mpsc::channel(self.inbox_capacity);
        let mut inboxes = self.inboxes.lock().await;
        inboxes.insert(agent_id.to_string(), tx);

        debug!(agent_id = %agent_id, "agent registered on bus");

        AgentInbox {
            agent_id: agent_id.to_string(),
            rx,
        }
    }

    /// Unregister an agent from the bus.
    ///
    /// The agent's inbox sender is dropped, causing the receiver to
    /// return `None` on next recv.
    pub async fn unregister_agent(&self, agent_id: &str) {
        let mut inboxes = self.inboxes.lock().await;
        if inboxes.remove(agent_id).is_some() {
            debug!(agent_id = %agent_id, "agent unregistered from bus");
        }
    }

    /// Send a message to the specified agent's inbox.
    ///
    /// # Errors
    ///
    /// Returns [`AgentBusError::AgentNotFound`] if the target agent
    /// is not registered.
    /// Returns [`AgentBusError::InboxFull`] if the inbox has reached
    /// its capacity limit.
    /// Returns [`AgentBusError::MessageExpired`] if the message has
    /// already expired.
    pub async fn send(&self, msg: InterAgentMessage) -> Result<(), AgentBusError> {
        // Check TTL before delivery.
        if msg.is_expired() {
            warn!(
                msg_id = %msg.id,
                from = %msg.from_agent,
                to = %msg.to_agent,
                "dropping expired message before delivery"
            );
            return Err(AgentBusError::MessageExpired { ttl: msg.ttl });
        }

        let inboxes = self.inboxes.lock().await;
        let tx = inboxes
            .get(&msg.to_agent)
            .ok_or_else(|| AgentBusError::AgentNotFound(msg.to_agent.clone()))?;

        tx.try_send(msg).map_err(|e| match e {
            mpsc::error::TrySendError::Full(msg) => {
                AgentBusError::InboxFull(msg.to_agent.clone())
            }
            mpsc::error::TrySendError::Closed(msg) => {
                AgentBusError::AgentNotFound(msg.to_agent.clone())
            }
        })
    }

    /// Check whether an agent is registered on the bus.
    pub async fn is_registered(&self, agent_id: &str) -> bool {
        let inboxes = self.inboxes.lock().await;
        inboxes.contains_key(agent_id)
    }

    /// List all registered agent IDs.
    pub async fn registered_agents(&self) -> Vec<String> {
        let inboxes = self.inboxes.lock().await;
        inboxes.keys().cloned().collect()
    }
}

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

/// Coordinator for dispatching subtasks to worker agents and collecting
/// results via the [`AgentBus`].
///
/// Implements the coordinator pattern: a lead agent dispatches work to
/// worker agents and waits for their replies.
pub struct SwarmCoordinator {
    /// Shared agent bus for message delivery.
    bus: Arc<AgentBus>,
    /// Agent ID of the coordinator.
    coordinator_id: String,
    /// Registered worker agent IDs.
    worker_agents: Vec<String>,
}

impl SwarmCoordinator {
    /// Create a new coordinator.
    pub fn new(
        bus: Arc<AgentBus>,
        coordinator_id: impl Into<String>,
        worker_agents: Vec<String>,
    ) -> Self {
        Self {
            bus,
            coordinator_id: coordinator_id.into(),
            worker_agents,
        }
    }

    /// Dispatch a subtask to a specific worker agent.
    ///
    /// Sends an [`InterAgentMessage`] to the worker and returns the
    /// message ID for later correlation.
    pub async fn dispatch_subtask(
        &self,
        task: &str,
        worker: &str,
        payload: serde_json::Value,
        ttl: Duration,
    ) -> Result<uuid::Uuid, AgentBusError> {
        let msg = InterAgentMessage::new(
            &self.coordinator_id,
            worker,
            task,
            payload,
            ttl,
        );
        let msg_id = msg.id;
        self.bus.send(msg).await?;
        debug!(
            coordinator = %self.coordinator_id,
            worker = %worker,
            task = %task,
            msg_id = %msg_id,
            "dispatched subtask"
        );
        Ok(msg_id)
    }

    /// Dispatch the same task to all registered workers.
    ///
    /// Returns a list of (worker_id, message_id) pairs.
    pub async fn broadcast_task(
        &self,
        task: &str,
        payload: serde_json::Value,
        ttl: Duration,
    ) -> Vec<(String, Result<uuid::Uuid, AgentBusError>)> {
        let mut results = Vec::new();
        for worker in &self.worker_agents {
            let result = self
                .dispatch_subtask(task, worker, payload.clone(), ttl)
                .await;
            results.push((worker.clone(), result));
        }
        results
    }

    /// Coordinator agent ID.
    pub fn coordinator_id(&self) -> &str {
        &self.coordinator_id
    }

    /// List of registered worker agent IDs.
    pub fn workers(&self) -> &[String] {
        &self.worker_agents
    }
}

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

    #[tokio::test]
    async fn register_and_send() {
        let bus = AgentBus::new();
        let mut inbox = bus.register_agent("agent-a").await;

        let msg = InterAgentMessage::new(
            "agent-b",
            "agent-a",
            "do work",
            json!({}),
            Duration::from_secs(60),
        );
        bus.send(msg).await.unwrap();

        let received = inbox.recv().await.unwrap();
        assert_eq!(received.from_agent, "agent-b");
        assert_eq!(received.to_agent, "agent-a");
        assert_eq!(received.task, "do work");
    }

    #[tokio::test]
    async fn send_to_unregistered_agent_fails() {
        let bus = AgentBus::new();
        let msg = InterAgentMessage::new(
            "sender",
            "nonexistent",
            "task",
            json!({}),
            Duration::from_secs(60),
        );
        let result = bus.send(msg).await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            AgentBusError::AgentNotFound(_)
        ));
    }

    #[tokio::test]
    async fn agent_isolation() {
        let bus = AgentBus::new();
        let mut inbox_a = bus.register_agent("agent-a").await;
        let mut inbox_b = bus.register_agent("agent-b").await;

        // Send to agent-a only.
        let msg = InterAgentMessage::new(
            "sender",
            "agent-a",
            "for a only",
            json!({}),
            Duration::from_secs(60),
        );
        bus.send(msg).await.unwrap();

        // agent-a should receive it.
        let received = inbox_a.try_recv();
        assert!(received.is_some());
        assert_eq!(received.unwrap().task, "for a only");

        // agent-b should not.
        let received_b = inbox_b.try_recv();
        assert!(received_b.is_none());
    }

    #[tokio::test]
    async fn unregister_agent() {
        let bus = AgentBus::new();
        let _inbox = bus.register_agent("agent-a").await;

        assert!(bus.is_registered("agent-a").await);
        bus.unregister_agent("agent-a").await;
        assert!(!bus.is_registered("agent-a").await);
    }

    #[tokio::test]
    async fn inbox_full_backpressure() {
        let bus = AgentBus::with_capacity(2);
        let _inbox = bus.register_agent("agent-a").await;

        // Fill inbox.
        for i in 0..2 {
            let msg = InterAgentMessage::new(
                "sender",
                "agent-a",
                &format!("task-{i}"),
                json!({}),
                Duration::from_secs(60),
            );
            bus.send(msg).await.unwrap();
        }

        // Third should fail.
        let msg = InterAgentMessage::new(
            "sender",
            "agent-a",
            "overflow",
            json!({}),
            Duration::from_secs(60),
        );
        let result = bus.send(msg).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), AgentBusError::InboxFull(_)));
    }

    #[tokio::test]
    async fn registered_agents_list() {
        let bus = AgentBus::new();
        bus.register_agent("a").await;
        bus.register_agent("b").await;

        let agents = bus.registered_agents().await;
        assert_eq!(agents.len(), 2);
        assert!(agents.contains(&"a".to_string()));
        assert!(agents.contains(&"b".to_string()));
    }

    #[tokio::test]
    async fn coordinator_dispatch() {
        let bus = Arc::new(AgentBus::new());
        let _inbox = bus.register_agent("worker-1").await;

        let coord = SwarmCoordinator::new(
            bus.clone(),
            "coordinator",
            vec!["worker-1".into()],
        );

        let msg_id = coord
            .dispatch_subtask("subtask-1", "worker-1", json!({"data": 42}), Duration::from_secs(60))
            .await
            .unwrap();

        // Message ID should be valid.
        assert!(!msg_id.is_nil());
    }

    #[tokio::test]
    async fn coordinator_broadcast() {
        let bus = Arc::new(AgentBus::new());
        let _inbox1 = bus.register_agent("w1").await;
        let _inbox2 = bus.register_agent("w2").await;

        let coord = SwarmCoordinator::new(
            bus.clone(),
            "coord",
            vec!["w1".into(), "w2".into()],
        );

        let results = coord
            .broadcast_task("broadcast-task", json!({}), Duration::from_secs(60))
            .await;

        assert_eq!(results.len(), 2);
        for (_, result) in &results {
            assert!(result.is_ok());
        }
    }

    #[test]
    fn agent_bus_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<AgentBus>();
    }
}