mecha-core 0.1.16

Provider-agnostic agent harness: loop, tools, MCP client, sessions.
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
//! Anthropic Messages API (`POST /v1/messages`).
//!
//! Raw HTTP: there is no official Anthropic SDK for Rust. Shapes follow the
//! documented wire format for the Claude 5 family.
//!
//! Notes that bite if forgotten:
//!   * `temperature` / `top_p` / `top_k` are rejected with a 400 on Opus 5 and
//!     Fable 5. We never send them.
//!   * `budget_tokens` is gone; thinking is `{"type": "adaptive"}`.
//!   * On Opus 5 thinking is ON by default, and `{"type": "disabled"}` is only
//!     accepted at effort `high` or below.
//!   * `stop_reason: "refusal"` arrives as a 200. Check it before reading content.

use crate::config::ProviderConfig;
use crate::message::*;
use crate::provider::{Provider, StreamEvent, StreamSink};
use anyhow::{anyhow, bail, Context, Result};
use async_trait::async_trait;
use futures::StreamExt;
use serde_json::{json, Value};
use std::collections::BTreeMap;

const API_VERSION: &str = "2023-06-01";
pub const DEFAULT_MODEL: &str = "claude-opus-5";

pub struct Anthropic {
    http: reqwest::Client,
    api_key: String,
    base_url: String,
    default_model: String,
    vision: bool,
    retry: crate::provider::retry::RetryPolicy,
}

impl Anthropic {
    pub fn from_config(cfg: &ProviderConfig) -> Result<Self> {
        // Refused up front rather than silently dropped: someone who pinned the
        // sampler for repeatable evals must not believe it is pinned here.
        if cfg.temperature.is_some() || cfg.seed.is_some() {
            bail!(
                "the Anthropic API rejects `temperature` and has no `seed`; remove them \
                 from this provider's config. Sampling cannot be pinned on this provider"
            );
        }
        let api_key = cfg
            .resolve_api_key()
            .context("no Anthropic credentials found. Set ANTHROPIC_API_KEY, or put api_key_env / api_key in the provider config")?;
        Ok(Self {
            http: reqwest::Client::builder()
                // Long turns are normal at high effort; the per-request cap is
                // generous and streaming keeps the connection alive anyway.
                .timeout(std::time::Duration::from_secs(900))
                .build()?,
            api_key,
            base_url: cfg
                .base_url
                .clone()
                .unwrap_or_else(|| "https://api.anthropic.com".to_string()),
            default_model: cfg
                .model
                .clone()
                .unwrap_or_else(|| DEFAULT_MODEL.to_string()),
            vision: cfg.vision_enabled(),
            retry: crate::provider::retry::RetryPolicy::from_config(cfg),
        })
    }

    /// Send with the retry policy; on giving up, keep this provider's error
    /// shape with the classification underneath for policy layers to match.
    async fn send(&self, body: &Value) -> Result<reqwest::Response> {
        crate::provider::retry::send_with_retry(|| self.request(body), &self.retry)
            .await
            .map_err(|f| {
                let message = match f.status {
                    Some(status) => format!("anthropic {status}: {}", api_error(&f.detail)),
                    None => format!("anthropic: {}", f.detail),
                };
                anyhow::Error::new(f.class).context(message)
            })
    }

    fn body(&self, req: &CompletionRequest, stream: bool) -> Result<Value> {
        let mut messages: Vec<Value> = req
            .messages
            .iter()
            .map(|m| encode_message(m, self.vision))
            .collect();

        // A second, *moving* breakpoint on the last content block. The static
        // one below pins tools+system; without this one the entire message
        // history is re-sent uncached on every turn, and where it has been
        // measured cache operations are ~80% of billed agent cost. The
        // transcript is append-only between turns, so each request is a strict
        // prefix of the next: this turn's cache write is next turn's cache
        // read, which is exactly the trade the write premium is for. Never on
        // a thinking block — the API rejects the marker there.
        if req.cache_prompt {
            let last_block = messages.iter_mut().rev().find_map(|m| {
                m.get_mut("content")?
                    .as_array_mut()?
                    .iter_mut()
                    .rev()
                    .find(|b| b.get("type").and_then(Value::as_str) != Some("thinking"))
            });
            if let Some(block) = last_block {
                block
                    .as_object_mut()
                    .unwrap()
                    .insert("cache_control".into(), json!({"type": "ephemeral"}));
            }
        }

        let mut body = json!({
            "model": req.model,
            "max_tokens": req.max_tokens,
            "messages": messages,
        });
        let obj = body.as_object_mut().unwrap();

        if stream {
            obj.insert("stream".into(), json!(true));
        }

        // Render order is tools -> system -> messages, so a cache breakpoint on
        // the last system block covers the tool definitions too.
        if !req.tools.is_empty() {
            let last = req.tools.len() - 1;
            let tools: Vec<Value> = req
                .tools
                .iter()
                .enumerate()
                .map(|(i, t)| {
                    let mut v = json!({
                        "name": t.name,
                        "description": t.description,
                        "input_schema": t.input_schema,
                    });
                    // Only lands here when there's no system prompt to carry it.
                    if req.cache_prompt && req.system.is_none() && i == last {
                        v.as_object_mut()
                            .unwrap()
                            .insert("cache_control".into(), json!({"type": "ephemeral"}));
                    }
                    v
                })
                .collect();
            obj.insert("tools".into(), json!(tools));
        }

        if let Some(system) = &req.system {
            let mut block = json!({"type": "text", "text": system});
            if req.cache_prompt {
                block
                    .as_object_mut()
                    .unwrap()
                    .insert("cache_control".into(), json!({"type": "ephemeral"}));
            }
            obj.insert("system".into(), json!([block]));
        }

        if req.thinking {
            obj.insert(
                "thinking".into(),
                json!({"type": "adaptive", "display": "summarized"}),
            );
        } else {
            // Disabling thinking above `high` effort is a 400. Fail here with a
            // message that says what to do rather than surfacing the API error.
            if matches!(req.effort, Some(Effort::XHigh) | Some(Effort::Max)) {
                bail!(
                    "thinking cannot be disabled at effort {}: lower effort to `high` or leave thinking on",
                    req.effort.unwrap().as_str()
                );
            }
            obj.insert("thinking".into(), json!({"type": "disabled"}));
        }

        if let Some(effort) = req.effort {
            obj.insert("output_config".into(), json!({"effort": effort.as_str()}));
        }

        Ok(body)
    }

