agent-works 0.1.6

Batteries-included Agent toolbox built on agent-base
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
//! Asynchronous message passing between agents.
//!
//! The [`MailboxHub`] manages per-agent mailboxes and a global sequence number
//! (`tokio::sync::watch<u64>`) that notifies waiters when any result arrives.
//!
//! # Architecture
//!
//! ```text
//! Parent (LLM tools)                      Child (AgentRuntime task)
//!        │                                         │
//!        │  send_task() / send_message()           │
//!        ├────────────────────────────────────────►│ task_rx
//!        │                                         │
//!        │                      post_result()      │
//!        │◄────────────────────────────────────────┤ (via result_tx clone)
//!        │                                         │
//!        │  wait_for_result() watches seq_rx       │
//!        │  (blocks until seq changes)             │
//! ```
//!
//! Every `post_result()` increments the global sequence number, waking all
//! `wait_for_result()` callers.

use std::collections::HashMap;
use std::sync::Mutex;

use tokio::sync::{mpsc, watch};

use super::path::AgentPath;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// A task sent from parent to child agent.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MailboxTask {
    /// The task description / user input for the child agent.
    pub task: String,
    /// Whether to interrupt the child's current execution.
    pub interrupt: bool,
    /// Pending messages accumulated before this task (from `send_message`).
    pub pending_messages: Vec<String>,
}

/// Status of a result posted from child to parent.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MailboxStatus {
    /// Child agent completed its task successfully.
    Ok,
    /// Child agent encountered an error.
    Error,
    /// Child agent was closed.
    Closed,
}

/// A result posted from child agent to parent.
#[derive(Clone, Debug)]
pub struct MailboxResult {
    /// Which agent produced this result.
    pub agent_path: AgentPath,
    /// The status of the result.
    pub status: MailboxStatus,
    /// The result text (if any).
    pub result: Option<String>,
}

// ---------------------------------------------------------------------------
// Per-agent mailbox handle (child side)
// ---------------------------------------------------------------------------

/// The child-side handle to a mailbox.
///
/// Given to the spawned child agent task. The child reads tasks from `task_rx`
/// and posts results via `hub.post_result()` using the agent path.
#[derive(Debug)]
pub struct ChildMailbox {
    /// Receive tasks from parent.
    pub task_rx: mpsc::Receiver<MailboxTask>,
}

// ---------------------------------------------------------------------------
// Per-agent mailbox (internal)
// ---------------------------------------------------------------------------

struct MailboxEntry {
    /// Send tasks to child.
    task_tx: mpsc::Sender<MailboxTask>,
    /// Results received from child (or posted by runtime).
    results: Vec<MailboxResult>,
    /// Pending messages (from `send_message`, no execution trigger).
    pending: Vec<String>,
}

// ---------------------------------------------------------------------------
// MailboxHub
// ---------------------------------------------------------------------------

/// Central hub for inter-agent message passing.
///
/// Manages per-agent mailboxes and a global sequence number. The sequence
/// number increments every time a result is posted, allowing `wait_for_result`
/// to efficiently block until new data arrives.
///
/// All methods use internal `Mutex` — the hub is designed to be shared
/// via `Arc<MailboxHub>`.
pub struct MailboxHub {
    entries: Mutex<HashMap<AgentPath, MailboxEntry>>,
    seq_tx: watch::Sender<u64>,
    seq_rx: watch::Receiver<u64>,
}

impl MailboxHub {
    /// Create a new empty mailbox hub.
    pub fn new() -> Self {
        let (seq_tx, seq_rx) = watch::channel(0);
        Self {
            entries: Mutex::new(HashMap::new()),
            seq_tx,
            seq_rx,
        }
    }

    /// Register a new agent mailbox.
    ///
    /// Returns the child-side handle to be given to the spawned agent task.
    /// Returns `None` if the agent_path is already registered.
    pub fn register(&self, agent_path: &AgentPath) -> Option<ChildMailbox> {
        let mut entries = self.entries.lock().unwrap();
        if entries.contains_key(agent_path) {
            return None;
        }
        let (task_tx, task_rx) = mpsc::channel(32);
        entries.insert(
            agent_path.clone(),
            MailboxEntry {
                task_tx,
                results: Vec::new(),
                pending: Vec::new(),
            },
        );
        Some(ChildMailbox { task_rx })
    }

    /// Unregister an agent mailbox.
    ///
    /// Posts a `Closed` result first (to wake any waiters), then removes the entry.
    /// Returns `true` if the agent was registered.
    pub fn unregister(&self, agent_path: &AgentPath) -> bool {
        let mut entries = self.entries.lock().unwrap();
        if entries.get(agent_path).is_some() {
            // Wake waiters with sequence bump
            let current = *self.seq_rx.borrow();
            let _ = self.seq_tx.send(current.wrapping_add(1));
            entries.remove(agent_path);
            true
        } else {
            false
        }
    }

