tuxevil-rotator 3.5.0 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/rotator.ts CHANGED
@@ -90,11 +90,14 @@ import {
90
90
  } from "./rate-limit-parser.js";
91
91
  import {
92
92
  getAccountIdentity,
93
+ getCredentialGeneration,
94
+ getCredentialGenerationFingerprint,
93
95
  getProviderCredentialDetails,
94
96
  } from "./account-identity.js";
95
97
 
96
98
  export {
97
99
  getAccountIdentity,
100
+ getCredentialGeneration,
98
101
  getProviderCredentialDetails,
99
102
  } from "./account-identity.js";
100
103
 
@@ -154,18 +157,6 @@ export function areAccountIdentitiesCompatible(
154
157
  return true;
155
158
  }
156
159
 
157
- export function getCredentialGeneration(
158
- account: AccountRuntime | AccountConfig,
159
- providerId: string,
160
- ): string {
161
- const config = "config" in account ? account.config : account;
162
- const norm = normalizeAccountConfig(config);
163
- const cred = norm.credentials?.find((c) => c.provider === providerId);
164
- const details = getProviderCredentialDetails(norm, cred ?? { provider: providerId });
165
- if (!cred && !details.secret) return "none";
166
- return `${providerId}:${details.projectId}:${details.providerAccountId}:${details.secret}`;
167
- }
168
-
169
160
  const rotatorLogger = logger.child("rotator");
170
161
 
