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(group): the engine loads the speaker's model before the speaker's turn
purple27626b34f1eb
diff --git a/src/components/chat/GroupCostHint.tsx b/src/components/chat/GroupCostHint.tsxindex 48209376..ee8dc740 100644--- a/src/components/chat/GroupCostHint.tsx+++ b/src/components/chat/GroupCostHint.tsx@@ -12,11 +12,22 @@import { Users } from 'lucide-react'import { useChatStore } from '../../stores/chatStore'+import { useProviderStore } from '../../stores/providerStore'import { isGroupChat } from '../../lib/group-chat'-/** The line itself. Kept pure so the wording is testable without a renderer. */-export function groupCostHintText(models: number): string {- return `1 round = ${models} answers = ${models}x the cost`+/** The line itself. Kept pure so the wording is testable without a renderer.+ *+ * `builtinSpeakers` is how many of the line-up run on the app's own engine.+ * From two upwards that costs time rather than money: llama-server holds one+ * model, so the engine is stopped and started again between those speakers.+ * The counter-check found the app hiding this by not reloading at all and+ * answering every speaker with the same model, which is the one thing we will+ * not do, so the wait gets named here before the user presses send. */+export function groupCostHintText(models: number, builtinSpeakers = 0): string {+ const base = `1 round = ${models} answers = ${models}x the cost`+ return builtinSpeakers >= 2+ ? `${base}, and the built-in engine reloads between local speakers`+ : base}export function GroupCostHint() {@@ -27,8 +38,18 @@ export function GroupCostHint() {: undefined,)+ // Only the app-managed engine has the one-model-at-a-time problem. A LAN+ // OpenAI-compatible server in the same slot serves many models at once.+ const managedBuiltin = useProviderStore(+ (s) => !!s.providers.openai?.enabled && s.providers.openai?.managed === true,+ )+if (!isGroupChat(groupModels)) return null+ const builtinSpeakers = managedBuiltin+ ? groupModels.filter((m) => m.startsWith('openai::')).length+ : 0+return (<div className="w-full max-w-[70%] mx-auto px-3 pb-1 flex justify-center"><div className="w-full flex items-center gap-1.5 px-2 py-1 rounded-md border border-amber-500/20 bg-amber-500/[0.04]">@@ -38,7 +59,7 @@ export function GroupCostHint() {className="flex-1 min-w-0 truncate text-[0.6rem] text-gray-700 dark:text-gray-300"title={groupModels.join(', ')}>- {groupCostHintText(groupModels.length)}+ {groupCostHintText(groupModels.length, builtinSpeakers)}</span></div></div>diff --git a/src/hooks/__tests__/group-speaker-model-swap.test.ts b/src/hooks/__tests__/group-speaker-model-swap.test.tsnew file mode 100644index 00000000..8ff4cb60--- /dev/null+++ b/src/hooks/__tests__/group-speaker-model-swap.test.ts@@ -0,0 +1,98 @@+/**+ * 2.6.7 counter-check, nebenbefund 1b: a group chat with two local models+ * showed two names and ran one model. llama-server holds a single model and+ * answers with it whatever the request's `model` field says, and the round+ * never reloaded between speakers.+ *+ * The cure is a real reload per speaker, announced before the wait. There is+ * no render harness for these hooks in this repo, so the loop's contract is+ * guarded at the source the way chat-budget-wiring.test.ts guards A4; the+ * functions it calls are unit tested in+ * src/api/__tests__/builtin-model-guard.test.ts.+ *+ * Run: npx vitest run src/hooks/__tests__/group-speaker-model-swap.test.ts+ */++import { describe, it, expect } from 'vitest'+import { readFileSync } from 'fs'+import { resolve } from 'path'+import { groupCostHintText } from '../../components/chat/GroupCostHint'++const read = (...p: string[]) => readFileSync(resolve(__dirname, '..', ...p), 'utf8')+const chat = read('useChat.ts')+const groupTurn = chat.slice(chat.indexOf('async function runGroupTurn'), chat.indexOf('export function useChat'))+const groupRound = chat.slice(chat.indexOf('const runGroupRound = useCallback'), chat.indexOf('const sendMessage'))++describe('every speaker gets its own model loaded', () => {+ it('asks whether the engine has to be reloaded and then reloads it', () => {+ expect(groupTurn).toMatch(/await builtinReloadNeeded\(model\)/)+ expect(groupTurn).toMatch(/await ensureBuiltinEngineAlive\(model\)/)+ })++ it('does the reload INSIDE the turn, so speaker two is not answered by speaker one', () => {+ // The whole bug was one load for the whole round. A reload hoisted up into+ // runGroupRound would put it straight back.+ expect(groupRound).not.toMatch(/builtinReloadNeeded|ensureBuiltinEngineAlive/)+ // The round still does nothing but hand each model its own turn, and the+ // turn is where the model is loaded.+ expect(groupRound).toMatch(/for \(const model of models\)/)+ expect(groupRound).toMatch(/await runGroupTurn\(convId, model, models, abort\)/)+ })++ it('reloads BEFORE the provider call, not after', () => {+ expect(groupTurn.indexOf('ensureBuiltinEngineAlive')).toBeLessThan(groupTurn.indexOf('getProviderForModel(model)'))+ })++ it('says what it is waiting for and clears the line again', () => {+ expect(groupTurn).toMatch(/Loading \$\{toLoad\} into the built-in engine for this turn/)+ // The announcement is written before the wait, and taken back after it, so+ // a turn that answers normally shows no leftover status line.+ const hint = groupTurn.indexOf('Loading ${toLoad}')+ const wait = groupTurn.indexOf('await ensureBuiltinEngineAlive')+ expect(hint).toBeLessThan(wait)+ expect(groupTurn.slice(wait)).toMatch(/updateMessageContent\(convId, assistantMessage\.id, ''\)/)+ })++ it('honours Stop while the engine is loading', () => {+ const wait = groupTurn.indexOf('await ensureBuiltinEngineAlive')+ expect(groupTurn.slice(wait)).toMatch(/if \(abort\.signal\.aborted\) return/)+ })++ // Negative control: a cloud or Ollama speaker has nothing to do with our+ // engine, and swapping it under them would be a bug of its own.+ it('only touches the turn of a speaker in the built-in slot', () => {+ expect(groupTurn).toMatch(/if \(providerId === 'openai'\) \{/)+ })++ // Negative control: this is speaker control, not history. The message+ // sequence and the history transformation belong to group-chat.ts.+ it('does not rewrite the history on its way past', () => {+ const block = groupTurn.slice(+ groupTurn.indexOf("if (providerId === 'openai') {"),+ groupTurn.indexOf('const { provider, modelId }'),+ )+ expect(block).not.toMatch(/messages|groupHistory|groupSystemPrompt/)+ })+})++describe('the composer hint tells the truth about local line-ups', () => {+ it('names the reload when two or more speakers share the engine', () => {+ expect(groupCostHintText(2, 2)).toBe(+ '1 round = 2 answers = 2x the cost, and the built-in engine reloads between local speakers',+ )+ })++ // Negative controls: one local speaker reloads nothing, and a line-up of+ // cloud models is unaffected, so neither may grow the sentence.+ it('stays the plain cost line otherwise', () => {+ expect(groupCostHintText(2, 1)).toBe('1 round = 2 answers = 2x the cost')+ expect(groupCostHintText(3, 0)).toBe('1 round = 3 answers = 3x the cost')+ expect(groupCostHintText(4)).toBe('1 round = 4 answers = 4x the cost')+ })++ it('counts only the app-managed engine, never a LAN server in the same slot', () => {+ const source = readFileSync(resolve(__dirname, '..', '..', 'components', 'chat', 'GroupCostHint.tsx'), 'utf8')+ expect(source).toMatch(/managed === true/)+ expect(source).toMatch(/m\.startsWith\('openai::'\)/)+ })+})diff --git a/src/hooks/useChat.ts b/src/hooks/useChat.tsindex e0bf2017..71b613b3 100644--- a/src/hooks/useChat.ts+++ b/src/hooks/useChat.ts@@ -31,6 +31,7 @@ import { stripNonCanonicalTags, finalStripThinkingTags } from "../lib/thinking-simport { isMultimodalUnsupportedError, MULTIMODAL_UNSUPPORTED_MESSAGE } from "../lib/ollama-errors"import type { ImageAttachment, Message } from "../types/chat"import { isGroupChat, groupSystemPrompt, groupHistory, stripImpersonatedSpeakers } from "../lib/group-chat"+import { builtinReloadNeeded, ensureBuiltinEngineAlive } from "../api/builtin-ensure"import { emptyAnswerExplanation } from "../lib/answer-notes"import { log } from "../lib/logger"import { CREDITS_EXHAUSTED_MESSAGE } from '../lib/credits-exhausted'@@ -118,6 +119,31 @@ async function runGroupTurn(convId: string, model: string, allModels: string[],const others = allModels.filter((m) => m !== model)try {+ // Local speakers share ONE engine process. llama-server holds a single+ // model and answers with it whatever the request's `model` field says, so+ // a group round used to send every speaker's turn to whichever model+ // happened to be loaded: the user saw two names and got one model+ // (counter-check on the Windows box, 2026-08-28). The engine loads this+ // speaker's model before its turn now.+ //+ // Announced first, because a swap stops and restarts llama-server and a+ // large GGUF takes long enough that a silent wait reads as a hang. The+ // line is overwritten by the first token, or by the empty-answer note if+ // no token ever comes.+ if (providerId === 'openai') {+ const toLoad = await builtinReloadNeeded(model)+ if (toLoad) {+ useChatStore.getState().updateMessageContent(+ convId,+ assistantMessage.id,+ `Loading ${toLoad} into the built-in engine for this turn...`,+ )+ await ensureBuiltinEngineAlive(model)+ if (abort.signal.aborted) return+ useChatStore.getState().updateMessageContent(convId, assistantMessage.id, '')+ }+ }+const { provider, modelId } = getProviderForModel(model)let effectiveCtx: number | undefined = settings.contextWindowOverride || undefinedif (providerId === 'ollama') {