aigw-anthropic 0.5.0

Anthropic provider for AI Gateway
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
//! Automatic `cache_control` injection + Anthropic API constraint enforcement.
//!
//! The Anthropic Messages API caches input by inserting `cache_control`
//! markers on specific blocks. The API enforces two hard rules that any
//! caller must respect:
//!
//! 1. **Maximum 4 breakpoints per request.** Any request with more than
//!    4 `cache_control` markers is rejected with HTTP 400.
//! 2. **`prompt-caching-scope-2026-01-05` TTL ordering.** In evaluation
//!    order (tools → system → messages), once a 5-minute (default-TTL)
//!    block has been seen, any subsequent block with a longer TTL must
//!    not carry the explicit `ttl` field — the longer TTL would be
//!    silently downgraded server-side, but the request is rejected if
//!    declared.
//!
//! Both rules are always enforced by [`AnthropicRequestTranslator`] after
//! the (swappable) [`CacheControlStrategy`] runs. The strategy decides
//! *where* to place breakpoints; this module enforces *correctness*.
//!
//! [`AnthropicRequestTranslator`]: super::request::AnthropicRequestTranslator

use crate::types::{
    CacheControl, ContentBlock, MessageContent, MessagesRequest, Role, SystemPrompt, TextBlock,
    TypedContentBlock,
};

/// Maximum number of `cache_control` markers Anthropic accepts per request.
pub const MAX_CACHE_BREAKPOINTS: usize = 4;

// ─── Strategy trait ─────────────────────────────────────────────────────────

/// A pluggable strategy for choosing where to inject `cache_control`
/// markers in an Anthropic [`MessagesRequest`].
///
/// Strategies run after the translator has produced the native request
/// from the canonical [`ChatRequest`] but before serialisation. The
/// translator always invokes [`enforce_breakpoint_cap`] and
/// [`normalize_ttl_ordering`] *after* the strategy so a strategy never
/// has to worry about API correctness rules.
///
/// The default strategy ([`DefaultCacheControlStrategy`]) is idempotent
/// — it skips injection entirely if any breakpoints are already present
/// — so users who set markers themselves on specific blocks don't get
/// double-injection.
///
/// [`ChatRequest`]: aigw_core::model::ChatRequest
pub trait CacheControlStrategy: Send + Sync {
    /// Mutate `req` to add `cache_control` markers.
    fn apply(&self, req: &mut MessagesRequest);
}

// ─── Default strategy ───────────────────────────────────────────────────────

/// Default Anthropic cache-injection strategy.
///
/// When no `cache_control` markers exist anywhere in the request, places
/// `cache_control: { type: "ephemeral" }` at three positions:
///
/// 1. The last [`Tool`] in `tools`.
/// 2. The last block of `system` (converting from [`SystemPrompt::Text`]
///    to [`SystemPrompt::Blocks`] if necessary).
/// 3. The last block of the second-to-last user message in `messages`
///    (converting from [`MessageContent::Text`] to
///    [`MessageContent::Blocks`] if necessary). Skipped if there are
///    fewer than two user messages.
///
/// If *any* `cache_control` marker is already present, the strategy is a
/// no-op — the user explicitly took control and we don't second-guess.
///
/// [`Tool`]: crate::types::Tool
pub struct DefaultCacheControlStrategy {
    /// Marker value to inject. Default is `{ type: "ephemeral" }` (no
    /// explicit TTL — the API uses 5 minutes server-side).
    pub marker: CacheControl,
    /// Minimum number of user messages required before the
    /// "second-to-last user message" injection runs. Default 2.
    pub min_user_messages: usize,
}

impl Default for DefaultCacheControlStrategy {
    fn default() -> Self {
        Self {
            marker: ephemeral_marker(),
            min_user_messages: 2,
        }
    }
}

impl CacheControlStrategy for DefaultCacheControlStrategy {
    fn apply(&self, req: &mut MessagesRequest) {
        if has_any_cache_control(req) {
            return;
        }
        inject_tools_cache(req, &self.marker);
        inject_system_cache(req, &self.marker);
        inject_messages_cache(req, &self.marker, self.min_user_messages);
    }
}

