causa-protocol 0.1.5

Causa — a minimal context kernel for Rust agents (facts, ports, adapters)
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
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
//! Anthropic Messages translation for the context kernel.
//!
//! Pure functions from [`causa_kernel::ContextFrame`] to an Anthropic
//! Messages request body, and from an Anthropic Messages response body
//! back to [`causa_kernel::ModelOutput`]. Transport-free — the reqwest
//! adapter in `causa-provider` owns HTTP.
//!
//! The renderer-independent policy (role mapping, empty-text skip,
//! no cross-block text join, tool id pairing, observation stringification) lives in
//! `super::context_frame`; this module only shapes the Anthropic wire.
//!
//! # Anthropic-specific structural rules
//!
//! - The leading run of `system` text segments moves to the top-level
//!   `system` parameter, always as a block array with no join; a `system`
//!   block after conversation content renders at its position as a
//!   `system` message, an append like any other. Media in a leading
//!   system-role block degrades to its placeholder text in the parameter —
//!   the parameter carries no media blocks.
//! - Consecutive segments with the same wire role merge into one
//!   message; Anthropic requires strictly alternating `user` /
//!   `assistant` roles.
//! - Resolved media parts render as `image` content blocks (`image/*`,
//!   `base64` source for inline payloads, `url` source for remote URLs)
//!   or `document` content blocks (`application/pdf`, with `title` when
//!   named) inside user messages; unresolved, unclassified, or unsupported
//!   media degrades to the shared placeholder text.
//! - Tool calls render as assistant `tool_use` content blocks with
//!   `input` as a JSON object; tool results render as `tool_result`
//!   content blocks in the following user message. Result media embeds
//!   natively: with attachments the `tool_result` `content` becomes a
//!   block array (text, then the images and documents); without, it stays a
//!   string.
//!   Any status other than `Succeeded` sets `is_error: true` (the flag
//!   is Anthropic-only; OpenAI-family wires carry error information in
//!   the content).
//! - `GenerationOptions::max_tokens` is required by Anthropic; a `None`
//!   fails with `InvalidRequest` before HTTP. The adapter sends no default
//!   ceiling. [`AnthropicRenderOptions`] holds the choices only this
//!   protocol has.
//! - Tools: by default `tools` carries the frame's current tool state, the
//!   fold of its `ToolChange` blocks, so a change rewrites it. With
//!   [`AnthropicRenderOptions::native_tool_changes`], `tools` stays at the
//!   frame's first change (the declaration), followed by a deferred
//!   placeholder tool, and each later change renders at its position as a
//!   `system` message of `tool_removal` and `tool_addition` blocks (inline
//!   definitions), as `pi-ai` sends them. The request then needs
//!   [`INLINE_TOOLS_BETA`]. A change must follow a user turn; one that would
//!   render first or after assistant content is an `InvalidRequest`. An
//!   empty declaration renders as by default, because the API rejects a
//!   tool list in which every tool is deferred.
//! - [`CacheDirective::StablePrefix`] marks three `cache_control`
//!   breakpoints: the last tool definition, the `system` parameter
//!   (rendered as a block array so the anchor can attach — Anthropic
//!   cannot hang the marker off a string parameter), and the latest
//!   stable conversation message (the previous message when the final
//!   one carries this round's dispatched `tool_result` blocks, the
//!   final message otherwise). [`CacheDirective::None`] renders
//!   byte-identically to a body without any cache key.
//! - Wire fidelity. Each `text` and `tool_use` block parses to one item
//!   whose `wire.fields` is the block minus the keys its content holds
//!   (`text`; `id`, `name`, `input`); the renderer starts from those fields
//!   and writes the fact-derived keys over them. Text fields apply only to a
//!   block of exactly one text part. An own-format sealed part is emitted
//!   verbatim as an assistant content block.
//! - Reasoning is material. Every `thinking` block, `redacted_thinking` block
//!   and content block of a type this adapter has no vocabulary for is kept
//!   as one sealed item in place, with its stored value written back verbatim
//!   on the next turn; the readable `thinking` text additionally fills the
//!   reasoning observation, which is display-only.
//! - `reasoning_effort` is written as `output_config.effort` exactly as
//!   given. No `thinking` object is derived from it and no value is refused:
//!   a host that wants `thinking`, with or without `block_binding`, sends the
//!   object in `GenerationOptions::extra_body`, which is merged into the
//!   body beside what this renderer wrote. `output_schema` has no mapping in
//!   this adapter and is rejected before HTTP.

use serde_json::{Value, json};

use causa_kernel::{
    CacheDirective, ContextFrame, GenerationOptions, ModelInvokeError, ModelInvokeErrorKind,
    ModelItem, ModelOutput, ModelRef, ModelResponse, ReasoningPayload, ToolCallDraft,
    ToolDefinition, ToolResultStatus,
};

use super::context_frame::{self, ResolvedMedia, Role, Segment};
use super::media::MediaSet;
use super::media::MediaSource;
use super::render_options::AnthropicRenderOptions;
use crate::Protocol;

/// The `anthropic-beta` value that in-place tool changes need: it covers
/// `tool_addition` and `tool_removal` blocks, by reference and with inline
/// definitions.
pub const INLINE_TOOLS_BETA: &str = "inline-tools-2026-09-15";

/// Name of the deferred placeholder tool sent in in-place mode.
const DEFERRED_PLACEHOLDER: &str = "__causa_deferred_placeholder__";

/// A deferred tool that is never added, sent last in `tools` in in-place
/// mode. The API adds hidden prompt scaffolding for tool changes; declaring a
/// deferred tool from the first request keeps that scaffolding in the cached
/// prefix, so the first change does not miss the cache (as `pi-ai` reports
/// and does). The model cannot see or call it.
fn deferred_placeholder() -> Value {
    json!({
        "name": DEFERRED_PLACEHOLDER,
        "description": "Reserved placeholder. Never available. Never call this.",
        "input_schema": {"type": "object", "properties": {}},
        "defer_loading": true,
    })
}

