Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(codex): keep proxy T0 typecheck after the origin/dev merge
Alfredo Lopezb58df1fce2f9
diff --git a/codex-llm-proxy/test/codexTransport.conformance.test.ts b/codex-llm-proxy/test/codexTransport.conformance.test.tsindex 277bd1c91..5cead2371 100644--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts@@ -1,12 +1,16 @@-import { describe, expect, it, vi } from 'vitest'import { readFileSync } from 'node:fs'+import { describe, expect, it, vi } from 'vitest'import {hashCodexCompletionRequest,hashCodexCompletionRequestV1,parseCodexCompletionRequest,parseCodexCompletionRequestV1,} from '@clerum/llm-provider-attempt-contract'-import { CodexTransportError, streamCodexCompletion } from '../src/codexTransport.js'+import {+ CodexTransportError,+ type StreamCodexCompletionInput,+ streamCodexCompletion,+} from '../src/codexTransport.js'import type { RedeemAttemptSuccess } from '../src/controlApiClient.js'import { CODEX_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'import { ToolNameMap } from '../src/toolNameMap.js'@@ -21,6 +25,12 @@ const REQUEST = {messages: [{ role: 'user' as const, content: 'hello' }],}+type FetchInput = string | URL | Request++function headerOf(init: RequestInit | undefined, name: string): string | undefined {+ return (init?.headers as Record<string, string> | undefined)?.[name]+}+const REQUEST_HASH = hashCodexCompletionRequestV1(REQUEST)function accessTokenFor(label: string): string {@@ -62,7 +72,7 @@ function sseResponse(}describe('streamCodexCompletion', () => {- it.each(['png', 'jpeg'])(+ it.each(['png', 'jpeg'] as const)('projects authorized %s parts without leaking provenance upstream',async format => {const fixtures = JSON.parse(@@ -73,20 +83,34 @@ describe('streamCodexCompletion', () => {),'utf8')- )+ ) as Record<+ string,+ {+ messages: Array<{+ contentParts: Array<{+ type: string+ text?: string+ mimeType?: string+ data?: string+ }>+ }>+ }+ >const parsed = parseCodexCompletionRequest({ ...fixtures[format], model: REQUEST.model })expect(parsed.ok).toBe(true)if (!parsed.ok) throw new Error(parsed.message)const request = parsed.valueconst requestHash = hashCodexCompletionRequest(request)- const fetchFn = vi.fn(async () => sseResponse(['data: {"type":"response.completed"}\n\n']))+ const fetchFn = vi.fn<typeof fetch>(async () =>+ sseResponse(['data: {"type":"response.completed"}\n\n'])+ )const redeem = vi.fn(async () => redeemSuccess())const finalize = vi.fn(async () => ({providerAttemptId: 'visual-attempt',outcome: 'success' as const,duplicate: false,}))- const input = {+ const input: StreamCodexCompletionInput = {executionTicket: 'visual-ticket',requestHash,request,@@ -241,7 +265,9 @@ describe('streamCodexCompletion', () => {}const original = structuredClone(request)const requestHash = hashCodexCompletionRequestV1(request)- const fetchFn = vi.fn(async () => sseResponse(['data: {"type":"response.completed"}\n\n']))+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>+ sseResponse(['data: {"type":"response.completed"}\n\n'])+ )await streamCodexCompletion({executionTicket: 'ticket-optionality',requestHash,@@ -256,7 +282,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-optionality',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,@@ -340,17 +366,19 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-arguments',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),- fetchFn: vi.fn(async () =>+ fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse([`data: ${JSON.stringify({ type: 'response.output_item.done', item: { type: 'function_call', call_id: 'call-optional', name: names.toWire(canonical), arguments: JSON.stringify(args) } })}\n\n`,'data: {"type":"response.completed"}\n\n',])),lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => frames.push(frame),+ onFrame: frame => {+ frames.push(frame)+ },})expect(frames).toEqual([{ type: 'tool_call', id: 'call-optional', name: canonical, arguments: args },@@ -383,9 +411,11 @@ describe('streamCodexCompletion', () => {outcome: 'success' as const,duplicate: false,})),- fetchFn: vi.fn(async () => sseResponse(frames)),+ fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames)),lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => emitted.push(frame),+ onFrame: frame => {+ emitted.push(frame)+ },})if (terminal === 'unterminated') expect((await pending).outcome).toBe('unknown')else await expect(pending).rejects.toThrow(/upstream response failed/)@@ -423,9 +453,11 @@ describe('streamCodexCompletion', () => {},redeem: vi.fn(async () => redeemSuccess()),finalize,- fetchFn: vi.fn(async () => sseResponse(frames)),+ fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames)),lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => emitted.push(frame),+ onFrame: frame => {+ emitted.push(frame)+ },})if (count === 32) {expect((await pending).outcome).toBe('success')@@ -447,7 +479,7 @@ describe('streamCodexCompletion', () => {duplicate: false,}))const frames: unknown[] = []- const fetchFn = vi.fn(async (url: string) => {+ const fetchFn = vi.fn(async (url: FetchInput, _init?: RequestInit) => {expect(url).toBe(CODEX_COMPLETIONS_ORIGIN)return sseResponse(['data: {"type":"response.output_text.delta","delta":"hi"}\n\n',@@ -471,7 +503,9 @@ describe('streamCodexCompletion', () => {finalize,fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => frames.push(frame),+ onFrame: frame => {+ frames.push(frame)+ },})expect(redeem).toHaveBeenCalledOnce()@@ -490,18 +524,18 @@ describe('streamCodexCompletion', () => {receipt: expect.objectContaining({ outcome: 'success', requestHash: REQUEST_HASH }),}))- expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(+ expect(String(headerOf(fetchFn.mock.calls[0]?.[1], 'authorization'))).toContain(accessTokenFor('live'))- expect(fetchFn.mock.calls[0]?.[1]?.headers?.['originator']).toBe('evenfire')- expect(fetchFn.mock.calls[0]?.[1]?.headers?.['openai-beta']).toBe('responses=v1')- expect(fetchFn.mock.calls[0]?.[1]?.headers?.['session_id']).toBe('req-001')- expect(fetchFn.mock.calls[0]?.[1]?.headers?.['chatgpt-account-id']).toBe('acct_live_1')+ expect(headerOf(fetchFn.mock.calls[0]?.[1], 'originator')).toBe('evenfire')+ expect(headerOf(fetchFn.mock.calls[0]?.[1], 'openai-beta')).toBe('responses=v1')+ expect(headerOf(fetchFn.mock.calls[0]?.[1], 'session_id')).toBe('req-001')+ expect(headerOf(fetchFn.mock.calls[0]?.[1], 'chatgpt-account-id')).toBe('acct_live_1')expect(String(fetchFn.mock.calls[0]?.[1]?.body)).toContain('"store":false')})it('flushes a completed event that arrives without a trailing blank line', async () => {- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{"input_tokens":1,"output_tokens":1}}}',])@@ -531,7 +565,7 @@ describe('streamCodexCompletion', () => {})it('parses CRLF-delimited SSE frames', async () => {- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{"input_tokens":4,"output_tokens":5}}}\r\n\r\n',])@@ -561,7 +595,7 @@ describe('streamCodexCompletion', () => {})it('uses the redeemed ChatGPT account id when the access token is opaque', async () => {- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n']))await streamCodexCompletion({@@ -579,17 +613,17 @@ describe('streamCodexCompletion', () => {redeemSuccess({ accessToken: 'opaque-token', chatgptAccountId: 'acct_from_id_token' }),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})- expect(fetchFn.mock.calls[0]?.[1]?.headers?.['chatgpt-account-id']).toBe('acct_from_id_token')+ expect(headerOf(fetchFn.mock.calls[0]?.[1], 'chatgpt-account-id')).toBe('acct_from_id_token')})it('keeps the redeemed ChatGPT account id when the access token JWT names another account', async () => {- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n']))await streamCodexCompletion({@@ -610,13 +644,13 @@ describe('streamCodexCompletion', () => {}),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})- expect(fetchFn.mock.calls[0]?.[1]?.headers?.['chatgpt-account-id']).toBe('acct_stored')+ expect(headerOf(fetchFn.mock.calls[0]?.[1], 'chatgpt-account-id')).toBe('acct_stored')})it('refuses to fetch completions when the access token has no ChatGPT account id', async () => {@@ -636,7 +670,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess({ accessToken: 'opaque-token' }),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'error',+ outcome: 'error' as const,duplicate: false,})),fetchFn,@@ -687,7 +721,7 @@ describe('streamCodexCompletion', () => {redeemSuccess({ transport: { ...redeemSuccess().transport, servedModel: 'other' } }),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'error',+ outcome: 'error' as const,duplicate: false,})),fetchFn: vi.fn(),@@ -716,7 +750,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'error',+ outcome: 'error' as const,duplicate: false,})),fetchFn,@@ -749,7 +783,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,@@ -775,10 +809,12 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'error',+ outcome: 'error' as const,duplicate: false,})),- fetchFn: vi.fn(async () => new Response('denied', { status: 401 })),+ fetchFn: vi.fn(+ async (_url: FetchInput, _init?: RequestInit) => new Response('denied', { status: 401 })+ ),lookup: async () => [{ address: '1.2.3.4', family: 4 }],})).rejects.toMatchObject({ code: 'connection_unavailable' })@@ -836,7 +872,7 @@ describe('streamCodexCompletion', () => {tokens.push(input.executionTicket)return redeemSuccess({ accessToken: accessTokenFor(input.executionTicket) })})- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n']))const ticket = {@@ -854,7 +890,7 @@ describe('streamCodexCompletion', () => {redeem,finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,@@ -868,23 +904,23 @@ describe('streamCodexCompletion', () => {redeem,finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],})expect(tokens).toEqual(['t-a', 't-b'])- expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(+ expect(String(headerOf(fetchFn.mock.calls[0]?.[1], 'authorization'))).toContain(accessTokenFor('t-a'))- expect(String(fetchFn.mock.calls[1]?.[1]?.headers?.['authorization'])).toContain(+ expect(String(headerOf(fetchFn.mock.calls[1]?.[1], 'authorization'))).toContain(accessTokenFor('t-b'))})it('does not retry after an ambiguous upstream response', async () => {- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.output_text.delta","delta":"partial"}\n\n']))const result = await streamCodexCompletion({@@ -901,7 +937,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'unknown',+ outcome: 'unknown' as const,duplicate: false,})),fetchFn,@@ -937,7 +973,7 @@ describe('streamCodexCompletion', () => {transportHints: { promptCacheKey: 'sess-1' },}const requestHash = hashCodexCompletionRequestV1(request)- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n']))await streamCodexCompletion({@@ -954,7 +990,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,@@ -1056,7 +1092,9 @@ describe('streamCodexCompletion', () => {}),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => emitted.push(frame),+ onFrame: frame => {+ emitted.push(frame)+ },})expect(result.outcome).toBe('success')expect(emitted).toContainEqual({ type: 'tool_call', id: 'new-call', name, arguments: {} })@@ -1105,7 +1143,9 @@ describe('streamCodexCompletion', () => {finalize,fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => emitted.push(frame),+ onFrame: frame => {+ emitted.push(frame)+ },})).rejects.toMatchObject({code: 'provider_unavailable',@@ -1121,7 +1161,7 @@ describe('streamCodexCompletion', () => {})it('omits Notify-like generation fields on the Responses wire', async () => {- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n']))await streamCodexCompletion({@@ -1138,7 +1178,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,@@ -1162,7 +1202,7 @@ describe('streamCodexCompletion', () => {generation: { maxOutputTokens: 4096, temperature: 0.2 },}const requestHash = hashCodexCompletionRequestV1(request)- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n']))await streamCodexCompletion({@@ -1179,7 +1219,7 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,@@ -1193,11 +1233,13 @@ describe('streamCodexCompletion', () => {})it('maps upstream 400 to invalid_request and finalizes error without usage', async () => {- const finalize = vi.fn(async () => ({- providerAttemptId: 'att-1',- outcome: 'error' as const,- duplicate: false,- }))+ const finalize = vi.fn(+ async (_input: Parameters<StreamCodexCompletionInput['finalize']>[0]) => ({+ providerAttemptId: 'att-1',+ outcome: 'error' as const,+ duplicate: false,+ })+ )await expect(streamCodexCompletion({executionTicket: 'ticket-1',@@ -1212,7 +1254,10 @@ describe('streamCodexCompletion', () => {},redeem: async () => redeemSuccess(),finalize,- fetchFn: vi.fn(async () => new Response('bad request', { status: 400 })),+ fetchFn: vi.fn(+ async (_url: FetchInput, _init?: RequestInit) =>+ new Response('bad request', { status: 400 })+ ),lookup: async () => [{ address: '1.2.3.4', family: 4 }],})).rejects.toMatchObject({ code: 'invalid_request' })@@ -1226,7 +1271,7 @@ describe('streamCodexCompletion', () => {it('emits a tool call only after argument deltas complete', async () => {const frames: unknown[] = []- const fetchFn = vi.fn(async () =>+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>sseResponse(['data: {"type":"response.output_item.added","item":{"type":"function_call","id":"item-1","call_id":"call-9","name":"lookup","arguments":""}}\n\n','data: {"type":"response.function_call_arguments.delta","item_id":"item-1","delta":"{\\"q\\":"}}\n\n',@@ -1249,12 +1294,14 @@ describe('streamCodexCompletion', () => {redeem: async () => redeemSuccess(),finalize: vi.fn(async () => ({providerAttemptId: 'att-1',- outcome: 'success',+ outcome: 'success' as const,duplicate: false,})),fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],- onFrame: frame => frames.push(frame),+ onFrame: frame => {+ frames.push(frame)+ },})expect(frames).toEqual([{ type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },diff --git a/codex-llm-proxy/test/sseBackpressure.test.ts b/codex-llm-proxy/test/sseBackpressure.test.tsindex 7902b39b5..bfd092bf1 100644--- a/codex-llm-proxy/test/sseBackpressure.test.ts+++ b/codex-llm-proxy/test/sseBackpressure.test.ts@@ -90,11 +90,13 @@ function config(): CodexLlmProxyConfig {adminPort: 0,probePort: 0,maxBodyBytes: 1_048_576,+ maxVisualBodyBytes: 24 * 1024 * 1024,maxStreamDurationMs: 60_000,maxDeadlineMs: 60_000,jwtIssuer: 'control-api',jwtPublicKey: publicKey,executionEnabled: true,+ imageInputModels: [],controlApiBaseUrl: 'http://control-api.invalid/api/v1',controlApiServiceName: 'codex-llm-proxy',controlApiServiceToken: 'unused',