synaps-engine 0.11.1

Runtime engine — streaming, tools, MCP, skills, extensions, sidecar
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
//! gzip `Content-Encoding` for Anthropic request bodies.
//!
//! The Messages API is stateless: every request re-uploads the whole
//! conversation (tool results, file contents, diffs). Upload is the slow
//! direction of most home and campus links, and these bodies are JSON text
//! that gzips about 2x, so large requests sent straight to
//! `api.anthropic.com` go out gzip-encoded. The server decodes them before
//! anything reads them: tokens, billing and prompt caching are unchanged,
//! and the trace keeps digesting the JSON, not the wire bytes.
//!
//! Only gzip: the API refuses `br`, `zstd` and `deflate` request bodies.
//!
//! The level defaults to 1. On real session bodies it runs at about 250 MB/s
//! and keeps over 80% of level 6's saving. Level 6 runs at about 55 MB/s,
//! which costs more than it saves on uplinks faster than 200-375 Mbps; level
//! 1 breaks even between 750 Mbps and 1.3 Gbps. On a slow uplink the extra
//! CPU time is tens of milliseconds and the upload saved is seconds, so a
//! higher level wins there: against level 1, levels 3-6 pay off below
//! roughly 10-50 Mbps, depending on the body. Above 6 the body barely
//! shrinks further.
//!
//! Configured by `request_compression` (`auto` | `on` | `off`) and
//! `request_compression_level` (1–9), both in /settings. With `auto` (the
//! default) the body goes out uncompressed when any of these hold:
//! * the endpoint is not `https://api.anthropic.com` (a gateway set through
//!   `SYNAPS_ANTHROPIC_BASE_URL` may not decode request bodies);
//! * an HTTPS proxy is configured, or a custom CA bundle (`SSL_CERT_FILE`,
//!   `SSL_CERT_DIR`): TLS-inspecting middleboxes are the usual reason a
//!   compressed request body gets mangled;
//! * the body is smaller than [`MIN_BYTES`].
//!
//! `on` drops the endpoint, proxy and CA checks, for a proxy or gateway known
//! to pass gzip bodies; `off` never compresses. In every mode the body goes
//! out uncompressed when `SYNAPS_GZIP_REQUEST_BODIES` is `0`, `false`, `off`
//! or `no`, when an earlier request in this process showed gzip is refused
//! (below), or when compressing would not make it smaller.
//!
//! Refusal fallback: a gzip request answered 400 or 415 is resent once
//! uncompressed, without spending retry budget. If that resend succeeds, the
//! encoding was the problem and gzip is turned off for the rest of the
//! process. If the resend fails too, the error was real and gzip stays on,
//! so an ordinary 400 (prompt too long, a bad image) never disables it.

use std::io::Write;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};

use agent_core::config::{
    RequestCompressionMode, DEFAULT_REQUEST_COMPRESSION_LEVEL, MAX_REQUEST_COMPRESSION_LEVEL,
};
use bytes::Bytes;
use reqwest::RequestBuilder;

/// Bodies below this go out uncompressed: the upload saved is a few ms at
/// most, and it keeps small requests (titles, probes) byte-for-byte as before.
pub const MIN_BYTES: usize = 16 * 1024;

/// Kill switch. Off on `0`, `false`, `off`, `no`; anything else is ignored.
pub const ENV_SWITCH: &str = "SYNAPS_GZIP_REQUEST_BODIES";

const ANTHROPIC_HOST: &str = "api.anthropic.com";

/// Proxy variables reqwest honours for an `https://` URL, plus the CA bundle
/// overrides rustls-native-certs reads. Upper and lower case, as reqwest
/// and curl both accept either.
const SKIP_VARS: &[&str] = &[
    "HTTPS_PROXY",
    "https_proxy",
    "ALL_PROXY",
    "all_proxy",
    "SSL_CERT_FILE",
    "SSL_CERT_DIR",
];

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
enum Mode {
    /// gzip when every condition in the module docs holds.
    #[default]
    Auto,
    /// gzip whatever the endpoint, proxy or CA bundle.
    On,
    /// Never compress.
    Off,
    /// Compress every body, whatever the host, size or environment. Tests
    /// point requests at loopback servers, which `Auto` never compresses.
    #[cfg(test)]
    Always,
}