/// Render a [`ContextFrame`] into an Anthropic Messages request body.
///
/// The body is complete: `model`, `max_tokens`, `messages`, plus `system`,
/// `temperature`, `tools` and `output_config` when the inputs call for them,
/// then the host's extra request fields. Media references resolve through `media`;
/// unresolvable or unsupported references degrade to the shared text
/// placeholder. `options` holds the choices only this protocol has; see
/// [`AnthropicRenderOptions`]. Rendering is deterministic — the same frame,
/// generation, model, media table, cache directive and options always
/// produce byte-identical JSON.
pub fn render_anthropic_messages(
    frame: &ContextFrame,
    media: &MediaSet,
    generation: &GenerationOptions,
    model: &ModelRef,
    cache: CacheDirective,
    options: &AnthropicRenderOptions,
) -> Result<Value, ModelInvokeError> {
    super::generation::validate_anthropic(generation)?;
    super::tool_history::validate_frame(frame)?;
    let tool_plan = context_frame::plan_tools(
        frame,
        if options.native_tool_changes {
            context_frame::ToolPlacement::AnthropicInPlace
        } else {
            context_frame::ToolPlacement::Fold
        },
    );
    let normalized = context_frame::normalize(
        frame,
        media,
        Protocol::AnthropicMessages,
        tool_plan.in_place,
    );

    // Only the leading system run belongs in the top-level `system`
    // parameter; a system block appended after conversation content renders at
    // its position as a `system` message, exactly where the fact sits. Nothing
    // is hoisted.
    let segments = &normalized.segments;
    let leading = context_frame::leading_system_len(segments);
    let (leading_segments, conversation) = segments.split_at(leading);
    let mut system_blocks: Vec<Value> = leading_segments
        .iter()
        .filter_map(|segment| match segment {
            Segment::Text {
                role: Role::System,
                text,
                ..
            } => Some(json!({"type": "text", "text": text})),
            Segment::Media {
                role: Role::System,
                media: ResolvedMedia::Placeholder(text),
                ..
            } => Some(json!({"type": "text", "text": text})),
            _ => None,
        })
        .collect();

    // Group consecutive same-wire-role segments into one message
    // (Anthropic requires strictly alternating roles).
    let mut messages: Vec<(&'static str, Vec<Value>)> = Vec::new();
    // Positions of verbatim sealed blocks in `messages`; the cache anchor
    // never lands on one, because a sealed block is never modified. The
    // stored item can be any content-block type, so the position is tracked
    // rather than guessed from the block's `type`.
    let mut sealed_blocks: Vec<(usize, usize)> = Vec::new();
    // Positions of tool change blocks: not the place for the cache anchor
    // either, and a message holding only them does not end the stable
    // prefix.
    let mut change_blocks: Vec<(usize, usize)> = Vec::new();
    // The tool state the in-place changes apply to, from the declaration on.
    let mut tools_offered: Vec<ToolDefinition> = tool_plan.tools.clone();
    for segment in conversation {
        match segment {
            Segment::Text {
                role: Role::System,
                text,
                ..
            } => append_message(
                &mut messages,
                "system",
                json!({"type": "text", "text": text}),
            ),
            Segment::Text {
                role: Role::User,
                text,
                ..
            } => {
                append_message(&mut messages, "user", json!({"type": "text", "text": text}));
            }
            Segment::Text {
                role: Role::Assistant,
                text,
                origin,
            } => {
                // A whitespace-only assistant text part is omitted from the
                // request (as `pi-ai` does); the fact stays in the Context.
                // This is a listed exception to the round-trip property.
                if context_frame::is_blank_text(text) {
                    continue;
                }
                // The provider's fields ride along only for a block that is
                // exactly one text part: they describe that one wire block.
                let mut block = match origin.wire.as_ref().filter(|_| origin.parts == 1) {
                    Some(fields) => context_frame::residue_object(Some(fields)),
                    None => serde_json::Map::new(),
                };
                block.entry("type").or_insert_with(|| json!("text"));
                block.insert("text".into(), json!(text));
                append_message(&mut messages, "assistant", Value::Object(block));
            }
            Segment::Sealed(item) => {
                sealed_blocks.push(append_sealed(&mut messages, item.clone()));
            }
            Segment::Media { role, media, .. } => {
                append_message(&mut messages, role_name(*role), media_block_json(media));
            }
            Segment::ToolCall(call) => {
                let mut block = context_frame::residue_object(call.wire.as_ref());
                block.entry("type").or_insert_with(|| json!("tool_use"));
                block.insert("id".into(), json!(call.wire_id));
                block.insert("name".into(), json!(call.name));
                block.insert("input".into(), call.arguments.clone());
                append_message(&mut messages, "assistant", Value::Object(block));
            }
            Segment::ToolResult {
                wire_id,
                status,
                content,
                notes,
                media,
            } => {
                let mut blocks = vec![json!({"type": "text", "text": content})];
                if !notes.is_empty() {
                    blocks.push(json!({"type": "text", "text": "Notes:"}));
                    blocks.extend(notes.iter().map(|note| {
                        json!({
                            "type": "text",
                            "text": format!("- {note}"),
                        })
                    }));
                }
                blocks.extend(media.iter().map(media_block_json));
                let content_json = if notes.is_empty() && media.is_empty() {
                    json!(content)
                } else {
                    json!(blocks)
                };
                let mut block_json = json!({
                    "type": "tool_result",
                    "tool_use_id": wire_id,
                    "content": content_json,
                });
                if *status != ToolResultStatus::Succeeded {
                    block_json["is_error"] = json!(true);
                }
                append_message(&mut messages, "user", block_json);
            }
            Segment::ToolChange(change) => {
                // The API takes a change only after a user turn (tool results
                // count) or within a system message; the renderer never moves
                // one there.
                if !matches!(messages.last(), Some(("user" | "system", _))) {
                    return Err(ModelInvokeError::new(
                        ModelInvokeErrorKind::InvalidRequest,
                        "an Anthropic tool change must follow a user turn",
                    ));
                }
                for block in tool_change_blocks(change, &tools_offered) {
                    append_message(&mut messages, "system", block);
                    let message = messages.len() - 1;
                    change_blocks.push((message, messages[message].1.len() - 1));
                }
                change.apply_to(&mut tools_offered);
            }
        }
    }

    super::tool_history::validate_anthropic(&messages)?;

    if messages.is_empty() {
        return Err(ModelInvokeError::new(
            ModelInvokeErrorKind::InvalidRequest,
            "frame rendered to zero messages; nothing to send",
        ));
    }

    // Stable-prefix anchors: within the system parameter the marker goes on
    // the last block; within the conversation on the last content block that is
    // not a sealed item, searching backward from the anchor message because a
    // sealed item is written verbatim and never modified.
    match cache {
        CacheDirective::None => {}
        CacheDirective::StablePrefix => {
            if let Some(last) = system_blocks.last_mut() {
                last["cache_control"] = json!({"type": "ephemeral"});
            }
            if let Some(last_block) = anchor_block(&mut messages, &sealed_blocks, &change_blocks) {
                last_block["cache_control"] = json!({"type": "ephemeral"});
            }
        }
    }

    // `validate_anthropic` rejected a missing limit, so this is always `Some`;
    // the error is repeated here rather than panicking if that invariant ever
    // changes.
    let max_tokens = generation.max_tokens.ok_or_else(|| {
        ModelInvokeError::new(
            ModelInvokeErrorKind::InvalidRequest,
            "max_tokens is required by the Anthropic Messages API; the adapter sends no default",
        )
    })?;
    let mut body = json!({
        "model": model.0,
        "max_tokens": max_tokens,
        "messages": messages
            .into_iter()
            .map(|(role, content)| json!({"role": role, "content": content}))
            .collect::<Vec<_>>(),
    });
    if let Some(temperature) = generation.temperature {
        body["temperature"] = json!(temperature);
    }
    if !system_blocks.is_empty() {
        // Always the block-array form: one block per leading system part, no
        // join, so the value does not change shape with the cache directive.
        body["system"] = json!(system_blocks);
    }
    if !tool_plan.tools.is_empty() {
        let mut tools =
            context_frame::tool_definitions(&tool_plan.tools, context_frame::ToolShape::Anthropic);
        match cache {
            CacheDirective::None => {}
            CacheDirective::StablePrefix => {
                if let Some(last_tool) = tools.last_mut() {
                    last_tool["cache_control"] = json!({"type": "ephemeral"});
                }
            }
        }
        if tool_plan.in_place {
            tools.push(deferred_placeholder());
        }
        body["tools"] = json!(tools);
    }
    // The effort is written as given. No `thinking` object is derived from
    // it: a host that wants one sends it among the extra request fields.
    if let Some(effort) = &generation.reasoning_effort {
        body["output_config"] = json!({"effort": effort});
    }
    super::generation::merge_extra_body(&mut body, generation);
    Ok(body)
}

/// The content block the stable-prefix cache anchor lands on: the last block
/// of the anchor message that did not come from a sealed item, searching
/// backward through earlier messages when the anchor holds only sealed items.
/// A sealed item is written verbatim and must never be modified, so it cannot
/// carry the marker; `sealed` holds the `(message, block)` positions of those
/// blocks. The anchor message is the previous one when the final message
/// carries this round's dispatched `tool_result` blocks, the final message
/// otherwise: anchoring before the fresh dispatch keeps the cached prefix
/// clear of content the current round just appended.
fn anchor_block<'a>(
    messages: &'a mut [(&'static str, Vec<Value>)],
    sealed: &[(usize, usize)],
    changes: &[(usize, usize)],
) -> Option<&'a mut Value> {
    // Trailing messages that hold only tool changes are this round's
    // additions; the stable prefix ends before them.
    let end = messages
        .iter()
        .enumerate()
        .rposition(|(message_index, message)| {
            (0..message.1.len()).any(|block| !changes.contains(&(message_index, block)))
        })?;
    let last = &messages[end];
    let dispatching = last
        .1
        .iter()
        .any(|b| b.get("type").and_then(Value::as_str) == Some("tool_result"));
    let start = if dispatching && end > 0 { end - 1 } else { end };
    for (message_index, message) in messages[..=start].iter_mut().enumerate().rev() {
        let found = message
            .1
            .iter_mut()
            .enumerate()
            .rev()
            .find(|(block_index, _)| {
                !sealed.contains(&(message_index, *block_index))
                    && !changes.contains(&(message_index, *block_index))
            })
            .map(|(_, block)| block);
        if found.is_some() {
            return found;
        }
    }
    None
}