/// No-op strategy — disables automatic injection entirely while still
/// letting the translator apply the always-on correctness rules
/// ([`enforce_breakpoint_cap`] and [`normalize_ttl_ordering`]).
pub struct NoCacheControlStrategy;

impl CacheControlStrategy for NoCacheControlStrategy {
    fn apply(&self, _req: &mut MessagesRequest) {}
}

// ─── Construction helpers ───────────────────────────────────────────────────

/// Build a default `{ type: "ephemeral" }` marker (no explicit TTL).
#[must_use]
pub fn ephemeral_marker() -> CacheControl {
    CacheControl {
        r#type: "ephemeral".to_owned(),
        ttl: None,
    }
}

/// Build a `{ type: "ephemeral", ttl: <seconds> }` marker. Use this only
/// if you've enabled the `prompt-caching-scope-2026-01-05` beta and want
/// a longer-than-default cache lifetime.
#[must_use]
pub fn ephemeral_marker_with_ttl(ttl_seconds: u64) -> CacheControl {
    CacheControl {
        r#type: "ephemeral".to_owned(),
        ttl: Some(ttl_seconds),
    }
}

// ─── Idempotency check ──────────────────────────────────────────────────────

fn has_any_cache_control(req: &MessagesRequest) -> bool {
    if let Some(tools) = &req.tools
        && tools.iter().any(|t| t.cache_control.is_some())
    {
        return true;
    }
    if let Some(SystemPrompt::Blocks(blocks)) = &req.system
        && blocks.iter().any(|b| b.cache_control.is_some())
    {
        return true;
    }
    for msg in &req.messages {
        if let MessageContent::Blocks(blocks) = &msg.content {
            for block in blocks {
                if block_has_cache_control(block) {
                    return true;
                }
            }
        }
    }
    false
}

fn block_has_cache_control(block: &ContentBlock) -> bool {
    match block {
        ContentBlock::Typed(t) => typed_block_cache_control(t).is_some(),
        ContentBlock::Raw(obj) => obj.contains_key("cache_control"),
    }
}

fn typed_block_cache_control(t: &TypedContentBlock) -> Option<&CacheControl> {
    match t {
        TypedContentBlock::Text { cache_control, .. }
        | TypedContentBlock::Image { cache_control, .. }
        | TypedContentBlock::ToolUse { cache_control, .. }
        | TypedContentBlock::ToolResult { cache_control, .. } => cache_control.as_ref(),
        TypedContentBlock::Thinking { .. } | TypedContentBlock::RedactedThinking { .. } => None,
    }
}

fn typed_block_cache_control_mut(t: &mut TypedContentBlock) -> Option<&mut Option<CacheControl>> {
    match t {
        TypedContentBlock::Text { cache_control, .. }
        | TypedContentBlock::Image { cache_control, .. }
        | TypedContentBlock::ToolUse { cache_control, .. }
        | TypedContentBlock::ToolResult { cache_control, .. } => Some(cache_control),
        TypedContentBlock::Thinking { .. } | TypedContentBlock::RedactedThinking { .. } => None,
    }
}

// ─── Injection — the three positions ────────────────────────────────────────

fn inject_tools_cache(req: &mut MessagesRequest, marker: &CacheControl) {
    if let Some(tools) = &mut req.tools
        && let Some(last) = tools.last_mut()
        && last.cache_control.is_none()
    {
        last.cache_control = Some(marker.clone());
    }
}

fn inject_system_cache(req: &mut MessagesRequest, marker: &CacheControl) {
    match req.system.as_mut() {
        Some(sp @ SystemPrompt::Text(_)) => {
            // Promote string → blocks so we have somewhere to attach
            // cache_control. Empty strings are left alone (no point
            // caching nothing).
            let SystemPrompt::Text(text) = sp else {
                unreachable!()
            };
            if text.is_empty() {
                return;
            }
            let promoted = vec![TextBlock {
                r#type: "text".to_owned(),
                text: std::mem::take(text),
                cache_control: Some(marker.clone()),
            }];
            *sp = SystemPrompt::Blocks(promoted);
        }
        Some(SystemPrompt::Blocks(blocks)) => {
            if let Some(last) = blocks.last_mut()
                && last.cache_control.is_none()
            {
                last.cache_control = Some(marker.clone());
            }
        }
        None => {}
    }
}

