Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: a chat remembers the model it last used, not only the one it started on (#14259)

Daniel Han87c640845541
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 71dda68782a..f189ffd408b 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -153,7 +153,7 @@ import {
} from "../utils/deep-research-handoff";
import {
consumeQueuedChatRunSettings,
- shouldPersistResolvedQueuedModel,
+ shouldPersistRunModel,
snapshotQueuedChatRunSettings,
} from "../utils/queued-chat-run-settings";
import {
@@ -4634,26 +4634,25 @@ export function createOpenAIStreamAdapter(
releasePreStreamRunForThreadIds([unstable_threadId, resolvedThreadId]);
const queuedRunSettings = consumeQueuedChatRunSettings(resolvedThreadId);
let queuedEmptyModelRuntime: QueuedResolvedModelRuntime | null = null;
- const persistResolvedQueuedModel = async (
+ const persistRunModel = async (
modelId: string,
modelGgufVariant: string | null,
) => {
- if (
- !queuedRunSettings ||
- queuedRunSettings.params.checkpoint ||
- !resolvedThreadId ||
- !modelId
- ) {
+ if (!resolvedThreadId || !modelId) {
return;
}
- const storedThread = await readThreadRecord?.();
- if (!shouldPersistResolvedQueuedModel(storedThread)) {
- return;
+ try {
+ const storedThread = await readThreadRecord?.();
+ if (!shouldPersistRunModel(storedThread, modelId, modelGgufVariant)) {
+ return;
+ }
+ await updateStoredChatThread(resolvedThreadId, {
+ modelId,
+ modelGgufVariant,
+ });
+ } catch {
+ // Only what the chat offers back on reopen.
}
- await updateStoredChatThread(resolvedThreadId, {
- modelId,
- modelGgufVariant,
- });
};
if (queuedRunSettings) {
runtime = { ...runtime, ...queuedRunSettings };
@@ -4769,10 +4768,6 @@ export function createOpenAIStreamAdapter(
const userMessageParentId =
userMessageIndex > 0 ? rawMessages[userMessageIndex - 1]!.id : null;
const { params } = runtime;
- await persistResolvedQueuedModel(
- params.checkpoint,
- runtime.activeGgufVariant,
- );
const selectedCheckpoint = params.checkpoint.trim();
const researchExternalSelection =
parseExternalModelId(selectedCheckpoint);
@@ -4883,6 +4878,7 @@ export function createOpenAIStreamAdapter(
};
runtime.registerThreadServerCancel(threadKey, researchServerCancel);
releaseCurrentPreStreamRun();
+ void persistRunModel(params.checkpoint, runtime.activeGgufVariant);
runtime.setThreadRunning(threadKey, true, {
owner: researchServerCancel,
});
@@ -5155,10 +5151,6 @@ export function createOpenAIStreamAdapter(
}
: liveRuntime;
const { params } = runtime;
- await persistResolvedQueuedModel(
- params.checkpoint,
- runtime.activeGgufVariant,
- );
const sandboxSessionId = await resolveSandboxSessionId(
resolvedThreadId,
readThreadRecord,
@@ -5806,6 +5798,7 @@ export function createOpenAIStreamAdapter(
const audioCancel = () => runAbort.abort();
runtime.registerThreadServerCancel(threadKey, audioCancel);
releaseCurrentPreStreamRun();
+ void persistRunModel(params.checkpoint, runtime.activeGgufVariant);
runtime.setThreadRunning(threadKey, true, { owner: audioCancel });
try {
yield {
@@ -5893,6 +5886,8 @@ export function createOpenAIStreamAdapter(
// Flagged local/external so the model-swap gate counts only the chats a reload ends; the
// backend excludes external runs from active_generations for the same reason.
releaseCurrentPreStreamRun();
+ // Past every gate that refuses the send, and not awaited: only what reopening the chat offers.
+ void persistRunModel(params.checkpoint, runtime.activeGgufVariant);
runtime.setThreadRunning(threadKey, true, {
local: !isExternalRequest,
owner: serverCancel,
diff --git a/studio/frontend/src/features/chat/components/chat-model-notice.tsx b/studio/frontend/src/features/chat/components/chat-model-notice.tsx
index ac319ecc623..4c5925f6e6f 100644
--- a/studio/frontend/src/features/chat/components/chat-model-notice.tsx
+++ b/studio/frontend/src/features/chat/components/chat-model-notice.tsx
@@ -31,7 +31,7 @@ import {
createChatModelHistoryReader,
} from "./chat-model-notice-switch";
-/** reads the saved chat's starting model and waits for queued initialization. */
+/** reads the saved chat's last used model and waits for queued initialization. */
export function useChatCreatedModel(
threadId: string | undefined,
): ChatModelSwitchTarget | null {
@@ -78,7 +78,7 @@ type ChatModelNoticeProps = {
onSwitch: (target: ChatModelSwitchTarget) => void;
};
-/** Offers to put a chat back on the model it was started on. Deliberately an offer and not an
+/** Offers to put a chat back on the model it last used. Deliberately an offer and not an
* automatic switch: for a local model that would evict whatever is resident and spend a
* multi-gigabyte load on opening a chat. */
export function ChatModelNotice({
@@ -117,7 +117,7 @@ export function ChatModelNotice({
<button
type="button"
data-chat-model-notice=""
- aria-label={`Original model: ${label}`}
+ aria-label={`Last used model: ${label}`}
className="flex h-[var(--studio-chat-control-height,34px)] min-w-[calc(30px*var(--ui-space-scale,1))] max-w-56 shrink cursor-pointer items-center rounded-[10px] px-2 text-ui-13 text-nav-fg transition-colors hover:bg-nav-surface-hover hover:text-black focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring aria-expanded:bg-nav-surface-hover aria-expanded:text-black dark:hover:text-white dark:aria-expanded:text-white max-sm:min-w-[calc(22px*var(--ui-space-scale,1))] max-sm:px-1"
>
<HugeiconsIcon
@@ -134,18 +134,18 @@ export function ChatModelNotice({
</PopoverTrigger>
</TooltipTrigger>
<TooltipContent side="bottom" sideOffset={6}>
- Original model: {label}
+ Last used model: {label}
</TooltipContent>
</Tooltip>
<PopoverContent
align="start"
sideOffset={6}
collisionPadding={12}
- aria-label="Original model"
+ aria-label="Last used model"
className="menu-soft-surface w-64 gap-1 rounded-[14px] p-1"
>
<div className="space-y-1 px-3 py-2">
- <p className="text-ui-12 text-muted-foreground">Original model</p>
+ <p className="text-ui-12 text-muted-foreground">Last used model</p>
<p className="break-words text-ui-13 font-medium">{label}</p>
</div>
<PopoverPrimitive.Close asChild={true}>
diff --git a/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts b/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts
index 0a0e90b8ceb..dc7fbc96c10 100644
--- a/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts
+++ b/studio/frontend/src/features/chat/utils/queued-chat-run-settings.ts
@@ -102,11 +102,27 @@ export function resolveDeferredQueuedModelSettings(
};
}
-/** A queued send may only fill in the model of a row that was written without one. */
-export function shouldPersistResolvedQueuedModel(
- storedThread: { modelId?: string | null } | null | undefined,
+/** A chat remembers the model it last sent to, so reopening it offers that one back. Compare panes
+ * keep the model they were opened with: their labels and pairing read it. An unknown variant
+ * never clears a saved one for the same model. */
+export function shouldPersistRunModel(
+ storedThread:
+ | {
+ modelId?: string | null;
+ modelGgufVariant?: string | null;
+ pairId?: string | null;
+ }
+ | null
+ | undefined,
+ modelId: string,
+ modelGgufVariant: string | null,
): boolean {
- return Boolean(storedThread && !storedThread.modelId);
+ if (!storedThread || !modelId || storedThread.pairId) return false;
+ if (storedThread.modelId !== modelId) return true;
+ return (
+ modelGgufVariant != null &&
+ modelGgufVariant !== (storedThread.modelGgufVariant ?? null)
+ );
}
export function registerQueuedChatRunSettings(
diff --git a/studio/frontend/tests/chat-remembers-its-model.test.ts b/studio/frontend/tests/chat-remembers-its-model.test.ts
index e8986c7e68b..f79b5683cb9 100644
--- a/studio/frontend/tests/chat-remembers-its-model.test.ts
+++ b/studio/frontend/tests/chat-remembers-its-model.test.ts
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-// Every thread row has carried modelId since long before this notice, so the model a chat
-// was started on is already known for chats that already exist. What was missing was
-// offering it back. The rules below are the ones that keep the offer from becoming a
+// Every thread row has carried modelId since long before this notice, and each send now
+// moves it to the model that send ran on, so a chat knows the model it last used. What was
+// missing was offering it back. The rules below are the ones that keep the offer from becoming a
// nuisance: it never loads anything on its own, and it stays quiet whenever it could not
// be honoured.
@@ -38,7 +38,7 @@ const {
resolveResidentInitialConfig,
savePerModelConfig,
} = await import("../src/features/model-picker/model-config/per-model-config.ts");
-const { shouldPersistResolvedQueuedModel } = await import(
+const { shouldPersistRunModel } = await import(
"../src/features/chat/utils/queued-chat-run-settings.ts"
);
const { wantsDownloadManagerStaging } = await import(
@@ -357,22 +357,32 @@ test("a chat thread keeps the GGUF variant it started on", () => {
assert.match(initialize, /modelGgufVariant: modelGgufVariantAtInit/);
});
-test("a queued empty-model send backfills its resolved GGUF variant", () => {
+test("a send records the model and GGUF variant it ran on", () => {
const persistence = slice(
adapter,
- "const persistResolvedQueuedModel",
+ "const persistRunModel",
"if (queuedRunSettings)",
);
assert.match(persistence, /modelGgufVariant: string \| null/);
assert.match(persistence, /modelGgufVariant,/);
- assert.match(persistence, /shouldPersistResolvedQueuedModel/);
+ assert.match(persistence, /shouldPersistRunModel\(storedThread, modelId, modelGgufVariant\)/);
+ // A failed write only loses the offer on reopen.
+ assert.match(persistence, /\} catch \{/);
assert.match(adapter, /activeGgufVariant: state\.activeGgufVariant/);
assert.match(adapter, /activeGgufVariant: status\.gguf_variant \?\? null/);
assert.match(
adapter,
/queuedEmptyModelRuntime !== null[\s\S]{0,100}queuedEmptyModelRuntime\.activeGgufVariant[\s\S]{0,80}liveRuntime\.activeGgufVariant/,
);
- assert.match(adapter, /params\.checkpoint,\s*runtime\.activeGgufVariant/);
+ // Chat, audio and research runs record it once past the gates that refuse a send, and never
+ // hold the send up waiting on it.
+ assert.doesNotMatch(adapter, /await persistRunModel\(/);
+ assert.equal(
+ adapter.match(
+ /releaseCurrentPreStreamRun\(\);\s*(?:\/\/[^\n]*\s*)?void persistRunModel\(params\.checkpoint, runtime\.activeGgufVariant\);\s*runtime\.setThreadRunning\(threadKey, true/g,
+ )?.length,
+ 3,
+ );
const queuedSettings = readText(
"../src/features/chat/utils/queued-chat-run-settings.ts",
);
@@ -383,22 +393,42 @@ test("a queued empty-model send backfills its resolved GGUF variant", () => {
);
});
-test("queued model backfill changes only a fresh empty thread row", () => {
- assert.equal(shouldPersistResolvedQueuedModel({ modelId: "" }), true);
+test("a chat remembers the model it last sent to", () => {
+ // A queued send on a row written without a model fills it in.
+ assert.equal(shouldPersistRunModel({ modelId: "" }, "unsloth/Qwen3-8B", null), true);
+ // Switching models mid-chat moves the chat to the new one.
+ assert.equal(shouldPersistRunModel({ modelId: "unsloth/Qwen3-8B" }, "unsloth/gemma-3-4b-it", null), true);
+ assert.equal(
+ shouldPersistRunModel(
+ { modelId: "unsloth/Qwen3-8B-GGUF", modelGgufVariant: "Q4_K_M" },
+ "unsloth/Qwen3-8B-GGUF",
+ "Q8_0",
+ ),
+ true,
+ );
+ // Same model again writes nothing, so repeated sends are a no-op.
+ assert.equal(shouldPersistRunModel({ modelId: "unsloth/Qwen3-8B" }, "unsloth/Qwen3-8B", null), false);
assert.equal(
- shouldPersistResolvedQueuedModel({ modelId: "original-model" }),
+ shouldPersistRunModel(
+ { modelId: "unsloth/Qwen3-8B-GGUF", modelGgufVariant: "Q4_K_M" },
+ "unsloth/Qwen3-8B-GGUF",
+ "Q4_K_M",
+ ),
false,
);
- assert.equal(shouldPersistResolvedQueuedModel(undefined), false);
- // The caller has already returned on a queued checkpoint four lines earlier.
- assert.match(
- slice(
- adapter,
- "const persistResolvedQueuedModel",
- "if (queuedRunSettings)",
+ // An unknown variant never clears the saved one for the same model.
+ assert.equal(
+ shouldPersistRunModel(
+ { modelId: "unsloth/Qwen3-8B-GGUF", modelGgufVariant: "Q4_K_M" },
+ "unsloth/Qwen3-8B-GGUF",
+ null,
),
- /queuedRunSettings\.params\.checkpoint \|\|/,
+ false,
);
+ // Compare panes keep the model they were opened with, and no row or no model means no write.
+ assert.equal(shouldPersistRunModel({ modelId: "a", pairId: "pair-1" }, "b", null), false);
+ assert.equal(shouldPersistRunModel(undefined, "b", null), false);
+ assert.equal(shouldPersistRunModel({ modelId: "a" }, "", null), false);
});
// The switch back is the picker's handler, so it has to arrive carrying what the picker
diff --git a/tests/studio/test_multi_chat_prompt_queue_contract.py b/tests/studio/test_multi_chat_prompt_queue_contract.py
index d37c21cbd2d..7f1c20ab0a2 100644
--- a/tests/studio/test_multi_chat_prompt_queue_contract.py
+++ b/tests/studio/test_multi_chat_prompt_queue_contract.py
@@ -502,8 +502,7 @@ def test_queued_settings_are_thread_scoped_without_cross_chat_fallback():
assert (
len(
re.findall(
- r"await persistResolvedQueuedModel\(\s*params\.checkpoint,"
- r"\s*runtime\.activeGgufVariant,\s*\)",
+ r"void persistRunModel\(params\.checkpoint, runtime\.activeGgufVariant\);",
CHAT_ADAPTER,
)
)