yoagent 0.25.13

The agent loop for Rust: stream from 7 LLM protocols, run tools, loop until done. Native or wasm32 (Cloudflare Workers).
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
//! Streaming tests for `BedrockProvider` against a local mock server (#174).
//!
//! ConverseStream answers with binary `application/vnd.amazon.eventstream`
//! frames. These tests build real frames — prelude, headers, JSON payload and
//! both CRC-32s — shaped after the AWS Bedrock Runtime API reference, and
//! serve them over HTTP. They are mocks of the documented format, not
//! recordings of a live endpoint.
//!
//! The frame encoder and CRC below are written independently of the crate's
//! decoder, so the two cross-check each other.

use serde_json::{json, Value};
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
use yoagent::agent::Agent;
use yoagent::provider::{
    ApiProtocol, BedrockProvider, ModelConfig, ProviderError, StreamConfig, StreamEvent,
    StreamProvider,
};
use yoagent::*;

const MODEL: &str = "anthropic.claude-test";
const EVENTSTREAM: &str = "application/vnd.amazon.eventstream";

// ---------------------------------------------------------------------------
// Frame encoding
// ---------------------------------------------------------------------------

/// Bitwise CRC-32 (IEEE, reflected 0xEDB88320) — deliberately not the
/// crate's table-driven version.
fn crc32(data: &[u8]) -> u32 {
    let mut crc = !0u32;
    for &byte in data {
        crc ^= byte as u32;
        for _ in 0..8 {
            let mask = (crc & 1).wrapping_neg();
            crc = (crc >> 1) ^ (0xEDB8_8320 & mask);
        }
    }
    !crc
}

/// One frame with string-typed headers.
fn frame(headers: &[(&str, &str)], payload: &[u8]) -> Vec<u8> {
    let mut h = Vec::new();
    for (name, value) in headers {
        h.push(name.len() as u8);
        h.extend_from_slice(name.as_bytes());
        h.push(7);
        h.extend_from_slice(&(value.len() as u16).to_be_bytes());
        h.extend_from_slice(value.as_bytes());
    }
    let total = (16 + h.len() + payload.len()) as u32;
    let mut out = Vec::new();
    out.extend_from_slice(&total.to_be_bytes());
    out.extend_from_slice(&(h.len() as u32).to_be_bytes());
    let c = crc32(&out);
    out.extend_from_slice(&c.to_be_bytes());
    out.extend_from_slice(&h);
    out.extend_from_slice(payload);
    let c = crc32(&out);
    out.extend_from_slice(&c.to_be_bytes());
    out
}

/// An event frame, as ConverseStream sends it: the payload is the event
/// structure itself, padded with a `p` field as Bedrock does.
fn event(event_type: &str, mut payload: Value) -> Vec<u8> {
    payload["p"] = json!("abcdefghijklmnopqrstuvwxyzABCDEF");
    frame(
        &[
            (":event-type", event_type),
            (":content-type", "application/json"),
            (":message-type", "event"),
        ],
        payload.to_string().as_bytes(),
    )
}

fn exception(kind: &str, message: &str) -> Vec<u8> {
    frame(
        &[
            (":exception-type", kind),
            (":content-type", "application/json"),
            (":message-type", "exception"),
        ],
        json!({ "message": message }).to_string().as_bytes(),
    )
}

fn message_start() -> Vec<u8> {
    event("messageStart", json!({"role": "assistant"}))
}
fn text(index: u64, t: &str) -> Vec<u8> {
    event(
        "contentBlockDelta",
        json!({"contentBlockIndex": index, "delta": {"text": t}}),
    )
}
fn tool_start(index: u64, id: &str, name: &str) -> Vec<u8> {
    event(
        "contentBlockStart",
        json!({"contentBlockIndex": index, "start": {"toolUse": {"toolUseId": id, "name": name}}}),
    )
}
fn tool_input(index: u64, input: &str) -> Vec<u8> {
    event(
        "contentBlockDelta",
        json!({"contentBlockIndex": index, "delta": {"toolUse": {"input": input}}}),
    )
}
fn reasoning(index: u64, delta: Value) -> Vec<u8> {
    event(
        "contentBlockDelta",
        json!({"contentBlockIndex": index, "delta": {"reasoningContent": delta}}),
    )
}
fn block_stop(index: u64) -> Vec<u8> {
    event("contentBlockStop", json!({"contentBlockIndex": index}))
}
fn message_stop(reason: &str) -> Vec<u8> {
    event("messageStop", json!({"stopReason": reason}))
}
fn metadata(usage: Value) -> Vec<u8> {
    event(
        "metadata",
        json!({"usage": usage, "metrics": {"latencyMs": 42}}),
    )
}
fn small_usage() -> Vec<u8> {
    metadata(json!({"inputTokens": 3, "outputTokens": 2, "totalTokens": 5}))
}

/// A complete plain-text response.
fn text_response(t: &str) -> Vec<u8> {
    [
        message_start(),
        text(0, t),
        block_stop(0),
        message_stop("end_turn"),
        small_usage(),
    ]
    .concat()
}

// ---------------------------------------------------------------------------
// Harness
// ---------------------------------------------------------------------------

fn model_config(base_url: &str) -> ModelConfig {
    ModelConfig::custom(
        ApiProtocol::BedrockConverseStream,
        "bedrock",
        base_url,
        MODEL,
        "Test model",
    )
}

fn stream_config(base_url: &str) -> StreamConfig {
    let mut config = StreamConfig::new(MODEL, "test-bedrock-api-key");
    config.messages = vec![Message::user("hi")];
    config.model_config = Some(model_config(base_url));
    config
}

async fn serve(body: Vec<u8>) -> MockServer {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(format!("/model/{MODEL}/converse-stream")))
        .respond_with(ResponseTemplate::new(200).set_body_raw(body, EVENTSTREAM))
        .mount(&server)
        .await;
    server
}

