From 309f507ef423d238e9c6738ed6d2ddde200149f1 Mon Sep 17 00:00:00 2001 From: AAEE86 Date: Tue, 29 Sep 2026 16:52:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(pool):=20=E9=85=8D=E9=A2=9D=E5=80=92?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E5=BD=92=E9=9B=B6=E5=90=8E=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E6=9D=A1=E4=B8=8E=E5=88=97=E8=A1=A8=E6=81=A2=E5=A4=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=20100%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端读取层 provider_key_status_snapshot_payload 复用调度侧同一判定 provider_pool_reset_deadline_elapsed,对已到期的 Codex 配额窗口归一化为 used_ratio=0 / remaining_ratio=1,并清除窗口级耗尽标记;跳过 window_minutes=0 与无用量观测的窗口 - 前端展示层在倒计时归零(isExpired)时兜底显示 100%,并隐藏重置前的旧用量 文本,保证归零瞬间即时恢复,与后端读取口径、调度口径一致 - 补充后端 2 个单测(到期归一化、遗留耗尽状态清理)与前端 1 个回归用例 (负向验证可复现旧行为) 验证:aether-provider-pool 71/71;aether-gateway lib 回归通过(仅 3 个依赖 PostgreSQL 服务端的用例因本机环境缺失失败,与本改动无关);前端 222 文件 / 1718 用例全部通过;clippy -D warnings、cargo fmt、vue-tsc、eslint 均通过。 --- .../src/handlers/shared/catalog.rs | 207 ++++++++++++++++++ crates/aether-provider/pool/src/lib.rs | 2 +- crates/aether-provider/pool/src/quota.rs | 9 +- frontend/src/views/admin/PoolManagement.vue | 39 ++-- .../PoolManagement.codex-cycle-stats.spec.ts | 45 +++- 5 files changed, 285 insertions(+), 17 deletions(-) diff --git a/apps/aether-gateway/src/handlers/shared/catalog.rs b/apps/aether-gateway/src/handlers/shared/catalog.rs index 96ab2f78c..66f8a998d 100644 --- a/apps/aether-gateway/src/handlers/shared/catalog.rs +++ b/apps/aether-gateway/src/handlers/shared/catalog.rs @@ -2575,6 +2575,63 @@ fn windsurf_quota_snapshot_has_stale_cooldown(quota_snapshot: &Map, + provider_type: &str, +) { + if !provider_type.trim().eq_ignore_ascii_case("codex") { + return; + } + let Some(quota) = snapshot.get_mut("quota").and_then(Value::as_object_mut) else { + return; + }; + let now_unix_secs = SystemTime::now() + .duration_since(UNIX_EPOCH) + .ok() + .map(|duration| duration.as_secs()) + .unwrap_or(0); + let fallback_observed_at = provider_quota_timestamp_unix_secs(quota.get("observed_at")) + .or_else(|| provider_quota_timestamp_unix_secs(quota.get("updated_at"))); + let Some(windows) = quota.get_mut("windows").and_then(Value::as_array_mut) else { + return; + }; + + for window in windows.iter_mut().filter_map(Value::as_object_mut) { + // 与同类 Codex 窗口处理保持一致:window_minutes=0 不是真实配额窗口。 + if window.get("window_minutes").and_then(Value::as_u64) == Some(0) { + continue; + } + if !aether_provider_pool::provider_pool_reset_deadline_elapsed( + window, + fallback_observed_at, + now_unix_secs, + ) { + continue; + } + // 只归一化带用量观测的窗口,避免把没有观测数据的窗口凭空显示成 100%。 + let has_usage_observation = ["used_ratio", "remaining_ratio"] + .into_iter() + .any(|field| window.get(field).is_some_and(Value::is_number)); + if !has_usage_observation { + continue; + } + window.insert("used_ratio".to_string(), json!(0.0)); + window.insert("remaining_ratio".to_string(), json!(1.0)); + // 同步清掉窗口级耗尽标记,避免展示与调度口径互相矛盾。 + for field in ["is_exhausted", "exhausted"] { + if let Some(slot) = window.get_mut(field) { + *slot = json!(false); + } + } + } +} + pub(crate) fn provider_key_status_snapshot_payload( key: &StoredProviderCatalogKey, provider_type: &str, @@ -2626,6 +2683,9 @@ pub(crate) fn provider_key_status_snapshot_payload( } } } + // 读取时归一化已到期的 Codex 配额窗口,保证列表进度条与调度侧、 + // 账号额度文本使用同一“已重置”口径。 + normalize_expired_codex_quota_windows(&mut snapshot, provider_type); snapshot.insert( "oauth".to_string(), build_provider_key_oauth_status_snapshot(key), @@ -3674,6 +3734,153 @@ mod tests { assert_eq!(window.get("reset_seconds"), Some(&json!(3_600u64))); } + #[test] + fn provider_key_status_snapshot_payload_normalizes_expired_codex_quota_windows() { + let mut key = sample_catalog_key(); + key.status_snapshot = Some(json!({ + "quota": { + "version": 2, + "provider_type": "codex", + "code": "ok", + "exhausted": false, + "updated_at": 1_700_000_000u64, + "windows": [ + { + "code": "5h", + "label": "5H", + "scope": "account", + "unit": "percent", + "used_ratio": 0.88, + "remaining_ratio": 0.12, + "reset_at": 1_700_003_600u64, + "window_minutes": 300 + }, + { + "code": "weekly", + "label": "周", + "scope": "account", + "unit": "percent", + "used_ratio": 0.5, + "remaining_ratio": 0.5, + "reset_at": 2_000_000_000u64, + "window_minutes": 10_080 + }, + { + "code": "spark_5h", + "label": "Spark 5H", + "scope": "account", + "unit": "percent", + "reset_at": 1_700_003_600u64, + "window_minutes": 300 + }, + { + "code": "unlimited", + "label": "无限", + "scope": "account", + "unit": "percent", + "used_ratio": 0.3, + "remaining_ratio": 0.7, + "reset_at": 1_700_003_600u64, + "window_minutes": 0 + } + ] + } + })); + + let payload = provider_key_status_snapshot_payload(&key, "codex"); + + // 已到期的窗口按“已重置”口径归一化:用量清零、剩余 100%。 + assert_eq!(payload.pointer("/quota/windows/0/code"), Some(&json!("5h"))); + assert_eq!( + payload.pointer("/quota/windows/0/used_ratio"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_ratio"), + Some(&json!(1.0)) + ); + // 未到期的窗口保留原始观测值。 + assert_eq!( + payload.pointer("/quota/windows/1/code"), + Some(&json!("weekly")) + ); + assert_eq!( + payload.pointer("/quota/windows/1/used_ratio"), + Some(&json!(0.5)) + ); + assert_eq!( + payload.pointer("/quota/windows/1/remaining_ratio"), + Some(&json!(0.5)) + ); + // 没有用量观测的窗口不会被凭空补成 100%。 + assert_eq!( + payload.pointer("/quota/windows/2/code"), + Some(&json!("spark_5h")) + ); + assert_eq!(payload.pointer("/quota/windows/2/used_ratio"), None); + assert_eq!(payload.pointer("/quota/windows/2/remaining_ratio"), None); + // window_minutes=0 不是真实配额窗口,与同类 Codex 窗口处理保持一致,不归一化。 + assert_eq!( + payload.pointer("/quota/windows/3/code"), + Some(&json!("unlimited")) + ); + assert_eq!( + payload.pointer("/quota/windows/3/used_ratio"), + Some(&json!(0.3)) + ); + assert_eq!( + payload.pointer("/quota/windows/3/remaining_ratio"), + Some(&json!(0.7)) + ); + } + + #[test] + fn provider_key_status_snapshot_payload_clears_expired_codex_window_exhausted_state() { + let mut key = sample_catalog_key(); + key.status_snapshot = Some(json!({ + "quota": { + "version": 2, + "provider_type": "codex", + "code": "exhausted", + "exhausted": true, + "usage_ratio": 1.0, + "updated_at": 1_700_000_000u64, + "windows": [ + { + "code": "weekly", + "label": "周", + "scope": "account", + "unit": "percent", + "used_ratio": 1.0, + "remaining_ratio": 0.0, + "reset_at": 1_700_003_600u64, + "window_minutes": 10_080, + "is_exhausted": true + } + ] + } + })); + + let payload = provider_key_status_snapshot_payload(&key, "codex"); + + // 汇总标志沿用既有调度口径(窗口已到期 → 不再视为耗尽)。 + assert_eq!(payload.pointer("/quota/exhausted"), Some(&json!(false))); + assert_eq!(payload.pointer("/quota/code"), Some(&json!("ok"))); + // 窗口级耗尽标记与剩余比例同步归一化。 + assert_eq!( + payload.pointer("/quota/windows/0/is_exhausted"), + Some(&json!(false)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/used_ratio"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_ratio"), + Some(&json!(1.0)) + ); + } + #[test] fn provider_key_status_snapshot_payload_backfills_chatgpt_web_image_quota() { let mut key = sample_catalog_key(); diff --git a/crates/aether-provider/pool/src/lib.rs b/crates/aether-provider/pool/src/lib.rs index 088987fff..31af87ddd 100644 --- a/crates/aether-provider/pool/src/lib.rs +++ b/crates/aether-provider/pool/src/lib.rs @@ -43,7 +43,7 @@ pub use quota::{ provider_pool_key_model_quota_hard_blocked, provider_pool_key_quota_hard_blocked, provider_pool_key_scheduling_label, provider_pool_member_quota_snapshot, provider_pool_quota_metadata_provider_type, provider_pool_quota_metadata_updated_at, - provider_pool_quota_snapshot_updated_at, + provider_pool_quota_snapshot_updated_at, provider_pool_reset_deadline_elapsed, }; pub use quota_refresh::ProviderPoolQuotaRequestSpec; pub use service::ProviderPoolService; diff --git a/crates/aether-provider/pool/src/quota.rs b/crates/aether-provider/pool/src/quota.rs index af49f3f63..ed01af2f9 100644 --- a/crates/aether-provider/pool/src/quota.rs +++ b/crates/aether-provider/pool/src/quota.rs @@ -857,7 +857,14 @@ fn provider_pool_reset_deadline_unix_secs( }) } -pub(crate) fn provider_pool_reset_deadline_elapsed( +/// Whether the quota window's reset deadline has already passed. +/// +/// The deadline is taken from `reset_at`/`next_reset_at` when present, otherwise +/// derived from `reset_seconds`/`reset_after_seconds` anchored at the window (or +/// fallback) observation time. Scheduling ignores exhausted windows once this is +/// true; read paths can reuse the same predicate so the displayed quota matches +/// the scheduling decision after a reset. +pub fn provider_pool_reset_deadline_elapsed( item: &Map, fallback_observed_at: Option, now_unix_secs: u64, diff --git a/frontend/src/views/admin/PoolManagement.vue b/frontend/src/views/admin/PoolManagement.vue index 17a8bf2b4..40bcc73a0 100644 --- a/frontend/src/views/admin/PoolManagement.vue +++ b/frontend/src/views/admin/PoolManagement.vue @@ -2061,17 +2061,23 @@ const quotaProgressMap = computed>(() => { const quotaProgressDisplayMap = computed>(() => { const map: Record = {} for (const key of keyPage.value.keys) { - map[key.key_id] = (quotaProgressMap.value[key.key_id] || []).map(item => ({ - label: getQuotaProgressLabel(item.label), - remainingPercent: item.remainingPercent, - resetText: getQuotaProgressResetDisplayText(item), - meterText: item.numericOnly - ? item.detail || formatQuotaValue(item.remainingPercent) - : getQuotaProgressMeterDisplayText(item), - barClass: getQuotaRemainingBarColorByRemaining(item.remainingPercent), - meterClass: getQuotaRemainingClassByRemaining(item.remainingPercent), - numericOnly: item.numericOnly, - })) + map[key.key_id] = (quotaProgressMap.value[key.key_id] || []).map(item => { + // 倒计时归零表示窗口已越过重置时间点:按“已重置”展示 100%, + // 不再显示重置前的旧用量文本,与后端读取口径、调度口径保持一致。 + const expired = !item.numericOnly && getQuotaProgressCountdown(item)?.isExpired === true + const remainingPercent = expired ? 100 : item.remainingPercent + return { + label: getQuotaProgressLabel(item.label), + remainingPercent, + resetText: getQuotaProgressResetDisplayText(item), + meterText: item.numericOnly + ? item.detail || formatQuotaValue(remainingPercent) + : getQuotaProgressMeterDisplayText(item, remainingPercent, expired), + barClass: getQuotaRemainingBarColorByRemaining(remainingPercent), + meterClass: getQuotaRemainingClassByRemaining(remainingPercent), + numericOnly: item.numericOnly, + } + }) } return map }) @@ -3544,10 +3550,15 @@ function getQuotaProgressResetDisplayText(item: QuotaProgressItem): string { return '' } -function getQuotaProgressMeterDisplayText(item: QuotaProgressItem): string { - const detail = item.detail?.trim() || '' +function getQuotaProgressMeterDisplayText( + item: QuotaProgressItem, + remainingPercent = item.remainingPercent, + suppressDetail = false, +): string { + // 窗口已重置时忽略重置前的旧用量文本,直接显示归一化后的剩余百分比。 + const detail = suppressDetail ? '' : (item.detail?.trim() || '') if (!shouldHideQuotaProgressDetailText(detail) && detail) return detail - return `${item.remainingPercent.toFixed(1)}%` + return `${remainingPercent.toFixed(1)}%` } function getQuotaFallbackText(key: PoolKeyDetail): string | null { diff --git a/frontend/src/views/admin/__tests__/PoolManagement.codex-cycle-stats.spec.ts b/frontend/src/views/admin/__tests__/PoolManagement.codex-cycle-stats.spec.ts index 2f7991b57..23ee031ce 100644 --- a/frontend/src/views/admin/__tests__/PoolManagement.codex-cycle-stats.spec.ts +++ b/frontend/src/views/admin/__tests__/PoolManagement.codex-cycle-stats.spec.ts @@ -35,6 +35,10 @@ const routeMocks = vi.hoisted(() => ({ }), })) +const countdownMocks = vi.hoisted(() => ({ + isExpired: false, +})) + const proxyStoreMocks = vi.hoisted(() => ({ ensureLoaded: vi.fn(), })) @@ -106,7 +110,7 @@ vi.mock('@/composables/useCountdownTimer', async () => { start: vi.fn(), }), getCodexResetCountdown: () => ({ - isExpired: false, + isExpired: countdownMocks.isExpired, text: '1h', }), } @@ -604,6 +608,7 @@ beforeEach(() => { window.sessionStorage.clear() routeMocks.patchQuery.mockClear() proxyStoreMocks.ensureLoaded.mockClear() + countdownMocks.isExpired = false endpointMocks.getPoolOverview.mockReset() endpointMocks.getPoolSchedulingPresets.mockReset() @@ -736,6 +741,44 @@ describe('PoolManagement Codex cycle stats mode', () => { expect(root.textContent).toContain('生图') }) + it('shows expired Codex quota windows as fully restored', async () => { + countdownMocks.isExpired = true + const expiredKey = createPoolKey('codex', { + status_snapshot: { + oauth: { code: 'valid' }, + account: { code: 'ok', blocked: false }, + quota: { + code: 'ok', + exhausted: false, + provider_type: 'codex', + windows: [ + { + code: '5h', + label: '5H', + scope: 'account', + remaining_ratio: 0.12, + reset_at: 1_700_000_000, + window_minutes: 300, + }, + ], + }, + }, + }) + endpointMocks.getPoolOverview.mockResolvedValue({ items: [createOverview('codex')] }) + endpointMocks.listPoolKeys.mockResolvedValue(createKeyPage(expiredKey)) + endpointMocks.getProvider.mockResolvedValue(createProvider('codex')) + + const root = mountPoolManagement() + await settle() + + // 倒计时归零后进度条与文本按“已重置”显示 100%,不再显示旧百分比。 + expect(root.querySelector('[data-testid="pool-quota-meter-text"]')?.textContent?.trim()).toBe('100.0%') + const track = root.querySelector('[data-testid="pool-quota-progress-track"]') + const bar = track?.firstElementChild as HTMLElement | null + expect(bar?.style.width).toBe('100%') + expect(root.querySelector('[data-testid="pool-quota-reset-text"]')).toBeNull() + }) + it('labels Codex quota by the actual refresh window duration', async () => { const monthlyCodexKey = createPoolKey('codex', { status_snapshot: { From 9abbf268a155eee82b6bf0a2598427978ed3b331 Mon Sep 17 00:00:00 2001 From: AAEE86 Date: Tue, 29 Sep 2026 19:01:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(pool):=20=E8=AF=BB=E5=8F=96=E5=B1=82?= =?UTF-8?q?=E9=85=8D=E9=A2=9D=E5=BD=92=E4=B8=80=E5=8C=96=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E5=88=B0=E5=85=A8=E9=83=A8=E6=8F=90=E4=BE=9B=E5=95=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - normalize_expired_codex_quota_windows 改为通用实现并去掉 Codex 门槛: 对全部带重置时间的提供商窗口(kiro/xai/grok/antigravity/gemini_cli/ chatgpt_web/windsurf 等)按“已重置”口径归一化比例 (used_ratio/remaining_ratio)、数值(used_value/remaining_value)与 窗口级耗尽标记 - 归一化条件收紧为“观测完整”(有比例观测,或有上限+用量数值),跳过 window_minutes=0、无重置时间、只有 remaining_value 却没有上限的窗口, 避免比例已恢复 100% 而数值仍停留在旧值 - 顶层汇总状态(exhausted/code)同样对所有提供商生效,避免列表状态筛选 与前端提醒继续显示旧的“额度耗尽” - 新增单测 3 个(非 Codex 窗口归一化/汇总清理/未到期保留)与 kiro 端到端 用例 1 个(额度文本 + 状态筛选);kiro 既有集成用例期望值更新为 “剩余 100.0% (0/20)”(窗口已到期);grok/windsurf 回填用例的重置时间 改到未来,继续覆盖“未到期”路径 验证:aether-gateway lib 全量回归通过(仅 3 个依赖 PostgreSQL 服务端的用例 因本机环境缺失失败,与本改动无关);cargo fmt、clippy -D warnings(lib)、 架构守卫 208/208 通过。 --- .../src/handlers/shared/catalog.rs | 342 ++++++++++++++++-- .../src/tests/control/admin/pool.rs | 89 ++++- 2 files changed, 403 insertions(+), 28 deletions(-) diff --git a/apps/aether-gateway/src/handlers/shared/catalog.rs b/apps/aether-gateway/src/handlers/shared/catalog.rs index 66f8a998d..e0f9eb97b 100644 --- a/apps/aether-gateway/src/handlers/shared/catalog.rs +++ b/apps/aether-gateway/src/handlers/shared/catalog.rs @@ -2575,19 +2575,17 @@ fn windsurf_quota_snapshot_has_stale_cooldown(quota_snapshot: &Map, - provider_type: &str, -) { - if !provider_type.trim().eq_ignore_ascii_case("codex") { - return; - } +/// +/// 覆盖所有带重置时间的提供商窗口(codex/kiro/xai/grok/antigravity/ +/// gemini_cli/chatgpt_web/windsurf 等):比例(used_ratio/remaining_ratio)、 +/// 数值(used_value/remaining_value)与窗口级耗尽标记会一起恢复为“已重置”。 +fn normalize_expired_quota_windows(snapshot: &mut serde_json::Map) { let Some(quota) = snapshot.get_mut("quota").and_then(Value::as_object_mut) else { return; }; @@ -2603,7 +2601,7 @@ fn normalize_expired_codex_quota_windows( }; for window in windows.iter_mut().filter_map(Value::as_object_mut) { - // 与同类 Codex 窗口处理保持一致:window_minutes=0 不是真实配额窗口。 + // 与同类窗口处理保持一致:window_minutes=0 不是真实配额窗口。 if window.get("window_minutes").and_then(Value::as_u64) == Some(0) { continue; } @@ -2614,15 +2612,38 @@ fn normalize_expired_codex_quota_windows( ) { continue; } - // 只归一化带用量观测的窗口,避免把没有观测数据的窗口凭空显示成 100%。 - let has_usage_observation = ["used_ratio", "remaining_ratio"] + // 只归一化“观测完整”的窗口:要么有比例观测,要么有上限 + 用量数值。 + // 这样既不会把无数据窗口凭空显示成 100%,也不会出现比例已恢复 100% + // 而数值仍停留在旧值的不一致(例如只有 remaining_value 却没有上限的窗口)。 + let has_ratio_observation = ["used_ratio", "remaining_ratio"] .into_iter() .any(|field| window.get(field).is_some_and(Value::is_number)); - if !has_usage_observation { + let limit_value = window + .get("limit_value") + .and_then(admin_provider_quota_pure::coerce_json_f64) + .filter(|value| *value > 0.0); + let has_value_observation = limit_value.is_some() + && ["used_value", "remaining_value"] + .into_iter() + .any(|field| window.get(field).is_some_and(Value::is_number)); + if !has_ratio_observation && !has_value_observation { continue; } - window.insert("used_ratio".to_string(), json!(0.0)); - window.insert("remaining_ratio".to_string(), json!(1.0)); + // 比例口径:已用清零、剩余 100%;字段原本为 null 时一并补齐,保证展示口径统一。 + for (field, value) in [("used_ratio", 0.0), ("remaining_ratio", 1.0)] { + if window.contains_key(field) { + window.insert(field.to_string(), json!(value)); + } + } + // 数值口径:已用清零;有上限时把剩余恢复到上限(用于“剩余 x/y”类文本展示)。 + if window.contains_key("used_value") { + window.insert("used_value".to_string(), json!(0.0)); + } + if window.contains_key("remaining_value") { + if let Some(limit_value) = limit_value { + window.insert("remaining_value".to_string(), json!(limit_value)); + } + } // 同步清掉窗口级耗尽标记,避免展示与调度口径互相矛盾。 for field in ["is_exhausted", "exhausted"] { if let Some(slot) = window.get_mut(field) { @@ -2673,9 +2694,8 @@ pub(crate) fn provider_key_status_snapshot_payload( // Legacy snapshots can retain an exhausted summary after a window reset or // newer quota observation. Use the same decision as scheduling so the // account list and its status filter do not keep displaying that stale block. - if provider_type.trim().eq_ignore_ascii_case("codex") - && !aether_provider_pool::provider_pool_key_account_quota_exhausted(key, provider_type) - { + // 对所有提供商生效:适配器判定已是“重置感知”的,与调度口径保持一致。 + if !aether_provider_pool::provider_pool_key_account_quota_exhausted(key, provider_type) { if let Some(quota) = snapshot.get_mut("quota").and_then(Value::as_object_mut) { quota.insert("exhausted".to_string(), json!(false)); if quota.get("code").and_then(Value::as_str) == Some("exhausted") { @@ -2683,9 +2703,9 @@ pub(crate) fn provider_key_status_snapshot_payload( } } } - // 读取时归一化已到期的 Codex 配额窗口,保证列表进度条与调度侧、 + // 读取时归一化已到期的配额窗口,保证列表进度条、额度文字与调度侧、 // 账号额度文本使用同一“已重置”口径。 - normalize_expired_codex_quota_windows(&mut snapshot, provider_type); + normalize_expired_quota_windows(&mut snapshot); snapshot.insert( "oauth".to_string(), build_provider_key_oauth_status_snapshot(key), @@ -3881,6 +3901,274 @@ mod tests { ); } + #[test] + fn provider_key_status_snapshot_payload_normalizes_expired_quota_windows_for_other_providers() { + let mut key = sample_catalog_key(); + key.status_snapshot = Some(json!({ + "quota": { + "version": 2, + "provider_type": "kiro", + "code": "ok", + "exhausted": false, + "observed_at": 1_700_000_000u64, + "updated_at": 1_700_000_000u64, + "windows": [ + { + "code": "usage", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": 0.4, + "remaining_ratio": 0.6, + "used_value": 60.0, + "remaining_value": 90.0, + "limit_value": 150.0, + "reset_at": 1_700_003_600u64, + "reset_seconds": 3_600u64 + }, + { + "code": "usage_null_ratio", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": null, + "remaining_ratio": null, + "used_value": 60.0, + "remaining_value": 90.0, + "limit_value": 150.0, + "reset_at": 1_700_003_600u64, + "reset_seconds": 3_600u64 + }, + { + "code": "usage_active", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": 0.4, + "remaining_ratio": 0.6, + "used_value": 60.0, + "remaining_value": 90.0, + "limit_value": 150.0, + "reset_at": 2_000_000_000u64, + "reset_seconds": 300_000_000u64 + }, + { + "code": "usage_no_observation", + "label": "额度", + "scope": "account", + "unit": "count", + "reset_at": 1_700_003_600u64, + "reset_seconds": 3_600u64 + }, + { + "code": "usage_no_deadline", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": 0.4, + "remaining_ratio": 0.6, + "used_value": 60.0, + "remaining_value": 90.0, + "limit_value": 150.0 + }, + { + "code": "usage_remaining_without_limit", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": null, + "remaining_ratio": null, + "remaining_value": 0.0, + "reset_at": 1_700_003_600u64, + "reset_seconds": 3_600u64 + } + ] + } + })); + + let payload = provider_key_status_snapshot_payload(&key, "kiro"); + + // 已到期窗口:比例与数值一起按“已重置”归一化(不再只覆盖 Codex)。 + assert_eq!( + payload.pointer("/quota/windows/0/used_ratio"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_ratio"), + Some(&json!(1.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/used_value"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_value"), + Some(&json!(150.0)) + ); + // 比例字段为 null 时补齐为已重置口径,保证优先读比例的展示实现一致。 + assert_eq!( + payload.pointer("/quota/windows/1/used_ratio"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/1/remaining_ratio"), + Some(&json!(1.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/1/used_value"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/1/remaining_value"), + Some(&json!(150.0)) + ); + // 未到期窗口保留原始观测值。 + assert_eq!( + payload.pointer("/quota/windows/2/used_ratio"), + Some(&json!(0.4)) + ); + assert_eq!( + payload.pointer("/quota/windows/2/remaining_value"), + Some(&json!(90.0)) + ); + // 没有用量观测的窗口不会被凭空补成 100%。 + assert_eq!(payload.pointer("/quota/windows/3/used_ratio"), None); + assert_eq!(payload.pointer("/quota/windows/3/remaining_ratio"), None); + // 没有重置时间的窗口无法判定是否已重置,保持原样。 + assert_eq!( + payload.pointer("/quota/windows/4/used_ratio"), + Some(&json!(0.4)) + ); + assert_eq!( + payload.pointer("/quota/windows/4/remaining_ratio"), + Some(&json!(0.6)) + ); + // 只有 remaining_value 却没有上限的窗口观测不完整,跳过归一化, + // 避免比例已恢复 100% 而数值仍停留在旧值。 + assert_eq!( + payload.pointer("/quota/windows/5/remaining_ratio"), + Some(&json!(null)) + ); + assert_eq!( + payload.pointer("/quota/windows/5/remaining_value"), + Some(&json!(0.0)) + ); + } + + #[test] + fn provider_key_status_snapshot_payload_clears_expired_quota_exhausted_state_for_other_providers( + ) { + let mut key = sample_catalog_key(); + key.status_snapshot = Some(json!({ + "quota": { + "version": 2, + "provider_type": "kiro", + "code": "exhausted", + "label": "额度耗尽", + "reason": "额度已耗尽", + "exhausted": true, + "usage_ratio": 1.0, + "observed_at": 1_700_000_000u64, + "updated_at": 1_700_000_000u64, + "windows": [ + { + "code": "usage", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": 1.0, + "remaining_ratio": 0.0, + "used_value": 150.0, + "remaining_value": 0.0, + "limit_value": 150.0, + "reset_at": 1_700_003_600u64, + "reset_seconds": 3_600u64, + "is_exhausted": true + } + ] + } + })); + + let payload = provider_key_status_snapshot_payload(&key, "kiro"); + + // 汇总状态沿用调度口径(窗口已到期 → 不再视为耗尽),对所有提供商生效。 + assert_eq!(payload.pointer("/quota/exhausted"), Some(&json!(false))); + assert_eq!(payload.pointer("/quota/code"), Some(&json!("ok"))); + // 窗口比例、数值与耗尽标记同步归一化。 + assert_eq!( + payload.pointer("/quota/windows/0/is_exhausted"), + Some(&json!(false)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/used_ratio"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_ratio"), + Some(&json!(1.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/used_value"), + Some(&json!(0.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_value"), + Some(&json!(150.0)) + ); + } + + #[test] + fn provider_key_status_snapshot_payload_keeps_active_quota_exhausted_state_for_other_providers() + { + let mut key = sample_catalog_key(); + key.status_snapshot = Some(json!({ + "quota": { + "version": 2, + "provider_type": "kiro", + "code": "exhausted", + "label": "额度耗尽", + "exhausted": true, + "usage_ratio": 1.0, + "observed_at": 1_700_000_000u64, + "updated_at": 1_700_000_000u64, + "windows": [ + { + "code": "usage", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": 1.0, + "remaining_ratio": 0.0, + "used_value": 150.0, + "remaining_value": 0.0, + "limit_value": 150.0, + "reset_at": 2_000_000_000u64, + "reset_seconds": 300_000_000u64, + "is_exhausted": true + } + ] + } + })); + + let payload = provider_key_status_snapshot_payload(&key, "kiro"); + + // 未到期的耗尽状态保留,不能被读取层提前“恢复”。 + assert_eq!(payload.pointer("/quota/exhausted"), Some(&json!(true))); + assert_eq!(payload.pointer("/quota/code"), Some(&json!("exhausted"))); + assert_eq!( + payload.pointer("/quota/windows/0/is_exhausted"), + Some(&json!(true)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/used_ratio"), + Some(&json!(1.0)) + ); + assert_eq!( + payload.pointer("/quota/windows/0/remaining_ratio"), + Some(&json!(0.0)) + ); + } + #[test] fn provider_key_status_snapshot_payload_backfills_chatgpt_web_image_quota() { let mut key = sample_catalog_key(); @@ -3959,14 +4247,14 @@ mod tests { "used_percent": 60.0, "remaining": 60.0, "total": 150.0, - "reset_at": 1_778_157_172u64, + "reset_at": 2_000_000_000u64, "is_exhausted": false }, "quota_heavy": { "display_name": "heavy", "remaining_fraction": 0.0, "used_percent": 100.0, - "reset_at": 1_778_157_172u64, + "reset_at": 2_000_000_000u64, "is_exhausted": true } } @@ -3989,7 +4277,7 @@ mod tests { assert_eq!(quota.get("pool_tier"), Some(&json!("heavy"))); assert_eq!(quota.get("exhausted"), Some(&json!(false))); assert_eq!(quota.get("usage_ratio"), Some(&json!(1.0))); - assert_eq!(quota.get("reset_at"), Some(&json!(1_778_157_172u64))); + assert_eq!(quota.get("reset_at"), Some(&json!(2_000_000_000u64))); assert_eq!(windows.len(), 2); assert!(windows.iter().any(|window| { window @@ -4102,8 +4390,8 @@ mod tests { "plan_name": "Pro", "daily_remaining_percent": 40.0, "weekly_remaining_percent": 65.0, - "daily_reset_at": 1_778_100_000u64, - "weekly_reset_at": 1_778_600_000u64, + "daily_reset_at": 2_000_000_000u64, + "weekly_reset_at": 2_000_600_000u64, "prompt_used": 12.0, "prompt_limit": 100.0, "prompt_remaining": 88.0, @@ -4138,13 +4426,13 @@ mod tests { assert_eq!(quota.get("code"), Some(&json!("ok"))); assert_eq!(quota.get("plan_type"), Some(&json!("Pro"))); assert_eq!(quota.get("usage_ratio"), Some(&json!(0.6))); - assert_eq!(quota.get("reset_at"), Some(&json!(1_778_100_000u64))); + assert_eq!(quota.get("reset_at"), Some(&json!(2_000_000_000u64))); assert_eq!(daily.get("remaining_ratio"), Some(&json!(0.4))); assert_eq!(daily.get("used_ratio"), Some(&json!(0.6))); - assert_eq!(daily.get("reset_seconds"), Some(&json!(32_754u64))); + assert_eq!(daily.get("reset_seconds"), Some(&json!(221_932_754u64))); assert_eq!(weekly.get("remaining_ratio"), Some(&json!(0.65))); assert_eq!(weekly.get("used_ratio"), Some(&json!(0.35))); - assert_eq!(weekly.get("reset_seconds"), Some(&json!(532_754u64))); + assert_eq!(weekly.get("reset_seconds"), Some(&json!(222_532_754u64))); assert_eq!(quota.get("allowed_models_count"), Some(&json!(82))); } diff --git a/apps/aether-gateway/src/tests/control/admin/pool.rs b/apps/aether-gateway/src/tests/control/admin/pool.rs index 21edb44b2..a3bb66ad6 100644 --- a/apps/aether-gateway/src/tests/control/admin/pool.rs +++ b/apps/aether-gateway/src/tests/control/admin/pool.rs @@ -3044,7 +3044,94 @@ async fn gateway_prefers_status_snapshot_kiro_quota_over_stale_metadata() { assert_eq!(keys[0]["scheduling_status"], json!("available")); assert_eq!(keys[0]["scheduling_reason"], json!("available")); assert_eq!(keys[0]["quota_updated_at"], json!(1_775_553_285u64)); - assert_eq!(keys[0]["account_quota"], json!("剩余 75.0% (5/20)")); + // 窗口重置时间已过:读取层按“已重置”口径归一化(调度侧同样不再视为耗尽), + // 同时仍优先使用快照(上限 20)而不是陈旧元数据(上限 100)。 + assert_eq!(keys[0]["account_quota"], json!("剩余 100.0% (0/20)")); +} + +#[tokio::test] +async fn gateway_kiro_pool_filter_clears_stale_exhausted_summary_after_window_reset() { + let mut provider = sample_provider("provider-kiro", "kiro", 10); + provider.provider_type = "kiro".to_string(); + provider.config = Some(json!({"pool_advanced": { + "reserve_minimum_quota": false, "skip_exhausted_accounts": true + }})); + let mut key = sample_key( + "key-kiro-expired", + "provider-kiro", + "claude:messages", + "oauth-placeholder", + ); + key.name = "kiro expired quota key".to_string(); + key.auth_type = "oauth".to_string(); + key.status_snapshot = Some(json!({ + "quota": { + "version": 2, + "provider_type": "kiro", + "code": "exhausted", + "label": "额度耗尽", + "reason": "额度已耗尽", + "freshness": "fresh", + "source": "refresh_api", + "observed_at": 1_775_553_285u64, + "exhausted": true, + "usage_ratio": 1.0, + "updated_at": 1_775_553_285u64, + "reset_seconds": 0u64, + "plan_type": "KIRO PRO+", + "windows": [ + { + "code": "usage", + "label": "额度", + "scope": "account", + "unit": "count", + "used_ratio": 1.0, + "remaining_ratio": 0.0, + "used_value": 20.0, + "remaining_value": 0.0, + "limit_value": 20.0, + "reset_at": 1_775_639_685u64, + "reset_seconds": 0u64, + "is_exhausted": true + } + ] + } + })); + + let state = AppState::new() + .expect("gateway should build") + .with_data_state_for_tests(GatewayDataState::with_provider_catalog_reader_for_tests( + Arc::new(InMemoryProviderCatalogReadRepository::seed( + vec![provider], + Vec::new(), + vec![key], + )), + )); + + for status in ["all", "available", "quota_exhausted"] { + let response = local_admin_pool_response( + &state, + http::Method::GET, + &format!("/api/admin/pool/provider-kiro/keys?page=1&page_size=50&status={status}"), + None, + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + let payload: serde_json::Value = serde_json::from_slice( + &to_bytes(response.into_body(), usize::MAX) + .await + .expect("body should read"), + ) + .expect("json body should parse"); + let keys = payload["keys"].as_array().expect("keys should be array"); + assert_eq!(keys.len(), usize::from(status != "quota_exhausted")); + if let Some(key) = keys.first() { + assert_eq!(key["scheduling_status"], json!("available")); + assert_eq!(key["status_snapshot"]["quota"]["code"], json!("ok")); + assert_eq!(key["status_snapshot"]["quota"]["exhausted"], json!(false)); + assert_eq!(key["account_quota"], json!("剩余 100.0% (0/20)")); + } + } } #[tokio::test]