Skip to content

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.

fix(agent): flash_timeout wird wie credits_exhausted als endgueltig erkannt (R5-53)

purplef3e46ac6cdbb
diff --git a/src/api/providers/__tests__/transient-retry.test.ts b/src/api/providers/__tests__/transient-retry.test.ts
index 6ab8aa79..8709e334 100644
--- a/src/api/providers/__tests__/transient-retry.test.ts
+++ b/src/api/providers/__tests__/transient-retry.test.ts
@@ -115,6 +115,30 @@ describe('sendWithTransientRetry', () => {
expect(await out.text()).toContain('credits_exhausted')
})
+ it('R5-53: does not retry a flash_timeout, which answers 504 and never clears', async () => {
+ const clock = recorder()
+ const send = vi.fn().mockResolvedValue(
+ res(504, JSON.stringify({ error: 'The free chat request reached its four-minute limit. Please retry.', code: 'flash_timeout' })),
+ )
+
+ const out = await sendWithTransientRetry(send, { wait: clock.wait })
+
+ expect(send).toHaveBeenCalledTimes(1)
+ expect(clock.waited).toEqual([])
+ expect(await out.text()).toContain('flash_timeout')
+ })
+
+ it('NEGATIVE CONTROL: a 504 without flash_timeout is still retried', async () => {
+ const clock = recorder()
+ const send = vi.fn().mockResolvedValue(res(504, 'bad gateway'))
+
+ const out = await sendWithTransientRetry(send, { wait: clock.wait })
+
+ expect(out.status).toBe(504)
+ expect(send).toHaveBeenCalledTimes(MAX_TRANSIENT_ATTEMPTS)
+ expect(clock.waited).toEqual([500, 1000])
+ })
+
it('stops on abort instead of queueing Stop behind a backoff', async () => {
const abort = new AbortController()
const send = vi.fn().mockResolvedValue(res(503))
diff --git a/src/api/providers/retry.ts b/src/api/providers/retry.ts
index 4b2af515..55d37ee7 100644
--- a/src/api/providers/retry.ts
+++ b/src/api/providers/retry.ts
@@ -75,6 +75,24 @@ async function isPermanent429(res: Response): Promise<boolean> {
}
}
+/**
+ * R5-53: a 504 carrying `code: 'flash_timeout'` means the free-tier request
+ * already sat out its own four-minute hard deadline (see the server's
+ * `abortedResponse` in app/api/inference/v1/chat/completions/route.ts).
+ * Retrying reruns the same request against the same deadline and produces
+ * the identical timeout every time — this is the 504 twin of
+ * `isPermanent429` above, not a transient gateway hiccup a second attempt
+ * could fix.
+ */
+async function isPermanentFlashTimeout(res: Response): Promise<boolean> {
+ if (res.status !== 504) return false
+ try {
+ return /flash_timeout/.test(await res.clone().text())
+ } catch {
+ return false
+ }
+}
+
export interface TransientRetryOptions {
signal?: AbortSignal
/** Total attempts including the first. Defaults to MAX_TRANSIENT_ATTEMPTS. */
@@ -104,6 +122,7 @@ export async function sendWithTransientRetry(
if (!TRANSIENT_STATUS.has(res.status)) return res
if (opts?.signal?.aborted) return res
if (await isPermanent429(res)) return res
+ if (await isPermanentFlashTimeout(res)) return res
// The server's own number wins — a fixed-window limiter refuses again for
// the whole window, so guessing shorter just burns an attempt inside it.
diff --git a/src/hooks/__tests__/flash-timeout-stops-the-loop.test.ts b/src/hooks/__tests__/flash-timeout-stops-the-loop.test.ts
new file mode 100644
index 00000000..09768d8b
--- /dev/null
+++ b/src/hooks/__tests__/flash-timeout-stops-the-loop.test.ts
@@ -0,0 +1,63 @@
+/**
+ * R5-53 — a 504 with code 'flash_timeout' means the free-tier request already
+ * sat out its own four-minute server-side deadline; retrying it repeats that
+ * same wait. Before this fix `isTerminalModelError` did not recognise the
+ * code (a 504 falls outside "status >= 400 && status < 500"), so the
+ * connRetries ladder in useAgentChat.ts retried it up to three times —
+ * twelve silent minutes before the run gave up with a generic message.
+ *
+ * Source-level, matching loop-stops-on-terminal.test.ts: what has to hold is
+ * a property of the catch block's control flow, not of a rendered component.
+ *
+ * Run: npx vitest run src/hooks/__tests__/flash-timeout-stops-the-loop.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { fileURLToPath } from 'node:url'
+import { dirname, resolve } from 'node:path'
+
+const here = dirname(fileURLToPath(import.meta.url))
+const agent = readFileSync(resolve(here, '../useAgentChat.ts'), 'utf8')
+
+describe('R5-53: the flash_timeout branch in useAgentChat.ts', () => {
+ it('sets loopHalt inside the flash_timeout branch', () => {
+ const branch = agent.indexOf("code === 'flash_timeout'")
+ const halt = agent.indexOf("loopHalt = 'flash timeout'")
+ expect(branch).toBeGreaterThan(-1)
+ expect(halt).toBeGreaterThan(-1)
+ expect(halt - branch).toBeGreaterThan(0)
+ expect(halt - branch).toBeLessThan(500)
+ })
+
+ it('leaves the server text standing instead of writing new prose', () => {
+ const branch = agent.indexOf("code === 'flash_timeout'")
+ const block = agent.slice(branch, branch + 900)
+ expect(block).toContain('+ errorMsg')
+ })
+
+ it('the driver checks loopHalt before scheduling another pass', () => {
+ const halt = agent.indexOf('opts?.loop && convId && loopHalt')
+ const schedule = agent.indexOf('agentLoopTimers.set(convForLoop, setTimeout(')
+ expect(halt).toBeGreaterThan(-1)
+ expect(schedule).toBeGreaterThan(-1)
+ expect(halt).toBeLessThan(schedule)
+ })
+})
+
+describe('R5-53: isTerminalModelError treats flash_timeout like credits_exhausted', () => {
+ const httpStatus = readFileSync(resolve(here, '../../lib/http-status.ts'), 'utf8')
+
+ it('the terminal guard checks the code, not just the 4xx range', () => {
+ expect(httpStatus).toContain("e?.code === 'flash_timeout'")
+ })
+
+ it('NEGATIVE CONTROL: a plain 504 (no code) is not named in the terminal guard', () => {
+ // The terminal check is code-based, not status-based, for this one — a
+ // bare 504 must keep falling through to the transient path below it.
+ const guardLine = httpStatus.slice(
+ httpStatus.indexOf('export function isTerminalModelError'),
+ httpStatus.indexOf('return status >= 400'),
+ )
+ expect(guardLine).not.toMatch(/status === 504/)
+ })
+})
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.ts
index 10562974..e07b740c 100644
--- a/src/hooks/useAgentChat.ts
+++ b/src/hooks/useAgentChat.ts
@@ -2546,6 +2546,17 @@ export function useAgentChat() {
(runState.content ? runState.content + '\n\n' : '') +
`The server is limiting how many requests this account may send in a short window, and the run waited for it once already. Give it ${when}, then send your message again. Nothing was charged for the refused attempts.`
)
+ } else if ((err as { code?: string })?.code === 'flash_timeout') {
+ // R5-53: isTerminalModelError now stops this from being retried at
+ // all, so this branch is reached after exactly one attempt, not
+ // three. Unlike the 429 branch above, the server's own line already
+ // says exactly what happened ("reached its four-minute limit"), so
+ // it is left standing instead of being replaced with new prose.
+ loopHalt = 'flash timeout'
+ useChatStore.getState().updateMessageContent(
+ convId!, assistantMessage.id,
+ (runState.content ? runState.content + '\n\n' : '') + errorMsg
+ )
} else if (sendRefusal) {
// Bug B3 round 2, nebenbefund 3: a chat-tools turn in PLAIN chat
// runs through this same executor, so the template's own Jinja
diff --git a/src/lib/__tests__/http-status.test.ts b/src/lib/__tests__/http-status.test.ts
index 83a7e03f..6b7b4381 100644
--- a/src/lib/__tests__/http-status.test.ts
+++ b/src/lib/__tests__/http-status.test.ts
@@ -98,3 +98,24 @@ describe('401 depends on whose credential it is', () => {
expect(isTerminalModelError(Object.assign(new Error('timeout'), { statusCode: 408 }))).toBe(false)
})
})
+
+// ── R5-53: a 504 with code 'flash_timeout' is a hard four-minute deadline ───
+//
+// Without this, `status >= 400 && status < 500` is false for a 504, so
+// isTerminalModelError fell through to "not terminal" and the connRetries
+// ladder in useAgentChat.ts repeated the same request up to three times.
+// The server had already spent its own four-minute deadline once, so three
+// attempts meant twelve silent minutes before the run gave up.
+describe('R5-53: flash_timeout is terminal like credits_exhausted', () => {
+ it('a 504 carrying code: flash_timeout is terminal', () => {
+ expect(isTerminalModelError(new ProviderError(
+ 'The free chat request reached its four-minute limit. Please retry.',
+ 'lu-cloud', 'flash_timeout', 504,
+ ))).toBe(true)
+ })
+
+ it('NEGATIVE CONTROL: a 504 WITHOUT that code is still retried', () => {
+ expect(isTerminalModelError(new ProviderError('bad gateway', 'lu-cloud', undefined, 504))).toBe(false)
+ expect(isTerminalModelError(Object.assign(new Error('gateway timeout'), { statusCode: 504 }))).toBe(false)
+ })
+})
diff --git a/src/lib/http-status.ts b/src/lib/http-status.ts
index 3e41669b..6aa87058 100644
--- a/src/lib/http-status.ts
+++ b/src/lib/http-status.ts
@@ -99,7 +99,14 @@ export function retryDelayMs(err: unknown, attempt: number): number {
export function isTerminalModelError(err: unknown): boolean {
const e = err as { code?: unknown; provider?: unknown } | null
- if (e?.code === 'credits_exhausted' || e?.code === 'signed_out') return true
+ // R5-53: `flash_timeout` is a 504, so without this line it fell through to
+ // "status >= 400 && status < 500" (false) and read as a plain gateway
+ // hiccup worth retrying. The free-tier request had already sat out its own
+ // four-minute hard deadline (see the server's route), so each of the three
+ // attempts in the connRetries ladder repeated the same four-minute wait —
+ // twelve silent minutes before the run gave up, same shape as the
+ // `credits_exhausted` bug this line is modelled on.
+ if (e?.code === 'credits_exhausted' || e?.code === 'signed_out' || e?.code === 'flash_timeout') return true
const status = httpStatusOf(err)
if (status === 429 || status === 408) return false
if (status === 401 && e?.provider === 'lu-cloud') return false