Skip to content

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

TypeScript

fix(codex-llm-proxy): send pre-stream error responses as JSON

Alfredo Lopezc77b867e1791
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.ts
index b70e343c0..e9bcdddd4 100644
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@ -249,6 +249,10 @@ export function createProxyApps(config: CodexLlmProxyConfig, deps: ProxyRuntimeD
res.end()
return
}
+ // Nothing was written yet, so the staged SSE headers can still be
+ // replaced; res.json() keeps an existing content-type.
+ res.removeHeader('cache-control')
+ res.setHeader('content-type', 'application/json; charset=utf-8')
res.status(mapped.status).json({ error: mapped.code })
} finally {
release?.()
diff --git a/codex-llm-proxy/test/runtimePath.hermetic.e2e.test.ts b/codex-llm-proxy/test/runtimePath.hermetic.e2e.test.ts
index a002134be..9426b4446 100644
--- a/codex-llm-proxy/test/runtimePath.hermetic.e2e.test.ts
+++ b/codex-llm-proxy/test/runtimePath.hermetic.e2e.test.ts
@@ -492,9 +492,9 @@ describe('hermetic authorize → proxy → fixture upstream → finalize', () =>
// Redemption fails before the first stream write, so the denial is a
// plain 403 with the authorizer's stable code — never a partial stream.
expect(res.status).toBe(403)
- // The SSE content-type header was already staged, so supertest surfaces
- // the JSON denial in res.text rather than res.body.
- expect(JSON.parse(res.text)).toEqual({ error: 'no_grant' })
+ // The staged SSE headers are replaced by a JSON response.
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'no_grant' })
expect(redeems).toHaveLength(1)
expect(finalizes).toHaveLength(0)
@@ -571,8 +571,13 @@ describe('hermetic per-response tool-call limit', () => {
// Liveness witness: the upstream really served the 65-call stream.
expect(counters.streams).toBe(1)
// One assertion so a regression reports both the status and the code.
- expect({ status: res.status, body: JSON.parse(res.text) }).toEqual({
+ expect({
+ status: res.status,
+ contentType: res.headers['content-type'],
+ body: res.body,
+ }).toEqual({
status: 422,
+ contentType: expect.stringMatching(/^application\/json/),
body: { error: 'tool_call_limit_exceeded' },
})
expect(res.text).not.toContain('"type":"tool_call"')
diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.ts
index a9623c16c..71b31b2a3 100644
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@ -590,8 +590,10 @@ describe('codex-llm-proxy attempt telemetry', () => {
it('(a) answers 422 and logs one attempt line when 65 calls arrive before any text', async () => {
const { res, receipts, lines, metricsText } = await run('att-limit-http', 0, 65)
expect(res.status).toBe(422)
- // The SSE content-type is set before streaming starts, so parse the text.
- expect(JSON.parse(res.text)).toEqual({ error: 'tool_call_limit_exceeded' })
+ // The staged SSE headers are replaced by a JSON response.
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.headers['cache-control']).toBeUndefined()
+ expect(res.body).toEqual({ error: 'tool_call_limit_exceeded' })
expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
expect(lines).toHaveLength(1)
expect(lines[0]).toMatchObject({
@@ -633,7 +635,8 @@ describe('codex-llm-proxy attempt telemetry', () => {
;(raw.messages as Record<string, unknown>[])[0]![secretField] = 'x'
})
expect(res.status).toBe(400)
- expect(JSON.parse(res.text)).toEqual({ error: 'invalid_request' })
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'invalid_request' })
// Witness: the attempt line is still emitted for the rejected request.
expect(lines).toHaveLength(1)
expect(lines[0]).toMatchObject({