agent-base 0.16.1

A lightweight Agent Runtime Kernel for building AI agents in Rust
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
use super::*;

#[tokio::test]
async fn orchestrate_executes_multiple_tool_calls() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);

    let event_bus = EventBus::new(16);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[
                (
                    "call_a".to_string(),
                    "echo".to_string(),
                    r#"{"text":"a"}"#.to_string(),
                ),
                (
                    "call_b".to_string(),
                    "echo".to_string(),
                    r#"{"text":"b"}"#.to_string(),
                ),
            ],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should succeed");

    assert_eq!(outcome.results.len(), 2);
    assert!(outcome.failures.is_empty());
    assert_eq!(outcome.results[0].id, "call_a");
    assert_eq!(content_text(&outcome.results[0].output), "echo: a");
    assert_eq!(outcome.results[1].id, "call_b");
    assert_eq!(content_text(&outcome.results[1].output), "echo: b");
}

#[tokio::test]
async fn orchestrate_collects_invalid_json_args_as_failure() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);

    let event_bus = EventBus::new(16);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[(
                "call_x".to_string(),
                "echo".to_string(),
                "not-json".to_string(),
            )],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should not hard-fail on bad args");

    assert!(
        outcome.results.is_empty(),
        "no tool should execute for bad args"
    );
    assert_eq!(outcome.failures.len(), 1);
    assert!(matches!(
        &outcome.failures[0].error,
        AgentError::ToolArgsInvalid { .. }
    ));
}

#[tokio::test]
async fn orchestrate_continues_past_bad_args_call() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);

    let event_bus = EventBus::new(16);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    // [good, bad-args, good] — the bad call in the middle must not stop the
    // trailing good call from executing.
    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[
                (
                    "call_a".to_string(),
                    "echo".to_string(),
                    r#"{"text":"a"}"#.to_string(),
                ),
                (
                    "call_b".to_string(),
                    "echo".to_string(),
                    "not-json".to_string(),
                ),
                (
                    "call_c".to_string(),
                    "echo".to_string(),
                    r#"{"text":"c"}"#.to_string(),
                ),
            ],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should succeed even with a bad call in the middle");

    assert_eq!(outcome.results.len(), 2, "both good calls should execute");
    assert_eq!(outcome.results[0].id, "call_a");
    assert_eq!(content_text(&outcome.results[0].output), "echo: a");
    assert_eq!(outcome.results[1].id, "call_c");
    assert_eq!(content_text(&outcome.results[1].output), "echo: c");

    assert_eq!(outcome.failures.len(), 1, "only the bad call should fail");
    assert_eq!(outcome.failures[0].id, "call_b");
    assert!(matches!(
        &outcome.failures[0].error,
        AgentError::ToolArgsInvalid { .. }
    ));
}

#[tokio::test]
async fn orchestrate_continues_past_execution_failure() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);
    registry.register(FailingTool);

    let event_bus = EventBus::new(16);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[
                (
                    "call_a".to_string(),
                    "echo".to_string(),
                    r#"{"text":"a"}"#.to_string(),
                ),
                (
                    "call_fail".to_string(),
                    "failing".to_string(),
                    "{}".to_string(),
                ),
                (
                    "call_b".to_string(),
                    "echo".to_string(),
                    r#"{"text":"b"}"#.to_string(),
                ),
            ],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should succeed even with a tool execution failure");

    assert_eq!(outcome.results.len(), 2);
    assert_eq!(outcome.results[0].id, "call_a");
    assert_eq!(outcome.results[1].id, "call_b");
    assert_eq!(outcome.failures.len(), 1);
    assert!(matches!(
        &outcome.failures[0].error,
        AgentError::ToolExecution { .. }
    ));
}

