The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(benchmark): the wall clock reaches a stream that stopped sending
Daviddebd71d44ce4
diff --git a/src/lib/__tests__/benchmark-run.test.ts b/src/lib/__tests__/benchmark-run.test.tsindex 6b3436e8..077541d1 100644--- a/src/lib/__tests__/benchmark-run.test.ts+++ b/src/lib/__tests__/benchmark-run.test.ts@@ -108,3 +108,74 @@ describe('measureRun', () => {expect(m.correct).toBe(false)})})++// P3 from the review of C1 (2026-08-14). The wall clock is the half of the+// brake meant for "the benchmark simply stops moving", and it was the one case+// it could not catch: the check lived inside the for-await body, so it only ran+// when a chunk arrived. A model that stops sending parks the loop on a pending+// next() forever. The token cap cannot help there either, because a stalled+// stream produces no tokens.+describe('the wall clock reaches a stream that has stopped sending', () => {+ /** A stream that yields one chunk and then never resolves again. */+ function stalling(): AsyncIterable<any> {+ let served = false+ return {+ [Symbol.asyncIterator]() {+ return {+ next() {+ if (!served) {+ served = true+ return Promise.resolve({ done: false, value: { content: 'hi' } })+ }+ return new Promise<never>(() => { /* the stall */ })+ },+ return() { return Promise.resolve({ done: true, value: undefined }) },+ }+ },+ }+ }++ it('stops with finishReason timeout instead of hanging', async () => {+ const onLimit = vi.fn()+ const m = await measureRun(stalling(), () => true, {+ clock: () => 0,+ maxMs: 50,+ // Trip immediately, so the test does not wait on a real timer.+ deadlineIn: () => Promise.resolve(),+ onLimit,+ })+ expect(m.finishReason).toBe('timeout')+ expect(onLimit).toHaveBeenCalledWith('timeout')+ })++ it('closes the generator so the model is not left generating', async () => {+ let closed = false+ const stream: AsyncIterable<any> = {+ [Symbol.asyncIterator]() {+ return {+ next() { return new Promise<never>(() => {}) },+ return() { closed = true; return Promise.resolve({ done: true, value: undefined }) },+ }+ },+ }+ await measureRun(stream, () => true, {+ clock: () => 0, maxMs: 10, deadlineIn: () => Promise.resolve(),+ })+ expect(closed).toBe(true)+ })++ it('a stream that finishes normally is untouched by the deadline', async () => {+ async function* good() {+ yield { content: 'a' }+ yield { content: 'b', finishReason: 'stop' }+ }+ const m = await measureRun(good(), () => true, {+ clock: () => 0,+ maxMs: 1000,+ // A deadline that never fires, the shape of a healthy run.+ deadlineIn: () => new Promise<void>(() => {}),+ })+ expect(m.finishReason).toBe('stop')+ expect(m.totalTokens).toBe(2)+ })+})diff --git a/src/lib/benchmark-run.ts b/src/lib/benchmark-run.tsindex 5fe17262..3f2b82f7 100644--- a/src/lib/benchmark-run.ts+++ b/src/lib/benchmark-run.ts@@ -45,6 +45,9 @@ export interface MeasureOptions {/** Called the moment a cap trips, so the caller can abort the upstream* request rather than leave the model generating into a dropped stream. */onLimit?: (reason: 'runaway' | 'timeout') => void+ /** Resolves after `ms`. Injected for the same reason `clock` is: a test must+ * be able to trip the wall clock without waiting five real minutes. */+ deadlineIn?: (ms: number) => Promise<void>}export async function measureRun(@@ -57,6 +60,10 @@ export async function measureRun(maxTokens = RUNAWAY_TOKEN_CAP,maxMs = RUNAWAY_MS_CAP,onLimit,+ deadlineIn = (ms) => new Promise<void>((resolve) => {+ const t = setTimeout(resolve, ms) as unknown as { unref?: () => void }+ t.unref?.()+ }),} = optionsconst startTime = clock()let firstTokenTime = 0@@ -67,7 +74,25 @@ export async function measureRun(let apiEvalCount: number | undefinedlet apiEvalDurationMs: number | undefined- for await (const chunk of stream) {+ // Not `for await`: that parks on the pending next() and the wall clock below+ // is only ever read when a chunk arrives. A model that stops sending is+ // exactly the case the wall clock exists for (ElBiggus: "the benchmark simply+ // stops moving"), and it was the one case it could not catch. One deadline+ // for the whole run, raced against every step.+ const iterator = stream[Symbol.asyncIterator]()+ const deadline = deadlineIn(maxMs).then(() => ({ kind: 'timeout' as const }))+ try {+ for (;;) {+ const step = await Promise.race([+ iterator.next().then((r) => ({ kind: 'step' as const, r })),+ deadline,+ ])+ if (step.kind === 'timeout') {+ finishReason = 'timeout'+ break+ }+ if (step.r.done) break+ const chunk = step.r.value// Time to first token is the first output of any kind: for a model that// reasons out loud the thinking arrives before the answer, and starting// the clock only at the first answer token would hide the whole reasoning@@ -108,6 +133,11 @@ export async function measureRun(break}}+ } finally {+ // for await closed the generator on break; doing it by hand keeps that,+ // so a stalled upstream is released instead of left generating.+ await iterator.return?.(undefined).catch(() => { /* already closed */ })+ }if (finishReason === 'runaway' || finishReason === 'timeout') {onLimit?.(finishReason)}