cpa-exec 0.4.2

Part of cliproxy: upstream provider executors
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
//! Anthropic shared-window vs model/entitlement rejection (helps/claude_ratelimit.go).

use cpa_core::gostr::GoStr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use cpa_core::exec::{ExecError, FailureScope};
use http::HeaderMap;

fn header(headers: &HeaderMap, name: &str) -> String {
    headers
        .get(name)
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default()
        .trim()
        .to_owned()
}
fn status(headers: &HeaderMap, window: &str) -> String {
    header(headers, &format!("anthropic-ratelimit-unified-{window}status")).to_ascii_lowercase()
}
fn allowed(status: &str) -> bool {
    matches!(status, "allowed" | "allowed_warning")
}
fn healthy(headers: &HeaderMap, window: &str) -> bool {
    header(headers, &format!("anthropic-ratelimit-unified-{window}-utilization"))
        .parse::<f64>()
        .is_ok_and(|n| n.is_finite() && (0.0..1.0).contains(&n))
}
fn overage_claim(headers: &HeaderMap) -> bool {
    header(headers, "anthropic-ratelimit-unified-representative-claim")
        .to_ascii_lowercase()
        .contains("overage")
}
fn overage_only(headers: &HeaderMap) -> bool {
    let five = status(headers, "5h-");
    let seven = status(headers, "7d-");
    if five == "rejected" || seven == "rejected" {
        return false;
    }
    let overage = status(headers, "7d_oi-") == "rejected"
        || status(headers, "overage-") == "rejected"
        || !header(headers, "anthropic-ratelimit-unified-overage-disabled-reason").is_empty()
        || overage_claim(headers);
    if !overage {
        return false;
    }
    if (allowed(&five) && allowed(&seven))
        || (allowed(&seven) && five.is_empty() && healthy(headers, "5h"))
        || (allowed(&five) && seven.is_empty() && healthy(headers, "7d"))
    {
        return true;
    }
    // v8.0.27 (8080967): Anthropic omits both shared-window statuses when it did not
    // evaluate them; only a rejected overage claim relies on that absence.
    let absent_or_healthy = |window: &str| {
        header(headers, &format!("anthropic-ratelimit-unified-{window}-utilization")).is_empty()
            || healthy(headers, window)
    };
    status(headers, "") == "rejected"
        && five.is_empty()
        && seven.is_empty()
        && overage_claim(headers)
        && absent_or_healthy("5h")
        && absent_or_healthy("7d")
}
/// `ClaudeHeadersIndicateUnifiedRateLimitRejection`.
pub(crate) fn shared_rejection(headers: &HeaderMap) -> bool {
    status(headers, "5h-") == "rejected"
        || status(headers, "7d-") == "rejected"
        || (status(headers, "") == "rejected" && !overage_only(headers))
}

/// `classifyClaudeUpstreamErrorWithCooling`. With `model_level_cooling` a shared-window
/// rejection cools only the model, like any other 429.
pub(crate) fn classify(mut error: ExecError, model_level_cooling: bool) -> ExecError {
    if (400..600).contains(&error.status) {
        error.retry_after = rate_limit_reset_for_error(&error.headers, &error.body);
    }
    if error.status == 429 {
        let value: serde_json::Value = serde_json::from_slice(&error.body).unwrap_or_default();
        let message = value["error"]["message"]
            .as_str()
            .filter(|s| !s.is_empty())
            .map(str::to_owned)
            .unwrap_or_else(|| String::from_utf8_lossy(&error.body).into_owned())
            .go_lower();
        error.scope = if !model_level_cooling && shared_rejection(&error.headers) {
            FailureScope::Credential
        } else if message.contains("fast request rejected")
            || (message.contains("fast")
                && (message.contains("usage credits") || message.contains("credits are required")))
        {
            FailureScope::Request
        } else {
            FailureScope::Model
        };
    }
    error
}

/// `ParseClaudeRateLimitResetForError` now, with Go's 1–30 s fuzz.
pub(crate) fn rate_limit_reset_for_error(headers: &HeaderMap, body: &[u8]) -> Option<Duration> {
    reset_for_error(headers, body, SystemTime::now(), fuzz())
}

