agent-base 0.1.0

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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
use std::pin::Pin;
use std::sync::Arc;
use std::sync::Mutex;

use agent_base::{
    AgentBuilder, AgentEvent, AgentResult, ApprovalDecision, ApprovalHandler,
    ApprovalRequest, ChatMessage, LlmCapabilities, LlmClient, ResponseFormat, RiskLevel, RunOutcome, StreamChunk, Tool,
    ToolContext, ToolControlFlow, ToolOutput, ToolPolicy,
};
use async_trait::async_trait;
use futures_core::Stream;
use serde_json::{json, Value};

type ChunkStream = Pin<Box<dyn Stream<Item = AgentResult<StreamChunk>> + Send>>;

struct MockLlmClient {
    responses: Mutex<std::vec::IntoIter<Vec<StreamChunk>>>,
    call_count: Mutex<usize>,
}

impl MockLlmClient {
    fn new(scripted_responses: Vec<Vec<StreamChunk>>) -> Self {
        Self {
            responses: Mutex::new(scripted_responses.into_iter()),
            call_count: Mutex::new(0),
        }
    }

    fn call_count(&self) -> usize {
        *self.call_count.lock().unwrap()
    }
}

#[async_trait]
impl LlmClient for MockLlmClient {
    async fn chat(
        &self,
        _messages: &[ChatMessage],
        _tools: &[Value],
        _enable_thinking: Option<bool>,
        _response_format: Option<&ResponseFormat>,
    ) -> AgentResult<Value> {
        unimplemented!()
    }

    async fn chat_stream(
        &self,
        _messages: &[ChatMessage],
        _tools: &[Value],
        _enable_thinking: Option<bool>,
        _response_format: Option<&ResponseFormat>,
    ) -> AgentResult<ChunkStream> {
        *self.call_count.lock().unwrap() += 1;

        let chunks: Vec<AgentResult<StreamChunk>> = self
            .responses
            .lock()
            .unwrap()
            .next()
            .unwrap_or_default()
            .into_iter()
            .map(Ok)
            .collect();

        let stream = futures_util::stream::iter(chunks);
        Ok(Box::pin(stream))
    }

    fn capabilities(&self) -> LlmCapabilities {
        LlmCapabilities {
            supports_streaming: true,
            supports_tools: true,
            supports_vision: false,
            supports_thinking: false,
            max_context_tokens: None,
            max_output_tokens: None,
        }
    }
}

struct EchoTool;

#[async_trait]
impl Tool for EchoTool {
    fn name(&self) -> &'static str {
        "echo"
    }

    fn definition(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": "echo",
                "description": "echo back the message",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "message": { "type": "string" }
                    },
                    "required": ["message"]
                }
            }
        })
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
        let msg = args["message"].as_str().unwrap_or("");
        Ok(ToolOutput {
            summary: format!("echo: {msg}"),
            raw: Some(json!({ "echo": msg })),
            control_flow: ToolControlFlow::Continue,
            truncated: false,
        })
    }
}

// ---------------------------------------------------------------------------
// Test suites
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_simple_text_reply() {
    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::Text("Hello, ".to_string()),
        StreamChunk::Text("world!".to_string()),
        StreamChunk::Stop,
    ]]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .system_prompt("You are a helpful assistant")
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id.clone(), "Hi").await;
    assert!(result.is_ok(), "Expected ok, got: {result:?}");
    let (_events, outcome) = result.unwrap();
    assert_eq!(outcome, RunOutcome::Completed);

    let session = runtime.session(&session_id).unwrap();
    let messages = session.chat_messages();
    assert_eq!(messages.len(), 3);
    assert!(matches!(messages[0], ChatMessage::System { .. }));
    assert!(matches!(messages[1], ChatMessage::User { .. }));
    assert!(matches!(messages[2], ChatMessage::Assistant { .. }));

    assert_eq!(llm.call_count(), 1);
}

