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.

Fix steering follow-ups disappearing when a response is cancelled (#13249)

Lee Jackson028db7b16dec
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 859f2f9b2a0..7f9a12ca343 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -1302,7 +1302,12 @@ function handlePromptQueueRunState(
if (run.paused) {
return;
}
- if (run.waitingForTargetIdle) {
+ // An idle edge before an unsent prompt dispatches belongs to the cancelled response (steering
+ // race with the dispatch timer); index -1 is the normal wait for the current response.
+ if (
+ run.waitingForTargetIdle ||
+ (run.index >= 0 && !getActivePromptQueueItem(run)?.dispatched)
+ ) {
clearPromptQueueRetryTimer(run);
run.waitingForTargetIdle = false;
const activeItem = run.items[run.index];
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx
index 0d63288bac1..55c681ce306 100644
--- a/studio/frontend/src/features/chat/runtime-provider.tsx
+++ b/studio/frontend/src/features/chat/runtime-provider.tsx
@@ -2226,19 +2226,20 @@ function createPersistedRunAdapter(
let adoptedThreadId: string | undefined;
try {
adoptedThreadId = await waitForRunStartHistoryAppend(options.messages);
- throwIfReservationCancelled();
} catch (error) {
if (reservationToken) {
releasePreStreamRunReservation(reservationToken);
}
- // Queued runs carry no direct-send reservation and their persisted preflight can still fail
- // before the model adapter consumes the queued settings, so match pending/waiting work too.
+ // The history write ignores abortSignal, so a rejection here is a real failure (or clear-all)
+ // even after a steer. Queued runs carry no reservation, so this also stops pending work.
requestPromptQueueStop(persistedRunThreadIds);
notifyPromptQueueRunFailed(
options.unstable_threadId ?? persistedRunThreadIds[0] ?? null,
);
throw error;
}
+ // Outside the catch: steering's reservation cancel is not a history write failure.
+ throwIfReservationCancelled();
if (reservationToken && adoptedThreadId) {
adoptPreStreamRunReservation(reservationToken, [
...reservationThreadIds,
diff --git a/studio/frontend/tests/composer-steering.test.ts b/studio/frontend/tests/composer-steering.test.ts
index 6300f6895d7..57ee67a8e0a 100644
--- a/studio/frontend/tests/composer-steering.test.ts
+++ b/studio/frontend/tests/composer-steering.test.ts
@@ -1182,3 +1182,68 @@ for (const behavior of ["queue", "steer"] as const) {
assert.deepEqual(w.appended, ["use the selected provider"]);
});
}
+
+for (const mode of ["composer", "queue-row"] as const) {
+ test(`${mode} steer survives old-run completion before dispatch timer`, async () => {
+ const w = world();
+ const target = makeTarget("chat", true);
+ if (mode === "composer") {
+ w.startPromptQueue(["urgent steer"], target, true, "steer");
+ } else {
+ w.startPromptQueue(["urgent steer"], target, true);
+ assert.equal(w.steerPromptQueueItem(w.run().items[0].id), true);
+ }
+ const run = w.run();
+ const steer = run.items[0];
+ assert.equal(target.cancels, 1);
+ assert.equal(steer.dispatched, false);
+ // Old run still alive in the store, then idle before the queued pump runs.
+ w.handlePromptQueueRunState(run, { chat: true });
+ target.running = false;
+ w.handlePromptQueueRunState(run, {});
+ assert.equal(
+ w.runs.has(run.id),
+ true,
+ "old-run completion deleted the unsent steer from the queue",
+ );
+ await w.dispatchQueuedPrompt(run, steer);
+ assert.deepEqual(w.appended, ["urgent steer"]);
+ });
+}
+
+test("an old cancellation edge preserves the steer ahead of later queued prompts", async () => {
+ const w = world();
+ const target = makeTarget("chat", true);
+ w.startPromptQueue(["old response", "later prompt"], target);
+ const run = w.run();
+ run.items[0].dispatched = true;
+ w.startPromptQueue(["urgent steer"], target, true, "steer");
+ const steer = run.items[0];
+ w.handlePromptQueueRunState(run, { chat: true });
+ target.running = false;
+ w.handlePromptQueueRunState(run, {});
+ assert.equal(run.items[run.index], steer);
+ await w.dispatchQueuedPrompt(run, steer);
+ assert.deepEqual(w.appended, ["urgent steer"]);
+ // Only completion of the steer itself may advance to the later prompt.
+ target.running = true;
+ w.handlePromptQueueRunState(run, { chat: true });
+ target.running = false;
+ w.handlePromptQueueRunState(run, {});
+ await w.dispatchQueuedPrompt(run, run.items[run.index]);
+ assert.deepEqual(w.appended, ["urgent steer", "later prompt"]);
+});
+
+test("a normal queue waiting at index -1 still starts after the current response", async () => {
+ const w = world();
+ const target = makeTarget("chat", true);
+ w.startPromptQueue(["next prompt"], target, true, "queue");
+ const run = w.run();
+ assert.equal(run.index, -1);
+ target.running = false;
+ w.handlePromptQueueRunState(run, {});
+ assert.equal(run.index, 0);
+ await w.dispatchQueuedPrompt(run, run.items[0]);
+ assert.deepEqual(w.appended, ["next prompt"]);
+ assert.equal(target.cancels, 0);
+});
diff --git a/studio/frontend/tests/persisted-run-steering.test.ts b/studio/frontend/tests/persisted-run-steering.test.ts
new file mode 100644
index 00000000000..fac7ff7666f
--- /dev/null
+++ b/studio/frontend/tests/persisted-run-steering.test.ts
@@ -0,0 +1,149 @@
+// 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 ts from "typescript";
+import { readSrc } from "./helpers/kit.ts";
+
+// Execute the production adapter wrapper around a controlled pending history write.
+const source = ts.createSourceFile(
+ "runtime-provider.tsx",
+ readSrc("features/chat/runtime-provider.tsx"),
+ ts.ScriptTarget.Latest,
+ true,
+ ts.ScriptKind.TSX,
+);
+const declaration = source.statements.find(
+ (node) =>
+ ts.isFunctionDeclaration(node) &&
+ node.name?.text === "createPersistedRunAdapter",
+);
+assert.ok(declaration);
+const js = ts.transpileModule(declaration.getText(source), {
+ compilerOptions: {
+ target: ts.ScriptTarget.ES2022,
+ module: ts.ModuleKind.None,
+ },
+}).outputText;
+
+function pendingRun(hasReservation = true) {
+ const controller = new AbortController();
+ const stops: string[][] = [];
+ const failures: string[] = [];
+ let cancelled = false;
+ let released = 0;
+ let calls = 0;
+ let resolve!: (id: string) => void;
+ let reject!: (error: Error) => void;
+ const preflight = new Promise<string>((yes, no) => {
+ resolve = yes;
+ reject = no;
+ });
+ const deps = {
+ runStartThreadIdsForMessages: () => ["chat"],
+ preStreamRunThreadIdsForRuntime: (ids: string[]) => [
+ ...new Set(ids.filter(Boolean)),
+ ],
+ useChatRuntimeStore: { getState: () => ({ activeThreadId: "chat" }) },
+ findPreStreamRunReservation: () => (hasReservation ? "reservation" : null),
+ claimPreStreamRunReservation: () => {},
+ isPreStreamRunReservationCancelled: () => cancelled,
+ releasePreStreamRunReservation: () => {
+ released += 1;
+ },
+ waitForRunStartHistoryAppend: () => preflight,
+ requestPromptQueueStop: (ids: string[]) => stops.push(ids),
+ notifyPromptQueueRunFailed: (id: string) => failures.push(id),
+ adoptPreStreamRunReservation: () => {},
+ };
+ const factory = new Function(
+ ...Object.keys(deps),
+ `${js}; return createPersistedRunAdapter;`,
+ )(...Object.values(deps));
+ const adapter = factory({
+ async *run() {
+ calls += 1;
+ yield { content: "answer" };
+ },
+ });
+ const run = adapter.run({
+ messages: [],
+ unstable_threadId: "chat",
+ abortSignal: controller.signal,
+ }) as AsyncGenerator<{ content: string }>;
+ return {
+ run,
+ resolve,
+ reject,
+ stops,
+ failures,
+ controller,
+ cancelReservation: () => {
+ cancelled = true;
+ },
+ released: () => released,
+ calls: () => calls,
+ };
+}
+
+test("steering during history persistence cancels only the old run", async () => {
+ const w = pendingRun();
+ const next = w.run.next();
+ w.cancelReservation();
+ w.controller.abort();
+ w.resolve("chat");
+ await assert.rejects(next, { name: "AbortError" });
+ assert.deepEqual(
+ w.stops,
+ [],
+ "an intentional cancellation must not delete accepted follow-ups",
+ );
+ assert.deepEqual(w.failures, []);
+ assert.equal(w.calls(), 0);
+ assert.equal(w.released(), 1);
+});
+
+test("reservation cancellation before signal propagation does not stop the follow-up queue", async () => {
+ const w = pendingRun();
+ const next = w.run.next();
+ w.cancelReservation();
+ w.resolve("chat");
+ await assert.rejects(next, { name: "AbortError" });
+ assert.deepEqual(w.stops, []);
+ assert.deepEqual(w.failures, []);
+ assert.equal(w.calls(), 0);
+});
+
+test("a genuine persistence failure after a steer abort still stops the queue", async () => {
+ const w = pendingRun(false);
+ const next = w.run.next();
+ w.controller.abort();
+ w.reject(new Error("history write failed"));
+ await assert.rejects(next, /history write failed/);
+ assert.deepEqual(w.stops, [["chat"]]);
+ assert.deepEqual(w.failures, ["chat"]);
+ assert.equal(w.calls(), 0);
+});
+
+test("a genuine history persistence failure still stops and reports the affected queue", async () => {
+ const w = pendingRun();
+ const next = w.run.next();
+ w.reject(new Error("history write failed"));
+ await assert.rejects(next, /history write failed/);
+ assert.deepEqual(w.stops, [["chat"]]);
+ assert.deepEqual(w.failures, ["chat"]);
+ assert.equal(w.calls(), 0);
+ assert.equal(w.released(), 1);
+});
+
+test("successful history persistence starts the adapter once", async () => {
+ const w = pendingRun();
+ const next = w.run.next();
+ w.resolve("chat");
+ assert.deepEqual(await next, { value: { content: "answer" }, done: false });
+ assert.equal((await w.run.next()).done, true);
+ assert.equal(w.calls(), 1);
+ assert.deepEqual(w.stops, []);
+ assert.deepEqual(w.failures, []);
+});