@bitkyc08/opencodex
Version:
Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code
524 lines (491 loc) • 29.6 kB
text/typescript
import { handleCodexAccountPreservation } from "./account-preservation";
import { persistCodexAccountSelection } from "./account-selection";
import { CODEX_ACCOUNT_LOG_LABEL_RE, codexAccountLogLabel } from "../account-label";
import { handleUseRemainingQuotaRoute } from "./use-remaining-route";
import { poolQuotaHistoryIdentity, readCodexAccountRecord } from "../account-store";
import { estimateCodexQuotaCapacity, insufficientCodexCapacity } from "../quota-capacity";
import type { CodexCapacityResult } from "../quota-capacity";
import { readUsageSnapshotForManagement } from "../../usage/log";
import { getAccountQuotaHistory, listAccountQuotas } from "../quota";
import { deleteCodexAccount } from "../account-lifecycle";
import { isCodexAccountPaused, setCodexAccountPaused } from "../account-pause";
import { setAllCodexAccountsCreditsAfterLimit, setCodexAccountCreditsAfterLimit } from "../account-credit-use";
import { clearCodexAccountPin, isCodexAccountPriorityKey, pinnedCodexAccountId, setCodexAccountPriority } from "../account-priority";
import { codexQuotaScopeForModel, clearCodexAccountCooldown, clearThreadAccountMapForAccount, getEffectiveActiveCodexAccountId, isEffectiveCodexAccountPinned } from "../routing";
import { DEFAULT_ACCOUNT_PRIORITY, MAX_ACCOUNT_PRIORITY, MIN_ACCOUNT_PRIORITY, normalizeAccountPoolStickyLimit, normalizeCodexAccountPoolStrategy, parseAccountPoolStickyLimit, parseCodexAccountPoolStrategy, parseAccountPriority } from "../pool-rotation";
import { MAIN_CODEX_ACCOUNT_ID } from "../main-account";
import { reconcileLiveStateStores } from "../../lib/state-store-registrations";
import type { OcxConfig } from "../../types";
import { CODEX_ACCOUNT_ID_RE, isSelectableCodexPoolAccount, isValidCodexAccountId } from "../account-id";
import { isCodexResetCreditOperationId } from "../reset-credit-recovery";
import { listCodexAuthAccounts, selectFallbackAfterPause, pauseExhaustedCodexAccounts } from "./account-list";
import { jsonResponse, manualImportDisabledResponse } from "./http";
import { convergeAccountNamespaceCatalog, handleCodexAuthLoginStart, handleCodexAuthLoginCode, handleCodexAuthLoginCancel, handleCodexAuthLoginStatus } from "./login-flow";
import type { CodexAuthCatalogConvergence } from "./login-flow";
import { PoolQuotaProbeBusyError } from "./pool-quota-probe";
import { inspectResetCredits, consumeResetCredits } from "./reset-credit-service";
import { getRuntimeConfig, saveRuntimeConfig, configuredPoolAccount } from "./runtime-config";
import { captureConfigTopLevelRollback } from "../../config/rebase-provenance";
import { getEffectiveCodexAutoSwitchThreshold, isCodexAccountAutoSwitchThresholdKey, parseCodexAutoSwitchThreshold, setCodexAccountAutoSwitchThresholdOverride } from "../account-auto-switch";
import { withCodexAccountPauseGroup } from "./account-pause-group";
import { PERMANENT_PAUSE_REASONS } from "../../oauth/account-resume-health";
export async function handleCodexAuthAPI(
req: Request,
url: URL,
config: OcxConfig,
convergeCodexCatalog?: CodexAuthCatalogConvergence,
principal?: import("../../server/management-auth").ManagementPrincipal,
): Promise<Response | null> {
if (url.pathname === "/api/codex-auth/preserve-signed-in" && (req.method === "GET" || req.method === "PUT")) {
return handleCodexAccountPreservation(req, config);
}
if (url.pathname === "/api/codex-auth/accounts/use-remaining" && (req.method === "GET" || req.method === "PUT")) {
return handleUseRemainingQuotaRoute(req, url, config);
}
if (url.pathname === "/api/codex-auth/accounts" && req.method === "GET") {
const forceRefresh = url.searchParams.get("refresh") === "1" || url.searchParams.get("refresh") === "true";
return jsonResponse({ accounts: await listCodexAuthAccounts(config, forceRefresh) });
}
if (url.pathname === "/api/codex-auth/accounts/refresh" && req.method === "POST") {
// Inference spends quota: only a dashboard session carries the consent
// required by AGENTS_INSTALL.md. Raw-admin/CLI refreshes remain observational.
return jsonResponse({ accounts: await listCodexAuthAccounts(config, true, {
validatePending: principal === "gui-session",
explicitRefresh: true,
}) });
}
if (url.pathname === "/api/codex-auth/accounts" && req.method === "POST") {
return manualImportDisabledResponse();
}
if (url.pathname === "/api/codex-auth/accounts" && req.method === "DELETE") {
const id = url.searchParams.get("id");
if (!id) return jsonResponse({ error: "Missing id" }, 400);
const runtimeConfig = getRuntimeConfig(config);
const isLegacyPoolAccount = CODEX_ACCOUNT_ID_RE.test(id)
&& (runtimeConfig.codexAccounts ?? []).some(account => !account.isMain && account.id === id);
if (!isValidCodexAccountId(id) && !isLegacyPoolAccount) {
return jsonResponse({ error: "Invalid account id format" }, 400);
}
const pickerVisibilityChanged = deleteCodexAccount(runtimeConfig, id);
saveRuntimeConfig(config, runtimeConfig);
reconcileLiveStateStores();
const catalogRefresh = await convergeAccountNamespaceCatalog(
runtimeConfig,
pickerVisibilityChanged,
convergeCodexCatalog,
);
return jsonResponse({ ok: true, ...catalogRefresh });
}
if (url.pathname === "/api/codex-auth/accounts/alias" && req.method === "PUT") {
const body = await req.json().catch(() => ({})) as { id?: unknown; alias?: unknown };
const id = typeof body.id === "string" ? body.id.trim() : "";
const alias = typeof body.alias === "string" ? body.alias.trim() : "";
if (id === MAIN_CODEX_ACCOUNT_ID) return jsonResponse({ error: "Main Codex account alias is not configurable" }, 400);
if (!isValidCodexAccountId(id)) return jsonResponse({ error: "Invalid account id format" }, 400);
if (typeof body.alias !== "string" || alias.length > 80 || /[\x00-\x1f\x7f]/.test(alias)) {
return jsonResponse({ error: "Alias must be a string of at most 80 printable characters" }, 400);
}
const runtimeConfig = getRuntimeConfig(config);
const account = (runtimeConfig.codexAccounts ?? []).find(candidate => candidate.id === id && !candidate.isMain);
if (!account) return jsonResponse({ error: "Account not found" }, 404);
if (alias) account.alias = alias;
else delete account.alias;
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({ ok: true, id, alias: alias || null });
}
if (url.pathname === "/api/codex-auth/accounts/pause" && req.method === "PUT") {
const body = await req.json().catch(() => ({})) as { id?: unknown; paused?: unknown; ifHealthy?: unknown };
const id = typeof body.id === "string" ? body.id.trim() : "";
if (id !== MAIN_CODEX_ACCOUNT_ID && !isValidCodexAccountId(id)) {
return jsonResponse({ error: "Invalid account id format" }, 400);
}
if (typeof body.paused !== "boolean") return jsonResponse({ error: "paused must be a boolean" }, 400);
if (body.ifHealthy !== undefined && typeof body.ifHealthy !== "boolean") return jsonResponse({ error: "ifHealthy must be a boolean" }, 400);
if (body.ifHealthy === true && body.paused !== false) return jsonResponse({ error: "ifHealthy applies only to a resume (paused: false)" }, 400);
let runtimeConfig = getRuntimeConfig(config);
const exists = id === MAIN_CODEX_ACCOUNT_ID
|| (runtimeConfig.codexAccounts ?? []).some(account => isSelectableCodexPoolAccount(account) && account.id === id);
if (!exists) return jsonResponse({ error: "Account not found" }, 404);
let healthPauseRevision: string | undefined;
if (body.ifHealthy === true) {
// Explicit resume gated on a forced pool quota read (#8398); the read can take a while, so
// the pause is committed against the configuration as it is after the read.
const { checkCodexResumeHealth } = await import("../account-resume-health");
const verdict = await checkCodexResumeHealth(runtimeConfig, id);
runtimeConfig = getRuntimeConfig(config);
const refusal = verdict && !verdict.ok ? verdict.reason
: verdict && (!isCodexAccountPaused(runtimeConfig, id) || runtimeConfig.codexAccountPauseState?.[id]?.revision !== verdict.pauseRevision)
? "account_changed" : undefined;
if (refusal) return jsonResponse({ error: `account was not resumed: ${refusal}`, code: "resume_refused", reason: refusal, id }, 409);
// Not paused when checked: a no-op that never enters the pause group.
if (!verdict) return jsonResponse({ ok: true, id, paused: false, unchanged: true, activeCodexAccountId: getEffectiveActiveCodexAccountId(runtimeConfig) ?? null });
if (verdict.ok) healthPauseRevision = verdict.pauseRevision;
}
const paused = body.paused;
return withCodexAccountPauseGroup(runtimeConfig, id, accountIds => {
if (body.ifHealthy === true) {
// Recheck right before the write: the pause-group claim above can wait, and an operator
// re-pause meanwhile must not be overwritten by this health-gated resume.
runtimeConfig = getRuntimeConfig(config);
if (!isCodexAccountPaused(runtimeConfig, id) || runtimeConfig.codexAccountPauseState?.[id]?.revision !== healthPauseRevision) {
return jsonResponse({ error: "account was not resumed: account_changed", code: "resume_refused", reason: "account_changed", id }, 409);
}
}
// A linked account resumes with the named one, so a permanent reason on any of them refuses (#8398).
if (body.ifHealthy === true && accountIds.some(accountId => {
const reason = runtimeConfig.codexAccountPauseState?.[accountId]?.details?.reason;
return reason !== undefined && PERMANENT_PAUSE_REASONS.has(reason);
})) {
return jsonResponse({ error: "account was not resumed: pause_not_remote_clearable", code: "resume_refused", reason: "pause_not_remote_clearable", id }, 409);
}
// Publish every exclusion before reconciling: a duplicate must never become the fallback.
for (const accountId of accountIds) setCodexAccountPaused(runtimeConfig, accountId, paused);
if (paused) {
for (const accountId of accountIds) clearThreadAccountMapForAccount(accountId);
for (const accountId of accountIds) selectFallbackAfterPause(runtimeConfig, accountId);
}
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({
ok: true,
id,
paused,
affectedAccountIds: accountIds,
activeCodexAccountId: getEffectiveActiveCodexAccountId(runtimeConfig) ?? null,
appliesImmediately: true,
});
});
}
// Pool accounts and `__main__`, like pause. Spending is opt-in: an account not on the list is
// skipped by the next selection while a usage window is full, and a bound thread is released
// through the usual block reason, so nothing is cleared here. `{ all }` is the dashboard's
// global switch: on lists every current account, off clears the list.
if (url.pathname === "/api/codex-auth/accounts/credits" && req.method === "PUT") {
const parsed: unknown = await req.json().catch(() => null);
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
return jsonResponse({ error: "body must be an object" }, 400);
}
const body = parsed as { id?: unknown; creditsAfterLimit?: unknown; all?: unknown };
const runtimeConfig = getRuntimeConfig(config);
if (body.all !== undefined) {
if (typeof body.all !== "boolean") return jsonResponse({ error: "all must be a boolean" }, 400);
const ids = [MAIN_CODEX_ACCOUNT_ID, ...(runtimeConfig.codexAccounts ?? [])
.filter(isSelectableCodexPoolAccount)
.map(account => account.id)];
setAllCodexAccountsCreditsAfterLimit(runtimeConfig, ids, body.all);
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({ ok: true, all: body.all, ids: runtimeConfig.creditCodexAccountIds ?? [] });
}
const id = typeof body.id === "string" ? body.id.trim() : "";
if (id !== MAIN_CODEX_ACCOUNT_ID && !isValidCodexAccountId(id)) return jsonResponse({ error: "Invalid account id format" }, 400);
if (typeof body.creditsAfterLimit !== "boolean") return jsonResponse({ error: "creditsAfterLimit must be a boolean" }, 400);
const exists = id === MAIN_CODEX_ACCOUNT_ID
|| (runtimeConfig.codexAccounts ?? []).some(account => isSelectableCodexPoolAccount(account) && account.id === id);
if (!exists) return jsonResponse({ error: "Account not found" }, 404);
setCodexAccountCreditsAfterLimit(runtimeConfig, id, body.creditsAfterLimit);
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({ ok: true, id, creditsAfterLimit: body.creditsAfterLimit });
}
// Deliberately a route of its own rather than a field on the alias PATCH: aliases
// are display-only and reject __main__, while selection order is routing metadata
// that the Desktop account must be able to carry. Re-ordering never kicks a live
// thread, so there is no affinity clearing and no appliesImmediately here.
if (url.pathname === "/api/codex-auth/accounts/priority" && req.method === "PUT") {
let parsedBody: unknown;
try { parsedBody = await req.json(); } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
if (typeof parsedBody !== "object" || parsedBody === null || Array.isArray(parsedBody)) {
return jsonResponse({ error: "body must be an object" }, 400);
}
const body = parsedBody as { id?: unknown; priority?: unknown };
const id = typeof body.id === "string" ? body.id.trim() : "";
if (!isCodexAccountPriorityKey(id)) {
return jsonResponse({ error: "Invalid account id format" }, 400);
}
let priority = DEFAULT_ACCOUNT_PRIORITY;
if (body.priority !== null) {
const parsed = parseAccountPriority(body.priority);
if (parsed === null) {
return jsonResponse({
error: `priority must be null or an integer ${MIN_ACCOUNT_PRIORITY}-${MAX_ACCOUNT_PRIORITY}`,
}, 400);
}
priority = parsed;
}
const runtimeConfig = getRuntimeConfig(config);
const exists = id === MAIN_CODEX_ACCOUNT_ID
|| (runtimeConfig.codexAccounts ?? []).some(account => isSelectableCodexPoolAccount(account) && account.id === id);
if (!exists) return jsonResponse({ error: "Account not found" }, 404);
setCodexAccountPriority(runtimeConfig, id, priority);
// Both a pin and an order are the operator saying which account to use, so the newer
// statement wins. Without this a pin made before any order existed — an ordinary
// account switch — would outrank the order forever: it blocks preemption and caps
// every eligibility list at its own tier until that account drains or is paused.
clearCodexAccountPin(runtimeConfig);
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({
ok: true,
id,
priority,
activeCodexAccountId: getEffectiveActiveCodexAccountId(runtimeConfig) ?? null,
});
}
if (url.pathname === "/api/codex-auth/accounts/pause-exhausted" && req.method === "PUT") {
const runtimeConfig = getRuntimeConfig(config);
const result = await pauseExhaustedCodexAccounts(
runtimeConfig,
() => saveRuntimeConfig(config, runtimeConfig),
);
const { pausedAccountIds, checkedAccountCount, failedAccountCount } = result;
if (checkedAccountCount === 0 && failedAccountCount > 0) {
return jsonResponse({
ok: false,
error: "Failed to refresh any Codex account quota",
checkedAccountCount,
failedAccountCount,
}, 502);
}
return jsonResponse({
ok: true,
pausedAccountIds,
pausedCount: pausedAccountIds.length,
checkedAccountCount,
failedAccountCount,
complete: failedAccountCount === 0,
activeCodexAccountId: getEffectiveActiveCodexAccountId(runtimeConfig) ?? null,
appliesImmediately: true,
});
}
// Manual escape from a quota cooldown. Injected Codex routing makes this proxy the only
// model path for Codex Desktop, so a cooldown that outlives the real upstream limit
// otherwise leaves editing config.toml as the user's only recovery.
//
// Existence is deliberately NOT disclosed: an unknown id returns 200 with cleared:false
// exactly like an account that simply had no live cooldown, so this route cannot be used
// to enumerate configured accounts. Cooldown state is runtime-only and independent of the
// account list, so 404 would carry no useful meaning anyway.
if (url.pathname === "/api/codex-auth/accounts/clear-cooldown" && req.method === "POST") {
const body = await req.json().catch(() => ({})) as { id?: unknown };
const id = typeof body.id === "string" ? body.id.trim() : "";
if (id !== MAIN_CODEX_ACCOUNT_ID && !isValidCodexAccountId(id)) {
return jsonResponse({ error: "Invalid account id format" }, 400);
}
return jsonResponse({ ok: true, id, cleared: clearCodexAccountCooldown(id) });
}
if (url.pathname === "/api/codex-auth/active" && req.method === "PUT") {
let body: unknown;
try { body = await req.json(); } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
if (!body || typeof body !== "object" || Array.isArray(body)
|| !("accountId" in body) || (body.accountId !== null && typeof body.accountId !== "string")) {
return jsonResponse({ error: "accountId must be an account id or null" }, 400);
}
const expectedPin = "expectedPinnedAccountId" in body ? body.expectedPinnedAccountId : undefined;
if (expectedPin !== undefined && (body.accountId !== null || !isCodexAccountPriorityKey(expectedPin))) {
return jsonResponse({ error: "expectedPinnedAccountId must be a valid account id for a release" }, 400);
}
return persistCodexAccountSelection(getRuntimeConfig(config), body.accountId, expectedPin as string | undefined);
}
if (url.pathname === "/api/codex-auth/active" && req.method === "GET") {
const runtimeConfig = getRuntimeConfig(config);
return jsonResponse({
activeCodexAccountId: getEffectiveActiveCodexAccountId(runtimeConfig) ?? null,
pinned: isEffectiveCodexAccountPinned(runtimeConfig),
// Which account carries the pin, not just whether the active one does. Under
// round-robin or fill-first the pin caps the tier ceiling at its own tier while the
// strategy cursor moves freely inside that tier, so `pinned` alone goes false on a
// sibling's turn even though the pin is still suppressing every higher tier. The id
// lets a surface mark the account the operator actually chose.
pinnedAccountId: pinnedCodexAccountId(runtimeConfig) ?? null,
autoSwitchThreshold: runtimeConfig.autoSwitchThreshold ?? 80,
upstreamFailoverThreshold: runtimeConfig.upstreamFailoverThreshold ?? 3,
accountPoolStrategy: normalizeCodexAccountPoolStrategy(runtimeConfig.accountPoolStrategy),
accountPoolStickyLimit: normalizeAccountPoolStickyLimit(runtimeConfig.accountPoolStickyLimit),
});
}
if (url.pathname === "/api/codex-auth/auto-switch" && req.method === "PUT") {
let parsedBody: unknown;
try { parsedBody = await req.json(); } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
if (typeof parsedBody !== "object" || parsedBody === null || Array.isArray(parsedBody)) {
return jsonResponse({ error: "body must be an object" }, 400);
}
const body = parsedBody as { id?: unknown; threshold?: unknown };
const runtimeConfig = getRuntimeConfig(config);
if (Object.hasOwn(body, "id")) {
if (!isCodexAccountAutoSwitchThresholdKey(body.id)) {
return jsonResponse({ error: "id must be a Codex account id" }, 400);
}
const threshold = body.threshold === null ? null : parseCodexAutoSwitchThreshold(body.threshold);
if (body.threshold !== null && threshold === null) {
return jsonResponse({ error: "threshold must be null or an integer 0-100" }, 400);
}
if (body.id !== MAIN_CODEX_ACCOUNT_ID && !configuredPoolAccount(runtimeConfig, body.id)) {
return jsonResponse({ error: "Codex account not found" }, 404);
}
const rollback = captureConfigTopLevelRollback(runtimeConfig, ["codexAccountAutoSwitchThresholds"]);
try {
// Inheritance resets delete children in place; keep the previous map intact for rollback.
if (runtimeConfig.codexAccountAutoSwitchThresholds) {
runtimeConfig.codexAccountAutoSwitchThresholds = { ...runtimeConfig.codexAccountAutoSwitchThresholds };
}
setCodexAccountAutoSwitchThresholdOverride(runtimeConfig, body.id, threshold);
saveRuntimeConfig(config, runtimeConfig);
} catch (error) {
rollback();
throw error;
}
return jsonResponse({
ok: true,
id: body.id,
autoSwitchThresholdOverride: threshold,
autoSwitchThreshold: getEffectiveCodexAutoSwitchThreshold(runtimeConfig, body.id),
});
}
if (typeof body.threshold !== "number" || !Number.isInteger(body.threshold) || body.threshold < 0 || body.threshold > 100) {
return jsonResponse({ error: "Threshold must be an integer 0-100" }, 400);
}
runtimeConfig.autoSwitchThreshold = body.threshold;
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({ ok: true });
}
if (
url.pathname === "/api/codex-auth/pool-strategy"
&& (req.method === "PUT" || req.method === "PATCH")
) {
let parsedBody: unknown;
try { parsedBody = await req.json(); } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
if (typeof parsedBody !== "object" || parsedBody === null || Array.isArray(parsedBody)) {
return jsonResponse({ error: "body must be an object" }, 400);
}
const body = parsedBody as { strategy?: unknown; stickyLimit?: unknown };
if (body.strategy === undefined && body.stickyLimit === undefined) {
return jsonResponse({ error: "strategy or stickyLimit required" }, 400);
}
const runtimeConfig = getRuntimeConfig(config);
let nextStrategy: NonNullable<ReturnType<typeof parseCodexAccountPoolStrategy>> | undefined;
let nextSticky: NonNullable<ReturnType<typeof parseAccountPoolStickyLimit>> | undefined;
if (body.strategy !== undefined) {
const parsed = parseCodexAccountPoolStrategy(body.strategy);
if (parsed === null) {
return jsonResponse({ error: 'strategy must be one of: quota, round-robin, fill-first, reset-first, reset-near-weighted' }, 400);
}
nextStrategy = parsed;
}
if (body.stickyLimit !== undefined) {
const parsed = parseAccountPoolStickyLimit(body.stickyLimit);
if (parsed === null) {
return jsonResponse({ error: "stickyLimit must be an integer 1-100" }, 400);
}
nextSticky = parsed;
}
if (nextStrategy !== undefined) runtimeConfig.accountPoolStrategy = nextStrategy;
if (nextSticky !== undefined) runtimeConfig.accountPoolStickyLimit = nextSticky;
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({
ok: true,
accountPoolStrategy: normalizeCodexAccountPoolStrategy(runtimeConfig.accountPoolStrategy),
accountPoolStickyLimit: normalizeAccountPoolStickyLimit(runtimeConfig.accountPoolStickyLimit),
});
}
if (url.pathname === "/api/codex-auth/failover" && req.method === "PUT") {
let body: { threshold: number };
try { body = (await req.json()) as typeof body; } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
if (typeof body.threshold !== "number" || !Number.isInteger(body.threshold) || body.threshold < 0 || body.threshold > 20) {
return jsonResponse({ error: "Threshold must be an integer 0-20" }, 400);
}
const runtimeConfig = getRuntimeConfig(config);
runtimeConfig.upstreamFailoverThreshold = body.threshold;
saveRuntimeConfig(config, runtimeConfig);
return jsonResponse({ ok: true });
}
if (url.pathname === "/api/codex-auth/quota/history" && req.method === "GET") {
const accountId = url.searchParams.get("accountId");
const rawLimit = url.searchParams.get("limit");
if (url.searchParams.getAll("accountId").length !== 1 || !isValidCodexAccountId(accountId)
|| url.searchParams.getAll("limit").length > 1
|| [...url.searchParams.keys()].some(key => key !== "accountId" && key !== "limit")
|| (rawLimit !== null && !/^(?:[1-9]|[1-9][0-9]|1[0-9]{2}|200)$/.test(rawLimit))) {
return jsonResponse({ error: "A stored pool accountId and optional limit from 1 to 200 are required" }, 400);
}
const runtimeConfig = getRuntimeConfig(config);
const account = configuredPoolAccount(runtimeConfig, accountId);
if (!account) return jsonResponse({ error: "Unknown pool account" }, 404);
const identity = poolQuotaHistoryIdentity(accountId);
const allHistory = getAccountQuotaHistory(accountId);
const limit = rawLimit === null ? 200 : Number(rawLimit);
const history = { ...allHistory, observations: allHistory.observations.slice(-limit), truncated: allHistory.observations.length > limit };
const label = account.logLabel;
const labelStillUnique = () => {
const current = getRuntimeConfig(config);
return configuredPoolAccount(current, accountId)?.logLabel === label
&& current.codexAccounts?.filter(row => codexAccountLogLabel(row) === label).length === 1;
};
let capacity: CodexCapacityResult = insufficientCodexCapacity("identity_unavailable");
if (identity && identity === poolQuotaHistoryIdentity(accountId) && label && CODEX_ACCOUNT_LOG_LABEL_RE.test(label) && labelStillUnique()) {
try {
const usage = await readUsageSnapshotForManagement();
if (poolQuotaHistoryIdentity(accountId) !== identity || !labelStillUnique()) capacity = insufficientCodexCapacity("identity_changed");
else if (!usage.revision) capacity = insufficientCodexCapacity("ledger_unavailable");
else if (usage.truncatedPrefixBytes > 0 || usage.entriesTruncated || usage.entriesDropped > 0) capacity = insufficientCodexCapacity("ledger_truncated");
else capacity = estimateCodexQuotaCapacity(allHistory.observations, usage.entries, label,
model => codexQuotaScopeForModel(model) === "shared");
} catch { capacity = insufficientCodexCapacity("ledger_unavailable"); }
}
if (!configuredPoolAccount(getRuntimeConfig(config), accountId)) return jsonResponse({ error: "Unknown pool account" }, 404);
if (identity !== poolQuotaHistoryIdentity(accountId) || (identity && label && !labelStillUnique())) {
return jsonResponse({ accountId, ...getAccountQuotaHistory(accountId, limit), capacity: insufficientCodexCapacity("identity_changed") });
}
return jsonResponse({ accountId, ...history, capacity });
}
if (url.pathname === "/api/codex-auth/quota" && req.method === "GET") {
const quotas: Record<string, unknown> = {};
for (const [id, q] of listAccountQuotas()) quotas[id] = q;
return jsonResponse({ quotas });
}
if (url.pathname === "/api/codex-auth/reset-credits" && req.method === "GET") {
const accountId = url.searchParams.get("accountId");
if (!accountId) return jsonResponse({ error: "accountId required" }, 400);
try {
return await inspectResetCredits(config, accountId, req.signal);
} catch (e) {
return jsonResponse({ error: e instanceof Error ? e.message : "Reset credit lookup failed" }, 500);
}
}
if (url.pathname === "/api/codex-auth/reset-credits/consume" && req.method === "POST") {
const body = (await req.json().catch(() => ({}))) as {
accountId?: string;
operationId?: unknown;
};
if (!body.accountId) return jsonResponse({ error: "accountId required" }, 400);
const accountId = body.accountId;
// Optional caller-owned idempotency identity (#3375 axis D). Absent => legacy
// behavior: a fresh random redeem_request_id and no durable ledger row.
// The ledger throws TypeError on a malformed id, so the format check has to
// happen here rather than at the call site, or it surfaces as a 500.
const hasOperationId = body.operationId !== undefined;
if (hasOperationId && !isCodexResetCreditOperationId(body.operationId)) {
return jsonResponse({ error: "Invalid operationId format" }, 400);
}
const requestedOperationId = hasOperationId ? body.operationId as string : undefined;
try {
return await consumeResetCredits(config, accountId, requestedOperationId);
} catch (e) {
if (e instanceof PoolQuotaProbeBusyError) {
const response = jsonResponse({ error: "server_busy", code: "server_busy" }, 503);
response.headers.set("Retry-After", "1");
return response;
}
return jsonResponse({ error: e instanceof Error ? e.message : "Reset credit consume failed" }, 500);
}
}
if (url.pathname === "/api/codex-auth/login" && req.method === "POST") {
return handleCodexAuthLoginStart(req, config, convergeCodexCatalog);
}
if (url.pathname === "/api/codex-auth/login/code" && req.method === "POST") {
return handleCodexAuthLoginCode(req);
}
if (url.pathname === "/api/codex-auth/login/cancel" && req.method === "POST") {
return handleCodexAuthLoginCancel(req);
}
if (url.pathname === "/api/codex-auth/login-status" && req.method === "GET") {
return handleCodexAuthLoginStatus(req, url, config);
}
return null;
}