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: keep a reasoning page start below its end (#10922)

Daniel Han3334e3ccd1a9
diff --git a/studio/frontend/src/components/assistant-ui/reasoning-pagination.ts b/studio/frontend/src/components/assistant-ui/reasoning-pagination.ts
index dbe0eeafed3..eb4972eed23 100644
--- a/studio/frontend/src/components/assistant-ui/reasoning-pagination.ts
+++ b/studio/frontend/src/components/assistant-ui/reasoning-pagination.ts
@@ -297,6 +297,17 @@ function pageStart(
break;
}
const candidate = found + separator.length;
+ // A START MUST STAY STRICTLY BELOW `end`, so that every page holds at
+ // least one character. `Earlier` sets the next page's end to this start
+ // (reasoning.tsx, showEarlierPage), so a start that reaches `end` yields
+ // an empty page whose own start is the same offset again: the chain stops
+ // advancing and every character before it becomes unreachable by paging.
+ // `indexOf` only moves forward, so once one candidate is too far they all
+ // are. Reachable when `maxCharacters <= BOUNDARY_SEARCH_CHARACTERS`,
+ // which lets `searchEnd` reach `end` and a separator land just under it.
+ if (candidate >= end) {
+ break;
+ }
if (!isInsideFence(candidate, fences)) {
return candidate;
}
@@ -304,7 +315,15 @@ function pageStart(
}
}
- return avoidBrokenSurrogate(markdown, target);
+ const start = avoidBrokenSurrogate(markdown, target);
+ if (start < end) {
+ return start;
+ }
+ // `maxCharacters` smaller than a surrogate pair. `avoidBrokenSurrogate` moved
+ // forward off a low half and landed on `end`; `target` is the high half, so
+ // stepping back one keeps the character whole rather than emitting an empty
+ // page or a page that opens on a lone low surrogate.
+ return Math.max(0, target - 1);
}
function selectReasoningPageWithFences(
diff --git a/studio/frontend/tests/reasoning-pagination.test.ts b/studio/frontend/tests/reasoning-pagination.test.ts
index 6d15fe7867c..6c3021f4e62 100644
--- a/studio/frontend/tests/reasoning-pagination.test.ts
+++ b/studio/frontend/tests/reasoning-pagination.test.ts
@@ -180,3 +180,72 @@ test("CRLF bytes and unicode survive complete pagination", () => {
assert.equal(rebuilt.includes("😀"), true);
assert.equal(rebuilt.includes("\uFFFD"), false);
});
+
+test("a small page budget still yields a page that holds characters", () => {
+ // Shrunk from a fuzz failure and kept verbatim: the trigger is a separator
+ // sitting immediately before `end`. `pageStart` searches FORWARD from its
+ // target and returns the offset just after the separator, which could reach
+ // `end` itself whenever the search window was allowed to extend that far --
+ // that is, whenever `maxCharacters <= BOUNDARY_SEARCH_CHARACTERS` (1,024).
+ //
+ // The invariant, as a sentence: a page start must stay STRICTLY BELOW the
+ // page end, because `Earlier` sets the next page's end to this start. A start
+ // equal to `end` yields an empty page whose own start is that same offset, so
+ // the chain stops advancing and everything before it is unreachable by paging.
+ const markdown = `${"x".repeat(4_000)}\n${"y".repeat(4_000)}\n`;
+
+ for (const maxCharacters of [64, 256, 1_023, 1_024, 1_025, 2_048]) {
+ const page = selectReasoningPage(markdown, {
+ end: markdown.length,
+ maxCharacters,
+ });
+ assert.ok(
+ page.start < page.end,
+ `start ${page.start} must stay below end ${page.end} at maxCharacters ${maxCharacters}`,
+ );
+ assert.equal(page.markdown.length, page.end - page.start);
+ assert.ok(page.markdown.length > 0);
+ }
+});
+
+test("the Earlier chain rebuilds the whole trace at every page budget", () => {
+ // The property the feature rests on: paging backwards loses nothing. Run at
+ // budgets on both sides of BOUNDARY_SEARCH_CHARACTERS, because the bug above
+ // was invisible at the shipped 8,192 default and only appeared below it.
+ const markdown = `${longReasoning}\n\`\`\`py\n${"c = 1\n".repeat(400)}\`\`\`\n${longReasoning}`;
+
+ for (const maxCharacters of [128, 1_024, 4_096, REASONING_PAGE_CHARACTERS]) {
+ const pages: string[] = [];
+ let end: number | null = null;
+ for (let step = 0; step < 5_000; step += 1) {
+ const page = selectReasoningPage(markdown, { end, maxCharacters });
+ assert.ok(page.start < page.end || page.end === 0);
+ pages.push(page.markdown);
+ if (!page.hasEarlier || page.start === 0) {
+ break;
+ }
+ end = page.start;
+ }
+ assert.equal(pages.reverse().join(""), markdown);
+ }
+});
+
+test("a page budget shorter than a surrogate pair keeps the character whole", () => {
+ const markdown = `${"a".repeat(200)}\n\u{1F600}`;
+ const page = selectReasoningPage(markdown, {
+ end: markdown.length,
+ maxCharacters: 1,
+ });
+
+ assert.ok(page.markdown.length > 0);
+ // Never a lone half: the page carries the whole astral character or none of
+ // it. Iterating a string yields code POINTS, so a surviving lone surrogate
+ // shows up here as a single unit still inside the surrogate range.
+ for (const codePoint of page.markdown) {
+ const value = codePoint.codePointAt(0) ?? 0;
+ assert.ok(
+ value < 0xd800 || value > 0xdfff,
+ `page opened on a lone surrogate (U+${value.toString(16)})`,
+ );
+ }
+});