/** * Claude (provider id `claude`) on the shared subscription core: the adapter * that supplies every Claude-specific input to the provider-neutral runtime * (credential codec for OAuth and setup tokens, OAuth refresh, relogin * classification, health policy, exact cache lifetime, capability flags), * the decoding of Claude usage into the shared quota model, and the database * binding. * * Core credential plaintext (the format the Claude cutover maps legacy rows * to): the same `ClaudeSubscriptionCredential` JSON object the legacy * `claude_subscription_credentials` table stores, * `{ version: 1, token, identity: { accountUuid, deviceId }, oauth? }`. A * credential with `oauth` has the format `claude_oauth_v1` and renews * through the OAuth refresh; one without it is a pasted setup token * (`claude_setup_token_v1`), which never renews: once Claude refuses it the * shared refresh marks the connection needs-relogin. The format is derived * from the decoded secret (`credential.format`) whenever the core decides * renewal; the stored `credential_format` column is informational only * (the cutover records `claude_oauth_v1` / `claude_setup_token_v1`, the * shared connect and reconnect writers record the envelope version `v1`) * and is never passed to `capabilitiesFor`. `provider_account_id` is * the Claude account UUID, else the `oauth:`/`setup:` HMAC id the writers * already derive; setup tokens report no email or plan. * * The binding is not in the provider registry until the Claude drained * cutover adds the SQL registry row with it (design 5.3, C3): until then * every shared runtime refuses it, and the worker never reaches it without * the `claude` cutover receipt. */ import { ClaudeSubscriptionCredential, claudeServiceStateInGrace, type ClaudeUsageObservation, } from "@opengeni/config"; import { CLAUDE_SUBSCRIPTION_PROVIDER_IDS, claudeSubscriptionModelIdForms, isClaudeSubscriptionModelId, } from "@opengeni/contracts/claude-subscription-model-ids"; import type { z } from "zod"; import type { QuotaBlockClaim, QuotaProviderDetail, SubscriptionCoreAdapter, SubscriptionQuota, } from "@opengeni/subscriptions"; import { claudeRefreshedCredential, ClaudeSubscriptionReconnectRequired, requestClaudeProfilePlan, requestClaudeTokenRefresh, } from "./subscription-core-claude-oauth"; import { subscriptionCoreDefaultErrors } from "./subscription-core/errors"; import type { SubscriptionCoreProvider } from "./subscription-core/provider"; import { decodeSubscriptionCoreClaudeUsage, fetchSubscriptionCoreClaudeUsage, subscriptionCoreClaudeUsageConfirmsAvailability, subscriptionCoreClaudeUsageEndsClaimedBlock, } from "./subscription-core-claude-usage"; /** Claude's provider id on the shared core and in the legacy tables. */ export const SUBSCRIPTION_CORE_CLAUDE_PROVIDER = "claude"; /** The credential format of an OAuth sign-in, which renews (derived from the decoded secret). */ export const SUBSCRIPTION_CORE_CLAUDE_OAUTH_FORMAT = "claude_oauth_v1"; /** The credential format of a pasted setup token, which never renews. */ export const SUBSCRIPTION_CORE_CLAUDE_SETUP_TOKEN_FORMAT = "claude_setup_token_v1"; /** Refresh this long before the OAuth token expires (the legacy resolver's margin). */ export const SUBSCRIPTION_CORE_CLAUDE_REFRESH_WINDOW_MS = 60 * 1000; /** An OAuth credential always records its expiry; this bounds one that somehow does not. */ export const SUBSCRIPTION_CORE_CLAUDE_REFRESH_FALLBACK_MS = 60 * 60 * 1000; /** Claude has no plan entitlements; kept equal to the other providers. */ export const SUBSCRIPTION_CORE_CLAUDE_ENTITLEMENT_COOLDOWN_MS = 24 * 60 * 60 * 1000; /** A 429 without a retry time rests as long as the legacy backpressure delay. */ export const SUBSCRIPTION_CORE_CLAUDE_RATE_LIMIT_FALLBACK_MS = 60 * 1000; /** A limit without a known reset is checked again after a minute, as the legacy capacity check does. */ export const SUBSCRIPTION_CORE_CLAUDE_UNKNOWN_RESET_MS = 60 * 1000; /** * The prompt-cache lifetime Opengeni sends on Claude requests (`cacheTtl: * "5m"`), declared as an exact TTL (SUB-STICK-04). Anthropic documents the * default cache lifetime as 5 minutes, refreshed each time the cached * content is used (https://platform.claude.com/docs/en/build-with-claude/prompt-caching). */ export const SUBSCRIPTION_CORE_CLAUDE_CACHE_TTL_MS = 5 * 60 * 1000; /** * The legacy product model id prefixes of Claude subscription models * (`claudeProviderId`), which the catalog still emits. Every Claude * subscription model is also named `claude/`; all forms are one * model (`claudeSubscriptionModelIdForms`). */ export const SUBSCRIPTION_CORE_CLAUDE_MODEL_PREFIXES = [ "organization-claude-subscription", "workspace-claude-subscription", ] as const; /** The decoded Claude credential (the legacy secret object, unchanged). */ export type SubscriptionCoreClaudeCredential = z.infer; const RELOGIN_TEXT = "Claude sign-in expired or was revoked. Sign in again in Models."; export type SubscriptionCoreClaudeAdapterDeps = { /** The fetch the OAuth refresh uses (tests inject a scripted upstream). */ fetch?: typeof fetch; now?: () => number; }; /** Decode the stored plaintext; fixed texts, never echoing the plaintext. */ export function decodeSubscriptionCoreClaudeCredential( plaintext: string, ): SubscriptionCoreClaudeCredential { let parsed: unknown; try { parsed = JSON.parse(plaintext); } catch { // No cause: a JSON.parse message quotes the plaintext it failed on. throw new Error("A core Claude credential is not valid JSON"); } const result = ClaudeSubscriptionCredential.safeParse(parsed); if (!result.success) { throw new Error("A core Claude credential does not hold the expected token object"); } return result.data; } /** Encode a credential as the stored plaintext (the legacy object, field order kept). */ export function encodeSubscriptionCoreClaudeCredential( credential: SubscriptionCoreClaudeCredential, ): string { const parsed = ClaudeSubscriptionCredential.safeParse(credential); if (!parsed.success) throw new Error("A core Claude credential cannot be encoded"); const { version, token, identity, oauth } = parsed.data; return JSON.stringify({ version, token, identity, ...(oauth ? { oauth } : {}) }); } export function subscriptionCoreClaudeAdapter( deps: SubscriptionCoreClaudeAdapterDeps = {}, ): SubscriptionCoreAdapter { const now = deps.now ?? Date.now; const capabilities = { autoRenews: true, // Limit reset grants the provider offers some plans, claimed on request // (`subscription-core-claude-limit-resets.ts`). resetCredits: true, // Claude's paid overage is the provider's own setting, read from its // usage reports (`decodeSubscriptionCoreClaudeQuota`), not a consent // Opengeni stores per connection. extraCredits: false, modelEntitlements: false, realtime: false, fundsMedia: false, apps: false, remoteCompaction: false, quotaWindows: true, }; const setupTokenCapabilities = { ...capabilities, autoRenews: false }; return { provider: SUBSCRIPTION_CORE_CLAUDE_PROVIDER, displayName: "Claude", capabilities, // Only an OAuth sign-in renews. Any other stored format, including one // this binary does not know, is never refreshed (fail closed). capabilitiesFor: (format) => format === SUBSCRIPTION_CORE_CLAUDE_OAUTH_FORMAT ? capabilities : setupTokenCapabilities, credentialKind: "oauth", quotaKind: "usage_windows", // Workspace model policy treats every Claude subscription provider id as // one provider (`evaluateWorkspaceModelPolicy`), whichever form a model // or a stored policy uses. modelPolicyProviderId: SUBSCRIPTION_CORE_CLAUDE_MODEL_PREFIXES[0], modelPolicyProviderIdsFor: subscriptionCoreClaudeModelPolicyProviderIds, modelIdFormsFor: (modelId) => isClaudeSubscriptionModelId(modelId) ? claudeSubscriptionModelIdForms(modelId) : null, cacheFacts: { kind: "exact_ttl", ttlMs: SUBSCRIPTION_CORE_CLAUDE_CACHE_TTL_MS }, health: { // Refresh proves token renewal, not permission to make model requests. // Do not return a refused account to rotation until it is reconnected. forbiddenQuarantineMs: null, entitlementCooldownMs: SUBSCRIPTION_CORE_CLAUDE_ENTITLEMENT_COOLDOWN_MS, rateLimitFallbackMs: SUBSCRIPTION_CORE_CLAUDE_RATE_LIMIT_FALLBACK_MS, exhaustedFallbackMs: SUBSCRIPTION_CORE_CLAUDE_UNKNOWN_RESET_MS, }, credential: { decode: decodeSubscriptionCoreClaudeCredential, encode: encodeSubscriptionCoreClaudeCredential, expiry: (credential) => (credential.oauth ? new Date(credential.oauth.expiresAt) : null), format: (credential) => credential.oauth ? SUBSCRIPTION_CORE_CLAUDE_OAUTH_FORMAT : SUBSCRIPTION_CORE_CLAUDE_SETUP_TOKEN_FORMAT, }, refresh: { windowMs: SUBSCRIPTION_CORE_CLAUDE_REFRESH_WINDOW_MS, fallbackMs: SUBSCRIPTION_CORE_CLAUDE_REFRESH_FALLBACK_MS, async rotate(credential) { // A setup token has no refresh token: never called for its format, // and refused as a relogin if it were. if (!credential.oauth) throw new ClaudeSubscriptionReconnectRequired(); // Claude's refresh reports no plan: read it from the profile with the // current token, before the refresh. Once Anthropic rotates the // refresh token nothing fallible may run before it is persisted. // Unknown (no scope, an expired token, any failure): the recorded // plan stays. const planType = await requestClaudeProfilePlan( credential.token, credential.oauth.scopes, deps.fetch ?? globalThis.fetch, ); // The OAuth call has its own deadline and classifies a revoked grant // as ClaudeSubscriptionReconnectRequired, anything else as retryable. const tokens = await requestClaudeTokenRefresh(credential, deps.fetch ?? globalThis.fetch); const rotated = claudeRefreshedCredential(credential, tokens, now()); return { ...rotated, planType }; }, reloginMessage(error) { return error instanceof ClaudeSubscriptionReconnectRequired ? error.message : null; }, }, reloginText: (message) => (message.trim().length > 0 ? message : RELOGIN_TEXT), // Usage reads and their decoding live in `subscription-core-claude-usage.ts` (C2a). fetchUsage: (read) => fetchSubscriptionCoreClaudeUsage(read), decodeQuota: (input) => decodeSubscriptionCoreClaudeUsage(input), // A usage reading may omit a window that still runs: keep it (design 5.3). usageReadingsComplete: false, // The usage endpoint is authoritative for the plan limits it reports: // Anthropic can reset a limit before the reset a refused request // reported. A reading that reports each account window below the limit, // or explicitly without one (a verified Ultra plan has no five-hour // window), ends, before that stale reset, each stored block whose claimed limits // (`QuotaBlockClaim`) it reports below the limit; an unclaimed block // runs to its own end. usageReadEndsQuotaExhaustion: true, usageReadConfirmsAvailability: (response) => subscriptionCoreClaudeUsageConfirmsAvailability(response), usageReadEndsClaimedBlock: (response) => subscriptionCoreClaudeUsageEndsClaimedBlock(response), }; } /** * The product model ids a Claude upstream model id is served as: the * canonical `claude/` id and both legacy forms, so a cooldown or allowlist * written under them holds for whichever form a reader names. */ export function subscriptionCoreClaudeProductModelIds(upstreamModelId: string): string[] { return claudeSubscriptionModelIdForms(`claude/${upstreamModelId}`); } /** * The workspace-policy provider ids of a Claude product model: every Claude * subscription provider id, which workspace model policy evaluates as one * provider, so the answer is the same for every id form and any form of * accepted policy matches. Other ids get the organization id alone. */ export function subscriptionCoreClaudeModelPolicyProviderIds(modelId: string): readonly string[] { return isClaudeSubscriptionModelId(modelId) ? CLAUDE_SUBSCRIPTION_PROVIDER_IDS : [SUBSCRIPTION_CORE_CLAUDE_MODEL_PREFIXES[0]]; } const ACCOUNT_WINDOWS = new Set(["five_hour", "seven_day"]); /** * The weekly limit of the models Claude includes beyond the plan's weekly * limit (`seven_day_overage_included`, header prefix `7d_oi`) is the Fable * limit: its percentage equals the Fable entry of the usage endpoint's * scoped limits (`seven_day_fable`), and Fable reset grants clear it. */ const FAMILY_WINDOWS: Record = { seven_day_opus: /^claude-opus-/, seven_day_sonnet: /^claude-sonnet-/, seven_day_fable: /^claude-fable-/, seven_day_overage_included: /^claude-fable-/, }; /** Window ids that name the same limit (see `FAMILY_WINDOWS`). */ export const SUBSCRIPTION_CORE_CLAUDE_WINDOW_ALIASES: Readonly> = { seven_day_overage_included: "seven_day_fable", seven_day_fable: "seven_day_overage_included", }; const allowed = (status: string | null | undefined) => status === "allowed" || status === "allowed_warning"; /** * The limits a stored Claude block may claim: the plan windows the usage * endpoint reports. Any other representative claim (a newer limit this * build does not know), or none, leaves the block unclaimed (fail closed). */ export const SUBSCRIPTION_CORE_CLAUDE_CLAIMABLE_WINDOW_IDS: ReadonlySet = new Set([ ...ACCOUNT_WINDOWS, ...Object.keys(FAMILY_WINDOWS), ]); /** Accumulates the claims of one decoded block: null once any part is unclaimed. */ function addClaim(current: Set | null | undefined, claim: string | null) { if ( current === null || claim === null || !SUBSCRIPTION_CORE_CLAUDE_CLAIMABLE_WINDOW_IDS.has(claim) ) return null; return new Set([...(current ?? []), claim]); } /** * One Claude usage observation (response headers or the usage endpoint) * in the shared quota model, with the legacy capacity rules * (`claudeSubscriptionCapacity`, applied to the one response observed): * * - Paid overage the response allowed covers every plan limit it reports: * a full account window is a `warning`, and no family or request limit * blocks. Overage is the Claude account's own setting, so it covers every * model of the account. * - A rejected request whose overage was refused with a known reset before * its own plan reset is checked again at the overage reset (while its plan * limit still runs): the blocks that response reports for the models it * concerns (its own model; without one, the family its limit names, else * every model) end no later than it. * - The account windows (five hours, seven days) decide the connection's * capacity: `rejected`, or full without a status, is `exhausted` until its * reset (a minute when the reset is unknown). * - A model-family window (Opus, Sonnet, Fable) limits only that family: when full * or rejected it becomes a cooldown of each listed model of the family, and * the window itself is reported with `unknown` status so it never decides * the account's capacity. The paid overage windows never block. * - A rejected request, and each rejected request restriction the * observation carries (unless a later allowance of the same model lifts * it), limits the model it was made for (a cooldown of that model's * product ids); without a model, the family its limit names, else the * whole connection (`exhaustedUntil`, kind `quota`). * - The model a response was allowed for dispatches whatever else that * response reports, until the allowance's reset (a limit outlasting it * limits that model too, from now). * - When some models are judged apart (an allowed model, or a recheck that * concerns only some models), an account limit cools the listed models * one by one instead of exhausting the connection, and the window is a * `warning`. * - Each block carries the plan windows it came from (`QuotaBlockClaim`): * the full window itself, or the representative claim a refusal named. * A block any part of which names no claim, or a limit this build does * not recognize, carries none and never ends before its own reset. * - Reduced service past a limit is neither exhaustion nor plain capacity: * a response served in the grace zone (grace utilization above zero, not * rejected) or in the slow tier (`slow-status: active`) blocks nothing, * reports its full windows as `warning`, and marks the connection * constrained (`constrainedUntil`) until the latest full window resets. * A refusal because the slow tier's slots are busy (`slot_busy`) rests * the connection as a rate limit for the slow tier's retry-after (else * the rate-limit fallback) and is constrained until the plan reset. * - Everything else the observation reported (reset grants, the limit * list, extra usage, service state) is kept as provider details, each * part stamped with the observation time. * * `upstreamModelIds` must list every Claude model the connection can serve * (the organization's catalog when decoded), so a family limit reaches each * of them; the response's own model is always included. */ export function decodeSubscriptionCoreClaudeQuota(input: { observation: ClaudeUsageObservation; refreshGeneration: number; upstreamModelIds: readonly string[]; }): Omit { const { observation } = input; const observedAt = Date.parse(observation.observedAt); const request = observation.requestStatus ?? null; const rejected = request?.status === "rejected"; const own = request?.upstreamModelId ?? null; const overageReset = request?.overageResetsAt ? Date.parse(request.overageResetsAt) : null; const paidOverage = rejected && allowed(request.overageStatus) && (overageReset ?? Infinity) > observedAt; // Reduced service: served past the limit (grace zone, slow tier), or // refused only because the slow tier's slots are busy for now. const service = observation.details?.serviceState ?? null; const slowStatus = service?.slow?.status ?? null; const reduced = !paidOverage && ((claudeServiceStateInGrace(service) && !rejected) || slowStatus === "active"); const slotBusy = !paidOverage && rejected && slowStatus === "slot_busy"; const overageCovers = paidOverage || reduced || slotBusy; const base = (reset: string | null, at: string) => reset === null ? Date.parse(at) + SUBSCRIPTION_CORE_CLAUDE_UNKNOWN_RESET_MS : Date.parse(reset); // Only while the request's own plan limit still runs, and only when the // overage resets first: a later overage reset never ends another limit. const planReset = rejected ? base(request.resetsAt, request.observedAt) : -Infinity; const recheck = rejected && request.overageStatus === "rejected" && planReset > observedAt && overageReset !== null && overageReset < planReset ? overageReset : null; const recheckFamily = own === null && request?.representativeClaim ? FAMILY_WINDOWS[request.representativeClaim] : undefined; /** The latest a block of this model (null: the connection) may run. */ const capOf = (model: string | null) => recheck === null ? Infinity : own !== null ? model === own ? recheck : Infinity : recheckFamily ? model !== null && recheckFamily.test(model) ? recheck : Infinity : recheck; // The model this response was allowed for dispatches whatever else it // reports, until the allowance's own reset (a minute when unknown). const spared = request && allowed(request.status) ? own : null; const allowanceEnd = spared === null ? -Infinity : request?.resetsAt ? Date.parse(request.resetsAt) : observedAt + SUBSCRIPTION_CORE_CLAUDE_UNKNOWN_RESET_MS; const perModel = spared !== null || (recheck !== null && capOf(null) === Infinity); // The response's own model is limited by its family's window even when the // listed catalog misses it. const listed = [...new Set([...input.upstreamModelIds, ...(own ? [own] : [])])]; const modelCooldowns: Record = {}; // Which plan windows each block came from (null: some part unclaimed). const cooldownClaims = new Map | null>(); let exhaustedClaims: Set | null | undefined; const cool = (upstreamModelId: string, claim: string | null, limit: number) => { const end = Math.min(limit, capOf(upstreamModelId)); if (end <= observedAt || (upstreamModelId === spared && end <= allowanceEnd)) return; for (const modelId of subscriptionCoreClaudeProductModelIds(upstreamModelId)) { modelCooldowns[modelId] = Math.max(modelCooldowns[modelId] ?? end, end); cooldownClaims.set(modelId, addClaim(cooldownClaims.get(modelId), claim)); } }; let exhaustedUntil: number | null = null; const block = (model: string | null, claim: string | null, limit: number) => { const family = claim === null ? undefined : FAMILY_WINDOWS[claim]; if (model !== null) cool(model, claim, limit); else if (family || perModel) { for (const id of listed) if (!family || family.test(id)) cool(id, claim, limit); } else { const end = Math.min(limit, capOf(null)); if (end > observedAt) { exhaustedUntil = Math.max(exhaustedUntil ?? end, end); exhaustedClaims = addClaim(exhaustedClaims, claim); } } }; const windows = observation.windows.map((window) => { const full = !overageCovers && (window.status === "rejected" || (window.status === null && (window.usedPercent ?? 0) >= 100)); const resetsAt = window.resetsAt === null ? null : Date.parse(window.resetsAt); const account = ACCOUNT_WINDOWS.has(window.id); // Only plan windows block: a family window its family, an account window // the connection (or, judged per model, each listed model). if (full && (FAMILY_WINDOWS[window.id] !== undefined || (account && perModel))) block(null, window.id, base(window.resetsAt, window.observedAt)); const status = !account ? ("unknown" as const) : full && !perModel ? ("exhausted" as const) : full || overageCovers || window.status === "allowed_warning" ? ("warning" as const) : window.status === "allowed" || window.usedPercent !== null ? ("ok" as const) : ("unknown" as const); return { id: window.id, usedPercent: window.usedPercent, resetsAt: status === "exhausted" ? Math.min(base(window.resetsAt, window.observedAt), capOf(null)) : resetsAt, status, }; }); if (rejected && !overageCovers) { block(own, request.representativeClaim, base(request.resetsAt, request.observedAt)); } const restrictions = observation.requestRestrictions ?? []; for (const item of restrictions) { if (item.status !== "rejected") continue; // Restrictions carry no overage of their own: the response's allowed // overage covers those it postdates, and a later allowance of the same // model lifts its restriction. if (overageCovers && request && item.observedAt <= request.observedAt) continue; const lifted = restrictions.some( (other) => item.upstreamModelId !== null && other.upstreamModelId === item.upstreamModelId && allowed(other.status) && other.observedAt > item.observedAt, ); if (!lifted) block(item.upstreamModelId, item.representativeClaim, base(item.resetsAt, item.observedAt)); } const claimOf = (claims: Set | null | undefined, until: number) => claims && claims.size > 0 ? { until, observedAt, windowIds: [...claims].sort() } : null; const modelCooldownClaims: Record = {}; for (const [modelId, until] of Object.entries(modelCooldowns)) { const claim = claimOf(cooldownClaims.get(modelId), until); if (claim) modelCooldownClaims[modelId] = claim; } const exhaustedClaim = exhaustedUntil === null ? null : claimOf(exhaustedClaims, exhaustedUntil); // Reduced service lasts until the latest limit it went past resets. let constrainedUntil: number | null = null; if (reduced || slotBusy) { const passed = observation.windows .filter((window) => window.status === "rejected" || (window.usedPercent ?? 0) >= 100) .map((window) => base(window.resetsAt, window.observedAt)); if (request && (rejected || passed.length === 0)) passed.push(base(request.resetsAt, request.observedAt)); constrainedUntil = Math.max(observedAt + SUBSCRIPTION_CORE_CLAUDE_UNKNOWN_RESET_MS, ...passed); } if (slotBusy) { const retryAfter = service?.slow?.retryAfterSeconds ?? null; const until = observedAt + (retryAfter === null ? SUBSCRIPTION_CORE_CLAUDE_RATE_LIMIT_FALLBACK_MS : retryAfter * 1000); exhaustedUntil = Math.max(exhaustedUntil ?? until, until); } const providerDetails: Record = {}; for (const [key, value] of Object.entries(observation.details ?? {})) if (value !== undefined) providerDetails[key] = { observedAt, value }; return { windows, modelCooldowns, ...(Object.keys(modelCooldownClaims).length > 0 ? { modelCooldownClaims } : {}), exhaustedUntil, exhaustedKind: exhaustedUntil === null ? null : slotBusy ? "rate_limit" : "quota", ...(exhaustedClaim && !slotBusy ? { exhaustedClaim } : {}), observedAt, observedRefreshGeneration: input.refreshGeneration, source: observation.source === "provider" ? "usage_endpoint" : "response_headers", ...(constrainedUntil !== null ? { constrainedUntil } : {}), ...(Object.keys(providerDetails).length > 0 ? { providerDetails } : {}), }; } /** Claude's database binding on the shared core. */ export function subscriptionCoreClaudeProvider( deps: SubscriptionCoreClaudeAdapterDeps = {}, ): SubscriptionCoreProvider { return { adapter: subscriptionCoreClaudeAdapter(deps), // Claude has no remote compaction state. sessionCompactionLock: null, errors: subscriptionCoreDefaultErrors("Claude"), settings: { primaryColumn: "claude_primary_connection_id" }, }; } /** The Claude binding (the production refresh call). */ export const SUBSCRIPTION_CORE_CLAUDE: SubscriptionCoreProvider = subscriptionCoreClaudeProvider();