    fn request(&self, body: &Value) -> reqwest::RequestBuilder {
        self.http
            .post(format!(
                "{}/v1/messages",
                self.base_url.trim_end_matches('/')
            ))
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", API_VERSION)
            .header("content-type", "application/json")
            .json(body)
    }
}

#[async_trait]
impl Provider for Anthropic {
    fn id(&self) -> &str {
        "anthropic"
    }

    fn default_model(&self) -> &str {
        &self.default_model
    }

    fn vision(&self) -> bool {
        self.vision
    }

    async fn complete(
        &self,
        req: &CompletionRequest,
        sink: Option<&StreamSink>,
    ) -> Result<CompletionResponse> {
        match sink {
            Some(sink) => self.complete_streaming(req, sink).await,
            None => self.complete_once(req).await,
        }
    }
}

impl Anthropic {
    async fn complete_once(&self, req: &CompletionRequest) -> Result<CompletionResponse> {
        let body = self.body(req, false)?;
        let text = self.send(&body).await?.text().await?;
        let v: Value = serde_json::from_str(&text).context("malformed response body")?;
        decode_response(&v)
    }

    async fn complete_streaming(
        &self,
        req: &CompletionRequest,
        sink: &StreamSink,
    ) -> Result<CompletionResponse> {
        let body = self.body(req, true)?;
        // Retries cover the send and the status line — nothing has streamed
        // yet. From here down a failure is mid-stream: deltas may already be
        // on the user's screen, so it propagates as-is, carrying no
        // `ProviderError` — which is also what tells the failover wrapper it
        // must not re-issue the request.
        let resp = self.send(&body).await?;

        let mut acc = StreamAccumulator::default();
        let mut buf = crate::provider::sse::SseBuffer::default();
        let mut stream = resp.bytes_stream();

        while let Some(chunk) = stream.next().await {
            buf.push(&chunk?);
            // SSE frames are separated by a blank line. Split on bytes, not
            // decoded text: a network chunk can end mid-character, and only a
            // complete frame is guaranteed to be complete UTF-8.
            while let Some(frame) = buf.next_segment(b"\n\n") {
                for line in frame.lines() {
                    let Some(data) = line.strip_prefix("data:") else {
                        continue;
                    };
                    let data = data.trim();
                    if data.is_empty() {
                        continue;
                    }
                    let event: Value =
                        serde_json::from_str(data).context("malformed SSE data frame")?;
                    acc.push(&event, sink)?;
                }
            }
        }

        acc.finish()
    }
}

/// Pull the useful part out of an error body without assuming its shape.
fn api_error(text: &str) -> String {
    serde_json::from_str::<Value>(text)
        .ok()
        .and_then(|v| {
            v.pointer("/error/message")
                .and_then(Value::as_str)
                .map(str::to_string)
        })
        .unwrap_or_else(|| text.chars().take(500).collect())
}

fn encode_message(m: &Message, vision: bool) -> Value {
    let role = match m.role {
        Role::User => "user",
        Role::Assistant => "assistant",
    };
    let content: Vec<Value> = m
        .content
        .iter()
        .filter_map(|b| encode_block(b, vision))
        .collect();
    json!({"role": role, "content": content})
}

