agent-base 0.15.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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
use super::*;
use crate::types::{AgentError, RetryConfig};
use async_trait::async_trait;
use llm_trait::{Capabilities, ChatRequest, ChatStream, LlmError, LlmProvider, ProviderInfo};
use std::pin::Pin;

struct StubProvider(&'static str);

#[async_trait]
impl LlmProvider for StubProvider {
    async fn stream(&self, _request: ChatRequest) -> Result<ChatStream, LlmError> {
        Ok(ChatStream::new(Box::pin(futures_util::stream::empty())))
    }

    async fn chat(&self, _request: ChatRequest) -> Result<llm_trait::ChatResponse, LlmError> {
        Err(LlmError::Llm("not implemented".into()))
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::default()
    }

    fn info(&self) -> ProviderInfo {
        ProviderInfo {
            name: "stub".to_string(),
            model: self.0.to_string(),
            version: None,
        }
    }
}

fn engine(model: &'static str) -> LlmEngine {
    LlmEngine::new(Arc::new(StubProvider(model)), EventBus::new(16))
}

#[test]
fn get_provider_and_set_provider_swap() {
    let engine = engine("model-a");
    assert_eq!(engine.get_provider().info().model, "model-a");
    engine.set_provider(Arc::new(StubProvider("model-b")));
    assert_eq!(engine.get_provider().info().model, "model-b");
}

#[test]
fn get_provider_returns_provider() {
    let engine = engine("model-a");
    let provider = engine.get_provider();
    assert_eq!(provider.info().model, "model-a");
}

#[tokio::test]
async fn process_stream_aggregates_text_usage_and_stop() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Text("Hello".into())),
        Ok(StreamChunk::Text(" world".into())),
        Ok(StreamChunk::Usage(UsageInfo {
            prompt_tokens: Some(5),
            completion_tokens: Some(2),
            total_tokens: Some(7),
            reasoning_tokens: None,
            cache_read_tokens: None,
        })),
        Ok(StreamChunk::Stop {
            finish_reason: Some("stop".into()),
        }),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let events_clone = events.clone();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(move |e| {
                events_clone.lock().unwrap().push(e);
                Ok(())
            })),
            &cancel,
        )
        .await
        .unwrap();

    assert_eq!(result.full_text, "Hello world");
    assert!(!result.is_tool_call);
    assert_eq!(result.finish_reason.as_deref(), Some("stop"));
    let usage = result.usage.unwrap();
    assert_eq!(usage.prompt_tokens, Some(5));
    assert_eq!(usage.completion_tokens, Some(2));
    assert_eq!(usage.total_tokens, Some(7));
    assert!(result.llm_duration_ms >= result.ttft_ms);

    let events = events.lock().unwrap();
    let texts: Vec<String> = events
        .iter()
        .filter_map(|e| match e {
            RuntimeEvent::TextDelta { text, .. } => Some(text.clone()),
            _ => None,
        })
        .collect();
    assert_eq!(texts, vec!["Hello".to_string(), " world".to_string()]);
}

#[tokio::test]
async fn process_stream_merges_partial_usage_events() {
    // Anthropic streams split usage across two events: message_start
    // carries prompt_tokens only, message_delta carries prompt (optional)
    // plus the final completion count. Replacing the struct wholesale
    // made every Anthropic session record input_tokens = 0.
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Usage(UsageInfo {
            prompt_tokens: Some(2),
            completion_tokens: Some(0),
            total_tokens: None,
            reasoning_tokens: None,
            cache_read_tokens: None,
        })),
        Ok(StreamChunk::Usage(UsageInfo {
            prompt_tokens: Some(63),
            completion_tokens: Some(26),
            total_tokens: None,
            reasoning_tokens: None,
            cache_read_tokens: None,
        })),
        Ok(StreamChunk::Stop {
            finish_reason: Some("end_turn".into()),
        }),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let noop = std::sync::Arc::new(std::sync::Mutex::new(|_e: RuntimeEvent| Ok(())));
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            noop,
            &cancel,
        )
        .await
        .unwrap();

    let usage = result.usage.unwrap();
    assert_eq!(usage.prompt_tokens, Some(63), "later Some wins");
    assert_eq!(usage.completion_tokens, Some(26));
}

