codex-helper-core 0.15.0

Core library for codex-helper.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};

use crate::config::{ServiceConfig, UpstreamConfig};
use crate::runtime_identity::ProviderEndpointKey;
use tracing::info;

pub const FAILURE_THRESHOLD: u32 = 3;
pub const COOLDOWN_SECS: u64 = 30;

#[derive(Debug, Clone, Copy)]
pub struct CooldownBackoff {
    pub factor: u64,
    pub max_secs: u64,
}

impl CooldownBackoff {
    pub(crate) fn effective_cooldown_secs(&self, base_secs: u64, penalty_streak: u32) -> u64 {
        if base_secs == 0 {
            return 0;
        }
        if self.factor <= 1 {
            return base_secs;
        }
        let cap = if self.max_secs == 0 {
            base_secs
        } else {
            self.max_secs.max(base_secs)
        };

        let mut secs = base_secs;
        for _ in 0..penalty_streak.min(64) {
            secs = secs.saturating_mul(self.factor);
            if secs >= cap {
                return cap;
            }
        }
        secs.min(cap)
    }
}

#[derive(Debug, Clone, Default)]
pub struct LbState {
    pub failure_counts: Vec<u32>,
    pub cooldown_until: Vec<Option<std::time::Instant>>,
    pub usage_exhausted: Vec<bool>,
    pub last_good_index: Option<usize>,
    pub penalty_streak: Vec<u32>,
    pub(crate) upstream_signature: Vec<String>,
}

impl LbState {
    pub(crate) fn ensure_layout(&mut self, service_name: &str, upstreams: &[UpstreamConfig]) {
        let signature = upstreams
            .iter()
            .enumerate()
            .map(|(idx, upstream)| upstream_signature_key(service_name, idx, upstream))
            .collect::<Vec<_>>();
        let legacy_signature = upstreams
            .iter()
            .map(|upstream| upstream.base_url.clone())
            .collect::<Vec<_>>();

        if has_duplicate_signatures(&signature) {
            self.reset_for_layout(signature);
            return;
        }

        let len = upstreams.len();
        if self.upstream_signature == signature
            && self.failure_counts.len() == len
            && self.cooldown_until.len() == len
            && self.usage_exhausted.len() == len
            && self.penalty_streak.len() == len
        {
            return;
        }

        self.migrate_layout(signature, legacy_signature);
    }

    fn reset_for_layout(&mut self, signature: Vec<String>) {
        let len = signature.len();
        self.failure_counts = vec![0; len];
        self.cooldown_until = vec![None; len];
        self.usage_exhausted = vec![false; len];
        self.penalty_streak = vec![0; len];
        // upstream 布局变化时,原来的粘性索引不再可信,直接清空。
        self.last_good_index = None;
        self.upstream_signature = signature;
    }

    fn migrate_layout(&mut self, signature: Vec<String>, legacy_signature: Vec<String>) {
        if self.upstream_signature.is_empty() {
            self.reset_for_layout(signature);
            return;
        }

        let old_signature = std::mem::take(&mut self.upstream_signature);
        if has_duplicate_signatures(&old_signature) {
            self.reset_for_layout(signature);
            return;
        }

        let old_index_by_signature = old_signature
            .iter()
            .enumerate()
            .map(|(idx, key)| (key.clone(), idx))
            .collect::<std::collections::HashMap<_, _>>();
        let legacy_fallback_enabled = !has_duplicate_signatures(&legacy_signature);

        let old_failure_counts = std::mem::take(&mut self.failure_counts);
        let old_cooldown_until = std::mem::take(&mut self.cooldown_until);
        let old_usage_exhausted = std::mem::take(&mut self.usage_exhausted);
        let old_penalty_streak = std::mem::take(&mut self.penalty_streak);
        let old_last_good_index = self.last_good_index.take();

        let len = signature.len();
        self.failure_counts = vec![0; len];
        self.cooldown_until = vec![None; len];
        self.usage_exhausted = vec![false; len];
        self.penalty_streak = vec![0; len];

        for (new_idx, key) in signature.iter().enumerate() {
            let old_idx = old_index_by_signature.get(key).copied().or_else(|| {
                legacy_fallback_enabled
                    .then(|| legacy_signature.get(new_idx))
                    .flatten()
                    .and_then(|legacy_key| old_index_by_signature.get(legacy_key).copied())
            });
            let Some(old_idx) = old_idx else {
                continue;
            };
            self.failure_counts[new_idx] = old_failure_counts.get(old_idx).copied().unwrap_or(0);
            self.cooldown_until[new_idx] = old_cooldown_until.get(old_idx).and_then(|until| *until);
            self.usage_exhausted[new_idx] =
                old_usage_exhausted.get(old_idx).copied().unwrap_or(false);
            self.penalty_streak[new_idx] = old_penalty_streak.get(old_idx).copied().unwrap_or(0);
        }

        self.last_good_index = old_last_good_index.and_then(|old_idx| {
            old_signature.get(old_idx).and_then(|key| {
                signature
                    .iter()
                    .position(|new_key| new_key == key)
                    .or_else(|| {
                        legacy_fallback_enabled
                            .then(|| {
                                legacy_signature
                                    .iter()
                                    .position(|legacy_key| legacy_key == key)
                            })
                            .flatten()
                    })
            })
        });
        self.upstream_signature = signature;
    }
}

