aidaemon-core 0.28.0

The aidaemon agent kernel: a small, hook-driven agent loop with tools, providers, receipts and resumable state
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
//! [`Failover`]: a chain of providers, each attempt with its own deadline.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use futures::channel::mpsc;
use futures::stream::{self, BoxStream, StreamExt};
use serde::{Deserialize, Serialize};

use crate::provider::{ModelRequest, ModelResponse, Provider, ProviderError, StreamEvent};

/// Longest wait between attempts on the same step, whatever a rate limit
/// asks for.
const MAX_BACKOFF: Duration = Duration::from_secs(30);

/// One link of a [`Failover`] chain: a provider, the model it is asked for,
/// how long an attempt may take to start answering, and how many attempts
/// it gets before the chain moves on.
#[derive(Clone)]
pub struct FailoverStep {
    provider: Arc<dyn Provider>,
    model: Option<String>,
    deadline: Duration,
    attempts: u32,
}

impl FailoverStep {
    pub fn new(provider: impl Provider + 'static) -> Self {
        Self::from_arc(Arc::new(provider))
    }

    pub fn from_arc(provider: Arc<dyn Provider>) -> Self {
        Self {
            provider,
            model: None,
            deadline: Duration::from_secs(90),
            attempts: 1,
        }
    }

    /// Ask this step for `model` instead of the request's model.
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// How long one attempt may take to produce its first stream event (or,
    /// through [`Provider::complete`], its whole response). Default 90s.
    /// Each attempt gets the full deadline, whatever earlier ones used.
    pub fn deadline(mut self, deadline: Duration) -> Self {
        self.deadline = deadline;
        self
    }

    /// Attempts on this step before the chain moves on (default 1).
    pub fn attempts(mut self, attempts: u32) -> Self {
        self.attempts = attempts.max(1);
        self
    }

    fn label(&self, request: &ModelRequest) -> String {
        format!(
            "{}:{}",
            self.provider.name(),
            self.model.as_deref().unwrap_or(&request.model)
        )
    }

    fn request(&self, request: &ModelRequest) -> ModelRequest {
        let mut request = request.clone();
        if let Some(model) = &self.model {
            request.model.clone_from(model);
        }
        request
    }
}

/// Why a [`Failover`] chain moved on from a step.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum SwitchReason {
    /// The step failed with this error.
    Failed { error: String },
    /// No answer started within the step's deadline.
    TimedOut { after_ms: u64 },
}

/// A move from one step of a [`Failover`] chain to the next, reported as
/// [`StreamEvent::Switched`] and then as [`crate::Event::ProviderSwitched`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProviderSwitch {
    /// `provider:model` of the step that gave up.
    pub from: String,
    /// `provider:model` of the step tried next.
    pub to: String,
    pub reason: SwitchReason,
}

/// What a [`Failover`] does after a failed attempt.
#[derive(Debug, Clone, PartialEq)]
pub enum FailoverAction {
    /// Try the same step again after this delay.
    Retry(Duration),
    /// Try the same step again after `delay` with `request` instead (fewer
    /// output tokens, no images, default options). Later attempts on the
    /// step keep it; the next step starts from the original request.
    RetryWith {
        delay: Duration,
        request: Box<ModelRequest>,
    },
    /// Move to the next step.
    Next,
    /// Give up: no other step would do better.
    Stop,
}

/// Decides what follows a failed attempt. The default ([`DefaultFailover`])
/// retries what [`ProviderError::is_retryable`] allows on the same step,
/// then moves on; it moves on at once from every other error (another
/// provider may accept what this one refused) and stops only on a context
/// overflow (the caller must shrink the request).
/// A request with [`crate::RequestOptions::single_attempt`] stops at its
/// first error whatever the policy says, unless it also sets
/// [`crate::RequestOptions::fallback_when_unspent`] and the error proves
/// the attempt used nothing: then it moves on to the next step.
pub trait FailoverPolicy: Send + Sync {
    /// Shape `request` for this step before its first attempt: it already
    /// names the step's model ([`FailoverStep::model`]), so what is only
    /// valid for another model (a reasoning signature, an encrypted
    /// reasoning item) can be dropped here. Default: leave it as it is.
    fn prepare_step(&self, _request: &mut ModelRequest) {}