#[tokio::test]
async fn process_stream_keeps_usage_when_delta_omits_prompt() {
    // Real Anthropic: message_delta has no input_tokens at all; the
    // message_start value must survive the merge rather than be zeroed.
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Usage(UsageInfo {
            prompt_tokens: Some(5),
            completion_tokens: Some(1),
            total_tokens: None,
            reasoning_tokens: None,
            cache_read_tokens: None,
        })),
        Ok(StreamChunk::Usage(UsageInfo {
            prompt_tokens: None,
            completion_tokens: Some(26),
            total_tokens: None,
            reasoning_tokens: None,
            cache_read_tokens: None,
        })),
        Ok(StreamChunk::Stop {
            finish_reason: Some("end_turn".into()),
        }),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let noop = std::sync::Arc::new(std::sync::Mutex::new(|_e: RuntimeEvent| Ok(())));
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            noop,
            &cancel,
        )
        .await
        .unwrap();

    let usage = result.usage.unwrap();
    assert_eq!(
        usage.prompt_tokens,
        Some(5),
        "prompt survives a partial delta"
    );
    assert_eq!(usage.completion_tokens, Some(26));
}

#[tokio::test]
async fn process_stream_aggregates_tool_calls() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "id": "call_1", "function": { "name": "shell", "arguments": "{\"cmd\":" } }
            ] }
        }))),
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "function": { "arguments": "\"ls\"" } }
            ] }
        }))),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .unwrap();

    assert!(result.is_tool_call);
    assert!(result.full_text.is_empty());
    assert_eq!(result.tool_calls.len(), 1);
    let tc = &result.tool_calls[0];
    assert_eq!(tc["id"].as_str(), Some("call_1"));
    assert_eq!(tc["type"].as_str(), Some("function"));
    assert_eq!(tc["function"]["name"].as_str(), Some("shell"));
    assert_eq!(
        tc["function"]["arguments"].as_str(),
        Some("{\"cmd\":\"ls\"")
    );
}

#[tokio::test]
async fn process_stream_emits_throttled_tool_call_drafts() {
    // Tool-call args arrive as fragments and are invisible to every
    // consumer until the stream drains — multi-KB briefs (spawn_agent)
    // were a tens-of-seconds black hole (session 20261006_9264ba3e).
    // The draft is the liveness signal: throttled to slot creation,
    // name arrival and each 512-char args bucket — never per fragment,
    // and never carrying the args themselves.
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let args_400 = "x".repeat(400);
    let args_200 = "y".repeat(200);
    let args_50 = "z".repeat(50);
    let chunks = vec![
        // Fresh slot + name → one draft.
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "id": "call_1", "function": { "name": "spawn_agent", "arguments": args_400 } }
            ] }
        }))),
        // +200 chars → crosses the 512 bucket → one draft.
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "function": { "arguments": args_200 } }
            ] }
        }))),
        // +50 chars → inside the same bucket → silent.
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "function": { "arguments": args_50 } }
            ] }
        }))),
        // Second call: fresh slot + name → one draft, count 2.
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 1, "id": "call_2", "function": { "name": "shell", "arguments": "{}" } }
            ] }
        }))),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let events_clone = events.clone();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(move |e| {
                events_clone.lock().unwrap().push(e);
                Ok(())
            })),
            &cancel,
        )
        .await
        .unwrap();
    assert_eq!(result.tool_calls.len(), 2);

    let events = events.lock().unwrap();
    let drafts: Vec<(usize, String, usize, usize)> = events
        .iter()
        .filter_map(|e| match e {
            RuntimeEvent::ActionDraft {
                index,
                name,
                args_len,
                count,
                ..
            } => Some((*index, name.clone(), *args_len, *count)),
            _ => None,
        })
        .collect();
    assert_eq!(
        drafts,
        vec![
            (0, "spawn_agent".to_string(), 400, 1),
            (0, "spawn_agent".to_string(), 600, 1),
            (1, "shell".to_string(), 2, 2),
        ]
    );
}

