diff --git a/backend/crates/gateway-core/src/account/selection.rs b/backend/crates/gateway-core/src/account/selection.rs index 2ef92ce41..578de09b5 100644 --- a/backend/crates/gateway-core/src/account/selection.rs +++ b/backend/crates/gateway-core/src/account/selection.rs @@ -87,6 +87,7 @@ pub struct AccountRuntimeSignals { pub in_flight: u32, pub last_started_at: Option, pub quota_reset_at: Option, + /// Provider 归一化的剩余额度基点:0 耗尽,10_000 全部可用。 pub quota_remaining_rank: Option, pub rate_limited_until: Option, pub failure_rate_basis_points: Option, @@ -323,6 +324,7 @@ pub struct AccountQuotaSignals { } impl AccountQuotaSignals { + /// `remaining_rank` 使用剩余额度基点,范围为 0..=10_000。 #[must_use] pub const fn new(reset_at: Option, remaining_rank: Option) -> Self { Self { @@ -640,6 +642,10 @@ const SMART_LOAD_WEIGHT: f64 = 1.0; const SMART_QUOTA_WEIGHT: f64 = 0.8; const SMART_FAILURE_WEIGHT: f64 = 1.0; const SMART_LATENCY_WEIGHT: f64 = 0.5; +// 容忍 5 个百分点的单项负载/失败率差异,避免微小信号波动独占新会话。 +const SMART_SCORE_TOLERANCE: f64 = 0.05; +// 首输出 10 秒时延迟得分减半;固定尺度不随其他候选账号变化。 +const SMART_LATENCY_HALF_SCORE_MS: f64 = 10_000.0; fn capacity_utilization(candidate: &AccountCandidate, default_concurrency: NonZeroU32) -> f64 { f64::from(candidate.signals.in_flight) @@ -651,94 +657,32 @@ fn capacity_utilization(candidate: &AccountCandidate, default_concurrency: NonZe ) } -struct SmartNormalization { - max_capacity_utilization: f64, - min_quota: Option, - max_quota: Option, - min_latency_ms: Option, - max_latency_ms: Option, -} - -impl SmartNormalization { - fn from_candidates(candidates: &[&AccountCandidate], default_concurrency: NonZeroU32) -> Self { - let max_capacity_utilization = candidates - .iter() - .map(|candidate| capacity_utilization(candidate, default_concurrency)) - .reduce(f64::max) - .unwrap_or_default(); - let min_quota = candidates - .iter() - .filter_map(|candidate| candidate.signals.quota_remaining_rank) - .min(); - let max_quota = candidates - .iter() - .filter_map(|candidate| candidate.signals.quota_remaining_rank) - .max(); - let min_latency_ms = candidates - .iter() - .filter_map(|candidate| candidate.signals.first_output_latency_ms) - .filter(|latency| *latency > 0) - .min(); - let max_latency_ms = candidates - .iter() - .filter_map(|candidate| candidate.signals.first_output_latency_ms) - .filter(|latency| *latency > 0) - .max(); - Self { - max_capacity_utilization, - min_quota, - max_quota, - min_latency_ms, - max_latency_ms, - } - } -} - fn select_smart_candidate<'a>( candidates: &[&'a AccountCandidate], default_concurrency: NonZeroU32, cursor: u64, ) -> Option<&'a AccountCandidate> { - let normalization = SmartNormalization::from_candidates(candidates, default_concurrency); let mut ranked = candidates .iter() - .map(|candidate| { - ( - *candidate, - smart_score(candidate, default_concurrency, &normalization), - ) - }) + .map(|candidate| (*candidate, smart_score(candidate, default_concurrency))) .collect::>(); - ranked.sort_by(|(left, left_score), (right, right_score)| { - right_score - .total_cmp(left_score) - .then_with(|| left.account.id().cmp(right.account.id())) - }); - let best_score = ranked.first()?.1.to_bits(); - let tied = ranked + let best_score = ranked .iter() - .take_while(|(_, score)| score.to_bits() == best_score) - .count(); - let index = cursor as usize % tied; + .map(|(_, score)| *score) + .max_by(f64::total_cmp)?; + ranked.retain(|(_, score)| best_score - score <= SMART_SCORE_TOLERANCE); + // 轮换顺序保持稳定,避免分数轻微交错与 cursor 同步后仍反复命中同一账号。 + ranked.sort_unstable_by(|(left, _), (right, _)| left.account.id().cmp(right.account.id())); + let index = (cursor % ranked.len() as u64) as usize; Some(ranked[index].0) } -fn smart_score( - candidate: &AccountCandidate, - default_concurrency: NonZeroU32, - normalization: &SmartNormalization, -) -> f64 { - let load = lower_ratio_is_better( - capacity_utilization(candidate, default_concurrency), - normalization.max_capacity_utilization, - ); - let quota = candidate.signals.quota_remaining_rank.map_or(0.5, |quota| { - higher_is_better( - quota, - normalization.min_quota.unwrap_or(quota), - normalization.max_quota.unwrap_or(quota), - ) - }); +fn smart_score(candidate: &AccountCandidate, default_concurrency: NonZeroU32) -> f64 { + let load = 1.0 - capacity_utilization(candidate, default_concurrency).clamp(0.0, 1.0); + let quota = candidate + .signals + .quota_remaining_rank + .map_or(0.5, |quota| quota.min(10_000) as f64 / 10_000.0); let failure = 1.0 - f64::from( candidate @@ -752,11 +696,7 @@ fn smart_score( .first_output_latency_ms .filter(|latency| *latency > 0) .map_or(1.0, |latency| { - lower_is_better( - latency, - normalization.min_latency_ms.unwrap_or(latency), - normalization.max_latency_ms.unwrap_or(latency), - ) + SMART_LATENCY_HALF_SCORE_MS / (SMART_LATENCY_HALF_SCORE_MS + latency as f64) }); SMART_LOAD_WEIGHT * load @@ -764,24 +704,3 @@ fn smart_score( + SMART_FAILURE_WEIGHT * failure + SMART_LATENCY_WEIGHT * latency } - -fn lower_ratio_is_better(value: f64, maximum: f64) -> f64 { - if maximum <= 0.0 { - return 1.0; - } - 1.0 - (value / maximum).clamp(0.0, 1.0) -} - -fn lower_is_better(value: u64, minimum: u64, maximum: u64) -> f64 { - if maximum <= minimum { - return 1.0; - } - 1.0 - (value.saturating_sub(minimum) as f64 / (maximum - minimum) as f64).clamp(0.0, 1.0) -} - -fn higher_is_better(value: u64, minimum: u64, maximum: u64) -> f64 { - if maximum <= minimum { - return 1.0; - } - (value.saturating_sub(minimum) as f64 / (maximum - minimum) as f64).clamp(0.0, 1.0) -} diff --git a/backend/crates/gateway-core/tests/account/mod.rs b/backend/crates/gateway-core/tests/account/mod.rs index e4e44dde2..7c15bbe43 100644 --- a/backend/crates/gateway-core/tests/account/mod.rs +++ b/backend/crates/gateway-core/tests/account/mod.rs @@ -697,8 +697,8 @@ fn account_feedback_should_decay_failure_rate_after_success() { #[test] fn smart_selector_should_use_provider_quota_rank_after_load_is_equal() { let candidates = vec![ - candidate("acct_low_quota", 0, Some(20)), - candidate("acct_high_quota", 0, Some(80)), + candidate("acct_low_quota", 0, Some(2_000)), + candidate("acct_high_quota", 0, Some(8_000)), ]; let selected = AccountSelector .select(&candidates, &context(RotationStrategy::Smart)) @@ -743,10 +743,10 @@ fn quota_reset_selector_should_use_capacity_utilization_as_load_tiebreaker() { } #[test] -fn smart_selector_should_prefer_known_quota_over_unknown_after_load_is_equal() { +fn smart_selector_should_prefer_sufficient_known_quota_over_unknown_after_load_is_equal() { let candidates = vec![ candidate("acct_unknown", 0, None), - candidate("acct_known", 0, Some(1)), + candidate("acct_known", 0, Some(8_000)), ]; let selected = AccountSelector .select(&candidates, &context(RotationStrategy::Smart)) diff --git a/backend/crates/gateway-core/tests/account/selection.rs b/backend/crates/gateway-core/tests/account/selection.rs index 44ff0db20..96f6d1f7b 100644 --- a/backend/crates/gateway-core/tests/account/selection.rs +++ b/backend/crates/gateway-core/tests/account/selection.rs @@ -1,8 +1,13 @@ use std::time::{Duration, Instant}; -use gateway_core::account::{AccountAttemptFeedback, AccountFeedbackStats, ProviderAccountId}; +use gateway_core::account::{ + AccountAttemptFeedback, AccountCandidate, AccountFeedbackStats, AccountSelector, + ProviderAccountId, RotationStrategy, +}; use gateway_core::routing::ProviderKind; +use super::{candidate, candidate_with_concurrency, context}; + const FAILURE_RATE_HALF_LIFE: Duration = Duration::from_secs(15 * 60); fn feedback_subject() -> (AccountFeedbackStats, ProviderKind, ProviderAccountId) { @@ -96,3 +101,139 @@ fn concurrent_account_failures_should_not_lose_samples() { assert_eq!(failure_rate, Some(8_322)); } +fn smart_selection_ids(candidates: &[AccountCandidate]) -> Vec<&str> { + let mut selection = context(RotationStrategy::Smart); + (0..20) + .map(|cursor| { + selection.round_robin_cursor = cursor; + AccountSelector + .select(candidates, &selection) + .expect("candidate available") + .candidate() + .account + .id() + .as_str() + }) + .collect() +} + +#[test] +fn smart_selector_should_rotate_despite_small_signal_differences() { + for signal in ["quota", "latency", "load", "failure"] { + let mut candidates = [ + candidate_with_concurrency("acct_a", 0, 100), + candidate_with_concurrency("acct_b", 0, 100), + ]; + match signal { + "quota" => { + candidates[0].signals.quota_remaining_rank = Some(600); + candidates[1].signals.quota_remaining_rank = Some(601); + } + "latency" => { + candidates[0].signals.first_output_latency_ms = Some(2_500); + candidates[1].signals.first_output_latency_ms = Some(2_501); + } + "load" => candidates[0].signals.in_flight = 1, + "failure" => candidates[0].signals.failure_rate_basis_points = Some(1), + _ => unreachable!(), + } + let selected = smart_selection_ids(&candidates); + + assert_eq!(selected, ["acct_a", "acct_b"].repeat(10), "{signal}"); + } +} + +#[test] +fn smart_selector_should_keep_rotation_order_when_nearby_scores_cross() { + let mut candidates = [ + candidate("acct_a", 0, Some(8_000)), + candidate("acct_b", 0, Some(8_001)), + candidate("acct_worse", 0, Some(2_000)), + ]; + let mut selection = context(RotationStrategy::Smart); + let mut selected = Vec::new(); + for cursor in 0..20 { + selection.round_robin_cursor = cursor; + for candidate in &mut candidates { + match candidate.account.id().as_str() { + "acct_a" => candidate.signals.quota_remaining_rank = Some(8_000 + cursor % 2), + "acct_b" => candidate.signals.quota_remaining_rank = Some(8_001 - cursor % 2), + _ => {} + } + } + candidates.reverse(); + selected.push( + AccountSelector + .select(&candidates, &selection) + .expect("candidate available") + .candidate() + .account + .id() + .as_str() + .to_owned(), + ); + } + + assert_eq!(selected, ["acct_a", "acct_b"].repeat(10)); +} + +#[test] +fn smart_selector_should_only_rotate_among_candidates_close_to_the_best() { + let candidates = [ + candidate("acct_a", 0, Some(10_000)), + candidate("acct_b", 0, Some(9_500)), + candidate("acct_c", 0, Some(9_000)), + ]; + + assert_eq!( + smart_selection_ids(&candidates), + ["acct_a", "acct_b"].repeat(10) + ); +} + +#[test] +fn smart_selector_should_preserve_material_signal_advantages() { + for signal in ["quota", "latency", "load", "failure"] { + let mut candidates = [ + candidate_with_concurrency("acct_a", 0, 10), + candidate_with_concurrency("acct_b", 0, 10), + ]; + match signal { + "quota" => { + candidates[0].signals.quota_remaining_rank = Some(600); + candidates[1].signals.quota_remaining_rank = Some(2_600); + } + "latency" => { + candidates[0].signals.first_output_latency_ms = Some(20_000); + candidates[1].signals.first_output_latency_ms = Some(2_500); + } + "load" => candidates[0].signals.in_flight = 1, + "failure" => candidates[0].signals.failure_rate_basis_points = Some(2_000), + _ => unreachable!(), + } + + assert_eq!(smart_selection_ids(&candidates), ["acct_b"; 20], "{signal}"); + } +} + +#[test] +fn smart_selector_should_balance_actual_load_against_remaining_quota() { + let mut candidates = [ + candidate_with_concurrency("acct_a", 0, 10), + candidate_with_concurrency("acct_b", 1, 10), + ]; + candidates[0].signals.quota_remaining_rank = Some(600); + candidates[1].signals.quota_remaining_rank = Some(2_600); + + assert_eq!(smart_selection_ids(&candidates), ["acct_b"; 20]); +} + +#[test] +fn smart_selector_should_treat_unknown_quota_as_neutral() { + let candidates = [ + candidate("acct_known_low", 0, Some(600)), + candidate("acct_unknown", 0, None), + ]; + + assert_eq!(smart_selection_ids(&candidates), ["acct_unknown"; 20]); +} diff --git a/backend/crates/providers/xai/tests/credential/selector.rs b/backend/crates/providers/xai/tests/credential/selector.rs index 2a74f2677..fb90c814d 100644 --- a/backend/crates/providers/xai/tests/credential/selector.rs +++ b/backend/crates/providers/xai/tests/credential/selector.rs @@ -949,15 +949,16 @@ async fn smart_strategy_uses_common_account_health_feedback() { #[tokio::test] async fn smart_strategy_never_reuses_quota_projection_after_credential_rotation() { - let fixture = SelectorFixture::new(&["aaa-stale-high", "zzz-current-low"]).await; + let fixture = SelectorFixture::new(&["aaa-stale-high", "zzz-current-known"]).await; let stale = account_id("aaa-stale-high"); fixture .seed_quota(&stale, 5.0, Duration::from_secs(600)) .await; fixture .seed_quota( - &account_id("zzz-current-low"), - 95.0, + // 剩余 75% 低于旧观测的 95%,但高于观测失效后的未知额度中性值。 + &account_id("zzz-current-known"), + 25.0, Duration::from_secs(600), ) .await; @@ -999,7 +1000,7 @@ async fn smart_strategy_never_reuses_quota_projection_after_credential_rotation( .await .expect("current quota-ranked account"); - assert_eq!(selected.account_id(), &account_id("zzz-current-low")); + assert_eq!(selected.account_id(), &account_id("zzz-current-known")); } #[tokio::test]