    /// Whether to send `request` on this step at all. An error skips the
    /// attempt and is handled as its failure (for example, a model the app
    /// knows is out of credit). Default: send it.
    fn before_attempt(&self, _request: &ModelRequest) -> Result<(), ProviderError> {
        Ok(())
    }

    /// `request` is what the failed attempt sent; `attempt` counts from 1;
    /// `attempts` is the step's limit.
    fn after_error(
        &self,
        request: &ModelRequest,
        error: &ProviderError,
        attempt: u32,
        attempts: u32,
    ) -> FailoverAction;
}

/// The default [`FailoverPolicy`].
#[derive(Debug, Clone, Copy)]
pub struct DefaultFailover {
    pub base_delay: Duration,
}

impl Default for DefaultFailover {
    fn default() -> Self {
        Self {
            base_delay: Duration::from_millis(500),
        }
    }
}

impl FailoverPolicy for DefaultFailover {
    fn after_error(
        &self,
        _request: &ModelRequest,
        error: &ProviderError,
        attempt: u32,
        attempts: u32,
    ) -> FailoverAction {
        match error {
            error if error.is_retryable() && attempt < attempts => {
                let delay = match error {
                    ProviderError::RateLimited {
                        retry_after: Some(after),
                    } => *after,
                    _ => self.base_delay * 2u32.saturating_pow(attempt.saturating_sub(1)),
                };
                FailoverAction::Retry(delay.min(MAX_BACKOFF))
            }
            ProviderError::ContextOverflow { .. } => FailoverAction::Stop,
            _ => FailoverAction::Next,
        }
    }
}

/// A provider chain: each step is tried in order until one answers.
///
/// Unlike wrapping the chain in one timeout, every attempt gets its own
/// [`FailoverStep::deadline`], so a step that hangs cannot use up the time
/// of the steps after it; a step whose attempt hit its deadline is not
/// retried. Once a step has started streaming, the chain is
/// committed to it: switching then would duplicate visible output.
///
/// Every move to the next step is yielded as [`StreamEvent::Switched`]
/// before the next step's events; the run loop reports it as
/// [`crate::Event::ProviderSwitched`]. [`Provider::complete`] has no way
/// to report them; the run loop always streams.
///
/// Which models form the chain, and in what order, is the app's policy.
pub struct Failover {
    steps: Vec<FailoverStep>,
    policy: Arc<dyn FailoverPolicy>,
    name: String,
}

impl Failover {
    pub fn new(steps: Vec<FailoverStep>) -> Self {
        Self {
            steps,
            policy: Arc::new(DefaultFailover::default()),
            name: "failover".into(),
        }
    }

    pub fn policy(mut self, policy: impl FailoverPolicy + 'static) -> Self {
        self.policy = Arc::new(policy);
        self
    }

    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    pub fn steps(&self) -> &[FailoverStep] {
        &self.steps
    }

    /// What follows a failed attempt that sent `sent`: retry the step
    /// (`Some`, possibly with a rewritten request), move on (`None`), or the
    /// error to give up with.
    fn next_move(
        &self,
        sent: &ModelRequest,
        step: &FailoverStep,
        attempt: u32,
        dispatched: bool,
        error: ProviderError,
    ) -> Result<Option<(Duration, Option<ModelRequest>)>, ProviderError> {
        if sent.options.single_attempt {
            let unspent = !dispatched || error.proves_nothing_spent();
            if sent.options.fallback_when_unspent && unspent {
                return Ok(None);
            }
            return Err(error);
        }
        match self
            .policy
            .after_error(sent, &error, attempt, step.attempts)
        {
            FailoverAction::Retry(delay) => Ok(Some((delay, None))),
            FailoverAction::RetryWith { delay, request } => Ok(Some((delay, Some(*request)))),
            FailoverAction::Next => Ok(None),
            FailoverAction::Stop => Err(error),
        }
    }
}

fn no_steps() -> ProviderError {
    ProviderError::Other("failover chain has no steps".into())
}

fn timed_out(deadline: Duration) -> ProviderError {
    ProviderError::Transient(format!("no answer within {deadline:?}"))
}

fn reason(error: &ProviderError, deadline: Duration) -> SwitchReason {
    if *error == timed_out(deadline) {
        SwitchReason::TimedOut {
            after_ms: deadline.as_millis().min(u64::MAX as u128) as u64,
        }
    } else {
        SwitchReason::Failed {
            error: error.to_string(),
        }
    }
}

