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(engine): der Start nach dem App-Start gibt nach einem Fehlschlag nicht auf

purple551e78646be3
diff --git a/src/hooks/useModels.ts b/src/hooks/useModels.ts
index 6f9c6609..c34f5999 100644
--- a/src/hooks/useModels.ts
+++ b/src/hooks/useModels.ts
@@ -13,6 +13,7 @@ import {
import { parseNDJSONStream } from '../api/stream'
import { log } from '../lib/logger'
import { cloudModelRow } from '../lib/cloud-model-row'
+import { RESUME_ATTEMPTS, resumeBackoffMs } from '../lib/engine-resume-policy'
import { useModelStore } from '../stores/modelStore'
import { useProviderStore } from '../stores/providerStore'
import { useSettingsStore } from '../stores/settingsStore'
@@ -34,18 +35,39 @@ import type { PullProgress, AIModel, ModelCategory, ImageModel, VideoModel, Clou
// starts a server that reports running:false.
let builtinResumeAttempted = false
+// GH #118: the boot resume used to be a single shot, and a failure was
+// swallowed without a word. The one moment it runs is the worst moment to ask
+// a machine for a GPU: right after login, with the antivirus scanning the
+// fresh install and the graphics driver still settling. A start that loses
+// that race left the user with a dead 127.0.0.1 port and no second attempt
+// until they re-picked the model by hand. Bounded on purpose, because the
+// other failure (a model this box genuinely cannot load) must not turn into an
+// endless restart loop. The policy lives in lib/engine-resume-policy.
+const wait = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
+
async function resumeBuiltinEngines(bundled: BundledModel[]) {
- try {
- const status = await bundledEngineStatus()
- const { activeModel } = useModelStore.getState()
- if (
- !status.running && activeModel &&
- getProviderIdFromModel(activeModel) === 'openai' &&
- bundled.some((m) => prefixModelName('openai', m.name) === activeModel)
- ) {
- await activateBuiltinModel(activeModel)
+ for (let attempt = 0; attempt < RESUME_ATTEMPTS; attempt++) {
+ try {
+ const status = await bundledEngineStatus()
+ if (status.running) break
+ const { activeModel } = useModelStore.getState()
+ if (
+ !activeModel ||
+ getProviderIdFromModel(activeModel) !== 'openai' ||
+ !bundled.some((m) => prefixModelName('openai', m.name) === activeModel)
+ ) {
+ break // nothing to resume, and no number of retries changes that
+ }
+ // False means the GGUF is gone from disk. Retrying cannot conjure it.
+ if (await activateBuiltinModel(activeModel)) break
+ break
+ } catch (err) {
+ log.warn('[useModels] built-in engine resume failed', { attempt: attempt + 1, err })
+ const delay = resumeBackoffMs(attempt)
+ if (delay === null) break
+ await wait(delay)
}
- } catch { /* engine unavailable — non-critical */ }
+ }
await resumeEmbedServer(bundled)
}
diff --git a/src/lib/__tests__/engine-resume-policy.test.ts b/src/lib/__tests__/engine-resume-policy.test.ts
new file mode 100644
index 00000000..2abaac76
--- /dev/null
+++ b/src/lib/__tests__/engine-resume-policy.test.ts
@@ -0,0 +1,37 @@
+/**
+ * GH #118: the boot resume of the built-in engine was one shot and swallowed
+ * its failure. The repair is a retry, and a retry needs a ceiling, because the
+ * failure it retries can also be a model this machine will never load.
+ *
+ * Run: npx vitest run src/lib/__tests__/engine-resume-policy.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { RESUME_ATTEMPTS, resumeBackoffMs } from '../engine-resume-policy'
+
+describe('resumeBackoffMs', () => {
+ it('waits between the tries, and waits longer the second time', () => {
+ const first = resumeBackoffMs(0)
+ const second = resumeBackoffMs(1)
+ expect(first).toBeGreaterThan(0)
+ expect(second).toBeGreaterThan(first!)
+ })
+
+ // Negative control, and the whole point of the ceiling: an engine that will
+ // never come up must not keep the machine busy forever.
+ it('stops after the budget instead of looping', () => {
+ expect(resumeBackoffMs(RESUME_ATTEMPTS - 1)).toBeNull()
+ expect(resumeBackoffMs(RESUME_ATTEMPTS)).toBeNull()
+ expect(resumeBackoffMs(99)).toBeNull()
+ })
+
+ it('refuses nonsense attempt numbers', () => {
+ expect(resumeBackoffMs(-1)).toBeNull()
+ expect(resumeBackoffMs(1.5)).toBeNull()
+ })
+
+ it('gives more than one try, which is what the ticket lacked', () => {
+ expect(RESUME_ATTEMPTS).toBeGreaterThan(1)
+ const delays = Array.from({ length: RESUME_ATTEMPTS }, (_, i) => resumeBackoffMs(i))
+ expect(delays.filter((d) => d !== null)).toHaveLength(RESUME_ATTEMPTS - 1)
+ })
+})
diff --git a/src/lib/engine-resume-policy.ts b/src/lib/engine-resume-policy.ts
new file mode 100644
index 00000000..ae38ca0a
--- /dev/null
+++ b/src/lib/engine-resume-policy.ts
@@ -0,0 +1,27 @@
+/**
+ * How often, and how patiently, the app may try to bring the built-in engine
+ * back up on its own after a start failed.
+ *
+ * GH #118: the boot resume was one shot and swallowed its failure without a
+ * word, so an engine that lost a race at login (antivirus scanning a fresh
+ * install, graphics driver still settling) stayed dead until the user re-picked
+ * the model by hand. The other direction is just as bad: a model this box
+ * genuinely cannot load must not turn into an endless restart loop that keeps
+ * the machine busy forever. So the policy is bounded and lives in one place.
+ */
+
+/** Total start attempts, the first one included. */
+export const RESUME_ATTEMPTS = 3
+
+/** How long to wait after attempt 1, and after attempt 2. */
+export const RESUME_BACKOFF_MS = [1500, 5000]
+
+/**
+ * Milliseconds to wait before the attempt that follows `attempt` (0-based), or
+ * null when the budget is spent and the app has to stop trying.
+ */
+export function resumeBackoffMs(attempt: number): number | null {
+ if (!Number.isInteger(attempt) || attempt < 0) return null
+ if (attempt >= RESUME_ATTEMPTS - 1) return null
+ return RESUME_BACKOFF_MS[attempt] ?? RESUME_BACKOFF_MS[RESUME_BACKOFF_MS.length - 1]
+}