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(models): die Zeile, die den Chat gerade bedient, ueberlebt ihren Standby-Zwilling

purplec907b4f2b1ee
diff --git a/src/hooks/__tests__/the-serving-row-outranks-its-standby-twin.test.ts b/src/hooks/__tests__/the-serving-row-outranks-its-standby-twin.test.ts
new file mode 100644
index 00000000..ef498b24
--- /dev/null
+++ b/src/hooks/__tests__/the-serving-row-outranks-its-standby-twin.test.ts
@@ -0,0 +1,144 @@
+/**
+ * @vitest-environment jsdom
+ *
+ * A16 counter-check 02.09., the regression the standby listing brought with
+ * it: the rows LM Studio reports while it waits beside the slot were pushed
+ * into the same array that is then handed to `dropDuplicateLuEngineRows` as
+ * "already listed". So a GGUF in the LU Engine folder was measured against a
+ * backend that is not serving anything, and lost. The measured pair was
+ * bundled `Qwen2.5-0.5B-Instruct-Q4_K_M` against standby
+ * `qwen2.5-0.5b-instruct@q4_k_m`, and it left zero rows for that model.
+ *
+ * What stayed on screen was the worse of the two. The LU Engine was answering
+ * the chat; the standby row's click hands the slot back and stops the engine,
+ * which is the opposite of what someone clicking the model he is already
+ * talking to expects.
+ *
+ * The rule in lib/lu-engine-rows has always been "precedence to the provider
+ * that holds the slot". This pins it in the direction the standby listing
+ * introduced.
+ *
+ * Run: npx vitest run src/hooks/__tests__/the-serving-row-outranks-its-standby-twin.test.ts
+ */
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { renderHook, act } from '@testing-library/react'
+
+const listStandbyBackendModels = vi.fn()
+const listBundledModels = vi.fn()
+
+vi.mock('../../api/backend', () => ({
+ isTauri: () => true, isMacOS: () => false, isWindows: () => true, isLinux: () => false,
+ backendCall: vi.fn(async () => null),
+ secretGet: vi.fn().mockRejectedValue(new Error('no vault')),
+ secretSet: vi.fn(), secretDelete: vi.fn(),
+}))
+vi.mock('../../api/comfyui', () => ({
+ getInstalledImageModels: vi.fn(async () => []),
+ getInstalledVideoModels: vi.fn(async () => []),
+ checkComfyConnection: vi.fn(async () => false),
+ readModelDiskSizes: vi.fn(async () => new Map()),
+}))
+vi.mock('../../api/ollama', () => ({
+ listModels: vi.fn(async () => []), unloadModel: vi.fn(async () => undefined),
+ pullModel: vi.fn(), pullModelTauri: vi.fn(), deleteModel: vi.fn(), showModel: vi.fn(),
+}))
+vi.mock('../../api/providers', async () => {
+ const actual = await vi.importActual<typeof import('../../api/providers')>('../../api/providers')
+ return { ...actual, getEnabledProviders: () => [] }
+})
+vi.mock('../../api/engine', async () => {
+ const actual = await vi.importActual<typeof import('../../api/engine')>('../../api/engine')
+ return {
+ ...actual,
+ listBundledModels: (...a: unknown[]) => listBundledModels(...(a as [])),
+ isManagedBuiltinActive: () => true,
+ bundledEngineStatus: vi.fn(async () => ({ running: true, healthy: true, port: 8127 })),
+ bundledEmbedStatus: vi.fn(async () => ({ running: true, healthy: true, port: 8128 })),
+ startBundledEmbed: vi.fn(),
+ activateBuiltinModel: vi.fn(async () => true),
+ resumeBuiltinEngines: vi.fn(async () => undefined),
+ resumeEmbedServer: vi.fn(async () => undefined),
+ }
+})
+vi.mock('../../api/lu-engine-switch', async () => {
+ const actual = await vi.importActual<typeof import('../../api/lu-engine-switch')>('../../api/lu-engine-switch')
+ return { ...actual, listStandbyBackendModels: (...a: unknown[]) => listStandbyBackendModels(...(a as [])) }
+})
+
+const { useModels } = await import('../useModels')
+const { useModelStore } = await import('../../stores/modelStore')
+const { useProviderStore } = await import('../../stores/providerStore')
+
+/** Exactly the pair the counter-check measured. */
+const LU_FILE = 'Qwen2.5-0.5B-Instruct-Q4_K_M'
+const LU_ROW = `openai::${LU_FILE}`
+const STANDBY_TWIN = 'openai::qwen2.5-0.5b-instruct@q4_k_m'
+/** A second LM Studio model with no counterpart on disk. It has to survive. */
+const STANDBY_OTHER = 'openai::phi-4-mini-instruct@q8_0'
+
+function rows() {
+ return useModelStore.getState().models as unknown as { name: string; providerName?: string }[]
+}
+
+beforeEach(() => {
+ listBundledModels.mockReset()
+ listBundledModels.mockResolvedValue([
+ { name: LU_FILE, path: 'C:\\lu-e2e-models\\Qwen2.5-0.5B-Instruct-Q4_K_M.gguf', size: 400_000_000, loaded: true },
+ ])
+ listStandbyBackendModels.mockReset()
+ listStandbyBackendModels.mockResolvedValue([
+ { id: 'qwen2.5-0.5b-instruct@q4_k_m', name: 'qwen2.5-0.5b-instruct@q4_k_m', provider: 'openai', providerName: 'LM Studio' },
+ { id: 'phi-4-mini-instruct@q8_0', name: 'phi-4-mini-instruct@q8_0', provider: 'openai', providerName: 'LM Studio' },
+ ])
+ useProviderStore.getState().resetProvidersToDefaults()
+ // The state the counter-check was in: our engine in the slot, LM Studio
+ // pushed onto the standby card with its server still up on 1234.
+ useProviderStore.getState().setProviderConfig('openai', {
+ enabled: true, managed: true, name: 'LU Engine', baseUrl: 'http://127.0.0.1:8127/v1',
+ isLocal: true,
+ displaced: { name: 'LM Studio', baseUrl: 'http://localhost:1234/v1', isLocal: true },
+ })
+ useModelStore.setState({ models: [] as never, activeModel: LU_ROW })
+})
+afterEach(() => { vi.clearAllMocks() })
+
+describe('one GGUF known to the LU Engine and to the backend on standby', () => {
+ it('keeps the row that is serving the chat and drops the standby twin', async () => {
+ const { result } = renderHook(() => useModels())
+ await act(async () => { await result.current.fetchModels() })
+
+ const lu = rows().find((m) => m.name === LU_ROW)
+ expect(lu, 'the row that is actually answering was dropped').toBeTruthy()
+ expect(lu?.providerName).toBe('LU Engine')
+ expect(
+ rows().some((m) => m.name === STANDBY_TWIN),
+ 'the standby twin stayed, so the model stands twice',
+ ).toBe(false)
+ // And the model is not gone altogether, which is what the counter-check
+ // measured: one file, two sources, zero rows.
+ expect(rows().filter((m) => m.name === LU_ROW || m.name === STANDBY_TWIN)).toHaveLength(1)
+ })
+
+ // NEGATIVE CONTROL: the de-duplication is about ONE file, not about the
+ // standby list as a whole. A model only LM Studio has must still be listed,
+ // or the way back to it is gone again.
+ it('leaves the standby models that have no counterpart on disk', async () => {
+ const { result } = renderHook(() => useModels())
+ await act(async () => { await result.current.fetchModels() })
+
+ const other = rows().find((m) => m.name === STANDBY_OTHER)
+ expect(other, 'LM Studio lost a model it alone has').toBeTruthy()
+ expect(other?.providerName).toBe('LM Studio')
+ })
+
+ // NEGATIVE CONTROL: with nothing in the LU Engine folder the standby list
+ // arrives untouched, exactly as before this fix.
+ it('touches nothing when the LU Engine folder is empty', async () => {
+ listBundledModels.mockResolvedValue([])
+ const { result } = renderHook(() => useModels())
+ await act(async () => { await result.current.fetchModels() })
+
+ expect(rows().some((m) => m.name === STANDBY_TWIN)).toBe(true)
+ expect(rows().some((m) => m.name === STANDBY_OTHER)).toBe(true)
+ })
+})
diff --git a/src/hooks/useModels.ts b/src/hooks/useModels.ts
index dc58df33..158d1aa3 100644
--- a/src/hooks/useModels.ts
+++ b/src/hooks/useModels.ts
@@ -16,7 +16,7 @@ import { cloudModelRow } from '../lib/cloud-model-row'
import { runEngineResume } from '../lib/engine-resume-policy'
import { engineStartIsWorthRetrying } from '../lib/engine-start-failure'
import { commandIsUnavailable } from '../lib/engine-command-availability'
-import { dropDuplicateLuEngineRows } from '../lib/lu-engine-rows'
+import { dropDuplicateLuEngineRows, dropStandbyRowsServedByLuEngine } from '../lib/lu-engine-rows'
import { isBuiltinEngineEntry, type InstalledModelLike } from '../lib/lmstudio-match'
import {
ensureLuEngineIsChatProvider, LU_ENGINE_SWITCH_NOTE, LU_ENGINE_FILE_GONE,
@@ -254,13 +254,23 @@ export function useModels() {
//
// Only reached when a local backend really was displaced, so on the
// ordinary machine with nothing on standby this costs no request at all.
+ //
+ // Fetched here and appended further down, AFTER the LU Engine rows.
+ // A16 counter-check 02.09.: pushed straight into `allModels`, these rows
+ // counted as "already listed" when the LU Engine rows were de-duplicated
+ // against them, so the row that is serving the chat was dropped in
+ // favour of the row that is only waiting beside the slot. Precedence
+ // belongs to whoever holds the slot (lib/lu-engine-rows), and while our
+ // engine holds it that is us, so the standby list is the side that gives
+ // way (`dropStandbyRowsServedByLuEngine`).
+ let standbyRows: CloudModel[] = []
const standby = managedBuiltin ? standbyChatBackend() : null
if (standby) {
try {
const waiting = await listStandbyBackendModels(standby)
- allModels.push(...waiting
+ standbyRows = waiting
.map((pm) => cloudModelRow(pm) satisfies CloudModel)
- .filter((m) => !isEmbeddingModel(m.name)))
+ .filter((m) => !isEmbeddingModel(m.name))
} catch {
// Its server went away in the meantime. No rows, exactly as before.
}
@@ -336,6 +346,12 @@ export function useModels() {
else void resumeEmbedServer(bundledRaw)
}
}
+ // The standby backend's rows, minus the ones our engine is already
+ // serving from the same file. Appended after the LU Engine rows on
+ // purpose: see the note where `standbyRows` is filled.
+ if (standbyRows.length > 0) {
+ allModels.push(...dropStandbyRowsServedByLuEngine(standbyRows, allModels))
+ }
const ollamaEnabled = useProviderStore.getState().providers.ollama.enabled
const hasOllamaModels = allModels.some(m => m.provider === 'ollama')
if (ollamaEnabled && !hasOllamaModels) {
diff --git a/src/lib/lu-engine-rows.ts b/src/lib/lu-engine-rows.ts
index 1f2924a0..8f4a83d6 100644
--- a/src/lib/lu-engine-rows.ts
+++ b/src/lib/lu-engine-rows.ts
@@ -100,6 +100,45 @@ export function dropDuplicateLuEngineRows<T extends InstalledModelLike>(
return bundled.filter((row) => !lmStudio.some((other) => sameFile(row, other, ownByIdentity)))
}
+/**
+ * The other direction: standby rows the LU Engine is already serving, removed.
+ *
+ * A16 counter-check 02.09.: the standby listing (`listStandbyBackendModels`)
+ * pushed LM Studio's rows into the same array that is then handed to
+ * `dropDuplicateLuEngineRows` as "already listed". So the row that IS serving
+ * the chat was measured against a row that is merely waiting beside the slot,
+ * and lost: bundled `Qwen2.5-0.5B-Instruct-Q4_K_M` against standby
+ * `qwen2.5-0.5b-instruct@q4_k_m` left zero rows. What stayed on screen was the
+ * standby row, whose click hands the slot away and stops the engine that was
+ * answering a second ago.
+ *
+ * The rule at the top of this file is unchanged and this is it, applied the
+ * right way round: precedence goes to the provider that holds the slot. While
+ * the LU Engine holds it, its row is the one that works right now, so the
+ * standby twin is the one that goes. Same identity logic, same three routes,
+ * only the roles swapped, which is why it reads the same `sameFile` and not a
+ * second copy of it.
+ *
+ * `alreadyListed` is the whole list; the LU Engine rows are picked out of it
+ * here, exactly as `dropDuplicateLuEngineRows` picks out the LM Studio ones.
+ */
+export function dropStandbyRowsServedByLuEngine<T extends InstalledModelLike>(
+ standby: T[],
+ alreadyListed: InstalledModelLike[],
+): T[] {
+ if (standby.length === 0) return standby
+ const luEngine = alreadyListed.filter(isBuiltinEngineEntry)
+ if (luEngine.length === 0) return standby
+ // How many of OUR files share each identity, counted over the LU Engine rows
+ // because those are "ours" in this direction. Route 3 only applies at one.
+ const ownByIdentity = new Map<string, number>()
+ for (const row of luEngine) {
+ const id = identityOf(row)
+ if (id) ownByIdentity.set(id, (ownByIdentity.get(id) ?? 0) + 1)
+ }
+ return standby.filter((row) => !luEngine.some((ours) => sameFile(ours, row, ownByIdentity)))
+}
+
/** Every id a row can be recognised by. LM Studio reports its own key, our own
* rows carry the file stem, and ours also carry the path. */
function idsOf(m: InstalledModelLike): string[] {