mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
fix provider pool quota status handling
This commit is contained in:
@@ -111,7 +111,14 @@ fn reset_codex_cycle_usage_windows(status_snapshot: &mut Value, now_unix_secs: u
|
||||
}
|
||||
|
||||
window.insert("usage_reset_at".to_string(), json!(now_unix_secs));
|
||||
window.remove("usage");
|
||||
window.insert(
|
||||
"usage".to_string(),
|
||||
json!({
|
||||
"request_count": 0,
|
||||
"total_tokens": 0,
|
||||
"total_cost_usd": "0.00000000",
|
||||
}),
|
||||
);
|
||||
reset_count += 1;
|
||||
}
|
||||
|
||||
@@ -173,9 +180,13 @@ mod tests {
|
||||
assert_eq!(reset_codex_cycle_usage_windows(&mut snapshot, 1_234), 2);
|
||||
let windows = snapshot["quota"]["windows"].as_array().expect("windows");
|
||||
assert_eq!(windows[0]["usage_reset_at"], json!(1_234));
|
||||
assert!(windows[0].get("usage").is_none());
|
||||
assert_eq!(windows[0]["usage"]["request_count"], json!(0));
|
||||
assert_eq!(windows[0]["usage"]["total_tokens"], json!(0));
|
||||
assert_eq!(windows[0]["usage"]["total_cost_usd"], json!("0.00000000"));
|
||||
assert_eq!(windows[1]["usage_reset_at"], json!(1_234));
|
||||
assert!(windows[1].get("usage").is_none());
|
||||
assert_eq!(windows[1]["usage"]["request_count"], json!(0));
|
||||
assert_eq!(windows[1]["usage"]["total_tokens"], json!(0));
|
||||
assert_eq!(windows[1]["usage"]["total_cost_usd"], json!("0.00000000"));
|
||||
assert!(windows[2].get("usage_reset_at").is_none());
|
||||
assert!(windows[2].get("usage").is_some());
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ pub(crate) fn merge_provider_oauth_refresh_failure_reason(
|
||||
return Some(refresh_reason.to_string());
|
||||
}
|
||||
if current_reason.starts_with(OAUTH_EXPIRED_PREFIX) {
|
||||
return Some(refresh_reason.to_string());
|
||||
return Some(current_reason.to_string());
|
||||
}
|
||||
if oauth_invalid_reason_is_account_level_block(Some(current_reason)) {
|
||||
return None;
|
||||
@@ -190,13 +190,13 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refresh_failure_replaces_access_token_expired_marker() {
|
||||
fn refresh_failure_does_not_replace_access_token_expired_marker() {
|
||||
assert_eq!(
|
||||
merge_provider_oauth_refresh_failure_reason(
|
||||
Some("[OAUTH_EXPIRED] access token invalid"),
|
||||
"[REFRESH_FAILED] Token 续期失败 (401): refresh_token 无效",
|
||||
),
|
||||
Some("[REFRESH_FAILED] Token 续期失败 (401): refresh_token 无效".to_string()),
|
||||
Some("[OAUTH_EXPIRED] access token invalid".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,8 @@ use aether_admin::provider::quota as admin_provider_quota_pure;
|
||||
use aether_data_contracts::repository::provider_catalog::{
|
||||
StoredProviderCatalogEndpoint, StoredProviderCatalogKey,
|
||||
};
|
||||
use aether_data_contracts::repository::usage::{
|
||||
ProviderApiKeyWindowUsageRequest, StoredProviderApiKeyWindowUsageSummary,
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeMap;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn admin_pool_string_list(value: Option<&serde_json::Value>) -> Option<Vec<String>> {
|
||||
let values = value
|
||||
@@ -293,128 +290,6 @@ fn admin_pool_quota_window<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) type AdminPoolCodexWindowUsageByKey =
|
||||
BTreeMap<(String, String), StoredProviderApiKeyWindowUsageSummary>;
|
||||
|
||||
fn admin_pool_provider_type_is_codex(provider_type: &str) -> bool {
|
||||
provider_type.trim().eq_ignore_ascii_case("codex")
|
||||
}
|
||||
|
||||
fn admin_pool_codex_window_usage_code(
|
||||
window: &serde_json::Map<String, serde_json::Value>,
|
||||
) -> Option<&'static str> {
|
||||
let code = window
|
||||
.get("code")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::trim)?;
|
||||
if code.eq_ignore_ascii_case("5h") {
|
||||
Some("5h")
|
||||
} else if code.eq_ignore_ascii_case("weekly") {
|
||||
Some("weekly")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn admin_pool_codex_window_usage_bounds(
|
||||
window: &serde_json::Map<String, serde_json::Value>,
|
||||
) -> Option<(u64, u64)> {
|
||||
let reset_at = admin_pool_json_to_u64(window.get("reset_at"))?;
|
||||
let window_minutes = admin_pool_json_to_u64(window.get("window_minutes"))?;
|
||||
let window_seconds = window_minutes.checked_mul(60)?;
|
||||
let window_start = reset_at.checked_sub(window_seconds)?;
|
||||
let usage_reset_at = admin_pool_json_to_u64(window.get("usage_reset_at"))
|
||||
.filter(|reset_at_override| *reset_at_override < reset_at)
|
||||
.unwrap_or(window_start);
|
||||
let start = window_start.max(usage_reset_at);
|
||||
(start < reset_at).then_some((start, reset_at))
|
||||
}
|
||||
|
||||
pub(super) fn build_admin_pool_codex_window_usage_requests(
|
||||
provider_type: &str,
|
||||
keys: &[StoredProviderCatalogKey],
|
||||
) -> Vec<ProviderApiKeyWindowUsageRequest> {
|
||||
if !admin_pool_provider_type_is_codex(provider_type) {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut requests = Vec::new();
|
||||
for key in keys {
|
||||
let status_snapshot = provider_key_status_snapshot_payload(key, provider_type);
|
||||
let Some(windows) = status_snapshot
|
||||
.get("quota")
|
||||
.and_then(serde_json::Value::as_object)
|
||||
.and_then(|quota| quota.get("windows"))
|
||||
.and_then(serde_json::Value::as_array)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
|
||||
for window in windows.iter().filter_map(serde_json::Value::as_object) {
|
||||
let Some(window_code) = admin_pool_codex_window_usage_code(window) else {
|
||||
continue;
|
||||
};
|
||||
let Some((start_unix_secs, end_unix_secs)) =
|
||||
admin_pool_codex_window_usage_bounds(window)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
requests.push(ProviderApiKeyWindowUsageRequest {
|
||||
provider_api_key_id: key.id.clone(),
|
||||
window_code: window_code.to_string(),
|
||||
start_unix_secs,
|
||||
end_unix_secs,
|
||||
});
|
||||
}
|
||||
}
|
||||
requests
|
||||
}
|
||||
|
||||
fn admin_pool_codex_window_usage_payload(
|
||||
usage: &StoredProviderApiKeyWindowUsageSummary,
|
||||
) -> serde_json::Value {
|
||||
json!({
|
||||
"request_count": usage.request_count,
|
||||
"total_tokens": usage.total_tokens,
|
||||
"total_cost_usd": format!("{:.8}", usage.total_cost_usd),
|
||||
})
|
||||
}
|
||||
|
||||
fn admin_pool_attach_codex_window_usage(
|
||||
status_snapshot: &mut serde_json::Value,
|
||||
key_id: &str,
|
||||
usage_by_key: &AdminPoolCodexWindowUsageByKey,
|
||||
) {
|
||||
if usage_by_key.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(windows) = status_snapshot
|
||||
.get_mut("quota")
|
||||
.and_then(serde_json::Value::as_object_mut)
|
||||
.and_then(|quota| quota.get_mut("windows"))
|
||||
.and_then(serde_json::Value::as_array_mut)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
for window in windows
|
||||
.iter_mut()
|
||||
.filter_map(serde_json::Value::as_object_mut)
|
||||
{
|
||||
let Some(window_code) = admin_pool_codex_window_usage_code(window) else {
|
||||
continue;
|
||||
};
|
||||
let lookup_key = (key_id.to_string(), window_code.to_string());
|
||||
if let Some(usage) = usage_by_key.get(&lookup_key) {
|
||||
window.insert(
|
||||
"usage".to_string(),
|
||||
admin_pool_codex_window_usage_payload(usage),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn admin_pool_quota_window_used_percent(
|
||||
window: &serde_json::Map<String, serde_json::Value>,
|
||||
) -> Option<f64> {
|
||||
@@ -528,6 +403,54 @@ fn admin_pool_build_codex_account_quota_from_snapshot(
|
||||
None
|
||||
}
|
||||
|
||||
fn admin_pool_current_unix_secs() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.ok()
|
||||
.map(|duration| duration.as_secs())
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn admin_pool_prune_expired_codex_window_usage(status_snapshot: &mut serde_json::Value) {
|
||||
let now_unix_secs = admin_pool_current_unix_secs();
|
||||
let Some(windows) = status_snapshot
|
||||
.get_mut("quota")
|
||||
.and_then(serde_json::Value::as_object_mut)
|
||||
.and_then(|quota| quota.get_mut("windows"))
|
||||
.and_then(serde_json::Value::as_array_mut)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
for window in windows
|
||||
.iter_mut()
|
||||
.filter_map(serde_json::Value::as_object_mut)
|
||||
{
|
||||
let code = window
|
||||
.get("code")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(str::trim)
|
||||
.unwrap_or_default();
|
||||
if !code.eq_ignore_ascii_case("5h") && !code.eq_ignore_ascii_case("weekly") {
|
||||
continue;
|
||||
}
|
||||
let Some(reset_at) = admin_pool_json_to_u64(window.get("reset_at")) else {
|
||||
continue;
|
||||
};
|
||||
if now_unix_secs < reset_at {
|
||||
continue;
|
||||
}
|
||||
window.insert(
|
||||
"usage".to_string(),
|
||||
json!({
|
||||
"request_count": 0,
|
||||
"total_tokens": 0,
|
||||
"total_cost_usd": "0.00000000",
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn admin_pool_quota_windows<'a>(
|
||||
quota_snapshot: &'a serde_json::Map<String, serde_json::Value>,
|
||||
) -> Vec<&'a serde_json::Map<String, serde_json::Value>> {
|
||||
@@ -930,7 +853,6 @@ pub(super) fn build_admin_pool_key_payload(
|
||||
key: &StoredProviderCatalogKey,
|
||||
runtime: &AdminProviderPoolRuntimeState,
|
||||
pool_config: Option<AdminProviderPoolConfig>,
|
||||
codex_window_usage_by_key: &AdminPoolCodexWindowUsageByKey,
|
||||
) -> serde_json::Value {
|
||||
let cooldown_reason = runtime.cooldown_reason_by_key.get(&key.id).cloned();
|
||||
let cooldown_ttl_seconds = cooldown_reason
|
||||
@@ -949,12 +871,8 @@ pub(super) fn build_admin_pool_key_payload(
|
||||
let oauth_plan_type =
|
||||
admin_pool_derive_oauth_plan_type(key, provider_type, auth_config.as_ref());
|
||||
let mut status_snapshot = provider_key_status_snapshot_payload(key, provider_type);
|
||||
if admin_pool_provider_type_is_codex(provider_type) {
|
||||
admin_pool_attach_codex_window_usage(
|
||||
&mut status_snapshot,
|
||||
&key.id,
|
||||
codex_window_usage_by_key,
|
||||
);
|
||||
if provider_type.trim().eq_ignore_ascii_case("codex") {
|
||||
admin_pool_prune_expired_codex_window_usage(&mut status_snapshot);
|
||||
}
|
||||
let account_snapshot = status_snapshot
|
||||
.get("account")
|
||||
@@ -1237,34 +1155,54 @@ mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn codex_window_usage_bounds_honor_manual_usage_reset_at() {
|
||||
let window = json!({
|
||||
"code": "5h",
|
||||
"reset_at": 20_000,
|
||||
"window_minutes": 300,
|
||||
"usage_reset_at": 9_000,
|
||||
fn expired_codex_window_usage_is_zeroed_for_display() {
|
||||
let mut snapshot = json!({
|
||||
"quota": {
|
||||
"windows": [
|
||||
{
|
||||
"code": "5h",
|
||||
"reset_at": 1,
|
||||
"usage": {
|
||||
"request_count": 7,
|
||||
"total_tokens": 700,
|
||||
"total_cost_usd": "7.00000000"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
let window = window.as_object().expect("window object");
|
||||
|
||||
assert_eq!(
|
||||
admin_pool_codex_window_usage_bounds(window),
|
||||
Some((9_000, 20_000))
|
||||
);
|
||||
admin_pool_prune_expired_codex_window_usage(&mut snapshot);
|
||||
|
||||
let usage = &snapshot["quota"]["windows"][0]["usage"];
|
||||
assert_eq!(usage["request_count"], json!(0));
|
||||
assert_eq!(usage["total_tokens"], json!(0));
|
||||
assert_eq!(usage["total_cost_usd"], json!("0.00000000"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_window_usage_bounds_ignore_reset_before_window_start() {
|
||||
let window = json!({
|
||||
"code": "weekly",
|
||||
"reset_at": 700_000,
|
||||
"window_minutes": 10_080,
|
||||
"usage_reset_at": 1,
|
||||
fn active_codex_window_usage_is_preserved_for_display() {
|
||||
let mut snapshot = json!({
|
||||
"quota": {
|
||||
"windows": [
|
||||
{
|
||||
"code": "weekly",
|
||||
"reset_at": 4_102_444_800u64,
|
||||
"usage": {
|
||||
"request_count": 3,
|
||||
"total_tokens": 375,
|
||||
"total_cost_usd": "0.60000000"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
let window = window.as_object().expect("window object");
|
||||
|
||||
assert_eq!(
|
||||
admin_pool_codex_window_usage_bounds(window),
|
||||
Some((95_200, 700_000))
|
||||
);
|
||||
admin_pool_prune_expired_codex_window_usage(&mut snapshot);
|
||||
|
||||
let usage = &snapshot["quota"]["windows"][0]["usage"];
|
||||
assert_eq!(usage["request_count"], json!(3));
|
||||
assert_eq!(usage["total_tokens"], json!(375));
|
||||
assert_eq!(usage["total_cost_usd"], json!("0.60000000"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,36 +253,6 @@ pub(super) async fn build_admin_pool_list_keys_response(
|
||||
}
|
||||
_ => AdminProviderPoolRuntimeState::default(),
|
||||
};
|
||||
let codex_window_usage_requests =
|
||||
pool_payloads::build_admin_pool_codex_window_usage_requests(&provider.provider_type, &keys);
|
||||
let mut codex_window_usage_by_key = codex_window_usage_requests
|
||||
.iter()
|
||||
.map(|request| {
|
||||
(
|
||||
(
|
||||
request.provider_api_key_id.clone(),
|
||||
request.window_code.clone(),
|
||||
),
|
||||
aether_data_contracts::repository::usage::StoredProviderApiKeyWindowUsageSummary {
|
||||
provider_api_key_id: request.provider_api_key_id.clone(),
|
||||
window_code: request.window_code.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect::<pool_payloads::AdminPoolCodexWindowUsageByKey>();
|
||||
if !codex_window_usage_requests.is_empty() {
|
||||
for usage in state
|
||||
.app()
|
||||
.summarize_usage_by_provider_api_key_windows(&codex_window_usage_requests)
|
||||
.await?
|
||||
{
|
||||
codex_window_usage_by_key.insert(
|
||||
(usage.provider_api_key_id.clone(), usage.window_code.clone()),
|
||||
usage,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let items = keys
|
||||
.into_iter()
|
||||
@@ -294,7 +264,6 @@ pub(super) async fn build_admin_pool_list_keys_response(
|
||||
&key,
|
||||
&runtime,
|
||||
pool_config.clone(),
|
||||
&codex_window_usage_by_key,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
@@ -240,6 +240,10 @@ fn tagged_oauth_invalid_reason(reason: Option<&str>, prefix: &str) -> Option<Str
|
||||
})
|
||||
}
|
||||
|
||||
fn oauth_access_token_expired(expires_at_unix_secs: Option<u64>, now_unix_secs: u64) -> bool {
|
||||
expires_at_unix_secs.is_none_or(|expires_at| expires_at == 0 || expires_at <= now_unix_secs)
|
||||
}
|
||||
|
||||
fn build_provider_key_oauth_status_snapshot(key: &StoredProviderCatalogKey) -> Value {
|
||||
if !key.auth_type.trim().eq_ignore_ascii_case("oauth") {
|
||||
return default_oauth_status_snapshot_value();
|
||||
@@ -271,14 +275,16 @@ fn build_provider_key_oauth_status_snapshot(key: &StoredProviderCatalogKey) -> V
|
||||
if let Some(reason) =
|
||||
tagged_oauth_invalid_reason(invalid_reason.as_deref(), OAUTH_REFRESH_FAILED_PREFIX)
|
||||
{
|
||||
let access_token_expired = oauth_access_token_expired(expires_at_unix_secs, now_unix_secs);
|
||||
return json!({
|
||||
"code": "invalid",
|
||||
"label": "已失效",
|
||||
"code": if access_token_expired { "invalid" } else { "reauth_required" },
|
||||
"label": if access_token_expired { "已失效" } else { "续期失败" },
|
||||
"reason": reason,
|
||||
"expires_at": expires_at_unix_secs,
|
||||
"invalid_at": invalid_at_unix_secs,
|
||||
"source": "oauth_refresh",
|
||||
"requires_reauth": true,
|
||||
"usable_until_expiry": !access_token_expired,
|
||||
"expiring_soon": false,
|
||||
});
|
||||
}
|
||||
@@ -320,11 +326,11 @@ fn build_provider_key_oauth_status_snapshot(key: &StoredProviderCatalogKey) -> V
|
||||
return json!({
|
||||
"code": "expired",
|
||||
"label": "已过期",
|
||||
"reason": "Token 已过期,请重新授权",
|
||||
"reason": "Access Token 已过期,等待自动续期",
|
||||
"expires_at": expires_at_unix_secs,
|
||||
"invalid_at": Value::Null,
|
||||
"source": "expires_at",
|
||||
"requires_reauth": true,
|
||||
"requires_reauth": false,
|
||||
"expiring_soon": false,
|
||||
});
|
||||
}
|
||||
@@ -556,10 +562,7 @@ fn quota_windows_all_exhausted(windows: &[Value]) -> bool {
|
||||
total > 0 && exhausted == total
|
||||
}
|
||||
|
||||
fn preserve_quota_window_usage_reset_at(
|
||||
current_status_snapshot: Option<&Value>,
|
||||
quota: &mut Value,
|
||||
) {
|
||||
fn preserve_quota_window_usage_state(current_status_snapshot: Option<&Value>, quota: &mut Value) {
|
||||
let Some(current_windows) = current_status_snapshot
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|snapshot| snapshot.get("quota"))
|
||||
@@ -582,23 +585,39 @@ fn preserve_quota_window_usage_reset_at(
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let Some(usage_reset_at) = current_windows
|
||||
.iter()
|
||||
.filter_map(Value::as_object)
|
||||
.find(|current_window| {
|
||||
current_window
|
||||
.get("code")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.is_some_and(|current_code| current_code.eq_ignore_ascii_case(code))
|
||||
})
|
||||
.and_then(|current_window| current_window.get("usage_reset_at"))
|
||||
.and_then(admin_provider_quota_pure::coerce_json_u64)
|
||||
else {
|
||||
let current_window =
|
||||
current_windows
|
||||
.iter()
|
||||
.filter_map(Value::as_object)
|
||||
.find(|current_window| {
|
||||
current_window
|
||||
.get("code")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.is_some_and(|current_code| current_code.eq_ignore_ascii_case(code))
|
||||
});
|
||||
let Some(current_window) = current_window else {
|
||||
continue;
|
||||
};
|
||||
let current_reset_at = current_window
|
||||
.get("reset_at")
|
||||
.and_then(admin_provider_quota_pure::coerce_json_u64);
|
||||
let next_reset_at = next_window
|
||||
.get("reset_at")
|
||||
.and_then(admin_provider_quota_pure::coerce_json_u64);
|
||||
if current_reset_at.is_none() || current_reset_at != next_reset_at {
|
||||
continue;
|
||||
}
|
||||
|
||||
next_window.insert("usage_reset_at".to_string(), json!(usage_reset_at));
|
||||
if let Some(usage_reset_at) = current_window
|
||||
.get("usage_reset_at")
|
||||
.and_then(admin_provider_quota_pure::coerce_json_u64)
|
||||
{
|
||||
next_window.insert("usage_reset_at".to_string(), json!(usage_reset_at));
|
||||
}
|
||||
if let Some(usage) = current_window.get("usage") {
|
||||
next_window.insert("usage".to_string(), usage.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1162,7 +1181,7 @@ pub(crate) fn sync_provider_key_quota_status_snapshot(
|
||||
_ => None,
|
||||
}?;
|
||||
if normalized_provider_type == "codex" {
|
||||
preserve_quota_window_usage_reset_at(status_snapshot, &mut quota);
|
||||
preserve_quota_window_usage_state(status_snapshot, &mut quota);
|
||||
}
|
||||
|
||||
let default_snapshot = default_provider_key_status_snapshot();
|
||||
@@ -2062,7 +2081,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_provider_key_quota_status_snapshot_preserves_codex_usage_reset_at() {
|
||||
fn sync_provider_key_quota_status_snapshot_preserves_codex_usage_state() {
|
||||
let current_status_snapshot = json!({
|
||||
"quota": {
|
||||
"version": 2,
|
||||
@@ -2071,12 +2090,22 @@ mod tests {
|
||||
{
|
||||
"code": "weekly",
|
||||
"usage_reset_at": 1_775_600_000u64,
|
||||
"usage": {
|
||||
"request_count": 3,
|
||||
"total_tokens": 375,
|
||||
"total_cost_usd": "0.60000000"
|
||||
},
|
||||
"reset_at": 1_900_000_000u64,
|
||||
"window_minutes": 10_080u64
|
||||
},
|
||||
{
|
||||
"code": "5h",
|
||||
"usage_reset_at": 1_775_700_000u64,
|
||||
"usage": {
|
||||
"request_count": 2,
|
||||
"total_tokens": 225,
|
||||
"total_cost_usd": "0.30000000"
|
||||
},
|
||||
"reset_at": 1_900_500_000u64,
|
||||
"window_minutes": 300u64
|
||||
}
|
||||
@@ -2088,10 +2117,10 @@ mod tests {
|
||||
"updated_at": 1_775_800_000u64,
|
||||
"plan_type": "plus",
|
||||
"primary_used_percent": 5.0,
|
||||
"primary_reset_at": 1_901_000_000u64,
|
||||
"primary_reset_at": 1_900_000_000u64,
|
||||
"primary_window_minutes": 10_080u64,
|
||||
"secondary_used_percent": 1.0,
|
||||
"secondary_reset_at": 1_901_500_000u64,
|
||||
"secondary_reset_at": 1_900_500_000u64,
|
||||
"secondary_window_minutes": 300u64
|
||||
}
|
||||
});
|
||||
@@ -2121,7 +2150,94 @@ mod tests {
|
||||
.expect("5h window should exist");
|
||||
|
||||
assert_eq!(weekly.get("usage_reset_at"), Some(&json!(1_775_600_000u64)));
|
||||
assert_eq!(
|
||||
weekly
|
||||
.get("usage")
|
||||
.and_then(|usage| usage.get("request_count")),
|
||||
Some(&json!(3))
|
||||
);
|
||||
assert_eq!(
|
||||
weekly
|
||||
.get("usage")
|
||||
.and_then(|usage| usage.get("total_tokens")),
|
||||
Some(&json!(375))
|
||||
);
|
||||
assert_eq!(
|
||||
weekly
|
||||
.get("usage")
|
||||
.and_then(|usage| usage.get("total_cost_usd")),
|
||||
Some(&json!("0.60000000"))
|
||||
);
|
||||
assert_eq!(five_h.get("usage_reset_at"), Some(&json!(1_775_700_000u64)));
|
||||
assert_eq!(
|
||||
five_h
|
||||
.get("usage")
|
||||
.and_then(|usage| usage.get("request_count")),
|
||||
Some(&json!(2))
|
||||
);
|
||||
assert_eq!(
|
||||
five_h
|
||||
.get("usage")
|
||||
.and_then(|usage| usage.get("total_tokens")),
|
||||
Some(&json!(225))
|
||||
);
|
||||
assert_eq!(
|
||||
five_h
|
||||
.get("usage")
|
||||
.and_then(|usage| usage.get("total_cost_usd")),
|
||||
Some(&json!("0.30000000"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_provider_key_quota_status_snapshot_drops_codex_usage_state_when_window_resets() {
|
||||
let current_status_snapshot = json!({
|
||||
"quota": {
|
||||
"version": 2,
|
||||
"provider_type": "codex",
|
||||
"windows": [
|
||||
{
|
||||
"code": "weekly",
|
||||
"usage_reset_at": 1_775_600_000u64,
|
||||
"usage": {
|
||||
"request_count": 3,
|
||||
"total_tokens": 375,
|
||||
"total_cost_usd": "0.60000000"
|
||||
},
|
||||
"reset_at": 1_900_000_000u64,
|
||||
"window_minutes": 10_080u64
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
let upstream_metadata = json!({
|
||||
"codex": {
|
||||
"updated_at": 1_900_000_100u64,
|
||||
"plan_type": "plus",
|
||||
"primary_used_percent": 0.0,
|
||||
"primary_reset_at": 1_960_480_100u64,
|
||||
"primary_window_minutes": 10_080u64
|
||||
}
|
||||
});
|
||||
|
||||
let payload = sync_provider_key_quota_status_snapshot(
|
||||
Some(¤t_status_snapshot),
|
||||
"codex",
|
||||
Some(&upstream_metadata),
|
||||
"refresh_api",
|
||||
)
|
||||
.expect("quota snapshot should sync");
|
||||
let weekly = payload["quota"]["windows"]
|
||||
.as_array()
|
||||
.expect("quota windows should exist")
|
||||
.iter()
|
||||
.filter_map(Value::as_object)
|
||||
.find(|window| window.get("code") == Some(&json!("weekly")))
|
||||
.expect("weekly window should exist");
|
||||
|
||||
assert_eq!(weekly.get("reset_at"), Some(&json!(1_960_480_100u64)));
|
||||
assert!(weekly.get("usage_reset_at").is_none());
|
||||
assert!(weekly.get("usage").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user