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.

Stop runaway question mark output from the local engine

David651b9ca537da
diff --git a/src/api/providers/__tests__/builtin-repetition-stop.test.ts b/src/api/providers/__tests__/builtin-repetition-stop.test.ts
new file mode 100644
index 00000000..0d0bbf43
--- /dev/null
+++ b/src/api/providers/__tests__/builtin-repetition-stop.test.ts
@@ -0,0 +1,47 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+const transport = vi.hoisted(() => ({ fetch: vi.fn() }))
+vi.mock('../../backend', () => ({
+ localFetch: vi.fn(async () => new Response('{}', { status: 404 })),
+ localFetchStream: (...args: unknown[]) => transport.fetch(...args),
+ isPrivateOrLanHost: () => true,
+ isDirectFetchAllowed: () => true,
+ hostnameOf: (url: string) => new URL(url).hostname,
+ ensureProxyAllowsHost: vi.fn(), backendCall: vi.fn(), isTauri: () => false,
+}))
+vi.mock('../../builtin-ensure', () => ({
+ ensureBuiltinEngineAlive: vi.fn(), explainDeadEngine: (e: unknown) => e,
+ explainEngineTransportMessage: (s: string) => s, isManagedBuiltinSlot: () => false,
+}))
+
+import { OpenAIProvider } from '../openai-provider'
+
+afterEach(() => vi.clearAllMocks())
+
+describe('built-in provider stops broken decoding at the transport', () => {
+ it.each(['content', 'reasoning_content'])('aborts endless %s without waiting for EOF', async field => {
+ let signal: AbortSignal | undefined
+ transport.fetch.mockImplementation(async (_url: string, init: RequestInit) => {
+ signal = init.signal ?? undefined
+ return new Response(new ReadableStream({
+ start(controller) {
+ const event = JSON.stringify({ choices: [{ delta: { [field]: '?'.repeat(256) } }] })
+ controller.enqueue(new TextEncoder().encode(`data: ${event}\n\n`))
+ signal?.addEventListener('abort', () => controller.error(new DOMException('Aborted', 'AbortError')), { once: true })
+ },
+ }))
+ })
+ const provider = new OpenAIProvider({
+ id: 'openai', name: 'Built-in Engine', enabled: true, apiKey: '',
+ baseUrl: 'http://127.0.0.1:8127/v1', isLocal: true, managed: true,
+ })
+ const run = async () => {
+ for await (const chunk of provider.chatStream('test-local-model', [{ role: 'user', content: 'Hello' }])) {
+ expect(chunk.content).not.toContain('?'.repeat(256))
+ }
+ }
+ await expect(run()).rejects.toThrow('Generation stopped because the local model repeated question marks continuously.')
+ expect(signal?.aborted).toBe(true)
+ expect(transport.fetch).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/src/api/providers/openai-provider.ts b/src/api/providers/openai-provider.ts
index a7faf97e..b04605bc 100644
--- a/src/api/providers/openai-provider.ts
+++ b/src/api/providers/openai-provider.ts
@@ -14,6 +14,7 @@ import type {
ChatStreamChunk, ToolCall, ToolDefinition,
} from './types'
import { ProviderError } from './types'
+import { RepetitionStop } from '../../lib/repetition-stop'
import { parseSSEStream } from '../sse'
import { idleAbortGuard, isStreamIdleTimeout } from '../stream-idle'
import { sendWithTransientRetry } from './retry'
@@ -723,6 +724,8 @@ export class OpenAIProvider implements ProviderClient {
// Stop still propagates inward, and a stream that goes silent can cancel
// its own request instead of leaving reader.read() pending forever.
const guard = idleAbortGuard(options?.signal)
+ const repetitionStop = this.config.managed === true ? new RepetitionStop() : undefined
+ const reasoningRepetitionStop = this.config.managed === true ? new RepetitionStop() : undefined
let res: Response
try {
res = await this.sendChat(model, body, guard.signal, fetcher)
@@ -808,6 +811,10 @@ export class OpenAIProvider implements ProviderClient {
// without this the entire reasoning phase of a cloud reasoner is
// silently dropped and the chat sits in dead air (uselu fc55c91).
const reasoning = choice.delta?.reasoning_content ?? choice.delta?.reasoning ?? ''
+ if (repetitionStop?.push(content) || reasoningRepetitionStop?.push(reasoning)) {
+ guard.abort()
+ throw new Error('Generation stopped because the local model repeated question marks continuously. Try setting GPU Layers to 0 in LU Engine settings, or verify and download the model again.')
+ }
if (reasoning) {
yield { content: '', thinking: reasoning, done: false }
}
diff --git a/src/lib/__tests__/repetition-stop.test.ts b/src/lib/__tests__/repetition-stop.test.ts
new file mode 100644
index 00000000..d59574f2
--- /dev/null
+++ b/src/lib/__tests__/repetition-stop.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from 'vitest'
+import { RepetitionStop } from '../repetition-stop'
+
+describe('local decoder repetition stop', () => {
+ it('stops a run split across individual token deltas', () => {
+ const guard = new RepetitionStop()
+ for (let i = 0; i < 255; i++) expect(guard.push('?')).toBe(false)
+ expect(guard.push('?')).toBe(true)
+ })
+
+ it('detects a run inside a large mixed chunk', () => {
+ expect(new RepetitionStop().push(`prefix${'?'.repeat(256)}suffix`)).toBe(true)
+ })
+
+ it('leaves multilingual text, code and separated questions alone', () => {
+ const guard = new RepetitionStop()
+ for (let i = 0; i < 1000; i++) {
+ expect(guard.push('Hello? 你好? Привет? const x = y ?? 0;\n')).toBe(false)
+ }
+ expect(guard.push(' '.repeat(4096))).toBe(false)
+ expect(guard.push('-'.repeat(4096))).toBe(false)
+ })
+})
diff --git a/src/lib/repetition-stop.ts b/src/lib/repetition-stop.ts
new file mode 100644
index 00000000..52b39c25
--- /dev/null
+++ b/src/lib/repetition-stop.ts
@@ -0,0 +1,14 @@
+/** Bound a broken local decoder's question-mark run across stream chunks.
+ * This stops generation; it never rewrites the model's answer or guesses its language.
+ */
+export class RepetitionStop {
+ private run = 0
+
+ push(text: string): boolean {
+ for (const char of text) {
+ this.run = char === '?' ? this.run + 1 : 0
+ if (this.run >= 256) return true
+ }
+ return false
+ }
+}