Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: keep queued prompts when generation is stopped
Chaitanya Laxman0c329be6a185
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsxindex 2916dd45e88..bc507bff51b 100644--- a/studio/frontend/src/components/assistant-ui/thread.tsx+++ b/studio/frontend/src/components/assistant-ui/thread.tsx@@ -185,6 +185,7 @@ import {localPromptQueueModelBoundary,notifyPromptQueueRunFailed,planLocalPromptQueueStop,+ planUserPromptQueueStop,registerQueuedChatRunSettings,releasePreStreamRunReservation,reservePreStreamRun,@@ -382,6 +383,7 @@ type PromptQueueRun = {generation: number;prevStoreRunning: boolean;waitingForTargetIdle: boolean;+ paused: boolean;retryTimer: ReturnType<typeof setTimeout> | null;deepResearchConsumed: boolean;};@@ -649,6 +651,7 @@ function isPromptQueueRunReadyToDispatch(run: PromptQueueRun) {run.index >= 0 &&!item.dispatched &&!run.waitingForTargetIdle &&+ !run.paused &&!run.retryTimer &&!promptQueueActiveRunIds.has(run.id) &&!promptQueueDispatchingRunIds.has(run.id),@@ -1170,6 +1173,9 @@ function handlePromptQueueRunState(if (!wasRunning || isRunning) {return;}+ if (run.paused) {+ return;+ }if (run.waitingForTargetIdle) {clearPromptQueueRetryTimer(run);run.waitingForTargetIdle = false;@@ -1216,15 +1222,21 @@ function startPromptQueue(waitForCurrentRun = false,) {const filtered = items.map((item) => item.trim()).filter(Boolean);+ const existingRun = findPromptQueueRunByTarget(target);if (filtered.length === 0) {+ if (existingRun?.paused) {+ existingRun.paused = false;+ syncPromptQueueUI();+ requestPromptQueuePump();+ }return;}- const existingRun = findPromptQueueRunByTarget(target);if (existingRun) {if (existingRun.deepResearchConsumed) {target.consumeDeepResearch();}+ existingRun.paused = false;existingRun.items.push(...filtered.map((prompt) => createQueuedPrompt(prompt, target)),);@@ -1244,6 +1256,7 @@ function startPromptQueue(generation: 0,prevStoreRunning: shouldWaitForCurrentRun,waitingForTargetIdle: false,+ paused: false,retryTimer: null,deepResearchConsumed: false,};@@ -1280,14 +1293,34 @@ function getPromptQueueRunsForThreadIds(threadIds?: string[]) {function stopPromptQueueRun(threadIds?: string[]) {for (const run of getPromptQueueRunsForThreadIds(threadIds)) {const activeItem = getActivePromptQueueItem(run);- const activeTarget = activeItem?.target;- const shouldCancelActiveRun = Boolean(activeItem?.dispatched);- deletePromptQueueRun(run);- if (!shouldCancelActiveRun) {+ const plan = planUserPromptQueueStop(+ run.items.map((item) => ({ dispatched: item.dispatched })),+ run.index,+ );+ if (plan.retainedItemIndexes.length === 0) {+ deletePromptQueueRun(run);+ } else {+ run.items = plan.retainedItemIndexes.map((index) => run.items[index]);+ const nextIndex = run.items.findIndex((item) => !item.dispatched);+ if (nextIndex < 0) {+ deletePromptQueueRun(run);+ } else {+ run.generation += 1;+ run.index = nextIndex;+ run.paused = plan.pause;+ run.waitingForTargetIdle = false;+ run.prevStoreRunning = false;+ clearPromptQueueRetryTimer(run);+ promptQueueActiveRunIds.delete(run.id);+ promptQueueDispatchingRunIds.delete(run.id);+ syncPromptQueueUI();+ }+ }+ if (!plan.cancelActiveItem) {continue;}try {- activeTarget?.cancel();+ activeItem?.target.cancel();} catch {// The active run may have already ended.}@@ -1376,6 +1409,9 @@ function stopLocalPromptQueueRunsForThreadIds(threadIds: string[]) {}function retainPendingPromptQueueItemsAfterFailure(run: PromptQueueRun) {+ if (run.paused) {+ return true;+ }const activeIndex = Math.max(run.index, 0);const activeItem = run.items[activeIndex];if (run.index < 0 || !activeItem?.dispatched) {diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.tsindex c85fbba06a6..60b79737f41 100644--- a/studio/frontend/src/features/chat/index.ts+++ b/studio/frontend/src/features/chat/index.ts@@ -165,6 +165,7 @@ export {shouldAbortPendingQueueForModelBoundary,shouldAbortPendingQueueForSettingsChange,} from "./utils/prompt-queue-model-boundary";+export { planUserPromptQueueStop } from "./utils/prompt-queue-user-stop";export { chatHistoryClearBoundary } from "./utils/chat-history-clear-boundary";export { rangeBetween, toggleSelected } from "./utils/row-selection";export {diff --git a/studio/frontend/src/features/chat/utils/prompt-queue-user-stop.ts b/studio/frontend/src/features/chat/utils/prompt-queue-user-stop.tsnew file mode 100644index 00000000000..d5ee035cd1f--- /dev/null+++ b/studio/frontend/src/features/chat/utils/prompt-queue-user-stop.ts@@ -0,0 +1,37 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++export type PromptQueueUserStopItem = {+ dispatched: boolean;+};++export type UserPromptQueueStopPlan = {+ cancelActiveItem: boolean;+ retainedItemIndexes: number[];+ pause: boolean;+};++/** Stop generation without discarding prompts that have not been sent yet. */+export function planUserPromptQueueStop(+ items: readonly PromptQueueUserStopItem[],+ runIndex: number,+): UserPromptQueueStopPlan {+ if (runIndex < 0) {+ return {+ cancelActiveItem: false,+ retainedItemIndexes: items.map((_, index) => index),+ pause: items.length > 0,+ };+ }+ const activeIndex = runIndex;+ const activeItem = items[activeIndex];+ const dropActive = Boolean(activeItem?.dispatched);+ const retainedItemIndexes = items.flatMap((_, index) =>+ dropActive && index === activeIndex ? [] : [index],+ );+ return {+ cancelActiveItem: dropActive,+ retainedItemIndexes,+ pause: retainedItemIndexes.some((index) => !items[index]?.dispatched),+ };+}diff --git a/studio/frontend/tests/prompt-queue-user-stop.test.ts b/studio/frontend/tests/prompt-queue-user-stop.test.tsnew file mode 100644index 00000000000..4862393deca--- /dev/null+++ b/studio/frontend/tests/prompt-queue-user-stop.test.ts@@ -0,0 +1,79 @@+import assert from "node:assert/strict";+import test from "node:test";++import { planUserPromptQueueStop } from "../src/features/chat/utils/prompt-queue-user-stop.ts";++test("a dispatched stop keeps pending follow-ups and pauses", () => {+ assert.deepEqual(+ planUserPromptQueueStop(+ [+ { dispatched: true },+ { dispatched: false },+ { dispatched: false },+ ],+ 0,+ ),+ {+ cancelActiveItem: true,+ retainedItemIndexes: [1, 2],+ pause: true,+ },+ );+});++test("a dispatched stop with no follow-ups leaves nothing to resume", () => {+ assert.deepEqual(+ planUserPromptQueueStop([{ dispatched: true }], 0),+ {+ cancelActiveItem: true,+ retainedItemIndexes: [],+ pause: false,+ },+ );+});++test("an undispatched stop keeps the whole queue and pauses", () => {+ assert.deepEqual(+ planUserPromptQueueStop(+ [{ dispatched: false }, { dispatched: false }],+ 0,+ ),+ {+ cancelActiveItem: false,+ retainedItemIndexes: [0, 1],+ pause: true,+ },+ );+});++test("completed history is kept when a later dispatched item is stopped", () => {+ assert.deepEqual(+ planUserPromptQueueStop(+ [+ { dispatched: true },+ { dispatched: true },+ { dispatched: false },+ ],+ 1,+ ),+ {+ cancelActiveItem: true,+ retainedItemIndexes: [0, 2],+ pause: true,+ },+ );+});++test("a queue still waiting to start is paused without cancelling", () => {+ assert.deepEqual(+ planUserPromptQueueStop(+ [{ dispatched: false }, { dispatched: false }],+ -1,+ ),+ {+ cancelActiveItem: false,+ retainedItemIndexes: [0, 1],+ pause: true,+ },+ );+});diff --git a/tests/studio/test_prompt_queue_user_stop.py b/tests/studio/test_prompt_queue_user_stop.pynew file mode 100644index 00000000000..e5c91bc122a--- /dev/null+++ b/tests/studio/test_prompt_queue_user_stop.py@@ -0,0 +1,77 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Stop generation must keep prompts that have not been sent yet.++https://github.com/unslothai/unsloth/issues/10428+"""++from pathlib import Path+++REPO = Path(__file__).resolve().parents[2]+FRONTEND = REPO / "studio/frontend/src"+THREAD = (FRONTEND / "components/assistant-ui/thread.tsx").read_text(encoding = "utf-8")+PLANNER_PATH = FRONTEND / "features/chat/utils/prompt-queue-user-stop.ts"+BARREL = (FRONTEND / "features/chat/index.ts").read_text(encoding = "utf-8")+++def _between(source: str, start: str, end: str) -> str:+ assert start in source, f"missing start marker: {start}"+ tail = source.split(start, 1)[1]+ assert end in tail, f"missing end marker after {start}: {end}"+ return tail.split(end, 1)[0]+++def test_user_stop_plans_with_pending_items_kept():+ assert PLANNER_PATH.is_file()+ planner = PLANNER_PATH.read_text(encoding = "utf-8")+ assert "export function planUserPromptQueueStop(" in planner+ assert 'from "./utils/prompt-queue-user-stop"' in BARREL+ stop = _between(+ THREAD,+ "function stopPromptQueueRun(threadIds?: string[])",+ "function stopPromptQueueRunForThreadIds(threadIds: string[])",+ )+ assert "planUserPromptQueueStop(" in stop+ assert stop.index("planUserPromptQueueStop(") < stop.index(+ "deletePromptQueueRun(run);"+ )+ assert "run.paused = plan.pause" in stop+ assert "run.generation += 1" in stop+++def test_paused_queue_does_not_dispatch_or_advance():+ ready = _between(+ THREAD,+ "function isPromptQueueRunReadyToDispatch(run: PromptQueueRun)",+ "function getNextReadyPromptQueueRun()",+ )+ assert "!run.paused" in ready+ run_state = _between(+ THREAD,+ "function handlePromptQueueRunState(",+ "function ensurePromptQueueSubscription()",+ )+ assert run_state.index("if (run.paused)") < run_state.index(+ "advancePromptQueue(run);"+ )+ retained = _between(+ THREAD,+ "function retainPendingPromptQueueItemsAfterFailure(run: PromptQueueRun)",+ "function cancelPendingPromptQueueFactoriesForStop<",+ )+ assert retained.index("if (run.paused)") < retained.index(+ "run.items.splice(activeIndex, 1);"+ )+++def test_start_unpauses_an_existing_run():+ start = _between(+ THREAD,+ "function startPromptQueue(",+ "function getPromptQueueRunsForThreadIds(threadIds?: string[])",+ )+ assert "existingRun.paused = false" in start+ assert "existingRun?.paused" in start+ assert "paused: false" in start