fn encode_block(b: &Block, vision: bool) -> Option<Value> {
    Some(match b {
        Block::Text { text } => json!({"type": "text", "text": text}),
        Block::Thinking { text, signature } => {
            // A thinking block without a signature can't be replayed; the API
            // rejects reconstructed ones. Drop it rather than send it back.
            let sig = signature.as_ref()?;
            json!({"type": "thinking", "thinking": text, "signature": sig})
        }
        Block::ToolUse { id, name, input } => {
            json!({"type": "tool_use", "id": id, "name": name, "input": input})
        }
        Block::ToolResult {
            tool_use_id,
            content,
            is_error,
        } => json!({
            "type": "tool_result",
            "tool_use_id": tool_use_id,
            "content": content,
            "is_error": is_error,
        }),
        // `data` is bare base64 in the type, because this is the dialect
        // that wants it bare — the `data:` prefix belongs to the other one.
        Block::Image {
            media_type, data, ..
        } if vision => json!({
            "type": "image",
            "source": {"type": "base64", "media_type": media_type, "data": data},
        }),
        // Same degrade as the OpenAI backend, and it has to be the same
        // words: a conversation carried across a `/model` switch would
        // otherwise describe its own history differently depending on who
        // was asked, which is two answers to "what was in this turn".
        Block::Image {
            media_type, source, ..
        } => json!({
            "type": "text",
            "text": Block::image_placeholder(media_type, source.as_deref()),
        }),
    })
}

fn decode_block(v: &Value) -> Option<Block> {
    match v.get("type")?.as_str()? {
        "text" => Some(Block::Text {
            text: v.get("text")?.as_str().unwrap_or_default().to_string(),
        }),
        "thinking" => Some(Block::Thinking {
            text: v
                .get("thinking")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_string(),
            signature: v
                .get("signature")
                .and_then(Value::as_str)
                .map(str::to_string),
        }),
        "tool_use" => Some(Block::ToolUse {
            id: v.get("id")?.as_str()?.to_string(),
            name: v.get("name")?.as_str()?.to_string(),
            input: v.get("input").cloned().unwrap_or_else(|| json!({})),
        }),
        // Server-side tool traffic and anything else we don't model yet.
        _ => None,
    }
}

fn decode_stop_reason(s: Option<&str>) -> StopReason {
    match s {
        Some("end_turn") => StopReason::EndTurn,
        Some("tool_use") => StopReason::ToolUse,
        Some("max_tokens") => StopReason::MaxTokens,
        Some("refusal") => StopReason::Refusal,
        Some("pause_turn") => StopReason::PauseTurn,
        _ => StopReason::Other,
    }
}

fn decode_usage(v: Option<&Value>) -> Usage {
    let Some(v) = v else { return Usage::default() };
    let g = |k: &str| v.get(k).and_then(Value::as_u64).unwrap_or(0);
    Usage {
        input_tokens: g("input_tokens"),
        output_tokens: g("output_tokens"),
        cache_creation_input_tokens: g("cache_creation_input_tokens"),
        cache_read_input_tokens: g("cache_read_input_tokens"),
    }
}

fn decode_refusal(v: &Value) -> Option<Refusal> {
    let d = v.get("stop_details")?;
    if d.is_null() {
        return None;
    }
    Some(Refusal {
        category: d
            .get("category")
            .and_then(Value::as_str)
            .map(str::to_string),
        explanation: d
            .get("explanation")
            .and_then(Value::as_str)
            .map(str::to_string),
    })
}

fn decode_response(v: &Value) -> Result<CompletionResponse> {
    let content = v
        .get("content")
        .and_then(Value::as_array)
        .ok_or_else(|| anyhow!("response has no content array"))?
        .iter()
        .filter_map(decode_block)
        .collect();

    Ok(CompletionResponse {
        message: Message::assistant(content),
        stop_reason: decode_stop_reason(v.get("stop_reason").and_then(Value::as_str)),
        usage: decode_usage(v.get("usage")),
        refusal: decode_refusal(v),
        model: v
            .get("model")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string(),
        // Non-streaming responses carry already-parsed JSON, so arguments
        // cannot arrive malformed on this path.
        malformed_tool_args: 0,
    })
}

/// Reassembles a streamed message. Blocks arrive interleaved by index, and
/// tool arguments arrive as partial JSON that only parses once complete.
#[derive(Default)]
struct StreamAccumulator {
    blocks: BTreeMap<usize, PartialBlock>,
    stop_reason: Option<StopReason>,
    usage: Usage,
    refusal: Option<Refusal>,
    model: String,
}

enum PartialBlock {
    Text(String),
    Thinking {
        text: String,
        signature: Option<String>,
    },
    ToolUse {
        id: String,
        name: String,
        json: String,
    },
    Ignored,
}