/// Request-body compression policy for the Anthropic transports. Lives on
/// `ApiOptions`; the default is `Auto` with the process-wide refusal latch.
#[derive(Debug, Clone)]
pub struct RequestCompression {
    mode: Mode,
    /// gzip level, 1–9.
    level: u32,
    /// Set once the API has accepted a request only uncompressed. Whether
    /// gzip survives the network path is a property of the machine, not the
    /// session, so production shares one latch across the process.
    refused: Arc<AtomicBool>,
}

fn process_latch() -> &'static Arc<AtomicBool> {
    static LATCH: OnceLock<Arc<AtomicBool>> = OnceLock::new();
    LATCH.get_or_init(|| Arc::new(AtomicBool::new(false)))
}

impl Default for RequestCompression {
    fn default() -> Self {
        Self::from_config(
            RequestCompressionMode::default(),
            DEFAULT_REQUEST_COMPRESSION_LEVEL,
        )
    }
}

impl RequestCompression {
    /// The policy for `request_compression` / `request_compression_level`,
    /// sharing the process-wide refusal latch. The level is clamped to 1–9.
    pub fn from_config(mode: RequestCompressionMode, level: u32) -> Self {
        Self {
            mode: match mode {
                RequestCompressionMode::Auto => Mode::Auto,
                RequestCompressionMode::On => Mode::On,
                RequestCompressionMode::Off => Mode::Off,
            },
            level: level.clamp(1, MAX_REQUEST_COMPRESSION_LEVEL),
            refused: Arc::clone(process_latch()),
        }
    }

    /// Compress every body, with a latch of its own. For loopback tests.
    #[cfg(test)]
    pub fn always() -> Self {
        Self {
            mode: Mode::Always,
            level: DEFAULT_REQUEST_COMPRESSION_LEVEL,
            refused: Arc::new(AtomicBool::new(false)),
        }
    }

    /// `(mode, level)` as configured; `None` for the test-only `always()`.
    #[cfg(test)]
    pub(crate) fn configured(&self) -> (Option<RequestCompressionMode>, u32) {
        let mode = match self.mode {
            Mode::Auto => Some(RequestCompressionMode::Auto),
            Mode::On => Some(RequestCompressionMode::On),
            Mode::Off => Some(RequestCompressionMode::Off),
            Mode::Always => None,
        };
        (mode, self.level)
    }

    /// True once a refusal has turned gzip off.
    pub fn refused(&self) -> bool {
        self.refused.load(Ordering::Relaxed)
    }

    fn wants_gzip(&self, url: &str, len: usize, env: impl Fn(&str) -> Option<String>) -> bool {
        if self.refused() {
            return false;
        }
        match self.mode {
            #[cfg(test)]
            Mode::Always => true,
            Mode::Off => false,
            Mode::On => len >= MIN_BYTES && !switched_off(&env),
            Mode::Auto => {
                len >= MIN_BYTES
                    && !switched_off(&env)
                    && is_direct_anthropic(url)
                    && !SKIP_VARS
                        .iter()
                        .any(|&k| env(k).is_some_and(|v| !v.trim().is_empty()))
            }
        }
    }

    /// The wire form of `raw` for a request to `url`. Compression runs on the
    /// blocking pool (about 4 ms per MB); if it fails, or would not shrink
    /// the body, the body goes out uncompressed.
    pub(super) async fn encode(&self, url: &str, raw: &Bytes) -> WireBody {
        if !self.wants_gzip(url, raw.len(), |k| std::env::var(k).ok()) {
            return WireBody::identity(raw);
        }
        let input = raw.clone();
        let level = self.level;
        let started = std::time::Instant::now();
        match tokio::task::spawn_blocking(move || gzip(&input, level)).await {
            Ok(Ok(out)) if out.len() < raw.len() => {
                tracing::debug!(
                    level,
                    raw_bytes = raw.len(),
                    wire_bytes = out.len(),
                    elapsed_us = started.elapsed().as_micros() as u64,
                    "gzip request body"
                );
                WireBody {
                    bytes: out.into(),
                    gzip: true,
                    resend: false,
                }
            }
            Ok(Ok(_)) => WireBody::identity(raw),
            Ok(Err(e)) => {
                tracing::warn!(error = %e, "gzip request body failed; sending uncompressed");
                WireBody::identity(raw)
            }
            Err(e) => {
                tracing::warn!(error = %e, "gzip request body task failed; sending uncompressed");
                WireBody::identity(raw)
            }
        }
    }

