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: stop a chat switch mid-reply crashing the app on a stale message index (#13352)
Daniel Han5fb2e85b1bc4
diff --git a/studio/frontend/package-lock.json b/studio/frontend/package-lock.jsonindex 4cae7ae5885..b4006d369ab 100644--- a/studio/frontend/package-lock.json+++ b/studio/frontend/package-lock.json@@ -10,6 +10,7 @@"dependencies": {"@assistant-ui/core": "0.1.17","@assistant-ui/react": "0.12.28",+ "@assistant-ui/store": "0.2.9","@assistant-ui/tap": "0.5.10","@base-ui/react": "^1.2.0","@dagrejs/dagre": "^2.0.4",diff --git a/studio/frontend/package.json b/studio/frontend/package.jsonindex 12a434f48ba..e76530b6727 100644--- a/studio/frontend/package.json+++ b/studio/frontend/package.json@@ -25,6 +25,7 @@"dependencies": {"@assistant-ui/core": "0.1.17","@assistant-ui/react": "0.12.28",+ "@assistant-ui/store": "0.2.9","@assistant-ui/tap": "0.5.10","@base-ui/react": "^1.2.0","@dagrejs/dagre": "^2.0.4",diff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsxindex 89f3e507169..9eafcdbf5ae 100644--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx@@ -33,6 +33,7 @@ import {partsSearchImagesSignature,partsTextKey,} from "@/components/assistant-ui/message-derived";+import { useMessagePartTextSafe } from "@/components/assistant-ui/stale-safe-scopes";import { copyToClipboard } from "@/lib/copy-to-clipboard";import { normalizeEscapedInlineMath } from "@/lib/escaped-inline-math";import { preprocessLaTeX } from "@/lib/latex";@@ -46,7 +47,6 @@ import {INTERNAL,useAui,useAuiState,- useMessagePartText,} from "@assistant-ui/react";import {Copy01Icon,@@ -1287,7 +1287,7 @@ function MarkdownTextRenderer({const MarkdownTextImpl = () => {const allowSearchImages = useContext(SearchImagesEnabledContext);const aui = useAui();- const { text, status } = useMessagePartText();+ const { text, status } = useMessagePartTextSafe();const partIndex =aui.part.source === "message" && aui.part.query.type === "index"? aui.part.query.indexdiff --git a/studio/frontend/src/components/assistant-ui/progressive-messages.tsx b/studio/frontend/src/components/assistant-ui/progressive-messages.tsxindex 567ac2c1940..4f5bbbf078d 100644--- a/studio/frontend/src/components/assistant-ui/progressive-messages.tsx+++ b/studio/frontend/src/components/assistant-ui/progressive-messages.tsx@@ -14,12 +14,7 @@// every pre-existing row and React skips those subtrees. A `components` object would allocate fresh props per row// per commit, so every delete would re-render every body, action bar and tooltip: the regression #9042 removed.-import {- AuiProvider,- MessageByIndexProvider,- useAui,- useAuiState,-} from "@assistant-ui/react";+import { AuiProvider, useAui, useAuiState } from "@assistant-ui/react";import {type FC,type ReactElement,@@ -35,6 +30,7 @@ import {} from "react";import { createRowNotificationGate } from "@/components/assistant-ui/row-notification-gate";+import { MessageByIndexProvider } from "@/components/assistant-ui/stale-safe-scopes";import {type AnchorSample,type MountWindow,diff --git a/studio/frontend/src/components/assistant-ui/stale-safe-scope-logic.ts b/studio/frontend/src/components/assistant-ui/stale-safe-scope-logic.tsnew file mode 100644index 00000000000..32760a8f9d3--- /dev/null+++ b/studio/frontend/src/components/assistant-ui/stale-safe-scope-logic.ts@@ -0,0 +1,82 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// Pure halves of stale-safe-scopes.tsx, kept free of React so node tests can import them.++import type {+ MessagePartState,+ ReasoningMessagePart,+ TextMessagePart,+} from "@assistant-ui/core";++type PartSelector = { index: number } | { toolCallId: string };++interface MessageLike<P> {+ getState(): { readonly parts: readonly unknown[] };+ part(selector: PartSelector): P;+}++interface ThreadLike<M> {+ getState(): { readonly messages: readonly unknown[] };+ message(query: { index: number }): M;+}++/**+ * One row's message lookup that keeps the last message (and per part index, the last part) it resolved+ * while the index is past the end; never resolved throws as upstream (assistant-ui#5127, #5948).+ */+export function createStaleSafeMessageResolver<P, M extends MessageLike<P>>(+ index: number,+): (thread: ThreadLike<M>) => M {+ let last: M | undefined;+ const lastParts = new Map<number, P>();+ const guarded = new WeakMap<M, M>();+ const guard = (message: M): M => {+ let proxy = guarded.get(message);+ if (proxy === undefined) {+ const part = (selector: PartSelector): P => {+ if (!("index" in selector)) return message.part(selector);+ const kept = lastParts.get(selector.index);+ if (+ kept !== undefined &&+ selector.index >= message.getState().parts.length+ ) {+ return kept;+ }+ const resolved = message.part(selector);+ lastParts.set(selector.index, resolved);+ return resolved;+ };+ // Not `new Proxy(message)`: the client is a Proxy reporting absent keys as non-configurable, which+ // trips the get invariant. Receiver stays `message`, since the client rebinds methods per receiver.+ proxy = new Proxy({} as M, {+ get: (_, prop) => (prop === "part" ? part : Reflect.get(message, prop)),+ has: (_, prop) => prop === "part" || Reflect.has(message, prop),+ });+ guarded.set(message, proxy);+ }+ return proxy;+ };+ return (thread) => {+ if (last === undefined || index < thread.getState().messages.length) {+ last = thread.message({ index });+ }+ return guard(last);+ };+}++export type TextPartState = MessagePartState &+ (TextMessagePart | ReasoningMessagePart);++export const EMPTY_TEXT_PART: TextPartState = Object.freeze({+ type: "text",+ text: "",+ status: Object.freeze({ type: "complete" }),+}) as TextPartState;++/** A text leaf whose index now points at another part type reads an empty text part, never throws. */+export function textPartOrEmpty(part: MessagePartState): TextPartState {+ return part.type === "text" || part.type === "reasoning"+ ? (part as TextPartState)+ : EMPTY_TEXT_PART;+}diff --git a/studio/frontend/src/components/assistant-ui/stale-safe-scopes.tsx b/studio/frontend/src/components/assistant-ui/stale-safe-scopes.tsxnew file mode 100644index 00000000000..27776671dbe--- /dev/null+++ b/studio/frontend/src/components/assistant-ui/stale-safe-scopes.tsx@@ -0,0 +1,44 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// A thread switch swaps the store before React unmounts the outgoing rows, so a row re-rendering in+// that window reads the incoming thread at its old index; assistant-ui 0.12 throws there (#10288).+// Backports assistant-ui#5119, #5127 and #5948 for the scopes Studio renders.++import type { MessageMethods, PartMethods } from "@assistant-ui/core/store";+import { AuiProvider, useAui, useAuiState } from "@assistant-ui/react";+import { Derived } from "@assistant-ui/store";+import { type FC, type PropsWithChildren, useMemo } from "react";++import {+ type TextPartState,+ createStaleSafeMessageResolver,+ textPartOrEmpty,+} from "@/components/assistant-ui/stale-safe-scope-logic";++/** `MessageByIndexProvider` whose row keeps its last message and parts while their index is past the end. */+export const MessageByIndexProvider: FC<+ PropsWithChildren<{ index: number }>+> = ({ index, children }) => {+ const resolve = useMemo(+ () => createStaleSafeMessageResolver<PartMethods, MessageMethods>(index),+ [index],+ );+ const aui = useAui({+ message: Derived({+ source: "thread",+ query: { type: "index", index },+ get: (aui) => resolve(aui.thread()),+ }),+ composer: Derived({+ source: "message",+ query: {},+ get: (aui) => resolve(aui.thread()).composer(),+ }),+ });+ return <AuiProvider value={aui}>{children}</AuiProvider>;+};++/** `useMessagePartText` that reads an empty text part instead of throwing on a mismatched part. */+export const useMessagePartTextSafe = (): TextPartState =>+ useAuiState((s) => textPartOrEmpty(s.part));diff --git a/studio/frontend/tests/stale-safe-scopes.test.ts b/studio/frontend/tests/stale-safe-scopes.test.tsnew file mode 100644index 00000000000..18b6bff1d27--- /dev/null+++ b/studio/frontend/tests/stale-safe-scopes.test.ts@@ -0,0 +1,184 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// #10288: a leaf still mounted from the outgoing thread re-renders before React unmounts it and+// reads the incoming thread through its old index. assistant-ui 0.12 threw there and took down the app.++import assert from "node:assert/strict";+import test from "node:test";++import {+ EMPTY_TEXT_PART,+ createStaleSafeMessageResolver,+ textPartOrEmpty,+} from "../src/components/assistant-ui/stale-safe-scope-logic.ts";+import { readSrc } from "./helpers/kit.ts";++function lookup<T>(items: readonly T[], index: number): T {+ if (index < 0 || index >= items.length) {+ throw new Error(+ `tapClientLookup: Index ${index} out of bounds (length: ${items.length})`,+ );+ }+ return items[index];+}++/** A message client whose `part({ index })` throws exactly as assistant-ui's tapClientLookup does. */+function message(name: string, parts: string[] = ["text"]) {+ return {+ name,+ getState: () => ({ parts }),+ part: (selector: { index: number } | { toolCallId: string }) =>+ "index" in selector+ ? `${name}.${lookup(parts, selector.index)}`+ : `${name}.tool`,+ };+}+type Msg = ReturnType<typeof message>;++function thread(messages: Msg[]) {+ return {+ getState: () => ({ messages }),+ message: ({ index }: { index: number }) => lookup(messages, index),+ };+}++const resolver = (index: number) =>+ createStaleSafeMessageResolver<string, Msg>(index);++test("New chat: the outgoing reply row keeps its message instead of throwing on the empty thread", () => {+ const resolve = resolver(1);+ assert.equal(+ resolve(thread([message("user"), message("assistant")])).name,+ "assistant",+ );+ // switchToNewThread() installs an empty thread while the row at index 1 is still mounted.+ const empty = thread([]);+ assert.throws(+ () => empty.message({ index: 1 }),+ /Index 1 out of bounds \(length: 0\)/,+ );+ assert.equal(resolve(empty).name, "assistant");+ // Opening a saved chat that is shorter, or still loading, is the same window.+ assert.equal(resolve(thread([message("other user")])).name, "assistant");+});++test("an in-range index always reads the current thread, so a same-length switch shows the new chat", () => {+ const resolve = resolver(1);+ assert.equal(resolve(thread([message("u"), message("first")])).name, "first");+ assert.equal(+ resolve(thread([message("u"), message("second")])).name,+ "second",+ );+ assert.equal(+ resolve(thread([message("u"), message("a"), message("u"), message("b")]))+ .name,+ "a",+ );+});++test("a part leaf past the incoming message's parts keeps its last part instead of throwing", () => {+ const resolve = resolver(1);+ const streaming = message("streaming", ["reasoning", "text"]);+ assert.equal(+ resolve(thread([message("u"), streaming])).part({ index: 1 }),+ "streaming.text",+ );+ // The chat opened mid-reply has one part at that message index.+ const saved = message("saved", ["text"]);+ assert.throws(+ () => saved.part({ index: 1 }),+ /Index 1 out of bounds \(length: 1\)/,+ );+ const row = resolve(thread([message("u"), saved]));+ assert.equal(row.name, "saved");+ assert.equal(row.part({ index: 1 }), "streaming.text");+ assert.equal(row.part({ index: 0 }), "saved.text");+ assert.equal(row.part({ toolCallId: "call_0" }), "saved.tool");+ // One wrapper per message client, so the scope's identity is stable across reads.+ assert.equal(resolve(thread([message("u"), saved])), row);+});++test("wrapping a real assistant-ui client proxy reads through it without tripping Proxy invariants", () => {+ // Shaped like @assistant-ui/store's ClientProxyHandler: an empty target, and a+ // getOwnPropertyDescriptor (BaseProxyHandler) that reports every key as non-configurable.+ const OUTPUT = Symbol("assistant-ui.store.getValue");+ const impl: Record<PropertyKey, unknown> = {+ ...message("assistant", ["text"]),+ [OUTPUT]: { getState: () => ({ parts: ["text"] }) },+ };+ const client = new Proxy({} as Msg, {+ get: (_, prop) => impl[prop],+ has: (_, prop) => prop in impl,+ getOwnPropertyDescriptor: (_, prop) =>+ impl[prop] === undefined+ ? undefined+ : {+ value: impl[prop],+ writable: false,+ enumerable: true,+ configurable: false,+ },+ });+ const row = resolver(1)(thread([message("u"), client]));+ assert.equal(row.name, "assistant");+ assert.equal(+ (row as unknown as Record<symbol, unknown>)[OUTPUT],+ impl[OUTPUT],+ );+ assert.ok(OUTPUT in row);+ assert.equal(row.part({ index: 0 }), "assistant.text");+});++test("a row or part that never resolved still reports the lookup error", () => {+ assert.throws(+ () => resolver(3)(thread([message("u")])),+ /Index 3 out of bounds \(length: 1\)/,+ );+ const row = resolver(0)(thread([message("u", ["text"])]));+ assert.throws(+ () => row.part({ index: 2 }),+ /Index 2 out of bounds \(length: 1\)/,+ );+});++test("a text leaf pointed at a tool call reads one frozen empty text part", () => {+ const tool = {+ type: "tool-call",+ toolName: "python",+ status: { type: "running" },+ };+ const first = textPartOrEmpty(tool as never);+ assert.equal(first, EMPTY_TEXT_PART);+ // Same object every time: useSyncExternalStore loops on a snapshot that changes per read.+ assert.equal(textPartOrEmpty(tool as never), first);+ assert.ok(Object.isFrozen(first));+ assert.deepEqual(first, {+ type: "text",+ text: "",+ status: { type: "complete" },+ });+ for (const part of [+ { type: "text", text: "hi", status: { type: "running" } },+ { type: "reasoning", text: "hmm", status: { type: "complete" } },+ ]) {+ assert.equal(textPartOrEmpty(part as never), part);+ }+});++test("Studio reads rows and text parts only through the stale-safe scopes", () => {+ const rows = readSrc("components/assistant-ui/progressive-messages.tsx");+ assert.match(+ rows,+ /import \{ MessageByIndexProvider \} from "@\/components\/assistant-ui\/stale-safe-scopes";/,+ );+ const markdown = readSrc("components/assistant-ui/markdown-text.tsx");+ assert.match(markdown, /useMessagePartTextSafe\(\)/);+ assert.doesNotMatch(markdown, /\buseMessagePartText\b/);+ const scopes = readSrc("components/assistant-ui/stale-safe-scopes.tsx");+ assert.match(scopes, /get: \(aui\) => resolve\(aui\.thread\(\)\),/);+ assert.match(+ scopes,+ /get: \(aui\) => resolve\(aui\.thread\(\)\)\.composer\(\),/,+ );+});