Skip to content

Commit e0379fb

Browse files
fix(web): collapsed reasoning previews no longer process the whole block (#23001)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent cbc68c9 commit e0379fb

4 files changed

Lines changed: 75 additions & 16 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ import {
4343
type PendingApproval,
4444
deriveTimelineEntriesFromVisibleTurnItems,
4545
} from "../../session-logic";
46-
import { remarkThoughtPreview } from "./TimelineWorkEntryRow.logic";
46+
import type { Root } from "mdast";
47+
import { remarkThoughtPreview, THOUGHT_PREVIEW_MAX_LENGTH } from "./TimelineWorkEntryRow.logic";
4748
import { parseMarkdownWithMath } from "@t3tools/shared/markdownMath";
4849
import { collectThreadFindRanges } from "./threadFindHighlights";
4950
import { toastManager } from "../ui/toast";
@@ -1722,6 +1723,44 @@ describe("compact reasoning math", () => {
17221723
expected,
17231724
);
17241725
});
1726+
1727+
it("stops reading the tree once the preview is full", () => {
1728+
const beyondCap = (): string => {
1729+
throw new Error("read beyond cap");
1730+
};
1731+
const head = "x".repeat(THOUGHT_PREVIEW_MAX_LENGTH - 2);
1732+
const tree: Root = {
1733+
type: "root",
1734+
children: [
1735+
{
1736+
type: "paragraph",
1737+
children: [
1738+
{ type: "text", value: head },
1739+
{ type: "strong", children: [{ type: "text", value: "yz beyond" }] },
1740+
{
1741+
type: "inlineMath",
1742+
get value() {
1743+
return beyondCap();
1744+
},
1745+
},
1746+
],
1747+
},
1748+
{
1749+
type: "paragraph",
1750+
children: [
1751+
{
1752+
type: "text",
1753+
get value() {
1754+
return beyondCap();
1755+
},
1756+
},
1757+
],
1758+
},
1759+
],
1760+
};
1761+
remarkThoughtPreview("Reasoning")(tree);
1762+
expect(tree.children).toEqual([{ type: "text", value: `${head}yz` }]);
1763+
});
17251764
});
17261765

17271766
it("renders automatic compaction as one live divider that finishes with its reason and token delta", async () => {

‎apps/web/src/components/chat/TimelineWorkEntryRow.logic.ts‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,44 +192,56 @@ export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconNa
192192
return workToneIcon(workEntry.tone).iconName;
193193
}
194194

195+
// Look past the visible label so long destinations and inline delimiters can close.
196+
// This is bounded lookahead, not document-wide Markdown parity: a closing delimiter
197+
// or reference definition beyond this prefix remains unresolved by the parser.
198+
export const THOUGHT_PREVIEW_SOURCE_MAX_LENGTH = 8_192;
199+
export const THOUGHT_PREVIEW_MAX_LENGTH = 2_048;
200+
195201
/** Compact reasoning keeps formulas while flattening other Markdown into label text. */
196202
export function remarkThoughtPreview(fallback: string) {
197203
return (tree: Root) => {
198204
const children: RootContent[] = [];
199-
let run: string[] = [];
200-
const append = (value: string) => {
201-
run.push(value);
202-
};
205+
let remaining = THOUGHT_PREVIEW_MAX_LENGTH;
206+
let fragments: string[] = [];
203207
const flush = () => {
204-
if (run.length === 0) return;
205-
children.push({ type: "text", value: run.join("").replace(/\s+/g, " ") });
206-
run = [];
208+
if (fragments.length === 0) return;
209+
children.push({ type: "text", value: fragments.join("").replace(/\s+/g, " ") });
210+
fragments = [];
211+
};
212+
const append = (value: string) => {
213+
const text = value.slice(0, remaining);
214+
remaining -= text.length;
215+
fragments.push(text);
207216
};
208217
const visit = (node: Root | RootContent) => {
209-
if (node.type === "html" || node.type === "definition") return;
218+
if (remaining === 0 || node.type === "html" || node.type === "definition") return;
210219
if (node.type === "math" || node.type === "inlineMath") {
211220
flush();
221+
const value = node.value.slice(0, remaining);
222+
remaining -= value.length;
212223
children.push({
213224
type: "inlineMath",
214-
value: node.value,
225+
value,
215226
position: node.position,
216227
data: {
217228
hName: "code",
218229
hProperties: { className: ["language-math", "math-inline"] },
219-
hChildren: [{ type: "text", value: node.value }],
230+
hChildren: [{ type: "text", value }],
220231
},
221232
});
222233
} else if ("alt" in node) append(node.alt ?? "");
223234
else if ("value" in node) append(node.value);
224235
else if ("children" in node) {
225-
node.children.forEach((child, index) => {
236+
for (const [index, child] of node.children.entries()) {
237+
if (remaining === 0) break;
226238
if (
227239
index &&
228240
["root", "blockquote", "list", "listItem", "table", "tableRow"].includes(node.type)
229241
)
230242
append(" ");
231243
visit(child);
232-
});
244+
}
233245
} else if (node.type === "break") append(" ");
234246
};
235247
visit(tree);

‎apps/web/src/components/chat/TimelineWorkEntryRow.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
workEntryIconName,
6868
THINKING_PREVIEW_PLUGINS,
6969
THOUGHT_PREVIEW_PLUGINS,
70+
THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
7071
} from "./TimelineWorkEntryRow.logic";
7172

7273
export function useThreadReadTarget(entry: TimelineWorkEntry, environmentId: EnvironmentId) {
@@ -462,7 +463,10 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {
462463
{isReasoning && !expanded ? (
463464
<ChatMarkdown
464465
inline
465-
text={workEntry.detail ?? previewText}
466+
text={(workEntry.detail ?? previewText).slice(
467+
0,
468+
THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
469+
)}
466470
cwd={ctx.markdownCwd}
467471
parseRawHtml={false}
468472
extraRemarkPlugins={

‎apps/web/src/components/chat/TimelineWorkGroupRows.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
import {
5151
THINKING_PREVIEW_PLUGINS,
5252
THOUGHT_PREVIEW_PLUGINS,
53+
THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
5354
toolGroupSummaryIconName,
5455
workEntryIconName,
5556
} from "./TimelineWorkEntryRow.logic";
@@ -518,7 +519,10 @@ export function LiveWorkEntryTimelineRow({
518519
if (pendingApproval) return <TimelinePendingApprovalRow approval={pendingApproval} />;
519520

520521
const failed = workEntryDisplayIndicatesToolFailure(row.entry);
521-
const thoughtLine = row.thought && !row.expanded ? liveThoughtLine(row.thought.detail ?? "") : "";
522+
const thoughtLine =
523+
row.thought && !row.expanded
524+
? liveThoughtLine((row.thought.detail ?? "").slice(0, THOUGHT_PREVIEW_SOURCE_MAX_LENGTH))
525+
: "";
522526
const thoughtIsStatus = thoughtLine !== "" && row.entry === row.thought;
523527

524528
return (
@@ -573,7 +577,7 @@ export function LiveWorkEntryTimelineRow({
573577
) : row.entry.itemType === "reasoning" ? (
574578
<ChatMarkdown
575579
inline
576-
text={row.entry.detail ?? label}
580+
text={(row.entry.detail ?? label).slice(0, THOUGHT_PREVIEW_SOURCE_MAX_LENGTH)}
577581
cwd={ctx.markdownCwd}
578582
parseRawHtml={false}
579583
extraRemarkPlugins={row.active ? THINKING_PREVIEW_PLUGINS : THOUGHT_PREVIEW_PLUGINS}

0 commit comments

Comments
 (0)