Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(codex-llm-proxy): bound the attempt failure metric label
Alfredo Lopezda3328b6a768
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.tsindex e9bcdddd4..7e048f1f0 100644--- a/codex-llm-proxy/src/server.ts+++ b/codex-llm-proxy/src/server.ts@@ -223,7 +223,7 @@ export function createProxyApps(config: CodexLlmProxyConfig, deps: ProxyRuntimeD} catch (err) {const mapped = mapError(err)metrics.observeAttempt('error', 'completion_stream')- metrics.observeAttemptFailure(mapped.code)+ metrics.observeAttemptFailure(failureLabel(mapped.code))const deliveredAs = res.headersSent ? 'sse_error' : 'http_status'logger.warn({@@ -362,28 +362,39 @@ export function startProxy(config: CodexLlmProxyConfig): ProxyServers {return servers}+// Every code the proxy or control-api is known to send. The failure metric+// uses this as its label allowlist because a control-api error body is not+// bounded by the proxy.+const ATTEMPT_ERROR_STATUS: Record<string, number> = {+ invalid_request: 400,+ Unauthorized: 401,+ origin_denied: 403,+ request_hash_mismatch: 403,+ ticket_invalid: 403,+ ticket_expired: 403,+ no_grant: 403,+ host_binding_mismatch: 403,+ model_not_allowed: 403,+ insufficient_scope: 403,+ disabled: 404,+ ticket_replayed: 409,+ tool_call_limit_exceeded: 422,+ connection_unavailable: 503,+ provider_unavailable: 503,+ sse_buffer_exceeded: 503,+ invalid_receipt: 503,+ conflict: 503,+}++function failureLabel(code: string): string {+ return Object.hasOwn(ATTEMPT_ERROR_STATUS, code) ? code : 'other'+}+function mapError(err: unknown): { status: number; code: string } {if (err instanceof OriginDeniedError) return { status: 403, code: 'origin_denied' }if (err instanceof RequestLimitError) return { status: 503, code: 'provider_unavailable' }if (err instanceof CodexTransportError || err instanceof ControlApiClientError) {- const statusByCode: Record<string, number> = {- invalid_request: 400,- Unauthorized: 401,- origin_denied: 403,- request_hash_mismatch: 403,- ticket_invalid: 403,- ticket_expired: 403,- no_grant: 403,- host_binding_mismatch: 403,- model_not_allowed: 403,- insufficient_scope: 403,- disabled: 404,- ticket_replayed: 409,- tool_call_limit_exceeded: 422,- connection_unavailable: 503,- provider_unavailable: 503,- }- return { status: statusByCode[err.code] ?? 503, code: err.code }+ return { status: ATTEMPT_ERROR_STATUS[err.code] ?? 503, code: err.code }}return { status: 503, code: 'provider_unavailable' }}diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.tsindex 71b31b2a3..13387da37 100644--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@ -546,6 +546,17 @@ describe('codex-llm-proxy attempt telemetry', () => {})) as typeof fetch}+ function denyingClient(code: string): ControlApiClient {+ return {+ async redeem() {+ throw new ControlApiClientError(code, 'control API request denied')+ },+ async finalize() {+ throw new Error('finalize must not run after a denied redeem')+ },+ } as unknown as ControlApiClient+ }+function failureCount(metricsText: string, code: string): number {const line = metricsText.split('\n')@@ -557,13 +568,14 @@ describe('codex-llm-proxy attempt telemetry', () => {providerAttemptId: string,textDeltas: number,calls: number,- tamper?: (raw: Record<string, unknown>) => void+ tamper?: (raw: Record<string, unknown>) => void,+ deniedCode?: string) {const info = vi.spyOn(logger, 'info')const warn = vi.spyOn(logger, 'warn')const { client, receipts } = grantingClient()const apps = createProxyApps(config({ maxBodyBytes: 65_536 }), {- controlApiClient: client,+ controlApiClient: deniedCode ? denyingClient(deniedCode) : client,fetchFn: upstream(textDeltas, calls),lookup,})@@ -670,4 +682,24 @@ describe('codex-llm-proxy attempt telemetry', () => {)expect(failureCount(metricsText, 'tool_call_limit_exceeded')).toBe(0)})++ it('(e) labels the failure metric with a known control-api code', async () => {+ const { res, lines, metricsText } = await run('att-no-grant', 0, 0, undefined, 'no_grant')+ expect(res.status).toBe(403)+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({ outcome: 'failed', code: 'no_grant', httpStatus: 403 })+ expect(failureCount(metricsText, 'no_grant')).toBe(1)+ expect(failureCount(metricsText, 'other')).toBe(0)+ })++ it('(f) labels an unknown control-api code as other and keeps the raw code in the log', async () => {+ const rawCode = 'k8s says: pod foo not found'+ const { res, lines, metricsText } = await run('att-unknown-code', 0, 0, undefined, rawCode)+ expect(res.status).toBe(503)+ // Witness: the attempt failed and was logged with the code control-api sent.+ expect(lines).toHaveLength(1)+ expect(lines[0]).toMatchObject({ outcome: 'failed', code: rawCode, httpStatus: 503 })+ expect(failureCount(metricsText, 'other')).toBe(1)+ expect(metricsText).not.toContain(rawCode)+ })})