Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(codex): keep Desktop streams from false-cancel and skipped 00a4

Alfredo Lopez147beb83fc66
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.ts
index d58e3ca24..ecb39611e 100644
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@ -132,7 +132,10 @@ export function createProxyApps(config: CodexLlmProxyConfig, deps: ProxyRuntimeD
void (async () => {
let release: (() => void) | undefined
const abort = new AbortController()
- req.on('close', () => abort.abort())
+ // After express.json() the incoming request is already complete. Listening
+ // to req 'close' aborts the ChatGPT hop on every call (3–12ms canceled).
+ // Abort only when the client drops the response before we finish writing.
+ abortWhenClientDisconnects(req, res, abort)
try {
release = await streamGate.acquire()
res.status(200)
@@ -300,6 +303,18 @@ function mapError(err: unknown): { status: number; code: string } {
return { status: 503, code: 'provider_unavailable' }
}
+export function abortWhenClientDisconnects(
+ req: Request,
+ res: Response,
+ abort: AbortController
+): void {
+ const cancel = () => {
+ if (!res.writableEnded) abort.abort()
+ }
+ res.on('close', cancel)
+ req.on('aborted', cancel)
+}
+
function closeServer(server: Server): Promise<void> {
return new Promise(resolve => {
server.close(() => resolve())
diff --git a/codex-llm-proxy/test/abortWhenClientDisconnects.test.ts b/codex-llm-proxy/test/abortWhenClientDisconnects.test.ts
new file mode 100644
index 000000000..7ada74ba6
--- /dev/null
+++ b/codex-llm-proxy/test/abortWhenClientDisconnects.test.ts
@@ -0,0 +1,39 @@
+import { EventEmitter } from 'node:events'
+import { describe, expect, it } from 'vitest'
+import { abortWhenClientDisconnects } from '../src/server.js'
+
+function fakeReqRes(): {
+ req: EventEmitter & { on: EventEmitter['on'] }
+ res: EventEmitter & { writableEnded: boolean }
+} {
+ const req = new EventEmitter()
+ const res = Object.assign(new EventEmitter(), { writableEnded: false })
+ return { req, res }
+}
+
+describe('abortWhenClientDisconnects', () => {
+ it('does not abort when the request body stream closes after JSON parse', () => {
+ const { req, res } = fakeReqRes()
+ const abort = new AbortController()
+ abortWhenClientDisconnects(req as never, res as never, abort)
+ req.emit('close')
+ expect(abort.signal.aborted).toBe(false)
+ })
+
+ it('aborts when the client drops the response before it ends', () => {
+ const { req, res } = fakeReqRes()
+ const abort = new AbortController()
+ abortWhenClientDisconnects(req as never, res as never, abort)
+ res.emit('close')
+ expect(abort.signal.aborted).toBe(true)
+ })
+
+ it('does not abort after the proxy finished writing the SSE response', () => {
+ const { req, res } = fakeReqRes()
+ const abort = new AbortController()
+ abortWhenClientDisconnects(req as never, res as never, abort)
+ res.writableEnded = true
+ res.emit('close')
+ expect(abort.signal.aborted).toBe(false)
+ })
+})
diff --git a/control-api/src/db.ts b/control-api/src/db.ts
index 914ac06c3..b18af5cd8 100644
--- a/control-api/src/db.ts
+++ b/control-api/src/db.ts
@@ -6140,14 +6140,16 @@ export async function initDb(db: DbConnector = pool): Promise<void> {
}
export async function assertDbReady(db: DbClient = pool): Promise<void> {
- const requiredVersion = CONTROL_API_MIGRATIONS.at(-1)?.version
- if (!requiredVersion) throw new Error('Control API database has no registered migrations')
- const result = await db.query(
- 'SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version = $1) AS ready',
- [requiredVersion]
- )
- if (!(result.rows[0] as { ready?: boolean } | undefined)?.ready) {
- throw new Error(`Control API database is not ready: migration ${requiredVersion} is required`)
+ if (!CONTROL_API_MIGRATIONS.length) {
+ throw new Error('Control API database has no registered migrations')
+ }
+ const applied = await loadAppliedMigrationVersions(db)
+ const missing = CONTROL_API_MIGRATIONS.filter(migration => {
+ if (applied.has(migration.version)) return false
+ return !migration.legacyVersions?.some(version => applied.has(version))
+ }).map(migration => migration.version)
+ if (missing.length) {
+ throw new Error(`Control API database is not ready: missing migrations ${missing.join(', ')}`)
}
}
diff --git a/control-api/test/db.ready.test.ts b/control-api/test/db.ready.test.ts
index 40464ce29..a9b5acf49 100644
--- a/control-api/test/db.ready.test.ts
+++ b/control-api/test/db.ready.test.ts
@@ -2,22 +2,40 @@ import { describe, expect, it, vi } from 'vitest'
import { CONTROL_API_MIGRATIONS, assertDbReady } from '../src/db.js'
const LATEST_MIGRATION = CONTROL_API_MIGRATIONS.at(-1)!.version
+const CODEX_ACCOUNT_ID_MIGRATION = '00a4_codex_chatgpt_account_id'
describe('assertDbReady', () => {
- it('accepts a database migrated through the authoritative GFS runtime role contract', async () => {
- const query = vi.fn().mockResolvedValue({ rows: [{ ready: true }], rowCount: 1 })
+ it('accepts a database that recorded every registered migration', async () => {
+ const query = vi.fn().mockResolvedValue({
+ rows: CONTROL_API_MIGRATIONS.map(migration => ({ version: migration.version })),
+ rowCount: CONTROL_API_MIGRATIONS.length,
+ })
await expect(assertDbReady({ query })).resolves.toBeUndefined()
- expect(query).toHaveBeenCalledWith(expect.stringContaining('schema_migrations'), [
- LATEST_MIGRATION,
- ])
+ expect(query).toHaveBeenCalledWith(expect.stringContaining('schema_migrations'))
})
- it('fails closed when the migration job has not applied the required schema', async () => {
- const query = vi.fn().mockResolvedValue({ rows: [{ ready: false }], rowCount: 1 })
+ it('fails closed when the latest migration is absent', async () => {
+ const query = vi.fn().mockResolvedValue({
+ rows: CONTROL_API_MIGRATIONS.filter(migration => migration.version !== LATEST_MIGRATION).map(
+ migration => ({ version: migration.version })
+ ),
+ rowCount: CONTROL_API_MIGRATIONS.length - 1,
+ })
+
+ await expect(assertDbReady({ query })).rejects.toThrow(`missing migrations ${LATEST_MIGRATION}`)
+ })
+
+ it('fails closed when an inserted Codex migration is absent after a later version landed', async () => {
+ const query = vi.fn().mockResolvedValue({
+ rows: CONTROL_API_MIGRATIONS.filter(
+ migration => migration.version !== CODEX_ACCOUNT_ID_MIGRATION
+ ).map(migration => ({ version: migration.version })),
+ rowCount: CONTROL_API_MIGRATIONS.length - 1,
+ })
await expect(assertDbReady({ query })).rejects.toThrow(
- `migration ${LATEST_MIGRATION} is required`
+ `missing migrations ${CODEX_ACCOUNT_ID_MIGRATION}`
)
})
})
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 7f6e1cb32..70e701bcf 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.277",
+ "version": "0.1.278",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.277",
+ "version": "0.1.278",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 1908c8523..b34ffdd5f 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.277",
+ "version": "0.1.278",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
index 8820056ac..c6a5548ca 100644
--- a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
@@ -86,6 +86,45 @@ describe('CodexLlmProxyClient', () => {
})
})
+ it('refreshes the platform JWT once and retries the proxy hop after HTTP 401', async () => {
+ let jwt = 'stale-jwt'
+ const refreshOnUnauthorized = vi.fn(async () => {
+ jwt = 'fresh-jwt'
+ })
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce({
+ ok: false,
+ status: 401,
+ json: async () => ({ error: 'Unauthorized' }),
+ })
+ .mockResolvedValueOnce({
+ ok: true,
+ body: sse([
+ { type: 'text', text: 'ok' },
+ { type: 'done', outcome: 'success' },
+ ]),
+ })
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: resolveCodexProxyRuntimeUrl(
+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'
+ ),
+ readPlatformJwt: () => jwt,
+ refreshOnUnauthorized,
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const result = await client.stream({
+ executionTicket: 'ticket-123456',
+ requestHash: 'a'.repeat(64),
+ request: {},
+ })
+ expect(refreshOnUnauthorized).toHaveBeenCalledTimes(1)
+ expect(fetchFn).toHaveBeenCalledTimes(2)
+ expect(fetchFn.mock.calls[0][1].headers.authorization).toBe('Bearer stale-jwt')
+ expect(fetchFn.mock.calls[1][1].headers.authorization).toBe('Bearer fresh-jwt')
+ expect(result.text).toBe('ok')
+ })
+
it('fails closed when the proxy emits an SSE error frame after headers', async () => {
const client = new CodexLlmProxyClient({
runtimeUrl: resolveCodexProxyRuntimeUrl(
diff --git a/mcp-host/src/llm/__tests__/codexPlatformJwt.test.ts b/mcp-host/src/llm/__tests__/codexPlatformJwt.test.ts
new file mode 100644
index 000000000..d6fdf1d21
--- /dev/null
+++ b/mcp-host/src/llm/__tests__/codexPlatformJwt.test.ts
@@ -0,0 +1,35 @@
+import { afterEach, describe, expect, it } from 'vitest'
+import {
+ readCodexPlatformJwt,
+ refreshCodexPlatformJwt,
+ setCodexPlatformJwtReader,
+ setCodexPlatformJwtRefresh,
+} from '../codexPlatformJwt'
+
+describe('codexPlatformJwt', () => {
+ afterEach(() => {
+ setCodexPlatformJwtReader(() => (process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN || '').trim())
+ setCodexPlatformJwtRefresh(async () => undefined)
+ })
+
+ it('reads the live runtime holder instead of a frozen boot env token', () => {
+ const previous = process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN
+ process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN = 'boot-env-token'
+ try {
+ setCodexPlatformJwtReader(() => 'rotated-runtime-token')
+ expect(readCodexPlatformJwt()).toBe('rotated-runtime-token')
+ } finally {
+ if (previous === undefined) delete process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN
+ else process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN = previous
+ }
+ })
+
+ it('invokes the registered refresh hook', async () => {
+ let refreshed = false
+ setCodexPlatformJwtRefresh(async () => {
+ refreshed = true
+ })
+ await refreshCodexPlatformJwt()
+ expect(refreshed).toBe(true)
+ })
+})
diff --git a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
index babd3e371..53f63847d 100644
--- a/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
+++ b/mcp-host/src/llm/__tests__/providerAttemptAuthorizer.test.ts
@@ -94,4 +94,41 @@ describe('ProviderAttemptAuthorizer', () => {
})
).rejects.toMatchObject({ code: 'insufficient_scope' })
})
+
+ it('refreshes the platform JWT once and retries authorize after HTTP 401', async () => {
+ let jwt = 'stale-jwt'
+ const refreshOnUnauthorized = vi.fn(async () => {
+ jwt = 'fresh-jwt'
+ })
+ const fetchFn = vi
+ .fn()
+ .mockResolvedValueOnce({
+ ok: false,
+ status: 401,
+ json: async () => ({ error: 'Unauthorized' }),
+ })
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => validAuthorize,
+ })
+ const authorizer = new ProviderAttemptAuthorizer({
+ authorizeUrl: resolveCodexAuthorizeUrl('http://gateway:8092'),
+ readPlatformJwt: () => jwt,
+ refreshOnUnauthorized,
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const result = await authorizer.authorize({
+ request: { schemaVersion: 'codex-completion-request.v1' },
+ invocationId: 'inv-1',
+ attemptGeneration: 1,
+ providerAttemptIndex: 1,
+ policyRevision: 1,
+ policyHash: 'b'.repeat(64),
+ })
+ expect(refreshOnUnauthorized).toHaveBeenCalledTimes(1)
+ expect(fetchFn).toHaveBeenCalledTimes(2)
+ expect(fetchFn.mock.calls[0][1].headers.authorization).toBe('Bearer stale-jwt')
+ expect(fetchFn.mock.calls[1][1].headers.authorization).toBe('Bearer fresh-jwt')
+ expect(result.executionTicket).toBe('ticket-123456')
+ })
})
diff --git a/mcp-host/src/llm/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.ts
index 5b23d6c1f..940a174a4 100644
--- a/mcp-host/src/llm/codexLlmProxyClient.ts
+++ b/mcp-host/src/llm/codexLlmProxyClient.ts
@@ -31,6 +31,7 @@ export type CodexProxyStreamResult = {
export type CodexLlmProxyClientOptions = {
runtimeUrl: string
readPlatformJwt: () => string
+ refreshOnUnauthorized?: () => Promise<void>
fetchFn?: typeof fetch
}
@@ -51,6 +52,19 @@ export class CodexLlmProxyClient {
if (input.signal?.aborted) {
throw new CodexProxyError('canceled', 'aborted before proxy stream')
}
+ return this.streamOnce(input, Boolean(this.options.refreshOnUnauthorized))
+ }
+
+ private async streamOnce(
+ input: {
+ executionTicket: string
+ requestHash: string
+ request: unknown
+ deadlineMs?: number
+ signal?: AbortSignal
+ },
+ retryOnUnauthorized: boolean
+ ): Promise<CodexProxyStreamResult> {
const jwt = this.options.readPlatformJwt()
const fetchFn = this.options.fetchFn ?? fetch
const response = await fetchFn(this.options.runtimeUrl, {
@@ -68,6 +82,10 @@ export class CodexLlmProxyClient {
signal: input.signal,
})
if (!response.ok) {
+ if (response.status === 401 && retryOnUnauthorized && this.options.refreshOnUnauthorized) {
+ await this.options.refreshOnUnauthorized()
+ return this.streamOnce(input, false)
+ }
const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>
const code = typeof payload.error === 'string' ? payload.error : 'provider_unavailable'
throw new CodexProxyError(code, `proxy stream failed with ${response.status} (${code})`)
diff --git a/mcp-host/src/llm/codexPlatformJwt.ts b/mcp-host/src/llm/codexPlatformJwt.ts
new file mode 100644
index 000000000..e9c664079
--- /dev/null
+++ b/mcp-host/src/llm/codexPlatformJwt.ts
@@ -0,0 +1,28 @@
+/**
+ * Live mcp-host runtime JWT for Codex authorize/proxy hops.
+ *
+ * The boot env token expires (~300s). Heartbeat, usage, and approvals already
+ * refresh `runtimeAuth.accessToken` in place. Codex must read that holder —
+ * a frozen `process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN` 401s after the first
+ * access-token lifetime.
+ */
+
+let reader: () => string = () => (process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN || '').trim()
+let refresh: (() => Promise<void>) | undefined
+
+export function setCodexPlatformJwtReader(fn: () => string): void {
+ reader = fn
+}
+
+export function setCodexPlatformJwtRefresh(fn: () => Promise<void>): void {
+ refresh = fn
+}
+
+export function readCodexPlatformJwt(): string {
+ return reader().trim()
+}
+
+export async function refreshCodexPlatformJwt(): Promise<void> {
+ if (!refresh) return
+ await refresh()
+}
diff --git a/mcp-host/src/llm/index.ts b/mcp-host/src/llm/index.ts
index 0aa33a459..bad16dd1d 100644
--- a/mcp-host/src/llm/index.ts
+++ b/mcp-host/src/llm/index.ts
@@ -5,6 +5,7 @@ import { config } from '../config'
import { ApiKeys, ModelConfig } from '../types'
import { ClaudeProvider } from './claude'
import { CodexLlmProxyClient, resolveCodexProxyRuntimeUrl } from './codexLlmProxyClient'
+import { readCodexPlatformJwt, refreshCodexPlatformJwt } from './codexPlatformJwt'
import { readLiveCodexPolicyBinding, resolveCodexAttemptPolicy } from './codexPolicyBinding'
import { OpenAIProvider } from './openai'
import { ProviderAttemptAuthorizer, resolveCodexAuthorizeUrl } from './providerAttemptAuthorizer'
@@ -20,20 +21,18 @@ export type { ClassifiedError, SingleTurnProvider } from './types'
const DEFAULT_CODEX_AUTHORIZE_GATEWAY =
'http://nginx-workflow-approval-gateway.control-plane.svc.cluster.local:8092'
-function readPlatformJwt(): string {
- return (process.env.MCP_HOST_RUNTIME_ACCESS_TOKEN || '').trim()
-}
-
function createCodexRuntimeDeps() {
const gateway = (config.mcpHostGatewayUrl ?? '').trim() || DEFAULT_CODEX_AUTHORIZE_GATEWAY
return {
authorizer: new ProviderAttemptAuthorizer({
authorizeUrl: resolveCodexAuthorizeUrl(gateway),
- readPlatformJwt,
+ readPlatformJwt: readCodexPlatformJwt,
+ refreshOnUnauthorized: refreshCodexPlatformJwt,
}),
proxy: new CodexLlmProxyClient({
runtimeUrl: resolveCodexProxyRuntimeUrl(config.codexProxyRuntimeBaseUrl),
- readPlatformJwt,
+ readPlatformJwt: readCodexPlatformJwt,
+ refreshOnUnauthorized: refreshCodexPlatformJwt,
}),
attemptContext: ({ model }: { model: string }) => {
const resolved = resolveCodexAttemptPolicy({
diff --git a/mcp-host/src/llm/providerAttemptAuthorizer.ts b/mcp-host/src/llm/providerAttemptAuthorizer.ts
index 1c2c35257..ba25b0b3c 100644
--- a/mcp-host/src/llm/providerAttemptAuthorizer.ts
+++ b/mcp-host/src/llm/providerAttemptAuthorizer.ts
@@ -30,6 +30,7 @@ export type AuthorizeAttemptBody = {
export type ProviderAttemptAuthorizerOptions = {
authorizeUrl: string
readPlatformJwt: () => string
+ refreshOnUnauthorized?: () => Promise<void>
fetchFn?: typeof fetch
}
@@ -50,6 +51,18 @@ export class ProviderAttemptAuthorizer {
requestHash: string
executionTicket: string
expiresAt: string
+ }> {
+ return this.authorizeOnce(body, Boolean(this.options.refreshOnUnauthorized))
+ }
+
+ private async authorizeOnce(
+ body: AuthorizeAttemptBody,
+ retryOnUnauthorized: boolean
+ ): Promise<{
+ providerAttemptId: string
+ requestHash: string
+ executionTicket: string
+ expiresAt: string
}> {
const jwt = this.options.readPlatformJwt()
if (!jwt) {
@@ -66,6 +79,10 @@ export class ProviderAttemptAuthorizer {
})
const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>
if (!response.ok) {
+ if (response.status === 401 && retryOnUnauthorized && this.options.refreshOnUnauthorized) {
+ await this.options.refreshOnUnauthorized()
+ return this.authorizeOnce(body, false)
+ }
const code = typeof payload.error === 'string' ? payload.error : 'provider_unavailable'
throw new CodexAuthorizeError(code, `authorize failed with ${response.status}`)
}
diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.ts
index 6c8bde1ca..5a2a5be62 100644
--- a/mcp-host/src/main.ts
+++ b/mcp-host/src/main.ts
@@ -61,6 +61,7 @@ import { TaskLifecycle } from './lifecycle/taskLifecycle'
import { isTerminal } from './lifecycle/types'
import type { TransitionEvent } from './lifecycle/types'
import { SingleTurnProvider, apiKeysFromEnv, createLLMProvider } from './llm'
+import { setCodexPlatformJwtReader, setCodexPlatformJwtRefresh } from './llm/codexPlatformJwt'
import { setCodexPolicyBindingReader } from './llm/codexPolicyBinding'
import { FailoverEngine } from './llm/failover/engine'
import { llmFallbackTotal } from './llm/failover/metrics'
@@ -215,6 +216,14 @@ let statelessHeartbeat: StatelessHeartbeat | null = null
// consumer (UsageReporter, WorkflowService) so refresh-on-401 propagates.
// Null when env is absent (dev mode without HCC/WRC).
let runtimeAuth: McpHostRuntimeAuth | null = null
+setCodexPlatformJwtReader(() =>
+ (runtimeAuth?.accessToken || config.mcpHostRuntimeAccessToken || '').trim()
+)
+setCodexPlatformJwtRefresh(async () => {
+ if (!runtimeAuth) runtimeAuth = createMcpHostRuntimeAuth()
+ if (!runtimeAuth) return
+ await refreshWithRecovery(runtimeAuth)
+})
let pluginWorkloadSdkServer: PluginWorkloadSdkServer | null = null
let pluginWorkloadSdkBootstrapServer: PluginWorkloadSdkBootstrapServer | null = null
let usageReporter: UsageReporter | null = null