async fn run_with_events(base_url: &str) -> (Result<Message, ProviderError>, Vec<StreamEvent>) {
    let (tx, mut rx) = mpsc::unbounded_channel();
    let result = BedrockProvider
        .stream(stream_config(base_url), tx, CancellationToken::new())
        .await;
    let mut events = Vec::new();
    while let Ok(e) = rx.try_recv() {
        events.push(e);
    }
    (result, events)
}

async fn run(body: Vec<u8>) -> Result<Message, ProviderError> {
    let server = serve(body).await;
    run_with_events(&server.uri()).await.0
}

fn parts(msg: &Message) -> (&Vec<Content>, StopReason, &Usage, Option<&str>) {
    match msg {
        Message::Assistant {
            content,
            stop_reason,
            usage,
            error_message,
            ..
        } => (
            content,
            stop_reason.clone(),
            usage,
            error_message.as_deref(),
        ),
        other => panic!("expected an assistant message, got {other:?}"),
    }
}

// ---------------------------------------------------------------------------
// Text, usage, stop reasons
// ---------------------------------------------------------------------------

#[tokio::test]
async fn text_across_frames_with_usage_including_cache() {
    let body = [
        message_start(),
        text(0, "Hel"),
        text(0, "lo, "),
        text(0, "wörld"),
        block_stop(0),
        message_stop("end_turn"),
        metadata(json!({
            "inputTokens": 11, "outputTokens": 7, "totalTokens": 918,
            "cacheReadInputTokens": 800, "cacheWriteInputTokens": 100
        })),
    ]
    .concat();
    let server = serve(body).await;
    let (result, events) = run_with_events(&server.uri()).await;
    let msg = result.expect("a complete response");
    let (content, stop, usage, err) = parts(&msg);
    assert_eq!(stop, StopReason::Stop);
    assert_eq!(err, None);
    assert_eq!(content.len(), 1);
    assert!(matches!(&content[0], Content::Text { text } if text == "Hello, wörld"));
    assert_eq!(
        *usage,
        Usage {
            input: 11,
            output: 7,
            cache_read: 800,
            cache_write: 100,
            total_tokens: 918,
        }
    );
    let deltas: Vec<(usize, String)> = events
        .iter()
        .filter_map(|e| match e {
            StreamEvent::TextDelta {
                content_index,
                delta,
            } => Some((*content_index, delta.clone())),
            _ => None,
        })
        .collect();
    assert_eq!(
        deltas,
        vec![
            (0, "Hel".to_string()),
            (0, "lo, ".to_string()),
            (0, "wörld".to_string())
        ]
    );
    assert!(matches!(events.first(), Some(StreamEvent::Start)));
    assert!(matches!(events.last(), Some(StreamEvent::Done { .. })));
}