/// [`reset`] with one exception: an explicit per-minute rate-limit error carries no
/// quota window, so an unclassified unified reset must not stretch its short
/// `Retry-After` into an hours-long cooldown. It applies only when the body is exactly
/// one per-minute message, no status header declares a quota window, and `Retry-After`
/// is one value inside the deadline filter's range.
pub(crate) fn reset_for_error(headers: &HeaderMap, body: &[u8], now: SystemTime, fuzz: Duration) -> Option<Duration> {
    let declares_window = QUOTA_STATUS.iter().any(|name| {
        headers
            .get_all(*name)
            .iter()
            .any(|v| !v.as_bytes().trim_ascii().is_empty())
    });
    let mut values = headers.get_all("retry-after").iter();
    let single = match (values.next(), values.next()) {
        (Some(value), None) => value.to_str().ok(),
        _ => None,
    };
    let short = single
        .and_then(|raw| retry_after(raw, now))
        .and_then(|t| t.duration_since(now).ok())
        .is_some_and(|d| !d.is_zero() && d <= MAX_WINDOW);
    if declares_window || !short || !per_minute_body(body) {
        return reset(headers, now, fuzz);
    }
    // Only the unclassified unified reset can stretch the cooldown here: each window's
    // own reset counts only when that window is rejected.
    let mut headers = headers.clone();
    headers.remove("anthropic-ratelimit-unified-reset");
    reset(&headers, now, fuzz)
}

/// `claudeQuotaStatusHeaders`: a non-empty value declares a quota window.
const QUOTA_STATUS: [&str; 5] = [
    "anthropic-ratelimit-unified-status",
    "anthropic-ratelimit-unified-5h-status",
    "anthropic-ratelimit-unified-7d-status",
    "anthropic-ratelimit-unified-7d_oi-status",
    "anthropic-ratelimit-unified-overage-status",
];

/// `claudeBodyIsExplicitPerMinuteRateLimit`: the body decodes as Go's
/// `{"error":{"type","message"}}` envelope with type `rate_limit_error` and exactly one
/// of the per-minute messages (case and surrounding whitespace aside).
fn per_minute_body(body: &[u8]) -> bool {
    const MESSAGES: [&str; 3] = [
        "this request would exceed the requests per minute rate limit. please try again later.",
        "this request would exceed the input tokens per minute rate limit. please try again later.",
        "this request would exceed the output tokens per minute rate limit. please try again later.",
    ];
    let Ok(serde_json::Value::Object(root)) = serde_json::from_slice::<serde_json::Value>(body) else {
        return false;
    };
    // encoding/json decodes every key that folds to a field name in order, merging
    // nested objects; null leaves a field unchanged and any other type fails the decode.
    // serde_json keeps only the last of two byte-identical keys, so `"error":{..},
    // "error":null` reads as null where Go keeps the object (DIFFERENCES-FROM-GO.md).
    let (mut kind, mut message) = ("", "");
    for error in go_fields(&root, "error") {
        let serde_json::Value::Object(error) = error else {
            if error.is_null() {
                continue;
            }
            return false;
        };
        for (slot, name) in [(&mut kind, "type"), (&mut message, "message")] {
            for value in go_fields(error, name) {
                match value {
                    serde_json::Value::String(s) => *slot = s,
                    serde_json::Value::Null => {}
                    _ => return false,
                }
            }
        }
    }
    kind == "rate_limit_error" && MESSAGES.contains(&message.trim().go_lower().as_str())
}

/// The values encoding/json decodes into the struct field `name`, in key order: keys
/// equal to it under Go's case folding (ASCII case, plus U+017F for `s` and U+212A for
/// `k`).
fn go_fields<'a>(
    object: &'a serde_json::Map<String, serde_json::Value>,
    name: &'a str,
) -> impl Iterator<Item = &'a serde_json::Value> + 'a {
    let fold = |c: char| match c {
        '\u{17F}' => 's',
        '\u{212A}' => 'k',
        c => c.to_ascii_lowercase(),
    };
    object
        .iter()
        .filter(move |(key, _)| key.chars().map(fold).eq(name.chars()))
        .map(|(_, v)| v)
}