/// The content blocks of one in-place tool change, applied to `offered`:
/// a `tool_removal` per withdrawn tool that is offered and not defined again
/// in the same change (a new definition replaces the old one), then a
/// `tool_addition` with the inline definition per added tool, as `pi-ai`
/// renders them.
fn tool_change_blocks(change: &causa_kernel::ToolChange, offered: &[ToolDefinition]) -> Vec<Value> {
    let mut blocks: Vec<Value> = change
        .removed
        .iter()
        .filter(|name| offered.iter().any(|definition| &definition.name == *name))
        .filter(|name| {
            !change
                .added
                .iter()
                .any(|definition| &definition.name == *name)
        })
        .map(|name| {
            json!({
                "type": "tool_removal",
                "tool": {"type": "tool_reference", "name": name},
            })
        })
        .collect();
    blocks.extend(
        context_frame::tool_definitions(&change.added, context_frame::ToolShape::Anthropic)
            .into_iter()
            .map(|definition| {
                json!({
                    "type": "tool_addition",
                    "tool": {"type": "tool_definition", "definition": definition},
                })
            }),
    );
    blocks
}

fn role_name(role: Role) -> &'static str {
    match role {
        Role::System => "system",
        Role::User => "user",
        Role::Assistant => "assistant",
    }
}

/// The `image` content block for a resolved image payload (inline or remote URL).
fn image_block(media_type: &str, source: &MediaSource) -> Value {
    let source_json = match source {
        MediaSource::Inline { data_base64 } => {
            json!({"type": "base64", "media_type": media_type, "data": data_base64})
        }
        MediaSource::Url(url) => {
            json!({"type": "url", "url": url})
        }
    };
    json!({
        "type": "image",
        "source": source_json,
    })
}

/// The `document` content block for a resolved document payload (inline or remote URL).
fn document_block(media_type: &str, source: &MediaSource, name: Option<&str>) -> Value {
    let source_json = match source {
        MediaSource::Inline { data_base64 } => {
            json!({"type": "base64", "media_type": media_type, "data": data_base64})
        }
        MediaSource::Url(url) => {
            json!({"type": "url", "url": url})
        }
    };
    match name {
        Some(title) => json!({
            "type": "document",
            "source": source_json,
            "title": title,
        }),
        None => json!({
            "type": "document",
            "source": source_json,
        }),
    }
}

