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
fix(studio): preserve chat tool selections across model unloads (#13621)
Eyera91f8457ab330
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.tsindex 52727182529..73c56736aca 100644--- a/studio/frontend/scripts/check-bundle-budget.ts+++ b/studio/frontend/scripts/check-bundle-budget.ts@@ -110,8 +110,11 @@ export const BUDGET = {// adds 3,018 B raw / 905 B transfer (6,760,371 -> 6,763,389 B raw, 2,032,234 -> 2,033,139 B transfer// against 525f3ceae, one machine and build, Node 24, chunk count unchanged at 95). Lazy-loading its store// from both save sites cost 0.3 KB transfer more in chunk overhead, so the budget is raised by that growth.- transferBytes: 2_033_207,- rawBytes: 6_763_768,+ // Keeping the tool pills in step with the loaded model's tool support (#13621) adds 1.2 KB raw / 0.4 KB+ // transfer to the eager chat chunk (6,753,387 -> 6,754,540 B raw, 2,029,398 -> 2,029,795 B transfer+ // against 97ed9415f7, before #13649, one machine and build), raised by that.+ transferBytes: 2_033_604,+ rawBytes: 6_764_921,};// 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/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsxindex d488427e43d..71edbf7ca90 100644--- a/studio/frontend/src/components/assistant-ui/thread.tsx+++ b/studio/frontend/src/components/assistant-ui/thread.tsx@@ -6933,6 +6933,7 @@ const ComposerToolsMenu: FC<{// Match Search and Code: allow pre-selection before a local model loads.const researchDisabled =!researchAvailable ||+ (modelLoaded && !externalSelection && !supportsTools) ||(Boolean(externalSelection) &&providerModelSupportsStudioTools(selectedExternalProvider?.providerType,diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex ec63b74bb12..22388b00d3c 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -260,6 +260,7 @@ import {takeEffortDisplacedByPin,threadScopedOverride,resolvePreserveThinkingOnLoad,+ resolveToolsEnabledOnLoad,useChatRuntimeStore,} from "./stores/chat-runtime-store";import { wantsDownloadManagerStaging } from "./utils/model-download-staging";@@ -3195,6 +3196,8 @@ export function ChatPage({supports_preserve_thinking: providerSupportsPreserveThinking(provider?.providerType),}),supportsTools: supportsStudioToolsHere,+ deepResearchEnabled: resolveToolsEnabledOnLoad(supportsStudioToolsHere)+ .deepResearchEnabled,supportsBuiltinWebSearch,supportsBuiltinCodeExecution,supportsBuiltinImageGeneration,diff --git a/studio/frontend/src/features/chat/shared-composer.tsx b/studio/frontend/src/features/chat/shared-composer.tsxindex 8ed9edb5213..24ae76c152a 100644--- a/studio/frontend/src/features/chat/shared-composer.tsx+++ b/studio/frontend/src/features/chat/shared-composer.tsx@@ -225,6 +225,7 @@ import {reconcilePersistedGpuIds,resolveLoadedSpeculativeSettings,resolvePreserveThinkingOnLoad,+ resolveToolsEnabledOnLoad,persistGpuMemoryModeOnLoad,resolveSpeculativeSettingsForLoad,saveSpeculativeType,@@ -2115,6 +2116,9 @@ export function SharedComposer({supportsPreserveThinking: resp.supports_preserve_thinking ?? false,preserveThinking: resolvePreserveThinkingOnLoad(resp),supportsTools: resp.supports_tools ?? false,+ deepResearchEnabled: resolveToolsEnabledOnLoad(+ resp.supports_tools ?? false,+ ).deepResearchEnabled,kvCacheDtype: resp.cache_type_kv ?? null,loadedKvCacheDtype: resp.cache_type_kv ?? null,...mlxRuntimeStateFrom(resp),diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.tsindex 49436a060dd..422663faf54 100644--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts@@ -36,6 +36,7 @@ import {externalModelSupportsStudioTools,isExternalModelId,parseExternalModelId,+ providerModelSupportsStudioTools,} from "../external-providers";import { isOllamaManifestRef } from "../utils/qwen-sampling-table";import {@@ -850,7 +851,16 @@ function persistSetting(key: string, raw: string): void {}writeStorageValue(key, raw);};- if (mirrored && captureThreadScopedEdit(mirrored.field, writeGlobal)) return;+ if (+ mirrored &&+ captureThreadScopedEdit(+ mirrored.field,+ writeGlobal,+ isClampedPillKey(mirrored.field) ? raw === "true" : undefined,+ )+ ) {+ return;+ }writeGlobal();}@@ -868,11 +878,21 @@ let threadSettingsWriteThreadId: string | null = null;// leaves in the store by the time the debounce fires.let threadSettingsWriteSnapshot: ThreadScopedSettings | null = null;-/** Where a thread-scoped key's live value is: sampling under `params`, the rest fields. */+/** Where a thread-scoped key's value is: sampling under `params`, the rest fields. */function readThreadScopedValue(state: ChatRuntimeStore,key: ThreadScopedSettingKey,): unknown {+ if (isClampedPillKey(key)) {+ if (!state.modelLoading && !isPillClampedOff(state, key)) return state[key];+ return (+ pillOnRecord(key) ??+ (threadScopedSettingsThreadId === null+ ? loadOptionalBool(MIRRORED_SETTINGS[key].storageKey)+ : undefined) ??+ state[key]+ );+ }return isThreadScopedParamKey(key)? state.params[key]: (state as Record<string, unknown>)[key];@@ -911,6 +931,9 @@ export function threadScopedDefault<K extends ThreadScopedSettingKey>(export function threadScopedOverride<K extends ThreadScopedSettingKey>(key: K,): ThreadScopedSettings[K] | undefined {+ if (isClampedPillKey(key)) {+ return pillOverride(key) as ThreadScopedSettings[K] | undefined;+ }// Effort is captured before a model pin can overwrite the live value.if (key === "reasoningEffort" && activeThreadScopedSettings?.reasoningEffort !== undefined) {return activeThreadScopedSettings.reasoningEffort as ThreadScopedSettings[K];@@ -994,9 +1017,77 @@ const CLAMPED_PILL_KEYS = ["codeToolsEnabled","imageToolsEnabled","webFetchToolsEnabled",+ "deepResearchEnabled",] as const;type ClampedPillKey = (typeof CLAMPED_PILL_KEYS)[number];+function isClampedPillKey(key: string): key is ClampedPillKey {+ return CLAMPED_PILL_KEYS.some((candidate) => candidate === key);+}++function pillOverride(key: ClampedPillKey): boolean | undefined {+ const held = heldThreadScopedParamValue(key);+ if (typeof held === "boolean" || pendingSnapshotReadsLive(key)) {+ const live = useChatRuntimeStore.getState();+ if (!live.modelLoading && !isPillClampedOff(live, key)) return live[key];+ }+ if (typeof held === "boolean") return held;+ const stored = activeThreadScopedSettings?.[key];+ return typeof stored === "boolean" ? stored : undefined;+}++function pillOnRecord(key: ClampedPillKey): boolean | undefined {+ return (+ pillOverride(key) ??+ (threadScopedSettingsThreadId !== null+ ? globalThreadScopedDefaults?.[key]+ : undefined)+ );+}++function pendingSnapshotReadsLive(key: ClampedPillKey): boolean {+ return (+ threadSettingsWriteSnapshot === null &&+ threadSettingsWriteThreadId !== null &&+ threadSettingsWriteThreadId === threadScopedSettingsThreadId &&+ (explicitlyEditedThreadFields.has(key) ||+ !constraintSuppressedThreadFields.has(key) ||+ typeof activeThreadScopedSettings?.[key] !== "boolean")+ );+}++function pillPreference(key: ClampedPillKey): boolean {+ return (+ pillOnRecord(key) ??+ loadOptionalBool(MIRRORED_SETTINGS[key].storageKey) ??+ false+ );+}++function isPillClampedOff(+ state: ChatRuntimeStore,+ key: ClampedPillKey,+): boolean {+ if (key === "deepResearchEnabled") {+ return (+ state.incognito ||+ localCheckpointRefusesDeepResearch(state) ||+ externalCheckpointRefusesDeepResearch(state.params.checkpoint)+ );+ }+ if (!state.params.checkpoint || state.modelLoading) return false;+ const capable: Record<+ Exclude<ClampedPillKey, "deepResearchEnabled">,+ boolean+ > = {+ toolsEnabled: state.supportsTools || state.supportsBuiltinWebSearch,+ codeToolsEnabled: state.supportsTools || state.supportsBuiltinCodeExecution,+ imageToolsEnabled: state.supportsBuiltinImageGeneration,+ webFetchToolsEnabled: state.supportsBuiltinWebFetch,+ };+ return !capable[key];+}+/** Sampling keys share `params` with the model's recommendation, so a load in this window* would pin the model's value onto the chat. Captured at the edit; last entry wins. */function heldThreadScopedParamValue(key: string): unknown {@@ -1128,20 +1219,6 @@ function buildThreadScopedSnapshot() {settings.permissionMode = activeThreadScopedSettings.permissionMode;}- // Same for deep research, which apply() also holds back, or toggling any other pill erases- // the stored true. Unless the user just cleared it by enabling Search, Tools or Images.- if (- threadId === threadScopedSettingsThreadId &&- !explicitlyEditedThreadFields.has("deepResearchEnabled") &&- activeThreadScopedSettings?.deepResearchEnabled === true &&- settings.deepResearchEnabled !== true &&- (externalCheckpointRefusesDeepResearch(- useChatRuntimeStore.getState().params.checkpoint,- ) ||- useChatRuntimeStore.getState().incognito)- ) {- settings.deepResearchEnabled = true;- }// 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.if (@@ -1158,29 +1235,6 @@ function buildThreadScopedSnapshot(const onRecord = reasoningEffortOnRecord();if (onRecord !== undefined) settings.reasoningEffort = onRecord;}- // Same for every pill the model-selection pass clamps off without touching the snapshot:- // each pill's own capability rule is exactly when the user could not have done it.- if (threadId === threadScopedSettingsThreadId) {- const live = useChatRuntimeStore.getState();- const modelLoaded = !!live.params.checkpoint && !live.modelLoading;- const capable: Record<ClampedPillKey, boolean> = {- toolsEnabled: live.supportsTools || live.supportsBuiltinWebSearch,- codeToolsEnabled: live.supportsTools || live.supportsBuiltinCodeExecution,- imageToolsEnabled: live.supportsBuiltinImageGeneration,- webFetchToolsEnabled: live.supportsBuiltinWebFetch,- };- for (const key of CLAMPED_PILL_KEYS) {- if (- modelLoaded &&- !capable[key] &&- !explicitlyEditedThreadFields.has(key) &&- activeThreadScopedSettings?.[key] === true &&- settings[key] !== true- ) {- settings[key] = true;- }- }- }// 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.if (keepsStoredValueUnderConstraint("reasoningEnabled", threadId, settings)) {@@ -1706,11 +1760,16 @@ function restoreDefaultsOverCommittedEdits(if (!hasKeys(fields) && !hasKeys(params)) return;// setState, not setParams: these values are already the installation's, and the setter would// persist them back to it and to the loaded model's memory.- useChatRuntimeStore.setState((state) =>- hasKeys(params)+ useChatRuntimeStore.setState((state) => {+ for (const key of CLAMPED_PILL_KEYS) {+ if (key in fields) {+ fields[key] = fields[key] === true && !isPillClampedOff(state, key);+ }+ }+ return hasKeys(params)? ({ ...fields, params: { ...state.params, ...params } } as Partial<ChatRuntimeStore>)- : (fields as Partial<ChatRuntimeStore>),- );+ : (fields as Partial<ChatRuntimeStore>);+ });}/** What the user actually touched, read off the store, which still holds their edits. */@@ -1722,12 +1781,16 @@ function heldThreadScopedChanges(// Same reader the snapshot path uses: sampling keys sit under `params`, so a direct field// read returns undefined and the sanitizer drops them.for (const edit of held) {- edited[edit.field] = edit.field === "reasoningEffort" && edit.value !== undefined- ? edit.value- : readThreadScopedValue(- live,- edit.field as ThreadScopedSettingKey,- );+ edited[edit.field] =+ edit.value !== undefined &&+ (edit.field === "reasoningEffort" ||+ (isClampedPillKey(edit.field) &&+ (live.modelLoading || isPillClampedOff(live, edit.field))))+ ? edit.value+ : readThreadScopedValue(+ live,+ edit.field as ThreadScopedSettingKey,+ );}return sanitizeThreadScopedSettings(edited);}@@ -1803,6 +1866,12 @@ function captureThreadScopedEdit(reasoningEffort: value as ReasoningEffort,};}+ if (isClampedPillKey(field) && typeof value === "boolean") {+ activeThreadScopedSettings = {+ ...activeThreadScopedSettings,+ [field]: value,+ };+ }explicitlyEditedThreadFields.add(field);// Set by the user now, so the chat stores this rather than what it had before a constraint moved the same field.constraintSuppressedThreadFields.delete(field);@@ -1849,17 +1918,21 @@ export function loadOptionalBool(key: string): boolean | null {export function resolveToolsEnabledOnLoad(supportsTools: boolean): {toolsEnabled: boolean;codeToolsEnabled: boolean;+ deepResearchEnabled: boolean;} {- if (!supportsTools) return { toolsEnabled: false, codeToolsEnabled: false };+ if (!supportsTools) {+ return {+ toolsEnabled: false,+ codeToolsEnabled: false,+ deepResearchEnabled: false,+ };+ }return {- toolsEnabled:- threadScopedOverride("toolsEnabled") ??- loadOptionalBool(CHAT_TOOLS_ENABLED_KEY) ??- false,- codeToolsEnabled:- threadScopedOverride("codeToolsEnabled") ??- loadOptionalBool(CHAT_CODE_TOOLS_ENABLED_KEY) ??- false,+ deepResearchEnabled:+ !useChatRuntimeStore.getState().incognito &&+ pillPreference("deepResearchEnabled"),+ toolsEnabled: pillPreference("toolsEnabled"),+ codeToolsEnabled: pillPreference("codeToolsEnabled"),};}@@ -3325,7 +3398,7 @@ function noteModelDefaultsBeforeHydration(}/** Whether the checkpoint is an external model that cannot run deep research, as the composer- * rules it. An unresolved provider is left alone: refusing would drop a Codex preference. */+ * rules it. */function externalCheckpointRefusesDeepResearch(checkpoint: string | null | undefined,): boolean {@@ -3334,7 +3407,19 @@ function externalCheckpointRefusesDeepResearch(const provider = useExternalProvidersStore.getState().providers.find((candidate) => candidate.id === parsed.providerId);- return provider != null && provider.providerType !== "openai_codex";+ return (+ providerModelSupportsStudioTools(provider?.providerType, parsed.modelId) !==+ true+ );+}++function localCheckpointRefusesDeepResearch(state: ChatRuntimeStore): boolean {+ return Boolean(+ state.params.checkpoint &&+ !state.modelLoading &&+ !isExternalModelId(state.params.checkpoint) &&+ !state.supportsTools,+ );}/** Kimi's $web_search requires thinking disabled and the composer keeps the pills exclusive,@@ -3512,15 +3597,6 @@ function getHydratedSettingsState() {continue;}- // Only a local model or an openai_codex provider can run deep research, so a- // stored true must not arm the pill for any other external checkpoint.- if (- key === "deepResearchEnabled" &&- value === true &&- externalCheckpointRefusesDeepResearch(state.params.checkpoint)- ) {- continue;- }// A click made before this response landed is held for the open chat rather than written// globally, so it advances no mutation version and the server's value would replace it.if (isHeldThreadScopedField(key)) {@@ -3538,6 +3614,13 @@ function getHydratedSettingsState(value !== undefined &&scalarSettingMutationVersions[key] === versions.scalarSettings[key]) {+ if (+ key === "deepResearchEnabled" &&+ isPillClampedOff(state, "deepResearchEnabled")+ ) {+ nextState.deepResearchEnabled = false;+ continue;+ }(nextState as Record<ScalarSettingKey, unknown>)[key] = value;}}@@ -4881,12 +4964,9 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({// stale persisted one would race the freshly-loaded model.saveLastExternalCheckpoint(isExternalModelId(modelId) ? modelId : null);// Only disarm research for a connection that cannot drive it: the id prefix alone switched- // it off for capable providers, and saveBool would write that off for every browser.+ // it off for capable providers.const clampsDeepResearch =isExternalModelId(modelId) && !externalModelSupportsStudioTools(modelId);- if (clampsDeepResearch) {- saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false);- }// Clear stale per-turn usage on model change, or the relaxed external render gate shows it.const checkpointChanged = state.params.checkpoint !== modelId;// An interactive pick during hydration has no adoption signal either, so@@ -4947,8 +5027,10 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({}}const nextGgufVariant = ggufVariant ?? null;- const nextDeepResearchEnabled = clampsDeepResearch- ? false+ const nextDeepResearchEnabled = isExternalModelId(modelId)+ ? !clampsDeepResearch &&+ !state.incognito &&+ pillPreference("deepResearchEnabled"): state.deepResearchEnabled;const queuedSettingsChanged = shouldAdvanceQueuedSettingsEpoch({@@ -5001,7 +5083,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({: {}),// Switching to a provider that cannot run Unsloth's tool loop disables Deep Research; a// capable one keeps the user's choice.- ...(clampsDeepResearch ? { deepResearchEnabled: false } : {}),+ deepResearchEnabled: nextDeepResearchEnabled,};});// An external pick is the whole story for that model: no load and no status follows it, so nothing@@ -5031,12 +5113,19 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({// or the read would silently undo a click the user already saw.const heldFields = new Set<string>();let heldEffort: ReasoningEffort | undefined;+ const heldPills: Partial<Record<ClampedPillKey, boolean>> = {};if (threadId !== null && threadId === pendingPairingThreadId) {for (const edit of heldThreadScopedEdits) {heldFields.add(edit.field);if (edit.field === "reasoningEffort" && edit.value !== undefined) {heldEffort = edit.value as ReasoningEffort;}+ if (+ isClampedPillKey(edit.field) &&+ typeof edit.value === "boolean"+ ) {+ heldPills[edit.field] = edit.value;+ }}heldThreadScopedEdits = [];pendingPairingThreadId = null;@@ -5110,7 +5199,21 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({for (const key of THREAD_SCOPED_SETTING_KEYS) {// The user set this one while the read was in flight, so it wins over what came back.if (heldFields.has(key)) {- if (key === "reasoningEffort" && heldEffort !== undefined) {+ if (isClampedPillKey(key) && heldPills[key] !== undefined) {+ const value =+ state.modelLoading || isPillClampedOff(state, key)+ ? heldPills[key]+ : state[key];+ applied[key] = value;+ activeThreadScopedSettings = {+ ...activeThreadScopedSettings,+ [key]: value,+ };+ const enabled = value && !isPillClampedOff(state, key);+ if (state[key] !== enabled) {+ target[key] = enabled;+ }+ } else if (key === "reasoningEffort" && heldEffort !== undefined) {applied[key] = heldEffort;activeThreadScopedSettings = { ...activeThreadScopedSettings, reasoningEffort: heldEffort };if (pinOwnsLiveReasoningEffort(state)) effortDisplacedByPin = heldEffort;@@ -5130,15 +5233,6 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({if (underneath !== "full") applied[key] = underneath;continue;}- // setCheckpoint clears deep research for external models in the store only, so a stored true- // comes back and fails every send. openai_codex is the composer's own exception.- if (- key === "deepResearchEnabled" &&- (externalCheckpointRefusesDeepResearch(state.params.checkpoint) ||- state.incognito)- ) {- continue;- }// A key the snapshot omits falls back to the defaults, not to the outgoing chat's value.const value = firstSetThreadScopedValue(stored?.[key],@@ -5146,6 +5240,13 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({);if (value === undefined) continue;applied[key] = value;+ if (isClampedPillKey(key)) {+ const enabled = value === true && !isPillClampedOff(state, key);+ if (state[key] !== enabled) {+ target[key] = enabled;+ }+ continue;+ }// The pin outranks the chat for as long as its model is selected, so the incoming level// stays the chat's on record, recorded just above, but must not land in the store over// the pin. Same shape as the "full" case: recorded, not applied.@@ -5174,7 +5275,7 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({// thread switch, so the restore drops thinking as clicking the Search pill does.if (isKimiCheckpoint(state.params.checkpoint) &&- (applied.toolsEnabled ?? state.toolsEnabled) === true &&+ (target.toolsEnabled ?? state.toolsEnabled) === true &&(applied.reasoningEnabled ?? state.reasoningEnabled) === true) {applied.reasoningEnabled = false;@@ -5218,7 +5319,6 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({clearCheckpoint: () => {// Mirror setCheckpoint's persistence: dropping the checkpoint must also clear any stored external selection.saveLastExternalCheckpoint(null);- saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false);const restoredEffort = pinHoldsLiveEffort()? takeEffortDisplacedByPin(): null;@@ -5258,14 +5358,12 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({supportsBuiltinCodeExecution: false,supportsBuiltinImageGeneration: false,supportsBuiltinWebFetch: false,- toolsEnabled: false,- codeToolsEnabled: false,- imageToolsEnabled: false,- deepResearchEnabled: false,- mcpEnabledForChat: false,- webFetchToolsEnabled: false,- // Only the per-session enable pill resets; source/mode/top_k persist.- ragEnabled: false,+ toolsEnabled: pillPreference("toolsEnabled"),+ codeToolsEnabled: pillPreference("codeToolsEnabled"),+ imageToolsEnabled: pillPreference("imageToolsEnabled"),+ webFetchToolsEnabled: pillPreference("webFetchToolsEnabled"),+ deepResearchEnabled:+ !state.incognito && pillPreference("deepResearchEnabled"),toolStatusByThreadId: {},toolLiveOutput: {},toolFullOutput: {},@@ -5485,6 +5583,12 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({}),setDeepResearchEnabled: (deepResearchEnabled) =>set((state) => {+ if (+ deepResearchEnabled &&+ isPillClampedOff(state, "deepResearchEnabled")+ ) {+ return state;+ }saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, deepResearchEnabled);// The level this chat carries, or the global one when it carries none.const permissionMode =diff --git a/studio/frontend/tests/chat-tool-selection-lifecycle.test.ts b/studio/frontend/tests/chat-tool-selection-lifecycle.test.tsnew file mode 100644index 00000000000..7aa9f619906--- /dev/null+++ b/studio/frontend/tests/chat-tool-selection-lifecycle.test.ts@@ -0,0 +1,785 @@+import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import { register } from "node:module";+import test, { type TestContext } from "node:test";+import type * as chatRuntime from "../src/features/chat/stores/chat-runtime-store.ts";+import { installLocalStorageFake } from "./helpers/kit.ts";++const { store: storage } = installLocalStorageFake();+register("./thread-sampling-resolver.mjs", import.meta.url);++const { settingsHttp } = await import("./helpers/store-stubs/settings-http.ts");+const { threadRows } = await import(+ "./helpers/store-stubs/chat-history-storage.ts"+);+const { useExternalProvidersStore } = await import(+ "../src/features/chat/stores/external-providers-store.ts"+);+const { setProviderModelCapabilities } = await import(+ "../src/features/chat/external-providers.ts"+);+type RuntimeModule = typeof chatRuntime;+const runtimeUrl = new URL(+ "../src/features/chat/stores/chat-runtime-store.ts",+ import.meta.url,+).href;+const THREAD = "tool-selection-chat";+const MODEL = "unsloth/tool-capable-model";+const LOCAL_TOOL_KEYS = [+ "toolsEnabled",+ "codeToolsEnabled",+ "mcpEnabledForChat",+ "ragEnabled",+] as const;+const TOOL_KEYS = [+ ...LOCAL_TOOL_KEYS,+ "imageToolsEnabled",+ "webFetchToolsEnabled",+] as const;++async function boot(t: TestContext, hydrate = true): Promise<RuntimeModule> {+ storage.clear();+ storage.set("unsloth_chat_settings_imported_to_studio_db", "true");+ settingsHttp.settings = { rememberParamsPerModel: false };+ settingsHttp.puts.length = 0;+ threadRows.reset();+ useExternalProvidersStore.setState({ providers: [] });+ const runtime = (await import(+ `${runtimeUrl}?scenario=${encodeURIComponent(t.name)}`+ )) as RuntimeModule;+ t.after(async () => {+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ await runtime.flushPendingChatSettings();+ await runtime.awaitStartedThreadScopedSettingsWrites();+ });+ const store = runtime.useChatRuntimeStore;+ if (hydrate) await store.getState().hydratePersistedSettings();+ store.getState().setCheckpoint(MODEL, null);+ store.setState({ supportsTools: true });+ return runtime;+}++for (const scope of ["defaults", "chat", "pending chat"] as const) {+ for (const search of [false, true]) {+ for (const code of [false, true]) {+ test(`${scope} remembers Search=${search} and Code=${code} across unload and reload`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ const original = { toolsEnabled: !search, codeToolsEnabled: !code };+ if (scope !== "defaults") {+ store.getState().setActiveThreadId(THREAD);+ if (scope === "pending chat") {+ runtime.beginThreadScopedPairing(THREAD);+ } else {+ store.getState().applyThreadScopedSettings(THREAD, original);+ }+ }+ store.getState().setToolsEnabled(search);+ store.getState().setCodeToolsEnabled(code);+ store.getState().setImageToolsEnabled(search);+ store.getState().setWebFetchToolsEnabled(code);+ store.getState().setMcpEnabledForChat(search);+ store.getState().setRagEnabled(code);+ const expected = Object.fromEntries(+ TOOL_KEYS.map((key) => [key, store.getState()[key]]),+ );++ store.getState().clearCheckpoint();+ assert.equal(store.getState().params.checkpoint, "");+ assert.equal(store.getState().supportsTools, false);+ for (const key of TOOL_KEYS) {+ assert.equal(store.getState()[key], expected[key], key);+ }++ if (scope === "pending chat") {+ store.getState().applyThreadScopedSettings(THREAD, original);+ }+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ await runtime.flushPendingChatSettings();+ if (scope !== "defaults") {+ for (const key of TOOL_KEYS) {+ assert.equal(+ threadRows.rows.get(THREAD)?.[key],+ expected[key],+ key,+ );+ }+ assert.equal(storage.get("unsloth_chat_tools_enabled"), undefined);+ assert.equal(+ storage.get("unsloth_chat_code_tools_enabled"),+ undefined,+ );+ }++ store.getState().setCheckpoint(MODEL, null);+ store.setState({+ supportsTools: true,+ ...runtime.resolveToolsEnabledOnLoad(true),+ });+ for (const key of LOCAL_TOOL_KEYS) {+ assert.equal(store.getState()[key], expected[key], key);+ }+ assert.deepEqual(runtime.resolveToolsEnabledOnLoad(false), {+ toolsEnabled: false,+ codeToolsEnabled: false,+ deepResearchEnabled: false,+ });+ });+ }+ }+}++test("unloading does not overwrite a settled chat or its defaults", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, {+ toolsEnabled: true,+ codeToolsEnabled: true,+ });+ await runtime.flushPendingChatSettings();+ const writesBeforeUnload = settingsHttp.puts.length;++ store.getState().clearCheckpoint();+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ await runtime.flushPendingChatSettings();+ assert.equal(threadRows.writes.length, 0);+ assert.equal(settingsHttp.puts.length, writesBeforeUnload);++ store.getState().setPermissionMode("ask");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.toolsEnabled, true);+ assert.equal(threadRows.rows.get(THREAD)?.codeToolsEnabled, true);++ store.getState().setActiveThreadId(null);+ store.getState().applyThreadScopedSettings(null, null);+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().codeToolsEnabled, false);+ store.getState().setActiveThreadId(THREAD);+ store+ .getState()+ .applyThreadScopedSettings(THREAD, threadRows.rows.get(THREAD)!);+ assert.equal(store.getState().toolsEnabled, true);+ assert.equal(store.getState().codeToolsEnabled, true);+});++test("unloading preserves armed research without changing tool exclusivity", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setToolsEnabled(true);+ store.getState().setCodeToolsEnabled(true);+ store.getState().setDeepResearchEnabled(true);+ store.getState().clearCheckpoint();+ assert.equal(store.getState().deepResearchEnabled, true);+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().codeToolsEnabled, false);+ assert.equal(storage.get("unsloth_chat_deep_research_enabled"), "true");++ store.getState().setCodeToolsEnabled(true);+ assert.equal(store.getState().deepResearchEnabled, false);+});++for (const scope of ["defaults", "chat", "pending chat"] as const) {+ test(`${scope} disables Research for an incompatible local model and restores its preference`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ if (scope !== "defaults") {+ store.getState().setActiveThreadId(THREAD);+ if (scope === "pending chat") {+ runtime.beginThreadScopedPairing(THREAD);+ } else {+ store+ .getState()+ .applyThreadScopedSettings(THREAD, { deepResearchEnabled: false });+ }+ }+ store.getState().setDeepResearchEnabled(true);+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({+ supportsTools: false,+ ...runtime.resolveToolsEnabledOnLoad(false),+ });+ assert.equal(store.getState().deepResearchEnabled, false);+ store.getState().setDeepResearchEnabled(true);+ assert.equal(store.getState().deepResearchEnabled, false);++ if (scope === "pending chat") {+ store+ .getState()+ .applyThreadScopedSettings(THREAD, { deepResearchEnabled: false });+ }+ store.getState().setPermissionMode("ask");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(store.getState().deepResearchEnabled, false);+ if (scope !== "defaults") {+ assert.equal(threadRows.rows.get(THREAD)?.deepResearchEnabled, true);+ store+ .getState()+ .applyThreadScopedSettings(THREAD, threadRows.rows.get(THREAD)!);+ assert.equal(store.getState().deepResearchEnabled, false);+ } else {+ assert.equal(storage.get("unsloth_chat_deep_research_enabled"), "true");+ }++ store.getState().clearCheckpoint();+ store.getState().setCheckpoint(MODEL, null);+ store.setState({+ supportsTools: true,+ ...runtime.resolveToolsEnabledOnLoad(true),+ });+ assert.equal(store.getState().deepResearchEnabled, true);+ store.getState().setDeepResearchEnabled(false);+ store.setState(runtime.resolveToolsEnabledOnLoad(true));+ assert.equal(store.getState().deepResearchEnabled, false);+ });+}++test("late settings hydration cannot arm Research on an incompatible local model", async (t) => {+ const runtime = await boot(t, false);+ const store = runtime.useChatRuntimeStore;+ settingsHttp.settings = { deepResearchEnabled: true };+ store.setState({+ supportsTools: false,+ ...runtime.resolveToolsEnabledOnLoad(false),+ });+ await store.getState().hydratePersistedSettings();+ assert.equal(store.getState().deepResearchEnabled, false);+ assert.equal(storage.get("unsloth_chat_deep_research_enabled"), "true");+ store.setState({+ supportsTools: true,+ ...runtime.resolveToolsEnabledOnLoad(true),+ });+ assert.equal(store.getState().deepResearchEnabled, true);+});++test("suppressed Research remains scoped to its chat", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setActiveThreadId(THREAD);+ store+ .getState()+ .applyThreadScopedSettings(THREAD, { deepResearchEnabled: true });+ store.setState({+ supportsTools: false,+ ...runtime.resolveToolsEnabledOnLoad(false),+ });+ store.getState().setActiveThreadId("other-chat");+ store+ .getState()+ .applyThreadScopedSettings("other-chat", { deepResearchEnabled: false });+ store.setState({+ supportsTools: true,+ ...runtime.resolveToolsEnabledOnLoad(true),+ });+ assert.equal(store.getState().deepResearchEnabled, false);+ store.getState().setActiveThreadId(THREAD);+ store+ .getState()+ .applyThreadScopedSettings(THREAD, { deepResearchEnabled: true });+ assert.equal(store.getState().deepResearchEnabled, true);+ store.getState().setToolsEnabled(true);+ store.setState(runtime.resolveToolsEnabledOnLoad(true));+ assert.equal(store.getState().toolsEnabled, true);+ assert.equal(store.getState().deepResearchEnabled, false);+});++const CLAMPED_TOOLS = [+ ["toolsEnabled", "setToolsEnabled"],+ ["codeToolsEnabled", "setCodeToolsEnabled"],+ ["imageToolsEnabled", "setImageToolsEnabled"],+ ["webFetchToolsEnabled", "setWebFetchToolsEnabled"],+] as const;++for (const [key, setter] of CLAMPED_TOOLS) {+ for (const scope of ["pending chat", "pending save", "saved chat"] as const) {+ test(`${key} survives capability suppression with a ${scope}`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setActiveThreadId(THREAD);+ if (scope === "pending chat") {+ runtime.beginThreadScopedPairing(THREAD);+ } else {+ store.getState().applyThreadScopedSettings(THREAD, { [key]: false });+ }+ store.getState()[setter](true);+ if (scope === "saved chat") {+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ }+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({ [key]: false, supportsTools: false });+ if (scope === "pending chat") {+ store.getState().applyThreadScopedSettings(THREAD, { [key]: false });+ }+ store.getState().clearCheckpoint();+ store.getState().setPermissionMode("ask");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.[key], true);+ assert.equal(runtime.threadScopedOverride(key), true);++ store.getState()[setter](false);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.[key], false);+ });+ }++ test(`${key} keeps the default when a chat opens under an incompatible model`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState()[setter](true);+ store.setState({ [key]: false, supportsTools: false });+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, null);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.[key], true);+ });+}++test("Research can be selected while model capabilities are still loading", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({ supportsTools: false });+ const lease = store.getState().beginModelLoading("loading");+ assert.ok(lease !== null);+ store.getState().setDeepResearchEnabled(true);+ store.getState().setCheckpoint(MODEL, null);+ store.setState({ supportsTools: true, ...runtime.resolveToolsEnabledOnLoad(true) });+ store.getState().endModelLoading(lease);+ assert.equal(store.getState().deepResearchEnabled, true);+});++test("Research hydration keeps the global default separate from a held chat edit", async (t) => {+ const runtime = await boot(t, false);+ const store = runtime.useChatRuntimeStore;+ settingsHttp.settings = { deepResearchEnabled: true };+ store.setState({ supportsTools: false });+ store.getState().setActiveThreadId(THREAD);+ runtime.beginThreadScopedPairing(THREAD);+ store.getState().setDeepResearchEnabled(false);+ await store.getState().hydratePersistedSettings();+ store.getState().applyThreadScopedSettings(THREAD, null);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.deepResearchEnabled, false);+ assert.equal(runtime.threadScopedDefault("deepResearchEnabled"), true);+ store.getState().setActiveThreadId(null);+ store.getState().applyThreadScopedSettings(null, null);+ store.setState({ supportsTools: true, ...runtime.resolveToolsEnabledOnLoad(true) });+ assert.equal(store.getState().deepResearchEnabled, true);+});++for (const scope of ["defaults", "chat"] as const) {+ test(`${scope} restores Research across incompatible and compatible external models`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.getState().setProviders([{+ id: "tools-provider",+ providerType: "vllm",+ name: "Tool capability fixture",+ baseUrl: "http://localhost:8000/v1",+ models: ["capable", "unsupported"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ setProviderModelCapabilities("vllm", {+ capable: { studio_tools: true },+ unsupported: { studio_tools: false },+ });+ if (scope === "chat") {+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, { deepResearchEnabled: false });+ }+ store.getState().setDeepResearchEnabled(true);+ store.getState().setCheckpoint("external::tools-provider::unsupported", null);+ assert.equal(store.getState().deepResearchEnabled, false);+ store.getState().setPermissionMode("ask");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(+ scope === "chat"+ ? threadRows.rows.get(THREAD)?.deepResearchEnabled+ : storage.get("unsloth_chat_deep_research_enabled") === "true",+ true,+ );+ store.getState().setCheckpoint("external::tools-provider::capable", null);+ assert.equal(store.getState().deepResearchEnabled, true);+ if (scope === "chat") {+ store.getState().applyThreadScopedSettings(THREAD, threadRows.rows.get(THREAD)!);+ assert.equal(store.getState().deepResearchEnabled, true);+ }+ store.getState().setDeepResearchEnabled(false);+ store.getState().setCheckpoint("external::tools-provider::unsupported", null);+ store.getState().setCheckpoint("external::tools-provider::capable", null);+ assert.equal(store.getState().deepResearchEnabled, false);+ });+}++for (const scope of ["defaults", "chat", "pending chat"] as const) {+ test(`${scope} restores Research when switching from an incompatible local model to Codex`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.getState().setProviders([{+ id: "codex",+ providerType: "openai_codex",+ name: "Codex fixture",+ baseUrl: "https://chatgpt.com/backend-api/codex",+ models: ["gpt-5.4"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ setProviderModelCapabilities("openai_codex", {+ "gpt-5.4": { studio_tools: true },+ });+ if (scope !== "defaults") {+ store.getState().setActiveThreadId(THREAD);+ if (scope === "pending chat") {+ runtime.beginThreadScopedPairing(THREAD);+ } else {+ store.getState().applyThreadScopedSettings(THREAD, { deepResearchEnabled: false });+ }+ }+ store.getState().setDeepResearchEnabled(true);+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({ supportsTools: false, ...runtime.resolveToolsEnabledOnLoad(false) });+ assert.equal(store.getState().deepResearchEnabled, false);+ store.getState().setCheckpoint("external::codex::gpt-5.4", null);+ assert.equal(store.getState().deepResearchEnabled, true);+ if (scope === "pending chat") {+ store.getState().applyThreadScopedSettings(THREAD, { deepResearchEnabled: false });+ assert.equal(store.getState().deepResearchEnabled, true);+ }+ });+}++test("an unresolved external provider preserves Research until its capabilities arrive", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setDeepResearchEnabled(true);+ store.getState().setCheckpoint("external::late-provider::research-model", null);+ assert.equal(store.getState().deepResearchEnabled, false);+ assert.equal(storage.get("unsloth_chat_deep_research_enabled"), "true");+ store.getState().setDeepResearchEnabled(true);+ assert.equal(store.getState().deepResearchEnabled, false);+ useExternalProvidersStore.getState().setProviders([{+ id: "late-provider",+ providerType: "vllm",+ name: "Late provider fixture",+ baseUrl: "http://localhost:8000/v1",+ models: ["research-model"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ setProviderModelCapabilities("vllm", { "research-model": { studio_tools: true } });+ store.setState({ supportsTools: true, ...runtime.resolveToolsEnabledOnLoad(true) });+ assert.equal(store.getState().deepResearchEnabled, true);+});++test("a temporary chat keeps Research off when another tab stores it on", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.getState().setProviders([{+ id: "codex",+ providerType: "openai_codex",+ name: "Codex fixture",+ baseUrl: "https://chatgpt.com/backend-api/codex",+ models: ["gpt-5.4"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ setProviderModelCapabilities("openai_codex", {+ "gpt-5.4": { studio_tools: true },+ });+ store.getState().setIncognito(true);+ storage.set("unsloth_chat_deep_research_enabled", "true");+ assert.equal(runtime.resolveToolsEnabledOnLoad(true).deepResearchEnabled, false);+ store.getState().setCheckpoint("external::codex::gpt-5.4", null);+ assert.equal(store.getState().deepResearchEnabled, false);+ store.getState().clearCheckpoint();+ assert.equal(store.getState().deepResearchEnabled, false);+});++test("Kimi keeps Search off and Thinking on when a chat opens without stored pills", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setToolsEnabled(true);+ assert.equal(storage.get("unsloth_chat_tools_enabled"), "true");+ useExternalProvidersStore.getState().setProviders([{+ id: "kimi",+ providerType: "kimi",+ name: "Kimi fixture",+ baseUrl: "https://api.moonshot.ai/v1",+ models: ["kimi-k2.5"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ store.getState().setCheckpoint("external::kimi::kimi-k2.5", null);+ store.setState({+ supportsTools: false,+ supportsBuiltinWebSearch: true,+ toolsEnabled: false,+ reasoningEnabled: true,+ });+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, null);+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().reasoningEnabled, true);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.toolsEnabled, false);+ assert.equal(threadRows.rows.get(THREAD)?.reasoningEnabled, true);+ store.getState().setActiveThreadId(null);+ store.getState().applyThreadScopedSettings(null, null);+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().reasoningEnabled, true);+ assert.equal(storage.get("unsloth_chat_tools_enabled"), "true");+});++test("unloading drops a Search pill the provider lit without a stored preference", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.setState({ toolsEnabled: true, codeToolsEnabled: true });+ assert.equal(storage.get("unsloth_chat_tools_enabled"), undefined);+ store.getState().clearCheckpoint();+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().codeToolsEnabled, false);+ store.getState().setToolsEnabled(true);+ store.getState().clearCheckpoint();+ assert.equal(store.getState().toolsEnabled, true);+});++test("a held edit committed on leave cannot re-arm Research on an incompatible local model", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setDeepResearchEnabled(true);+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({ supportsTools: false, ...runtime.resolveToolsEnabledOnLoad(false) });+ assert.equal(store.getState().deepResearchEnabled, false);+ store.getState().setActiveThreadId(THREAD);+ runtime.beginThreadScopedPairing(THREAD);+ store.getState().setMcpEnabledForChat(true);+ store.getState().setActiveThreadId(null);+ const commit = runtime.commitHeldThreadScopedEditsToTheirThread();+ assert.equal(store.getState().deepResearchEnabled, false);+ assert.equal(store.getState().mcpEnabledForChat, false);+ await commit;+ assert.equal(threadRows.rows.get(THREAD)?.deepResearchEnabled, false);+ assert.equal(threadRows.rows.get(THREAD)?.mcpEnabledForChat, true);+ assert.equal(storage.get("unsloth_chat_deep_research_enabled"), "true");+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint(MODEL, null);+ store.setState({ supportsTools: true, ...runtime.resolveToolsEnabledOnLoad(true) });+ assert.equal(store.getState().deepResearchEnabled, true);+});++test("a chat's stored Search and Code stay off in the composer under a model without tools", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({ supportsTools: false, ...runtime.resolveToolsEnabledOnLoad(false) });+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, {+ toolsEnabled: true,+ codeToolsEnabled: true,+ });+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().codeToolsEnabled, false);+ assert.equal(runtime.threadScopedOverride("toolsEnabled"), true);+ store.getState().setPermissionMode("ask");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.toolsEnabled, true);+ assert.equal(threadRows.rows.get(THREAD)?.codeToolsEnabled, true);+ store.getState().clearCheckpoint();+ assert.equal(store.getState().toolsEnabled, true);+ store.getState().setCheckpoint(MODEL, null);+ store.setState({ supportsTools: true, ...runtime.resolveToolsEnabledOnLoad(true) });+ assert.equal(store.getState().toolsEnabled, true);+ assert.equal(store.getState().codeToolsEnabled, true);+});++for (const pills of [+ { toolsEnabled: true, codeToolsEnabled: true },+ { deepResearchEnabled: true },+] as const) {+ test(`a snapshot written while a model loads keeps ${Object.keys(pills).join(" and ")} on the chat`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().clearCheckpoint();+ store.getState().setCheckpoint("unsloth/no-tools", null);+ store.setState({ supportsTools: false, ...runtime.resolveToolsEnabledOnLoad(false) });+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, pills);+ for (const key of Object.keys(pills) as (keyof typeof pills)[]) {+ assert.equal(store.getState()[key], false, key);+ }+ const lease = store.getState().beginModelLoading("loading");+ assert.ok(lease !== null);+ store.getState().setPermissionMode("ask");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ store.setState({ supportsTools: false, ...runtime.resolveToolsEnabledOnLoad(false) });+ store.getState().setPermissionMode("auto");+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ for (const key of Object.keys(pills) as (keyof typeof pills)[]) {+ assert.equal(threadRows.rows.get(THREAD)?.[key], true, key);+ assert.equal(runtime.threadScopedOverride(key), true, key);+ }+ store.getState().setCheckpoint(MODEL, null);+ store.setState({ supportsTools: true, ...runtime.resolveToolsEnabledOnLoad(true) });+ store.getState().endModelLoading(lease);+ for (const key of Object.keys(pills) as (keyof typeof pills)[]) {+ assert.equal(store.getState()[key], true, key);+ }+ });+}++for (const leave of [false, true]) {+ test(`Kimi keeps a Thinking click over a held Search click when the chat ${leave ? "is left" : "opens"}`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.getState().setProviders([{+ id: "kimi",+ providerType: "kimi",+ name: "Kimi fixture",+ baseUrl: "https://api.moonshot.ai/v1",+ models: ["kimi-k2.5"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ store.getState().setCheckpoint("external::kimi::kimi-k2.5", null);+ store.setState({+ supportsTools: false,+ supportsBuiltinWebSearch: true,+ toolsEnabled: false,+ reasoningEnabled: false,+ });+ store.getState().setActiveThreadId(THREAD);+ runtime.beginThreadScopedPairing(THREAD);+ store.getState().setToolsEnabled(true);+ store.getState().setReasoningEnabled(true);+ store.getState().setToolsEnabled(false, { persist: false });+ if (leave) {+ store.getState().setActiveThreadId(null);+ await runtime.commitHeldThreadScopedEditsToTheirThread();+ } else {+ store.getState().applyThreadScopedSettings(THREAD, null);+ assert.equal(store.getState().toolsEnabled, false);+ assert.equal(store.getState().reasoningEnabled, true);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ }+ assert.equal(threadRows.rows.get(THREAD)?.toolsEnabled, false);+ assert.equal(threadRows.rows.get(THREAD)?.reasoningEnabled, true);+ });+}++for (const saved of [false, true]) {+ test(`Kimi's Thinking click after ${saved ? "a saved" : "an unsaved"} Search click leaves Search ${saved ? "on" : "off"} for the next model`, async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.getState().setProviders([{+ id: "kimi",+ providerType: "kimi",+ name: "Kimi fixture",+ baseUrl: "https://api.moonshot.ai/v1",+ models: ["kimi-k2.5"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ store.getState().setCheckpoint("external::kimi::kimi-k2.5", null);+ store.setState({+ supportsTools: false,+ supportsBuiltinWebSearch: true,+ toolsEnabled: false,+ reasoningEnabled: true,+ });+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, {+ toolsEnabled: false,+ reasoningEnabled: true,+ });+ store.getState().setToolsEnabled(true);+ store.getState().setReasoningEnabled(false, { persist: false });+ if (saved) await runtime.awaitThreadScopedSettingsWrite(THREAD);+ store.getState().setReasoningEnabled(true);+ store.getState().setToolsEnabled(false, { persist: false });+ assert.equal(runtime.threadScopedOverride("toolsEnabled"), saved);+ store.getState().clearCheckpoint();+ assert.equal(store.getState().toolsEnabled, saved);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.toolsEnabled, saved);+ });+}++test("enabling Search on a chat whose stored Research is clamped off clears the stored Research", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.getState().setProviders([{+ id: "search-only",+ providerType: "vllm",+ name: "Search-only fixture",+ baseUrl: "http://localhost:8000/v1",+ models: ["no-studio-tools"],+ createdAt: 0,+ updatedAt: 0,+ }]);+ setProviderModelCapabilities("vllm", { "no-studio-tools": { studio_tools: false } });+ store.getState().setCheckpoint("external::search-only::no-studio-tools", null);+ store.setState({ supportsTools: false, supportsBuiltinWebSearch: true });+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, { deepResearchEnabled: true });+ assert.equal(store.getState().deepResearchEnabled, false);+ assert.equal(runtime.threadScopedOverride("deepResearchEnabled"), true);+ store.getState().setToolsEnabled(true);+ await runtime.awaitThreadScopedSettingsWrite(THREAD);+ assert.equal(threadRows.rows.get(THREAD)?.toolsEnabled, true);+ assert.equal(threadRows.rows.get(THREAD)?.deepResearchEnabled, false);+ assert.equal(runtime.threadScopedOverride("deepResearchEnabled"), false);+});++test("Research follows the providers store when the browser mirror is empty", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ useExternalProvidersStore.setState({+ providers: [{+ id: "codex",+ providerType: "openai_codex",+ name: "Codex fixture",+ baseUrl: "https://chatgpt.com/backend-api/codex",+ models: ["gpt-5.4"],+ createdAt: 0,+ updatedAt: 0,+ }],+ });+ setProviderModelCapabilities("openai_codex", {+ "gpt-5.4": { studio_tools: true },+ });+ assert.equal(storage.get("unsloth_chat_external_providers"), undefined);+ store.getState().setCheckpoint("external::codex::gpt-5.4", null);+ store.getState().setDeepResearchEnabled(true);+ assert.equal(store.getState().deepResearchEnabled, true);+ assert.equal(runtime.resolveToolsEnabledOnLoad(true).deepResearchEnabled, true);+});++test("a chat stored before the pill keys has no Search override of its own", async (t) => {+ const runtime = await boot(t);+ const store = runtime.useChatRuntimeStore;+ store.getState().setActiveThreadId(THREAD);+ store.getState().applyThreadScopedSettings(THREAD, { permissionMode: "ask" });+ assert.equal(runtime.threadScopedOverride("toolsEnabled"), undefined);+ assert.equal(runtime.resolveToolsEnabledOnLoad(true).toolsEnabled, false);+ store.getState().setToolsEnabled(true);+ assert.equal(runtime.threadScopedOverride("toolsEnabled"), true);+});++test("a same-model reload keeps the Search and Code pills the model was running with", () => {+ const hook = readFileSync(+ new URL("../src/features/chat/hooks/use-chat-model-runtime.ts", import.meta.url),+ "utf8",+ );+ const marker = "...(reloadingSameModel && supportsTools";+ const reload = hook.slice(hook.indexOf(marker), hook.indexOf(marker) + 400);+ assert.match(+ reload,+ /\?\s*\{\s*toolsEnabled: stateBeforeUnload\.toolsEnabled,\s*codeToolsEnabled: stateBeforeUnload\.codeToolsEnabled,\s*\}\s*:\s*resolveToolsEnabledOnLoad\(supportsTools\)/,+ );+});diff --git a/studio/frontend/tests/mirrored-settings-write-authority.test.ts b/studio/frontend/tests/mirrored-settings-write-authority.test.tsindex d01d8b270f8..ea8ae4294df 100644--- a/studio/frontend/tests/mirrored-settings-write-authority.test.ts+++ b/studio/frontend/tests/mirrored-settings-write-authority.test.ts@@ -1,13 +1,8 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-// Two writes that reach every browser on the install now that saveBool and the hydration-// backfill mirror to /api/chat/settings, so each needs a reason to fire:-// 1. setCheckpoint's deep-research clamp. Codex runs deep research (thread.tsx exempts-// openai_codex from researchDisabled, chat-adapter accepts its research requests), so-// clamping on every external id would turn a Codex user's preference off everywhere.-// 2. The backfill, which reads "field absent" as "unset on the server". That only holds-// for a GET that answered; the legacy-storage fallback knows nothing about the server.+// The backfill reads "field absent" as "unset on the server". That only holds+// for a GET that answered; the legacy-storage fallback knows nothing about the server.// The store and chat-settings-storage cannot be imported in a bare node test (a .tsx barrel// sits in both graphs), so these pin the source the way the sibling store tests do.@@ -27,57 +22,6 @@ function slice(source: string, from: string, to: string): string {return source.slice(start, end);}-const setCheckpoint = () =>- slice(- store,- "setCheckpoint: (modelId, ggufVariant, options) =>",- "\n setActiveThreadId:",- );--test("selecting a Codex checkpoint keeps deep research", () => {- const body = setCheckpoint();- // Main settled on externalModelSupportsStudioTools for this; the property under test is- // that the clamp is provider-aware and computed once, not which helper answers it.- assert.match(- body,- /const clampsDeepResearch =\s*isExternalModelId\(modelId\) && !externalModelSupportsStudioTools\(modelId\);/,- );- // Every deep-research write in the setter goes through that one clamp rather than- // re-testing the id, which is what switched it off for capable providers.- for (const line of body.split("\n")) {- if (!/deepResearch/i.test(line)) continue;- assert.doesNotMatch(- line,- /isExternalModelId/,- `deep research still clamped on any external id: ${line.trim()}`,- );- }- assert.match(- body,- /if \(clampsDeepResearch\) \{\s*saveBool\(CHAT_DEEP_RESEARCH_ENABLED_KEY, false\);/,- );- assert.match(body, /const nextDeepResearchEnabled = clampsDeepResearch/);- assert.match(- body,- /\.\.\.\(clampsDeepResearch \? \{ deepResearchEnabled: false \} : \{\}\)/,- );-});--// The helper resolves the provider and refuses only a known non-Codex one, so an-// unresolved provider (connection list still loading) never drops the preference.-test("the clamp exempts Codex and an unresolved provider", () => {- const helper = slice(- store,- "function externalCheckpointRefusesDeepResearch(",- "\n}",- );- assert.match(helper, /if \(!parsed\) return false;/);- assert.match(- helper,- /provider != null && provider\.providerType !== "openai_codex"/,- );-});-test("hydration backfills only from an authoritative read", () => {const hydrate = slice(store,diff --git a/studio/frontend/tests/thread-scoped-pairing-invariants.test.ts b/studio/frontend/tests/thread-scoped-pairing-invariants.test.tsindex 47fea4b7c72..3d25b585b2e 100644--- a/studio/frontend/tests/thread-scoped-pairing-invariants.test.ts+++ b/studio/frontend/tests/thread-scoped-pairing-invariants.test.ts@@ -202,24 +202,6 @@ test("a normal flush stays resendable until it lands", () => {assert.match(terminal, /beaconUnsettledThreadSettingsWrites\(sentNewest\)/);});-test("a capability clamp never overwrites the preference the chat stored", () => {- // The clamp is the model's, not the user's. All four pills chat-page clamps go through- // the same preservation, under each pill's own capability rule.- const build = slice(store, "function buildThreadScopedSnapshot", "\n}");- for (const key of [- "toolsEnabled",- "codeToolsEnabled",- "imageToolsEnabled",- "webFetchToolsEnabled",- ]) {- assert.ok(- store.includes(`"${key}"`) && build.includes("CLAMPED_PILL_KEYS"),- `${key} is not covered by the clamp preservation`,- );- }- assert.match(build, /modelLoaded &&\s*!capable\[key\] &&/);-});-test("write ordering is per writer, never one browser's counter against another's", () => {// Comparing unrelated clients means whichever is behind has every edit refused while// the server still answers 200, so the user is told it saved and it did not.@@ -396,15 +378,6 @@ test("a retry keeps the defaults snapshot it already took", () => {assert.doesNotMatch(commit, /pairingWindowDefaultsThreadId = null;/);});-test("an explicit clear beats a capability preservation", () => {- // Enabling Search clears Deep Research deliberately; restoring the stored true would- // bring it back, alongside Search, once the chat is on a local model again.- const build = slice(store, "function buildThreadScopedSnapshot", "\n}");- assert.match(build, /!explicitlyEditedThreadFields\.has\("deepResearchEnabled"\)/);- const capture = slice(store, "function captureThreadScopedEdit", "\n}");- assert.match(capture, /explicitlyEditedThreadFields\.add\(field\)/);-});-test("the thread read that gates sends aborts when it times out", () => {// `bounded` alone is the 30s WRITE timeout, so the losing side of the race below stayed// open while the retry opened the next one. Each attempt carries its own deadline.diff --git a/tests/studio/test_deep_research_frontend_contract.py b/tests/studio/test_deep_research_frontend_contract.pyindex 607db2c370d..c6acb0f156f 100644--- a/tests/studio/test_deep_research_frontend_contract.py+++ b/tests/studio/test_deep_research_frontend_contract.py@@ -183,6 +183,7 @@ def test_research_presentation_is_integrated() -> None:assert "Deep research" in threadresearch_gate = thread.split("const researchDisabled =", 1)[1].split(";", 1)[0]assert "!modelLoaded" not in research_gate+ assert re.search(r"modelLoaded\s*&&\s*!externalSelection\s*&&\s*!supportsTools", research_gate)assert "<ResearchMessage />" in threadassert "if (researchRunId) return null" in threadassert "!researchRunId &&" in thread@@ -284,7 +285,7 @@ def test_research_presentation_is_integrated() -> None:checkpoint_update = store.split("setCheckpoint: (modelId, ggufVariant, options) =>", 1)[1].split("setActiveThreadId:", 1)[0]- assert "saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY, false)" in checkpoint_update+ assert "saveBool(CHAT_DEEP_RESEARCH_ENABLED_KEY" not in checkpoint_update# #8686 put a chat-scoped override in front of the global read here, so the literal `const permissionMode =# loadPermissionMode();` this used to pin is gone. The read itself is the contract, and it is still per call:# toggling deep research re-resolves the permission level, taking the chat's own level when it has one and the