fn inject_messages_cache(
    req: &mut MessagesRequest,
    marker: &CacheControl,
    min_user_messages: usize,
) {
    let user_indices: Vec<usize> = req
        .messages
        .iter()
        .enumerate()
        .filter(|(_, m)| m.role == Role::User)
        .map(|(i, _)| i)
        .collect();

    if user_indices.len() < min_user_messages {
        return;
    }
    let target_idx = user_indices[user_indices.len() - 2];
    let msg = &mut req.messages[target_idx];

    // Promote string content → block array so we have somewhere to
    // attach cache_control.
    if let MessageContent::Text(text) = &msg.content {
        let promoted = vec![ContentBlock::Typed(TypedContentBlock::Text {
            text: text.clone(),
            cache_control: None,
        })];
        msg.content = MessageContent::Blocks(promoted);
    }

    if let MessageContent::Blocks(blocks) = &mut msg.content
        && let Some(last) = blocks.last_mut()
        && let ContentBlock::Typed(typed) = last
        && let Some(slot) = typed_block_cache_control_mut(typed)
        && slot.is_none()
    {
        *slot = Some(marker.clone());
    }
}

// ─── Counting ──────────────────────────────────────────────────────────────

/// Count `cache_control` markers in tools, system, and message content.
///
/// Public for tests and for callers wanting to verify their own
/// post-translation state.
#[must_use]
pub fn count_cache_controls(req: &MessagesRequest) -> usize {
    let mut count = 0;
    if let Some(tools) = &req.tools {
        count += tools.iter().filter(|t| t.cache_control.is_some()).count();
    }
    if let Some(SystemPrompt::Blocks(blocks)) = &req.system {
        count += blocks.iter().filter(|b| b.cache_control.is_some()).count();
    }
    for msg in &req.messages {
        if let MessageContent::Blocks(blocks) = &msg.content {
            for block in blocks {
                if block_has_cache_control(block) {
                    count += 1;
                }
            }
        }
    }
    count
}

// ─── Always-on rule 1: 4-breakpoint cap ─────────────────────────────────────

/// Strip excess `cache_control` markers down to `MAX_CACHE_BREAKPOINTS`
/// (4) per Anthropic's hard API limit.
///
/// Removal priority (lowest-value first):
///
/// 1. System blocks earliest-first (preserve last).
/// 2. Tool blocks earliest-first (preserve last).
/// 3. Message content blocks earliest-first.
pub fn enforce_breakpoint_cap(req: &mut MessagesRequest) {
    enforce_breakpoint_cap_with_max(req, MAX_CACHE_BREAKPOINTS);
}

/// As [`enforce_breakpoint_cap`] but with a custom maximum. Useful for
/// testing; production should use the constant.
pub fn enforce_breakpoint_cap_with_max(req: &mut MessagesRequest, max_blocks: usize) {
    let total = count_cache_controls(req);
    if total <= max_blocks {
        return;
    }
    let mut excess = total - max_blocks;

    // Phase 1: system blocks (preserve last).
    if let Some(SystemPrompt::Blocks(blocks)) = req.system.as_mut() {
        let last_cc = blocks.iter().rposition(|b| b.cache_control.is_some());
        for (i, block) in blocks.iter_mut().enumerate() {
            if excess == 0 {
                break;
            }
            if Some(i) != last_cc && block.cache_control.is_some() {
                block.cache_control = None;
                excess -= 1;
            }
        }
    }
    if excess == 0 {
        return;
    }

    // Phase 2: tool blocks (preserve last).
    if let Some(tools) = req.tools.as_mut() {
        let last_cc = tools.iter().rposition(|t| t.cache_control.is_some());
        for (i, t) in tools.iter_mut().enumerate() {
            if excess == 0 {
                break;
            }
            if Some(i) != last_cc && t.cache_control.is_some() {
                t.cache_control = None;
                excess -= 1;
            }
        }
    }
    if excess == 0 {
        return;
    }

    // Phase 3: message content blocks (earliest-first, no preservation).
    for msg in req.messages.iter_mut() {
        if excess == 0 {
            break;
        }
        if let MessageContent::Blocks(blocks) = &mut msg.content {
            for block in blocks.iter_mut() {
                if excess == 0 {
                    break;
                }
                clear_cache_control(block, &mut excess);
            }
        }
    }
}