#[tokio::test]
async fn orchestrate_delivers_events_exactly_once() {
    // Regression: parallel orchestrate must not double-deliver events.
    // Previously, events were delivered via local_rx (resubscribe) AND
    // again via the original event_rx drain, causing duplicates.
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);

    let event_bus = EventBus::new(64);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
    let events_clone = events.clone();

    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[
                (
                    "call_a".to_string(),
                    "echo".to_string(),
                    r#"{"text":"a"}"#.to_string(),
                ),
                (
                    "call_b".to_string(),
                    "echo".to_string(),
                    r#"{"text":"b"}"#.to_string(),
                ),
            ],
            &c,
            &mut event_rx,
            Arc::new(Mutex::new(move |ev| -> AgentResult<()> {
                let tag = match &ev {
                    RuntimeEvent::ActionStarted { tool_name, .. } => {
                        format!("started:{tool_name}")
                    }
                    RuntimeEvent::ActionFinished { tool_name, .. } => {
                        format!("finished:{tool_name}")
                    }
                    _ => return Ok(()),
                };
                events_clone.lock().unwrap().push(tag);
                Ok(())
            })),
        )
        .await
        .expect("orchestrate should succeed");

    assert_eq!(outcome.results.len(), 2);

    let ev = events.lock().unwrap();
    // Each tool should produce exactly one started + one finished = 4 total.
    let started: Vec<_> = ev.iter().filter(|e| e.starts_with("started:")).collect();
    let finished: Vec<_> = ev.iter().filter(|e| e.starts_with("finished:")).collect();
    assert_eq!(
        started.len(),
        2,
        "expected 2 ActionStarted, got {started:?}"
    );
    assert_eq!(
        finished.len(),
        2,
        "expected 2 ActionFinished, got {finished:?}"
    );
}

#[tokio::test]
async fn orchestrate_invalid_args_error_includes_serde_details() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);

    let event_bus = EventBus::new(16);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    // Malformed JSON — serde should tell us what went wrong
    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[(
                "call_x".to_string(),
                "echo".to_string(),
                "{invalid".to_string(),
            )],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should not hard-fail on bad args");

    assert_eq!(outcome.failures.len(), 1);
    match &outcome.failures[0].error {
        AgentError::ToolArgsInvalid { raw, .. } => {
            // The raw field should now contain the serde error message,
            // not just the original args string
            assert!(
                raw.contains("args: {invalid"),
                "should include original args, got: {}",
                raw
            );
            // Serde error should be present (e.g., "key must be a string")
            assert!(
                raw.len() > "{invalid".to_string().len() + 10,
                "raw should contain serde error in addition to args, got: {}",
                raw
            );
        }
        other => panic!("expected ToolArgsInvalid, got {:?}", other),
    }
}

#[tokio::test]
async fn orchestrate_non_json_args_error_includes_parse_details() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);

    let event_bus = EventBus::new(16);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[(
                "call_x".to_string(),
                "echo".to_string(),
                "not-valid-json".to_string(),
            )],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should not hard-fail");

    assert_eq!(outcome.failures.len(), 1);
    match &outcome.failures[0].error {
        AgentError::ToolArgsInvalid { raw, .. } => {
            // Should contain the serde error description (e.g., "expected value at line 1 column 1")
            assert!(
                raw.contains("args: not-valid-json"),
                "should include original args, got: {}",
                raw
            );
            assert!(
                raw.len() > "not-valid-json".to_string().len(),
                "raw should contain serde error in addition to args, got: {}",
                raw
            );
        }
        other => panic!("expected ToolArgsInvalid, got {:?}", other),
    }
}

// ── Parallel orchestrate tests ──

struct SlowEchoTool;
#[async_trait]
impl Tool for SlowEchoTool {
    fn name(&self) -> &'static str {
        "slow_echo"
    }
    fn description(&self) -> &'static str {
        ""
    }
    fn schema(&self) -> Value {
        serde_json::json!({})
    }
    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<Vec<Content>> {
        // Simulate a slow tool (50ms)
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        let text = args.get("text").and_then(Value::as_str).unwrap_or("");
        Ok(vec![Content::text(format!("slow: {text}"))])
    }
}