#[tokio::test]
async fn separate_text_blocks_stay_separate() {
    let body = [
        message_start(),
        text(0, "first"),
        block_stop(0),
        text(1, "second"),
        block_stop(1),
        message_stop("end_turn"),
        small_usage(),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    let (content, ..) = parts(&msg);
    assert_eq!(content.len(), 2);
    assert!(matches!(&content[0], Content::Text { text } if text == "first"));
    assert!(matches!(&content[1], Content::Text { text } if text == "second"));
}

#[tokio::test]
async fn max_tokens_is_length() {
    let body = [
        message_start(),
        text(0, "cut o"),
        block_stop(0),
        message_stop("max_tokens"),
        small_usage(),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert_eq!(parts(&msg).1, StopReason::Length);
}

#[tokio::test]
async fn guardrail_and_content_filter_are_refusals() {
    for reason in ["guardrail_intervened", "content_filtered"] {
        let body = [
            message_start(),
            text(0, "Sorry."),
            block_stop(0),
            message_stop(reason),
            small_usage(),
        ]
        .concat();
        let msg = run(body).await.unwrap();
        let (content, stop, _, err) = parts(&msg);
        assert_eq!(stop, StopReason::Refusal, "{reason}");
        assert!(err.unwrap().contains(reason), "{err:?}");
        assert!(matches!(&content[0], Content::Text { text } if text == "Sorry."));
    }
}

#[tokio::test]
async fn context_window_exceeded_stop_is_an_overflow() {
    let body = [
        message_start(),
        message_stop("model_context_window_exceeded"),
        small_usage(),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert_eq!(parts(&msg).1, StopReason::Error);
    assert!(msg.is_context_overflow());
}

// ---------------------------------------------------------------------------
// Tool calls
// ---------------------------------------------------------------------------

/// Records the arguments of every execution.
struct Recorder(Arc<Mutex<Vec<Value>>>);

#[async_trait::async_trait]
impl AgentTool for Recorder {
    fn name(&self) -> &str {
        "read_file"
    }
    fn label(&self) -> &str {
        "Read file"
    }
    fn description(&self) -> &str {
        "Read a file"
    }
    fn parameters_schema(&self) -> Value {
        json!({"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}}})
    }
    async fn execute(&self, params: Value, _ctx: ToolContext) -> Result<ToolResult, ToolError> {
        self.0.lock().unwrap().push(params);
        Ok(ToolResult {
            content: vec![Content::Text {
                text: "file contents".into(),
            }],
            details: Value::Null,
        })
    }
}

/// Serve `first` to the first request and `rest` to every later one; run
/// one prompt through the agent loop with the recording tool.
async fn run_agent(first: Vec<u8>, rest: Vec<u8>) -> (Vec<Value>, Vec<AgentMessage>, MockServer) {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(first, EVENTSTREAM))
        .up_to_n_times(1)
        .with_priority(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(rest, EVENTSTREAM))
        .with_priority(2)
        .mount(&server)
        .await;
    let calls = Arc::new(Mutex::new(Vec::new()));
    let mut agent = Agent::from_provider(BedrockProvider, model_config(&server.uri()))
        .with_api_key("test-bedrock-api-key")
        .with_tools(vec![Box::new(Recorder(calls.clone()))]);
    let mut rx = agent.prompt("read it").await;
    while rx.recv().await.is_some() {}
    agent.finish().await;
    let ran = calls.lock().unwrap().clone();
    (ran, agent.messages().to_vec(), server)
}

fn tool_results(messages: &[AgentMessage]) -> Vec<(bool, String)> {
    messages
        .iter()
        .filter_map(|m| match m {
            AgentMessage::Llm(Message::ToolResult {
                content, is_error, ..
            }) => Some((
                *is_error,
                content
                    .iter()
                    .filter_map(|c| match c {
                        Content::Text { text } => Some(text.as_str()),
                        _ => None,
                    })
                    .collect::<String>(),
            )),
            _ => None,
        })
        .collect()
}

/// The #174 headline: `toolUse.input` arrives in pieces and the tool runs
/// with the whole parsed object — not `{}`.
#[tokio::test]
async fn chunked_tool_input_runs_with_the_full_arguments() {
    let first = [
        message_start(),
        text(0, "Reading."),
        block_stop(0),
        tool_start(1, "tooluse_1", "read_file"),
        tool_input(1, ""),
        tool_input(1, "{\"pa"),
        tool_input(1, "th\": \"src/ma"),
        tool_input(1, "in.rs\", \"limit\""),
        tool_input(1, ": 10}"),
        block_stop(1),
        message_stop("tool_use"),
        small_usage(),
    ]
    .concat();
    let (ran, messages, _server) = run_agent(first, text_response("Done.")).await;
    assert_eq!(ran, vec![json!({"path": "src/main.rs", "limit": 10})]);
    let results = tool_results(&messages);
    assert_eq!(results, vec![(false, "file contents".to_string())]);
}

/// Two tool calls whose deltas interleave by `contentBlockIndex`: each
/// input goes to its own block, and every event carries that block's
/// content index.
#[tokio::test]
async fn interleaved_tool_calls_accumulate_by_block_index() {
    let body = [
        message_start(),
        tool_start(0, "a", "read_file"),
        tool_start(1, "b", "read_file"),
        tool_input(1, "{\"path\":"),
        tool_input(0, "{\"path\":"),
        tool_input(1, "\"b.txt\"}"),
        tool_input(0, "\"a.txt\"}"),
        block_stop(1),
        block_stop(0),
        message_stop("tool_use"),
        small_usage(),
    ]
    .concat();
    let server = serve(body).await;
    let (result, events) = run_with_events(&server.uri()).await;
    let msg = result.unwrap();
    let (content, stop, ..) = parts(&msg);
    assert_eq!(stop, StopReason::ToolUse);
    let calls: Vec<(&str, &Value)> = content
        .iter()
        .map(|c| match c {
            Content::ToolCall { id, arguments, .. } => (id.as_str(), arguments),
            other => panic!("unexpected {other:?}"),
        })
        .collect();
    assert_eq!(
        calls,
        vec![
            ("a", &json!({"path": "a.txt"})),
            ("b", &json!({"path": "b.txt"}))
        ]
    );
    let deltas: Vec<(usize, String)> = events
        .iter()
        .filter_map(|e| match e {
            StreamEvent::ToolCallDelta {
                content_index,
                delta,
            } => Some((*content_index, delta.clone())),
            _ => None,
        })
        .collect();
    assert_eq!(
        deltas,
        vec![
            (1, "{\"path\":".into()),
            (0, "{\"path\":".into()),
            (1, "\"b.txt\"}".into()),
            (0, "\"a.txt\"}".into()),
        ]
    );
    let ends: Vec<usize> = events
        .iter()
        .filter_map(|e| match e {
            StreamEvent::ToolCallEnd { content_index } => Some(*content_index),
            _ => None,
        })
        .collect();
    assert_eq!(ends, vec![1, 0]);
}

/// A tool with no parameters: a closed block with no input is `{}`, and
/// the tool runs.
#[tokio::test]
async fn closed_block_without_input_is_a_zero_argument_call() {
    let body = [
        message_start(),
        tool_start(0, "z", "read_file"),
        block_stop(0),
        message_stop("tool_use"),
        small_usage(),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert!(
        matches!(&parts(&msg).0[0], Content::ToolCall { arguments, .. } if *arguments == json!({}))
    );
}

/// #167: input cut off at the token limit is never run on `{}` or on its
/// fragment — the call carries the unparsed marker, the loop answers it
/// with an error result, and the turn reports `Length`.
#[tokio::test]
async fn truncated_tool_input_is_answered_with_an_error_not_run() {
    let first = [
        message_start(),
        tool_start(0, "tooluse_1", "read_file"),
        tool_input(0, "{\"path\": \"src/ma"),
        block_stop(0),
        message_stop("max_tokens"),
        small_usage(),
    ]
    .concat();
    let msg = run(first.clone()).await.unwrap();
    let (content, stop, ..) = parts(&msg);
    assert_eq!(stop, StopReason::Length);
    let Content::ToolCall { arguments, .. } = &content[0] else {
        panic!("{content:?}")
    };
    assert_eq!(
        yoagent::provider::unparsed_tool_arguments(arguments),
        Some("{\"path\": \"src/ma")
    );

    let (ran, messages, _server) = run_agent(first, text_response("unused")).await;
    assert!(ran.is_empty(), "the tool must not run: {ran:?}");
    let results = tool_results(&messages);
    assert_eq!(results.len(), 1);
    assert!(results[0].0, "must be an error result: {results:?}");
}

/// A block that never got its `contentBlockStop` is not a runnable call,
/// even with no input at all — "nothing arrived" and "no parameters" cannot
/// be told apart.
#[tokio::test]
async fn unclosed_tool_block_is_never_runnable() {
    for input in [None, Some("{\"path\": \"a.txt\"}")] {
        let mut frames = vec![message_start(), tool_start(0, "t", "read_file")];
        if let Some(i) = input {
            frames.push(tool_input(0, i));
        }
        frames.push(message_stop("tool_use"));
        frames.push(small_usage());
        let first = frames.concat();

        let msg = run(first.clone()).await.unwrap();
        let Content::ToolCall { arguments, .. } = &parts(&msg).0[0] else {
            panic!()
        };
        assert!(
            yoagent::provider::unparsed_tool_arguments(arguments).is_some(),
            "{input:?}: {arguments}"
        );

        let (ran, messages, _server) = run_agent(first, text_response("ok")).await;
        assert!(ran.is_empty(), "{input:?}: tool ran with {ran:?}");
        assert!(tool_results(&messages)[0].0);
    }
}

// ---------------------------------------------------------------------------
// Reasoning
// ---------------------------------------------------------------------------

/// Reasoning text and signature accumulate into one thinking block, and the
/// next request replays it — text and signature unmodified, as the API
/// reference requires.
#[tokio::test]
async fn reasoning_with_signature_is_accumulated_and_replayed() {
    let first = [
        message_start(),
        reasoning(0, json!({"text": "Let me "})),
        reasoning(0, json!({"text": "think."})),
        reasoning(0, json!({"signature": "c2lnLTE="})),
        block_stop(0),
        tool_start(1, "t1", "read_file"),
        tool_input(1, "{\"path\":\"x\"}"),
        block_stop(1),
        message_stop("tool_use"),
        small_usage(),
    ]
    .concat();

    let msg = run(first.clone()).await.unwrap();
    let (content, ..) = parts(&msg);
    assert_eq!(
        content[0],
        Content::thinking_signed("Let me think.", "c2lnLTE=")
    );

    let (ran, _, server) = run_agent(first, text_response("done")).await;
    assert_eq!(ran, vec![json!({"path": "x"})]);
    let requests = server.received_requests().await.unwrap();
    assert_eq!(requests.len(), 2);
    let body: Value = serde_json::from_slice(&requests[1].body).unwrap();
    let assistant = &body["messages"][1];
    assert_eq!(assistant["role"], "assistant");
    assert_eq!(
        assistant["content"][0]["reasoningContent"]["reasoningText"],
        json!({"text": "Let me think.", "signature": "c2lnLTE="})
    );
    assert_eq!(
        assistant["content"][1]["toolUse"]["input"],
        json!({"path": "x"})
    );
}

/// A run that failed leaves an errored assistant message (one empty text
/// block) in the history; the next prompt does not send it as an empty
/// message, so the two user turns go back as one (#369). The history keeps it.
#[tokio::test]
async fn an_errored_turn_is_not_resent_as_an_empty_message() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(400).set_body_json(json!({"message": "Bad request"})))
        .up_to_n_times(1)
        .with_priority(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200).set_body_raw(text_response("ok"), EVENTSTREAM))
        .with_priority(2)
        .mount(&server)
        .await;
    let mut agent = Agent::from_provider(BedrockProvider, model_config(&server.uri()))
        .with_api_key("test-bedrock-api-key");
    for prompt in ["one", "two"] {
        let mut rx = agent.prompt(prompt).await;
        while rx.recv().await.is_some() {}
        agent.finish().await;
    }
    let errored = agent.messages().iter().any(|m| {
        matches!(
            m,
            AgentMessage::Llm(Message::Assistant {
                stop_reason: StopReason::Error,
                ..
            })
        )
    });
    assert!(errored, "the history keeps the errored turn");
    let requests = server.received_requests().await.unwrap();
    assert_eq!(requests.len(), 2);
    let body: Value = serde_json::from_slice(&requests[1].body).unwrap();
    assert_eq!(
        body["messages"],
        json!([{"role": "user", "content": [{"text": "one"}, {"text": "two"}]}])
    );
}

/// A parallel tool call through the real loop: both results go back in one
/// user message, in call order, so Converse's roles alternate (#223).
#[tokio::test]
async fn parallel_tool_results_go_back_in_one_message() {
    let first = [
        message_start(),
        tool_start(0, "a", "read_file"),
        tool_input(0, "{\"path\":\"a\"}"),
        block_stop(0),
        tool_start(1, "b", "read_file"),
        tool_input(1, "{\"path\":\"b\"}"),
        block_stop(1),
        message_stop("tool_use"),
        small_usage(),
    ]
    .concat();
    let (ran, _, server) = run_agent(first, text_response("Done.")).await;
    assert_eq!(ran.len(), 2);
    let requests = server.received_requests().await.unwrap();
    assert_eq!(requests.len(), 2);
    let body: Value = serde_json::from_slice(&requests[1].body).unwrap();
    let messages = body["messages"].as_array().unwrap();
    let roles: Vec<&str> = messages
        .iter()
        .map(|m| m["role"].as_str().unwrap())
        .collect();
    assert_eq!(roles, ["user", "assistant", "user"], "{body}");
    let results = messages[2]["content"].as_array().unwrap();
    assert_eq!(results.len(), 2, "{body}");
    assert_eq!(results[0]["toolResult"]["toolUseId"], "a");
    assert_eq!(results[1]["toolResult"]["toolUseId"], "b");
}

// ---------------------------------------------------------------------------
// Framing over the network
// ---------------------------------------------------------------------------

/// Serve one response over a raw socket as HTTP/1.1 chunked transfer, one
/// chunk per piece with a pause between, so the client sees the body in
/// those pieces. `complete = false` drops the connection without the final
/// chunk.
async fn serve_chunked(pieces: Vec<Vec<u8>>, complete: bool) -> ChunkedServer {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let handle = tokio::spawn(async move {
        let (mut sock, _) = listener.accept().await.unwrap();
        // Read the request: headers, then Content-Length bytes of body.
        let mut req = Vec::new();
        let mut buf = [0u8; 4096];
        loop {
            let n = sock.read(&mut buf).await.unwrap();
            req.extend_from_slice(&buf[..n]);
            if let Some(end) = req.windows(4).position(|w| w == b"\r\n\r\n") {
                let head = String::from_utf8_lossy(&req[..end]).to_ascii_lowercase();
                let len: usize = head
                    .lines()
                    .find_map(|l| l.strip_prefix("content-length:"))
                    .map(|v| v.trim().parse().unwrap())
                    .unwrap_or(0);
                if req.len() >= end + 4 + len {
                    break;
                }
            }
            if n == 0 {
                break;
            }
        }
        let head = format!(
            "HTTP/1.1 200 OK\r\ncontent-type: {EVENTSTREAM}\r\ntransfer-encoding: chunked\r\n\r\n"
        );
        sock.write_all(head.as_bytes()).await.unwrap();
        for piece in pieces {
            let mut chunk = format!("{:x}\r\n", piece.len()).into_bytes();
            chunk.extend_from_slice(&piece);
            chunk.extend_from_slice(b"\r\n");
            sock.write_all(&chunk).await.unwrap();
            sock.flush().await.unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
        if complete {
            sock.write_all(b"0\r\n\r\n").await.unwrap();
        }
        sock.shutdown().await.ok();
    });
    ChunkedServer {
        base: format!("http://{addr}"),
        handle,
    }
}

struct ChunkedServer {
    base: String,
    handle: tokio::task::JoinHandle<()>,
}

impl ChunkedServer {
    /// Wait for the server task, so a panic in it surfaces as itself and a
    /// hung accept cannot hang the suite.
    async fn join(self) {
        tokio::time::timeout(std::time::Duration::from_secs(10), self.handle)
            .await
            .expect("raw-socket test server hung")
            .expect("raw-socket test server panicked");
    }
}

/// Run one request against a raw-socket server and join it.
async fn run_chunked(pieces: Vec<Vec<u8>>, complete: bool) -> Result<Message, ProviderError> {
    let server = serve_chunked(pieces, complete).await;
    let (result, _) = run_with_events(&server.base).await;
    server.join().await;
    result
}

fn split_at_offsets(body: &[u8], offsets: &[usize]) -> Vec<Vec<u8>> {
    let mut pieces = Vec::new();
    let mut start = 0;
    for &o in offsets {
        pieces.push(body[start..o].to_vec());
        start = o;
    }
    pieces.push(body[start..].to_vec());
    pieces
}

/// Smoke test of the real HTTP path: the body arrives in chunks that split
/// frames inside a prelude, inside headers and between the bytes of a
/// multi-byte character, and still decodes. (A split character is harmless
/// by construction — payloads are only decoded from complete frames; the
/// proof that *every* split point works is the every-chunk-size unit tests
/// in `bedrock.rs` and `eventstream.rs`.)
#[tokio::test]
async fn frames_split_across_network_chunks_at_awkward_offsets() {
    let frames = [
        message_start(),
        text(0, "日本語のテキスト"),
        tool_start(1, "t", "read_file"),
        tool_input(1, "{\"path\":\"ü.txt\"}"),
        block_stop(1),
        message_stop("tool_use"),
        small_usage(),
    ];
    let body = frames.concat();
    let f0 = frames[0].len();
    // Inside the second frame's prelude (5 bytes in), inside its headers
    // (20 bytes in), inside the first 3-byte character of its payload, and
    // one byte into the third frame's CRC-covered prelude.
    let utf8 = body[f0..]
        .windows(3)
        .position(|w| w == "日".as_bytes())
        .unwrap()
        + f0;
    let offsets = [
        f0 + 5,
        f0 + 20,
        utf8 + 1,
        utf8 + 2,
        f0 + frames[1].len() + 1,
    ];
    let msg = run_chunked(split_at_offsets(&body, &offsets), true)
        .await
        .unwrap();
    let (content, stop, ..) = parts(&msg);
    assert_eq!(stop, StopReason::ToolUse);
    assert!(
        matches!(&content[0], Content::Text { text } if text == "日本語のテキスト"),
        "{content:?}"
    );
    assert!(
        matches!(&content[1], Content::ToolCall { arguments, .. } if *arguments == json!({"path": "ü.txt"}))
    );
}

/// The connection drops mid-frame after real content arrived: an error,
/// never the partial content as a finished turn.
#[tokio::test]
async fn connection_dropped_mid_stream_is_an_error() {
    let body = [
        message_start(),
        text(0, "partial answer"),
        message_stop("end_turn"),
    ]
    .concat();
    let cut = body.len() - 10;
    let result = run_chunked(vec![body[..cut].to_vec()], false).await;
    assert!(
        matches!(result, Err(ProviderError::Network(_))),
        "{result:?}"
    );
}

// ---------------------------------------------------------------------------
// Errors: never an empty successful turn
// ---------------------------------------------------------------------------

#[tokio::test]
async fn crc_mismatch_is_an_error() {
    let mut body = text_response("hello");
    // Flip a byte inside the second frame's JSON payload (10 bytes before
    // its trailing CRC); its message CRC no longer matches.
    let end_of_second = message_start().len() + text(0, "hello").len();
    body[end_of_second - 10] ^= 0x01;
    let result = run(body).await;
    let err = result.expect_err("a corrupted frame must fail the turn");
    assert!(err.to_string().contains("checksum"), "{err}");
}

#[tokio::test]
async fn truncated_frame_at_end_of_body_is_an_error() {
    let body = text_response("hello");
    let result = run(body[..body.len() - 3].to_vec()).await;
    assert!(
        matches!(result, Err(ProviderError::Network(_))),
        "{result:?}"
    );
}

#[tokio::test]
async fn stream_without_message_stop_is_an_error() {
    let body = [message_start(), text(0, "Hello"), block_stop(0)].concat();
    let result = run(body).await;
    assert!(
        matches!(result, Err(ProviderError::Network(ref m)) if m.contains("messageStop")),
        "{result:?}"
    );
}

#[tokio::test]
async fn empty_body_is_an_error_not_an_empty_turn() {
    let result = run(Vec::new()).await;
    assert!(
        matches!(result, Err(ProviderError::Network(_))),
        "{result:?}"
    );
}

/// What #174 reported: a JSON-lines body (what the old parser expected) is
/// not an event stream and must fail loudly instead of parsing to nothing.
#[tokio::test]
async fn json_lines_body_is_an_error() {
    let body = b"{\"contentBlockDelta\":{\"delta\":{\"text\":\"hi\"},\"contentBlockIndex\":0}}\n{\"messageStop\":{\"stopReason\":\"end_turn\"}}\n".to_vec();
    let result = run(body).await;
    // Rejected by the framing layer: the first 12 bytes are not a prelude
    // with a matching CRC.
    match result {
        Err(ProviderError::Other(m)) => assert!(m.contains("prelude checksum"), "{m}"),
        other => panic!("expected a framing error, got {other:?}"),
    }
}

#[tokio::test]
async fn throttling_exception_is_a_retryable_rate_limit() {
    let body = [
        message_start(),
        exception(
            "throttlingException",
            "Too many tokens, please wait before trying again.",
        ),
    ]
    .concat();
    let result = run(body).await;
    assert!(
        matches!(result, Err(ProviderError::RateLimited { .. })),
        "{result:?}"
    );
}

#[tokio::test]
async fn validation_exception_with_overflow_message_is_context_overflow() {
    let body = exception(
        "validationException",
        "Input is too long for requested model.",
    );
    let result = run(body).await;
    assert!(
        matches!(result, Err(ref e) if e.is_context_overflow()),
        "{result:?}"
    );
}

#[tokio::test]
async fn model_stream_error_exception_mid_stream_is_an_error() {
    let body = [
        message_start(),
        text(0, "Half an ans"),
        exception("modelStreamErrorException", "The model stream failed."),
    ]
    .concat();
    let result = run(body).await;
    match result {
        Err(ProviderError::Api(m)) => assert!(m.contains("modelStreamErrorException"), "{m}"),
        other => panic!("expected an API error, got {other:?}"),
    }
}

#[tokio::test]
async fn http_errors_are_classified() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(400)
                .insert_header(
                    "x-amzn-ErrorType",
                    "ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/",
                )
                .set_body_json(json!({"message": "Input is too long for requested model."})),
        )
        .mount(&server)
        .await;
    let (result, _) = run_with_events(&server.uri()).await;
    assert!(
        matches!(result, Err(ref e) if e.is_context_overflow()),
        "{result:?}"
    );

    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(429)
                .insert_header("retry-after", "2")
                .set_body_json(json!({"message": "Too many requests"})),
        )
        .mount(&server)
        .await;
    let (result, _) = run_with_events(&server.uri()).await;
    assert!(
        matches!(
            result,
            Err(ProviderError::RateLimited {
                retry_after_ms: Some(2000)
            })
        ),
        "{result:?}"
    );
}