impl StreamAccumulator {
    fn push(&mut self, event: &Value, sink: &StreamSink) -> Result<()> {
        match event.get("type").and_then(Value::as_str).unwrap_or("") {
            "message_start" => {
                if let Some(m) = event.get("message") {
                    self.model = m
                        .get("model")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string();
                    self.usage.add(&decode_usage(m.get("usage")));
                    // Input tokens — including both cache tiers — are known
                    // from this first frame. Report them now so a run cancelled
                    // mid-stream still knows what the prompt cost.
                    let _ = sink.send(StreamEvent::Usage(self.usage.clone()));
                }
            }
            "content_block_start" => {
                let idx = event.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
                let cb = event.get("content_block").cloned().unwrap_or(Value::Null);
                let partial = match cb.get("type").and_then(Value::as_str).unwrap_or("") {
                    "text" => PartialBlock::Text(
                        cb.get("text")
                            .and_then(Value::as_str)
                            .unwrap_or_default()
                            .to_string(),
                    ),
                    "thinking" => PartialBlock::Thinking {
                        text: cb
                            .get("thinking")
                            .and_then(Value::as_str)
                            .unwrap_or_default()
                            .to_string(),
                        signature: None,
                    },
                    "tool_use" => {
                        let name = cb
                            .get("name")
                            .and_then(Value::as_str)
                            .unwrap_or_default()
                            .to_string();
                        let _ = sink.send(StreamEvent::ToolUseStart { name: name.clone() });
                        PartialBlock::ToolUse {
                            id: cb
                                .get("id")
                                .and_then(Value::as_str)
                                .unwrap_or_default()
                                .to_string(),
                            name,
                            json: String::new(),
                        }
                    }
                    _ => PartialBlock::Ignored,
                };
                self.blocks.insert(idx, partial);
            }
            "content_block_delta" => {
                let idx = event.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
                let Some(delta) = event.get("delta") else {
                    return Ok(());
                };
                let Some(block) = self.blocks.get_mut(&idx) else {
                    return Ok(());
                };
                match (
                    delta.get("type").and_then(Value::as_str).unwrap_or(""),
                    block,
                ) {
                    ("text_delta", PartialBlock::Text(buf)) => {
                        let t = delta
                            .get("text")
                            .and_then(Value::as_str)
                            .unwrap_or_default();
                        buf.push_str(t);
                        let _ = sink.send(StreamEvent::TextDelta(t.to_string()));
                    }
                    ("thinking_delta", PartialBlock::Thinking { text, .. }) => {
                        let t = delta
                            .get("thinking")
                            .and_then(Value::as_str)
                            .unwrap_or_default();
                        text.push_str(t);
                        let _ = sink.send(StreamEvent::ThinkingDelta(t.to_string()));
                    }
                    ("signature_delta", PartialBlock::Thinking { signature, .. }) => {
                        let s = delta
                            .get("signature")
                            .and_then(Value::as_str)
                            .unwrap_or_default();
                        signature.get_or_insert_with(String::new).push_str(s);
                    }
                    ("input_json_delta", PartialBlock::ToolUse { json, .. }) => {
                        json.push_str(
                            delta
                                .get("partial_json")
                                .and_then(Value::as_str)
                                .unwrap_or_default(),
                        );
                    }
                    _ => {}
                }
            }
            "message_delta" => {
                if let Some(d) = event.get("delta") {
                    if let Some(sr) = d.get("stop_reason").and_then(Value::as_str) {
                        self.stop_reason = Some(decode_stop_reason(Some(sr)));
                    }
                    if let Some(r) = decode_refusal(d) {
                        self.refusal = Some(r);
                    }
                }
                self.usage.add(&decode_usage(event.get("usage")));
                let _ = sink.send(StreamEvent::Usage(self.usage.clone()));
            }
            "error" => {
                bail!(
                    "anthropic stream error: {}",
                    event
                        .pointer("/error/message")
                        .and_then(Value::as_str)
                        .unwrap_or("unknown")
                );
            }
            _ => {}
        }
        Ok(())
    }

