forked from tctinh/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccounts.ts
More file actions
1258 lines (1095 loc) · 43.3 KB
/
accounts.ts
File metadata and controls
1258 lines (1095 loc) · 43.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { formatRefreshParts, parseRefreshParts } from "./auth";
import { loadAccounts, saveAccounts, type AccountStorageV4, type AccountMetadataV3, type RateLimitStateV3, type ModelFamily, type HeaderStyle, type CooldownReason } from "./storage";
import type { OAuthAuthDetails, RefreshParts } from "./types";
import type { AccountSelectionStrategy } from "./config/schema";
import { getHealthTracker, getTokenTracker, selectHybridAccount, type AccountWithMetrics } from "./rotation";
import { generateFingerprint, updateFingerprintVersion, type Fingerprint, type FingerprintVersion, MAX_FINGERPRINT_HISTORY } from "./fingerprint";
import type { QuotaGroup, QuotaGroupSummary } from "./quota";
import { getModelFamily } from "./transform/model-resolver";
import { debugLogToFile } from "./debug";
import { formatAccountLabel } from "./logging-utils";
export type { ModelFamily, HeaderStyle, CooldownReason } from "./storage";
export type { AccountSelectionStrategy } from "./config/schema";
export type RateLimitReason =
| "QUOTA_EXHAUSTED"
| "RATE_LIMIT_EXCEEDED"
| "MODEL_CAPACITY_EXHAUSTED"
| "SERVER_ERROR"
| "UNKNOWN";
export interface RateLimitBackoffResult {
backoffMs: number;
reason: RateLimitReason;
}
const QUOTA_EXHAUSTED_BACKOFFS = [60_000, 300_000, 1_800_000, 7_200_000] as const;
const RATE_LIMIT_EXCEEDED_BACKOFF = 30_000;
// Increased from 15s to 45s base + jitter to reduce retry pressure on capacity errors
const MODEL_CAPACITY_EXHAUSTED_BASE_BACKOFF = 45_000;
const MODEL_CAPACITY_EXHAUSTED_JITTER_MAX = 30_000; // ±15s jitter range
const SERVER_ERROR_BACKOFF = 20_000;
const UNKNOWN_BACKOFF = 60_000;
const MIN_BACKOFF_MS = 2_000;
/**
* Generate a random jitter value for backoff timing.
* Helps prevent thundering herd problem when multiple clients retry simultaneously.
*/
function generateJitter(maxJitterMs: number): number {
return Math.random() * maxJitterMs - (maxJitterMs / 2);
}
export function parseRateLimitReason(
reason: string | undefined,
message: string | undefined,
status?: number
): RateLimitReason {
// 1. Status Code Checks (Rust parity)
// 529 = Site Overloaded, 503 = Service Unavailable -> Capacity issues
if (status === 529 || status === 503) return "MODEL_CAPACITY_EXHAUSTED";
// 500 = Internal Server Error -> Treat as Server Error (soft wait)
if (status === 500) return "SERVER_ERROR";
// 2. Explicit Reason String
if (reason) {
switch (reason.toUpperCase()) {
case "QUOTA_EXHAUSTED": return "QUOTA_EXHAUSTED";
case "RATE_LIMIT_EXCEEDED": return "RATE_LIMIT_EXCEEDED";
case "MODEL_CAPACITY_EXHAUSTED": return "MODEL_CAPACITY_EXHAUSTED";
}
}
// 3. Message Text Scanning (Rust Regex parity)
if (message) {
const lower = message.toLowerCase();
// Capacity / Overloaded (Transient) - Check FIRST before "exhausted"
if (lower.includes("capacity") || lower.includes("overloaded") || lower.includes("resource exhausted")) {
return "MODEL_CAPACITY_EXHAUSTED";
}
// RPM / TPM (Short Wait)
// "per minute", "rate limit", "too many requests"
// "presque" (French: almost) - retained for i18n parity with Rust reference
if (lower.includes("per minute") || lower.includes("rate limit") || lower.includes("too many requests") || lower.includes("presque")) {
return "RATE_LIMIT_EXCEEDED";
}
// Quota (Long Wait)
if (lower.includes("exhausted") || lower.includes("quota")) {
return "QUOTA_EXHAUSTED";
}
}
// Default fallback for 429 without clearer info
if (status === 429) {
return "UNKNOWN";
}
return "UNKNOWN";
}
export function calculateBackoffMs(
reason: RateLimitReason,
consecutiveFailures: number,
retryAfterMs?: number | null
): number {
// Respect explicit Retry-After header if reasonable
if (retryAfterMs && retryAfterMs > 0) {
// Rust uses 2s min buffer, we keep 2s
return Math.max(retryAfterMs, MIN_BACKOFF_MS);
}
switch (reason) {
case "QUOTA_EXHAUSTED": {
const index = Math.min(consecutiveFailures, QUOTA_EXHAUSTED_BACKOFFS.length - 1);
return QUOTA_EXHAUSTED_BACKOFFS[index] ?? UNKNOWN_BACKOFF;
}
case "RATE_LIMIT_EXCEEDED":
return RATE_LIMIT_EXCEEDED_BACKOFF; // 30s
case "MODEL_CAPACITY_EXHAUSTED":
// Apply jitter to prevent thundering herd on capacity errors
return MODEL_CAPACITY_EXHAUSTED_BASE_BACKOFF + generateJitter(MODEL_CAPACITY_EXHAUSTED_JITTER_MAX);
case "SERVER_ERROR":
return SERVER_ERROR_BACKOFF; // 20s
case "UNKNOWN":
default:
return UNKNOWN_BACKOFF; // 60s
}
}
export type BaseQuotaKey = "claude" | "gemini-antigravity" | "gemini-cli";
export type QuotaKey = BaseQuotaKey | `${BaseQuotaKey}:${string}`;
export interface ManagedAccount {
index: number;
email?: string;
addedAt: number;
lastUsed: number;
parts: RefreshParts;
access?: string;
expires?: number;
enabled: boolean;
rateLimitResetTimes: RateLimitStateV3;
lastSwitchReason?: "rate-limit" | "initial" | "rotation";
coolingDownUntil?: number;
cooldownReason?: CooldownReason;
touchedForQuota: Record<string, number>;
consecutiveFailures?: number;
/** Timestamp of last failure for TTL-based reset of consecutiveFailures */
lastFailureTime?: number;
/** Per-account device fingerprint for rate limit mitigation */
fingerprint?: import("./fingerprint").Fingerprint;
/** History of previous fingerprints for this account */
fingerprintHistory?: FingerprintVersion[];
/** Cached quota data from last checkAccountsQuota() call */
cachedQuota?: Partial<Record<QuotaGroup, QuotaGroupSummary>>;
cachedQuotaUpdatedAt?: number;
verificationRequired?: boolean;
verificationRequiredAt?: number;
verificationRequiredReason?: string;
verificationUrl?: string;
}
function nowMs(): number {
return Date.now();
}
function clampNonNegativeInt(value: unknown, fallback: number): number {
if (typeof value !== "number" || !Number.isFinite(value)) {
return fallback;
}
return value < 0 ? 0 : Math.floor(value);
}
function getQuotaKey(family: ModelFamily, headerStyle: HeaderStyle, model?: string | null): QuotaKey {
if (family === "claude") {
return "claude";
}
const base = headerStyle === "gemini-cli" ? "gemini-cli" : "gemini-antigravity";
if (model) {
return `${base}:${model}`;
}
return base;
}
function isRateLimitedForQuotaKey(account: ManagedAccount, key: QuotaKey): boolean {
const resetTime = account.rateLimitResetTimes[key];
return resetTime !== undefined && nowMs() < resetTime;
}
function isRateLimitedForFamily(account: ManagedAccount, family: ModelFamily, model?: string | null): boolean {
if (family === "claude") {
return isRateLimitedForQuotaKey(account, "claude");
}
const antigravityIsLimited = isRateLimitedForHeaderStyle(account, family, "antigravity", model);
const cliIsLimited = isRateLimitedForHeaderStyle(account, family, "gemini-cli", model);
return antigravityIsLimited && cliIsLimited;
}
function isRateLimitedForHeaderStyle(account: ManagedAccount, family: ModelFamily, headerStyle: HeaderStyle, model?: string | null): boolean {
clearExpiredRateLimits(account);
if (family === "claude") {
return isRateLimitedForQuotaKey(account, "claude");
}
// Check model-specific quota first if provided
if (model) {
const modelKey = getQuotaKey(family, headerStyle, model);
if (isRateLimitedForQuotaKey(account, modelKey)) {
return true;
}
}
// Then check base family quota
const baseKey = getQuotaKey(family, headerStyle);
return isRateLimitedForQuotaKey(account, baseKey);
}
function clearExpiredRateLimits(account: ManagedAccount): void {
const now = nowMs();
const keys = Object.keys(account.rateLimitResetTimes) as QuotaKey[];
for (const key of keys) {
const resetTime = account.rateLimitResetTimes[key];
if (resetTime !== undefined && now >= resetTime) {
delete account.rateLimitResetTimes[key];
}
}
}
/**
* Resolve the quota group for soft quota checks.
*
* When a model string is available, we can precisely determine the quota group.
* When model is null/undefined, we fall back based on family:
* - Claude → "claude" quota group
* - Gemini → "gemini-pro" (conservative fallback; may misclassify flash models)
*
* @param family - The model family ("claude" | "gemini")
* @param model - Optional model string for precise resolution
* @returns The QuotaGroup to use for soft quota checks
*/
export function resolveQuotaGroup(family: ModelFamily, model?: string | null): QuotaGroup {
if (model) {
return getModelFamily(model);
}
return family === "claude" ? "claude" : "gemini-pro";
}
function isOverSoftQuotaThreshold(
account: ManagedAccount,
family: ModelFamily,
thresholdPercent: number,
cacheTtlMs: number,
model?: string | null
): boolean {
if (thresholdPercent >= 100) return false;
if (!account.cachedQuota) return false;
if (account.cachedQuotaUpdatedAt == null) return false;
const age = nowMs() - account.cachedQuotaUpdatedAt;
if (age > cacheTtlMs) return false;
const quotaGroup = resolveQuotaGroup(family, model);
const groupData = account.cachedQuota[quotaGroup];
if (groupData?.remainingFraction == null) return false;
const remainingFraction = Math.max(0, Math.min(1, groupData.remainingFraction));
const usedPercent = (1 - remainingFraction) * 100;
const isOverThreshold = usedPercent >= thresholdPercent;
if (isOverThreshold) {
const accountLabel = formatAccountLabel(account.email, account.index);
const resetSuffix = groupData.resetTime ? ` (resets: ${groupData.resetTime})` : "";
const message = `[SoftQuota] Skipping ${accountLabel}: ${quotaGroup} usage ${usedPercent.toFixed(1)}% >= threshold ${thresholdPercent}%${resetSuffix}`;
debugLogToFile(message);
}
return isOverThreshold;
}
export function computeSoftQuotaCacheTtlMs(
ttlConfig: "auto" | number,
refreshIntervalMinutes: number
): number {
if (ttlConfig === "auto") {
return Math.max(2 * refreshIntervalMinutes, 10) * 60 * 1000;
}
return ttlConfig * 60 * 1000;
}
/**
* In-memory multi-account manager with sticky account selection.
*
* Uses the same account until it hits a rate limit (429), then switches.
* Rate limits are tracked per-model-family (claude/gemini) so an account
* rate-limited for Claude can still be used for Gemini.
*
* Source of truth for the pool is `antigravity-accounts.json`.
*/
export class AccountManager {
private accounts: ManagedAccount[] = [];
private cursor = 0;
private currentAccountIndexByFamily: Record<ModelFamily, number> = {
claude: -1,
gemini: -1,
};
private sessionOffsetApplied: Record<ModelFamily, boolean> = {
claude: false,
gemini: false,
};
private lastToastAccountIndex = -1;
private lastToastTime = 0;
private savePending = false;
private saveTimeout: ReturnType<typeof setTimeout> | null = null;
private savePromiseResolvers: Array<() => void> = [];
private lastSavedSnapshot: string | null = null;
static async loadFromDisk(authFallback?: OAuthAuthDetails): Promise<AccountManager> {
const stored = await loadAccounts();
return new AccountManager(authFallback, stored);
}
constructor(authFallback?: OAuthAuthDetails, stored?: AccountStorageV4 | null) {
const authParts = authFallback ? parseRefreshParts(authFallback.refresh) : null;
if (stored && stored.accounts.length === 0) {
this.accounts = [];
this.cursor = 0;
return;
}
if (stored && stored.accounts.length > 0) {
const baseNow = nowMs();
this.accounts = stored.accounts
.map((acc, index): ManagedAccount | null => {
if (!acc.refreshToken || typeof acc.refreshToken !== "string") {
return null;
}
const matchesFallback = !!(
authFallback &&
authParts &&
authParts.refreshToken &&
acc.refreshToken === authParts.refreshToken
);
return {
index,
email: acc.email,
addedAt: clampNonNegativeInt(acc.addedAt, baseNow),
lastUsed: clampNonNegativeInt(acc.lastUsed, 0),
parts: {
refreshToken: acc.refreshToken,
projectId: acc.projectId,
managedProjectId: acc.managedProjectId,
},
access: matchesFallback ? authFallback?.access : undefined,
expires: matchesFallback ? authFallback?.expires : undefined,
enabled: acc.enabled !== false,
rateLimitResetTimes: acc.rateLimitResetTimes ?? {},
lastSwitchReason: acc.lastSwitchReason,
coolingDownUntil: acc.coolingDownUntil,
cooldownReason: acc.cooldownReason,
touchedForQuota: {},
fingerprint: acc.fingerprint ?? generateFingerprint(),
fingerprintHistory: acc.fingerprintHistory ?? [],
cachedQuota: acc.cachedQuota as Partial<Record<QuotaGroup, QuotaGroupSummary>> | undefined,
cachedQuotaUpdatedAt: acc.cachedQuotaUpdatedAt,
verificationRequired: acc.verificationRequired,
verificationRequiredAt: acc.verificationRequiredAt,
verificationRequiredReason: acc.verificationRequiredReason,
verificationUrl: acc.verificationUrl,
};
})
.filter((a): a is ManagedAccount => a !== null);
// Update fingerprint versions to match the current runtime version.
// Saved fingerprints may carry an older version string; this ensures
// they always reflect the latest fetched (or fallback) version.
let fingerprintVersionChanged = false;
for (const acc of this.accounts) {
if (acc.fingerprint && updateFingerprintVersion(acc.fingerprint)) {
fingerprintVersionChanged = true;
}
}
this.cursor = clampNonNegativeInt(stored.activeIndex, 0);
if (this.accounts.length > 0) {
this.cursor = this.cursor % this.accounts.length;
const defaultIndex = this.cursor;
this.currentAccountIndexByFamily.claude = clampNonNegativeInt(
stored.activeIndexByFamily?.claude,
defaultIndex
) % this.accounts.length;
this.currentAccountIndexByFamily.gemini = clampNonNegativeInt(
stored.activeIndexByFamily?.gemini,
defaultIndex
) % this.accounts.length;
}
// Persist updated fingerprint versions to disk
if (fingerprintVersionChanged) {
this.requestSaveToDisk();
}
return;
}
// If we have stored accounts, check if we need to add the current auth
if (authFallback && this.accounts.length > 0) {
const authParts = parseRefreshParts(authFallback.refresh);
const hasMatching = this.accounts.some(acc => acc.parts.refreshToken === authParts.refreshToken);
if (!hasMatching && authParts.refreshToken) {
const now = nowMs();
const newAccount: ManagedAccount = {
index: this.accounts.length,
email: undefined,
addedAt: now,
lastUsed: 0,
parts: authParts,
access: authFallback.access,
expires: authFallback.expires,
enabled: true,
rateLimitResetTimes: {},
touchedForQuota: {},
};
this.accounts.push(newAccount);
// Update indices to include the new account
this.currentAccountIndexByFamily.claude = Math.min(this.currentAccountIndexByFamily.claude, this.accounts.length - 1);
this.currentAccountIndexByFamily.gemini = Math.min(this.currentAccountIndexByFamily.gemini, this.accounts.length - 1);
}
}
if (authFallback) {
const parts = parseRefreshParts(authFallback.refresh);
if (parts.refreshToken) {
const now = nowMs();
this.accounts = [
{
index: 0,
email: undefined,
addedAt: now,
lastUsed: 0,
parts,
access: authFallback.access,
expires: authFallback.expires,
enabled: true,
rateLimitResetTimes: {},
touchedForQuota: {},
},
];
this.cursor = 0;
this.currentAccountIndexByFamily.claude = 0;
this.currentAccountIndexByFamily.gemini = 0;
}
}
}
getAccountCount(): number {
return this.getEnabledAccounts().length;
}
getTotalAccountCount(): number {
return this.accounts.length;
}
getEnabledAccounts(): ManagedAccount[] {
return this.accounts.filter((account) => account.enabled !== false);
}
getAccountsSnapshot(): ManagedAccount[] {
return this.accounts.map((a) => ({ ...a, parts: { ...a.parts }, rateLimitResetTimes: { ...a.rateLimitResetTimes } }));
}
getCurrentAccountForFamily(family: ModelFamily): ManagedAccount | null {
const currentIndex = this.currentAccountIndexByFamily[family];
if (currentIndex >= 0 && currentIndex < this.accounts.length) {
const account = this.accounts[currentIndex] ?? null;
// Only return account if it's enabled - disabled accounts should not be selected
if (account && account.enabled !== false) {
return account;
}
}
return null;
}
markSwitched(account: ManagedAccount, reason: "rate-limit" | "initial" | "rotation", family: ModelFamily): void {
account.lastSwitchReason = reason;
this.currentAccountIndexByFamily[family] = account.index;
}
/**
* Check if we should show an account switch toast.
* Debounces repeated toasts for the same account.
*/
shouldShowAccountToast(accountIndex: number, debounceMs = 30000): boolean {
const now = nowMs();
if (accountIndex !== this.lastToastAccountIndex) {
return true;
}
return now - this.lastToastTime >= debounceMs;
}
markToastShown(accountIndex: number): void {
this.lastToastAccountIndex = accountIndex;
this.lastToastTime = nowMs();
}
getCurrentOrNextForFamily(
family: ModelFamily,
model?: string | null,
strategy: AccountSelectionStrategy = 'sticky',
headerStyle: HeaderStyle = 'antigravity',
pidOffsetEnabled: boolean = false,
softQuotaThresholdPercent: number = 100,
softQuotaCacheTtlMs: number = 10 * 60 * 1000,
): ManagedAccount | null {
const quotaKey = getQuotaKey(family, headerStyle, model);
if (strategy === 'round-robin') {
const next = this.getNextForFamily(family, model, headerStyle, softQuotaThresholdPercent, softQuotaCacheTtlMs);
if (next) {
this.markTouchedForQuota(next, quotaKey);
this.currentAccountIndexByFamily[family] = next.index;
}
return next;
}
if (strategy === 'hybrid') {
const healthTracker = getHealthTracker();
const tokenTracker = getTokenTracker();
const accountsWithMetrics: AccountWithMetrics[] = this.accounts
.filter(acc => acc.enabled !== false)
.map(acc => {
clearExpiredRateLimits(acc);
return {
index: acc.index,
lastUsed: acc.lastUsed,
healthScore: healthTracker.getScore(acc.index),
isRateLimited: isRateLimitedForFamily(acc, family, model) ||
isOverSoftQuotaThreshold(acc, family, softQuotaThresholdPercent, softQuotaCacheTtlMs, model),
isCoolingDown: this.isAccountCoolingDown(acc),
};
});
// Get current account index for stickiness
const currentIndex = this.currentAccountIndexByFamily[family] ?? null;
const selectedIndex = selectHybridAccount(accountsWithMetrics, tokenTracker, currentIndex);
if (selectedIndex !== null) {
const selected = this.accounts[selectedIndex];
if (selected) {
selected.lastUsed = nowMs();
this.markTouchedForQuota(selected, quotaKey);
this.currentAccountIndexByFamily[family] = selected.index;
return selected;
}
}
}
// Fallback: sticky selection (used when hybrid finds no candidates)
// PID-based offset for multi-session distribution (opt-in)
// Different sessions (PIDs) will prefer different starting accounts
if (pidOffsetEnabled && !this.sessionOffsetApplied[family] && this.accounts.length > 1) {
const pidOffset = process.pid % this.accounts.length;
const baseIndex = this.currentAccountIndexByFamily[family] ?? 0;
const newIndex = (baseIndex + pidOffset) % this.accounts.length;
debugLogToFile(`[Account] Applying PID offset: pid=${process.pid} offset=${pidOffset} family=${family} index=${baseIndex}->${newIndex}`);
this.currentAccountIndexByFamily[family] = newIndex;
this.sessionOffsetApplied[family] = true;
}
const current = this.getCurrentAccountForFamily(family);
if (current) {
clearExpiredRateLimits(current);
const isLimitedForRequestedStyle = isRateLimitedForHeaderStyle(current, family, headerStyle, model);
const isOverThreshold = isOverSoftQuotaThreshold(current, family, softQuotaThresholdPercent, softQuotaCacheTtlMs, model);
if (!isLimitedForRequestedStyle && !isOverThreshold && !this.isAccountCoolingDown(current)) {
this.markTouchedForQuota(current, quotaKey);
return current;
}
}
const next = this.getNextForFamily(family, model, headerStyle, softQuotaThresholdPercent, softQuotaCacheTtlMs);
if (next) {
this.markTouchedForQuota(next, quotaKey);
this.currentAccountIndexByFamily[family] = next.index;
}
return next;
}
getNextForFamily(family: ModelFamily, model?: string | null, headerStyle: HeaderStyle = "antigravity", softQuotaThresholdPercent: number = 100, softQuotaCacheTtlMs: number = 10 * 60 * 1000): ManagedAccount | null {
const available = this.accounts.filter((a) => {
clearExpiredRateLimits(a);
return a.enabled !== false &&
!isRateLimitedForHeaderStyle(a, family, headerStyle, model) &&
!isOverSoftQuotaThreshold(a, family, softQuotaThresholdPercent, softQuotaCacheTtlMs, model) &&
!this.isAccountCoolingDown(a);
});
if (available.length === 0) {
return null;
}
const account = available[this.cursor % available.length];
if (!account) {
return null;
}
this.cursor++;
// Note: lastUsed is now updated after successful request via markAccountUsed()
return account;
}
markRateLimited(
account: ManagedAccount,
retryAfterMs: number,
family: ModelFamily,
headerStyle: HeaderStyle = "antigravity",
model?: string | null
): void {
const key = getQuotaKey(family, headerStyle, model);
account.rateLimitResetTimes[key] = nowMs() + retryAfterMs;
}
/**
* Mark an account as used after a successful API request.
* This updates the lastUsed timestamp for freshness calculations.
* Should be called AFTER request completion, not during account selection.
*/
markAccountUsed(accountIndex: number): void {
const account = this.accounts.find(a => a.index === accountIndex);
if (account) {
account.lastUsed = nowMs();
}
}
markRateLimitedWithReason(
account: ManagedAccount,
family: ModelFamily,
headerStyle: HeaderStyle,
model: string | null | undefined,
reason: RateLimitReason,
retryAfterMs?: number | null,
failureTtlMs: number = 3600_000, // Default 1 hour TTL
): number {
const now = nowMs();
// TTL-based reset: if last failure was more than failureTtlMs ago, reset count
if (account.lastFailureTime !== undefined && (now - account.lastFailureTime) > failureTtlMs) {
account.consecutiveFailures = 0;
}
const failures = (account.consecutiveFailures ?? 0) + 1;
account.consecutiveFailures = failures;
account.lastFailureTime = now;
const backoffMs = calculateBackoffMs(reason, failures - 1, retryAfterMs);
const key = getQuotaKey(family, headerStyle, model);
account.rateLimitResetTimes[key] = now + backoffMs;
return backoffMs;
}
markRequestSuccess(account: ManagedAccount): void {
if (account.consecutiveFailures) {
account.consecutiveFailures = 0;
}
}
clearAllRateLimitsForFamily(family: ModelFamily, model?: string | null): void {
for (const account of this.accounts) {
if (family === "claude") {
delete account.rateLimitResetTimes.claude;
} else {
const antigravityKey = getQuotaKey(family, "antigravity", model);
const cliKey = getQuotaKey(family, "gemini-cli", model);
delete account.rateLimitResetTimes[antigravityKey];
delete account.rateLimitResetTimes[cliKey];
}
account.consecutiveFailures = 0;
}
}
shouldTryOptimisticReset(family: ModelFamily, model?: string | null): boolean {
const minWaitMs = this.getMinWaitTimeForFamily(family, model);
return minWaitMs > 0 && minWaitMs <= 2_000;
}
markAccountCoolingDown(account: ManagedAccount, cooldownMs: number, reason: CooldownReason): void {
account.coolingDownUntil = nowMs() + cooldownMs;
account.cooldownReason = reason;
}
isAccountCoolingDown(account: ManagedAccount): boolean {
if (account.coolingDownUntil === undefined) {
return false;
}
if (nowMs() >= account.coolingDownUntil) {
this.clearAccountCooldown(account);
return false;
}
return true;
}
clearAccountCooldown(account: ManagedAccount): void {
delete account.coolingDownUntil;
delete account.cooldownReason;
}
getAccountCooldownReason(account: ManagedAccount): CooldownReason | undefined {
return this.isAccountCoolingDown(account) ? account.cooldownReason : undefined;
}
markTouchedForQuota(account: ManagedAccount, quotaKey: string): void {
account.touchedForQuota[quotaKey] = nowMs();
}
isFreshForQuota(account: ManagedAccount, quotaKey: string): boolean {
const touchedAt = account.touchedForQuota[quotaKey];
if (!touchedAt) return true;
const resetTime = account.rateLimitResetTimes[quotaKey as QuotaKey];
if (resetTime && touchedAt < resetTime) return true;
return false;
}
getFreshAccountsForQuota(quotaKey: string, family: ModelFamily, model?: string | null): ManagedAccount[] {
return this.accounts.filter(acc => {
clearExpiredRateLimits(acc);
return acc.enabled !== false &&
this.isFreshForQuota(acc, quotaKey) &&
!isRateLimitedForFamily(acc, family, model) &&
!this.isAccountCoolingDown(acc);
});
}
isRateLimitedForHeaderStyle(
account: ManagedAccount,
family: ModelFamily,
headerStyle: HeaderStyle,
model?: string | null
): boolean {
return isRateLimitedForHeaderStyle(account, family, headerStyle, model);
}
getAvailableHeaderStyle(account: ManagedAccount, family: ModelFamily, model?: string | null): HeaderStyle | null {
clearExpiredRateLimits(account);
if (family === "claude") {
return isRateLimitedForHeaderStyle(account, family, "antigravity") ? null : "antigravity";
}
if (!isRateLimitedForHeaderStyle(account, family, "antigravity", model)) {
return "antigravity";
}
if (!isRateLimitedForHeaderStyle(account, family, "gemini-cli", model)) {
return "gemini-cli";
}
return null;
}
/**
* Check if any OTHER account has antigravity quota available for the given family/model.
*
* Used to determine whether to switch accounts vs fall back to gemini-cli:
* - If true: Switch to another account (preserve antigravity priority)
* - If false: All accounts exhausted antigravity, safe to fall back to gemini-cli
*
* @param currentAccountIndex - Index of the current account (will be excluded from check)
* @param family - Model family ("gemini" or "claude")
* @param model - Optional model name for model-specific rate limits
* @returns true if any other enabled, non-cooling-down account has antigravity available
*/
hasOtherAccountWithAntigravityAvailable(
currentAccountIndex: number,
family: ModelFamily,
model?: string | null
): boolean {
// Claude has no gemini-cli fallback - always return false
// (This method is only relevant for Gemini's dual quota pools)
if (family === "claude") {
return false;
}
return this.accounts.some(acc => {
// Skip current account
if (acc.index === currentAccountIndex) {
return false;
}
// Skip disabled accounts
if (acc.enabled === false) {
return false;
}
// Skip cooling down accounts
if (this.isAccountCoolingDown(acc)) {
return false;
}
// Clear expired rate limits before checking
clearExpiredRateLimits(acc);
// Check if antigravity is available for this account
return !isRateLimitedForHeaderStyle(acc, family, "antigravity", model);
});
}
setAccountEnabled(accountIndex: number, enabled: boolean): boolean {
const account = this.accounts[accountIndex];
if (!account) {
return false;
}
account.enabled = enabled;
if (!enabled) {
for (const family of Object.keys(this.currentAccountIndexByFamily) as ModelFamily[]) {
if (this.currentAccountIndexByFamily[family] === accountIndex) {
const next = this.accounts.find((a, i) => i !== accountIndex && a.enabled !== false);
this.currentAccountIndexByFamily[family] = next?.index ?? -1;
}
}
}
this.requestSaveToDisk();
return true;
}
markAccountVerificationRequired(accountIndex: number, reason?: string, verifyUrl?: string): boolean {
const account = this.accounts[accountIndex];
if (!account) {
return false;
}
account.verificationRequired = true;
account.verificationRequiredAt = nowMs();
account.verificationRequiredReason = reason?.trim() || undefined;
const normalizedVerifyUrl = verifyUrl?.trim();
if (normalizedVerifyUrl) {
account.verificationUrl = normalizedVerifyUrl;
}
if (account.enabled !== false) {
this.setAccountEnabled(accountIndex, false);
} else {
this.requestSaveToDisk();
}
return true;
}
clearAccountVerificationRequired(accountIndex: number, enableAccount = false): boolean {
const account = this.accounts[accountIndex];
if (!account) {
return false;
}
const wasVerificationRequired = account.verificationRequired === true;
const hadMetadata = (
account.verificationRequiredAt !== undefined ||
account.verificationRequiredReason !== undefined ||
account.verificationUrl !== undefined
);
account.verificationRequired = false;
account.verificationRequiredAt = undefined;
account.verificationRequiredReason = undefined;
account.verificationUrl = undefined;
if (enableAccount && wasVerificationRequired && account.enabled === false) {
this.setAccountEnabled(accountIndex, true);
} else if (wasVerificationRequired || hadMetadata) {
this.requestSaveToDisk();
}
return true;
}
removeAccountByIndex(accountIndex: number): boolean {
if (accountIndex < 0 || accountIndex >= this.accounts.length) {
return false;
}
const account = this.accounts[accountIndex];
if (!account) {
return false;
}
return this.removeAccount(account);
}
removeAccount(account: ManagedAccount): boolean {
const idx = this.accounts.indexOf(account);
if (idx < 0) {
return false;
}
this.accounts.splice(idx, 1);
this.accounts.forEach((acc, index) => {
acc.index = index;
});
if (this.accounts.length === 0) {
this.cursor = 0;
this.currentAccountIndexByFamily.claude = -1;
this.currentAccountIndexByFamily.gemini = -1;
return true;
}
if (this.cursor > idx) {
this.cursor -= 1;
}
this.cursor = this.cursor % this.accounts.length;
for (const family of ["claude", "gemini"] as ModelFamily[]) {
if (this.currentAccountIndexByFamily[family] > idx) {
this.currentAccountIndexByFamily[family] -= 1;
}
if (this.currentAccountIndexByFamily[family] >= this.accounts.length) {
this.currentAccountIndexByFamily[family] = -1;
}
}
return true;
}
updateFromAuth(account: ManagedAccount, auth: OAuthAuthDetails): void {
const parts = parseRefreshParts(auth.refresh);
// Preserve existing (disk-loaded) projectId/managedProjectId over runtime values.
// The account's existing values may have been manually configured by the user;
// the auth refresh string may contain an auto-detected fallback that should not
// overwrite those manual edits.
account.parts = {
...parts,
projectId: account.parts.projectId ?? parts.projectId,
managedProjectId: account.parts.managedProjectId ?? parts.managedProjectId,
};
account.access = auth.access;
account.expires = auth.expires;
}
toAuthDetails(account: ManagedAccount): OAuthAuthDetails {
return {
type: "oauth",
refresh: formatRefreshParts(account.parts),
access: account.access,
expires: account.expires,
};
}
getMinWaitTimeForFamily(
family: ModelFamily,
model?: string | null,
headerStyle?: HeaderStyle,
strict?: boolean,
): number {
const available = this.accounts.filter((a) => {
clearExpiredRateLimits(a);
return a.enabled !== false && (strict && headerStyle
? !isRateLimitedForHeaderStyle(a, family, headerStyle, model)
: !isRateLimitedForFamily(a, family, model));
});
if (available.length > 0) {
return 0;
}
const waitTimes: number[] = [];
for (const a of this.accounts) {
if (family === "claude") {
const t = a.rateLimitResetTimes.claude;
if (t !== undefined) waitTimes.push(Math.max(0, t - nowMs()));
} else if (strict && headerStyle) {
const key = getQuotaKey(family, headerStyle, model);
const t = a.rateLimitResetTimes[key];
if (t !== undefined) waitTimes.push(Math.max(0, t - nowMs()));
} else {
// For Gemini, account becomes available when EITHER pool expires for this model/family
const antigravityKey = getQuotaKey(family, "antigravity", model);
const cliKey = getQuotaKey(family, "gemini-cli", model);
const t1 = a.rateLimitResetTimes[antigravityKey];
const t2 = a.rateLimitResetTimes[cliKey];
const accountWait = Math.min(
t1 !== undefined ? Math.max(0, t1 - nowMs()) : Infinity,
t2 !== undefined ? Math.max(0, t2 - nowMs()) : Infinity
);
if (accountWait !== Infinity) waitTimes.push(accountWait);
}
}
return waitTimes.length > 0 ? Math.min(...waitTimes) : 0;
}
getAccounts(): ManagedAccount[] {
return [...this.accounts];
}
private buildStorageState(): AccountStorageV4 {
const claudeIndex = Math.max(0, this.currentAccountIndexByFamily.claude);
const geminiIndex = Math.max(0, this.currentAccountIndexByFamily.gemini);
return {
version: 4,