#[async_trait]
impl Provider for Failover {
    fn name(&self) -> &str {
        &self.name
    }

    async fn complete(&self, request: &ModelRequest) -> Result<ModelResponse, ProviderError> {
        let mut last = None;
        for step in &self.steps {
            let mut step_request = step.request(request);
            self.policy.prepare_step(&mut step_request);
            let mut attempt = 0;
            let error = loop {
                attempt += 1;
                let mut dispatched = true;
                let error = match self.policy.before_attempt(&step_request) {
                    Err(error) => {
                        dispatched = false;
                        error
                    }
                    Ok(()) => match tokio::time::timeout(
                        step.deadline,
                        step.provider.complete(&step_request),
                    )
                    .await
                    {
                        Ok(Ok(response)) => return Ok(response),
                        Ok(Err(error)) => error,
                        // A step that hung is not retried: the next step
                        // gets the time instead (a single-attempt request
                        // stops below).
                        Err(_) if !step_request.options.single_attempt => {
                            break timed_out(step.deadline);
                        }
                        Err(_) => timed_out(step.deadline),
                    },
                };
                match self.next_move(&step_request, step, attempt, dispatched, error.clone())? {
                    Some((delay, rewritten)) => {
                        tokio::time::sleep(delay).await;
                        if let Some(rewritten) = rewritten {
                            step_request = rewritten;
                        }
                    }
                    None => break error,
                }
            };
            last = Some(error);
        }
        Err(last.unwrap_or_else(no_steps))
    }

