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: colour chat code blocks from the worker while scrolling and streaming (#17160)

Daniel Hanc71abe215145
diff --git a/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx b/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx
index d30444fe0a1..d76ab3193f4 100644
--- a/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx
+++ b/studio/frontend/src/components/assistant-ui/code-fence-defer.tsx
@@ -51,6 +51,8 @@ import {
needsHighlightBatches,
retainHighlightProgress,
} from "./fence-highlight-progress";
+import { unslothDarkTheme, unslothLightTheme } from "./code-themes";
+import { installTokenClasses, tokenClassesFor } from "./fence-token-classes";
import {
EMPTY_LINE_WINDOW,
HYSTERESIS_VIEWPORTS,
@@ -231,7 +233,7 @@ type FenceGate = {
outer: HTMLElement | null;
language: string | null;
chars: number;
- warm: (tokens: boolean) => void;
+ warm: (tokens: boolean, inView?: boolean) => void;
speculate: (settle: (seeded: boolean) => void) => (() => void) | null;
latch: () => void;
/** A no-op state write that gives React sync work to do (see latchNow). */
@@ -257,19 +259,27 @@ const gateOpen = (gate: FenceGate): boolean =>
inBand(gate.node, gate.near)
&& (gate.near === gate.outer || inBand(gate.near as HTMLElement, gate.outer));
+const gateInView = (gate: FenceGate): boolean =>
+ inBand(gate.node, gate.near, 0)
+ && (gate.near === gate.outer || inBand(gate.near as HTMLElement, gate.outer, 0));
+
+const LATCH_INLINE_MS = 8;
+
/*
* Upgrade inside this task so the browser never paints the plain shell. All three steps are needed:
- * warm(true) so render is a cache hit, flushSync(latch), and flushSync(poke) nested in an outer
- * flushSync so React runs the pending passive effect at discrete priority.
+ * warm(true) so render is a cache hit (tokens the worker seeded, or for a fence already on screen
+ * tokenized here), flushSync(latch), and flushSync(poke) nested in an outer flushSync so React runs
+ * the pending passive effect at discrete priority.
*/
const latchNow = (arrived: readonly FenceGate[]): void => {
if (arrived.length === 0) return;
+ const started = performance.now();
for (const gate of arrived) {
pendingReaches.get(gate)?.();
pendingReaches.delete(gate);
unreached.delete(gate);
fenceSpeculator.remove(gate);
- gate.warm(true);
+ gate.warm(true, performance.now() - started < LATCH_INLINE_MS && gateInView(gate));
}
flushSync(() => {
flushSync(() => {
@@ -441,9 +451,10 @@ const rankGates = (gates: readonly FenceGate[]): number[] => {
};
const SPECULATION_IN_FLIGHT = 3;
+const SPECULATION_VIEWPORTS = 3;
const fenceSpeculator = createFenceSpeculator<FenceGate>({
- idle: (callback, timeout) => scheduleQuietIdleTask(callback, timeout),
+ idle: (callback, timeout) => scheduleIdleTask(callback, timeout),
wait: (callback, ms) => {
const timer = setTimeout(callback, ms);
return () => clearTimeout(timer);
@@ -451,13 +462,15 @@ const fenceSpeculator = createFenceSpeculator<FenceGate>({
enabled: () =>
fenceMode() !== "off" && highlightWorkerState() !== "unavailable",
busy: () => streamActive() || highlightWorkerState() === "stalled"
- || inputPointerHeld() || inputQuietIn() > 0,
- eligible: (gate) => grammarsWarmed.has(grammarOf(gate)),
+ || inputPointerHeld() || nonScrollInputQuietIn() > 0,
+ eligible: (gate) =>
+ highlightWorkerState() === "ready" || grammarsWarmed.has(grammarOf(gate)),
rank: rankGates,
maxChars: MAX_HIGHLIGHT_CHARS,
budgetChars: MAX_CACHED_CHARACTERS / 2,
budgetFences: MAX_FENCES / 2,
maxInFlight: SPECULATION_IN_FLIGHT,
+ maxDistance: () => SPECULATION_VIEWPORTS * window.innerHeight,
});
// One capturing listener flags a code body scrolled off its first line, for the fade under its header.
@@ -544,13 +557,12 @@ const outermostScrollerOf = (node: HTMLElement): HTMLElement | null => {
return found;
};
-/** Is `node` inside `scroller`'s box grown by one of its own heights, the observer's margin? */
-const inBand = (node: HTMLElement, scroller: HTMLElement | null): boolean => {
+const inBand = (node: HTMLElement, scroller: HTMLElement | null, margin = 1): boolean => {
const bounds = scroller?.getBoundingClientRect();
const top = bounds ? bounds.top : 0;
const height = bounds ? bounds.height : window.innerHeight;
const rect = node.getBoundingClientRect();
- return rect.bottom > top - height && rect.top < top + height * 2;
+ return rect.bottom > top - height * margin && rect.top < top + height * (1 + margin);
};
/**
@@ -598,7 +610,7 @@ export function useFenceReached(
language: string | null,
chars: number,
source: string,
- warm: (tokens: boolean) => void,
+ warm: (tokens: boolean, inView?: boolean) => void,
speculate: (settle: (seeded: boolean) => void) => (() => void) | null,
): boolean {
const speculateRef = useRef(speculate);
@@ -676,7 +688,7 @@ export function useFenceReached(
outer,
language,
chars,
- warm: (tokens) => warmRef.current(tokens),
+ warm: (tokens, inView) => warmRef.current(tokens, inView),
speculate: (settle) => speculateRef.current(settle),
latch: () => setLatched(true),
// Reuses `generation`: this fence just latched, so the bump costs one render.
@@ -760,6 +772,19 @@ const parseDeclarations = (text: string): Record<string, string> => {
return style;
};
+installTokenClasses([unslothLightTheme, unslothDarkTheme]);
+
+const styledSpan = (
+ base: string,
+ style: Record<string, string>,
+): { className: string; style?: CSSProperties } => {
+ if (Object.keys(style).length === 0) return { className: base };
+ const classes = tokenClassesFor(style);
+ return classes === null
+ ? { className: base, style: style as CSSProperties }
+ : { className: base + classes };
+};
+
const tokenStyle = (
token: FenceToken,
): { style: Record<string, string>; hasBackground: boolean } => {
@@ -805,23 +830,17 @@ export const FenceLine = memo(function FenceLine({
const foreground = fenceLineForeground(line);
return (
<span
- className={foreground ? `${lineClass} ${TOKEN_CLASS}` : lineClass}
- style={
- foreground
- ? { "--sdm-c": foreground.light, "--shiki-dark": foreground.dark } as CSSProperties
- : undefined
- }
+ {...(foreground
+ ? styledSpan(`${lineClass} ${TOKEN_CLASS}`, { "--sdm-c": foreground.light, "--shiki-dark": foreground.dark })
+ : { className: lineClass })}
>
{(highlighted >= line.length ? line : line.slice(0, highlighted)).map((token, index) => {
if (foreground?.inherited[index]) return token.content;
const { style, hasBackground } = tokenStyle(token);
return (
<span
- className={
- hasBackground ? `${TOKEN_CLASS} ${TOKEN_BG_CLASS}` : TOKEN_CLASS
- }
+ {...styledSpan(hasBackground ? `${TOKEN_CLASS} ${TOKEN_BG_CLASS}` : TOKEN_CLASS, style)}
key={index}
- style={style}
{...token.htmlAttrs}
>
{token.content}
@@ -847,9 +866,8 @@ const FenceTokenChunk = memo(function FenceTokenChunk({
if (foreground?.inherited[start + index]) return token.content;
const { style, hasBackground } = tokenStyle(token);
return <span
- className={hasBackground ? `${TOKEN_CLASS} ${TOKEN_BG_CLASS}` : TOKEN_CLASS}
+ {...styledSpan(hasBackground ? `${TOKEN_CLASS} ${TOKEN_BG_CLASS}` : TOKEN_CLASS, style)}
key={index}
- style={style}
{...token.htmlAttrs}
>{token.content}</span>;
})}{highlighted < tokens.length ? tokens.slice(highlighted).map((token) => token.content).join("") : null}</>;
@@ -865,10 +883,9 @@ const DenseFenceLine = memo(function DenseFenceLine({
const chunks = useMemo(() => updateFenceChunks(null, line), [line]);
const foreground = useMemo(() => fenceLineForeground(line), [line]);
return <span
- className={foreground ? `${LINE_CLASS} ${TOKEN_CLASS}` : LINE_CLASS}
- style={foreground
- ? { "--sdm-c": foreground.light, "--shiki-dark": foreground.dark } as CSSProperties
- : undefined}
+ {...(foreground
+ ? styledSpan(`${LINE_CLASS} ${TOKEN_CLASS}`, { "--sdm-c": foreground.light, "--shiki-dark": foreground.dark })
+ : { className: LINE_CLASS })}
>{chunks.map((tokens, index) => <FenceTokenChunk
key={index}
tokens={tokens}
@@ -1099,23 +1116,24 @@ const highlightResize = (): void => {
};
// Bounded entries ignore wheel and touch scrolling; the rest still wait for the reader to stop.
-// Only with requestIdleCallback: without it (WebKit) each step is a timer inside the scroll that
-// swaps markup without colour (scroll cost rose about 6x).
-const MID_SCROLL_WORK = typeof window !== "undefined"
- && typeof (window as { requestIdleCallback?: unknown }).requestIdleCallback === "function";
const highlightQuietIn = (duringScroll: boolean): number =>
Math.max(
duringScroll ? nonScrollInputQuietIn() : inputQuietIn(),
300 - (performance.now() - highlightResizedAt),
);
+const NO_IDLE_CALLBACK = typeof window !== "undefined"
+ && typeof (window as { requestIdleCallback?: unknown }).requestIdleCallback !== "function";
+const HIGHLIGHT_RUN_MS = 6;
+
const highlightQueue = createFenceHighlightQueue({
schedule: (run, duringScroll) => {
let frame = 0;
let cancelIdle: (() => void) | undefined;
const timer = setTimeout(() => {
frame = requestAnimationFrame(() => {
- cancelIdle = (duringScroll ? scheduleIdleTask : scheduleQuietIdleTask)(run, 100);
+ cancelIdle = (duringScroll ? scheduleIdleTask : scheduleQuietIdleTask)(
+ run, duringScroll && NO_IDLE_CALLBACK ? 0 : 100);
});
}, Math.max(0, highlightQuietIn(duringScroll), inputPointerHeld() || selectionInFence(liveSelection()) ? 80 : 0));
return () => {
@@ -1127,6 +1145,7 @@ const highlightQueue = createFenceHighlightQueue({
blocked: (duringScroll) =>
inputPointerHeld() || highlightQuietIn(duringScroll) > 0 || selectionInFence(liveSelection()),
commit: (run) => flushSync(run),
+ budgetMs: HIGHLIGHT_RUN_MS,
});
const watchHighlighting = (): void => {
@@ -1170,7 +1189,7 @@ export function queueFenceWork(work: () => void): () => void {
const remove = highlightQueue.add(() => {
finish();
return true;
- }, finish, MID_SCROLL_WORK);
+ }, finish, true);
cancel = () => {
remove();
unwatchHighlighting();
@@ -1319,7 +1338,7 @@ function useFenceHighlightLimit(
);
setPrevious({ tokens, parked, progress: next });
return true;
- }, () => setPrevious({ tokens, parked, progress: null }), capped !== null && MID_SCROLL_WORK);
+ }, () => setPrevious({ tokens, parked, progress: null }), capped !== null);
return () => {
cancel();
unwatchHighlighting();
diff --git a/studio/frontend/src/components/assistant-ui/code-plugin.ts b/studio/frontend/src/components/assistant-ui/code-plugin.ts
index 4693ecdcfc4..b297968bb69 100644
--- a/studio/frontend/src/components/assistant-ui/code-plugin.ts
+++ b/studio/frontend/src/components/assistant-ui/code-plugin.ts
@@ -104,6 +104,7 @@ export const normalizeLanguage = (language: string): BundledLanguage => {
};
export const MIN_INCREMENTAL_CHARS = 2000;
+export const LIVE_TAIL_CHARS = 1024;
const REFRESH_MS = 250;
const REFRESH_SLICE_MS = 8;
// A wall-clock limit can cache a half-plain theme; bound line length for deterministic dual-theme output.
@@ -659,7 +660,8 @@ export function createCodePlugin(
const grewLargeFence =
fence.result !== null &&
fence.code.length >= MIN_INCREMENTAL_CHARS &&
- opts.code.length > fence.code.length;
+ opts.code.length > fence.code.length &&
+ opts.code.length - fence.committedLength > LIVE_TAIL_CHARS;
if (!exact && grewLargeFence && elapsed < REFRESH_MS) {
const result = approximateResult(fence, opts.code);
queuePending(fence, opts.code, callback);
diff --git a/studio/frontend/src/components/assistant-ui/fence-highlight-queue.ts b/studio/frontend/src/components/assistant-ui/fence-highlight-queue.ts
index cc7b3d4661a..1e23624f1bc 100644
--- a/studio/frontend/src/components/assistant-ui/fence-highlight-queue.ts
+++ b/studio/frontend/src/components/assistant-ui/fence-highlight-queue.ts
@@ -23,6 +23,8 @@ export type FenceHighlightQueueOptions = {
schedule: (callback: () => void, duringScroll: boolean) => () => void;
blocked: (duringScroll: boolean) => boolean;
commit: (callback: () => void) => void;
+ budgetMs?: number;
+ now?: () => number;
};
export function createFenceHighlightQueue(options: FenceHighlightQueueOptions): {
@@ -58,23 +60,42 @@ export function createFenceHighlightQueue(options: FenceHighlightQueueOptions):
}
};
+ const budgetMs = options.budgetMs ?? 0;
+ const now = options.now ?? (() => performance.now());
+
const run = (): void => {
running = true;
let entry: HighlightEntry | undefined;
+ const started = budgetMs > 0 ? now() : 0;
+ const taken = new Set<HighlightEntry>();
+ const verdicts = new Map<boolean, boolean>();
+ const blocked = (duringScroll: boolean): boolean => {
+ let verdict = verdicts.get(duringScroll);
+ if (verdict === undefined) {
+ verdict = options.blocked(duringScroll);
+ verdicts.set(duringScroll, verdict);
+ }
+ return verdict;
+ };
try {
- for (const candidate of entries) {
- if (!options.blocked(candidate.duringScroll)) {
- entry = candidate;
- break;
+ for (;;) {
+ entry = undefined;
+ for (const candidate of entries) {
+ if (!taken.has(candidate) && !blocked(candidate.duringScroll)) {
+ entry = candidate;
+ break;
+ }
}
+ if (!entry) return;
+ const current = entry;
+ taken.add(current);
+ let complete = false;
+ options.commit(() => {
+ if (entries.has(current)) complete = current.step();
+ });
+ if (entries.delete(current) && !complete) entries.add(current);
+ if (budgetMs <= 0 || flushing || now() - started >= budgetMs) return;
}
- if (!entry) return;
- const current = entry;
- let complete = false;
- options.commit(() => {
- if (entries.has(current)) complete = current.step();
- });
- if (entries.delete(current) && !complete) entries.add(current);
} catch (error) {
if (entry) entries.delete(entry);
else entries.clear();
diff --git a/studio/frontend/src/components/assistant-ui/fence-speculation.ts b/studio/frontend/src/components/assistant-ui/fence-speculation.ts
index 9f248d5752b..7fef555d6c6 100644
--- a/studio/frontend/src/components/assistant-ui/fence-speculation.ts
+++ b/studio/frontend/src/components/assistant-ui/fence-speculation.ts
@@ -18,6 +18,7 @@ export type FenceSpeculatorOptions<G extends SpeculationGate> = {
budgetFences: number;
maxInFlight?: number;
rankEvery?: number;
+ maxDistance?: () => number;
};
export type FenceSpeculator<G> = {
@@ -30,6 +31,7 @@ export type FenceSpeculator<G> = {
seededChars: number;
inFlight: boolean;
inFlightCount: number;
+ distanced: number;
};
};
@@ -42,10 +44,13 @@ export function createFenceSpeculator<G extends SpeculationGate>(
const seeded = new Map<G, number>();
let seededChars = 0;
let ranked: G[] | null = null;
+ const distance = new Map<G, number>();
let picks = 0;
const inFlight = new Map<G, { cancel: (() => void) | null }>();
let inFlightChars = 0;
let pending: (() => void) | null = null;
+ let parked = false;
+ const PARKED_RECHECK_MS = 200;
const schedule = (): void => {
if (pending || inFlight.size >= maxInFlight || candidates.size === 0) return;
@@ -56,10 +61,14 @@ export function createFenceSpeculator<G extends SpeculationGate>(
if (ranked) return ranked;
const gates = [...candidates];
const distances = options.rank(gates);
+ distance.clear();
ranked = gates
.map((gate, index) => ({ gate, at: distances[index] ?? Infinity }))
.sort((a, b) => a.at - b.at)
- .map((entry) => entry.gate);
+ .map((entry) => {
+ distance.set(entry.gate, entry.at);
+ return entry.gate;
+ });
picks = 0;
return ranked;
};
@@ -68,8 +77,13 @@ export function createFenceSpeculator<G extends SpeculationGate>(
if (seeded.size + inFlight.size >= options.budgetFences)
return { gate: null, waiting: false };
let waiting = false;
+ const horizon = options.maxDistance?.() ?? Number.POSITIVE_INFINITY;
for (const gate of order()) {
if (!candidates.has(gate)) continue;
+ if ((distance.get(gate) ?? Number.POSITIVE_INFINITY) > horizon) {
+ parked = true;
+ break;
+ }
if (seededChars + inFlightChars + gate.chars > options.budgetChars) continue;
if (!options.eligible(gate)) {
waiting = true;
@@ -142,6 +156,7 @@ export function createFenceSpeculator<G extends SpeculationGate>(
},
remove: (gate) => {
candidates.delete(gate);
+ distance.delete(gate);
ranked = null;
const share = seeded.get(gate);
if (share !== undefined) {
@@ -157,6 +172,9 @@ export function createFenceSpeculator<G extends SpeculationGate>(
},
rerank: () => {
ranked = null;
+ if (!parked || pending || inFlight.size >= maxInFlight) return;
+ parked = false;
+ pending = options.wait(run, PARKED_RECHECK_MS);
},
snapshot: () => ({
candidates: candidates.size,
@@ -164,6 +182,7 @@ export function createFenceSpeculator<G extends SpeculationGate>(
seededChars,
inFlight: inFlight.size > 0,
inFlightCount: inFlight.size,
+ distanced: distance.size,
}),
};
}
diff --git a/studio/frontend/src/components/assistant-ui/fence-token-classes.ts b/studio/frontend/src/components/assistant-ui/fence-token-classes.ts
new file mode 100644
index 00000000000..cd2b8763a0e
--- /dev/null
+++ b/studio/frontend/src/components/assistant-ui/fence-token-classes.ts
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import type { ThemeRegistrationAny } from "shiki";
+
+// WebKit shares no computed style across inline-styled elements (3x style work), so token colours are classes written once.
+// Must be installed before any chat renders: a sheet edited later restyles the whole document on WebKit.
+
+const COLOR = /^#(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i;
+const COLOR_PROPERTIES = ["--sdm-c", "--shiki-dark"] as const;
+const KEYWORD_PROPERTIES: Readonly<Record<string, readonly string[]>> = {
+ "font-style": ["italic", "normal", "inherit"],
+ "font-weight": ["bold", "normal", "inherit"],
+ "text-decoration": ["underline", "line-through", "none", "inherit"],
+};
+
+const themeColors = (theme: ThemeRegistrationAny): string[] => {
+ const found = new Set<string>();
+ const add = (value: unknown): void => {
+ if (typeof value === "string" && COLOR.test(value)) found.add(value);
+ };
+ add(theme.fg);
+ add(theme.colors?.["editor.foreground"]);
+ for (const rule of theme.tokenColors ?? theme.settings ?? []) add(rule.settings?.foreground);
+ return [...found];
+};
+
+export function buildTokenClasses(
+ themes: readonly ThemeRegistrationAny[],
+): { names: Map<string, string>; css: string } {
+ const names = new Map<string, string>();
+ const rules: string[] = [];
+ const add = (property: string, value: string): void => {
+ const key = `${property}:${value}`;
+ if (names.has(key)) return;
+ const name = `sdm-t${names.size.toString(36)}`;
+ names.set(key, name);
+ rules.push(`.${name}{${property}:${value}}`);
+ };
+ for (const color of themes.flatMap(themeColors)) {
+ for (const spelling of new Set([color, color.toLowerCase(), color.toUpperCase()])) {
+ for (const property of COLOR_PROPERTIES) add(property, spelling);
+ }
+ }
+ for (const [property, values] of Object.entries(KEYWORD_PROPERTIES)) {
+ for (const prefix of ["", "--shiki-light-", "--shiki-dark-"]) {
+ for (const value of values) add(prefix ? `${prefix}${property}` : property, value);
+ }
+ }
+ return { names, css: rules.join("\n") };
+}
+
+let installed: Map<string, string> | null = null;
+
+export function installTokenClasses(themes: readonly ThemeRegistrationAny[]): void {
+ if (installed !== null || typeof document === "undefined" || !document.head) return;
+ const { names, css } = buildTokenClasses(themes);
+ const element = document.createElement("style");
+ element.setAttribute("data-fence-token-classes", "");
+ element.textContent = css;
+ document.head.appendChild(element);
+ installed = names;
+}
+
+export function tokenClassesFor(style: Readonly<Record<string, string>>): string | null {
+ if (installed === null) return null;
+ let classes = "";
+ for (const [property, value] of Object.entries(style)) {
+ const name = installed.get(`${property}:${value}`);
+ if (name === undefined) return null;
+ classes += ` ${name}`;
+ }
+ return classes;
+}
diff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
index 9ebc9e25d3b..3ab87483647 100644
--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx
+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
@@ -1015,7 +1015,8 @@ const tokenizesOffThread = (body: string, streaming: boolean): boolean =>
const slowMainThreadRegex =
typeof window !== "undefined"
&& typeof window.requestIdleCallback !== "function";
-const MAIN_THREAD_TAIL_CHARS = 512;
+// JavaScriptCore interprets lookbehind patterns, so there even a short fence goes to the worker.
+const MAIN_THREAD_TAIL_CHARS = slowMainThreadRegex ? 0 : 512;
const settlesOffThread = (
body: string,
@@ -1023,7 +1024,7 @@ const settlesOffThread = (
options: ReturnType<typeof fenceHighlightOptions>,
): boolean => {
if (!tokenizesOffThread(body, streaming)) {
- if (streaming || !slowMainThreadRegex) return false;
+ if (streaming) return false;
const state = highlightWorkerState();
if (state === "unavailable" || state === "stalled") return false;
}
@@ -1165,7 +1166,7 @@ function useFenceTokens(
}
show(settled);
};
- if (defer()) {
+ if (defer() && !settlesOffThread(body, streaming, options)) {
if (displayed.current !== null
&& (displayed.current.body !== body || displayed.current.language !== languageToken)) {
show(null);
@@ -1302,12 +1303,13 @@ function FenceBlock({
/* warm(true) tokenizes now (a sync cache hit for render); warm(false) only loads the grammar. */
const warm = useCallback(
- (tokens: boolean) => {
+ (tokens: boolean, inView = false) => {
const body = trimTrailingNewlines(source);
const options = fenceHighlightOptions(body, languageToken);
const inline =
tokens
- && (!settlesOffThread(body, Boolean(isIncomplete), options)
+ && ((inView && !slowMainThreadRegex && !tokenizesOffThread(body, Boolean(isIncomplete)))
+ || !settlesOffThread(body, Boolean(isIncomplete), options)
|| code.cached(options) !== null);
if (inline && !isIncomplete) code.highlightExact(options, () => {});
else code.highlight(inline ? options : fenceHighlightOptions("", languageToken), () => {});
diff --git a/studio/frontend/src/components/assistant-ui/reasoning-highlight.ts b/studio/frontend/src/components/assistant-ui/reasoning-highlight.ts
index 2947c69f4b5..1726916ac0c 100644
--- a/studio/frontend/src/components/assistant-ui/reasoning-highlight.ts
+++ b/studio/frontend/src/components/assistant-ui/reasoning-highlight.ts
@@ -52,6 +52,33 @@ export const orderHighlightRequests = <T extends { speculative?: boolean }>(
...requests.filter((request) => request.speculative),
];
+/*
+ * A full result as it crosses to the main thread: tokens with equal styles share one style object
+ * (the clone keeps shared references, so the main thread rebuilds a handful of styles instead of
+ * one per token), and no grammar state, which nothing there resumes from a worker result.
+ */
+export function transferableResult(result: HighlightResult): HighlightResult {
+ const styles = new Map<string, Record<string, string>>();
+ const shared = (style: Record<string, string>): Record<string, string> => {
+ let key = "";
+ for (const property in style) key += `${property}:${style[property]};`;
+ const known = styles.get(key);
+ if (known !== undefined) return known;
+ styles.set(key, style);
+ return style;
+ };
+ const transferred: HighlightResult = {
+ ...result,
+ tokens: result.tokens.map((line) =>
+ line.map((token) =>
+ token.htmlStyle ? { ...token, htmlStyle: shared(token.htmlStyle) } : token,
+ ),
+ ),
+ };
+ delete transferred.grammarState;
+ return transferred;
+}
+
/** Transfer only requested lines; the worker keeps the complete grammar context. */
export function reasoningHighlightReply(
request: ReasoningHighlightRequest,
@@ -66,6 +93,8 @@ export function reasoningHighlightReply(
return tokens ? [{ line, tokens }] : [];
})
: [],
- ...(request.full ? { result } : {}),
+ ...(request.full
+ ? { result: result === null ? null : transferableResult(result) }
+ : {}),
};
}
diff --git a/studio/frontend/tests/code-fence-body.test.ts b/studio/frontend/tests/code-fence-body.test.ts
index c983509be64..20f95e506a6 100644
--- a/studio/frontend/tests/code-fence-body.test.ts
+++ b/studio/frontend/tests/code-fence-body.test.ts
@@ -408,7 +408,7 @@ test("a capped block colours only what is in view and a streaming one follows it
const hook = DEFER.slice(DEFER.indexOf("function useFenceHighlightLimit("), DEFER.indexOf("const READER_SCROLL_EVENTS"));
assert.match(hook, /const capped = innerLineWindow\(code\.current, body\);/);
assert.match(hook, /lineWindowColoured\(tokens, progress, capped\)/);
- assert.match(hook, /\}, \(\) => setPrevious\(\{ tokens, parked, progress: null \}\), capped !== null && MID_SCROLL_WORK\);/);
+ assert.match(hook, /\}, \(\) => setPrevious\(\{ tokens, parked, progress: null \}\), capped !== null\);/);
const pin = DEFER.slice(DEFER.indexOf("function usePinnedWhileStreaming("), DEFER.indexOf("export const FenceBody = memo("));
assert.match(pin, /if \(atBottom \|\| performance\.now\(\) - readerAt < 250\) following\.current = atBottom;/);
assert.match(pin, /body\.scrollTop = body\.scrollHeight;/);
diff --git a/studio/frontend/tests/code-fence-defer.test.ts b/studio/frontend/tests/code-fence-defer.test.ts
index 6d74feee637..6e311f952f4 100644
--- a/studio/frontend/tests/code-fence-defer.test.ts
+++ b/studio/frontend/tests/code-fence-defer.test.ts
@@ -293,9 +293,9 @@ test("a print upgrades the whole document, and never puts it back", () => {
test("an upgrade taken inside one task warms, flushes, and flushes again", () => {
const latchNow = SOURCE.slice(SOURCE.indexOf("const latchNow"));
const body = latchNow.slice(0, latchNow.indexOf("\n};"));
- assert.ok(body.includes("gate.warm(true)"), "the tokens have to exist before the swap renders");
+ assert.ok(body.includes("gate.warm(true, "), "the tokens have to exist before the swap renders");
assert.ok(
- body.indexOf("gate.warm(true)") < body.indexOf("flushSync"),
+ body.indexOf("gate.warm(true, ") < body.indexOf("flushSync"),
"warming after the flush is warming after the paint",
);
assert.equal(
diff --git a/studio/frontend/tests/code-fence-foreground.test.ts b/studio/frontend/tests/code-fence-foreground.test.ts
index 720364baaa9..cc709fc6fb3 100644
--- a/studio/frontend/tests/code-fence-foreground.test.ts
+++ b/studio/frontend/tests/code-fence-foreground.test.ts
@@ -41,6 +41,7 @@ const wanted = new Set([
"TOKEN_CLASS",
"TOKEN_BG_CLASS",
"tokenStyle",
+ "styledSpan",
"FenceLine",
]);
const declarations = parsed.statements
@@ -70,6 +71,7 @@ const scope = {
isBlankLine,
plainLineText,
fenceLineForeground,
+ tokenClassesFor: (() => null) as (style: Record<string, string>) => string | null,
};
runInNewContext(outputText, scope);
const render = (line: Token[], windowed = true) =>
diff --git a/studio/frontend/tests/code-plugin-fork.test.ts b/studio/frontend/tests/code-plugin-fork.test.ts
index e0915b3fe8e..1ec6058cdfa 100644
--- a/studio/frontend/tests/code-plugin-fork.test.ts
+++ b/studio/frontend/tests/code-plugin-fork.test.ts
@@ -49,6 +49,7 @@ test("settled fences that extend one another each get exact tokens once, and the
});
test("an exact request leaves a streaming sibling's queued refresh in place", async () => {
+ const b = a + lines("b", 80);
const reference = createCodePlugin({ themes: THEMES });
const expected = await exactly(reference, b);
const plugin = createCodePlugin({ themes: THEMES });
diff --git a/studio/frontend/tests/code-plugin-incremental.test.ts b/studio/frontend/tests/code-plugin-incremental.test.ts
index faca40ce35b..0acf37d9627 100644
--- a/studio/frontend/tests/code-plugin-incremental.test.ts
+++ b/studio/frontend/tests/code-plugin-incremental.test.ts
@@ -13,6 +13,8 @@ import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
import {
createCodePlugin,
+ LIVE_TAIL_CHARS,
+ MIN_INCREMENTAL_CHARS,
TOKENIZE_LIMITS,
} from "../src/components/assistant-ui/code-plugin.ts";
@@ -541,6 +543,9 @@ test("a changed theme definition is not answered from the old theme's cache", as
);
});
+// A cached prefix more than LIVE_TAIL_CHARS short of the body, so growing to it takes the throttled path.
+const cachedHead = (body: string): string => body.slice(0, body.indexOf("\n", body.length - LIVE_TAIL_CHARS - 200) + 1);
+
test("a shorter delimiter-like sibling cannot drop a queued refresh", async () => {
const plugin = createCodePlugin({ themes: THEMES });
const language = "python" as HighlightOptions["language"];
@@ -548,7 +553,7 @@ test("a shorter delimiter-like sibling cannot drop a queued refresh", async () =
{ length: 120 },
(_, index) => `value_${index} = ${index} # a completed Python line`,
).join("\n")}\n`;
- await highlightOnce(plugin, { code: body, language, themes: THEMES });
+ await highlightOnce(plugin, { code: cachedHead(body), language, themes: THEMES });
const longer = `${body}\`\`\``;
const arrived: HighlightResult[] = [];
@@ -576,7 +581,7 @@ test("shedding a possible closing delimiter settles before the shorter result",
{ length: 120 },
(_, index) => `value_${index} = ${index} # a completed Python line`,
).join("\n")}\n`;
- await highlightOnce(plugin, { code: `${body}\`\``, language, themes: THEMES });
+ await highlightOnce(plugin, { code: cachedHead(body), language, themes: THEMES });
const settled: HighlightResult[] = [];
plugin.highlight({ code: `${body}\`\`\``, language, themes: THEMES }, (result) =>
@@ -661,6 +666,22 @@ test("a block that keeps failing to tokenize does not fill the cache", async ()
);
});
+test("a short growth of a large fence is coloured at once", async () => {
+ const plugin = createCodePlugin({ themes: THEMES });
+ const language = "python" as HighlightOptions["language"];
+ const initial = Array.from(
+ { length: 90 },
+ (_, index) => `value_${index} = ${index} # a completed Python line`,
+ ).join("\n");
+ assert.ok(initial.length > MIN_INCREMENTAL_CHARS);
+ await highlightOnce(plugin, { code: initial, language, themes: THEMES });
+ for (const grown of [`${initial}\nnext_value = 100`, `${initial}\nnext_value = 100\nand_more = 101`]) {
+ const now = plugin.highlight({ code: grown, language, themes: THEMES }, () => {});
+ assert.ok(now, "answered in the same call");
+ assert.deepEqual(now.tokens, (await reference(grown, "python")).tokens, "every streamed line is coloured, none waits for the refresh");
+ }
+});
+
test("a throttled multiline tail matches the previous plugin", async () => {
const plugin = createCodePlugin({ themes: THEMES });
const language = "python" as HighlightOptions["language"];
@@ -673,7 +694,7 @@ test("a throttled multiline tail matches the previous plugin", async () => {
language,
themes: THEMES,
});
- const grown = `${initial}\nnext_value = 100\nand_more = 101`;
+ const grown = `${initial}\n${Array.from({ length: 40 }, (_, index) => `next_value_${index} = ${index} # streamed`).join("\n")}`;
const throttled = await highlightOnce(plugin, {
code: grown,
language,
diff --git a/studio/frontend/tests/code-plugin-seed.test.ts b/studio/frontend/tests/code-plugin-seed.test.ts
index 8bad97e1569..6cc9c23498d 100644
--- a/studio/frontend/tests/code-plugin-seed.test.ts
+++ b/studio/frontend/tests/code-plugin-seed.test.ts
@@ -26,7 +26,7 @@ test("the exact path never answers with the throttled approximation of a fence t
const reference = createCodePlugin({ themes: THEMES });
const base = Array.from({ length: 120 }, (_, i) => `const value${i} = ${i} * 2;`).join("\n") + "\n";
assert.ok(base.length > 2000);
- const longer = `${base}export function tail() {\n return value1 + value2;\n}\n`;
+ const longer = `${base}${Array.from({ length: 60 }, (_, i) => `export function tail${i}() {\n return value1 + value2;\n}`).join("\n")}\n`;
await highlighted(plugin, base);
const approximate = plugin.highlight(options(longer), () => {});
const exact = plugin.highlightExact(options(longer));
diff --git a/studio/frontend/tests/desktop-webkit-paths.test.ts b/studio/frontend/tests/desktop-webkit-paths.test.ts
index 54961308824..5f4b3139a2d 100644
--- a/studio/frontend/tests/desktop-webkit-paths.test.ts
+++ b/studio/frontend/tests/desktop-webkit-paths.test.ts
@@ -66,11 +66,12 @@ test("grammar work waits for a held pointer and the reader, and without requestI
assert.ok(defer.indexOf("if (warmMustWait()) {") < defer.indexOf("grammarsWarmed.add(language);"), "a warm that has to wait does not mark its grammar warmed");
});
-test("settled fences go to the worker where the main thread's regex engine is slow", () => {
+test("settled fences go to the worker on every engine, and on WebKit even a short one", () => {
const markdown = readSrc("components/assistant-ui/markdown-text.tsx");
assert.match(markdown, /const slowMainThreadRegex =\s*typeof window !== "undefined"\s*&& typeof window\.requestIdleCallback !== "function";/);
- assert.match(markdown, /if \(!tokenizesOffThread\(body, streaming\)\) \{\s*if \(streaming \|\| !slowMainThreadRegex\) return false;\s*const state = highlightWorkerState\(\);\s*if \(state === "unavailable" \|\| state === "stalled"\) return false;\s*\}\s*return code\.cover\(options\)\.uncovered > MAIN_THREAD_TAIL_CHARS;/, "JavaScriptCore interprets every lookbehind pattern, so WebKit tokenizes settled fences off the main thread, and a fence too long for the main thread goes to the worker, unless the main thread's own cache leaves only a short tail, as it does for the fence that just streamed");
- assert.match(markdown, /const MAIN_THREAD_TAIL_CHARS = 512;/);
+ assert.match(markdown, /if \(!tokenizesOffThread\(body, streaming\)\) \{\s*if \(streaming\) return false;\s*const state = highlightWorkerState\(\);\s*if \(state === "unavailable" \|\| state === "stalled"\) return false;\s*\}\s*return code\.cover\(options\)\.uncovered > MAIN_THREAD_TAIL_CHARS;/, "a settled fence is tokenized by the worker while it answers, so scrolling and opening a chat keep the tokenizer off the main thread, unless the main thread's own cache leaves only a short tail, as it does for the fence that just streamed");
+ assert.match(markdown, /const MAIN_THREAD_TAIL_CHARS = slowMainThreadRegex \? 0 : 512;/, "JavaScriptCore interprets every lookbehind pattern, so WebKit sends even a short fence to the worker");
+ assert.match(markdown, /\(inView && !slowMainThreadRegex && !tokenizesOffThread\(body, Boolean\(isIncomplete\)\)\)/, "only a fence on screen with no tokens yet is tokenized on the main thread at the latch, and never on WebKit");
const warm = markdown.slice(markdown.indexOf("const warm = useCallback("), markdown.indexOf("const speculate = useCallback("));
assert.match(warm, /!settlesOffThread\(body, Boolean\(isIncomplete\), options\)/, "the reach latch's warm routes a settled fence the same way the render does, or a scroll tokenizes on the main thread a fence WebKit sends to the worker");
assert.equal(markdown.includes("fenceJustStreamed"), false, "routing reads the cache itself, not a guess about which fence streamed last");
@@ -113,11 +114,10 @@ test("settled WebKitGTK fences only line-window themselves while code wrapping i
assert.match(defer, /attributeFilter: \["data-code-wrap", "data-wrap"\]/);
});
-test("without requestIdleCallback fence work waits for the reader to stop scrolling", () => {
+test("fence work runs mid-scroll on every engine once the worker colours it", () => {
const defer = readSrc("components/assistant-ui/code-fence-defer.tsx");
- assert.match(defer, /const MID_SCROLL_WORK = typeof window !== "undefined"\s*&& typeof \(window as \{ requestIdleCallback\?: unknown \}\)\.requestIdleCallback === "function";/,
- "WebKit has no requestIdleCallback: a mid-scroll step there is a timer inside the scroll, and the reach it makes swaps markup the worker has not coloured yet");
- assert.match(defer, /const remove = highlightQueue\.add\(\(\) => \{\s*finish\(\);\s*return true;\s*\}, finish, MID_SCROLL_WORK\);/,
- "a reach, a start and a worker answer run mid-scroll only where idle callbacks exist");
- assert.match(defer, /capped !== null && MID_SCROLL_WORK\);/, "so do the lines in view inside a capped block");
+ assert.doesNotMatch(defer, /MID_SCROLL_WORK/,
+ "the worker supplies coloured tokens mid-scroll, so WebKit no longer has to wait for the reader to stop");
+ assert.match(defer, /const remove = highlightQueue\.add\(\(\) => \{\s*finish\(\);\s*return true;\s*\}, finish, true\);/);
+ assert.match(defer, /budgetMs: HIGHLIGHT_RUN_MS,/, "each mid-scroll run stays inside a slice of a frame");
});
diff --git a/studio/frontend/tests/fence-highlight-queue.test.ts b/studio/frontend/tests/fence-highlight-queue.test.ts
index 889ff6ee082..888fe52d8b9 100644
--- a/studio/frontend/tests/fence-highlight-queue.test.ts
+++ b/studio/frontend/tests/fence-highlight-queue.test.ts
@@ -261,3 +261,54 @@ test("mid-scroll only entries marked for it run, ahead of older ones that wait f
h.run();
assert.deepEqual(ran, ["lines in view", "rest of a long fence"]);
});
+
+test("with a budget one run finishes several waiting fences, each at most once, until the budget is spent", () => {
+ let clock = 0;
+ const h = harness({ budgetMs: 6, now: () => clock });
+ const ran: string[] = [];
+ let unfinished = 0;
+ h.queue.add(() => { ran.push("long"); clock += 1; return ++unfinished === 2; }, () => {});
+ h.queue.add(() => { ran.push("reach"); clock += 1; return true; }, () => {});
+ h.queue.add(() => { ran.push("answer"); clock += 1; return true; }, () => {});
+ h.run();
+ assert.deepEqual(ran, ["long", "reach", "answer"], "one task, three fences, and the unfinished one waits for the next run");
+ assert.equal(h.commits(), 3, "each fence still commits on its own");
+ assert.equal(h.queue.pending(), 1);
+ h.run();
+ assert.deepEqual(ran, ["long", "reach", "answer", "long"]);
+ assert.equal(h.queue.pending(), 0);
+
+ for (let i = 0; i < 4; i += 1) h.queue.add(() => { ran.push(`slow ${i}`); clock += 5; return true; }, () => {});
+ h.run();
+ assert.deepEqual(ran.slice(4), ["slow 0", "slow 1"], "the run stops once the budget is spent");
+ assert.equal(h.queue.pending(), 2);
+});
+
+test("a run asks whether work is blocked once per kind, not once per fence", () => {
+ let asked = 0;
+ const h = harness({
+ budgetMs: 100,
+ now: () => 0,
+ blocked: (duringScroll) => { asked += 1; return !duringScroll; },
+ });
+ const ran: number[] = [];
+ for (let i = 0; i < 5; i += 1) h.queue.add(() => { ran.push(i); return true; }, () => {}, i % 2 === 0);
+ h.run();
+ assert.deepEqual(ran, [0, 2, 4], "only the mid-scroll entries run while scrolling");
+ assert.equal(asked, 2, "a selection or a held pointer is read twice per run, not once per fence");
+});
+
+test("without a budget a run still takes exactly one fence", () => {
+ const h = harness();
+ const ran: number[] = [];
+ for (let i = 0; i < 3; i += 1) h.queue.add(() => { ran.push(i); return true; }, () => {});
+ h.run();
+ assert.deepEqual(ran, [0]);
+});
+
+test("the fence queue is given a run budget", () => {
+ const defer = readSrc("components/assistant-ui/code-fence-defer.tsx");
+ assert.match(defer, /const HIGHLIGHT_RUN_MS = 6;/);
+ assert.match(defer, /commit: \(run\) => flushSync\(run\),\s*budgetMs: HIGHLIGHT_RUN_MS,/);
+ assert.match(defer, /\(\s*run, duringScroll && NO_IDLE_CALLBACK \? 0 : 100\)/, "without requestIdleCallback mid-scroll work runs after the frame instead of a 100 ms timer");
+});
diff --git a/studio/frontend/tests/fence-speculation-wiring.test.ts b/studio/frontend/tests/fence-speculation-wiring.test.ts
index 108a7e99d4d..8afed365e84 100644
--- a/studio/frontend/tests/fence-speculation-wiring.test.ts
+++ b/studio/frontend/tests/fence-speculation-wiring.test.ts
@@ -11,14 +11,17 @@ const MARKDOWN = readSrc("components/assistant-ui/markdown-text.tsx");
const THREAD = readSrc("components/assistant-ui/thread.tsx");
const PLUGIN = readSrc("components/assistant-ui/code-plugin.ts");
-test("the speculator seeds the same cache key the demanded path reads, and only fences the main thread has warmed", () => {
- assert.match(DEFER, /eligible: \(gate\) => grammarsWarmed\.has\(grammarOf\(gate\)\),/);
+test("the speculator seeds the same cache key the demanded path reads, once the worker answers or the main thread has warmed the grammar", () => {
+ assert.match(DEFER, /eligible: \(gate\) =>\s*highlightWorkerState\(\) === "ready" \|\| grammarsWarmed\.has\(grammarOf\(gate\)\),/);
assert.match(DEFER, /enabled: \(\) =>\s*fenceMode\(\) !== "off" && highlightWorkerState\(\) !== "unavailable",/);
- assert.match(DEFER, /busy: \(\) => streamActive\(\) \|\| highlightWorkerState\(\) === "stalled"\s*\|\| inputPointerHeld\(\) \|\| inputQuietIn\(\) > 0,/);
+ assert.match(DEFER, /busy: \(\) => streamActive\(\) \|\| highlightWorkerState\(\) === "stalled"\s*\|\| inputPointerHeld\(\) \|\| nonScrollInputQuietIn\(\) > 0,/, "a wheel or touch scroll does not hold speculation: the worker tokenizes, the main thread only posts and seeds");
+ assert.match(DEFER, /const fenceSpeculator = createFenceSpeculator<FenceGate>\(\{\s*idle: \(callback, timeout\) => scheduleIdleTask\(callback, timeout\),/, "nor does the idle slot it waits for");
assert.match(DEFER, /maxChars: MAX_HIGHLIGHT_CHARS,\s*budgetChars: MAX_CACHED_CHARACTERS \/ 2,\s*budgetFences: MAX_FENCES \/ 2,/);
assert.match(DEFER, /budgetFences: MAX_FENCES \/ 2,\s*maxInFlight: SPECULATION_IN_FLIGHT,/, "a few requests stay queued in the worker so speculation is not paced by main-thread idle slots");
assert.match(DEFER, /const SPECULATION_IN_FLIGHT = 3;/);
- assert.match(DEFER, /busy: \(\) => streamActive\(\) \|\| highlightWorkerState\(\) === "stalled"\s*\|\| inputPointerHeld\(\) \|\| inputQuietIn\(\) > 0,/, "a worker that is late pauses speculation instead of dropping every candidate, so it resumes on the ready message");
+ assert.match(DEFER, /maxDistance: \(\) => SPECULATION_VIEWPORTS \* window\.innerHeight,/, "only fences a quick scroll can reach are tokenized ahead, however long the chat");
+ assert.match(DEFER, /const SPECULATION_VIEWPORTS = 3;/);
+ assert.match(DEFER, /busy: \(\) => streamActive\(\) \|\| highlightWorkerState\(\) === "stalled"\s*\|\| inputPointerHeld\(\) \|\| nonScrollInputQuietIn\(\) > 0,/, "a worker that is late pauses speculation instead of dropping every candidate, so it resumes on the ready message");
assert.match(DEFER, /fenceMode\(\) !== "off" && highlightWorkerState\(\) !== "unavailable",/, "only a worker that failed turns speculation off");
assert.match(DEFER, /rect \?\?= gate\.node\.getBoundingClientRect\(\);/, "ranked by the fence itself, or by its row while the row is skipped");
assert.equal(DEFER.includes("gate.anchor"), false);
@@ -31,7 +34,8 @@ test("the speculator seeds the same cache key the demanded path reads, and only
test("every gate joins and leaves the speculator with its registration, and a latch withdraws it first", () => {
assert.match(DEFER, /scheduleGrammarWarm\(\);\s*fenceSpeculator\.add\(registered\);/);
assert.match(DEFER, /fenceSpeculator\.remove\(registered\);\s*unreached\.delete\(registered\);/);
- assert.match(DEFER, /unreached\.delete\(gate\);\s*fenceSpeculator\.remove\(gate\);\s*gate\.warm\(true\);/);
+ assert.match(DEFER, /unreached\.delete\(gate\);\s*fenceSpeculator\.remove\(gate\);\s*gate\.warm\(true, performance\.now\(\) - started < LATCH_INLINE_MS && gateInView\(gate\)\);/, "a latch tokenizes here only fences already on screen, inside its budget");
+ assert.match(DEFER, /warm: \(tokens, inView\) => warmRef\.current\(tokens, inView\),/, "the registered warm forwards on-screen, so a latched fence tokenizes inline");
assert.match(DEFER, /latchNow\(arrived\);\s*fenceSpeculator\.rerank\(\);/);
assert.match(PLUGIN, /const reachInto = \(fence: Fence, code: string\): number => \{\s*if \(fence\.result === null \|\| fence\.seeded\) return -1;/, "a seeded entry is an exact hit only, never a prefix anchor");
assert.match(PLUGIN, /export const MAX_FENCES = 512;\s*export const MAX_CACHED_CHARACTERS = 512_000;/);
diff --git a/studio/frontend/tests/fence-speculation.test.ts b/studio/frontend/tests/fence-speculation.test.ts
index 9254e4bcd28..8ebc86b7fa9 100644
--- a/studio/frontend/tests/fence-speculation.test.ts
+++ b/studio/frontend/tests/fence-speculation.test.ts
@@ -254,3 +254,52 @@ test("a settle for a fence that is no longer in flight is ignored and ranking re
h.runIdle();
assert.ok(h.settled.has(b));
});
+
+test("fences beyond the horizon wait, and a rerank that brings them closer resumes speculation", () => {
+ let horizon = 250;
+ const h = harness({ maxDistance: () => horizon });
+ h.warmed.add("js");
+ const near = h.make("near", 100);
+ const far = h.make("far", 900);
+ h.speculator.add(near);
+ h.speculator.add(far);
+ h.runIdle();
+ assert.deepEqual([...h.settled.keys()].map((gate) => gate.name), ["near"]);
+ h.settled.get(near)?.(true);
+ h.settled.delete(near);
+ h.runIdle();
+ assert.equal(h.settled.size, 0, "the far fence is past the horizon");
+ assert.equal(h.idle.length, 0, "and nothing polls for it");
+ horizon = 1000;
+ h.speculator.rerank();
+ assert.equal(h.waits.length, 1, "a rerank looks again after a short wait, not on every scroll event");
+ assert.equal(h.waits[0].ms, 200);
+ h.speculator.rerank();
+ assert.equal(h.waits.length, 1, "further reranks while it waits add nothing");
+ h.waits.shift()?.callback();
+ assert.deepEqual([...h.settled.keys()].map((gate) => gate.name), ["far"]);
+});
+
+test("without a horizon a rerank only refreshes the order", () => {
+ const h = harness();
+ h.warmed.add("js");
+ h.speculator.add(h.make("only", 100));
+ h.runIdle();
+ h.speculator.rerank();
+ assert.equal(h.idle.length, 0);
+ assert.equal(h.waits.length, 0);
+});
+
+test("a removed fence leaves the distance cache, so an unmounted message is not kept alive", () => {
+ const h = harness();
+ h.warmed.add("js");
+ const a = h.make("a", 0);
+ const b = h.make("b", 5);
+ h.speculator.add(a);
+ h.speculator.add(b);
+ h.runIdle();
+ assert.equal(h.speculator.snapshot().distanced, 2);
+ h.speculator.remove(a);
+ h.speculator.remove(b);
+ assert.equal(h.speculator.snapshot().distanced, 0);
+});
diff --git a/studio/frontend/tests/fence-token-classes.test.ts b/studio/frontend/tests/fence-token-classes.test.ts
new file mode 100644
index 00000000000..abfd93c6f04
--- /dev/null
+++ b/studio/frontend/tests/fence-token-classes.test.ts
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { unslothDarkTheme, unslothLightTheme } from "../src/components/assistant-ui/code-themes.ts";
+import {
+ buildTokenClasses,
+ installTokenClasses,
+ tokenClassesFor,
+} from "../src/components/assistant-ui/fence-token-classes.ts";
+import { readSrc } from "./helpers/kit.ts";
+
+const THEMES = [unslothLightTheme, unslothDarkTheme];
+
+// Every declaration the bundled themes put on token spans in a long rendered chat (both colour cases).
+const SEEN = [
+ "--sdm-c:#383A42", "--sdm-c:#383a42", "--sdm-c:#E45649", "--sdm-c:#e45649", "--sdm-c:#A626A4",
+ "--sdm-c:#a626a4", "--sdm-c:#986801", "--sdm-c:#4078F2", "--sdm-c:#50A14F", "--sdm-c:#C18401",
+ "--sdm-c:#0184BC", "--sdm-c:#A0A1A7", "--shiki-dark:#ABB2BF", "--shiki-dark:#abb2bf",
+ "--shiki-dark:#E06C75", "--shiki-dark:#e06c75", "--shiki-dark:#C678DD", "--shiki-dark:#c678dd",
+ "--shiki-dark:#E5C07B", "--shiki-dark:#D19A66", "--shiki-dark:#d19a66", "--shiki-dark:#61AFEF",
+ "--shiki-dark:#56B6C2", "--shiki-dark:#98C379", "--shiki-dark:#7F848E",
+ "--shiki-dark-font-style:italic", "--shiki-light-font-style:inherit", "--shiki-light-font-style:italic",
+];
+
+test("the bundled themes yield a class for every declaration their tokens carry, each setting exactly it", () => {
+ const { names, css } = buildTokenClasses(THEMES);
+ for (const key of SEEN) assert.ok(names.has(key), key);
+ const rules = css.split("\n");
+ assert.equal(rules.length, names.size);
+ for (const [key, name] of names) {
+ const colon = key.indexOf(":");
+ assert.ok(rules.includes(`.${name}{${key.slice(0, colon)}:${key.slice(colon + 1)}}`), key);
+ }
+ assert.ok(names.size < 400, "a few hundred rules at most, written once");
+});
+
+test("without a document nothing is installed and spans keep inline colours; once installed, known sets map to classes", () => {
+ assert.equal(tokenClassesFor({ "--sdm-c": "#383A42" }), null);
+ const appended: { textContent: string }[] = [];
+ const element = { textContent: "", setAttribute: () => {} };
+ (globalThis as { document?: unknown }).document = {
+ head: { appendChild: (node: { textContent: string }) => appended.push(node) },
+ createElement: () => element,
+ };
+ try {
+ installTokenClasses(THEMES);
+ installTokenClasses(THEMES);
+ assert.equal(appended.length, 1, "the sheet is written once and never edited");
+ const classes = tokenClassesFor({ "--sdm-c": "#383A42", "--shiki-dark": "#ABB2BF" });
+ assert.match(classes ?? "", /^ sdm-t\w+ sdm-t\w+$/);
+ assert.equal(tokenClassesFor({ "--sdm-c": "#123456" }), null, "a colour outside the themes stays inline");
+ assert.equal(tokenClassesFor({ "--sdm-c": "#383A42", "--sdm-tbg": "#383A42" }), null);
+ } finally {
+ delete (globalThis as { document?: unknown }).document;
+ }
+});
+
+test("every token span takes its colours through the shared classes, installed at module load from the bundled themes", () => {
+ const defer = readSrc("components/assistant-ui/code-fence-defer.tsx");
+ assert.match(defer, /installTokenClasses\(\[unslothLightTheme, unslothDarkTheme\]\);/);
+ assert.equal(defer.includes("style={style}"), false);
+ assert.equal((defer.match(/\{\.\.\.styledSpan\(/g) ?? []).length, 2);
+ assert.equal((defer.match(/styledSpan\(`\$\{(?:lineClass|LINE_CLASS)\} \$\{TOKEN_CLASS\}`/g) ?? []).length, 2);
+ const markdown = readSrc("components/assistant-ui/markdown-text.tsx");
+ assert.match(markdown, /const STREAMDOWN_SHIKI_THEME = \[\s*unslothLightTheme,\s*unslothDarkTheme,\s*\]/,
+ "the classes come from the themes the highlighter uses");
+});
diff --git a/studio/frontend/tests/fence-token-quiet-work.test.ts b/studio/frontend/tests/fence-token-quiet-work.test.ts
index 397048877f6..a5f8d24d0d6 100644
--- a/studio/frontend/tests/fence-token-quiet-work.test.ts
+++ b/studio/frontend/tests/fence-token-quiet-work.test.ts
@@ -153,21 +153,34 @@ test("streaming, window, off and recent completions retain immediate origin", ()
}
});
+test("an off-thread fence asks the worker at once; only the answer waits for quiet", () => {
+ const h = harness();
+ h.options.offThread = true;
+ h.options.long = true;
+ assert.equal(h.render(), null);
+ assert.equal(h.jobs.size, 0, "posting the request is not queued behind the reader");
+ assert.equal(h.workers.length, 1);
+ assert.deepEqual(h.calls, [""], "nothing is tokenized here; only the grammar is asked for");
+ h.workers[0].callback({ source: "old source", language: "javascript" });
+ assert.equal(h.jobs.size, 1, "the answer is applied in a queued turn");
+ assert.equal(h.flushOne()?.source, "old source");
+ h.unmount();
+});
+
test("worker results wait for a queued commit and stale source or language callbacks are discarded", () => {
const h = harness();
h.options.offThread = true;
h.options.long = true;
h.render();
- h.flushOne();
const first = h.workers[0];
first.callback({ source: "old source", language: "javascript" });
assert.equal(h.render(), null);
assert.equal(h.jobs.size, 1);
h.render({ language: "python" });
assert.equal(first.cancelled, true);
+ assert.equal(h.jobs.size, 0, "the stale publication is cancelled and the new request is already out");
first.callback({ source: "old source", language: "javascript" });
- assert.equal(h.jobs.size, 1);
- h.flushOne();
+ assert.equal(h.jobs.size, 0);
h.workers[1].callback({ source: "old source", language: "python" });
assert.equal(h.flushOne()?.language, "python");
h.unmount();
@@ -204,7 +217,6 @@ test("disable and unmount cancel queued starts and delayed publications", () =>
assert.equal(h.jobs.size, 0);
h.options.offThread = true;
h.render({ enabled: true });
- h.flushOne();
const worker = h.workers[0];
worker.callback({ source: "old source", language: "javascript" });
assert.equal(h.jobs.size, 1);
@@ -221,7 +233,6 @@ test("failed short worker requests queue fallback but long requests never tokeni
h.options.offThread = true;
h.options.long = long;
h.render();
- h.flushOne();
const before = h.calls.length;
h.workers[0].callback(null);
assert.equal(h.calls.length, before);
@@ -238,7 +249,6 @@ test("print fallback honors quiet work normally and completes synchronously when
h.options.offThread = true;
h.options.long = true;
h.render();
- h.flushOne();
h.options.force = force;
const callback = h.printing.values().next().value;
assert.ok(callback);
@@ -256,13 +266,14 @@ test("print fallback honors quiet work normally and completes synchronously when
}
});
-test("forcing pending startup makes its print fallback available before the snapshot", () => {
+test("an off-thread fence has its print fallback before any queued turn runs", () => {
const h = harness();
h.options.offThread = true;
h.options.long = true;
h.render();
+ assert.equal(h.jobs.size, 0);
+ assert.equal(h.printing.size, 1);
h.options.force = true;
- h.flushOne();
for (const fallback of h.printing) fallback();
assert.equal(h.render()?.source, "old source");
assert.equal(h.jobs.size, 0);
diff --git a/studio/frontend/tests/fence-worker-token-parity.test.ts b/studio/frontend/tests/fence-worker-token-parity.test.ts
new file mode 100644
index 00000000000..3170ab8738e
--- /dev/null
+++ b/studio/frontend/tests/fence-worker-token-parity.test.ts
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import type { HighlightOptions, HighlightResult, ThemeInput } from "@streamdown/code";
+
+import { createCodePlugin } from "../src/components/assistant-ui/code-plugin.ts";
+import { unslothDarkTheme, unslothLightTheme } from "../src/components/assistant-ui/code-themes.ts";
+import {
+ reasoningHighlightReply,
+ transferableResult,
+} from "../src/components/assistant-ui/reasoning-highlight.ts";
+
+// The chat renderer's themes (markdown-text.tsx) and the worker's (reasoning-highlight.worker.ts).
+const THEMES: [ThemeInput, ThemeInput] = [unslothLightTheme, unslothDarkTheme];
+
+const FIXTURES: Record<string, string> = {
+ python: 'from dataclasses import dataclass\n\n@dataclass\nclass Row:\n weight: float = 0.5 # default\n\ndef score(rows: list[Row]) -> float:\n """Sum the weights."""\n return sum(r.weight for r in rows if r.weight > 0)\n',
+ typescript: "export interface Row { readonly weight: number }\nexport const score = (rows: readonly Row[]): number =>\n rows.reduce((t, r) => t + r.weight * 2, 0); // total\n",
+ tsx: "export function Badge({ label }: { label: string }) {\n return <span className=\"badge\">{label.toUpperCase()}</span>;\n}\n",
+ javascript: "const rows = [{ w: 1 }, { w: 2 }];\nconsole.log(rows.map((r) => r.w * 2).join(`, `));\n",
+ rust: "fn score(rows: &[Row]) -> f64 {\n rows.iter().map(|r| r.weight * 1.5).sum::<f64>() // step\n}\n",
+ go: "func Score(rows []Row) float64 {\n\ttotal := 0.0\n\tfor _, r := range rows { total += r.Weight }\n\treturn total\n}\n",
+ cpp: "#include <vector>\ndouble score(const std::vector<Row>& rows) {\n double t = 0; for (auto& r : rows) t += r.weight; return t;\n}\n",
+ c: "#include <stdio.h>\nint main(void) { printf(\"%d\\n\", 42); return 0; }\n",
+ java: "public final class Score {\n public static double of(List<Row> rows) { return rows.stream().mapToDouble(r -> r.weight).sum(); }\n}\n",
+ json: '{"id": "row-1", "weight": 0.5, "tags": ["a", "b"], "ok": true, "next": null}\n',
+ yaml: "steps:\n - name: build\n run: npm ci && npm run build # ci\n env: { CI: true }\n",
+ bash: 'for f in ./data/*.csv; do\n echo "file: $f"; wc -l "$f" | awk \'{print $1}\'\ndone\n',
+ sql: "SELECT id, SUM(weight) AS total FROM rows WHERE label <> 'skip' GROUP BY id ORDER BY total DESC;\n",
+ css: ".row:hover { color: hsl(210, 70%, 55%); margin: 4px 0; }\n@media (max-width: 600px) { .row { display: none } }\n",
+ html: '<!doctype html>\n<div class="row" data-step="1"><a href="/rows/1">open</a></div>\n<script>const x = 1 < 2;</script>\n',
+ markdown: "# Title\n\nSome *emphasis* and `code`, and a [link](https://example.com).\n",
+ diff: "--- a/x.py\n+++ b/x.py\n@@ -1 +1 @@\n-old = 1\n+new = 2\n",
+ text: "plain text stays plain\n",
+};
+
+const exact = (plugin: ReturnType<typeof createCodePlugin>, options: HighlightOptions): Promise<HighlightResult> =>
+ new Promise((resolve) => {
+ const now = plugin.highlightExact(options, resolve);
+ if (now) resolve(now);
+ });
+
+test("a fence coloured by the worker renders exactly the tokens the main thread would have", async () => {
+ const main = createCodePlugin({ themes: THEMES });
+ const worker = createCodePlugin({ themes: THEMES });
+ for (const [language, code] of Object.entries(FIXTURES)) {
+ const options = { code, language: language as never, themes: THEMES };
+ const local = await exact(main, options);
+ const reply = reasoningHighlightReply(
+ { client: 1, revision: 1, source: code, language, lines: [], full: true },
+ await exact(worker, options),
+ );
+ const received = structuredClone(reply).result as HighlightResult;
+ assert.deepEqual(received.tokens, local.tokens, `${language}: same tokens`);
+ for (const key of ["bg", "fg", "themeName", "rootStyle"] as const) {
+ assert.deepEqual(received[key], local[key], `${language}: same ${key}`);
+ }
+ assert.equal("grammarState" in received, false, `${language}: no grammar state crosses over`);
+ }
+});
+
+test("a worker result shares one style object per distinct style, and survives the clone that way", async () => {
+ const plugin = createCodePlugin({ themes: THEMES });
+ const code = Array.from({ length: 200 }, (_, i) => `def step_${i}(rows):\n return sum(r.w for r in rows) * ${i} # step`).join("\n");
+ const result = await exact(plugin, { code, language: "python" as never, themes: THEMES });
+ const compact = structuredClone(transferableResult(result));
+ const tokens = compact.tokens.flat();
+ const styles = new Set(tokens.map((token) => token.htmlStyle));
+ assert.ok(tokens.length > 1000);
+ assert.ok(styles.size < 20, `${styles.size} style objects for ${tokens.length} tokens`);
+ assert.deepEqual(compact.tokens, result.tokens);
+ assert.equal(result.grammarState !== undefined, true, "the cached result keeps its own grammar state");
+});
+
+test("a failed or null worker result still replies, and a partial request carries no result", () => {
+ const request = { client: 2, revision: 1, source: "x", language: "python", lines: [0], full: true };
+ assert.deepEqual(reasoningHighlightReply(request, null), { client: 2, revision: 1, lines: [], result: null });
+ assert.equal("result" in reasoningHighlightReply({ ...request, full: false }, null), false);
+});