fn clear_cache_control(block: &mut ContentBlock, excess: &mut usize) {
    match block {
        ContentBlock::Typed(t) => {
            if let Some(slot) = typed_block_cache_control_mut(t)
                && slot.is_some()
            {
                *slot = None;
                *excess -= 1;
            }
        }
        ContentBlock::Raw(obj) => {
            if obj.remove("cache_control").is_some() {
                *excess -= 1;
            }
        }
    }
}

// ─── Always-on rule 2: TTL ordering ─────────────────────────────────────────

/// Normalize `cache_control.ttl` for `prompt-caching-scope-2026-01-05`.
///
/// The API requires that, in evaluation order (tools → system → message
/// content), no block with an explicit `ttl > 300` (i.e. anything longer
/// than the default 5-minute lifetime) appears *after* a block with the
/// default short TTL. Violating this returns HTTP 400.
///
/// This function walks the request in evaluation order and strips the
/// explicit `ttl` field from any longer-TTL block that follows a short
/// block. The block keeps its `cache_control` marker — only the `ttl`
/// override is removed, so the block falls back to the 5-minute default.
pub fn normalize_ttl_ordering(req: &mut MessagesRequest) {
    let mut seen_short = false;

    // Tools.
    if let Some(tools) = req.tools.as_mut() {
        for t in tools.iter_mut() {
            if let Some(cc) = t.cache_control.as_mut() {
                normalize_ttl(cc, &mut seen_short);
            }
        }
    }
    // System.
    if let Some(SystemPrompt::Blocks(blocks)) = req.system.as_mut() {
        for b in blocks.iter_mut() {
            if let Some(cc) = b.cache_control.as_mut() {
                normalize_ttl(cc, &mut seen_short);
            }
        }
    }
    // Messages.
    for msg in req.messages.iter_mut() {
        if let MessageContent::Blocks(blocks) = &mut msg.content {
            for block in blocks.iter_mut() {
                normalize_block_ttl(block, &mut seen_short);
            }
        }
    }
}

fn normalize_block_ttl(block: &mut ContentBlock, seen_short: &mut bool) {
    match block {
        ContentBlock::Typed(t) => {
            if let Some(slot) = typed_block_cache_control_mut(t)
                && let Some(cc) = slot.as_mut()
            {
                normalize_ttl(cc, seen_short);
            }
        }
        ContentBlock::Raw(obj) => {
            if let Some(cc_val) = obj.get_mut("cache_control")
                && let serde_json::Value::Object(cc_obj) = cc_val
            {
                let ttl = cc_obj.get("ttl").and_then(serde_json::Value::as_u64);
                match ttl {
                    None | Some(0..=300) => *seen_short = true,
                    Some(_) if *seen_short => {
                        cc_obj.remove("ttl");
                    }
                    _ => {}
                }
            }
        }
    }
}

