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: bound fence tokenization by line length, not by the wall clock (#9787)

Daniel Hancb23ffa1fac3
diff --git a/studio/frontend/src/components/assistant-ui/code-plugin.ts b/studio/frontend/src/components/assistant-ui/code-plugin.ts
index 4193a884a5a..156106fe6bb 100644
--- a/studio/frontend/src/components/assistant-ui/code-plugin.ts
+++ b/studio/frontend/src/components/assistant-ui/code-plugin.ts
@@ -73,6 +73,21 @@ const normalizeLanguage = (language: string): BundledLanguage => {
// text, keeping the 250 ms plain-tail cadence.
export const MIN_INCREMENTAL_CHARS = 2000;
const REFRESH_MS = 250;
+// Shiki's own tokenizer guard is a wall clock: a line that takes longer than
+// `tokenizeTimeLimit` (default 500 ms) is abandoned part-way and the rest of it
+// is emitted as one uncoloured token. Dual themes are two passes with two
+// separate budgets, and only the first pays to compile the grammar's regexes,
+// so a loaded machine drops the light theme to plain and keeps the dark one
+// correct -- for the same line, in the same result. Committed lines are never
+// re-tokenized, so that half-plain line is then cached for the life of the
+// fence. Bound the work by line length instead: same protection against a
+// minified blob (~120 ms at this length, growing quadratically past it, which
+// is VS Code's `editor.maxTokenizationLineLength` default for the same reason),
+// but the output depends on the source rather than on the host.
+export const TOKENIZE_LIMITS = {
+ tokenizeTimeLimit: 0,
+ tokenizeMaxLineLength: 20_000,
+} as const;
// An unvirtualized thread mounts every fence. Bound both their count and source
// size; token data measured roughly 30 bytes per source character.
const MAX_FENCES = 512;
@@ -328,6 +343,7 @@ export function createCodePlugin(
lang,
themes,
grammarState: fence.state,
+ ...TOKENIZE_LIMITS,
});
// Commit every newly completed line.
diff --git a/studio/frontend/tests/code-plugin-embedded-grammars.test.ts b/studio/frontend/tests/code-plugin-embedded-grammars.test.ts
index eefb63fd756..02ce92bad84 100644
--- a/studio/frontend/tests/code-plugin-embedded-grammars.test.ts
+++ b/studio/frontend/tests/code-plugin-embedded-grammars.test.ts
@@ -32,6 +32,7 @@ import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
import {
createCodePlugin,
MIN_INCREMENTAL_CHARS,
+ TOKENIZE_LIMITS,
} from "../src/components/assistant-ui/code-plugin.ts";
const THEMES: [ThemeInput, ThemeInput] = ["github-light", "github-dark"];
@@ -74,6 +75,10 @@ async function reference(code: string, language: HighlightOptions["language"]) {
return highlighter.codeToTokens(code, {
lang: language,
themes: { light: "github-light", dark: "github-dark" },
+ // The reference must run under the plugin's own tokenizer limits, or shiki's
+ // default wall-clock bail can degrade whichever side of the comparison is
+ // unlucky on a slow runner.
+ ...TOKENIZE_LIMITS,
});
}
diff --git a/studio/frontend/tests/code-plugin-engine-and-cache.test.ts b/studio/frontend/tests/code-plugin-engine-and-cache.test.ts
index 64dc91a94e1..774e82c5041 100644
--- a/studio/frontend/tests/code-plugin-engine-and-cache.test.ts
+++ b/studio/frontend/tests/code-plugin-engine-and-cache.test.ts
@@ -32,7 +32,10 @@ import type {
import { createHighlighter } from "shiki";
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
-import { createCodePlugin } from "../src/components/assistant-ui/code-plugin.ts";
+import {
+ createCodePlugin,
+ TOKENIZE_LIMITS,
+} from "../src/components/assistant-ui/code-plugin.ts";
const THEMES: [ThemeInput, ThemeInput] = ["github-light", "github-dark"];
@@ -201,6 +204,8 @@ test("a fence evicted mid-stream still tokenizes correctly when it resumes", asy
highlighter.codeToTokens(code, {
lang: "html",
themes: { light: "github-light", dark: "github-dark" },
+ // Same tokenizer limits as the plugin, so neither side can degrade.
+ ...TOKENIZE_LIMITS,
}).tokens;
const settle = () => new Promise((r) => setTimeout(r, 260));
diff --git a/studio/frontend/tests/code-plugin-incremental.test.ts b/studio/frontend/tests/code-plugin-incremental.test.ts
index 1daa3b374fb..998ea4e9038 100644
--- a/studio/frontend/tests/code-plugin-incremental.test.ts
+++ b/studio/frontend/tests/code-plugin-incremental.test.ts
@@ -11,7 +11,10 @@ import type {
import { createHighlighter } from "shiki";
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
-import { createCodePlugin } from "../src/components/assistant-ui/code-plugin.ts";
+import {
+ createCodePlugin,
+ TOKENIZE_LIMITS,
+} from "../src/components/assistant-ui/code-plugin.ts";
const THEMES: [ThemeInput, ThemeInput] = ["github-light", "github-dark"];
@@ -98,6 +101,10 @@ async function reference(code: string, language: HighlightOptions["language"]) {
return highlighter.codeToTokens(code, {
lang: language,
themes: { light: "github-light", dark: "github-dark" },
+ // The reference must run under the plugin's own tokenizer limits, or shiki's
+ // default wall-clock bail can degrade whichever side of the comparison is
+ // unlucky on a slow runner.
+ ...TOKENIZE_LIMITS,
});
}
diff --git a/studio/frontend/tests/code-plugin-tokenization-work.test.ts b/studio/frontend/tests/code-plugin-tokenization-work.test.ts
index 58be51b6854..3a39dc66813 100644
--- a/studio/frontend/tests/code-plugin-tokenization-work.test.ts
+++ b/studio/frontend/tests/code-plugin-tokenization-work.test.ts
@@ -42,9 +42,8 @@ import { createHighlighter } from "shiki";
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
register("./shiki-tokenization-resolver.mjs", import.meta.url);
-const { createCodePlugin, MIN_INCREMENTAL_CHARS } = await import(
- "../src/components/assistant-ui/code-plugin.ts"
-);
+const { createCodePlugin, MIN_INCREMENTAL_CHARS, TOKENIZE_LIMITS } =
+ await import("../src/components/assistant-ui/code-plugin.ts");
const { tokenized } = await import("./shiki-tokenization-counter.mts");
const THEMES: [ThemeInput, ThemeInput] = ["github-light", "github-dark"];
@@ -135,6 +134,61 @@ test("a streaming fence is tokenized once, not once per update", async () => {
reference.codeToTokens(SOURCE, {
lang: "typescript",
themes: { light: "github-light", dark: "github-dark" },
+ ...TOKENIZE_LIMITS,
+ }).tokens,
+ );
+});
+
+/*
+ * WHAT MADE THE ASSERTION ABOVE FAIL ON WINDOWS ONE RUN IN TWENTY.
+ *
+ * Shiki abandons a line once `tokenizeTimeLimit` of wall clock has gone by and emits the rest of it
+ * as one uncoloured token. Dual themes are two passes with two budgets and only the first compiles
+ * the grammar's regexes, so a slow enough host returns the light theme plain and the dark theme
+ * correct -- and the plugin then commits that line and never tokenizes it again. Neither side of the
+ * comparison was safe: CI produced diffs with the plugin degraded, diffs with the reference
+ * degraded, and diffs with both on different lines.
+ *
+ * Racing `Date.now` reproduces an overrun on any machine, without waiting for one: every elapsed
+ * check clears any finite limit, and `tokenizeTimeLimit: 0` skips the check entirely. Which tokens
+ * a real overrun loses depends on where in the line it lands, so this pins the invariant rather
+ * than one signature -- the wall clock must not reach the output at all. The plugin's own throttle
+ * reads `performance.now`, so it is unaffected.
+ */
+test("tokenization does not degrade when the tokenizer overruns the wall clock", async () => {
+ const plugin = createCodePlugin({ themes: THEMES });
+ const prefix = SOURCE.slice(0, MIN_INCREMENTAL_CHARS + 500);
+
+ await highlightOnce(plugin, prefix);
+ // Leave the throttle window so the grown fence takes the tokenizing path.
+ await settle();
+
+ const realNow = Date.now;
+ let result: HighlightResult;
+ try {
+ let elapsed = 0;
+ Date.now = () => realNow() + (elapsed += 60_000);
+ result = plugin.highlight({
+ code: SOURCE,
+ language: LANGUAGE,
+ themes: THEMES,
+ }) as HighlightResult;
+ } finally {
+ Date.now = realNow;
+ }
+
+ assert.ok(result, "the grown fence should tokenize synchronously here");
+ const reference = await createHighlighter({
+ themes: THEMES,
+ langs: ["typescript"],
+ engine: createJavaScriptRegexEngine({ forgiving: true }),
+ });
+ assert.deepEqual(
+ result.tokens,
+ reference.codeToTokens(SOURCE, {
+ lang: "typescript",
+ themes: { light: "github-light", dark: "github-dark" },
+ ...TOKENIZE_LIMITS,
}).tokens,
);
});