Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(llm-proxy): require auth before admission and a body-read deadline (#739)
Alfredo Lopez631f78531635
diff --git a/codex-llm-proxy/test/bodyAdmission.test.ts b/codex-llm-proxy/test/bodyAdmission.test.tsindex 08c62ab34..72e85dc30 100644--- a/codex-llm-proxy/test/bodyAdmission.test.ts+++ b/codex-llm-proxy/test/bodyAdmission.test.ts@@ -9,7 +9,7 @@* body was read, JSON-parsed, contract-parsed and hash-checked, so the number* of attempts held there is the number of bodies in memory.*/-import { describe, expect, it } from 'vitest'+import { describe, expect, it, vi } from 'vitest'import jwt from 'jsonwebtoken'import { generateKeyPairSync, randomUUID } from 'node:crypto'import { request as httpRequest } from 'node:http'@@ -25,9 +25,17 @@ import type {FinalizeAttemptSuccess,RedeemAttemptSuccess,} from '../src/controlApiClient.js'+import { logger } from '../src/logger.js'import { CODEX_CATALOG_ORIGIN, CODEX_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'-import { DEFAULT_MAX_BODY_BYTES, STREAM_LIMITS } from '../src/requestLimits.js'-import { createProxyApps } from '../src/server.js'+import {+ BODY_READ_DEADLINE_MS,+ DEFAULT_MAX_BODY_BYTES,+ STREAM_LIMITS,+} from '../src/requestLimits.js'+import { type ProxyRuntimeDeps, createProxyApps } from '../src/server.js'++/** Captured before any test fakes the clock, so waits stay on real time. */+const realSetTimeout = globalThis.setTimeoutconst { privateKey, publicKey } = generateKeyPairSync('rsa', {modulusLength: 2048,@@ -107,11 +115,16 @@ function completionBody(contentChars: number, attempt: string): string {})}+const adminPermit = sign(+ { sub: 'admin-1', typ: 'codex-admin-permit', operation: 'catalog_list' },+ 'codex-llm-proxy-admin'+)+/*** A proxy whose `redeem` holds every attempt until `releaseAll`, so an attempt- * that reached it keeps its parsed body in memory.+ * that reached it keeps its parsed body in memory. `hold` holds again.*/-async function heldProxy(maxBodyBytes: number) {+async function heldProxy(maxBodyBytes: number, deps: Pick<ProxyRuntimeDeps, 'bodyReadDeadlineMs'> = {}) {let redeemed = 0let holding = trueconst held: Array<() => void> = []@@ -154,12 +167,25 @@ async function heldProxy(maxBodyBytes: number) {controlApiClient: client,fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ ...deps,})await new Promise<void>(resolve => servers.runtime.listen(0, '127.0.0.1', () => resolve()))+ await new Promise<void>(resolve => servers.admin.listen(0, '127.0.0.1', () => resolve()))const { port } = servers.runtime.address() as AddressInfo+ const { port: adminPort } = servers.admin.address() as AddressInfo+ let arrived = 0+ servers.runtime.on('request', () => {+ arrived += 1+ })return {port,+ adminPort,redeemed: () => redeemed,+ /** Requests whose headers the runtime listener has received. */+ arrived: () => arrived,+ hold: () => {+ holding = true+ },releaseAll: () => {holding = falsefor (const resolve of held.splice(0)) resolve()@@ -215,6 +241,60 @@ async function settle(read: () => number): Promise<number> {throw new Error(`count did not settle within 20s (last ${last})`)}+function sleep(ms: number): Promise<void> {+ return new Promise(resolve => realSetTimeout(resolve, ms))+}++type TimedReply = Reply & { elapsedMs: number }++/** The reply, or status 0 when none arrived within `ms` of real time. */+function within(reply: Promise<TimedReply>, ms: number): Promise<TimedReply> {+ return Promise.race([+ reply,+ sleep(ms).then(() => ({ status: 0, body: `no reply within ${ms} ms`, elapsedMs: ms })),+ ])+}++/**+ * A POST that declares `declaredBytes`, sends only `sent` and then stalls.+ * `destroy` closes the client socket so the listener can shut down.+ */+function stalledPost(+ port: number,+ options: { declaredBytes: number; token?: string; path?: string; sent?: string }+): { reply: Promise<TimedReply>; destroy: () => void } {+ const started = Date.now()+ const req = httpRequest({+ host: '127.0.0.1',+ port,+ method: 'POST',+ path: options.path ?? COMPLETIONS_PATH,+ agent: false,+ headers: {+ 'content-type': 'application/json',+ 'content-length': options.declaredBytes,+ ...(options.token ? { authorization: `Bearer ${options.token}` } : {}),+ },+ })+ const reply = new Promise<TimedReply>((resolve, reject) => {+ req.on('response', res => {+ const chunks: Buffer[] = []+ res.on('data', chunk => chunks.push(chunk as Buffer))+ res.on('end', () =>+ resolve({+ status: res.statusCode ?? 0,+ body: Buffer.concat(chunks).toString('utf8'),+ elapsedMs: Date.now() - started,+ })+ )+ res.on('error', reject)+ })+ req.on('error', reject)+ })+ req.write(options.sent ?? '{')+ return { reply, destroy: () => req.destroy() }+}+describe('codex-llm-proxy body admission (#731 R3-2)', () => {it('T-R3-2a holds at most three near-cap bodies at once and admits the rest as they finish', async () => {const proxy = await heldProxy(DEFAULT_MAX_BODY_BYTES)@@ -293,3 +373,201 @@ describe('codex-llm-proxy body admission (#731 R3-2)', () => {}}, 60_000)})++/**+ * R9-M-B — a stalled body must not hold the budget. The platform JWT is+ * checked before a reservation is taken, and a reserved body that is not read+ * within the read deadline is answered 408 and releases its reservation.+ */+describe('codex-llm-proxy authentication before admission and body-read deadline (R9-M-B)', () => {+ // A small body limit keeps these cheap; the budget scales with it.+ const maxBodyBytes = 16 * 1024+ const fillingBody = (attempt: string) => completionBody(12_000, attempt)++ it('T-MB-0 fixes the production body-read deadline at 10 000 ms', () => {+ expect(BODY_READ_DEADLINE_MS).toBe(10_000)+ })++ it('T-MB-1 answers anonymous stalled bodies 401 within 1 s without reserving the budget', async () => {+ const proxy = await heldProxy(maxBodyBytes)+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes })+ )+ const filling: Array<Promise<Reply>> = []+ try {+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {+ expect(reply.status).toBe(401)+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })+ }+ // Zero bytes reserved: authenticated bodies that fill the whole budget+ // are all read and redeemed at once.+ const payloadBytes = Buffer.byteLength(fillingBody('probe'))+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)+ proxy.releaseAll()+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)+ } finally {+ for (const s of anonymous) s.destroy()+ proxy.releaseAll()+ await Promise.allSettled(filling)+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-1-admin answers anonymous stalled admin bodies 401 within 1 s without reserving the shared budget', async () => {+ const deadlineMs = 1_000+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })+ const modelsPath = '/internal/admin/v1/codex/models'+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>+ stalledPost(proxy.adminPort, { declaredBytes: maxBodyBytes, path: modelsPath })+ )+ const permitted = stalledPost(proxy.adminPort, {+ declaredBytes: maxBodyBytes,+ path: modelsPath,+ token: adminPermit,+ })+ const filling: Array<Promise<Reply>> = []+ try {+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {+ expect(reply.status).toBe(401)+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })+ }+ // Witness: a valid admin permit passes the gate and is then bounded by+ // the read deadline, not refused by the gate.+ const held = await within(permitted.reply, deadlineMs + 1_000)+ expect(held.status).toBe(408)+ expect(JSON.parse(held.body)).toEqual({ error: 'request_timeout' })+ // The budget is shared with the runtime listener, and all of it is free.+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)+ proxy.releaseAll()+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)+ } finally {+ for (const s of [...anonymous, permitted]) s.destroy()+ proxy.releaseAll()+ await Promise.allSettled(filling)+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-2 answers an authenticated stalled body 408 request_timeout at the deadline and frees its reservation', async () => {+ const deadlineMs = 1_000+ const warn = vi.spyOn(logger, 'warn')+ const error = vi.spyOn(logger, 'error')+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })+ proxy.releaseAll()+ const stalled = Array.from({ length: BUDGET_BODIES }, () =>+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })+ )+ const refill: Array<Promise<Reply>> = []+ try {+ while (proxy.arrived() < BUDGET_BODIES) await sleep(10)+ await sleep(50)+ // The stalled bodies hold the whole budget, so this one waits in the queue.+ const queued = post(proxy.port, fillingBody('queued'))++ for (const reply of await Promise.all(stalled.map(s => within(s.reply, deadlineMs + 1_000)))) {+ expect(reply.status).toBe(408)+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })+ expect(reply.elapsedMs).toBeGreaterThanOrEqual(deadlineMs)+ expect(reply.elapsedMs).toBeLessThan(deadlineMs + 1_000)+ }+ // Witness: the queued request is admitted once the reservations are freed.+ const served = await queued+ expect(served.status).toBe(200)+ expect(served.body).toContain('"outcome":"success"')+ expect(proxy.redeemed()).toBe(1)+ expect(warn).toHaveBeenCalledWith(+ { event: 'codex_proxy_denied', code: 'request_timeout' },+ 'request denied'+ )+ expect(error).not.toHaveBeenCalledWith(+ expect.objectContaining({ event: 'codex_proxy_error' }),+ expect.anything()+ )++ // The budget is back to zero: bodies that fill all of it are admitted at once.+ proxy.hold()+ for (let i = 0; i < BUDGET_BODIES; i += 1) refill.push(post(proxy.port, fillingBody(`refill-${i}`)))+ expect(await settle(proxy.redeemed)).toBe(1 + BUDGET_BODIES)+ proxy.releaseAll()+ for (const reply of await Promise.all(refill)) expect(reply.status).toBe(200)+ } finally {+ for (const s of stalled) s.destroy()+ proxy.releaseAll()+ await Promise.allSettled(refill)+ await proxy.close()+ warn.mockRestore()+ error.mockRestore()+ }+ }, 30_000)++ it('T-MB-2d arms BODY_READ_DEADLINE_MS when no deadline is injected', async () => {+ const proxy = await heldProxy(maxBodyBytes)+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })+ const stalled = stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })+ try {+ while (proxy.arrived() < 1) await sleep(10)+ await sleep(50)+ vi.advanceTimersByTime(BODY_READ_DEADLINE_MS - 1)+ expect((await within(stalled.reply, 200)).status).toBe(0)+ vi.advanceTimersByTime(1)+ const reply = await within(stalled.reply, 2_000)+ expect(reply.status).toBe(408)+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })+ } finally {+ vi.useRealTimers()+ stalled.destroy()+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-3 starts the deadline at grant, so a body queued past it is still served', async () => {+ const deadlineMs = 500+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })+ const holders = Array.from({ length: BUDGET_BODIES }, (_, i) =>+ post(proxy.port, fillingBody(`holder-${i}`))+ )+ try {+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)+ const queued = post(proxy.port, fillingBody('queued'))+ await sleep(3 * deadlineMs)+ // Witness: the body really waited in the queue for longer than the deadline.+ expect(proxy.redeemed()).toBe(BUDGET_BODIES)+ proxy.releaseAll()+ const served = await queued+ expect(served.status).toBe(200)+ expect(served.body).toContain('"outcome":"success"')+ expect(proxy.redeemed()).toBe(BUDGET_BODIES + 1)+ for (const reply of await Promise.all(holders)) expect(reply.status).toBe(200)+ } finally {+ proxy.releaseAll()+ await Promise.allSettled(holders)+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-4 answers an unauthenticated oversize body 401 without parsing it', async () => {+ const proxy = await heldProxy(maxBodyBytes)+ proxy.releaseAll()+ // Over the visual ceiling as well, so no parser could accept it.+ const declaredBytes = LIMITS.maxVisualRequestBodyBytes + 1+ const anonymous = stalledPost(proxy.port, { declaredBytes })+ const authenticated = stalledPost(proxy.port, { declaredBytes, token: platformToken })+ try {+ const refused = await within(anonymous.reply, 1_000)+ expect(refused.status).toBe(401)+ expect(JSON.parse(refused.body)).toEqual({ error: 'Unauthorized' })+ // Witness: the same body with a platform JWT reaches the size check.+ const oversize = await within(authenticated.reply, 1_000)+ expect(oversize.status).toBe(413)+ expect(JSON.parse(oversize.body)).toEqual({ error: 'payload_too_large' })+ expect(proxy.redeemed()).toBe(0)+ } finally {+ anonymous.destroy()+ authenticated.destroy()+ await proxy.close()+ }+ }, 30_000)+})diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.tsindex b66bcd1e8..0bc342965 100644--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@ -192,15 +192,17 @@ describe('codex-llm-proxy security surface', () => {expect(admitted.status).toBe(403)expect(admitted.body.error).toBe('ticket_invalid')+ // R9-M-B: without a platform JWT the body is never read, so the caller+ // gets 401 instead of the ordinary parser's 413.const anonymous = await request(runtimeApp).post('/internal/runtime/v1/codex/completions').send(payload)- expect(anonymous.status).toBe(413)+ expect(anonymous.status).toBe(401)const noScope = await request(runtimeApp).post('/internal/runtime/v1/codex/completions').set('Authorization', `Bearer ${platformToken({ workflowControlScopes: [] })}`).send(payload)- expect(noScope.status).toBe(413)+ expect(noScope.status).toBe(401)const v1 = await request(runtimeApp).post('/internal/runtime/v1/codex/completions').set('Authorization', `Bearer ${platformToken()}`)@@ -244,15 +246,15 @@ describe('codex-llm-proxy security surface', () => {request: { schemaVersion: 'codex-completion-request.v2', pad: 'x'.repeat(4096) },}// With budget left, an unauthenticated oversize request is stopped by the- // ordinary transport cap instead of the limiter.+ // platform JWT gate (R9-M-B), before its body is admitted or parsed.const withinBudget = await completion().send(oversized)- expect(withinBudget.status).toBe(413)+ expect(withinBudget.status).toBe(401)for (let i = 0; i < 59; i += 1) {const accepted = await completion().send({})expect(accepted.status).toBe(401)}// The limiter runs first, so the exhausted window rejects before the identity- // check and body parsing turn the same request into a 413.+ // check turns the same request into a 401.const limited = await completion().send(oversized)expect(limited.status).toBe(429)})diff --git a/grok-llm-proxy/test/bodyAdmission.test.ts b/grok-llm-proxy/test/bodyAdmission.test.tsindex ec5fbb0a7..b036de509 100644--- a/grok-llm-proxy/test/bodyAdmission.test.ts+++ b/grok-llm-proxy/test/bodyAdmission.test.ts@@ -9,7 +9,7 @@* body was read, JSON-parsed, contract-parsed and hash-checked, so the number* of attempts held there is the number of bodies in memory.*/-import { describe, expect, it } from 'vitest'+import { describe, expect, it, vi } from 'vitest'import jwt from 'jsonwebtoken'import { generateKeyPairSync, randomUUID } from 'node:crypto'import { request as httpRequest } from 'node:http'@@ -25,9 +25,17 @@ import type {FinalizeAttemptSuccess,RedeemAttemptSuccess,} from '../src/controlApiClient.js'+import { logger } from '../src/logger.js'import { GROK_CATALOG_ORIGIN, GROK_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'-import { DEFAULT_MAX_BODY_BYTES, STREAM_LIMITS } from '../src/requestLimits.js'-import { createProxyApps } from '../src/server.js'+import {+ BODY_READ_DEADLINE_MS,+ DEFAULT_MAX_BODY_BYTES,+ STREAM_LIMITS,+} from '../src/requestLimits.js'+import { type ProxyRuntimeDeps, createProxyApps } from '../src/server.js'++/** Captured before any test fakes the clock, so waits stay on real time. */+const realSetTimeout = globalThis.setTimeoutconst { privateKey, publicKey } = generateKeyPairSync('rsa', {modulusLength: 2048,@@ -106,11 +114,16 @@ function completionBody(contentChars: number, attempt: string): string {})}+const adminPermit = sign(+ { sub: 'admin-1', typ: 'grok-admin-permit', operation: 'catalog_list' },+ 'grok-llm-proxy-admin'+)+/*** A proxy whose `redeem` holds every attempt until `releaseAll`, so an attempt- * that reached it keeps its parsed body in memory.+ * that reached it keeps its parsed body in memory. `hold` holds again.*/-async function heldProxy(maxBodyBytes: number) {+async function heldProxy(maxBodyBytes: number, deps: Pick<ProxyRuntimeDeps, 'bodyReadDeadlineMs'> = {}) {let redeemed = 0let holding = trueconst held: Array<() => void> = []@@ -152,12 +165,25 @@ async function heldProxy(maxBodyBytes: number) {controlApiClient: client,fetchFn,lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ ...deps,})await new Promise<void>(resolve => servers.runtime.listen(0, '127.0.0.1', () => resolve()))+ await new Promise<void>(resolve => servers.admin.listen(0, '127.0.0.1', () => resolve()))const { port } = servers.runtime.address() as AddressInfo+ const { port: adminPort } = servers.admin.address() as AddressInfo+ let arrived = 0+ servers.runtime.on('request', () => {+ arrived += 1+ })return {port,+ adminPort,redeemed: () => redeemed,+ /** Requests whose headers the runtime listener has received. */+ arrived: () => arrived,+ hold: () => {+ holding = true+ },releaseAll: () => {holding = falsefor (const resolve of held.splice(0)) resolve()@@ -213,6 +239,60 @@ async function settle(read: () => number): Promise<number> {throw new Error(`count did not settle within 20s (last ${last})`)}+function sleep(ms: number): Promise<void> {+ return new Promise(resolve => realSetTimeout(resolve, ms))+}++type TimedReply = Reply & { elapsedMs: number }++/** The reply, or status 0 when none arrived within `ms` of real time. */+function within(reply: Promise<TimedReply>, ms: number): Promise<TimedReply> {+ return Promise.race([+ reply,+ sleep(ms).then(() => ({ status: 0, body: `no reply within ${ms} ms`, elapsedMs: ms })),+ ])+}++/**+ * A POST that declares `declaredBytes`, sends only `sent` and then stalls.+ * `destroy` closes the client socket so the listener can shut down.+ */+function stalledPost(+ port: number,+ options: { declaredBytes: number; token?: string; path?: string; sent?: string }+): { reply: Promise<TimedReply>; destroy: () => void } {+ const started = Date.now()+ const req = httpRequest({+ host: '127.0.0.1',+ port,+ method: 'POST',+ path: options.path ?? COMPLETIONS_PATH,+ agent: false,+ headers: {+ 'content-type': 'application/json',+ 'content-length': options.declaredBytes,+ ...(options.token ? { authorization: `Bearer ${options.token}` } : {}),+ },+ })+ const reply = new Promise<TimedReply>((resolve, reject) => {+ req.on('response', res => {+ const chunks: Buffer[] = []+ res.on('data', chunk => chunks.push(chunk as Buffer))+ res.on('end', () =>+ resolve({+ status: res.statusCode ?? 0,+ body: Buffer.concat(chunks).toString('utf8'),+ elapsedMs: Date.now() - started,+ })+ )+ res.on('error', reject)+ })+ req.on('error', reject)+ })+ req.write(options.sent ?? '{')+ return { reply, destroy: () => req.destroy() }+}+describe('grok-llm-proxy body admission (#731 R3-2)', () => {it('T-R3-2a-grok holds at most three near-cap bodies at once and admits the rest as they finish', async () => {const proxy = await heldProxy(DEFAULT_MAX_BODY_BYTES)@@ -291,3 +371,199 @@ describe('grok-llm-proxy body admission (#731 R3-2)', () => {}}, 60_000)})++/**+ * R9-M-B — a stalled body must not hold the budget. The platform JWT is+ * checked before a reservation is taken, and a reserved body that is not read+ * within the read deadline is answered 408 and releases its reservation.+ */+describe('grok-llm-proxy authentication before admission and body-read deadline (R9-M-B)', () => {+ // A small body limit keeps these cheap; the budget scales with it.+ const maxBodyBytes = 16 * 1024+ const fillingBody = (attempt: string) => completionBody(12_000, attempt)++ it('T-MB-0-grok fixes the production body-read deadline at 10 000 ms', () => {+ expect(BODY_READ_DEADLINE_MS).toBe(10_000)+ })++ it('T-MB-1-grok answers anonymous stalled bodies 401 within 1 s without reserving the budget', async () => {+ const proxy = await heldProxy(maxBodyBytes)+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes })+ )+ const filling: Array<Promise<Reply>> = []+ try {+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {+ expect(reply.status).toBe(401)+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })+ }+ // Zero bytes reserved: authenticated bodies that fill the whole budget+ // are all read and redeemed at once.+ const payloadBytes = Buffer.byteLength(fillingBody('probe'))+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)+ proxy.releaseAll()+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)+ } finally {+ for (const s of anonymous) s.destroy()+ proxy.releaseAll()+ await Promise.allSettled(filling)+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-1-admin-grok answers anonymous stalled admin bodies 401 within 1 s without reserving the shared budget', async () => {+ const deadlineMs = 1_000+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })+ const modelsPath = '/internal/admin/v1/grok/models'+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>+ stalledPost(proxy.adminPort, { declaredBytes: maxBodyBytes, path: modelsPath })+ )+ const permitted = stalledPost(proxy.adminPort, {+ declaredBytes: maxBodyBytes,+ path: modelsPath,+ token: adminPermit,+ })+ const filling: Array<Promise<Reply>> = []+ try {+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {+ expect(reply.status).toBe(401)+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })+ }+ // Witness: a valid admin permit passes the gate and is then bounded by+ // the read deadline, not refused by the gate.+ const held = await within(permitted.reply, deadlineMs + 1_000)+ expect(held.status).toBe(408)+ expect(JSON.parse(held.body)).toEqual({ error: 'request_timeout' })+ // The budget is shared with the runtime listener, and all of it is free.+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)+ proxy.releaseAll()+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)+ } finally {+ for (const s of [...anonymous, permitted]) s.destroy()+ proxy.releaseAll()+ await Promise.allSettled(filling)+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-2-grok answers an authenticated stalled body 408 request_timeout at the deadline and frees its reservation', async () => {+ const deadlineMs = 1_000+ const warn = vi.spyOn(logger, 'warn')+ const error = vi.spyOn(logger, 'error')+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })+ proxy.releaseAll()+ const stalled = Array.from({ length: BUDGET_BODIES }, () =>+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })+ )+ const refill: Array<Promise<Reply>> = []+ try {+ while (proxy.arrived() < BUDGET_BODIES) await sleep(10)+ await sleep(50)+ // The stalled bodies hold the whole budget, so this one waits in the queue.+ const queued = post(proxy.port, fillingBody('queued'))++ for (const reply of await Promise.all(stalled.map(s => within(s.reply, deadlineMs + 1_000)))) {+ expect(reply.status).toBe(408)+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })+ expect(reply.elapsedMs).toBeGreaterThanOrEqual(deadlineMs)+ expect(reply.elapsedMs).toBeLessThan(deadlineMs + 1_000)+ }+ // Witness: the queued request is admitted once the reservations are freed.+ const served = await queued+ expect(served.status).toBe(200)+ expect(served.body).toContain('"outcome":"success"')+ expect(proxy.redeemed()).toBe(1)+ expect(warn).toHaveBeenCalledWith(+ { event: 'grok_proxy_denied', code: 'request_timeout' },+ 'request denied'+ )+ expect(error).not.toHaveBeenCalledWith(+ expect.objectContaining({ event: 'grok_proxy_error' }),+ expect.anything()+ )++ // The budget is back to zero: bodies that fill all of it are admitted at once.+ proxy.hold()+ for (let i = 0; i < BUDGET_BODIES; i += 1) refill.push(post(proxy.port, fillingBody(`refill-${i}`)))+ expect(await settle(proxy.redeemed)).toBe(1 + BUDGET_BODIES)+ proxy.releaseAll()+ for (const reply of await Promise.all(refill)) expect(reply.status).toBe(200)+ } finally {+ for (const s of stalled) s.destroy()+ proxy.releaseAll()+ await Promise.allSettled(refill)+ await proxy.close()+ warn.mockRestore()+ error.mockRestore()+ }+ }, 30_000)++ it('T-MB-2d-grok arms BODY_READ_DEADLINE_MS when no deadline is injected', async () => {+ const proxy = await heldProxy(maxBodyBytes)+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })+ const stalled = stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })+ try {+ while (proxy.arrived() < 1) await sleep(10)+ await sleep(50)+ vi.advanceTimersByTime(BODY_READ_DEADLINE_MS - 1)+ expect((await within(stalled.reply, 200)).status).toBe(0)+ vi.advanceTimersByTime(1)+ const reply = await within(stalled.reply, 2_000)+ expect(reply.status).toBe(408)+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })+ } finally {+ vi.useRealTimers()+ stalled.destroy()+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-3-grok starts the deadline at grant, so a body queued past it is still served', async () => {+ const deadlineMs = 500+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })+ const holders = Array.from({ length: BUDGET_BODIES }, (_, i) =>+ post(proxy.port, fillingBody(`holder-${i}`))+ )+ try {+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)+ const queued = post(proxy.port, fillingBody('queued'))+ await sleep(3 * deadlineMs)+ // Witness: the body really waited in the queue for longer than the deadline.+ expect(proxy.redeemed()).toBe(BUDGET_BODIES)+ proxy.releaseAll()+ const served = await queued+ expect(served.status).toBe(200)+ expect(served.body).toContain('"outcome":"success"')+ expect(proxy.redeemed()).toBe(BUDGET_BODIES + 1)+ for (const reply of await Promise.all(holders)) expect(reply.status).toBe(200)+ } finally {+ proxy.releaseAll()+ await Promise.allSettled(holders)+ await proxy.close()+ }+ }, 30_000)++ it('T-MB-4-grok answers an unauthenticated oversize body 401 without parsing it', async () => {+ const proxy = await heldProxy(maxBodyBytes)+ proxy.releaseAll()+ const anonymous = stalledPost(proxy.port, { declaredBytes: maxBodyBytes + 1 })+ try {+ const refused = await within(anonymous.reply, 1_000)+ expect(refused.status).toBe(401)+ expect(JSON.parse(refused.body)).toEqual({ error: 'Unauthorized' })+ // Witness: an oversize body with a platform JWT reaches the size check.+ const oversizePayload = completionBody(20_000, 'oversize')+ expect(Buffer.byteLength(oversizePayload)).toBeGreaterThan(maxBodyBytes)+ const oversize = await post(proxy.port, oversizePayload)+ expect(oversize.status).toBe(413)+ expect(JSON.parse(oversize.body)).toEqual({ error: 'payload_too_large' })+ expect(proxy.redeemed()).toBe(0)+ } finally {+ anonymous.destroy()+ await proxy.close()+ }+ }, 30_000)+})