fn normalize_ttl(cc: &mut CacheControl, seen_short: &mut bool) {
    match cc.ttl {
        None | Some(0..=300) => *seen_short = true,
        Some(_) if *seen_short => {
            cc.ttl = None;
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{Message, Tool};

    fn user_text(text: &str) -> Message {
        Message {
            role: Role::User,
            content: MessageContent::Text(text.to_owned()),
        }
    }

    fn assistant_text(text: &str) -> Message {
        Message {
            role: Role::Assistant,
            content: MessageContent::Text(text.to_owned()),
        }
    }

    fn req_with(messages: Vec<Message>) -> MessagesRequest {
        MessagesRequest::builder()
            .model("claude-sonnet-4-20250514")
            .messages(messages)
            .max_tokens(1024_u64)
            .build()
    }

    fn tool(name: &str) -> Tool {
        Tool {
            name: name.to_owned(),
            description: None,
            input_schema: serde_json::json!({"type":"object"}),
            cache_control: None,
        }
    }

    fn ephemeral_with_ttl(ttl: u64) -> CacheControl {
        ephemeral_marker_with_ttl(ttl)
    }

    // ── Default strategy: 3-position injection ────────────────────────────

    #[test]
    fn default_strategy_no_op_with_only_one_user_message() {
        let mut req = req_with(vec![user_text("hi")]);
        DefaultCacheControlStrategy::default().apply(&mut req);
        // Single user message → no message-side breakpoint added.
        assert_eq!(count_cache_controls(&req), 0);
    }

    #[test]
    fn default_strategy_injects_on_second_to_last_user_message() {
        let mut req = req_with(vec![
            user_text("first"),
            assistant_text("reply"),
            user_text("second"),
        ]);
        DefaultCacheControlStrategy::default().apply(&mut req);
        // First user message gets the breakpoint, second is untouched.
        let MessageContent::Blocks(blocks) = &req.messages[0].content else {
            panic!("expected Blocks");
        };
        let typed = match &blocks[0] {
            ContentBlock::Typed(t) => t,
            _ => panic!("expected Typed"),
        };
        let TypedContentBlock::Text { cache_control, .. } = typed else {
            panic!("expected Text");
        };
        assert!(cache_control.is_some());
        // Second user message untouched (still string).
        assert!(matches!(req.messages[2].content, MessageContent::Text(_)));
    }

    #[test]
    fn default_strategy_injects_on_last_tool() {
        let mut req = req_with(vec![user_text("hi")]);
        req.tools = Some(vec![tool("a"), tool("b")]);
        DefaultCacheControlStrategy::default().apply(&mut req);
        let tools = req.tools.as_ref().unwrap();
        assert!(tools[0].cache_control.is_none());
        assert!(tools[1].cache_control.is_some());
    }

    #[test]
    fn default_strategy_promotes_system_string_to_blocks() {
        let mut req = req_with(vec![user_text("hi")]);
        req.system = Some(SystemPrompt::Text("You are helpful.".into()));
        DefaultCacheControlStrategy::default().apply(&mut req);
        let SystemPrompt::Blocks(blocks) = req.system.as_ref().unwrap() else {
            panic!("expected promoted Blocks");
        };
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].text, "You are helpful.");
        assert!(blocks[0].cache_control.is_some());
    }

    #[test]
    fn default_strategy_injects_on_last_system_block() {
        let mut req = req_with(vec![user_text("hi")]);
        req.system = Some(SystemPrompt::Blocks(vec![
            TextBlock {
                r#type: "text".into(),
                text: "first".into(),
                cache_control: None,
            },
            TextBlock {
                r#type: "text".into(),
                text: "second".into(),
                cache_control: None,
            },
        ]));
        DefaultCacheControlStrategy::default().apply(&mut req);
        let SystemPrompt::Blocks(blocks) = req.system.as_ref().unwrap() else {
            panic!()
        };
        assert!(blocks[0].cache_control.is_none());
        assert!(blocks[1].cache_control.is_some());
    }

    #[test]
    fn default_strategy_skips_when_any_cache_control_present() {
        let mut req = req_with(vec![user_text("first"), user_text("second")]);
        req.tools = Some(vec![Tool {
            name: "t".into(),
            description: None,
            input_schema: serde_json::json!({"type":"object"}),
            cache_control: Some(ephemeral_marker()),
        }]);
        DefaultCacheControlStrategy::default().apply(&mut req);
        // Only the user-set tool marker, no message breakpoints injected.
        assert_eq!(count_cache_controls(&req), 1);
    }

    #[test]
    fn default_strategy_skips_empty_system_string() {
        let mut req = req_with(vec![user_text("hi")]);
        req.system = Some(SystemPrompt::Text(String::new()));
        DefaultCacheControlStrategy::default().apply(&mut req);
        // Empty string isn't promoted.
        assert!(matches!(req.system, Some(SystemPrompt::Text(_))));
        assert_eq!(count_cache_controls(&req), 0);
    }

    #[test]
    fn no_cache_control_strategy_is_a_noop() {
        let mut req = req_with(vec![user_text("first"), user_text("second")]);
        req.tools = Some(vec![tool("a")]);
        NoCacheControlStrategy.apply(&mut req);
        assert_eq!(count_cache_controls(&req), 0);
    }

    // ── 4-breakpoint cap ─────────────────────────────────────────────────

    #[test]
    fn enforce_cap_strips_excess() {
        let mut req = req_with(vec![user_text("hi")]);
        req.tools = Some(vec![
            Tool {
                name: "a".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                cache_control: Some(ephemeral_marker()),
            },
            Tool {
                name: "b".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                cache_control: Some(ephemeral_marker()),
            },
            Tool {
                name: "c".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                cache_control: Some(ephemeral_marker()),
            },
        ]);
        req.system = Some(SystemPrompt::Blocks(vec![
            TextBlock {
                r#type: "text".into(),
                text: "s1".into(),
                cache_control: Some(ephemeral_marker()),
            },
            TextBlock {
                r#type: "text".into(),
                text: "s2".into(),
                cache_control: Some(ephemeral_marker()),
            },
        ]));
        // 5 total → must drop to 4.
        enforce_breakpoint_cap(&mut req);
        assert_eq!(count_cache_controls(&req), 4);
        // Last system block must be preserved.
        let SystemPrompt::Blocks(blocks) = req.system.as_ref().unwrap() else {
            panic!()
        };
        assert!(blocks[1].cache_control.is_some());
        // Last tool must be preserved.
        let tools = req.tools.as_ref().unwrap();
        assert!(tools[2].cache_control.is_some());
    }

    #[test]
    fn enforce_cap_under_limit_no_change() {
        let mut req = req_with(vec![user_text("hi")]);
        req.tools = Some(vec![Tool {
            name: "a".into(),
            description: None,
            input_schema: serde_json::json!({"type":"object"}),
            cache_control: Some(ephemeral_marker()),
        }]);
        enforce_breakpoint_cap(&mut req);
        assert_eq!(count_cache_controls(&req), 1);
    }

    // ── TTL ordering ─────────────────────────────────────────────────────

    #[test]
    fn normalize_ttl_strips_long_after_short() {
        let mut req = req_with(vec![user_text("hi")]);
        req.tools = Some(vec![
            Tool {
                name: "a".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                // No TTL → counts as short.
                cache_control: Some(ephemeral_marker()),
            },
            Tool {
                name: "b".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                // 1h → must be downgraded.
                cache_control: Some(ephemeral_with_ttl(3600)),
            },
        ]);
        normalize_ttl_ordering(&mut req);
        let tools = req.tools.as_ref().unwrap();
        assert!(tools[0].cache_control.as_ref().unwrap().ttl.is_none());
        // ttl was stripped because a short block came first.
        assert!(tools[1].cache_control.as_ref().unwrap().ttl.is_none());
    }

    #[test]
    fn normalize_ttl_preserves_long_when_no_short_seen() {
        let mut req = req_with(vec![user_text("hi")]);
        req.tools = Some(vec![
            Tool {
                name: "a".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                cache_control: Some(ephemeral_with_ttl(3600)),
            },
            Tool {
                name: "b".into(),
                description: None,
                input_schema: serde_json::json!({"type":"object"}),
                cache_control: Some(ephemeral_with_ttl(3600)),
            },
        ]);
        normalize_ttl_ordering(&mut req);
        let tools = req.tools.as_ref().unwrap();
        assert_eq!(tools[0].cache_control.as_ref().unwrap().ttl, Some(3600));
        assert_eq!(tools[1].cache_control.as_ref().unwrap().ttl, Some(3600));
    }

    #[test]
    fn normalize_ttl_walks_in_evaluation_order() {
        // Long-TTL system block follows short-TTL tool → must be stripped.
        let mut req = req_with(vec![user_text("hi")]);
        req.tools = Some(vec![Tool {
            name: "a".into(),
            description: None,
            input_schema: serde_json::json!({"type":"object"}),
            cache_control: Some(ephemeral_marker()),
        }]);
        req.system = Some(SystemPrompt::Blocks(vec![TextBlock {
            r#type: "text".into(),
            text: "s".into(),
            cache_control: Some(ephemeral_with_ttl(3600)),
        }]));
        normalize_ttl_ordering(&mut req);
        let SystemPrompt::Blocks(blocks) = req.system.as_ref().unwrap() else {
            panic!()
        };
        assert!(blocks[0].cache_control.as_ref().unwrap().ttl.is_none());
    }

    // ── Helpers ──────────────────────────────────────────────────────────

    #[test]
    fn dyn_dispatch_compiles() {
        let _: Box<dyn CacheControlStrategy> = Box::new(DefaultCacheControlStrategy::default());
        let _: Box<dyn CacheControlStrategy> = Box::new(NoCacheControlStrategy);
    }
}