    /// After a non-success status to an attempt sent as `wire`: the body to
    /// resend when the server may have refused the encoding. The caller
    /// resends it once without spending retry budget; `None` means handle the
    /// error as usual.
    pub(super) fn resend_uncompressed(&self, wire: &WireBody, raw: &Bytes, status: u16) -> Option<WireBody> {
        if !wire.gzip || !matches!(status, 400 | 415) {
            return None;
        }
        tracing::warn!(status, "gzip request body refused; resending it uncompressed once");
        Some(WireBody {
            bytes: raw.clone(),
            gzip: false,
            resend: true,
        })
    }

    /// Record the status of an attempt sent as `wire`. When that attempt was
    /// the uncompressed resend and it succeeded, the encoding was the problem:
    /// gzip goes off for the rest of the process.
    pub(super) fn settle(&self, wire: &mut WireBody, success: bool) {
        if !std::mem::take(&mut wire.resend) {
            return;
        }
        if success && !self.refused.swap(true, Ordering::Relaxed) {
            tracing::warn!(
                "{ANTHROPIC_HOST} accepted a request only uncompressed; sending request bodies uncompressed for the rest of this process"
            );
        }
    }
}

/// The bytes handed to reqwest for one request, and how they are encoded.
#[derive(Debug, Clone)]
pub(super) struct WireBody {
    pub(super) bytes: Bytes,
    pub(super) gzip: bool,
    /// This is the uncompressed resend after a refusal; its outcome decides
    /// the latch (see [`RequestCompression::settle`]).
    resend: bool,
}

impl WireBody {
    pub(super) fn identity(raw: &Bytes) -> Self {
        Self {
            bytes: raw.clone(),
            gzip: false,
            resend: false,
        }
    }

    /// Adds `Content-Encoding: gzip` when the body is compressed.
    pub(super) fn encoding_header(&self, req: RequestBuilder) -> RequestBuilder {
        if self.gzip {
            req.header(reqwest::header::CONTENT_ENCODING, "gzip")
        } else {
            req
        }
    }
}

fn switched_off(env: &impl Fn(&str) -> Option<String>) -> bool {
    env(ENV_SWITCH).is_some_and(|v| {
        matches!(
            v.trim().to_ascii_lowercase().as_str(),
            "0" | "false" | "off" | "no"
        )
    })
}

fn is_direct_anthropic(url: &str) -> bool {
    reqwest::Url::parse(url).is_ok_and(|u| {
        u.scheme() == "https"
            && u.host_str() == Some(ANTHROPIC_HOST)
            && u.port_or_known_default() == Some(443)
    })
}

