adk-model 3.0.0

LLM model integrations for Rust Agent Development Kit (ADK-Rust) (Gemini, OpenAI, Claude, DeepSeek, etc.)
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
use adk_core::{AdkError, ErrorCategory, Result};
use std::{future::Future, time::Duration};

/// Configuration for automatic retry with exponential backoff.
#[derive(Clone, Debug)]
pub struct RetryConfig {
    /// Whether retries are enabled.
    pub enabled: bool,
    /// Maximum number of retry attempts before giving up.
    pub max_retries: u32,
    /// Initial delay before the first retry.
    pub initial_delay: Duration,
    /// Maximum exponential backoff delay.
    pub max_delay: Duration,
    /// Largest server-provided delay this executor will wait for.
    /// A larger delay returns the original failure without another attempt.
    pub max_server_delay: Duration,
    /// Multiplier applied to the delay after each retry.
    pub backoff_multiplier: f32,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            max_retries: 3,
            initial_delay: Duration::from_millis(250),
            max_delay: Duration::from_secs(5),
            max_server_delay: Duration::from_secs(60),
            backoff_multiplier: 2.0,
        }
    }
}

impl RetryConfig {
    /// Create a disabled retry configuration (no retries).
    #[must_use]
    pub fn disabled() -> Self {
        Self { enabled: false, ..Self::default() }
    }

    /// Set the maximum number of retry attempts.
    #[must_use]
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Set the initial delay before the first retry.
    #[must_use]
    pub fn with_initial_delay(mut self, initial_delay: Duration) -> Self {
        self.initial_delay = initial_delay;
        self
    }

    /// Set the maximum exponential backoff delay.
    #[must_use]
    pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
        self.max_delay = max_delay;
        self
    }

    /// Set the backoff multiplier applied after each retry.
    #[must_use]
    pub fn with_backoff_multiplier(mut self, backoff_multiplier: f32) -> Self {
        self.backoff_multiplier = backoff_multiplier;
        self
    }

    /// Set the largest accepted server-provided retry delay.
    ///
    /// The default is 60 seconds. A hint above the limit returns the original
    /// error; an accepted hint is never shortened to [`Self::max_delay`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_model::retry::RetryConfig;
    /// use std::time::Duration;
    ///
    /// let retry = RetryConfig::default().with_max_server_delay(Duration::from_secs(30));
    /// assert_eq!(retry.max_server_delay, Duration::from_secs(30));
    /// ```
    #[must_use]
    pub fn with_max_server_delay(mut self, limit: Duration) -> Self {
        self.max_server_delay = limit;
        self
    }
}

/// Returns `true` if the HTTP status code indicates a transient error worth retrying.
#[must_use]
pub fn is_retryable_status_code(status_code: u16) -> bool {
    matches!(status_code, 408 | 429 | 500 | 502 | 503 | 504 | 520..=524 | 529)
}

/// Maps an upstream HTTP error status to the [`ErrorCategory`] provider clients report.
///
/// The transient server statuses (500, 502–504, 520–524, and Anthropic's 529) map
/// to [`ErrorCategory::Unavailable`], which [`execute_with_retry`] retries; every
/// supported provider documents them as safe to retry after a short wait. 402 Payment
/// Required maps to [`ErrorCategory::ResourceExhausted`], which is not retried: DeepSeek,
/// OpenRouter and Anthropic send it when the account cannot pay for the request.
///
/// # Example
///
/// ```rust
/// use adk_core::ErrorCategory;
/// use adk_model::retry::category_for_status_code;
///
/// assert_eq!(category_for_status_code(502), ErrorCategory::Unavailable);
/// assert_eq!(category_for_status_code(429), ErrorCategory::RateLimited);
/// assert_eq!(category_for_status_code(400), ErrorCategory::InvalidInput);
/// assert_eq!(category_for_status_code(402), ErrorCategory::ResourceExhausted);
/// ```
#[must_use]
pub fn category_for_status_code(status_code: u16) -> ErrorCategory {
    match status_code {
        401 => ErrorCategory::Unauthorized,
        402 => ErrorCategory::ResourceExhausted,
        403 => ErrorCategory::Forbidden,
        404 => ErrorCategory::NotFound,
        408 => ErrorCategory::Timeout,
        429 => ErrorCategory::RateLimited,
        500 | 502 | 503 | 504 | 520..=524 | 529 => ErrorCategory::Unavailable,
        500..=599 => ErrorCategory::Internal,
        _ => ErrorCategory::InvalidInput,
    }
}

