From c85ff74f3cdd9b346d1d4d929c29104ab16c658f Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Sat, 3 Jan 2026 16:44:25 -0500 Subject: [PATCH] feat(agy): add preflight quota check with auto-switch Before launching Claude CLI for Antigravity, check if current account has remaining quota. If exhausted, auto-switch to alternative account with available quota. Prevents session failures mid-use. --- src/cliproxy/cliproxy-executor.ts | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/cliproxy/cliproxy-executor.ts b/src/cliproxy/cliproxy-executor.ts index 0de849d6..6a031402 100644 --- a/src/cliproxy/cliproxy-executor.ts +++ b/src/cliproxy/cliproxy-executor.ts @@ -58,6 +58,7 @@ import { import { detectRunningProxy, waitForProxyHealthy, reclaimOrphanedProxy } from './proxy-detector'; import { withStartupLock } from './startup-lock'; import { loadOrCreateUnifiedConfig } from '../config/unified-config-loader'; +import { fetchAccountQuota, findAvailableAccount } from './quota-fetcher'; /** Default executor configuration */ const DEFAULT_CONFIG: ExecutorConfig = { @@ -463,6 +464,40 @@ export async function execClaudeWithCLIProxy( } } + // 3b. Preflight quota check - auto-switch to account with quota before launch + // Only for agy (Antigravity) which has quota tracking + if (provider === 'agy') { + const defaultAccount = getDefaultAccount(provider); + if (defaultAccount) { + log(`Checking quota for ${defaultAccount.email || defaultAccount.id}`); + const quota = await fetchAccountQuota(provider, defaultAccount.id); + + // Check if current account is exhausted (no model with >5% quota) + const hasQuota = quota.success && quota.models.some((m) => m.percentage > 5); + + if (!hasQuota && quota.success) { + // Current account exhausted, try to find alternative + log('Current account quota exhausted, searching for alternatives...'); + const alternative = await findAvailableAccount(provider, defaultAccount.id); + + if (alternative) { + // Auto-switch to account with remaining quota + setDefaultAccount(provider, alternative.account.id); + touchAccount(provider, alternative.account.id); + console.log( + info( + `Auto-switched to ${alternative.account.email || alternative.account.id} (current account quota exhausted)` + ) + ); + } else { + // No alternatives available - warn but continue + console.log(warn('All accounts appear quota-exhausted')); + console.log(` Run: ccs cliproxy doctor`); + } + } + } + } + // 4. First-run model configuration (interactive) // For supported providers, prompt user to select model on first run // Pass customSettingsPath for CLIProxy variants