#[tokio::test]
async fn process_stream_emits_text_after_tool_call_chunks() {
    // Regression: the old gate `!aggregator.is_tool_call` suppressed every
    // TextDelta/ThoughtDelta after the first tool_call chunk, but
    // aggregator.full_text still accumulated them — the user saw nothing
    // while the model context carried the text (user sees ≠ model sees,
    // session 20260904_efad759c: 2935 chars of prose rendered nowhere).
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Text("before".into())),
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "id": "call_1", "function": { "name": "shell", "arguments": "{}" } }
            ] }
        }))),
        Ok(StreamChunk::Text("interleaved".into())),
        Ok(StreamChunk::Thought("late thought".into())),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let events_clone = events.clone();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(move |e| {
                events_clone.lock().unwrap().push(e);
                Ok(())
            })),
            &cancel,
        )
        .await
        .unwrap();

    assert!(result.is_tool_call);
    assert_eq!(result.full_text, "beforeinterleaved");

    let events = events.lock().unwrap();
    let texts: Vec<&str> = events
        .iter()
        .filter_map(|e| match e {
            RuntimeEvent::TextDelta { text, .. } => Some(text.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(texts, vec!["before", "interleaved"]);
    let thoughts: Vec<&str> = events
        .iter()
        .filter_map(|e| match e {
            RuntimeEvent::ThoughtDelta { text, .. } => Some(text.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(thoughts, vec!["late thought"]);
}

#[tokio::test]
async fn process_stream_flags_reasoning_only_without_promoting() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![Ok(StreamChunk::Thought("deep thought".into()))];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .unwrap();

    // Reasoning must NOT be promoted into the answer text.
    assert_eq!(result.full_text, "");
    assert_eq!(result.reasoning_text, "deep thought");
    assert!(!result.is_tool_call);
    assert!(result.reasoning_only, "reasoning-only should be flagged");
}

#[tokio::test]
async fn process_stream_keeps_reasoning_on_tool_call_turns() {
    // Issue #32 regression anchor at the stream-result boundary: a turn
    // that carries both Thought chunks and a tool call must keep the
    // reasoning text in the result. `thinking_bytes` is derived from this
    // string (same UTF-8 byte source as the `reasoning_len` log field),
    // so an empty reasoning_text here is what zeroed the metric on every
    // tool-call turn (session 20260927_4180845c: 27x undercount).
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Thought("why this tool, now".into())),
        Ok(StreamChunk::ToolCall(serde_json::json!({
            "delta": { "tool_calls": [
                { "index": 0, "id": "call_1", "function": { "name": "shell", "arguments": "{}" } }
            ] }
        }))),
        Ok(StreamChunk::Thought("...and what it should do".into())),
        Ok(StreamChunk::Stop {
            finish_reason: Some("tool_calls".to_string()),
        }),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .unwrap();

    assert!(result.is_tool_call);
    assert!(!result.reasoning_only);
    assert_eq!(
        result.reasoning_text,
        "why this tool, now...and what it should do"
    );
    // UTF-8 bytes — the unit `thinking_bytes` reports downstream.
    assert_eq!(
        result.reasoning_text.len(),
        "why this tool, now".len() + "...and what it should do".len()
    );
}

#[tokio::test]
async fn process_stream_propagates_error() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Text("partial".into())),
        Err(AgentError::internal("boom")),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let err = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .err()
        .expect("stream should error");
    assert!(err.to_string().contains("boom"));
}

#[tokio::test]
async fn process_stream_cancelled() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let stream: Pin<Box<dyn Stream<Item = AgentResult<StreamChunk>> + Send>> =
        Box::pin(futures_util::stream::pending());
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    cancel.cancel();
    let err = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .err()
        .expect("stream should error");
    assert!(matches!(err, AgentError::Cancelled));
}

// ── B5: chat_stream + retry + remaining process_stream branches ────────

/// A provider whose `stream()` always fails.
struct AlwaysFail;

#[async_trait]
impl LlmProvider for AlwaysFail {
    async fn stream(&self, _request: ChatRequest) -> Result<ChatStream, LlmError> {
        Err(LlmError::Llm("api down".into()))
    }

    async fn chat(&self, _request: ChatRequest) -> Result<llm_trait::ChatResponse, LlmError> {
        Err(LlmError::Llm("api down".into()))
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::default()
    }

    fn info(&self) -> ProviderInfo {
        ProviderInfo {
            name: "always-fail".to_string(),
            model: "always-fail".to_string(),
            version: None,
        }
    }
}

/// A provider that fails the first `n` `stream()` calls, then succeeds.
struct FailThenSucceed {
    remaining: std::sync::Mutex<usize>,
}

