Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

5.3 KB
Raw Blame
// #6863: combo path model lockout must prefer a parsed reset over the base
// cooldown ladder while still respecting the operator's max for body prose.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-combo-quota-reset-6863-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = "test-combo-quota-reset-6863";

const core = await import("../../src/lib/db/core.ts");
const { handleComboChat } = await import("../../open-sse/services/combo.ts");
const { getModelLockoutInfo, clearAllModelLockouts, parseRetryFromErrorText } =
  await import("../../open-sse/services/accountFallback.ts");

const UPSTREAM_429_MESSAGE =
  "429: Individual quota reached. Please upgrade your subscription to increase your limits. Resets in 92h27m28s.";

function createLog() {
  return { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} };
}

test.beforeEach(() => {
  clearAllModelLockouts();
});

test.after(() => {
  clearAllModelLockouts();
  try {
    core.resetDbInstance();
    fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  } catch {}
});

test("combo 429 body reset beats base cooldown but is capped by maxCooldownMs (#6863)", async () => {
  const provider = "antigravity"; // OAuth category → quota signals preserved on 429
  const model = "claude-sonnet-4.6";

  const settings = {
    modelLockout: {
      enabled: true,
      errorCodes: [429],
      baseCooldownMs: 3000,
      maxCooldownMs: 1_800_000,
      maxBackoffSteps: 10,
      useExponentialBackoff: true,
    },
  };

  await handleComboChat({
    body: {},
    combo: {
      name: "quota-reset-combo",
      strategy: "priority",
      models: [`${provider}/${model}`],
      config: { maxRetries: 0, retryDelayMs: 0, fallbackDelayMs: 0 },
    },
    handleSingleModel: async () =>
      new Response(JSON.stringify({ error: { message: UPSTREAM_429_MESSAGE } }), {
        status: 429,
        headers: { "content-type": "application/json" },
      }),
    isModelAvailable: async () => true,
    log: createLog(),
    settings,
    allCombos: null,
  });

  const parsedResetMs = parseRetryFromErrorText(UPSTREAM_429_MESSAGE);
  assert.ok(
    parsedResetMs && parsedResetMs > 90 * 3600 * 1000,
    `sanity: reset text must parse to ~92.5h, got ${parsedResetMs}`
  );

  const info = getModelLockoutInfo(provider, "", model);
  assert.ok(info, "combo 429 must record a model lockout");
  // Preserve #6863 (do not fall back to ~seconds), but prose is not an
  // authoritative reset and must not bypass the operator's 30m maximum.
  assert.ok(
    info!.remainingMs > settings.modelLockout.maxCooldownMs - 5_000 &&
      info!.remainingMs <= settings.modelLockout.maxCooldownMs,
    `body reset must clamp to maxCooldownMs (${settings.modelLockout.maxCooldownMs}ms); got ${info!.remainingMs}ms`
  );
});

test("combo 429 lockout prefers a SHORT parsed reset over the subscription fallback cooldown", async () => {
  // Review follow-up on #6863: the subscription-quota branch returns
  // cooldownMs = 1h fallback when useUpstreamRetryHints is off (OAuth default),
  // while quotaResetHintMs carries the real parsed reset. A max() of the two
  // would over-lock (1h) — the lockout must follow the parsed value (~45m),
  // matching the single-model path in src/sse/services/auth.ts.
  //
  // Native `claude` is deliberately NOT used here: since #13888 an explicit Claude
  // quota 429 without fresh per-model quota evidence is CONNECTION-scoped (the
  // connection is exhausted, no model lockout — covered by
  // tests/unit/claude-scoped-quota-auth.test.ts). Codex is another OAuth provider
  // whose subscription "usage limit reached" 429 still takes this branch.
  const provider = "codex"; // OAuth category → subscription-quota branch applies
  const model = "gpt-5.3-codex";
  const shortResetMessage = "429: Usage limit reached. Your usage limit resets in 45m0s.";

  const settings = {
    modelLockout: {
      enabled: true,
      errorCodes: [429],
      baseCooldownMs: 3000,
      maxCooldownMs: 7_200_000,
      maxBackoffSteps: 10,
      useExponentialBackoff: true,
    },
  };

  await handleComboChat({
    body: {},
    combo: {
      name: "short-reset-combo",
      strategy: "priority",
      models: [`${provider}/${model}`],
      config: { maxRetries: 0, retryDelayMs: 0, fallbackDelayMs: 0 },
    },
    handleSingleModel: async () =>
      new Response(JSON.stringify({ error: { message: shortResetMessage } }), {
        status: 429,
        headers: { "content-type": "application/json" },
      }),
    isModelAvailable: async () => true,
    log: createLog(),
    settings,
    allCombos: null,
  });

  const parsedResetMs = parseRetryFromErrorText(shortResetMessage);
  assert.equal(parsedResetMs, 45 * 60 * 1000, "sanity: reset text must parse to 45m");

  const info = getModelLockoutInfo(provider, "", model);
  assert.ok(info, "combo 429 must record a model lockout");
  // Must be the parsed 45m — NOT the 1h subscription fallback (over-lock).
  assert.ok(
    info!.remainingMs > parsedResetMs! - 5_000 && info!.remainingMs <= parsedResetMs!,
    `lockout must follow the parsed 45m reset, not the 1h fallback; got ${info!.remainingMs}ms (~${Math.round(info!.remainingMs / 1000)}s)`
  );
});