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
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
/**
* Shared refresh-policy helpers for Token Health Check + CredentialHealth.
*
* Kept tiny and dependency-free on purpose: CredentialHealth's sweep needs to
* honor the OAuth refresh backoff window, but importing `tokenHealthCheck`
* would pull in its module-level scheduler (which auto-starts timers).
*
* Moved verbatim out of `src/lib/tokenHealthCheck.ts`, which re-exports it for
* existing callers and tests.
*/
export function isInRefreshBackoff(conn: any, nowMs: number): boolean {
const until = conn?.providerSpecificData?.refreshCircuit?.until;
if (typeof until !== "string") return false;
const untilMs = new Date(until).getTime();
return Number.isFinite(untilMs) && untilMs > nowMs;
}
/**
* Rotating-refresh providers whose refresh token must survive an "unrecoverable"
* refresh error instead of being nulled on the first failure.
*
* #11414 keeps the connection active for EXPIRED_RETRY_MAX retries, but the same
* update also nulled the refresh token for every rotating provider. The next sweep
* then hits the `!conn.refreshToken` guard, whose self-heal branch only fires while
* testStatus is empty or "active" — the row is already "expired", so the sweep
* returns silently and the retry budget is never spent. The connection stays active,
* expired and unrecoverable until a manual re-auth (#13183).
*
* Claude access tokens are short-lived (~8h) and an invalid_grant /
* refresh_token_reused is frequently a dual-consumer race (the same Claude Max
* account refreshed by another OAuth client), not a confirmed revoke — so the token
* is worth keeping for the retries. Same reasoning as #3679 for non-rotating
* providers: the stored refresh token is the user's only recovery artifact.
*/
// #16984: a Kimi Coding invalid_grant can be a provider-side misclassification;
// keep the stored credential available to the existing bounded retry path.
// Codex/OpenAI one-time-use refresh tokens are deliberately excluded.
const PRESERVE_REFRESH_TOKEN_PROVIDERS = new Set(["claude", "kimi-coding"]);
export function preservesRefreshTokenOnUnrecoverable(provider: unknown): boolean {
return PRESERVE_REFRESH_TOKEN_PROVIDERS.has(String(provider || "").toLowerCase());
}