fn has_duplicate_signatures(values: &[String]) -> bool {
    let mut seen = HashSet::new();
    values.iter().any(|value| !seen.insert(value))
}

fn upstream_signature_key(
    service_name: &str,
    upstream_index: usize,
    upstream: &UpstreamConfig,
) -> String {
    let provider_id = upstream
        .tags
        .get("provider_id")
        .cloned()
        .unwrap_or_else(|| format!("{service_name}#{upstream_index}"));
    let endpoint_id = upstream
        .tags
        .get("endpoint_id")
        .cloned()
        .unwrap_or_else(|| upstream_index.to_string());
    let provider_endpoint = ProviderEndpointKey::new(service_name, provider_id, endpoint_id);
    format!("{}|{}", provider_endpoint.stable_key(), upstream.base_url)
}

/// Upstream selection result
#[derive(Debug, Clone)]
pub struct SelectedUpstream {
    pub station_name: String,
    pub index: usize,
    pub upstream: UpstreamConfig,
}

/// 简单的负载选择器,当前仅按权重随机,未来可扩展为按 usage / 失败次数等切换。
#[derive(Clone)]
pub struct LoadBalancer {
    pub service: Arc<ServiceConfig>,
    pub states: Arc<Mutex<HashMap<String, LbState>>>,
}

impl LoadBalancer {
    pub fn new(service: Arc<ServiceConfig>, states: Arc<Mutex<HashMap<String, LbState>>>) -> Self {
        Self { service, states }
    }

    #[cfg(test)]
    pub fn select_upstream(&self) -> Option<SelectedUpstream> {
        self.select_upstream_avoiding(&HashSet::new())
    }

    pub fn select_upstream_avoiding(&self, avoid: &HashSet<usize>) -> Option<SelectedUpstream> {
        self.select_upstream_avoiding_inner(avoid, false)
    }

    pub fn select_upstream_avoiding_strict(
        &self,
        avoid: &HashSet<usize>,
    ) -> Option<SelectedUpstream> {
        self.select_upstream_avoiding_inner(avoid, true)
    }

