aimux-core 0.5.0

Core abstractions for aimux: LanguageModel trait, message types, and user-facing API
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
//! Operation-level retry, aligned with the AI SDK retry utilities.

use std::future::Future;
use std::time::Duration;

use rand::Rng;

use crate::AbortSignal;
use crate::error::{AiMuxError, RetryError, RetryErrorReason};

const DEFAULT_MAX_RETRIES: u32 = 2;
const INITIAL_DELAY_MS: u64 = 2_000;
const BACKOFF_FACTOR: u32 = 2;

/// Default retry settings for model operations.
///
/// Per-call `max_retries` overrides only [`Self::max_retries`]; the configured
/// delay and backoff factor remain in effect.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetryConfig {
    /// Maximum number of retries after the initial attempt.
    pub max_retries: u32,
    /// Initial delay between attempts.
    pub initial_delay: Duration,
    /// Multiplier applied to the delay after each retry.
    pub backoff_factor: u32,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: DEFAULT_MAX_RETRIES,
            initial_delay: Duration::from_millis(INITIAL_DELAY_MS),
            backoff_factor: BACKOFF_FACTOR,
        }
    }
}

/// A prepared operation retry function and its resolved retry count.
///
/// This is the Rust representation of the AI SDK's
/// `prepareRetries()` result: `{ maxRetries, retry }`.
#[derive(Debug, Clone)]
pub struct PreparedRetries {
    /// The effective maximum after applying the default.
    pub max_retries: u32,
    initial_delay_ms: u64,
    backoff_factor: u64,
    abort_signal: Option<AbortSignal>,
}

/// Bind retry settings, applying a per-call retry-count override.
#[must_use]
pub fn prepare_retries(
    max_retries: Option<u32>,
    mut config: RetryConfig,
    abort_signal: Option<AbortSignal>,
) -> PreparedRetries {
    if let Some(max_retries) = max_retries {
        config.max_retries = max_retries;
    }
    PreparedRetries {
        max_retries: config.max_retries,
        initial_delay_ms: u64::try_from(config.initial_delay.as_millis()).unwrap_or(u64::MAX),
        backoff_factor: u64::from(config.backoff_factor),
        abort_signal,
    }
}

impl PreparedRetries {
    /// Retry a complete operation using API-call retryability and response hints.
    ///
    /// # Errors
    ///
    /// Returns the operation error, retry exhaustion, or caller cancellation.
    pub async fn retry<F, Fut, T>(&self, operation: F) -> Result<T, AiMuxError>
    where
        F: FnMut() -> Fut,
        Fut: Future<Output = Result<T, AiMuxError>>,
    {
        retry_with_exponential_backoff_respecting_retry_headers(
            operation,
            self.max_retries,
            self.initial_delay_ms,
            self.backoff_factor,
            self.abort_signal.as_ref(),
        )
        .await
    }
}

async fn retry_with_exponential_backoff_respecting_retry_headers<F, Fut, T>(
    operation: F,
    max_retries: u32,
    initial_delay_ms: u64,
    backoff_factor: u64,
    abort_signal: Option<&AbortSignal>,
) -> Result<T, AiMuxError>
where
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, AiMuxError>>,
{
    retry_with_exponential_backoff(
        operation,
        max_retries,
        initial_delay_ms,
        backoff_factor,
        abort_signal,
        |error: &AiMuxError| matches!(error, AiMuxError::ApiCall(detail) if detail.is_retryable),
        |error: &AiMuxError, exponential_delay| {
            retry_delay_with_jitter(error, exponential_delay, |maximum| {
                rand::thread_rng().gen_range(0..=maximum)
            })
        },
    )
    .await
}

