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: stop offscreen reasoning transcripts listening to thread scroll (#13046)

Daniel Han73e1b5b382ae
diff --git a/studio/frontend/src/components/assistant-ui/reasoning-transcript.tsx b/studio/frontend/src/components/assistant-ui/reasoning-transcript.tsx
index 813bfa61de1..e8be0d8e773 100644
--- a/studio/frontend/src/components/assistant-ui/reasoning-transcript.tsx
+++ b/studio/frontend/src/components/assistant-ui/reasoning-transcript.tsx
@@ -17,6 +17,7 @@ import {
useRef,
useState,
} from "react";
+import { flushSync } from "react-dom";
import {
CodeBlockActions,
MarkdownTextSource,
@@ -287,6 +288,41 @@ const Fragment = memo(function Fragment({
);
});
+// IntersectionObserver reports after paint; this catches jumps past its margin before paint.
+const jumpWatchers = new WeakMap<
+ HTMLElement,
+ { checks: Set<() => void>; stop: () => void }
+>();
+
+function onViewportJump(scroll: HTMLElement, check: () => void): () => void {
+ let watcher = jumpWatchers.get(scroll);
+ if (!watcher) {
+ const checks = new Set<() => void>();
+ let last = scroll.scrollTop;
+ const listener = () => {
+ const top = scroll.scrollTop;
+ const jumped = Math.abs(top - last) >= scroll.clientHeight / 2;
+ last = top;
+ if (jumped) for (const run of [...checks]) run();
+ };
+ scroll.addEventListener("scroll", listener, { passive: true });
+ watcher = {
+ checks,
+ stop: () => {
+ scroll.removeEventListener("scroll", listener);
+ jumpWatchers.delete(scroll);
+ },
+ };
+ jumpWatchers.set(scroll, watcher);
+ }
+ const { checks, stop } = watcher;
+ checks.add(check);
+ return () => {
+ checks.delete(check);
+ if (!checks.size) stop();
+ };
+}
+
export function ReasoningTranscript({
initialAnchor,
documents,
@@ -295,6 +331,11 @@ export function ReasoningTranscript({
streaming,
}: Props) {
const root = useRef<HTMLDivElement>(null);
+ // Over a screen away, a transcript stops listening to the shared viewport's scroll (#12025).
+ const nearby = useRef(true);
+ const followOffset = useRef<((on: boolean, park?: number) => void) | null>(
+ null,
+ );
const [index] = useState(() => new ReasoningTranscriptIndex());
const fragments = useMemo(() => index.update(documents), [documents, index]);
const [anchor, setAnchor] = useState(
@@ -326,10 +367,31 @@ export function ReasoningTranscript({
scrollMargin: geometry.top,
initialOffset: () => viewport()?.scrollTop ?? 0,
observeElementOffset: (instance, callback) => {
- // The viewport predates this transcript. Subscribe with its current offset;
- // waiting for the next scroll event leaves TanStack's initial zero cached.
- callback(instance.scrollElement?.scrollTop ?? 0, false);
- return observeElementOffset(instance, callback);
+ let stop: (() => void) | undefined;
+ let generation = 0;
+ const follow = (on: boolean, park?: number) => {
+ stop?.();
+ stop = undefined;
+ // TanStack's scroll-end debounce outlives its unsubscribe: drop a stopped one's call.
+ const current = ++generation;
+ if (!on) {
+ // A frozen offset can land mid-transcript once content above moves, mounting
+ // rows a live offset never would. Park it where the live one keeps the range.
+ if (park !== undefined) callback(park, false);
+ return;
+ }
+ // Seed the live offset, else TanStack keeps its initial zero or the one from before it left.
+ callback(instance.scrollElement?.scrollTop ?? 0, false);
+ stop = observeElementOffset(instance, (offset, isScrolling) => {
+ if (current === generation) callback(offset, isScrolling);
+ });
+ };
+ followOffset.current = follow;
+ follow(nearby.current);
+ return () => {
+ if (followOffset.current === follow) followOffset.current = null;
+ stop?.();
+ };
},
overscan: 2,
measureElement: (element) => {
@@ -391,12 +453,21 @@ export function ReasoningTranscript({
adjustAbove(offset + adjustments - element.scrollTop);
},
});
- virtualizer.shouldAdjustScrollPositionOnItemSizeChange = (item) =>
+ virtualizer.shouldAdjustScrollPositionOnItemSizeChange = (item, delta) => {
// Ref measurements run before the virtualizer observes the shared viewport.
// Its offset is still zero then: applying that correction would send a reader
// back to the beginning when a live trace first crosses the threshold.
- virtualizer.scrollElement !== null &&
- item.end < (viewport()?.scrollTop ?? 0);
+ if (
+ virtualizer.scrollElement === null ||
+ item.end >= (viewport()?.scrollTop ?? 0)
+ )
+ return false;
+ if (nearby.current) return true;
+ // A far transcript's offset froze when it stopped listening; correcting from it
+ // would throw the reader back to where it left. Shift by the size change alone.
+ adjustAbove(delta);
+ return false;
+ };
useLayoutEffect(() => {
const element = root.current;
@@ -434,15 +505,16 @@ export function ReasoningTranscript({
virtualizer.measure();
} else {
readingAnchor = undefined;
- for (const row of element.querySelectorAll<HTMLElement>(
- "[data-index]",
- )) {
- const captured = captureReasoningAnchor(row, scroll);
- if (captured) {
- readingAnchor = { ...captured, index: Number(row.dataset.index) };
- break;
+ if (nearby.current)
+ for (const row of element.querySelectorAll<HTMLElement>(
+ "[data-index]",
+ )) {
+ const captured = captureReasoningAnchor(row, scroll);
+ if (captured) {
+ readingAnchor = { ...captured, index: Number(row.dataset.index) };
+ break;
+ }
}
- }
}
width = rect.width;
};
@@ -453,10 +525,65 @@ export function ReasoningTranscript({
observer.observe(element);
observer.observe(scroll);
if (scroll.firstElementChild) observer.observe(scroll.firstElementChild);
- scroll.addEventListener("scroll", schedule, { passive: true });
+ if (nearby.current)
+ scroll.addEventListener("scroll", schedule, { passive: true });
+ let stopJumpWatch: (() => void) | undefined;
+ const setNearby = (near: boolean, beforePaint = false) => {
+ if (near === nearby.current) return;
+ nearby.current = near;
+ if (near) {
+ stopJumpWatch?.();
+ stopJumpWatch = undefined;
+ scroll.addEventListener("scroll", schedule, { passive: true });
+ if (beforePaint)
+ // Its top went stale while away: re-measure and render the live range before this paint.
+ flushSync(() => {
+ measure();
+ followOffset.current?.(true);
+ });
+ else {
+ followOffset.current?.(true);
+ schedule();
+ }
+ } else {
+ const box = element.getBoundingClientRect();
+ // Far means wholly above or below the reader; a closed folded round has no box.
+ followOffset.current?.(
+ false,
+ box.height > 0
+ ? box.bottom <= scroll.getBoundingClientRect().top
+ ? 1e9
+ : -1e9
+ : undefined,
+ );
+ scroll.removeEventListener("scroll", schedule);
+ readingAnchor = undefined;
+ stopJumpWatch = onViewportJump(scroll, () => {
+ const box = element.getBoundingClientRect();
+ const view = scroll.getBoundingClientRect();
+ if (
+ box.height > 0 &&
+ box.bottom > view.top - view.height &&
+ box.top < view.bottom + view.height
+ )
+ setNearby(true, true);
+ });
+ }
+ };
+ const proximity =
+ typeof IntersectionObserver === "undefined"
+ ? undefined
+ : new IntersectionObserver(
+ (entries) =>
+ setNearby(entries.at(-1)?.isIntersecting ?? nearby.current),
+ { root: scroll, rootMargin: "100% 0px" },
+ );
+ proximity?.observe(element);
measure();
return () => {
observer.disconnect();
+ proximity?.disconnect();
+ stopJumpWatch?.();
scroll.removeEventListener("scroll", schedule);
cancelAnimationFrame(frame);
};
diff --git a/studio/frontend/tests/reasoning-transcript-offscreen-scroll.test.ts b/studio/frontend/tests/reasoning-transcript-offscreen-scroll.test.ts
new file mode 100644
index 00000000000..ab56baba53b
--- /dev/null
+++ b/studio/frontend/tests/reasoning-transcript-offscreen-scroll.test.ts
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Offscreen transcripts must not listen to the shared viewport's scroll (#12025); pinned at source.
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { readSrc } from "./helpers/kit.ts";
+
+const source = readSrc("components/assistant-ui/reasoning-transcript.tsx");
+const transcript = source.slice(
+ source.indexOf("export function ReasoningTranscript("),
+);
+
+function between(text: string, start: string, end: string): string {
+ const from = text.indexOf(start);
+ assert.notEqual(from, -1, `${start} is gone; this test needs rewriting`);
+ const to = text.indexOf(end, from);
+ assert.notEqual(to, -1, `${end} is gone; this test needs rewriting`);
+ return text.slice(from, to);
+}
+
+test("a transcript watches its distance from the thread viewport", () => {
+ assert.match(
+ transcript,
+ /new IntersectionObserver\([\s\S]*?setNearby\([\s\S]*?\{ root: scroll, rootMargin: "100% 0px" \}/,
+ "no proximity observer rooted at the thread viewport",
+ );
+ assert.match(transcript, /proximity\?\.disconnect\(\)/);
+ assert.match(
+ transcript,
+ /stopJumpWatch\?\.\(\);\s*scroll\.removeEventListener/,
+ );
+});
+
+test("a far transcript drops both viewport scroll listeners and resumes when near", () => {
+ const toggle = between(transcript, "const setNearby = ", "const proximity =");
+ const [near, far] = toggle.split("} else {");
+ assert.match(
+ far,
+ /followOffset\.current\?\.\(\s*false,/,
+ "offset observer kept offscreen",
+ );
+ assert.match(
+ far,
+ /box\.bottom <= scroll\.getBoundingClientRect\(\)\.top\s*\?\s*1e9\s*:\s*-1e9/,
+ );
+ assert.match(
+ far,
+ /scroll\.removeEventListener\("scroll", schedule\)/,
+ "measure listener kept offscreen",
+ );
+ assert.match(
+ near,
+ /scroll\.addEventListener\("scroll", schedule/,
+ "measure listener not restored",
+ );
+ assert.match(
+ near,
+ /if \(beforePaint\)[\s\S]*?flushSync\(\(\) => \{\s*measure\(\);\s*followOffset\.current\?\.\(true\);\s*\}\)/,
+ );
+
+ const observe = between(
+ transcript,
+ "observeElementOffset: (instance, callback) => {",
+ "overscan:",
+ );
+ assert.match(
+ observe,
+ /if \(!on\) \{[\s\S]*?if \(park !== undefined\) callback\(park, false\);\s*return;\s*\}/,
+ "offset observer cannot be stopped or parked",
+ );
+ assert.match(
+ observe,
+ /callback\(instance\.scrollElement\?\.scrollTop \?\? 0, false\);\s*stop = observeElementOffset\(instance,/,
+ "resubscribing does not seed the current offset first",
+ );
+ assert.match(
+ observe,
+ /if \(current === generation\) callback\(offset, isScrolling\)/,
+ );
+});
+
+test("a jump past the observer margin wakes far transcripts through one listener per viewport", () => {
+ const watch = between(
+ source,
+ "function onViewportJump(",
+ "export function ReasoningTranscript(",
+ );
+ assert.match(
+ watch,
+ /jumpWatchers\.get\(scroll\)/,
+ "a listener per transcript instead of per viewport",
+ );
+ assert.match(watch, /Math\.abs\(top - last\) >= scroll\.clientHeight \/ 2/);
+ assert.match(
+ between(transcript, "stopJumpWatch = onViewportJump(", "const proximity ="),
+ /setNearby\(true, true\)/,
+ );
+});
+
+test("a far transcript corrects a resize above the reader by its size change, not its frozen offset", () => {
+ const adjust = between(
+ transcript,
+ "virtualizer.shouldAdjustScrollPositionOnItemSizeChange = (item, delta) => {",
+ "useLayoutEffect(",
+ );
+ assert.match(
+ adjust,
+ /if \(nearby\.current\) return true;\n(?:[ \t]*\/\/[^\n]*\n)*[ \t]*adjustAbove\(delta\);\s*return false;/,
+ "a far transcript hands TanStack a correction from its frozen offset",
+ );
+});
+
+test("a far transcript skips the reading-anchor scan", () => {
+ assert.match(
+ transcript,
+ /if \(nearby\.current\)\s*for \(const row of element\.querySelectorAll<HTMLElement>\(\s*"\[data-index\]",?\s*\)\)/,
+ );
+});