Skip to content

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

Studio: remember Max Tokens on Max as Max, not as the old context length (#13223)

Daniel Han2afec3cbf488
diff --git a/studio/frontend/src/features/chat/lib/per-model-params.ts b/studio/frontend/src/features/chat/lib/per-model-params.ts
index ae9282f9d0f..e038e49abaa 100644
--- a/studio/frontend/src/features/chat/lib/per-model-params.ts
+++ b/studio/frontend/src/features/chat/lib/per-model-params.ts
@@ -14,6 +14,37 @@ import { normalizeSavedMinP } from "./min-p-policy.ts";
export type PersistedInferenceParamKey = keyof PersistedInferenceParams;
+/** "Max" Max Tokens as the memory records it. The window belongs to one load, so recording "Max"
+ * as that number replayed a stale cap at a larger context, and admission charges any cap below the
+ * window in full: one chat then held the whole KV pool and the rest queued (#10671). Above every
+ * window, so a build without this still clamps it to the load; this one never lets it into params. */
+export const REMEMBERED_MAX_TOKENS_MAX = 2 ** 31 - 1;
+
+/** An entry with a Max Tokens at or above `contextWindow` (what the slider shows as "Max") recorded
+ * as "Max". The window of the model the entry belongs to; null when unknown or external. `kept` is
+ * the entry's current cap when Max Tokens was not itself edited: a chosen cap the window clamped
+ * down to Max stays the chosen cap. */
+export function recordMaxTokensAsMax<T extends PersistedInferenceParams>(
+ entry: T,
+ contextWindow: number | null,
+ kept?: number,
+): T {
+ if (
+ contextWindow === null ||
+ entry.maxTokens === undefined ||
+ entry.maxTokens < contextWindow
+ ) {
+ return entry;
+ }
+ const maxTokens =
+ kept !== undefined &&
+ kept !== REMEMBERED_MAX_TOKENS_MAX &&
+ kept >= contextWindow
+ ? kept
+ : REMEMBERED_MAX_TOKENS_MAX;
+ return { ...entry, maxTokens };
+}
+
/** Params that persist across a reload. `checkpoint` is the key, not a value. */
export const PERSISTED_INFERENCE_PARAM_KEYS = [
"temperature",
@@ -135,6 +166,10 @@ export function getReplayedParams(
setInferenceParam(replayed, key, value);
}
}
+ // "Max" is the load's window; with no load to size it, what is on screen stands.
+ if (replayed.maxTokens === REMEMBERED_MAX_TOKENS_MAX) {
+ replayed.maxTokens = maxTokensCap ?? current.maxTokens;
+ }
if (maxTokensCap !== undefined && replayed.maxTokens > maxTokensCap) {
replayed.maxTokens = maxTokensCap;
}
diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
index 395d5affecf..10c567d1ee3 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -59,11 +59,13 @@ import {
import {
PERSISTED_INFERENCE_PARAM_KEYS,
REMEMBERED_INFERENCE_PARAM_KEYS,
+ REMEMBERED_MAX_TOKENS_MAX,
type PersistedInferenceParamKey,
getRememberedParamsPatch,
getReplayedParams,
pickRememberedChanges,
pickRememberedParams,
+ recordMaxTokensAsMax,
setInferenceParam,
} from "../lib/per-model-params";
import {
@@ -3102,7 +3104,13 @@ function getParamsByModelAfterEdit(
changedParams,
// Same filter as the outgoing snapshot: an edit to a key the memory keeps but the chat does
// not still records the WHOLE snapshot, riding the chat's sampling into the entry.
- pickRememberedParams(withoutActiveThreadParams(state, nextParams)),
+ recordMaxTokensAsMax(
+ pickRememberedParams(withoutActiveThreadParams(state, nextParams)),
+ rememberedWindow(nextParams.checkpoint),
+ changedParams.maxTokens === undefined
+ ? (outgoing ?? state.paramsByModel)[nextParams.checkpoint]?.maxTokens
+ : undefined,
+ ),
),
nextParams.checkpoint,
);
@@ -3117,9 +3125,14 @@ function rememberOutgoingModel(
if (!state.settingsHydrated && outgoing.checkpoint) {
modelLeftBeforeHydration = outgoing.checkpoint;
}
- const snapshot = pickRememberedParams(
+ const picked = pickRememberedParams(
withoutActiveThreadParams(state, outgoing),
);
+ const snapshot = recordMaxTokensAsMax(
+ picked,
+ rememberedWindow(outgoing.checkpoint),
+ state.paramsByModel[outgoing.checkpoint]?.maxTokens,
+ );
// Only to seed a model with no entry: later changes are written key by key, and a full
// snapshot would put this browser's copy of untouched keys over another tab's.
const seeding = state.paramsByModel[outgoing.checkpoint] === undefined;
@@ -3154,7 +3167,10 @@ function persistParamEdit(
}
// Only what moved: the server merges per key, so sending the rest would overwrite another
// tab's copy of every other key.
- const rememberedChanges = pickRememberedChanges(changedParams);
+ const rememberedChanges = recordMaxTokensAsMax(
+ pickRememberedChanges(changedParams),
+ modelId ? rememberedWindow(modelId) : null,
+ );
saveSettingsPatch({
inferenceParams: changedParams,
...(paramsByModel && modelId && hasKeys(rememberedChanges)
@@ -3242,6 +3258,13 @@ function loadedContextFor(checkpoint: string): number | null {
return loadedContext?.checkpoint === checkpoint ? loadedContext.cap : null;
}
+/** The window a memory entry's "Max" is measured against. Keyed by checkpoint, never
+ * loadedContextLength: a switch records the outgoing model after the incoming one may have
+ * published its own. An external model's cap is the provider's, not a window. */
+function rememberedWindow(checkpoint: string): number | null {
+ return isExternalModelId(checkpoint) ? null : loadedContextFor(checkpoint);
+}
+
function capParamsToLoadedContext(
state: ChatRuntimeStore,
params: InferenceParams,
@@ -3250,6 +3273,10 @@ function capParamsToLoadedContext(
? null
: state.loadedContextLength;
const cap = loadedContextFor(params.checkpoint) ?? residentContextCap;
+ // A migrated row can carry the Max marker; on screen it is this load's window.
+ if (params.maxTokens === REMEMBERED_MAX_TOKENS_MAX) {
+ return { ...params, maxTokens: cap ?? state.params.maxTokens };
+ }
return cap !== null && params.maxTokens > cap
? { ...params, maxTokens: cap }
: params;
@@ -3382,7 +3409,10 @@ function getHydratedSettingsState(
// The entry arriving for this model predates the fenced edit, so lay the edit over it or the
// next defaults update replays the stale one.
if (checkpoint) {
- const edited = pickLocallyEditedParams(params, versions);
+ const edited = recordMaxTokensAsMax(
+ pickLocallyEditedParams(params, versions),
+ rememberedWindow(checkpoint),
+ );
if (hasKeys(edited)) {
hydrated[checkpoint] = { ...hydrated[checkpoint], ...edited };
}
@@ -3391,7 +3421,10 @@ function getHydratedSettingsState(
} else if (checkpoint) {
// No map in the response: an install upgraded from before this feature. With no entry the
// next defaults update puts the recommendation back over the fenced edit.
- const edited = pickLocallyEditedParams(params, versions);
+ const edited = recordMaxTokensAsMax(
+ pickLocallyEditedParams(params, versions),
+ rememberedWindow(checkpoint),
+ );
if (hasKeys(edited)) {
nextState.paramsByModel = {
...state.paramsByModel,
@@ -3418,7 +3451,10 @@ function getHydratedSettingsState(
}
}
if (hasKeys(inherited)) {
- nextState.paramsByModel = { ...byModel, [left]: inherited };
+ nextState.paramsByModel = {
+ ...byModel,
+ [left]: recordMaxTokensAsMax(inherited, rememberedWindow(left)),
+ };
}
}
// Same fence as the scalar loop: a click made while this response was out is the newer
@@ -3513,6 +3549,15 @@ function getHydratedSettingsState(
setInferenceParam(replayed, key, value);
}
}
+ // "Max" is the window this model is on; with none known, what is on screen stands.
+ if (replayed.maxTokens === REMEMBERED_MAX_TOKENS_MAX) {
+ replayed.maxTokens =
+ rememberedWindow(params.checkpoint) ??
+ (isExternalModelId(params.checkpoint)
+ ? null
+ : state.loadedContextLength) ??
+ params.maxTokens;
+ }
// The same cap the load and status replays apply.
nextState.params = replayed;
}
@@ -3882,7 +3927,10 @@ function adoptMigratedFieldsAfterLocalEdit(
return {
// As the normal application path does: applying a thread pin advances no
// mutation version, so nothing above would have noticed it.
- params: restoreThreadScopedParams(nextParams),
+ params: capParamsToLoadedContext(
+ state,
+ restoreThreadScopedParams(nextParams),
+ ),
...(storedRow
? {
paramsByModel: {
@@ -4839,7 +4887,10 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
checkpointChanged && options?.persist !== false
? rememberOutgoingModel(state, state.params)
: null;
- const baseParams = getReplayedParams(
+ // A compare pane's load reaches the store only here.
+ const previousWindow = rememberedWindow(modelId);
+ noteLoadedContext(modelId, options?.maxTokensCap);
+ let baseParams = getReplayedParams(
state.rememberParamsPerModel,
outgoing ?? state.paramsByModel,
state.params,
@@ -4847,6 +4898,17 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
checkpointChanged,
options?.maxTokensCap,
);
+ // The same model reloaded at a new context replays nothing, so Max has to follow the window here.
+ if (
+ !checkpointChanged &&
+ options?.maxTokensCap !== undefined &&
+ ((previousWindow !== null && state.params.maxTokens >= previousWindow) ||
+ (state.rememberParamsPerModel &&
+ state.paramsByModel[modelId]?.maxTokens ===
+ REMEMBERED_MAX_TOKENS_MAX))
+ ) {
+ baseParams = { ...baseParams, maxTokens: options.maxTokensCap };
+ }
// Clamp maxTokens to the new model's cap when switching into an external model, so a value
// carried over from a local session cannot exceed the slider's max.
let nextMaxTokens = baseParams.maxTokens;
@@ -5695,14 +5757,20 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
const snapshot = pickRememberedParams(
withoutActiveThreadParams(state, state.params),
);
+ // The global set keeps the number; only the model's entry records "Max".
+ const remembered = recordMaxTokensAsMax(
+ snapshot,
+ rememberedWindow(state.params.checkpoint),
+ state.paramsByModel[state.params.checkpoint]?.maxTokens,
+ );
const paramsByModel = trackParamsByModel(
state,
getRememberedParamsPatch(
rememberParamsPerModel,
state.paramsByModel,
state.params.checkpoint,
- snapshot,
- snapshot,
+ remembered,
+ remembered,
),
state.params.checkpoint,
);
diff --git a/studio/frontend/tests/per-model-params-hydration-races.test.ts b/studio/frontend/tests/per-model-params-hydration-races.test.ts
index 11c64673530..450e1f693b1 100644
--- a/studio/frontend/tests/per-model-params-hydration-races.test.ts
+++ b/studio/frontend/tests/per-model-params-hydration-races.test.ts
@@ -214,3 +214,32 @@ test("a model left before hydration keeps the globals it was running with", asyn
assert.equal(params.temperature, 0.33);
assert.equal(params.systemPrompt, "A's prompt");
});
+
+// #10671: the global set A inherits records its Max as Max, as every other
+// snapshot of a model on Max does.
+test("a model left before hydration on Max inherits Max, not its window", async () => {
+ const { REMEMBERED_MAX_TOKENS_MAX } = await import(
+ "../src/features/chat/lib/per-model-params.ts"
+ );
+ settingsHttp.settings = { inferenceParams: { maxTokens: 30000 } };
+ reset({ checkpoint: A, maxTokens: 30000 });
+ settingsHttp.hold();
+ const hydrating = useChatRuntimeStore.getState().hydratePersistedSettings();
+ const resident = useChatRuntimeStore.getState();
+ resident.setParams(
+ { ...resident.params, checkpoint: A, maxTokens: 30000 },
+ { fromModelDefaults: true, maxTokensCap: 30000 },
+ );
+ const store = useChatRuntimeStore.getState();
+ store.setParams(
+ { ...store.params, checkpoint: B, maxTokens: 8192 },
+ { fromModelDefaults: true },
+ );
+ settingsHttp.release?.();
+ await hydrating;
+
+ assert.equal(
+ useChatRuntimeStore.getState().paramsByModel[A]?.maxTokens,
+ REMEMBERED_MAX_TOKENS_MAX,
+ );
+});
diff --git a/studio/frontend/tests/per-model-params-hydration.test.ts b/studio/frontend/tests/per-model-params-hydration.test.ts
index 7906969bcec..cf42fa7ecd0 100644
--- a/studio/frontend/tests/per-model-params-hydration.test.ts
+++ b/studio/frontend/tests/per-model-params-hydration.test.ts
@@ -28,6 +28,9 @@ const { mergeBackendRecommendedInference } = await import(
const { DEFAULT_INFERENCE_PARAMS } = await import(
"../src/features/chat/types/runtime.ts"
);
+const { REMEMBERED_MAX_TOKENS_MAX } = await import(
+ "../src/features/chat/lib/per-model-params.ts"
+);
const QWEN = "unsloth/Qwen3.5-9B-GGUF";
const LLAMA = "unsloth/Llama-4-8B";
@@ -961,3 +964,234 @@ test("a resident GGUF context does not cap an external model", async () => {
await useChatRuntimeStore.getState().hydratePersistedSettings();
assert.equal(useChatRuntimeStore.getState().params.maxTokens, 8192);
});
+
+/** Loads `checkpoint` the way the load and status paths do: defaults, then the replay, capped. */
+function loadAt(checkpoint: string, context: number) {
+ const store = useChatRuntimeStore.getState();
+ store.setParams(
+ { ...store.params, checkpoint, maxTokens: context },
+ { fromModelDefaults: true, maxTokensCap: context },
+ );
+}
+
+/** What the settings writer sent for one model, merged across PUTs. */
+function writtenFor(modelId: string): Record<string, unknown> {
+ const written: Record<string, unknown> = {};
+ for (const put of settingsHttp.puts) {
+ Object.assign(
+ written,
+ (put.inferenceParamsByModel as Record<string, Record<string, unknown>>)?.[
+ modelId
+ ] ?? {},
+ );
+ }
+ return written;
+}
+
+// #10671: "Max" was remembered as the window it was loaded with, so a reload at a
+// larger context replayed a cap one token short of nothing, admission charged it in
+// full, and every other chat queued behind it.
+test("Max Tokens left on Max stays Max at a larger context", async () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: true,
+ paramsByModel: {},
+ params: { ...useChatRuntimeStore.getState().params, checkpoint: QWEN },
+ });
+ await settled();
+ settingsHttp.puts.length = 0;
+ loadAt(QWEN, 30000);
+ useChatRuntimeStore.getState().clearCheckpoint();
+ await settled();
+
+ loadAt(QWEN, 35000);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 35000);
+ // And what survives a restart: the row the server keeps.
+ assert.equal(writtenFor(QWEN).maxTokens, REMEMBERED_MAX_TOKENS_MAX);
+
+ // An edit recorded while on Max says the same.
+ const store = useChatRuntimeStore.getState();
+ store.setParams({ ...store.params, temperature: 0.33 });
+ assert.equal(
+ useChatRuntimeStore.getState().paramsByModel[QWEN]?.maxTokens,
+ REMEMBERED_MAX_TOKENS_MAX,
+ );
+ loadAt(QWEN, 8192);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 8192);
+});
+
+// Turning the memory on adopts what is on screen, which for a model on Max is its window.
+test("turning the memory on records Max as Max", async () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: false,
+ paramsByModel: {},
+ params: { ...useChatRuntimeStore.getState().params, checkpoint: QWEN },
+ });
+ loadAt(QWEN, 30000);
+ await settled();
+ settingsHttp.puts.length = 0;
+ useChatRuntimeStore.getState().setRememberParamsPerModel(true);
+ await settled();
+
+ assert.equal(writtenFor(QWEN).maxTokens, REMEMBERED_MAX_TOKENS_MAX);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 30000);
+ loadAt(QWEN, 35000);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 35000);
+});
+
+test("a remembered Max is the window after a restart", async () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: false,
+ rememberParamsPerModel: true,
+ loadedContextLength: null,
+ paramsByModel: {},
+ params: { ...useChatRuntimeStore.getState().params, checkpoint: QWEN },
+ });
+ settingsHttp.settings = {
+ inferenceParams: { maxTokens: 30000 },
+ inferenceParamsByModel: {
+ [QWEN]: { maxTokens: REMEMBERED_MAX_TOKENS_MAX },
+ },
+ };
+ settingsHttp.hold();
+ const hydrating = useChatRuntimeStore.getState().hydratePersistedSettings();
+ loadAt(QWEN, 35000);
+ settingsHttp.release?.();
+ await hydrating;
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 35000);
+
+ // With nothing loaded to size it, the marker never reaches a request.
+ useChatRuntimeStore.setState({
+ settingsHydrated: false,
+ loadedContextLength: null,
+ paramsByModel: {},
+ params: { ...useChatRuntimeStore.getState().params, checkpoint: LLAMA },
+ });
+ settingsHttp.settings = {
+ inferenceParams: { maxTokens: 30000 },
+ inferenceParamsByModel: {
+ [LLAMA]: { maxTokens: REMEMBERED_MAX_TOKENS_MAX },
+ },
+ };
+ await useChatRuntimeStore.getState().hydratePersistedSettings();
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 30000);
+});
+
+test("a Max Tokens chosen below Max still replays as chosen", async () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: true,
+ paramsByModel: {},
+ params: { ...useChatRuntimeStore.getState().params, checkpoint: QWEN },
+ });
+ loadAt(QWEN, 30000);
+ const store = useChatRuntimeStore.getState();
+ store.setParams({ ...store.params, maxTokens: 4096 });
+ useChatRuntimeStore.getState().clearCheckpoint();
+
+ loadAt(QWEN, 35000);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 4096);
+});
+
+// An external model's cap is the provider's output limit, not a window to follow.
+test("an external model's cap is remembered as a number", () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: true,
+ paramsByModel: {},
+ params: { ...useChatRuntimeStore.getState().params, checkpoint: EXTERNAL },
+ });
+ loadAt(EXTERNAL, 8192);
+ useChatRuntimeStore.getState().clearCheckpoint();
+ assert.equal(
+ useChatRuntimeStore.getState().paramsByModel[EXTERNAL]?.maxTokens,
+ 8192,
+ );
+});
+
+// A compare pane loads through setCheckpoint alone, so the window it reports has to
+// count there too, or leaving that model records its Max as the number.
+test("a compare load keeps Max as Max when the next pane loads", () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: true,
+ paramsByModel: {},
+ });
+ loadAt(LLAMA, 8192);
+ useChatRuntimeStore.setState({
+ paramsByModel: { [QWEN]: { maxTokens: REMEMBERED_MAX_TOKENS_MAX } },
+ });
+ useChatRuntimeStore
+ .getState()
+ .setCheckpoint(QWEN, undefined, { maxTokensCap: 30000 });
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 30000);
+ useChatRuntimeStore
+ .getState()
+ .setCheckpoint(LLAMA, null, { maxTokensCap: 8192 });
+
+ assert.equal(
+ useChatRuntimeStore.getState().paramsByModel[QWEN]?.maxTokens,
+ REMEMBERED_MAX_TOKENS_MAX,
+ );
+ loadAt(QWEN, 35000);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 35000);
+});
+
+// A cap the user chose is not Max just because a smaller load clamped it to the
+// window, or met it exactly: the next larger load gets the chosen cap back.
+test("a chosen cap clamped to a smaller window stays the chosen cap", () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: true,
+ paramsByModel: {},
+ });
+ loadAt(QWEN, 35000);
+ const store = useChatRuntimeStore.getState();
+ store.setParams({ ...store.params, maxTokens: 30000 });
+ assert.equal(useChatRuntimeStore.getState().paramsByModel[QWEN]?.maxTokens, 30000);
+
+ for (const smaller of [20000, 30000]) {
+ loadAt(QWEN, smaller);
+ useChatRuntimeStore.getState().clearCheckpoint();
+ assert.equal(
+ useChatRuntimeStore.getState().paramsByModel[QWEN]?.maxTokens,
+ 30000,
+ );
+ loadAt(QWEN, 50000);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 30000);
+ }
+
+ // Moving it to Max is the user saying Max.
+ const chosen = useChatRuntimeStore.getState();
+ chosen.setParams({ ...chosen.params, maxTokens: 50000 });
+ assert.equal(
+ useChatRuntimeStore.getState().paramsByModel[QWEN]?.maxTokens,
+ REMEMBERED_MAX_TOKENS_MAX,
+ );
+ loadAt(QWEN, 60000);
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 60000);
+});
+
+// The compare pane reloads the model it already shows through setCheckpoint,
+// which replays nothing for an unchanged checkpoint.
+test("a compare reload of the same model keeps Max as Max", () => {
+ useChatRuntimeStore.setState({
+ settingsHydrated: true,
+ rememberParamsPerModel: true,
+ paramsByModel: {},
+ });
+ loadAt(QWEN, 30000);
+ useChatRuntimeStore
+ .getState()
+ .setCheckpoint(QWEN, undefined, { maxTokensCap: 35000 });
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 35000);
+
+ // A chosen cap is left alone.
+ const store = useChatRuntimeStore.getState();
+ store.setParams({ ...store.params, maxTokens: 4096 });
+ useChatRuntimeStore
+ .getState()
+ .setCheckpoint(QWEN, undefined, { maxTokensCap: 40000 });
+ assert.equal(useChatRuntimeStore.getState().params.maxTokens, 4096);
+});
diff --git a/studio/frontend/tests/per-model-params.test.ts b/studio/frontend/tests/per-model-params.test.ts
index 78c562111a0..f6a97577e96 100644
--- a/studio/frontend/tests/per-model-params.test.ts
+++ b/studio/frontend/tests/per-model-params.test.ts
@@ -15,9 +15,11 @@ registerBundlerResolver();
import {
REMEMBERED_INFERENCE_PARAM_KEYS,
+ REMEMBERED_MAX_TOKENS_MAX,
getRememberedParamsPatch,
getReplayedParams,
pickRememberedParams,
+ recordMaxTokensAsMax,
} from "../src/features/chat/lib/per-model-params.ts";
import type { InferenceParams } from "../src/features/chat/types/runtime.ts";
@@ -420,3 +422,26 @@ for (const [label, id] of [
assert.equal(replayed.temperature, 0.2);
});
}
+
+// #10671: "Max" is recorded apart from the window it was on, and replays as the
+// window of whatever load replays it.
+test("a remembered Max replays as the load's window, never as itself", () => {
+ assert.deepEqual(recordMaxTokensAsMax({ maxTokens: 30000 }, 30000), {
+ maxTokens: REMEMBERED_MAX_TOKENS_MAX,
+ });
+ assert.deepEqual(recordMaxTokensAsMax({ maxTokens: 4096 }, 30000), {
+ maxTokens: 4096,
+ });
+ // No window known (or an external model): recorded as before.
+ assert.deepEqual(recordMaxTokensAsMax({ maxTokens: 30000 }, null), {
+ maxTokens: 30000,
+ });
+
+ const memory = { [QWEN]: { maxTokens: REMEMBERED_MAX_TOKENS_MAX } };
+ const at = (cap?: number) =>
+ getReplayedParams(true, memory, params({ maxTokens: 777 }), QWEN, true, cap)
+ .maxTokens;
+ assert.equal(at(35000), 35000);
+ assert.equal(at(8192), 8192);
+ assert.equal(at(undefined), 777, "nothing sized a window: what is on screen");
+});
diff --git a/studio/frontend/tests/qwen-defaults-migration-safety.test.ts b/studio/frontend/tests/qwen-defaults-migration-safety.test.ts
index 4479ab84a3c..0c90103e4d2 100644
--- a/studio/frontend/tests/qwen-defaults-migration-safety.test.ts
+++ b/studio/frontend/tests/qwen-defaults-migration-safety.test.ts
@@ -1035,3 +1035,35 @@ test("a custom preset chosen mid-write keeps its own legacy-valued fields", asyn
assert.equal(after.presencePenalty, 0);
assert.equal(after.minP, 0.01);
});
+
+// #10671: a remembered "Max" is a marker, never a live cap; adopting a migrated
+// row that carries it must leave the load's window on screen.
+test("adopting a migrated row never puts the Max marker on screen", async () => {
+ const { REMEMBERED_MAX_TOKENS_MAX } = await import(
+ "../src/features/chat/lib/per-model-params.ts"
+ );
+ const alias = QWEN38.toLowerCase();
+ const legacyMax = { ...LEGACY_SNAPSHOT, maxTokens: REMEMBERED_MAX_TOKENS_MAX };
+ resetHttp({
+ ...BUILTIN_DEFAULT,
+ inferenceParamsByModel: { [alias]: legacyMax },
+ });
+ settingsHttp.beforeConditionalApply = () => {
+ useChatRuntimeStore.getState().setActivePresetSource("modified");
+ };
+ seedActiveQwen({
+ paramsByModel: { [alias]: { ...legacyMax, minPMode: "custom" } },
+ });
+
+ const active = useChatRuntimeStore.getState();
+ active.setParams(
+ { ...active.params, minP: 0, presencePenalty: 1.5 },
+ { fromModelDefaults: true },
+ );
+ await sleep(60);
+
+ assert.notEqual(
+ useChatRuntimeStore.getState().params.maxTokens,
+ REMEMBERED_MAX_TOKENS_MAX,
+ );
+});