    /// Send a message to a sub-agent (no execution trigger).
    ///
    /// The message is appended to the agent's pending message buffer.
    /// Returns `true` if the message was queued, `false` if the agent is not registered.
    pub fn send_message(&self, agent_path: &AgentPath, message: String) -> bool {
        let mut entries = self.entries.lock().unwrap();
        match entries.get_mut(agent_path) {
            Some(entry) => {
                entry.pending.push(message);
                true
            }
            None => false,
        }
    }

    /// Send a task to a sub-agent (triggers execution).
    ///
    /// Drains pending messages and packages them with the task.
    /// Returns `true` if the task was sent, `false` if the agent is not registered
    /// or the channel is full.
    pub fn send_task(&self, agent_path: &AgentPath, task: String, interrupt: bool) -> bool {
        let mut entries = self.entries.lock().unwrap();
        match entries.get_mut(agent_path) {
            Some(entry) => {
                let pending = std::mem::take(&mut entry.pending);
                let mailbox_task = MailboxTask {
                    task,
                    interrupt,
                    pending_messages: pending,
                };
                entry.task_tx.try_send(mailbox_task).is_ok()
            }
            None => false,
        }
    }

    /// Check if an agent has pending (unread) messages.
    pub fn has_pending(&self, agent_path: &AgentPath) -> bool {
        let entries = self.entries.lock().unwrap();
        entries
            .get(agent_path)
            .map(|e| !e.pending.is_empty())
            .unwrap_or(false)
    }

    /// Post a result from a child agent.
    ///
    /// Increments the global sequence number, waking all `wait_for_result` callers.
    pub fn post_result(&self, result: MailboxResult) {
        let mut entries = self.entries.lock().unwrap();
        if let Some(entry) = entries.get_mut(&result.agent_path) {
            entry.results.push(result);
            // Notify waiters
            let current = *self.seq_rx.borrow();
            let _ = self.seq_tx.send(current.wrapping_add(1));
        }
    }

    /// Get a clone of the global sequence number receiver.
    ///
    /// Used by `wait_agent` to watch for changes before polling.
    pub fn subscribe_seq(&self) -> watch::Receiver<u64> {
        self.seq_rx.clone()
    }

    /// Try to receive a result for a specific agent (non-blocking).
    ///
    /// Returns the oldest unread result for the agent, or `None`.
    pub fn try_recv_result(&self, agent_path: &AgentPath) -> Option<MailboxResult> {
        let mut entries = self.entries.lock().unwrap();
        entries.get_mut(agent_path).and_then(|e| {
            if e.results.is_empty() {
                None
            } else {
                Some(e.results.remove(0))
            }
        })
    }

    /// Try to receive any result (non-blocking).
    ///
    /// Returns the first available result from any agent mailbox.
    pub fn try_recv_any(&self) -> Option<MailboxResult> {
        let mut entries = self.entries.lock().unwrap();
        for entry in entries.values_mut() {
            if !entry.results.is_empty() {
                return Some(entry.results.remove(0));
            }
        }
        None
    }

    /// Check if an agent has unread results.
    pub fn has_results(&self, agent_path: &AgentPath) -> bool {
        let entries = self.entries.lock().unwrap();
        entries
            .get(agent_path)
            .map(|e| !e.results.is_empty())
            .unwrap_or(false)
    }

    /// Return the total number of unread results across all agents.
    pub fn total_pending_results(&self) -> usize {
        let entries = self.entries.lock().unwrap();
        entries.values().map(|e| e.results.len()).sum()
    }

    /// Check if an agent is registered.
    pub fn contains(&self, agent_path: &AgentPath) -> bool {
        let entries = self.entries.lock().unwrap();
        entries.contains_key(agent_path)
    }

    /// Return the number of registered agents.
    pub fn len(&self) -> usize {
        let entries = self.entries.lock().unwrap();
        entries.len()
    }