    fn stream<'a>(
        &'a self,
        request: &'a ModelRequest,
    ) -> BoxStream<'a, Result<StreamEvent, ProviderError>> {
        // Each step's request is owned by the driver below and its stream
        // borrows it, so events reach the caller through a channel.
        let (tx, rx) = mpsc::unbounded::<Result<StreamEvent, ProviderError>>();
        let driver = async move {
            let mut last = None;
            for (index, step) in self.steps.iter().enumerate() {
                let mut step_request = step.request(request);
                self.policy.prepare_step(&mut step_request);
                let mut attempt = 0;
                let error = loop {
                    attempt += 1;
                    let mut dispatched = true;
                    let error = match self.policy.before_attempt(&step_request) {
                        Err(error) => {
                            dispatched = false;
                            error
                        }
                        Ok(()) => {
                            let mut events = step.provider.stream(&step_request);
                            match tokio::time::timeout(step.deadline, events.next()).await {
                                Ok(Some(Ok(first))) => {
                                    // Committed to this step: forward everything.
                                    if tx.unbounded_send(Ok(first)).is_err() {
                                        return;
                                    }
                                    while let Some(event) = events.next().await {
                                        if tx.unbounded_send(event).is_err() {
                                            return;
                                        }
                                    }
                                    return;
                                }
                                Ok(Some(Err(error))) => error,
                                Ok(None) => ProviderError::Other(
                                    "provider stream ended without a response".into(),
                                ),
                                // A step that hung is not retried: the next
                                // step gets the time instead (a single-attempt
                                // request stops below).
                                Err(_) if !step_request.options.single_attempt => {
                                    break timed_out(step.deadline);
                                }
                                Err(_) => timed_out(step.deadline),
                            }
                        }
                    };
                    match self.next_move(&step_request, step, attempt, dispatched, error.clone()) {
                        Ok(Some((delay, rewritten))) => {
                            tokio::time::sleep(delay).await;
                            if let Some(rewritten) = rewritten {
                                step_request = rewritten;
                            }
                        }
                        Ok(None) => break error,
                        Err(error) => {
                            let _ = tx.unbounded_send(Err(error));
                            return;
                        }
                    }
                };
                if let Some(next) = self.steps.get(index + 1) {
                    let switch = ProviderSwitch {
                        from: step.label(request),
                        to: next.label(request),
                        reason: reason(&error, step.deadline),
                    };
                    if tx
                        .unbounded_send(Ok(StreamEvent::Switched(switch)))
                        .is_err()
                    {
                        return;
                    }
                }
                last = Some(error);
            }
            let _ = tx.unbounded_send(Err(last.unwrap_or_else(no_steps)));
        };
        // The driver ends after its last send, so the merged stream ends
        // once the channel drains.
        Box::pin(stream::select(
            rx,
            stream::once(driver).filter_map(|()| async { None }),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::{ScriptedProvider, text};

    /// Never answers.
    struct Hanging;

    #[async_trait]
    impl Provider for Hanging {
        fn name(&self) -> &str {
            "hanging"
        }

        async fn complete(&self, _: &ModelRequest) -> Result<ModelResponse, ProviderError> {
            std::future::pending().await
        }
    }

    fn request() -> ModelRequest {
        ModelRequest {
            model: "primary-model".into(),
            messages: vec![crate::Message::user("hello")],
            tools: Vec::new(),
            tool_choice: Default::default(),
            max_output_tokens: None,
            ..Default::default()
        }
    }

    async fn collect(provider: &Failover) -> Vec<Result<StreamEvent, ProviderError>> {
        let request = request();
        provider.stream(&request).collect().await
    }

    /// The primary hangs; the fallback answers within its own deadline, and
    /// the switch is reported before the answer.
    #[tokio::test(start_paused = true)]
    async fn a_hanging_primary_falls_back_within_the_fallbacks_own_deadline() {
        let fallback = ScriptedProvider::new(vec![text("from the fallback")]);
        let chain = Failover::new(vec![
            FailoverStep::new(Hanging).deadline(Duration::from_secs(30)),
            FailoverStep::new(fallback)
                .model("fallback-model")
                .deadline(Duration::from_secs(30)),
        ]);
        let started = tokio::time::Instant::now();
        let events = collect(&chain).await;
        assert_eq!(started.elapsed(), Duration::from_secs(30));
        assert_eq!(
            events[0],
            Ok(StreamEvent::Switched(ProviderSwitch {
                from: "hanging:primary-model".into(),
                to: "scripted:fallback-model".into(),
                reason: SwitchReason::TimedOut { after_ms: 30_000 },
            }))
        );
        match events.last() {
            Some(Ok(StreamEvent::Done(response))) => assert_eq!(response.text, "from the fallback"),
            other => panic!("expected the fallback's answer, got {other:?}"),
        }
    }

    #[tokio::test(start_paused = true)]
    async fn complete_gives_each_attempt_its_own_deadline() {
        let chain = Failover::new(vec![
            FailoverStep::new(Hanging).deadline(Duration::from_secs(5)),
            FailoverStep::new(ScriptedProvider::new(vec![text("ok")])),
        ]);
        let response = chain.complete(&request()).await.unwrap();
        assert_eq!(response.text, "ok");
    }

    #[tokio::test(start_paused = true)]
    async fn transient_errors_retry_the_same_step_before_moving_on() {
        let flaky = ScriptedProvider::with_results(vec![
            Err(ProviderError::Transient("blip".into())),
            Ok(text("second try")),
        ]);
        let chain = Failover::new(vec![
            FailoverStep::new(flaky).attempts(2),
            FailoverStep::new(ScriptedProvider::new(vec![text("fallback")])),
        ]);
        let events = collect(&chain).await;
        assert!(
            !events
                .iter()
                .any(|e| matches!(e, Ok(StreamEvent::Switched(_))))
        );
        assert!(matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "second try"));
    }

    #[tokio::test]
    async fn a_request_one_provider_refuses_goes_to_the_next() {
        let chain = Failover::new(vec![
            FailoverStep::new(ScriptedProvider::with_results(vec![Err(
                ProviderError::InvalidRequest("bad schema".into()),
            )])),
            FailoverStep::new(ScriptedProvider::new(vec![text("next provider")])),
        ]);
        let events = collect(&chain).await;
        assert!(
            matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "next provider")
        );
    }

    #[tokio::test]
    async fn a_context_overflow_stops_the_chain() {
        let overflow = ProviderError::ContextOverflow {
            message: "too long".into(),
            limit: None,
        };
        let chain = Failover::new(vec![
            FailoverStep::new(ScriptedProvider::with_results(vec![Err(overflow.clone())])),
            FailoverStep::new(ScriptedProvider::new(vec![text("never")])),
        ]);
        assert_eq!(collect(&chain).await, vec![Err(overflow)]);
    }

    #[tokio::test]
    async fn when_every_step_fails_the_last_error_ends_the_stream() {
        let chain = Failover::new(vec![
            FailoverStep::new(ScriptedProvider::with_results(vec![Err(
                ProviderError::Auth("expired".into()),
            )])),
            FailoverStep::new(ScriptedProvider::with_results(vec![Err(
                ProviderError::Other("down".into()),
            )])),
        ]);
        let events = collect(&chain).await;
        assert!(
            matches!(&events[0], Ok(StreamEvent::Switched(s)) if s.reason == SwitchReason::Failed { error: "authentication failed: expired".into() })
        );
        assert_eq!(
            events.last(),
            Some(&Err(ProviderError::Other("down".into())))
        );
        assert_eq!(events.len(), 2);
    }

    /// Through an agent run, the switch is a typed run event.
    #[tokio::test(start_paused = true)]
    async fn a_run_reports_the_switch_as_an_event() {
        let events = Arc::new(std::sync::Mutex::new(Vec::new()));
        let sink = {
            let events = events.clone();
            move |_: crate::RunId, event: &crate::Event| events.lock().unwrap().push(event.clone())
        };
        let agent = crate::Agent::builder("failover")
            .model("primary-model")
            .provider(Failover::new(vec![
                FailoverStep::new(Hanging).deadline(Duration::from_secs(10)),
                FailoverStep::new(ScriptedProvider::new(vec![text("answered")])),
            ]))
            .event_sink(sink)
            .build()
            .unwrap();
        let outcome = agent.run("cli:synthetic-user-1", "hello").await.unwrap();
        assert_eq!(outcome.final_text.as_deref(), Some("answered"));
        let events = events.lock().unwrap();
        assert!(events.iter().any(|event| matches!(
            event,
            crate::Event::ProviderSwitched { turn: 1, switch }
                if switch.from == "hanging:primary-model"
                    && switch.reason == SwitchReason::TimedOut { after_ms: 10_000 }
        )));
    }

    /// Retries once with an output cap after a billing error, and skips a
    /// model it knows is out of credit without calling it.
    struct CapThenSkip;

    impl FailoverPolicy for CapThenSkip {
        fn before_attempt(&self, request: &ModelRequest) -> Result<(), ProviderError> {
            if request.model == "broke-model" {
                return Err(ProviderError::Billing {
                    message: "cached".into(),
                    affordable_tokens: None,
                });
            }
            Ok(())
        }

        fn after_error(
            &self,
            request: &ModelRequest,
            error: &ProviderError,
            _attempt: u32,
            _attempts: u32,
        ) -> FailoverAction {
            match error {
                ProviderError::Billing {
                    affordable_tokens: Some(tokens),
                    ..
                } if request.max_output_tokens.is_none() => {
                    let mut capped = request.clone();
                    capped.max_output_tokens = Some(*tokens as u32);
                    FailoverAction::RetryWith {
                        delay: Duration::ZERO,
                        request: Box::new(capped),
                    }
                }
                _ => FailoverAction::Next,
            }
        }
    }

    #[tokio::test]
    async fn a_policy_can_retry_with_a_rewritten_request() {
        let primary = Arc::new(ScriptedProvider::with_results(vec![
            Err(ProviderError::Billing {
                message: "402".into(),
                affordable_tokens: Some(600),
            }),
            Ok(text("capped answer")),
        ]));
        let chain = Failover::new(vec![FailoverStep::from_arc(primary.clone()).attempts(3)])
            .policy(CapThenSkip);
        let events = collect(&chain).await;
        assert!(
            matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "capped answer")
        );
        let sent = primary.requests();
        assert_eq!(sent[0].max_output_tokens, None);
        assert_eq!(sent[1].max_output_tokens, Some(600));
    }

    #[tokio::test]
    async fn a_policy_can_skip_an_attempt_without_sending_it() {
        let broke = Arc::new(ScriptedProvider::new(vec![text("never")]));
        let chain = Failover::new(vec![
            FailoverStep::from_arc(broke.clone()).model("broke-model"),
            FailoverStep::new(ScriptedProvider::new(vec![text("from the fallback")])),
        ])
        .policy(CapThenSkip);
        let events = collect(&chain).await;
        assert!(broke.requests().is_empty());
        assert!(
            matches!(&events[0], Ok(StreamEvent::Switched(s)) if s.from == "scripted:broke-model")
        );
        assert!(
            matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "from the fallback")
        );
    }

    /// Marks each request with the model it was prepared for (1 for
    /// `model-a`, 2 for any other).
    struct TagModel;

    impl FailoverPolicy for TagModel {
        fn prepare_step(&self, request: &mut ModelRequest) {
            request.max_output_tokens = Some(if request.model == "model-a" { 1 } else { 2 });
        }

        fn after_error(
            &self,
            _request: &ModelRequest,
            _error: &ProviderError,
            _attempt: u32,
            _attempts: u32,
        ) -> FailoverAction {
            FailoverAction::Next
        }
    }

    /// Each step's request is prepared for that step's own model, not the
    /// one the caller asked for.
    #[tokio::test]
    async fn each_step_is_prepared_for_its_own_model() {
        let first = Arc::new(ScriptedProvider::with_results(vec![Err(
            ProviderError::Transient("down".into()),
        )]));
        let second = Arc::new(ScriptedProvider::new(vec![text("from the fallback")]));
        let chain = Failover::new(vec![
            FailoverStep::from_arc(first.clone()).model("model-a"),
            FailoverStep::from_arc(second.clone()).model("model-b"),
        ])
        .policy(TagModel);
        let events = collect(&chain).await;
        assert!(
            matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "from the fallback")
        );
        assert_eq!(first.requests()[0].max_output_tokens, Some(1));
        assert_eq!(second.requests()[0].max_output_tokens, Some(2));
    }

    /// A hung step is not retried, even when its policy would retry a
    /// transient error; a single-attempt request stops at the first one.
    #[tokio::test(start_paused = true)]
    async fn a_timed_out_step_moves_on_unless_the_request_is_single_attempt() {
        let chain = || {
            Failover::new(vec![
                FailoverStep::new(Hanging)
                    .deadline(Duration::from_secs(5))
                    .attempts(5),
                FailoverStep::new(ScriptedProvider::new(vec![text("fallback")])),
            ])
        };
        let started = tokio::time::Instant::now();
        let events = collect(&chain()).await;
        assert_eq!(started.elapsed(), Duration::from_secs(5));
        assert!(matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "fallback"));

        let mut single = request();
        single.options.single_attempt = true;
        let events: Vec<_> = chain().stream(&single).collect().await;
        assert!(matches!(
            events.as_slice(),
            [Err(ProviderError::Transient(_))]
        ));
    }

    /// A budget-fenced call moves on only past an error that proves the
    /// attempt used nothing; anything else still ends the chain.
    #[tokio::test]
    async fn a_fenced_call_moves_on_only_past_an_unspent_error() {
        let fenced = || {
            let mut request = request();
            request.options.single_attempt = true;
            request.options.fallback_when_unspent = true;
            request
        };
        let chain = |first: ProviderError| {
            Failover::new(vec![
                FailoverStep::new(ScriptedProvider::with_results(vec![Err(first)])),
                FailoverStep::new(ScriptedProvider::new(vec![text("fallback")])),
            ])
        };
        let refused = ProviderError::QuotaExhausted {
            message: "limit".into(),
            resets_at: None,
            per_model: false,
            in_stream: false,
        };
        for unspent in [
            refused.clone(),
            ProviderError::Auth("bad key".into()),
            ProviderError::NotFound("gone".into()),
        ] {
            let events: Vec<_> = chain(unspent).stream(&fenced()).collect().await;
            assert!(
                matches!(events.last(), Some(Ok(StreamEvent::Done(r))) if r.text == "fallback")
            );
        }
        for spent in [
            ProviderError::QuotaExhausted {
                message: "limit".into(),
                resets_at: None,
                per_model: false,
                in_stream: true,
            },
            ProviderError::Transient("503".into()),
            ProviderError::RateLimited { retry_after: None },
        ] {
            let events: Vec<_> = chain(spent).stream(&fenced()).collect().await;
            assert!(matches!(events.as_slice(), [Err(_)]), "{events:?}");
        }
    }

    #[test]
    fn the_default_policy_caps_backoff_and_honours_retry_after() {
        let policy = DefaultFailover::default();
        let limited = ProviderError::RateLimited {
            retry_after: Some(Duration::from_secs(120)),
        };
        assert_eq!(
            policy.after_error(&request(), &limited, 1, 3),
            FailoverAction::Retry(MAX_BACKOFF)
        );
        assert_eq!(
            policy.after_error(&request(), &limited, 3, 3),
            FailoverAction::Next
        );
        let blip = ProviderError::Transient("x".into());
        assert_eq!(
            policy.after_error(&request(), &blip, 2, 3),
            FailoverAction::Retry(Duration::from_secs(1))
        );
    }
}