#[tokio::test]
async fn test_multiple_turns_with_tool() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "echo",
                            "arguments": "{\"message\": \"hello\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![
            StreamChunk::Text("Done!".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id, "Echo hello").await;
    assert!(result.is_ok(), "Expected ok, got: {result:?}");

    assert_eq!(llm.call_count(), 2);
}

#[tokio::test]
async fn test_tool_not_found() {
    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::ToolCall(json!({
            "delta": {
                "tool_calls": [{
                    "id": "call_1",
                    "function": {
                        "name": "nonexistent_tool",
                        "arguments": "{}"
                    }
                }]
            }
        })),
        StreamChunk::Stop,
    ]]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .system_prompt("system prompt")
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id, "test").await;
    assert!(result.is_ok(), "Tool not found should not crash: {result:?}");

    let (events, _outcome) = result.unwrap();
    let has_tool_error = events.iter().any(|e| {
        matches!(e, AgentEvent::ToolCallFinished { summary, .. } if summary.contains("not found"))
    });
    assert!(has_tool_error, "Should have tool not found in finished events");
}

#[tokio::test]
async fn test_approval_deny_stops_execution() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "echo",
                            "arguments": "{\"message\": \"test\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![
            StreamChunk::Text("I cannot proceed without approval".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    struct DenyHandler;

    #[async_trait]
    impl ApprovalHandler for DenyHandler {
        async fn approve(&self, _request: ApprovalRequest) -> AgentResult<ApprovalDecision> {
            Ok(ApprovalDecision::Deny)
        }
    }

    struct RequireApprovalPolicy;

    impl ToolPolicy for RequireApprovalPolicy {
        fn evaluate_approval(
            &self,
            _tool_name: &str,
            _args: &Value,
            _args_json: &str,
        ) -> Option<ApprovalRequest> {
            Some(ApprovalRequest {
                title: "Test".to_string(),
                message: "Require approval".to_string(),
                action_key: None,
                risk_level: RiskLevel::Sensitive,
                raw: None,
            })
        }

        fn on_pre_call(&self, _: &str, _: &Value, _: &ToolContext) {}
        fn on_post_call(&self, _: &str, _: &Value, _: &ToolOutput, _: &ToolContext) {}
    }

    let mut runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .approval_handler(Arc::new(DenyHandler))
        .tool_policy(Arc::new(RequireApprovalPolicy))
        .error_recovery(Arc::new(agent_base::RetryOnError))
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id, "test").await;
    let (events, _outcome) = result.expect("Approval denial should be handled gracefully");

    let has_awaiting_approval = events
        .iter()
        .any(|e| matches!(e, AgentEvent::AwaitingApproval { .. }));
    assert!(has_awaiting_approval, "Should emit AwaitingApproval event");

    let has_denial_finished = events.iter().any(|e| {
        matches!(e, AgentEvent::ToolCallFinished { summary, .. } if summary.contains("rejected by approval"))
    });
    assert!(has_denial_finished, "Should emit ToolCallFinished with denial summary");

    assert_eq!(llm.call_count(), 2, "Should make 2 LLM calls (tool call then recovery)");
}

#[tokio::test]
async fn test_approval_allow_once_executes_tool() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "echo",
                            "arguments": "{\"message\": \"hello\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![
            StreamChunk::Text("done".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    struct AllowOnceHandler;

    #[async_trait]
    impl ApprovalHandler for AllowOnceHandler {
        async fn approve(&self, _request: ApprovalRequest) -> AgentResult<ApprovalDecision> {
            Ok(ApprovalDecision::AllowOnce)
        }
    }

    struct RequireApprovalPolicy;

    impl ToolPolicy for RequireApprovalPolicy {
        fn evaluate_approval(
            &self,
            _tool_name: &str,
            _args: &Value,
            _args_json: &str,
        ) -> Option<ApprovalRequest> {
            Some(ApprovalRequest {
                title: "Test".to_string(),
                message: "Require approval".to_string(),
                action_key: None,
                risk_level: RiskLevel::Sensitive,
                raw: None,
            })
        }

        fn on_pre_call(&self, _: &str, _: &Value, _: &ToolContext) {}
        fn on_post_call(&self, _: &str, _: &Value, _: &ToolOutput, _: &ToolContext) {}
    }

    let mut runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .approval_handler(Arc::new(AllowOnceHandler))
        .tool_policy(Arc::new(RequireApprovalPolicy))
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id, "test").await;
    assert!(result.is_ok(), "Expected ok, got: {result:?}");
    assert_eq!(llm.call_count(), 2);
}

