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: restore WebKitGTK offscreen code fences in batches and on any pointer release (#16919)
Daniel Han07a9c871e186
diff --git a/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx b/studio/frontend/src/components/assistant-ui/code-fence-defer.tsxindex e8937a7af1f..6283f4f4c24 100644--- a/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx+++ b/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx@@ -1146,6 +1146,7 @@ function useFenceHighlightLimit(source: string,code: RefObject<HTMLElement | null>,frame: RefObject<HTMLElement | null>,+ parked = false,): { progress: HighlightProgress | null; staged: boolean } {const eligible = !(typeof window === "undefined" || windowing || isIncomplete|| fenceMode() !== "defer"@@ -1161,30 +1162,37 @@ function useFenceHighlightLimit(const [previous, setPrevious] = useState<{tokens: typeof tokens;progress: HighlightProgress | null;- }>(() => ({ tokens, progress: immediate ? null : tokens === null+ parked: boolean;+ }>(() => ({ tokens, parked, progress: immediate ? null : tokens === null? EMPTY_HIGHLIGHT_PROGRESS : initialHighlightProgress(tokens) }));let current = previous;if (staged && previous.tokens !== tokens) {- current = { tokens, progress: immediate || tokens === null+ current = { tokens, parked, progress: immediate || tokens === null? null : retainHighlightProgress(previous.tokens, tokens, previous.progress) };} else if (immediate && previous.progress !== null) {- current = { tokens, progress: null };+ current = { tokens, parked, progress: null };+ }+ // Parked as plain text, the highlighted lines are unmounted; restart the batches so returning+ // to the fence does not remount every highlighted span in one commit.+ if (parked !== current.parked) {+ current = { tokens, parked, progress: parked && !immediate && tokens !== null+ ? initialHighlightProgress(tokens) : current.progress };}if (current !== previous) setPrevious(current);const progress = immediate ? null : current.progress;useLayoutEffect(() => {- if (progress === null || tokens === null) return;+ if (progress === null || tokens === null || parked) return;watchHighlighting();const cancel = highlightQueue.add(() => {const visible = progress.located ? 0 : visibleHighlightLine(code.current, frame.current);- setPrevious({ tokens, progress: advanceHighlightProgress(tokens, progress, visible) });+ setPrevious({ tokens, parked, progress: advanceHighlightProgress(tokens, progress, visible) });return true;- }, () => setPrevious({ tokens, progress: null }));+ }, () => setPrevious({ tokens, parked, progress: null }));return () => {cancel();unwatchHighlighting();};- }, [tokens, progress, code, frame]);+ }, [tokens, progress, parked, code, frame]);return { progress, staged };}@@ -1217,8 +1225,9 @@ const onPointerDown = (event: PointerEvent): void => {pointerHeld = event.target instanceof Node ? event.target : null;};+// Not gated on `pointerHeld`: touch never sets it, and a hold that began before the listeners+// were attached never did either, yet the offscreen-text skip marks `heldLastFrame` for both.const releasePointer = (): void => {- if (pointerHeld === null) return;pointerHeld = null;if (heldLastFrame) scheduleRemeasure();};@@ -1651,7 +1660,6 @@ export const FenceBody = memo(function FenceBody({const surface = useRef<HTMLDivElement | null>(null);const wrapper = useRef<HTMLDivElement | null>(null);const frame = useRef<HTMLDivElement | null>(null);- const highlight = useFenceHighlightLimit(tokens, isIncomplete, windowing, language, source, code, frame);const { window: lineWindow, pins, measured } = useLineWindow(code,surface,@@ -1665,6 +1673,9 @@ export const FenceBody = memo(function FenceBody({frame,WINDOW_SETTLED_FENCES && !!tokens?.length && !isIncomplete,);+ const highlight = useFenceHighlightLimit(+ tokens, isIncomplete, windowing, language, source, code, frame, offscreenPlain,+ );const languageClass = language === null ? null : `language-${language}`;const rootStyle = useMemo(() => {diff --git a/studio/frontend/tests/code-fence-offscreen-restore.test.ts b/studio/frontend/tests/code-fence-offscreen-restore.test.tsnew file mode 100644index 00000000000..72d1a031f5b--- /dev/null+++ b/studio/frontend/tests/code-fence-offscreen-restore.test.ts@@ -0,0 +1,276 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// WebKitGTK parks settled offscreen fences as plain text. These run the real hook and listener+// code from code-fence-defer.tsx against small fakes, so they check behaviour, not spelling.++import assert from "node:assert/strict";+import test from "node:test";+import { runInNewContext } from "node:vm";+import ts from "@typescript/typescript6";++import {+ advanceHighlightProgress,+ EMPTY_HIGHLIGHT_PROGRESS,+ HIGHLIGHT_BATCH_TOKENS,+ type HighlightProgress,+ initialHighlightProgress,+ needsHighlightBatches,+ retainHighlightProgress,+} from "../src/components/assistant-ui/fence-highlight-progress.ts";+import { WINDOW_CAP_LINES } from "../src/components/assistant-ui/code-fence-window.ts";++import { readSrc } from "./helpers/kit.ts";++const DEFER = readSrc("components/assistant-ui/code-fence-defer.tsx");+const PARSED = ts.createSourceFile("code-fence-defer.tsx", DEFER, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);++function declarations(names: readonly string[]): string {+ const wanted = new Set(names);+ const found: string[] = [];+ for (const statement of PARSED.statements) {+ const named = ts.isFunctionDeclaration(statement)+ ? [statement.name?.text]+ : ts.isVariableStatement(statement)+ ? statement.declarationList.declarations.map((node) => ts.isIdentifier(node.name) ? node.name.text : undefined)+ : [];+ if (!named.some((name) => name !== undefined && wanted.has(name))) continue;+ for (const name of named) if (name !== undefined) wanted.delete(name);+ found.push(statement.getText(PARSED).replace(/^export\s+/, ""));+ }+ assert.deepEqual([...wanted], [], "every extracted declaration exists in code-fence-defer.tsx");+ return ts.transpileModule(found.join("\n"), {+ compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React },+ }).outputText;+}++// Just enough of React's hook semantics: render-phase setState re-renders at once, layout effects+// run after a settled render when their deps change.+function hookRunner<P, R>(hook: (props: P) => R) {+ const slots: unknown[] = [];+ const effects: { deps: unknown[]; cleanup?: (() => void) | void }[] = [];+ let pending: { index: number; run: () => (() => void) | void; deps: unknown[] }[] = [];+ let slot = 0;+ let effect = 0;+ let dirty = false;+ let props: P;+ let result: R;+ const same = (a: unknown[] | undefined, b: unknown[]) =>+ a !== undefined && a.length === b.length && a.every((value, i) => Object.is(value, b[i]));+ const react = {+ useState<T>(init: T | (() => T)): [T, (next: T) => void] {+ const index = slot++;+ if (!(index in slots)) slots[index] = typeof init === "function" ? (init as () => T)() : init;+ return [slots[index] as T, (next: T) => {+ if (Object.is(slots[index], next)) return;+ slots[index] = next;+ dirty = true;+ }];+ },+ useMemo<T>(make: () => T, deps: unknown[]): T {+ const index = slot++;+ const known = slots[index] as { deps: unknown[]; value: T } | undefined;+ if (known !== undefined && same(known.deps, deps)) return known.value;+ const value = make();+ slots[index] = { deps, value };+ return value;+ },+ useLayoutEffect(run: () => (() => void) | void, deps: unknown[]) {+ const index = effect++;+ if (!same(effects[index]?.deps, deps)) pending.push({ index, run, deps });+ },+ };+ const render = (next: P): R => {+ props = next;+ for (let rounds = 0; ; rounds += 1) {+ assert.ok(rounds < 100, "renders settle");+ do {+ dirty = false;+ slot = 0;+ effect = 0;+ pending = [];+ result = hook(props);+ } while (dirty);+ const ready = pending;+ pending = [];+ for (const { index, run, deps } of ready) {+ effects[index]?.cleanup?.();+ effects[index] = { deps, cleanup: run() };+ }+ if (!dirty) return result;+ }+ };+ return { react, render, rerender: () => render(props) };+}++test("a fully highlighted fence parked as plain text comes back in quiet-work batches", () => {+ type Tokens = { content: string }[][];+ type Hook = (+ tokens: Tokens,+ isIncomplete: boolean,+ windowing: boolean,+ language: string,+ source: string,+ code: { current: null },+ frame: { current: null },+ parked: boolean,+ ) => { progress: HighlightProgress | null; staged: boolean };+ const queue: { run: () => boolean; flush: () => void; cancelled: boolean }[] = [];+ const context: Record<string, unknown> = {+ exports: {} as { hook?: Hook },+ window: {},+ printing: false,+ findBarOpen: () => false,+ fenceMode: () => "defer",+ justStreamed: () => false,+ FENCE_HEAD_CHARS: 512,+ trimmedLength: (source: string) => source.trim().length,+ WINDOW_CAP_LINES,+ needsHighlightBatches,+ initialHighlightProgress,+ advanceHighlightProgress,+ retainHighlightProgress,+ EMPTY_HIGHLIGHT_PROGRESS,+ watchHighlighting: () => {},+ unwatchHighlighting: () => {},+ visibleHighlightLine: () => 0,+ highlightQueue: {+ add(run: () => boolean, flush: () => void) {+ const entry = { run, flush, cancelled: false };+ queue.push(entry);+ return () => {+ entry.cancelled = true;+ };+ },+ },+ };+ const runner = hookRunner((props: Parameters<Hook>) => (context.exports as { hook: Hook }).hook(...props));+ Object.assign(context, runner.react);+ runInNewContext(`${declarations(["useFenceHighlightLimit"])}\nexports.hook = useFenceHighlightLimit;`, context);++ // Dense and under the line cap: the shape WebKitGTK leaves on the batched path.+ const lines = 120;+ assert.ok(lines < WINDOW_CAP_LINES);+ const tokens: Tokens = Array.from({ length: lines }, (_, line) =>+ Array.from({ length: 64 }, (_, at) => ({ content: `t${line}_${at} ` })));+ assert.ok(needsHighlightBatches(tokens, WINDOW_CAP_LINES));+ const refs = { current: null };+ const props = (parked: boolean): Parameters<Hook> =>+ [tokens, false, false, "text", "source", refs, refs, parked];+ const live = () => queue.filter((entry) => !entry.cancelled);+ const highlighted = (progress: HighlightProgress | null) =>+ progress === null ? Number.POSITIVE_INFINITY+ : progress.counts.flat().reduce((sum, count) => sum + count, 0);++ let state = runner.render(props(false));+ assert.ok(state.staged);+ for (let steps = 0; state.progress !== null; steps += 1) {+ assert.ok(steps < 100, "highlighting completes");+ const [next] = live();+ assert.ok(next, "pending batches are queued");+ next.cancelled = true;+ next.run();+ state = runner.rerender();+ }+ assert.equal(state.progress, null, "fully highlighted before scrolling away");++ state = runner.render(props(true));+ assert.equal(live().length, 0, "no highlight work runs while the fence is plain text");++ state = runner.render(props(false));+ assert.ok(+ highlighted(state.progress) <= HIGHLIGHT_BATCH_TOKENS,+ `returning remounts at most one batch, got ${highlighted(state.progress)} of ${lines * 64} tokens`,+ );+ assert.equal(live().length, 1, "the rest is queued as quiet work again");++ // Idempotent: another render while still on screen does not restart the batches.+ const before = state.progress;+ state = runner.rerender();+ assert.equal(state.progress, before);+});++type Listener = (event: Record<string, unknown>) => void;+class Target {+ listeners = new Map<string, Set<Listener>>();+ addEventListener(type: string, listener: Listener) {+ if (!this.listeners.has(type)) this.listeners.set(type, new Set());+ this.listeners.get(type)!.add(listener);+ }+ removeEventListener(type: string, listener: Listener) {+ this.listeners.get(type)?.delete(listener);+ }+ fire(type: string, event: Record<string, unknown> = {}) {+ for (const listener of [...(this.listeners.get(type) ?? [])]) listener(event);+ }+}++for (const [pointerType, pressedEarlier] of [["touch", false], ["mouse", false], ["mouse", true]] as const) {+ const when = pressedEarlier ? " pressed before the listeners attached" : "";+ test(`a plain fence skipped under a held ${pointerType} pointer${when} is restored when it lifts`, () => {+ const window = new Target();+ const document = Object.assign(new Target(), {+ documentElement: { hasAttribute: () => false },+ getSelection: () => null,+ });+ const frames: (() => void)[] = [];+ type Observed = (entries: { isIntersecting: boolean }[]) => void;+ // Assigned inside the fake observer, so annotate rather than let TS narrow the initial null.+ let observed = null as Observed | null;+ let held = false;+ const plain: boolean[] = [];+ const node = { isConnected: true, contains: () => false, closest: () => null };+ const context: Record<string, unknown> = {+ exports: {},+ window,+ document,+ requestAnimationFrame: (run: () => void) => frames.push(run),+ cancelAnimationFrame: () => {},+ forgetScrollable: () => {},+ inputPointerHeld: () => held,+ Node: Object,+ MutationObserver: class {+ observe() {}+ disconnect() {}+ },+ IntersectionObserver: class {+ constructor(callback: (entries: { isIntersecting: boolean }[]) => void) {+ observed = callback;+ }+ observe() {}+ disconnect() {}+ },+ useState: (init: boolean) => [init, (value: boolean) => plain.push(value)],+ useLayoutEffect: (run: () => void) => run(),+ };+ runInNewContext(`${declarations([+ "windowedFences", "windowFrame", "windowWatched", "findBarObserver", "frameRects",+ "frameFlying", "frameSettle", "pointerHeld", "heldLastFrame", "frameSelection", "printing",+ "liveSelection", "remeasureWindows", "FIND_BAR_FLAG", "findBarOpen", "scheduleRemeasure",+ "onPointerDown", "releasePointer", "onPointerMove", "onSelectionChange", "watchWindows",+ "unwatchWindows", "useOffscreenFenceText",+ ])}\nexports.hook = useOffscreenFenceText;`, context);+ const hook = (context.exports as { hook: (frame: { current: unknown }, enabled: boolean) => boolean }).hook;+ hook({ current: node }, true);+ assert.ok(observed);+ const intersect = observed as Observed;++ intersect([{ isIntersecting: false }]);+ assert.deepEqual(plain, [true], "parked as plain text while far off screen");++ // The finger (or button) goes down, the fence scrolls into view, and the pointer pauses.+ held = true;+ if (!pressedEarlier) document.fire("pointerdown", { pointerType, button: 0, target: {} });+ intersect([{ isIntersecting: true }]);+ assert.deepEqual(plain, [true], "a held pointer defers the restore");++ // Lift without any further scroll, resize or selection change.+ held = false;+ window.fire("pointerup", { pointerType, button: 0 });+ for (let steps = 0; frames.length > 0; steps += 1) {+ assert.ok(steps < 10);+ frames.shift()!();+ }+ assert.deepEqual(plain, [true, false], "the release reaches the plain fence and restores its highlighting");+ });+}diff --git a/studio/frontend/tests/desktop-webkit-paths.test.ts b/studio/frontend/tests/desktop-webkit-paths.test.tsindex 1f0f3c32a2d..a118bbc0b38 100644--- a/studio/frontend/tests/desktop-webkit-paths.test.ts+++ b/studio/frontend/tests/desktop-webkit-paths.test.ts@@ -97,7 +97,7 @@ test("an offscreen fence update skipped under a held pointer or selection is ret/if \(selected \|\| inputPointerHeld\(\)\) \{[^}]*heldLastFrame = true;\s*return;\s*\}/,"releasePointer and selectionchange remeasure only after a held frame, so a skip must mark one or a fence that entered view mid-drag stays plain",);- assert.match(defer, /const releasePointer = \(\): void => \{\s*if \(pointerHeld === null\) return;\s*pointerHeld = null;\s*if \(heldLastFrame\) scheduleRemeasure\(\);/);+ assert.match(defer, /const releasePointer = \(\): void => \{\s*pointerHeld = null;\s*if \(heldLastFrame\) scheduleRemeasure\(\);/, "touch never sets pointerHeld, so the release must not be gated on it (behaviour: code-fence-offscreen-restore.test.ts)");});test("settled WebKitGTK fences only line-window themselves while code wrapping is off", () => {