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: long chats stop re-rendering source badges per token and tool cards per run (#16277)

Daniel Hanedde2920f6f5
diff --git a/.github/workflows/studio-frontend-ci.yml b/.github/workflows/studio-frontend-ci.yml
index 5ca0c04f61b..309b41c62a4 100644
--- a/.github/workflows/studio-frontend-ci.yml
+++ b/.github/workflows/studio-frontend-ci.yml
@@ -40,6 +40,7 @@ on:
- 'tests/studio/playwright_link_definition_probe.py'
- 'tests/studio/playwright_user_message_time.py'
- 'tests/studio/playwright_progressive_rows.py'
+ - 'tests/studio/playwright_long_chat_renders.py'
- 'tests/studio/playwright_model_picker_renders.py'
- 'tests/studio/playwright_recipe_drag_renders.py'
- 'tests/studio/playwright_gallery_strip_renders.py'
@@ -88,6 +89,7 @@ on:
- 'tests/studio/playwright_link_definition_probe.py'
- 'tests/studio/playwright_user_message_time.py'
- 'tests/studio/playwright_progressive_rows.py'
+ - 'tests/studio/playwright_long_chat_renders.py'
- 'tests/studio/playwright_model_picker_renders.py'
- 'tests/studio/playwright_recipe_drag_renders.py'
- 'tests/studio/playwright_gallery_strip_renders.py'
@@ -529,6 +531,11 @@ jobs:
timeout-minutes: 5
run: python3 tests/studio/playwright_progressive_rows.py
+ - name: Browser check for long-chat re-renders
+ working-directory: ${{ github.workspace }}
+ timeout-minutes: 5
+ run: python3 tests/studio/playwright_long_chat_renders.py
+
# Render counts, not timings: a search keystroke or a quant probe answer in the model
# picker's On Device tab must not re-render every row, and the rows must still navigate.
- name: Browser check for model picker re-renders
diff --git a/studio/frontend/smoke-thread-weight-main.tsx b/studio/frontend/smoke-thread-weight-main.tsx
index 14d91e81aef..25e2093f385 100644
--- a/studio/frontend/smoke-thread-weight-main.tsx
+++ b/studio/frontend/smoke-thread-weight-main.tsx
@@ -95,13 +95,26 @@ function userMarkdown(index: number): string {
*/
type SeedOptions = {
plainAssistants?: readonly number[] | "all";
- /** `rounds` x reasoning, tool call, answer per reply; truncation every `compactionEvery`th. */
- agent?: { rounds: number; compactionEvery?: number };
+ /**
+ * `rounds` x reasoning, tool call, answer per reply; truncation every `compactionEvery`th.
+ * `sources` adds six URL source parts after every web_search call.
+ */
+ agent?: { rounds: number; compactionEvery?: number; sources?: boolean };
};
const AGENT_TOOLS = ["web_search", "terminal", "python"] as const;
-function agentContent(index: number, rounds: number) {
+function urlSources(tag: string) {
+ return Array.from({ length: 6 }, (_, source) => ({
+ type: "source" as const,
+ sourceType: "url" as const,
+ id: `source_${tag}_${source}`,
+ url: `https://example${source}.org/${tag}`,
+ title: `Source ${source} for ${tag}`,
+ }));
+}
+
+function agentContent(index: number, rounds: number, sources = false) {
const parts: Exclude<ThreadMessageLike["content"], string>[number][] = [];
for (let round = 0; round < rounds; round++) {
const tag = `${index}_${round}`;
@@ -122,6 +135,7 @@ function agentContent(index: number, rounds: number) {
argsText: JSON.stringify(args),
result: `Result ${tag}. ${PROSE}\n${PROSE}\n${CLOSING}`,
});
+ if (sources && toolName === "web_search") parts.push(...urlSources(tag));
parts.push({
type: "text" as const,
text: `Answer ${tag}. ${PROSE}\n\n- first item ${tag}\n- second item ${tag}`,
@@ -155,7 +169,7 @@ function buildMessages(
const compacted = every > 0 && ordinal > 0 && ordinal % every === 0;
return {
role: "assistant" as const,
- content: agentContent(index, agent.rounds),
+ content: agentContent(index, agent.rounds, agent.sources),
...(compacted
? {
metadata: {
@@ -189,15 +203,32 @@ function buildMessages(
const STREAM_CHUNKS = Number(
new URLSearchParams(window.location.search).get("stream") ?? "0",
);
+// `&sources=1`: the streamed reply follows a web_search call with six URL sources.
+const STREAM_SOURCES =
+ new URLSearchParams(window.location.search).get("sources") === "1";
const NEVER_RUNS: ChatModelAdapter = STREAM_CHUNKS > 0
? {
async *run({ abortSignal }) {
+ const args = { query: "live reference" };
+ const prefix = STREAM_SOURCES
+ ? [
+ {
+ type: "tool-call" as const,
+ toolCallId: "call_live",
+ toolName: "web_search",
+ args,
+ argsText: JSON.stringify(args),
+ result: `Result live. ${PROSE}`,
+ },
+ ...urlSources("live"),
+ ]
+ : [];
let text = "";
for (let chunk = 0; chunk < STREAM_CHUNKS; chunk++) {
await new Promise((resolve) => setTimeout(resolve, 20));
if (abortSignal.aborted) return;
text += `word${chunk} `;
- yield { content: [{ type: "text" as const, text }] };
+ yield { content: [...prefix, { type: "text" as const, text }] };
}
},
}
diff --git a/studio/frontend/src/components/assistant-ui/sources.tsx b/studio/frontend/src/components/assistant-ui/sources.tsx
index 48ec2426c70..d2a0f127283 100644
--- a/studio/frontend/src/components/assistant-ui/sources.tsx
+++ b/studio/frontend/src/components/assistant-ui/sources.tsx
@@ -7,10 +7,12 @@ import {
useRef,
useEffect,
useCallback,
+ useMemo,
type ComponentProps,
type FC,
} from "react";
-import { useMessage } from "@assistant-ui/react";
+import { useAuiState } from "@assistant-ui/react";
+import { memoOnArray } from "@/components/assistant-ui/message-derived";
import { cn } from "@/lib/utils";
import { Badge, badgeVariants, type BadgeProps } from "./badge";
import {
@@ -176,42 +178,69 @@ const SourceBadge: FC<{ source: SourceData; allowRemoteIcons?: boolean }> = ({
);
};
+const NO_SOURCES: readonly SourceData[] = [];
+
+// The URL sources of a message as a string, so the selector result keeps its value while a
+// reply streams: every token rebuilds the content array, but not its source parts.
+const urlSourcesKey = memoOnArray((content: readonly unknown[]): string => {
+ const sources: SourceData[] = [];
+ for (const item of content) {
+ const part = item as {
+ type?: unknown;
+ sourceType?: unknown;
+ url?: unknown;
+ id?: unknown;
+ title?: string;
+ metadata?: { description?: string };
+ };
+ if (
+ part.type === "source" &&
+ "sourceType" in part &&
+ part.sourceType === "url" &&
+ "url" in part &&
+ part.url
+ ) {
+ const url = part.url as string;
+ sources.push({
+ id: typeof part.id === "string" ? part.id : url,
+ url,
+ title: part.title || "",
+ description: part.metadata?.description,
+ });
+ }
+ }
+ return sources.length === 0 ? "" : JSON.stringify(sources);
+});
+
const SourcesGroup: FC<{ sources?: SourceData[]; allowRemoteIcons?: boolean }> = ({
sources: suppliedSources,
allowRemoteIcons = true,
}) => {
- const message = useMessage();
+ const key = useAuiState(({ message }) =>
+ suppliedSources || !Array.isArray(message.content)
+ ? ""
+ : urlSourcesKey(message.content),
+ );
+ const messageSources = useMemo<readonly SourceData[]>(
+ () => (key === "" ? NO_SOURCES : (JSON.parse(key) as SourceData[])),
+ [key],
+ );
+ return (
+ <SourcesGroupView
+ sources={suppliedSources ?? messageSources}
+ allowRemoteIcons={allowRemoteIcons}
+ />
+ );
+};
+
+const SourcesGroupView = memo<{
+ sources: readonly SourceData[];
+ allowRemoteIcons: boolean;
+}>(({ sources, allowRemoteIcons }) => {
const containerRef = useRef<HTMLDivElement>(null);
const [visibleCount, setVisibleCount] = useState<number | null>(null);
const [expanded, setExpanded] = useState(false);
- const messageSources: SourceData[] = [];
- if (!suppliedSources && message.content) {
- for (const part of message.content) {
- if (
- part.type === "source" &&
- "sourceType" in part &&
- part.sourceType === "url" &&
- "url" in part &&
- part.url
- ) {
- const url = part.url as string;
- const partId =
- typeof (part as { id?: unknown }).id === "string"
- ? ((part as { id: string }).id)
- : url;
- messageSources.push({
- id: partId,
- url,
- title: (part as { title?: string }).title || "",
- description: (part as { metadata?: { description?: string } })
- .metadata?.description,
- });
- }
- }
- }
- const sources = suppliedSources ?? messageSources;
-
const measure = useCallback(() => {
const container = containerRef.current;
if (!container || sources.length === 0) return;
@@ -311,7 +340,9 @@ const SourcesGroup: FC<{ sources?: SourceData[]; allowRemoteIcons?: boolean }> =
</div>
</div>
);
-};
+});
+
+SourcesGroupView.displayName = "SourcesGroupView";
const SourcesNoop: FC<Record<string, unknown>> = () => null;
diff --git a/studio/frontend/src/features/chat/tool-output-scope.ts b/studio/frontend/src/features/chat/tool-output-scope.ts
index 89dadb3eb56..62802e32a21 100644
--- a/studio/frontend/src/features/chat/tool-output-scope.ts
+++ b/studio/frontend/src/features/chat/tool-output-scope.ts
@@ -36,12 +36,15 @@ export function useToolOutputFor(
toolCallId: string,
): string {
const unresolvedScope = useUnresolvedToolPaneScope();
- // Only a running thread may fall back; local ids repeat, so others would show its stdout.
- const isRunning = useAuiState(({ thread }) => thread.isRunning);
const own = map[toolOutputKey(paneScope, toolCallId)];
- if (own !== undefined) return own;
- if (!isRunning) return "";
- return map[toolOutputKey(unresolvedScope, toolCallId)] ?? "";
+ // Only a running thread may fall back (local ids repeat). Select the string, not isRunning,
+ // so a run starting or ending does not re-render every tool card in the history.
+ const fallback = useAuiState(({ thread }) =>
+ own === undefined && thread.isRunning
+ ? (map[toolOutputKey(unresolvedScope, toolCallId)] ?? "")
+ : "",
+ );
+ return own ?? fallback;
}
export function toolOutputKey(paneScope: string, toolCallId: string): string {
diff --git a/tests/studio/playwright_long_chat_renders.py b/tests/studio/playwright_long_chat_renders.py
new file mode 100644
index 00000000000..b3e01f342cf
--- /dev/null
+++ b/tests/studio/playwright_long_chat_renders.py
@@ -0,0 +1,152 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Render counts while a reply streams into a chat with tool cards and web sources.
+
+A streamed token must not re-render the reply's source badges, and a run starting or ending
+must not re-render the python / terminal cards of earlier replies. Counts come from a React
+DevTools hook installed before the page loads, using DevTools' rule for which fibers rendered.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import sys
+from pathlib import Path
+
+from playwright.sync_api import sync_playwright
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from _playwright_robust import ( # noqa: E402
+ chromium_launch_args,
+ start_vite,
+ stop_process,
+ wait_for_smoke_page,
+)
+
+PORT = int(os.environ.get("SMOKE_PORT", "5217"))
+_EXTERNAL = os.environ.get("SMOKE_BASE_URL", "").strip().rstrip("/")
+BASE = _EXTERNAL or f"http://127.0.0.1:{PORT}"
+# Under MIN_PROGRESSIVE_MESSAGES (40), so the whole history mounts in one commit.
+MESSAGES = 30
+CHUNKS = 60
+
+RENDER_COUNTER = r"""
+(() => {
+ window.__renders = null;
+ const nameOf = (fiber) => {
+ const type = fiber.type;
+ if (!type) return null;
+ if (fiber.tag === 0 || fiber.tag === 1 || fiber.tag === 15) return type.displayName || type.name || null;
+ if (fiber.tag === 11) return type.displayName || type.render?.displayName || type.render?.name || null;
+ if (fiber.tag === 14) return type.displayName || type.type?.displayName || type.type?.name || null;
+ return null;
+ };
+ const walk = (fiber, counts) => {
+ // Children that were not cloned this commit were not visited by React.
+ if (fiber.alternate && fiber.alternate.child === fiber.child) return;
+ for (let child = fiber.child; child; child = child.sibling) {
+ const name = nameOf(child);
+ // PerformedWork (1) is set only on fibers whose component function ran this commit.
+ if (name && child.alternate && (child.flags & 1)) counts[name] = (counts[name] || 0) + 1;
+ walk(child, counts);
+ }
+ };
+ window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
+ supportsFiber: true,
+ isDisabled: false,
+ renderers: new Map(),
+ inject(renderer) { const id = this.renderers.size + 1; this.renderers.set(id, renderer); return id; },
+ checkDCE() {},
+ onScheduleFiberRoot() {},
+ onCommitFiberUnmount() {},
+ onPostCommitFiberRoot() {},
+ onCommitFiberRoot(_id, root) {
+ if (window.__renders) walk(root.current, window.__renders);
+ },
+ };
+})();
+"""
+
+
+def info(message: str) -> None:
+ print(f"[long-chat-renders] {message}", flush = True)
+
+
+def run(page) -> dict[str, int]:
+ page.goto(f"{BASE}/smoke-thread-weight.html?stream={CHUNKS}&sources=1")
+ page.wait_for_function("() => window.__threadWeight !== undefined", timeout = 120_000)
+ page.evaluate(
+ "(n) => window.__threadWeight.seed(n, { agent: { rounds: 1, sources: true } })",
+ MESSAGES,
+ )
+ page.wait_for_function(
+ "(n) => window.__threadWeight.messageCount() >= n", arg = MESSAGES, timeout = 120_000
+ )
+ seeded = page.evaluate(
+ '() => document.querySelectorAll(\'[data-role="assistant"] a[href^="https://example"]\').length'
+ )
+ assert seeded > 0, "the seeded history shows no source badges"
+ page.wait_for_timeout(500)
+ page.evaluate("() => { window.__renders = {}; }")
+ page.evaluate("() => window.__threadWeight.send('go')")
+ page.wait_for_function("() => window.__threadWeight.isRunning()", timeout = 60_000)
+ page.wait_for_function("() => !window.__threadWeight.isRunning()", timeout = 120_000, polling = 100)
+ page.wait_for_timeout(300)
+ counts = page.evaluate(
+ "() => { const c = window.__renders; window.__renders = null; return c; }"
+ )
+ live = page.evaluate(
+ "() => window.__threadWeight.lastAssistantMessage()?.querySelectorAll('a[href$=\"/live\"]').length ?? 0"
+ )
+ text = page.evaluate("() => window.__threadWeight.lastAssistantMessage()?.innerText ?? ''")
+ assert live > 0, "the streamed reply shows no source badges, so nothing was measured"
+ assert f"word{CHUNKS - 1}" in text, "the stream did not finish"
+ return counts
+
+
+def main() -> int:
+ proc = None
+ try:
+ if not _EXTERNAL:
+ proc = start_vite(PORT)
+ wait_for_smoke_page(
+ f"{BASE}/smoke-thread-weight.html",
+ "smoke-thread-weight-main.tsx",
+ proc = proc,
+ info = info,
+ )
+ with sync_playwright() as playwright:
+ browser = playwright.chromium.launch(headless = True, args = chromium_launch_args())
+ try:
+ context = browser.new_context(viewport = {"width": 1280, "height": 900})
+ context.add_init_script(RENDER_COUNTER)
+ page = context.new_page()
+ errors: list[str] = []
+ page.on("pageerror", lambda error: errors.append(str(error)))
+ counts = run(page)
+ finally:
+ browser.close()
+ badges = counts.get("SourceBadge", 0)
+ cards = counts.get("PythonToolUIImpl", 0) + counts.get("TerminalToolUIImpl", 0)
+ info(json.dumps({"SourceBadge": badges, "python/terminal cards": cards, "chunks": CHUNKS}))
+ assert not errors, errors
+ # The live reply's six badges may render a couple of times (mount, status change), not per token.
+ assert badges <= 2 * 6, (
+ f"source badges re-rendered {badges} times over a {CHUNKS}-chunk stream: "
+ f"a streamed token is re-rendering them"
+ )
+ assert cards <= 2, (
+ f"python / terminal cards re-rendered {cards} times: the run start or end is "
+ f"re-rendering every card in the history"
+ )
+ info("ok")
+ return 0
+ finally:
+ if proc is not None:
+ stop_process(proc)
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())