@cortexkit/anthropic-auth-core 2.11.0 → 2.11.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.
@@ -437,6 +437,7 @@ export declare function quotaRefusalTiming(input: {
437
437
  }): {
438
438
  resetAt: number | undefined;
439
439
  retryAfterSeconds: number | undefined;
440
+ limitName?: string;
440
441
  };
441
442
  /** Compatibility accessor for callers that need only the capped retry delay. */
442
443
  export declare function killswitchRetryAfterSeconds(mainQuota: OAuthQuotaSnapshot | undefined, fallbackAccounts: Array<QuotaResetEvidence>, now: number, scopedModelId?: string, options?: {
@@ -0,0 +1,6 @@
1
+ /** Hosts recheck availability periodically instead of sleeping until a distant quota reset. */
2
+ export declare const MAX_HOST_RETRY_MS: number;
3
+ export declare function clampHostRetryMs(delayMs: number): number;
4
+ /** Keep quota reset headers intact: they are evidence, not instructions to the host. */
5
+ export declare function hostRetryHeaders(upstream: Headers, now?: number): Headers;
6
+ export declare function hostRetryResponse(response: Response, now?: number): Response;
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export * from './dump.ts';
21
21
  export * from './fast.ts';
22
22
  export * from './holder-registry.ts';
23
23
  export * from './host-paths.ts';
24
+ export * from './host-retry.ts';
24
25
  export * from './json.ts';
25
26
  export * from './killswitch.ts';
26
27
  export * from './logger.ts';
package/dist/index.js CHANGED
@@ -10234,6 +10234,40 @@ async function exchange(input, verifier, redirectUri, expectedState) {
10234
10234
  return exchangeCode(callback, verifier, redirectUri);
10235
10235
  }
10236
10236
 
10237
+ // src/host-retry.ts
10238
+ var MAX_HOST_RETRY_MS = 15 * 60000;
10239
+ function clampHostRetryMs(delayMs) {
10240
+ return Math.max(0, Math.min(MAX_HOST_RETRY_MS, Number.isNaN(delayMs) ? 0 : delayMs));
10241
+ }
10242
+ function hostRetryHeaders(upstream, now = Date.now()) {
10243
+ const headers = new Headers(upstream);
10244
+ for (const name of ["retry-after", "retry-after-ms"]) {
10245
+ const value = headers.get(name);
10246
+ if (value === null)
10247
+ continue;
10248
+ const numeric = value.trim() ? Number(value) : NaN;
10249
+ const delayMs = name === "retry-after-ms" ? numeric : Number.isNaN(numeric) ? Date.parse(value) - now : numeric * 1000;
10250
+ if (Number.isNaN(delayMs)) {
10251
+ headers.delete(name);
10252
+ continue;
10253
+ }
10254
+ const capped = clampHostRetryMs(delayMs);
10255
+ if (capped !== delayMs)
10256
+ headers.set(name, String(name === "retry-after-ms" ? capped : capped / 1000));
10257
+ }
10258
+ return headers;
10259
+ }
10260
+ function hostRetryResponse(response, now = Date.now()) {
10261
+ const headers = hostRetryHeaders(response.headers, now);
10262
+ if (["retry-after", "retry-after-ms"].every((name) => headers.get(name) === response.headers.get(name)))
10263
+ return response;
10264
+ return new Response(response.body, {
10265
+ status: response.status,
10266
+ statusText: response.statusText,
10267
+ headers
10268
+ });
10269
+ }
10270
+
10237
10271
  // src/routing.ts
10238
10272
  var CLAUDE_ROUTING_COMMAND_NAME = "claude-routing";
10239
10273
  var ROUTING_MODES = [
@@ -11046,32 +11080,44 @@ function quotaRefusalTiming(input) {
11046
11080
  break;
11047
11081
  }
11048
11082
  if (window.remainingPercent <= 0 || window.remainingPercent < Math.max(floors[key], killswitch[key]))
11049
- blockers.push(window);
11083
+ blockers.push({
11084
+ window,
11085
+ name: key === "five_hour" ? "5-hour" : "weekly"
11086
+ });
11050
11087
  }
11051
11088
  if (scoped) {
11052
11089
  if (!Number.isFinite(scoped.remainingPercent))
11053
11090
  unknown = true;
11054
11091
  else if (scoped.remainingPercent <= killswitch.scoped)
11055
- blockers.push(scoped);
11092
+ blockers.push({
11093
+ window: scoped,
11094
+ name: `${scoped.modelName ?? "Model"} weekly`
11095
+ });
11056
11096
  }
11057
11097
  let usableAt;
11058
- for (const window of blockers) {
11098
+ let limitName = "";
11099
+ for (const { window, name } of blockers) {
11059
11100
  const resetAt2 = Date.parse(window?.resetsAt ?? "");
11060
11101
  if (!Number.isFinite(resetAt2) || resetAt2 <= now) {
11061
11102
  unknown = true;
11062
11103
  break;
11063
11104
  }
11064
- usableAt = Math.max(usableAt ?? resetAt2, resetAt2);
11105
+ if (usableAt === undefined || resetAt2 >= usableAt) {
11106
+ usableAt = resetAt2;
11107
+ limitName = name;
11108
+ }
11065
11109
  }
11066
11110
  if (!unknown && usableAt !== undefined)
11067
- resetTimes.push(usableAt);
11111
+ resetTimes.push({ resetAt: usableAt, limitName });
11068
11112
  }
11069
- const resetAt = resetTimes.length ? Math.min(...resetTimes) : undefined;
11113
+ const earliest = resetTimes.sort((a, b) => a.resetAt - b.resetAt)[0];
11114
+ const resetAt = earliest?.resetAt;
11070
11115
  const wait = getMaxSameAccountWait(input.storage ?? null);
11071
11116
  const ttlMs = input.sessionTtlMs && input.sessionTtlMs > 0 && Number.isFinite(input.sessionTtlMs) ? input.sessionTtlMs : 300000;
11072
- const capMs = wait === "max" ? ttlMs : Number(wait) * 60000;
11117
+ const capMs = clampHostRetryMs(wait === "max" ? ttlMs : Number(wait) * 60000);
11073
11118
  return {
11074
11119
  resetAt,
11120
+ ...earliest ? { limitName: earliest.limitName } : {},
11075
11121
  retryAfterSeconds: wait === "0" ? undefined : Math.max(1, Math.ceil(Math.min(resetAt === undefined ? 300000 : resetAt - now, capMs) / 1000))
11076
11122
  };
11077
11123
  }
@@ -24709,21 +24755,39 @@ function mergeHeaderQuotaSnapshot(existing, incoming) {
24709
24755
  // src/quota-refusal.ts
24710
24756
  var details = new WeakMap;
24711
24757
  var quotaRefusalDetails = (response) => details.get(response);
24712
- function createQuotaRefusalResponse(reason, timing, _now) {
24758
+ function retryMinutes(seconds) {
24759
+ const minutes = Math.max(1, Math.ceil(seconds / 60));
24760
+ return minutes === 1 ? "1 minute" : `${minutes} minutes`;
24761
+ }
24762
+ function createQuotaRefusalResponse(reason, timing, now = Date.now(), options = {}) {
24763
+ timing = {
24764
+ ...timing,
24765
+ retryAfterSeconds: timing.retryAfterSeconds === undefined ? undefined : clampHostRetryMs(timing.retryAfterSeconds * 1000) / 1000
24766
+ };
24713
24767
  const reset = timing.resetAt === undefined ? "Reset time is unknown." : `Reset at ${new Intl.DateTimeFormat("en-US", {
24714
24768
  month: "long",
24715
24769
  day: "numeric",
24716
24770
  hour: "2-digit",
24717
24771
  minute: "2-digit",
24718
- timeZoneName: "short"
24772
+ timeZoneName: "short",
24773
+ timeZone: "UTC"
24719
24774
  }).format(timing.resetAt)}.`;
24720
24775
  const seconds = timing.retryAfterSeconds;
24721
24776
  const retry = seconds === undefined ? "" : ` Retry in ${Math.floor(seconds / 60)}m ${seconds % 60}s.`;
24777
+ const resetTime = timing.resetAt === undefined ? undefined : new Intl.DateTimeFormat("en-GB", {
24778
+ hour: "2-digit",
24779
+ minute: "2-digit",
24780
+ timeZone: "UTC",
24781
+ hour12: false
24782
+ }).format(timing.resetAt);
24783
+ const remaining = timing.resetAt === undefined ? 0 : Math.max(0, timing.resetAt - now);
24784
+ const duration = remaining >= 3600000 ? `${Math.ceil(remaining / 3600000)} h` : `${Math.ceil(remaining / 60000)} min`;
24785
+ const message = timing.limitName && resetTime ? `No Claude account has room for this request (${timing.limitName} limit or your quota floor). The earliest reset is ${resetTime} UTC (in ${duration}). ${seconds === undefined || options.retry === "manual" ? "Send the message again after that." : `Retrying every ${retryMinutes(seconds)}.`} ${reason}` : `${reason}${retry} ${reset}`;
24722
24786
  const response = new Response(JSON.stringify({
24723
24787
  type: "error",
24724
24788
  error: {
24725
24789
  type: timing.retryAfterSeconds === undefined ? "FAILED_PRECONDITION" : "rate_limit_error",
24726
- message: `${reason}${retry} ${reset}`
24790
+ message
24727
24791
  }
24728
24792
  }), {
24729
24793
  status: timing.retryAfterSeconds === undefined ? 412 : 429,
@@ -42918,7 +42982,7 @@ function reportRoutingResponse(sessionId, subject, headers, status, modelId, ser
42918
42982
  }
42919
42983
 
42920
42984
  // src/routing-relay.ts
42921
- var ROUTING_HOLD_LIMIT_MS = 15 * 60000 + 20000;
42985
+ var ROUTING_HOLD_LIMIT_MS = MAX_HOST_RETRY_MS;
42922
42986
  function routingCandidates(accounts) {
42923
42987
  return accounts.filter((account) => account.type === "oauth" && nativeAccountCanServe(account));
42924
42988
  }
@@ -42936,6 +43000,8 @@ class RoutingRefusalError extends Error {
42936
43000
  this.code = refusal2.code;
42937
43001
  this.status = refusal2.code === "account_selection_final" ? 403 : 429;
42938
43002
  this.retryAfterSecs = this.status === 429 ? refusal2.retryAfterSecs ?? (refusal2.resetsAtMs === undefined ? undefined : Math.max(0, Math.ceil((refusal2.resetsAtMs - now) / 1000))) : undefined;
43003
+ if (this.retryAfterSecs !== undefined)
43004
+ this.retryAfterSecs = clampHostRetryMs(this.retryAfterSecs * 1000) / 1000;
42939
43005
  }
42940
43006
  response() {
42941
43007
  return new Response(JSON.stringify({
@@ -45629,6 +45695,8 @@ export {
45629
45695
  isCache1hEnabled,
45630
45696
  isApiKeyAccount,
45631
45697
  inspectNativeHostAuthEntry,
45698
+ hostRetryResponse,
45699
+ hostRetryHeaders,
45632
45700
  homeDefaultConfigDir,
45633
45701
  holderEntryLiveness,
45634
45702
  holderEntryIsLive,
@@ -45780,6 +45848,7 @@ export {
45780
45848
  classifySafeErrorForLog,
45781
45849
  classifyGzipRejection,
45782
45850
  classifyCacheKeepFailure,
45851
+ clampHostRetryMs,
45783
45852
  captureNativeLocalPoolBinding,
45784
45853
  canonicalPath,
45785
45854
  cacheWarmCacheablePrefixFingerprint,
@@ -45907,6 +45976,7 @@ export {
45907
45976
  MESSAGE_THREADS_BETA,
45908
45977
  MAX_UNCONFIRMED_SAME_ACCOUNT_RETRIES,
45909
45978
  MAX_SAME_ACCOUNT_WAITS,
45979
+ MAX_HOST_RETRY_MS,
45910
45980
  LOGGING_LEVELS,
45911
45981
  LEGACY_STATE_LAYOUT,
45912
45982
  KILLSWITCH_COMMAND_NAME,
@@ -3,4 +3,6 @@ export declare const quotaRefusalDetails: (response: Response) => {
3
3
  reason: string;
4
4
  timing: ReturnType<typeof quotaRefusalTiming>;
5
5
  } | undefined;
6
- export declare function createQuotaRefusalResponse(reason: string, timing: ReturnType<typeof quotaRefusalTiming>, _now?: number): Response;
6
+ export declare function createQuotaRefusalResponse(reason: string, timing: ReturnType<typeof quotaRefusalTiming>, now?: number, options?: {
7
+ retry?: 'automatic' | 'manual';
8
+ }): Response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/anthropic-auth-core",
3
- "version": "2.11.0",
3
+ "version": "2.11.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",