    /// Return whether there are no registered agents.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Return all registered agent paths.
    pub fn agent_paths(&self) -> Vec<AgentPath> {
        let entries = self.entries.lock().unwrap();
        entries.keys().cloned().collect()
    }
}

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

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

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;

    fn test_path(name: &str) -> AgentPath {
        AgentPath::root().join(name)
    }

    #[test]
    fn register_and_unregister() {
        let hub = MailboxHub::new();
        let path = test_path("test-agent");

        assert!(!hub.contains(&path));
        assert_eq!(hub.len(), 0);

        let child = hub.register(&path);
        assert!(child.is_some());
        assert!(hub.contains(&path));
        assert_eq!(hub.len(), 1);

        // Duplicate register fails
        assert!(hub.register(&path).is_none());

        assert!(hub.unregister(&path));
        assert!(!hub.contains(&path));
        assert_eq!(hub.len(), 0);

        // Double unregister is a no-op
        assert!(!hub.unregister(&path));
    }

    #[test]
    fn send_message_and_task() {
        let hub = MailboxHub::new();
        let path = test_path("worker");

        let mut child = hub.register(&path).unwrap();

        // Messages accumulate without triggering
        assert!(hub.send_message(&path, "hello".into()));
        assert!(hub.send_message(&path, "world".into()));
        assert!(hub.has_pending(&path));

        // Send to non-existent agent
        assert!(!hub.send_message(&test_path("ghost"), "nope".into()));

        // Task drains pending
        assert!(hub.send_task(&path, "do work".into(), true));
        assert!(!hub.has_pending(&path));

        // Child receives task with pending messages
        let received = child.task_rx.try_recv().unwrap();
        assert_eq!(received.task, "do work");
        assert!(received.interrupt);
        assert_eq!(received.pending_messages, vec!["hello", "world"]);
    }

    #[test]
    fn post_and_receive_result() {
        let hub = MailboxHub::new();
        let path = test_path("worker");

        hub.register(&path);

        hub.post_result(MailboxResult {
            agent_path: path.clone(),
            status: MailboxStatus::Ok,
            result: Some("done!".into()),
        });

        assert!(hub.has_results(&path));

        let received = hub.try_recv_result(&path);
        assert!(received.is_some());
        let r = received.unwrap();
        assert_eq!(r.agent_path, path);
        assert_eq!(r.status, MailboxStatus::Ok);
        assert_eq!(r.result.unwrap(), "done!");

        assert!(!hub.has_results(&path));
    }

    #[test]
    fn try_recv_any_returns_all() {
        let hub = MailboxHub::new();
        let a = test_path("a");
        let b = test_path("b");

        hub.register(&a);
        hub.register(&b);

        hub.post_result(MailboxResult {
            agent_path: a.clone(),
            status: MailboxStatus::Ok,
            result: Some("first".into()),
        });
        hub.post_result(MailboxResult {
            agent_path: b.clone(),
            status: MailboxStatus::Error,
            result: Some("second".into()),
        });

        // HashMap iteration order is non-deterministic, so just check we get both
        let r1 = hub.try_recv_any().unwrap();
        let r2 = hub.try_recv_any().unwrap();
        assert!(hub.try_recv_any().is_none());

        let mut paths = vec![r1.agent_path.to_string(), r2.agent_path.to_string()];
        paths.sort();
        assert_eq!(paths, vec!["root/a", "root/b"]);
    }

    #[test]
    fn sequence_number_changes_on_post() {
        let hub = MailboxHub::new();
        let path = test_path("worker");
        hub.register(&path);

        let seq = hub.subscribe_seq();
        let initial = *seq.borrow();

        hub.post_result(MailboxResult {
            agent_path: path.clone(),
            status: MailboxStatus::Ok,
            result: None,
        });

        assert!(seq.has_changed().unwrap());
        assert_ne!(*seq.borrow(), initial);
    }

    #[test]
    fn sequence_number_changes_on_unregister() {
        let hub = MailboxHub::new();
        let path = test_path("worker");
        hub.register(&path);

        let seq = hub.subscribe_seq();
        let initial = *seq.borrow();

        hub.unregister(&path);

        assert!(seq.has_changed().unwrap());
        assert_ne!(*seq.borrow(), initial);
    }

    #[test]
    fn agent_paths() {
        let hub = MailboxHub::new();
        hub.register(&test_path("a"));
        hub.register(&test_path("b"));

        let mut paths = hub.agent_paths();
        paths.sort();
        assert_eq!(paths.len(), 2);
    }

    #[test]
    fn total_pending_results() {
        let hub = MailboxHub::new();
        let a = test_path("a");
        hub.register(&a);

        assert_eq!(hub.total_pending_results(), 0);

        hub.post_result(MailboxResult {
            agent_path: a.clone(),
            status: MailboxStatus::Ok,
            result: None,
        });
        assert_eq!(hub.total_pending_results(), 1);

        hub.post_result(MailboxResult {
            agent_path: a.clone(),
            status: MailboxStatus::Ok,
            result: None,
        });
        assert_eq!(hub.total_pending_results(), 2);

        hub.try_recv_any();
        assert_eq!(hub.total_pending_results(), 1);
    }

    #[tokio::test]
    async fn wait_for_result_pattern() {
        let hub = Arc::new(MailboxHub::new());
        let path = test_path("worker");
        hub.register(&path);

        let hub_clone = hub.clone();
        let path_clone = path.clone();

        // Spawn a task that posts a result after a short delay
        tokio::spawn(async move {
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
            hub_clone.post_result(MailboxResult {
                agent_path: path_clone,
                status: MailboxStatus::Ok,
                result: Some("async result".into()),
            });
        });

        // Wait for result using the seq pattern
        let mut seq = hub.subscribe_seq();
        loop {
            match hub.try_recv_any() {
                Some(r) => {
                    assert_eq!(r.status, MailboxStatus::Ok);
                    assert_eq!(r.result.unwrap(), "async result");
                    break;
                }
                None => {
                    let _ = seq.changed().await;
                }
            }
        }
    }
}