tuxevil-rotator 4.2.2 → 4.4.0

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +48 -1
  2. package/README.md +11 -4
  3. package/package.json +2 -1
  4. package/plugins/opencode-discovery/capabilities.d.mts +7 -0
  5. package/plugins/opencode-discovery/capabilities.mjs +54 -0
  6. package/plugins/opencode-discovery/index.d.mts +2 -1
  7. package/plugins/opencode-discovery/index.mjs +6 -1
  8. package/plugins/opencode-discovery/package.json +3 -1
  9. package/src/access-evidence.ts +153 -0
  10. package/src/access-probe.ts +85 -0
  11. package/src/admin-auth.ts +4 -1
  12. package/src/audio-transcription.ts +32 -6
  13. package/src/auto-access-shadow.ts +11 -0
  14. package/src/auto-model-access.ts +185 -0
  15. package/src/auto-output-health.ts +12 -0
  16. package/src/auto-routing-evaluation.ts +80 -0
  17. package/src/auto-routing-quality.ts +308 -0
  18. package/src/auto-routing.ts +403 -26
  19. package/src/cli.ts +3 -3
  20. package/src/compat/model-specs.ts +4 -0
  21. package/src/compat.ts +253 -37
  22. package/src/compression/index.ts +6 -6
  23. package/src/compression/lite.ts +3 -9
  24. package/src/compression/rtk/index.ts +0 -3
  25. package/src/config-defaults.ts +7 -0
  26. package/src/dashboard-app.ts +1 -0
  27. package/src/dashboard.ts +5 -0
  28. package/src/db-store.ts +10 -0
  29. package/src/index.ts +2 -0
  30. package/src/install-agent.ts +211 -129
  31. package/src/install-opencode.ts +80 -18
  32. package/src/model-access-api.ts +74 -0
  33. package/src/model-access.ts +56 -0
  34. package/src/ollama-output-gate.ts +18 -0
  35. package/src/onboarding.ts +1 -1
  36. package/src/provider-catalog-evidence.ts +26 -0
  37. package/src/providers/google-antigravity/catalog.ts +14 -6
  38. package/src/providers/google-antigravity/dynamic-catalog.ts +87 -3
  39. package/src/providers/google-antigravity/forward.ts +4 -2
  40. package/src/providers/google-antigravity/quota.ts +7 -0
  41. package/src/providers/ollama/context-window.ts +13 -0
  42. package/src/providers/ollama/forward.ts +10 -0
  43. package/src/providers/ollama/quota.ts +4 -0
  44. package/src/providers/ollama/translators.ts +19 -2
  45. package/src/providers/openai-codex/catalog.ts +157 -18
  46. package/src/providers/openai-codex/compat.ts +5 -1
  47. package/src/providers/openai-codex/credentials.ts +22 -6
  48. package/src/providers/openai-codex/index.ts +9 -1
  49. package/src/providers/openai-codex/quota.ts +19 -6
  50. package/src/providers/opencode-zen/forward.ts +1 -1
  51. package/src/providers/opencode-zen/quota.ts +2 -0
  52. package/src/proxy.ts +113 -5
  53. package/src/quota-observation.ts +26 -0
  54. package/src/quota-pools.ts +322 -0
  55. package/src/rotator.ts +167 -40
  56. package/src/settings-repository.ts +1 -0
  57. package/src/types.ts +106 -29
  58. package/src/usage-limits-auth.ts +24 -0
  59. package/src/usage-limits.ts +148 -0
  60. package/src/validators.ts +26 -0
  61. package/src/web/app.tsx +4 -0
  62. package/src/web/components/icons.tsx +15 -0
  63. package/src/web/components/overlays.tsx +4 -0
  64. package/src/web/components/ui.tsx +9 -9
  65. package/src/web/lib/audio-recorder.ts +159 -0
  66. package/src/web/lib/colors.ts +4 -0
  67. package/src/web/lib/playground-storage.ts +261 -0
  68. package/src/web/lib/playground.ts +483 -0
  69. package/src/web/lib/pool-navigation.ts +4 -0
  70. package/src/web/lib/pools.ts +2 -243
  71. package/src/web/nav.ts +1 -0
  72. package/src/web/pages/AccountDetail.tsx +2 -1
  73. package/src/web/pages/ModelAccess.tsx +88 -0
  74. package/src/web/pages/Models.tsx +2 -0
  75. package/src/web/pages/Overview.tsx +26 -9
  76. package/src/web/pages/Playground.tsx +1692 -0
  77. package/src/web/pages/Transcription.tsx +562 -0
  78. package/src/web/state/api.ts +6 -0
  79. package/src/web/state/playground-client.ts +136 -0
  80. package/src/web/state/playground.ts +69 -0
  81. package/src/web/state/router.ts +2 -2
  82. package/src/web/state/transcription-client.ts +93 -0
  83. package/src/web/styles/app.css +501 -0
  84. package/tools/telemetry-receiver/receiver.js +59 -46
