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: keep the loaded model when Switch back has to download a GGUF (#16474)

Nilay7a462cc810b6
diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
index 0437cb214fe..3943ef6a987 100644
--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts
@@ -236,6 +236,7 @@ type ActiveModelLoadRun = {
rollbackLoadedState: ReturnType<typeof useChatRuntimeStore.getState>;
/** Set once the preliminary unload removed the prior resident; cancel must then reconcile. */
residentModelUnloaded: boolean;
+ downloadKeepsResident: boolean;
forceCancelActive: boolean;
/** Resolves when the owning coroutine unwinds; cancellation holds the slot until then. */
settledPromise: Promise<void>;
@@ -1076,7 +1077,11 @@ export function useChatModelRuntime() {
if (ownsModelLoadRun(activeLoadRunRef.current, run)) {
// Hold the slot until the aborted coroutine unwinds from its unabortable awaits.
await run.settledPromise;
- if (run.loadAttemptPath && run.forceCancelActive && !run.residentModelUnloaded) {
+ if (
+ run.loadAttemptPath &&
+ (run.forceCancelActive || run.downloadKeepsResident) &&
+ !run.residentModelUnloaded
+ ) {
try {
const status = await getInferenceStatus(undefined, run.rollbackCheckpoint ?? undefined);
if ((status.loading?.length ?? 0) === 0) {
@@ -1092,7 +1097,9 @@ export function useChatModelRuntime() {
}
if (
(!run.loadAttemptPath && run.residentModelUnloaded) ||
- (run.loadAttemptPath && run.forceCancelActive && run.residentModelUnloaded)
+ (run.loadAttemptPath &&
+ (run.forceCancelActive || run.downloadKeepsResident) &&
+ run.residentModelUnloaded)
) {
try {
// The store names the former resident but holds the cancelled target's settings.
@@ -1876,6 +1883,7 @@ export function useChatModelRuntime() {
: currentRollbackState.activeNativePathExpiresAtMs ?? null,
rollbackLoadedState: inheritedPendingRollback?.loadedState ?? currentRollbackState,
residentModelUnloaded: inheritedPendingRollback?.residentUnloaded === true,
+ downloadKeepsResident: false,
forceCancelActive,
settledPromise: loadRunSettled,
markSettled: markLoadRunSettled,
@@ -2542,8 +2550,10 @@ export function useChatModelRuntime() {
stopQueuedRuns(stopDecision, keepsOthers || touchesOnlySelected);
if (currentCheckpoint && !keepsOthers) {
+ // /load fetches a missing GGUF before stopping the resident, so a failed download keeps it.
+ loadRun.downloadKeepsResident = isGguf && !downloadComplete;
// With chats generating, skip the preliminary unload so a rejected target truncates nothing.
- if (!forceCancelActive && !touchesOnlySelected) {
+ if (!forceCancelActive && !touchesOnlySelected && !loadRun.downloadKeepsResident) {
await unloadModel({ model_path: currentCheckpoint });
// Only a real /unload removes the resident; the forced path leaves it to /load.
loadRun.residentModelUnloaded = true;
@@ -3566,6 +3576,7 @@ export function useChatModelRuntime() {
previous.activeNativePathExpiresAtMs ?? null,
rollbackLoadedState: previous,
residentModelUnloaded: false,
+ downloadKeepsResident: false,
forceCancelActive: false,
settledPromise,
markSettled: markLoadRunSettled,
diff --git a/studio/frontend/tests/chat-model-load-replacement.test.ts b/studio/frontend/tests/chat-model-load-replacement.test.ts
index 6e74b0a8b94..f2a337d8837 100644
--- a/studio/frontend/tests/chat-model-load-replacement.test.ts
+++ b/studio/frontend/tests/chat-model-load-replacement.test.ts
@@ -40,7 +40,7 @@ const blocks = {
"if (pendingReplacementRollback?.config)",
),
rollbackClear: section(runtime, "function restoreRollbackConfigForClear(", "const approvedRemoteCodeFingerprints"),
- preliminaryUnload: section(runtime, "if (!forceCancelActive && !touchesOnlySelected) {", "// Set either way: /load can still leave no model resident"),
+ preliminaryUnload: section(runtime, "if (!forceCancelActive && !touchesOnlySelected", "// Set either way: /load can still leave no model resident"),
preflightLease: section(runtime, "// Hold the lifecycle lease through confirmation and loading.", "if (lifecycleLease === null) {"),
loadGate: section(runtime, "async function performLoad(): Promise<void> {", "const pendingLoadConfig ="),
stagedMetadata: section(runtime, "if (isGguf && isDiffusion === undefined) {", "const targetIsDiffusion = isDiffusion === true;"),
@@ -106,6 +106,10 @@ const contracts: Contract[] = [
["cancel", /activeLoadRunRef\.current = releaseOwnedModelLoadRun\(/]],
order: [["preliminaryUnload", "await unloadModel({ model_path: currentCheckpoint });", "loadRun.residentModelUnloaded = true;"],
["cancel", "run.residentModelUnloaded", "activeLoadRunRef.current = releaseOwnedModelLoadRun("]] },
+ { name: "a GGUF that still has to download keeps the resident model until /load stops it",
+ match: [["runtime", /loadRun\.downloadKeepsResident = isGguf && !downloadComplete;\s*\/\/[^\n]*\n\s*if \(!forceCancelActive && !touchesOnlySelected && !loadRun\.downloadKeepsResident\) \{\s*await unloadModel\(/],
+ ["cancel", /\(run\.forceCancelActive \|\| run\.downloadKeepsResident\) &&\s*!run\.residentModelUnloaded/],
+ ["cancel", /\(run\.forceCancelActive \|\| run\.downloadKeepsResident\) &&\s*run\.residentModelUnloaded/]] },
{ name: "a cancelled preflight keeps the slot until its coroutine unwinds",
match: [["runtime", /settledPromise: Promise<void>;/], ["runtime", /markLoadRunSettled = resolve;/],
["runtime", /settledPromise: loadRunSettled,/],
diff --git a/studio/frontend/tests/resident-config-match.test.ts b/studio/frontend/tests/resident-config-match.test.ts
index 990f3b0eef4..45dbcc0050d 100644
--- a/studio/frontend/tests/resident-config-match.test.ts
+++ b/studio/frontend/tests/resident-config-match.test.ts
@@ -1749,7 +1749,7 @@ test("a pick asks the status about its own model and keeps or replaces the other
assert.match(USE_CHAT_MODEL_RUNTIME, /stopQueuedRuns\(stopDecision, keepsOthers \|\| touchesOnlySelected\);/);
assert.match(USE_CHAT_MODEL_RUNTIME, /if \(!keepsOthers && !touchesOnlySelected\) \{\s*requestLocalPromptQueueStop\(\);/);
assert.match(USE_CHAT_MODEL_RUNTIME, /if \(currentCheckpoint && !keepsOthers\)/);
- assert.match(USE_CHAT_MODEL_RUNTIME, /if \(!forceCancelActive && !touchesOnlySelected\) \{/);
+ assert.match(USE_CHAT_MODEL_RUNTIME, /if \(!forceCancelActive && !touchesOnlySelected\b/);
assert.equal(
USE_CHAT_MODEL_RUNTIME.match(/alongside: keepModelsLoaded \|\| touchesOnlySelected,/g)?.length,
2,