/// Parses a `Retry-After` header value given in seconds or as an HTTP-date.
///
/// Delay-seconds follow RFC 9110's non-negative integer grammar. An HTTP-date
/// in any of its three formats yields the time remaining, or zero when it has
/// passed. Invalid or overflowing values are ignored so normal backoff applies.
/// Returns `None` for any other value.
///
/// # Example
///
/// ```rust
/// use adk_model::retry::parse_retry_after;
/// use std::time::Duration;
///
/// assert_eq!(parse_retry_after("7"), Some(Duration::from_secs(7)));
/// assert_eq!(parse_retry_after("1.5"), None);
/// assert_eq!(parse_retry_after("Wed, 21 Oct 2015 07:28:00 GMT"), Some(Duration::ZERO));
/// assert_eq!(parse_retry_after("soon"), None);
/// ```
#[must_use]
pub fn parse_retry_after(value: &str) -> Option<Duration> {
    // `RetryHint` stores whole milliseconds as `u64`.
    let value = value.trim_matches([' ', '\t']);
    if !value.is_empty() && value.bytes().all(|byte| byte.is_ascii_digit()) {
        let delay = Duration::from_secs(value.parse().ok()?);
        u64::try_from(delay.as_millis()).ok()?;
        return Some(delay);
    }
    let date = httpdate::parse_http_date(value).ok()?;
    let delay = date.duration_since(std::time::SystemTime::now()).unwrap_or(Duration::ZERO);
    // Round up so a retry never starts before the requested date.
    let millis = delay.as_nanos().div_ceil(1_000_000);
    u64::try_from(millis).ok().map(Duration::from_millis)
}

/// Attaches a server-provided `Retry-After` delay to a retryable error.
///
/// Errors in a non-retryable category are returned unchanged.
///
/// # Example
///
/// ```rust
/// use adk_core::{AdkError, ErrorCategory, ErrorComponent};
/// use adk_model::retry::with_retry_after;
/// use std::time::Duration;
///
/// let error = AdkError::new(ErrorComponent::Model, ErrorCategory::Unavailable, "m.e", "down");
/// let error = with_retry_after(error, Some(Duration::from_secs(2)));
/// assert_eq!(error.retry.retry_after(), Some(Duration::from_secs(2)));
/// ```
#[must_use]
pub fn with_retry_after(error: AdkError, retry_after: Option<Duration>) -> AdkError {
    match retry_after {
        Some(delay) if error.retry.should_retry => {
            let retry = error.retry.clone().with_retry_after(delay);
            error.with_retry(retry)
        }
        _ => error,
    }
}

/// Returns `true` if the error message contains patterns indicating a transient failure.
#[must_use]
pub fn is_retryable_error_message(message: &str) -> bool {
    let normalized = message.to_ascii_uppercase();
    normalized.contains("429")
        || normalized.contains("408")
        || normalized.contains("500")
        || normalized.contains("502")
        || normalized.contains("503")
        || normalized.contains("504")
        || normalized.contains("529")
        || normalized.contains("RATE LIMIT")
        || normalized.contains("TOO MANY REQUESTS")
        || normalized.contains("RESOURCE_EXHAUSTED")
        || normalized.contains("UNAVAILABLE")
        || normalized.contains("DEADLINE_EXCEEDED")
        || normalized.contains("TIMEOUT")
        || normalized.contains("TIMED OUT")
        || normalized.contains("CONNECTION RESET")
        || normalized.contains("OVERLOADED")
}