fn fuzz() -> Duration {
    // Rejection sampling avoids modulo bias, matching crypto/rand.Int(30).
    loop {
        let mut byte = [0];
        if getrandom::fill(&mut byte).is_err() {
            return Duration::from_secs(1);
        }
        if byte[0] < 240 {
            return Duration::from_secs(u64::from(byte[0] % 30) + 1);
        }
    }
}

/// `maxClaudeRateLimitWindow` (v8.0.27, 8080967): the longest Anthropic window, 7 days,
/// plus an hour of clock skew. A later reset is a billing boundary, not a rate limit.
const MAX_WINDOW: Duration = Duration::from_secs(7 * 24 * 3600 + 3600);

/// `claudeUnifiedResetIsOverageBillingBoundary`: with an overage claim, a unified reset
/// equal to the overage reset is the billing period's end.
fn overage_billing_boundary(headers: &HeaderMap, unified_reset: &str) -> bool {
    overage_claim(headers)
        && timestamp(unified_reset).is_some_and(|unified| {
            timestamp(&header(headers, "anthropic-ratelimit-unified-overage-reset")) == Some(unified)
        })
}

pub(crate) fn reset(headers: &HeaderMap, now: SystemTime, fuzz: Duration) -> Option<Duration> {
    let overage = overage_only(headers);
    let five = status(headers, "5h-");
    let seven = status(headers, "7d-");
    let oi = status(headers, "7d_oi-");
    let unified = status(headers, "");
    let mut deadlines = Vec::new();
    if !overage && let Some(deadline) = retry_after(&header(headers, "retry-after"), now) {
        deadlines.push(deadline);
    }
    for (window, rejected) in [
        ("5h-", five == "rejected"),
        ("7d-", seven == "rejected"),
        ("7d_oi-", oi == "rejected" && !overage),
        (
            "",
            !overage
                && (unified == "rejected"
                    || five == "rejected"
                    || seven == "rejected"
                    || oi == "rejected"
                    || (unified.is_empty() && !allowed(&five) && !allowed(&seven))),
        ),
    ] {
        let raw = header(headers, &format!("anthropic-ratelimit-unified-{window}reset"));
        if rejected
            && !(window.is_empty() && overage_billing_boundary(headers, &raw))
            && let Some(deadline) = timestamp(&raw)
        {
            deadlines.push(deadline);
        }
    }
    deadlines
        .into_iter()
        .filter_map(|t| t.duration_since(now).ok())
        .filter(|d| !d.is_zero() && *d <= MAX_WINDOW)
        .max()
        .and_then(|d| d.checked_add(fuzz))
}

pub(crate) fn retry_after(raw: &str, now: SystemTime) -> Option<SystemTime> {
    if let Ok(seconds) = raw.trim().parse::<f64>()
        && seconds.is_finite()
        && seconds > 0.0
    {
        return Duration::try_from_secs_f64(seconds)
            .ok()
            .and_then(|d| now.checked_add(d));
    }
    timestamp_date(raw)
}
fn timestamp(raw: &str) -> Option<SystemTime> {
    if let Ok(seconds) = raw.trim().parse::<f64>()
        && seconds.is_finite()
        && seconds > 0.0
    {
        return Duration::try_from_secs_f64(seconds)
            .ok()
            .and_then(|d| UNIX_EPOCH.checked_add(d));
    }
    timestamp_date(raw)
}
fn timestamp_date(raw: &str) -> Option<SystemTime> {
    chrono::DateTime::parse_from_rfc3339(raw.trim())
        .ok()
        .map(SystemTime::from)
        .or_else(|| httpdate::parse_http_date(raw.trim()).ok())
}