/// Generic exponential retry primitive. The caller supplies retry
/// classification and delay selection.
///
/// # Errors
///
/// Returns the operation error, retry exhaustion, or caller cancellation.
pub(crate) async fn retry_with_exponential_backoff<F, Fut, T, ShouldRetry, GetDelay>(
    mut operation: F,
    max_retries: u32,
    initial_delay_ms: u64,
    backoff_factor: u64,
    abort_signal: Option<&AbortSignal>,
    mut should_retry: ShouldRetry,
    mut get_delay_ms: GetDelay,
) -> Result<T, AiMuxError>
where
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, AiMuxError>>,
    ShouldRetry: FnMut(&AiMuxError) -> bool,
    GetDelay: FnMut(&AiMuxError, u64) -> u64,
{
    let mut errors = Vec::new();
    let mut exponential_delay = initial_delay_ms;

    loop {
        let result = match abort_signal {
            Some(signal) => {
                if signal.is_aborted() {
                    return Err(AiMuxError::from_abort_signal(signal));
                }
                tokio::select! {
                    biased;
                    () = signal.cancelled() => Err(AiMuxError::from_abort_signal(signal)),
                    result = async { operation().await } => result,
                }
            }
            None => operation().await,
        };

        match result {
            Ok(value) => return Ok(value),
            Err(error @ (AiMuxError::Aborted(_) | AiMuxError::Timeout(_))) => {
                return Err(error);
            }
            // A safe inner exchange may exhaust its own retries inside a
            // larger non-idempotent operation. Pass that exhaustion through
            // so the enclosing operation is not submitted again.
            Err(error @ AiMuxError::Retry(_)) => return Err(error),
            Err(error) if max_retries == 0 => return Err(error),
            Err(error) => {
                tracing::warn!(
                    target: "aimux_core::retry",
                    attempt = errors.len() + 1,
                    error = %error,
                    "operation attempt failed"
                );
                errors.push(error);
                let try_number = errors.len() as u32;

                if try_number > max_retries {
                    tracing::error!(
                        target: "aimux_core::retry",
                        attempts = errors.len(),
                        error = %errors.last().expect("retry history is non-empty"),
                        "operation retry exhausted"
                    );
                    return Err(AiMuxError::Retry(RetryError {
                        reason: RetryErrorReason::MaxRetriesExceeded,
                        errors,
                    }));
                }

                let error = errors.last().expect("attempt error was just appended");
                if should_retry(error) {
                    let retry_delay = get_delay_ms(error, exponential_delay);
                    delay(Duration::from_millis(retry_delay), abort_signal).await?;
                    exponential_delay = exponential_delay.saturating_mul(backoff_factor);
                    continue;
                }

                if try_number == 1 {
                    tracing::error!(
                        target: "aimux_core::retry",
                        error = %error,
                        "operation failed without retry"
                    );
                    return Err(errors.pop().expect("the first attempt error exists"));
                }

                tracing::error!(
                    target: "aimux_core::retry",
                    attempts = errors.len(),
                    error = %error,
                    "operation stopped on non-retryable error"
                );
                return Err(AiMuxError::Retry(RetryError {
                    reason: RetryErrorReason::ErrorNotRetryable,
                    errors,
                }));
            }
        }
    }
}

/// Abort-aware delay between attempts (also used by video poll pacing).
///
/// # Errors
///
/// Returns [`AiMuxError::Aborted`] when the caller cancels during the delay.
pub(crate) async fn delay(
    duration: Duration,
    abort_signal: Option<&AbortSignal>,
) -> Result<(), AiMuxError> {
    match abort_signal {
        Some(signal) => {
            tokio::select! {
                biased;
                () = signal.cancelled() => Err(AiMuxError::from_abort_signal(signal)),
                () = tokio::time::sleep(duration) => Ok(()),
            }
        }
        None => {
            tokio::time::sleep(duration).await;
            Ok(())
        }
    }
}

fn retry_header_delay(error: &AiMuxError, exponential_delay: u64) -> Option<u64> {
    let parsed = u64::try_from(error.retry_after_hint()?).ok()?;

    (parsed < 60_000 || parsed < exponential_delay).then_some(parsed)
}