#[tokio::test]
async fn test_empty_text_and_no_tool_call_continues() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![StreamChunk::Text(String::new()), StreamChunk::Stop],
        vec![
            StreamChunk::Text("final reply".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .system_prompt("sys")
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id, "test").await;
    assert!(result.is_ok(), "Expected ok, got: {result:?}");
    assert_eq!(llm.call_count(), 2);
}

#[tokio::test]
async fn test_tool_parse_error_recovers() {
    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::ToolCall(json!({
            "delta": {
                "tool_calls": [{
                    "id": "call_1",
                    "function": {
                        "name": "echo",
                        "arguments": "invalid json {{{"
                    }
                }]
            }
        })),
        StreamChunk::Stop,
    ]]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .system_prompt("sys")
        .build();

    let session_id = runtime.create_session();
    let result = runtime.run_turn_stream(session_id, "test").await;
    assert!(result.is_ok(), "Should recover from tool parse error: {result:?}");
}

#[tokio::test]
async fn test_event_collection() {
    let llm = Arc::new(MockLlmClient::new(vec![vec![
        StreamChunk::Text("reply".to_string()),
        StreamChunk::Stop,
    ]]));

    let mut runtime = AgentBuilder::new(llm.clone()).build();

    let session_id = runtime.create_session();
    let (events, _outcome) = runtime.run_turn_stream(session_id, "test").await.unwrap();

    let has_text_delta = events.iter().any(|e| matches!(e, AgentEvent::TextDelta { .. }));
    let has_run_finished =
        events.iter().any(|e| matches!(e, AgentEvent::RunFinished { .. }));

    assert!(has_text_delta, "Should have TextDelta event");
    assert!(has_run_finished, "Should have RunFinished event");
}

// ---------------------------------------------------------------------------
// 6.2 multi-modal message tests
// ---------------------------------------------------------------------------

#[test]
fn test_chat_message_user_with_images() {
    use agent_base::{ChatMessage, ImageAttachment, ImageDetail};

    let msg = ChatMessage::user("hello");
    match &msg {
        ChatMessage::User { images, .. } => {
            assert!(images.is_empty());
        }
        _ => panic!("expected User variant"),
    }

    let images = vec![
        ImageAttachment::Url {
            url: "https://example.com/img.jpg".to_string(),
            detail: Some(ImageDetail::High),
        },
        ImageAttachment::Base64 {
            data: "abc123".to_string(),
            media_type: Some("image/png".to_string()),
            detail: None,
        },
    ];
    let msg_with_images = ChatMessage::user_with_images("describe this", images);
    match &msg_with_images {
        ChatMessage::User { content, images } => {
            assert_eq!(content, "describe this");
            assert_eq!(images.len(), 2);
            assert!(matches!(images[0], ImageAttachment::Url { .. }));
            assert!(matches!(images[1], ImageAttachment::Base64 { .. }));
        }
        _ => panic!("expected User variant"),
    }
}

#[test]
fn test_image_attachment_serialization() {
    use agent_base::ImageAttachment;
    use serde_json;

    let img = ImageAttachment::Url {
        url: "https://example.com/img.jpg".to_string(),
        detail: None,
    };
    let json_str = serde_json::to_string(&img).unwrap();
    let parsed: ImageAttachment = serde_json::from_str(&json_str).unwrap();
    match parsed {
        ImageAttachment::Url { url, .. } => {
            assert_eq!(url, "https://example.com/img.jpg");
        }
        _ => panic!("expected Url variant"),
    }

    let img_base64 = ImageAttachment::Base64 {
        data: "abc123".to_string(),
        media_type: Some("image/jpeg".to_string()),
        detail: None,
    };
    let json_str = serde_json::to_string(&img_base64).unwrap();
    let parsed: ImageAttachment = serde_json::from_str(&json_str).unwrap();
    match parsed {
        ImageAttachment::Base64 { data, .. } => {
            assert_eq!(data, "abc123");
        }
        _ => panic!("expected Base64 variant"),
    }
}

