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): stop built-in engine when switching to another local model

purplecc1d18928635
diff --git a/src/stores/__tests__/modelStore-builtin-vram.test.ts b/src/stores/__tests__/modelStore-builtin-vram.test.ts
new file mode 100644
index 00000000..bad88ca4
--- /dev/null
+++ b/src/stores/__tests__/modelStore-builtin-vram.test.ts
@@ -0,0 +1,68 @@
+import { describe, it, expect, beforeEach, vi } from 'vitest'
+
+// The built-in engine (2.5.7) holds its GGUF in VRAM with -ngl 999. Switching
+// away from a built-in model to any OTHER local backend must stop the sidecar,
+// or two models sit in VRAM at once. These tests lock that mutual-exclusion.
+
+const backendCall = vi.fn(async () => ({}))
+const unloadModel = vi.fn(async () => undefined)
+const unloadLmStudioModel = vi.fn(async () => undefined)
+
+vi.mock('../../api/backend', () => ({
+ backendCall: (...a: unknown[]) => backendCall(...a),
+ isTauri: () => true,
+}))
+vi.mock('../../api/ollama', () => ({
+ unloadModel: (...a: unknown[]) => unloadModel(...a),
+}))
+vi.mock('../../api/lmstudio', () => ({
+ unloadLmStudioModel: (...a: unknown[]) => unloadLmStudioModel(...a),
+}))
+vi.mock('../../lib/hf-to-provider', () => ({
+ isLmStudioProvider: (n?: string) => (n || '').includes('LM Studio'),
+}))
+
+import { useModelStore } from '../modelStore'
+import type { AIModel } from '../../types/models'
+
+const builtin = (name: string): AIModel =>
+ ({ name: `openai::${name}`, model: name, size: 1, type: 'text', provider: 'openai', providerName: 'Built-in Engine' } as unknown as AIModel)
+const ollama = (name: string): AIModel =>
+ ({ name, model: name, size: 1, type: 'text' } as unknown as AIModel)
+const lms = (name: string): AIModel =>
+ ({ name: `openai::${name}`, model: name, size: 1, type: 'text', provider: 'openai', providerName: 'LM Studio' } as unknown as AIModel)
+
+describe('modelStore.setActiveModel — built-in engine VRAM exclusion (2.5.7)', () => {
+ beforeEach(() => {
+ backendCall.mockClear(); unloadModel.mockClear(); unloadLmStudioModel.mockClear()
+ useModelStore.setState({
+ models: [builtin('qwenA'), builtin('qwenB'), ollama('llama3'), lms('mistral')],
+ activeModel: null,
+ })
+ })
+
+ it('stops the built-in engine when switching built-in → Ollama', () => {
+ useModelStore.getState().setActiveModel('openai::qwenA') // prev null → no-op
+ useModelStore.getState().setActiveModel('llama3')
+ expect(backendCall).toHaveBeenCalledWith('stop_bundled_engine')
+ })
+
+ it('stops the built-in engine when switching built-in → LM Studio', () => {
+ useModelStore.getState().setActiveModel('openai::qwenA')
+ useModelStore.getState().setActiveModel('openai::mistral')
+ expect(backendCall).toHaveBeenCalledWith('stop_bundled_engine')
+ })
+
+ it('does NOT stop the engine on built-in → built-in (that is an in-place swap)', () => {
+ useModelStore.getState().setActiveModel('openai::qwenA')
+ useModelStore.getState().setActiveModel('openai::qwenB')
+ expect(backendCall).not.toHaveBeenCalledWith('stop_bundled_engine')
+ })
+
+ it('still unloads the previous Ollama model on Ollama → built-in (no regression)', () => {
+ useModelStore.getState().setActiveModel('llama3')
+ useModelStore.getState().setActiveModel('openai::qwenA')
+ expect(unloadModel).toHaveBeenCalledWith('llama3')
+ expect(backendCall).not.toHaveBeenCalledWith('stop_bundled_engine')
+ })
+})
diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.ts
index ad3d0b16..52e109be 100644
--- a/src/stores/modelStore.ts
+++ b/src/stores/modelStore.ts
@@ -4,7 +4,7 @@ import type { AIModel, PullProgress, ModelCategory } from '../types/models'
import { unloadModel } from '../api/ollama'
import { unloadLmStudioModel } from '../api/lmstudio'
import { isLmStudioProvider } from '../lib/hf-to-provider'
-import { isTauri } from '../api/backend'
+import { isTauri, backendCall } from '../api/backend'
import { log } from '../lib/logger'
export interface PullState {
@@ -82,11 +82,31 @@ export const useModelStore = create<ModelState>()(
const prevIsLms = isLmStudioProvider(
(prevModel && 'providerName' in prevModel && prevModel.providerName) as string | undefined,
)
+ // The built-in engine (llama.cpp sidecar) occupies the `openai::` slot
+ // with providerName 'Built-in Engine' and holds its GGUF in VRAM with
+ // -ngl 999. It is NOT caught by the LM-Studio or the bare-Ollama branch
+ // below, so before 2.5.7 wired this in, switching away from a built-in
+ // model to an Ollama/LM-Studio model left the sidecar resident → two
+ // models in VRAM at once (the exact case this guard exists to prevent).
+ const prevIsBuiltin =
+ !!prevModel && 'providerName' in prevModel && prevModel.providerName === 'Built-in Engine'
if (prevIsLms) {
const bareKey = prev.replace(/^[^:]+::/, '') // strip LU's routing prefix
unloadLmStudioModel(bareKey).catch((e) =>
log.warn('[modelStore] failed to unload previous LM Studio model', { model: prev, err: e }),
)
+ } else if (prevIsBuiltin) {
+ // built-in → built-in is a swap (activateBuiltinModel handles it on
+ // the same port), so only stop the engine when the NEXT model is not
+ // itself a built-in GGUF.
+ const nextModel = get().models.find((m) => m.name === name)
+ const nextIsBuiltin =
+ !!nextModel && 'providerName' in nextModel && nextModel.providerName === 'Built-in Engine'
+ if (!nextIsBuiltin) {
+ backendCall('stop_bundled_engine').catch((e) =>
+ log.warn('[modelStore] failed to stop built-in engine on switch-away', { err: e }),
+ )
+ }
} else if (!prev.includes('::')) {
unloadModel(prev).catch((e) =>
log.warn('[modelStore] failed to unload previous model', { model: prev, err: e }),