/** * The Codex Apps designation on the shared subscription core (M3 PR 2b). * * Dormant until an organization's Codex cutover row is enabled: callers reach * this module only for that disposition, and every database routine it uses * refuses unless the cutover is enabled. Apps credentials load by their * designation, never through placement (design 6.3): the designation names * one shared Codex connection per workspace, written by an organization * administrator or that connection's delegated manager, and is usable in any * inference source mode. Reads and refreshes go through the * `*_subscription_codex_apps_*` routines (migration 0670), which recheck the * designation, the connection's scope and its health in the database and * serialize refresh on the same per-connection key as chat refresh. */ import { sql } from "drizzle-orm"; import { environmentsEncryptionKeyBytes, type Settings } from "@opengeni/config"; import { accessTokenExpiry, CODEX_CLIENT_VERSION, CODEX_REFRESH_FALLBACK_MS, CODEX_REFRESH_WINDOW_MS, CodexAppsCredentialUnavailable, CodexReloginRequired, parseIdToken, refreshCodexToken, } from "@opengeni/codex"; import { withCodexTokenDeadline } from "./codex-token-resolver"; import { rawRows, withRlsContext, type Database } from "./database"; import { setSubscriptionAdministrationSubject } from "./subscription-core/account-administrator"; import { decryptEnvironmentValue, encryptEnvironmentValue } from "./environment-crypto"; import { withLosslessContentWriteVersion } from "./lossless-json"; import * as schema from "./schema"; import { persistSubscriptionCoreRefreshWithPlan, resolveSubscriptionConnectionId, } from "./subscription-core-repository"; import { reserveSubscriptionCoreCodexAppsRequest, settleSubscriptionCoreCodexAppsRequest, } from "./subscription-core-codex-requests"; /** The designated core connection is no longer usable for Apps. */ export class SubscriptionCoreCodexAppsUnavailableError extends CodexAppsCredentialUnavailable { constructor(message = "Codex Apps authorization is no longer active") { super(message); this.name = "SubscriptionCoreCodexAppsUnavailableError"; } } export type SubscriptionCoreCodexAppsDesignation = { connectionId: string; /** Connection health; only `active` can serve Apps requests. */ status: string; }; /** * The workspace's usable core Apps designation: the designation still names a * shared Codex connection in the workspace's scope, and the cutover is * enabled. Never returns credential material. */ export async function resolveSubscriptionCoreCodexAppsDesignation( db: Database, input: { accountId: string; workspaceId: string }, ): Promise { return await withRlsContext( db, { accountId: input.accountId, workspaceId: input.workspaceId }, async (tx) => { const [row] = await rawRows<{ connection_id: string; status: string }>( tx, sql`select connection_id::text as connection_id, status from opengeni_private.resolve_subscription_codex_apps_designation( ${input.accountId}::uuid, ${input.workspaceId}::uuid)`, ); return row ? { connectionId: row.connection_id, status: row.status } : null; }, ); } /** The legacy `CodexAppsSettings` projection of the core designation row. */ export type SubscriptionCoreCodexAppsSettings = { credentialId: string | null; version: number; designatedAt: Date | null; }; const NO_DESIGNATION: SubscriptionCoreCodexAppsSettings = { credentialId: null, version: 0, designatedAt: null, }; async function readDesignationRow( tx: Database, input: { accountId: string; workspaceId: string }, ): Promise { const [row] = await rawRows<{ connection_id: string; version: number | string; updated_at: Date | string; }>( tx, sql`select connection_id::text as connection_id, version, updated_at from subscription_apps_designations where account_id = ${input.accountId}::uuid and workspace_id = ${input.workspaceId}::uuid`, ); return row ? { credentialId: row.connection_id, version: Number(row.version), designatedAt: new Date(row.updated_at), } : NO_DESIGNATION; } export async function getSubscriptionCoreCodexAppsSettings( db: Database, input: { accountId: string; workspaceId: string }, ): Promise { return await withRlsContext( db, { accountId: input.accountId, workspaceId: input.workspaceId }, async (tx) => await readDesignationRow(tx, input), ); } export type DesignateSubscriptionCoreCodexAppsResult = | ({ kind: "updated" } & SubscriptionCoreCodexAppsSettings) | ({ kind: "conflict" | "already_designated" } & SubscriptionCoreCodexAppsSettings) | { kind: "not_found" } | { kind: "forbidden" } | { kind: "unavailable" }; function isRlsRefusal(error: unknown): boolean { const code = (error as { code?: unknown } | null)?.code; const causeCode = (error as { cause?: { code?: unknown } } | null)?.cause?.code; return code === "42501" || causeCode === "42501"; } async function withAppsAdministration( db: Database, input: { accountId: string; workspaceId: string; subjectId: string }, fn: (tx: Database) => Promise, ): Promise { return await withRlsContext( db, { accountId: input.accountId, workspaceId: input.workspaceId }, async (tx) => { await setSubscriptionAdministrationSubject(tx, input); // The same key the Apps request recheck holds, so a clear cannot commit // between that recheck and the request it authorizes. await tx.execute( sql`select pg_advisory_xact_lock(hashtextextended(${`codex-apps-settings:${input.workspaceId}`}, 0))`, ); return await fn(tx); }, ); } async function auditAppsDesignation( tx: Database, input: { accountId: string; workspaceId: string; subjectId: string }, action: "codex_apps.designated" | "codex_apps.cleared", connectionId: string, version: number, ): Promise { await tx.insert(schema.auditEvents).values( withLosslessContentWriteVersion( { accountId: input.accountId, workspaceId: input.workspaceId, subjectId: input.subjectId, action, targetType: "subscription_connection", targetId: connectionId, metadata: { version }, }, "metadata", "metadataCodecVersion", ), ); } /** * Designate a shared Codex connection for Apps. Authorization is the * designation table's own policy: an organization administrator, or a * workspace administrator for a connection this workspace manages; the * connection must be active and in the workspace's scope. Legacy account ids * resolve through aliases first. */ export async function designateSubscriptionCoreCodexApps( db: Database, input: { accountId: string; workspaceId: string; connectionId: string; subjectId: string; expectedVersion: number; }, ): Promise { return await withAppsAdministration(db, input, async (tx) => { const current = await readDesignationRow(tx, input); if (current.version !== input.expectedVersion) return { kind: "conflict", ...current }; if (current.credentialId !== null) return { kind: "already_designated", ...current }; const connectionId = await resolveSubscriptionConnectionId(tx, { accountId: input.accountId, provider: "codex", connectionId: input.connectionId, }); if (!connectionId) return { kind: "not_found" }; // Only a target the Apps resolver can serve: a shared Codex subscription // connection that is organization-scoped or assigned to exactly this // workspace. A personal, people-scoped or elsewhere-assigned connection // would be an inert designation, so it is refused as not found. const [connection] = await rawRows<{ status: string; kind: string; ownership: string; in_scope: boolean; }>( tx, sql`select connection.status, connection.kind, connection.ownership, (connection.scope_kind = 'organization' or (connection.scope_kind = 'workspaces' and exists ( select 1 from subscription_connection_workspaces assignment where assignment.account_id = connection.account_id and assignment.connection_id = connection.id and assignment.workspace_id = ${input.workspaceId}::uuid))) as in_scope from subscription_connections connection where connection.account_id = ${input.accountId}::uuid and connection.provider = 'codex' and connection.id = ${connectionId}::uuid`, ); if ( !connection || connection.kind !== "subscription" || connection.ownership !== "shared" || connection.in_scope !== true ) return { kind: "not_found" }; if (connection.status !== "active") return { kind: "unavailable" }; let written: { version: number | string; updated_at: Date | string } | undefined; try { written = await tx.transaction(async (savepoint) => { // Clear deletes the row, so the version cannot simply count up from // the stored one: a designation made after a clear would reuse a // version a stale client still holds, and that client's clear would // remove the newer designation. The version is therefore the // transaction-clock microsecond (never below the stored version + 1). // Designate and clear serialize on the workspace's settings lock, so // every designation of a workspace gets a version strictly greater // than any earlier one, and a stale expectedVersion always conflicts. const [row] = await rawRows<{ version: number | string; updated_at: Date | string }>( savepoint as unknown as Database, sql`insert into subscription_apps_designations ( account_id, workspace_id, connection_id, version, updated_by_subject_id, updated_at ) values ( ${input.accountId}::uuid, ${input.workspaceId}::uuid, ${connectionId}::uuid, greatest(${current.version + 1}::bigint, floor(extract(epoch from clock_timestamp()) * 1000000)::bigint), ${input.subjectId}, clock_timestamp() ) returning version, updated_at`, ); return row; }); } catch (error) { if (isRlsRefusal(error)) return { kind: "forbidden" }; throw error; } if (!written) throw new Error("Core Codex Apps designation was not persisted"); const version = Number(written.version); await auditAppsDesignation(tx, input, "codex_apps.designated", connectionId, version); return { kind: "updated", credentialId: connectionId, version, designatedAt: new Date(written.updated_at), }; }); } export type ClearSubscriptionCoreCodexAppsResult = { kind: "updated" | "unchanged" | "conflict" | "forbidden"; } & SubscriptionCoreCodexAppsSettings; /** * Clear the designation, valid in any inference source mode. The core row is * deleted, so the projected version returns to 0 (no designation); the next * designation's version is greater than every earlier one (see designate). */ export async function clearSubscriptionCoreCodexApps( db: Database, input: { accountId: string; workspaceId: string; subjectId: string; expectedVersion: number }, ): Promise { return await withAppsAdministration(db, input, async (tx) => { const current = await readDesignationRow(tx, input); if (current.version !== input.expectedVersion) return { kind: "conflict", ...current }; if (current.credentialId === null) return { kind: "unchanged", ...current }; const removed = await rawRows<{ connection_id: string }>( tx, sql`delete from subscription_apps_designations where account_id = ${input.accountId}::uuid and workspace_id = ${input.workspaceId}::uuid and version = ${current.version} returning connection_id::text as connection_id`, ); // The delete policy hides a row this subject may not manage. if (removed.length === 0) return { kind: "forbidden", ...current }; await auditAppsDesignation(tx, input, "codex_apps.cleared", current.credentialId, 0); return { kind: "updated", ...NO_DESIGNATION }; }); } type AppsCredential = { refreshGeneration: number; tokens: { accessToken: string; refreshToken: string; idToken: string }; chatgptAccountId: string | null; expiresAt: Date | null; lastRefreshAt: Date | null; }; function encryptionKey(settings: Settings): Uint8Array { const key = environmentsEncryptionKeyBytes(settings); if (!key) { throw new Error( "core Codex credential present but OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY is not configured", ); } return key; } function decodeTokens(key: Uint8Array, encrypted: string): AppsCredential["tokens"] { let parsed: unknown; try { parsed = JSON.parse(decryptEnvironmentValue(key, encrypted)); } catch { // Fixed text and no cause: a parse error would quote the plaintext. throw new Error("A core Codex credential could not be decrypted"); } const record = parsed as Record | null; if ( !record || typeof record.access_token !== "string" || typeof record.refresh_token !== "string" || typeof record.id_token !== "string" ) { throw new Error("A core Codex credential does not hold the expected token object"); } return { accessToken: record.access_token, refreshToken: record.refresh_token, idToken: record.id_token, }; } type AppsTarget = { accountId: string; workspaceId: string; connectionId: string }; async function loadAppsCredential( db: Database, key: Uint8Array, target: AppsTarget, ): Promise { const row = await withRlsContext( db, { accountId: target.accountId, workspaceId: target.workspaceId }, async (tx) => ( await rawRows<{ status: string; refresh_generation: number | string; credential_encrypted: string | null; expires_at: Date | string | null; last_refresh_at: Date | string | null; provider_account_id: string | null; }>( tx, sql`select status, refresh_generation, credential_encrypted, expires_at, last_refresh_at, provider_account_id from opengeni_private.read_subscription_codex_apps_credential( ${target.accountId}::uuid, ${target.workspaceId}::uuid, ${target.connectionId}::uuid)`, ) )[0], ); if (!row) throw new SubscriptionCoreCodexAppsUnavailableError(); if (row.status !== "active" || row.credential_encrypted === null) { // Still the authorized designation, but its sign-in must be renewed. throw new CodexReloginRequired("The designated Codex Apps account must be reconnected."); } return { refreshGeneration: Number(row.refresh_generation), tokens: decodeTokens(key, row.credential_encrypted), chatgptAccountId: row.provider_account_id, expiresAt: row.expires_at === null ? null : new Date(row.expires_at), lastRefreshAt: row.last_refresh_at === null ? null : new Date(row.last_refresh_at), }; } type AppsRefreshOutcome = | { kind: "refreshed"; accessToken: string } | { kind: "superseded" } | { kind: "unavailable" } | { kind: "relogin"; message: string } | { kind: "error"; error: unknown }; export type SubscriptionCoreCodexAppsDeps = { refresh?: typeof refreshCodexToken; now?: () => Date; resolveToken?: () => Promise; reserveRequest?: typeof reserveSubscriptionCoreCodexAppsRequest; settleRequest?: typeof settleSubscriptionCoreCodexAppsRequest; }; /** * Rotate the designated connection's refresh token: begin authorizes by the * designation and takes the per-connection advisory key, the rotated token is * persisted immediately after the provider returns, and a permanent OAuth * refusal marks the connection needs-relogin under the same authorization. */ async function refreshAppsCredential( db: Database, key: Uint8Array, target: AppsTarget, observedRefreshGeneration: number, deps: SubscriptionCoreCodexAppsDeps, ): Promise { const refresh = deps.refresh ?? refreshCodexToken; const now = deps.now ?? (() => new Date()); return await withRlsContext( db, { accountId: target.accountId, workspaceId: target.workspaceId }, async (tx): Promise => { const [begun] = await rawRows<{ refresh_generation: number | string; credential_encrypted: string; }>( tx, sql`select refresh_generation, credential_encrypted from opengeni_private.begin_subscription_codex_apps_refresh( ${target.accountId}::uuid, ${target.workspaceId}::uuid, ${target.connectionId}::uuid)`, ); if (!begun) return { kind: "unavailable" }; const generation = Number(begun.refresh_generation); if (generation !== observedRefreshGeneration) return { kind: "superseded" }; try { const tokens = decodeTokens(key, begun.credential_encrypted); const next = await withCodexTokenDeadline(refresh(tokens.refreshToken)); const rotated = { access_token: next.accessToken ?? tokens.accessToken, refresh_token: next.refreshToken ?? tokens.refreshToken, id_token: next.idToken ?? tokens.idToken, }; // The plan the new token reports; an unreadable token keeps the // recorded plan. let planType: string | null = null; try { planType = next.idToken ? parseIdToken(next.idToken).planType : null; } catch { planType = null; } // Persist before any other fallible work, through the shared refresh // writer: outside a chat turn, on the authorization begin minted. const persisted = await persistSubscriptionCoreRefreshWithPlan(tx, { provider: "codex", accountId: target.accountId, workspaceId: target.workspaceId, sessionId: null, turnId: null, connectionId: target.connectionId, expectedRefreshGeneration: generation, credentialEncrypted: encryptEnvironmentValue(key, JSON.stringify(rotated)), expiresAt: accessTokenExpiry(rotated.access_token), lastRefreshAt: now(), planType, }); if (!persisted) return { kind: "superseded" }; return { kind: "refreshed", accessToken: rotated.access_token }; } catch (error) { if (error instanceof CodexReloginRequired) { await tx.execute( sql`select opengeni_private.fail_subscription_codex_apps_refresh( ${target.accountId}::uuid, ${target.workspaceId}::uuid, ${target.connectionId}::uuid, ${generation}::bigint, ${error.message})`, ); return { kind: "relogin", message: error.message }; } return { kind: "error", error }; } }, ); } const appsRefreshFlights = new Map>(); /** * Bearer for the designated core connection: the stored token while fresh, * otherwise one refresh per connection and generation in this process (the * advisory key serializes other replicas). */ export function buildSubscriptionCoreCodexAppsTokenResolver( db: Database, settings: Settings, target: AppsTarget, deps: SubscriptionCoreCodexAppsDeps = {}, ): () => Promise<{ accessToken: string; chatgptAccountId: string | null }> { return async () => { const key = encryptionKey(settings); const credential = await loadAppsCredential(db, key, target); const expiry = credential.expiresAt ?? accessTokenExpiry(credential.tokens.accessToken); const stale = expiry ? expiry.getTime() <= Date.now() + CODEX_REFRESH_WINDOW_MS : credential.lastRefreshAt ? credential.lastRefreshAt.getTime() < Date.now() - CODEX_REFRESH_FALLBACK_MS : true; if (!stale) { return { accessToken: credential.tokens.accessToken, chatgptAccountId: credential.chatgptAccountId, }; } // Per workspace as well: an organization-scoped connection can be // designated by several workspaces, and one workspace's `unavailable` // (its designation ended) must never be shared with another's request. const flightKey = `${target.workspaceId}:${target.connectionId}:${credential.refreshGeneration}`; let flight = appsRefreshFlights.get(flightKey); if (!flight) { flight = refreshAppsCredential(db, key, target, credential.refreshGeneration, deps).finally( () => appsRefreshFlights.delete(flightKey), ); appsRefreshFlights.set(flightKey, flight); } const outcome = await flight; switch (outcome.kind) { case "refreshed": return { accessToken: outcome.accessToken, chatgptAccountId: credential.chatgptAccountId }; case "superseded": { const reloaded = await loadAppsCredential(db, key, target); return { accessToken: reloaded.tokens.accessToken, chatgptAccountId: reloaded.chatgptAccountId, }; } case "relogin": throw new CodexReloginRequired(outcome.message); case "unavailable": throw new SubscriptionCoreCodexAppsUnavailableError(); default: throw outcome.error; } }; } type SubscriptionCoreCodexAppsToken = { accessToken: string; chatgptAccountId: string | null }; export type SubscriptionCoreCodexAppsRequestAuth = { clientVersion: string; withAuthorization: (use: (token: SubscriptionCoreCodexAppsToken) => Promise) => Promise; /** The runtime must put its physical fetch inside this callback, not extract a bearer. */ withRequest: ( use: (token: SubscriptionCoreCodexAppsToken) => Promise, options?: { signal?: AbortSignal | null | undefined }, ) => Promise; }; /** Observe the consumer's actual EOF, without buffering an Apps MCP/SSE stream. */ function trackAppsResponse( response: Response, finish: (outcome: "response_received" | "refused" | "unknown") => Promise, ): Response { if (!response.body) return response; const reader = response.body.getReader(); let released = false; const release = () => { if (!released) { released = true; reader.releaseLock(); } }; const body = new ReadableStream( { async pull(controller) { try { const next = await reader.read(); if (next.done) { await finish("response_received"); release(); controller.close(); } else controller.enqueue(next.value); } catch (error) { await finish("unknown"); release(); controller.error(error); } }, async cancel(reason) { await finish("unknown"); try { await reader.cancel(reason); } finally { release(); } }, }, { highWaterMark: 0 }, ); return new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers, }); } /** * Runtime Apps authentication for one core designation. Physical requests use * withRequest, whose native reservation atomically checks the designation and * disconnect fence before dispatch. withAuthorization is an access-only check; * its callback is not a physical request permit. */ export function subscriptionCoreCodexAppsRequestAuth( db: Database, settings: Settings, target: AppsTarget, deps: SubscriptionCoreCodexAppsDeps = {}, ): SubscriptionCoreCodexAppsRequestAuth { const resolve = deps.resolveToken ?? buildSubscriptionCoreCodexAppsTokenResolver(db, settings, target, deps); return { clientVersion: CODEX_CLIENT_VERSION, withRequest: async (use, options = {}) => { const signal = options.signal; signal?.throwIfAborted(); const token = await resolve(); const { operationId } = await ( deps.reserveRequest ?? reserveSubscriptionCoreCodexAppsRequest )(db, target, { requestId: crypto.randomUUID(), transportAttempt: 1 }); let settled = false; const finish = async (outcome: "response_received" | "refused" | "unknown") => { if (settled) return; settled = true; signal?.removeEventListener("abort", onAbort); await (deps.settleRequest ?? settleSubscriptionCoreCodexAppsRequest)(db, target, { operationId, outcome, }).catch(() => undefined); }; const onAbort = () => void finish("unknown"); if (signal?.aborted) { await finish("refused"); signal.throwIfAborted(); } signal?.addEventListener("abort", onAbort, { once: true }); try { const response = await use(token); if (!response.body) await finish("response_received"); return trackAppsResponse(response, finish); } catch (error) { await finish("unknown"); throw error; } }, withAuthorization: async (use) => { const token = await resolve(); return await withRlsContext( db, { accountId: target.accountId, workspaceId: target.workspaceId }, async (tx) => { await tx.execute( sql`select pg_advisory_xact_lock(hashtextextended(${`codex-apps-settings:${target.workspaceId}`}, 0))`, ); const [current] = await rawRows<{ connection_id: string; status: string }>( tx, sql`select connection_id::text as connection_id, status from opengeni_private.resolve_subscription_codex_apps_designation( ${target.accountId}::uuid, ${target.workspaceId}::uuid)`, ); if (current?.connection_id !== target.connectionId) { throw new SubscriptionCoreCodexAppsUnavailableError(); } if (current.status !== "active") { throw new CodexReloginRequired( "The designated Codex Apps account must be reconnected.", ); } return await use(token); }, ); }, }; }