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(chat): the picked model survives the restart

purple9834a05cc1da
diff --git a/src/components/layout/AppShell.tsx b/src/components/layout/AppShell.tsx
index d9028089..43526be6 100644
--- a/src/components/layout/AppShell.tsx
+++ b/src/components/layout/AppShell.tsx
@@ -27,7 +27,7 @@ import { idbStorage } from '../../lib/idbStorage'
import { STORE_KEYS, IDB_STORE_KEYS, collectStoreSnapshot } from '../../lib/store-backup'
import { idbKeysToRestore, mayReloadForIdbRestore } from '../../lib/idb-restore'
import { log } from '../../lib/logger'
-import type { AIModel } from '../../types/models'
+import { pickForMode } from '../../lib/active-model-mode'
import { useKeyboardShortcuts } from '../../hooks/useKeyboardShortcuts'
import { useCloudAuth } from '../../hooks/useCloudAuth'
import { useCloudAuthStore, deriveCloudAvailable } from '../../stores/cloudAuthStore'
@@ -104,29 +104,28 @@ export function AppShell() {
// moment those models land.
const allModels = useModelStore((s) => s.models)
useEffect(() => {
+ // Nothing to judge against yet. This guard is the whole of Befund 3 of
+ // the abnahme counter-check (2026-08-29): the picked model did not
+ // survive a restart, and the picker came back on a different model.
+ //
+ // The pick IS persisted, and it was rehydrated correctly. This effect
+ // then ran on mount, before the first model list had landed, found the
+ // active model in an empty array, decided it was out of mode and cleared
+ // it. setModels later saw no active model and auto-selected the first
+ // chat entry, which is how Qwen3 4B turned into Hermes over a restart.
+ // An empty list is not evidence that a model is gone; it is the absence
+ // of evidence, and this effect only ever runs again the moment the real
+ // list arrives.
const { activeModel, setActiveModel } = useModelStore.getState()
- // Chat models only — ComfyUI image/video checkpoints share the list but
- // carry no provider field, so a bare provider check would pin a checkpoint
- // as the active CHAT model (mirrors the pull auto-activate guard in
- // useModels; an unprefixed checkpoint name routes to Ollama and fails).
- const chatCapable = (m: AIModel) => m.type !== 'image' && m.type !== 'video'
- const inMode = (name: string | null) => {
- if (!name) return false
- const m = allModels.find((x) => x.name === name)
- if (!m || !chatCapable(m)) return false
- const isCloud = m.provider === 'lu-cloud'
- return appMode === 'cloud' ? isCloud : !isCloud
- }
- if (inMode(activeModel)) return
- const fallback = allModels.find((m) =>
- chatCapable(m) && (appMode === 'cloud' ? m.provider === 'lu-cloud' : m.provider !== 'lu-cloud'),
- )
- // No in-mode model to fall back to? Then the selection must CLEAR, not
- // silently stay on the old mode's model: a lu-cloud model left active in
- // Local mode kept billing credits after the switch said Local (Discord
- // bug-reports 2026-08-09, helpslowlydying). The send path refuses the
- // mismatch too, this just keeps the header honest.
- if (activeModel !== null || fallback) setActiveModel(fallback ? fallback.name : null)
+ // The rule itself lives in lib/active-model-mode.ts, where it can be
+ // tested. It keeps chat models only (a ComfyUI checkpoint shares this
+ // list and routes to Ollama as a chat model, where every send fails), it
+ // holds the empty list harmless, and it clears rather than leaves an
+ // out-of-mode model active when the new mode has nothing to offer: a
+ // lu-cloud model left active in Local mode kept billing credits after the
+ // switch said Local (Discord 2026-08-09, helpslowlydying).
+ const pick = pickForMode(activeModel, allModels, appMode)
+ if (pick.change) setActiveModel(pick.next)
}, [appMode, allModels])
// Local-hardware views (Models/Benchmark) don't exist in cloud mode — the
diff --git a/src/lib/__tests__/modeGate.test.ts b/src/lib/__tests__/modeGate.test.ts
index 0c5d939e..02f3429a 100644
--- a/src/lib/__tests__/modeGate.test.ts
+++ b/src/lib/__tests__/modeGate.test.ts
@@ -13,6 +13,7 @@ import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { modelOutOfMode } from '../modeGate'
+import { pickForMode } from '../active-model-mode'
const here = dirname(fileURLToPath(import.meta.url))
const read = (rel: string) => readFileSync(resolve(here, rel), 'utf8')
@@ -57,7 +58,14 @@ describe('wiring: the gate sits at the choke points', () => {
})
it('the AppShell reselect clears the selection when the new mode has no model', () => {
+ // The rule moved into lib/active-model-mode.ts on 2026-08-29 so it could
+ // be tested against an empty model list, which is what used to eat the
+ // user's pick across a restart (Befund 3, abnahme counter-check). The
+ // clearing behaviour is unchanged and is asserted here on the rule
+ // itself, not on the shape of the call site.
const src = read('../../components/layout/AppShell.tsx')
- expect(src).toContain('setActiveModel(fallback ? fallback.name : null)')
+ expect(src).toContain('if (pick.change) setActiveModel(pick.next)')
+ const cloudOnly = [{ name: 'lu-cloud::glm-5.3', type: 'text', provider: 'lu-cloud' }]
+ expect(pickForMode('lu-cloud::glm-5.3', cloudOnly, 'local')).toEqual({ change: true, next: null })
})
})
diff --git a/src/lib/__tests__/model-pick-survives-restart.test.ts b/src/lib/__tests__/model-pick-survives-restart.test.ts
new file mode 100644
index 00000000..83677177
--- /dev/null
+++ b/src/lib/__tests__/model-pick-survives-restart.test.ts
@@ -0,0 +1,86 @@
+/**
+ * Befund 3 of the abnahme counter-check on the 2.6.7 Windows build
+ * (2026-08-29, ergebnis-abnahme-durchklick.md).
+ *
+ * Qwen3-4B-Q4_K_M was the active model, the app was restarted the way the
+ * script prescribes, and the picker came back on Hermes-3-Llama-3.2-3B. The
+ * model was one click away, but the user had made that choice already.
+ *
+ * The pick was never the problem. modelStore persists activeModel and zustand
+ * rehydrates it from localStorage before the first render. Two guards then
+ * threw it away between them:
+ *
+ * 1. AppShell's Local/Cloud reselect runs on mount, when the model list is
+ * still an empty array. It looked the active model up in that array,
+ * did not find it, concluded it was out of mode and cleared it.
+ * 2. modelStore.setModels auto-selects the first chat model whenever the
+ * active one is not in the incoming list. With the pick already cleared
+ * by (1), the first entry won, and the first entry is Hermes.
+ *
+ * Neither one is wrong about a real deletion. Both were wrong about an empty
+ * list, which says nothing at all. An empty list is now held harmless in both
+ * places, and the pick is re-judged the moment a real list arrives.
+ *
+ * Run: npx vitest run src/lib/__tests__/model-pick-survives-restart.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { pickForMode } from '../active-model-mode'
+
+const QWEN = 'openai::Qwen3-4B-Q4_K_M'
+const HERMES = 'openai::Hermes-3-Llama-3.2-3B.Q4_K_M'
+const GEMMA = 'openai::mlabonne_gemma-3-4b-it-abliterated-Q4_K_M'
+
+/** The three built-in models the box actually had, in the order the Installed
+ * list showed them. Hermes first, which is why Hermes is what a fallback
+ * lands on. */
+const LOCAL_LIST = [
+ { name: HERMES, type: 'text', provider: 'openai' },
+ { name: QWEN, type: 'text', provider: 'openai' },
+ { name: GEMMA, type: 'text', provider: 'openai' },
+]
+
+describe('the picked model survives a restart', () => {
+ it('THE FIX: the mount-time run against an empty list changes nothing', () => {
+ // This is the exact moment the pick used to die.
+ expect(pickForMode(QWEN, [], 'local')).toEqual({ change: false, next: QWEN })
+ })
+
+ it('the list lands a moment later and the pick is still the pick', () => {
+ expect(pickForMode(QWEN, LOCAL_LIST, 'local')).toEqual({ change: false, next: QWEN })
+ })
+
+ it('NEGATIVE CONTROL: a model that really is gone still hands over to the first one', () => {
+ // The dead-name guard is the reason this rule exists. A picker showing a
+ // model the provider no longer has opens an empty list on click.
+ expect(pickForMode('openai::deleted-model', LOCAL_LIST, 'local'))
+ .toEqual({ change: true, next: HERMES })
+ })
+
+ it('NEGATIVE CONTROL: flipping to Cloud still moves off a local model', () => {
+ const withCloud = [...LOCAL_LIST, { name: 'lu-cloud::glm-5.3', type: 'text', provider: 'lu-cloud' }]
+ expect(pickForMode(QWEN, withCloud, 'cloud')).toEqual({ change: true, next: 'lu-cloud::glm-5.3' })
+ })
+
+ it('NEGATIVE CONTROL: Local mode with nothing but cloud models clears the pick', () => {
+ // A lu-cloud model left active in Local mode kept spending credits after
+ // the switch said Local. That must still clear, not linger.
+ const cloudOnly = [{ name: 'lu-cloud::glm-5.3', type: 'text', provider: 'lu-cloud' }]
+ expect(pickForMode('lu-cloud::glm-5.3', cloudOnly, 'local')).toEqual({ change: true, next: null })
+ })
+
+ it('NEGATIVE CONTROL: a ComfyUI checkpoint never becomes the active chat model', () => {
+ // It shares this list and carries no provider, so a bare provider check
+ // would pin it. It routes to Ollama and every send fails.
+ const mediaOnly = [{ name: 'sd_turbo.safetensors', type: 'image' }]
+ expect(pickForMode(null, mediaOnly, 'local')).toEqual({ change: false, next: null })
+ expect(pickForMode(QWEN, mediaOnly, 'local')).toEqual({ change: true, next: null })
+ })
+
+ it('an empty list does not clear an already empty selection either', () => {
+ expect(pickForMode(null, [], 'local')).toEqual({ change: false, next: null })
+ })
+
+ it('a first launch with models and no pick takes the first in-mode one', () => {
+ expect(pickForMode(null, LOCAL_LIST, 'local')).toEqual({ change: true, next: HERMES })
+ })
+})
diff --git a/src/lib/active-model-mode.ts b/src/lib/active-model-mode.ts
new file mode 100644
index 00000000..b47ab3ce
--- /dev/null
+++ b/src/lib/active-model-mode.ts
@@ -0,0 +1,57 @@
+import type { AppMode } from '../types/settings'
+
+/**
+ * Which chat model may stay selected under the current Local/Cloud switch.
+ *
+ * Lifted out of AppShell so it can be tested, because it is what lost the
+ * user's pick across a restart (Befund 3 of the abnahme counter-check,
+ * 2026-08-29: Qwen3 4B was active before the restart, and the picker came
+ * back on Hermes). The pick was persisted and rehydrated correctly. The rule
+ * then ran on mount against an EMPTY model list, could not find the pick in
+ * it, called it out of mode and cleared it. The store's own auto-select then
+ * put the first chat model in its place.
+ *
+ * An empty list is not evidence that a model is gone. It is the absence of
+ * evidence, and this rule is re-run the moment the real list lands.
+ */
+export interface ModeCandidate {
+ name: string
+ type?: string
+ provider?: string
+}
+
+export interface ModePick {
+ /** Whether the caller has to write anything at all. */
+ change: boolean
+ /** What to write when it does. null clears the selection on purpose: a
+ * lu-cloud model left active in Local mode kept spending credits after the
+ * switch said Local (Discord 2026-08-09, helpslowlydying). */
+ next: string | null
+}
+
+/** ComfyUI image/video checkpoints share the model list but are never a chat
+ * model. An unprefixed checkpoint name routes to Ollama and every send
+ * fails with model-not-found. */
+function chatCapable(m: ModeCandidate): boolean {
+ return m.type !== 'image' && m.type !== 'video'
+}
+
+export function pickForMode(
+ activeModel: string | null,
+ models: ModeCandidate[],
+ appMode: AppMode,
+): ModePick {
+ // Nothing to judge against. THE guard: without it, the mount-time run of
+ // this rule wipes a perfectly good persisted pick.
+ if (models.length === 0) return { change: false, next: activeModel }
+
+ const wanted = (m: ModeCandidate) =>
+ chatCapable(m) && (appMode === 'cloud' ? m.provider === 'lu-cloud' : m.provider !== 'lu-cloud')
+
+ const current = activeModel ? models.find((m) => m.name === activeModel) : undefined
+ if (current && wanted(current)) return { change: false, next: activeModel }
+
+ const fallback = models.find(wanted)
+ if (activeModel === null && !fallback) return { change: false, next: null }
+ return { change: true, next: fallback ? fallback.name : null }
+}
diff --git a/src/stores/__tests__/modelStore-pick-persistence.test.ts b/src/stores/__tests__/modelStore-pick-persistence.test.ts
new file mode 100644
index 00000000..f4dafc3b
--- /dev/null
+++ b/src/stores/__tests__/modelStore-pick-persistence.test.ts
@@ -0,0 +1,89 @@
+/**
+ * Befund 3 of the abnahme counter-check (2026-08-29), store side.
+ *
+ * The second half of what lost the pick across a restart: setModels
+ * auto-selects the first chat model whenever the active one is not in the
+ * incoming list. That is right for a model the user deleted and wrong for an
+ * empty list, and fetchModels writes its result here even when every provider
+ * failed.
+ *
+ * Run: npx vitest run src/stores/__tests__/modelStore-pick-persistence.test.ts
+ */
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { readFileSync } from 'node:fs'
+import { fileURLToPath } from 'node:url'
+import { dirname, resolve } from 'node:path'
+
+vi.mock('@tauri-apps/api/core', () => ({ invoke: vi.fn(async () => undefined) }))
+vi.mock('../../api/backend', () => ({
+ isTauri: vi.fn(() => false),
+ backendCall: vi.fn(async () => undefined),
+}))
+vi.mock('../../api/ollama', () => ({ unloadModel: vi.fn(async () => undefined) }))
+vi.mock('../../api/lmstudio', () => ({ unloadLmStudioModel: vi.fn(async () => undefined) }))
+vi.mock('../../api/engine', () => ({ activateBuiltinModel: vi.fn(async () => undefined) }))
+
+import { useModelStore } from '../modelStore'
+
+const here = dirname(fileURLToPath(import.meta.url))
+const storeSrc = readFileSync(resolve(here, '../modelStore.ts'), 'utf8')
+const shellSrc = readFileSync(resolve(here, '../../components/layout/AppShell.tsx'), 'utf8')
+
+const QWEN = 'openai::Qwen3-4B-Q4_K_M'
+const HERMES = 'openai::Hermes-3-Llama-3.2-3B.Q4_K_M'
+
+const chat = (name: string) => ({
+ name, model: name, size: 0, type: 'text' as const,
+ provider: 'openai' as const, providerName: 'Built-in Engine',
+})
+
+beforeEach(() => {
+ useModelStore.setState({ models: [], activeModel: QWEN })
+})
+
+describe('an empty model list is not a reason to drop the pick', () => {
+ it('THE FIX: setModels([]) leaves the rehydrated pick alone', () => {
+ useModelStore.getState().setModels([])
+ expect(useModelStore.getState().activeModel).toBe(QWEN)
+ })
+
+ it('the real list arrives and the pick is confirmed, not replaced', () => {
+ useModelStore.getState().setModels([chat(HERMES), chat(QWEN)])
+ expect(useModelStore.getState().activeModel).toBe(QWEN)
+ })
+
+ it('NEGATIVE CONTROL: a pick that is genuinely gone still hands over', () => {
+ // The dead-name guard has to keep working, or the picker shows a model
+ // the provider no longer has and clicking it opens an empty list.
+ useModelStore.setState({ activeModel: 'openai::deleted-model' })
+ useModelStore.getState().setModels([chat(HERMES), chat(QWEN)])
+ expect(useModelStore.getState().activeModel).toBe(HERMES)
+ })
+
+ it('NEGATIVE CONTROL: an empty list does not invent a pick out of nothing', () => {
+ useModelStore.setState({ activeModel: null })
+ useModelStore.getState().setModels([])
+ expect(useModelStore.getState().activeModel).toBeNull()
+ })
+
+ it('NEGATIVE CONTROL: a list of nothing but ComfyUI files selects no chat model', () => {
+ useModelStore.setState({ activeModel: null })
+ useModelStore.getState().setModels([
+ { name: 'sd_turbo.safetensors', model: 'sd_turbo.safetensors', size: 0, type: 'image', format: 'safetensors', architecture: 'sdxl', providerName: 'ComfyUI' },
+ ])
+ expect(useModelStore.getState().activeModel).toBeNull()
+ })
+})
+
+describe('the pick is written to disk in the first place', () => {
+ it('activeModel is part of what persist keeps', () => {
+ expect(storeSrc).toMatch(/partialize:.*activeModel: state\.activeModel/)
+ })
+
+ it('the mode reselect no longer decides anything by hand', () => {
+ // It asks pickForMode, which holds the empty list harmless. The old
+ // inline version is what cleared the pick on mount.
+ expect(shellSrc).toContain('pickForMode(activeModel, allModels, appMode)')
+ expect(shellSrc).not.toMatch(/const inMode = \(name: string \| null\)/)
+ })
+})
diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.ts
index 6969ca79..62f70f49 100644
--- a/src/stores/modelStore.ts
+++ b/src/stores/modelStore.ts
@@ -67,7 +67,15 @@ export const useModelStore = create<ModelState>()(
// list. Falls back to the first available model, mirroring the
// first-launch behavior so a user is never stuck with no
// selection while a model exists.
- const stillValid = !!state.activeModel && models.some((m) => m.name === state.activeModel)
+ // An empty list validates nothing. fetchModels writes its result
+ // here even when every provider failed, and dropping the pick on
+ // that answer is how a transient failure turned into a silently
+ // different model in the picker (Befund 3, abnahme counter-check
+ // 2026-08-29). The pick has its own guard on the way in and its
+ // own moment to be re-checked: the next non-empty list.
+ const stillValid =
+ !!state.activeModel &&
+ (models.length === 0 || models.some((m) => m.name === state.activeModel))
// Chat models only for the auto-select — ComfyUI image/video
// checkpoints share this list and must never become the active CHAT
// model (an unprefixed checkpoint name routes to Ollama and every