    fn select_upstream_avoiding_inner(
        &self,
        avoid: &HashSet<usize>,
        strict: bool,
    ) -> Option<SelectedUpstream> {
        if self.service.upstreams.is_empty() {
            return None;
        }

        let mut map = match self.states.lock() {
            Ok(m) => m,
            Err(e) => e.into_inner(),
        };
        let entry = map.entry(self.service.name.clone()).or_default();
        entry.ensure_layout(self.service.name.as_str(), &self.service.upstreams);

        let now = std::time::Instant::now();

        // 更新冷却状态:如果冷却期已过,重置失败计数和冷却时间。
        for idx in 0..self.service.upstreams.len() {
            if let Some(until) = entry.cooldown_until.get(idx).and_then(|v| *v)
                && now >= until
            {
                entry.failure_counts[idx] = 0;
                if let Some(slot) = entry.cooldown_until.get_mut(idx) {
                    *slot = None;
                }
            }
        }

        // 优先使用最近一次“成功”的 upstream,实现粘性路由:
        // 一旦已经切换到可用线路,就尽量保持在该线路上,而不是每次都从头熔断。
        if let Some(idx) = entry.last_good_index
            && idx < self.service.upstreams.len()
            && entry.failure_counts[idx] < FAILURE_THRESHOLD
            && !entry.usage_exhausted.get(idx).copied().unwrap_or(false)
            && !avoid.contains(&idx)
        {
            let upstream = self.service.upstreams[idx].clone();
            return Some(SelectedUpstream {
                station_name: self.service.name.clone(),
                index: idx,
                upstream,
            });
        }

        // 第一轮:按顺序选择第一个「未熔断 + 未标记用量用尽」的 upstream。
        if let Some(idx) = self
            .service
            .upstreams
            .iter()
            .enumerate()
            .find_map(|(idx, _)| {
                if avoid.contains(&idx) {
                    return None;
                }
                if entry.failure_counts[idx] >= FAILURE_THRESHOLD {
                    return None;
                }
                if entry.usage_exhausted.get(idx).copied().unwrap_or(false) {
                    return None;
                }
                Some(idx)
            })
        {
            let upstream = self.service.upstreams[idx].clone();
            return Some(SelectedUpstream {
                station_name: self.service.name.clone(),
                index: idx,
                upstream,
            });
        }

        // 第二轮:忽略 usage_exhausted,只看失败阈值,仍然按顺序选第一个。
        if let Some(idx) = self
            .service
            .upstreams
            .iter()
            .enumerate()
            .find_map(|(idx, _)| {
                if avoid.contains(&idx) {
                    return None;
                }
                if entry.failure_counts[idx] >= FAILURE_THRESHOLD {
                    None
                } else {
                    Some(idx)
                }
            })
        {
            let upstream = self.service.upstreams[idx].clone();
            return Some(SelectedUpstream {
                station_name: self.service.name.clone(),
                index: idx,
                upstream,
            });
        }

        if strict {
            return None;
        }

        // 兜底:所有 upstream 都已达到失败阈值时,仍然返回第一个,以保证永远有兜底。
        // 如果 avoid 把所有都排除了,则兜底返回第一个“非 avoid”的 upstream;仍然没有则返回 0。
        let idx = (0..self.service.upstreams.len())
            .find(|i| !avoid.contains(i))
            .unwrap_or(0);
        let upstream = self.service.upstreams[idx].clone();
        Some(SelectedUpstream {
            station_name: self.service.name.clone(),
            index: idx,
            upstream,
        })
    }

    pub fn penalize_with_backoff(
        &self,
        index: usize,
        cooldown_secs: u64,
        reason: &str,
        backoff: CooldownBackoff,
    ) {
        let mut map = match self.states.lock() {
            Ok(m) => m,
            Err(_) => return,
        };
        let entry = map
            .entry(self.service.name.clone())
            .or_insert_with(LbState::default);
        entry.ensure_layout(self.service.name.as_str(), &self.service.upstreams);
        if index >= entry.failure_counts.len() {
            return;
        }

        let streak = entry.penalty_streak.get(index).copied().unwrap_or(0);
        let effective_secs = backoff.effective_cooldown_secs(cooldown_secs, streak);

        entry.failure_counts[index] = FAILURE_THRESHOLD;
        if let Some(slot) = entry.cooldown_until.get_mut(index) {
            *slot =
                Some(std::time::Instant::now() + std::time::Duration::from_secs(effective_secs));
        }
        if let Some(slot) = entry.penalty_streak.get_mut(index) {
            *slot = streak.saturating_add(1);
        }
        if entry.last_good_index == Some(index) {
            entry.last_good_index = None;
        }
        info!(
            "lb: upstream '{}' index {} penalized for {}s (reason: {})",
            self.service.name, index, effective_secs, reason
        );
    }