fn gzip(raw: &[u8], level: u32) -> std::io::Result<Vec<u8>> {
    let mut enc = flate2::write::GzEncoder::new(
        Vec::with_capacity(raw.len() / 2),
        flate2::Compression::new(level),
    );
    enc.write_all(raw)?;
    enc.finish()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::io::Read;

    const API: &str = "https://api.anthropic.com/v1/messages";

    fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
        let map: HashMap<String, String> = pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        move |k| map.get(k).cloned()
    }

    /// A production policy with a latch of its own.
    fn policy(mode: RequestCompressionMode, level: u32) -> RequestCompression {
        RequestCompression {
            refused: Arc::new(AtomicBool::new(false)),
            ..RequestCompression::from_config(mode, level)
        }
    }

    fn auto() -> RequestCompression {
        policy(RequestCompressionMode::Auto, DEFAULT_REQUEST_COMPRESSION_LEVEL)
    }

    fn gunzip(b: &[u8]) -> Vec<u8> {
        let mut out = Vec::new();
        flate2::read::GzDecoder::new(b).read_to_end(&mut out).unwrap();
        out
    }

    /// A body shaped like a real request: repetitive JSON well above the
    /// threshold.
    fn big_body() -> Bytes {
        let msgs: Vec<serde_json::Value> = (0..400)
            .map(|i| serde_json::json!({"role": "user", "content": [{"type": "text", "text": format!("turn {i}: cargo test --workspace output line")}]}))
            .collect();
        serde_json::to_vec(&serde_json::json!({"model": "claude-sonnet-4-6", "messages": msgs}))
            .unwrap()
            .into()
    }

    #[test]
    fn auto_compresses_large_bodies_to_the_anthropic_api_only() {
        let c = auto();
        let none = env(&[]);
        assert!(c.wants_gzip(API, MIN_BYTES, &none));
        assert!(!c.wants_gzip(API, MIN_BYTES - 1, &none), "below threshold");
        for url in [
            "http://api.anthropic.com/v1/messages",
            "https://api.anthropic.com:8443/v1/messages",
            "https://gateway.example.com/v1/messages",
            "https://api.anthropic.com.example.com/v1/messages",
            "http://127.0.0.1:4000/v1/messages",
            "not a url",
        ] {
            assert!(!c.wants_gzip(url, 1 << 20, &none), "{url}");
        }
        assert!(c.wants_gzip("https://api.anthropic.com:443/v1/messages", 1 << 20, &none));
    }

    #[test]
    fn proxies_custom_cas_and_the_switch_turn_it_off() {
        let c = auto();
        for &var in SKIP_VARS {
            assert!(!c.wants_gzip(API, 1 << 20, env(&[(var, "http://proxy:3128")])), "{var}");
        }
        // Set but empty is unset, as reqwest treats it.
        assert!(c.wants_gzip(API, 1 << 20, env(&[("HTTPS_PROXY", " ")])));
        // HTTP_PROXY never applies to an https:// URL.
        assert!(c.wants_gzip(API, 1 << 20, env(&[("HTTP_PROXY", "http://proxy:3128")])));
        for off in ["0", "false", "OFF", " no "] {
            assert!(!c.wants_gzip(API, 1 << 20, env(&[(ENV_SWITCH, off)])), "{off}");
        }
        for on in ["1", "true", "yes", ""] {
            assert!(c.wants_gzip(API, 1 << 20, env(&[(ENV_SWITCH, on)])), "{on:?}");
        }
    }

    #[test]
    fn on_skips_endpoint_proxy_and_ca_checks_but_not_the_switch() {
        let on = policy(RequestCompressionMode::On, 1);
        let none = env(&[]);
        assert!(on.wants_gzip("https://gateway.example.com/v1/messages", 1 << 20, &none));
        assert!(on.wants_gzip("http://127.0.0.1:4000/v1/messages", 1 << 20, &none));
        for &var in SKIP_VARS {
            assert!(on.wants_gzip(API, 1 << 20, env(&[(var, "x")])), "{var}");
        }
        assert!(!on.wants_gzip(API, MIN_BYTES - 1, &none), "threshold still applies");
        assert!(!on.wants_gzip(API, 1 << 20, env(&[(ENV_SWITCH, "0")])), "the env switch wins");

        let off = policy(RequestCompressionMode::Off, 9);
        assert!(!off.wants_gzip(API, 1 << 20, &none));
    }

    #[test]
    fn config_level_is_clamped_and_shares_the_process_latch() {
        let at = |level| RequestCompression::from_config(RequestCompressionMode::Auto, level).level;
        assert_eq!(at(0), 1);
        assert_eq!(at(6), 6);
        assert_eq!(at(42), MAX_REQUEST_COMPRESSION_LEVEL);
        assert_eq!(RequestCompression::default().level, DEFAULT_REQUEST_COMPRESSION_LEVEL);
        assert_eq!(RequestCompression::default().mode, Mode::Auto);
        assert!(Arc::ptr_eq(
            &RequestCompression::from_config(RequestCompressionMode::On, 9).refused,
            process_latch()
        ));
    }

    #[test]
    fn the_runtime_takes_the_policy_from_config_and_passes_it_on() {
        let mut rt = crate::runtime::Runtime::new_headless();
        assert_eq!(rt.request_compression.mode, Mode::Auto);
        assert_eq!(rt.request_compression.level, DEFAULT_REQUEST_COMPRESSION_LEVEL);
        rt.apply_config(&agent_core::config::SynapsConfig {
            request_compression: RequestCompressionMode::On,
            request_compression_level: 6,
            ..Default::default()
        });
        assert_eq!(rt.request_compression.mode, Mode::On);
        assert_eq!(rt.request_compression.level, 6);
        // Subagents are cloned runtimes: same policy, same process latch.
        let child = rt.clone();
        assert_eq!((child.request_compression.mode, child.request_compression.level), (Mode::On, 6));
        assert!(Arc::ptr_eq(&child.request_compression.refused, process_latch()));
    }

    #[tokio::test]
    async fn a_higher_level_shrinks_more() {
        // Varied text, so the levels' match search actually differs.
        const WORDS: &[&str] = &[
            "fn", "let", "mut", "self", "impl", "match", "Some", "None", "Ok", "Err", "await",
            "async", "return", "struct", "enum", "pub", "use", "crate", "error:", "warning:",
            "test", "passed", "failed", "src/runtime/api.rs", "line", "=>", "{", "}", "(", ")",
        ];
        let mut x: u64 = 0x2545_f491_4f6c_dd1d;
        let text: Vec<&str> = (0..20_000)
            .map(|_| {
                x ^= x << 13;
                x ^= x >> 7;
                x ^= x << 17;
                WORDS[(x % WORDS.len() as u64) as usize]
            })
            .collect();
        let body = serde_json::json!({"messages": [{"role": "user", "content": text.join(" ")}]});
        let raw: Bytes = serde_json::to_vec(&body).unwrap().into();
        let fast = policy(RequestCompressionMode::On, 1).encode(API, &raw).await;
        let best = policy(RequestCompressionMode::On, 9).encode(API, &raw).await;
        assert!(fast.gzip && best.gzip);
        let (best_len, fast_len) = (best.bytes.len(), fast.bytes.len());
        assert!(best_len < fast_len, "level 9 {best_len} vs level 1 {fast_len}");
        assert_eq!(gunzip(&best.bytes), raw.as_ref());
    }

    #[test]
    fn modes_and_the_latch() {
        let none = env(&[]);
        let always = RequestCompression::always();
        assert!(always.wants_gzip("http://127.0.0.1:1/v1/messages", 1, &none));
        always.refused.store(true, Ordering::Relaxed);
        assert!(!always.wants_gzip("http://127.0.0.1:1/v1/messages", 1, &none));
        // `always()` latches are independent of each other and of the process.
        assert!(!RequestCompression::always().refused());
        assert!(Arc::ptr_eq(
            &RequestCompression::default().refused,
            &RequestCompression::default().refused
        ));
    }

    #[tokio::test]
    async fn encode_round_trips_and_shrinks() {
        let raw = big_body();
        let wire = RequestCompression::always().encode(API, &raw).await;
        assert!(wire.gzip);
        assert!(wire.bytes.len() * 2 < raw.len(), "{} -> {}", raw.len(), wire.bytes.len());
        assert_eq!(gunzip(&wire.bytes), raw.as_ref());

        // Production policy, loopback URL: never compressed.
        let plain = auto().encode("http://127.0.0.1:1/v1/messages", &raw).await;
        assert!(!plain.gzip);
        assert_eq!(plain.bytes, raw);
    }

    #[tokio::test]
    async fn incompressible_bodies_go_out_as_they_are() {
        // Random bytes (what an already-compressed image looks like) do not
        // shrink, so the body goes out as it is.
        let mut x: u64 = 0x9e37_79b9_7f4a_7c15;
        let raw: Bytes = (0..64 * 1024)
            .map(|_| {
                x ^= x << 13;
                x ^= x >> 7;
                x ^= x << 17;
                x as u8
            })
            .collect::<Vec<u8>>()
            .into();
        let wire = RequestCompression::always().encode(API, &raw).await;
        assert!(!wire.gzip);
        assert_eq!(wire.bytes, raw);
    }

    #[tokio::test]
    async fn refusal_latches_only_when_the_uncompressed_resend_succeeds() {
        let raw = big_body();
        for (status, success, latched) in [(400, true, true), (415, true, true), (400, false, false)] {
            let c = RequestCompression::always();
            let wire = c.encode(API, &raw).await;
            let mut resend = c.resend_uncompressed(&wire, &raw, status).expect("resend");
            assert!(!resend.gzip);
            assert_eq!(resend.bytes, raw);
            c.settle(&mut resend, success);
            assert_eq!(c.refused(), latched, "{status} success={success}");
            // Settling is one-shot: a later success on the same body is not
            // evidence about the encoding.
            c.settle(&mut resend, true);
            assert_eq!(c.refused(), latched);
        }
    }

    #[tokio::test]
    async fn no_resend_for_other_statuses_or_uncompressed_requests() {
        let raw = big_body();
        let c = RequestCompression::always();
        let wire = c.encode(API, &raw).await;
        for status in [401, 403, 404, 413, 422, 429, 500, 529] {
            assert!(c.resend_uncompressed(&wire, &raw, status).is_none(), "{status}");
        }
        let plain = WireBody::identity(&raw);
        assert!(c.resend_uncompressed(&plain, &raw, 400).is_none());
        let mut w = wire.clone();
        c.settle(&mut w, true);
        assert!(!c.refused(), "a normal success never latches");
    }
}

