Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(llm-proxies): pin why a queued wait ended, and refuse ticket life on an early timer
Alfredo Lopez4d4c6fb3d41d
diff --git a/codex-llm-proxy/test/bodyBudget.test.ts b/codex-llm-proxy/test/bodyBudget.test.tsindex 8dc82ccce..da17c9e68 100644--- a/codex-llm-proxy/test/bodyBudget.test.ts+++ b/codex-llm-proxy/test/bodyBudget.test.ts@@ -117,6 +117,7 @@ describe('BodyBudget admission deadline (#739 D1)', () => {await vi.advanceTimersByTimeAsync(1)expect(outcome).toBeInstanceOf(RequestLimitError)expect((outcome as Error).message).toBe('body admission wait exceeded')+ expect((outcome as RequestLimitError).kind).toBe('deadline')expect(budget.queued).toBe(0)expect(budget.inFlightBytes).toBe(30)} finally {diff --git a/codex-llm-proxy/test/requestLimits.test.ts b/codex-llm-proxy/test/requestLimits.test.tsindex 8c5770fc1..c8a2cb38a 100644--- a/codex-llm-proxy/test/requestLimits.test.ts+++ b/codex-llm-proxy/test/requestLimits.test.ts@@ -153,6 +153,8 @@ describe('StreamGate', () => {await expect(gate.acquire()).rejects.toMatchObject({name: 'RequestLimitError',message: 'stream queue wait exceeded',+ // No caller deadline governed this wait, so a dead ticket cannot be inferred.+ kind: 'queue_wait',})// Witness: the waiter was queued for the whole wait, not refused at once.expect(Date.now() - started).toBeGreaterThanOrEqual(45)@@ -177,7 +179,12 @@ describe('StreamGate', () => {setTimeout(() => reject(new Error('the admission deadline did not end the wait')), 1_000)),])- ).rejects.toMatchObject({ name: 'RequestLimitError', message: 'stream queue wait exceeded' })+ ).rejects.toMatchObject({+ name: 'RequestLimitError',+ message: 'stream queue wait exceeded',+ // The caller's deadline ended the wait; the server reads this, not the clock.+ kind: 'deadline',+ })expect(Date.now() - started).toBeGreaterThanOrEqual(45)expect(gate.snapshot().queued).toBe(0)release()diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.tsindex 068a0eb56..929be782c 100644--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@ -1308,7 +1308,7 @@ describe('codex-llm-proxy attempt telemetry', () => {it('(l) logs the request-limit reason and labels its failure metric request_limit', async () => {const acquire = vi.spyOn(streamGate, 'acquire')- .mockRejectedValueOnce(new RequestLimitError('stream queue is full'))+ .mockRejectedValueOnce(new RequestLimitError('stream queue is full', 'queue_full'))try {const { res, receipts, lines, metricsText } = await run('att-queue-full', 0, 0)// Witness: the refusal came from the stream gate this test replaced.diff --git a/grok-llm-proxy/test/bodyBudget.test.ts b/grok-llm-proxy/test/bodyBudget.test.tsindex 987dbba1d..f0689d0a1 100644--- a/grok-llm-proxy/test/bodyBudget.test.ts+++ b/grok-llm-proxy/test/bodyBudget.test.ts@@ -117,6 +117,7 @@ describe('BodyBudget admission deadline (#739 D1)', () => {await vi.advanceTimersByTimeAsync(1)expect(outcome).toBeInstanceOf(RequestLimitError)expect((outcome as Error).message).toBe('body admission wait exceeded')+ expect((outcome as RequestLimitError).kind).toBe('deadline')expect(budget.queued).toBe(0)expect(budget.inFlightBytes).toBe(30)} finally {diff --git a/grok-llm-proxy/test/bodyStructure.test.ts b/grok-llm-proxy/test/bodyStructure.test.tsindex b37286400..897f05f2a 100644--- a/grok-llm-proxy/test/bodyStructure.test.ts+++ b/grok-llm-proxy/test/bodyStructure.test.ts@@ -13,6 +13,7 @@ import { logger } from '../src/logger.js'import {BodyBudget,ENVELOPE_ALLOWANCE_BYTES,+ RequestLimitError,streamGate,visualStreamGate,} from '../src/requestLimits.js'@@ -547,5 +548,53 @@ describe('grok proxy raw-body structure bounds (A8)', () => {expect(stream.calls).toHaveLength(3)await waitFor(() => visualStreamGate.snapshot().running === 0, 'the visual slot was not released')}, 10_000)++ it('refuses for ticket life when the budget deadline fires before the clock reaches exp', async () => {+ const stream = controlledStream()+ streams.push(stream)+ const warn = vi.spyOn(logger, 'warn')+ const maxBodyBytes = ordinaryCapFor()+ // A timer armed for exp can run a millisecond before Date.now() reaches+ // exp. The budget reports that its own deadline ended the wait while the+ // ticket still has two seconds on the clock.+ const budgetAcquire = vi+ .spyOn(BodyBudget.prototype, 'acquire')+ .mockRejectedValueOnce(new RequestLimitError('body admission wait exceeded', 'deadline'))+ const port = listen(createProxyApps(config({ maxBodyBytes }), { streamCompletion: stream.impl }))++ const res = await post(port, demotedV2(maxBodyBytes, 2))+ expect(res.status).toBe(503)+ expect(await res.json()).toEqual({ error: 'provider_unavailable' })+ expect(budgetAcquire).toHaveBeenCalledTimes(1)+ expect(warn).toHaveBeenCalledWith(+ expect.objectContaining({ event: 'grok_proxy_admission_refused', reason: 'ticket_life' }),+ 'admission refused'+ )+ expect(stream.calls).toHaveLength(0)+ await waitFor(() => visualStreamGate.snapshot().running === 0, 'the visual slot was not released')+ })++ it('does not blame the ticket when the budget queue is full', async () => {+ const stream = controlledStream()+ streams.push(stream)+ const warn = vi.spyOn(logger, 'warn')+ const maxBodyBytes = ordinaryCapFor()+ const budgetAcquire = vi+ .spyOn(BodyBudget.prototype, 'acquire')+ .mockRejectedValueOnce(new RequestLimitError('body admission queue is full', 'queue_full'))+ const port = listen(createProxyApps(config({ maxBodyBytes }), { streamCompletion: stream.impl }))++ const res = await post(port, demotedV2(maxBodyBytes, 2))+ expect(res.status).toBe(503)+ expect(await res.json()).toEqual({ error: 'provider_unavailable' })+ // Witness: the refusal came from the budget, the path under test.+ expect(budgetAcquire).toHaveBeenCalledTimes(1)+ expect(warn).not.toHaveBeenCalledWith(+ expect.objectContaining({ reason: 'ticket_life' }),+ expect.anything()+ )+ expect(stream.calls).toHaveLength(0)+ await waitFor(() => visualStreamGate.snapshot().running === 0, 'the visual slot was not released')+ })})})diff --git a/grok-llm-proxy/test/requestLimits.test.ts b/grok-llm-proxy/test/requestLimits.test.tsindex 7ba6426d8..0c1202848 100644--- a/grok-llm-proxy/test/requestLimits.test.ts+++ b/grok-llm-proxy/test/requestLimits.test.ts@@ -153,6 +153,8 @@ describe('StreamGate', () => {await expect(gate.acquire()).rejects.toMatchObject({name: 'RequestLimitError',message: 'stream queue wait exceeded',+ // No caller deadline governed this wait, so a dead ticket cannot be inferred.+ kind: 'queue_wait',})// Witness: the waiter was queued for the whole wait, not refused at once.expect(Date.now() - started).toBeGreaterThanOrEqual(45)@@ -180,7 +182,12 @@ describe('StreamGate', () => {setTimeout(() => reject(new Error('the admission deadline did not end the wait')), 1_000)),])- ).rejects.toMatchObject({ name: 'RequestLimitError', message: 'stream queue wait exceeded' })+ ).rejects.toMatchObject({+ name: 'RequestLimitError',+ message: 'stream queue wait exceeded',+ // The caller's deadline ended the wait; the server reads this, not the clock.+ kind: 'deadline',+ })expect(Date.now() - started).toBeGreaterThanOrEqual(45)// The queue slot is free again, so a new waiter queues instead of "queue is full".const next = gate.acquire()diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.tsindex f4bf7e5b3..6005294ed 100644--- a/grok-llm-proxy/test/server.security.test.ts+++ b/grok-llm-proxy/test/server.security.test.ts@@ -1504,7 +1504,7 @@ describe('grok-llm-proxy attempt telemetry', () => {it('(rl1) logs the request-limit reason and labels its failure metric request_limit', async () => {const acquire = vi.spyOn(streamGate, 'acquire')- .mockRejectedValueOnce(new RequestLimitError('stream queue is full'))+ .mockRejectedValueOnce(new RequestLimitError('stream queue is full', 'queue_full'))try {const { res, receipts, lines, metricsText } = await run({ providerAttemptId: 'att-queue-full' })// Witness: the refusal came from the stream gate this test replaced.