@bitkyc08/opencodex
Version:
Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code
139 lines (133 loc) • 7 kB
text/typescript
import { readCodexAccountRecord } from "./account-store";
import { isAccountNeedsReauth } from "./account-runtime-state";
import {
MAIN_CODEX_ACCOUNT_ID,
getMainAccountCredentialStatus,
isMainAccountTokenLive,
} from "./main-account";
import { hasLegacyMainCodexPoolAccount, isSelectableCodexPoolAccount } from "./account-id";
import type { OcxConfig } from "../types";
import { isNativeMainTrafficBlocked } from "./native-profile-startup";
import { isMainAccountHardLocked } from "./main-account-hard-lock";
import { configuredCodexMainDuplicate } from "./collision-identity";
export interface CodexAccountUsabilityOptions {
/** Route using cached runtime state only; the caller must reject selected main before auth. */
nativeMainSelectionOnly?: boolean;
/** Liveness override; ownership/read fences independently control physical credential reads. */
isMainAccountTokenLive?: typeof isMainAccountTokenLive;
/** Confirmed account ids for an account-gated model; omitted for ordinary native models. */
modelEligibleAccountIds?: ReadonlySet<string>;
/**
* Accounts whose own confirmed roster definitively omits the requested model (#4768).
*
* Deliberately NOT read by this module. `modelEligibleAccountIds` is an eligibility boundary and
* produces `model_not_entitled`; this is an ORDERING preference applied once, in
* `getEligiblePoolAccounts`, and dropped whenever honouring it would leave no candidate. Reading
* it here would turn a preference into a refusal and re-create the fail-closed behaviour the
* flagships were deliberately taken out of.
*/
deniedModelAccountIds?: ReadonlySet<string>;
/**
* This request's conversation carries live uploaded-file references (#4778).
*
* Also not read by this module, and for the same reason: it is a retention preference, never an
* eligibility boundary. Uploaded files are scoped to the account that issued them, so moving
* such a conversation orphans the reference and every later turn is refused with
* `409 account_change_file_scope` -- the reference stays in history, so the conversation is
* effectively dead. Retention makes that refusal rarer; it can never replace it, because an
* account can always become unable to serve.
*/
retainAccountForUploadedFiles?: boolean;
/**
* Main is live for this request only because the request carries its own main bearer (#5019).
*
* Main may then win this request's selection, but that credential belongs to the request, not
* to the Pool: selecting it must never be written back as the shared active account. Persisting
* it made every later request -- including one whose caller identity is unreadable, or one that
* owns the cooled subscription -- follow a main the operator never chose.
*/
requestOwnedMainCredential?: boolean;
/**
* The caller named this account explicitly (an account-qualified route). Explicit selectors are
* an operator choice outside the pool-only policy; every automatic path omits this, so a path
* that forgets it still keeps native main out of Pool rotation.
*/
explicitAccountSelector?: boolean;
}
/**
* Why an account was refused, in the order the checks run. This is the attribution half of
* selection: an operator whose model quietly disappeared needs to know that one account fell out
* and why, not merely that the pool got smaller (#4212).
*/
export type CodexAccountUnusableReason =
| "model_not_entitled"
| "pool_only"
| "main_hard_locked"
| "main_traffic_blocked"
| "legacy_pool_sentinel"
| "needs_reauth"
| "main_credential_unavailable"
| "not_in_pool"
| "missing_credential"
| "deleted"
| "validation_pending"
| "duplicate_main";
/** `codexPool.poolOnly` (#7410): automatic Pool resolution never serves native main. */
export function codexPoolOnlyExcludesMain(config: Pick<OcxConfig, "codexPool"> | undefined): boolean {
return config?.codexPool?.poolOnly === true;
}
/**
* The single source of truth for both selection and its explanation. `isCodexAccountUsable()` is
* this function's boolean projection rather than a parallel copy of the same branches, so a reason
* can never claim an account is fine while routing drops it, or name a cause routing did not use.
*/
export function codexAccountUnusableReason(
config: OcxConfig,
accountId: string,
options: CodexAccountUsabilityOptions = {},
): CodexAccountUnusableReason | undefined {
if (options.modelEligibleAccountIds && !options.modelEligibleAccountIds.has(accountId)) {
return "model_not_entitled";
}
if (accountId === MAIN_CODEX_ACCOUNT_ID) {
if (codexPoolOnlyExcludesMain(config) && options.explicitAccountSelector !== true) return "pool_only";
if (isMainAccountHardLocked(config)) return "main_hard_locked";
// Startup recovery owns the physical auth/vault boundary. Never parse or select
// native __main__ while an encrypted switch journal is pending or inconclusive.
if (!options.nativeMainSelectionOnly && isNativeMainTrafficBlocked()) return "main_traffic_blocked";
// A legacy pool row with the sentinel makes an active `__main__` ambiguous.
// Fail closed until the authenticated compatibility-delete path removes it.
if (hasLegacyMainCodexPoolAccount(config.codexAccounts)) return "legacy_pool_sentinel";
// These scopes cannot consume physical stored-main state, even indirectly through an
// ordinary quarantine plus hasMainAccountRefreshGrant(). Caller liveness is request-owned.
if (options.nativeMainSelectionOnly) return undefined;
if (options.requestOwnedMainCredential) {
return options.isMainAccountTokenLive?.() === false ? "main_credential_unavailable" : undefined;
}
const credential = getMainAccountCredentialStatus();
if (credential.refreshGrantRejected) return "needs_reauth";
if (isAccountNeedsReauth(accountId) && !credential.hasRefreshGrant) return "needs_reauth";
// Main account: a refresh grant is enough to route; materialization refreshes before I/O.
const mainLive = options.isMainAccountTokenLive
? options.isMainAccountTokenLive()
: credential.usable;
return mainLive ? undefined : "main_credential_unavailable";
}
const exists = (config.codexAccounts ?? [])
.some(account => isSelectableCodexPoolAccount(account) && account.id === accountId);
if (!exists) return "not_in_pool";
if (isAccountNeedsReauth(accountId)) return "needs_reauth";
const record = readCodexAccountRecord(accountId);
if (!record?.credential) return "missing_credential";
if (record.deletedAt != null) return "deleted";
if (record.codexValidationPending) return "validation_pending";
if (configuredCodexMainDuplicate(config, accountId)) return "duplicate_main";
return undefined;
}
export function isCodexAccountUsable(
config: OcxConfig,
accountId: string,
options: CodexAccountUsabilityOptions = {},
): boolean {
return codexAccountUnusableReason(config, accountId, options) === undefined;
}