package/src/rotator.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { summarizeAutoAccess } from "./auto-access-shadow.js";
2
+ import { evaluateAutoModelAccess, type AutoAccountScope } from "./auto-model-access.js";
3
+ import { primeOllamaContextWindows } from "./providers/ollama/context-window.js";
1
4
  import {
2
5
  type AccountConfig,
3
6
  type AccountRuntime,
@@ -207,6 +210,7 @@ interface AccountRequestWaiter {
207
210
  queueKey: string;
208
211
  model?: string;
209
212
  providerId?: string;
213
+ autoScope?: AutoAccountScope;
210
214
  signal?: AbortSignal;
211
215
  timer: ReturnType<typeof setTimeout>;
212
216
  onAbort?: () => void;
@@ -321,6 +325,41 @@ export class AccountRotator {
321
325
  * Exposes only aggregate routing health to the optional Jev selector.
322
326
  * Account identities and credentials stay inside the rotator.
323
327
  */
328
+ getAutoRoutingEvidenceStatus(model: string, providerId: string) {
329
+ const key=this.resolvePoolKeyForModel(model,providerId);
330
+ const now=Date.now(),policy=this.config.routingPolicy||"timer-first";
331
+ const accounts=key?this.accounts.filter(a=>!this.getRoutingRejectionForModel(a,key,now,policy)).map(a=>a.config):[];
332
+ return summarizeAutoAccess(accounts,providerId,model);
333
+ }
334
+
335
+ /**
336
+ * Applies account/model access policy only to operationally routable accounts.
337
+ * Keep this separate from candidate health so dashboards continue to report
338
+ * provider availability independently of plan eligibility.
339
+ */
340
+ getAutoRoutingAccessStatus(model: string, providerId: string): {
341
+ eligible: boolean;
342
+ reason?: string;
343
+ } {
344
+ const poolKey = this.resolvePoolKeyForModel(model, providerId);
345
+ if (!poolKey) return { eligible: true };
346
+ const now = Date.now();
347
+ const policy = this.config.routingPolicy || "timer-first";
348
+ let operationalAccounts = 0;
349
+ let firstReason: string | undefined;
350
+ for (const account of this.accounts) {
351
+ if (this.getRoutingRejectionForModel(account, poolKey, now, policy)) continue;
352
+ operationalAccounts++;
353
+ const access = evaluateAutoModelAccess(account.config, providerId, model);
354
+ if (access.eligible) return { eligible: true };
355
+ firstReason ??= access.reason;
356
+ }
357
+ // Availability is reported by getAutoRoutingCandidateStatus. If no
358
+ // account is operational, this method has no access denial to add.
359
+ if (operationalAccounts === 0) return { eligible: true };
360
+ return { eligible: false, ...(firstReason ? { reason: firstReason } : {}) };
361
+ }
362
+
324
363
  getAutoRoutingCandidateStatus(
325
364
  model: string,
326
365
  providerId: string,
@@ -340,7 +379,12 @@ export class AccountRotator {
340
379
  let available = false;
341
380
  let firstReason: string | undefined;
342
381
  for (const account of this.accounts) {
343
- const rejection = this.getRoutingRejectionForModel(account, poolKey, now, policy);
382
+ const rejection = this.getRoutingRejectionForModel(
383
+ account,
384
+ poolKey,
385
+ now,
386
+ policy,
387
+ );
344
388
  if (rejection) {
345
389
  firstReason ??= rejection.detail;
346
390
  continue;
@@ -453,7 +497,10 @@ export class AccountRotator {
453
497
  const names = (data.models ?? [])
454
498
  .map((m) => m.name ?? m.model ?? "")
455
499
  .filter(Boolean);
456
- if (names.length > 0) this.setOllamaModels(names);
500
+ if (names.length > 0) {
501
+ this.setOllamaModels(names);
502
+ void primeOllamaContextWindows(names, ollamaAccount).catch(() => {});
503
+ }
457
504
  } catch {
458
505
  // Catalog refresh is non-fatal
459
506
  }
@@ -649,6 +696,12 @@ export class AccountRotator {
649
696
  getCredentialGenerationFingerprint(account, DEFAULT_PROVIDER),
650
697
  })),
651
698
  );
699
+ dynamicCatalog.restorePersistedSnapshots(state.dynamicModelSnapshots,
700
+ this.accounts.filter((account) => hasCredential(account.config, DEFAULT_PROVIDER)).map((account) => ({
701
+ id: getAccountIdentity(account),
702
+ credentialGenerationFingerprint: getCredentialGenerationFingerprint(account, DEFAULT_PROVIDER),
703
+ })),
704
+ );
652
705
  // Load per-model account assignments
653
706
  if (state.modelAccounts) {
654
707
  for (const [model, idx] of Object.entries(state.modelAccounts)) {
@@ -837,6 +890,13 @@ export class AccountRotator {
837
890
 
838
891
  const disabledProvidersByAccount: Record<string, string[]> = {};
839
892
  const state: PersistedState = {
893
+ dynamicModelSnapshots: dynamicCatalog.getPersistedSnapshots(
894
+ this.accounts.filter((account) => !account.disabled && !account.flagged &&
895
+ !account.invalidProviders?.[DEFAULT_PROVIDER] && hasCredential(account.config, DEFAULT_PROVIDER)).map((account) => ({
896
+ id: getAccountIdentity(account),
897
+ credentialGenerationFingerprint: getCredentialGenerationFingerprint(account, DEFAULT_PROVIDER),
898
+ })),
899
+ ),
840
900
  dynamicModelQuotaPools: dynamicCatalog.getDiscoveredQuotaPools(),
841
901
  dynamicModelOwnership: dynamicCatalog.getPersistedModelOwnership(
842
902
  this.accounts
@@ -1018,6 +1078,7 @@ export class AccountRotator {
1018
1078
 
1019
1079
  private async pollAccountQuotaOnce(account: AccountRuntime): Promise<boolean> {
1020
1080
  let quotaPublished = false;
1081
+ const snapshotRevision = dynamicCatalog.snapshotRevision;
1021
1082
  try {
1022
1083
  const quotaCtx: QuotaFetchContext = {
1023
1084
  log: (message) => this.log(message),
@@ -1075,10 +1136,10 @@ export class AccountRotator {
1075
1136
  cooldownChanged = true;
1076
1137
  }
1077
1138
  }
1078
- if (cooldownChanged) {
1139
+ if (cooldownChanged || snapshotRevision !== dynamicCatalog.snapshotRevision) {
1079
1140
  this.scheduleStateSave();
1080
- this.requestWaiterDrain();
1081
1141
  }
1142
+ if (cooldownChanged) this.requestWaiterDrain();
1082
1143
 
1083
1144
  // Consolidated RAW POLL across all providers on this account:
1084
1145
  // google first (Antigravity OAuth pools), then ollama (usage pools).
@@ -1334,6 +1395,7 @@ export class AccountRotator {
1334
1395
  modelKey: string,
1335
1396
  now: number,
1336
1397
  excludeIdx: number = -1,
1398
+ autoScope?: AutoAccountScope,
1337
1399
  ): AccountRuntime | null {
1338
1400
  let best: AccountRuntime | null = null;
1339
1401
  let bestMetrics: {
@@ -1360,7 +1422,7 @@ export class AccountRotator {
1360
1422
  if (i === excludeIdx) continue;
1361
1423
  const account = this.accounts[i];
1362
1424
  if (!this.isProviderEligibleForKey(account, modelKey)) continue;
1363
- if (!this.isAvailableForModel(account, modelKey, now)) continue;
1425
+ if (!this.isAvailableForModel(account, modelKey, now, autoScope)) continue;
1364
1426
 
1365
1427
  const quota = this.getModelQuota(account, modelKey);
1366
1428
  if (quota === 0) continue;
@@ -1416,6 +1478,7 @@ export class AccountRotator {
1416
1478
  private pickLeastLoadedModelAccount(
1417
1479
  modelKey: string,
1418
1480
  now: number,
1481
+ autoScope?: AutoAccountScope,
1419
1482
  ): AccountRuntime | null {
1420
1483
  let best: AccountRuntime | null = null;
1421
1484
  let bestLoad = Infinity;
@@ -1425,7 +1488,7 @@ export class AccountRotator {
1425
1488
 
1426
1489
  for (let i = 0; i < this.accounts.length; i++) {
1427
1490
  const account = this.accounts[i];
1428
- if (!this.isRoutableForModel(account, modelKey, now)) continue;
1491
+ if (!this.isRoutableForModel(account, modelKey, now, autoScope)) continue;
1429
1492
  const load = account.inFlightRequests;
1430
1493
  const tier = this.getTierRank(account);
1431
1494
  const distance = this.requestCursorIndex < 0
@@ -1475,8 +1538,9 @@ export class AccountRotator {
1475
1538
  modelKey: string,
1476
1539
  now: number = Date.now(),
1477
1540
  excludeIdx?: number,
1541
+ autoScope?: AutoAccountScope,
1478
1542
  ): Promise<AccountRuntime | null> {
1479
- return this.rotateModel(modelKey, now, excludeIdx, true);
1543
+ return this.rotateModel(modelKey, now, excludeIdx, true, autoScope);
1480
1544
  }
1481
1545
 
1482
1546
  private rollDailySafetyIfNeeded(now: number): void {
@@ -1661,11 +1725,25 @@ export class AccountRotator {
1661
1725
  now: number,
1662
1726
  policy: Config["routingPolicy"],
1663
1727
  ignoreConcurrency = false,
1728
+ autoScope?: AutoAccountScope,
1664
1729
  ): { reason: RoutingRejectionReason; detail: string } | null {
1665
1730
  if (account.disabled)
1666
1731
  return { reason: "disabled", detail: "account disabled" };
1667
1732
  if (account.flagged)
1668
1733
  return { reason: "flagged", detail: "account quarantined or flagged" };
1734
+ if (autoScope) {
1735
+ const access = evaluateAutoModelAccess(
1736
+ account.config,
1737
+ autoScope.providerId,
1738
+ autoScope.modelId,
1739
+ );
1740
+ if (!access.eligible) {
1741
+ return {
1742
+ reason: "provider-ineligible",
1743
+ detail: `auto access policy: ${access.reason}`,
1744
+ };
1745
+ }
1746
+ }
1669
1747
  const providerRejection = this.getProviderEligibilityRejection(
1670
1748
  account,
1671
1749
  modelKey,
@@ -2036,19 +2114,20 @@ export class AccountRotator {
2036
2114
  model?: string,
2037
2115
  signal?: AbortSignal,
2038
2116
  providerId?: string,
2117
+ autoScope?: AutoAccountScope,
2039
2118
  ): Promise<AccountRuntime | null> {
2040
2119
  if (signal?.aborted) return null;
2041
2120
  const queueKey = this.getRequestQueueKey(model, providerId);
2042
2121
  if (
2043
2122
  this.hasQueuedRequest(queueKey) ||
2044
- this.isConcurrencySaturated(model, providerId)
2123
+ this.isConcurrencySaturated(model, providerId, autoScope)
2045
2124
  ) {
2046
- return this.enqueueAccountRequest(model, signal, queueKey, providerId);
2125
+ return this.enqueueAccountRequest(model, signal, queueKey, providerId, autoScope);
2047
2126
  }
2048
2127
 
2049
- const account = await this.tryGetActiveAccount(model, providerId);
2050
- if (account || !this.isConcurrencySaturated(model, providerId)) return account;
2051
- return this.enqueueAccountRequest(model, signal, queueKey, providerId);
2128
+ const account = await this.tryGetActiveAccount(model, providerId, autoScope);
2129
+ if (account || !this.isConcurrencySaturated(model, providerId, autoScope)) return account;
2130
+ return this.enqueueAccountRequest(model, signal, queueKey, providerId, autoScope);
2052
2131
  }
2053
2132
 
2054
2133
  /** Keep fairness within a provider pool without making unrelated providers wait. */
@@ -2066,6 +2145,7 @@ export class AccountRotator {
2066
2145
  signal?: AbortSignal,
2067
2146
  queueKey = this.getRequestQueueKey(model),
2068
2147
  providerId?: string,
2148
+ autoScope?: AutoAccountScope,
2069
2149
  ): Promise<AccountRuntime | null> {
2070
2150
  if (signal?.aborted) return Promise.resolve(null);
2071
2151
 
@@ -2074,6 +2154,7 @@ export class AccountRotator {
2074
2154
  waiter.queueKey = queueKey;
2075
2155
  waiter.model = model;
2076
2156
  waiter.providerId = providerId;
2157
+ waiter.autoScope = autoScope;
2077
2158
  waiter.signal = signal;
2078
2159
  waiter.resolve = resolve;
2079
2160
  waiter.reject = reject;
@@ -2187,7 +2268,7 @@ export class AccountRotator {
2187
2268
 
2188
2269
  let account: AccountRuntime | null;
2189
2270
  try {
2190
- account = await this.tryGetActiveAccount(waiter.model, waiter.providerId);
2271
+ account = await this.tryGetActiveAccount(waiter.model, waiter.providerId, waiter.autoScope);
2191
2272
  } catch (error) {
2192
2273
  this.rejectAccountRequestWaiter(waiter, error);
2193
2274
  progressed = true;
@@ -2216,9 +2297,9 @@ export class AccountRotator {
2216
2297
  progressed = true;
2217
2298
  continue;
2218
2299
  }
2219
- const wakeAt = this.getNextRequestAvailabilityAt(waiter.model, waiter.providerId);
2300
+ const wakeAt = this.getNextRequestAvailabilityAt(waiter.model, waiter.providerId, waiter.autoScope);
2220
2301
  if (wakeAt !== null) this.scheduleRequestWaiterWake(wakeAt);
2221
- if (!this.isConcurrencySaturated(waiter.model, waiter.providerId) && wakeAt === null) {
2302
+ if (!this.isConcurrencySaturated(waiter.model, waiter.providerId, waiter.autoScope) && wakeAt === null) {
2222
2303
  this.settleAccountRequestWaiter(waiter, null);
2223
2304
  progressed = true;
2224
2305
  }
@@ -2230,7 +2311,11 @@ export class AccountRotator {
2230
2311
  }
2231
2312
  }
2232
2313
 
2233
- private isConcurrencySaturated(model?: string, providerId?: string): boolean {
2314
+ private isConcurrencySaturated(
2315
+ model?: string,
2316
+ providerId?: string,
2317
+ autoScope?: AutoAccountScope,
2318
+ ): boolean {
2234
2319
  const now = Date.now();
2235
2320
  if (this.isProtectivePauseActive(now)) return false;
2236
2321
  const modelKey = model ? this.resolveRequestPoolKey(model, providerId) : null;
@@ -2246,6 +2331,7 @@ export class AccountRotator {
2246
2331
  now,
2247
2332
  policy,
2248
2333
  true,
2334
+ autoScope,
2249
2335
  )
2250
2336
  ) {
2251
2337
  continue;
@@ -2255,6 +2341,8 @@ export class AccountRotator {
2255
2341
  modelKey,
2256
2342
  now,
2257
2343
  policy,
2344
+ false,
2345
+ autoScope,
2258
2346
  );
2259
2347
  if (!rejection) return false;
2260
2348
  if (
@@ -2269,7 +2357,11 @@ export class AccountRotator {
2269
2357
 
2270
2358
  // Try to reserve an account immediately. The public method queues only
2271
2359
  // when every otherwise-eligible account is blocked by concurrency.
2272
- private async tryGetActiveAccount(model?: string, providerId?: string): Promise<AccountRuntime | null> {
2360
+ private async tryGetActiveAccount(
2361
+ model?: string,
2362
+ providerId?: string,
2363
+ autoScope?: AutoAccountScope,
2364
+ ): Promise<AccountRuntime | null> {
2273
2365
  const now = Date.now();
2274
2366
  if (this.accounts.length === 0) return null;
2275
2367
  if (this.isProtectivePauseActive(now)) return null;
@@ -2284,7 +2376,7 @@ export class AccountRotator {
2284
2376
  !state &&
2285
2377
  current &&
2286
2378
  this.isQuotaAwarePolicy() &&
2287
- this.isRoutableForModel(current, modelKey, now)
2379
+ this.isRoutableForModel(current, modelKey, now, autoScope)
2288
2380
  ) {
2289
2381
  // Seed the preference on the first request as well. Without this, a
2290
2382
  // default account that later enters cooldown would be indistinguishable
@@ -2303,18 +2395,18 @@ export class AccountRotator {
2303
2395
  );
2304
2396
  const policy = this.config.routingPolicy || "timer-first";
2305
2397
  if (modelKey && policy === "tier-first" && !hasActiveRequests) {
2306
- const preferred = this.pickBestModelAccount(modelKey, now);
2398
+ const preferred = this.pickBestModelAccount(modelKey, now, -1, autoScope);
2307
2399
  if (preferred && preferred !== current) {
2308
2400
  // A persisted assignment may have been selected under a previous
2309
2401
  // policy or before account tiers were corrected. Re-evaluate it while
2310
2402
  // idle so tier-first does not keep serving a lower-tier account.
2311
- return this.rotateModelForRequest(modelKey, now, idx);
2403
+ return this.rotateModelForRequest(modelKey, now, idx, autoScope);
2312
2404
  }
2313
2405
  }
2314
2406
  if (modelKey && hasActiveRequests) {
2315
- const leastLoaded = this.pickLeastLoadedModelAccount(modelKey, now);
2407
+ const leastLoaded = this.pickLeastLoadedModelAccount(modelKey, now, autoScope);
2316
2408
  if (leastLoaded && leastLoaded !== current) {
2317
- return this.rotateModelForRequest(modelKey, now, idx);
2409
+ return this.rotateModelForRequest(modelKey, now, idx, autoScope);
2318
2410
  }
2319
2411
  }
2320
2412
 
@@ -2325,19 +2417,23 @@ export class AccountRotator {
2325
2417
  now,
2326
2418
  state,
2327
2419
  idx,
2420
+ autoScope,
2328
2421
  );
2329
2422
  if (restored) return restored;
2330
2423
  } catch (error) {
2331
2424
  if (!this.isCodexPoolKey(modelKey)) throw error;
2332
- return this.rotateModelForRequest(modelKey, now, idx);
2425
+ return this.rotateModelForRequest(modelKey, now, idx, autoScope);
2333
2426
  }
2334
2427
  }
2335
2428
 
2336
2429
  if (current && modelKey) {
2337
- const providerRejection = this.getProviderEligibilityRejection(
2430
+ const providerRejection = this.getRoutingRejectionForModel(
2338
2431
  current,
2339
2432
  modelKey,
2340
2433
  now,
2434
+ this.config.routingPolicy || "timer-first",
2435
+ false,
2436
+ autoScope,
2341
2437
  );
2342
2438
  if (providerRejection) {
2343
2439
  // A cooldown is expected after a provider 429 and can be hit by many
@@ -2357,14 +2453,14 @@ export class AccountRotator {
2357
2453
  );
2358
2454
  }
2359
2455
  }
2360
- return this.rotateModelForRequest(modelKey, now, idx);
2456
+ return this.rotateModelForRequest(modelKey, now, idx, autoScope);
2361
2457
  }
2362
2458
  }
2363
2459
  if (
2364
2460
  current &&
2365
2461
  (!modelKey
2366
2462
  ? this.isAvailable(current, now)
2367
- : this.isAvailableForModel(current, modelKey, now))
2463
+ : this.isAvailableForModel(current, modelKey, now, autoScope))
2368
2464
  ) {
2369
2465
  // Check if this account has quota for the requested model
2370
2466
  if (modelKey) {
@@ -2372,7 +2468,7 @@ export class AccountRotator {
2372
2468
  this.log(
2373
2469
  `${current.config.label || current.config.email} [${modelKey}]: hit rotation threshold (${this.config.requestsPerRotation})`,
2374
2470
  );
2375
- const rotated = await this.rotateModelForRequest(modelKey, now, idx);
2471
+ const rotated = await this.rotateModelForRequest(modelKey, now, idx, autoScope);
2376
2472
  if (rotated) {
2377
2473
  current.requestsSinceRotation = 0;
2378
2474
  return rotated;
@@ -2387,7 +2483,7 @@ export class AccountRotator {
2387
2483
  this.log(
2388
2484
  `${current.config.label || current.config.email} [${modelKey}]: 0% quota, skipping`,
2389
2485
  );
2390
- return this.rotateModelForRequest(modelKey);
2486
+ return this.rotateModelForRequest(modelKey, now, undefined, autoScope);
2391
2487
  }
2392
2488
  if (!this.isFreshWindowAllowed(current, modelKey)) {
2393
2489
  const label = current.config.label || current.config.email;
@@ -2397,7 +2493,7 @@ export class AccountRotator {
2397
2493
  : `${label} [${modelKey}]: fresh window blocked by operator toggle`,
2398
2494
  "warn",
2399
2495
  );
2400
- return this.rotateModelForRequest(modelKey);
2496
+ return this.rotateModelForRequest(modelKey, now, undefined, autoScope);
2401
2497
  }
2402
2498
  if ((this.config.routingPolicy || "timer-first") === "hybrid") {
2403
2499
  const tokenSnapshot = this.getTokenBucketSnapshot(current, now);
@@ -2406,7 +2502,7 @@ export class AccountRotator {
2406
2502
  `${current.config.label || current.config.email} [${modelKey}]: local token bucket is empty, rotating to another candidate`,
2407
2503
  "warn",
2408
2504
  );
2409
- return this.rotateModelForRequest(modelKey, now, idx);
2505
+ return this.rotateModelForRequest(modelKey, now, idx, autoScope);
2410
2506
  }
2411
2507
  }
2412
2508
  }
@@ -2426,7 +2522,7 @@ export class AccountRotator {
2426
2522
  this.refundTokenBucket(current, Date.now());
2427
2523
  this.finishRequest(current, modelKey ?? undefined, providerId);
2428
2524
  if (modelKey && this.isCodexPoolKey(modelKey)) {
2429
- return this.rotateModelForRequest(modelKey, now, idx);
2525
+ return this.rotateModelForRequest(modelKey, now, idx, autoScope);
2430
2526
  }
2431
2527
  throw err;
2432
2528
  }
@@ -2434,7 +2530,7 @@ export class AccountRotator {
2434
2530
 
2435
2531
  // Current unavailable, or no per-model assignment yet
2436
2532
  if (modelKey) {
2437
- return this.rotateModelForRequest(modelKey, now, state ? idx : -1);
2533
+ return this.rotateModelForRequest(modelKey, now, state ? idx : -1, autoScope);
2438
2534
  }
2439
2535
  const defaultAcc = await this.rotateDefault(now, true);
2440
2536
  return defaultAcc;
@@ -2445,6 +2541,7 @@ export class AccountRotator {
2445
2541
  now: number,
2446
2542
  state: ModelRotationState,
2447
2543
  activeIndex: number,
2544
+ autoScope?: AutoAccountScope,
2448
2545
  ): Promise<AccountRuntime | null> {
2449
2546
  if (!this.isQuotaAwarePolicy()) return null;
2450
2547
  const preferredIndex = state.stickyAccountIndex;
@@ -2465,7 +2562,7 @@ export class AccountRotator {
2465
2562
  this.scheduleStateSave();
2466
2563
  return null;
2467
2564
  }
2468
- if (!this.isRoutableForModel(preferred, modelKey, now)) return null;
2565
+ if (!this.isRoutableForModel(preferred, modelKey, now, autoScope)) return null;
2469
2566
 
2470
2567
  this.log(
2471
2568
  `[${modelKey}] Restoring preferred account ${preferred.config.label || preferred.config.email} after temporary fallback`,
@@ -2486,10 +2583,11 @@ export class AccountRotator {
2486
2583
  excludeIdx: number = this.modelState.get(modelKey)?.activeAccountIndex ??
2487
2584
  -1,
2488
2585
  forRequest = false,
2586
+ autoScope?: AutoAccountScope,
2489
2587
  ): Promise<AccountRuntime | null> {
2490
2588
  const best = forRequest
2491
- ? this.pickLeastLoadedModelAccount(modelKey, now)
2492
- : this.pickBestModelAccount(modelKey, now, excludeIdx);
2589
+ ? this.pickLeastLoadedModelAccount(modelKey, now, autoScope)
2590
+ : this.pickBestModelAccount(modelKey, now, excludeIdx, autoScope);
2493
2591
 
2494
2592
  if (best) {
2495
2593
  const previous = this.modelState.get(modelKey);
@@ -2505,7 +2603,7 @@ export class AccountRotator {
2505
2603
  preferred &&
2506
2604
  this.getModelQuota(preferred, modelKey) !== 0 &&
2507
2605
  (preferredIndex !== previous.activeAccountIndex ||
2508
- !this.isAvailableForModel(preferred, modelKey, now))
2606
+ !this.isAvailableForModel(preferred, modelKey, now, autoScope))
2509
2607
  ) {
2510
2608
  // Keep the original account as the preference while a cooldown,
2511
2609
  // breaker, or concurrency limit forces a temporary fallback.
@@ -2534,6 +2632,7 @@ export class AccountRotator {
2534
2632
  now,
2535
2633
  this.accounts.indexOf(best),
2536
2634
  forRequest,
2635
+ autoScope,
2537
2636
  );
2538
2637
  }
2539
2638
  }
@@ -2691,6 +2790,7 @@ export class AccountRotator {
2691
2790
  model?: string,
2692
2791
  failedAccount?: AccountRuntime | number | string,
2693
2792
  providerId?: string,
2793
+ autoScope?: AutoAccountScope,
2694
2794
  ): Promise<AccountRuntime | null> {
2695
2795
  if (this.isProtectivePauseActive(Date.now())) return null;
2696
2796
  const modelKey = model ? this.resolveRequestPoolKey(model, providerId) : null;
@@ -2711,7 +2811,7 @@ export class AccountRotator {
2711
2811
  : this.defaultIndex;
2712
2812
  }
2713
2813
  return modelKey
2714
- ? this.rotateModel(modelKey, Date.now(), excludeIdx)
2814
+ ? this.rotateModel(modelKey, Date.now(), excludeIdx, false, autoScope)
2715
2815
  : this.rotateDefault(Date.now(), false, excludeIdx);
2716
2816
  }
2717
2817
 
@@ -3889,9 +3989,20 @@ export class AccountRotator {
3889
3989
  account: AccountRuntime,
3890
3990
  modelKey: string,
3891
3991
  now: number,
3992
+ autoScope?: AutoAccountScope,
3892
3993
  ): boolean {
3893
3994
  if (!this.isAvailable(account, now)) return false;
3894
3995
  const providerId = getProviderIdForPoolKey(modelKey);
3996
+ if (
3997
+ autoScope &&
3998
+ !evaluateAutoModelAccess(
3999
+ account.config,
4000
+ autoScope.providerId,
4001
+ autoScope.modelId,
4002
+ ).eligible
4003
+ ) {
4004
+ return false;
4005
+ }
3895
4006
  if (account.invalidProviders?.[providerId]) return false;
3896
4007
  if (this.isProviderOffForAccount(account, providerId)) return false;
3897
4008
  if ((account.providerCooldowns?.[providerId] ?? 0) > now) return false;
@@ -4133,10 +4244,11 @@ export class AccountRotator {
4133
4244
  account: AccountRuntime,
4134
4245
  modelKey: string,
4135
4246
  now: number,
4247
+ autoScope?: AutoAccountScope,
4136
4248
  ): boolean {
4137
4249
  modelKey = this.resolvePoolKey(account, modelKey);
4138
4250
  if (!this.isProviderEligibleForKey(account, modelKey)) return false;
4139
- if (!this.isAvailableForModel(account, modelKey, now)) return false;
4251
+ if (!this.isAvailableForModel(account, modelKey, now, autoScope)) return false;
4140
4252
  if (this.getModelQuota(account, modelKey) === 0) return false;
4141
4253
  if (!this.isFreshWindowAllowed(account, modelKey)) return false;
4142
4254
  if ((this.config.routingPolicy || "timer-first") === "hybrid") {
@@ -4150,6 +4262,7 @@ export class AccountRotator {
4150
4262
  model: string | undefined,
4151
4263
  now: number,
4152
4264
  providerId?: string,
4265
+ autoScope?: AutoAccountScope,
4153
4266
  ): number[] {
4154
4267
  const retryTimes: number[] = [];
4155
4268
  if (this.protectivePauseUntil > now)
@@ -4164,6 +4277,16 @@ export class AccountRotator {
4164
4277
  for (const account of this.accounts) {
4165
4278
  if (account.disabled || account.flagged) continue;
4166
4279
  const candidateProviderId = providerId ?? getProviderIdForPoolKey(modelKey);
4280
+ if (
4281
+ autoScope &&
4282
+ !evaluateAutoModelAccess(
4283
+ account.config,
4284
+ autoScope.providerId,
4285
+ autoScope.modelId,
4286
+ ).eligible
4287
+ ) {
4288
+ continue;
4289
+ }
4167
4290
  if (
4168
4291
  !hasCredential(account.config, candidateProviderId) ||
4169
4292
  account.invalidProviders?.[candidateProviderId] ||
@@ -4200,9 +4323,13 @@ export class AccountRotator {
4200
4323
  return retryTimes;
4201
4324
  }
4202
4325
 
4203
- private getNextRequestAvailabilityAt(model?: string, providerId?: string): number | null {
4326
+ private getNextRequestAvailabilityAt(
4327
+ model?: string,
4328
+ providerId?: string,
4329
+ autoScope?: AutoAccountScope,
4330
+ ): number | null {
4204
4331
  const now = Date.now();
4205
- const retryTimes = this.getRequestAvailabilityTimes(model, now, providerId);
4332
+ const retryTimes = this.getRequestAvailabilityTimes(model, now, providerId, autoScope);
4206
4333
  return retryTimes.length > 0 ? Math.min(...retryTimes) : null;
4207
4334
  }
4208
4335
 
@@ -85,6 +85,7 @@ const DISK_FILES: Record<string, DiskFileSpec> = {
85
85
  trim: false,
86
86
  mode: 0o600,
87
87
  },
88
+ model_access_evidence: { filename: "model-access-evidence.json", backup: false, trim: false, mode: 0o600 },
88
89
  rotator_state: { filename: "state.json", backup: true, trim: false },
89
90
  token_usage: { filename: "token-usage.json", backup: false, trim: false },
90
91
  responses_store: { filename: "responses.json", backup: false, trim: false },