#[tokio::test]
async fn orchestrate_executes_in_parallel() {
    let mut registry = ToolRegistry::default();
    registry.register(SlowEchoTool);

    let event_bus = EventBus::new(64);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let start = std::time::Instant::now();
    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[
                ("c1".into(), "slow_echo".into(), r#"{"text":"a"}"#.into()),
                ("c2".into(), "slow_echo".into(), r#"{"text":"b"}"#.into()),
                ("c3".into(), "slow_echo".into(), r#"{"text":"c"}"#.into()),
            ],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should succeed");

    let elapsed = start.elapsed();

    assert_eq!(outcome.results.len(), 3);
    assert!(outcome.failures.is_empty());

    // If sequential, 3 × 50ms = 150ms+. With parallel, should be ~50ms.
    // Use 120ms as a generous threshold (sequential would be 150ms+).
    assert!(
        elapsed < std::time::Duration::from_millis(120),
        "expected parallel execution < 120ms, took {elapsed:?}"
    );
}

#[tokio::test]
async fn orchestrate_parallel_failure_does_not_abort_others() {
    let mut registry = ToolRegistry::default();
    registry.register(EchoTool);
    registry.register(FailingTool);

    let event_bus = EventBus::new(64);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);

    let sm = session_manager();
    let c = ctx(&sm);

    let outcome = engine
        .orchestrate(
            &SessionId::new(1),
            &[
                ("c1".into(), "echo".into(), r#"{"text":"ok"}"#.into()),
                ("c2".into(), "failing".into(), "{}".into()),
                ("c3".into(), "echo".into(), r#"{"text":"also ok"}"#.into()),
            ],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        )
        .await
        .expect("orchestrate should succeed even with a failure");

    assert_eq!(outcome.results.len(), 2);
    assert_eq!(content_text(&outcome.results[0].output), "echo: ok");
    assert_eq!(content_text(&outcome.results[1].output), "echo: also ok");

    assert_eq!(outcome.failures.len(), 1);
    assert!(matches!(
        &outcome.failures[0].error,
        AgentError::ToolExecution { .. }
    ));
}

/// Tool that does synchronous file I/O — mimics history_list_items.
/// This exercises the real-world path where a blocking `fs::read_to_string`
/// runs inside an async `call` on a multi-thread tokio runtime.
struct SyncFileReadTool {
    dir: tempfile::TempDir,
}

impl SyncFileReadTool {
    fn new() -> Self {
        let dir = tempfile::TempDir::new().unwrap();
        // Write a few small files to simulate session history
        for i in 0..5 {
            let path = dir.path().join(format!("file_{i}.jsonl"));
            let content = format!("line1 for file {i}\nline2 for file {i}\n");
            std::fs::write(&path, content).unwrap();
        }
        Self { dir }
    }
}

#[async_trait]
impl Tool for SyncFileReadTool {
    fn name(&self) -> &'static str {
        "sync_file_read"
    }
    fn description(&self) -> &'static str {
        ""
    }
    fn schema(&self) -> Value {
        serde_json::json!({})
    }
    async fn call(&self, _args: &Value, _ctx: &ToolContext) -> AgentResult<Vec<Content>> {
        // Synchronous file I/O — same pattern as history_list_items
        let mut output = String::new();
        for i in 0..5 {
            let path = self.dir.path().join(format!("file_{i}.jsonl"));
            let content = std::fs::read_to_string(&path)
                .map_err(|e| AgentError::internal(format!("sync_file_read: {e}")))?;
            output.push_str(&content);
        }
        Ok(vec![Content::text(output)])
    }
}

#[tokio::test]
async fn orchestrate_sync_io_tool_does_not_hang() {
    let tool = SyncFileReadTool::new();
    let mut registry = ToolRegistry::default();
    registry.register(tool);

    let event_bus = EventBus::new(64);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);
    let sm = session_manager();
    let c = ctx(&sm);

    let result = tokio::time::timeout(
        std::time::Duration::from_secs(5),
        engine.orchestrate(
            &SessionId::new(1),
            &[("c1".into(), "sync_file_read".into(), "{}".into())],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        ),
    )
    .await;

    assert!(result.is_ok(), "sync I/O tool timed out (hang!)");
    let outcome = result.unwrap().unwrap();
    assert_eq!(outcome.results.len(), 1);
    assert!(outcome.failures.is_empty());
}

#[tokio::test]
async fn orchestrate_parallel_sync_io_tools_do_not_hang() {
    let tool = SyncFileReadTool::new();
    let mut registry = ToolRegistry::default();
    registry.register(tool);
    registry.register(EchoTool);

    let event_bus = EventBus::new(64);
    let mut event_rx = event_bus.subscribe();
    let engine = ToolEngine::new(registry, None, None, Arc::new(StopOnError), event_bus);
    let sm = session_manager();
    let c = ctx(&sm);

    // Mix sync I/O tools with fast tools in parallel
    let result = tokio::time::timeout(
        std::time::Duration::from_secs(5),
        engine.orchestrate(
            &SessionId::new(1),
            &[
                ("c1".into(), "sync_file_read".into(), "{}".into()),
                ("c2".into(), "echo".into(), r#"{"text":"hello"}"#.into()),
                ("c3".into(), "sync_file_read".into(), "{}".into()),
                ("c4".into(), "echo".into(), r#"{"text":"world"}"#.into()),
            ],
            &c,
            &mut event_rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_| -> AgentResult<()> { Ok(()) })),
        ),
    )
    .await;

    assert!(result.is_ok(), "parallel sync I/O tools timed out (hang!)");
    let outcome = result.unwrap().unwrap();
    assert_eq!(outcome.results.len(), 4, "expected 4 results");
    assert!(
        outcome.failures.is_empty(),
        "failures: {:?}",
        outcome.failures
    );
}