#[tokio::test]
async fn json_success_body_is_an_error() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"output": {"message": {"content": [{"text": "hi"}]}}})),
        )
        .mount(&server)
        .await;
    let (result, _) = run_with_events(&server.uri()).await;
    assert!(matches!(result, Err(ProviderError::Api(_))), "{result:?}");
}

// ---------------------------------------------------------------------------
// Review round: redacted reasoning, completeness, dropped content, protocol
// ---------------------------------------------------------------------------

/// A reasoning block that is only `redactedContent` is kept as a redacted
/// thinking block and replayed as `redactedContent` — never as an empty
/// `reasoningText` with an empty signature, which Bedrock rejects.
#[tokio::test]
async fn redacted_reasoning_is_kept_and_replayed() {
    // Two deltas: the bytes are concatenated, not the base64 strings.
    let first = [
        message_start(),
        reasoning(0, json!({"redactedContent": "AAEC"})), // 00 01 02
        reasoning(0, json!({"redactedContent": "AwQ="})), // 03 04
        block_stop(0),
        text(1, "Reading."),
        block_stop(1),
        tool_start(2, "t1", "read_file"),
        tool_input(2, "{\"path\":\"x\"}"),
        block_stop(2),
        message_stop("tool_use"),
        small_usage(),
    ]
    .concat();

    let msg = run(first.clone()).await.unwrap();
    let (content, ..) = parts(&msg);
    assert_eq!(
        content[0],
        Content::thinking_redacted(ApiProtocol::BedrockConverseStream, "AAECAwQ=")
    );
    // The text next to it keeps its own block and index.
    assert!(matches!(&content[1], Content::Text { text } if text == "Reading."));

    let (ran, _, server) = run_agent(first, text_response("done")).await;
    assert_eq!(ran, vec![json!({"path": "x"})]);
    let requests = server.received_requests().await.unwrap();
    let body: Value = serde_json::from_slice(&requests[1].body).unwrap();
    let replayed = &body["messages"][1]["content"];
    assert_eq!(
        replayed[0],
        json!({"reasoningContent": {"redactedContent": "AAECAwQ="}})
    );
    assert_eq!(replayed[1], json!({"text": "Reading."}));
    assert!(
        !body.to_string().contains("\"signature\":\"\""),
        "no empty signature may be sent: {body}"
    );
}

