/** * Connection-level pieces of the operations runtime, for any provider: * which shared connections may serve an operation outside chat (candidate * ordering), a refreshing bearer resolver for one connection under an * operation lease or a workspace connection read, running an operation on * the first candidate whose lease is granted, and the out-of-turn usage and * live-catalog probes that the provider's adapter implements * (`fetchUsage`, `decodeQuota`, `liveModels`). * * Shared-only by construction: candidates are organization- or * workspace-scoped shared connections in the workspace's effective pool, and * the credential seam refuses every other connection outside an exact * accepted turn. Nothing here writes the chat session binding. */ import { sql } from "drizzle-orm"; import type { Settings } from "@opengeni/config"; import { quotaCapacity, type SubscriptionCoreConnectionBearer, type SubscriptionCoreConnectionRead, type SubscriptionQuota, } from "@opengeni/subscriptions"; import { rawRows, type Database } from "../database"; import { readSubscriptionCoreWorkspacePool } from "./administration"; import { buildSubscriptionCoreCredentialResolver, subscriptionCoreRefreshPolicy, } from "./credential-resolver"; import { SubscriptionCoreError } from "./errors"; import { subscriptionCoreOperations, subscriptionCoreQuotaBlocksNow, subscriptionCoreUsageMergeOptions, subscriptionCoreUsageNotRecorded, type SubscriptionCoreConnectionCredential, type SubscriptionCoreConnectionCredentialLoad, type SubscriptionCoreConnectionRefreshDeps, type SubscriptionCoreConnectionRefreshOutcome, type SubscriptionCoreFetch, type SubscriptionCoreOperationLeaseRef, type SubscriptionCoreOperationScope, type SubscriptionCoreUsageNotApplied, } from "./operations"; import { memoByProvider, subscriptionCoreProviderId, type SubscriptionCoreProvider, } from "./provider"; import { decodeSubscriptionQuota, readSubscriptionSessionBinding, type SubscriptionOperationKind, } from "../subscription-core-repository"; export type SubscriptionCoreOperationCandidate = { connectionId: string; planType: string | null; /** The session's explicit choice (session scope only). */ explicit: boolean; /** The connection's model allowlist (null: every model). */ allowedModelIds: string[] | null; }; /** One connection's bearer for an operation, with the generation that minted it. */ export type SubscriptionCoreConnectionToken = SubscriptionCoreConnectionBearer & { planType: string | null; /** The refresh generation of this bearer (fences quota observations). */ credentialVersion: number; }; export type SubscriptionCoreConnectionResolverDeps = SubscriptionCoreConnectionRefreshDeps & { /** Test seams for the credential read and the refresh under the core lock. */ load?: ( db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, ) => Promise; refreshCredential?: ( db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, observedRefreshGeneration: number, deps: SubscriptionCoreConnectionRefreshDeps, ) => Promise; /** The caller's relogin error (default: a core error with the adapter's text). */ relogin?: (message: string | null) => Error; /** The caller's lost-operation error (default: the binding's `operationUnavailable`). */ unavailable?: () => Error; }; /** One adapter call made with a connection's read (`runSubscriptionCoreConnectionRead`). */ export type SubscriptionCoreConnectionReadRun = /** Not readable in this context. */ | { kind: "not_visible" } | { kind: "relogin" } | { kind: "token_error"; error: unknown } /** The call threw. */ | { kind: "fetch_error"; error: unknown } | { kind: "read"; value: T }; /** What a probe reports; provider and refresh failures never throw. */ export type SubscriptionCoreUsageProbe = /** The adapter has no usage probe. */ | { kind: "unsupported" } /** Not readable in this workspace context (for example a personal connection). */ | { kind: "not_visible" } | { kind: "relogin" } | { kind: "token_error"; error: unknown } | { kind: "fetch_error"; error: unknown } | { kind: "read"; /** The adapter's decoded-or-raw usage response. */ response: unknown; /** The recorded observation, or null when the response carried none. */ quota: SubscriptionQuota | null; /** * The observation was written to the connection's quota (false when the * response carried none, the refresh-generation fence refused it, or * the write failed). */ applied: boolean; /** Why it was not written (absent when it was). */ notApplied?: SubscriptionCoreUsageNotApplied; /** The error of a failed write (`notApplied: "write_failed"`). */ writeError?: unknown; /** The observation ended a stored exhaustion (the caller wakes waiters). */ recovered: boolean; }; export type SubscriptionCoreLiveModelsProbe = | { kind: "unsupported" } | { kind: "not_visible" } | { kind: "relogin" } | { kind: "error"; error: unknown } | { kind: "read"; modelIds: readonly string[] }; export type SubscriptionCoreConnectionProbeDeps = { /** The physical transport under request custody (default: global fetch). */ fetchImpl?: SubscriptionCoreFetch; /** A caller-built custody fetch (replaces the core's for this read). */ requestFetch?: SubscriptionCoreFetch; resolver?: SubscriptionCoreConnectionResolverDeps; /** * The product models the connection can serve (the caller's catalog when * the usage is read), passed to the adapter's `decodeQuota` so a limit of * a model group reaches each listed model. Absent: no catalog. */ modelIds?: readonly string[]; }; class ProbeUnavailable extends Error {} class ProbeRelogin extends Error {} /** The connection-level operations runtime bound to one provider (memoized per binding). */ export const subscriptionCoreOperationConnections = memoByProvider( (provider: SubscriptionCoreProvider) => { const providerId = subscriptionCoreProviderId(provider); const operations = () => subscriptionCoreOperations(provider); /** * Shared organization- or workspace-scoped connections that can serve an * operation in this workspace, in placement order: the session's explicit * choice (session scope only), then the effective primary, then pool * order. Only active, allocatable connections that reach the workspace * qualify, exactly as for its chat turns. */ async function listSubscriptionCoreOperationCandidates( db: Database, scope: Exclude, ): Promise { const access = await operations().withOperationScope(db, scope, async (tx) => { const pool = await readSubscriptionCoreWorkspacePool(tx, provider, scope); if (pool.source.effectiveSource === "disabled") return []; const eligible = pool.connections.filter( (entry) => entry.row.status === "active" && entry.row.allocator_enabled, ); // A workspace-scoped connection listed only because personal // workspaces may use it is not assigned here: operations refuse it. const assigned = await assignedConnectionIds( tx, scope, eligible.map((entry) => entry.row.id), ); let explicitId: string | null = null; if (scope.kind === "session") { const binding = await readSubscriptionSessionBinding(tx, { ...scope, provider: providerId, }).catch(() => null); if (binding?.choice === "explicit") { explicitId = binding.connectionId; } } const rank = (id: string) => id === explicitId ? 0 : id === pool.primaryConnectionId ? 1 : 2; return eligible .filter((entry) => assigned.has(entry.row.id)) .map((entry, index) => ({ entry, index })) .sort((a, b) => rank(a.entry.row.id) - rank(b.entry.row.id) || a.index - b.index) .map(({ entry }) => ({ connectionId: entry.row.id, planType: entry.row.plan_type, explicit: entry.row.id === explicitId, allowedModelIds: entry.row.allowed_model_ids ?? null, })); }); return access?.value ?? []; } /** * The workspace's shared pool of this provider as a status probe sees it * (no credential material): its connections in the effective pool and * the effective primary, if it is one of them. Personal connections are * never listed. */ async function readSubscriptionCoreWorkspaceConnections( db: Database, scope: Extract, ): Promise<{ connections: Array<{ connectionId: string; label: string | null; providerAccountId: string | null; source: "workspace" | "organization"; status: string; }>; primaryConnectionId: string | null; }> { const access = await operations().withOperationScope(db, scope, async (tx) => { const pool = await readSubscriptionCoreWorkspacePool(tx, provider, scope); const connections = pool.connections.map((entry) => ({ connectionId: entry.row.id, label: entry.row.label, providerAccountId: entry.row.provider_account_id, source: entry.source, status: entry.row.status, })); return { connections, primaryConnectionId: connections.some( (connection) => connection.connectionId === pool.primaryConnectionId, ) ? pool.primaryConnectionId : null, }; }); return access?.value ?? { connections: [], primaryConnectionId: null }; } /** * Bearer resolver for one operation (or connection read): the same * staleness refresh as chat, with process-wide single-flight per * connection and generation (the shared core resolver, in its own flight * namespace). Only connection-level outcomes are shared; a refused * authorization belongs to the operation that hit it. */ function buildSubscriptionCoreConnectionTokenResolver( db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, connectionId: string, ref: SubscriptionCoreOperationLeaseRef | null, deps: SubscriptionCoreConnectionResolverDeps = {}, ): { getToken: () => Promise; getFreshToken: () => Promise; refresh: () => Promise; } { const load = deps.load ?? operations().loadSubscriptionCoreConnectionCredential; const refreshCredential = deps.refreshCredential ?? operations().refreshSubscriptionCoreConnectionCredential; const unavailable = deps.unavailable ?? (() => provider.errors.operationUnavailable()); const snapshot = ( loaded: SubscriptionCoreConnectionCredential, credential: unknown = loaded.credential, credentialVersion = loaded.refreshGeneration, ): SubscriptionCoreConnectionToken => ({ credential, providerAccountId: loaded.providerAccountId, providerState: loaded.providerState, planType: loaded.planType, credentialVersion, }); return buildSubscriptionCoreCredentialResolver< SubscriptionCoreConnectionCredential, Extract, SubscriptionCoreConnectionToken >({ flightNamespace: "connection", connectionId, holderKey: ref ? `${ref.operationId}:${ref.holderId}:${ref.generation}` : `connection:${connectionId}`, policy: (loaded) => subscriptionCoreRefreshPolicy(provider.adapter, loaded.credential), load: () => load(db, settings, scope, connectionId, ref), embeddedExpiry: (loaded) => provider.adapter.credential.expiry(loaded.credential), refresh: (loaded) => refreshCredential(db, settings, scope, connectionId, ref, loaded.refreshGeneration, deps), snapshot: (loaded) => snapshot(loaded), refreshedSnapshot: (outcome, loaded) => snapshot(loaded, outcome.credential, outcome.refreshGeneration), errors: { relogin: (message) => deps.relogin?.(message) ?? new SubscriptionCoreError( "subscription_core_relogin_required", provider.adapter.reloginText(message ?? ""), ), leaseLost: unavailable, accessLost: unavailable, }, }); } /** * Run one operation on the first candidate whose operation lease is * granted: take the lease, hand `run` a resolver, a custody fetch and a * pre-dispatch fence on that exact lease, and release the lease after * `run` settles (its result already parsed). `unavailable` when no * candidate's lease was granted; failures inside `run` are final for * the operation (never retried on another connection). */ async function runSubscriptionCoreOperation( db: Database, settings: Settings, scope: SubscriptionCoreOperationScope, input: { candidates: readonly string[]; operationKind: SubscriptionOperationKind; holderId: string; /** Defaults to a fresh operation id; the lease row is keyed by it. */ operationId?: string; attemptId?: string; generation?: number; /** A durable logical request identity for custody (default: random). */ requestId?: string; fetchImpl?: SubscriptionCoreFetch; }, run: (operation: { connectionId: string; ref: SubscriptionCoreOperationLeaseRef; resolver: ReturnType; fetch: SubscriptionCoreFetch; /** True while the exact operation lease is still live (renews it). */ fence: () => Promise; }) => Promise, ): Promise<{ kind: "ran"; value: T } | { kind: "unavailable" }> { const operationId = input.operationId ?? crypto.randomUUID(); const attemptId = input.attemptId ?? crypto.randomUUID(); for (const connectionId of input.candidates) { const ref: SubscriptionCoreOperationLeaseRef = { operationId, attemptId, operationKind: input.operationKind, connectionId, holderId: input.holderId.slice(0, 256), generation: input.generation ?? 1, }; const lease = await operations().acquireSubscriptionCoreOperationLease(db, scope, ref); if (lease.kind !== "acquired") continue; try { return { kind: "ran", value: await run({ connectionId, ref, resolver: buildSubscriptionCoreConnectionTokenResolver( db, settings, scope, connectionId, ref, ), fetch: operations().buildSubscriptionCoreOperationFetch( db, scope, ref, connectionId, input.fetchImpl ?? fetch, input.requestId ? { requestId: input.requestId } : {}, ), fence: async () => (await operations().renewSubscriptionCoreOperationLease(db, scope, ref)) !== null, }), }; } finally { await operations() .releaseSubscriptionCoreOperationLease(db, scope, ref) .catch(() => false); } } return { kind: "unavailable" }; } /** * A connection read for the provider's adapter (usage, live catalog) in a * workspace context: a refreshing bearer through the core seam and a * custody fetch. The first bearer is resolved before the adapter runs, so * an unreadable connection never reaches the provider. */ async function connectionRead( db: Database, settings: Settings, scope: Extract, connectionId: string, deps: SubscriptionCoreConnectionProbeDeps, ): Promise< | { kind: "ready"; read: SubscriptionCoreConnectionRead; current: () => SubscriptionCoreConnectionToken; } | { kind: "not_visible" } | { kind: "relogin" } | { kind: "token_error"; error: unknown } > { const resolver = buildSubscriptionCoreConnectionTokenResolver( db, settings, scope, connectionId, null, { ...deps.resolver, relogin: () => new ProbeRelogin(), unavailable: () => new ProbeUnavailable(), }, ); let token: SubscriptionCoreConnectionToken; try { token = await resolver.getToken(); } catch (error) { if (error instanceof ProbeUnavailable) return { kind: "not_visible" }; if (error instanceof ProbeRelogin) return { kind: "relogin" }; return { kind: "token_error", error }; } const requestFetch = deps.requestFetch ?? operations().buildSubscriptionCoreOperationFetch( db, scope, null, connectionId, deps.fetchImpl ?? fetch, ); return { kind: "ready", current: () => token, read: { // The bearer resolved above serves the first request. getToken: async () => token, refresh: async () => { token = await resolver.refresh(); return token; }, fetch: requestFetch, }, }; } /** * Live usage for one shared connection in the caller's workspace scope * (design 5.3, the core quota probe): read the provider's usage endpoint * through the adapter, decode it, and record it as a generation-fenced * quota observation. Provider and refresh failures are reported, never * thrown; a relogin is recorded on the connection by the refresh seam. */ async function probeSubscriptionCoreConnectionUsage( db: Database, settings: Settings, scope: Extract, connectionId: string, deps: SubscriptionCoreConnectionProbeDeps = {}, ): Promise { const { fetchUsage, decodeQuota } = provider.adapter; if (!fetchUsage || !decodeQuota) return { kind: "unsupported" }; const prepared = await connectionRead(db, settings, scope, connectionId, deps); if (prepared.kind !== "ready") return prepared; let response: unknown; try { response = await fetchUsage.call(provider.adapter, prepared.read); } catch (error) { return { kind: "fetch_error", error }; } const quota = decodeQuota.call(provider.adapter, { response, observedAt: Date.now(), refreshGeneration: prepared.current().credentialVersion, ...(deps.modelIds ? { modelIds: deps.modelIds } : {}), }); if (!quota) return { kind: "read", response, quota: null, applied: false, recovered: false, notApplied: "no_reading", }; let writeError: unknown; const record = await operations() .recordSubscriptionCoreUsageObservation( db, scope, connectionId, quota, subscriptionCoreUsageMergeOptions(provider.adapter, response), ) .catch((error: unknown) => { writeError = error; return subscriptionCoreUsageNotRecorded("write_failed"); }); return { kind: "read", response, quota, applied: record.applied, recovered: record.recovered, ...(record.notApplied ? { notApplied: record.notApplied } : {}), ...(writeError !== undefined ? { writeError } : {}), }; } /** The live model catalog of one shared connection through the adapter. */ async function probeSubscriptionCoreConnectionLiveModels( db: Database, settings: Settings, scope: Extract, connectionId: string, deps: SubscriptionCoreConnectionProbeDeps = {}, ): Promise { const { liveModels } = provider.adapter; if (!liveModels) return { kind: "unsupported" }; const prepared = await connectionRead(db, settings, scope, connectionId, deps); if (prepared.kind === "token_error") return { kind: "error", error: prepared.error }; if (prepared.kind !== "ready") return prepared; try { return { kind: "read", modelIds: await liveModels.call(provider.adapter, prepared.read) }; } catch (error) { return { kind: "error", error }; } } /** * One provider call of the adapter's own (a limit reset claim, for * example) made with a shared connection's read in the caller's * workspace scope: the same refreshing bearer and request custody as a * usage read. Records nothing; failures are reported, never thrown. */ async function runSubscriptionCoreConnectionRead( db: Database, settings: Settings, scope: Extract, connectionId: string, run: (read: SubscriptionCoreConnectionRead) => Promise, deps: SubscriptionCoreConnectionProbeDeps = {}, ): Promise> { const prepared = await connectionRead(db, settings, scope, connectionId, deps); if (prepared.kind !== "ready") return prepared; try { return { kind: "read", value: await run(prepared.read) }; } catch (error) { return { kind: "fetch_error", error }; } } /** * Re-read live usage before a stored exhaustion is treated as current * (design 5.3, EP-N22): each shared connection of the workspace's pool * whose stored quota blocks now (exhausted, or a model cooldown runs that * the adapter's reading can end: `subscriptionCoreQuotaBlocksNow`) and * was not observed within `minIntervalMs` is probed on its own, in the * caller's explicit workspace context. Probe failures keep the stored * state. Returns the connections whose exhaustion or cooldown ended (the * caller wakes waiters). */ async function refreshExhaustedSubscriptionCoreQuota( db: Database, settings: Settings, scope: Extract, deps: SubscriptionCoreConnectionProbeDeps & { minIntervalMs?: number; now?: number } = {}, ): Promise { if (!provider.adapter.fetchUsage || !provider.adapter.decodeQuota) return []; const now = deps.now ?? Date.now(); const minIntervalMs = deps.minIntervalMs ?? 30_000; const pool = await readSubscriptionCoreWorkspaceConnections(db, scope); const ids = pool.connections .filter((connection) => connection.status === "active") .map((connection) => connection.connectionId); if (ids.length === 0) return []; const stale = (await readStoredQuotaRows(db, scope, ids)).filter((row) => { const quota = decodeSubscriptionQuota(row); if (!subscriptionCoreQuotaBlocksNow(provider.adapter, quota, now)) return false; const updatedAt = row.quota_updated_at ? new Date(row.quota_updated_at).getTime() : 0; return now - updatedAt >= minIntervalMs; }); const recovered: string[] = []; for (const row of stale) { const probe = await probeSubscriptionCoreConnectionUsage( db, settings, scope, row.connection_id, deps, ); if (probe.kind === "read" && probe.recovered) recovered.push(row.connection_id); } return recovered; } /** The stored quota rows of `connectionIds`, read in the caller's scope. */ async function readStoredQuotaRows( db: Database, scope: Exclude, connectionIds: readonly string[], ) { if (connectionIds.length === 0) return []; const access = await operations().withOperationScope(db, scope, async (tx) => rawRows<{ connection_id: string; quota: unknown; quota_revision: number | string | null; quota_observed_refresh_generation: number | string | null; quota_updated_at: Date | string | null; }>( tx, sql`select connection_id::text as connection_id, quota, revision as quota_revision, observed_refresh_generation as quota_observed_refresh_generation, updated_at as quota_updated_at from subscription_connection_quota where account_id = ${scope.accountId}::uuid and connection_id = any(${`{${connectionIds.join(",")}}`}::uuid[])`, ), ); return access?.value ?? []; } /** * The connections among `connectionIds` whose stored quota is exhausted * now (design 5.3): a caller that commits one shared connection to work * that outlives the turn (video funding) skips them, as legacy selection * treated an exhausted account as unavailable. A connection with no * stored observation, or whose exhaustion has passed, is not exhausted. */ async function readSubscriptionCoreExhaustedConnectionIds( db: Database, scope: Exclude, connectionIds: readonly string[], now: number = Date.now(), ): Promise> { const exhausted = new Set(); for (const row of await readStoredQuotaRows(db, scope, connectionIds)) { const quota = decodeSubscriptionQuota(row); if (quota && quotaCapacity(quota, now).kind === "exhausted") { exhausted.add(row.connection_id); } } return exhausted; } return { listSubscriptionCoreOperationCandidates, readSubscriptionCoreExhaustedConnectionIds, readSubscriptionCoreWorkspaceConnections, buildSubscriptionCoreConnectionTokenResolver, runSubscriptionCoreOperation, probeSubscriptionCoreConnectionUsage, probeSubscriptionCoreConnectionLiveModels, runSubscriptionCoreConnectionRead, refreshExhaustedSubscriptionCoreQuota, }; }, ); /** Assigned (not merely personal-workspace-visible) connections among `ids`. */ async function assignedConnectionIds( tx: Database, scope: { accountId: string; workspaceId: string }, ids: readonly string[], ): Promise> { if (ids.length === 0) return new Set(); const rows = await rawRows<{ id: string }>( tx, sql`select connection.id::text as id from subscription_connections connection where connection.account_id = ${scope.accountId}::uuid and connection.id = any(${`{${ids.join(",")}}`}::uuid[]) and connection.ownership = 'shared' and connection.status = 'active' and (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 = ${scope.workspaceId}::uuid)))`, ); return new Set(rows.map((row) => row.id)); }