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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(engine): defer the second, unguarded LU Engine stop until the local lane is free
purple6ed68e35f228
diff --git a/src/stores/__tests__/modelStore-defers-engine-stop-during-local-run.test.ts b/src/stores/__tests__/modelStore-defers-engine-stop-during-local-run.test.tsnew file mode 100644index 00000000..933ab5a3--- /dev/null+++ b/src/stores/__tests__/modelStore-defers-engine-stop-during-local-run.test.ts@@ -0,0 +1,89 @@+import { describe, it, expect, beforeEach, vi } from 'vitest'++/**+ * B6 (lu-301/bau/klaerung-n7.md), zweiter unbewachter Weg: `setActiveModel`+ * rief `stop_bundled_engine` bisher UNBEDINGT, sobald das vorige Modell der+ * eingebaute Motor war und das neue keiner ist. `AppShell.tsx` ruft genau+ * diesen Wechsel bei JEDEM Cloud-Umschalten automatisch auf+ * (`setActiveModel(pick.next)`), auch waehrend eine ANDERE Unterhaltung noch+ * lokal generiert. Der schon bestehende Schutz `offloadWhenLocalLaneFree`+ * (`lib/cloud-offload-defer.ts`) bewachte bis jetzt nur AppShells eigenen+ * `offload_local_models`-Aufruf, nicht diesen hier.+ *+ * Dieser Test faehrt den ECHTEN `modelStore` gegen den ECHTEN+ * `generationStore`, nur `backendCall` ist gemockt (kein Tauri-Invoke).+ *+ * Lauf: npx vitest run src/stores/__tests__/modelStore-defers-engine-stop-during-local-run.test.ts+ */++const backendCall = vi.fn(async (..._args: unknown[]) => ({}))+const unloadModel = vi.fn(async (..._args: unknown[]) => undefined)+const unloadLmStudioModel = vi.fn(async (..._args: unknown[]) => 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 { useGenerationStore } from '../generationStore'+import type { AIModel } from '../../types/models'++const builtin = (name: string): AIModel =>+ ({ name: `openai::${name}`, model: name, size: 1, type: 'text', provider: 'openai', providerName: 'LU Engine' } as unknown as AIModel)+const cloudModel = (name: string): AIModel =>+ ({ name, model: name, size: 1, type: 'text', provider: 'anthropic', providerName: 'Anthropic' } as unknown as AIModel)++const CONV_ID = 'conv-with-a-running-local-turn'+const RUN_TOKEN = {}++describe('modelStore.setActiveModel defers stop_bundled_engine while a local run is booked', () => {+ beforeEach(() => {+ backendCall.mockClear()+ useModelStore.setState({+ models: [builtin('qwenA'), cloudModel('claude-x')],+ activeModel: 'openai::qwenA',+ })+ useGenerationStore.setState({ runs: {}, generating: {}, aborters: {} })+ })++ it('does NOT stop the engine while a local run is booked, and fires exactly once after it ends', () => {+ useGenerationStore.getState().bookRun(CONV_ID, 'local', RUN_TOKEN)++ // The mode-switch reselect AppShell.tsx does on entering Cloud.+ useModelStore.getState().setActiveModel('claude-x')++ expect(backendCall).not.toHaveBeenCalledWith('stop_bundled_engine')++ useGenerationStore.getState().endRun(CONV_ID, RUN_TOKEN)++ expect(backendCall).toHaveBeenCalledWith('stop_bundled_engine')+ expect(backendCall.mock.calls.filter((c) => c[0] === 'stop_bundled_engine')).toHaveLength(1)+ })++ it('Gegenprobe: without an active local run, the stop fires immediately (unchanged behaviour)', () => {+ useModelStore.getState().setActiveModel('claude-x')+ expect(backendCall).toHaveBeenCalledWith('stop_bundled_engine')+ })++ it('Gegenprobe: switching back to Local (a built-in model) before the run ends means NO stop ever fires', () => {+ useGenerationStore.getState().bookRun(CONV_ID, 'local', RUN_TOKEN)++ useModelStore.getState().setActiveModel('claude-x') // defers+ useModelStore.getState().setActiveModel('openai::qwenA') // user flips back to Local++ backendCall.mockClear()+ useGenerationStore.getState().endRun(CONV_ID, RUN_TOKEN)++ expect(backendCall).not.toHaveBeenCalledWith('stop_bundled_engine')+ })+})diff --git a/src/stores/modelStore.ts b/src/stores/modelStore.tsindex 7e32e10d..be48707e 100644--- a/src/stores/modelStore.ts+++ b/src/stores/modelStore.ts@@ -9,8 +9,10 @@ import { activateBuiltinModel } from '../api/engine'import { isLmStudioProvider } from '../lib/hf-to-provider'import { isTauri, backendCall } from '../api/backend'import { useChatStore } from './chatStore'+import { useGenerationStore } from './generationStore'import { log } from '../lib/logger'import { isLuEngineName } from '../lib/engine-name'+import { offloadWhenLocalLaneFree } from '../lib/cloud-offload-defer'// Which provider slot a model name routes to. There is exactly one answer to// that question in this app and it lives in api/providers/registry, the same// function getProviderForModel uses to pick the client that actually sends the@@ -277,9 +279,34 @@ export const useModelStore = create<ModelState>()(const nextIsBuiltin =!!nextModel && 'providerName' in nextModel && isLuEngineName(nextModel.providerName)if (!nextIsBuiltin) {- backendCall('stop_bundled_engine').catch((e) =>- log.warn('[modelStore] failed to stop the LU Engine on switch-away', { err: e }),- )+ // B6 (lu-301/bau/klaerung-n7.md): this reselect is the SECOND,+ // unguarded path that stops the LU Engine. AppShell's cloud-mode+ // effect calls setActiveModel(pick.next) on every mode flip+ // BEFORE its own offload effect runs, and this stop used to fire+ // unconditionally, killing llama-server mid-stream for whatever+ // OTHER conversation was still generating locally. Same guard as+ // AppShell's offload: defer until the local lane is free.+ //+ // A re-check at fire time, not just a deferred call: by the time+ // the local lane frees up, the user may have switched back to+ // Local or picked another built-in model themselves (this same+ // function runs again for that), so the stop must look at the+ // CURRENT active model, not the one that was picked when this+ // closure was created.+ offloadWhenLocalLaneFree(useGenerationStore, () => {+ const stillActiveModel = get().activeModel+ const stillActiveEntry = stillActiveModel+ ? get().models.find((m) => m.name === stillActiveModel)+ : undefined+ const stillNonBuiltin =+ !stillActiveEntry ||+ !('providerName' in stillActiveEntry) ||+ !isLuEngineName(stillActiveEntry.providerName)+ if (!stillNonBuiltin) return+ backendCall('stop_bundled_engine').catch((e) =>+ log.warn('[modelStore] failed to stop the LU Engine on switch-away', { err: e }),+ )+ })} else if (name) {// built-in → DIFFERENT built-in: llama-server serves exactly ONE// gguf and ignores the request's model field, and the send-path