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: keep a chat's own settings when its send waits on a model load (#17565)
oobaboogabe23b571da28
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex 23e4cb2ac68..066bda7e8f2 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -135,6 +135,7 @@ import {releasePreStreamRunReservation,} from "../utils/pre-stream-run-reservation";import { readThreadCreationClaim } from "../utils/chat-thread-creation-claim";+import { withRunThreadScopedSettings } from "../utils/thread-scoped-settings";import { contextUsageMarkers, threadBranchRecords } from "../utils/saved-context-usage";import { contextUsageReasoningKey } from "../utils/context-usage-reasoning";import {@@ -301,6 +302,8 @@ import {awaitPendingQwenDefaultsMigration,flushPendingChatSettings,codeToolsOn,+ withChatOwnedSettings,+ withLoadedModelOverrides,localMediaToolsForTurn,useChatRuntimeStore,} from "../stores/chat-runtime-store";@@ -4956,6 +4959,9 @@ export function createOpenAIStreamAdapter(store.clearPendingImageEditReference();}};+ // The waits below let the user open another chat, whose settings then fill the store.+ const runtimeAtSend = runtime;+ const chatOwnedAtSend = withChatOwnedSettings(runtime);if (runtime.modelLoading) {toast.info("Waiting for model to finish loading…");try {@@ -4996,6 +5002,12 @@ export function createOpenAIStreamAdapter(}const liveRuntime = useChatRuntimeStore.getState();+ // A pending read means the store holds defaults, not this chat's settings.+ const runChatSettingsLive =+ !liveRuntime.threadScopedSettingsPending &&+ (liveRuntime.activeThreadId === runtimeAtSend.activeThreadId ||+ (resolvedThreadId !== undefined &&+ liveRuntime.activeThreadId === resolvedThreadId));runtime = queuedRunSettings? queuedRunSettings.params.checkpoint? { ...liveRuntime, ...queuedRunSettings }@@ -5057,7 +5069,16 @@ export function createOpenAIStreamAdapter(queuedEmptyModelRuntime?.modelCapabilities ?? null,),}- : liveRuntime;+ : runChatSettingsLive+ ? liveRuntime+ : // The same model keeps the chat's effective values; a new one re-derives what its load sets.+ liveRuntime.params.checkpoint === runtimeAtSend.params.checkpoint &&+ (liveRuntime.activeGgufVariant ?? null) === (runtimeAtSend.activeGgufVariant ?? null)+ ? withRunThreadScopedSettings(liveRuntime, runtimeAtSend)+ : withLoadedModelOverrides(+ withRunThreadScopedSettings(liveRuntime, chatOwnedAtSend),+ liveRuntime,+ );runtime = withChatFullAccess(runtime, liveRuntime.chatFullAccessGrants, resolvedThreadId);const { params } = runtime;const sandboxSessionId = await resolveSandboxSessionId(diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.tsindex 5ff64a9e9a9..7fa16284f48 100644--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts@@ -1197,7 +1197,11 @@ function firstSetThreadScopedValue(...values: unknown[]): unknown {return values.find((value) => value !== undefined);}+/** The model-derived params the latest restore laid the open chat's values over. */+let paramsUnderChatValues: InferenceParams | null = null;+function restoreThreadScopedParams(params: InferenceParams): InferenceParams {+ paramsUnderChatValues = params;const kept: Record<string, unknown> = {};for (const key of THREAD_SCOPED_PARAM_KEYS) {const held = firstSetThreadScopedValue(@@ -1280,22 +1284,13 @@ function isSameThreadScopedValue(next: unknown, current: unknown): boolean {return false;}-function buildThreadScopedSnapshot(+/** Puts the chat's own values back where the store shows the loaded model's or a provider's. */+function restoreChatOwnedValues(threadId: string,- snapshot: ThreadScopedSettings | null,-): ThreadScopedSettings {- const settings =- snapshot ?? readThreadScopedSettings(useChatRuntimeStore.getState());- // Keep the stored level: the sanitizer drops a live "full", which would erase it.- if (- settings.permissionMode === undefined &&- threadId === threadScopedSettingsThreadId &&- activeThreadScopedSettings?.permissionMode !== undefined- ) {- settings.permissionMode = activeThreadScopedSettings.permissionMode;- }- // And for thinking, which a model that cannot stop thinking forces on: that true is the- // model's, so persisting it would erase a chat's stored false.+ settings: ThreadScopedSettings,+): void {+ // A model that cannot stop thinking forces thinking on: that true is the model's, so+ // persisting it would erase a chat's stored false.if (threadId === threadScopedSettingsThreadId &&!explicitlyEditedThreadFields.has("reasoningEnabled") &&@@ -1309,14 +1304,30 @@ function buildThreadScopedSnapshot(const onRecord = reasoningEffortOnRecord();if (onRecord !== undefined) settings.reasoningEffort = onRecord;}- // And for pills a provider constraint moved without persisting: the store holds the- // provider's value, so the chat keeps the one it was stored with.+ // A provider constraint moves pills without persisting: the chat keeps its stored value.if (keepsStoredValueUnderConstraint("reasoningEnabled", threadId, settings)) {settings.reasoningEnabled = activeThreadScopedSettings?.reasoningEnabled;}if (keepsStoredValueUnderConstraint("toolsEnabled", threadId, settings)) {settings.toolsEnabled = activeThreadScopedSettings?.toolsEnabled;}+}++function buildThreadScopedSnapshot(+ threadId: string,+ snapshot: ThreadScopedSettings | null,+): ThreadScopedSettings {+ const settings =+ snapshot ?? readThreadScopedSettings(useChatRuntimeStore.getState());+ // Keep the stored level: the sanitizer drops a live "full", which would erase it.+ if (+ settings.permissionMode === undefined &&+ threadId === threadScopedSettingsThreadId &&+ activeThreadScopedSettings?.permissionMode !== undefined+ ) {+ settings.permissionMode = activeThreadScopedSettings.permissionMode;+ }+ restoreChatOwnedValues(threadId, settings);if (threadId === threadScopedSettingsThreadId) {activeThreadScopedSettings = settings;}@@ -1981,6 +1992,48 @@ function codeDeclinedOnEnteringFullAccess(state: ChatRuntimeStore): boolean {: false;}+/** `state` with the paired chat's own values where the store shows its model's (pill clamps, a pin). */+export function withChatOwnedSettings<T extends ChatRuntimeStore>(state: T): T {+ const owned: ThreadScopedSettings = {+ reasoningEnabled: state.reasoningEnabled,+ reasoningEffort: state.reasoningEffort,+ };+ for (const key of CLAMPED_PILL_KEYS) {+ owned[key] = readThreadScopedValue(state, key) === true;+ }+ if (threadScopedSettingsThreadId !== null) {+ restoreChatOwnedValues(threadScopedSettingsThreadId, owned);+ }+ // Sampling the chat does not own is left unset: a new model's load supplies it (restoreThreadScopedParams).+ const params: Record<string, unknown> = { ...state.params };+ for (const key of THREAD_SCOPED_PARAM_KEYS) {+ if (firstSetThreadScopedValue(heldThreadScopedParamValue(key), threadScopedOverride(key)) === undefined) {+ params[key] = undefined;+ }+ }+ return { ...state, ...owned, params: params as unknown as T["params"] };+}++/** `run`, which carries the model `live` just loaded, with what that load sets: pill clamps, its effort pin, Think, unowned sampling. */+export function withLoadedModelOverrides<T extends ChatRuntimeStore>(run: T, live: T): T {+ const next: Record<string, unknown> = { ...run };+ for (const key of CLAMPED_PILL_KEYS) {+ next[key] = run[key] === true && !isPillClampedOff(run, key);+ }+ if (pinOwnsLiveReasoningEffort(run)) next.reasoningEffort = live.reasoningEffort;+ // Every load writes Think from the model (pin, forced gates, default), not from the chat.+ next.reasoningEnabled = live.reasoningEnabled;+ const loaded = (paramsUnderChatValues?.checkpoint === live.params.checkpoint+ ? paramsUnderChatValues+ : live.params) as unknown as Record<string, unknown>;+ const params: Record<string, unknown> = { ...run.params };+ for (const key of THREAD_SCOPED_PARAM_KEYS) {+ if (params[key] === undefined) params[key] = loaded[key];+ }+ next.params = params;+ return next as T;+}+/** Read the level live: Full access outlives a chat switch but codeToolsEnabled does not. */export function codeToolsOn(state: Pick<diff --git a/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts b/studio/frontend/src/features/chat/utils/thread-scoped-settings.tsindex 654ebabfd6d..c469f2e438a 100644--- a/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts+++ b/studio/frontend/src/features/chat/utils/thread-scoped-settings.ts@@ -158,6 +158,31 @@ export function isThreadOwnedSettingKey(key: string): boolean {);}+// Derived from permissionMode, so they must travel with it (codeToolsOn reads the last).+const PERMISSION_MIRROR_KEYS = [+ "confirmToolCalls",+ "bypassPermissions",+ "chatFullAccessPreviousMode",+ "codeToolsDeclinedUnderFullAccess",+] as const;++/** `live` with `own`'s thread-scoped settings and permission mirrors; the model fields stay live. */+export function withRunThreadScopedSettings<T extends { params: object }>(+ live: T,+ own: T,+): T {+ const source = own as unknown as Record<string, unknown>;+ const ownParams = own.params as Record<string, unknown>;+ const params: Record<string, unknown> = { ...live.params };+ const next: Record<string, unknown> = { ...live, params };+ for (const key of THREAD_SCOPED_SETTING_KEYS) {+ if (isThreadScopedParamKey(key)) params[key] = ownParams[key];+ else next[key] = source[key];+ }+ for (const key of PERMISSION_MIRROR_KEYS) next[key] = source[key];+ return next as T;+}+// The patch model is extra="forbid", so one bad field would 400 the whole write.export function sanitizeThreadScopedSettings(value: unknown,diff --git a/studio/frontend/tests/run-keeps-chat-settings-across-model-load.test.ts b/studio/frontend/tests/run-keeps-chat-settings-across-model-load.test.tsnew file mode 100644index 00000000000..5ea4e58ba48--- /dev/null+++ b/studio/frontend/tests/run-keeps-chat-settings-across-model-load.test.ts@@ -0,0 +1,416 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// A send that waits on a model load must not pick up the settings of the chat opened meanwhile.++import assert from "node:assert/strict";+import { register } from "node:module";+import test from "node:test";++import ts from "@typescript/typescript6";++import { installLocalStorageFake, readSrc } from "./helpers/kit.ts";+import { useModelReasoningEffortStore } from "../src/features/model-picker/components/model-selector/model-reasoning-effort.ts";+import {+ THREAD_SCOPED_PARAM_KEYS,+ THREAD_SCOPED_SETTING_KEYS,+ isThreadScopedParamKey,+ withRunThreadScopedSettings,+} from "../src/features/chat/utils/thread-scoped-settings.ts";++const { store: localStorageFake } = installLocalStorageFake();+localStorageFake.set("unsloth_chat_settings_imported_to_studio_db", "true");+register("./thread-sampling-resolver.mjs", import.meta.url);+await import("./helpers/store-stubs/settings-http.ts");+await import("./helpers/store-stubs/chat-history-storage.ts");++type StoreState = Record<string, unknown> & { params: { checkpoint: string } };+const {+ useChatRuntimeStore,+ codeToolsOn,+ withChatOwnedSettings,+ withLoadedModelOverrides,+ resolveToolsEnabledOnLoad,+} = (await import("../src/features/chat/stores/chat-runtime-store.ts")) as unknown as {+ useChatRuntimeStore: {+ getState: () => StoreState;+ setState: (patch: Record<string, unknown>) => void;+ };+ codeToolsOn: (state: StoreState) => boolean;+ withChatOwnedSettings: (state: StoreState) => StoreState;+ withLoadedModelOverrides: (run: StoreState, live: StoreState) => StoreState;+ resolveToolsEnabledOnLoad: (supportsTools: boolean) => Record<string, boolean>;+};++function chatState(overrides: Record<string, unknown>, params: Record<string, unknown>) {+ return {+ activeThreadId: "chat-a",+ permissionMode: "ask",+ confirmToolCalls: true,+ bypassPermissions: false,+ chatFullAccessPreviousMode: null,+ toolsEnabled: false,+ ragEnabled: false,+ ragSource: { type: "thread" },+ reasoningEffort: "low",+ supportsTools: false,+ loadedContextLength: 4096,+ loadedIsGguf: false,+ paramsByModel: {},+ ...overrides,+ params: {+ checkpoint: "",+ temperature: 0.2,+ topP: 0.9,+ systemPrompt: "chat A prompt",+ maxTokens: 512,+ ...params,+ },+ };+}++test("chat A keeps its permission mode, tools, prompt and sampling over chat B's", () => {+ const atSend = chatState({}, {});+ const afterSwitchAndLoad = chatState(+ {+ activeThreadId: "chat-b",+ permissionMode: "auto",+ toolsEnabled: true,+ ragEnabled: true,+ ragSource: { type: "project", projectId: "p" },+ reasoningEffort: "high",+ supportsTools: true,+ loadedContextLength: 32768,+ loadedIsGguf: true,+ },+ {+ checkpoint: "unsloth/new-model-GGUF",+ temperature: 1.1,+ topP: 0.5,+ systemPrompt: "chat B prompt",+ maxTokens: 2048,+ },+ );++ const run = withRunThreadScopedSettings(afterSwitchAndLoad, atSend);++ assert.equal(run.permissionMode, "ask");+ assert.equal(run.confirmToolCalls, true);+ assert.equal(run.toolsEnabled, false);+ assert.equal(run.ragEnabled, false);+ assert.deepEqual(run.ragSource, { type: "thread" });+ assert.equal(run.reasoningEffort, "low");+ assert.equal(run.params.systemPrompt, "chat A prompt");+ assert.equal(run.params.temperature, 0.2);+ assert.equal(run.params.topP, 0.9);+ assert.equal(run.params.checkpoint, "unsloth/new-model-GGUF");+ assert.equal(run.supportsTools, true);+ assert.equal(run.loadedContextLength, 32768);+ assert.equal(run.loadedIsGguf, true);+ assert.equal(run.params.maxTokens, 2048);+ assert.equal(run.activeThreadId, "chat-b");+ assert.equal(afterSwitchAndLoad.permissionMode, "auto");+ assert.equal(afterSwitchAndLoad.params.systemPrompt, "chat B prompt");+});++test("a chat-only full access grant keeps the mode it displaced", () => {+ const atSend = chatState(+ {+ permissionMode: "full",+ bypassPermissions: true,+ confirmToolCalls: false,+ chatFullAccessPreviousMode: "ask",+ },+ {},+ );+ const live = chatState({ activeThreadId: "chat-b", permissionMode: "off", confirmToolCalls: false }, {});++ const run = withRunThreadScopedSettings(live, atSend);++ assert.equal(run.permissionMode, "full");+ assert.equal(run.bypassPermissions, true);+ assert.equal(run.confirmToolCalls, false);+ assert.equal(run.chatFullAccessPreviousMode, "ask");+});++test("every thread-scoped key comes from the run's own chat", () => {+ const own: Record<string, unknown> = { params: {} };+ const live: Record<string, unknown> = { params: {} };+ for (const key of THREAD_SCOPED_SETTING_KEYS) {+ const target = isThreadScopedParamKey(key) ? "params" : null;+ const ownTarget = (target ? own.params : own) as Record<string, unknown>;+ const liveTarget = (target ? live.params : live) as Record<string, unknown>;+ ownTarget[key] = `own:${key}`;+ liveTarget[key] = `live:${key}`;+ }++ const run = withRunThreadScopedSettings(+ live as { params: object },+ own as { params: object },+ ) as Record<string, unknown> & { params: Record<string, unknown> };++ for (const key of THREAD_SCOPED_SETTING_KEYS) {+ const value = isThreadScopedParamKey(key) ? run.params[key] : run[key];+ assert.equal(value, `own:${key}`, key);+ }+ for (const key of THREAD_SCOPED_PARAM_KEYS) {+ assert.equal(Object.hasOwn(run, key), false, `${key} belongs under params`);+ }+});++test("chat A's Code-off under Full access holds after chat B picks Ask for approval", () => {+ const store = useChatRuntimeStore;+ store.setState({+ params: { ...store.getState().params, checkpoint: "unsloth/Qwen3-1.7B-GGUF" },+ supportsTools: true,+ });+ const call = (name: string, value: unknown) =>+ (store.getState()[name] as (value: unknown) => void)(value);+ call("setPermissionMode", "full");+ call("setCodeToolsEnabled", false);+ const atSend = store.getState();+ assert.equal(codeToolsOn(atSend), false, "chat A switched Code off");++ // Full access outlives the switch; picking Ask in chat B clears the decline.+ call("setPermissionMode", "ask");+ const live = store.getState();+ assert.equal(live.codeToolsDeclinedUnderFullAccess, false);++ const run = withRunThreadScopedSettings(live, atSend);+ assert.equal(run.permissionMode, "full");+ assert.equal(codeToolsOn(run), false);+});++// The adapter's post-wait block, lifted from source: importing the adapter drags in stores and network.+const ADAPTER = readSrc("features/chat/api/chat-adapter.ts");+const selectionStart = ADAPTER.indexOf(+ " const liveRuntime = useChatRuntimeStore.getState();",+);+const selectionEnd = ADAPTER.indexOf(" runtime = withChatFullAccess(", selectionStart);+assert.ok(selectionStart >= 0 && selectionEnd > selectionStart, "post-wait selection moved");+const selectRunRuntime = new Function(+ "useChatRuntimeStore",+ "queuedRunSettings",+ "queuedEmptyModelRuntime",+ "runtimeAtSend",+ "chatOwnedAtSend",+ "resolvedThreadId",+ "withRunThreadScopedSettings",+ "withLoadedModelOverrides",+ "mergeQueuedModelCapabilities",+ `let runtime;\n${ts.transpileModule(ADAPTER.slice(selectionStart, selectionEnd), {+ compilerOptions: { target: ts.ScriptTarget.ES2020 },+ }).outputText}\nreturn runtime;`,+) as (...args: unknown[]) => ReturnType<typeof chatState>;++function runAfterWait(+ live: ReturnType<typeof chatState>,+ atSend: ReturnType<typeof chatState>,+ resolvedThreadId: string | undefined,+ overrides: (run: never, live: never) => unknown = (run) => run,+ chatOwned: unknown = atSend,+) {+ return selectRunRuntime(+ { getState: () => live },+ null,+ null,+ atSend,+ chatOwned,+ resolvedThreadId,+ withRunThreadScopedSettings,+ overrides,+ () => {+ throw new Error("a direct send has no queued model capabilities");+ },+ );+}++const loadedB = (overrides: Record<string, unknown> = {}) =>+ chatState(+ { activeThreadId: "chat-b", permissionMode: "auto", supportsTools: true, ...overrides },+ { checkpoint: "unsloth/new-model-GGUF", systemPrompt: "chat B prompt", temperature: 1.1 },+ );++test("the adapter keeps the sent chat's settings when another chat is on screen", () => {+ const run = runAfterWait(loadedB(), chatState({}, {}), "chat-a");+ assert.equal(run.permissionMode, "ask");+ assert.equal(run.params.systemPrompt, "chat A prompt");+ assert.equal(run.params.temperature, 0.2);+ assert.equal(run.params.checkpoint, "unsloth/new-model-GGUF");+ assert.equal(run.supportsTools, true);+});++test("the adapter reads the live store while the sent chat is still on screen", () => {+ const live = loadedB({ activeThreadId: "chat-a" });+ assert.equal(runAfterWait(live, chatState({}, {}), "chat-a"), live);+ // A first send: no id on screen at send, then the new chat's own id.+ const firstSend = loadedB({ activeThreadId: "chat-new" });+ assert.equal(+ runAfterWait(firstSend, chatState({ activeThreadId: null }, {}), "chat-new"),+ firstSend,+ );+});++test("the adapter keeps the sent chat's settings while the shown chat's settings load", () => {+ const live = loadedB({ activeThreadId: "chat-a", threadScopedSettingsPending: true });+ const run = runAfterWait(live, chatState({}, {}), "chat-a");+ assert.equal(run.permissionMode, "ask");+ assert.equal(run.params.systemPrompt, "chat A prompt");+});++test("the adapter takes its send-time settings before the model wait", () => {+ const capture = ADAPTER.indexOf(+ " const runtimeAtSend = runtime;\n const chatOwnedAtSend = withChatOwnedSettings(runtime);",+ );+ const wait = ADAPTER.indexOf("await waitForModelReady(abortSignal);", capture);+ assert.ok(capture >= 0, "runtimeAtSend is captured");+ assert.ok(wait - capture < 400 && wait < selectionStart, "captured just before the wait");+});++test("chat A's tools follow its choice, not the clamp the outgoing model left", () => {+ const store = useChatRuntimeStore;+ const call = (name: string, value: unknown) =>+ (store.getState()[name] as (value: unknown) => void)(value);+ store.setState({ params: { ...store.getState().params, checkpoint: "tools-model" }, supportsTools: true });+ call("setPermissionMode", "ask");+ call("setToolsEnabled", true);+ call("setCodeToolsEnabled", true);+ store.setState({+ params: { ...store.getState().params, checkpoint: "plain-model" },+ supportsTools: false,+ ...resolveToolsEnabledOnLoad(false),+ });+ store.setState({ modelLoading: true });+ assert.equal(store.getState().toolsEnabled, false, "the outgoing model clamps the pill");+ const atSend = withChatOwnedSettings(store.getState());++ const loaded = {+ ...store.getState(),+ activeThreadId: "chat-b",+ modelLoading: false,+ supportsTools: true,+ params: { ...store.getState().params, checkpoint: "unsloth/new-model-GGUF" },+ } as unknown as ReturnType<typeof chatState>;+ const run = runAfterWait(loaded, atSend as never, "chat-a", withLoadedModelOverrides as never) as unknown as StoreState;+ assert.equal(run.toolsEnabled, true);+ assert.equal(codeToolsOn(run), true);++ const plain = { ...loaded, supportsTools: false } as typeof loaded;+ const offRun = runAfterWait(plain, atSend as never, "chat-a", withLoadedModelOverrides as never) as unknown as StoreState;+ assert.equal(offRun.toolsEnabled, false);+ assert.equal(offRun.codeToolsEnabled, false);+ store.setState({ modelLoading: false });+});++test("chat A runs at its own effort, not the outgoing model's pin", () => {+ const store = useChatRuntimeStore;+ store.setState({ settingsHydrated: true, modelLoading: false });+ (store.getState().applyThreadScopedSettings as (id: string, s: object) => void)("chat-a", {+ reasoningEffort: "low",+ });+ useModelReasoningEffortStore.setState({+ effortByModel: { "pinned-model": "high", "other-pinned-model": "medium" },+ });+ const reasoning = {+ supportsReasoning: true,+ reasoningStyle: "reasoning_effort",+ reasoningEffortLevels: ["low", "medium", "high"],+ };+ store.setState({+ ...reasoning,+ params: { ...store.getState().params, checkpoint: "pinned-model" },+ reasoningEffort: "high",+ modelLoading: true,+ });+ const atSend = store.getState();+ const chatOwned = withChatOwnedSettings(atSend);+ assert.equal(chatOwned.reasoningEffort, "low");++ const loaded = (checkpoint: string, reasoningEffort: string) =>+ ({+ ...store.getState(),+ ...reasoning,+ activeThreadId: "chat-b",+ modelLoading: false,+ reasoningEffort,+ params: { ...store.getState().params, checkpoint },+ }) as unknown as ReturnType<typeof chatState>;+ const run = (live: ReturnType<typeof chatState>) =>+ runAfterWait(live, atSend as never, "chat-a", withLoadedModelOverrides as never, chatOwned) as unknown as StoreState;+ assert.equal(run(loaded("unsloth/local-model", "high")).reasoningEffort, "low");+ assert.equal(run(loaded("other-pinned-model", "medium")).reasoningEffort, "medium");+ assert.equal(run(loaded("pinned-model", "low")).reasoningEffort, "high");+ store.setState({ modelLoading: false });+});++test("chat A's Think on a new model is what that model's load set", () => {+ const atSend = chatState({ reasoningEnabled: true }, {});+ const loaded = (reasoningEnabled: boolean) =>+ loadedB({ activeGgufVariant: "Q4_K_M", supportsReasoning: true, reasoningEnabled });+ const think = (reasoningEnabled: boolean) =>+ (runAfterWait(loaded(reasoningEnabled), atSend, "chat-a", withLoadedModelOverrides as never) as unknown as StoreState)+ .reasoningEnabled;+ // A Think pin, a model that cannot stop thinking, or a Think-off default: the load decided.+ assert.equal(think(true), true);+ assert.equal(think(false), false);+});++test("a load that leaves the model in place keeps chat A's effective values", () => {+ // Kimi: Think on suppressed Search without persisting it; the chat's record still has Search on.+ const model = { params: { checkpoint: "kimi::kimi-k2.6" }, activeGgufVariant: null };+ // A rolled-back load restores the variant as null where the send may have had none at all.+ const atSend = chatState(+ { reasoningEnabled: true, toolsEnabled: false, ...model, activeGgufVariant: undefined },+ model.params,+ );+ const chatOwned = chatState({ reasoningEnabled: true, toolsEnabled: true, ...model }, model.params);+ const live = chatState(+ { activeThreadId: "chat-b", reasoningEnabled: false, toolsEnabled: true, permissionMode: "auto", ...model },+ { ...model.params, systemPrompt: "chat B prompt" },+ );+ const run = runAfterWait(live, atSend, "chat-a", withLoadedModelOverrides as never, chatOwned) as unknown as StoreState;+ assert.equal(run.toolsEnabled, false);+ assert.equal(run.reasoningEnabled, true);+ assert.equal(run.permissionMode, "ask");+ assert.equal((run.params as unknown as { systemPrompt: string }).systemPrompt, "chat A prompt");+});++test("chat A's unowned sampling follows the new model's load; its own stays", () => {+ const store = useChatRuntimeStore;+ const apply = store.getState().applyThreadScopedSettings as (id: string, s: object) => void;+ const send = (thread: string, stored: object, recommended = 0.31) => {+ store.setState({+ settingsHydrated: true,+ modelLoading: true,+ activeThreadId: thread,+ params: { ...store.getState().params, checkpoint: "old-model", temperature: 0.6, topP: 0.95 },+ });+ apply(thread, stored);+ const atSend = store.getState();+ const chatOwned = withChatOwnedSettings(atSend);+ apply("chat-b", { temperature: 1.1 });+ store.setState({ activeThreadId: "chat-b", modelLoading: false });+ (store.getState().setParams as (p: object, o: object) => void)(+ { ...store.getState().params, checkpoint: "new-model", temperature: recommended, topP: 0.41 },+ { fromModelDefaults: true },+ );+ const live = store.getState() as unknown as ReturnType<typeof chatState>;+ assert.equal(live.params.temperature, 1.1, "chat B's own value is on screen");+ const run = runAfterWait(live, atSend as never, thread, withLoadedModelOverrides as never, chatOwned);+ store.setState({ modelLoading: false });+ return run.params;+ };+ // A legacy row stores other settings but no sampling (thread-scoped-sampling-compat C1b).+ const legacy = send("legacy-a", { permissionMode: "ask" });+ assert.equal(legacy.temperature, 0.31);+ assert.equal(legacy.topP, 0.41);+ // Remembered per-model values are not the load's when model memory is off.+ store.setState({ rememberParamsPerModel: false, paramsByModel: { "new-model": { temperature: 1.5 } } });+ assert.equal(send("off-a", { permissionMode: "ask" }).temperature, 0.31);+ store.setState({ rememberParamsPerModel: true, paramsByModel: {} });+ // Chat B already holds the recommendation, so the load moves no stored default.+ assert.equal(send("same-a", { permissionMode: "ask" }, 1.1).temperature, 1.1);+ const pinned = send("pinned-a", { permissionMode: "ask", temperature: 0.2 });+ assert.equal(pinned.temperature, 0.2);+ assert.equal(pinned.topP, 0.41);+});diff --git a/studio/frontend/tests/thread-scoped-pairing-invariants.test.ts b/studio/frontend/tests/thread-scoped-pairing-invariants.test.tsindex a583551e79d..12614194acd 100644--- a/studio/frontend/tests/thread-scoped-pairing-invariants.test.ts+++ b/studio/frontend/tests/thread-scoped-pairing-invariants.test.ts@@ -197,8 +197,10 @@ test("a tab-close write that could not be confirmed is replayed next session", (test("a model that forces thinking on does not erase a chat's stored preference", () => {const build = slice(store, "function buildThreadScopedSnapshot", "\n}");- assert.match(build, /activeThreadScopedSettings\?\.reasoningEnabled === false/);- assert.match(build, /reasoningAlwaysOn/);+ assert.match(build, /restoreChatOwnedValues\(threadId, settings\);/);+ const restore = slice(store, "function restoreChatOwnedValues", "\n}");+ assert.match(restore, /activeThreadScopedSettings\?\.reasoningEnabled === false/);+ assert.match(restore, /reasoningAlwaysOn/);});test("compare mode drops the thread-scoped state rather than keeping the last chat's", () => {@@ -491,9 +493,9 @@ test("a provider constraint does not rewrite what the chat stored", () => {assert.match(reasoning, /noteConstraintSuppressedThreadField\("reasoningEnabled"\)/);const tools = slice(store, "setToolsEnabled: (toolsEnabled, options)", "\n }),");assert.match(tools, /noteConstraintSuppressedThreadField\("toolsEnabled"\)/);- const build = slice(store, "function buildThreadScopedSnapshot", "\nconst THREAD_SETTINGS_REPLAY_KEY");- assert.match(build, /keepsStoredValueUnderConstraint\("reasoningEnabled", threadId, settings\)/);- assert.match(build, /keepsStoredValueUnderConstraint\("toolsEnabled", threadId, settings\)/);+ const restore = slice(store, "function restoreChatOwnedValues", "\nfunction buildThreadScopedSnapshot");+ assert.match(restore, /keepsStoredValueUnderConstraint\("reasoningEnabled", threadId, settings\)/);+ assert.match(restore, /keepsStoredValueUnderConstraint\("toolsEnabled", threadId, settings\)/);const keeps = slice(store, "function keepsStoredValueUnderConstraint", "\n}");assert.match(keeps, /!explicitlyEditedThreadFields\.has\(key\)/);const capture = slice(store, "function captureThreadScopedEdit", "\n}");@@ -509,8 +511,8 @@ test("the sampling params are read and applied through params", () => {);assert.equal(store.match(/readThreadScopedValue\(state, key\)/g)?.length,- 3,- "the snapshot, the held edit and the sameness check",+ 4,+ "the snapshot, the held edit, the sameness check and a send's pill choices",);assert.match(store, /paramsPatch\[key\] = value;/);assert.match(