@devries-cameron20 / t3code Mirror · read-only / commits / e0379fbf5c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 23:30 UTC

fix(web): collapsed reasoning previews no longer process the whole block (#23001)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 11, 2026 at 04:48 UTC e0379fbf5c48737210a98fd5e542eaf1c5246089
4 files changed +75 -16
apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
+40 -1
@@ -43,7 +43,8 @@ import {
43 type PendingApproval,
44 deriveTimelineEntriesFromVisibleTurnItems,
45 } 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";
48 import { parseMarkdownWithMath } from "@t3tools/shared/markdownMath";
49 import { collectThreadFindRanges } from "./threadFindHighlights";
50 import { toastManager } from "../ui/toast";
@@ -1722,6 +1723,44 @@ describe("compact reasoning math", () => {
1723 expected,
1724 );
1725 });
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 + });
1764 });
1765
1766 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
+24 -12
@@ -192,44 +192,56 @@ export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconNa
192 return workToneIcon(workEntry.tone).iconName;
193 }
194
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 +
201 /** Compact reasoning keeps formulas while flattening other Markdown into label text. */
202 export function remarkThoughtPreview(fallback: string) {
203 return (tree: Root) => {
204 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[] = [];
207 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);
216 };
217 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;
219 if (node.type === "math" || node.type === "inlineMath") {
220 flush();
221 + const value = node.value.slice(0, remaining);
222 + remaining -= value.length;
223 children.push({
224 type: "inlineMath",
214 - value: node.value,
225 + value,
226 position: node.position,
227 data: {
228 hName: "code",
229 hProperties: { className: ["language-math", "math-inline"] },
219 - hChildren: [{ type: "text", value: node.value }],
230 + hChildren: [{ type: "text", value }],
231 },
232 });
233 } else if ("alt" in node) append(node.alt ?? "");
234 else if ("value" in node) append(node.value);
235 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;
238 if (
239 index &&
240 ["root", "blockquote", "list", "listItem", "table", "tableRow"].includes(node.type)
241 )
242 append(" ");
243 visit(child);
232 - });
244 + }
245 } else if (node.type === "break") append(" ");
246 };
247 visit(tree);
apps/web/src/components/chat/TimelineWorkEntryRow.tsx
+5 -1
@@ -67,6 +67,7 @@ import {
67 workEntryIconName,
68 THINKING_PREVIEW_PLUGINS,
69 THOUGHT_PREVIEW_PLUGINS,
70 + THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
71 } from "./TimelineWorkEntryRow.logic";
72
73 export function useThreadReadTarget(entry: TimelineWorkEntry, environmentId: EnvironmentId) {
@@ -462,7 +463,10 @@ function WorkEntryLogRow(props: WorkEntryRowProps) {
463 {isReasoning && !expanded ? (
464 <ChatMarkdown
465 inline
465 - text={workEntry.detail ?? previewText}
466 + text={(workEntry.detail ?? previewText).slice(
467 + 0,
468 + THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
469 + )}
470 cwd={ctx.markdownCwd}
471 parseRawHtml={false}
472 extraRemarkPlugins={
apps/web/src/components/chat/TimelineWorkGroupRows.tsx
+6 -2
@@ -50,6 +50,7 @@ import {
50 import {
51 THINKING_PREVIEW_PLUGINS,
52 THOUGHT_PREVIEW_PLUGINS,
53 + THOUGHT_PREVIEW_SOURCE_MAX_LENGTH,
54 toolGroupSummaryIconName,
55 workEntryIconName,
56 } from "./TimelineWorkEntryRow.logic";
@@ -518,7 +519,10 @@ export function LiveWorkEntryTimelineRow({
519 if (pendingApproval) return <TimelinePendingApprovalRow approval={pendingApproval} />;
520
521 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 + : "";
526 const thoughtIsStatus = thoughtLine !== "" && row.entry === row.thought;
527
528 return (
@@ -573,7 +577,7 @@ export function LiveWorkEntryTimelineRow({
577 ) : row.entry.itemType === "reasoning" ? (
578 <ChatMarkdown
579 inline
576 - text={row.entry.detail ?? label}
580 + text={(row.entry.detail ?? label).slice(0, THOUGHT_PREVIEW_SOURCE_MAX_LENGTH)}
581 cwd={ctx.markdownCwd}
582 parseRawHtml={false}
583 extraRemarkPlugins={row.active ? THINKING_PREVIEW_PLUGINS : THOUGHT_PREVIEW_PLUGINS}