#[test]
fn test_session_push_user_with_images() {
    use agent_base::types::SessionId;
    use agent_base::{AgentSession, ChatMessage, ImageAttachment, MessageRole};

    let session_id = SessionId {
        id: 1,
        external_id: None,
    };
    let mut session = AgentSession::new(session_id);

    let images = vec![ImageAttachment::Url {
        url: "https://example.com/img.jpg".to_string(),
        detail: None,
    }];
    session.push_user_message_with_images("describe this image", images);

    let chat_msgs = session.chat_messages();
    assert_eq!(chat_msgs.len(), 1);
    match &chat_msgs[0] {
        ChatMessage::User { content, images } => {
            assert_eq!(content, "describe this image");
            assert_eq!(images.len(), 1);
        }
        _ => panic!("expected User variant"),
    }

    let msgs = session.messages();
    assert_eq!(msgs.len(), 1);
    assert_eq!(msgs[0].role, MessageRole::User);
    assert_eq!(msgs[0].content, "describe this image");
}

// ---------------------------------------------------------------------------
// 6.4 Checkpoint / Resume tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_checkpoint_events_emitted() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "echo",
                            "arguments": "{\"message\": \"hello\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![
            StreamChunk::Text("done".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .system_prompt("sys")
        .build();

    let session_id = runtime.create_session();
    let (events, _outcome) = runtime.run_turn_stream(session_id, "test checkpoint").await.unwrap();

    let checkpoint_count = events
        .iter()
        .filter(|e| matches!(e, AgentEvent::Checkpoint { .. }))
        .count();
    assert!(
        checkpoint_count >= 2,
        "Should have at least AfterUserInput and BeforeLlm checkpoints, got {checkpoint_count}"
    );

    let has_after_user_input = events.iter().any(|e| {
        matches!(e, AgentEvent::Checkpoint { checkpoint, .. } if matches!(checkpoint.step, agent_base::CheckpointStep::AfterUserInput))
    });
    assert!(has_after_user_input, "Should have AfterUserInput checkpoint");

    let has_before_llm = events.iter().any(|e| {
        matches!(e, AgentEvent::Checkpoint { checkpoint, .. } if matches!(checkpoint.step, agent_base::CheckpointStep::BeforeLlm { .. }))
    });
    assert!(has_before_llm, "Should have BeforeLlm checkpoint");

    let has_before_tool_calls = events.iter().any(|e| {
        matches!(e, AgentEvent::Checkpoint { checkpoint, .. } if matches!(checkpoint.step, agent_base::CheckpointStep::BeforeToolCalls { .. }))
    });
    assert!(has_before_tool_calls, "Should have BeforeToolCalls checkpoint");
}

#[tokio::test]
async fn test_resume_from_after_user_input_checkpoint() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::Text("resumed reply".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .system_prompt("sys")
        .build();

    let session_id = runtime.create_session();

    let mut checkpoint_opt: Option<agent_base::CheckpointData> = None;
    let _ = runtime
        .run_turn_with_handler(session_id.clone(), "resume test", |event| {
            if let AgentEvent::Checkpoint { checkpoint, .. } = &event {
                if matches!(checkpoint.step, agent_base::CheckpointStep::AfterUserInput) {
                    checkpoint_opt = Some(checkpoint.clone());
                    return Err(agent_base::AgentError::Cancelled);
                }
            }
            Ok(())
        })
        .await;

    let checkpoint = checkpoint_opt.expect("Should have captured AfterUserInput checkpoint");

    let result = runtime.resume_from_checkpoint(checkpoint, |_| Ok(())).await;
    assert!(result.is_ok(), "Resume should succeed: {result:?}");

    let session = runtime.session(&session_id).unwrap();
    let chat_msgs = session.chat_messages();
    let has_assistant_reply = chat_msgs
        .iter()
        .any(|m| matches!(m, ChatMessage::Assistant { content, .. } if content.as_deref() == Some("resumed reply")));
    assert!(has_assistant_reply, "Should have resumed reply in session");
}

