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 a stopped prompt in later chat turns (#10625)
Chaitanya Laxman855bfeed1c33
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex eedb149eded..7209f9c0686 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -257,6 +257,7 @@ import {budgetImpliesTruncation,CONTINUE_INSTRUCTION,createContinuationMerger,+ incompleteLabel,type IncompleteReason,readIncompleteInfo,resolveIncompleteReason,@@ -1325,7 +1326,10 @@ function toOpenAIMessages(}if (message.role === "assistant") {- return serializeAssistantReplayMessages(message, includeReasoningContent);+ return fillStoppedAssistantReplay(+ message,+ serializeAssistantReplayMessages(message, includeReasoningContent),+ );}const textContent = collectTextParts(message).join("\n");@@ -1364,6 +1368,46 @@ function assistantTurnEndedEarly(message: RunMessage): boolean {);}+/** #10428: an empty Stop loses its prompt to the prune. Only `cancelled` in `status` is deliberate. */+function stoppedAssistantReplayText(message: RunMessage): string {+ const info = readIncompleteInfo(+ (message as { metadata?: unknown }).metadata,+ );+ const status = message.status;+ const fromStatus: IncompleteReason =+ status?.type !== "incomplete" || status.reason === "cancelled"+ ? "cancelled"+ : status.reason === "length"+ ? "length"+ : "interrupted";+ return incompleteLabel(info?.reason ?? fromStatus);+}++function fillStoppedAssistantReplay(+ message: RunMessage,+ serialized: SerializedMessage[],+): SerializedMessage[] {+ if (!assistantTurnEndedEarly(message)) {+ return serialized;+ }+ if (serialized.length === 0) {+ // Only a refusal serialises to nothing; filling it puts a stop label back on a dropped turn.+ return serialized;+ }+ const [only, ...rest] = serialized;+ if (rest.length !== 0 || only?.role !== "assistant") {+ return serialized;+ }+ if (+ hasReplayContent(only.content) ||+ only.tool_calls ||+ only.reasoning_content+ ) {+ return serialized;+ }+ return [{ ...only, content: stoppedAssistantReplayText(message) }];+}+/** A Stop before the turn produced anything serialises to a lone empty assistant message,* stranding two user turns in a row. */function isAbandonedAssistantTurn(@@ -1963,7 +2007,8 @@ function waitForModelReady(abortSignal?: AbortSignal): Promise<void> {return new Promise((resolve, reject) => {const check = () => {if (abortSignal?.aborted) {- reject(new Error("Aborted"));+ // Uncaught in the adapter, and a Stop that is not an AbortError files as a failure (#10428).+ reject(abortSignal.reason ?? new DOMException("Aborted", "AbortError"));return;}if (!useChatRuntimeStore.getState().modelLoading) {@@ -6198,7 +6243,17 @@ export function createOpenAIStreamAdapter(releaseLiveGenerationRun(cancelId);}if (!generationRun) {- if (generationDecision === "durable") return;+ // Null when the Stop won the race AND when json() could not parse a 2xx body;+ // a bare return settles both "complete" (#10428).+ if (generationDecision === "durable") {+ if (runSignal.aborted) {+ throw runSignal.reason ??+ new DOMException("Aborted", "AbortError");+ }+ throw new Error(+ "The server accepted the request without starting a generation run",+ );+ }} else {generationRunId = generationRun.id;// Normally the same id claimed above; claimed again in case the server echoes a different one.diff --git a/studio/frontend/tests/cancelled-turn-history-prune.test.ts b/studio/frontend/tests/cancelled-turn-history-prune.test.tsindex 1a67dee378a..7efca12844d 100644--- a/studio/frontend/tests/cancelled-turn-history-prune.test.ts+++ b/studio/frontend/tests/cancelled-turn-history-prune.test.ts@@ -2,6 +2,7 @@// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0import assert from "node:assert/strict";+import { existsSync, readFileSync } from "node:fs";import test from "node:test";import { readSrc } from "./helpers/kit.ts";@@ -58,6 +59,78 @@ test("an abandoned turn is pruned with the user prompt that triggered it", () =>);});+test("a stopped empty assistant is filled before the prune sees it", () => {+ assert.match(adapter, /function fillStoppedAssistantReplay\(/);+ assert.match(+ adapter,+ /return fillStoppedAssistantReplay\(\s*message,\s*serializeAssistantReplayMessages\(/,+ );+ assert.match(adapter, /function stoppedAssistantReplayText\(/);+ assert.match(adapter, /incompleteLabel\(info\?\.reason \?\? fromStatus\)/);+});++test("the fill leaves a refusal suppressed", () => {+ assert.match(+ adapter,+ /if \(serialized\.length === 0\) \{[\s\S]{0,200}?return serialized;/,+ );+ assert.doesNotMatch(+ adapter,+ /serialized\.length === 0[\s\S]{0,200}content: stoppedAssistantReplayText/,+ );+});++test("a Stop while a model loads arrives as a Stop", () => {+ assert.match(+ adapter,+ /reject\(abortSignal\.reason \?\? new DOMException\("Aborted", "AbortError"\)\)/,+ );+});++test("a Stop that beats the durable admission arrives as a Stop", () => {+ assert.match(adapter, /if \(runSignal\.aborted\) \{/);+ assert.match(+ adapter,+ /throw runSignal\.reason \?\?\s*new DOMException\("Aborted", "AbortError"\)/,+ );+});++test("an admission that answered without a run is not replayed as a Stop", () => {+ // Ungated, a transport failure files as a cancellation: no Retry, and a stop label on the wire.+ assert.match(+ adapter,+ /throw new Error\(\s*"The server accepted the request without starting a generation run",/,+ );+});++test("assistant-ui still stops a run with an AbortError, not a bare detach marker", () => {+ // The forwarded reason only reads as a Stop while cancelRun throws this class, not the plain+ // `{ detach }` our tests stand in with; otherwise it must be normalised before it is thrown.+ const core = new URL(+ "../node_modules/@assistant-ui/core/dist/runtimes/local/local-thread-runtime-core.js",+ import.meta.url,+ );+ if (!existsSync(core)) return;+ const source = readFileSync(core, "utf8");+ assert.match(+ source,+ /class AbortError extends Error \{\s*name = "AbortError";/,+ );+ assert.match(+ source,+ /cancelRun\(\) \{\s*const error = new AbortError\(false\);/,+ );+ assert.match(source, /detach\(\) \{\s*const error = new AbortError\(true\);/);+});++test("only a deliberate Stop is replayed as one", () => {+ // Collapsing this back to a constant tells the model a failed turn was stopped.+ assert.match(+ adapter,+ /status\?\.type !== "incomplete" \|\| status\.reason === "cancelled"/,+ );+});+test("a trailing abandoned turn keeps the prompt it followed", () => {assert.match(adapter, /if \(refused \|\| index < lastSurviving\) \{/);});diff --git a/studio/frontend/tests/chat-generation-reconnect.test.ts b/studio/frontend/tests/chat-generation-reconnect.test.tsindex afc46a6e6f5..ecb0ad50e3f 100644--- a/studio/frontend/tests/chat-generation-reconnect.test.ts+++ b/studio/frontend/tests/chat-generation-reconnect.test.ts@@ -329,6 +329,19 @@ test("Stop during create cancels the run after its delayed reply", async () => {assert.equal(cancelled, 1);});+test("a null admission does not mean the run was stopped", async () => {+ // json() makes an unparseable 2xx body null and `ok` keeps it, so the create resolves null.+ globalThis.fetch = (async () =>+ new Response("", { status: 200 })) as typeof fetch;+ const controller = new AbortController();+ const created = await createChatGenerationRunUntilAbort(+ createInput(),+ controller.signal,+ );+ assert.equal(created, null);+ assert.equal(controller.signal.aborted, false);+});+test("Stop before admission resolves still reaches the server", () => {// Admission resolves long after the abort listener is installed (model auto-load,// RAG, attachment upload, first history save). A Stop in that window has no run iddiff --git a/tests/studio/test_cancelled_turn_history_prune.py b/tests/studio/test_cancelled_turn_history_prune.pyindex de06f254f4a..74999645803 100644--- a/tests/studio/test_cancelled_turn_history_prune.py+++ b/tests/studio/test_cancelled_turn_history_prune.py@@ -5,8 +5,8 @@The abandoned turn serialises to a lone empty assistant message, every backend drops it, andstrict templates (Ministral, Gemma) then refuse the two user turns that are left touching.-``pruneOutboundHistory`` drops that turn together with the prompt that triggered it, the way-refusals are already dropped.+``toOpenAIMessages`` fills it with ``incompleteLabel`` instead, so the prompt stays on the wire+and roles still alternate (#10428). Refusals and silent empty turns still drop with theirs.The prune, ``toOpenAIMessages`` and ``serializeAssistantReplayMessages`` are sliced verbatim outof the studio sources and run under ``node`` (see ``_node_harness``), so what is asserted is the@@ -128,7 +128,7 @@ def _harness_source() -> str:+ slice_between(read(CONTINUATION),"export type IncompleteReason =",- "const INCOMPLETE_LABELS",+ "export function stripContinuationOverlap(",)+ """// The refusal-only prune this fix replaces, kept so each case can show what the wire looked@@ -155,6 +155,17 @@ export function wireRoles(messages: any[], includeReasoningContent: boolean): st.map((message: any) => message.role);}+/** Same as wireRoles, but skips fillStoppedAssistantReplay so the #9484 pair still shows. */+export function wireRolesBeforeFill(messages: any[], includeReasoningContent: boolean): string[] {+ return messages+ .flatMap((message: any) => message.role === "assistant"+ ? serializeAssistantReplayMessages(message, includeReasoningContent)+ : toOpenAIMessages(message, includeReasoningContent))+ .filter((message: any) => !(message.role === "assistant" && !message.content+ && !message.tool_calls && !message.reasoning_content))+ .map((message: any) => message.role);+}+export { pruneOutboundHistory, toOpenAIMessages };"""+ _send_path_slice()@@ -171,13 +182,22 @@ CANCELLED = ('{ role: "assistant", content: [], status: { type: "incomplete" },'' metadata: { custom: { incomplete: { reason: "cancelled" } } } }')+# Nothing yielded, so status is the only record; a failure has the same shape under "error".+STOPPED_UNMARKED = (+ '{ role: "assistant", content: [], status: { type: "incomplete", reason: "cancelled" } }'+)+FAILED_UNMARKED = (+ '{ role: "assistant", content: [], status: { type: "incomplete", reason: "error" } }'+)+STOPPED = "Response stopped"+INTERRUPTED = "Response interrupted"def _script(history: str, include_reasoning: str = "true") -> str:return textwrap.dedent(f"""// @ts-nocheck- import {{ pruneOutboundHistory, pruneRefusalsOnly, toOpenAIMessages, wireRoles }}+ import {{ pruneOutboundHistory, pruneRefusalsOnly, toOpenAIMessages, wireRoles, wireRolesBeforeFill }}from "./harness.ts";const history = {history};const kept = pruneOutboundHistory(history, {include_reasoning});@@ -186,7 +206,7 @@ def _script(history: str, include_reasoning: str = "true") -> str:keptText: kept.flatMap((m) => (m.content ?? []).filter((p) => p.type === "text").map((p) => p.text)),wire: wireRoles(kept, {include_reasoning}),- wireBefore: wireRoles(pruneRefusalsOnly(history), {include_reasoning}),+ wireBefore: wireRolesBeforeFill(pruneRefusalsOnly(history), {include_reasoning}),}}));""")@@ -205,11 +225,13 @@ def test_the_defect_is_two_user_turns_touching_on_the_wire():)-def test_a_stop_before_any_output_takes_its_prompt_with_it():+def test_a_stop_before_any_output_keeps_its_prompt_on_the_wire():+ """#10428: the prompt must still transmit; #9484 wanted alternating roles, not deletion."""out = _run(_script(f"[{_user('first')}, {CANCELLED}, {_user('second')}]"))- assert out["kept"] == ["user"]- assert out["keptText"] == ["second"]- assert out["wire"] == ["user"]+ assert out["kept"] == ["user", "assistant", "user"]+ assert out["keptText"] == ["first", "second"]+ assert out["wire"] == ["user", "assistant", "user"]+ assert out["wireBefore"] == ["user", "user"]def test_a_reply_that_produced_text_is_kept_with_its_prompt():@@ -220,12 +242,12 @@ def test_a_reply_that_produced_text_is_kept_with_its_prompt():assert out["wire"] == ["user", "assistant", "user"]-def test_a_stop_during_reasoning_is_abandoned_on_both_serialisations():+def test_a_stop_during_reasoning_keeps_its_prompt_on_both_serialisations():"""An incomplete turn never replays its reasoning, so the local and external builds agree.They pass different ``includeReasoningContent``: the recount always sends true, the request- sends ``!isExternalRequest``. A turn cut mid-think must prune either way or the two paths- would price and send different histories.+ sends ``!isExternalRequest``. A turn cut mid-think must keep the prompt either way or+ the two paths would price and send different histories."""thinking = ('{ role: "assistant", content: [{ type: "reasoning", text: "let me think" }],'@@ -233,8 +255,13 @@ def test_a_stop_during_reasoning_is_abandoned_on_both_serialisations():)for include_reasoning in ("true", "false"):out = _run(_script(f"[{_user('first')}, {thinking}, {_user('second')}]", include_reasoning))- assert out["kept"] == ["user"], f"includeReasoningContent={include_reasoning}"- assert out["keptText"] == ["second"]+ assert out["kept"] == [+ "user",+ "assistant",+ "user",+ ], f"includeReasoningContent={include_reasoning}"+ assert out["keptText"] == ["first", "second"]+ assert out["wire"] == ["user", "assistant", "user"]def test_a_turn_that_called_a_tool_is_not_abandoned():@@ -260,12 +287,12 @@ def test_refusals_are_still_pruned_with_their_prompt():assert out["keptText"] == ["second"]-def test_back_to_back_stops_collapse_to_the_live_prompt():- """Stop twice and both abandoned pairs go, rather than one prune uncovering the next."""+def test_back_to_back_stops_keep_every_interrupted_prompt():history = f"[{_user('first')}, {CANCELLED}, {_user('second')}, {CANCELLED}, {_user('third')}]"out = _run(_script(history))- assert out["kept"] == ["user"]- assert out["keptText"] == ["third"]+ assert out["kept"] == ["user", "assistant", "user", "assistant", "user"]+ assert out["keptText"] == ["first", "second", "third"]+ assert out["wire"] == ["user", "assistant", "user", "assistant", "user"]assert out["wireBefore"] == ["user", "user", "user"]@@ -316,13 +343,19 @@ def test_a_tool_call_the_replay_cannot_carry_prunes_with_its_prompt():' toolName: "delete_file", args: "{}" }]' + marker)out = _run(_script(f"[{_user('first')}, {unreplayable}, {_user('second')}]"))- assert out["kept"] == ["user"], marker- assert out["keptText"] == ["second"]- assert out["wire"] == ["user"]assert out["wireBefore"] == ["user", "user"], ("the refusal-only prune must still strand the pair here, or this case has stopped ""measuring the defect it was written for")+ if marker == stopped:+ # Stopped, so the prompt is history. The unreplayable call stays off the wire.+ assert out["kept"] == ["user", "assistant", "user"], marker+ assert out["keptText"] == ["first", "second"]+ assert out["wire"] == ["user", "assistant", "user"]+ else:+ assert out["kept"] == ["user"], marker+ assert out["keptText"] == ["second"]+ assert out["wire"] == ["user"]def test_a_resultless_call_that_replays_without_role_tool_keeps_its_prompt():@@ -353,8 +386,12 @@ def test_a_trailing_abandoned_turn_keeps_the_prompt_it_followed():"""for include_reasoning in ("true", "false"):out = _run(_script(f"[{_user('first')}, {CANCELLED}]", include_reasoning))- assert out["kept"] == ["user"], f"includeReasoningContent={include_reasoning}"+ assert out["kept"] == [+ "user",+ "assistant",+ ], f"includeReasoningContent={include_reasoning}"assert out["keptText"] == ["first"]+ assert out["wire"] == ["user", "assistant"]def test_a_thread_that_is_only_a_stop_keeps_its_system_prompt_and_prompt():@@ -363,7 +400,7 @@ def test_a_thread_that_is_only_a_stop_keeps_its_system_prompt_and_prompt():f"{_user('first')}, {CANCELLED}]")out = _run(_script(history))- assert out["kept"] == ["system", "user"]+ assert out["kept"] == ["system", "user", "assistant"]assert out["keptText"] == ["sys", "first"]@@ -414,6 +451,54 @@ def test_the_send_path_builds_its_payload_out_of_pruned_history():"""for is_external in ("false", "true"):out = _run(_send_script(f"[{_user('first')}, {CANCELLED}, {_user('second')}]", is_external))+ assert out["roles"] == ["user", "assistant", "user"], f"isExternalRequest={is_external}"+ assert out["contents"] == ["first", STOPPED, "second"]+++def test_a_stop_with_no_persisted_marker_still_reads_as_a_stop():+ """The real shape: nothing is yielded before the first token, so only the status is left."""+ for is_external in ("false", "true"):+ out = _run(+ _send_script(f"[{_user('first')}, {STOPPED_UNMARKED}, {_user('second')}]", is_external)+ )+ assert out["roles"] == ["user", "assistant", "user"], f"isExternalRequest={is_external}"+ assert out["contents"] == ["first", STOPPED, "second"]+++def test_a_generation_that_failed_is_not_replayed_as_a_stop():+ """Same empty shape under ``reason: "error"``: the prompt stays, but "stopped" misreports why."""+ for is_external in ("false", "true"):+ out = _run(+ _send_script(f"[{_user('first')}, {FAILED_UNMARKED}, {_user('second')}]", is_external)+ )+ assert out["roles"] == ["user", "assistant", "user"], f"isExternalRequest={is_external}"+ assert out["contents"] == ["first", INTERRUPTED, "second"]+++def test_a_reloaded_stop_keeps_its_prompt_once_the_marker_was_persisted():+ """Reloaded, not in-session: ``status`` rebuilds as ``complete``, so the persisted marker is+ all that still says this was stopped. The durable path stamps it; the next test is the gap."""+ reloaded = (+ '{ role: "assistant", content: [], status: { type: "complete", reason: "unknown" },'+ ' metadata: { custom: { incomplete: { reason: "cancelled" } } } }'+ )+ for is_external in ("false", "true"):+ out = _run(_send_script(f"[{_user('first')}, {reloaded}, {_user('second')}]", is_external))+ assert out["roles"] == ["user", "assistant", "user"], f"isExternalRequest={is_external}"+ assert out["contents"] == ["first", STOPPED, "second"]+++def test_a_reloaded_stop_with_no_persisted_marker_is_still_dropped():+ """The known limit, pinned as a decision: nothing persists a marker, so the reloaded row is+ indistinguishable from a silent reply. Closing it means persisting at Stop time, not here."""+ reloaded_unmarked = (+ '{ role: "assistant", content: [], status: { type: "complete", reason: "unknown" },'+ " metadata: { custom: {} } }"+ )+ for is_external in ("false", "true"):+ out = _run(+ _send_script(f"[{_user('first')}, {reloaded_unmarked}, {_user('second')}]", is_external)+ )assert out["roles"] == ["user"], f"isExternalRequest={is_external}"assert out["contents"] == ["second"]@@ -426,7 +511,7 @@ def test_the_send_path_still_carries_an_answered_exchange():assert out["contents"] == ["first", "an answer", "second"]-def test_a_stop_that_produced_only_whitespace_takes_its_prompt_with_it():+def test_a_stop_that_produced_only_whitespace_keeps_its_prompt():"""Whitespace is not an answer, and the backend already agrees.``_build_external_messages`` drops any assistant turn whose string content trims away@@ -441,8 +526,9 @@ def test_a_stop_that_produced_only_whitespace_takes_its_prompt_with_it():' metadata: { custom: { incomplete: { reason: "cancelled" } } } }' % blank)out = _run(_script(f"[{_user('first')}, {whitespace}, {_user('second')}]"))- assert out["kept"] == ["user"], repr(blank)- assert out["keptText"] == ["second"]+ assert out["kept"] == ["user", "assistant", "user"], repr(blank)+ assert [text for text in out["keptText"] if text.strip()] == ["first", "second"]+ assert out["wire"] == ["user", "assistant", "user"]assert out["wireBefore"] == ["user", "assistant", "user"], ("the refusal-only prune kept the whitespace turn, which is the hop the backend ""trim then undid"@@ -454,3 +540,35 @@ def test_whitespace_only_replies_leave_no_pair_for_the_backend_to_split():whitespace = '{ role: "assistant", content: [{ type: "text", text: " " }] }'out = _run(_script(f"[{_user('first')}, {whitespace}, {_user('second')}]"))assert out["kept"] == ["user"]+++def test_a_refusal_is_never_filled_back_onto_the_wire():+ """A refusal is the only turn that serialises to nothing, and that is deliberate.++ ``serializeAssistantReplayMessages`` force-flushes every other assistant shape, so the+ empty-list branch of the fill is reachable only for a refusal. Synthesising a turn there+ puts a stop label back on the wire for the one message the serialiser suppressed, and the+ refusal can carry an incomplete marker of its own: a context_window refusal stamps both+ into the same ``custom`` object. Today ``pruneOutboundHistory`` drops it first either way,+ so this pins the layer rather than an outcome the prune already fixes.+ """+ refusals = (+ " metadata: { custom: { anthropicRefusal: true } } }",+ ' status: { type: "incomplete", reason: "cancelled" },'+ " metadata: { custom: { anthropicRefusal: true,"+ ' incomplete: { reason: "cancelled" } } } }',+ )+ for tail in refusals:+ refused = '{ role: "assistant", content: [],' + tail+ out = _run(+ textwrap.dedent(+ f"""+ // @ts-nocheck+ import {{ toOpenAIMessages }} from "./harness.ts";+ console.log(JSON.stringify({{+ serialized: toOpenAIMessages({refused}, true),+ }}));+ """+ )+ )+ assert out["serialized"] == [], tail