    fn finish(self) -> Result<CompletionResponse> {
        let mut content = Vec::new();
        let mut malformed = 0u32;
        for (_, block) in self.blocks {
            match block {
                PartialBlock::Text(text) => {
                    if !text.is_empty() {
                        content.push(Block::Text { text });
                    }
                }
                PartialBlock::Thinking { text, signature } => {
                    content.push(Block::Thinking { text, signature })
                }
                PartialBlock::ToolUse { id, name, json } => {
                    // An empty-argument call streams no partial_json at all.
                    let input = if json.trim().is_empty() {
                        json!({})
                    } else {
                        match serde_json::from_str(&json) {
                            Ok(v) => v,
                            Err(e) => {
                                // Don't kill the turn: hand the model an error
                                // result so it can retry, and count it as the
                                // reliability signal it is.
                                malformed += 1;
                                tracing::warn!(
                                    tool = %name,
                                    error = %e,
                                    "tool arguments did not parse"
                                );
                                json!({"__malformed_arguments": json})
                            }
                        }
                    };
                    content.push(Block::ToolUse { id, name, input });
                }
                PartialBlock::Ignored => {}
            }
        }

        Ok(CompletionResponse {
            message: Message::assistant(content),
            stop_reason: self.stop_reason.unwrap_or(StopReason::Other),
            usage: self.usage,
            refusal: self.refusal,
            model: self.model,
            malformed_tool_args: malformed,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn client() -> Anthropic {
        client_at("http://localhost:1")
    }

    /// Shared with `retry_tests` below.
    pub(super) fn client_at(base_url: &str) -> Anthropic {
        Anthropic {
            http: reqwest::Client::new(),
            api_key: "test-key".into(),
            base_url: base_url.into(),
            default_model: DEFAULT_MODEL.into(),
            vision: true,
            // Fast retries: these tests measure counts and outcomes, not
            // wall clock.
            retry: crate::provider::retry::RetryPolicy {
                base_delay: std::time::Duration::from_millis(1),
                ..Default::default()
            },
        }
    }

    fn req() -> CompletionRequest {
        CompletionRequest {
            model: DEFAULT_MODEL.into(),
            system: None,
            messages: vec![Message::user("hi")],
            tools: Vec::new(),
            max_tokens: 1024,
            effort: None,
            thinking: true,
            cache_prompt: false,
        }
    }

    fn spec(name: &str) -> ToolSpec {
        ToolSpec {
            name: name.into(),
            description: "does a thing".into(),
            input_schema: json!({"type": "object"}),
        }
    }

    #[test]
    fn a_configured_temperature_is_refused_at_construction() {
        let cfg = crate::config::ProviderConfig {
            kind: "anthropic".into(),
            api_key: Some("test-key".into()),
            temperature: Some(0.0),
            ..Default::default()
        };
        let err = match Anthropic::from_config(&cfg) {
            Err(e) => e.to_string(),
            Ok(_) => panic!("a pinned temperature must not construct an Anthropic provider"),
        };
        assert!(err.contains("temperature"), "{err}");
    }

    /// Anywhere in the tree, at any depth — a knob smuggled into a nested
    /// object 400s exactly as loudly as one at the top level.
    fn mentions_key(v: &Value, key: &str) -> bool {
        match v {
            Value::Object(map) => {
                map.contains_key(key) || map.values().any(|v| mentions_key(v, key))
            }
            Value::Array(items) => items.iter().any(|v| mentions_key(v, key)),
            _ => false,
        }
    }

    #[test]
    fn the_sampling_knobs_are_never_sent_whatever_the_request_asks_for() {
        // The Claude 5 family rejects these outright, so the guarantee has to
        // hold across every shape of request rather than the common one.
        for thinking in [true, false] {
            for effort in [None, Some(Effort::Low), Some(Effort::High)] {
                for cache_prompt in [true, false] {
                    let r = CompletionRequest {
                        system: Some("be brief".into()),
                        tools: vec![spec("fs_read")],
                        thinking,
                        effort,
                        cache_prompt,
                        ..req()
                    };
                    let body = client().body(&r, false).unwrap();
                    for knob in ["temperature", "top_p", "top_k", "budget_tokens"] {
                        assert!(
                            !mentions_key(&body, knob),
                            "{knob} was sent (thinking={thinking}, effort={effort:?})"
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn the_cache_breakpoint_goes_on_the_last_system_block_and_nothing_after_it() {
        let r = CompletionRequest {
            system: Some("you are a harness".into()),
            tools: vec![spec("fs_read"), spec("shell")],
            cache_prompt: true,
            ..req()
        };
        let body = client().body(&r, false).unwrap();

        // Render order is tools -> system -> messages, so one marker on the
        // last system block covers the tool definitions too. Prompt caching is
        // a prefix match: a marker on the tools instead would leave the system
        // prompt outside the cached span.
        let system = body["system"].as_array().unwrap();
        assert_eq!(system.len(), 1);
        assert_eq!(system[0]["cache_control"], json!({"type": "ephemeral"}));

        for tool in body["tools"].as_array().unwrap() {
            assert!(
                tool.get("cache_control").is_none(),
                "a tool carried the breakpoint too"
            );
        }
    }

    #[test]
    fn the_moving_breakpoint_sits_on_the_last_message_block_and_only_there() {
        let r = CompletionRequest {
            system: Some("you are a harness".into()),
            tools: vec![spec("fs_read")],
            cache_prompt: true,
            messages: vec![
                Message::user("first"),
                Message::assistant(vec![Block::text("ok")]),
                Message::user("second"),
            ],
            ..req()
        };
        let body = client().body(&r, false).unwrap();
        let messages = body["messages"].as_array().unwrap();

        // Exactly one marker in the messages, on the very last block: the
        // transcript is append-only between turns, so this turn's request is a
        // prefix of the next and the whole history reads from cache. Two
        // markers total (system + here) stays well under the API's limit of 4.
        assert!(!mentions_key(&messages[0], "cache_control"));
        assert!(!mentions_key(&messages[1], "cache_control"));
        let last = messages[2]["content"].as_array().unwrap();
        assert_eq!(
            last.last().unwrap()["cache_control"],
            json!({"type": "ephemeral"})
        );
    }

    #[test]
    fn the_moving_breakpoint_never_lands_on_a_thinking_block() {
        // The API rejects cache_control on thinking blocks. When one ends the
        // transcript, the marker backs up to the nearest markable block.
        let r = CompletionRequest {
            cache_prompt: true,
            messages: vec![
                Message::user("go"),
                Message::assistant(vec![
                    Block::text("partial answer"),
                    Block::Thinking {
                        text: "hmm".into(),
                        signature: Some("sig".into()),
                    },
                ]),
            ],
            ..req()
        };
        let body = client().body(&r, false).unwrap();
        let blocks = body["messages"].as_array().unwrap()[1]["content"]
            .as_array()
            .unwrap();

        assert_eq!(blocks[1]["type"], "thinking");
        assert!(
            blocks[1].get("cache_control").is_none(),
            "marker on a thinking block 400s"
        );
        assert_eq!(blocks[0]["cache_control"], json!({"type": "ephemeral"}));
    }

    #[test]
    fn with_no_system_prompt_the_breakpoint_falls_to_the_last_tool() {
        let r = CompletionRequest {
            system: None,
            tools: vec![spec("fs_read"), spec("shell")],
            cache_prompt: true,
            ..req()
        };
        let body = client().body(&r, false).unwrap();
        let tools = body["tools"].as_array().unwrap();

        assert!(
            tools[0].get("cache_control").is_none(),
            "the breakpoint must be last, not first"
        );
        assert_eq!(tools[1]["cache_control"], json!({"type": "ephemeral"}));
    }

    #[test]
    fn nothing_is_marked_cacheable_unless_it_was_asked_for() {
        let r = CompletionRequest {
            system: Some("you are a harness".into()),
            tools: vec![spec("fs_read")],
            cache_prompt: false,
            ..req()
        };
        assert!(!mentions_key(
            &client().body(&r, false).unwrap(),
            "cache_control"
        ));
    }

    #[test]
    fn thinking_is_adaptive_rather_than_a_token_budget() {
        let body = client()
            .body(
                &CompletionRequest {
                    thinking: true,
                    ..req()
                },
                false,
            )
            .unwrap();
        assert_eq!(body["thinking"]["type"], "adaptive");
    }

    #[test]
    fn disabling_thinking_above_high_effort_is_refused_before_the_request_is_sent() {
        // On Opus 5 the API rejects this combination. Failing here costs a
        // function call; failing there costs a round trip and returns an error
        // that does not say which knob to move.
        for effort in [Effort::XHigh, Effort::Max] {
            let r = CompletionRequest {
                thinking: false,
                effort: Some(effort),
                ..req()
            };
            let err = client().body(&r, false).unwrap_err().to_string();
            assert!(
                err.contains(effort.as_str()),
                "the error should name the effort: {err}"
            );
            assert!(
                err.contains("high"),
                "the error should say what to do: {err}"
            );
        }

        // At `high` and below it is accepted, and must actually be sent.
        for effort in [
            None,
            Some(Effort::Low),
            Some(Effort::Medium),
            Some(Effort::High),
        ] {
            let r = CompletionRequest {
                thinking: false,
                effort,
                ..req()
            };
            let body = client().body(&r, false).unwrap();
            assert_eq!(body["thinking"], json!({"type": "disabled"}));
        }
    }

    #[test]
    fn a_thinking_block_with_no_signature_is_dropped_rather_than_replayed() {
        // Signatures are opaque and checked. Sending a reconstructed one 400s
        // the *next* turn, which is a confusing place to discover it.
        let dropped = encode_block(
            &Block::Thinking {
                text: "reasoning".into(),
                signature: None,
            },
            true,
        );
        assert!(dropped.is_none());

        let kept = encode_block(
            &Block::Thinking {
                text: "reasoning".into(),
                signature: Some("sig-abc".into()),
            },
            true,
        )
        .unwrap();
        assert_eq!(kept["signature"], "sig-abc");
        assert_eq!(kept["thinking"], "reasoning");
    }

    #[test]
    fn an_image_is_a_base64_source_block_when_seen_and_a_named_line_when_not() {
        let block = Block::image("image/png", b"\x89PNG-ish", Some("shot.png".into()));

        let seen = encode_block(&block, true).unwrap();
        assert_eq!(seen["type"], "image");
        assert_eq!(seen["source"]["type"], "base64");
        assert_eq!(seen["source"]["media_type"], "image/png");
        let data = seen["source"]["data"].as_str().unwrap();
        assert!(
            !data.starts_with("data:"),
            "bare base64 here — the `data:` prefix is the other dialect's: {data}"
        );

        let blind = encode_block(&block, false).unwrap();
        assert_eq!(blind["type"], "text", "degrades to text, never dropped");
        let text = blind["text"].as_str().unwrap();
        assert!(text.contains("shot.png"), "{text}");
        assert!(!text.contains("PNG-ish"), "and never the payload: {text}");
    }

    /// The two backends must describe the same block the same way, or a
    /// conversation carried across a `/model` switch tells two stories about
    /// its own history.
    #[test]
    fn both_backends_name_an_unseen_image_identically() {
        let block = Block::image("image/png", b"x", Some("shot.png".into()));
        let mine = encode_block(&block, false).unwrap();

        let mut theirs = Vec::new();
        crate::provider::openai::encode_message_for_test(
            &Message {
                role: Role::User,
                content: vec![block],
            },
            &mut theirs,
            false,
        );
        assert_eq!(
            mine["text"].as_str().unwrap(),
            theirs[0]["content"].as_str().unwrap()
        );
    }

    #[test]
    fn tool_results_and_a_steer_ride_in_one_user_message() {
        // There is no legal slot for a user message between a `tool_use` and
        // its result, so steering text has to travel as another block of the
        // message already carrying the results.
        let encoded = encode_message(
            &Message::tool_results(vec![
                Block::ToolResult {
                    tool_use_id: "t1".into(),
                    content: "42".into(),
                    is_error: false,
                },
                Block::text("actually, focus on X"),
            ]),
            true,
        );

        assert_eq!(encoded["role"], "user");
        let content = encoded["content"].as_array().unwrap();
        assert_eq!(content.len(), 2);
        assert_eq!(content[0]["type"], "tool_result");
        assert_eq!(content[1]["type"], "text");
    }

    #[test]
    fn a_refusal_arrives_as_an_ordinary_response_and_shows_up_in_the_stop_reason() {
        // HTTP 200. Reading `content` without checking the stop reason gets you
        // an empty string and no idea why.
        let v = json!({
            "content": [],
            "model": DEFAULT_MODEL,
            "stop_reason": "refusal",
            "stop_details": {"category": "policy", "explanation": "declined"},
            "usage": {"input_tokens": 12, "output_tokens": 0},
        });

        let resp = decode_response(&v).unwrap();
        assert_eq!(resp.stop_reason, StopReason::Refusal);
        let refusal = resp.refusal.expect("a refusal must carry its details");
        assert_eq!(refusal.category.as_deref(), Some("policy"));
        assert_eq!(resp.usage.input_tokens, 12);
    }

    #[test]
    fn an_ordinary_response_carries_no_refusal() {
        let v = json!({
            "content": [{"type": "text", "text": "hello"}],
            "model": DEFAULT_MODEL,
            "stop_reason": "end_turn",
            "stop_details": null,
            "usage": {"input_tokens": 5, "output_tokens": 2},
        });

        let resp = decode_response(&v).unwrap();
        assert_eq!(resp.stop_reason, StopReason::EndTurn);
        assert!(resp.refusal.is_none());
        assert_eq!(resp.message.text(), "hello");
    }
}

#[cfg(test)]
mod retry_tests {
    use super::tests::client_at;
    use super::*;
    use crate::provider::retry::ProviderError;
    use crate::provider::Provider;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    fn req() -> CompletionRequest {
        CompletionRequest {
            model: DEFAULT_MODEL.into(),
            system: None,
            messages: vec![Message::user("hi")],
            tools: Vec::new(),
            max_tokens: 64,
            effort: None,
            thinking: false,
            cache_prompt: false,
        }
    }

    fn ok_body(text: &str) -> String {
        serde_json::json!({
            "content": [{"type": "text", "text": text}],
            "stop_reason": "end_turn",
            "usage": {"input_tokens": 1, "output_tokens": 1},
            "model": DEFAULT_MODEL,
        })
        .to_string()
    }

    fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
        haystack.windows(needle.len()).position(|w| w == needle)
    }

    type MockResponse = (u16, Vec<(&'static str, String)>, String);

    /// A scripted HTTP server: each connection gets the next canned response
    /// and is closed. Anything past the script gets a 500, which fails the
    /// test through the assertion on the request count.
    async fn mock_http(responses: Vec<MockResponse>) -> (String, Arc<AtomicUsize>) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let count = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&count);

        tokio::spawn(async move {
            let mut responses = responses.into_iter();
            loop {
                let Ok((mut sock, _)) = listener.accept().await else {
                    break;
                };
                counter.fetch_add(1, Ordering::SeqCst);

                // Read the request head, then its content-length body, so the
                // client never sees a reset while still writing.
                let mut buf = Vec::new();
                let mut tmp = [0u8; 8192];
                let (head_end, body_len) = loop {
                    let n = sock.read(&mut tmp).await.unwrap_or(0);
                    if n == 0 {
                        break (buf.len(), 0);
                    }
                    buf.extend_from_slice(&tmp[..n]);
                    if let Some(pos) = find(&buf, b"\r\n\r\n") {
                        let head = String::from_utf8_lossy(&buf[..pos]).to_string();
                        let len = head
                            .lines()
                            .find_map(|l| {
                                let l = l.to_ascii_lowercase();
                                l.strip_prefix("content-length:")
                                    .and_then(|v| v.trim().parse::<usize>().ok())
                            })
                            .unwrap_or(0);
                        break (pos + 4, len);
                    }
                };
                while buf.len() < head_end + body_len {
                    let n = sock.read(&mut tmp).await.unwrap_or(0);
                    if n == 0 {
                        break;
                    }
                    buf.extend_from_slice(&tmp[..n]);
                }

                let (status, headers, body) =
                    responses
                        .next()
                        .unwrap_or((500, Vec::new(), "script exhausted".into()));
                let mut resp = format!(
                    "HTTP/1.1 {status} R\r\ncontent-length: {}\r\nconnection: close\r\n",
                    body.len()
                );
                for (k, v) in headers {
                    resp.push_str(&format!("{k}: {v}\r\n"));
                }
                resp.push_str("\r\n");
                resp.push_str(&body);
                let _ = sock.write_all(resp.as_bytes()).await;
                let _ = sock.shutdown().await;
            }
        });
        (format!("http://{addr}"), count)
    }

    #[tokio::test]
    async fn transient_failures_are_retried_until_the_request_succeeds() {
        let (url, count) = mock_http(vec![
            (
                429,
                vec![("retry-after", "0".into())],
                "rate limited".into(),
            ),
            (
                429,
                vec![("retry-after", "0".into())],
                "rate limited".into(),
            ),
            (200, vec![], ok_body("recovered")),
        ])
        .await;

        let response = client_at(&url).complete(&req(), None).await.unwrap();
        assert_eq!(response.message.text(), "recovered");
        assert_eq!(count.load(Ordering::SeqCst), 3, "two retries, then success");
    }

    #[tokio::test]
    async fn max_retries_zero_disables_retrying() {
        let (url, count) = mock_http(vec![
            (
                429,
                vec![("retry-after", "0".into())],
                "rate limited".into(),
            ),
            (200, vec![], ok_body("never reached")),
        ])
        .await;

        let mut provider = client_at(&url);
        provider.retry.max_retries = 0;
        let err = provider.complete(&req(), None).await.unwrap_err();
        assert!(err.to_string().contains("429"), "{err:#}");
        assert_eq!(count.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn auth_failures_are_terminal_by_request_count_not_by_elapsed_time() {
        let (url, count) = mock_http(vec![(401, vec![], "invalid x-api-key".into())]).await;

        let err = client_at(&url).complete(&req(), None).await.unwrap_err();
        assert!(err.to_string().contains("401"), "{err:#}");
        assert_eq!(
            err.downcast_ref::<ProviderError>(),
            Some(&ProviderError::Auth),
            "the class rides under the message"
        );
        // The assertion that matters: one request. A retried 401 is a lockout
        // risk, and elapsed-time assertions measure the scheduler.
        assert_eq!(count.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn a_retry_after_past_the_cap_is_a_failure_not_a_nap() {
        let (url, count) = mock_http(vec![(
            429,
            vec![("retry-after", "3600".into())],
            "later".into(),
        )])
        .await;

        let err = client_at(&url).complete(&req(), None).await.unwrap_err();
        assert!(matches!(
            err.downcast_ref::<ProviderError>(),
            Some(ProviderError::RateLimit { .. })
        ));
        assert_eq!(
            count.load(Ordering::SeqCst),
            1,
            "an hour-long wait must not be slept"
        );
    }

    #[tokio::test]
    async fn context_overflow_stays_out_of_the_retry_path_and_reaches_compaction() {
        let (url, count) = mock_http(vec![(
            400,
            Vec::new(),
            r#"{"error":{"type":"exceed_context_size_error","message":"too big"}}"#.into(),
        )])
        .await;

        let err = client_at(&url).complete(&req(), None).await.unwrap_err();
        assert_eq!(
            count.load(Ordering::SeqCst),
            1,
            "overflow retried with the same payload"
        );
        // The loop's compact-and-retry-once recovery keys on this.
        assert!(crate::agent::is_context_overflow(&err), "{err:#}");
    }

    #[tokio::test]
    async fn a_mid_run_transient_error_never_re_executes_a_tool() {
        use crate::agent::{Agent, Conversation};
        use crate::config::{AgentConfig, PermissionMode};
        use crate::tool::{ModeApprover, Registry, Tool, ToolCtx, ToolOutput};

        // Turn 1 asks for a tool; the request carrying its result dies with a
        // retryable 429; the retry succeeds. The tool must have run exactly
        // once — the retry re-sends the HTTP request, never the turn.
        struct CountingTool(Arc<AtomicUsize>);
        #[async_trait]
        impl Tool for CountingTool {
            fn name(&self) -> &str {
                "echo"
            }
            fn description(&self) -> &str {
                "counts"
            }
            fn input_schema(&self) -> serde_json::Value {
                serde_json::json!({"type": "object"})
            }
            fn read_only(&self) -> bool {
                true
            }
            async fn call(&self, _input: serde_json::Value, _ctx: &ToolCtx) -> Result<ToolOutput> {
                self.0.fetch_add(1, Ordering::SeqCst);
                Ok(ToolOutput::ok("ran"))
            }
        }

        let tool_use_body = serde_json::json!({
            "content": [{"type": "tool_use", "id": "t1", "name": "echo", "input": {}}],
            "stop_reason": "tool_use",
            "usage": {"input_tokens": 1, "output_tokens": 1},
            "model": DEFAULT_MODEL,
        })
        .to_string();
        let (url, requests) = mock_http(vec![
            (200, vec![], tool_use_body),
            (
                429,
                vec![("retry-after", "0".into())],
                "rate limited".into(),
            ),
            (200, vec![], ok_body("done")),
        ])
        .await;

        let executions = Arc::new(AtomicUsize::new(0));
        let mut registry = Registry::new();
        registry.insert(Arc::new(CountingTool(Arc::clone(&executions))));
        let agent = Agent::new(
            Box::new(client_at(&url)),
            registry,
            Arc::new(ModeApprover {
                mode: PermissionMode::Allow,
            }),
            ToolCtx {
                workspace: std::env::temp_dir(),
                ..Default::default()
            },
            AgentConfig {
                thinking: false,
                force_final_answer: false,
                ..Default::default()
            },
            None,
        )
        .unwrap();

        let mut convo = Conversation::user("go");
        let outcome = agent.run(&mut convo, None).await.unwrap();

        assert_eq!(outcome.text, "done");
        assert_eq!(
            requests.load(Ordering::SeqCst),
            3,
            "turn 2 was retried at the HTTP layer"
        );
        assert_eq!(
            executions.load(Ordering::SeqCst),
            1,
            "the retry duplicated a tool execution"
        );
    }
}