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): skip live quota queries while the gateway is in use

Jason8a5c5886b390
diff --git a/src-tauri/src/commands/agy_account.rs b/src-tauri/src/commands/agy_account.rs
index 01a09ae51..9ef2aa717 100644
--- a/src-tauri/src/commands/agy_account.rs
+++ b/src-tauri/src/commands/agy_account.rs
@@ -76,9 +76,15 @@ pub(crate) fn remember_account_quota(
crate::tray::schedule_tray_refresh(app);
}
+/// 要不要只给快照、不起 agy:指定的号不是 agy 当前登录的,或者网关在用(钥匙串里的登录
+/// 没在用)。只有 agy 正在用的那个登录才实时查。
+fn snapshot_only(gateway_in_use: bool, requested: Option<&str>, current: Option<&str>) -> bool {
+ gateway_in_use || requested.is_some_and(|requested| current != Some(requested))
+}
+
/// Antigravity 官方登录的额度,只问 agy 自己的 `/usage`(不用 agy 的身份发请求)。
-/// 不指定账号就是 agy 现在登着的号;指定的号不是 agy 当前登录时,返回它最后一次查到的。
-/// `reuse` 内查过同一个号就不再起 agy。查成功的结果按账号记下。
+/// 不指定账号就是 agy 现在登着的号;指定的号不是 agy 当前登录、或者网关在用时,返回它最后
+/// 一次查到的。`reuse` 内查过同一个号就不再起 agy。查成功的结果按账号记下。
pub(crate) async fn agy_quota(
manager: &AgyAccountManager,
account_id: Option<&str>,
@@ -88,13 +94,14 @@ pub(crate) async fn agy_quota(
return crate::services::subscription_agy::get_agy_subscription_quota().await;
}
let current = manager.current_account_id().await;
- if let Some(account_id) = account_id {
- if current.as_deref() != Some(account_id) {
- return Ok(manager
- .last_quota(account_id)
- .await
- .unwrap_or_else(|| SubscriptionQuota::not_found(TOOL)));
- }
+ let gateway = crate::services::subscription_agy::gateway_in_use().await;
+ if snapshot_only(gateway, account_id, current.as_deref()) {
+ let shown = account_id.or(current.as_deref());
+ let last = match shown {
+ Some(id) => manager.last_quota(id).await,
+ None => None,
+ };
+ return Ok(last.unwrap_or_else(|| SubscriptionQuota::not_found(TOOL)));
}
let outcome = query_live_with_cache(
@@ -418,4 +425,16 @@ mod tests {
"env -u GEMINI_API_KEY -u GOOGLE_GEMINI_BASE_URL '/Users/o'\\''neil/.local/bin/agy'"
);
}
+
+ #[test]
+ fn only_the_login_agy_is_using_is_queried_live() {
+ // 没开网关:当前登录(不指定或指定的就是它)实时查,别的号给快照
+ assert!(!snapshot_only(false, None, Some("a")));
+ assert!(!snapshot_only(false, Some("a"), Some("a")));
+ assert!(snapshot_only(false, Some("b"), Some("a")));
+ // 网关在用:钥匙串里的登录没在用,谁都只给快照
+ assert!(snapshot_only(true, None, Some("a")));
+ assert!(snapshot_only(true, Some("a"), Some("a")));
+ assert!(snapshot_only(true, None, None));
+ }
}
diff --git a/src-tauri/src/services/agy_account/tests.rs b/src-tauri/src/services/agy_account/tests.rs
index 66ae789d1..008c6650f 100644
--- a/src-tauri/src/services/agy_account/tests.rs
+++ b/src-tauri/src/services/agy_account/tests.rs
@@ -114,7 +114,10 @@ mod switching {
}
#[test]
+ #[serial]
fn saving_a_new_provider_binding_changes_the_live_login() {
+ // 保存会写 config.json 和设备状态,都在进程共用的测试 home 下;用自己的 home。
+ let _home = TestHome::new();
let (_dir, login, manager) = manager();
let state = AppState::new(Arc::new(Database::memory().unwrap()));
set_live(&login, &credential("alice", "a1"));
@@ -315,7 +318,10 @@ mod switching {
}
#[test]
+ #[serial]
fn saving_a_binding_recovers_from_a_transient_readback_failure() {
+ // 保存会写 config.json 和设备状态,都在进程共用的测试 home 下;用自己的 home。
+ let _home = TestHome::new();
let (_dir, login, manager) = manager();
let state = AppState::new(Arc::new(Database::memory().unwrap()));
set_live(&login, &credential("alice", "a1"));
@@ -1214,6 +1220,7 @@ mod running_guard_and_scope {
}
#[test]
+ #[serial_test::serial]
fn saving_new_provider_binding_rolls_back_db_when_switch_fails() {
use crate::app_config::AppType;
use crate::database::Database;
@@ -1221,6 +1228,8 @@ mod running_guard_and_scope {
use crate::services::provider::agy_direct::save_from_editor;
use crate::store::AppState;
+ // 保存会写 config.json 和设备状态,都在进程共用的测试 home 下;用自己的 home。
+ let _home = crate::test_support::TestHome::new();
let (_dir, _desktop_dir, login, manager) = test_env();
set_live(&login, &credential("alice", "a1"));
tauri::async_runtime::block_on(add(&manager, "bob"));
@@ -1258,7 +1267,7 @@ mod running_guard_and_scope {
.as_mut()
.unwrap()
.account_id = Some("bob".into());
- assert!(save_from_editor(&state, &manager, bob_card).is_err());
+ assert!(save_from_editor(&state, &manager, bob_card, None).is_err());
// DB row must remain bound to alice and keychain must stay on alice.
let persisted = state
diff --git a/src-tauri/src/services/subscription_agy.rs b/src-tauri/src/services/subscription_agy.rs
index 3572f0dee..7a48cf8c9 100644
--- a/src-tauri/src/services/subscription_agy.rs
+++ b/src-tauri/src/services/subscription_agy.rs
@@ -193,9 +193,21 @@ async fn run_command_output(
}
}
-/// 由 `subscription::get_subscription_quota("agy")` 分发。没装 agy 时按「没有凭据」处理(卡片不显示);
-/// 超时、没登录或输出看不懂都写成查询失败,错误里不带 agy 的原始输出。
+/// `config.json` 开着网关(启用的是第三方卡):agy 走网关,官方登录没在用,`/usage` 只会
+/// 返回空的额度组。按「只查正在用的登录」,这时谁的额度都不实时查。
+pub(crate) async fn gateway_in_use() -> bool {
+ tokio::task::spawn_blocking(crate::agy_config::live_gateway_enabled)
+ .await
+ .unwrap_or(false)
+}
+
+/// 由 `subscription::get_subscription_quota("agy")` 分发。没装 agy、或网关在用(官方登录没在用)
+/// 时按「没有凭据」处理(卡片不显示);超时、没登录或输出看不懂都写成查询失败,错误里不带
+/// agy 的原始输出。
pub(crate) async fn get_agy_subscription_quota() -> Result<SubscriptionQuota, String> {
+ if gateway_in_use().await {
+ return Ok(SubscriptionQuota::not_found(TOOL));
+ }
let Some(bin) = tokio::task::spawn_blocking(crate::agy_config::real_agy_bin)
.await
.map_err(|e| e.to_string())?
@@ -302,6 +314,26 @@ mod tests {
assert_eq!(tiers[2].resets_at.as_deref(), Some("2026-10-27T18:00:00Z"));
}
+ /// 网关在用时不起 agy:直接按「没有凭据」返回,不会把空的额度组报成「没登录」。
+ #[tokio::test]
+ #[serial_test::serial]
+ async fn no_live_query_while_the_gateway_is_in_use() {
+ let _home = crate::test_support::TestHome::new();
+ let path = crate::agy_config::gateway_config_path();
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(
+ &path,
+ r#"{"gateway":{"enabled":true,"url":"https://relay.example","apiKey":"k"}}"#,
+ )
+ .unwrap();
+ let quota = get_agy_subscription_quota().await.unwrap();
+ assert!(!quota.success);
+ assert_eq!(
+ serde_json::to_value(&quota).unwrap(),
+ serde_json::to_value(SubscriptionQuota::not_found(TOOL)).unwrap()
+ );
+ }
+
#[test]
fn failed_or_empty_output_is_not_quota() {
assert!(parse_usage(b"not json").is_none());