impl FailThenSucceed {
    fn new(failures: usize) -> Self {
        Self {
            remaining: std::sync::Mutex::new(failures),
        }
    }
}

#[async_trait]
impl LlmProvider for FailThenSucceed {
    async fn stream(&self, _request: ChatRequest) -> Result<ChatStream, LlmError> {
        let mut remaining = self.remaining.lock().unwrap();
        if *remaining > 0 {
            *remaining -= 1;
            Err(LlmError::Llm("transient".into()))
        } else {
            Ok(ChatStream::new(Box::pin(futures_util::stream::empty())))
        }
    }

    async fn chat(&self, _request: ChatRequest) -> Result<llm_trait::ChatResponse, LlmError> {
        Err(LlmError::Llm("not implemented".into()))
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::default()
    }

    fn info(&self) -> ProviderInfo {
        ProviderInfo {
            name: "fail-then-succeed".to_string(),
            model: "fail-then-succeed".to_string(),
            version: None,
        }
    }
}

#[tokio::test]
async fn chat_stream_returns_stream_on_ok() {
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), EventBus::new(16));
    let mut stream = engine
        .chat_stream(
            &[],
            &[],
            None,
            None,
            false,
            &tokio_util::sync::CancellationToken::new(),
        )
        .await
        .unwrap();
    let next = futures_util::StreamExt::next(&mut stream).await;
    assert!(next.is_none());
}

#[tokio::test]
async fn chat_stream_with_thinking_disabled() {
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), EventBus::new(16));
    // Test with thinking_disabled=true — should still work, just without reasoning
    let mut stream = engine
        .chat_stream(
            &[],
            &[],
            None,
            None,
            true,
            &tokio_util::sync::CancellationToken::new(),
        )
        .await
        .unwrap();
    let next = futures_util::StreamExt::next(&mut stream).await;
    assert!(next.is_none());
}

#[tokio::test]
async fn chat_stream_forwards_error() {
    let engine = LlmEngine::new(Arc::new(AlwaysFail), EventBus::new(16));
    let err = engine
        .chat_stream(
            &[],
            &[],
            None,
            None,
            false,
            &tokio_util::sync::CancellationToken::new(),
        )
        .await
        .err()
        .expect("should fail");
    assert!(err.to_string().contains("api down"));
}

#[tokio::test]
async fn run_llm_turn_retries_then_succeeds() {
    let engine = LlmEngine::new(Arc::new(FailThenSucceed::new(2)), EventBus::new(16));
    let cfg = RetryConfig {
        max_retries: 3,
        initial_backoff_ms: 1,
        max_backoff_ms: 1000,
        backoff_multiplier: 2.0,
        jitter: false,
    };
    let _stream = engine
        .run_llm_turn_with_retry(
            &SessionId::new(1),
            &[],
            &[],
            None,
            None,
            cfg,
            false,
            &tokio_util::sync::CancellationToken::new(),
        )
        .await
        .expect("should succeed after retries");
}

#[tokio::test]
async fn run_llm_turn_retries_exhausted() {
    let engine = LlmEngine::new(Arc::new(FailThenSucceed::new(100)), EventBus::new(16));
    let cfg = RetryConfig {
        max_retries: 2,
        initial_backoff_ms: 1,
        max_backoff_ms: 1,
        backoff_multiplier: 2.0,
        jitter: false,
    };
    let err = engine
        .run_llm_turn_with_retry(
            &SessionId::new(1),
            &[],
            &[],
            None,
            None,
            cfg,
            false,
            &tokio_util::sync::CancellationToken::new(),
        )
        .await
        .err()
        .expect("should be exhausted");
    assert!(err.to_string().contains("transient"));
}