/// Go `collectQuotaSignals("claude", headers)`: `Retry-After` and every
/// `Anthropic-Ratelimit-Unified-*` header under its canonical name with its last value,
/// trimmed; empty, oversized (over 512 bytes) and control-character values are dropped.
/// Both kinds rank first, so at most 64 are kept in name order.
pub fn claude_signals(headers: &HeaderMap) -> std::collections::BTreeMap<String, String> {
    const MAX_SIGNALS: usize = 64;
    const MAX_VALUE: usize = 512;
    let mut signals = std::collections::BTreeMap::new();
    for name in headers.keys() {
        let lower = name.as_str();
        if lower != "retry-after" && !lower.starts_with("anthropic-ratelimit-unified-") {
            continue;
        }
        let Some(value) = headers.get_all(name).iter().next_back() else {
            continue;
        };
        let value = String::from_utf8_lossy(value.as_bytes());
        let value = value.trim();
        if value.is_empty() || value.len() > MAX_VALUE || value.chars().any(|c| c < '\u{20}' || c == '\u{7f}') {
            continue;
        }
        signals.insert(crate::proxy::canonical_header(lower), value.to_owned());
    }
    signals.into_iter().take(MAX_SIGNALS).collect()
}

/// The latest passive quota snapshots per Claude credential and per model (Go
/// `QuotaState.ObserveResponseHeadersForProvider` for provider `claude` on `Auth.Quota`
/// and the result model's `ModelState.Quota`): each upstream Messages response that
/// carries a signal replaces both snapshots; one without leaves them untouched. The
/// store is Codex's; only the header rules differ.
// ponytail: snapshots stay until the process restarts, one per Claude credential ever
// used; Go drops them with the auth. Add a retain pass if credential churn ever matters.
#[derive(Default)]
pub struct Observations(crate::codex_quota::QuotaSignals);

impl Observations {
    /// The credential's snapshot only.
    pub fn observe(&self, credential_id: &str, headers: &HeaderMap) {
        self.observe_model(credential_id, "", headers);
    }

    /// The credential's and `model`'s snapshots (keyed by the model without its
    /// thinking suffix).
    pub fn observe_model(&self, credential_id: &str, model: &str, headers: &HeaderMap) {
        self.observe_at(credential_id, model, headers, SystemTime::now());
    }

    fn observe_at(&self, credential_id: &str, model: &str, headers: &HeaderMap, observed_at: SystemTime) {
        self.0
            .record(credential_id, model, claude_signals(headers), observed_at);
    }

    pub fn snapshot(&self, credential_id: &str) -> Option<crate::codex_quota::Snapshot> {
        self.0.snapshot(credential_id)
    }