/// Returns `true` if the `AdkError` should be retried based on its retry hint or message.
#[must_use]
pub fn is_retryable_model_error(error: &AdkError) -> bool {
    // Primary: use structured retry hint (single source of truth)
    if error.retry.should_retry {
        return true;
    }
    // Fallback: for backward-compat `.legacy` errors during transition,
    // check the error message for retryable patterns
    if error.code.ends_with(".legacy") && error.is_model() {
        return is_retryable_error_message(&error.message);
    }
    false
}

fn next_retry_delay(current: Duration, retry_config: &RetryConfig) -> Duration {
    if current >= retry_config.max_delay {
        return retry_config.max_delay;
    }

    let multiplier = retry_config.backoff_multiplier.max(1.0) as f64;
    let scaled = Duration::from_secs_f64(current.as_secs_f64() * multiplier);
    scaled.min(retry_config.max_delay)
}

/// Hint from the server about when to retry.
///
/// When the server provides a `retry-after` header, this hint overrides the
/// exponential backoff calculation for the next retry attempt.
///
/// # Example
///
/// ```rust
/// use adk_model::retry::ServerRetryHint;
/// use std::time::Duration;
///
/// let hint = ServerRetryHint { retry_after: Some(Duration::from_secs(30)) };
/// assert_eq!(hint.retry_after, Some(Duration::from_secs(30)));
/// ```
#[derive(Debug, Clone, Default)]
pub struct ServerRetryHint {
    /// Server-suggested delay before retrying.
    pub retry_after: Option<Duration>,
}

/// Execute an operation with automatic retry on transient errors.
pub async fn execute_with_retry<T, Op, Fut, Classify>(
    retry_config: &RetryConfig,
    classify_error: Classify,
    mut operation: Op,
) -> Result<T>
where
    Op: FnMut() -> Fut,
    Fut: Future<Output = Result<T>>,
    Classify: Fn(&AdkError) -> bool,
{
    execute_with_retry_hint(retry_config, classify_error, None, &mut operation).await
}

/// Execute an operation with retry logic, optionally using a server-provided
/// retry hint to override the backoff delay.
///
/// When `server_hint` contains a `retry_after` duration, that duration is used
/// instead of the exponential backoff calculation. This respects server-provided
/// timing from `retry-after` headers (Requirement 5.1).
///
/// A server-provided delay is honored without shortening it, up to
/// [`RetryConfig::max_server_delay`]; a larger hint returns the original error.
pub async fn execute_with_retry_hint<T, Op, Fut, Classify>(
    retry_config: &RetryConfig,
    classify_error: Classify,
    server_hint: Option<&ServerRetryHint>,
    operation: &mut Op,
) -> Result<T>
where
    Op: FnMut() -> Fut,
    Fut: Future<Output = Result<T>>,
    Classify: Fn(&AdkError) -> bool,
{
    execute_with_retry_observer(retry_config, classify_error, server_hint, operation, |_, _, _| {
        std::future::ready(Ok(()))
    })
    .await
}

