Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(control-api): reconcile Codex subscription catalog
Alfredo Lopezee76d84c6df2
diff --git a/control-api/src/routes/admin/codexSubscription.ts b/control-api/src/routes/admin/codexSubscription.tsindex fea7e4a70..95341f264 100644--- a/control-api/src/routes/admin/codexSubscription.ts+++ b/control-api/src/routes/admin/codexSubscription.ts@@ -4,6 +4,12 @@ import { pool } from '../../db.js'import { asyncHandler } from '../../http/asyncHandler.js'import { deriveOAuthEncryptionKey } from '../../oauth/encryption.js'import { rootLogger } from '../../observability/logger.js'+import {+ type CodexCatalogTransport,+ createUnavailableCodexCatalogTransport,+ syncCodexSubscriptionCatalog,+} from '../../services/codexSubscriptionCatalog.js'+import { loadCodexSubscriptionSecrets } from '../../services/codexSubscriptionConnection.js'import {type CodexOAuthDeps,CodexSubscriptionOAuthError,@@ -77,7 +83,9 @@ function sendOAuthError(throw err}-export function createAdminCodexSubscriptionRouter(): Router {+export function createAdminCodexSubscriptionRouter(+ catalogTransport: CodexCatalogTransport = createUnavailableCodexCatalogTransport()+): Router {const router = Router()router.get(@@ -144,6 +152,37 @@ export function createAdminCodexSubscriptionRouter(): Router {}))+ router.post(+ `${BASE}/catalog/sync`,+ asyncHandler(async (req, res) => {+ try {+ if (!config.codexSubscriptionEnabled) {+ res.status(404).json({ error: 'disabled' })+ return+ }+ const db = dbClient()+ const secrets = await loadCodexSubscriptionSecrets(+ db,+ deriveOAuthEncryptionKey(config.oauthEncryptionKey)+ )+ if (!secrets?.accessToken) {+ res.status(404).json({ error: 'no_grant' })+ return+ }+ const synced = await syncCodexSubscriptionCatalog(db, catalogTransport, secrets.accessToken)+ res.status(200).json({+ outcome: synced.outcome,+ added: synced.added,+ refreshed: synced.refreshed,+ staled: synced.staled,+ connection: synced.connection,+ })+ } catch (err) {+ sendOAuthError(res, err)+ }+ })+ )+router.post(`${BASE}/revoke`,asyncHandler(async (req, res) => {diff --git a/control-api/src/services/codexSubscriptionCatalog.ts b/control-api/src/services/codexSubscriptionCatalog.tsnew file mode 100644index 000000000..0532e89fa--- /dev/null+++ b/control-api/src/services/codexSubscriptionCatalog.ts@@ -0,0 +1,221 @@+import type { DbClient } from '../db.js'+import { rootLogger } from '../observability/logger.js'+import {+ type CodexSubscriptionCatalogStatus,+ type CodexSubscriptionConnectionStatus,+ type CodexSubscriptionSafeConnection,+ getSafeCodexSubscriptionConnection,+ recordCodexCatalogOutcome,+} from './codexSubscriptionConnection.js'++const log = rootLogger.child({ module: 'codex-subscription-catalog' })+const PROVIDER = 'codex-subscription'++export type CodexCatalogOutcome = 'ready' | 'auth-rejected' | 'unavailable'++export type CodexDiscoveredModel = {+ model: string+ displayName?: string+ contextWindowTokens?: number+}++export type CodexCatalogTransportResult =+ | { outcome: 'ready'; models: CodexDiscoveredModel[] }+ | { outcome: 'auth-rejected' }+ | { outcome: 'unavailable' }++export type CodexCatalogTransport = {+ listModels(input: { accessToken: string }): Promise<CodexCatalogTransportResult>+}++export type CodexCatalogRow = {+ model: string+ source: 'manual' | 'discovery'+ enabled: boolean+ stale: boolean+}++export type CodexCatalogPlan = {+ inserts: CodexDiscoveredModel[]+ refresh: string[]+ stale: string[]+ catalogStatus: CodexCatalogOutcome+ connectionStatus?: Extract<CodexSubscriptionConnectionStatus, 'reauth_required'>+ mutateRows: boolean+}++export function planCodexCatalogReconcile(+ existing: CodexCatalogRow[],+ result: CodexCatalogTransportResult+): CodexCatalogPlan {+ if (result.outcome === 'auth-rejected') {+ return {+ inserts: [],+ refresh: [],+ stale: [],+ catalogStatus: 'auth-rejected',+ connectionStatus: 'reauth_required',+ mutateRows: false,+ }+ }+ if (result.outcome === 'unavailable') {+ return {+ inserts: [],+ refresh: [],+ stale: [],+ catalogStatus: 'unavailable',+ mutateRows: false,+ }+ }++ const discovered = new Map(result.models.map(model => [model.model, model]))+ const inserts: CodexDiscoveredModel[] = []+ const refresh: string[] = []+ const stale: string[] = []+ for (const model of discovered.values()) {+ const row = existing.find(candidate => candidate.model === model.model)+ if (!row) {+ inserts.push(model)+ continue+ }+ if (row.source === 'manual') continue+ refresh.push(row.model)+ }+ for (const row of existing) {+ if (row.source !== 'discovery') continue+ if (!discovered.has(row.model)) stale.push(row.model)+ }+ return {+ inserts,+ refresh,+ stale,+ catalogStatus: 'ready',+ mutateRows: true,+ }+}++export async function syncCodexSubscriptionCatalog(+ db: DbClient,+ transport: CodexCatalogTransport,+ accessToken: string,+ expected?: { credentialRevision: number; catalogRevision: number }+): Promise<{+ outcome: CodexCatalogOutcome+ connection: CodexSubscriptionSafeConnection | null+ added: number+ refreshed: number+ staled: number+}> {+ const connection = await getSafeCodexSubscriptionConnection(db)+ if (!connection || connection.status === 'revoked' || connection.status === 'disconnected') {+ throw new Error('codex_subscription_not_connected')+ }+ const expectedCredentialRevision = expected?.credentialRevision ?? connection.credentialRevision+ const expectedCatalogRevision = expected?.catalogRevision ?? connection.catalogRevision+ const result = await transport.listModels({ accessToken })+ const existing = await loadCodexRows(db)+ const plan = planCodexCatalogReconcile(existing, result)+ const recorded = await recordCodexCatalogOutcome(db, {+ catalogStatus: plan.catalogStatus as CodexSubscriptionCatalogStatus,+ connectionStatus: plan.connectionStatus,+ expectedCredentialRevision,+ expectedCatalogRevision,+ })+ let added = 0+ let refreshed = 0+ let staled = 0+ if (recorded && plan.mutateRows) {+ added = await insertDiscovered(db, plan.inserts)+ refreshed = await refreshDiscovered(db, plan.refresh)+ staled = await staleMissing(db, plan.stale)+ }+ if (!recorded) {+ log.warn(+ { event: 'codex_catalog_stale_writer' },+ 'catalog outcome lost the connection revision race'+ )+ } else {+ log.info(+ {+ event: 'codex_catalog_reconciled',+ outcome: plan.catalogStatus,+ added,+ refreshed,+ staled,+ },+ 'Codex catalog reconciled'+ )+ }+ return {+ outcome: plan.catalogStatus,+ connection: recorded,+ added,+ refreshed,+ staled,+ }+}++export function createUnavailableCodexCatalogTransport(): CodexCatalogTransport {+ return {+ async listModels() {+ return { outcome: 'unavailable' }+ },+ }+}++async function loadCodexRows(db: DbClient): Promise<CodexCatalogRow[]> {+ const result = await db.query(+ `SELECT model, source, enabled, stale+ FROM llm_allowed_models+ WHERE provider = $1`,+ [PROVIDER]+ )+ return (result.rows as Array<Record<string, unknown>>).map(row => ({+ model: String(row.model),+ source: row.source === 'manual' ? 'manual' : 'discovery',+ enabled: row.enabled === true,+ stale: row.stale === true,+ }))+}++async function insertDiscovered(db: DbClient, models: CodexDiscoveredModel[]): Promise<number> {+ let added = 0+ for (const model of models) {+ const result = await db.query(+ `INSERT INTO llm_allowed_models+ (provider, model, enabled, source, discovered_at, last_seen_at, stale, display_name, context_window_tokens, vendor)+ VALUES ($1, $2, false, 'discovery', NOW(), NOW(), false, $3, $4, 'OpenAI')+ ON CONFLICT (provider, model) DO NOTHING`,+ [PROVIDER, model.model, model.displayName ?? null, model.contextWindowTokens ?? null]+ )+ added += result.rowCount ?? 0+ }+ return added+}++async function refreshDiscovered(db: DbClient, models: string[]): Promise<number> {+ if (models.length === 0) return 0+ const result = await db.query(+ `UPDATE llm_allowed_models+ SET last_seen_at = NOW(),+ stale = false+ WHERE provider = $1+ AND source = 'discovery'+ AND model = ANY($2::text[])`,+ [PROVIDER, models]+ )+ return result.rowCount ?? 0+}++async function staleMissing(db: DbClient, models: string[]): Promise<number> {+ if (models.length === 0) return 0+ const result = await db.query(+ `UPDATE llm_allowed_models+ SET stale = true+ WHERE provider = $1+ AND source = 'discovery'+ AND model = ANY($2::text[])`,+ [PROVIDER, models]+ )+ return result.rowCount ?? 0+}diff --git a/control-api/src/services/codexSubscriptionConnection.ts b/control-api/src/services/codexSubscriptionConnection.tsindex c658cb9cd..2f5dd539f 100644--- a/control-api/src/services/codexSubscriptionConnection.ts+++ b/control-api/src/services/codexSubscriptionConnection.ts@@ -328,6 +328,38 @@ export async function loadCodexSubscriptionSecrets(}}+export async function recordCodexCatalogOutcome(+ db: DbClient,+ input: {+ catalogStatus: CodexSubscriptionCatalogStatus+ connectionStatus?: CodexSubscriptionConnectionStatus+ expectedCredentialRevision: number+ expectedCatalogRevision: number+ }+): Promise<CodexSubscriptionSafeConnection | null> {+ const result = await db.query(+ `UPDATE codex_subscription_connections+ SET catalog_status = $1,+ status = COALESCE($2, status),+ catalog_revision = catalog_revision + 1,+ catalog_synced_at = now(),+ updated_at = now()+ WHERE connection_key = $3+ AND credential_revision = $4+ AND catalog_revision = $5+ RETURNING ${SAFE_CONNECTION_COLUMNS}`,+ [+ input.catalogStatus,+ input.connectionStatus ?? null,+ CODEX_SUBSCRIPTION_CONNECTION_KEY,+ input.expectedCredentialRevision,+ input.expectedCatalogRevision,+ ]+ )+ const row = result.rows[0] as SafeConnectionRow | undefined+ return row ? toSafeConnection(row) : null+}+function toSafeConnection(row: SafeConnectionRow): CodexSubscriptionSafeConnection {return {connectionKey: CODEX_SUBSCRIPTION_CONNECTION_KEY,diff --git a/control-api/src/services/llmAllowedModels.ts b/control-api/src/services/llmAllowedModels.tsindex ff9c8e114..dfb979a23 100644--- a/control-api/src/services/llmAllowedModels.ts+++ b/control-api/src/services/llmAllowedModels.ts@@ -472,6 +472,7 @@ export async function listEnabledGroupedByProvider(`SELECT provider, model, vendor, display_name, context_window_tokensFROM llm_allowed_modelsWHERE enabled+ AND NOT (provider = 'codex-subscription' AND stale)ORDER BY provider ASC, model ASC`)// Null-prototype map so a stray/legacy `provider` value equal to andiff --git a/control-api/test/helpers/llmCatalogSyncFakeDb.ts b/control-api/test/helpers/llmCatalogSyncFakeDb.tsindex c93742d17..2f4db88cd 100644--- a/control-api/test/helpers/llmCatalogSyncFakeDb.ts+++ b/control-api/test/helpers/llmCatalogSyncFakeDb.ts@@ -74,14 +74,15 @@ export function makeFakeDb(seed: SeedRow[] = []): FakeDb {return { rows: out, rowCount: out.length }}- // Materializer read (enabled rows only, stale IGNORED).+ // Materializer read (enabled rows). Static stale+enabled still+ // materializes; Codex stale targets stay visible in DB but are not executable.if (/SELECT provider, model, vendor, display_name, context_window_tokens\s+FROM llm_allowed_models\s+WHERE enabled/.test(sql)) {const out = rows- .filter(r => r.enabled)+ .filter(r => r.enabled && !(r.provider === 'codex-subscription' && r.stale)).sort((a, b) => a.provider.localeCompare(b.provider) || a.model.localeCompare(b.model)).map(r => ({provider: r.provider,diff --git a/control-api/test/services.codexSubscriptionCatalog.realPostgres.integration.test.ts b/control-api/test/services.codexSubscriptionCatalog.realPostgres.integration.test.tsnew file mode 100644index 000000000..86e475fe1--- /dev/null+++ b/control-api/test/services.codexSubscriptionCatalog.realPostgres.integration.test.ts@@ -0,0 +1,95 @@+import { afterAll, beforeAll, describe, expect, it } from 'vitest'+import { createHash, randomBytes } from 'node:crypto'+import { Pool } from 'pg'+import { initDb } from '../src/db.js'+import { deriveOAuthEncryptionKey } from '../src/oauth/encryption.js'+import { syncCodexSubscriptionCatalog } from '../src/services/codexSubscriptionCatalog.js'+import { insertInitialCodexSubscriptionConnection } from '../src/services/codexSubscriptionConnection.js'++const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL+const describeRealPostgres = adminUrl ? describe : describe.skip+const KEY = deriveOAuthEncryptionKey('ab'.repeat(32))++function databaseUrl(baseUrl: string, database: string): string {+ const url = new URL(baseUrl)+ url.pathname = `/${database}`+ return url.toString()+}++function quoteIdent(value: string): string {+ return `"${value.replace(/"/g, '""')}"`+}++describeRealPostgres('Codex subscription catalog on real PostgreSQL', () => {+ const database = `codex_catalog_${randomBytes(6).toString('hex')}`+ let adminPool: Pool+ let pool: Pool++ beforeAll(async () => {+ adminPool = new Pool({ connectionString: adminUrl })+ await adminPool.query(`CREATE DATABASE ${quoteIdent(database)}`)+ pool = new Pool({ connectionString: databaseUrl(adminUrl!, database) })+ await initDb({ connect: () => pool.connect() })+ await insertInitialCodexSubscriptionConnection(pool, KEY, {+ refreshToken: 'catalog-refresh',+ accountFingerprint: createHash('sha256').update('acct-catalog', 'utf8').digest('hex'),+ })+ }, 60_000)++ afterAll(async () => {+ await pool?.end()+ if (adminPool) {+ await adminPool+ .query(`DROP DATABASE IF EXISTS ${quoteIdent(database)}`)+ .catch(() => undefined)+ await adminPool.end()+ }+ })++ it('ready stales missing discovery rows and a stale writer cannot stale a newer connection', async () => {+ const ready = await syncCodexSubscriptionCatalog(+ pool,+ {+ async listModels() {+ return { outcome: 'ready', models: [{ model: 'gpt-5' }, { model: 'gpt-5.3-codex' }] }+ },+ },+ 'access-token'+ )+ expect(ready.outcome).toBe('ready')+ expect(ready.added).toBe(2)+ expect(ready.connection?.catalogStatus).toBe('ready')++ const next = await syncCodexSubscriptionCatalog(+ pool,+ {+ async listModels() {+ return { outcome: 'ready', models: [{ model: 'gpt-5' }] }+ },+ },+ 'access-token'+ )+ expect(next.staled).toBe(1)+ const stale = await pool.query<{ stale: boolean }>(+ `SELECT stale FROM llm_allowed_models WHERE provider = 'codex-subscription' AND model = 'gpt-5.3-codex'`+ )+ expect(stale.rows[0]?.stale).toBe(true)++ const lost = await syncCodexSubscriptionCatalog(+ pool,+ {+ async listModels() {+ return { outcome: 'ready', models: [] }+ },+ },+ 'access-token',+ { credentialRevision: 1, catalogRevision: 0 }+ )+ expect(lost.connection).toBeNull()+ expect(lost.staled).toBe(0)+ const kept = await pool.query<{ stale: boolean }>(+ `SELECT stale FROM llm_allowed_models WHERE provider = 'codex-subscription' AND model = 'gpt-5'`+ )+ expect(kept.rows[0]?.stale).toBe(false)+ })+})diff --git a/control-api/test/services.codexSubscriptionCatalog.test.ts b/control-api/test/services.codexSubscriptionCatalog.test.tsnew file mode 100644index 000000000..7e7de80ba--- /dev/null+++ b/control-api/test/services.codexSubscriptionCatalog.test.ts@@ -0,0 +1,140 @@+import { describe, expect, it, vi } from 'vitest'+import {+ planCodexCatalogReconcile,+ syncCodexSubscriptionCatalog,+} from '../src/services/codexSubscriptionCatalog.js'+import { listEnabledGroupedByProvider } from '../src/services/llmAllowedModels.js'+import { buildConfigMapData } from '../src/services/llmAllowedModelsConfigMap.js'+import { makeFakeDb } from './helpers/llmCatalogSyncFakeDb.js'++describe('planCodexCatalogReconcile', () => {+ it('ready upserts discovered rows, inserts disabled, and stales missing discovery models', () => {+ const plan = planCodexCatalogReconcile(+ [+ { model: 'gpt-5', source: 'discovery', enabled: true, stale: false },+ { model: 'legacy-codex', source: 'discovery', enabled: true, stale: false },+ { model: 'manual-keep', source: 'manual', enabled: true, stale: false },+ ],+ {+ outcome: 'ready',+ models: [+ { model: 'gpt-5', displayName: 'GPT-5' },+ { model: 'gpt-5.3-codex', displayName: 'GPT-5.3 Codex' },+ ],+ }+ )+ expect(plan.mutateRows).toBe(true)+ expect(plan.inserts.map(model => model.model)).toEqual(['gpt-5.3-codex'])+ expect(plan.refresh).toEqual(['gpt-5'])+ expect(plan.stale).toEqual(['legacy-codex'])+ expect(plan.catalogStatus).toBe('ready')+ expect(plan.connectionStatus).toBeUndefined()+ })++ it('auth-rejected preserves rows and enablement and does not stale', () => {+ const plan = planCodexCatalogReconcile(+ [{ model: 'gpt-5', source: 'discovery', enabled: true, stale: false }],+ { outcome: 'auth-rejected' }+ )+ expect(plan.mutateRows).toBe(false)+ expect(plan.stale).toEqual([])+ expect(plan.inserts).toEqual([])+ expect(plan.catalogStatus).toBe('auth-rejected')+ expect(plan.connectionStatus).toBe('reauth_required')+ })++ it('unavailable preserves rows and never substitutes a model', () => {+ const plan = planCodexCatalogReconcile(+ [{ model: 'gpt-5', source: 'discovery', enabled: true, stale: false }],+ { outcome: 'unavailable' }+ )+ expect(plan.mutateRows).toBe(false)+ expect(plan.stale).toEqual([])+ expect(plan.catalogStatus).toBe('unavailable')+ expect(plan.connectionStatus).toBeUndefined()+ })+})++describe('Codex catalog materialization', () => {+ it('keeps a stale Codex target visible in the DB but not executable in the ConfigMap', async () => {+ const db = makeFakeDb([+ {+ provider: 'codex-subscription',+ model: 'gpt-5',+ source: 'discovery',+ enabled: true,+ stale: true,+ },+ {+ provider: 'claude',+ model: 'claude-opus-4-5',+ source: 'discovery',+ enabled: true,+ stale: true,+ },+ ])+ const client = await db.connector.connect()+ const grouped = await listEnabledGroupedByProvider({ query: client.query })+ const { data, contentHash } = buildConfigMapData(grouped)+ expect(data.claude).toContain('claude-opus-4-5')+ expect(data['codex-subscription']).toBeUndefined()+ expect(db.get('codex-subscription', 'gpt-5')?.enabled).toBe(true)+ expect(contentHash).toMatch(/^[0-9a-f]{64}$/)+ })+})++describe('syncCodexSubscriptionCatalog fencing', () => {+ it('does not apply a stale writer after a newer catalog revision', async () => {+ const query = vi.fn(async (sql: string) => {+ if (/FROM codex_subscription_connections/.test(sql) && /SELECT/.test(sql)) {+ return {+ rows: [+ {+ connection_key: 'deployment-default',+ status: 'connected',+ credential_revision: 2,+ catalog_revision: 4,+ account_fingerprint: 'fp',+ catalog_status: 'ready',+ catalog_synced_at: new Date(),+ last_refresh_at: null,+ last_auth_at: null,+ refresh_lock_token: null,+ refresh_lock_expires_at: null,+ revoked_at: null,+ created_at: new Date(),+ updated_at: new Date(),+ },+ ],+ rowCount: 1,+ }+ }+ if (/FROM llm_allowed_models/.test(sql)) {+ return {+ rows: [{ model: 'gpt-5', source: 'discovery', enabled: true, stale: false }],+ rowCount: 1,+ }+ }+ if (/UPDATE llm_allowed_models[\s\S]*stale = true/.test(sql)) {+ throw new Error('stale writer must not mark missing models')+ }+ if (/UPDATE codex_subscription_connections/.test(sql)) {+ return { rows: [], rowCount: 0 }+ }+ return { rows: [], rowCount: 0 }+ })+ const result = await syncCodexSubscriptionCatalog(+ { query },+ {+ async listModels() {+ return { outcome: 'unavailable' }+ },+ },+ 'access-token',+ { credentialRevision: 1, catalogRevision: 1 }+ )+ expect(result.outcome).toBe('unavailable')+ expect(result.connection).toBeNull()+ expect(result.staled).toBe(0)+ })+})diff --git a/control-api/test/services.llmAllowedModelsConfigMap.test.ts b/control-api/test/services.llmAllowedModelsConfigMap.test.tsindex 0098fd32a..be259d9db 100644--- a/control-api/test/services.llmAllowedModelsConfigMap.test.ts+++ b/control-api/test/services.llmAllowedModelsConfigMap.test.ts@@ -70,6 +70,14 @@ describe('buildConfigMapData', () => {// The lifecycle fields never leak into the serialized entry.expect(withHash.data.zai).not.toMatch(/source|discovered_at|last_seen_at|stale/)})++ it('keeps the content-hash annotation as the drift authority while excluding stale Codex rows', async () => {+ const source = await import('node:fs').then(fs =>+ fs.readFileSync(new URL('../src/services/llmAllowedModels.ts', import.meta.url), 'utf8')+ )+ expect(source).toContain("NOT (provider = 'codex-subscription' AND stale)")+ expect(CONTENT_HASH_ANNOTATION).toBe('clerum.io/content-hash')+ })})describe('LlmAllowedModelsConfigMapWriter', () => {diff --git a/control-api/test/services.llmCatalogSync.invariants.test.ts b/control-api/test/services.llmCatalogSync.invariants.test.tsindex 7badc55e7..8d805ebfd 100644--- a/control-api/test/services.llmCatalogSync.invariants.test.ts+++ b/control-api/test/services.llmCatalogSync.invariants.test.ts@@ -87,4 +87,19 @@ describe('catalog sync — load-bearing invariants (Fase 4)', () => {expect(models).toContain('claude-opus-4-5') // stale + enabled → presentexpect(models).not.toContain('claude-sonnet-5') // disabled → absent})++ it('does not discover or stale Codex subscription rows from models.dev', async () => {+ const db = makeFakeDb([+ { provider: 'codex-subscription', model: 'gpt-5', source: 'discovery', enabled: false },+ ])+ const catalog = trimSnapshot({ anthropic: ['claude-opus-4-5'] })+ await syncDiscoveredModels(+ { loadCatalog: loadStub(catalog, 'live'), ...LOW_FLOOR },+ db.connector+ )+ expect(db.get('codex-subscription', 'gpt-5')?.stale).toBe(false)+ expect(+ db.rows.some(row => row.provider === 'codex-subscription' && row.model !== 'gpt-5')+ ).toBe(false)+ })})