/// #197 provenance: encrypted reasoning goes back only to the API that
/// produced it. Anthropic's `redacted_thinking` data is not documented to be
/// Bedrock's `redactedContent` bytes, so it is skipped here, while Bedrock's
/// own block in the same turn is still replayed in place.
#[tokio::test]
async fn redacted_thinking_from_the_anthropic_api_is_not_replayed_to_bedrock() {
    let server = serve(text_response("ok")).await;
    let mut config = stream_config(&server.uri());
    config.messages = vec![
        Message::user("hi"),
        Message::assistant(
            vec![
                Content::thinking_redacted(ApiProtocol::AnthropicMessages, "ANTHROPIC-DATA"),
                Content::thinking_redacted(ApiProtocol::BedrockConverseStream, "AAEC"),
                Content::Text { text: "a".into() },
            ],
            StopReason::Stop,
            MODEL,
            "anthropic",
            Usage::default(),
        ),
        Message::user("next"),
    ];
    let (tx, _rx) = mpsc::unbounded_channel();
    BedrockProvider
        .stream(config, tx, CancellationToken::new())
        .await
        .expect("stream succeeds");

    let requests = server.received_requests().await.unwrap();
    let body: Value = serde_json::from_slice(&requests[0].body).unwrap();
    assert!(
        !body.to_string().contains("ANTHROPIC-DATA"),
        "Anthropic's redacted data must not reach Bedrock: {body}"
    );
    assert_eq!(
        body["messages"][1]["content"],
        json!([
            {"reasoningContent": {"redactedContent": "AAEC"}},
            {"text": "a"},
        ])
    );
}

