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: answer Refresh and Edit in a Compare pane with its own model (#16698)

Nilayff41710b735b
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 8332eda8227..b17f089ba9b 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -143,6 +143,7 @@ import {
} from "../utils/auto-compaction";
import { tabCloseRunFields } from "../utils/keep-running-when-tab-closed";
import { resolveModelCatalogEntry } from "../model-catalog";
+import { prepareComparePaneRun } from "../compare-run-ownership";
import {
lengthIncompleteReason,
lengthStopCause,
@@ -4464,6 +4465,13 @@ export function createOpenAIStreamAdapter(
"This chat's settings could not be loaded, so the message was not sent. Reopen the chat and try again.",
);
}
+ await prepareComparePaneRun(
+ options.pairId,
+ options.modelType,
+ abortSignal,
+ () => waitForModelReady(abortSignal),
+ );
+ abortSignal.throwIfAborted();
let runtime = useChatRuntimeStore.getState();
// Capture the thread ID once so it stays stable across chat switches during model load.
const resolvedThreadId =
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index d24660f2b0d..e63abff5919 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -253,6 +253,13 @@ import {
RegisterCompareHandle,
SharedComposer,
} from "./shared-composer";
+import {
+ isLoadingPick,
+ loadComparePaneModel,
+ registerComparePaneRunPreparer,
+} from "./compare-run-ownership";
+import { checkpointMatchesPick } from "./lib/resident-model-match";
+import { compareModelDisplayName } from "./lib/external-model-label";
import { BypassPermissionsConfirmDialog } from "./bypass-permissions-menu-item";
import { RootSandboxSetupDialog } from "./sandbox-setup-dialog";
import {
@@ -1146,6 +1153,8 @@ const CompareContent = memo(function CompareContent({
onModelsChange,
deleteDisabled,
onExitCompare,
+ loadPaneModel,
+ cancelPaneModelLoad,
}: {
pairId: string;
projectId?: string | null;
@@ -1157,6 +1166,8 @@ const CompareContent = memo(function CompareContent({
onModelsChange?: (deletedModel?: DeletedModelRef) => void;
deleteDisabled?: boolean;
onExitCompare?: () => void;
+ loadPaneModel: (selection: SelectedModelInput) => Promise<void>;
+ cancelPaneModelLoad: () => Promise<boolean>;
}): ReactElement {
const { state: compareRead, retry: retryCompareRead } =
useCompareVariant(pairId);
@@ -1184,6 +1195,8 @@ const CompareContent = memo(function CompareContent({
onModelsChange={onModelsChange}
deleteDisabled={deleteDisabled}
onExitCompare={onExitCompare}
+ loadPaneModel={loadPaneModel}
+ cancelPaneModelLoad={cancelPaneModelLoad}
/>
);
});
@@ -1550,6 +1563,8 @@ const GeneralCompareContent = memo(function GeneralCompareContent({
onModelsChange,
deleteDisabled,
onExitCompare,
+ loadPaneModel,
+ cancelPaneModelLoad,
}: {
pairId: string;
projectId?: string | null;
@@ -1561,6 +1576,8 @@ const GeneralCompareContent = memo(function GeneralCompareContent({
onModelsChange?: (deletedModel?: DeletedModelRef) => void;
deleteDisabled?: boolean;
onExitCompare?: () => void;
+ loadPaneModel: (selection: SelectedModelInput) => Promise<void>;
+ cancelPaneModelLoad: () => Promise<boolean>;
}): ReactElement {
const handlesRef = useRef<Record<string, CompareHandle>>({});
const [model1ThreadId, setModel1ThreadId] = useState<string>();
@@ -1592,6 +1609,50 @@ const GeneralCompareContent = memo(function GeneralCompareContent({
isLora: false,
});
+ useEffect(
+ () =>
+ registerComparePaneRunPreparer(pairId, async (pane, signal) => {
+ const sel = pane === "model1" ? model1 : model2;
+ if (!sel.id) return;
+ const store = () => useChatRuntimeStore.getState();
+ // A shared Send has already loaded this pane's model, so it is active here.
+ await loadComparePaneModel(
+ {
+ name: compareModelDisplayName(sel.id),
+ isActive: () =>
+ checkpointMatchesPick(
+ store().params.checkpoint,
+ store().activeGgufVariant,
+ sel,
+ ),
+ isLoading: () => isLoadingPick(store().loadingModelPick, sel),
+ cancelLoad: () => void cancelPaneModelLoad(),
+ load: () =>
+ loadPaneModel({
+ id: sel.id,
+ isLora: sel.isLora,
+ ggufVariant: sel.ggufVariant,
+ ...(sel.ggufVariant || sel.id.toLowerCase().endsWith(".gguf")
+ ? { isGguf: true }
+ : {}),
+ isDiffusion: sel.isDiffusion,
+ config:
+ sel.config ??
+ resolveResidentInitialConfig(sel.id, sel.ggufVariant ?? null)
+ .config,
+ keepSpeculative: true,
+ throwOnError: true,
+ previousConfig: currentRuntimePerModelConfig({
+ includeMaxSeqLength: true,
+ }),
+ }),
+ },
+ signal,
+ );
+ }),
+ [pairId, model1, model2, loadPaneModel, cancelPaneModelLoad],
+ );
+
const handleModelsChange = useCallback(
(deletedModel?: DeletedModelRef) => {
if (modelMatchesDeleted(model1, deletedModel)) {
@@ -4932,6 +4993,8 @@ export function ChatPage({
onModelsChange={refreshModelLists}
deleteDisabled={modelOperationInProgress}
onExitCompare={exitCompare}
+ loadPaneModel={selectModel}
+ cancelPaneModelLoad={cancelLoading}
/>
) : null}
diff --git a/studio/frontend/src/features/chat/compare-run-ownership.ts b/studio/frontend/src/features/chat/compare-run-ownership.ts
index c056b9d59f8..ab2955a1275 100644
--- a/studio/frontend/src/features/chat/compare-run-ownership.ts
+++ b/studio/frontend/src/features/chat/compare-run-ownership.ts
@@ -81,3 +81,92 @@ export function isCompareCancellation(
(error instanceof DOMException && error.name === "AbortError")
);
}
+
+type ComparePaneRunPreparer = (
+ pane: "model1" | "model2",
+ signal: AbortSignal,
+) => Promise<void>;
+
+export type ComparePaneModel = {
+ name: string;
+ isActive: () => boolean;
+ isLoading: () => boolean;
+ load: () => Promise<void>;
+ cancelLoad: () => void;
+};
+
+const CANCEL_POLL_MS = 100;
+let latestPaneLoad = 0;
+
+export function isLoadingPick(
+ pick: { id: string; ggufVariant?: string | null } | null | undefined,
+ sel: { id: string; ggufVariant?: string | null },
+): boolean {
+ return (
+ pick?.id === sel.id &&
+ (pick.ggufVariant ?? null) === (sel.ggufVariant ?? null)
+ );
+}
+
+export async function loadComparePaneModel(
+ model: ComparePaneModel,
+ signal: AbortSignal,
+): Promise<void> {
+ if (model.isActive()) return;
+ signal.throwIfAborted();
+ // Stopping cancels this pane's load, as stopping a shared Send does. The load is published only
+ // after its preflight, so a stop before then watches for it until a newer pane load takes over.
+ const thisLoad = ++latestPaneLoad;
+ let settled = false;
+ const cancel = () => {
+ if (settled || latestPaneLoad !== thisLoad) return;
+ if (model.isLoading()) model.cancelLoad();
+ else setTimeout(cancel, CANCEL_POLL_MS);
+ };
+ signal.addEventListener("abort", cancel, { once: true });
+ try {
+ await model.load();
+ } finally {
+ settled = true;
+ signal.removeEventListener("abort", cancel);
+ }
+ signal.throwIfAborted();
+ if (!model.isActive()) {
+ throw new Error(
+ `${model.name} is not loaded, so this pane was not answered.`,
+ );
+ }
+}
+
+const comparePaneRunPreparers = new Map<string, ComparePaneRunPreparer>();
+
+export function registerComparePaneRunPreparer(
+ pairId: string,
+ prepare: ComparePaneRunPreparer,
+): () => void {
+ comparePaneRunPreparers.set(pairId, prepare);
+ return () => {
+ if (comparePaneRunPreparers.get(pairId) === prepare) {
+ comparePaneRunPreparers.delete(pairId);
+ }
+ };
+}
+
+// Waits out a load in flight first: until it lands, the store still names the outgoing model.
+// The preparer is read after the wait, as the one registered before it holds the old model.
+export async function prepareComparePaneRun(
+ pairId: string | undefined,
+ pane: string | undefined,
+ signal: AbortSignal,
+ waitForModelIdle: () => Promise<void>,
+): Promise<void> {
+ if (
+ !pairId ||
+ !comparePaneRunPreparers.has(pairId) ||
+ (pane !== "model1" && pane !== "model2")
+ ) {
+ return;
+ }
+ await waitForModelIdle();
+ await comparePaneRunPreparers.get(pairId)?.(pane, signal);
+}
diff --git a/studio/frontend/src/features/chat/lib/resident-model-match.ts b/studio/frontend/src/features/chat/lib/resident-model-match.ts
index 08696a5713d..234b77ba58c 100644
--- a/studio/frontend/src/features/chat/lib/resident-model-match.ts
+++ b/studio/frontend/src/features/chat/lib/resident-model-match.ts
@@ -23,6 +23,27 @@ export type ResidentModelStatus = {
gguf_variant?: string | null;
};
+// A standalone file has no quant choice; the backend derives a variant from its name.
+function variantMatchesPick(
+ variant: string | null | undefined,
+ pick: ModelPickNames,
+): boolean {
+ if (!pick.ggufVariant && isStandaloneGgufPath(pick.loadPath ?? pick.id)) {
+ return true;
+ }
+ return ggufVariantsMatch(variant, pick.ggufVariant);
+}
+
+export function checkpointMatchesPick(
+ checkpoint: string | null | undefined,
+ variant: string | null | undefined,
+ pick: ModelPickNames,
+): boolean {
+ return (
+ modelIdsMatch(checkpoint, pick.id) && variantMatchesPick(variant, pick)
+ );
+}
+
/** Each side has two names for one model; matching them avoids a needless reload. */
export function residentModelMatchesPick(
status: ResidentModelStatus,
@@ -31,14 +52,7 @@ export function residentModelMatchesPick(
if (!status.active_model) {
return false;
}
- // A standalone file has no quant choice; the backend derives a variant from its name.
- const picksItsOwnVariant = !(
- !pick.ggufVariant && isStandaloneGgufPath(pick.loadPath ?? pick.id)
- );
- if (
- picksItsOwnVariant &&
- !ggufVariantsMatch(status.gguf_variant, pick.ggufVariant)
- ) {
+ if (!variantMatchesPick(status.gguf_variant, pick)) {
return false;
}
// Only the raw identifier says which snapshot is resident.
diff --git a/studio/frontend/tests/compare-pane-run-model.test.ts b/studio/frontend/tests/compare-pane-run-model.test.ts
new file mode 100644
index 00000000000..551eef9426d
--- /dev/null
+++ b/studio/frontend/tests/compare-pane-run-model.test.ts
@@ -0,0 +1,286 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import {
+ isLoadingPick,
+ loadComparePaneModel,
+ prepareComparePaneRun,
+ registerComparePaneRunPreparer,
+} from "../src/features/chat/compare-run-ownership.ts";
+
+import { readSrc, registerBundlerResolver } from "./helpers/kit.ts";
+
+registerBundlerResolver();
+const { checkpointMatchesPick } = await import(
+ "../src/features/chat/lib/resident-model-match.ts"
+);
+
+const adapterSource = readSrc("features/chat/api/chat-adapter.ts");
+const chatPageSource = readSrc("features/chat/chat-page.tsx");
+const generalCompareSource = chatPageSource.slice(
+ chatPageSource.indexOf("const GeneralCompareContent"),
+ chatPageSource.indexOf("function formatProjectChatDate"),
+);
+
+const idle = async () => {};
+const live = () => new AbortController().signal;
+
+test("a compare pane run is prepared for its own pane and pair", async () => {
+ const calls: string[] = [];
+ const unregister = registerComparePaneRunPreparer("pair-a", async (pane) => {
+ calls.push(pane);
+ });
+ await prepareComparePaneRun("pair-a", "model1", live(), idle);
+ await prepareComparePaneRun("pair-a", "model2", live(), idle);
+ await prepareComparePaneRun("pair-b", "model1", live(), idle);
+ await prepareComparePaneRun("pair-a", "base", live(), idle);
+ await prepareComparePaneRun(undefined, "model1", live(), idle);
+ assert.deepEqual(calls, ["model1", "model2"]);
+ unregister();
+ await prepareComparePaneRun("pair-a", "model1", live(), idle);
+ assert.deepEqual(calls, ["model1", "model2"]);
+});
+
+test("a pane checks its model only after a load in flight lands", async () => {
+ const events: string[] = [];
+ const unregister = registerComparePaneRunPreparer("pair-e", async (pane) => {
+ events.push(`prepare ${pane}`);
+ });
+ const wait = async () => {
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ events.push("load landed");
+ };
+ await prepareComparePaneRun("pair-e", "model2", live(), wait);
+ await prepareComparePaneRun("pair-e", "base", live(), wait);
+ await prepareComparePaneRun("pair-x", "model1", live(), wait);
+ assert.deepEqual(events, ["load landed", "prepare model2"]);
+ unregister();
+});
+
+test("a pane uses the preparer registered while it waited", async () => {
+ const calls: string[] = [];
+ const stale = registerComparePaneRunPreparer("pair-f", async () => {
+ calls.push("stale");
+ });
+ let fresh = () => {};
+ await prepareComparePaneRun("pair-f", "model1", live(), async () => {
+ stale();
+ fresh = registerComparePaneRunPreparer("pair-f", async () => {
+ calls.push("fresh");
+ });
+ });
+ assert.deepEqual(calls, ["fresh"]);
+ fresh();
+});
+
+test("the preparer gets the run's abort signal", async () => {
+ let seen: AbortSignal | undefined;
+ const unregister = registerComparePaneRunPreparer("pair-g", async (_, signal) => {
+ seen = signal;
+ });
+ const signal = live();
+ await prepareComparePaneRun("pair-g", "model1", signal, idle);
+ assert.equal(seen, signal);
+ unregister();
+});
+
+test("a failed pane load stops the pane run", async () => {
+ const unregister = registerComparePaneRunPreparer("pair-c", async () => {
+ throw new Error("not loaded");
+ });
+ await assert.rejects(
+ prepareComparePaneRun("pair-c", "model1", live(), idle),
+ /not loaded/,
+ );
+ unregister();
+});
+
+test("a stale unregister keeps the newer preparer", async () => {
+ const calls: string[] = [];
+ const first = registerComparePaneRunPreparer("pair-d", async () => {
+ calls.push("first");
+ });
+ const second = registerComparePaneRunPreparer("pair-d", async () => {
+ calls.push("second");
+ });
+ first();
+ await prepareComparePaneRun("pair-d", "model1", live(), idle);
+ assert.deepEqual(calls, ["second"]);
+ second();
+});
+
+function paneModel(active: boolean) {
+ const state = { active, loading: false, loads: 0, cancels: 0 };
+ let finishLoad = (_landed: boolean) => {};
+ const model = {
+ name: "gemma-3-270m-it",
+ isActive: () => state.active,
+ isLoading: () => state.loading,
+ load: () => {
+ state.loads += 1;
+ state.loading = true;
+ return new Promise<void>((resolve) => {
+ finishLoad = (landed) => {
+ state.loading = false;
+ state.active = landed;
+ resolve();
+ };
+ });
+ },
+ cancelLoad: () => {
+ state.cancels += 1;
+ finishLoad(false);
+ },
+ };
+ return { state, model, finishLoad: (landed: boolean) => finishLoad(landed) };
+}
+
+test("a pane whose model is active runs without loading", async () => {
+ const pane = paneModel(true);
+ await loadComparePaneModel(pane.model, live());
+ assert.equal(pane.state.loads, 0);
+});
+
+test("a pane loads its own model before it runs", async () => {
+ const pane = paneModel(false);
+ const run = loadComparePaneModel(pane.model, live());
+ assert.equal(pane.state.loads, 1);
+ pane.finishLoad(true);
+ await run;
+});
+
+test("a pane whose load did not land is not answered", async () => {
+ const pane = paneModel(false);
+ const run = loadComparePaneModel(pane.model, live());
+ pane.finishLoad(false);
+ await assert.rejects(run, /gemma-3-270m-it is not loaded/);
+});
+
+test("stopping a pane cancels the load it started", async () => {
+ const pane = paneModel(false);
+ const controller = new AbortController();
+ const run = loadComparePaneModel(pane.model, controller.signal);
+ controller.abort();
+ await assert.rejects(run, { name: "AbortError" });
+ assert.equal(pane.state.cancels, 1);
+ assert.equal(pane.state.active, false);
+});
+
+test("stopping a pane during load preflight cancels the load once it starts", async () => {
+ const pane = paneModel(false);
+ const load = pane.model.load;
+ pane.model.load = () => {
+ const started = load();
+ pane.state.loading = false;
+ setTimeout(() => {
+ pane.state.loading = true;
+ }, 150);
+ return started;
+ };
+ const controller = new AbortController();
+ const run = loadComparePaneModel(pane.model, controller.signal);
+ controller.abort();
+ await assert.rejects(run, { name: "AbortError" });
+ assert.equal(pane.state.cancels, 1);
+ assert.equal(pane.state.active, false);
+});
+
+test("a stopped pane leaves a newer pane's load alone", async () => {
+ const stopped = paneModel(false);
+ const newer = paneModel(false);
+ const controller = new AbortController();
+ const stoppedRun = loadComparePaneModel(stopped.model, controller.signal);
+ stopped.state.loading = false;
+ controller.abort();
+ const newerRun = loadComparePaneModel(newer.model, live());
+ // Same model and quant in both panes: the stopped pane's watch would see the newer load as its own.
+ stopped.state.loading = true;
+ await new Promise((resolve) => setTimeout(resolve, 250));
+ assert.equal(stopped.state.cancels, 0);
+ stopped.finishLoad(false);
+ newer.finishLoad(true);
+ await assert.rejects(stoppedRun, { name: "AbortError" });
+ await newerRun;
+});
+
+test("a pane's loading pick is its own model and quant", () => {
+ const sel = { id: "unsloth/Qwen3-0.6B-GGUF", ggufVariant: "Q4_K_M" };
+ assert.ok(isLoadingPick({ id: sel.id, ggufVariant: "Q4_K_M" }, sel));
+ assert.ok(!isLoadingPick({ id: sel.id, ggufVariant: "Q8_0" }, sel));
+ assert.ok(!isLoadingPick({ id: "unsloth/other-GGUF", ggufVariant: "Q4_K_M" }, sel));
+ assert.ok(!isLoadingPick(null, sel));
+ assert.ok(isLoadingPick({ id: "org/m", ggufVariant: null }, { id: "org/m" }));
+});
+
+test("stopping a pane leaves a load it did not start alone", async () => {
+ const pane = paneModel(false);
+ const controller = new AbortController();
+ const run = loadComparePaneModel(pane.model, controller.signal);
+ pane.state.loading = false;
+ controller.abort();
+ pane.finishLoad(true);
+ await assert.rejects(run, { name: "AbortError" });
+ assert.equal(pane.state.cancels, 0);
+});
+
+test("the chat adapter prepares a compare pane before it reads the model", () => {
+ const prepare = adapterSource.indexOf("await prepareComparePaneRun(");
+ const firstRead = adapterSource.indexOf(
+ "let runtime = useChatRuntimeStore.getState();",
+ );
+ assert.ok(prepare > 0, "the run must prepare its compare pane");
+ assert.ok(prepare < firstRead, "before the checkpoint is read");
+ assert.match(
+ adapterSource.slice(prepare, firstRead),
+ /abortSignal,\s*\(\) => waitForModelReady\(abortSignal\),/,
+ );
+});
+
+test("Refresh or Edit in a compare pane loads that pane's own model", () => {
+ assert.match(
+ generalCompareSource,
+ /registerComparePaneRunPreparer\(pairId, async \(pane, signal\) => \{\s*const sel = pane === "model1" \? model1 : model2;/,
+ );
+ assert.match(generalCompareSource, /loadPaneModel\(\{\s*id: sel\.id,/);
+ assert.match(generalCompareSource, /checkpointMatchesPick\(\s*store\(\)\.params\.checkpoint,/);
+ assert.match(
+ generalCompareSource,
+ /isLoading: \(\) => isLoadingPick\(store\(\)\.loadingModelPick, sel\),/,
+ );
+
+ assert.match(
+ generalCompareSource,
+ /\.\.\.\(sel\.ggufVariant \|\| sel\.id\.toLowerCase\(\)\.endsWith\("\.gguf"\)\s*\? \{ isGguf: true \}/,
+ "a standalone GGUF pane reloads with GGUF load fields, as shared Send does",
+ );
+ assert.match(
+ generalCompareSource,
+ /throwOnError: true,\s*previousConfig: currentRuntimePerModelConfig\(\{\s*includeMaxSeqLength: true,/,
+ "a failed pane load restores the resident model's settings, as a regular pick does",
+ );
+ assert.match(chatPageSource, /loadPaneModel=\{selectModel\}/);
+ assert.match(chatPageSource, /cancelPaneModelLoad=\{cancelLoading\}/);
+});
+
+test("a standalone GGUF pane matches the quant the backend derives", () => {
+ const file = "/models/Qwen3-0.6B-Q4_K_M.gguf";
+ assert.ok(checkpointMatchesPick(file, "Q4_K_M", { id: file }));
+ assert.ok(
+ checkpointMatchesPick("unsloth/Qwen3-0.6B-GGUF", "Q4_K_M", {
+ id: "unsloth/Qwen3-0.6B-GGUF",
+ ggufVariant: "Q4_K_M",
+ }),
+ );
+ assert.ok(
+ !checkpointMatchesPick("unsloth/Qwen3-0.6B-GGUF", "Q8_0", {
+ id: "unsloth/Qwen3-0.6B-GGUF",
+ ggufVariant: "Q4_K_M",
+ }),
+ );
+ assert.ok(
+ !checkpointMatchesPick("/models/other.gguf", "Q4_K_M", { id: file }),
+ );
+});