/// A resolved-or-placeholder attachment as a content block: images and
/// documents embed inline or as remote URL sources, everything else degrades to
/// its placeholder text.
fn media_block_json(media: &ResolvedMedia) -> Value {
    match media {
        ResolvedMedia::Image { media_type, source } => image_block(media_type, source),
        ResolvedMedia::Document {
            media_type,
            source,
            name,
        } => document_block(media_type, source, name.as_deref()),
        ResolvedMedia::Placeholder(text) => json!({"type": "text", "text": text}),
    }
}

/// Parse an Anthropic Messages response body into a kernel
/// [`ModelOutput`].
///
/// Each `text` block becomes one text item and each `tool_use` block one
/// tool-call item (`provider_call_id` carries the provider id), in the
/// order returned. An item's `wire` holds the block minus the fields its
/// content holds (`text`; `id`, `name` and `input`), so the renderer can
/// send the block back as returned. Every `thinking` block,
/// `redacted_thinking` block and unknown block type becomes one sealed item
/// **in place**, holding the block exactly as returned, so the turn can be
/// echoed back; the readable `thinking` text additionally fills the reasoning
/// observation, which is display-only. `stop_reason` is required; a value the kernel has no meaning for maps to
/// [`causa_kernel::ModelStopReason::Unrecognized`] rather than being guessed, and the raw
/// wire value is preserved on [`ModelOutput::raw_stop_reason`]. One mapping
/// table is shared with the streaming decoder.
pub fn parse_anthropic_response(value: &Value) -> Result<ModelOutput, ModelInvokeError> {
    fn undecodable(message: impl Into<String>) -> ModelInvokeError {
        ModelInvokeError::new(ModelInvokeErrorKind::Undecodable, message)
    }

    let empty = Vec::new();
    let content = match value.get("content") {
        None | Some(Value::Null) => &empty,
        Some(Value::Array(items)) => items,
        Some(_) => return Err(undecodable("content: expected an array")),
    };

    let mut items: Vec<ModelItem> = Vec::new();
    let mut thinking_parts: Vec<String> = Vec::new();
    for block in content {
        match block.get("type").and_then(Value::as_str) {
            Some("text") => {
                let text = block
                    .get("text")
                    .and_then(Value::as_str)
                    .ok_or_else(|| undecodable("text block: missing `text` string"))?;
                items.push({
                    let mut item = ModelItem::text(text);
                    item.wire = Some(context_frame::wire_meta(
                        Protocol::AnthropicMessages,
                        context_frame::without_keys(block, &["text"]),
                    ));
                    item
                });
            }
            Some("tool_use") => {
                let id = block
                    .get("id")
                    .and_then(Value::as_str)
                    .ok_or_else(|| undecodable("tool_use block: missing `id` string"))?;
                let name = block
                    .get("name")
                    .and_then(Value::as_str)
                    .ok_or_else(|| undecodable("tool_use block: missing `name` string"))?;
                items.push({
                    let mut item = ModelItem::tool_call(
                        ToolCallDraft::new(
                            name.to_string(),
                            block.get("input").cloned().unwrap_or_else(|| json!({})),
                        )
                        .with_provider_call_id(id.to_string()),
                    );
                    item.wire = Some(context_frame::wire_meta(
                        Protocol::AnthropicMessages,
                        context_frame::without_keys(block, &["id", "name", "input"]),
                    ));
                    item
                });
            }
            Some("thinking") => {
                let text = block
                    .get("thinking")
                    .and_then(Value::as_str)
                    .ok_or_else(|| undecodable("thinking block: missing `thinking` string"))?;
                thinking_parts.push(text.to_string());
                // The block, signature included, is sealed material; the text
                // above is the display-only observation.
                items.push(context_frame::sealed_item(
                    Protocol::AnthropicMessages,
                    block.clone(),
                ));
            }
            // `redacted_thinking` and every block type this adapter has no
            // vocabulary for are kept as sealed items in place, so the turn
            // can be echoed back as returned.
            _ => items.push(context_frame::sealed_item(
                Protocol::AnthropicMessages,
                block.clone(),
            )),
        }
    }

    let raw_stop_reason = match crate::translation::stop_reason::anthropic_raw(value) {
        Some(raw) => raw.to_owned(),
        None if value.get("stop_reason").is_some_and(|v| !v.is_null()) => {
            return Err(undecodable("stop_reason: expected a string"));
        }
        None => return Err(undecodable("missing stop_reason")),
    };
    let stop_reason = crate::translation::stop_reason::anthropic(&raw_stop_reason);

    let usage = value
        .get("usage")
        .filter(|v| v.is_object())
        .map(crate::translation::usage::model_usage_from_anthropic);

    Ok({
        let mut output = ModelOutput::new(ModelResponse { items }, stop_reason);
        output.usage = usage;
        output.raw_stop_reason = Some(raw_stop_reason);
        output.reasoning = if thinking_parts.is_empty() {
            None
        } else {
            Some(ReasoningPayload::new(thinking_parts.join("\n")))
        };
        output
    })
}