#[tokio::test]
async fn test_resume_from_before_tool_calls_checkpoint() {
    let llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "echo",
                            "arguments": "{\"message\":\"hello\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![
            StreamChunk::Text("tool results processed".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let mut runtime = AgentBuilder::new(llm.clone())
        .register_tool(EchoTool)
        .system_prompt("sys")
        .build();

    let session_id = runtime.create_session();

    let mut checkpoint_opt: Option<agent_base::CheckpointData> = None;
    let _ = runtime
        .run_turn_with_handler(session_id.clone(), "echo hello", |event| {
            if let AgentEvent::Checkpoint { checkpoint, .. } = &event {
                if matches!(checkpoint.step, agent_base::CheckpointStep::BeforeToolCalls { .. }) {
                    checkpoint_opt = Some(checkpoint.clone());
                    return Err(agent_base::AgentError::Cancelled);
                }
            }
            Ok(())
        })
        .await;

    let checkpoint =
        checkpoint_opt.expect("Should have captured BeforeToolCalls checkpoint");

    let result = runtime.resume_from_checkpoint(checkpoint, |_| Ok(())).await;
    assert!(result.is_ok(), "Resume from BeforeToolCalls should succeed: {result:?}");

    let session = runtime.session(&session_id).unwrap();
    let chat_msgs = session.chat_messages();
    let has_tool_result = chat_msgs.iter().any(|m| {
        matches!(m, ChatMessage::Tool { content, .. } if content.contains("echo: hello"))
    });
    assert!(has_tool_result, "Should have echo tool result in session");
}

// ---------------------------------------------------------------------------
// 6.3 sub-agent tool tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn test_sub_agent_tool() {
    use agent_base::SubAgentTool;

    let sub_llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::Text("sub-agent processed: ".to_string()),
            StreamChunk::Text("task completed".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let sub_runtime = AgentBuilder::new(sub_llm.clone())
        .system_prompt("you are a sub-agent")
        .build();

    let sub_agent_tool = SubAgentTool::new(
        "delegate_task",
        "delegate a task to a sub-agent",
        sub_runtime,
    );

    let parent_llm = Arc::new(MockLlmClient::new(vec![
        vec![
            StreamChunk::ToolCall(json!({
                "delta": {
                    "tool_calls": [{
                        "id": "call_1",
                        "function": {
                            "name": "delegate_task",
                            "arguments": "{\"task\": \"analyze the data\"}"
                        }
                    }]
                }
            })),
            StreamChunk::Stop,
        ],
        vec![
            StreamChunk::Text("parent final reply".to_string()),
            StreamChunk::Stop,
        ],
    ]));

    let mut parent_runtime = AgentBuilder::new(parent_llm.clone())
        .register_tool(sub_agent_tool)
        .system_prompt("you are the main agent")
        .build();

    let session_id = parent_runtime.create_session();
    let result = parent_runtime
        .run_turn_stream(session_id.clone(), "delegate this task")
        .await;
    assert!(result.is_ok(), "Sub-agent delegation should succeed: {result:?}");
    assert_eq!(parent_llm.call_count(), 2, "Parent should make 2 LLM calls");

    let session = parent_runtime.session(&session_id).unwrap();
    let chat_msgs = session.chat_messages();
    let has_parent_final = chat_msgs.iter().any(|m| {
        matches!(m, ChatMessage::Assistant { content, .. } if content.as_deref() == Some("parent final reply"))
    });
    assert!(has_parent_final, "Should have parent final reply");
}