#[tokio::test]
async fn chat_stream_cancelled_while_awaiting_response() {
    // A provider whose `stream()` never resolves — the shape of a silent
    // network drop while waiting for response headers. Cancelling must
    // abort the wait instead of hanging forever.
    struct NeverResolves;

    #[async_trait]
    impl LlmProvider for NeverResolves {
        async fn stream(&self, _request: ChatRequest) -> Result<ChatStream, LlmError> {
            std::future::pending().await
        }

        async fn chat(&self, _request: ChatRequest) -> Result<llm_trait::ChatResponse, LlmError> {
            Err(LlmError::Llm("not implemented".into()))
        }

        fn capabilities(&self) -> Capabilities {
            Capabilities::default()
        }

        fn info(&self) -> ProviderInfo {
            ProviderInfo {
                name: "never-resolves".to_string(),
                model: "never-resolves".to_string(),
                version: None,
            }
        }
    }

    let engine = LlmEngine::new(Arc::new(NeverResolves), EventBus::new(16));
    let token = tokio_util::sync::CancellationToken::new();
    let fut = engine.chat_stream(&[], &[], None, None, false, &token);
    tokio::pin!(fut);
    token.cancel();
    let err = tokio::time::timeout(std::time::Duration::from_secs(5), fut)
        .await
        .expect("chat_stream must resolve after cancel")
        .err()
        .expect("must be cancelled");
    assert!(err.is_cancelled(), "expected Cancelled, got: {err}");
}

#[tokio::test]
async fn process_stream_tool_call_missing_delta_continues() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::ToolCall(
            serde_json::json!({ "no_delta": true }),
        )),
        Ok(StreamChunk::Stop {
            finish_reason: Some("stop".into()),
        }),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .unwrap();
    assert!(result.is_tool_call);
    assert!(result.tool_calls.is_empty());
}

#[tokio::test]
async fn process_stream_tool_call_empty_id_and_name_ignored() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![Ok(StreamChunk::ToolCall(serde_json::json!({
        "delta": { "tool_calls": [
            { "index": 0, "id": "", "function": { "name": "", "arguments": "{}" } }
        ] }
    })))];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .unwrap();
    // A bucket that carries an index but never a function name is a
    // phantom fragment some OpenAI-compatible providers emit. It must NOT
    // surface as a tool call — kept, it becomes a name-less assistant
    // tool_call and the NEXT request is rejected HTTP 400 "missing a
    // function name". The stream is fully drained here, so an empty name
    // at assembly time is permanent, not a name that arrives later.
    assert_eq!(result.tool_calls.len(), 0);
}

#[tokio::test]
async fn process_stream_stop_none_finish_reason_ignored() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let chunks = vec![
        Ok(StreamChunk::Stop {
            finish_reason: Some("length".into()),
        }),
        Ok(StreamChunk::Stop {
            finish_reason: None,
        }),
    ];
    let stream = Box::pin(futures_util::stream::iter(chunks));
    let mut rx = bus.subscribe();
    let cancel = tokio_util::sync::CancellationToken::new();
    let result = engine
        .process_stream(
            &SessionId::new(1),
            stream,
            tracing::Span::none(),
            &mut rx,
            std::sync::Arc::new(std::sync::Mutex::new(|_e| Ok(()))),
            &cancel,
        )
        .await
        .unwrap();
    assert_eq!(result.finish_reason.as_deref(), Some("length"));
}

#[test]
fn emit_text_delta_emits_event() {
    let bus = EventBus::new(16);
    let engine = LlmEngine::new(Arc::new(StubProvider("x")), bus.clone());
    let mut rx = bus.subscribe();
    engine.emit_text_delta(&SessionId::new(1), "hello".into());
    let ev = rx.try_recv().expect("event should be emitted");
    assert!(matches!(ev, RuntimeEvent::TextDelta { text, .. } if text == "hello"));
}

/// protects: a word-only history builds byte-identical — the image budget is
/// a no-op without pictures and the wire copy is not rewritten in passing.
/// fails_when: text messages gain, lose or reorder content during the build.
/// no_claim: picture handling — pinned in `image_tests.rs` and
/// `crate::image_ops`'s own tests.
#[test]
fn a_word_only_history_is_untouched() {
    let messages = vec![
        ChatMessage::user("hi"),
        ChatMessage::tool("t1", "done"),
        ChatMessage::assistant("ok"),
    ];
    let request = build_chat_request(&messages, &[], None, None, false, None)
        .expect("a word-only history builds");
    assert_eq!(
        format!("{:?}", request.messages),
        format!("{messages:?}"),
        "byte-identical pass-through"
    );
}

