Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(agy): reuse timed-out quota queries (#10232)

Ruixia Zhong8213acfdaced
diff --git a/src-tauri/src/commands/agy_account.rs b/src-tauri/src/commands/agy_account.rs
index 9db717cf5..25e4ac63e 100644
--- a/src-tauri/src/commands/agy_account.rs
+++ b/src-tauri/src/commands/agy_account.rs
@@ -59,7 +59,7 @@ pub struct AgyAccountState(pub Arc<AgyAccountManager>);
struct LiveQuery {
account_id: Option<String>,
at: Instant,
- quota: SubscriptionQuota,
+ result: Result<SubscriptionQuota, String>,
}
static LAST_LIVE_QUERY: LazyLock<Mutex<Option<LiveQuery>>> = LazyLock::new(|| Mutex::new(None));
@@ -219,32 +219,33 @@ where
{
let mut last = cache.lock().await;
if let Some(query) = last.as_ref() {
- let effective_reuse = if query.quota.success {
+ let effective_reuse = if query.result.as_ref().is_ok_and(|quota| quota.success) {
reuse
} else {
reuse.min(QUOTA_FAILURE_REUSE)
};
if query.account_id.as_deref() == current && query.at.elapsed() < effective_reuse {
return Ok(LiveQueryResult {
- quota: query.quota.clone(),
+ quota: query.result.clone()?,
should_record: false,
still_current: true,
});
}
}
- let quota = fetch().await?;
+ // 超时也走失败缓存,避免排队中的请求立即再次拉起 agy。
+ let result = fetch().await;
// 查询要七八秒:期间在别处换了号,这份结果就不知道是谁的,不记到任何账号上
let still_current = is_still_current().await;
- let should_record = still_current && quota.success;
+ let should_record = still_current && result.as_ref().is_ok_and(|quota| quota.success);
if still_current {
*last = Some(LiveQuery {
account_id: current.map(str::to_string),
at: Instant::now(),
- quota: quota.clone(),
+ result: result.clone(),
});
}
Ok(LiveQueryResult {
- quota,
+ quota: result?,
should_record,
still_current,
})
@@ -437,6 +438,7 @@ mod tests {
let cache = Mutex::new(None);
let calls = AtomicUsize::new(0);
let tray_reuse = Duration::from_secs(300);
+ let timeout = "Antigravity quota query timed out";
// 1. 失败在短 TTL(20s)内复用,防止并发/托盘反复拉起 agy
let first = query_live_with_cache(
@@ -445,15 +447,12 @@ mod tests {
tray_reuse,
|| async {
calls.fetch_add(1, Ordering::SeqCst);
- Ok(SubscriptionQuota::not_found(TOOL))
+ Err(timeout.to_string())
},
|| async { true },
)
- .await
- .unwrap();
- assert!(!first.quota.success);
- assert!(!first.should_record);
- assert!(first.still_current);
+ .await;
+ assert_eq!(first.err().as_deref(), Some(timeout));
assert_eq!(calls.load(Ordering::SeqCst), 1);
let cached_fail = query_live_with_cache(
@@ -466,11 +465,8 @@ mod tests {
},
|| async { true },
)
- .await
- .unwrap();
- assert!(!cached_fail.quota.success);
- assert!(!cached_fail.should_record);
- assert!(cached_fail.still_current);
+ .await;
+ assert_eq!(cached_fail.err().as_deref(), Some(timeout));
assert_eq!(calls.load(Ordering::SeqCst), 1);
// 2. 超过 20s 后失败过期(即使 tray_reuse = 300s),重新触发查询
@@ -513,7 +509,17 @@ mod tests {
assert!(!switched.should_record);
assert!(!switched.still_current);
assert_eq!(calls.load(Ordering::SeqCst), 3);
- assert!(!cache.lock().await.as_ref().unwrap().quota.success);
+ assert!(
+ !cache
+ .lock()
+ .await
+ .as_ref()
+ .unwrap()
+ .result
+ .as_ref()
+ .unwrap()
+ .success
+ );
// 4. 同一账号查询成功写入缓存,25s 后在 300s 复用窗口内仍命中
cache.lock().await.as_mut().unwrap().at = Instant::now() - Duration::from_secs(25);