/// Execute with one retry owner and an asynchronous observation before each backoff.
///
/// The observer receives the next retry number, retry limit and actual delay.
/// An observer error stops recovery before sleeping or invoking the operation again.
/// Disabled retries and rejected server delays do not call the observer.
///
/// # Example
///
/// ```rust
/// use adk_core::AdkError;
/// use adk_model::retry::{RetryConfig, execute_with_retry_observer};
///
/// # tokio::runtime::Builder::new_current_thread().enable_time().build().unwrap().block_on(async {
/// let mut calls = 0;
/// let result = execute_with_retry_observer(
///     &RetryConfig::default(),
///     AdkError::is_retryable,
///     None,
///     &mut || {
///         calls += 1;
///         std::future::ready(Err::<(), _>(AdkError::unavailable(adk_core::ErrorComponent::Model, "model.busy", "busy")))
///     },
///     |_, _, _| std::future::ready(Err(AdkError::model("retry cancelled"))),
/// ).await;
/// assert!(result.is_err());
/// assert_eq!(calls, 1);
/// # });
/// ```
pub async fn execute_with_retry_observer<T, Op, Fut, Classify, Observe, ObserveFut>(
    retry_config: &RetryConfig,
    classify_error: Classify,
    server_hint: Option<&ServerRetryHint>,
    operation: &mut Op,
    mut observe: Observe,
) -> Result<T>
where
    Op: FnMut() -> Fut,
    Fut: Future<Output = Result<T>>,
    Classify: Fn(&AdkError) -> bool,
    Observe: FnMut(u32, u32, Duration) -> ObserveFut,
    ObserveFut: Future<Output = Result<()>>,
{
    if !retry_config.enabled {
        return operation().await;
    }

    let mut attempt: u32 = 0;
    let mut delay = retry_config.initial_delay.min(retry_config.max_delay);

    // If the server provided a retry-after hint, use it for the first retry delay.
    let server_delay = server_hint.and_then(|h| h.retry_after);

    loop {
        match operation().await {
            Ok(value) => return Ok(value),
            Err(error) if attempt < retry_config.max_retries && classify_error(&error) => {
                // Priority: error hint, initial server hint, then bounded backoff.
                let requested = error
                    .retry
                    .retry_after()
                    .or_else(|| (attempt == 0).then_some(server_delay).flatten());
                if requested.is_some_and(|delay| delay > retry_config.max_server_delay) {
                    return Err(error);
                }
                let effective_delay = requested.unwrap_or(delay);
                if tokio::time::Instant::now().checked_add(effective_delay).is_none() {
                    return Err(error);
                }
                attempt += 1;
                observe(attempt, retry_config.max_retries, effective_delay).await?;

                // The message can echo the request, and the client sees no `RunConfig`, so
                // only the content-free part is logged.
                adk_telemetry::warn!(
                    attempt = attempt,
                    max_retries = retry_config.max_retries,
                    delay_ms = effective_delay.as_millis(),
                    error = %error.diagnostics(),
                    "provider request failed with a retryable error; retrying"
                );
                tokio::time::sleep(effective_delay).await;
                delay = next_retry_delay(delay, retry_config);
            }
            Err(error) => return Err(error),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{
        Arc,
        atomic::{AtomicU32, Ordering},
    };

    #[tokio::test]
    async fn execute_with_retry_retries_when_classified_retryable() {
        let retry_config = RetryConfig::default()
            .with_max_retries(2)
            .with_initial_delay(Duration::ZERO)
            .with_max_delay(Duration::ZERO);
        let attempts = Arc::new(AtomicU32::new(0));

        let result = execute_with_retry(&retry_config, is_retryable_model_error, || {
            let attempts = Arc::clone(&attempts);
            async move {
                let attempt = attempts.fetch_add(1, Ordering::SeqCst);
                if attempt < 2 {
                    return Err(AdkError::model("HTTP 429 rate limit"));
                }
                Ok("ok")
            }
        })
        .await
        .expect("operation should succeed after retries");

        assert_eq!(result, "ok");
        assert_eq!(attempts.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn execute_with_retry_stops_on_non_retryable_error() {
        let retry_config = RetryConfig::default()
            .with_max_retries(3)
            .with_initial_delay(Duration::ZERO)
            .with_max_delay(Duration::ZERO);
        let attempts = Arc::new(AtomicU32::new(0));

        let error = execute_with_retry(&retry_config, is_retryable_model_error, || {
            let attempts = Arc::clone(&attempts);
            async move {
                attempts.fetch_add(1, Ordering::SeqCst);
                Err::<(), _>(AdkError::model("HTTP 400 bad request"))
            }
        })
        .await
        .expect_err("operation should fail without retries");

        assert!(error.is_model());
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn execute_with_retry_respects_disabled_config() {
        let retry_config = RetryConfig::disabled().with_max_retries(10);
        let attempts = Arc::new(AtomicU32::new(0));

        let error = execute_with_retry(&retry_config, is_retryable_model_error, || {
            let attempts = Arc::clone(&attempts);
            async move {
                attempts.fetch_add(1, Ordering::SeqCst);
                Err::<(), _>(AdkError::model("HTTP 429 too many requests"))
            }
        })
        .await
        .expect_err("disabled retries should return first error");

        assert!(error.is_model());
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn retryable_status_code_matches_transient_errors() {
        assert!(is_retryable_status_code(429));
        assert!(is_retryable_status_code(503));
        assert!(is_retryable_status_code(529));
        assert!(!is_retryable_status_code(400));
        assert!(!is_retryable_status_code(401));
    }

    #[tokio::test]
    async fn refuses_excessive_server_delays() {
        let retry_config = RetryConfig::default()
            .with_max_retries(1)
            .with_initial_delay(Duration::ZERO)
            .with_max_delay(Duration::from_millis(20));
        let attempts = Arc::new(AtomicU32::new(0));
        let started = std::time::Instant::now();

        let result = execute_with_retry(&retry_config, is_retryable_model_error, || {
            let attempts = Arc::clone(&attempts);
            async move {
                if attempts.fetch_add(1, Ordering::SeqCst) == 0 {
                    let error = AdkError::new(
                        adk_core::ErrorComponent::Model,
                        ErrorCategory::Unavailable,
                        "model.test.unavailable",
                        "overloaded",
                    );
                    return Err(with_retry_after(error, Some(Duration::from_secs(3600))));
                }
                Ok("ok")
            }
        })
        .await;

        let error = result.expect_err("excessive server delay must not be shortened");
        assert_eq!(error.category, ErrorCategory::Unavailable);
        assert_eq!(error.retry.retry_after(), Some(Duration::from_secs(3600)));
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
        assert!(started.elapsed() < Duration::from_secs(1));
    }

    #[tokio::test]
    async fn refuses_excessive_initial_hints() {
        let retry_config = RetryConfig::default()
            .with_max_retries(1)
            .with_initial_delay(Duration::ZERO)
            .with_max_delay(Duration::from_millis(20));
        let hint = ServerRetryHint { retry_after: Some(Duration::from_secs(3600)) };
        let attempts = Arc::new(AtomicU32::new(0));
        let started = std::time::Instant::now();

        let result = execute_with_retry_hint(
            &retry_config,
            is_retryable_model_error,
            Some(&hint),
            &mut || {
                let attempts = Arc::clone(&attempts);
                async move {
                    if attempts.fetch_add(1, Ordering::SeqCst) == 0 {
                        return Err(AdkError::model("HTTP 503 unavailable"));
                    }
                    Ok("ok")
                }
            },
        )
        .await;

        let error = result.expect_err("excessive initial hint must not be shortened");
        assert_eq!(error.message, "HTTP 503 unavailable");
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
        assert!(started.elapsed() < Duration::from_secs(1));
    }

    #[test]
    fn transient_server_statuses_map_to_retryable_unavailable() {
        for status in [500, 502, 503, 504, 520, 521, 522, 523, 524, 529] {
            assert_eq!(category_for_status_code(status), ErrorCategory::Unavailable, "{status}");
        }
        assert_eq!(category_for_status_code(501), ErrorCategory::Internal);
        assert_eq!(category_for_status_code(429), ErrorCategory::RateLimited);
        assert_eq!(category_for_status_code(408), ErrorCategory::Timeout);
        assert_eq!(category_for_status_code(422), ErrorCategory::InvalidInput);
        assert_eq!(category_for_status_code(402), ErrorCategory::ResourceExhausted);
    }

    #[test]
    fn retry_after_is_attached_only_to_retryable_errors() {
        let delay = Some(Duration::from_secs(4));
        let unavailable = AdkError::new(
            adk_core::ErrorComponent::Model,
            ErrorCategory::Unavailable,
            "model.test",
            "down",
        );
        let invalid = AdkError::new(
            adk_core::ErrorComponent::Model,
            ErrorCategory::InvalidInput,
            "model.test",
            "bad",
        );

        assert_eq!(
            with_retry_after(unavailable, delay).retry.retry_after(),
            Some(Duration::from_secs(4))
        );
        assert_eq!(with_retry_after(invalid, delay).retry.retry_after(), None);
        assert_eq!(parse_retry_after("-1"), None);
        assert_eq!(parse_retry_after(" 30 "), Some(Duration::from_secs(30)));
    }

    #[test]
    fn parses_dates_and_rejects_invalid_seconds() {
        let future = std::time::SystemTime::now() + Duration::from_secs(30);
        for date in [
            httpdate::fmt_http_date(future),
            "Sunday, 06-Nov-94 08:49:37 GMT".to_string(),
            "Sun Nov  6 08:49:37 1994".to_string(),
        ] {
            let delay = parse_retry_after(&date).expect("HTTP-date should parse");
            assert!(delay <= Duration::from_secs(30), "{date}");
        }
        assert!(parse_retry_after(&httpdate::fmt_http_date(future)).unwrap() > Duration::ZERO);

        assert_eq!(parse_retry_after("61"), Some(Duration::from_secs(61)));
        assert_eq!(parse_retry_after("3600"), Some(Duration::from_secs(3600)));
        for invalid in [
            "",
            "soon",
            "inf",
            "NaN",
            "-0.5",
            "-1",
            "+1",
            "1.5",
            "1e300",
            "18446744073709551616",
            "18446744073709552",
            "1\n",
        ] {
            assert_eq!(parse_retry_after(invalid), None, "{invalid:?}");
        }
    }

    #[tokio::test]
    async fn observer_receives_accepted_delays() {
        let config = RetryConfig::default()
            .with_max_retries(1)
            .with_max_delay(Duration::ZERO)
            .with_max_server_delay(Duration::from_millis(30));
        let mut calls = 0;
        let mut observations = Vec::new();
        let started = std::time::Instant::now();
        let result = execute_with_retry_observer(
            &config,
            AdkError::is_retryable,
            None,
            &mut || {
                calls += 1;
                std::future::ready(if calls == 1 {
                    Err(with_retry_after(
                        AdkError::unavailable(
                            adk_core::ErrorComponent::Model,
                            "model.busy",
                            "busy",
                        ),
                        Some(Duration::from_millis(30)),
                    ))
                } else {
                    Ok("done")
                })
            },
            |attempt, limit, delay| {
                observations.push((attempt, limit, delay));
                std::future::ready(Ok(()))
            },
        )
        .await
        .unwrap();
        assert_eq!(result, "done");
        assert_eq!(calls, 2);
        assert_eq!(observations, [(1, 1, Duration::from_millis(30))]);
        assert!(started.elapsed() >= Duration::from_millis(30));
    }

    #[tokio::test]
    async fn observer_can_cancel_before_replay() {
        let mut calls = 0;
        let mut observations = 0;
        let error = execute_with_retry_observer(
            &RetryConfig::default(),
            AdkError::is_retryable,
            None,
            &mut || {
                calls += 1;
                std::future::ready(Err::<(), _>(AdkError::unavailable(
                    adk_core::ErrorComponent::Model,
                    "model.busy",
                    "busy",
                )))
            },
            |attempt, limit, delay| {
                observations += 1;
                assert_eq!((attempt, limit, delay), (1, 3, Duration::from_millis(250)));
                std::future::ready(Err(AdkError::model("cancelled")))
            },
        )
        .await
        .unwrap_err();
        assert_eq!(error.message, "cancelled");
        assert_eq!((calls, observations), (1, 1));
    }

    #[tokio::test]
    async fn rejected_hints_skip_the_observer() {
        for seconds in [61, 3600] {
            let mut calls = 0;
            let mut observations = 0;
            let error = execute_with_retry_observer(
                &RetryConfig::default(),
                AdkError::is_retryable,
                None,
                &mut || {
                    calls += 1;
                    std::future::ready(Err::<(), _>(with_retry_after(
                        AdkError::unavailable(
                            adk_core::ErrorComponent::Model,
                            "model.busy",
                            "original",
                        ),
                        Some(Duration::from_secs(seconds)),
                    )))
                },
                |_, _, _| {
                    observations += 1;
                    std::future::ready(Ok(()))
                },
            )
            .await
            .unwrap_err();
            assert_eq!(error.message, "original");
            assert_eq!(error.retry.retry_after(), Some(Duration::from_secs(seconds)));
            assert_eq!((calls, observations), (1, 0));
        }
    }

    #[test]
    fn retryable_error_message_matches_529_and_overloaded() {
        assert!(is_retryable_error_message("HTTP 529 overloaded"));
        assert!(is_retryable_error_message("Server OVERLOADED, try again"));
    }

    #[tokio::test]
    async fn execute_with_retry_hint_uses_server_delay() {
        let retry_config = RetryConfig::default()
            .with_max_retries(2)
            .with_initial_delay(Duration::ZERO)
            .with_max_delay(Duration::ZERO);
        let attempts = Arc::new(AtomicU32::new(0));
        let hint = ServerRetryHint { retry_after: Some(Duration::ZERO) };

        let result = execute_with_retry_hint(
            &retry_config,
            is_retryable_model_error,
            Some(&hint),
            &mut || {
                let attempts = Arc::clone(&attempts);
                async move {
                    let attempt = attempts.fetch_add(1, Ordering::SeqCst);
                    if attempt < 1 {
                        return Err(AdkError::model("HTTP 429 rate limit"));
                    }
                    Ok("ok")
                }
            },
        )
        .await
        .expect("operation should succeed after retry with hint");

        assert_eq!(result, "ok");
        assert_eq!(attempts.load(Ordering::SeqCst), 2);
    }

    /// Requirement 5.2: HTTP 529 (overloaded) is retried end-to-end.
    #[tokio::test]
    async fn status_529_is_retried_end_to_end() {
        let retry_config = RetryConfig::default()
            .with_max_retries(2)
            .with_initial_delay(Duration::ZERO)
            .with_max_delay(Duration::ZERO);
        let attempts = Arc::new(AtomicU32::new(0));

        let result = execute_with_retry(&retry_config, is_retryable_model_error, || {
            let attempts = Arc::clone(&attempts);
            async move {
                let attempt = attempts.fetch_add(1, Ordering::SeqCst);
                if attempt == 0 {
                    return Err(AdkError::model("HTTP 529 overloaded"));
                }
                Ok("recovered")
            }
        })
        .await
        .expect("529 should be retried and succeed on second attempt");

        assert_eq!(result, "recovered");
        assert_eq!(attempts.load(Ordering::SeqCst), 2);
    }

    /// Requirement 5.4: Exponential backoff when retry-after is absent.
    /// With initial_delay=20ms and multiplier=2.0, the delays should be
    /// ~20ms (attempt 1) then ~40ms (attempt 2). We verify each gap is
    /// at least the expected delay.
    #[tokio::test]
    async fn exponential_backoff_without_retry_after() {
        let retry_config = RetryConfig::default()
            .with_max_retries(3)
            .with_initial_delay(Duration::from_millis(20))
            .with_max_delay(Duration::from_millis(200))
            .with_backoff_multiplier(2.0);

        let timestamps: Arc<std::sync::Mutex<Vec<std::time::Instant>>> =
            Arc::new(std::sync::Mutex::new(Vec::new()));

        let result = execute_with_retry(&retry_config, is_retryable_model_error, || {
            let timestamps = Arc::clone(&timestamps);
            async move {
                let now = std::time::Instant::now();
                let mut ts = timestamps.lock().unwrap();
                let attempt = ts.len();
                ts.push(now);
                if attempt < 3 {
                    return Err(AdkError::model("HTTP 429 rate limit"));
                }
                Ok("done")
            }
        })
        .await
        .expect("should succeed after backoff retries");

        assert_eq!(result, "done");

        let ts = timestamps.lock().unwrap();
        assert_eq!(ts.len(), 4); // initial + 3 retries

        // Gap between attempt 0 and 1 should be >= initial_delay (20ms).
        let gap1 = ts[1].duration_since(ts[0]);
        assert!(gap1 >= Duration::from_millis(18), "first backoff gap {gap1:?} should be >= ~20ms");

        // Gap between attempt 1 and 2 should be >= 2 * initial_delay (40ms).
        let gap2 = ts[2].duration_since(ts[1]);
        assert!(
            gap2 >= Duration::from_millis(36),
            "second backoff gap {gap2:?} should be >= ~40ms"
        );

        // Gap 2 should be roughly double gap 1 (with tolerance for scheduling).
        assert!(gap2 >= gap1, "backoff should increase: gap2={gap2:?} should be >= gap1={gap1:?}");
    }
}