Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
chore(merge): merge origin/dev a0502ba7f into fix/731-context-gauge-minified-json
Alfredo Lopezcc07a74253aa
diff --cc codex-llm-proxy/src/requestLimits.tsindex f0607af43,e7e046990..9a5ec8c3d--- a/codex-llm-proxy/src/requestLimits.ts+++ b/codex-llm-proxy/src/requestLimits.ts@@@ -48,17 -4,13 +48,18 @@@ export const STREAM_LIMITS =maxConcurrentStreams: 8,maxQueuedRequests: 16,maxStreamDurationMs: 1_800_000,- // Longest time a request may wait for a stream slot. A visual request can- // wait twice (visualStreamGate, then streamGate), so the bound is chosen so- // that two queue waits + redeem + one heartbeat interval stay below the Host- // HTTP client's 300 s header timeout.+ // Longest total time a request may spend queued in this proxy: one+ // admission clock from arrival bounds the body budget, the visual gate and- // the stream gate together (#739 D1). Bounded so that queue wait + redeem +- // the first keepalive stays below the Host HTTP client's 300 s header- // timeout.++ // the stream gate together (#739 D1), so a visual request that waits at both++ // gates still waits at most this long in total. Bounded so that queue wait +++ // redeem + the first keepalive stays below the Host HTTP client's 300 s++ // header timeout.maxQueueWaitMs: 60_000,// Longest silence tolerated while waiting on the upstream (response headers- // or the next SSE chunk). Matches the Codex CLI stream idle timeout.+ // or the next SSE chunk). Same value as the Codex CLI default,+ // `DEFAULT_STREAM_IDLE_TIMEOUT_MS` (300 s) in openai/codex+ // codex-rs/model-provider-info/src/lib.rs:63 (commit 6824dabe0, read+ // 2026-09-23).upstreamIdleTimeoutMs: 300_000,} as const@@@ -132,42 -88,47 +152,58 @@@ export class StreamGate* Take a stream slot. When `signal` aborts (client disconnected) while the* caller is still queued, the waiter is rejected and its queue slot freed, so* a dropped client never proceeds to redeem an attempt. A waiter still- * queued after `maxQueueWaitMs` is rejected the same way. A deadline timer- * settles it at the bound, and a poll that runs after the bound checks the- * elapsed wait before the slot count, so a slot that frees after the bound- * does not admit it even when the event loop stalled across the bound.+ * queued after `maxQueueWaitMs`, or at `deadlineAt` (epoch ms) when that- * comes first, is rejected the same way (#739 D1). A free slot is granted at- * once whatever the deadline; the caller checks a deadline already past.++ * comes first, is rejected the same way (#739 D1). The bound is fixed when++ * the caller queues, as the smaller of the two, and measured on the++ * monotonic clock from then on. A deadline timer settles the waiter at the++ * bound, and a poll that runs after the bound checks the elapsed wait before++ * the slot count, so a slot that frees after the bound does not admit it++ * even when the event loop stalled across the bound. A free slot is granted++ * at once whatever the deadline; the caller checks a deadline already past.*/- async acquire(signal?: AbortSignal): Promise<() => void> {+ async acquire(signal?: AbortSignal, deadlineAt?: number): Promise<() => void> {+ if (deadlineAt !== undefined && !Number.isFinite(deadlineAt)) {+ throw new RangeError(`a stream gate deadline must be a finite epoch time, got ${deadlineAt}`)+ }if (signal?.aborted) throw new RequestLimitError('stream request was aborted')if (this.running >= this.maxConcurrent) {if (this.queued >= this.maxQueued) throw new RequestLimitError('stream queue is full')this.queued += 1try {- const queuedAt = Date.now()await new Promise<void>((resolve, reject) => {- let timer: ReturnType<typeof setTimeout> | undefined+ let poll: ReturnType<typeof setTimeout> | undefined+ const settle = () => {+ if (poll !== undefined) clearTimeout(poll)+ clearTimeout(deadline)+ signal?.removeEventListener('abort', onAbort)+ }const onAbort = () => {- if (timer !== undefined) clearTimeout(timer)+ settle()reject(new RequestLimitError('stream request was aborted'))}+ const expire = () => {+ settle()+ reject(new RequestLimitError('stream queue wait exceeded'))+ }+ const queuedAt = performance.now()- const deadline = setTimeout(expire, this.maxQueueWaitMs)++ const waitBoundMs =++ deadlineAt === undefined++ ? this.maxQueueWaitMs++ : Math.min(this.maxQueueWaitMs, deadlineAt - Date.now())++ const deadline = setTimeout(expire, Math.max(0, waitBoundMs))const wait = () => {- if (this.running < this.maxConcurrent) {- signal?.removeEventListener('abort', onAbort)- resolve()+ // After the event loop stalls, a poll and the deadline can both be+ // overdue, and Node runs the poll first because it was due first.- if (performance.now() - queuedAt >= this.maxQueueWaitMs) {++ if (performance.now() - queuedAt >= waitBoundMs) {+ expire()return}- const now = Date.now()- if (- now - queuedAt >= this.maxQueueWaitMs ||- (deadlineAt !== undefined && now >= deadlineAt)- ) {- signal?.removeEventListener('abort', onAbort)- reject(new RequestLimitError('stream queue wait exceeded'))+ if (this.running < this.maxConcurrent) {+ settle()+ resolve()return}- timer = setTimeout(wait, 10)+ poll = setTimeout(wait, 10)}signal?.addEventListener('abort', onAbort, { once: true })wait()diff --cc codex-llm-proxy/test/requestLimits.test.tsindex f20c6b198,5a7378bff..8c5770fc1--- a/codex-llm-proxy/test/requestLimits.test.ts+++ b/codex-llm-proxy/test/requestLimits.test.ts@@@ -121,26 -162,169 +163,222 @@@ describe('StreamGate', () =>releaseNext()})+ it('T-AC-3 rejects a waiter at its admission deadline when that comes before maxQueueWaitMs', async () => {+ const gate = new StreamGate(1, 1, 60_000)+ const release = await gate.acquire()+ const started = Date.now()+ const queued = gate.acquire(undefined, started + 50)+ // Witness: the waiter really queued, it was not refused at once.+ expect(gate.snapshot().queued).toBe(1)+ await expect(+ Promise.race([+ queued,+ new Promise<never>((_, reject) =>+ setTimeout(() => reject(new Error('the admission deadline did not end the wait')), 1_000)+ ),+ ])+ ).rejects.toMatchObject({ name: 'RequestLimitError', message: 'stream queue wait exceeded' })+ expect(Date.now() - started).toBeGreaterThanOrEqual(45)+ expect(gate.snapshot().queued).toBe(0)+ release()+ })+++ // The admission deadline shortens the bound the gate's own timer enforces.++ // 55 ms falls between two 10 ms polls, so only the deadline timer can++ // settle the waiter at 55 ms; a timer armed with maxQueueWaitMs would leave++ // it to the 60 ms poll.++ it('T-AC-3b settles at deadlineAt on the deadline timer, not on the next poll', async () => {++ vi.useFakeTimers()++ try {++ const gate = new StreamGate(1, 1, 60_000)++ const release = await gate.acquire()++ const state: { outcome?: string } = {}++ const waiter = gate.acquire(undefined, Date.now() + 55).then(++ () => {++ state.outcome = 'admitted'++ },++ (err: Error) => {++ state.outcome = err.message++ }++ )++ await vi.advanceTimersByTimeAsync(54)++ // Witness: one millisecond before the deadline the waiter is still queued.++ expect(state.outcome).toBeUndefined()++ expect(gate.snapshot()).toEqual({ running: 1, queued: 1 })++ await vi.advanceTimersByTimeAsync(1)++ expect(state.outcome).toBe('stream queue wait exceeded')++ expect(vi.getTimerCount()).toBe(0)++ await waiter++ expect(gate.snapshot()).toEqual({ running: 1, queued: 0 })++ release()++ } finally {++ vi.useRealTimers()++ }++ })+++ it('refuses a waiter at the 60 000 ms default maxQueueWaitMs', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ await vi.advanceTimersByTimeAsync(59_999)+ expect(state.outcome).toBeUndefined()+ await vi.advanceTimersByTimeAsync(1)+ expect(state.outcome).toBe('stream queue wait exceeded')+ await waiter+ release()+ } finally {+ vi.useRealTimers()+ }+ })++ // The gate polls for a free slot every 10 ms. A bound that is not a multiple+ // of 10 falls between two polls, so the next two cases separate a deadline+ // the gate enforces on its own timer from one it only notices on the next poll.+ it('rejects at maxQueueWaitMs and leaves no timer or abort listener behind', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 55)+ const release = await gate.acquire()+ const abort = new AbortController()+ const { state, waiter } = track(gate, abort.signal)+ await vi.advanceTimersByTimeAsync(54)+ // Witness: one millisecond before the bound the waiter is still queued,+ // with its deadline, its poll and its abort listener live.+ expect(state.outcome).toBeUndefined()+ expect(gate.snapshot()).toEqual({ running: 1, queued: 1 })+ expect(vi.getTimerCount()).toBe(2)+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)+ await vi.advanceTimersByTimeAsync(1)+ expect(state.outcome).toBe('stream queue wait exceeded')+ expect(vi.getTimerCount()).toBe(0)+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)+ await waiter+ // The queue slot was returned exactly once: one new waiter queues and+ // the one after it finds the queue full.+ expect(gate.snapshot()).toEqual({ running: 1, queued: 0 })+ const next = gate.acquire()+ await expect(gate.acquire()).rejects.toThrow('stream queue is full')+ release()+ await vi.advanceTimersByTimeAsync(10)+ ;(await next)()+ } finally {+ vi.useRealTimers()+ }+ })++ it('settles at maxQueueWaitMs when a slot frees between the last poll and the bound', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 55)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ // Last poll at 50 ms sees the slot taken; it frees at 52 ms.+ await vi.advanceTimersByTimeAsync(52)+ release()+ // Witness: nothing has settled the waiter yet.+ expect(state.outcome).toBeUndefined()+ await vi.advanceTimersByTimeAsync(3)+ // The bound is a deadline: the waiter is settled at 55 ms, not at the+ // 60 ms poll, and the deadline does not look at the slot count.+ expect(state.outcome).toBe('stream queue wait exceeded')+ await waiter+ expect(gate.snapshot()).toEqual({ running: 0, queued: 0 })+ } finally {+ vi.useRealTimers()+ }+ })++ it('refuses a waiter when the poll due at the bound finds a free slot', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 50)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ await vi.advanceTimersByTimeAsync(45)+ release()+ expect(state.outcome).toBeUndefined()+ // The deadline and the 50 ms poll fall due together; the poll must not+ // admit a waiter whose wait has reached the bound.+ await vi.advanceTimersByTimeAsync(5)+ expect(state.outcome).toBe('stream queue wait exceeded')+ await waiter+ } finally {+ vi.useRealTimers()+ }+ })++ it('refuses a waiter whose poll runs after the bound because the event loop stalled', async () => {+ const gate = new StreamGate(1, 1, 500)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ // Read after acquire() has recorded its own start, so the block below+ // measures at least as long as the gate does. Read before it, a runner+ // pause between the two reads would come out of the 30 ms margin.+ const queuedAt = performance.now()+ await new Promise(resolve => setTimeout(resolve, 20))+ // Witness: the poll has run and found the slot taken. The 500 ms bound+ // leaves 25x the 20 ms wait, so a paused runner does not fire the deadline+ // first; without this witness the deadline could do the rejecting and the+ // test would pass with the elapsed check removed.+ expect(state.outcome).toBeUndefined()+ // Hold the event loop past the bound, then free the slot before any timer+ // can run. The next poll and the deadline are now both overdue, and Node+ // runs the poll first because it was due first.+ while (performance.now() - queuedAt < 530) {+ // busy wait+ }+ release()+ ;(await waiter)?.()+ expect(state.outcome).toBe('stream queue wait exceeded')+ })++ it('admits a waiter whose slot frees before maxQueueWaitMs', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 55)+ const release = await gate.acquire()+ const abort = new AbortController()+ const { state, waiter } = track(gate, abort.signal)+ await vi.advanceTimersByTimeAsync(45)+ // Witness: the abort listener is registered while the waiter is queued.+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)+ release()+ await vi.advanceTimersByTimeAsync(5)+ expect(state.outcome).toBe('admitted')+ // The deadline timer and the abort listener were cleared on admission.+ expect(vi.getTimerCount()).toBe(0)+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)+ await vi.advanceTimersByTimeAsync(100)+ expect(state.outcome).toBe('admitted')+ expect(gate.snapshot()).toEqual({ running: 1, queued: 0 })+ ;(await waiter)?.()+ } finally {+ vi.useRealTimers()+ }+ })++ it('refuses a maxQueueWaitMs that setTimeout cannot honor', () => {+ // Witness: the bounds themselves are accepted.+ expect(() => new StreamGate(1, 1, 1)).not.toThrow()+ expect(() => new StreamGate(1, 1, 2_147_483_647)).not.toThrow()+ for (const maxQueueWaitMs of [0, -1, 1.5, Number.NaN, 2_147_483_648]) {+ expect(() => new StreamGate(1, 1, maxQueueWaitMs), String(maxQueueWaitMs)).toThrow(RangeError)+ }+ })++ it('refuses a slot or queue size the gate cannot enforce', () => {+ // Witness: the smallest sizes are accepted, including a gate with no queue.+ expect(() => new StreamGate(1, 0)).not.toThrow()+ for (const maxConcurrent of [0, -1, 1.5, Number.NaN]) {+ expect(() => new StreamGate(maxConcurrent, 1), `maxConcurrent=${maxConcurrent}`).toThrow(RangeError)+ }+ for (const maxQueued of [-1, 1.5, Number.NaN]) {+ expect(() => new StreamGate(1, maxQueued), `maxQueued=${maxQueued}`).toThrow(RangeError)+ }+ })+it('pins the visual 2/8 sibling against the ordinary 8/16 stream gate', () => {expect(VISUAL_STREAM_LIMITS).toEqual({ maxConcurrentStreams: 2, maxQueuedRequests: 8 })expect(STREAM_LIMITS.maxConcurrentStreams).toBe(8)diff --cc docs/architecture/codex-subscription-transport-contract.mdindex 5f729207c,ebcca9a6c..16fb4304f--- a/docs/architecture/codex-subscription-transport-contract.md+++ b/docs/architecture/codex-subscription-transport-contract.md@@@ -343,20 -308,11 +343,25 @@@ behavior changes- It drops queued stream-gate waiters on abort and checks the abort signalbefore redeeming a ticket. It also rejects an invalid or out-of-boundsdeadline before the redeem, so the single-use ticket is not consumed.- - It rejects a stream-gate waiter still queued after `maxQueueWaitMs` with- `provider_unavailable` (reason `stream queue wait exceeded`). A visual- request can wait twice (the visual gate, then the ordinary gate), so two- queue waits, the 15 s control-api redeem timeout and one heartbeat interval- together stay below the Host HTTP client's 300 s header timeout.+ - It gives each request one admission clock, stamped at arrival: arrival ++ `maxQueueWaitMs`. The body budget, the visual gate and the stream gate+ all wait against that same instant, so `maxQueueWaitMs` is the total+ time a request may spend queued in the proxy. A waiter still queued when+ it runs out is rejected with `provider_unavailable` (reason+ `body admission wait exceeded` or `stream queue wait exceeded`). Queue+ wait, the 15 s control-api redeem timeout and the first keepalive+ together (60 + 15 + 60 = 135 s) stay below the Host HTTP client's 300 s+ header timeout.++ - A gate waiter's bound is enforced by its own timer, measured on the++ monotonic clock from the moment it queues. A poll that runs after the++ bound checks the elapsed wait before the slot count, so a slot that frees++ after the bound does not admit the waiter even when the event loop++ stalled across the bound.+ - The stream-gate wait also ends at the execution ticket's `exp`, with no+ margin. A request still queued then is answered 503+ `provider_unavailable` without a redeem, and the proxy logs+ `codex_proxy_admission_refused` with `reason: ticket_life`,+ `providerAttemptId` and `hostRef`.- It requires the redeem response to carry `maxStreamDurationMs` greaterthan 0. An absent value is a contract violation, not a default.- It logs one `codex_proxy_attempt_finished` event per completion attempt,diff --cc grok-llm-proxy/src/requestLimits.tsindex 592d7b87d,7cb86ea60..7496b9085--- a/grok-llm-proxy/src/requestLimits.ts+++ b/grok-llm-proxy/src/requestLimits.ts@@@ -123,42 -83,47 +142,58 @@@ export class StreamGate* Take a stream slot. When `signal` aborts (client disconnected) while the* caller is still queued, the waiter is rejected and its queue slot freed, so* a dropped client never proceeds to redeem an attempt. A waiter still- * queued after `maxQueueWaitMs` is rejected the same way. A deadline timer- * settles it at the bound, and a poll that runs after the bound checks the- * elapsed wait before the slot count, so a slot that frees after the bound- * does not admit it even when the event loop stalled across the bound.+ * queued after `maxQueueWaitMs`, or at `deadlineAt` (epoch ms) when that- * comes first, is rejected the same way (#739 D1). A free slot is granted at- * once whatever the deadline; the caller checks a deadline already past.++ * comes first, is rejected the same way (#739 D1). The bound is fixed when++ * the caller queues, as the smaller of the two, and measured on the++ * monotonic clock from then on. A deadline timer settles the waiter at the++ * bound, and a poll that runs after the bound checks the elapsed wait before++ * the slot count, so a slot that frees after the bound does not admit it++ * even when the event loop stalled across the bound. A free slot is granted++ * at once whatever the deadline; the caller checks a deadline already past.*/- async acquire(signal?: AbortSignal): Promise<() => void> {+ async acquire(signal?: AbortSignal, deadlineAt?: number): Promise<() => void> {+ if (deadlineAt !== undefined && !Number.isFinite(deadlineAt)) {+ throw new RangeError(`a stream gate deadline must be a finite epoch time, got ${deadlineAt}`)+ }if (signal?.aborted) throw new RequestLimitError('stream request was aborted')if (this.running >= this.maxConcurrent) {if (this.queued >= this.maxQueued) throw new RequestLimitError('stream queue is full')this.queued += 1try {- const queuedAt = Date.now()await new Promise<void>((resolve, reject) => {- let timer: ReturnType<typeof setTimeout> | undefined+ let poll: ReturnType<typeof setTimeout> | undefined+ const settle = () => {+ if (poll !== undefined) clearTimeout(poll)+ clearTimeout(deadline)+ signal?.removeEventListener('abort', onAbort)+ }const onAbort = () => {- if (timer !== undefined) clearTimeout(timer)+ settle()reject(new RequestLimitError('stream request was aborted'))}+ const expire = () => {+ settle()+ reject(new RequestLimitError('stream queue wait exceeded'))+ }+ const queuedAt = performance.now()- const deadline = setTimeout(expire, this.maxQueueWaitMs)++ const waitBoundMs =++ deadlineAt === undefined++ ? this.maxQueueWaitMs++ : Math.min(this.maxQueueWaitMs, deadlineAt - Date.now())++ const deadline = setTimeout(expire, Math.max(0, waitBoundMs))const wait = () => {- if (this.running < this.maxConcurrent) {- signal?.removeEventListener('abort', onAbort)- resolve()+ // After the event loop stalls, a poll and the deadline can both be+ // overdue, and Node runs the poll first because it was due first.- if (performance.now() - queuedAt >= this.maxQueueWaitMs) {++ if (performance.now() - queuedAt >= waitBoundMs) {+ expire()return}- const now = Date.now()- if (- now - queuedAt >= this.maxQueueWaitMs ||- (deadlineAt !== undefined && now >= deadlineAt)- ) {- signal?.removeEventListener('abort', onAbort)- reject(new RequestLimitError('stream queue wait exceeded'))+ if (this.running < this.maxConcurrent) {+ settle()+ resolve()return}- timer = setTimeout(wait, 10)+ poll = setTimeout(wait, 10)}signal?.addEventListener('abort', onAbort, { once: true })wait()diff --cc grok-llm-proxy/test/requestLimits.test.tsindex 593970115,bd271b6e4..cd7891d79--- a/grok-llm-proxy/test/requestLimits.test.ts+++ b/grok-llm-proxy/test/requestLimits.test.ts@@@ -120,29 -156,162 +161,226 @@@ describe('StreamGate', () =>releaseNext()})+ it('T-AC-3-grok rejects a waiter at its admission deadline when that comes before maxQueueWaitMs', async () => {+ const gate = new StreamGate(1, 1, 60_000)+ const release = await gate.acquire()+ const started = Date.now()+ const queued = gate.acquire(undefined, started + 50)+ // Witness: the waiter really queued, so the one queue slot is taken.+ await expect(gate.acquire()).rejects.toMatchObject({+ name: 'RequestLimitError',+ message: 'stream queue is full',+ })+ await expect(+ Promise.race([+ queued,+ new Promise<never>((_, reject) =>+ setTimeout(() => reject(new Error('the admission deadline did not end the wait')), 1_000)+ ),+ ])+ ).rejects.toMatchObject({ name: 'RequestLimitError', message: 'stream queue wait exceeded' })+ 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()+ release()+ const releaseNext = await next+ releaseNext()+ })++++ // The admission deadline shortens the bound the gate's own timer enforces.++ // 55 ms falls between two 10 ms polls, so only the deadline timer can++ // settle the waiter at 55 ms; a timer armed with maxQueueWaitMs would leave++ // it to the 60 ms poll.++ it('T-AC-3b-grok settles at deadlineAt on the deadline timer, not on the next poll', async () => {++ vi.useFakeTimers()++ try {++ const gate = new StreamGate(1, 1, 60_000)++ const release = await gate.acquire()++ const state: { outcome?: string } = {}++ const waiter = gate.acquire(undefined, Date.now() + 55).then(++ () => {++ state.outcome = 'admitted'++ },++ (err: Error) => {++ state.outcome = err.message++ }++ )++ await vi.advanceTimersByTimeAsync(54)++ // Witness: one millisecond before the deadline the waiter is still++ // queued, holding the one queue slot.++ expect(state.outcome).toBeUndefined()++ await expect(gate.acquire()).rejects.toThrow('stream queue is full')++ await vi.advanceTimersByTimeAsync(1)++ expect(state.outcome).toBe('stream queue wait exceeded')++ expect(vi.getTimerCount()).toBe(0)++ await waiter++ // The queue slot is free again: a new waiter queues and is admitted++ // once the running stream releases.++ const next = gate.acquire()++ release()++ await vi.advanceTimersByTimeAsync(10)++ ;(await next)()++ } finally {++ vi.useRealTimers()++ }++ })+++ it('refuses a waiter at the 60 000 ms default maxQueueWaitMs', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ await vi.advanceTimersByTimeAsync(59_999)+ expect(state.outcome).toBeUndefined()+ await vi.advanceTimersByTimeAsync(1)+ expect(state.outcome).toBe('stream queue wait exceeded')+ await waiter+ release()+ } finally {+ vi.useRealTimers()+ }+ })++ // The gate polls for a free slot every 10 ms. A bound that is not a multiple+ // of 10 falls between two polls, so the next two cases separate a deadline+ // the gate enforces on its own timer from one it only notices on the next poll.+ it('rejects at maxQueueWaitMs and leaves no timer or abort listener behind', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 55)+ const release = await gate.acquire()+ const abort = new AbortController()+ const { state, waiter } = track(gate, abort.signal)+ await vi.advanceTimersByTimeAsync(54)+ // Witness: one millisecond before the bound the waiter is still queued,+ // with its deadline, its poll and its abort listener live.+ expect(state.outcome).toBeUndefined()+ expect(vi.getTimerCount()).toBe(2)+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)+ await vi.advanceTimersByTimeAsync(1)+ expect(state.outcome).toBe('stream queue wait exceeded')+ expect(vi.getTimerCount()).toBe(0)+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)+ await waiter+ // The queue slot was returned exactly once: one new waiter queues and+ // the one after it finds the queue full.+ const next = gate.acquire()+ await expect(gate.acquire()).rejects.toThrow('stream queue is full')+ release()+ await vi.advanceTimersByTimeAsync(10)+ ;(await next)()+ } finally {+ vi.useRealTimers()+ }+ })++ it('settles at maxQueueWaitMs when a slot frees between the last poll and the bound', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 55)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ // Last poll at 50 ms sees the slot taken; it frees at 52 ms.+ await vi.advanceTimersByTimeAsync(52)+ release()+ // Witness: nothing has settled the waiter yet.+ expect(state.outcome).toBeUndefined()+ await vi.advanceTimersByTimeAsync(3)+ // The bound is a deadline: the waiter is settled at 55 ms, not at the+ // 60 ms poll, and the deadline does not look at the slot count.+ expect(state.outcome).toBe('stream queue wait exceeded')+ await waiter+ } finally {+ vi.useRealTimers()+ }+ })++ it('refuses a waiter when the poll due at the bound finds a free slot', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 50)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ await vi.advanceTimersByTimeAsync(45)+ release()+ expect(state.outcome).toBeUndefined()+ // The deadline and the 50 ms poll fall due together; the poll must not+ // admit a waiter whose wait has reached the bound.+ await vi.advanceTimersByTimeAsync(5)+ expect(state.outcome).toBe('stream queue wait exceeded')+ await waiter+ } finally {+ vi.useRealTimers()+ }+ })++ it('refuses a waiter whose poll runs after the bound because the event loop stalled', async () => {+ const gate = new StreamGate(1, 1, 500)+ const release = await gate.acquire()+ const { state, waiter } = track(gate)+ // Read after acquire() has recorded its own start, so the block below+ // measures at least as long as the gate does. Read before it, a runner+ // pause between the two reads would come out of the 30 ms margin.+ const queuedAt = performance.now()+ await new Promise(resolve => setTimeout(resolve, 20))+ // Witness: the poll has run and found the slot taken. The 500 ms bound+ // leaves 25x the 20 ms wait, so a paused runner does not fire the deadline+ // first; without this witness the deadline could do the rejecting and the+ // test would pass with the elapsed check removed.+ expect(state.outcome).toBeUndefined()+ // Hold the event loop past the bound, then free the slot before any timer+ // can run. The next poll and the deadline are now both overdue, and Node+ // runs the poll first because it was due first.+ while (performance.now() - queuedAt < 530) {+ // busy wait+ }+ release()+ ;(await waiter)?.()+ expect(state.outcome).toBe('stream queue wait exceeded')+ })++ it('admits a waiter whose slot frees before maxQueueWaitMs', async () => {+ vi.useFakeTimers()+ try {+ const gate = new StreamGate(1, 1, 55)+ const release = await gate.acquire()+ const abort = new AbortController()+ const { state, waiter } = track(gate, abort.signal)+ await vi.advanceTimersByTimeAsync(45)+ // Witness: the abort listener is registered while the waiter is queued.+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(1)+ release()+ await vi.advanceTimersByTimeAsync(5)+ expect(state.outcome).toBe('admitted')+ // The deadline timer and the abort listener were cleared on admission.+ expect(vi.getTimerCount()).toBe(0)+ expect(getEventListeners(abort.signal, 'abort')).toHaveLength(0)+ await vi.advanceTimersByTimeAsync(100)+ expect(state.outcome).toBe('admitted')+ ;(await waiter)?.()+ } finally {+ vi.useRealTimers()+ }+ })++ it('refuses a maxQueueWaitMs that setTimeout cannot honor', () => {+ // Witness: the bounds themselves are accepted.+ expect(() => new StreamGate(1, 1, 1)).not.toThrow()+ expect(() => new StreamGate(1, 1, 2_147_483_647)).not.toThrow()+ for (const maxQueueWaitMs of [0, -1, 1.5, Number.NaN, 2_147_483_648]) {+ expect(() => new StreamGate(1, 1, maxQueueWaitMs), String(maxQueueWaitMs)).toThrow(RangeError)+ }+ })++ it('refuses a slot or queue size the gate cannot enforce', () => {+ // Witness: the smallest sizes are accepted, including a gate with no queue.+ expect(() => new StreamGate(1, 0)).not.toThrow()+ for (const maxConcurrent of [0, -1, 1.5, Number.NaN]) {+ expect(() => new StreamGate(maxConcurrent, 1), `maxConcurrent=${maxConcurrent}`).toThrow(RangeError)+ }+ for (const maxQueued of [-1, 1.5, Number.NaN]) {+ expect(() => new StreamGate(1, maxQueued), `maxQueued=${maxQueued}`).toThrow(RangeError)+ }+ })})