/// End to end through the real Anthropic transports, against a loopback
/// server that decodes gzip request bodies the way the API does.
#[cfg(test)]
mod transport_tests {
    use super::RequestCompression;
    use crate::runtime::api::{ApiMethods, ApiOptions};
    use crate::runtime::telemetry::TelemetryLevel;
    use crate::runtime::types::AuthState;
    use crate::{StreamEvent, ToolRegistry};
    use axum::body::Bytes;
    use axum::http::{HeaderMap, StatusCode};
    use axum::response::IntoResponse;
    use axum::routing::post as axum_post;
    use axum::Router;
    use serde_json::{json, Value};
    use std::io::Read;
    use std::sync::{Arc, Mutex};
    use tokio::sync::{mpsc, RwLock};

    /// Per request: the `Content-Encoding` header and the decoded JSON body.
    type Seen = Arc<Mutex<Vec<(Option<String>, Value)>>>;

    #[derive(Clone, Copy)]
    enum Server {
        Accepts,
        /// 400 to every gzip body: a middlebox mangles them, say.
        RefusesGzip,
        /// 400 to everything: a genuinely bad request.
        RefusesAll,
    }

    const SSE: &str = concat!(
        "data: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_1\",\"type\":\"message\",",
        "\"role\":\"assistant\",\"content\":[],\"model\":\"claude-sonnet-4-6\",\"stop_reason\":null,",
        "\"usage\":{\"input_tokens\":10,\"output_tokens\":1}}}\n\n",
        "data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n",
        "data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"ok\"}}\n\n",
        "data: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
        "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":2}}\n\n",
        "data: {\"type\":\"message_stop\"}\n\n",
    );

    async fn server(mode: Server) -> (String, Seen) {
        let seen: Seen = Arc::new(Mutex::new(Vec::new()));
        let cap = Arc::clone(&seen);
        let app = Router::new().route(
            "/v1/messages",
            axum_post(move |headers: HeaderMap, body: Bytes| {
                let cap = Arc::clone(&cap);
                async move {
                    let encoding = headers
                        .get("content-encoding")
                        .and_then(|v| v.to_str().ok())
                        .map(str::to_string);
                    let raw = match encoding.as_deref() {
                        Some("gzip") => {
                            let mut out = Vec::new();
                            flate2::read::GzDecoder::new(&body[..])
                                .read_to_end(&mut out)
                                .expect("valid gzip");
                            out
                        }
                        None => body.to_vec(),
                        Some(other) => panic!("unexpected encoding {other}"),
                    };
                    let json: Value = serde_json::from_slice(&raw).expect("JSON body");
                    let stream = json["stream"].as_bool().unwrap_or(false);
                    let refuse = match mode {
                        Server::Accepts => false,
                        Server::RefusesGzip => encoding.is_some(),
                        Server::RefusesAll => true,
                    };
                    cap.lock().unwrap().push((encoding, json));
                    if refuse {
                        return (
                            StatusCode::BAD_REQUEST,
                            r#"{"type":"error","error":{"type":"invalid_request_error","message":"bad request"}}"#,
                        )
                            .into_response();
                    }
                    if stream {
                        (StatusCode::OK, [("content-type", "text/event-stream")], SSE)
                            .into_response()
                    } else {
                        (
                            StatusCode::OK,
                            [("content-type", "application/json")],
                            json!({
                                "content": [{"type": "text", "text": "summary"}],
                                "stop_reason": "end_turn",
                                "usage": {"input_tokens": 3, "output_tokens": 1}
                            })
                            .to_string(),
                        )
                            .into_response()
                    }
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        (format!("http://{addr}"), seen)
    }

    fn auth() -> Arc<RwLock<AuthState>> {
        Arc::new(RwLock::new(AuthState {
            auth_token: "sk-test".into(),
            auth_type: "api_key".into(),
            refresh_token: None,
            token_expires: Some(9_999_999_999_999),
            auth_file_mtime: None,
            bound_credential: None,
        }))
    }

    /// A conversation big enough to look like a real one.
    fn messages() -> Vec<crate::SharedMessage> {
        let log = "test runtime::request_encoding::tests ... ok\n".repeat(800);
        vec![Arc::new(json!({"role": "user", "content": format!("why did this pass?\n{log}")}))]
    }

    fn options(url: &str, compression: RequestCompression) -> ApiOptions {
        ApiOptions {
            anthropic_base_url: Some(url.to_string()),
            request_compression: compression,
            ..Default::default()
        }
    }

    async fn stream_turn(options: &ApiOptions) -> crate::error::Result<Value> {
        let (tx, _rx) = mpsc::unbounded_channel::<StreamEvent>();
        ApiMethods::call_api_stream(
            &auth(),
            &reqwest::Client::new(),
            "claude-sonnet-4-6",
            &ToolRegistry::new(),
            &None,
            0,
            agent_core::reasoning::ReasoningLevel::Adaptive,
            &messages(),
            tx,
            0, // max_retries: the fallback must not depend on the error budget
            0,
            options,
            TelemetryLevel::Off,
        )
        .await
    }

    fn encodings(seen: &Seen) -> Vec<Option<String>> {
        seen.lock().unwrap().iter().map(|(e, _)| e.clone()).collect()
    }

    #[tokio::test]
    async fn streaming_body_goes_out_gzip_and_decodes_to_the_identity_json() {
        let (url, seen) = server(Server::Accepts).await;
        stream_turn(&options(&url, RequestCompression::always()))
            .await
            .unwrap();
        // The production policy never compresses for a loopback URL.
        stream_turn(&options(&url, RequestCompression::default()))
            .await
            .unwrap();
        assert_eq!(encodings(&seen), [Some("gzip".to_string()), None]);
        let seen = seen.lock().unwrap();
        assert_eq!(seen[0].1, seen[1].1, "same request either way");
    }

    #[tokio::test]
    async fn refused_gzip_is_resent_uncompressed_then_stays_off() {
        let (url, seen) = server(Server::RefusesGzip).await;
        let c = RequestCompression::always();
        let opts = options(&url, c.clone());
        stream_turn(&opts)
            .await
            .expect("a gzip refusal must never fail the turn");
        assert!(c.refused(), "latched off once the plain resend went through");
        stream_turn(&opts).await.unwrap();
        assert_eq!(
            encodings(&seen),
            [Some("gzip".to_string()), None, None],
            "refused + resend + next turn"
        );
        let seen = seen.lock().unwrap();
        assert_eq!(seen[0].1, seen[1].1, "the resend is the same request");
    }

    #[tokio::test]
    async fn a_genuine_400_still_fails_and_leaves_gzip_on() {
        let (url, seen) = server(Server::RefusesAll).await;
        let c = RequestCompression::always();
        let err = stream_turn(&options(&url, c.clone())).await.unwrap_err();
        assert!(err.to_string().contains("400"), "{err}");
        assert_eq!(encodings(&seen), [Some("gzip".to_string()), None]);
        assert!(!c.refused(), "the plain resend failed too: not the encoding");
    }

    /// The window clamp is wired into all three transports: a model that
    /// rejects input + `max_tokens` over the window (Sonnet 4) gets the room
    /// its ~160k-token input leaves of 200k; a Claude 4.5+ model gets its
    /// full 64k, the same body as before the clamp existed.
    #[tokio::test]
    async fn every_transport_clamps_max_tokens_only_for_models_that_reject_overflow() {
        let near = vec![Arc::new(json!({"role": "user", "content": "x".repeat(400_000)}))
            as crate::SharedMessage];
        for (model, clamped) in [("claude-sonnet-4-20250514", true), ("claude-sonnet-4-5-20250929", false)] {
            let (url, seen) = server(Server::Accepts).await;
            let opts = options(&url, RequestCompression::default());
            let (tx, _rx) = mpsc::unbounded_channel::<StreamEvent>();
            ApiMethods::call_api_stream(
                &auth(),
                &reqwest::Client::new(),
                model,
                &ToolRegistry::new(),
                &None,
                0,
                agent_core::reasoning::ReasoningLevel::Adaptive,
                &near,
                tx,
                0,
                0,
                &opts,
                TelemetryLevel::Off,
            )
            .await
            .expect("stream");
            ApiMethods::call_api(
                &auth(),
                &reqwest::Client::new(),
                model,
                &ToolRegistry::new(),
                &None,
                0,
                agent_core::reasoning::ReasoningLevel::Adaptive,
                &near,
                0,
                &opts,
            )
            .await
            .expect("sync");
            ApiMethods::call_api_simple(
                &auth(),
                &reqwest::Client::new(),
                model,
                "summarize",
                0,
                agent_core::reasoning::ReasoningLevel::Adaptive,
                &near,
                0,
                &opts,
            )
            .await
            .expect("compaction");
            let seen = seen.lock().unwrap();
            assert_eq!(seen.len(), 3, "{model}");
            for (_, body) in seen.iter() {
                let max = body["max_tokens"].as_u64().unwrap();
                assert_eq!(body["thinking"]["budget_tokens"], 16_384, "{model}");
                if clamped {
                    // 200k − ~160k of messages − the tool schemas (none on
                    // the compaction path), above the thinking floor.
                    assert!((20_480..40_000).contains(&max), "{model}: {max}");
                } else {
                    assert_eq!(max, 64_000, "{model}");
                }
            }
        }
    }

    /// `thinking_display = omitted` reaches the streaming and the
    /// non-streaming turn; compaction (an internal summary) stays summarized.
    #[tokio::test]
    async fn thinking_display_reaches_both_turn_transports_not_compaction() {
        let (url, seen) = server(Server::Accepts).await;
        let opts = ApiOptions {
            thinking_display: crate::config::ThinkingDisplay::Omitted,
            ..options(&url, RequestCompression::default())
        };
        let level = agent_core::reasoning::ReasoningLevel::High;
        let model = "claude-opus-4-7";
        let (tx, _rx) = mpsc::unbounded_channel::<StreamEvent>();
        ApiMethods::call_api_stream(
            &auth(), &reqwest::Client::new(), model, &ToolRegistry::new(), &None, 0, level,
            &messages(), tx, 0, 0, &opts, TelemetryLevel::Off,
        )
        .await
        .expect("stream");
        ApiMethods::call_api(
            &auth(), &reqwest::Client::new(), model, &ToolRegistry::new(), &None, 0, level,
            &messages(), 0, &opts,
        )
        .await
        .expect("sync");
        ApiMethods::call_api_simple(
            &auth(), &reqwest::Client::new(), model, "summarize", 0, level, &messages(), 0, &opts,
        )
        .await
        .expect("compaction");
        let seen = seen.lock().unwrap();
        let displays: Vec<&Value> = seen.iter().map(|(_, b)| &b["thinking"]["display"]).collect();
        assert_eq!(displays, [&json!("omitted"), &json!("omitted"), &json!("summarized")]);
    }

    #[tokio::test]
    async fn sync_and_compaction_paths_compress_and_fall_back_too() {
        let (url, seen) = server(Server::RefusesGzip).await;
        let c = RequestCompression::always();
        let opts = options(&url, c.clone());
        ApiMethods::call_api(
            &auth(),
            &reqwest::Client::new(),
            "claude-sonnet-4-6",
            &ToolRegistry::new(),
            &None,
            0,
            agent_core::reasoning::ReasoningLevel::Adaptive,
            &messages(),
            0,
            &opts,
        )
        .await
        .expect("sync call survives the refusal");
        assert!(c.refused());

        let (url, seen2) = server(Server::RefusesGzip).await;
        let c = RequestCompression::always();
        let text = ApiMethods::call_api_simple(
            &auth(),
            &reqwest::Client::new(),
            "claude-haiku-4-5",
            "summarize",
            0,
            agent_core::reasoning::ReasoningLevel::Adaptive,
            &messages(),
            0,
            &options(&url, c.clone()),
        )
        .await
        .expect("compaction survives the refusal");
        assert_eq!(text, "summary");
        assert!(c.refused());
        for seen in [&seen, &seen2] {
            assert_eq!(encodings(seen), [Some("gzip".to_string()), None]);
        }
    }
}