    pub fn record_result_with_backoff(
        &self,
        index: usize,
        success: bool,
        failure_threshold_cooldown_secs: u64,
        backoff: CooldownBackoff,
    ) {
        let mut map = match self.states.lock() {
            Ok(m) => m,
            Err(_) => return,
        };
        let entry = map
            .entry(self.service.name.clone())
            .or_insert_with(LbState::default);
        entry.ensure_layout(self.service.name.as_str(), &self.service.upstreams);
        if index >= entry.failure_counts.len() {
            return;
        }
        if success {
            entry.failure_counts[index] = 0;
            if let Some(slot) = entry.cooldown_until.get_mut(index) {
                *slot = None;
            }
            if let Some(slot) = entry.penalty_streak.get_mut(index) {
                *slot = 0;
            }
            // 成功请求会将该 upstream 记为“最近可用线路”,后续优先继续使用。
            entry.last_good_index = Some(index);
        } else {
            entry.failure_counts[index] = entry.failure_counts[index].saturating_add(1);
            if entry.failure_counts[index] >= FAILURE_THRESHOLD
                && let Some(slot) = entry.cooldown_until.get_mut(index)
            {
                let base_secs = if failure_threshold_cooldown_secs == 0 {
                    COOLDOWN_SECS
                } else {
                    failure_threshold_cooldown_secs
                };
                let streak = entry.penalty_streak.get(index).copied().unwrap_or(0);
                let effective_secs = backoff.effective_cooldown_secs(base_secs, streak);
                let now = std::time::Instant::now();
                let new_until = now + std::time::Duration::from_secs(effective_secs);
                let should_update = match *slot {
                    Some(existing) => new_until > existing,
                    None => true,
                };
                if should_update {
                    *slot = Some(new_until);
                }
                if let Some(slot) = entry.penalty_streak.get_mut(index) {
                    *slot = streak.saturating_add(1);
                }
                info!(
                    "lb: upstream '{}' index {} reached failure threshold {} (count = {}), entering cooldown for {}s",
                    self.service.name,
                    index,
                    FAILURE_THRESHOLD,
                    entry.failure_counts[index],
                    effective_secs
                );
                // 触发熔断时,如当前 last_good_index 指向该线路,则清空,允许后续选择其他线路。
                if entry.last_good_index == Some(index) {
                    entry.last_good_index = None;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{ServiceConfig, UpstreamAuth, UpstreamConfig};

    fn make_service(name: &str, urls: &[&str]) -> ServiceConfig {
        ServiceConfig {
            name: name.to_string(),
            alias: None,
            enabled: true,
            level: 1,
            upstreams: urls
                .iter()
                .map(|u| UpstreamConfig {
                    base_url: u.to_string(),
                    auth: UpstreamAuth {
                        auth_token: Some("sk-test".to_string()),
                        auth_token_env: None,
                        api_key: None,
                        api_key_env: None,
                    },
                    tags: HashMap::new(),
                    supported_models: HashMap::new(),
                    model_mapping: HashMap::new(),
                })
                .collect(),
        }
    }

    fn make_provider_endpoint_service(
        name: &str,
        upstreams: &[(&str, &str, &str)],
    ) -> ServiceConfig {
        ServiceConfig {
            name: name.to_string(),
            alias: None,
            enabled: true,
            level: 1,
            upstreams: upstreams
                .iter()
                .map(|(base_url, provider_id, endpoint_id)| UpstreamConfig {
                    base_url: (*base_url).to_string(),
                    auth: UpstreamAuth {
                        auth_token: Some("sk-test".to_string()),
                        auth_token_env: None,
                        api_key: None,
                        api_key_env: None,
                    },
                    tags: HashMap::from([
                        ("provider_id".to_string(), (*provider_id).to_string()),
                        ("endpoint_id".to_string(), (*endpoint_id).to_string()),
                    ]),
                    supported_models: HashMap::new(),
                    model_mapping: HashMap::new(),
                })
                .collect(),
        }
    }

    #[test]
    fn lb_prefers_non_exhausted_upstream_when_available() {
        let service = make_service(
            "codex-main",
            &["https://primary.example", "https://backup.example"],
        );
        let states = Arc::new(Mutex::new(HashMap::new()));
        let lb = LoadBalancer::new(Arc::new(service), states.clone());

        // 初次选择应选第一个 upstream(index 0)。
        let first = lb.select_upstream().expect("should select an upstream");
        assert_eq!(first.index, 0);

        // 标记 index 0 为 usage_exhausted,index 1 为可用。
        {
            let mut guard = states.lock().unwrap();
            let entry = guard
                .entry("codex-main".to_string())
                .or_insert_with(LbState::default);
            entry.ensure_layout(lb.service.name.as_str(), &lb.service.upstreams);
            entry.usage_exhausted[0] = true;
            entry.usage_exhausted[1] = false;
        }

        // 此时应优先选择未 exhausted 的 index 1。
        let second = lb.select_upstream().expect("should select backup upstream");
        assert_eq!(second.index, 1);
    }

    #[test]
    fn lb_falls_back_when_all_exhausted() {
        let service = make_service(
            "codex-main",
            &["https://primary.example", "https://backup.example"],
        );
        let states = Arc::new(Mutex::new(HashMap::new()));
        let lb = LoadBalancer::new(Arc::new(service), states.clone());

        // 初始化状态
        let _ = lb.select_upstream();

        {
            let mut guard = states.lock().unwrap();
            let entry = guard
                .entry("codex-main".to_string())
                .or_insert_with(LbState::default);
            entry.ensure_layout(lb.service.name.as_str(), &lb.service.upstreams);
            entry.usage_exhausted[0] = true;
            entry.usage_exhausted[1] = true;
        }

        // 所有 upstream 都 exhausted 时,仍然应返回 index 0 做兜底。
        let selected = lb
            .select_upstream()
            .expect("should still select an upstream");
        assert_eq!(selected.index, 0);
    }

    #[test]
    fn lb_strict_mode_still_falls_back_when_all_usage_exhausted() {
        let service = make_service(
            "codex-main",
            &["https://primary.example", "https://backup.example"],
        );
        let states = Arc::new(Mutex::new(HashMap::new()));
        let lb = LoadBalancer::new(Arc::new(service), states.clone());

        {
            let mut guard = states.lock().unwrap();
            let entry = guard
                .entry("codex-main".to_string())
                .or_insert_with(LbState::default);
            entry.ensure_layout(lb.service.name.as_str(), &lb.service.upstreams);
            entry.usage_exhausted[0] = true;
            entry.usage_exhausted[1] = true;
        }

        let selected = lb
            .select_upstream_avoiding_strict(&HashSet::new())
            .expect("strict mode should still ignore usage exhaustion on fallback");
        assert_eq!(selected.index, 0);
    }

    #[test]
    fn lb_resets_state_when_upstream_layout_changes() {
        let states = Arc::new(Mutex::new(HashMap::new()));
        let initial = LoadBalancer::new(
            Arc::new(make_service(
                "codex-main",
                &["https://primary.example", "https://backup.example"],
            )),
            states.clone(),
        );
        initial.record_result_with_backoff(
            0,
            false,
            COOLDOWN_SECS,
            CooldownBackoff {
                factor: 1,
                max_secs: 0,
            },
        );

        {
            let guard = states.lock().unwrap();
            let entry = guard.get("codex-main").expect("state exists");
            assert_eq!(entry.failure_counts, vec![1, 0]);
        }

        let reordered = LoadBalancer::new(
            Arc::new(make_service(
                "codex-main",
                &["https://backup.example", "https://primary.example"],
            )),
            states.clone(),
        );
        let selected = reordered
            .select_upstream()
            .expect("should select an upstream");
        assert_eq!(selected.index, 0);

        let guard = states.lock().unwrap();
        let entry = guard.get("codex-main").expect("state exists");
        assert_eq!(entry.failure_counts, vec![0, 0]);
        assert_eq!(entry.last_good_index, None);
    }

    #[test]
    fn lb_migrates_state_when_provider_endpoint_order_changes() {
        let states = Arc::new(Mutex::new(HashMap::new()));
        let initial = LoadBalancer::new(
            Arc::new(make_provider_endpoint_service(
                "routing",
                &[
                    ("https://primary.example", "primary", "default"),
                    ("https://backup.example", "backup", "default"),
                ],
            )),
            states.clone(),
        );

        {
            let mut guard = states.lock().unwrap();
            let entry = guard
                .entry("routing".to_string())
                .or_insert_with(LbState::default);
            entry.ensure_layout(initial.service.name.as_str(), &initial.service.upstreams);
            entry.failure_counts[0] = 2;
            entry.cooldown_until[0] =
                Some(std::time::Instant::now() + std::time::Duration::from_secs(30));
            entry.penalty_streak[0] = 3;
            entry.usage_exhausted[1] = true;
            entry.last_good_index = Some(1);
        }

        let reordered = LoadBalancer::new(
            Arc::new(make_provider_endpoint_service(
                "routing",
                &[
                    ("https://backup.example", "backup", "default"),
                    ("https://primary.example", "primary", "default"),
                ],
            )),
            states.clone(),
        );
        let selected = reordered
            .select_upstream()
            .expect("should select a migrated non-exhausted upstream");
        assert_eq!(selected.index, 1);

        let guard = states.lock().unwrap();
        let entry = guard.get("routing").expect("state exists");
        assert_eq!(entry.failure_counts, vec![0, 2]);
        assert_eq!(entry.usage_exhausted, vec![true, false]);
        assert_eq!(entry.penalty_streak, vec![0, 3]);
        assert!(entry.cooldown_until[0].is_none());
        assert!(entry.cooldown_until[1].is_some());
        assert_eq!(entry.last_good_index, Some(0));
    }

    #[test]
    fn lb_migrates_legacy_base_url_signature_when_endpoint_identity_is_unambiguous() {
        let states = Arc::new(Mutex::new(HashMap::new()));
        let primary_url = "https://primary.example";
        let backup_url = "https://backup.example";

        {
            let mut guard = states.lock().unwrap();
            guard.insert(
                "routing".to_string(),
                LbState {
                    failure_counts: vec![FAILURE_THRESHOLD, 0],
                    cooldown_until: vec![None, None],
                    usage_exhausted: vec![false, true],
                    last_good_index: Some(1),
                    penalty_streak: vec![2, 0],
                    upstream_signature: vec![primary_url.to_string(), backup_url.to_string()],
                },
            );
        }

        let reordered = LoadBalancer::new(
            Arc::new(make_provider_endpoint_service(
                "routing",
                &[
                    (backup_url, "backup", "default"),
                    (primary_url, "primary", "default"),
                ],
            )),
            states.clone(),
        );
        {
            let mut guard = states.lock().unwrap();
            let entry = guard.get_mut("routing").expect("state exists");
            entry.ensure_layout(
                reordered.service.name.as_str(),
                &reordered.service.upstreams,
            );
        }

        let guard = states.lock().unwrap();
        let entry = guard.get("routing").expect("state exists");
        assert_eq!(entry.failure_counts, vec![0, FAILURE_THRESHOLD]);
        assert_eq!(entry.usage_exhausted, vec![true, false]);
        assert_eq!(entry.penalty_streak, vec![0, 2]);
        assert_eq!(entry.last_good_index, Some(0));
    }

    #[test]
    fn lb_replaces_state_when_provider_endpoint_base_url_changes() {
        let states = Arc::new(Mutex::new(HashMap::new()));
        let initial = LoadBalancer::new(
            Arc::new(make_provider_endpoint_service(
                "routing",
                &[("https://old.example", "input", "default")],
            )),
            states.clone(),
        );

        {
            let mut guard = states.lock().unwrap();
            let entry = guard
                .entry("routing".to_string())
                .or_insert_with(LbState::default);
            entry.ensure_layout(initial.service.name.as_str(), &initial.service.upstreams);
            entry.failure_counts[0] = FAILURE_THRESHOLD;
            entry.cooldown_until[0] =
                Some(std::time::Instant::now() + std::time::Duration::from_secs(30));
            entry.usage_exhausted[0] = true;
            entry.penalty_streak[0] = 2;
            entry.last_good_index = Some(0);
        }

        let updated = LoadBalancer::new(
            Arc::new(make_provider_endpoint_service(
                "routing",
                &[("https://new.example", "input", "default")],
            )),
            states.clone(),
        );
        let selected = updated
            .select_upstream()
            .expect("new endpoint URL should be selectable after state replacement");
        assert_eq!(selected.index, 0);

        let guard = states.lock().unwrap();
        let entry = guard.get("routing").expect("state exists");
        assert_eq!(entry.failure_counts, vec![0]);
        assert_eq!(entry.cooldown_until, vec![None]);
        assert_eq!(entry.usage_exhausted, vec![false]);
        assert_eq!(entry.penalty_streak, vec![0]);
        assert_eq!(entry.last_good_index, None);
    }

    #[test]
    fn lb_avoids_upstreams_past_failure_threshold() {
        let service = make_service(
            "codex-main",
            &["https://primary.example", "https://backup.example"],
        );
        let states = Arc::new(Mutex::new(HashMap::new()));
        let lb = LoadBalancer::new(Arc::new(service), states.clone());

        let disabled_backoff = CooldownBackoff {
            factor: 1,
            max_secs: 0,
        };

        // 对 primary 连续记录 FAILURE_THRESHOLD 次失败。
        for _ in 0..FAILURE_THRESHOLD {
            lb.record_result_with_backoff(0, false, COOLDOWN_SECS, disabled_backoff);
        }

        // 此时应选择 backup(index 1),因为 index 0 已达到失败阈值。
        let selected = lb
            .select_upstream()
            .expect("should select backup after failures");
        assert_eq!(selected.index, 1);
    }

    #[test]
    fn lb_cooldown_expiry_restores_upstream_selection() {
        let service = make_service(
            "codex-main",
            &["https://primary.example", "https://backup.example"],
        );
        let states = Arc::new(Mutex::new(HashMap::new()));
        let lb = LoadBalancer::new(Arc::new(service), states.clone());

        let disabled_backoff = CooldownBackoff {
            factor: 1,
            max_secs: 0,
        };

        for _ in 0..FAILURE_THRESHOLD {
            lb.record_result_with_backoff(0, false, 2, disabled_backoff);
        }

        {
            let guard = states.lock().unwrap();
            let entry = guard.get("codex-main").expect("lb state exists");
            assert_eq!(entry.failure_counts[0], FAILURE_THRESHOLD);
            assert!(entry.cooldown_until[0].is_some());
        }

        let during_cooldown = lb
            .select_upstream()
            .expect("should select backup while primary cools down");
        assert_eq!(during_cooldown.index, 1);

        {
            let mut guard = states.lock().unwrap();
            let entry = guard.get_mut("codex-main").expect("lb state exists");
            entry.cooldown_until[0] =
                Some(std::time::Instant::now() - std::time::Duration::from_secs(1));
        }

        let recovered = lb
            .select_upstream()
            .expect("should select primary after cooldown expiry");
        assert_eq!(recovered.index, 0);

        {
            let guard = states.lock().unwrap();
            let entry = guard.get("codex-main").expect("lb state exists");
            assert_eq!(entry.failure_counts[0], 0);
            assert!(entry.cooldown_until[0].is_none());
        }
    }

    #[test]
    fn lb_threshold_cooldown_backoff_grows_and_success_resets_streak() {
        let service = make_service(
            "codex-main",
            &["https://primary.example", "https://backup.example"],
        );
        let states = Arc::new(Mutex::new(HashMap::new()));
        let lb = LoadBalancer::new(Arc::new(service), states.clone());

        let backoff = CooldownBackoff {
            factor: 2,
            max_secs: 10,
        };

        for _ in 0..FAILURE_THRESHOLD {
            lb.record_result_with_backoff(0, false, 2, backoff);
        }

        let first_remaining_secs = {
            let guard = states.lock().unwrap();
            let entry = guard.get("codex-main").expect("lb state exists");
            assert_eq!(entry.penalty_streak[0], 1);
            entry.cooldown_until[0]
                .map(|until| {
                    until
                        .saturating_duration_since(std::time::Instant::now())
                        .as_secs()
                })
                .expect("first cooldown exists")
        };
        assert!(first_remaining_secs <= 2);

        {
            let mut guard = states.lock().unwrap();
            let entry = guard.get_mut("codex-main").expect("lb state exists");
            entry.cooldown_until[0] =
                Some(std::time::Instant::now() - std::time::Duration::from_secs(1));
        }
        let _ = lb.select_upstream();

        for _ in 0..FAILURE_THRESHOLD {
            lb.record_result_with_backoff(0, false, 2, backoff);
        }

        let second_remaining_secs = {
            let guard = states.lock().unwrap();
            let entry = guard.get("codex-main").expect("lb state exists");
            assert_eq!(entry.penalty_streak[0], 2);
            entry.cooldown_until[0]
                .map(|until| {
                    until
                        .saturating_duration_since(std::time::Instant::now())
                        .as_secs()
                })
                .expect("second cooldown exists")
        };
        assert!(second_remaining_secs <= 4);
        assert!(second_remaining_secs >= first_remaining_secs);

        lb.record_result_with_backoff(0, true, 2, backoff);

        {
            let guard = states.lock().unwrap();
            let entry = guard.get("codex-main").expect("lb state exists");
            assert_eq!(entry.failure_counts[0], 0);
            assert!(entry.cooldown_until[0].is_none());
            assert_eq!(entry.penalty_streak[0], 0);
            assert_eq!(entry.last_good_index, Some(0));
        }
    }
}