fn retry_delay_with_jitter(
    error: &AiMuxError,
    exponential_delay: u64,
    jitter: impl FnOnce(u64) -> u64,
) -> u64 {
    retry_header_delay(error, exponential_delay)
        .unwrap_or_else(|| jitter(exponential_delay).min(exponential_delay))
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::*;
    use crate::ApiCallError;

    fn api_error(retryable: bool) -> AiMuxError {
        AiMuxError::ApiCall(Box::new(ApiCallError {
            is_retryable: retryable,
            ..ApiCallError::new("retry", "https://example.test", serde_json::json!({}))
        }))
    }

    fn api_error_with_headers(status: u16, headers: &[(&str, &str)]) -> AiMuxError {
        AiMuxError::ApiCall(Box::new(ApiCallError {
            status_code: Some(status),
            response_headers: Some(
                headers
                    .iter()
                    .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
                    .collect::<HashMap<_, _>>(),
            ),
            is_retryable: true,
            ..ApiCallError::new("retry", "https://example.test", serde_json::json!({}))
        }))
    }

    fn should_retry(error: &AiMuxError) -> bool {
        error.is_retryable()
    }

    fn unchanged_delay(_error: &AiMuxError, delay: u64) -> u64 {
        delay
    }

    #[tokio::test]
    async fn prepared_retries_resolves_the_default_and_binds_abort() {
        assert_eq!(
            prepare_retries(None, RetryConfig::default(), None).max_retries,
            2
        );

        let signal = AbortSignal::new();
        signal.abort();
        let retries = prepare_retries(Some(4), RetryConfig::default(), Some(signal));
        let attempts = AtomicUsize::new(0);

        let error = retries
            .retry(|| {
                attempts.fetch_add(1, Ordering::SeqCst);
                async { Ok::<(), AiMuxError>(()) }
            })
            .await
            .unwrap_err();

        assert_eq!(retries.max_retries, 4);
        assert!(matches!(error, AiMuxError::Aborted(_)));
        assert_eq!(attempts.load(Ordering::SeqCst), 0);
    }

    #[test]
    fn per_call_max_preserves_the_model_delay_and_backoff() {
        let retries = prepare_retries(
            Some(4),
            RetryConfig {
                max_retries: 7,
                initial_delay: Duration::from_millis(123),
                backoff_factor: 3,
            },
            None,
        );

        assert_eq!(retries.max_retries, 4);
        assert_eq!(retries.initial_delay_ms, 123);
        assert_eq!(retries.backoff_factor, 3);
    }

    #[tokio::test]
    async fn first_non_retryable_error_is_not_wrapped() {
        let error = retry_with_exponential_backoff(
            || async { Err::<(), _>(AiMuxError::InvalidArgument("bad".into())) },
            2,
            0,
            2,
            None,
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();
        assert!(matches!(error, AiMuxError::InvalidArgument(_)));
    }

    #[tokio::test]
    async fn zero_retries_returns_the_original_error() {
        let attempts = AtomicUsize::new(0);
        let error = retry_with_exponential_backoff(
            || {
                attempts.fetch_add(1, Ordering::SeqCst);
                async { Err::<(), _>(api_error(true)) }
            },
            0,
            0,
            2,
            None,
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();
        assert!(matches!(error, AiMuxError::ApiCall(_)));
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn exhausted_attempts_preserve_the_complete_history() {
        let attempts = AtomicUsize::new(0);
        let error = retry_with_exponential_backoff(
            || {
                attempts.fetch_add(1, Ordering::SeqCst);
                async { Err::<(), _>(api_error(true)) }
            },
            2,
            0,
            2,
            None,
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();
        let AiMuxError::Retry(error) = error else {
            panic!("expected retry error")
        };
        assert_eq!(error.reason, RetryErrorReason::MaxRetriesExceeded);
        assert_eq!(error.errors.len(), 3);
        assert_eq!(attempts.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn later_non_retryable_error_preserves_both_attempts() {
        let attempts = AtomicUsize::new(0);
        let error = retry_with_exponential_backoff(
            || {
                let attempt = attempts.fetch_add(1, Ordering::SeqCst);
                async move { Err::<(), _>(api_error(attempt == 0)) }
            },
            2,
            0,
            2,
            None,
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();
        let AiMuxError::Retry(error) = error else {
            panic!("expected retry error")
        };
        assert_eq!(error.reason, RetryErrorReason::ErrorNotRetryable);
        assert_eq!(error.errors.len(), 2);
    }

    #[tokio::test]
    async fn retry_error_is_never_nested() {
        let inner = RetryError {
            reason: RetryErrorReason::MaxRetriesExceeded,
            errors: vec![api_error(true), api_error(true)],
        };
        let error = retry_with_exponential_backoff(
            || {
                let error = inner.clone();
                async move { Err::<(), _>(AiMuxError::Retry(error)) }
            },
            2,
            0,
            2,
            None,
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();

        assert!(matches!(error, AiMuxError::Retry(_)));
        let AiMuxError::Retry(error) = error else {
            unreachable!()
        };
        assert!(
            error
                .errors
                .iter()
                .all(|error| !matches!(error, AiMuxError::Retry(_)))
        );
    }

    #[tokio::test]
    async fn timeout_and_abort_are_never_wrapped() {
        for source in [
            AiMuxError::Timeout("Total timeout of 1ms exceeded".into()),
            AiMuxError::Aborted("request aborted".into()),
        ] {
            let expected = source.to_string();
            let error = retry_with_exponential_backoff(
                || {
                    let source = source.clone();
                    async move { Err::<(), _>(source) }
                },
                2,
                0,
                2,
                None,
                should_retry,
                unchanged_delay,
            )
            .await
            .unwrap_err();
            assert_eq!(error.to_string(), expected);
            assert!(!matches!(error, AiMuxError::Retry(_)));
        }
    }

    #[tokio::test]
    async fn max_retries_check_precedes_final_retryability() {
        let attempts = AtomicUsize::new(0);
        let error = retry_with_exponential_backoff(
            || {
                let attempt = attempts.fetch_add(1, Ordering::SeqCst);
                async move { Err::<(), _>(api_error(attempt < 2)) }
            },
            2,
            0,
            2,
            None,
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();
        let AiMuxError::Retry(error) = error else {
            panic!("expected retry error")
        };
        assert_eq!(error.reason, RetryErrorReason::MaxRetriesExceeded);
        assert_eq!(error.errors.len(), 3);
    }

    #[test]
    fn retry_headers_are_exact_and_only_exponential_delay_is_jittered() {
        let milliseconds = api_error_with_headers(503, &[("retry-after-ms", "1500")]);
        assert_eq!(retry_header_delay(&milliseconds, 2_000), Some(1_500));
        assert_eq!(
            retry_delay_with_jitter(&milliseconds, 2_000, |_| panic!("hint must not jitter")),
            1_500
        );

        let seconds = api_error_with_headers(503, &[("retry-after", "1.25")]);
        assert_eq!(retry_header_delay(&seconds, 2_000), Some(1_250));

        let unreasonable = api_error_with_headers(503, &[("retry-after-ms", "120000")]);
        assert_eq!(retry_header_delay(&unreasonable, 2_000), None);
        assert_eq!(retry_header_delay(&unreasonable, u64::MAX), Some(120_000));
        assert_eq!(retry_delay_with_jitter(&unreasonable, 2_000, |_| 0), 0);
        assert_eq!(
            retry_delay_with_jitter(&unreasonable, 2_000, |max| max),
            2_000
        );

        let nan_then_seconds =
            api_error_with_headers(503, &[("retry-after-ms", "NaN"), ("retry-after", "1")]);
        assert_eq!(retry_header_delay(&nan_then_seconds, 2_000), Some(1_000));
    }

    #[tokio::test]
    async fn abort_during_backoff_returns_abort_without_history() {
        let signal = AbortSignal::new();
        let abort_after_attempt = signal.clone();
        let error = retry_with_exponential_backoff(
            || async { Err::<(), _>(api_error(true)) },
            2,
            60_000,
            2,
            Some(&signal),
            move |_error: &AiMuxError| {
                abort_after_attempt.abort();
                true
            },
            unchanged_delay,
        )
        .await
        .unwrap_err();
        assert!(matches!(error, AiMuxError::Aborted(message) if message == "request aborted"));
    }

    #[tokio::test]
    async fn pre_aborted_signal_does_not_start_an_attempt() {
        let signal = AbortSignal::new();
        signal.abort();
        let attempts = AtomicUsize::new(0);

        let error = retry_with_exponential_backoff(
            || {
                attempts.fetch_add(1, Ordering::SeqCst);
                async { Ok::<(), AiMuxError>(()) }
            },
            2,
            0,
            2,
            Some(&signal),
            should_retry,
            unchanged_delay,
        )
        .await
        .unwrap_err();

        assert!(matches!(error, AiMuxError::Aborted(message) if message == "request aborted"));
        assert_eq!(attempts.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn abort_during_attempt_drops_the_attempt_future() {
        struct DropMarker(Arc<AtomicUsize>);

        impl Drop for DropMarker {
            fn drop(&mut self) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }

        let signal = AbortSignal::new();
        let dropped = Arc::new(AtomicUsize::new(0));
        let marker = dropped.clone();
        let future = retry_with_exponential_backoff(
            move || {
                let marker = DropMarker(marker.clone());
                async move {
                    let _marker = marker;
                    std::future::pending::<Result<(), AiMuxError>>().await
                }
            },
            2,
            0,
            2,
            Some(&signal),
            should_retry,
            unchanged_delay,
        );
        tokio::pin!(future);
        assert!(matches!(
            futures::poll!(future.as_mut()),
            std::task::Poll::Pending
        ));

        signal.abort();
        let error = future.await.unwrap_err();

        assert!(matches!(error, AiMuxError::Aborted(message) if message == "request aborted"));
        assert_eq!(dropped.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn retry_error_messages_match_the_ai_sdk() {
        let max = RetryError {
            reason: RetryErrorReason::MaxRetriesExceeded,
            errors: vec![api_error(true), api_error(true), api_error(true)],
        };
        assert_eq!(
            max.to_string(),
            "Failed after 3 attempts. Last error: API call error: retry"
        );

        let non_retryable = RetryError {
            reason: RetryErrorReason::ErrorNotRetryable,
            errors: vec![api_error(true), api_error(false)],
        };
        assert_eq!(
            non_retryable.to_string(),
            "Failed after 2 attempts with non-retryable error: 'API call error: retry'"
        );
    }
}