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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio chat: go back to a chat's model when it is still loaded (#14568)
Debojyoti Banerjee96121e6ad11f
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.tsindex 22278c834ca..1943bb13af7 100644--- a/studio/frontend/scripts/check-bundle-budget.ts+++ b/studio/frontend/scripts/check-bundle-budget.ts@@ -393,8 +393,13 @@ export const BUDGET = {// catalog the chat chunk imports: +1,298 B raw / +181 B transfer (6,919,946 -> 6,921,244 B raw, 2,084,333 ->// 2,084,514 B transfer against 4e2838ec5f, 2026-10-22, one machine and build, Node 24, chunk count unchanged// at 97). Raised by that growth so main's margin stays where it was.- transferBytes: 2_085_018,- rawBytes: 6_922_543,+ // A chat going back to its model when it is still loaded (#14568): the eager chat model notice and the+ // adopt-only pick in the chat runtime, +1,970 B raw / +627 B transfer (6,922,858 -> 6,924,828 B raw,+ // 2,085,257 -> 2,085,884 B transfer against 1f5576747, Node 24, 98 chunks), where main was already 315 B raw /+ // 239 B transfer over. Set to the measured build plus about 300 B raw / 100 B transfer (CI builds measure+ // about 100 B over a local one).+ transferBytes: 2_085_990,+ rawBytes: 6_925_130,};// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering thediff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex efbe775ca6e..a81deb56cef 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -3842,16 +3842,26 @@ export function ChatPage({onAttachVideo: handleNativeVideoDrop,});+ // Every user pick, staged and no-op included: an automatic switch dispatched earlier must not land after it.+ const userPickCountRef = useRef(0);const handleCheckpointChange = useCallback((value: string,// Partial: the switch-back carries only what the resolver cannot recover.- meta?: Partial<ModelSelectorChangeMeta>,+ meta?: Partial<ModelSelectorChangeMeta> & {+ adoptOnly?: boolean;+ stillWanted?: () => boolean;+ },) => {const store = useChatRuntimeStore.getState();const currentCheckpoint = store.params.checkpoint;const currentVariant = store.activeGgufVariant;+ if (!meta?.adoptOnly) userPickCountRef.current += 1;if (!value) return;+ // NPU picks have no adopt path: loadNpuModel always reaches /load.+ if (meta?.adoptOnly && (isNpuModelId(value) || isExternalModelId(value))) {+ return;+ }setPendingHubAutoLoad(null);const isExternalSelection =meta?.source === "external" || isExternalModelId(value);@@ -4117,6 +4127,8 @@ export function ChatPage({nativePathToken: meta?.nativePathToken,nativePathExpiresAtMs: meta?.nativePathExpiresAtMs,forceReload: meta?.forceReload ?? (isSameLoadedModel || undefined),+ adoptOnly: meta?.adoptOnly,+ stillWanted: meta?.stillWanted,};await stageOrLoad(selection);})();@@ -4496,6 +4508,19 @@ export function ChatPage({},[handleCheckpointChange, loraModels],);+ // Opening a chat may only take a slot that already serves its model as is: a reload stays a click.+ const handleAutoSwitchToChatModel = useCallback(+ (target: ChatModelSwitchTarget, stillWanted: () => boolean) => {+ const picks = userPickCountRef.current;+ handleCheckpointChange(target.modelId, {+ ...chatModelSwitchMeta(target, loraModels),+ isDownloaded: true,+ adoptOnly: true,+ stillWanted: () => stillWanted() && userPickCountRef.current === picks,+ });+ },+ [handleCheckpointChange, loraModels],+ );const inventoryRefreshStartedRef = useRef(false);const refreshDeferredModelInventories = useCallback(() => {@@ -4790,7 +4815,9 @@ export function ChatPage({checkpoint={inferenceCheckpoint}activeGgufVariant={activeGgufVariant}selectableModelIds={selectableModelIds}+ loadedModels={loadedModels}onSwitch={handleSwitchBackToChatModel}+ onAutoSwitch={handleAutoSwitchToChatModel}/>)}{view.mode !== "compare" && currentProjectId && (diff --git a/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts b/studio/frontend/src/features/chat/components/chat-model-notice-switch.tsindex a841805cdd7..cb0cc984ad6 100644--- a/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts+++ b/studio/frontend/src/features/chat/components/chat-model-notice-switch.ts@@ -68,6 +68,78 @@ export function chatModelIsResident();}+export type LoadedChatModelSlot = {+ id: string;+ quant?: string | null;+ checkpoint?: string;+};++/** The loaded slot already serving a chat's model, or null. Another quant of the same repo is a different load. */+export function chatModelLoadedTarget(+ createdModel: ChatModelSwitchTarget,+ loadedModels: readonly LoadedChatModelSlot[],+): ChatModelSwitchTarget | null {+ for (const slot of loadedModels) {+ const checkpoint = slot.checkpoint ?? slot.id;+ // undefined = the slot's quant is still being looked up: wait for it rather than guess.+ if (+ slot.quant === undefined ||+ !sameHfCacheIdentity(checkpoint, createdModel.modelId)+ ) {+ continue;+ }+ if (createdModel.ggufVariant != null) {+ if (+ !slot.quant ||+ !ggufVariantsMatch(createdModel.ggufVariant, slot.quant)+ ) {+ continue;+ }+ }+ return {+ modelId: checkpoint,+ ggufVariant: slot.quant ?? createdModel.ggufVariant ?? null,+ };+ }+ return null;+}++export type ChatModelAutoSwitch = { threadId?: string; settled: boolean };++/** Settles once the open chat is on its model, so a later pick inside it is never undone. */+export function stepChatModelAutoSwitch(+ previous: ChatModelAutoSwitch,+ input: {+ threadId: string | undefined;+ active: boolean;+ resident: boolean;+ loadedTarget: ChatModelSwitchTarget | null;+ modelLoading: boolean;+ },+): { state: ChatModelAutoSwitch; switchTo: ChatModelSwitchTarget | null } {+ if (!(input.active && input.threadId)) {+ return { state: { threadId: undefined, settled: false }, switchTo: null };+ }+ const state =+ previous.threadId === input.threadId+ ? previous+ : { threadId: input.threadId, settled: false };+ if (state.settled) {+ return { state, switchTo: null };+ }+ if (input.resident) {+ return { state: { ...state, settled: true }, switchTo: null };+ }+ if (!input.loadedTarget) {+ return { state, switchTo: null };+ }+ return {+ state: { ...state, settled: true },+ // A load already running is a newer pick than the one this chat saved.+ switchTo: input.modelLoading ? null : input.loadedTarget,+ };+}+/** The live picker id Switch Back loads: the exact id, else a row of the same HF cache repo. */export function chatModelSelectableId(modelId: string,@@ -160,6 +232,15 @@ export function createChatModelHistoryReader(updateSeen = true;emit(chatModelSwitchTargetFromThread(thread));},+ // Another tab's change: no model until a fresh read lands, and no in-flight opening read answers.+ invalidate(): void {+ if (disposed) {+ return;+ }+ updateSeen = true;+ emitted = undefined;+ onModel(null);+ },dispose(): void {disposed = true;},diff --git a/studio/frontend/src/features/chat/components/chat-model-notice.tsx b/studio/frontend/src/features/chat/components/chat-model-notice.tsxindex 4c5925f6e6f..0bd3358994b 100644--- a/studio/frontend/src/features/chat/components/chat-model-notice.tsx+++ b/studio/frontend/src/features/chat/components/chat-model-notice.tsx@@ -4,7 +4,7 @@import { ArrowTurnBackwardIcon } from "@hugeicons/core-free-icons";import { HugeiconsIcon } from "@hugeicons/react";import { Popover as PopoverPrimitive } from "radix-ui";-import { useEffect, useState } from "react";+import { useEffect, useMemo, useRef, useState } from "react";import { Button } from "@/components/ui/button";import {@@ -23,12 +23,17 @@ import {type ChatHistoryUpdatedDetail,} from "../api/chat-api";import { externalModelLabel } from "../lib/external-model-label";+import { useChatRuntimeStore } from "../stores/chat-runtime-store";import { getStoredChatThread } from "../utils/chat-history-storage";import {+ type ChatModelAutoSwitch,type ChatModelSwitchTarget,+ type LoadedChatModelSlot,chatModelIsResident,+ chatModelLoadedTarget,chatModelSelectableId,createChatModelHistoryReader,+ stepChatModelAutoSwitch,} from "./chat-model-notice-switch";/** reads the saved chat's last used model and waits for queued initialization. */@@ -48,10 +53,25 @@ export function useChatCreatedModel(const reader = createChatModelHistoryReader(threadId, (model) =>setRead({ threadId, model }),);+ // Bumped by every newer answer, so a slower re-read never lands over it.+ let readGeneration = 0;const onHistoryUpdated = (event: Event): void => {const updatedThread = (event as CustomEvent<ChatHistoryUpdatedDetail>).detail?.thread;- if (updatedThread) reader.applyUpdate(updatedThread);+ if (updatedThread) {+ if (updatedThread.id === threadId) readGeneration += 1;+ reader.applyUpdate(updatedThread);+ return;+ }+ // Another tab's change arrives as a plain Event; this tab's thread-less ones (chunks) are CustomEvents.+ if (event instanceof CustomEvent) return;+ reader.invalidate();+ const generation = ++readGeneration;+ void getStoredChatThread(threadId)+ .then((thread) => {+ if (thread && generation === readGeneration) reader.applyUpdate(thread);+ })+ .catch(() => undefined);};window.addEventListener(CHAT_HISTORY_UPDATED_EVENT, onHistoryUpdated);void getStoredChatThread(threadId)@@ -60,6 +80,8 @@ export function useChatCreatedModel(return () => {reader.dispose();window.removeEventListener(CHAT_HISTORY_UPDATED_EVENT, onHistoryUpdated);+ // Unsubscribed from here on, so coming back must wait for a fresh read.+ setRead(null);};}, [threadId]);return read && read.threadId === threadId ? read.model : null;@@ -75,27 +97,72 @@ type ChatModelNoticeProps = {activeGgufVariant: string | null;/** Every model that can be selected right now, by id. */selectableModelIds: ReadonlySet<string>;+ loadedModels: readonly LoadedChatModelSlot[];onSwitch: (target: ChatModelSwitchTarget) => void;+ onAutoSwitch: (+ target: ChatModelSwitchTarget,+ stillWanted: () => boolean,+ ) => void;};-/** 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. */+/** Adopts a chat's last used model when it is still loaded, else only offers it: a local load evicts. */export function ChatModelNotice({active,threadId,checkpoint,activeGgufVariant,selectableModelIds,+ loadedModels,onSwitch,+ onAutoSwitch,}: ChatModelNoticeProps) {const createdModel = useChatCreatedModel(threadId);+ const resident =+ createdModel != null &&+ chatModelIsResident(createdModel, checkpoint, activeGgufVariant);+ const loadedTarget = useMemo(+ () =>+ createdModel && !resident+ ? chatModelLoadedTarget(createdModel, loadedModels)+ : null,+ [createdModel, resident, loadedModels],+ );+ // A send that moves the chat to another model before the switch lands drops the switch.+ const createdKey = createdModel+ ? `${createdModel.modelId}\0${createdModel.ggufVariant ?? ""}`+ : null;+ const createdKeyRef = useRef(createdKey);+ useEffect(() => {+ createdKeyRef.current = createdKey;+ }, [createdKey]);+ const autoSwitchRef = useRef<ChatModelAutoSwitch>({ settled: false });+ // Unmounting (Compare, a Project) ends the open too, so a pending switch must not land after it.+ useEffect(+ () => () => {+ autoSwitchRef.current = { settled: false };+ },+ [],+ );+ useEffect(() => {+ const { state, switchTo } = stepChatModelAutoSwitch(autoSwitchRef.current, {+ threadId,+ active,+ resident,+ loadedTarget,+ modelLoading: useChatRuntimeStore.getState().modelLoading,+ });+ autoSwitchRef.current = state;+ if (switchTo) {+ const key = createdKeyRef.current;+ onAutoSwitch(+ switchTo,+ () => autoSwitchRef.current === state && createdKeyRef.current === key,+ );+ }+ }, [active, threadId, resident, loadedTarget, onAutoSwitch]);// Drop the portaled UI off-route, retaining the successful read and history subscription.if (!active) return null;- if (!createdModel) return null;- if (chatModelIsResident(createdModel, checkpoint, activeGgufVariant)) {- return null;- }+ if (!createdModel || resident) return null;// A model that has since been deleted, or a connection that is gone: the switch could not be// honoured, and saying so on every open is just noise. A gone snapshot loads its repo's live row.const selectableId = chatModelSelectableId(diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.tsindex 15e1594d3f7..dab2197b65d 100644--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts@@ -289,6 +289,10 @@ export type SelectedModelInput = {/** Keep the current speculative-decoding choice across the model switch instead of resetting it* to the standing preference. */keepSpeculative?: boolean;+ /** Only take a model already serving exactly as this pick would load it; never reach /load. */+ adoptOnly?: boolean;+ /** For an adoptOnly pick: false once the chat that asked for it is no longer open. */+ stillWanted?: () => boolean;config?: PerModelConfig;previousConfig?: PerModelConfig;};@@ -1320,6 +1324,19 @@ export function useChatModelRuntime() {typeof selection === "string" ? false : selection.throwOnError ?? false;const keepSpeculative =typeof selection === "string" ? false : selection.keepSpeculative ?? false;+ const adoptOnly =+ typeof selection === "string" ? false : selection.adoptOnly ?? false;+ const stillWanted =+ (typeof selection === "string" ? undefined : selection.stillWanted) ??+ (() => true);+ // An automatic pick never displaces a load, running or still in preflight: that pick is newer.+ const loadInFlight = () =>+ Boolean(+ activeLoadRunRef.current ||+ loadingModelRef.current ||+ useChatRuntimeStore.getState().loadingModelPick ||+ useChatRuntimeStore.getState().modelLoading,+ );const currentVariant = useChatRuntimeStore.getState().activeGgufVariant;const initiallyLoadingSamePick =loadingModelRef.current?.id === modelId &&@@ -1348,6 +1365,10 @@ export function useChatModelRuntime() {restorePreviousConfig();return;}+ if (adoptOnly && (loadInFlight() || !stillWanted())) {+ restorePreviousConfig();+ return;+ }dismissStartToastsForModelSelection();// Register the user's intent before awaiting cancellation. A superseded run's@@ -1360,8 +1381,9 @@ export function useChatModelRuntime() {let hfToken = useChatRuntimeStore.getState().hfToken || null;// Credential prompts must complete before cancelling a pending run: a decline must not// strand the resident that run already unloaded while preparing its own Hub load.+ // An adopt-only pick downloads nothing, so it never opens the token prompt.const mayReachHub =- !isLocal && !isOllamaModelId(modelId) && nativePathToken == null;+ !adoptOnly && !isLocal && !isOllamaModelId(modelId) && nativePathToken == null;if (mayReachHub) {const preparedToken = await prepareHfTokenForUse(hfToken);if (!preparedToken.proceed) {@@ -1517,6 +1539,10 @@ export function useChatModelRuntime() {const inFlightLoad =loadingModelRef.current ??useChatRuntimeStore.getState().loadingModelPick;+ if (adoptOnly && (inFlightLoad || activeRun)) {+ restorePreviousConfig();+ return;+ }// Cancellation clears the picker before its unload settles, yet the run keeps// the slot and the lifecycle lease until then. Break only when neither is left:// a pick arriving mid-cancel would otherwise find the lease held and be dropped.@@ -1732,6 +1758,10 @@ export function useChatModelRuntime() {// Same window as the confirmation below: a newer pick that could not take the// lease still supersedes the status this preflight was about to adopt.if (modelSelectionIntentEpoch !== loadIntentId) return;+ if (adoptOnly && (loadInFlight() || !stillWanted())) {+ restorePreviousConfig();+ return;+ }// Roll back the config pre-applied for the load that is not happening, before hydrating, so the// resident status wins over the staged snapshot. The helper carries the diffusion flag.restorePreviousConfig();@@ -1793,6 +1823,16 @@ export function useChatModelRuntime() {}}}+ if (adoptOnly) {+ // Same unwind as a declined confirmation: nothing was unloaded.+ if (modelSelectionIntentEpoch === loadIntentId) {+ restorePreviousConfig();+ if (pendingReplacementRollback?.residentUnloaded === false) {+ pendingReplacementRollback = null;+ }+ }+ return;+ }// Hold the lifecycle lease through confirmation and loading. A PREFLIGHT holder (parked on// the stop-running-chats confirmation) owns the lease without having published a run or adiff --git a/studio/frontend/tests/chat-remembers-its-model.test.ts b/studio/frontend/tests/chat-remembers-its-model.test.tsindex 248d36fabf6..f78ad52aed3 100644--- a/studio/frontend/tests/chat-remembers-its-model.test.ts+++ b/studio/frontend/tests/chat-remembers-its-model.test.ts@@ -4,8 +4,8 @@// 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.+// nuisance: it never loads anything on its own, it stays quiet whenever it could not be+// honoured, and a model still loaded alongside is switched to without asking.import assert from "node:assert/strict";import test from "node:test";@@ -21,10 +21,12 @@ registerBundlerResolver();const { store } = installLocalStorageFake();const {chatModelIsResident,+ chatModelLoadedTarget,chatModelSelectableId,chatModelSwitchMeta,createChatModelHistoryReader,resolveChatModelSwitchTarget,+ stepChatModelAutoSwitch,} = await import("../src/features/chat/components/chat-model-notice-switch.ts");const { modelDisplayName } = await import("../src/features/hub/lib/model-identity.ts"@@ -48,6 +50,7 @@ const { wantsDownloadManagerStaging } = await import(const notice = atDefaultUiScale(readText("../src/features/chat/components/chat-model-notice.tsx"));const page = atDefaultUiScale(readText("../src/features/chat/chat-page.tsx"));const runtimeProvider = atDefaultUiScale(readText("../src/features/chat/runtime-provider.tsx"));+const runtimeHook = readText("../src/features/chat/hooks/use-chat-model-runtime.ts");const adapter = atDefaultUiScale(readText("../src/features/chat/api/chat-adapter.ts"));const chatApi = atDefaultUiScale(readText("../src/features/chat/api/chat-api.ts"));const types = atDefaultUiScale(readText("../src/features/chat/types.ts"));@@ -82,14 +85,207 @@ function slice(source: string, from: string, to: string): string {return source.slice(start, end);}-test("the notice never switches a model on its own", () => {+// Source assertions: the notice reaches external-providers, which does not resolve in a+// bare node test. The sibling thread-scoped suites do the same for the same reason.++test("the notice never loads a model on its own", () => {+ // Opening a chat must not evict what is resident: a local load is multi-gigabyte.assert.doesNotMatch(notice, /loadModel|setCheckpoint/);+ // A model that is not loaded is only ever switched to from the button.assert.match(notice, /onClick=\{\(\) => onSwitch\(switchTarget\)\}/);+ // The one automatic switch is to a slot that is already loaded.+ const body = notice.slice(notice.indexOf("export function ChatModelNotice"));+ assert.match(body, /chatModelLoadedTarget\(createdModel, loadedModels\)/);+ assert.match(+ body,+ /onAutoSwitch\(\s*switchTo,\s*\(\) => autoSwitchRef\.current === state && createdKeyRef\.current === key,\s*\);/,+ );+});++test("a chat goes back to its model when that model is still loaded", () => {+ const slots = [+ { id: "unsloth/Model-A-GGUF", quant: "Q4_K_M", checkpoint: "unsloth/Model-A-GGUF" },+ { id: "Model-B", quant: null, checkpoint: "/models/Model-B" },+ ];+ assert.deepEqual(+ chatModelLoadedTarget(+ { modelId: "unsloth/Model-A-GGUF", ggufVariant: "Q4_K_M" },+ slots,+ ),+ { modelId: "unsloth/Model-A-GGUF", ggufVariant: "Q4_K_M" },+ );+ // A local model is loaded by its path, so the slot's checkpoint is what gets selected.+ assert.deepEqual(+ chatModelLoadedTarget({ modelId: "/models/Model-B", ggufVariant: null }, slots),+ { modelId: "/models/Model-B", ggufVariant: null },+ );+ // A chat saved without a variant takes the quant the slot is serving.+ assert.deepEqual(+ chatModelLoadedTarget({ modelId: "unsloth/Model-A-GGUF" }, slots),+ { modelId: "unsloth/Model-A-GGUF", ggufVariant: "Q4_K_M" },+ );+});++test("a chat is not switched to a loaded slot that would need another load", () => {+ const slots = [+ { id: "unsloth/Model-A-GGUF", quant: "Q4_K_M", checkpoint: "unsloth/Model-A-GGUF" },+ ];+ // Another quant of the same repo is a different load.+ assert.equal(+ chatModelLoadedTarget(+ { modelId: "unsloth/Model-A-GGUF", ggufVariant: "Q8_0" },+ slots,+ ),+ null,+ );+ // A slot whose quant is not known yet cannot vouch for the saved variant.+ assert.equal(+ chatModelLoadedTarget(+ { modelId: "unsloth/Model-A-GGUF", ggufVariant: "Q4_K_M" },+ [{ id: "unsloth/Model-A-GGUF", checkpoint: "unsloth/Model-A-GGUF" }],+ ),+ null,+ );+ // Nor a chat saved without a variant: the slot may serve another quant until its lookup lands.+ assert.equal(+ chatModelLoadedTarget(+ { modelId: "unsloth/Model-A-GGUF" },+ [{ id: "unsloth/Model-A-GGUF", checkpoint: "unsloth/Model-A-GGUF" }],+ ),+ null,+ );+ assert.equal(+ chatModelLoadedTarget({ modelId: "unsloth/Model-C" }, slots),+ null,+ );+ assert.equal(chatModelLoadedTarget({ modelId: "unsloth/Model-A-GGUF" }, []), null);+});++test("the automatic switch runs once per chat open", () => {+ const modelA = { modelId: "unsloth/Model-A-GGUF", ggufVariant: "Q4_K_M" };+ const open = (threadId: string | undefined) => ({+ threadId,+ active: true,+ resident: false,+ loadedTarget: modelA as typeof modelA | null,+ modelLoading: false,+ });+ // Opening chat A while Model B is selected switches to A's loaded slot, once.+ let step = stepChatModelAutoSwitch({ settled: false }, open("a"));+ assert.deepEqual(step.switchTo, modelA);+ step = stepChatModelAutoSwitch(step.state, open("a"));+ assert.equal(step.switchTo, null);+ // Going to chat B and back to A switches again.+ step = stepChatModelAutoSwitch(step.state, { ...open("b"), loadedTarget: null });+ step = stepChatModelAutoSwitch(step.state, open("a"));+ assert.deepEqual(step.switchTo, modelA);++ // Opened already on its model: a pick made afterwards inside the chat is not undone.+ step = stepChatModelAutoSwitch({ settled: false }, {+ ...open("a"),+ resident: true,+ loadedTarget: null,+ });+ step = stepChatModelAutoSwitch(step.state, open("a"));+ assert.equal(step.switchTo, null);++ // Waits for the saved model and the loaded slots to be known, then switches.+ step = stepChatModelAutoSwitch({ settled: false }, { ...open("a"), loadedTarget: null });+ assert.equal(step.state.settled, false);+ step = stepChatModelAutoSwitch(step.state, open("a"));+ assert.deepEqual(step.switchTo, modelA);++ // A load already in flight wins, and is not undone when it lands.+ step = stepChatModelAutoSwitch({ settled: false }, { ...open("a"), modelLoading: true });+ assert.equal(step.switchTo, null);+ step = stepChatModelAutoSwitch(step.state, open("a"));+ assert.equal(step.switchTo, null);++ // Leaving the chat route and coming back to the same chat is a new open, so it switches again.+ step = stepChatModelAutoSwitch({ settled: false }, open("a"));+ step = stepChatModelAutoSwitch(step.state, { ...open("a"), active: false });+ step = stepChatModelAutoSwitch(step.state, open("a"));+ assert.deepEqual(step.switchTo, modelA);++ // Off the chat route, or on an unsent New Chat, nothing happens.+ assert.equal(+ stepChatModelAutoSwitch({ settled: false }, { ...open("a"), active: false }).switchTo,+ null,+ );+ assert.equal(stepChatModelAutoSwitch({ settled: false }, open(undefined)).switchTo, null);++ const body = notice.slice(notice.indexOf("export function ChatModelNotice"));+ assert.match(+ body,+ /onAutoSwitch\(\s*switchTo,\s*\(\) => autoSwitchRef\.current === state && createdKeyRef\.current === key,\s*\);/,+ );+ // Any loaded slot counts, Keep models loaded on or off: the switch only ever adopts it.+ assert.match(body, /createdModel && !resident\s*\?\s*chatModelLoadedTarget/);+ assert.doesNotMatch(body, /keepModelsLoaded/);+ // A cross-tab revision carries no thread, so the saved model is read again rather than kept stale.+ // Only the plain cross-tab Event re-reads (streamed chunks are CustomEvents), and a slower read+ // never lands over a newer answer.+ assert.match(notice, /if \(event instanceof CustomEvent\) return;\s*reader\.invalidate\(\);\s*const generation = \+\+readGeneration;/);+ assert.match(notice, /if \(updatedThread\.id === threadId\) readGeneration \+= 1;/);+ assert.match(+ notice,+ /getStoredChatThread\(threadId\)\s*\.then\(\(thread\) => \{\s*if \(thread && generation === readGeneration\) reader\.applyUpdate\(thread\);/,+ );+ // Leaving a chat drops its read: a change made while unsubscribed must not settle the next open.+ assert.match(+ notice,+ /window\.removeEventListener\(CHAT_HISTORY_UPDATED_EVENT, onHistoryUpdated\);\s*(\/\/[^\n]*\n\s*)?setRead\(null\);/,+ );+ // Unmounting ends the open, so a pending switch's check turns false.+ assert.match(body, /useEffect\(\s*\(\) => \(\) => \{\s*autoSwitchRef\.current = \{ settled: false \};\s*\},\s*\[\],\s*\);/);+ assert.match(+ page,+ /loadedModels=\{loadedModels\}\s*onSwitch=\{handleSwitchBackToChatModel\}\s*onAutoSwitch=\{handleAutoSwitchToChatModel\}/,+ );+});++test("the automatic switch only adopts a slot, it never reaches /load", () => {+ const auto = slice(page, "const handleAutoSwitchToChatModel", "\n );\n");+ assert.match(auto, /isDownloaded: true,\s*adoptOnly: true,/);+ // Any pick of the user's after dispatch, staged or no-op, cancels the pending adoption.+ assert.match(auto, /stillWanted: \(\) => stillWanted\(\) && userPickCountRef\.current === picks,/);+ assert.match(page, /if \(!meta\?\.adoptOnly\) userPickCountRef\.current \+= 1;\s*if \(!value\) return;/);+ assert.match(page, /adoptOnly: meta\?\.adoptOnly,\s*stillWanted: meta\?\.stillWanted,\s*\};\s*await stageOrLoad\(selection\);/);+ // Bails after the adopt check and before the lease that every /load holds.+ const select = slice(runtimeHook, "const selectModel = useCallback", "let lifecycleLease");+ const adopted = select.lastIndexOf("void refreshContextUsage({ afterModelLoad: true });");+ const bail = select.indexOf("if (adoptOnly) {");+ assert.ok(adopted !== -1 && bail > adopted, "adoptOnly bails after the adopt path");+ assert.match(select.slice(bail), /restorePreviousConfig\(\);[\s\S]*?return;/);+ // Never displaces a load (before taking an intent, and in the replacement loop that would cancel it),+ // and never lands on a chat that is no longer open.+ const guard = /if \(adoptOnly && \(loadInFlight\(\) \|\| !stillWanted\(\)\)\) \{\s*restorePreviousConfig\(\);\s*return;/g;+ assert.equal(select.match(guard)?.length, 2);+ assert.ok(+ select.indexOf("if (adoptOnly && (loadInFlight() || !stillWanted()))") <+ select.indexOf("const loadIntentId = ++modelSelectionIntentEpoch;"),+ );+ assert.match(select, /if \(adoptOnly && \(inFlightLoad \|\| activeRun\)\) \{\s*restorePreviousConfig\(\);\s*return;/);+ // Downloads nothing, so it never opens the Hub token prompt.+ assert.match(select, /const mayReachHub =\s*!adoptOnly &&/);+ // NPU picks always reach /load, and an external pick is never a loaded slot.+ assert.match(+ page,+ /if \(meta\?\.adoptOnly && \(isNpuModelId\(value\) \|\| isExternalModelId\(value\)\)\) \{\s*return;\s*\}/,+ );+ assert.ok(+ page.indexOf("if (meta?.adoptOnly && (isNpuModelId(value)") < page.indexOf("void loadNpuModel(value"),+ );+ assert.ok(+ select.indexOf("if (adoptOnly && (inFlightLoad || activeRun))") <+ select.indexOf("await cancelLoadRun(activeRun, true)"),+ );});test("the notice stays quiet when it has nothing to offer", () => {const body = notice.slice(notice.indexOf("export function ChatModelNotice"));- assert.match(body, /if \(!createdModel\) return null;/);+ // no stamp, already on the pick (including snapshot vs repo id), or a model that has since gone away+ assert.match(body, /if \(!createdModel \|\| resident\) return null;/);assert.match(body,/chatModelIsResident\(createdModel, checkpoint, activeGgufVariant\)/,@@ -736,3 +932,18 @@ test("a Hub or external id is still switched on the id alone", () => {undefined,);});++test("another tab's change leaves no model until a fresh read lands", () => {+ const seen: unknown[] = [];+ const reader = createChatModelHistoryReader("t1", (model) => seen.push(model));+ reader.applyInitial({ id: "t1", modelId: "unsloth/A" } as never);+ reader.invalidate();+ // The opening read that was still in flight no longer answers.+ reader.applyInitial({ id: "t1", modelId: "unsloth/A" } as never);+ // The fresh read lands even when it names the same model.+ reader.applyUpdate({ id: "t1", modelId: "unsloth/A" } as never);+ assert.deepEqual(+ seen.map((m) => (m as { modelId?: string } | null)?.modelId ?? null),+ ["unsloth/A", null, "unsloth/A"],+ );+});diff --git a/tests/studio/test_new_chat_context_recount.py b/tests/studio/test_new_chat_context_recount.pyindex c1c716e5141..68c96832f46 100644--- a/tests/studio/test_new_chat_context_recount.py+++ b/tests/studio/test_new_chat_context_recount.py@@ -592,6 +592,10 @@ export async function adoptResidentModel(props: any): Promise<void> {const nativePathToken = undefined;const residentStatus: any = props.residentStatus;const readServerWideReloadHints = async (): Promise<boolean> => false;+ // A user pick, not the chat's automatic adopt-only one: these guards never return here.+ const adoptOnly = false;+ const loadInFlight = (): boolean => false;+ const stillWanted = (): boolean => true;// The two conditions the tail sits inside, restated so the braces balance and the// entry conditions are visible. Both say the same thing: this pick needs no load.if (!forceReload && !nativePathToken) {