    /// The latest snapshot per model (Go `model_quotas`), empty when none was observed.
    pub fn model_snapshots(
        &self,
        credential_id: &str,
    ) -> std::collections::BTreeMap<String, crate::codex_quota::Snapshot> {
        self.0.model_snapshots(credential_id)
    }
}

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

    fn headers(values: &[(&str, &str)]) -> HeaderMap {
        values
            .iter()
            .map(|(key, value)| {
                (
                    http::HeaderName::from_bytes(key.as_bytes()).unwrap(),
                    value.parse().unwrap(),
                )
            })
            .collect()
    }

    #[test]
    fn model_shared_and_fast_entitlement_scopes() {
        let mut error = ExecError::local(429, FailureScope::Credential, r#"{"error":{"message":"slow down"}}"#);
        assert_eq!(classify(error.clone(), false).scope, FailureScope::Model);
        error
            .headers
            .insert("anthropic-ratelimit-unified-5h-status", " Rejected ".parse().unwrap());
        assert_eq!(classify(error.clone(), false).scope, FailureScope::Credential);
        error.body = bytes::Bytes::from_static(br#"{"error":{"message":"Usage credits are required for fast mode"}}"#);
        assert_eq!(
            classify(error.clone(), false).scope,
            FailureScope::Credential,
            "shared window takes precedence"
        );
        error.headers.clear();
        assert_eq!(classify(error, false).scope, FailureScope::Request);
    }

    #[test]
    fn overage_only_excludes_retry_after_and_unhealthy_missing_windows_do_not() {
        let now = UNIX_EPOCH + Duration::from_secs(1000);
        let mut h = headers(&[
            ("anthropic-ratelimit-unified-status", "rejected"),
            ("anthropic-ratelimit-unified-7d-status", "allowed_warning"),
            ("anthropic-ratelimit-unified-5h-utilization", "0.0"),
            ("anthropic-ratelimit-unified-7d_oi-status", "rejected"),
            ("anthropic-ratelimit-unified-reset", "2000"),
            ("anthropic-ratelimit-unified-7d_oi-reset", "4000"),
            ("retry-after", "5000"),
        ]);
        assert!(!shared_rejection(&h));
        assert_eq!(reset(&h, now, Duration::ZERO), None);
        for utilization in ["NaN", "-0.1", "1", "inf", "bad"] {
            h.insert(
                "anthropic-ratelimit-unified-5h-utilization",
                utilization.parse().unwrap(),
            );
            assert!(shared_rejection(&h), "{utilization}");
            assert_eq!(reset(&h, now, Duration::from_secs(3)), Some(Duration::from_secs(5003)));
        }
    }

    #[test]
    fn latest_relevant_deadline_fractional_seconds_and_dates() {
        let now = UNIX_EPOCH + Duration::from_secs(1000);
        let h = headers(&[
            ("anthropic-ratelimit-unified-5h-status", "rejected"),
            ("anthropic-ratelimit-unified-5h-reset", "1030.5"),
            ("anthropic-ratelimit-unified-7d-status", "allowed"),
            ("anthropic-ratelimit-unified-7d-reset", "999999"),
            ("retry-after", "20.25"),
        ]);
        assert_eq!(
            reset(&h, now, Duration::from_secs(7)),
            Some(Duration::from_millis(37500))
        );
        let date = "Fri, 02 Oct 2026 12:00:00 GMT";
        let expected = httpdate::parse_http_date(date).unwrap();
        assert_eq!(retry_after(date, now), Some(expected));
        assert_eq!(timestamp("2026-10-02T12:00:00Z"), Some(expected));
        for bad in ["NaN", "inf", "-1", "0", "1e100", "wrong"] {
            assert_eq!(retry_after(bad, now), None);
        }
    }

    /// The Issue6465 cases of v8.0.27's claude_ratelimit_test.go (8080967): whether the
    /// shared windows rejected the credential, and the cooldown without fuzz.
    #[test]
    fn overage_billing_resets_and_far_deadlines_are_ignored_like_go() {
        let now = |rfc3339: &str| SystemTime::from(chrono::DateTime::parse_from_rfc3339(rfc3339).unwrap());
        let oct7 = now("2026-10-07T20:46:04Z");
        let unix = |t: SystemTime, after: u64| (t.duration_since(UNIX_EPOCH).unwrap().as_secs() + after).to_string();
        let five_hours = unix(oct7, 5 * 3600);
        let oct29 = now("2026-10-29T00:00:00Z");
        let billing = unix(oct29, 48 * 3600);
        let h = |pairs: &[(&str, &str)]| headers(pairs);
        let cases = [
            (
                "overage claim without shared windows",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-representative-claim", "overage"),
                    ("anthropic-ratelimit-unified-reset", "1793491200"),
                    ("anthropic-ratelimit-unified-overage-status", "allowed"),
                    ("anthropic-ratelimit-unified-overage-reset", "1793491200"),
                    ("anthropic-ratelimit-unified-overage-utilization", "0.11"),
                ]),
                oct7,
                false,
                None,
            ),
            (
                "rejected 5h window ignores the monthly unified reset",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-5h-status", "rejected"),
                    ("anthropic-ratelimit-unified-5h-reset", &five_hours),
                    ("anthropic-ratelimit-unified-reset", "1793491200"),
                    ("anthropic-ratelimit-unified-representative-claim", "five_hour"),
                ]),
                oct7,
                true,
                Some(5 * 3600),
            ),
            (
                "unified reset equal to the overage reset leaves Retry-After",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-representative-claim", "overage"),
                    ("anthropic-ratelimit-unified-7d-status", "allowed"),
                    ("anthropic-ratelimit-unified-7d-utilization", "0.69"),
                    ("anthropic-ratelimit-unified-reset", &billing),
                    ("anthropic-ratelimit-unified-overage-status", "rejected"),
                    ("anthropic-ratelimit-unified-overage-reset", &billing),
                    ("retry-after", "60"),
                ]),
                oct29,
                true,
                Some(60),
            ),
            (
                "five_hour claim with overage disabled stays credential-wide",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-representative-claim", "five_hour"),
                    (
                        "anthropic-ratelimit-unified-overage-disabled-reason",
                        "org_level_disabled",
                    ),
                    ("retry-after", "10800"),
                ]),
                oct7,
                true,
                Some(3 * 3600),
            ),
            (
                "overage claim with exhausted 5h utilization stays credential-wide",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-representative-claim", "overage"),
                    ("anthropic-ratelimit-unified-5h-utilization", "1.0"),
                    ("retry-after", "10800"),
                ]),
                oct7,
                true,
                Some(3 * 3600),
            ),
            (
                "overage claim with healthy utilization stays model-scoped",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-representative-claim", "overage"),
                    ("anthropic-ratelimit-unified-5h-utilization", "0.00"),
                    ("anthropic-ratelimit-unified-reset", "1793491200"),
                    ("anthropic-ratelimit-unified-overage-reset", "1793491200"),
                    ("retry-after", "10800"),
                ]),
                oct7,
                false,
                None,
            ),
            (
                "Retry-After beyond seven days",
                h(&[
                    ("anthropic-ratelimit-unified-status", "rejected"),
                    ("anthropic-ratelimit-unified-5h-status", "rejected"),
                    ("retry-after", "2034192"),
                ]),
                oct7,
                true,
                None,
            ),
            (
                "allowed unified status keeps a short Retry-After",
                h(&[
                    ("anthropic-ratelimit-unified-status", "allowed"),
                    ("anthropic-ratelimit-unified-representative-claim", "overage"),
                    ("anthropic-ratelimit-unified-overage-status", "allowed"),
                    ("retry-after", "30"),
                ]),
                oct7,
                false,
                Some(30),
            ),
            (
                "missing unified status keeps a short Retry-After",
                h(&[
                    ("anthropic-ratelimit-unified-representative-claim", "overage"),
                    ("anthropic-ratelimit-unified-overage-status", "allowed"),
                    ("retry-after", "30"),
                ]),
                oct7,
                false,
                Some(30),
            ),
        ];
        for (name, headers, now, shared, cooldown) in cases {
            assert_eq!(shared_rejection(&headers), shared, "{name}: shared rejection");
            assert_eq!(
                reset(&headers, now, Duration::ZERO),
                cooldown.map(Duration::from_secs),
                "{name}: cooldown"
            );
        }
    }

    /// TestQuotaStateObserveResponseHeadersRetainsMeasuredClaudeAndCodexWatermarks (the
    /// Claude half), quota_signals_test.go.
    #[test]
    fn claude_observation_keeps_go_measured_watermarks() {
        let measured = [
            ("Anthropic-Ratelimit-Unified-5h-Status", "allowed"),
            ("Anthropic-Ratelimit-Unified-5h-Utilization", "0.0"),
            ("Anthropic-Ratelimit-Unified-5h-Reset", "1787296800"),
            ("Anthropic-Ratelimit-Unified-7d-Status", "allowed"),
            ("Anthropic-Ratelimit-Unified-7d-Utilization", "0.53"),
            ("Anthropic-Ratelimit-Unified-7d-Reset", "1787695200"),
            ("Anthropic-Ratelimit-Unified-Fallback-Percentage", "0.5"),
            (
                "Anthropic-Ratelimit-Unified-Overage-Disabled-Reason",
                "member_zero_credit_limit",
            ),
            ("Anthropic-Ratelimit-Unified-Overage-Status", "rejected"),
            ("Anthropic-Ratelimit-Unified-Representative-Claim", "five_hour"),
            ("Anthropic-Ratelimit-Unified-Reset", "1787296800"),
            ("Anthropic-Ratelimit-Unified-Status", "allowed"),
        ];
        let mut all = measured.to_vec();
        all.push(("Anthropic-Workspace-Id", "workspace-must-not-be-quota-signal"));
        // Codex's signals are not Claude's (TestQuotaStateObserveResponseHeadersKeepsProviderScopedSignals).
        all.push(("X-Codex-Primary-Used-Percent", "2"));
        all.push(("Authorization", "Bearer secret"));
        let signals = claude_signals(&headers(&all));
        let want: std::collections::BTreeMap<String, String> = measured
            .iter()
            .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
            .collect();
        assert_eq!(signals, want);
    }

    /// TestObserveResponseHeadersReplacesStaleWatermarks,
    /// ...KeepsSnapshotWhenResponseCarriesNoSignal, ...AdvancesObservedAtOnRepeatedValues
    /// and ...RejectsControlCharacterValues, for Claude's headers; per model as in
    /// TestMarkResultQuotaFailureDoesNotEraseSiblingObservation.
    #[test]
    fn observations_replace_keep_advance_and_reject_like_go() {
        let at = |secs| UNIX_EPOCH + Duration::from_secs(secs);
        let obs = Observations::default();
        obs.observe_at(
            "c",
            "claude-a(high)",
            &headers(&[
                ("Retry-After", "120"),
                ("Anthropic-Ratelimit-Unified-Status", "rejected"),
            ]),
            at(100),
        );
        assert_eq!(obs.snapshot("c").unwrap().signals["Retry-After"], "120");
        obs.observe_at(
            "c",
            "claude-b",
            &headers(&[("Anthropic-Ratelimit-Unified-Status", "allowed")]),
            at(200),
        );
        let snap = obs.snapshot("c").unwrap();
        assert!(
            !snap.signals.contains_key("Retry-After"),
            "a stale Retry-After never survives"
        );
        assert_eq!(snap.signals["Anthropic-Ratelimit-Unified-Status"], "allowed");
        assert_eq!(snap.observed_at, at(200));
        // Each model keeps its own latest snapshot, keyed without the thinking suffix:
        // a response for one model never replaces a sibling's.
        let models = obs.model_snapshots("c");
        assert_eq!(models.keys().collect::<Vec<_>>(), ["claude-a", "claude-b"]);
        assert_eq!(models["claude-a"].signals["Retry-After"], "120");
        assert_eq!(models["claude-a"].observed_at, at(100));
        assert_eq!(
            models["claude-b"].signals["Anthropic-Ratelimit-Unified-Status"],
            "allowed"
        );
        // No signal: the previous snapshots stay.
        obs.observe_at(
            "c",
            "claude-b",
            &headers(&[("Content-Type", "application/json")]),
            at(300),
        );
        assert_eq!(obs.snapshot("c").unwrap().observed_at, at(200));
        assert_eq!(obs.model_snapshots("c")["claude-b"].observed_at, at(200));
        // Repeated values still advance the observation time.
        obs.observe_at(
            "c",
            "claude-b",
            &headers(&[("Anthropic-Ratelimit-Unified-Status", "allowed")]),
            at(400),
        );
        assert_eq!(obs.snapshot("c").unwrap().observed_at, at(400));
        // Control characters, empty and oversized values are never stored.
        let mut bad = headers(&[("Anthropic-Ratelimit-Unified-Status", "")]);
        bad.insert(
            "anthropic-ratelimit-unified-reset",
            // The only control byte an HTTP header value can carry.
            http::HeaderValue::from_bytes(b"1\tX-Injected: 1").unwrap(),
        );
        bad.insert("anthropic-ratelimit-unified-claim", "x".repeat(513).parse().unwrap());
        assert!(claude_signals(&bad).is_empty());
        assert!(obs.snapshot("other").is_none());
    }

    /// TestObserveResponseHeadersTruncatesDeterministically, for Claude: the first 64 names.
    #[test]
    fn claude_signals_cap_at_64_by_name() {
        let many: Vec<(String, String)> = (0..128)
            .map(|i| (format!("Anthropic-Ratelimit-Unified-L{i:03}-Status"), i.to_string()))
            .collect();
        let refs: Vec<(&str, &str)> = many.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
        let signals = claude_signals(&headers(&refs));
        assert_eq!(signals.len(), 64);
        assert!(signals.contains_key("Anthropic-Ratelimit-Unified-L000-Status"));
        assert!(signals.contains_key("Anthropic-Ratelimit-Unified-L063-Status"));
        assert!(!signals.contains_key("Anthropic-Ratelimit-Unified-L064-Status"));
    }
}