fn append_message(
    messages: &mut Vec<(&'static str, Vec<Value>)>,
    role: &'static str,
    block: Value,
) {
    match messages.last_mut() {
        Some((last_role, content)) if *last_role == role => content.push(block),
        _ => messages.push((role, vec![block])),
    }
}

/// Append one verbatim sealed block to the open assistant message, or open a
/// new assistant message for it. Returns its `(message, block)` position so
/// the cache anchor can skip it.
fn append_sealed(messages: &mut Vec<(&'static str, Vec<Value>)>, block: Value) -> (usize, usize) {
    if matches!(messages.last(), Some(("assistant", _))) {
        let content = &mut messages
            .last_mut()
            .expect("assistant message checked above")
            .1;
        let index = content.len();
        content.push(block);
        (messages.len() - 1, index)
    } else {
        messages.push(("assistant", vec![block]));
        (messages.len() - 1, 0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use causa_kernel::{ContentPart, MediaRef, ModelStopReason, ModelUsage, ToolDefinition};

    use crate::translation::media::{MediaPayload, MediaSet};
    use crate::translation::test_support::{
        call, frame, media_part, parts, result, result_with_media, text, text_part,
        text_with_source, with_tools,
    };

    /// Anthropic requires `max_tokens`; the renderer tests always supply one.
    fn test_generation() -> GenerationOptions {
        GenerationOptions::default().with_max_tokens(4096)
    }

    fn render(frame: &ContextFrame) -> Value {
        render_anthropic_messages(
            frame,
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap()
    }

    // --- rendering ---------------------------------------------------------

    #[test]
    fn role_mapping_and_source_independence() {
        let f = frame(vec![
            text(0, "be terse", Role::System),
            text(1, "model said", Role::Assistant),
            text(2, "user said", Role::User),
            text(3, "replayed", Role::Assistant),
            // source set to "system" but role is User: role wins, source ignored
            text_with_source(4, "user override", Role::User, Some("system")),
            // source absent and role is User: renders as user
            text(5, "user no source", Role::User),
        ]);
        let v = render(&f);
        // The top-level system parameter is always the block-array form.
        assert_eq!(v["system"], json!([{"type": "text", "text": "be terse"}]));
        let msgs = v["messages"].as_array().unwrap();
        assert_eq!(msgs.len(), 4);
        assert_eq!(msgs[0]["role"], "assistant");
        assert_eq!(
            msgs[0]["content"],
            json!([{"type": "text", "text": "model said"}])
        );
        assert_eq!(msgs[1]["role"], "user");
        assert_eq!(msgs[2]["role"], "assistant");
        assert_eq!(msgs[3]["role"], "user");
        // adjacent same-role texts stay separate content blocks: no text is
        // joined across block seams
        assert_eq!(
            msgs[3]["content"],
            json!([
                {"type": "text", "text": "user override"},
                {"type": "text", "text": "user no source"},
            ])
        );
    }

    #[test]
    fn empty_text_blocks_are_skipped() {
        // The host door can commit empty texts; the renderer mirrors the
        // model door and skips them.
        let f = frame(vec![
            text(0, "", Role::User),
            text(1, "real", Role::User),
            text(2, "", Role::System),
            text(3, "still here", Role::Assistant),
        ]);
        let v = render(&f);
        // the skipped leading empty text must not leave an empty message
        let msgs = v["messages"].as_array().unwrap();
        assert_eq!(msgs.len(), 2);
        assert_eq!(
            msgs[0]["content"],
            json!([{"type": "text", "text": "real"}])
        );
        assert!(v.get("system").is_none());
    }

    #[test]
    fn tool_round_trip_pairing_and_consecutive_merge() {
        let fallback_id = call(2, "kc2", None, "list", json!({})).id().0.to_string();
        let f = frame(vec![
            text(0, "reading now", Role::Assistant),
            call(1, "kc1", Some("toolu_a"), "read", json!({"path": "a"})),
            call(2, "kc2", None, "list", json!({})),
            result(3, "kc1", ToolResultStatus::Succeeded, json!("file-a")),
            result(4, "kc2", ToolResultStatus::Failed, json!({"error": "boom"})),
        ]);
        let v = render(&f);
        let msgs = v["messages"].as_array().unwrap();
        assert_eq!(msgs.len(), 2);
        // text + both calls merge into one assistant message; the wire id
        // is provider_call_id when present, declaration BlockId UUID otherwise
        assert_eq!(msgs[0]["role"], "assistant");
        assert_eq!(
            msgs[0]["content"][0],
            json!({"type": "text", "text": "reading now"})
        );
        assert_eq!(
            msgs[0]["content"][1],
            json!({"type": "tool_use", "id": "toolu_a", "name": "read", "input": {"path": "a"}})
        );
        assert_eq!(
            msgs[0]["content"][2],
            json!({"type": "tool_use", "id": fallback_id, "name": "list", "input": {}})
        );
        // both results merge into one user message, ids via the pairing map
        assert_eq!(msgs[1]["role"], "user");
        assert_eq!(
            msgs[1]["content"][0],
            json!({"type": "tool_result", "tool_use_id": "toolu_a", "content": "file-a"})
        );
        // non-string observations serialize to a string; Failed sets is_error
        assert_eq!(
            msgs[1]["content"][1],
            json!({
                "type": "tool_result",
                "tool_use_id": fallback_id,
                "content": "{\"error\":\"boom\"}",
                "is_error": true,
            })
        );
    }

    #[test]
    fn unpaired_tool_result_is_invalid_before_http() {
        let f = frame(vec![result(
            0,
            "orphan",
            ToolResultStatus::Succeeded,
            json!("x"),
        )]);
        let error = render_anthropic_messages(
            &f,
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap_err();
        assert_eq!(error.kind, ModelInvokeErrorKind::InvalidRequest);
    }

    #[test]
    fn tools_generation_and_byte_determinism() {
        let surface = vec![ToolDefinition::new(
            "read",
            "read a file",
            json!({"type": "object", "properties": {"path": {"type": "string"}}}),
        )];
        let f = frame(vec![text(0, "hi", Role::User)]);

        let generation = GenerationOptions::default()
            .with_temperature(0.5)
            .with_max_tokens(4096);
        let v = render_anthropic_messages(
            &with_tools(&f, &surface),
            &MediaSet::new(),
            &generation,
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(v["model"], json!("claude-test"));
        assert_eq!(v["temperature"], json!(0.5));
        assert_eq!(v["max_tokens"], json!(4096));
        assert_eq!(
            v["tools"],
            json!([{
                "name": "read",
                "description": "read a file",
                "input_schema": {"type": "object", "properties": {"path": {"type": "string"}}},
            }])
        );

        let generation = GenerationOptions::default()
            .with_temperature(0.5)
            .with_max_tokens(100);
        let v2 = render_anthropic_messages(
            &with_tools(&f, &surface),
            &MediaSet::new(),
            &generation,
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(v2["max_tokens"], json!(100));
        // byte determinism over the full body
        let again = render_anthropic_messages(
            &with_tools(&f, &surface),
            &MediaSet::new(),
            &generation,
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            serde_json::to_string(&v2).unwrap(),
            serde_json::to_string(&again).unwrap()
        );
    }

    // --- effort and extra request fields ----------------------------------

    fn render_with(
        generation: &GenerationOptions,
        options: &AnthropicRenderOptions,
    ) -> Result<Value, ModelInvokeError> {
        let f = frame(vec![text(0, "hi", Role::User)]);
        render_anthropic_messages(
            &f,
            &MediaSet::new(),
            generation,
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            options,
        )
    }

    #[test]
    fn effort_is_written_as_given_and_derives_no_thinking_object() {
        // unset: neither field
        let body = render_with(&test_generation(), &AnthropicRenderOptions::default()).unwrap();
        assert!(body.get("thinking").is_none());
        assert!(body.get("output_config").is_none());

        // Any string, including ones this adapter once refused or mapped.
        for effort in ["none", "minimal", "high", "a-level-of-tomorrow", ""] {
            let mut generation = test_generation();
            generation.reasoning_effort = Some(effort.into());
            let body = render_with(&generation, &AnthropicRenderOptions::default()).unwrap();
            assert_eq!(body["output_config"], json!({"effort": effort}), "{effort}");
            assert!(body.get("thinking").is_none(), "{effort}");
        }
    }

    #[test]
    fn extra_request_fields_are_copied_and_never_replace_rendered_keys() {
        let thinking = json!({
            "type": "adaptive",
            "block_binding": {"prefix_mismatch_behavior": "drop_block"},
            "unknown_to_this_adapter": [1, {"a": null}],
        });
        let mut generation = test_generation();
        generation.reasoning_effort = Some("high".into());
        generation.temperature = Some(0.5);
        generation
            .extra_body
            .insert("thinking".into(), thinking.clone());
        generation.extra_body.insert("model".into(), json!("other"));
        generation.extra_body.insert("messages".into(), json!([]));
        generation
            .extra_body
            .insert("output_config".into(), json!({"effort": "low"}));
        let body = render_with(&generation, &AnthropicRenderOptions::default()).unwrap();
        assert_eq!(body["thinking"], thinking);
        // An effort beside a temperature is the provider's to judge.
        assert_eq!(body["temperature"], json!(0.5));
        assert_eq!(body["model"], "claude-test");
        assert_eq!(body["messages"].as_array().unwrap().len(), 1);
        assert_eq!(body["output_config"], json!({"effort": "high"}));
    }

    #[test]
    fn missing_max_tokens_still_fails_before_rendering() {
        let error = render_with(
            &GenerationOptions::default(),
            &AnthropicRenderOptions::default(),
        )
        .unwrap_err();
        assert_eq!(error.kind, ModelInvokeErrorKind::InvalidRequest);
        assert!(error.message.contains("max_tokens"), "{}", error.message);
    }

    #[test]
    fn empty_frame_is_invalid_request() {
        let f = frame(vec![]);
        let e = render_anthropic_messages(
            &f,
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("m"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap_err();
        assert!(matches!(e.kind, ModelInvokeErrorKind::InvalidRequest));
    }

    #[test]
    fn cache_none_omits_every_cache_control_key() {
        let f = frame(vec![
            text(0, "be terse", Role::System),
            text(1, "hi", Role::User),
        ]);
        let surface = vec![ToolDefinition::new(
            "read",
            "read a file",
            json!({"type": "object"}),
        )];
        let v = render_anthropic_messages(
            &with_tools(&f, &surface),
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert!(!serde_json::to_string(&v).unwrap().contains("cache_control"));
    }

    #[test]
    fn stable_prefix_marks_tools_system_and_previous_message() {
        let f = frame(vec![
            text(0, "be terse", Role::System),
            text(1, "hi", Role::User),
            call(2, "kc1", Some("toolu_a"), "read", json!({"path": "a"})),
            result(3, "kc1", ToolResultStatus::Succeeded, json!("file-a")),
        ]);
        let surface = vec![ToolDefinition::new(
            "read",
            "read a file",
            json!({"type": "object"}),
        )];
        let v = render_anthropic_messages(
            &with_tools(&f, &surface),
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::StablePrefix,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        // system renders as a block array carrying the anchor
        assert_eq!(
            v["system"],
            json!([{
                "type": "text",
                "text": "be terse",
                "cache_control": {"type": "ephemeral"},
            }])
        );
        // the final message carries this round's dispatched tool results,
        // so the anchor lands on the last block of the assistant message
        let msgs = v["messages"].as_array().unwrap();
        assert_eq!(msgs.len(), 3);
        assert!(msgs[1]["content"][0].get("cache_control").is_some());
        // the last tool definition carries the anchor
        assert_eq!(v["tools"][0]["cache_control"], json!({"type": "ephemeral"}));
    }

    #[test]
    fn stable_prefix_without_dispatch_anchors_the_final_message() {
        let f = frame(vec![text(0, "hi", Role::User)]);
        let v = render_anthropic_messages(
            &f,
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::StablePrefix,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        let msgs = v["messages"].as_array().unwrap();
        assert!(msgs[0]["content"][0].get("cache_control").is_some());
    }

    // --- media -------------------------------------------------------------

    fn resolved(reference: &str) -> MediaSet {
        let mut m = MediaSet::new();
        m.insert(reference, MediaPayload::inline("image/png", "AAAA"));
        m
    }

    #[test]
    fn image_parts_render_as_image_blocks_in_user_position() {
        let f = frame(vec![parts(
            0,
            vec![
                text_part("caption"),
                media_part("image/png", "asset-1"),
                text_part("after"),
            ],
            Role::User,
        )]);
        let v = render_anthropic_messages(
            &f,
            &resolved("asset-1"),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        // part boundaries are preserved: text, image, text as separate blocks
        assert_eq!(
            v["messages"][0]["content"],
            json!([
                {"type": "text", "text": "caption"},
                {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}},
                {"type": "text", "text": "after"},
            ])
        );
    }

    #[test]
    fn unresolved_media_degrades_to_the_deterministic_placeholder() {
        let f = frame(vec![parts(
            0,
            vec![media_part("image/png", "missing-asset")],
            Role::User,
        )]);
        let v = render_anthropic_messages(
            &f,
            &MediaSet::new(),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][0]["content"],
            json!([{"type": "text", "text": "[media: image/png missing-asset]"}])
        );
    }

    #[test]
    fn url_image_parts_render_as_url_image_blocks_in_user_position() {
        let f = frame(vec![parts(
            0,
            vec![
                text_part("caption"),
                media_part("image/png", "asset-1"),
                text_part("after"),
            ],
            Role::User,
        )]);
        let mut media = MediaSet::new();
        media.insert(
            "asset-1",
            MediaPayload::url("image/png", "https://example.com/pic.png"),
        );
        let v = render_anthropic_messages(
            &f,
            &media,
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][0]["content"],
            json!([
                {"type": "text", "text": "caption"},
                {"type": "image", "source": {"type": "url", "url": "https://example.com/pic.png"}},
                {"type": "text", "text": "after"},
            ])
        );
    }

    #[test]
    fn non_image_media_degrades_even_when_resolved() {
        let f = frame(vec![parts(
            0,
            vec![media_part("application/zip", "doc-1")],
            Role::User,
        )]);
        let mut media = MediaSet::new();
        media.insert("doc-1", MediaPayload::inline("application/zip", "AAAA"));
        let v = render_anthropic_messages(
            &f,
            &media,
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][0]["content"],
            json!([{"type": "text", "text": "[media: application/zip doc-1]"}])
        );
    }

    #[test]
    fn document_parts_render_as_document_blocks_in_user_position() {
        let f = frame(vec![parts(
            0,
            vec![
                text_part("caption"),
                ContentPart::Media(
                    MediaRef::new("application/pdf", "doc-inline").with_name("spec.pdf"),
                ),
                ContentPart::Media(MediaRef::new("application/pdf", "doc-url")),
            ],
            Role::User,
        )]);
        let mut media = MediaSet::new();
        media.insert(
            "doc-inline",
            MediaPayload::inline("application/pdf", "AAAA"),
        );
        media.insert(
            "doc-url",
            MediaPayload::url("application/pdf", "https://example.com/guide.pdf"),
        );
        let v = render_anthropic_messages(
            &f,
            &media,
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][0]["content"],
            json!([
                {"type": "text", "text": "caption"},
                {
                    "type": "document",
                    "source": {"type": "base64", "media_type": "application/pdf", "data": "AAAA"},
                    "title": "spec.pdf"
                },
                {
                    "type": "document",
                    "source": {"type": "url", "url": "https://example.com/guide.pdf"}
                },
            ])
        );
    }

    #[test]
    fn media_in_system_position_degrades_to_placeholder_text() {
        let f = frame(vec![
            parts(0, vec![media_part("image/png", "asset-1")], Role::System),
            text(1, "hi", Role::User),
        ]);
        let v = render_anthropic_messages(
            &f,
            &resolved("asset-1"),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        // the system parameter is text-only; the walk degraded it, and the
        // block-array form carries the placeholder
        assert_eq!(
            v["system"],
            json!([{"type": "text", "text": "[media: image/png asset-1]"}])
        );
    }

    #[test]
    fn tool_result_media_embeds_in_the_tool_result_content() {
        let f = frame(vec![
            call(0, "kc1", Some("toolu_a"), "render", json!({})),
            result_with_media(
                1,
                "kc1",
                ToolResultStatus::Succeeded,
                json!("chart ready"),
                vec![
                    MediaRef::new("image/png", "asset-1"),
                    MediaRef::new("image/png", "asset-2"),
                ],
            ),
        ]);
        let mut media = MediaSet::new();
        media.insert("asset-1", MediaPayload::inline("image/png", "AAAA"));
        // asset-2 deliberately unregistered -> placeholder inside the array
        let v = render_anthropic_messages(
            &f,
            &media,
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][1]["content"][0]["content"],
            json!([
                {"type": "text", "text": "chart ready"},
                {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}},
                {"type": "text", "text": "[media: image/png asset-2]"},
            ])
        );
    }

    #[test]
    fn tool_result_document_media_embeds_in_the_tool_result_content() {
        let f = frame(vec![
            call(0, "kc1", Some("toolu_a"), "generate_report", json!({})),
            result_with_media(
                1,
                "kc1",
                ToolResultStatus::Succeeded,
                json!("report ready"),
                vec![
                    MediaRef::new("application/pdf", "res-inline"),
                    MediaRef::new("application/pdf", "res-url").with_name("report.pdf"),
                ],
            ),
        ]);
        let mut media = MediaSet::new();
        media.insert(
            "res-inline",
            MediaPayload::inline("application/pdf", "BBBB"),
        );
        media.insert(
            "res-url",
            MediaPayload::url("application/pdf", "https://example.com/report.pdf"),
        );
        let v = render_anthropic_messages(
            &f,
            &media,
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][1]["content"][0]["content"],
            json!([
                {"type": "text", "text": "report ready"},
                {
                    "type": "document",
                    "source": {"type": "base64", "media_type": "application/pdf", "data": "BBBB"}
                },
                {
                    "type": "document",
                    "source": {"type": "url", "url": "https://example.com/report.pdf"},
                    "title": "report.pdf"
                },
            ])
        );
    }

    #[test]
    fn tool_result_url_media_renders_as_url_image_blocks() {
        let f = frame(vec![
            call(0, "kc1", Some("toolu_a"), "render", json!({})),
            result_with_media(
                1,
                "kc1",
                ToolResultStatus::Succeeded,
                json!("chart ready"),
                vec![MediaRef::new("image/png", "asset-1")],
            ),
        ]);
        let mut media = MediaSet::new();
        media.insert(
            "asset-1",
            MediaPayload::url("image/png", "https://example.com/chart.png"),
        );
        let v = render_anthropic_messages(
            &f,
            &media,
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(
            v["messages"][1]["content"][0]["content"],
            json!([
                {"type": "text", "text": "chart ready"},
                {"type": "image", "source": {"type": "url", "url": "https://example.com/chart.png"}},
            ])
        );
    }

    #[test]
    fn result_without_media_keeps_the_string_content_shape() {
        let f = frame(vec![
            call(0, "kc1", None, "read", json!({})),
            result(1, "kc1", ToolResultStatus::Succeeded, json!("ok")),
        ]);
        let v = render_anthropic_messages(
            &f,
            &resolved("unused"),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::None,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        assert_eq!(v["messages"][1]["content"][0]["content"], json!("ok"));
    }

    #[test]
    fn stable_prefix_anchor_lands_on_parts_messages() {
        let f = frame(vec![
            text(0, "hi", Role::User),
            parts(
                1,
                vec![text_part("see"), media_part("image/png", "asset-1")],
                Role::User,
            ),
        ]);
        let v = render_anthropic_messages(
            &f,
            &resolved("asset-1"),
            &test_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::StablePrefix,
            &AnthropicRenderOptions::default(),
        )
        .unwrap();
        // same-role blocks share one message but their texts stay separate
        // content blocks: [text "hi", text "see", image]
        let msgs = v["messages"].as_array().unwrap();
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0]["content"][0], json!({"type": "text", "text": "hi"}));
        assert_eq!(
            msgs[0]["content"][1],
            json!({"type": "text", "text": "see"})
        );
        // the final message carries no tool_result, so it anchors itself
        assert!(
            msgs[0]["content"]
                .as_array()
                .unwrap()
                .last()
                .unwrap()
                .get("cache_control")
                .is_some()
        );
    }

    // --- parsing -----------------------------------------------------------

    #[test]
    fn parse_three_content_shapes_with_provider_id_passthrough() {
        let v = json!({
            "content": [
                {"type": "thinking", "thinking": "hmm", "signature": "sig1"},
                {"type": "text", "text": "Let me check."},
                {"type": "tool_use", "id": "toolu_9", "name": "read", "input": {"path": "a"}},
            ],
            "stop_reason": "tool_use",
            "usage": {"input_tokens": 10, "output_tokens": 5},
        });
        let out = parse_anthropic_response(&v).unwrap();
        assert_eq!(out.response.text(), "Let me check.");
        assert_eq!(out.response.tool_calls().count(), 1);
        let tc = &out.response.tool_calls().next().unwrap();
        assert_eq!(tc.tool_name, "read");
        assert_eq!(tc.arguments, json!({"path": "a"}));
        assert_eq!(tc.provider_call_id.as_deref(), Some("toolu_9"));
        assert!(matches!(out.stop_reason, ModelStopReason::ToolUse));
        // The whole block, signature included, is sealed material in place:
        // item order is the wire order, and the text/tool items follow it.
        assert_eq!(out.response.items.len(), 3);
        assert_eq!(
            sealed_items(&out),
            vec![json!({
                "type": "thinking", "thinking": "hmm", "signature": "sig1"
            })]
        );
        let reasoning = out.reasoning.unwrap();
        assert_eq!(reasoning.text, "hmm");
    }

    /// The stored value of every sealed part of a response, in item order.
    fn sealed_items(out: &ModelOutput) -> Vec<Value> {
        out.response
            .items
            .iter()
            .flat_map(|item| match &item.content {
                causa_kernel::ModelItemContent::Parts(parts) => parts
                    .iter()
                    .filter_map(|part| match part {
                        causa_kernel::ContentPart::Sealed(sealed) => Some(sealed.item.clone()),
                        _ => None,
                    })
                    .collect::<Vec<_>>(),
                _ => Vec::new(),
            })
            .collect()
    }

    #[test]
    fn usage_mapping_includes_cache_fields() {
        let v = json!({
            "content": [{"type": "text", "text": "ok"}],
            "stop_reason": "end_turn",
            "usage": {
                "input_tokens": 100,
                "output_tokens": 20,
                "cache_read_input_tokens": 11,
                "cache_creation_input_tokens": 3,
            },
        });
        let out = parse_anthropic_response(&v).unwrap();
        let u: ModelUsage = out.usage.unwrap();
        assert_eq!(u.input_tokens, 100);
        assert_eq!(u.output_tokens, 20);
        assert_eq!(u.cache_read_tokens, Some(11));
        assert_eq!(u.cache_write_tokens, Some(3));
        // Anthropic does not expose reasoning tokens
        assert_eq!(u.reasoning_tokens, None);
    }

    #[test]
    fn absent_usage_is_none() {
        let v = json!({"content": [{"type": "text", "text": "ok"}], "stop_reason": "end_turn"});
        let out = parse_anthropic_response(&v).unwrap();
        assert!(out.usage.is_none());
    }

    #[test]
    fn stop_reason_mapping_full_and_unknown_is_unrecognized() {
        for (wire, expected) in [
            ("end_turn", ModelStopReason::EndTurn),
            ("stop_sequence", ModelStopReason::EndTurn),
            ("tool_use", ModelStopReason::ToolUse),
            ("max_tokens", ModelStopReason::MaxTokens),
            ("refusal", ModelStopReason::Refusal),
            (
                "model_context_window_exceeded",
                ModelStopReason::ContextWindowExceeded,
            ),
            ("pause_turn", ModelStopReason::Unrecognized),
            // An unknown value is reported as what it is, never guessed.
            ("model_context_overflow", ModelStopReason::Unrecognized),
        ] {
            let v = json!({"content": [], "stop_reason": wire});
            let out = parse_anthropic_response(&v).unwrap();
            assert_eq!(
                std::mem::discriminant(&out.stop_reason),
                std::mem::discriminant(&expected),
                "wire stop_reason = {wire}"
            );
            // The wire value reaches the host unchanged.
            assert_eq!(out.raw_stop_reason.as_deref(), Some(wire));
        }
    }

    #[test]
    fn missing_stop_reason_is_undecodable_error() {
        let v = json!({"content": []});
        let e = parse_anthropic_response(&v).unwrap_err();
        assert!(matches!(e.kind, ModelInvokeErrorKind::Undecodable));
        assert!(e.message.contains("stop_reason"));
        // Present but not a string is reported as such, not as missing.
        let e = parse_anthropic_response(&json!({"content": [], "stop_reason": 7})).unwrap_err();
        assert!(matches!(e.kind, ModelInvokeErrorKind::Undecodable));
        assert!(e.message.contains("expected a string"));
    }

    #[test]
    fn exotic_blocks_are_sealed_in_place_not_skipped() {
        let v = json!({
            "content": [
                {"type": "redacted_thinking", "data": "encrypted"},
                {"type": "text", "text": "a"},
                {"type": "server_tool_use", "id": "x", "name": "web_search"},
                {"type": "text", "text": "b"},
            ],
            "stop_reason": "end_turn",
        });
        let out = parse_anthropic_response(&v).unwrap();
        // one item per content block, in wire order, nothing skipped: the two
        // text blocks stay separate and the two unknown blocks are sealed
        assert_eq!(out.response.items.len(), 4);
        assert_eq!(out.response.text(), "ab");
        assert!(out.response.tool_calls().next().is_none());
        assert!(out.reasoning.is_none());
        assert_eq!(
            sealed_items(&out),
            vec![
                json!({"type": "redacted_thinking", "data": "encrypted"}),
                json!({"type": "server_tool_use", "id": "x", "name": "web_search"}),
            ]
        );
    }

    #[test]
    fn malformed_tool_use_is_undecodable_error() {
        let v = json!({"content": [{"type": "tool_use", "id": "x"}], "stop_reason": "tool_use"});
        let e = parse_anthropic_response(&v).unwrap_err();
        assert!(matches!(e.kind, ModelInvokeErrorKind::Undecodable));
        assert!(e.message.contains("name"));
    }
}