/// protects: the model's freedom to batch independent tool calls — the engine
/// must claim it on every request, or the provider applies its own default and
/// the harness may quietly serialize.
/// fails_when: the claim is dropped from the request, which no later test can
/// see because it lives on the wire.
/// no_claim: that the model will actually batch; phimint session
/// 20261115_2fbff82e averaged 1.41 calls/turn with nothing forbidding it.
#[test]
fn every_request_claims_tool_call_batching() {
    let messages = vec![ChatMessage::user("hi")];
    let request = build_chat_request(&messages, &[], None, None, false, None)
        .expect("a one-message request builds");
    assert_eq!(
        request.parallel_tool_calls,
        Some(true),
        "the engine must claim batching on every request"
    );
}

// ── Turn-safe provider swap (set_provider_when_idle) ───────────────────────

/// protects: a swap staged while a run executes lands only when the last
/// active run ends — one conversation is never split across two models (the
/// tool-continuation steps must keep the provider the turn started with).
/// fails_when: the swap lands mid-run, or never lands after the run ends.
/// no_claim: which provider a product stages or when it stages one.
#[test]
fn a_swap_staged_during_a_run_lands_when_the_run_ends() {
    let engine = engine("old");
    let run = engine.begin_run();
    assert!(engine.has_active_run());
    let deferred = engine.set_provider_when_idle(Arc::new(StubProvider("new")));
    assert!(deferred, "the swap defers while a run is active");
    assert_eq!(
        engine.get_provider().info().model,
        "old",
        "the live run keeps its provider"
    );
    drop(run);
    assert!(!engine.has_active_run());
    assert_eq!(
        engine.get_provider().info().model,
        "new",
        "the staged swap lands at run end"
    );
}

/// protects: an idle engine swaps at once — `true` back means deferred, and
/// a caller that says "applies after the current turn" reads that bit.
/// fails_when: an idle swap parks until some future run ends. no_claim:
/// the product's confirm wording.
#[test]
fn a_swap_staged_while_idle_lands_immediately() {
    let engine = engine("old");
    let deferred = engine.set_provider_when_idle(Arc::new(StubProvider("new")));
    assert!(!deferred, "an idle engine swaps at once");
    assert_eq!(engine.get_provider().info().model, "new");
}

/// protects: overlapping runs hold the swap until the LAST one ends.
/// fails_when: the swap lands while any run is still executing. no_claim:
/// why runs overlap (sub-agents, back-to-back turns).
#[test]
fn overlapping_runs_hold_the_swap_until_the_last_one_ends() {
    let engine = engine("old");
    let run_a = engine.begin_run();
    let run_b = engine.begin_run();
    assert!(engine.set_provider_when_idle(Arc::new(StubProvider("new"))));
    drop(run_a);
    assert_eq!(
        engine.get_provider().info().model,
        "old",
        "run_b is still active"
    );
    drop(run_b);
    assert_eq!(engine.get_provider().info().model, "new");
}

/// protects: two staged swaps resolve last-wins — a flip-flop of picks must
/// land on the final choice, not replay both. fails_when: the first staged
/// swap overwrites the second. no_claim: how a product stages two swaps.
#[test]
fn the_last_staged_swap_wins() {
    let engine = engine("old");
    let run = engine.begin_run();
    assert!(engine.set_provider_when_idle(Arc::new(StubProvider("first"))));
    assert!(engine.set_provider_when_idle(Arc::new(StubProvider("last"))));
    drop(run);
    assert_eq!(engine.get_provider().info().model, "last");
}

#[test]
fn provider_handle_follows_set_provider() {
    // A handle is the anti-snapshot seam (session 20261107_27dc727a): holders
    // that cloned the provider at build time kept judging on the boot model
    // after a `/model` swap. A handle must always resolve the current slot.
    let engine = engine("model-a");
    let handle = engine.provider_handle();
    assert_eq!(handle.current().info().model, "model-a");
    engine.set_provider(Arc::new(StubProvider("model-b")));
    assert_eq!(handle.current().info().model, "model-b");
    // Independent handles stay in sync — they alias one slot.
    let second = handle.clone();
    engine.set_provider(Arc::new(StubProvider("model-c")));
    assert_eq!(second.current().info().model, "model-c");
}

#[test]
fn standalone_provider_handle_resolves_its_initial() {
    let handle = ProviderHandle::new(Arc::new(StubProvider("model-x")));
    assert_eq!(handle.current().info().model, "model-x");
}