/// Signature deltas are appended (a delta union member, like text).
#[tokio::test]
async fn signature_deltas_are_appended() {
    let body = [
        message_start(),
        reasoning(0, json!({"text": "hmm"})),
        reasoning(0, json!({"signature": "sig-a"})),
        reasoning(0, json!({"signature": "sig-b"})),
        block_stop(0),
        text(1, "ok"),
        block_stop(1),
        message_stop("end_turn"),
        small_usage(),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert_eq!(
        parts(&msg).0[0],
        Content::thinking_signed("hmm", "sig-asig-b")
    );
}

/// Once `messageStop` and `metadata` have arrived the response is complete:
/// a connection that then drops (cleanly or mid-frame) keeps it rather than
/// failing into a re-billed retry.
#[tokio::test]
async fn drop_after_a_complete_response_keeps_it() {
    let complete = text_response("all here");
    // Drop without the terminating chunk.
    let msg = run_chunked(vec![complete.clone()], false).await.unwrap();
    assert!(matches!(&parts(&msg).0[0], Content::Text { text } if text == "all here"));
    // Half a trailing frame, then a clean end of body.
    let mut body = complete.clone();
    body.extend_from_slice(&text(0, "extra")[..10]);
    let msg = run(body).await.unwrap();
    assert_eq!(parts(&msg).1, StopReason::Stop);
    // Half a trailing frame, then a dropped connection.
    let mut body = complete;
    body.extend_from_slice(&text(0, "extra")[..10]);
    let msg = run_chunked(vec![body], false).await.unwrap();
    assert_eq!(parts(&msg).2.total_tokens, 5);
}

/// `messageStop` alone is not complete: a drop before `metadata` is still an
/// error (usage would be unknown).
#[tokio::test]
async fn drop_before_metadata_is_an_error() {
    let mut body = [
        message_start(),
        text(0, "hi"),
        block_stop(0),
        message_stop("end_turn"),
    ]
    .concat();
    body.extend_from_slice(&small_usage()[..10]);
    let result = run_chunked(vec![body], false).await;
    assert!(
        matches!(result, Err(ProviderError::Network(_))),
        "{result:?}"
    );
}

/// A stream that ends cleanly after `messageStop` without `metadata` is a
/// finished turn with zero usage, as the other providers report it.
#[tokio::test]
async fn message_stop_without_metadata_reports_zero_usage() {
    let body = [
        message_start(),
        text(0, "hi"),
        block_stop(0),
        message_stop("end_turn"),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert_eq!(*parts(&msg).2, Usage::default());
}

/// The last `metadata` event wins.
#[tokio::test]
async fn last_metadata_wins() {
    let body = [
        message_start(),
        text(0, "hi"),
        message_stop("end_turn"),
        metadata(json!({"inputTokens": 1, "outputTokens": 1, "totalTokens": 2})),
        metadata(json!({"inputTokens": 9, "outputTokens": 4, "totalTokens": 13})),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert_eq!(parts(&msg).2.input, 9);
    assert_eq!(parts(&msg).2.total_tokens, 13);
}

/// Fields the provider does not need may be missing without failing a
/// finished turn.
#[tokio::test]
async fn unneeded_fields_may_be_missing() {
    let body = [
        event("messageStart", json!({})),
        text(0, "hi"),
        message_stop("end_turn"),
        metadata(json!({"inputTokens": 2, "outputTokens": 3})),
    ]
    .concat();
    let msg = run(body).await.unwrap();
    assert_eq!(parts(&msg).2.input, 2);
    assert_eq!(parts(&msg).2.total_tokens, 0);
}

/// A server-side tool (`type: server_tool_use`) runs on AWS's side: it is
/// not surfaced as a tool call and nothing executes locally; the text block
/// after it keeps index 0.
#[tokio::test]
async fn server_side_tool_use_is_not_executed() {
    let first = [
        message_start(),
        event(
            "contentBlockStart",
            json!({"contentBlockIndex": 0, "start": {"toolUse": {"toolUseId": "s1", "name": "read_file", "type": "server_tool_use"}}}),
        ),
        tool_input(0, "{\"path\":\"secret\"}"),
        block_stop(0),
        event(
            "contentBlockStart",
            json!({"contentBlockIndex": 1, "start": {"toolResult": {"toolUseId": "s1"}}}),
        ),
        block_stop(1),
        text(2, "The answer."),
        block_stop(2),
        message_stop("end_turn"),
        small_usage(),
    ]
    .concat();
    let msg = run(first.clone()).await.unwrap();
    let (content, stop, ..) = parts(&msg);
    assert_eq!(stop, StopReason::Stop);
    assert_eq!(content.len(), 1, "{content:?}");
    assert!(matches!(&content[0], Content::Text { text } if text == "The answer."));

    let (ran, messages, _server) = run_agent(first, text_response("unused")).await;
    assert!(ran.is_empty(), "{ran:?}");
    assert!(tool_results(&messages).is_empty());
}

/// A refusal or an error stop after a complete tool block keeps its verdict
/// and the tool is not run.
#[tokio::test]
async fn refusal_or_error_after_a_tool_block_is_not_run() {
    for (reason, expected) in [
        ("guardrail_intervened", StopReason::Refusal),
        ("malformed_tool_use", StopReason::Error),
    ] {
        let first = [
            message_start(),
            tool_start(0, "t", "read_file"),
            tool_input(0, "{\"path\":\"a\"}"),
            block_stop(0),
            message_stop(reason),
            small_usage(),
        ]
        .concat();
        let msg = run(first.clone()).await.unwrap();
        assert_eq!(parts(&msg).1, expected, "{reason}");
        let (ran, _, _server) = run_agent(first, text_response("unused")).await;
        assert!(ran.is_empty(), "{reason}: tool ran with {ran:?}");
    }
}

/// An unmodeled error frame (`:message-type: error`, name in `:error-code`).
#[tokio::test]
async fn error_frame_with_throttling_code_is_a_rate_limit() {
    let body = [
        message_start(),
        text(0, "partial"),
        frame(
            &[
                (":message-type", "error"),
                (":error-code", "ThrottlingException"),
                (":error-message", "Rate exceeded"),
            ],
            b"",
        ),
    ]
    .concat();
    let result = run(body).await;
    assert!(
        matches!(result, Err(ProviderError::RateLimited { .. })),
        "{result:?}"
    );
}

/// Protocol violations are errors, not guesses.
#[tokio::test]
async fn protocol_violations_are_errors() {
    let cases: Vec<(&str, Vec<u8>)> = vec![
        (
            "delta after stop",
            [message_start(), text(0, "a"), block_stop(0), text(0, "b")].concat(),
        ),
        (
            "duplicate start",
            [
                message_start(),
                tool_start(0, "a", "read_file"),
                tool_start(0, "b", "read_file"),
            ]
            .concat(),
        ),
        (
            "tool delta without start",
            [message_start(), tool_input(0, "{}")].concat(),
        ),
        (
            "kind mismatch",
            [
                message_start(),
                text(0, "a"),
                reasoning(0, json!({"text": "b"})),
            ]
            .concat(),
        ),
    ];
    for (name, mut body) in cases {
        body.extend(message_stop("end_turn"));
        body.extend(small_usage());
        let result = run(body).await;
        assert!(
            matches!(result, Err(ProviderError::Other(ref m)) if m.contains("Bedrock stream")),
            "{name}: {result:?}"
        );
    }
}

/// An unclosed tool block still closes its lifecycle with `ToolCallEnd`.
#[tokio::test]
async fn unclosed_tool_block_emits_tool_call_end() {
    let body = [
        message_start(),
        text(0, "x"),
        tool_start(1, "t", "read_file"),
        tool_input(1, "{\"pa"),
        message_stop("max_tokens"),
        small_usage(),
    ]
    .concat();
    let server = serve(body).await;
    let (result, events) = run_with_events(&server.uri()).await;
    result.unwrap();
    let ends: Vec<usize> = events
        .iter()
        .filter_map(|e| match e {
            StreamEvent::ToolCallEnd { content_index } => Some(*content_index),
            _ => None,
        })
        .collect();
    assert_eq!(ends, vec![1]);
}

/// Forward compatibility: an unknown event type, an unknown `start` member
/// and a not-surfaced delta member do not fail the turn or shift indices,
/// and a block whose start was unrecognised is typed by its first delta.
#[tokio::test]
async fn unknown_members_and_events_are_skipped() {
    let body = [
        message_start(),
        event("someFutureEvent", json!({"data": 1})),
        event(
            "contentBlockStart",
            json!({"contentBlockIndex": 0, "start": {"futureThing": {"a": 1}}}),
        ),
        text(0, "typed by its delta"),
        block_stop(0),
        event(
            "contentBlockStart",
            json!({"contentBlockIndex": 1, "start": {"image": {"format": "png"}}}),
        ),
        event(
            "contentBlockDelta",
            json!({"contentBlockIndex": 1, "delta": {"image": {"source": {}}}}),
        ),
        block_stop(1),
        event(
            "contentBlockDelta",
            json!({"contentBlockIndex": 2, "delta": {"citation": {"title": "x"}}}),
        ),
        text(3, "after"),
        block_stop(3),
        message_stop("end_turn"),
        small_usage(),
    ]
    .concat();
    let server = serve(body).await;
    let (result, events) = run_with_events(&server.uri()).await;
    let msg = result.unwrap();
    let (content, ..) = parts(&msg);
    assert_eq!(content.len(), 2, "{content:?}");
    assert!(matches!(&content[0], Content::Text { text } if text == "typed by its delta"));
    assert!(matches!(&content[1], Content::Text { text } if text == "after"));
    let indices: Vec<usize> = events
        .iter()
        .filter_map(|e| match e {
            StreamEvent::TextDelta { content_index, .. } => Some(*content_index),
            _ => None,
        })
        .collect();
    assert_eq!(indices, vec![0, 1]);
}

/// `x-amzn-ErrorType` names the error in the message.
#[tokio::test]
async fn http_error_names_the_aws_error_type() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(403)
                .insert_header("x-amzn-ErrorType", "AccessDeniedException:http://x/")
                .set_body_json(json!({"message": "not allowed"})),
        )
        .mount(&server)
        .await;
    let (result, _) = run_with_events(&server.uri()).await;
    match result {
        Err(ProviderError::Auth(m)) => assert!(
            m.contains("AccessDeniedException") && m.contains("not allowed"),
            "{m}"
        ),
        other => panic!("expected Auth, got {other:?}"),
    }
}

/// A 200 with some other declared content type reports its body.
#[tokio::test]
async fn non_eventstream_content_type_reports_the_body() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(200).set_body_raw("<html>gateway says no</html>", "text/html"),
        )
        .mount(&server)
        .await;
    let (result, _) = run_with_events(&server.uri()).await;
    match result {
        Err(ProviderError::Api(m)) => assert!(
            m.contains("text/html") && m.contains("gateway says no"),
            "{m}"
        ),
        other => panic!("expected Api, got {other:?}"),
    }
}