171
162
  function currentUtcDay(now = Date.now()): string {
@@ -359,9 +350,27 @@ export class AccountRotator {
359
350
  this.config = applyConfigDefaults(config);
360
351
  this.initAccounts();
361
352
  this.loadState();
353
+ this.reconcileDynamicCatalog();
362
354
  this.startQuotaPolling();
363
355
  }
364
356
 
357
+ private reconcileDynamicCatalog(): void {
358
+ dynamicCatalog.retainAccounts(
359
+ this.accounts
360
+ .filter(
361
+ (account) =>
362
+ !account.disabled &&
363
+ !account.flagged &&
364
+ !account.invalidProviders?.[DEFAULT_PROVIDER] &&
365
+ hasCredential(account.config, DEFAULT_PROVIDER),
366
+ )
367
+ .map((account) => ({
368
+ id: getAccountIdentity(account),
369
+ generation: getCredentialGeneration(account, DEFAULT_PROVIDER),
370
+ })),
371
+ );
372
+ }
373
+
365
374
  private initAccounts(): void {
366
375
  this.accounts = this.config.accounts.map((config) => ({
367
376
  config,
@@ -485,6 +494,17 @@ export class AccountRotator {
485
494
  }
486
495
 
487
496
  try {
497
+ dynamicCatalog.restoreDiscoveredQuotaPools(state.dynamicModelQuotaPools);
498
+ dynamicCatalog.restorePersistedModelOwnership(
499
+ state.dynamicModelOwnership,
500
+ this.accounts
501
+ .filter((account) => hasCredential(account.config, DEFAULT_PROVIDER))
502
+ .map((account) => ({
503
+ id: getAccountIdentity(account),
504
+ credentialGenerationFingerprint:
505
+ getCredentialGenerationFingerprint(account, DEFAULT_PROVIDER),
506
+ })),
507
+ );
488
508
  // Load per-model account assignments
489
509
  if (state.modelAccounts) {
490
510
  for (const [model, idx] of Object.entries(state.modelAccounts)) {
@@ -541,8 +561,9 @@ export class AccountRotator {
541
561
  saved.allowFreshWindowStartsOverride ?? false;
542
562
  }
543
563
  }
544
- // Cap generic stale cooldowns to 30 min max from now. Antigravity's
545
- // explicit Claude/Gemini reset deadlines must survive restarts intact.
564
+ this.migratePersistedQuotaStateKeys();
565
+ // Cap explicit generic/non-Google cooldowns to 30 min. An unresolved key
566
+ // may be a dynamic Google model, so preserve it until catalog hydration.
546
567
  const maxCooldown = 30 * 60 * 1000;
547
568
  const now = Date.now();
548
569
  for (const account of this.accounts) {
@@ -552,6 +573,8 @@ export class AccountRotator {
552
573
  if (
553
574
  model !== "claude" &&
554
575
  model !== "gemini" &&
576
+ (model === "__default__" ||
577
+ getProviderIdForPoolKey(model) !== DEFAULT_PROVIDER) &&
555
578
  cooldown > now + maxCooldown
556
579
  ) {
557
580
  account.cooldownsByModel[model] = now + maxCooldown;
@@ -565,6 +588,41 @@ export class AccountRotator {
565
588
  this.loadTokenUsage();
566
589
  }
567
590
 
591
+ private migratePersistedQuotaStateKeys(): void {
592
+ const foldDeadlines = (deadlines: Record<string, number>) => {
593
+ const folded: Record<string, number> = {};
594
+ for (const [key, deadline] of Object.entries(deadlines)) {
595
+ const canonical = this.resolveQuotaStateKey(key);
596
+ folded[canonical] = Math.max(folded[canonical] ?? 0, deadline);
597
+ }
598
+ return folded;
599
+ };
600
+
601
+ this.modelBreakers = foldDeadlines(this.modelBreakers);
602
+ const projectBreakers: Record<string, number> = {};
603
+ for (const [key, deadline] of Object.entries(this.projectModelBreakers)) {
604
+ const separator = key.lastIndexOf("::");
605
+ const canonical = separator < 0
606
+ ? key
607
+ : projectModelKey(
608
+ key.slice(0, separator),
609
+ this.resolveQuotaStateKey(key.slice(separator + 2)),
610
+ );
611
+ projectBreakers[canonical] = Math.max(
612
+ projectBreakers[canonical] ?? 0,
613
+ deadline,
614
+ );
615
+ }
616
+ this.projectModelBreakers = projectBreakers;
617
+ this.provider429Events = this.provider429Events.map((event) => ({
618
+ ...event,
619
+ modelKey: this.resolveQuotaStateKey(event.modelKey),
620
+ }));
621
+ for (const account of this.accounts) {
622
+ account.cooldownsByModel = foldDeadlines(account.cooldownsByModel);
623
+ }
624
+ }
625
+
568
626
  private loadTokenUsage(): void {
569
627
  try {
570
628
  const parsed = getCachedTokenUsage() as any;
@@ -621,6 +679,22 @@ export class AccountRotator {
621
679
  }
622
680
 
623
681
  const state: PersistedState = {
682
+ dynamicModelQuotaPools: dynamicCatalog.getDiscoveredQuotaPools(),
683
+ dynamicModelOwnership: dynamicCatalog.getPersistedModelOwnership(
684
+ this.accounts
685
+ .filter(
686
+ (account) =>
687
+ !account.disabled &&
688
+ !account.flagged &&
689
+ !account.invalidProviders?.[DEFAULT_PROVIDER] &&
690
+ hasCredential(account.config, DEFAULT_PROVIDER),
691
+ )
692
+ .map((account) => ({
693
+ id: getAccountIdentity(account),
694
+ credentialGenerationFingerprint:
695
+ getCredentialGenerationFingerprint(account, DEFAULT_PROVIDER),
696
+ })),
697
+ ),
624
698
  modelAccounts,
625
699
  modelRequestCounts,
626
700
  modelStickyAccounts,
@@ -805,6 +879,9 @@ export class AccountRotator {
805
879
  try {
806
880
  await this.ensureValidTokenForProvider(account, pid);
807
881
  await getProviderAdapter(pid).fetchQuota(account, quotaCtx);
882
+ if (pid === DEFAULT_PROVIDER) {
883
+ this.migratePersistedQuotaStateKeys();
884
+ }
808
885
  quotaPublished = true;
809
886
  } catch {
810
887
  // One invalid provider credential must not suppress sibling pools.
@@ -860,11 +937,7 @@ export class AccountRotator {
860
937
  void this.refreshOllamaCatalogOnce().catch(() => {});
861
938
 
862
939
  const available = this.accounts.filter((a) => !a.disabled && !a.flagged);
863
- dynamicCatalog.retainAccounts(
864
- available
865
- .filter((account) => hasCredential(account.config, DEFAULT_PROVIDER))
866
- .map(getAccountIdentity),
867
- );
940
+ this.reconcileDynamicCatalog();
868
941
  let quotaPublished = false;
869
942
  for (const account of available) {
870
943
  if (await this.pollAccountQuota(account)) {
@@ -1346,9 +1419,10 @@ export class AccountRotator {
1346
1419
  ignoreConcurrency = false,
1347
1420
  ): string | null {
1348
1421
  const projectId = this.getProjectIdForModel(account, modelKey);
1349
- if (this.isModelBreakerActive(modelKey, now))
1422
+ const quotaStateKey = this.resolveQuotaStateKey(modelKey);
1423
+ if (this.isModelBreakerActive(quotaStateKey, now))
1350
1424
  return "model circuit breaker active";
1351
- if (this.isProjectModelBreakerActive(projectId, modelKey, now))
1425
+ if (this.isProjectModelBreakerActive(projectId, quotaStateKey, now))
1352
1426
  return "project circuit breaker active";
1353
1427
  if (
1354
1428
  projectId &&
@@ -1369,23 +1443,12 @@ export class AccountRotator {
1369
1443
  ): AccountStatus["status"] {
1370
1444
  const defaultCooldownActive =
1371
1445
  (account.cooldownsByModel.__default__ ?? 0) > now;
1372
- const knownPools = new Set(account.quota.map((quota) => quota.modelKey));
1373
- for (const [modelKey, cooldownUntil] of Object.entries(
1374
- account.cooldownsByModel,
1375
- )) {
1376
- if (modelKey !== "__default__" && cooldownUntil > now) {
1377
- knownPools.add(modelKey);
1378
- }
1379
- }
1380
- const allKnownPoolsCooling =
1381
- knownPools.size > 0 &&
1382
- [...knownPools].every(
1383
- (modelKey) => (account.cooldownsByModel[modelKey] ?? 0) > now,
1384
- );
1385
1446
  if (account.flagged) return "flagged";
1386
1447
  if (account.disabled) return "disabled";
1387
1448
  if (account.consecutiveErrors > 0 && !account.disabled) return "error";
1388
- if (defaultCooldownActive || allKnownPoolsCooling) return "cooldown";
1449
+ // Pool cooldowns stay visible in cooldownsByModel but must not make the
1450
+ // whole account unavailable for unrelated quota pools.
1451
+ if (defaultCooldownActive) return "cooldown";
1389
1452
  if (this.isDailySafetyStopped(account, now)) return "exhausted";
1390
1453
  if (activeForModels.length > 0) return "active";
1391
1454
  return "ready";
@@ -1427,7 +1490,8 @@ export class AccountRotator {
1427
1490
  const defaultCooldown = account.cooldownsByModel["__default__"] ?? 0;
1428
1491
  if (defaultCooldown > now)
1429
1492
  return { reason: "cooldown", detail: "default cooldown active" };
1430
- const modelCooldown = account.cooldownsByModel[modelKey] ?? 0;
1493
+ const modelCooldown =
1494
+ account.cooldownsByModel[this.resolveQuotaStateKey(modelKey)] ?? 0;
1431
1495
  if (modelCooldown > now)
1432
1496
  return { reason: "cooldown", detail: "model cooldown active" };
1433
1497
  // Ollama Cloud (pool key "session") imposes no per-account
@@ -2426,7 +2490,7 @@ export class AccountRotator {
2426
2490
  ): void {
2427
2491
  const now = new Date();
2428
2492
  const minuteKey = now.toISOString().slice(0, 16); // "2026-04-28T12:05"
2429
- const modelKey = model ? resolveDisplayModelKey(model) : "unknown";
2493
+ const modelKey = model ? this.resolveObservedModelKey(model) : "unknown";
2430
2494
 
2431
2495
  // Upsert minute bucket
2432
2496
  let bucket = this.tokenBuckets.minutes.find((b) => b.period === minuteKey);
@@ -2464,7 +2528,7 @@ export class AccountRotator {
2464
2528
  ttfbMs: number,
2465
2529
  totalMs: number,
2466
2530
  ): void {
2467
- const modelKey = model ? resolveDisplayModelKey(model) : "unknown";
2531
+ const modelKey = model ? this.resolveObservedModelKey(model) : "unknown";
2468
2532
  let records = this.latencyRecords.get(modelKey);
2469
2533
  if (!records) {
2470
2534
  records = [];
@@ -2916,6 +2980,21 @@ export class AccountRotator {
2916
2980
  account.cooldownsByModel[modelKey] = now + cooldownMs;
2917
2981
  account.quotaExhaustedAt = now;
2918
2982
 
2983
+ // Keep the dashboard aligned with provider truth. The quota endpoint can
2984
+ // report an untouched 100% pool while generation has already exhausted it.
2985
+ if (modelKey !== "__default__" && cooldownMs > 0) {
2986
+ const quota = account.quota.find(
2987
+ (candidate) =>
2988
+ candidate.modelKey === modelKey &&
2989
+ candidate.providerId === DEFAULT_PROVIDER,
2990
+ );
2991
+ if (quota) {
2992
+ quota.percentRemaining = 0;
2993
+ quota.resetTime = new Date(now + cooldownMs).toISOString();
2994
+ quota.timerType = cooldownMs < 6 * 60 * 60 * 1000 ? "5h" : "7d";
2995
+ }
2996
+ }
2997
+
2919
2998
  const errorDetail = errorText ? ` | ${errorText}` : "";
2920
2999
  this.log(
2921
3000
  `${account.config.label || account.config.email} [${modelKey}]: EXHAUSTED, cooldown ${Math.ceil(cooldownMs / 1000)}s${errorDetail}`,
@@ -3035,6 +3114,7 @@ export class AccountRotator {
3035
3114
  account.consecutiveErrors++;
3036
3115
  if (account.consecutiveErrors >= 5) {
3037
3116
  account.disabled = true;
3117
+ this.reconcileDynamicCatalog();
3038
3118
  this.log(
3039
3119
  `${account.config.email}: DISABLED after ${account.consecutiveErrors} consecutive errors`,
3040
3120
  "error",
@@ -3058,6 +3138,7 @@ export class AccountRotator {
3058
3138
  account.consecutiveErrors = 0;
3059
3139
  account.lastError = null;
3060
3140
  account.cooldownsByModel = {};
3141
+ this.reconcileDynamicCatalog();
3061
3142
  await this.saveState();
3062
3143
  this.log(`${email}: re-enabled`);
3063
3144
  this.requestWaiterDrain();
@@ -3069,6 +3150,7 @@ export class AccountRotator {
3069
3150
  if (!account) return false;
3070
3151
  account.disabled = true;
3071
3152
  account.lastError = "Disabled by operator";
3153
+ this.reconcileDynamicCatalog();
3072
3154
  await this.saveState();
3073
3155
  this.log(`${email}: disabled by operator`, "warn");
3074
3156
  return true;
@@ -3082,6 +3164,7 @@ export class AccountRotator {
3082
3164
  if (!account) return false;
3083
3165
  account.flagged = true;
3084
3166
  account.lastError = reason;
3167
+ this.reconcileDynamicCatalog();
3085
3168
  await this.saveState();
3086
3169
  this.log(`${email}: quarantined by operator`, "warn");
3087
3170
  return true;
@@ -3094,6 +3177,7 @@ export class AccountRotator {
3094
3177
  account.flagged = false;
3095
3178
  account.consecutiveErrors = 0;
3096
3179
  account.lastError = null;
3180
+ this.reconcileDynamicCatalog();
3097
3181
  await this.saveState();
3098
3182
  this.log(`${email}: restored by operator`, "warn");
3099
3183
  this.requestWaiterDrain();
@@ -3111,6 +3195,7 @@ export class AccountRotator {
3111
3195
  (entry) => entry.email === email,
3112
3196
  );
3113
3197
  if (existing) Object.assign(existing, patch);
3198
+ this.reconcileDynamicCatalog();
3114
3199
  await saveAccountsConfig(this.config);
3115
3200
  await this.saveState();
3116
3201
  this.log(`${email}: metadata updated by operator`, "warn");
@@ -3383,6 +3468,7 @@ export class AccountRotator {
3383
3468
  account.providerTokens ??= {};
3384
3469
  account.providerTokens[providerId] = { accessToken: null, tokenExpires: 0 };
3385
3470
  account.lastError = reason;
3471
+ if (providerId === DEFAULT_PROVIDER) this.reconcileDynamicCatalog();
3386
3472
  this.log(`${account.config.email}: ${providerId} credential requires re-authentication`, "warn");
3387
3473
  this.scheduleStateSave();
3388
3474
  }
@@ -3417,7 +3503,8 @@ export class AccountRotator {
3417
3503
  const providerId = getProviderIdForPoolKey(modelKey);
3418
3504
  if (account.invalidProviders?.[providerId]) return false;
3419
3505
  if ((account.providerCooldowns?.[providerId] ?? 0) > now) return false;
3420
- const modelCooldown = account.cooldownsByModel[modelKey] ?? 0;
3506
+ const modelCooldown =
3507
+ account.cooldownsByModel[this.resolveQuotaStateKey(modelKey)] ?? 0;
3421
3508
  if (modelCooldown > now) return false;
3422
3509
  // Ollama Cloud (pool key "session") imposes no per-account concurrency
3423
3510
  // limit. Antigravity keeps it so long streams don't pile up on a
@@ -3442,6 +3529,7 @@ export class AccountRotator {
3442
3529
  account.lastError = reason;
3443
3530
  account.inFlightRequests = 0;
3444
3531
  account.inFlightByModel = {};
3532
+ this.reconcileDynamicCatalog();
3445
3533
  this.requestWaiterDrain();
3446
3534
  this.log(`${account.config.email}: FLAGGED - ${reason}`, "error");
3447
3535
  const triggerProtectivePause = options.triggerProtectivePause ?? true;
@@ -3495,10 +3583,11 @@ export class AccountRotator {
3495
3583
  account: AccountRuntime,
3496
3584
  modelKey: string,
3497
3585
  ): boolean {
3586
+ const accountId = getAccountIdentity(account);
3498
3587
  if (
3499
3588
  !isStaticAntigravityModel(modelKey) &&
3500
- dynamicCatalog.wasDiscovered(modelKey) &&
3501
- !dynamicCatalog.hasModelForAccount(getAccountIdentity(account), modelKey)
3589
+ dynamicCatalog.hasOwnershipForModel(modelKey) &&
3590
+ !dynamicCatalog.hasModelForAccount(accountId, modelKey)
3502
3591
  ) {
3503
3592
  return false;
3504
3593
  }
@@ -3513,15 +3602,24 @@ export class AccountRotator {
3513
3602
  return this.resolveRequestPoolKey(model);
3514
3603
  }
3515
3604
 
3605
+ /** Preserve the exact identity of models learned from the runtime catalog. */
3606
+ resolveObservedModelKey(model: string): string {
3607
+ return dynamicCatalog.getObservedModelId(model) ?? resolveDisplayModelKey(model);
3608
+ }
3609
+
3516
3610
  private resolveRequestPoolKey(model: string): string {
3517
3611
  return this.resolvePoolKeyForModel(model) ?? "__default__";
3518
3612
  }
3519
3613
 
3520
3614
  private resolvePoolKeyForModel(model: string): string | null {
3615
+ const normalizedModel = model.trim().toLowerCase();
3521
3616
  if (
3522
3617
  !isStaticAntigravityModel(model) &&
3523
3618
  dynamicCatalog.wasDiscovered(model)
3524
- ) return model.toLowerCase();
3619
+ ) return normalizedModel;
3620
+ if (this.hasRelevantQuotaStateForModel(normalizedModel, Date.now())) {
3621
+ return normalizedModel;
3622
+ }
3525
3623
  const context = {
3526
3624
  ollamaModels: this.ollamaModels,
3527
3625
  codexModels: this.codexModels,
@@ -3533,6 +3631,30 @@ export class AccountRotator {
3533
3631
  return resolveQuotaModelKey(model) ?? null;
3534
3632
  }
3535
3633
 
3634
+ private hasRelevantQuotaStateForModel(modelKey: string, now: number): boolean {
3635
+ if ((this.modelBreakers[modelKey] ?? 0) > now) return true;
3636
+ if (
3637
+ this.accounts.some(
3638
+ (account) => (account.cooldownsByModel[modelKey] ?? 0) > now,
3639
+ )
3640
+ ) return true;
3641
+ if (
3642
+ Object.entries(this.projectModelBreakers).some(([key, deadline]) =>
3643
+ key.endsWith(`::${modelKey}`) && deadline > now
3644
+ )
3645
+ ) return true;
3646
+ const windowMs = this.config.projectCircuitBreakerWindowMs ?? 10 * 60 * 1000;
3647
+ return this.provider429Events.some(
3648
+ (event) => event.modelKey === modelKey && now - event.ts <= windowMs,
3649
+ );
3650
+ }
3651
+
3652
+ private resolveQuotaStateKey(modelKey: string): string {
3653
+ return dynamicCatalog.resolveQuotaPool(modelKey) ??
3654
+ resolveQuotaModelKey(modelKey) ??
3655
+ modelKey;
3656
+ }
3657
+
3536
3658
  /** Map a candidate key (model name or pool key) to the account's pool key. */
3537
3659
  private resolvePoolKey(account: AccountRuntime, key: string): string {
3538
3660
  if (
@@ -3549,6 +3671,8 @@ export class AccountRotator {
3549
3671
  private resolveAccountPoolKey(account: AccountRuntime, key: string): string {
3550
3672
  const kickstartPool = QUOTA_POOL_FOR_KICKSTART_MODEL[key];
3551
3673
  if (kickstartPool) return this.resolvePoolKey(account, kickstartPool);
3674
+ const dynamicPool = dynamicCatalog.resolveQuotaPool(key);
3675
+ if (dynamicPool) return this.resolvePoolKey(account, dynamicPool);
3552
3676
  if (
3553
3677
  Object.values(QUOTA_POOL_FOR_KICKSTART_MODEL).includes(key) ||
3554
3678
  account.quota.some((quota) => quota.modelKey === key) ||
@@ -3587,8 +3711,9 @@ export class AccountRotator {
3587
3711
  const modelKey = model
3588
3712
  ? (this.resolvePoolKeyForModel(model) ?? resolveQuotaModelKey(model) ?? "__default__")
3589
3713
  : "__default__";
3714
+ const quotaStateKey = this.resolveQuotaStateKey(modelKey);
3590
3715
  const dailyResetAt = nextUtcDayStartMs(now);
3591
- const modelBreaker = this.modelBreakers[modelKey] ?? 0;
3716
+ const modelBreaker = this.modelBreakers[quotaStateKey] ?? 0;
3592
3717
  if (modelBreaker > now) retryTimes.push(modelBreaker);
3593
3718
  for (const account of this.accounts) {
3594
3719
  if (account.disabled || account.flagged) continue;
@@ -3604,14 +3729,14 @@ export class AccountRotator {
3604
3729
  if (this.isDailySafetyStopped(account, now))
3605
3730
  retryTimes.push(dailyResetAt);
3606
3731
  const cooldown = Math.max(
3607
- account.cooldownsByModel[modelKey] ?? 0,
3732
+ account.cooldownsByModel[quotaStateKey] ?? 0,
3608
3733
  account.cooldownsByModel.__default__ ?? 0,
3609
3734
  );
3610
3735
  if (cooldown > now) retryTimes.push(cooldown);
3611
3736
  const projectId = this.getProjectIdForModel(account, modelKey);
3612
3737
  if (projectId) {
3613
3738
  const projectBreaker =
3614
- this.projectModelBreakers[projectModelKey(projectId, modelKey)] ?? 0;
3739
+ this.projectModelBreakers[projectModelKey(projectId, quotaStateKey)] ?? 0;
3615
3740
  if (projectBreaker > now) retryTimes.push(projectBreaker);
3616
3741
  }
3617
3742
  if ((this.config.routingPolicy || "timer-first") === "hybrid") {
@@ -3882,6 +4007,7 @@ export class AccountRotator {
3882
4007
  const mergedAccounts = normalized.accounts.map(matchAndReuseAccount);
3883
4008
  this.config = { ...normalized, accounts: mergedAccounts.map((account) => account.config) };
3884
4009
  this.accounts = mergedAccounts;
4010
+ this.reconcileDynamicCatalog();
3885
4011
  this.requestWaiterDrain();
3886
4012
  await saveAccountsConfig(this.config);
3887
4013
  await this.saveState();
@@ -3993,6 +4119,7 @@ export class AccountRotator {
3993
4119
  this.log(`${accountConfig.email}: account added via hosted login`);
3994
4120
  }
3995
4121
 
4122
+ this.reconcileDynamicCatalog();
3996
4123
  await saveAccountsConfig(this.config);
3997
4124
  await this.saveState();
3998
4125
  this.requestWaiterDrain();
@@ -4013,6 +4140,7 @@ export class AccountRotator {
4013
4140
  this.accounts.splice(idx, 1);
4014
4141
  const configIdx = this.config.accounts.findIndex((a) => a.email === email);
4015
4142
  if (configIdx >= 0) this.config.accounts.splice(configIdx, 1);
4143
+ this.reconcileDynamicCatalog();
4016
4144
 
4017
4145
  // Fix up modelState indices that may now be stale after the splice
4018
4146
  for (const [, mState] of this.modelState.entries()) {
@@ -4342,6 +4470,19 @@ export class AccountRotator {
4342
4470
  const upstreamModel = target.upstreamModel;
4343
4471
  const poolKey = target.poolKey;
4344
4472
 
4473
+ const activeCooldown = Math.max(
4474
+ account.cooldownsByModel[poolKey] ?? 0,
4475
+ account.cooldownsByModel.__default__ ?? 0,
4476
+ );
4477
+ if (activeCooldown > Date.now()) {
4478
+ return {
4479
+ ok: false,
4480
+ status: 429,
4481
+ upstreamModel,
4482
+ error: `quota cooldown active for ${Math.ceil((activeCooldown - Date.now()) / 1000)}s`,
4483
+ };
4484
+ }
4485
+
4345
4486
  const controller = new AbortController();
4346
4487
  const timeout = setTimeout(() => controller.abort(), 15_000);
4347
4488
 
@@ -1221,13 +1221,15 @@ var MODEL_PRICING_CLIENT = {
1221
1221
  function getModelPricingClient(m) {
1222
1222
  if (MODEL_PRICING_CLIENT[m]) return MODEL_PRICING_CLIENT[m];
1223
1223
  var lower = (m || "").toLowerCase();
1224
- if (lower.indexOf("opus") !== -1) return MODEL_PRICING_CLIENT["claude-opus-4-6-thinking"];
1225
- if (lower.indexOf("sonnet") !== -1) return MODEL_PRICING_CLIENT["claude-sonnet-4-6"];
1226
- if (lower.indexOf("3.8-flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3.8-flash-high"];
1227
- if (lower.indexOf("3.7-flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3.7-flash-tiered"];
1228
- if (lower.indexOf("3.6-flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3.6-flash-high"];
1229
- if (lower.indexOf("flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3-flash"];
1230
- if (lower.indexOf("pro") !== -1) return MODEL_PRICING_CLIENT["gemini-3.1-pro"];
1224
+ if (lower.indexOf("claude") !== -1 && lower.indexOf("opus") !== -1) return MODEL_PRICING_CLIENT["claude-opus-4-6-thinking"];
1225
+ if (lower.indexOf("claude") !== -1 && lower.indexOf("sonnet") !== -1) return MODEL_PRICING_CLIENT["claude-sonnet-4-6"];
1226
+ if (lower.indexOf("gemini") !== -1) {
1227
+ if (lower.indexOf("3.8") !== -1 && lower.indexOf("flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3.8-flash-high"];
1228
+ if (lower.indexOf("3.7") !== -1 && lower.indexOf("flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3.7-flash-tiered"];
1229
+ if (lower.indexOf("3.6") !== -1 && lower.indexOf("flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3.6-flash-high"];
1230
+ if (lower.indexOf("flash") !== -1) return MODEL_PRICING_CLIENT["gemini-3-flash"];
1231
+ if (lower.indexOf("pro") !== -1) return MODEL_PRICING_CLIENT["gemini-3.1-pro"];
1232
+ }
1231
1233
  if (lower.indexOf("gpt-oss") !== -1) return MODEL_PRICING_CLIENT["gpt-oss-120b-medium"];
1232
1234
  return null;
1233
1235
  }